/cp
[official-gcc.git] / gcc / cp / parser.c
blob4e06e8af0b66500348a7e88f96b9fe135a1343c6
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 compound_literal_p;
5822 cp_parser_parse_tentatively (parser);
5823 /* Consume the `('. */
5824 cp_lexer_consume_token (parser->lexer);
5826 /* Avoid calling cp_parser_type_id pointlessly, see comment
5827 in cp_parser_cast_expression about c++/29234. */
5828 cp_lexer_save_tokens (parser->lexer);
5830 compound_literal_p
5831 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5832 /*consume_paren=*/true)
5833 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5835 /* Roll back the tokens we skipped. */
5836 cp_lexer_rollback_tokens (parser->lexer);
5838 if (!compound_literal_p)
5839 cp_parser_simulate_error (parser);
5840 else
5842 /* Parse the type. */
5843 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5844 parser->in_type_id_in_expr_p = true;
5845 type = cp_parser_type_id (parser);
5846 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5847 /* Look for the `)'. */
5848 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5851 /* If things aren't going well, there's no need to
5852 keep going. */
5853 if (!cp_parser_error_occurred (parser))
5855 bool non_constant_p;
5856 /* Parse the brace-enclosed initializer list. */
5857 initializer = cp_parser_braced_list (parser,
5858 &non_constant_p);
5860 /* If that worked, we're definitely looking at a
5861 compound-literal expression. */
5862 if (cp_parser_parse_definitely (parser))
5864 /* Warn the user that a compound literal is not
5865 allowed in standard C++. */
5866 pedwarn (input_location, OPT_Wpedantic,
5867 "ISO C++ forbids compound-literals");
5868 /* For simplicity, we disallow compound literals in
5869 constant-expressions. We could
5870 allow compound literals of integer type, whose
5871 initializer was a constant, in constant
5872 expressions. Permitting that usage, as a further
5873 extension, would not change the meaning of any
5874 currently accepted programs. (Of course, as
5875 compound literals are not part of ISO C++, the
5876 standard has nothing to say.) */
5877 if (cp_parser_non_integral_constant_expression (parser,
5878 NIC_NCC))
5880 postfix_expression = error_mark_node;
5881 break;
5883 /* Form the representation of the compound-literal. */
5884 postfix_expression
5885 = finish_compound_literal (type, initializer,
5886 tf_warning_or_error);
5887 break;
5891 /* It must be a primary-expression. */
5892 postfix_expression
5893 = cp_parser_primary_expression (parser, address_p, cast_p,
5894 /*template_arg_p=*/false,
5895 decltype_p,
5896 &idk);
5898 break;
5901 /* Note that we don't need to worry about calling build_cplus_new on a
5902 class-valued CALL_EXPR in decltype when it isn't the end of the
5903 postfix-expression; unary_complex_lvalue will take care of that for
5904 all these cases. */
5906 /* Keep looping until the postfix-expression is complete. */
5907 while (true)
5909 if (idk == CP_ID_KIND_UNQUALIFIED
5910 && identifier_p (postfix_expression)
5911 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5912 /* It is not a Koenig lookup function call. */
5913 postfix_expression
5914 = unqualified_name_lookup_error (postfix_expression);
5916 /* Peek at the next token. */
5917 token = cp_lexer_peek_token (parser->lexer);
5919 switch (token->type)
5921 case CPP_OPEN_SQUARE:
5922 if (cp_next_tokens_can_be_std_attribute_p (parser))
5924 cp_parser_error (parser,
5925 "two consecutive %<[%> shall "
5926 "only introduce an attribute");
5927 return error_mark_node;
5929 postfix_expression
5930 = cp_parser_postfix_open_square_expression (parser,
5931 postfix_expression,
5932 false,
5933 decltype_p);
5934 idk = CP_ID_KIND_NONE;
5935 is_member_access = false;
5936 break;
5938 case CPP_OPEN_PAREN:
5939 /* postfix-expression ( expression-list [opt] ) */
5941 bool koenig_p;
5942 bool is_builtin_constant_p;
5943 bool saved_integral_constant_expression_p = false;
5944 bool saved_non_integral_constant_expression_p = false;
5945 tsubst_flags_t complain = complain_flags (decltype_p);
5946 vec<tree, va_gc> *args;
5948 is_member_access = false;
5950 is_builtin_constant_p
5951 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5952 if (is_builtin_constant_p)
5954 /* The whole point of __builtin_constant_p is to allow
5955 non-constant expressions to appear as arguments. */
5956 saved_integral_constant_expression_p
5957 = parser->integral_constant_expression_p;
5958 saved_non_integral_constant_expression_p
5959 = parser->non_integral_constant_expression_p;
5960 parser->integral_constant_expression_p = false;
5962 args = (cp_parser_parenthesized_expression_list
5963 (parser, non_attr,
5964 /*cast_p=*/false, /*allow_expansion_p=*/true,
5965 /*non_constant_p=*/NULL));
5966 if (is_builtin_constant_p)
5968 parser->integral_constant_expression_p
5969 = saved_integral_constant_expression_p;
5970 parser->non_integral_constant_expression_p
5971 = saved_non_integral_constant_expression_p;
5974 if (args == NULL)
5976 postfix_expression = error_mark_node;
5977 break;
5980 /* Function calls are not permitted in
5981 constant-expressions. */
5982 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5983 && cp_parser_non_integral_constant_expression (parser,
5984 NIC_FUNC_CALL))
5986 postfix_expression = error_mark_node;
5987 release_tree_vector (args);
5988 break;
5991 koenig_p = false;
5992 if (idk == CP_ID_KIND_UNQUALIFIED
5993 || idk == CP_ID_KIND_TEMPLATE_ID)
5995 if (identifier_p (postfix_expression))
5997 if (!args->is_empty ())
5999 koenig_p = true;
6000 if (!any_type_dependent_arguments_p (args))
6001 postfix_expression
6002 = perform_koenig_lookup (postfix_expression, args,
6003 /*include_std=*/false,
6004 complain);
6006 else
6007 postfix_expression
6008 = unqualified_fn_lookup_error (postfix_expression);
6010 /* We do not perform argument-dependent lookup if
6011 normal lookup finds a non-function, in accordance
6012 with the expected resolution of DR 218. */
6013 else if (!args->is_empty ()
6014 && is_overloaded_fn (postfix_expression))
6016 tree fn = get_first_fn (postfix_expression);
6017 fn = STRIP_TEMPLATE (fn);
6019 /* Do not do argument dependent lookup if regular
6020 lookup finds a member function or a block-scope
6021 function declaration. [basic.lookup.argdep]/3 */
6022 if (!DECL_FUNCTION_MEMBER_P (fn)
6023 && !DECL_LOCAL_FUNCTION_P (fn))
6025 koenig_p = true;
6026 if (!any_type_dependent_arguments_p (args))
6027 postfix_expression
6028 = perform_koenig_lookup (postfix_expression, args,
6029 /*include_std=*/false,
6030 complain);
6035 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6037 tree instance = TREE_OPERAND (postfix_expression, 0);
6038 tree fn = TREE_OPERAND (postfix_expression, 1);
6040 if (processing_template_decl
6041 && (type_dependent_expression_p (instance)
6042 || (!BASELINK_P (fn)
6043 && TREE_CODE (fn) != FIELD_DECL)
6044 || type_dependent_expression_p (fn)
6045 || any_type_dependent_arguments_p (args)))
6047 postfix_expression
6048 = build_nt_call_vec (postfix_expression, args);
6049 release_tree_vector (args);
6050 break;
6053 if (BASELINK_P (fn))
6055 postfix_expression
6056 = (build_new_method_call
6057 (instance, fn, &args, NULL_TREE,
6058 (idk == CP_ID_KIND_QUALIFIED
6059 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6060 : LOOKUP_NORMAL),
6061 /*fn_p=*/NULL,
6062 complain));
6064 else
6065 postfix_expression
6066 = finish_call_expr (postfix_expression, &args,
6067 /*disallow_virtual=*/false,
6068 /*koenig_p=*/false,
6069 complain);
6071 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6072 || TREE_CODE (postfix_expression) == MEMBER_REF
6073 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6074 postfix_expression = (build_offset_ref_call_from_tree
6075 (postfix_expression, &args,
6076 complain));
6077 else if (idk == CP_ID_KIND_QUALIFIED)
6078 /* A call to a static class member, or a namespace-scope
6079 function. */
6080 postfix_expression
6081 = finish_call_expr (postfix_expression, &args,
6082 /*disallow_virtual=*/true,
6083 koenig_p,
6084 complain);
6085 else
6086 /* All other function calls. */
6087 postfix_expression
6088 = finish_call_expr (postfix_expression, &args,
6089 /*disallow_virtual=*/false,
6090 koenig_p,
6091 complain);
6093 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6094 idk = CP_ID_KIND_NONE;
6096 release_tree_vector (args);
6098 break;
6100 case CPP_DOT:
6101 case CPP_DEREF:
6102 /* postfix-expression . template [opt] id-expression
6103 postfix-expression . pseudo-destructor-name
6104 postfix-expression -> template [opt] id-expression
6105 postfix-expression -> pseudo-destructor-name */
6107 /* Consume the `.' or `->' operator. */
6108 cp_lexer_consume_token (parser->lexer);
6110 postfix_expression
6111 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6112 postfix_expression,
6113 false, &idk, loc);
6115 is_member_access = true;
6116 break;
6118 case CPP_PLUS_PLUS:
6119 /* postfix-expression ++ */
6120 /* Consume the `++' token. */
6121 cp_lexer_consume_token (parser->lexer);
6122 /* Generate a representation for the complete expression. */
6123 postfix_expression
6124 = finish_increment_expr (postfix_expression,
6125 POSTINCREMENT_EXPR);
6126 /* Increments may not appear in constant-expressions. */
6127 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6128 postfix_expression = error_mark_node;
6129 idk = CP_ID_KIND_NONE;
6130 is_member_access = false;
6131 break;
6133 case CPP_MINUS_MINUS:
6134 /* postfix-expression -- */
6135 /* Consume the `--' token. */
6136 cp_lexer_consume_token (parser->lexer);
6137 /* Generate a representation for the complete expression. */
6138 postfix_expression
6139 = finish_increment_expr (postfix_expression,
6140 POSTDECREMENT_EXPR);
6141 /* Decrements may not appear in constant-expressions. */
6142 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6143 postfix_expression = error_mark_node;
6144 idk = CP_ID_KIND_NONE;
6145 is_member_access = false;
6146 break;
6148 default:
6149 if (pidk_return != NULL)
6150 * pidk_return = idk;
6151 if (member_access_only_p)
6152 return is_member_access? postfix_expression : error_mark_node;
6153 else
6154 return postfix_expression;
6158 /* We should never get here. */
6159 gcc_unreachable ();
6160 return error_mark_node;
6163 /* This function parses Cilk Plus array notations. If a normal array expr. is
6164 parsed then the array index is passed back to the caller through *INIT_INDEX
6165 and the function returns a NULL_TREE. If array notation expr. is parsed,
6166 then *INIT_INDEX is ignored by the caller and the function returns
6167 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6168 error_mark_node. */
6170 static tree
6171 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6172 tree array_value)
6174 cp_token *token = NULL;
6175 tree length_index, stride = NULL_TREE, value_tree, array_type;
6176 if (!array_value || array_value == error_mark_node)
6178 cp_parser_skip_to_end_of_statement (parser);
6179 return error_mark_node;
6182 array_type = TREE_TYPE (array_value);
6184 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6185 parser->colon_corrects_to_scope_p = false;
6186 token = cp_lexer_peek_token (parser->lexer);
6188 if (!token)
6190 cp_parser_error (parser, "expected %<:%> or numeral");
6191 return error_mark_node;
6193 else if (token->type == CPP_COLON)
6195 /* Consume the ':'. */
6196 cp_lexer_consume_token (parser->lexer);
6198 /* If we are here, then we have a case like this A[:]. */
6199 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6201 cp_parser_error (parser, "expected %<]%>");
6202 cp_parser_skip_to_end_of_statement (parser);
6203 return error_mark_node;
6205 *init_index = NULL_TREE;
6206 stride = NULL_TREE;
6207 length_index = NULL_TREE;
6209 else
6211 /* If we are here, then there are three valid possibilities:
6212 1. ARRAY [ EXP ]
6213 2. ARRAY [ EXP : EXP ]
6214 3. ARRAY [ EXP : EXP : EXP ] */
6216 *init_index = cp_parser_expression (parser, false, NULL);
6217 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6219 /* This indicates that we have a normal array expression. */
6220 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6221 return NULL_TREE;
6224 /* Consume the ':'. */
6225 cp_lexer_consume_token (parser->lexer);
6226 length_index = cp_parser_expression (parser, false, NULL);
6227 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6229 cp_lexer_consume_token (parser->lexer);
6230 stride = cp_parser_expression (parser, false, NULL);
6233 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6235 if (*init_index == error_mark_node || length_index == error_mark_node
6236 || stride == error_mark_node)
6238 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6239 cp_lexer_consume_token (parser->lexer);
6240 return error_mark_node;
6242 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6244 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6245 length_index, stride, array_type);
6246 return value_tree;
6249 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6250 by cp_parser_builtin_offsetof. We're looking for
6252 postfix-expression [ expression ]
6253 postfix-expression [ braced-init-list ] (C++11)
6255 FOR_OFFSETOF is set if we're being called in that context, which
6256 changes how we deal with integer constant expressions. */
6258 static tree
6259 cp_parser_postfix_open_square_expression (cp_parser *parser,
6260 tree postfix_expression,
6261 bool for_offsetof,
6262 bool decltype_p)
6264 tree index = NULL_TREE;
6265 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6266 bool saved_greater_than_is_operator_p;
6268 /* Consume the `[' token. */
6269 cp_lexer_consume_token (parser->lexer);
6271 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6272 parser->greater_than_is_operator_p = true;
6274 /* Parse the index expression. */
6275 /* ??? For offsetof, there is a question of what to allow here. If
6276 offsetof is not being used in an integral constant expression context,
6277 then we *could* get the right answer by computing the value at runtime.
6278 If we are in an integral constant expression context, then we might
6279 could accept any constant expression; hard to say without analysis.
6280 Rather than open the barn door too wide right away, allow only integer
6281 constant expressions here. */
6282 if (for_offsetof)
6283 index = cp_parser_constant_expression (parser, false, NULL);
6284 else
6286 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6288 bool expr_nonconst_p;
6289 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6290 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6291 if (flag_enable_cilkplus
6292 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6294 error_at (cp_lexer_peek_token (parser->lexer)->location,
6295 "braced list index is not allowed with array "
6296 "notation");
6297 cp_parser_skip_to_end_of_statement (parser);
6298 return error_mark_node;
6301 else if (flag_enable_cilkplus)
6303 /* Here are have these two options:
6304 ARRAY[EXP : EXP] - Array notation expr with default
6305 stride of 1.
6306 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6307 stride. */
6308 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6309 postfix_expression);
6310 if (an_exp)
6311 return an_exp;
6313 else
6314 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6317 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6319 /* Look for the closing `]'. */
6320 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6322 /* Build the ARRAY_REF. */
6323 postfix_expression = grok_array_decl (loc, postfix_expression,
6324 index, decltype_p);
6326 /* When not doing offsetof, array references are not permitted in
6327 constant-expressions. */
6328 if (!for_offsetof
6329 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6330 postfix_expression = error_mark_node;
6332 return postfix_expression;
6335 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6336 by cp_parser_builtin_offsetof. We're looking for
6338 postfix-expression . template [opt] id-expression
6339 postfix-expression . pseudo-destructor-name
6340 postfix-expression -> template [opt] id-expression
6341 postfix-expression -> pseudo-destructor-name
6343 FOR_OFFSETOF is set if we're being called in that context. That sorta
6344 limits what of the above we'll actually accept, but nevermind.
6345 TOKEN_TYPE is the "." or "->" token, which will already have been
6346 removed from the stream. */
6348 static tree
6349 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6350 enum cpp_ttype token_type,
6351 tree postfix_expression,
6352 bool for_offsetof, cp_id_kind *idk,
6353 location_t location)
6355 tree name;
6356 bool dependent_p;
6357 bool pseudo_destructor_p;
6358 tree scope = NULL_TREE;
6360 /* If this is a `->' operator, dereference the pointer. */
6361 if (token_type == CPP_DEREF)
6362 postfix_expression = build_x_arrow (location, postfix_expression,
6363 tf_warning_or_error);
6364 /* Check to see whether or not the expression is type-dependent. */
6365 dependent_p = type_dependent_expression_p (postfix_expression);
6366 /* The identifier following the `->' or `.' is not qualified. */
6367 parser->scope = NULL_TREE;
6368 parser->qualifying_scope = NULL_TREE;
6369 parser->object_scope = NULL_TREE;
6370 *idk = CP_ID_KIND_NONE;
6372 /* Enter the scope corresponding to the type of the object
6373 given by the POSTFIX_EXPRESSION. */
6374 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6376 scope = TREE_TYPE (postfix_expression);
6377 /* According to the standard, no expression should ever have
6378 reference type. Unfortunately, we do not currently match
6379 the standard in this respect in that our internal representation
6380 of an expression may have reference type even when the standard
6381 says it does not. Therefore, we have to manually obtain the
6382 underlying type here. */
6383 scope = non_reference (scope);
6384 /* The type of the POSTFIX_EXPRESSION must be complete. */
6385 if (scope == unknown_type_node)
6387 error_at (location, "%qE does not have class type",
6388 postfix_expression);
6389 scope = NULL_TREE;
6391 /* Unlike the object expression in other contexts, *this is not
6392 required to be of complete type for purposes of class member
6393 access (5.2.5) outside the member function body. */
6394 else if (postfix_expression != current_class_ref
6395 && !(processing_template_decl && scope == current_class_type))
6396 scope = complete_type_or_else (scope, NULL_TREE);
6397 /* Let the name lookup machinery know that we are processing a
6398 class member access expression. */
6399 parser->context->object_type = scope;
6400 /* If something went wrong, we want to be able to discern that case,
6401 as opposed to the case where there was no SCOPE due to the type
6402 of expression being dependent. */
6403 if (!scope)
6404 scope = error_mark_node;
6405 /* If the SCOPE was erroneous, make the various semantic analysis
6406 functions exit quickly -- and without issuing additional error
6407 messages. */
6408 if (scope == error_mark_node)
6409 postfix_expression = error_mark_node;
6412 /* Assume this expression is not a pseudo-destructor access. */
6413 pseudo_destructor_p = false;
6415 /* If the SCOPE is a scalar type, then, if this is a valid program,
6416 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6417 is type dependent, it can be pseudo-destructor-name or something else.
6418 Try to parse it as pseudo-destructor-name first. */
6419 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6421 tree s;
6422 tree type;
6424 cp_parser_parse_tentatively (parser);
6425 /* Parse the pseudo-destructor-name. */
6426 s = NULL_TREE;
6427 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6428 &s, &type);
6429 if (dependent_p
6430 && (cp_parser_error_occurred (parser)
6431 || !SCALAR_TYPE_P (type)))
6432 cp_parser_abort_tentative_parse (parser);
6433 else if (cp_parser_parse_definitely (parser))
6435 pseudo_destructor_p = true;
6436 postfix_expression
6437 = finish_pseudo_destructor_expr (postfix_expression,
6438 s, type, location);
6442 if (!pseudo_destructor_p)
6444 /* If the SCOPE is not a scalar type, we are looking at an
6445 ordinary class member access expression, rather than a
6446 pseudo-destructor-name. */
6447 bool template_p;
6448 cp_token *token = cp_lexer_peek_token (parser->lexer);
6449 /* Parse the id-expression. */
6450 name = (cp_parser_id_expression
6451 (parser,
6452 cp_parser_optional_template_keyword (parser),
6453 /*check_dependency_p=*/true,
6454 &template_p,
6455 /*declarator_p=*/false,
6456 /*optional_p=*/false));
6457 /* In general, build a SCOPE_REF if the member name is qualified.
6458 However, if the name was not dependent and has already been
6459 resolved; there is no need to build the SCOPE_REF. For example;
6461 struct X { void f(); };
6462 template <typename T> void f(T* t) { t->X::f(); }
6464 Even though "t" is dependent, "X::f" is not and has been resolved
6465 to a BASELINK; there is no need to include scope information. */
6467 /* But we do need to remember that there was an explicit scope for
6468 virtual function calls. */
6469 if (parser->scope)
6470 *idk = CP_ID_KIND_QUALIFIED;
6472 /* If the name is a template-id that names a type, we will get a
6473 TYPE_DECL here. That is invalid code. */
6474 if (TREE_CODE (name) == TYPE_DECL)
6476 error_at (token->location, "invalid use of %qD", name);
6477 postfix_expression = error_mark_node;
6479 else
6481 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6483 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6485 error_at (token->location, "%<%D::%D%> is not a class member",
6486 parser->scope, name);
6487 postfix_expression = error_mark_node;
6489 else
6490 name = build_qualified_name (/*type=*/NULL_TREE,
6491 parser->scope,
6492 name,
6493 template_p);
6494 parser->scope = NULL_TREE;
6495 parser->qualifying_scope = NULL_TREE;
6496 parser->object_scope = NULL_TREE;
6498 if (parser->scope && name && BASELINK_P (name))
6499 adjust_result_of_qualified_name_lookup
6500 (name, parser->scope, scope);
6501 postfix_expression
6502 = finish_class_member_access_expr (postfix_expression, name,
6503 template_p,
6504 tf_warning_or_error);
6508 /* We no longer need to look up names in the scope of the object on
6509 the left-hand side of the `.' or `->' operator. */
6510 parser->context->object_type = NULL_TREE;
6512 /* Outside of offsetof, these operators may not appear in
6513 constant-expressions. */
6514 if (!for_offsetof
6515 && (cp_parser_non_integral_constant_expression
6516 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6517 postfix_expression = error_mark_node;
6519 return postfix_expression;
6522 /* Parse a parenthesized expression-list.
6524 expression-list:
6525 assignment-expression
6526 expression-list, assignment-expression
6528 attribute-list:
6529 expression-list
6530 identifier
6531 identifier, expression-list
6533 CAST_P is true if this expression is the target of a cast.
6535 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6536 argument pack.
6538 Returns a vector of trees. Each element is a representation of an
6539 assignment-expression. NULL is returned if the ( and or ) are
6540 missing. An empty, but allocated, vector is returned on no
6541 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6542 if we are parsing an attribute list for an attribute that wants a
6543 plain identifier argument, normal_attr for an attribute that wants
6544 an expression, or non_attr if we aren't parsing an attribute list. If
6545 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6546 not all of the expressions in the list were constant. */
6548 static vec<tree, va_gc> *
6549 cp_parser_parenthesized_expression_list (cp_parser* parser,
6550 int is_attribute_list,
6551 bool cast_p,
6552 bool allow_expansion_p,
6553 bool *non_constant_p)
6555 vec<tree, va_gc> *expression_list;
6556 bool fold_expr_p = is_attribute_list != non_attr;
6557 tree identifier = NULL_TREE;
6558 bool saved_greater_than_is_operator_p;
6560 /* Assume all the expressions will be constant. */
6561 if (non_constant_p)
6562 *non_constant_p = false;
6564 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6565 return NULL;
6567 expression_list = make_tree_vector ();
6569 /* Within a parenthesized expression, a `>' token is always
6570 the greater-than operator. */
6571 saved_greater_than_is_operator_p
6572 = parser->greater_than_is_operator_p;
6573 parser->greater_than_is_operator_p = true;
6575 /* Consume expressions until there are no more. */
6576 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6577 while (true)
6579 tree expr;
6581 /* At the beginning of attribute lists, check to see if the
6582 next token is an identifier. */
6583 if (is_attribute_list == id_attr
6584 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6586 cp_token *token;
6588 /* Consume the identifier. */
6589 token = cp_lexer_consume_token (parser->lexer);
6590 /* Save the identifier. */
6591 identifier = token->u.value;
6593 else
6595 bool expr_non_constant_p;
6597 /* Parse the next assignment-expression. */
6598 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6600 /* A braced-init-list. */
6601 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6602 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6603 if (non_constant_p && expr_non_constant_p)
6604 *non_constant_p = true;
6606 else if (non_constant_p)
6608 expr = (cp_parser_constant_expression
6609 (parser, /*allow_non_constant_p=*/true,
6610 &expr_non_constant_p));
6611 if (expr_non_constant_p)
6612 *non_constant_p = true;
6614 else
6615 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6617 if (fold_expr_p)
6618 expr = fold_non_dependent_expr (expr);
6620 /* If we have an ellipsis, then this is an expression
6621 expansion. */
6622 if (allow_expansion_p
6623 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6625 /* Consume the `...'. */
6626 cp_lexer_consume_token (parser->lexer);
6628 /* Build the argument pack. */
6629 expr = make_pack_expansion (expr);
6632 /* Add it to the list. We add error_mark_node
6633 expressions to the list, so that we can still tell if
6634 the correct form for a parenthesized expression-list
6635 is found. That gives better errors. */
6636 vec_safe_push (expression_list, expr);
6638 if (expr == error_mark_node)
6639 goto skip_comma;
6642 /* After the first item, attribute lists look the same as
6643 expression lists. */
6644 is_attribute_list = non_attr;
6646 get_comma:;
6647 /* If the next token isn't a `,', then we are done. */
6648 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6649 break;
6651 /* Otherwise, consume the `,' and keep going. */
6652 cp_lexer_consume_token (parser->lexer);
6655 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6657 int ending;
6659 skip_comma:;
6660 /* We try and resync to an unnested comma, as that will give the
6661 user better diagnostics. */
6662 ending = cp_parser_skip_to_closing_parenthesis (parser,
6663 /*recovering=*/true,
6664 /*or_comma=*/true,
6665 /*consume_paren=*/true);
6666 if (ending < 0)
6667 goto get_comma;
6668 if (!ending)
6670 parser->greater_than_is_operator_p
6671 = saved_greater_than_is_operator_p;
6672 return NULL;
6676 parser->greater_than_is_operator_p
6677 = saved_greater_than_is_operator_p;
6679 if (identifier)
6680 vec_safe_insert (expression_list, 0, identifier);
6682 return expression_list;
6685 /* Parse a pseudo-destructor-name.
6687 pseudo-destructor-name:
6688 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6689 :: [opt] nested-name-specifier template template-id :: ~ type-name
6690 :: [opt] nested-name-specifier [opt] ~ type-name
6692 If either of the first two productions is used, sets *SCOPE to the
6693 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6694 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6695 or ERROR_MARK_NODE if the parse fails. */
6697 static void
6698 cp_parser_pseudo_destructor_name (cp_parser* parser,
6699 tree object,
6700 tree* scope,
6701 tree* type)
6703 bool nested_name_specifier_p;
6705 /* Handle ~auto. */
6706 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6707 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6708 && !type_dependent_expression_p (object))
6710 if (cxx_dialect < cxx1y)
6711 pedwarn (input_location, 0,
6712 "%<~auto%> only available with "
6713 "-std=c++1y or -std=gnu++1y");
6714 cp_lexer_consume_token (parser->lexer);
6715 cp_lexer_consume_token (parser->lexer);
6716 *scope = NULL_TREE;
6717 *type = TREE_TYPE (object);
6718 return;
6721 /* Assume that things will not work out. */
6722 *type = error_mark_node;
6724 /* Look for the optional `::' operator. */
6725 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6726 /* Look for the optional nested-name-specifier. */
6727 nested_name_specifier_p
6728 = (cp_parser_nested_name_specifier_opt (parser,
6729 /*typename_keyword_p=*/false,
6730 /*check_dependency_p=*/true,
6731 /*type_p=*/false,
6732 /*is_declaration=*/false)
6733 != NULL_TREE);
6734 /* Now, if we saw a nested-name-specifier, we might be doing the
6735 second production. */
6736 if (nested_name_specifier_p
6737 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6739 /* Consume the `template' keyword. */
6740 cp_lexer_consume_token (parser->lexer);
6741 /* Parse the template-id. */
6742 cp_parser_template_id (parser,
6743 /*template_keyword_p=*/true,
6744 /*check_dependency_p=*/false,
6745 class_type,
6746 /*is_declaration=*/true);
6747 /* Look for the `::' token. */
6748 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6750 /* If the next token is not a `~', then there might be some
6751 additional qualification. */
6752 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6754 /* At this point, we're looking for "type-name :: ~". The type-name
6755 must not be a class-name, since this is a pseudo-destructor. So,
6756 it must be either an enum-name, or a typedef-name -- both of which
6757 are just identifiers. So, we peek ahead to check that the "::"
6758 and "~" tokens are present; if they are not, then we can avoid
6759 calling type_name. */
6760 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6761 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6762 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6764 cp_parser_error (parser, "non-scalar type");
6765 return;
6768 /* Look for the type-name. */
6769 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6770 if (*scope == error_mark_node)
6771 return;
6773 /* Look for the `::' token. */
6774 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6776 else
6777 *scope = NULL_TREE;
6779 /* Look for the `~'. */
6780 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6782 /* Once we see the ~, this has to be a pseudo-destructor. */
6783 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6784 cp_parser_commit_to_topmost_tentative_parse (parser);
6786 /* Look for the type-name again. We are not responsible for
6787 checking that it matches the first type-name. */
6788 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6791 /* Parse a unary-expression.
6793 unary-expression:
6794 postfix-expression
6795 ++ cast-expression
6796 -- cast-expression
6797 unary-operator cast-expression
6798 sizeof unary-expression
6799 sizeof ( type-id )
6800 alignof ( type-id ) [C++0x]
6801 new-expression
6802 delete-expression
6804 GNU Extensions:
6806 unary-expression:
6807 __extension__ cast-expression
6808 __alignof__ unary-expression
6809 __alignof__ ( type-id )
6810 alignof unary-expression [C++0x]
6811 __real__ cast-expression
6812 __imag__ cast-expression
6813 && identifier
6814 sizeof ( type-id ) { initializer-list , [opt] }
6815 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
6816 __alignof__ ( type-id ) { initializer-list , [opt] }
6818 ADDRESS_P is true iff the unary-expression is appearing as the
6819 operand of the `&' operator. CAST_P is true if this expression is
6820 the target of a cast.
6822 Returns a representation of the expression. */
6824 static tree
6825 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6826 bool decltype_p, cp_id_kind * pidk)
6828 cp_token *token;
6829 enum tree_code unary_operator;
6831 /* Peek at the next token. */
6832 token = cp_lexer_peek_token (parser->lexer);
6833 /* Some keywords give away the kind of expression. */
6834 if (token->type == CPP_KEYWORD)
6836 enum rid keyword = token->keyword;
6838 switch (keyword)
6840 case RID_ALIGNOF:
6841 case RID_SIZEOF:
6843 tree operand, ret;
6844 enum tree_code op;
6845 location_t first_loc;
6847 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6848 /* Consume the token. */
6849 cp_lexer_consume_token (parser->lexer);
6850 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6851 /* Parse the operand. */
6852 operand = cp_parser_sizeof_operand (parser, keyword);
6854 if (TYPE_P (operand))
6855 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6856 else
6858 /* ISO C++ defines alignof only with types, not with
6859 expressions. So pedwarn if alignof is used with a non-
6860 type expression. However, __alignof__ is ok. */
6861 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6862 pedwarn (token->location, OPT_Wpedantic,
6863 "ISO C++ does not allow %<alignof%> "
6864 "with a non-type");
6866 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6868 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6869 SIZEOF_EXPR with the original operand. */
6870 if (op == SIZEOF_EXPR && ret != error_mark_node)
6872 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6874 if (!processing_template_decl && TYPE_P (operand))
6876 ret = build_min (SIZEOF_EXPR, size_type_node,
6877 build1 (NOP_EXPR, operand,
6878 error_mark_node));
6879 SIZEOF_EXPR_TYPE_P (ret) = 1;
6881 else
6882 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6883 TREE_SIDE_EFFECTS (ret) = 0;
6884 TREE_READONLY (ret) = 1;
6886 SET_EXPR_LOCATION (ret, first_loc);
6888 return ret;
6891 case RID_NEW:
6892 return cp_parser_new_expression (parser);
6894 case RID_DELETE:
6895 return cp_parser_delete_expression (parser);
6897 case RID_EXTENSION:
6899 /* The saved value of the PEDANTIC flag. */
6900 int saved_pedantic;
6901 tree expr;
6903 /* Save away the PEDANTIC flag. */
6904 cp_parser_extension_opt (parser, &saved_pedantic);
6905 /* Parse the cast-expression. */
6906 expr = cp_parser_simple_cast_expression (parser);
6907 /* Restore the PEDANTIC flag. */
6908 pedantic = saved_pedantic;
6910 return expr;
6913 case RID_REALPART:
6914 case RID_IMAGPART:
6916 tree expression;
6918 /* Consume the `__real__' or `__imag__' token. */
6919 cp_lexer_consume_token (parser->lexer);
6920 /* Parse the cast-expression. */
6921 expression = cp_parser_simple_cast_expression (parser);
6922 /* Create the complete representation. */
6923 return build_x_unary_op (token->location,
6924 (keyword == RID_REALPART
6925 ? REALPART_EXPR : IMAGPART_EXPR),
6926 expression,
6927 tf_warning_or_error);
6929 break;
6931 case RID_TRANSACTION_ATOMIC:
6932 case RID_TRANSACTION_RELAXED:
6933 return cp_parser_transaction_expression (parser, keyword);
6935 case RID_NOEXCEPT:
6937 tree expr;
6938 const char *saved_message;
6939 bool saved_integral_constant_expression_p;
6940 bool saved_non_integral_constant_expression_p;
6941 bool saved_greater_than_is_operator_p;
6943 cp_lexer_consume_token (parser->lexer);
6944 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6946 saved_message = parser->type_definition_forbidden_message;
6947 parser->type_definition_forbidden_message
6948 = G_("types may not be defined in %<noexcept%> expressions");
6950 saved_integral_constant_expression_p
6951 = parser->integral_constant_expression_p;
6952 saved_non_integral_constant_expression_p
6953 = parser->non_integral_constant_expression_p;
6954 parser->integral_constant_expression_p = false;
6956 saved_greater_than_is_operator_p
6957 = parser->greater_than_is_operator_p;
6958 parser->greater_than_is_operator_p = true;
6960 ++cp_unevaluated_operand;
6961 ++c_inhibit_evaluation_warnings;
6962 expr = cp_parser_expression (parser, false, NULL);
6963 --c_inhibit_evaluation_warnings;
6964 --cp_unevaluated_operand;
6966 parser->greater_than_is_operator_p
6967 = saved_greater_than_is_operator_p;
6969 parser->integral_constant_expression_p
6970 = saved_integral_constant_expression_p;
6971 parser->non_integral_constant_expression_p
6972 = saved_non_integral_constant_expression_p;
6974 parser->type_definition_forbidden_message = saved_message;
6976 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6977 return finish_noexcept_expr (expr, tf_warning_or_error);
6980 default:
6981 break;
6985 /* Look for the `:: new' and `:: delete', which also signal the
6986 beginning of a new-expression, or delete-expression,
6987 respectively. If the next token is `::', then it might be one of
6988 these. */
6989 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6991 enum rid keyword;
6993 /* See if the token after the `::' is one of the keywords in
6994 which we're interested. */
6995 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6996 /* If it's `new', we have a new-expression. */
6997 if (keyword == RID_NEW)
6998 return cp_parser_new_expression (parser);
6999 /* Similarly, for `delete'. */
7000 else if (keyword == RID_DELETE)
7001 return cp_parser_delete_expression (parser);
7004 /* Look for a unary operator. */
7005 unary_operator = cp_parser_unary_operator (token);
7006 /* The `++' and `--' operators can be handled similarly, even though
7007 they are not technically unary-operators in the grammar. */
7008 if (unary_operator == ERROR_MARK)
7010 if (token->type == CPP_PLUS_PLUS)
7011 unary_operator = PREINCREMENT_EXPR;
7012 else if (token->type == CPP_MINUS_MINUS)
7013 unary_operator = PREDECREMENT_EXPR;
7014 /* Handle the GNU address-of-label extension. */
7015 else if (cp_parser_allow_gnu_extensions_p (parser)
7016 && token->type == CPP_AND_AND)
7018 tree identifier;
7019 tree expression;
7020 location_t loc = token->location;
7022 /* Consume the '&&' token. */
7023 cp_lexer_consume_token (parser->lexer);
7024 /* Look for the identifier. */
7025 identifier = cp_parser_identifier (parser);
7026 /* Create an expression representing the address. */
7027 expression = finish_label_address_expr (identifier, loc);
7028 if (cp_parser_non_integral_constant_expression (parser,
7029 NIC_ADDR_LABEL))
7030 expression = error_mark_node;
7031 return expression;
7034 if (unary_operator != ERROR_MARK)
7036 tree cast_expression;
7037 tree expression = error_mark_node;
7038 non_integral_constant non_constant_p = NIC_NONE;
7039 location_t loc = token->location;
7040 tsubst_flags_t complain = complain_flags (decltype_p);
7042 /* Consume the operator token. */
7043 token = cp_lexer_consume_token (parser->lexer);
7044 /* Parse the cast-expression. */
7045 cast_expression
7046 = cp_parser_cast_expression (parser,
7047 unary_operator == ADDR_EXPR,
7048 /*cast_p=*/false,
7049 /*decltype*/false,
7050 pidk);
7051 /* Now, build an appropriate representation. */
7052 switch (unary_operator)
7054 case INDIRECT_REF:
7055 non_constant_p = NIC_STAR;
7056 expression = build_x_indirect_ref (loc, cast_expression,
7057 RO_UNARY_STAR,
7058 complain);
7059 break;
7061 case ADDR_EXPR:
7062 non_constant_p = NIC_ADDR;
7063 /* Fall through. */
7064 case BIT_NOT_EXPR:
7065 expression = build_x_unary_op (loc, unary_operator,
7066 cast_expression,
7067 complain);
7068 break;
7070 case PREINCREMENT_EXPR:
7071 case PREDECREMENT_EXPR:
7072 non_constant_p = unary_operator == PREINCREMENT_EXPR
7073 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7074 /* Fall through. */
7075 case UNARY_PLUS_EXPR:
7076 case NEGATE_EXPR:
7077 case TRUTH_NOT_EXPR:
7078 expression = finish_unary_op_expr (loc, unary_operator,
7079 cast_expression, complain);
7080 break;
7082 default:
7083 gcc_unreachable ();
7086 if (non_constant_p != NIC_NONE
7087 && cp_parser_non_integral_constant_expression (parser,
7088 non_constant_p))
7089 expression = error_mark_node;
7091 return expression;
7094 return cp_parser_postfix_expression (parser, address_p, cast_p,
7095 /*member_access_only_p=*/false,
7096 decltype_p,
7097 pidk);
7100 static inline tree
7101 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7102 cp_id_kind * pidk)
7104 return cp_parser_unary_expression (parser, address_p, cast_p,
7105 /*decltype*/false, pidk);
7108 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7109 unary-operator, the corresponding tree code is returned. */
7111 static enum tree_code
7112 cp_parser_unary_operator (cp_token* token)
7114 switch (token->type)
7116 case CPP_MULT:
7117 return INDIRECT_REF;
7119 case CPP_AND:
7120 return ADDR_EXPR;
7122 case CPP_PLUS:
7123 return UNARY_PLUS_EXPR;
7125 case CPP_MINUS:
7126 return NEGATE_EXPR;
7128 case CPP_NOT:
7129 return TRUTH_NOT_EXPR;
7131 case CPP_COMPL:
7132 return BIT_NOT_EXPR;
7134 default:
7135 return ERROR_MARK;
7139 /* Parse a new-expression.
7141 new-expression:
7142 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7143 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7145 Returns a representation of the expression. */
7147 static tree
7148 cp_parser_new_expression (cp_parser* parser)
7150 bool global_scope_p;
7151 vec<tree, va_gc> *placement;
7152 tree type;
7153 vec<tree, va_gc> *initializer;
7154 tree nelts = NULL_TREE;
7155 tree ret;
7157 /* Look for the optional `::' operator. */
7158 global_scope_p
7159 = (cp_parser_global_scope_opt (parser,
7160 /*current_scope_valid_p=*/false)
7161 != NULL_TREE);
7162 /* Look for the `new' operator. */
7163 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7164 /* There's no easy way to tell a new-placement from the
7165 `( type-id )' construct. */
7166 cp_parser_parse_tentatively (parser);
7167 /* Look for a new-placement. */
7168 placement = cp_parser_new_placement (parser);
7169 /* If that didn't work out, there's no new-placement. */
7170 if (!cp_parser_parse_definitely (parser))
7172 if (placement != NULL)
7173 release_tree_vector (placement);
7174 placement = NULL;
7177 /* If the next token is a `(', then we have a parenthesized
7178 type-id. */
7179 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7181 cp_token *token;
7182 const char *saved_message = parser->type_definition_forbidden_message;
7184 /* Consume the `('. */
7185 cp_lexer_consume_token (parser->lexer);
7187 /* Parse the type-id. */
7188 parser->type_definition_forbidden_message
7189 = G_("types may not be defined in a new-expression");
7190 type = cp_parser_type_id (parser);
7191 parser->type_definition_forbidden_message = saved_message;
7193 /* Look for the closing `)'. */
7194 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7195 token = cp_lexer_peek_token (parser->lexer);
7196 /* There should not be a direct-new-declarator in this production,
7197 but GCC used to allowed this, so we check and emit a sensible error
7198 message for this case. */
7199 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7201 error_at (token->location,
7202 "array bound forbidden after parenthesized type-id");
7203 inform (token->location,
7204 "try removing the parentheses around the type-id");
7205 cp_parser_direct_new_declarator (parser);
7208 /* Otherwise, there must be a new-type-id. */
7209 else
7210 type = cp_parser_new_type_id (parser, &nelts);
7212 /* If the next token is a `(' or '{', then we have a new-initializer. */
7213 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7214 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7215 initializer = cp_parser_new_initializer (parser);
7216 else
7217 initializer = NULL;
7219 /* A new-expression may not appear in an integral constant
7220 expression. */
7221 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7222 ret = error_mark_node;
7223 else
7225 /* Create a representation of the new-expression. */
7226 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7227 tf_warning_or_error);
7230 if (placement != NULL)
7231 release_tree_vector (placement);
7232 if (initializer != NULL)
7233 release_tree_vector (initializer);
7235 return ret;
7238 /* Parse a new-placement.
7240 new-placement:
7241 ( expression-list )
7243 Returns the same representation as for an expression-list. */
7245 static vec<tree, va_gc> *
7246 cp_parser_new_placement (cp_parser* parser)
7248 vec<tree, va_gc> *expression_list;
7250 /* Parse the expression-list. */
7251 expression_list = (cp_parser_parenthesized_expression_list
7252 (parser, non_attr, /*cast_p=*/false,
7253 /*allow_expansion_p=*/true,
7254 /*non_constant_p=*/NULL));
7256 return expression_list;
7259 /* Parse a new-type-id.
7261 new-type-id:
7262 type-specifier-seq new-declarator [opt]
7264 Returns the TYPE allocated. If the new-type-id indicates an array
7265 type, *NELTS is set to the number of elements in the last array
7266 bound; the TYPE will not include the last array bound. */
7268 static tree
7269 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7271 cp_decl_specifier_seq type_specifier_seq;
7272 cp_declarator *new_declarator;
7273 cp_declarator *declarator;
7274 cp_declarator *outer_declarator;
7275 const char *saved_message;
7277 /* The type-specifier sequence must not contain type definitions.
7278 (It cannot contain declarations of new types either, but if they
7279 are not definitions we will catch that because they are not
7280 complete.) */
7281 saved_message = parser->type_definition_forbidden_message;
7282 parser->type_definition_forbidden_message
7283 = G_("types may not be defined in a new-type-id");
7284 /* Parse the type-specifier-seq. */
7285 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7286 /*is_trailing_return=*/false,
7287 &type_specifier_seq);
7288 /* Restore the old message. */
7289 parser->type_definition_forbidden_message = saved_message;
7291 if (type_specifier_seq.type == error_mark_node)
7292 return error_mark_node;
7294 /* Parse the new-declarator. */
7295 new_declarator = cp_parser_new_declarator_opt (parser);
7297 /* Determine the number of elements in the last array dimension, if
7298 any. */
7299 *nelts = NULL_TREE;
7300 /* Skip down to the last array dimension. */
7301 declarator = new_declarator;
7302 outer_declarator = NULL;
7303 while (declarator && (declarator->kind == cdk_pointer
7304 || declarator->kind == cdk_ptrmem))
7306 outer_declarator = declarator;
7307 declarator = declarator->declarator;
7309 while (declarator
7310 && declarator->kind == cdk_array
7311 && declarator->declarator
7312 && declarator->declarator->kind == cdk_array)
7314 outer_declarator = declarator;
7315 declarator = declarator->declarator;
7318 if (declarator && declarator->kind == cdk_array)
7320 *nelts = declarator->u.array.bounds;
7321 if (*nelts == error_mark_node)
7322 *nelts = integer_one_node;
7324 if (outer_declarator)
7325 outer_declarator->declarator = declarator->declarator;
7326 else
7327 new_declarator = NULL;
7330 return groktypename (&type_specifier_seq, new_declarator, false);
7333 /* Parse an (optional) new-declarator.
7335 new-declarator:
7336 ptr-operator new-declarator [opt]
7337 direct-new-declarator
7339 Returns the declarator. */
7341 static cp_declarator *
7342 cp_parser_new_declarator_opt (cp_parser* parser)
7344 enum tree_code code;
7345 tree type, std_attributes = NULL_TREE;
7346 cp_cv_quals cv_quals;
7348 /* We don't know if there's a ptr-operator next, or not. */
7349 cp_parser_parse_tentatively (parser);
7350 /* Look for a ptr-operator. */
7351 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7352 /* If that worked, look for more new-declarators. */
7353 if (cp_parser_parse_definitely (parser))
7355 cp_declarator *declarator;
7357 /* Parse another optional declarator. */
7358 declarator = cp_parser_new_declarator_opt (parser);
7360 declarator = cp_parser_make_indirect_declarator
7361 (code, type, cv_quals, declarator, std_attributes);
7363 return declarator;
7366 /* If the next token is a `[', there is a direct-new-declarator. */
7367 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7368 return cp_parser_direct_new_declarator (parser);
7370 return NULL;
7373 /* Parse a direct-new-declarator.
7375 direct-new-declarator:
7376 [ expression ]
7377 direct-new-declarator [constant-expression]
7381 static cp_declarator *
7382 cp_parser_direct_new_declarator (cp_parser* parser)
7384 cp_declarator *declarator = NULL;
7386 while (true)
7388 tree expression;
7389 cp_token *token;
7391 /* Look for the opening `['. */
7392 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7394 token = cp_lexer_peek_token (parser->lexer);
7395 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7396 /* The standard requires that the expression have integral
7397 type. DR 74 adds enumeration types. We believe that the
7398 real intent is that these expressions be handled like the
7399 expression in a `switch' condition, which also allows
7400 classes with a single conversion to integral or
7401 enumeration type. */
7402 if (!processing_template_decl)
7404 expression
7405 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7406 expression,
7407 /*complain=*/true);
7408 if (!expression)
7410 error_at (token->location,
7411 "expression in new-declarator must have integral "
7412 "or enumeration type");
7413 expression = error_mark_node;
7417 /* Look for the closing `]'. */
7418 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7420 /* Add this bound to the declarator. */
7421 declarator = make_array_declarator (declarator, expression);
7423 /* If the next token is not a `[', then there are no more
7424 bounds. */
7425 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7426 break;
7429 return declarator;
7432 /* Parse a new-initializer.
7434 new-initializer:
7435 ( expression-list [opt] )
7436 braced-init-list
7438 Returns a representation of the expression-list. */
7440 static vec<tree, va_gc> *
7441 cp_parser_new_initializer (cp_parser* parser)
7443 vec<tree, va_gc> *expression_list;
7445 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7447 tree t;
7448 bool expr_non_constant_p;
7449 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7450 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7451 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7452 expression_list = make_tree_vector_single (t);
7454 else
7455 expression_list = (cp_parser_parenthesized_expression_list
7456 (parser, non_attr, /*cast_p=*/false,
7457 /*allow_expansion_p=*/true,
7458 /*non_constant_p=*/NULL));
7460 return expression_list;
7463 /* Parse a delete-expression.
7465 delete-expression:
7466 :: [opt] delete cast-expression
7467 :: [opt] delete [ ] cast-expression
7469 Returns a representation of the expression. */
7471 static tree
7472 cp_parser_delete_expression (cp_parser* parser)
7474 bool global_scope_p;
7475 bool array_p;
7476 tree expression;
7478 /* Look for the optional `::' operator. */
7479 global_scope_p
7480 = (cp_parser_global_scope_opt (parser,
7481 /*current_scope_valid_p=*/false)
7482 != NULL_TREE);
7483 /* Look for the `delete' keyword. */
7484 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7485 /* See if the array syntax is in use. */
7486 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7488 /* Consume the `[' token. */
7489 cp_lexer_consume_token (parser->lexer);
7490 /* Look for the `]' token. */
7491 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7492 /* Remember that this is the `[]' construct. */
7493 array_p = true;
7495 else
7496 array_p = false;
7498 /* Parse the cast-expression. */
7499 expression = cp_parser_simple_cast_expression (parser);
7501 /* A delete-expression may not appear in an integral constant
7502 expression. */
7503 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7504 return error_mark_node;
7506 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7507 tf_warning_or_error);
7510 /* Returns true if TOKEN may start a cast-expression and false
7511 otherwise. */
7513 static bool
7514 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7516 cp_token *token = cp_lexer_peek_token (parser->lexer);
7517 switch (token->type)
7519 case CPP_COMMA:
7520 case CPP_SEMICOLON:
7521 case CPP_QUERY:
7522 case CPP_COLON:
7523 case CPP_CLOSE_SQUARE:
7524 case CPP_CLOSE_PAREN:
7525 case CPP_CLOSE_BRACE:
7526 case CPP_OPEN_BRACE:
7527 case CPP_DOT:
7528 case CPP_DOT_STAR:
7529 case CPP_DEREF:
7530 case CPP_DEREF_STAR:
7531 case CPP_DIV:
7532 case CPP_MOD:
7533 case CPP_LSHIFT:
7534 case CPP_RSHIFT:
7535 case CPP_LESS:
7536 case CPP_GREATER:
7537 case CPP_LESS_EQ:
7538 case CPP_GREATER_EQ:
7539 case CPP_EQ_EQ:
7540 case CPP_NOT_EQ:
7541 case CPP_EQ:
7542 case CPP_MULT_EQ:
7543 case CPP_DIV_EQ:
7544 case CPP_MOD_EQ:
7545 case CPP_PLUS_EQ:
7546 case CPP_MINUS_EQ:
7547 case CPP_RSHIFT_EQ:
7548 case CPP_LSHIFT_EQ:
7549 case CPP_AND_EQ:
7550 case CPP_XOR_EQ:
7551 case CPP_OR_EQ:
7552 case CPP_XOR:
7553 case CPP_OR:
7554 case CPP_OR_OR:
7555 case CPP_EOF:
7556 return false;
7558 case CPP_OPEN_PAREN:
7559 /* In ((type ()) () the last () isn't a valid cast-expression,
7560 so the whole must be parsed as postfix-expression. */
7561 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7562 != CPP_CLOSE_PAREN;
7564 /* '[' may start a primary-expression in obj-c++. */
7565 case CPP_OPEN_SQUARE:
7566 return c_dialect_objc ();
7568 default:
7569 return true;
7573 /* Parse a cast-expression.
7575 cast-expression:
7576 unary-expression
7577 ( type-id ) cast-expression
7579 ADDRESS_P is true iff the unary-expression is appearing as the
7580 operand of the `&' operator. CAST_P is true if this expression is
7581 the target of a cast.
7583 Returns a representation of the expression. */
7585 static tree
7586 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7587 bool decltype_p, cp_id_kind * pidk)
7589 /* If it's a `(', then we might be looking at a cast. */
7590 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7592 tree type = NULL_TREE;
7593 tree expr = NULL_TREE;
7594 bool cast_expression_p;
7595 const char *saved_message;
7597 /* There's no way to know yet whether or not this is a cast.
7598 For example, `(int (3))' is a unary-expression, while `(int)
7599 3' is a cast. So, we resort to parsing tentatively. */
7600 cp_parser_parse_tentatively (parser);
7601 /* Types may not be defined in a cast. */
7602 saved_message = parser->type_definition_forbidden_message;
7603 parser->type_definition_forbidden_message
7604 = G_("types may not be defined in casts");
7605 /* Consume the `('. */
7606 cp_lexer_consume_token (parser->lexer);
7607 /* A very tricky bit is that `(struct S) { 3 }' is a
7608 compound-literal (which we permit in C++ as an extension).
7609 But, that construct is not a cast-expression -- it is a
7610 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7611 is legal; if the compound-literal were a cast-expression,
7612 you'd need an extra set of parentheses.) But, if we parse
7613 the type-id, and it happens to be a class-specifier, then we
7614 will commit to the parse at that point, because we cannot
7615 undo the action that is done when creating a new class. So,
7616 then we cannot back up and do a postfix-expression.
7617 Another tricky case is the following (c++/29234):
7619 struct S { void operator () (); };
7621 void foo ()
7623 ( S()() );
7626 As a type-id we parse the parenthesized S()() as a function
7627 returning a function, groktypename complains and we cannot
7628 back up in this case either.
7630 Therefore, we scan ahead to the closing `)', and check to see
7631 if the tokens after the `)' can start a cast-expression. Otherwise
7632 we are dealing with an unary-expression, a postfix-expression
7633 or something else.
7635 Save tokens so that we can put them back. */
7636 cp_lexer_save_tokens (parser->lexer);
7638 /* We may be looking at a cast-expression. */
7639 cast_expression_p
7640 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7641 /*consume_paren=*/true)
7642 && cp_parser_tokens_start_cast_expression (parser));
7644 /* Roll back the tokens we skipped. */
7645 cp_lexer_rollback_tokens (parser->lexer);
7646 /* If we aren't looking at a cast-expression, simulate an error so
7647 that the call to cp_parser_parse_definitely below will fail. */
7648 if (!cast_expression_p)
7649 cp_parser_simulate_error (parser);
7650 else
7652 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7653 parser->in_type_id_in_expr_p = true;
7654 /* Look for the type-id. */
7655 type = cp_parser_type_id (parser);
7656 /* Look for the closing `)'. */
7657 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7658 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7661 /* Restore the saved message. */
7662 parser->type_definition_forbidden_message = saved_message;
7664 /* At this point this can only be either a cast or a
7665 parenthesized ctor such as `(T ())' that looks like a cast to
7666 function returning T. */
7667 if (!cp_parser_error_occurred (parser))
7669 cp_parser_parse_definitely (parser);
7670 expr = cp_parser_cast_expression (parser,
7671 /*address_p=*/false,
7672 /*cast_p=*/true,
7673 /*decltype_p=*/false,
7674 pidk);
7676 /* Warn about old-style casts, if so requested. */
7677 if (warn_old_style_cast
7678 && !in_system_header
7679 && !VOID_TYPE_P (type)
7680 && current_lang_name != lang_name_c)
7681 warning (OPT_Wold_style_cast, "use of old-style cast");
7683 /* Only type conversions to integral or enumeration types
7684 can be used in constant-expressions. */
7685 if (!cast_valid_in_integral_constant_expression_p (type)
7686 && cp_parser_non_integral_constant_expression (parser,
7687 NIC_CAST))
7688 return error_mark_node;
7690 /* Perform the cast. */
7691 expr = build_c_cast (input_location, type, expr);
7692 return expr;
7694 else
7695 cp_parser_abort_tentative_parse (parser);
7698 /* If we get here, then it's not a cast, so it must be a
7699 unary-expression. */
7700 return cp_parser_unary_expression (parser, address_p, cast_p,
7701 decltype_p, pidk);
7704 /* Parse a binary expression of the general form:
7706 pm-expression:
7707 cast-expression
7708 pm-expression .* cast-expression
7709 pm-expression ->* cast-expression
7711 multiplicative-expression:
7712 pm-expression
7713 multiplicative-expression * pm-expression
7714 multiplicative-expression / pm-expression
7715 multiplicative-expression % pm-expression
7717 additive-expression:
7718 multiplicative-expression
7719 additive-expression + multiplicative-expression
7720 additive-expression - multiplicative-expression
7722 shift-expression:
7723 additive-expression
7724 shift-expression << additive-expression
7725 shift-expression >> additive-expression
7727 relational-expression:
7728 shift-expression
7729 relational-expression < shift-expression
7730 relational-expression > shift-expression
7731 relational-expression <= shift-expression
7732 relational-expression >= shift-expression
7734 GNU Extension:
7736 relational-expression:
7737 relational-expression <? shift-expression
7738 relational-expression >? shift-expression
7740 equality-expression:
7741 relational-expression
7742 equality-expression == relational-expression
7743 equality-expression != relational-expression
7745 and-expression:
7746 equality-expression
7747 and-expression & equality-expression
7749 exclusive-or-expression:
7750 and-expression
7751 exclusive-or-expression ^ and-expression
7753 inclusive-or-expression:
7754 exclusive-or-expression
7755 inclusive-or-expression | exclusive-or-expression
7757 logical-and-expression:
7758 inclusive-or-expression
7759 logical-and-expression && inclusive-or-expression
7761 logical-or-expression:
7762 logical-and-expression
7763 logical-or-expression || logical-and-expression
7765 All these are implemented with a single function like:
7767 binary-expression:
7768 simple-cast-expression
7769 binary-expression <token> binary-expression
7771 CAST_P is true if this expression is the target of a cast.
7773 The binops_by_token map is used to get the tree codes for each <token> type.
7774 binary-expressions are associated according to a precedence table. */
7776 #define TOKEN_PRECEDENCE(token) \
7777 (((token->type == CPP_GREATER \
7778 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7779 && !parser->greater_than_is_operator_p) \
7780 ? PREC_NOT_OPERATOR \
7781 : binops_by_token[token->type].prec)
7783 static tree
7784 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7785 bool no_toplevel_fold_p,
7786 bool decltype_p,
7787 enum cp_parser_prec prec,
7788 cp_id_kind * pidk)
7790 cp_parser_expression_stack stack;
7791 cp_parser_expression_stack_entry *sp = &stack[0];
7792 cp_parser_expression_stack_entry current;
7793 tree rhs;
7794 cp_token *token;
7795 enum tree_code rhs_type;
7796 enum cp_parser_prec new_prec, lookahead_prec;
7797 tree overload;
7799 /* Parse the first expression. */
7800 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7801 cast_p, decltype_p, pidk);
7802 current.lhs_type = ERROR_MARK;
7803 current.prec = prec;
7805 if (cp_parser_error_occurred (parser))
7806 return error_mark_node;
7808 for (;;)
7810 /* Get an operator token. */
7811 token = cp_lexer_peek_token (parser->lexer);
7813 if (warn_cxx0x_compat
7814 && token->type == CPP_RSHIFT
7815 && !parser->greater_than_is_operator_p)
7817 if (warning_at (token->location, OPT_Wc__0x_compat,
7818 "%<>>%> operator is treated"
7819 " as two right angle brackets in C++11"))
7820 inform (token->location,
7821 "suggest parentheses around %<>>%> expression");
7824 new_prec = TOKEN_PRECEDENCE (token);
7826 /* Popping an entry off the stack means we completed a subexpression:
7827 - either we found a token which is not an operator (`>' where it is not
7828 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7829 will happen repeatedly;
7830 - or, we found an operator which has lower priority. This is the case
7831 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7832 parsing `3 * 4'. */
7833 if (new_prec <= current.prec)
7835 if (sp == stack)
7836 break;
7837 else
7838 goto pop;
7841 get_rhs:
7842 current.tree_type = binops_by_token[token->type].tree_type;
7843 current.loc = token->location;
7845 /* We used the operator token. */
7846 cp_lexer_consume_token (parser->lexer);
7848 /* For "false && x" or "true || x", x will never be executed;
7849 disable warnings while evaluating it. */
7850 if (current.tree_type == TRUTH_ANDIF_EXPR)
7851 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7852 else if (current.tree_type == TRUTH_ORIF_EXPR)
7853 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7855 /* Extract another operand. It may be the RHS of this expression
7856 or the LHS of a new, higher priority expression. */
7857 rhs = cp_parser_simple_cast_expression (parser);
7858 rhs_type = ERROR_MARK;
7860 /* Get another operator token. Look up its precedence to avoid
7861 building a useless (immediately popped) stack entry for common
7862 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7863 token = cp_lexer_peek_token (parser->lexer);
7864 lookahead_prec = TOKEN_PRECEDENCE (token);
7865 if (lookahead_prec > new_prec)
7867 /* ... and prepare to parse the RHS of the new, higher priority
7868 expression. Since precedence levels on the stack are
7869 monotonically increasing, we do not have to care about
7870 stack overflows. */
7871 *sp = current;
7872 ++sp;
7873 current.lhs = rhs;
7874 current.lhs_type = rhs_type;
7875 current.prec = new_prec;
7876 new_prec = lookahead_prec;
7877 goto get_rhs;
7879 pop:
7880 lookahead_prec = new_prec;
7881 /* If the stack is not empty, we have parsed into LHS the right side
7882 (`4' in the example above) of an expression we had suspended.
7883 We can use the information on the stack to recover the LHS (`3')
7884 from the stack together with the tree code (`MULT_EXPR'), and
7885 the precedence of the higher level subexpression
7886 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7887 which will be used to actually build the additive expression. */
7888 rhs = current.lhs;
7889 rhs_type = current.lhs_type;
7890 --sp;
7891 current = *sp;
7894 /* Undo the disabling of warnings done above. */
7895 if (current.tree_type == TRUTH_ANDIF_EXPR)
7896 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7897 else if (current.tree_type == TRUTH_ORIF_EXPR)
7898 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7900 overload = NULL;
7901 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7902 ERROR_MARK for everything that is not a binary expression.
7903 This makes warn_about_parentheses miss some warnings that
7904 involve unary operators. For unary expressions we should
7905 pass the correct tree_code unless the unary expression was
7906 surrounded by parentheses.
7908 if (no_toplevel_fold_p
7909 && lookahead_prec <= current.prec
7910 && sp == stack)
7911 current.lhs = build2 (current.tree_type,
7912 TREE_CODE_CLASS (current.tree_type)
7913 == tcc_comparison
7914 ? boolean_type_node : TREE_TYPE (current.lhs),
7915 current.lhs, rhs);
7916 else
7917 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7918 current.lhs, current.lhs_type,
7919 rhs, rhs_type, &overload,
7920 complain_flags (decltype_p));
7921 current.lhs_type = current.tree_type;
7922 if (EXPR_P (current.lhs))
7923 SET_EXPR_LOCATION (current.lhs, current.loc);
7925 /* If the binary operator required the use of an overloaded operator,
7926 then this expression cannot be an integral constant-expression.
7927 An overloaded operator can be used even if both operands are
7928 otherwise permissible in an integral constant-expression if at
7929 least one of the operands is of enumeration type. */
7931 if (overload
7932 && cp_parser_non_integral_constant_expression (parser,
7933 NIC_OVERLOADED))
7934 return error_mark_node;
7937 return current.lhs;
7940 static tree
7941 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7942 bool no_toplevel_fold_p,
7943 enum cp_parser_prec prec,
7944 cp_id_kind * pidk)
7946 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
7947 /*decltype*/false, prec, pidk);
7950 /* Parse the `? expression : assignment-expression' part of a
7951 conditional-expression. The LOGICAL_OR_EXPR is the
7952 logical-or-expression that started the conditional-expression.
7953 Returns a representation of the entire conditional-expression.
7955 This routine is used by cp_parser_assignment_expression.
7957 ? expression : assignment-expression
7959 GNU Extensions:
7961 ? : assignment-expression */
7963 static tree
7964 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7966 tree expr;
7967 tree assignment_expr;
7968 struct cp_token *token;
7969 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7971 /* Consume the `?' token. */
7972 cp_lexer_consume_token (parser->lexer);
7973 token = cp_lexer_peek_token (parser->lexer);
7974 if (cp_parser_allow_gnu_extensions_p (parser)
7975 && token->type == CPP_COLON)
7977 pedwarn (token->location, OPT_Wpedantic,
7978 "ISO C++ does not allow ?: with omitted middle operand");
7979 /* Implicit true clause. */
7980 expr = NULL_TREE;
7981 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7982 warn_for_omitted_condop (token->location, logical_or_expr);
7984 else
7986 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7987 parser->colon_corrects_to_scope_p = false;
7988 /* Parse the expression. */
7989 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7990 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7991 c_inhibit_evaluation_warnings +=
7992 ((logical_or_expr == truthvalue_true_node)
7993 - (logical_or_expr == truthvalue_false_node));
7994 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7997 /* The next token should be a `:'. */
7998 cp_parser_require (parser, CPP_COLON, RT_COLON);
7999 /* Parse the assignment-expression. */
8000 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8001 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8003 /* Build the conditional-expression. */
8004 return build_x_conditional_expr (loc, logical_or_expr,
8005 expr,
8006 assignment_expr,
8007 tf_warning_or_error);
8010 /* Parse an assignment-expression.
8012 assignment-expression:
8013 conditional-expression
8014 logical-or-expression assignment-operator assignment_expression
8015 throw-expression
8017 CAST_P is true if this expression is the target of a cast.
8018 DECLTYPE_P is true if this expression is the operand of decltype.
8020 Returns a representation for the expression. */
8022 static tree
8023 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8024 bool decltype_p, cp_id_kind * pidk)
8026 tree expr;
8028 /* If the next token is the `throw' keyword, then we're looking at
8029 a throw-expression. */
8030 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8031 expr = cp_parser_throw_expression (parser);
8032 /* Otherwise, it must be that we are looking at a
8033 logical-or-expression. */
8034 else
8036 /* Parse the binary expressions (logical-or-expression). */
8037 expr = cp_parser_binary_expression (parser, cast_p, false,
8038 decltype_p,
8039 PREC_NOT_OPERATOR, pidk);
8040 /* If the next token is a `?' then we're actually looking at a
8041 conditional-expression. */
8042 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8043 return cp_parser_question_colon_clause (parser, expr);
8044 else
8046 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8048 /* If it's an assignment-operator, we're using the second
8049 production. */
8050 enum tree_code assignment_operator
8051 = cp_parser_assignment_operator_opt (parser);
8052 if (assignment_operator != ERROR_MARK)
8054 bool non_constant_p;
8055 location_t saved_input_location;
8057 /* Parse the right-hand side of the assignment. */
8058 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8060 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8061 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8063 /* An assignment may not appear in a
8064 constant-expression. */
8065 if (cp_parser_non_integral_constant_expression (parser,
8066 NIC_ASSIGNMENT))
8067 return error_mark_node;
8068 /* Build the assignment expression. Its default
8069 location is the location of the '=' token. */
8070 saved_input_location = input_location;
8071 input_location = loc;
8072 expr = build_x_modify_expr (loc, expr,
8073 assignment_operator,
8074 rhs,
8075 complain_flags (decltype_p));
8076 input_location = saved_input_location;
8081 return expr;
8084 static tree
8085 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8086 cp_id_kind * pidk)
8088 return cp_parser_assignment_expression (parser, cast_p,
8089 /*decltype*/false, pidk);
8092 /* Parse an (optional) assignment-operator.
8094 assignment-operator: one of
8095 = *= /= %= += -= >>= <<= &= ^= |=
8097 GNU Extension:
8099 assignment-operator: one of
8100 <?= >?=
8102 If the next token is an assignment operator, the corresponding tree
8103 code is returned, and the token is consumed. For example, for
8104 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8105 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8106 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8107 operator, ERROR_MARK is returned. */
8109 static enum tree_code
8110 cp_parser_assignment_operator_opt (cp_parser* parser)
8112 enum tree_code op;
8113 cp_token *token;
8115 /* Peek at the next token. */
8116 token = cp_lexer_peek_token (parser->lexer);
8118 switch (token->type)
8120 case CPP_EQ:
8121 op = NOP_EXPR;
8122 break;
8124 case CPP_MULT_EQ:
8125 op = MULT_EXPR;
8126 break;
8128 case CPP_DIV_EQ:
8129 op = TRUNC_DIV_EXPR;
8130 break;
8132 case CPP_MOD_EQ:
8133 op = TRUNC_MOD_EXPR;
8134 break;
8136 case CPP_PLUS_EQ:
8137 op = PLUS_EXPR;
8138 break;
8140 case CPP_MINUS_EQ:
8141 op = MINUS_EXPR;
8142 break;
8144 case CPP_RSHIFT_EQ:
8145 op = RSHIFT_EXPR;
8146 break;
8148 case CPP_LSHIFT_EQ:
8149 op = LSHIFT_EXPR;
8150 break;
8152 case CPP_AND_EQ:
8153 op = BIT_AND_EXPR;
8154 break;
8156 case CPP_XOR_EQ:
8157 op = BIT_XOR_EXPR;
8158 break;
8160 case CPP_OR_EQ:
8161 op = BIT_IOR_EXPR;
8162 break;
8164 default:
8165 /* Nothing else is an assignment operator. */
8166 op = ERROR_MARK;
8169 /* If it was an assignment operator, consume it. */
8170 if (op != ERROR_MARK)
8171 cp_lexer_consume_token (parser->lexer);
8173 return op;
8176 /* Parse an expression.
8178 expression:
8179 assignment-expression
8180 expression , assignment-expression
8182 CAST_P is true if this expression is the target of a cast.
8183 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8184 except possibly parenthesized or on the RHS of a comma (N3276).
8186 Returns a representation of the expression. */
8188 static tree
8189 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
8190 cp_id_kind * pidk)
8192 tree expression = NULL_TREE;
8193 location_t loc = UNKNOWN_LOCATION;
8195 while (true)
8197 tree assignment_expression;
8199 /* Parse the next assignment-expression. */
8200 assignment_expression
8201 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8203 /* We don't create a temporary for a call that is the immediate operand
8204 of decltype or on the RHS of a comma. But when we see a comma, we
8205 need to create a temporary for a call on the LHS. */
8206 if (decltype_p && !processing_template_decl
8207 && TREE_CODE (assignment_expression) == CALL_EXPR
8208 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8209 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8210 assignment_expression
8211 = build_cplus_new (TREE_TYPE (assignment_expression),
8212 assignment_expression, tf_warning_or_error);
8214 /* If this is the first assignment-expression, we can just
8215 save it away. */
8216 if (!expression)
8217 expression = assignment_expression;
8218 else
8219 expression = build_x_compound_expr (loc, expression,
8220 assignment_expression,
8221 complain_flags (decltype_p));
8222 /* If the next token is not a comma, then we are done with the
8223 expression. */
8224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8225 break;
8226 /* Consume the `,'. */
8227 loc = cp_lexer_peek_token (parser->lexer)->location;
8228 cp_lexer_consume_token (parser->lexer);
8229 /* A comma operator cannot appear in a constant-expression. */
8230 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8231 expression = error_mark_node;
8234 return expression;
8237 static inline tree
8238 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
8240 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
8243 /* Parse a constant-expression.
8245 constant-expression:
8246 conditional-expression
8248 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8249 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8250 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8251 is false, NON_CONSTANT_P should be NULL. */
8253 static tree
8254 cp_parser_constant_expression (cp_parser* parser,
8255 bool allow_non_constant_p,
8256 bool *non_constant_p)
8258 bool saved_integral_constant_expression_p;
8259 bool saved_allow_non_integral_constant_expression_p;
8260 bool saved_non_integral_constant_expression_p;
8261 tree expression;
8263 /* It might seem that we could simply parse the
8264 conditional-expression, and then check to see if it were
8265 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8266 one that the compiler can figure out is constant, possibly after
8267 doing some simplifications or optimizations. The standard has a
8268 precise definition of constant-expression, and we must honor
8269 that, even though it is somewhat more restrictive.
8271 For example:
8273 int i[(2, 3)];
8275 is not a legal declaration, because `(2, 3)' is not a
8276 constant-expression. The `,' operator is forbidden in a
8277 constant-expression. However, GCC's constant-folding machinery
8278 will fold this operation to an INTEGER_CST for `3'. */
8280 /* Save the old settings. */
8281 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8282 saved_allow_non_integral_constant_expression_p
8283 = parser->allow_non_integral_constant_expression_p;
8284 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8285 /* We are now parsing a constant-expression. */
8286 parser->integral_constant_expression_p = true;
8287 parser->allow_non_integral_constant_expression_p
8288 = (allow_non_constant_p || cxx_dialect >= cxx11);
8289 parser->non_integral_constant_expression_p = false;
8290 /* Although the grammar says "conditional-expression", we parse an
8291 "assignment-expression", which also permits "throw-expression"
8292 and the use of assignment operators. In the case that
8293 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8294 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8295 actually essential that we look for an assignment-expression.
8296 For example, cp_parser_initializer_clauses uses this function to
8297 determine whether a particular assignment-expression is in fact
8298 constant. */
8299 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8300 /* Restore the old settings. */
8301 parser->integral_constant_expression_p
8302 = saved_integral_constant_expression_p;
8303 parser->allow_non_integral_constant_expression_p
8304 = saved_allow_non_integral_constant_expression_p;
8305 if (cxx_dialect >= cxx11)
8307 /* Require an rvalue constant expression here; that's what our
8308 callers expect. Reference constant expressions are handled
8309 separately in e.g. cp_parser_template_argument. */
8310 bool is_const = potential_rvalue_constant_expression (expression);
8311 parser->non_integral_constant_expression_p = !is_const;
8312 if (!is_const && !allow_non_constant_p)
8313 require_potential_rvalue_constant_expression (expression);
8315 if (allow_non_constant_p)
8316 *non_constant_p = parser->non_integral_constant_expression_p;
8317 parser->non_integral_constant_expression_p
8318 = saved_non_integral_constant_expression_p;
8320 return expression;
8323 /* Parse __builtin_offsetof.
8325 offsetof-expression:
8326 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8328 offsetof-member-designator:
8329 id-expression
8330 | offsetof-member-designator "." id-expression
8331 | offsetof-member-designator "[" expression "]"
8332 | offsetof-member-designator "->" id-expression */
8334 static tree
8335 cp_parser_builtin_offsetof (cp_parser *parser)
8337 int save_ice_p, save_non_ice_p;
8338 tree type, expr;
8339 cp_id_kind dummy;
8340 cp_token *token;
8342 /* We're about to accept non-integral-constant things, but will
8343 definitely yield an integral constant expression. Save and
8344 restore these values around our local parsing. */
8345 save_ice_p = parser->integral_constant_expression_p;
8346 save_non_ice_p = parser->non_integral_constant_expression_p;
8348 /* Consume the "__builtin_offsetof" token. */
8349 cp_lexer_consume_token (parser->lexer);
8350 /* Consume the opening `('. */
8351 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8352 /* Parse the type-id. */
8353 type = cp_parser_type_id (parser);
8354 /* Look for the `,'. */
8355 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8356 token = cp_lexer_peek_token (parser->lexer);
8358 /* Build the (type *)null that begins the traditional offsetof macro. */
8359 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8360 tf_warning_or_error);
8362 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8363 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8364 true, &dummy, token->location);
8365 while (true)
8367 token = cp_lexer_peek_token (parser->lexer);
8368 switch (token->type)
8370 case CPP_OPEN_SQUARE:
8371 /* offsetof-member-designator "[" expression "]" */
8372 expr = cp_parser_postfix_open_square_expression (parser, expr,
8373 true, false);
8374 break;
8376 case CPP_DEREF:
8377 /* offsetof-member-designator "->" identifier */
8378 expr = grok_array_decl (token->location, expr,
8379 integer_zero_node, false);
8380 /* FALLTHRU */
8382 case CPP_DOT:
8383 /* offsetof-member-designator "." identifier */
8384 cp_lexer_consume_token (parser->lexer);
8385 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8386 expr, true, &dummy,
8387 token->location);
8388 break;
8390 case CPP_CLOSE_PAREN:
8391 /* Consume the ")" token. */
8392 cp_lexer_consume_token (parser->lexer);
8393 goto success;
8395 default:
8396 /* Error. We know the following require will fail, but
8397 that gives the proper error message. */
8398 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8399 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8400 expr = error_mark_node;
8401 goto failure;
8405 success:
8406 /* If we're processing a template, we can't finish the semantics yet.
8407 Otherwise we can fold the entire expression now. */
8408 if (processing_template_decl)
8409 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8410 else
8411 expr = finish_offsetof (expr);
8413 failure:
8414 parser->integral_constant_expression_p = save_ice_p;
8415 parser->non_integral_constant_expression_p = save_non_ice_p;
8417 return expr;
8420 /* Parse a trait expression.
8422 Returns a representation of the expression, the underlying type
8423 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8425 static tree
8426 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8428 cp_trait_kind kind;
8429 tree type1, type2 = NULL_TREE;
8430 bool binary = false;
8431 cp_decl_specifier_seq decl_specs;
8433 switch (keyword)
8435 case RID_HAS_NOTHROW_ASSIGN:
8436 kind = CPTK_HAS_NOTHROW_ASSIGN;
8437 break;
8438 case RID_HAS_NOTHROW_CONSTRUCTOR:
8439 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8440 break;
8441 case RID_HAS_NOTHROW_COPY:
8442 kind = CPTK_HAS_NOTHROW_COPY;
8443 break;
8444 case RID_HAS_TRIVIAL_ASSIGN:
8445 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8446 break;
8447 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8448 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8449 break;
8450 case RID_HAS_TRIVIAL_COPY:
8451 kind = CPTK_HAS_TRIVIAL_COPY;
8452 break;
8453 case RID_HAS_TRIVIAL_DESTRUCTOR:
8454 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8455 break;
8456 case RID_HAS_VIRTUAL_DESTRUCTOR:
8457 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8458 break;
8459 case RID_IS_ABSTRACT:
8460 kind = CPTK_IS_ABSTRACT;
8461 break;
8462 case RID_IS_BASE_OF:
8463 kind = CPTK_IS_BASE_OF;
8464 binary = true;
8465 break;
8466 case RID_IS_CLASS:
8467 kind = CPTK_IS_CLASS;
8468 break;
8469 case RID_IS_CONVERTIBLE_TO:
8470 kind = CPTK_IS_CONVERTIBLE_TO;
8471 binary = true;
8472 break;
8473 case RID_IS_EMPTY:
8474 kind = CPTK_IS_EMPTY;
8475 break;
8476 case RID_IS_ENUM:
8477 kind = CPTK_IS_ENUM;
8478 break;
8479 case RID_IS_FINAL:
8480 kind = CPTK_IS_FINAL;
8481 break;
8482 case RID_IS_LITERAL_TYPE:
8483 kind = CPTK_IS_LITERAL_TYPE;
8484 break;
8485 case RID_IS_POD:
8486 kind = CPTK_IS_POD;
8487 break;
8488 case RID_IS_POLYMORPHIC:
8489 kind = CPTK_IS_POLYMORPHIC;
8490 break;
8491 case RID_IS_STD_LAYOUT:
8492 kind = CPTK_IS_STD_LAYOUT;
8493 break;
8494 case RID_IS_TRIVIAL:
8495 kind = CPTK_IS_TRIVIAL;
8496 break;
8497 case RID_IS_UNION:
8498 kind = CPTK_IS_UNION;
8499 break;
8500 case RID_UNDERLYING_TYPE:
8501 kind = CPTK_UNDERLYING_TYPE;
8502 break;
8503 case RID_BASES:
8504 kind = CPTK_BASES;
8505 break;
8506 case RID_DIRECT_BASES:
8507 kind = CPTK_DIRECT_BASES;
8508 break;
8509 default:
8510 gcc_unreachable ();
8513 /* Consume the token. */
8514 cp_lexer_consume_token (parser->lexer);
8516 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8518 type1 = cp_parser_type_id (parser);
8520 if (type1 == error_mark_node)
8521 return error_mark_node;
8523 /* Build a trivial decl-specifier-seq. */
8524 clear_decl_specs (&decl_specs);
8525 decl_specs.type = type1;
8527 /* Call grokdeclarator to figure out what type this is. */
8528 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8529 /*initialized=*/0, /*attrlist=*/NULL);
8531 if (binary)
8533 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8535 type2 = cp_parser_type_id (parser);
8537 if (type2 == error_mark_node)
8538 return error_mark_node;
8540 /* Build a trivial decl-specifier-seq. */
8541 clear_decl_specs (&decl_specs);
8542 decl_specs.type = type2;
8544 /* Call grokdeclarator to figure out what type this is. */
8545 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8546 /*initialized=*/0, /*attrlist=*/NULL);
8549 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8551 /* Complete the trait expression, which may mean either processing
8552 the trait expr now or saving it for template instantiation. */
8553 switch(kind)
8555 case CPTK_UNDERLYING_TYPE:
8556 return finish_underlying_type (type1);
8557 case CPTK_BASES:
8558 return finish_bases (type1, false);
8559 case CPTK_DIRECT_BASES:
8560 return finish_bases (type1, true);
8561 default:
8562 return finish_trait_expr (kind, type1, type2);
8566 /* Lambdas that appear in variable initializer or default argument scope
8567 get that in their mangling, so we need to record it. We might as well
8568 use the count for function and namespace scopes as well. */
8569 static GTY(()) tree lambda_scope;
8570 static GTY(()) int lambda_count;
8571 typedef struct GTY(()) tree_int
8573 tree t;
8574 int i;
8575 } tree_int;
8576 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8578 static void
8579 start_lambda_scope (tree decl)
8581 tree_int ti;
8582 gcc_assert (decl);
8583 /* Once we're inside a function, we ignore other scopes and just push
8584 the function again so that popping works properly. */
8585 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8586 decl = current_function_decl;
8587 ti.t = lambda_scope;
8588 ti.i = lambda_count;
8589 vec_safe_push (lambda_scope_stack, ti);
8590 if (lambda_scope != decl)
8592 /* Don't reset the count if we're still in the same function. */
8593 lambda_scope = decl;
8594 lambda_count = 0;
8598 static void
8599 record_lambda_scope (tree lambda)
8601 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8602 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8605 static void
8606 finish_lambda_scope (void)
8608 tree_int *p = &lambda_scope_stack->last ();
8609 if (lambda_scope != p->t)
8611 lambda_scope = p->t;
8612 lambda_count = p->i;
8614 lambda_scope_stack->pop ();
8617 /* Parse a lambda expression.
8619 lambda-expression:
8620 lambda-introducer lambda-declarator [opt] compound-statement
8622 Returns a representation of the expression. */
8624 static tree
8625 cp_parser_lambda_expression (cp_parser* parser)
8627 tree lambda_expr = build_lambda_expr ();
8628 tree type;
8629 bool ok;
8631 LAMBDA_EXPR_LOCATION (lambda_expr)
8632 = cp_lexer_peek_token (parser->lexer)->location;
8634 if (cp_unevaluated_operand)
8635 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8636 "lambda-expression in unevaluated context");
8638 /* We may be in the middle of deferred access check. Disable
8639 it now. */
8640 push_deferring_access_checks (dk_no_deferred);
8642 cp_parser_lambda_introducer (parser, lambda_expr);
8644 type = begin_lambda_type (lambda_expr);
8645 if (type == error_mark_node)
8646 return error_mark_node;
8648 record_lambda_scope (lambda_expr);
8650 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8651 determine_visibility (TYPE_NAME (type));
8653 /* Now that we've started the type, add the capture fields for any
8654 explicit captures. */
8655 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8658 /* Inside the class, surrounding template-parameter-lists do not apply. */
8659 unsigned int saved_num_template_parameter_lists
8660 = parser->num_template_parameter_lists;
8661 unsigned char in_statement = parser->in_statement;
8662 bool in_switch_statement_p = parser->in_switch_statement_p;
8663 bool fully_implicit_function_template_p = parser->fully_implicit_function_template_p;
8665 parser->num_template_parameter_lists = 0;
8666 parser->in_statement = 0;
8667 parser->in_switch_statement_p = false;
8668 parser->fully_implicit_function_template_p = false;
8670 /* By virtue of defining a local class, a lambda expression has access to
8671 the private variables of enclosing classes. */
8673 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8675 if (ok)
8676 cp_parser_lambda_body (parser, lambda_expr);
8677 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8678 cp_parser_skip_to_end_of_block_or_statement (parser);
8680 /* The capture list was built up in reverse order; fix that now. */
8681 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8682 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8684 if (ok)
8685 maybe_add_lambda_conv_op (type);
8687 type = finish_struct (type, /*attributes=*/NULL_TREE);
8689 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8690 parser->in_statement = in_statement;
8691 parser->in_switch_statement_p = in_switch_statement_p;
8692 parser->fully_implicit_function_template_p = fully_implicit_function_template_p;
8695 pop_deferring_access_checks ();
8697 /* This field is only used during parsing of the lambda. */
8698 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8700 /* This lambda shouldn't have any proxies left at this point. */
8701 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8702 /* And now that we're done, push proxies for an enclosing lambda. */
8703 insert_pending_capture_proxies ();
8705 if (ok)
8706 return build_lambda_object (lambda_expr);
8707 else
8708 return error_mark_node;
8711 /* Parse the beginning of a lambda expression.
8713 lambda-introducer:
8714 [ lambda-capture [opt] ]
8716 LAMBDA_EXPR is the current representation of the lambda expression. */
8718 static void
8719 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8721 /* Need commas after the first capture. */
8722 bool first = true;
8724 /* Eat the leading `['. */
8725 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8727 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8728 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8729 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8730 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8731 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8732 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8734 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8736 cp_lexer_consume_token (parser->lexer);
8737 first = false;
8740 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8742 cp_token* capture_token;
8743 tree capture_id;
8744 tree capture_init_expr;
8745 cp_id_kind idk = CP_ID_KIND_NONE;
8746 bool explicit_init_p = false;
8748 enum capture_kind_type
8750 BY_COPY,
8751 BY_REFERENCE
8753 enum capture_kind_type capture_kind = BY_COPY;
8755 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8757 error ("expected end of capture-list");
8758 return;
8761 if (first)
8762 first = false;
8763 else
8764 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8766 /* Possibly capture `this'. */
8767 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8769 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8770 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8771 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8772 "with by-copy capture default");
8773 cp_lexer_consume_token (parser->lexer);
8774 add_capture (lambda_expr,
8775 /*id=*/this_identifier,
8776 /*initializer=*/finish_this_expr(),
8777 /*by_reference_p=*/false,
8778 explicit_init_p);
8779 continue;
8782 /* Remember whether we want to capture as a reference or not. */
8783 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8785 capture_kind = BY_REFERENCE;
8786 cp_lexer_consume_token (parser->lexer);
8789 /* Get the identifier. */
8790 capture_token = cp_lexer_peek_token (parser->lexer);
8791 capture_id = cp_parser_identifier (parser);
8793 if (capture_id == error_mark_node)
8794 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8795 delimiters, but I modified this to stop on unnested ']' as well. It
8796 was already changed to stop on unnested '}', so the
8797 "closing_parenthesis" name is no more misleading with my change. */
8799 cp_parser_skip_to_closing_parenthesis (parser,
8800 /*recovering=*/true,
8801 /*or_comma=*/true,
8802 /*consume_paren=*/true);
8803 break;
8806 /* Find the initializer for this capture. */
8807 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
8808 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
8809 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8811 bool direct, non_constant;
8812 /* An explicit initializer exists. */
8813 if (cxx_dialect < cxx1y)
8814 pedwarn (input_location, 0,
8815 "lambda capture initializers "
8816 "only available with -std=c++1y or -std=gnu++1y");
8817 capture_init_expr = cp_parser_initializer (parser, &direct,
8818 &non_constant);
8819 explicit_init_p = true;
8821 else
8823 const char* error_msg;
8825 /* Turn the identifier into an id-expression. */
8826 capture_init_expr
8827 = cp_parser_lookup_name_simple (parser, capture_id,
8828 capture_token->location);
8830 if (capture_init_expr == error_mark_node)
8832 unqualified_name_lookup_error (capture_id);
8833 continue;
8835 else if (DECL_P (capture_init_expr)
8836 && (!VAR_P (capture_init_expr)
8837 && TREE_CODE (capture_init_expr) != PARM_DECL))
8839 error_at (capture_token->location,
8840 "capture of non-variable %qD ",
8841 capture_init_expr);
8842 inform (0, "%q+#D declared here", capture_init_expr);
8843 continue;
8845 if (VAR_P (capture_init_expr)
8846 && decl_storage_duration (capture_init_expr) != dk_auto)
8848 pedwarn (capture_token->location, 0, "capture of variable "
8849 "%qD with non-automatic storage duration",
8850 capture_init_expr);
8851 inform (0, "%q+#D declared here", capture_init_expr);
8852 continue;
8855 capture_init_expr
8856 = finish_id_expression
8857 (capture_id,
8858 capture_init_expr,
8859 parser->scope,
8860 &idk,
8861 /*integral_constant_expression_p=*/false,
8862 /*allow_non_integral_constant_expression_p=*/false,
8863 /*non_integral_constant_expression_p=*/NULL,
8864 /*template_p=*/false,
8865 /*done=*/true,
8866 /*address_p=*/false,
8867 /*template_arg_p=*/false,
8868 &error_msg,
8869 capture_token->location);
8871 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8873 cp_lexer_consume_token (parser->lexer);
8874 capture_init_expr = make_pack_expansion (capture_init_expr);
8876 else
8877 check_for_bare_parameter_packs (capture_init_expr);
8880 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8881 && !explicit_init_p)
8883 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8884 && capture_kind == BY_COPY)
8885 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8886 "of %qD redundant with by-copy capture default",
8887 capture_id);
8888 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8889 && capture_kind == BY_REFERENCE)
8890 pedwarn (capture_token->location, 0, "explicit by-reference "
8891 "capture of %qD redundant with by-reference capture "
8892 "default", capture_id);
8895 add_capture (lambda_expr,
8896 capture_id,
8897 capture_init_expr,
8898 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8899 explicit_init_p);
8902 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8905 /* Parse the (optional) middle of a lambda expression.
8907 lambda-declarator:
8908 < template-parameter-list [opt] >
8909 ( parameter-declaration-clause [opt] )
8910 attribute-specifier [opt]
8911 mutable [opt]
8912 exception-specification [opt]
8913 lambda-return-type-clause [opt]
8915 LAMBDA_EXPR is the current representation of the lambda expression. */
8917 static bool
8918 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8920 /* 5.1.1.4 of the standard says:
8921 If a lambda-expression does not include a lambda-declarator, it is as if
8922 the lambda-declarator were ().
8923 This means an empty parameter list, no attributes, and no exception
8924 specification. */
8925 tree param_list = void_list_node;
8926 tree attributes = NULL_TREE;
8927 tree exception_spec = NULL_TREE;
8928 tree template_param_list = NULL_TREE;
8930 /* The template-parameter-list is optional, but must begin with
8931 an opening angle if present. */
8932 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8934 if (cxx_dialect < cxx1y)
8935 pedwarn (parser->lexer->next_token->location, 0,
8936 "lambda templates are only available with "
8937 "-std=c++1y or -std=gnu++1y");
8939 cp_lexer_consume_token (parser->lexer);
8941 template_param_list = cp_parser_template_parameter_list (parser);
8943 cp_parser_skip_to_end_of_template_parameter_list (parser);
8945 /* We just processed one more parameter list. */
8946 ++parser->num_template_parameter_lists;
8949 /* The parameter-declaration-clause is optional (unless
8950 template-parameter-list was given), but must begin with an
8951 opening parenthesis if present. */
8952 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8954 cp_lexer_consume_token (parser->lexer);
8956 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8958 /* Parse parameters. */
8959 param_list = cp_parser_parameter_declaration_clause (parser);
8961 /* Default arguments shall not be specified in the
8962 parameter-declaration-clause of a lambda-declarator. */
8963 for (tree t = param_list; t; t = TREE_CHAIN (t))
8964 if (TREE_PURPOSE (t))
8965 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8966 "default argument specified for lambda parameter");
8968 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8970 attributes = cp_parser_attributes_opt (parser);
8972 /* Parse optional `mutable' keyword. */
8973 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8975 cp_lexer_consume_token (parser->lexer);
8976 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8979 /* Parse optional exception specification. */
8980 exception_spec = cp_parser_exception_specification_opt (parser);
8982 /* Parse optional trailing return type. */
8983 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8985 cp_lexer_consume_token (parser->lexer);
8986 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8987 = cp_parser_trailing_type_id (parser);
8990 /* The function parameters must be in scope all the way until after the
8991 trailing-return-type in case of decltype. */
8992 pop_bindings_and_leave_scope ();
8994 else if (template_param_list != NULL_TREE) // generate diagnostic
8995 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8997 /* Create the function call operator.
8999 Messing with declarators like this is no uglier than building up the
9000 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9001 other code. */
9003 cp_decl_specifier_seq return_type_specs;
9004 cp_declarator* declarator;
9005 tree fco;
9006 int quals;
9007 void *p;
9009 clear_decl_specs (&return_type_specs);
9010 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9011 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9012 else
9013 /* Maybe we will deduce the return type later. */
9014 return_type_specs.type = make_auto ();
9016 p = obstack_alloc (&declarator_obstack, 0);
9018 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9019 sfk_none);
9021 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9022 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9023 declarator = make_call_declarator (declarator, param_list, quals,
9024 VIRT_SPEC_UNSPECIFIED,
9025 REF_QUAL_NONE,
9026 exception_spec,
9027 /*late_return_type=*/NULL_TREE);
9028 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9030 fco = grokmethod (&return_type_specs,
9031 declarator,
9032 attributes);
9033 if (fco != error_mark_node)
9035 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9036 DECL_ARTIFICIAL (fco) = 1;
9037 /* Give the object parameter a different name. */
9038 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9039 if (template_param_list)
9041 fco = finish_member_template_decl (fco);
9042 finish_template_decl (template_param_list);
9043 --parser->num_template_parameter_lists;
9045 else if (parser->fully_implicit_function_template_p)
9046 fco = finish_fully_implicit_template (parser, fco);
9049 finish_member_declaration (fco);
9051 obstack_free (&declarator_obstack, p);
9053 return (fco != error_mark_node);
9057 /* Parse the body of a lambda expression, which is simply
9059 compound-statement
9061 but which requires special handling.
9062 LAMBDA_EXPR is the current representation of the lambda expression. */
9064 static void
9065 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9067 bool nested = (current_function_decl != NULL_TREE);
9068 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9069 if (nested)
9070 push_function_context ();
9071 else
9072 /* Still increment function_depth so that we don't GC in the
9073 middle of an expression. */
9074 ++function_depth;
9075 /* Clear this in case we're in the middle of a default argument. */
9076 parser->local_variables_forbidden_p = false;
9078 /* Finish the function call operator
9079 - class_specifier
9080 + late_parsing_for_member
9081 + function_definition_after_declarator
9082 + ctor_initializer_opt_and_function_body */
9084 tree fco = lambda_function (lambda_expr);
9085 tree body;
9086 bool done = false;
9087 tree compound_stmt;
9088 tree cap;
9090 /* Let the front end know that we are going to be defining this
9091 function. */
9092 start_preparsed_function (fco,
9093 NULL_TREE,
9094 SF_PRE_PARSED | SF_INCLASS_INLINE);
9096 start_lambda_scope (fco);
9097 body = begin_function_body ();
9099 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9100 goto out;
9102 /* Push the proxies for any explicit captures. */
9103 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9104 cap = TREE_CHAIN (cap))
9105 build_capture_proxy (TREE_PURPOSE (cap));
9107 compound_stmt = begin_compound_stmt (0);
9109 /* 5.1.1.4 of the standard says:
9110 If a lambda-expression does not include a trailing-return-type, it
9111 is as if the trailing-return-type denotes the following type:
9112 * if the compound-statement is of the form
9113 { return attribute-specifier [opt] expression ; }
9114 the type of the returned expression after lvalue-to-rvalue
9115 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9116 (_conv.array_ 4.2), and function-to-pointer conversion
9117 (_conv.func_ 4.3);
9118 * otherwise, void. */
9120 /* In a lambda that has neither a lambda-return-type-clause
9121 nor a deducible form, errors should be reported for return statements
9122 in the body. Since we used void as the placeholder return type, parsing
9123 the body as usual will give such desired behavior. */
9124 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9125 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9126 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9128 tree expr = NULL_TREE;
9129 cp_id_kind idk = CP_ID_KIND_NONE;
9131 /* Parse tentatively in case there's more after the initial return
9132 statement. */
9133 cp_parser_parse_tentatively (parser);
9135 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9137 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
9139 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9140 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9142 if (cp_parser_parse_definitely (parser))
9144 if (!processing_template_decl)
9145 apply_deduced_return_type (fco, lambda_return_type (expr));
9147 /* Will get error here if type not deduced yet. */
9148 finish_return_stmt (expr);
9150 done = true;
9154 if (!done)
9156 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9157 cp_parser_label_declaration (parser);
9158 cp_parser_statement_seq_opt (parser, NULL_TREE);
9159 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9162 finish_compound_stmt (compound_stmt);
9164 out:
9165 finish_function_body (body);
9166 finish_lambda_scope ();
9168 /* Finish the function and generate code for it if necessary. */
9169 tree fn = finish_function (/*inline*/2);
9171 /* Only expand if the call op is not a template. */
9172 if (!DECL_TEMPLATE_INFO (fco))
9173 expand_or_defer_fn (fn);
9176 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9177 if (nested)
9178 pop_function_context();
9179 else
9180 --function_depth;
9183 /* Statements [gram.stmt.stmt] */
9185 /* Parse a statement.
9187 statement:
9188 labeled-statement
9189 expression-statement
9190 compound-statement
9191 selection-statement
9192 iteration-statement
9193 jump-statement
9194 declaration-statement
9195 try-block
9197 C++11:
9199 statement:
9200 labeled-statement
9201 attribute-specifier-seq (opt) expression-statement
9202 attribute-specifier-seq (opt) compound-statement
9203 attribute-specifier-seq (opt) selection-statement
9204 attribute-specifier-seq (opt) iteration-statement
9205 attribute-specifier-seq (opt) jump-statement
9206 declaration-statement
9207 attribute-specifier-seq (opt) try-block
9209 TM Extension:
9211 statement:
9212 atomic-statement
9214 IN_COMPOUND is true when the statement is nested inside a
9215 cp_parser_compound_statement; this matters for certain pragmas.
9217 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9218 is a (possibly labeled) if statement which is not enclosed in braces
9219 and has an else clause. This is used to implement -Wparentheses. */
9221 static void
9222 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9223 bool in_compound, bool *if_p)
9225 tree statement, std_attrs = NULL_TREE;
9226 cp_token *token;
9227 location_t statement_location, attrs_location;
9229 restart:
9230 if (if_p != NULL)
9231 *if_p = false;
9232 /* There is no statement yet. */
9233 statement = NULL_TREE;
9235 cp_lexer_save_tokens (parser->lexer);
9236 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9237 if (c_dialect_objc ())
9238 /* In obj-c++, seeing '[[' might be the either the beginning of
9239 c++11 attributes, or a nested objc-message-expression. So
9240 let's parse the c++11 attributes tentatively. */
9241 cp_parser_parse_tentatively (parser);
9242 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9243 if (c_dialect_objc ())
9245 if (!cp_parser_parse_definitely (parser))
9246 std_attrs = NULL_TREE;
9249 /* Peek at the next token. */
9250 token = cp_lexer_peek_token (parser->lexer);
9251 /* Remember the location of the first token in the statement. */
9252 statement_location = token->location;
9253 /* If this is a keyword, then that will often determine what kind of
9254 statement we have. */
9255 if (token->type == CPP_KEYWORD)
9257 enum rid keyword = token->keyword;
9259 switch (keyword)
9261 case RID_CASE:
9262 case RID_DEFAULT:
9263 /* Looks like a labeled-statement with a case label.
9264 Parse the label, and then use tail recursion to parse
9265 the statement. */
9266 cp_parser_label_for_labeled_statement (parser, std_attrs);
9267 goto restart;
9269 case RID_IF:
9270 case RID_SWITCH:
9271 statement = cp_parser_selection_statement (parser, if_p);
9272 break;
9274 case RID_WHILE:
9275 case RID_DO:
9276 case RID_FOR:
9277 statement = cp_parser_iteration_statement (parser, false);
9278 break;
9280 case RID_BREAK:
9281 case RID_CONTINUE:
9282 case RID_RETURN:
9283 case RID_GOTO:
9284 statement = cp_parser_jump_statement (parser);
9285 break;
9287 /* Objective-C++ exception-handling constructs. */
9288 case RID_AT_TRY:
9289 case RID_AT_CATCH:
9290 case RID_AT_FINALLY:
9291 case RID_AT_SYNCHRONIZED:
9292 case RID_AT_THROW:
9293 statement = cp_parser_objc_statement (parser);
9294 break;
9296 case RID_TRY:
9297 statement = cp_parser_try_block (parser);
9298 break;
9300 case RID_NAMESPACE:
9301 /* This must be a namespace alias definition. */
9302 cp_parser_declaration_statement (parser);
9303 return;
9305 case RID_TRANSACTION_ATOMIC:
9306 case RID_TRANSACTION_RELAXED:
9307 statement = cp_parser_transaction (parser, keyword);
9308 break;
9309 case RID_TRANSACTION_CANCEL:
9310 statement = cp_parser_transaction_cancel (parser);
9311 break;
9313 default:
9314 /* It might be a keyword like `int' that can start a
9315 declaration-statement. */
9316 break;
9319 else if (token->type == CPP_NAME)
9321 /* If the next token is a `:', then we are looking at a
9322 labeled-statement. */
9323 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9324 if (token->type == CPP_COLON)
9326 /* Looks like a labeled-statement with an ordinary label.
9327 Parse the label, and then use tail recursion to parse
9328 the statement. */
9330 cp_parser_label_for_labeled_statement (parser, std_attrs);
9331 goto restart;
9334 /* Anything that starts with a `{' must be a compound-statement. */
9335 else if (token->type == CPP_OPEN_BRACE)
9336 statement = cp_parser_compound_statement (parser, NULL, false, false);
9337 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9338 a statement all its own. */
9339 else if (token->type == CPP_PRAGMA)
9341 /* Only certain OpenMP pragmas are attached to statements, and thus
9342 are considered statements themselves. All others are not. In
9343 the context of a compound, accept the pragma as a "statement" and
9344 return so that we can check for a close brace. Otherwise we
9345 require a real statement and must go back and read one. */
9346 if (in_compound)
9347 cp_parser_pragma (parser, pragma_compound);
9348 else if (!cp_parser_pragma (parser, pragma_stmt))
9349 goto restart;
9350 return;
9352 else if (token->type == CPP_EOF)
9354 cp_parser_error (parser, "expected statement");
9355 return;
9358 /* Everything else must be a declaration-statement or an
9359 expression-statement. Try for the declaration-statement
9360 first, unless we are looking at a `;', in which case we know that
9361 we have an expression-statement. */
9362 if (!statement)
9364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9366 if (std_attrs != NULL_TREE)
9368 /* Attributes should be parsed as part of the the
9369 declaration, so let's un-parse them. */
9370 cp_lexer_rollback_tokens (parser->lexer);
9371 std_attrs = NULL_TREE;
9374 cp_parser_parse_tentatively (parser);
9375 /* Try to parse the declaration-statement. */
9376 cp_parser_declaration_statement (parser);
9377 /* If that worked, we're done. */
9378 if (cp_parser_parse_definitely (parser))
9379 return;
9381 /* Look for an expression-statement instead. */
9382 statement = cp_parser_expression_statement (parser, in_statement_expr);
9385 /* Set the line number for the statement. */
9386 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9387 SET_EXPR_LOCATION (statement, statement_location);
9389 /* Note that for now, we don't do anything with c++11 statements
9390 parsed at this level. */
9391 if (std_attrs != NULL_TREE)
9392 warning_at (attrs_location,
9393 OPT_Wattributes,
9394 "attributes at the beginning of statement are ignored");
9397 /* Parse the label for a labeled-statement, i.e.
9399 identifier :
9400 case constant-expression :
9401 default :
9403 GNU Extension:
9404 case constant-expression ... constant-expression : statement
9406 When a label is parsed without errors, the label is added to the
9407 parse tree by the finish_* functions, so this function doesn't
9408 have to return the label. */
9410 static void
9411 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9413 cp_token *token;
9414 tree label = NULL_TREE;
9415 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9417 /* The next token should be an identifier. */
9418 token = cp_lexer_peek_token (parser->lexer);
9419 if (token->type != CPP_NAME
9420 && token->type != CPP_KEYWORD)
9422 cp_parser_error (parser, "expected labeled-statement");
9423 return;
9426 parser->colon_corrects_to_scope_p = false;
9427 switch (token->keyword)
9429 case RID_CASE:
9431 tree expr, expr_hi;
9432 cp_token *ellipsis;
9434 /* Consume the `case' token. */
9435 cp_lexer_consume_token (parser->lexer);
9436 /* Parse the constant-expression. */
9437 expr = cp_parser_constant_expression (parser,
9438 /*allow_non_constant_p=*/false,
9439 NULL);
9441 ellipsis = cp_lexer_peek_token (parser->lexer);
9442 if (ellipsis->type == CPP_ELLIPSIS)
9444 /* Consume the `...' token. */
9445 cp_lexer_consume_token (parser->lexer);
9446 expr_hi =
9447 cp_parser_constant_expression (parser,
9448 /*allow_non_constant_p=*/false,
9449 NULL);
9450 /* We don't need to emit warnings here, as the common code
9451 will do this for us. */
9453 else
9454 expr_hi = NULL_TREE;
9456 if (parser->in_switch_statement_p)
9457 finish_case_label (token->location, expr, expr_hi);
9458 else
9459 error_at (token->location,
9460 "case label %qE not within a switch statement",
9461 expr);
9463 break;
9465 case RID_DEFAULT:
9466 /* Consume the `default' token. */
9467 cp_lexer_consume_token (parser->lexer);
9469 if (parser->in_switch_statement_p)
9470 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9471 else
9472 error_at (token->location, "case label not within a switch statement");
9473 break;
9475 default:
9476 /* Anything else must be an ordinary label. */
9477 label = finish_label_stmt (cp_parser_identifier (parser));
9478 break;
9481 /* Require the `:' token. */
9482 cp_parser_require (parser, CPP_COLON, RT_COLON);
9484 /* An ordinary label may optionally be followed by attributes.
9485 However, this is only permitted if the attributes are then
9486 followed by a semicolon. This is because, for backward
9487 compatibility, when parsing
9488 lab: __attribute__ ((unused)) int i;
9489 we want the attribute to attach to "i", not "lab". */
9490 if (label != NULL_TREE
9491 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9493 tree attrs;
9494 cp_parser_parse_tentatively (parser);
9495 attrs = cp_parser_gnu_attributes_opt (parser);
9496 if (attrs == NULL_TREE
9497 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9498 cp_parser_abort_tentative_parse (parser);
9499 else if (!cp_parser_parse_definitely (parser))
9501 else
9502 attributes = chainon (attributes, attrs);
9505 if (attributes != NULL_TREE)
9506 cplus_decl_attributes (&label, attributes, 0);
9508 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9511 /* Parse an expression-statement.
9513 expression-statement:
9514 expression [opt] ;
9516 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9517 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9518 indicates whether this expression-statement is part of an
9519 expression statement. */
9521 static tree
9522 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9524 tree statement = NULL_TREE;
9525 cp_token *token = cp_lexer_peek_token (parser->lexer);
9527 /* If the next token is a ';', then there is no expression
9528 statement. */
9529 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9531 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9532 if (statement == error_mark_node
9533 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9535 cp_parser_skip_to_end_of_block_or_statement (parser);
9536 return error_mark_node;
9540 /* Give a helpful message for "A<T>::type t;" and the like. */
9541 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9542 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9544 if (TREE_CODE (statement) == SCOPE_REF)
9545 error_at (token->location, "need %<typename%> before %qE because "
9546 "%qT is a dependent scope",
9547 statement, TREE_OPERAND (statement, 0));
9548 else if (is_overloaded_fn (statement)
9549 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9551 /* A::A a; */
9552 tree fn = get_first_fn (statement);
9553 error_at (token->location,
9554 "%<%T::%D%> names the constructor, not the type",
9555 DECL_CONTEXT (fn), DECL_NAME (fn));
9559 /* Consume the final `;'. */
9560 cp_parser_consume_semicolon_at_end_of_statement (parser);
9562 if (in_statement_expr
9563 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9564 /* This is the final expression statement of a statement
9565 expression. */
9566 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9567 else if (statement)
9568 statement = finish_expr_stmt (statement);
9570 return statement;
9573 /* Parse a compound-statement.
9575 compound-statement:
9576 { statement-seq [opt] }
9578 GNU extension:
9580 compound-statement:
9581 { label-declaration-seq [opt] statement-seq [opt] }
9583 label-declaration-seq:
9584 label-declaration
9585 label-declaration-seq label-declaration
9587 Returns a tree representing the statement. */
9589 static tree
9590 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9591 bool in_try, bool function_body)
9593 tree compound_stmt;
9595 /* Consume the `{'. */
9596 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9597 return error_mark_node;
9598 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9599 && !function_body)
9600 pedwarn (input_location, OPT_Wpedantic,
9601 "compound-statement in constexpr function");
9602 /* Begin the compound-statement. */
9603 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9604 /* If the next keyword is `__label__' we have a label declaration. */
9605 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9606 cp_parser_label_declaration (parser);
9607 /* Parse an (optional) statement-seq. */
9608 cp_parser_statement_seq_opt (parser, in_statement_expr);
9609 /* Finish the compound-statement. */
9610 finish_compound_stmt (compound_stmt);
9611 /* Consume the `}'. */
9612 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9614 return compound_stmt;
9617 /* Parse an (optional) statement-seq.
9619 statement-seq:
9620 statement
9621 statement-seq [opt] statement */
9623 static void
9624 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9626 /* Scan statements until there aren't any more. */
9627 while (true)
9629 cp_token *token = cp_lexer_peek_token (parser->lexer);
9631 /* If we are looking at a `}', then we have run out of
9632 statements; the same is true if we have reached the end
9633 of file, or have stumbled upon a stray '@end'. */
9634 if (token->type == CPP_CLOSE_BRACE
9635 || token->type == CPP_EOF
9636 || token->type == CPP_PRAGMA_EOL
9637 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9638 break;
9640 /* If we are in a compound statement and find 'else' then
9641 something went wrong. */
9642 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9644 if (parser->in_statement & IN_IF_STMT)
9645 break;
9646 else
9648 token = cp_lexer_consume_token (parser->lexer);
9649 error_at (token->location, "%<else%> without a previous %<if%>");
9653 /* Parse the statement. */
9654 cp_parser_statement (parser, in_statement_expr, true, NULL);
9658 /* Parse a selection-statement.
9660 selection-statement:
9661 if ( condition ) statement
9662 if ( condition ) statement else statement
9663 switch ( condition ) statement
9665 Returns the new IF_STMT or SWITCH_STMT.
9667 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9668 is a (possibly labeled) if statement which is not enclosed in
9669 braces and has an else clause. This is used to implement
9670 -Wparentheses. */
9672 static tree
9673 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9675 cp_token *token;
9676 enum rid keyword;
9678 if (if_p != NULL)
9679 *if_p = false;
9681 /* Peek at the next token. */
9682 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9684 /* See what kind of keyword it is. */
9685 keyword = token->keyword;
9686 switch (keyword)
9688 case RID_IF:
9689 case RID_SWITCH:
9691 tree statement;
9692 tree condition;
9694 /* Look for the `('. */
9695 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9697 cp_parser_skip_to_end_of_statement (parser);
9698 return error_mark_node;
9701 /* Begin the selection-statement. */
9702 if (keyword == RID_IF)
9703 statement = begin_if_stmt ();
9704 else
9705 statement = begin_switch_stmt ();
9707 /* Parse the condition. */
9708 condition = cp_parser_condition (parser);
9709 /* Look for the `)'. */
9710 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9711 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9712 /*consume_paren=*/true);
9714 if (keyword == RID_IF)
9716 bool nested_if;
9717 unsigned char in_statement;
9719 /* Add the condition. */
9720 finish_if_stmt_cond (condition, statement);
9722 /* Parse the then-clause. */
9723 in_statement = parser->in_statement;
9724 parser->in_statement |= IN_IF_STMT;
9725 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9727 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9728 add_stmt (build_empty_stmt (loc));
9729 cp_lexer_consume_token (parser->lexer);
9730 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9731 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9732 "empty body in an %<if%> statement");
9733 nested_if = false;
9735 else
9736 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9737 parser->in_statement = in_statement;
9739 finish_then_clause (statement);
9741 /* If the next token is `else', parse the else-clause. */
9742 if (cp_lexer_next_token_is_keyword (parser->lexer,
9743 RID_ELSE))
9745 /* Consume the `else' keyword. */
9746 cp_lexer_consume_token (parser->lexer);
9747 begin_else_clause (statement);
9748 /* Parse the else-clause. */
9749 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9751 location_t loc;
9752 loc = cp_lexer_peek_token (parser->lexer)->location;
9753 warning_at (loc,
9754 OPT_Wempty_body, "suggest braces around "
9755 "empty body in an %<else%> statement");
9756 add_stmt (build_empty_stmt (loc));
9757 cp_lexer_consume_token (parser->lexer);
9759 else
9760 cp_parser_implicitly_scoped_statement (parser, NULL);
9762 finish_else_clause (statement);
9764 /* If we are currently parsing a then-clause, then
9765 IF_P will not be NULL. We set it to true to
9766 indicate that this if statement has an else clause.
9767 This may trigger the Wparentheses warning below
9768 when we get back up to the parent if statement. */
9769 if (if_p != NULL)
9770 *if_p = true;
9772 else
9774 /* This if statement does not have an else clause. If
9775 NESTED_IF is true, then the then-clause is an if
9776 statement which does have an else clause. We warn
9777 about the potential ambiguity. */
9778 if (nested_if)
9779 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9780 "suggest explicit braces to avoid ambiguous"
9781 " %<else%>");
9784 /* Now we're all done with the if-statement. */
9785 finish_if_stmt (statement);
9787 else
9789 bool in_switch_statement_p;
9790 unsigned char in_statement;
9792 /* Add the condition. */
9793 finish_switch_cond (condition, statement);
9795 /* Parse the body of the switch-statement. */
9796 in_switch_statement_p = parser->in_switch_statement_p;
9797 in_statement = parser->in_statement;
9798 parser->in_switch_statement_p = true;
9799 parser->in_statement |= IN_SWITCH_STMT;
9800 cp_parser_implicitly_scoped_statement (parser, NULL);
9801 parser->in_switch_statement_p = in_switch_statement_p;
9802 parser->in_statement = in_statement;
9804 /* Now we're all done with the switch-statement. */
9805 finish_switch_stmt (statement);
9808 return statement;
9810 break;
9812 default:
9813 cp_parser_error (parser, "expected selection-statement");
9814 return error_mark_node;
9818 /* Parse a condition.
9820 condition:
9821 expression
9822 type-specifier-seq declarator = initializer-clause
9823 type-specifier-seq declarator braced-init-list
9825 GNU Extension:
9827 condition:
9828 type-specifier-seq declarator asm-specification [opt]
9829 attributes [opt] = assignment-expression
9831 Returns the expression that should be tested. */
9833 static tree
9834 cp_parser_condition (cp_parser* parser)
9836 cp_decl_specifier_seq type_specifiers;
9837 const char *saved_message;
9838 int declares_class_or_enum;
9840 /* Try the declaration first. */
9841 cp_parser_parse_tentatively (parser);
9842 /* New types are not allowed in the type-specifier-seq for a
9843 condition. */
9844 saved_message = parser->type_definition_forbidden_message;
9845 parser->type_definition_forbidden_message
9846 = G_("types may not be defined in conditions");
9847 /* Parse the type-specifier-seq. */
9848 cp_parser_decl_specifier_seq (parser,
9849 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9850 &type_specifiers,
9851 &declares_class_or_enum);
9852 /* Restore the saved message. */
9853 parser->type_definition_forbidden_message = saved_message;
9854 /* If all is well, we might be looking at a declaration. */
9855 if (!cp_parser_error_occurred (parser))
9857 tree decl;
9858 tree asm_specification;
9859 tree attributes;
9860 cp_declarator *declarator;
9861 tree initializer = NULL_TREE;
9863 /* Parse the declarator. */
9864 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9865 /*ctor_dtor_or_conv_p=*/NULL,
9866 /*parenthesized_p=*/NULL,
9867 /*member_p=*/false);
9868 /* Parse the attributes. */
9869 attributes = cp_parser_attributes_opt (parser);
9870 /* Parse the asm-specification. */
9871 asm_specification = cp_parser_asm_specification_opt (parser);
9872 /* If the next token is not an `=' or '{', then we might still be
9873 looking at an expression. For example:
9875 if (A(a).x)
9877 looks like a decl-specifier-seq and a declarator -- but then
9878 there is no `=', so this is an expression. */
9879 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9880 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9881 cp_parser_simulate_error (parser);
9883 /* If we did see an `=' or '{', then we are looking at a declaration
9884 for sure. */
9885 if (cp_parser_parse_definitely (parser))
9887 tree pushed_scope;
9888 bool non_constant_p;
9889 bool flags = LOOKUP_ONLYCONVERTING;
9891 /* Create the declaration. */
9892 decl = start_decl (declarator, &type_specifiers,
9893 /*initialized_p=*/true,
9894 attributes, /*prefix_attributes=*/NULL_TREE,
9895 &pushed_scope);
9897 /* Parse the initializer. */
9898 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9900 initializer = cp_parser_braced_list (parser, &non_constant_p);
9901 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9902 flags = 0;
9904 else
9906 /* Consume the `='. */
9907 cp_parser_require (parser, CPP_EQ, RT_EQ);
9908 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9910 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9911 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9913 /* Process the initializer. */
9914 cp_finish_decl (decl,
9915 initializer, !non_constant_p,
9916 asm_specification,
9917 flags);
9919 if (pushed_scope)
9920 pop_scope (pushed_scope);
9922 return convert_from_reference (decl);
9925 /* If we didn't even get past the declarator successfully, we are
9926 definitely not looking at a declaration. */
9927 else
9928 cp_parser_abort_tentative_parse (parser);
9930 /* Otherwise, we are looking at an expression. */
9931 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9934 /* Parses a for-statement or range-for-statement until the closing ')',
9935 not included. */
9937 static tree
9938 cp_parser_for (cp_parser *parser, bool ivdep)
9940 tree init, scope, decl;
9941 bool is_range_for;
9943 /* Begin the for-statement. */
9944 scope = begin_for_scope (&init);
9946 /* Parse the initialization. */
9947 is_range_for = cp_parser_for_init_statement (parser, &decl);
9949 if (is_range_for)
9950 return cp_parser_range_for (parser, scope, init, decl, ivdep);
9951 else
9952 return cp_parser_c_for (parser, scope, init, ivdep);
9955 static tree
9956 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
9958 /* Normal for loop */
9959 tree condition = NULL_TREE;
9960 tree expression = NULL_TREE;
9961 tree stmt;
9963 stmt = begin_for_stmt (scope, init);
9964 /* The for-init-statement has already been parsed in
9965 cp_parser_for_init_statement, so no work is needed here. */
9966 finish_for_init_stmt (stmt);
9968 /* If there's a condition, process it. */
9969 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9970 condition = cp_parser_condition (parser);
9971 else if (ivdep)
9973 cp_parser_error (parser, "missing loop condition in loop with "
9974 "%<GCC ivdep%> pragma");
9975 condition = error_mark_node;
9977 finish_for_cond (condition, stmt, ivdep);
9978 /* Look for the `;'. */
9979 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9981 /* If there's an expression, process it. */
9982 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9983 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9984 finish_for_expr (expression, stmt);
9986 return stmt;
9989 /* Tries to parse a range-based for-statement:
9991 range-based-for:
9992 decl-specifier-seq declarator : expression
9994 The decl-specifier-seq declarator and the `:' are already parsed by
9995 cp_parser_for_init_statement. If processing_template_decl it returns a
9996 newly created RANGE_FOR_STMT; if not, it is converted to a
9997 regular FOR_STMT. */
9999 static tree
10000 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10001 bool ivdep)
10003 tree stmt, range_expr;
10005 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10007 bool expr_non_constant_p;
10008 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10010 else
10011 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10013 /* If in template, STMT is converted to a normal for-statement
10014 at instantiation. If not, it is done just ahead. */
10015 if (processing_template_decl)
10017 if (check_for_bare_parameter_packs (range_expr))
10018 range_expr = error_mark_node;
10019 stmt = begin_range_for_stmt (scope, init);
10020 if (ivdep)
10021 RANGE_FOR_IVDEP (stmt) = 1;
10022 finish_range_for_decl (stmt, range_decl, range_expr);
10023 if (!type_dependent_expression_p (range_expr)
10024 /* do_auto_deduction doesn't mess with template init-lists. */
10025 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10026 do_range_for_auto_deduction (range_decl, range_expr);
10028 else
10030 stmt = begin_for_stmt (scope, init);
10031 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10033 return stmt;
10036 /* Subroutine of cp_convert_range_for: given the initializer expression,
10037 builds up the range temporary. */
10039 static tree
10040 build_range_temp (tree range_expr)
10042 tree range_type, range_temp;
10044 /* Find out the type deduced by the declaration
10045 `auto &&__range = range_expr'. */
10046 range_type = cp_build_reference_type (make_auto (), true);
10047 range_type = do_auto_deduction (range_type, range_expr,
10048 type_uses_auto (range_type));
10050 /* Create the __range variable. */
10051 range_temp = build_decl (input_location, VAR_DECL,
10052 get_identifier ("__for_range"), range_type);
10053 TREE_USED (range_temp) = 1;
10054 DECL_ARTIFICIAL (range_temp) = 1;
10056 return range_temp;
10059 /* Used by cp_parser_range_for in template context: we aren't going to
10060 do a full conversion yet, but we still need to resolve auto in the
10061 type of the for-range-declaration if present. This is basically
10062 a shortcut version of cp_convert_range_for. */
10064 static void
10065 do_range_for_auto_deduction (tree decl, tree range_expr)
10067 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10068 if (auto_node)
10070 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10071 range_temp = convert_from_reference (build_range_temp (range_expr));
10072 iter_type = (cp_parser_perform_range_for_lookup
10073 (range_temp, &begin_dummy, &end_dummy));
10074 if (iter_type)
10076 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10077 iter_type);
10078 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10079 tf_warning_or_error);
10080 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10081 iter_decl, auto_node);
10086 /* Converts a range-based for-statement into a normal
10087 for-statement, as per the definition.
10089 for (RANGE_DECL : RANGE_EXPR)
10090 BLOCK
10092 should be equivalent to:
10095 auto &&__range = RANGE_EXPR;
10096 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10097 __begin != __end;
10098 ++__begin)
10100 RANGE_DECL = *__begin;
10101 BLOCK
10105 If RANGE_EXPR is an array:
10106 BEGIN_EXPR = __range
10107 END_EXPR = __range + ARRAY_SIZE(__range)
10108 Else if RANGE_EXPR has a member 'begin' or 'end':
10109 BEGIN_EXPR = __range.begin()
10110 END_EXPR = __range.end()
10111 Else:
10112 BEGIN_EXPR = begin(__range)
10113 END_EXPR = end(__range);
10115 If __range has a member 'begin' but not 'end', or vice versa, we must
10116 still use the second alternative (it will surely fail, however).
10117 When calling begin()/end() in the third alternative we must use
10118 argument dependent lookup, but always considering 'std' as an associated
10119 namespace. */
10121 tree
10122 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10123 bool ivdep)
10125 tree begin, end;
10126 tree iter_type, begin_expr, end_expr;
10127 tree condition, expression;
10129 if (range_decl == error_mark_node || range_expr == error_mark_node)
10130 /* If an error happened previously do nothing or else a lot of
10131 unhelpful errors would be issued. */
10132 begin_expr = end_expr = iter_type = error_mark_node;
10133 else
10135 tree range_temp;
10137 if (TREE_CODE (range_expr) == VAR_DECL
10138 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10139 /* Can't bind a reference to an array of runtime bound. */
10140 range_temp = range_expr;
10141 else
10143 range_temp = build_range_temp (range_expr);
10144 pushdecl (range_temp);
10145 cp_finish_decl (range_temp, range_expr,
10146 /*is_constant_init*/false, NULL_TREE,
10147 LOOKUP_ONLYCONVERTING);
10148 range_temp = convert_from_reference (range_temp);
10150 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10151 &begin_expr, &end_expr);
10154 /* The new for initialization statement. */
10155 begin = build_decl (input_location, VAR_DECL,
10156 get_identifier ("__for_begin"), iter_type);
10157 TREE_USED (begin) = 1;
10158 DECL_ARTIFICIAL (begin) = 1;
10159 pushdecl (begin);
10160 cp_finish_decl (begin, begin_expr,
10161 /*is_constant_init*/false, NULL_TREE,
10162 LOOKUP_ONLYCONVERTING);
10164 end = build_decl (input_location, VAR_DECL,
10165 get_identifier ("__for_end"), iter_type);
10166 TREE_USED (end) = 1;
10167 DECL_ARTIFICIAL (end) = 1;
10168 pushdecl (end);
10169 cp_finish_decl (end, end_expr,
10170 /*is_constant_init*/false, NULL_TREE,
10171 LOOKUP_ONLYCONVERTING);
10173 finish_for_init_stmt (statement);
10175 /* The new for condition. */
10176 condition = build_x_binary_op (input_location, NE_EXPR,
10177 begin, ERROR_MARK,
10178 end, ERROR_MARK,
10179 NULL, tf_warning_or_error);
10180 finish_for_cond (condition, statement, ivdep);
10182 /* The new increment expression. */
10183 expression = finish_unary_op_expr (input_location,
10184 PREINCREMENT_EXPR, begin,
10185 tf_warning_or_error);
10186 finish_for_expr (expression, statement);
10188 /* The declaration is initialized with *__begin inside the loop body. */
10189 cp_finish_decl (range_decl,
10190 build_x_indirect_ref (input_location, begin, RO_NULL,
10191 tf_warning_or_error),
10192 /*is_constant_init*/false, NULL_TREE,
10193 LOOKUP_ONLYCONVERTING);
10195 return statement;
10198 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10199 We need to solve both at the same time because the method used
10200 depends on the existence of members begin or end.
10201 Returns the type deduced for the iterator expression. */
10203 static tree
10204 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10206 if (error_operand_p (range))
10208 *begin = *end = error_mark_node;
10209 return error_mark_node;
10212 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10214 error ("range-based %<for%> expression of type %qT "
10215 "has incomplete type", TREE_TYPE (range));
10216 *begin = *end = error_mark_node;
10217 return error_mark_node;
10219 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10221 /* If RANGE is an array, we will use pointer arithmetic. */
10222 *begin = range;
10223 *end = build_binary_op (input_location, PLUS_EXPR,
10224 range,
10225 array_type_nelts_top (TREE_TYPE (range)),
10227 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10229 else
10231 /* If it is not an array, we must do a bit of magic. */
10232 tree id_begin, id_end;
10233 tree member_begin, member_end;
10235 *begin = *end = error_mark_node;
10237 id_begin = get_identifier ("begin");
10238 id_end = get_identifier ("end");
10239 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10240 /*protect=*/2, /*want_type=*/false,
10241 tf_warning_or_error);
10242 member_end = lookup_member (TREE_TYPE (range), id_end,
10243 /*protect=*/2, /*want_type=*/false,
10244 tf_warning_or_error);
10246 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10248 /* Use the member functions. */
10249 if (member_begin != NULL_TREE)
10250 *begin = cp_parser_range_for_member_function (range, id_begin);
10251 else
10252 error ("range-based %<for%> expression of type %qT has an "
10253 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10255 if (member_end != NULL_TREE)
10256 *end = cp_parser_range_for_member_function (range, id_end);
10257 else
10258 error ("range-based %<for%> expression of type %qT has a "
10259 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10261 else
10263 /* Use global functions with ADL. */
10264 vec<tree, va_gc> *vec;
10265 vec = make_tree_vector ();
10267 vec_safe_push (vec, range);
10269 member_begin = perform_koenig_lookup (id_begin, vec,
10270 /*include_std=*/true,
10271 tf_warning_or_error);
10272 *begin = finish_call_expr (member_begin, &vec, false, true,
10273 tf_warning_or_error);
10274 member_end = perform_koenig_lookup (id_end, vec,
10275 /*include_std=*/true,
10276 tf_warning_or_error);
10277 *end = finish_call_expr (member_end, &vec, false, true,
10278 tf_warning_or_error);
10280 release_tree_vector (vec);
10283 /* Last common checks. */
10284 if (*begin == error_mark_node || *end == error_mark_node)
10286 /* If one of the expressions is an error do no more checks. */
10287 *begin = *end = error_mark_node;
10288 return error_mark_node;
10290 else if (type_dependent_expression_p (*begin)
10291 || type_dependent_expression_p (*end))
10292 /* Can happen, when, eg, in a template context, Koenig lookup
10293 can't resolve begin/end (c++/58503). */
10294 return NULL_TREE;
10295 else
10297 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10298 /* The unqualified type of the __begin and __end temporaries should
10299 be the same, as required by the multiple auto declaration. */
10300 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10301 error ("inconsistent begin/end types in range-based %<for%> "
10302 "statement: %qT and %qT",
10303 TREE_TYPE (*begin), TREE_TYPE (*end));
10304 return iter_type;
10309 /* Helper function for cp_parser_perform_range_for_lookup.
10310 Builds a tree for RANGE.IDENTIFIER(). */
10312 static tree
10313 cp_parser_range_for_member_function (tree range, tree identifier)
10315 tree member, res;
10316 vec<tree, va_gc> *vec;
10318 member = finish_class_member_access_expr (range, identifier,
10319 false, tf_warning_or_error);
10320 if (member == error_mark_node)
10321 return error_mark_node;
10323 vec = make_tree_vector ();
10324 res = finish_call_expr (member, &vec,
10325 /*disallow_virtual=*/false,
10326 /*koenig_p=*/false,
10327 tf_warning_or_error);
10328 release_tree_vector (vec);
10329 return res;
10332 /* Parse an iteration-statement.
10334 iteration-statement:
10335 while ( condition ) statement
10336 do statement while ( expression ) ;
10337 for ( for-init-statement condition [opt] ; expression [opt] )
10338 statement
10340 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10342 static tree
10343 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10345 cp_token *token;
10346 enum rid keyword;
10347 tree statement;
10348 unsigned char in_statement;
10350 /* Peek at the next token. */
10351 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10352 if (!token)
10353 return error_mark_node;
10355 /* Remember whether or not we are already within an iteration
10356 statement. */
10357 in_statement = parser->in_statement;
10359 /* See what kind of keyword it is. */
10360 keyword = token->keyword;
10361 switch (keyword)
10363 case RID_WHILE:
10365 tree condition;
10367 /* Begin the while-statement. */
10368 statement = begin_while_stmt ();
10369 /* Look for the `('. */
10370 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10371 /* Parse the condition. */
10372 condition = cp_parser_condition (parser);
10373 finish_while_stmt_cond (condition, statement, ivdep);
10374 /* Look for the `)'. */
10375 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10376 /* Parse the dependent statement. */
10377 parser->in_statement = IN_ITERATION_STMT;
10378 cp_parser_already_scoped_statement (parser);
10379 parser->in_statement = in_statement;
10380 /* We're done with the while-statement. */
10381 finish_while_stmt (statement);
10383 break;
10385 case RID_DO:
10387 tree expression;
10389 /* Begin the do-statement. */
10390 statement = begin_do_stmt ();
10391 /* Parse the body of the do-statement. */
10392 parser->in_statement = IN_ITERATION_STMT;
10393 cp_parser_implicitly_scoped_statement (parser, NULL);
10394 parser->in_statement = in_statement;
10395 finish_do_body (statement);
10396 /* Look for the `while' keyword. */
10397 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10398 /* Look for the `('. */
10399 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10400 /* Parse the expression. */
10401 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10402 /* We're done with the do-statement. */
10403 finish_do_stmt (expression, statement, ivdep);
10404 /* Look for the `)'. */
10405 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10406 /* Look for the `;'. */
10407 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10409 break;
10411 case RID_FOR:
10413 /* Look for the `('. */
10414 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10416 statement = cp_parser_for (parser, ivdep);
10418 /* Look for the `)'. */
10419 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10421 /* Parse the body of the for-statement. */
10422 parser->in_statement = IN_ITERATION_STMT;
10423 cp_parser_already_scoped_statement (parser);
10424 parser->in_statement = in_statement;
10426 /* We're done with the for-statement. */
10427 finish_for_stmt (statement);
10429 break;
10431 default:
10432 cp_parser_error (parser, "expected iteration-statement");
10433 statement = error_mark_node;
10434 break;
10437 return statement;
10440 /* Parse a for-init-statement or the declarator of a range-based-for.
10441 Returns true if a range-based-for declaration is seen.
10443 for-init-statement:
10444 expression-statement
10445 simple-declaration */
10447 static bool
10448 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10450 /* If the next token is a `;', then we have an empty
10451 expression-statement. Grammatically, this is also a
10452 simple-declaration, but an invalid one, because it does not
10453 declare anything. Therefore, if we did not handle this case
10454 specially, we would issue an error message about an invalid
10455 declaration. */
10456 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10458 bool is_range_for = false;
10459 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10461 parser->colon_corrects_to_scope_p = false;
10463 /* We're going to speculatively look for a declaration, falling back
10464 to an expression, if necessary. */
10465 cp_parser_parse_tentatively (parser);
10466 /* Parse the declaration. */
10467 cp_parser_simple_declaration (parser,
10468 /*function_definition_allowed_p=*/false,
10469 decl);
10470 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10471 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10473 /* It is a range-for, consume the ':' */
10474 cp_lexer_consume_token (parser->lexer);
10475 is_range_for = true;
10476 if (cxx_dialect < cxx11)
10478 error_at (cp_lexer_peek_token (parser->lexer)->location,
10479 "range-based %<for%> loops are not allowed "
10480 "in C++98 mode");
10481 *decl = error_mark_node;
10484 else
10485 /* The ';' is not consumed yet because we told
10486 cp_parser_simple_declaration not to. */
10487 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10489 if (cp_parser_parse_definitely (parser))
10490 return is_range_for;
10491 /* If the tentative parse failed, then we shall need to look for an
10492 expression-statement. */
10494 /* If we are here, it is an expression-statement. */
10495 cp_parser_expression_statement (parser, NULL_TREE);
10496 return false;
10499 /* Parse a jump-statement.
10501 jump-statement:
10502 break ;
10503 continue ;
10504 return expression [opt] ;
10505 return braced-init-list ;
10506 goto identifier ;
10508 GNU extension:
10510 jump-statement:
10511 goto * expression ;
10513 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10515 static tree
10516 cp_parser_jump_statement (cp_parser* parser)
10518 tree statement = error_mark_node;
10519 cp_token *token;
10520 enum rid keyword;
10521 unsigned char in_statement;
10523 /* Peek at the next token. */
10524 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10525 if (!token)
10526 return error_mark_node;
10528 /* See what kind of keyword it is. */
10529 keyword = token->keyword;
10530 switch (keyword)
10532 case RID_BREAK:
10533 in_statement = parser->in_statement & ~IN_IF_STMT;
10534 switch (in_statement)
10536 case 0:
10537 error_at (token->location, "break statement not within loop or switch");
10538 break;
10539 default:
10540 gcc_assert ((in_statement & IN_SWITCH_STMT)
10541 || in_statement == IN_ITERATION_STMT);
10542 statement = finish_break_stmt ();
10543 break;
10544 case IN_OMP_BLOCK:
10545 error_at (token->location, "invalid exit from OpenMP structured block");
10546 break;
10547 case IN_OMP_FOR:
10548 error_at (token->location, "break statement used with OpenMP for loop");
10549 break;
10551 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10552 break;
10554 case RID_CONTINUE:
10555 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10557 case 0:
10558 error_at (token->location, "continue statement not within a loop");
10559 break;
10560 case IN_ITERATION_STMT:
10561 case IN_OMP_FOR:
10562 statement = finish_continue_stmt ();
10563 break;
10564 case IN_OMP_BLOCK:
10565 error_at (token->location, "invalid exit from OpenMP structured block");
10566 break;
10567 default:
10568 gcc_unreachable ();
10570 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10571 break;
10573 case RID_RETURN:
10575 tree expr;
10576 bool expr_non_constant_p;
10578 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10580 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10581 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10583 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10584 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10585 else
10586 /* If the next token is a `;', then there is no
10587 expression. */
10588 expr = NULL_TREE;
10589 /* Build the return-statement. */
10590 statement = finish_return_stmt (expr);
10591 /* Look for the final `;'. */
10592 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10594 break;
10596 case RID_GOTO:
10597 /* Create the goto-statement. */
10598 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10600 /* Issue a warning about this use of a GNU extension. */
10601 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10602 /* Consume the '*' token. */
10603 cp_lexer_consume_token (parser->lexer);
10604 /* Parse the dependent expression. */
10605 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10607 else
10608 finish_goto_stmt (cp_parser_identifier (parser));
10609 /* Look for the final `;'. */
10610 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10611 break;
10613 default:
10614 cp_parser_error (parser, "expected jump-statement");
10615 break;
10618 return statement;
10621 /* Parse a declaration-statement.
10623 declaration-statement:
10624 block-declaration */
10626 static void
10627 cp_parser_declaration_statement (cp_parser* parser)
10629 void *p;
10631 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10632 p = obstack_alloc (&declarator_obstack, 0);
10634 /* Parse the block-declaration. */
10635 cp_parser_block_declaration (parser, /*statement_p=*/true);
10637 /* Free any declarators allocated. */
10638 obstack_free (&declarator_obstack, p);
10641 /* Some dependent statements (like `if (cond) statement'), are
10642 implicitly in their own scope. In other words, if the statement is
10643 a single statement (as opposed to a compound-statement), it is
10644 none-the-less treated as if it were enclosed in braces. Any
10645 declarations appearing in the dependent statement are out of scope
10646 after control passes that point. This function parses a statement,
10647 but ensures that is in its own scope, even if it is not a
10648 compound-statement.
10650 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10651 is a (possibly labeled) if statement which is not enclosed in
10652 braces and has an else clause. This is used to implement
10653 -Wparentheses.
10655 Returns the new statement. */
10657 static tree
10658 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10660 tree statement;
10662 if (if_p != NULL)
10663 *if_p = false;
10665 /* Mark if () ; with a special NOP_EXPR. */
10666 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10668 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10669 cp_lexer_consume_token (parser->lexer);
10670 statement = add_stmt (build_empty_stmt (loc));
10672 /* if a compound is opened, we simply parse the statement directly. */
10673 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10674 statement = cp_parser_compound_statement (parser, NULL, false, false);
10675 /* If the token is not a `{', then we must take special action. */
10676 else
10678 /* Create a compound-statement. */
10679 statement = begin_compound_stmt (0);
10680 /* Parse the dependent-statement. */
10681 cp_parser_statement (parser, NULL_TREE, false, if_p);
10682 /* Finish the dummy compound-statement. */
10683 finish_compound_stmt (statement);
10686 /* Return the statement. */
10687 return statement;
10690 /* For some dependent statements (like `while (cond) statement'), we
10691 have already created a scope. Therefore, even if the dependent
10692 statement is a compound-statement, we do not want to create another
10693 scope. */
10695 static void
10696 cp_parser_already_scoped_statement (cp_parser* parser)
10698 /* If the token is a `{', then we must take special action. */
10699 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10700 cp_parser_statement (parser, NULL_TREE, false, NULL);
10701 else
10703 /* Avoid calling cp_parser_compound_statement, so that we
10704 don't create a new scope. Do everything else by hand. */
10705 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10706 /* If the next keyword is `__label__' we have a label declaration. */
10707 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10708 cp_parser_label_declaration (parser);
10709 /* Parse an (optional) statement-seq. */
10710 cp_parser_statement_seq_opt (parser, NULL_TREE);
10711 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10715 /* Declarations [gram.dcl.dcl] */
10717 /* Parse an optional declaration-sequence.
10719 declaration-seq:
10720 declaration
10721 declaration-seq declaration */
10723 static void
10724 cp_parser_declaration_seq_opt (cp_parser* parser)
10726 while (true)
10728 cp_token *token;
10730 token = cp_lexer_peek_token (parser->lexer);
10732 if (token->type == CPP_CLOSE_BRACE
10733 || token->type == CPP_EOF
10734 || token->type == CPP_PRAGMA_EOL)
10735 break;
10737 if (token->type == CPP_SEMICOLON)
10739 /* A declaration consisting of a single semicolon is
10740 invalid. Allow it unless we're being pedantic. */
10741 cp_lexer_consume_token (parser->lexer);
10742 if (!in_system_header)
10743 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10744 continue;
10747 /* If we're entering or exiting a region that's implicitly
10748 extern "C", modify the lang context appropriately. */
10749 if (!parser->implicit_extern_c && token->implicit_extern_c)
10751 push_lang_context (lang_name_c);
10752 parser->implicit_extern_c = true;
10754 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10756 pop_lang_context ();
10757 parser->implicit_extern_c = false;
10760 if (token->type == CPP_PRAGMA)
10762 /* A top-level declaration can consist solely of a #pragma.
10763 A nested declaration cannot, so this is done here and not
10764 in cp_parser_declaration. (A #pragma at block scope is
10765 handled in cp_parser_statement.) */
10766 cp_parser_pragma (parser, pragma_external);
10767 continue;
10770 /* Parse the declaration itself. */
10771 cp_parser_declaration (parser);
10775 /* Parse a declaration.
10777 declaration:
10778 block-declaration
10779 function-definition
10780 template-declaration
10781 explicit-instantiation
10782 explicit-specialization
10783 linkage-specification
10784 namespace-definition
10786 GNU extension:
10788 declaration:
10789 __extension__ declaration */
10791 static void
10792 cp_parser_declaration (cp_parser* parser)
10794 cp_token token1;
10795 cp_token token2;
10796 int saved_pedantic;
10797 void *p;
10798 tree attributes = NULL_TREE;
10800 /* Check for the `__extension__' keyword. */
10801 if (cp_parser_extension_opt (parser, &saved_pedantic))
10803 /* Parse the qualified declaration. */
10804 cp_parser_declaration (parser);
10805 /* Restore the PEDANTIC flag. */
10806 pedantic = saved_pedantic;
10808 return;
10811 /* Try to figure out what kind of declaration is present. */
10812 token1 = *cp_lexer_peek_token (parser->lexer);
10814 if (token1.type != CPP_EOF)
10815 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10816 else
10818 token2.type = CPP_EOF;
10819 token2.keyword = RID_MAX;
10822 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10823 p = obstack_alloc (&declarator_obstack, 0);
10825 /* If the next token is `extern' and the following token is a string
10826 literal, then we have a linkage specification. */
10827 if (token1.keyword == RID_EXTERN
10828 && cp_parser_is_pure_string_literal (&token2))
10829 cp_parser_linkage_specification (parser);
10830 /* If the next token is `template', then we have either a template
10831 declaration, an explicit instantiation, or an explicit
10832 specialization. */
10833 else if (token1.keyword == RID_TEMPLATE)
10835 /* `template <>' indicates a template specialization. */
10836 if (token2.type == CPP_LESS
10837 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10838 cp_parser_explicit_specialization (parser);
10839 /* `template <' indicates a template declaration. */
10840 else if (token2.type == CPP_LESS)
10841 cp_parser_template_declaration (parser, /*member_p=*/false);
10842 /* Anything else must be an explicit instantiation. */
10843 else
10844 cp_parser_explicit_instantiation (parser);
10846 /* If the next token is `export', then we have a template
10847 declaration. */
10848 else if (token1.keyword == RID_EXPORT)
10849 cp_parser_template_declaration (parser, /*member_p=*/false);
10850 /* If the next token is `extern', 'static' or 'inline' and the one
10851 after that is `template', we have a GNU extended explicit
10852 instantiation directive. */
10853 else if (cp_parser_allow_gnu_extensions_p (parser)
10854 && (token1.keyword == RID_EXTERN
10855 || token1.keyword == RID_STATIC
10856 || token1.keyword == RID_INLINE)
10857 && token2.keyword == RID_TEMPLATE)
10858 cp_parser_explicit_instantiation (parser);
10859 /* If the next token is `namespace', check for a named or unnamed
10860 namespace definition. */
10861 else if (token1.keyword == RID_NAMESPACE
10862 && (/* A named namespace definition. */
10863 (token2.type == CPP_NAME
10864 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10865 != CPP_EQ))
10866 /* An unnamed namespace definition. */
10867 || token2.type == CPP_OPEN_BRACE
10868 || token2.keyword == RID_ATTRIBUTE))
10869 cp_parser_namespace_definition (parser);
10870 /* An inline (associated) namespace definition. */
10871 else if (token1.keyword == RID_INLINE
10872 && token2.keyword == RID_NAMESPACE)
10873 cp_parser_namespace_definition (parser);
10874 /* Objective-C++ declaration/definition. */
10875 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10876 cp_parser_objc_declaration (parser, NULL_TREE);
10877 else if (c_dialect_objc ()
10878 && token1.keyword == RID_ATTRIBUTE
10879 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10880 cp_parser_objc_declaration (parser, attributes);
10881 /* We must have either a block declaration or a function
10882 definition. */
10883 else
10884 /* Try to parse a block-declaration, or a function-definition. */
10885 cp_parser_block_declaration (parser, /*statement_p=*/false);
10887 /* Free any declarators allocated. */
10888 obstack_free (&declarator_obstack, p);
10891 /* Parse a block-declaration.
10893 block-declaration:
10894 simple-declaration
10895 asm-definition
10896 namespace-alias-definition
10897 using-declaration
10898 using-directive
10900 GNU Extension:
10902 block-declaration:
10903 __extension__ block-declaration
10905 C++0x Extension:
10907 block-declaration:
10908 static_assert-declaration
10910 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10911 part of a declaration-statement. */
10913 static void
10914 cp_parser_block_declaration (cp_parser *parser,
10915 bool statement_p)
10917 cp_token *token1;
10918 int saved_pedantic;
10920 /* Check for the `__extension__' keyword. */
10921 if (cp_parser_extension_opt (parser, &saved_pedantic))
10923 /* Parse the qualified declaration. */
10924 cp_parser_block_declaration (parser, statement_p);
10925 /* Restore the PEDANTIC flag. */
10926 pedantic = saved_pedantic;
10928 return;
10931 /* Peek at the next token to figure out which kind of declaration is
10932 present. */
10933 token1 = cp_lexer_peek_token (parser->lexer);
10935 /* If the next keyword is `asm', we have an asm-definition. */
10936 if (token1->keyword == RID_ASM)
10938 if (statement_p)
10939 cp_parser_commit_to_tentative_parse (parser);
10940 cp_parser_asm_definition (parser);
10942 /* If the next keyword is `namespace', we have a
10943 namespace-alias-definition. */
10944 else if (token1->keyword == RID_NAMESPACE)
10945 cp_parser_namespace_alias_definition (parser);
10946 /* If the next keyword is `using', we have a
10947 using-declaration, a using-directive, or an alias-declaration. */
10948 else if (token1->keyword == RID_USING)
10950 cp_token *token2;
10952 if (statement_p)
10953 cp_parser_commit_to_tentative_parse (parser);
10954 /* If the token after `using' is `namespace', then we have a
10955 using-directive. */
10956 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10957 if (token2->keyword == RID_NAMESPACE)
10958 cp_parser_using_directive (parser);
10959 /* If the second token after 'using' is '=', then we have an
10960 alias-declaration. */
10961 else if (cxx_dialect >= cxx11
10962 && token2->type == CPP_NAME
10963 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10964 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
10965 cp_parser_alias_declaration (parser);
10966 /* Otherwise, it's a using-declaration. */
10967 else
10968 cp_parser_using_declaration (parser,
10969 /*access_declaration_p=*/false);
10971 /* If the next keyword is `__label__' we have a misplaced label
10972 declaration. */
10973 else if (token1->keyword == RID_LABEL)
10975 cp_lexer_consume_token (parser->lexer);
10976 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10977 cp_parser_skip_to_end_of_statement (parser);
10978 /* If the next token is now a `;', consume it. */
10979 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10980 cp_lexer_consume_token (parser->lexer);
10982 /* If the next token is `static_assert' we have a static assertion. */
10983 else if (token1->keyword == RID_STATIC_ASSERT)
10984 cp_parser_static_assert (parser, /*member_p=*/false);
10985 /* Anything else must be a simple-declaration. */
10986 else
10987 cp_parser_simple_declaration (parser, !statement_p,
10988 /*maybe_range_for_decl*/NULL);
10991 /* Parse a simple-declaration.
10993 simple-declaration:
10994 decl-specifier-seq [opt] init-declarator-list [opt] ;
10996 init-declarator-list:
10997 init-declarator
10998 init-declarator-list , init-declarator
11000 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11001 function-definition as a simple-declaration.
11003 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11004 parsed declaration if it is an uninitialized single declarator not followed
11005 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11006 if present, will not be consumed. */
11008 static void
11009 cp_parser_simple_declaration (cp_parser* parser,
11010 bool function_definition_allowed_p,
11011 tree *maybe_range_for_decl)
11013 cp_decl_specifier_seq decl_specifiers;
11014 int declares_class_or_enum;
11015 bool saw_declarator;
11017 if (maybe_range_for_decl)
11018 *maybe_range_for_decl = NULL_TREE;
11020 /* Defer access checks until we know what is being declared; the
11021 checks for names appearing in the decl-specifier-seq should be
11022 done as if we were in the scope of the thing being declared. */
11023 push_deferring_access_checks (dk_deferred);
11025 /* Parse the decl-specifier-seq. We have to keep track of whether
11026 or not the decl-specifier-seq declares a named class or
11027 enumeration type, since that is the only case in which the
11028 init-declarator-list is allowed to be empty.
11030 [dcl.dcl]
11032 In a simple-declaration, the optional init-declarator-list can be
11033 omitted only when declaring a class or enumeration, that is when
11034 the decl-specifier-seq contains either a class-specifier, an
11035 elaborated-type-specifier, or an enum-specifier. */
11036 cp_parser_decl_specifier_seq (parser,
11037 CP_PARSER_FLAGS_OPTIONAL,
11038 &decl_specifiers,
11039 &declares_class_or_enum);
11040 /* We no longer need to defer access checks. */
11041 stop_deferring_access_checks ();
11043 /* In a block scope, a valid declaration must always have a
11044 decl-specifier-seq. By not trying to parse declarators, we can
11045 resolve the declaration/expression ambiguity more quickly. */
11046 if (!function_definition_allowed_p
11047 && !decl_specifiers.any_specifiers_p)
11049 cp_parser_error (parser, "expected declaration");
11050 goto done;
11053 /* If the next two tokens are both identifiers, the code is
11054 erroneous. The usual cause of this situation is code like:
11056 T t;
11058 where "T" should name a type -- but does not. */
11059 if (!decl_specifiers.any_type_specifiers_p
11060 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11062 /* If parsing tentatively, we should commit; we really are
11063 looking at a declaration. */
11064 cp_parser_commit_to_tentative_parse (parser);
11065 /* Give up. */
11066 goto done;
11069 /* If we have seen at least one decl-specifier, and the next token
11070 is not a parenthesis, then we must be looking at a declaration.
11071 (After "int (" we might be looking at a functional cast.) */
11072 if (decl_specifiers.any_specifiers_p
11073 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11074 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11075 && !cp_parser_error_occurred (parser))
11076 cp_parser_commit_to_tentative_parse (parser);
11078 /* Keep going until we hit the `;' at the end of the simple
11079 declaration. */
11080 saw_declarator = false;
11081 while (cp_lexer_next_token_is_not (parser->lexer,
11082 CPP_SEMICOLON))
11084 cp_token *token;
11085 bool function_definition_p;
11086 tree decl;
11088 if (saw_declarator)
11090 /* If we are processing next declarator, coma is expected */
11091 token = cp_lexer_peek_token (parser->lexer);
11092 gcc_assert (token->type == CPP_COMMA);
11093 cp_lexer_consume_token (parser->lexer);
11094 if (maybe_range_for_decl)
11095 *maybe_range_for_decl = error_mark_node;
11097 else
11098 saw_declarator = true;
11100 /* Parse the init-declarator. */
11101 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11102 /*checks=*/NULL,
11103 function_definition_allowed_p,
11104 /*member_p=*/false,
11105 declares_class_or_enum,
11106 &function_definition_p,
11107 maybe_range_for_decl);
11108 /* If an error occurred while parsing tentatively, exit quickly.
11109 (That usually happens when in the body of a function; each
11110 statement is treated as a declaration-statement until proven
11111 otherwise.) */
11112 if (cp_parser_error_occurred (parser))
11113 goto done;
11114 /* Handle function definitions specially. */
11115 if (function_definition_p)
11117 /* If the next token is a `,', then we are probably
11118 processing something like:
11120 void f() {}, *p;
11122 which is erroneous. */
11123 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11125 cp_token *token = cp_lexer_peek_token (parser->lexer);
11126 error_at (token->location,
11127 "mixing"
11128 " declarations and function-definitions is forbidden");
11130 /* Otherwise, we're done with the list of declarators. */
11131 else
11133 pop_deferring_access_checks ();
11134 return;
11137 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11138 *maybe_range_for_decl = decl;
11139 /* The next token should be either a `,' or a `;'. */
11140 token = cp_lexer_peek_token (parser->lexer);
11141 /* If it's a `,', there are more declarators to come. */
11142 if (token->type == CPP_COMMA)
11143 /* will be consumed next time around */;
11144 /* If it's a `;', we are done. */
11145 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11146 break;
11147 /* Anything else is an error. */
11148 else
11150 /* If we have already issued an error message we don't need
11151 to issue another one. */
11152 if (decl != error_mark_node
11153 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11154 cp_parser_error (parser, "expected %<,%> or %<;%>");
11155 /* Skip tokens until we reach the end of the statement. */
11156 cp_parser_skip_to_end_of_statement (parser);
11157 /* If the next token is now a `;', consume it. */
11158 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11159 cp_lexer_consume_token (parser->lexer);
11160 goto done;
11162 /* After the first time around, a function-definition is not
11163 allowed -- even if it was OK at first. For example:
11165 int i, f() {}
11167 is not valid. */
11168 function_definition_allowed_p = false;
11171 /* Issue an error message if no declarators are present, and the
11172 decl-specifier-seq does not itself declare a class or
11173 enumeration: [dcl.dcl]/3. */
11174 if (!saw_declarator)
11176 if (cp_parser_declares_only_class_p (parser))
11178 if (!declares_class_or_enum
11179 && decl_specifiers.type
11180 && OVERLOAD_TYPE_P (decl_specifiers.type))
11181 /* Ensure an error is issued anyway when finish_decltype_type,
11182 called via cp_parser_decl_specifier_seq, returns a class or
11183 an enumeration (c++/51786). */
11184 decl_specifiers.type = NULL_TREE;
11185 shadow_tag (&decl_specifiers);
11187 /* Perform any deferred access checks. */
11188 perform_deferred_access_checks (tf_warning_or_error);
11191 /* Consume the `;'. */
11192 if (!maybe_range_for_decl)
11193 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11195 done:
11196 pop_deferring_access_checks ();
11199 /* Parse a decl-specifier-seq.
11201 decl-specifier-seq:
11202 decl-specifier-seq [opt] decl-specifier
11203 decl-specifier attribute-specifier-seq [opt] (C++11)
11205 decl-specifier:
11206 storage-class-specifier
11207 type-specifier
11208 function-specifier
11209 friend
11210 typedef
11212 GNU Extension:
11214 decl-specifier:
11215 attributes
11217 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11219 The parser flags FLAGS is used to control type-specifier parsing.
11221 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11222 flags:
11224 1: one of the decl-specifiers is an elaborated-type-specifier
11225 (i.e., a type declaration)
11226 2: one of the decl-specifiers is an enum-specifier or a
11227 class-specifier (i.e., a type definition)
11231 static void
11232 cp_parser_decl_specifier_seq (cp_parser* parser,
11233 cp_parser_flags flags,
11234 cp_decl_specifier_seq *decl_specs,
11235 int* declares_class_or_enum)
11237 bool constructor_possible_p = !parser->in_declarator_p;
11238 bool found_decl_spec = false;
11239 cp_token *start_token = NULL;
11240 cp_decl_spec ds;
11242 /* Clear DECL_SPECS. */
11243 clear_decl_specs (decl_specs);
11245 /* Assume no class or enumeration type is declared. */
11246 *declares_class_or_enum = 0;
11248 /* Keep reading specifiers until there are no more to read. */
11249 while (true)
11251 bool constructor_p;
11252 cp_token *token;
11253 ds = ds_last;
11255 /* Peek at the next token. */
11256 token = cp_lexer_peek_token (parser->lexer);
11258 /* Save the first token of the decl spec list for error
11259 reporting. */
11260 if (!start_token)
11261 start_token = token;
11262 /* Handle attributes. */
11263 if (cp_next_tokens_can_be_attribute_p (parser))
11265 /* Parse the attributes. */
11266 tree attrs = cp_parser_attributes_opt (parser);
11268 /* In a sequence of declaration specifiers, c++11 attributes
11269 appertain to the type that precede them. In that case
11270 [dcl.spec]/1 says:
11272 The attribute-specifier-seq affects the type only for
11273 the declaration it appears in, not other declarations
11274 involving the same type.
11276 But for now let's force the user to position the
11277 attribute either at the beginning of the declaration or
11278 after the declarator-id, which would clearly mean that it
11279 applies to the declarator. */
11280 if (cxx11_attribute_p (attrs))
11282 if (!found_decl_spec)
11283 /* The c++11 attribute is at the beginning of the
11284 declaration. It appertains to the entity being
11285 declared. */;
11286 else
11288 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11290 /* This is an attribute following a
11291 class-specifier. */
11292 if (decl_specs->type_definition_p)
11293 warn_misplaced_attr_for_class_type (token->location,
11294 decl_specs->type);
11295 attrs = NULL_TREE;
11297 else
11299 decl_specs->std_attributes
11300 = chainon (decl_specs->std_attributes,
11301 attrs);
11302 if (decl_specs->locations[ds_std_attribute] == 0)
11303 decl_specs->locations[ds_std_attribute] = token->location;
11305 continue;
11309 decl_specs->attributes
11310 = chainon (decl_specs->attributes,
11311 attrs);
11312 if (decl_specs->locations[ds_attribute] == 0)
11313 decl_specs->locations[ds_attribute] = token->location;
11314 continue;
11316 /* Assume we will find a decl-specifier keyword. */
11317 found_decl_spec = true;
11318 /* If the next token is an appropriate keyword, we can simply
11319 add it to the list. */
11320 switch (token->keyword)
11322 /* decl-specifier:
11323 friend
11324 constexpr */
11325 case RID_FRIEND:
11326 if (!at_class_scope_p ())
11328 error_at (token->location, "%<friend%> used outside of class");
11329 cp_lexer_purge_token (parser->lexer);
11331 else
11333 ds = ds_friend;
11334 /* Consume the token. */
11335 cp_lexer_consume_token (parser->lexer);
11337 break;
11339 case RID_CONSTEXPR:
11340 ds = ds_constexpr;
11341 cp_lexer_consume_token (parser->lexer);
11342 break;
11344 /* function-specifier:
11345 inline
11346 virtual
11347 explicit */
11348 case RID_INLINE:
11349 case RID_VIRTUAL:
11350 case RID_EXPLICIT:
11351 cp_parser_function_specifier_opt (parser, decl_specs);
11352 break;
11354 /* decl-specifier:
11355 typedef */
11356 case RID_TYPEDEF:
11357 ds = ds_typedef;
11358 /* Consume the token. */
11359 cp_lexer_consume_token (parser->lexer);
11360 /* A constructor declarator cannot appear in a typedef. */
11361 constructor_possible_p = false;
11362 /* The "typedef" keyword can only occur in a declaration; we
11363 may as well commit at this point. */
11364 cp_parser_commit_to_tentative_parse (parser);
11366 if (decl_specs->storage_class != sc_none)
11367 decl_specs->conflicting_specifiers_p = true;
11368 break;
11370 /* storage-class-specifier:
11371 auto
11372 register
11373 static
11374 extern
11375 mutable
11377 GNU Extension:
11378 thread */
11379 case RID_AUTO:
11380 if (cxx_dialect == cxx98)
11382 /* Consume the token. */
11383 cp_lexer_consume_token (parser->lexer);
11385 /* Complain about `auto' as a storage specifier, if
11386 we're complaining about C++0x compatibility. */
11387 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11388 " changes meaning in C++11; please remove it");
11390 /* Set the storage class anyway. */
11391 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11392 token);
11394 else
11395 /* C++0x auto type-specifier. */
11396 found_decl_spec = false;
11397 break;
11399 case RID_REGISTER:
11400 case RID_STATIC:
11401 case RID_EXTERN:
11402 case RID_MUTABLE:
11403 /* Consume the token. */
11404 cp_lexer_consume_token (parser->lexer);
11405 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11406 token);
11407 break;
11408 case RID_THREAD:
11409 /* Consume the token. */
11410 ds = ds_thread;
11411 cp_lexer_consume_token (parser->lexer);
11412 break;
11414 default:
11415 /* We did not yet find a decl-specifier yet. */
11416 found_decl_spec = false;
11417 break;
11420 if (found_decl_spec
11421 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11422 && token->keyword != RID_CONSTEXPR)
11423 error ("decl-specifier invalid in condition");
11425 if (ds != ds_last)
11426 set_and_check_decl_spec_loc (decl_specs, ds, token);
11428 /* Constructors are a special case. The `S' in `S()' is not a
11429 decl-specifier; it is the beginning of the declarator. */
11430 constructor_p
11431 = (!found_decl_spec
11432 && constructor_possible_p
11433 && (cp_parser_constructor_declarator_p
11434 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11436 /* If we don't have a DECL_SPEC yet, then we must be looking at
11437 a type-specifier. */
11438 if (!found_decl_spec && !constructor_p)
11440 int decl_spec_declares_class_or_enum;
11441 bool is_cv_qualifier;
11442 tree type_spec;
11444 type_spec
11445 = cp_parser_type_specifier (parser, flags,
11446 decl_specs,
11447 /*is_declaration=*/true,
11448 &decl_spec_declares_class_or_enum,
11449 &is_cv_qualifier);
11450 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11452 /* If this type-specifier referenced a user-defined type
11453 (a typedef, class-name, etc.), then we can't allow any
11454 more such type-specifiers henceforth.
11456 [dcl.spec]
11458 The longest sequence of decl-specifiers that could
11459 possibly be a type name is taken as the
11460 decl-specifier-seq of a declaration. The sequence shall
11461 be self-consistent as described below.
11463 [dcl.type]
11465 As a general rule, at most one type-specifier is allowed
11466 in the complete decl-specifier-seq of a declaration. The
11467 only exceptions are the following:
11469 -- const or volatile can be combined with any other
11470 type-specifier.
11472 -- signed or unsigned can be combined with char, long,
11473 short, or int.
11475 -- ..
11477 Example:
11479 typedef char* Pc;
11480 void g (const int Pc);
11482 Here, Pc is *not* part of the decl-specifier seq; it's
11483 the declarator. Therefore, once we see a type-specifier
11484 (other than a cv-qualifier), we forbid any additional
11485 user-defined types. We *do* still allow things like `int
11486 int' to be considered a decl-specifier-seq, and issue the
11487 error message later. */
11488 if (type_spec && !is_cv_qualifier)
11489 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11490 /* A constructor declarator cannot follow a type-specifier. */
11491 if (type_spec)
11493 constructor_possible_p = false;
11494 found_decl_spec = true;
11495 if (!is_cv_qualifier)
11496 decl_specs->any_type_specifiers_p = true;
11500 /* If we still do not have a DECL_SPEC, then there are no more
11501 decl-specifiers. */
11502 if (!found_decl_spec)
11503 break;
11505 decl_specs->any_specifiers_p = true;
11506 /* After we see one decl-specifier, further decl-specifiers are
11507 always optional. */
11508 flags |= CP_PARSER_FLAGS_OPTIONAL;
11511 /* Don't allow a friend specifier with a class definition. */
11512 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11513 && (*declares_class_or_enum & 2))
11514 error_at (decl_specs->locations[ds_friend],
11515 "class definition may not be declared a friend");
11518 /* Parse an (optional) storage-class-specifier.
11520 storage-class-specifier:
11521 auto
11522 register
11523 static
11524 extern
11525 mutable
11527 GNU Extension:
11529 storage-class-specifier:
11530 thread
11532 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11534 static tree
11535 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11537 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11539 case RID_AUTO:
11540 if (cxx_dialect != cxx98)
11541 return NULL_TREE;
11542 /* Fall through for C++98. */
11544 case RID_REGISTER:
11545 case RID_STATIC:
11546 case RID_EXTERN:
11547 case RID_MUTABLE:
11548 case RID_THREAD:
11549 /* Consume the token. */
11550 return cp_lexer_consume_token (parser->lexer)->u.value;
11552 default:
11553 return NULL_TREE;
11557 /* Parse an (optional) function-specifier.
11559 function-specifier:
11560 inline
11561 virtual
11562 explicit
11564 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11565 Updates DECL_SPECS, if it is non-NULL. */
11567 static tree
11568 cp_parser_function_specifier_opt (cp_parser* parser,
11569 cp_decl_specifier_seq *decl_specs)
11571 cp_token *token = cp_lexer_peek_token (parser->lexer);
11572 switch (token->keyword)
11574 case RID_INLINE:
11575 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11576 break;
11578 case RID_VIRTUAL:
11579 /* 14.5.2.3 [temp.mem]
11581 A member function template shall not be virtual. */
11582 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11583 error_at (token->location, "templates may not be %<virtual%>");
11584 else
11585 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11586 break;
11588 case RID_EXPLICIT:
11589 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11590 break;
11592 default:
11593 return NULL_TREE;
11596 /* Consume the token. */
11597 return cp_lexer_consume_token (parser->lexer)->u.value;
11600 /* Parse a linkage-specification.
11602 linkage-specification:
11603 extern string-literal { declaration-seq [opt] }
11604 extern string-literal declaration */
11606 static void
11607 cp_parser_linkage_specification (cp_parser* parser)
11609 tree linkage;
11611 /* Look for the `extern' keyword. */
11612 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11614 /* Look for the string-literal. */
11615 linkage = cp_parser_string_literal (parser, false, false);
11617 /* Transform the literal into an identifier. If the literal is a
11618 wide-character string, or contains embedded NULs, then we can't
11619 handle it as the user wants. */
11620 if (strlen (TREE_STRING_POINTER (linkage))
11621 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11623 cp_parser_error (parser, "invalid linkage-specification");
11624 /* Assume C++ linkage. */
11625 linkage = lang_name_cplusplus;
11627 else
11628 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11630 /* We're now using the new linkage. */
11631 push_lang_context (linkage);
11633 /* If the next token is a `{', then we're using the first
11634 production. */
11635 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11637 cp_ensure_no_omp_declare_simd (parser);
11639 /* Consume the `{' token. */
11640 cp_lexer_consume_token (parser->lexer);
11641 /* Parse the declarations. */
11642 cp_parser_declaration_seq_opt (parser);
11643 /* Look for the closing `}'. */
11644 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11646 /* Otherwise, there's just one declaration. */
11647 else
11649 bool saved_in_unbraced_linkage_specification_p;
11651 saved_in_unbraced_linkage_specification_p
11652 = parser->in_unbraced_linkage_specification_p;
11653 parser->in_unbraced_linkage_specification_p = true;
11654 cp_parser_declaration (parser);
11655 parser->in_unbraced_linkage_specification_p
11656 = saved_in_unbraced_linkage_specification_p;
11659 /* We're done with the linkage-specification. */
11660 pop_lang_context ();
11663 /* Parse a static_assert-declaration.
11665 static_assert-declaration:
11666 static_assert ( constant-expression , string-literal ) ;
11668 If MEMBER_P, this static_assert is a class member. */
11670 static void
11671 cp_parser_static_assert(cp_parser *parser, bool member_p)
11673 tree condition;
11674 tree message;
11675 cp_token *token;
11676 location_t saved_loc;
11677 bool dummy;
11679 /* Peek at the `static_assert' token so we can keep track of exactly
11680 where the static assertion started. */
11681 token = cp_lexer_peek_token (parser->lexer);
11682 saved_loc = token->location;
11684 /* Look for the `static_assert' keyword. */
11685 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11686 RT_STATIC_ASSERT))
11687 return;
11689 /* We know we are in a static assertion; commit to any tentative
11690 parse. */
11691 if (cp_parser_parsing_tentatively (parser))
11692 cp_parser_commit_to_tentative_parse (parser);
11694 /* Parse the `(' starting the static assertion condition. */
11695 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11697 /* Parse the constant-expression. Allow a non-constant expression
11698 here in order to give better diagnostics in finish_static_assert. */
11699 condition =
11700 cp_parser_constant_expression (parser,
11701 /*allow_non_constant_p=*/true,
11702 /*non_constant_p=*/&dummy);
11704 /* Parse the separating `,'. */
11705 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11707 /* Parse the string-literal message. */
11708 message = cp_parser_string_literal (parser,
11709 /*translate=*/false,
11710 /*wide_ok=*/true);
11712 /* A `)' completes the static assertion. */
11713 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11714 cp_parser_skip_to_closing_parenthesis (parser,
11715 /*recovering=*/true,
11716 /*or_comma=*/false,
11717 /*consume_paren=*/true);
11719 /* A semicolon terminates the declaration. */
11720 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11722 /* Complete the static assertion, which may mean either processing
11723 the static assert now or saving it for template instantiation. */
11724 finish_static_assert (condition, message, saved_loc, member_p);
11727 /* Parse the expression in decltype ( expression ). */
11729 static tree
11730 cp_parser_decltype_expr (cp_parser *parser,
11731 bool &id_expression_or_member_access_p)
11733 cp_token *id_expr_start_token;
11734 tree expr;
11736 /* First, try parsing an id-expression. */
11737 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11738 cp_parser_parse_tentatively (parser);
11739 expr = cp_parser_id_expression (parser,
11740 /*template_keyword_p=*/false,
11741 /*check_dependency_p=*/true,
11742 /*template_p=*/NULL,
11743 /*declarator_p=*/false,
11744 /*optional_p=*/false);
11746 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11748 bool non_integral_constant_expression_p = false;
11749 tree id_expression = expr;
11750 cp_id_kind idk;
11751 const char *error_msg;
11753 if (identifier_p (expr))
11754 /* Lookup the name we got back from the id-expression. */
11755 expr = cp_parser_lookup_name_simple (parser, expr,
11756 id_expr_start_token->location);
11758 if (expr
11759 && expr != error_mark_node
11760 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11761 && TREE_CODE (expr) != TYPE_DECL
11762 && (TREE_CODE (expr) != BIT_NOT_EXPR
11763 || !TYPE_P (TREE_OPERAND (expr, 0)))
11764 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11766 /* Complete lookup of the id-expression. */
11767 expr = (finish_id_expression
11768 (id_expression, expr, parser->scope, &idk,
11769 /*integral_constant_expression_p=*/false,
11770 /*allow_non_integral_constant_expression_p=*/true,
11771 &non_integral_constant_expression_p,
11772 /*template_p=*/false,
11773 /*done=*/true,
11774 /*address_p=*/false,
11775 /*template_arg_p=*/false,
11776 &error_msg,
11777 id_expr_start_token->location));
11779 if (expr == error_mark_node)
11780 /* We found an id-expression, but it was something that we
11781 should not have found. This is an error, not something
11782 we can recover from, so note that we found an
11783 id-expression and we'll recover as gracefully as
11784 possible. */
11785 id_expression_or_member_access_p = true;
11788 if (expr
11789 && expr != error_mark_node
11790 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11791 /* We have an id-expression. */
11792 id_expression_or_member_access_p = true;
11795 if (!id_expression_or_member_access_p)
11797 /* Abort the id-expression parse. */
11798 cp_parser_abort_tentative_parse (parser);
11800 /* Parsing tentatively, again. */
11801 cp_parser_parse_tentatively (parser);
11803 /* Parse a class member access. */
11804 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11805 /*cast_p=*/false, /*decltype*/true,
11806 /*member_access_only_p=*/true, NULL);
11808 if (expr
11809 && expr != error_mark_node
11810 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11811 /* We have an id-expression. */
11812 id_expression_or_member_access_p = true;
11815 if (id_expression_or_member_access_p)
11816 /* We have parsed the complete id-expression or member access. */
11817 cp_parser_parse_definitely (parser);
11818 else
11820 /* Abort our attempt to parse an id-expression or member access
11821 expression. */
11822 cp_parser_abort_tentative_parse (parser);
11824 /* Parse a full expression. */
11825 expr = cp_parser_expression (parser, /*cast_p=*/false,
11826 /*decltype*/true, NULL);
11829 return expr;
11832 /* Parse a `decltype' type. Returns the type.
11834 simple-type-specifier:
11835 decltype ( expression )
11836 C++14 proposal:
11837 decltype ( auto ) */
11839 static tree
11840 cp_parser_decltype (cp_parser *parser)
11842 tree expr;
11843 bool id_expression_or_member_access_p = false;
11844 const char *saved_message;
11845 bool saved_integral_constant_expression_p;
11846 bool saved_non_integral_constant_expression_p;
11847 bool saved_greater_than_is_operator_p;
11848 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11850 if (start_token->type == CPP_DECLTYPE)
11852 /* Already parsed. */
11853 cp_lexer_consume_token (parser->lexer);
11854 return start_token->u.value;
11857 /* Look for the `decltype' token. */
11858 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11859 return error_mark_node;
11861 /* Parse the opening `('. */
11862 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11863 return error_mark_node;
11865 /* decltype (auto) */
11866 if (cxx_dialect >= cxx1y
11867 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
11869 cp_lexer_consume_token (parser->lexer);
11870 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11871 return error_mark_node;
11872 expr = make_decltype_auto ();
11873 AUTO_IS_DECLTYPE (expr) = true;
11874 goto rewrite;
11877 /* Types cannot be defined in a `decltype' expression. Save away the
11878 old message. */
11879 saved_message = parser->type_definition_forbidden_message;
11881 /* And create the new one. */
11882 parser->type_definition_forbidden_message
11883 = G_("types may not be defined in %<decltype%> expressions");
11885 /* The restrictions on constant-expressions do not apply inside
11886 decltype expressions. */
11887 saved_integral_constant_expression_p
11888 = parser->integral_constant_expression_p;
11889 saved_non_integral_constant_expression_p
11890 = parser->non_integral_constant_expression_p;
11891 parser->integral_constant_expression_p = false;
11893 /* Within a parenthesized expression, a `>' token is always
11894 the greater-than operator. */
11895 saved_greater_than_is_operator_p
11896 = parser->greater_than_is_operator_p;
11897 parser->greater_than_is_operator_p = true;
11899 /* Do not actually evaluate the expression. */
11900 ++cp_unevaluated_operand;
11902 /* Do not warn about problems with the expression. */
11903 ++c_inhibit_evaluation_warnings;
11905 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
11907 /* Go back to evaluating expressions. */
11908 --cp_unevaluated_operand;
11909 --c_inhibit_evaluation_warnings;
11911 /* The `>' token might be the end of a template-id or
11912 template-parameter-list now. */
11913 parser->greater_than_is_operator_p
11914 = saved_greater_than_is_operator_p;
11916 /* Restore the old message and the integral constant expression
11917 flags. */
11918 parser->type_definition_forbidden_message = saved_message;
11919 parser->integral_constant_expression_p
11920 = saved_integral_constant_expression_p;
11921 parser->non_integral_constant_expression_p
11922 = saved_non_integral_constant_expression_p;
11924 /* Parse to the closing `)'. */
11925 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11927 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11928 /*consume_paren=*/true);
11929 return error_mark_node;
11932 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11933 tf_warning_or_error);
11935 rewrite:
11936 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11937 it again. */
11938 start_token->type = CPP_DECLTYPE;
11939 start_token->u.value = expr;
11940 start_token->keyword = RID_MAX;
11941 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11943 return expr;
11946 /* Special member functions [gram.special] */
11948 /* Parse a conversion-function-id.
11950 conversion-function-id:
11951 operator conversion-type-id
11953 Returns an IDENTIFIER_NODE representing the operator. */
11955 static tree
11956 cp_parser_conversion_function_id (cp_parser* parser)
11958 tree type;
11959 tree saved_scope;
11960 tree saved_qualifying_scope;
11961 tree saved_object_scope;
11962 tree pushed_scope = NULL_TREE;
11964 /* Look for the `operator' token. */
11965 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11966 return error_mark_node;
11967 /* When we parse the conversion-type-id, the current scope will be
11968 reset. However, we need that information in able to look up the
11969 conversion function later, so we save it here. */
11970 saved_scope = parser->scope;
11971 saved_qualifying_scope = parser->qualifying_scope;
11972 saved_object_scope = parser->object_scope;
11973 /* We must enter the scope of the class so that the names of
11974 entities declared within the class are available in the
11975 conversion-type-id. For example, consider:
11977 struct S {
11978 typedef int I;
11979 operator I();
11982 S::operator I() { ... }
11984 In order to see that `I' is a type-name in the definition, we
11985 must be in the scope of `S'. */
11986 if (saved_scope)
11987 pushed_scope = push_scope (saved_scope);
11988 /* Parse the conversion-type-id. */
11989 type = cp_parser_conversion_type_id (parser);
11990 /* Leave the scope of the class, if any. */
11991 if (pushed_scope)
11992 pop_scope (pushed_scope);
11993 /* Restore the saved scope. */
11994 parser->scope = saved_scope;
11995 parser->qualifying_scope = saved_qualifying_scope;
11996 parser->object_scope = saved_object_scope;
11997 /* If the TYPE is invalid, indicate failure. */
11998 if (type == error_mark_node)
11999 return error_mark_node;
12000 return mangle_conv_op_name_for_type (type);
12003 /* Parse a conversion-type-id:
12005 conversion-type-id:
12006 type-specifier-seq conversion-declarator [opt]
12008 Returns the TYPE specified. */
12010 static tree
12011 cp_parser_conversion_type_id (cp_parser* parser)
12013 tree attributes;
12014 cp_decl_specifier_seq type_specifiers;
12015 cp_declarator *declarator;
12016 tree type_specified;
12017 const char *saved_message;
12019 /* Parse the attributes. */
12020 attributes = cp_parser_attributes_opt (parser);
12022 saved_message = parser->type_definition_forbidden_message;
12023 parser->type_definition_forbidden_message
12024 = G_("types may not be defined in a conversion-type-id");
12026 /* Parse the type-specifiers. */
12027 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12028 /*is_trailing_return=*/false,
12029 &type_specifiers);
12031 parser->type_definition_forbidden_message = saved_message;
12033 /* If that didn't work, stop. */
12034 if (type_specifiers.type == error_mark_node)
12035 return error_mark_node;
12036 /* Parse the conversion-declarator. */
12037 declarator = cp_parser_conversion_declarator_opt (parser);
12039 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12040 /*initialized=*/0, &attributes);
12041 if (attributes)
12042 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12044 /* Don't give this error when parsing tentatively. This happens to
12045 work because we always parse this definitively once. */
12046 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12047 && type_uses_auto (type_specified))
12049 if (cxx_dialect < cxx1y)
12051 error ("invalid use of %<auto%> in conversion operator");
12052 return error_mark_node;
12054 else if (template_parm_scope_p ())
12055 warning (0, "use of %<auto%> in member template "
12056 "conversion operator can never be deduced");
12059 return type_specified;
12062 /* Parse an (optional) conversion-declarator.
12064 conversion-declarator:
12065 ptr-operator conversion-declarator [opt]
12069 static cp_declarator *
12070 cp_parser_conversion_declarator_opt (cp_parser* parser)
12072 enum tree_code code;
12073 tree class_type, std_attributes = NULL_TREE;
12074 cp_cv_quals cv_quals;
12076 /* We don't know if there's a ptr-operator next, or not. */
12077 cp_parser_parse_tentatively (parser);
12078 /* Try the ptr-operator. */
12079 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12080 &std_attributes);
12081 /* If it worked, look for more conversion-declarators. */
12082 if (cp_parser_parse_definitely (parser))
12084 cp_declarator *declarator;
12086 /* Parse another optional declarator. */
12087 declarator = cp_parser_conversion_declarator_opt (parser);
12089 declarator = cp_parser_make_indirect_declarator
12090 (code, class_type, cv_quals, declarator, std_attributes);
12092 return declarator;
12095 return NULL;
12098 /* Parse an (optional) ctor-initializer.
12100 ctor-initializer:
12101 : mem-initializer-list
12103 Returns TRUE iff the ctor-initializer was actually present. */
12105 static bool
12106 cp_parser_ctor_initializer_opt (cp_parser* parser)
12108 /* If the next token is not a `:', then there is no
12109 ctor-initializer. */
12110 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12112 /* Do default initialization of any bases and members. */
12113 if (DECL_CONSTRUCTOR_P (current_function_decl))
12114 finish_mem_initializers (NULL_TREE);
12116 return false;
12119 /* Consume the `:' token. */
12120 cp_lexer_consume_token (parser->lexer);
12121 /* And the mem-initializer-list. */
12122 cp_parser_mem_initializer_list (parser);
12124 return true;
12127 /* Parse a mem-initializer-list.
12129 mem-initializer-list:
12130 mem-initializer ... [opt]
12131 mem-initializer ... [opt] , mem-initializer-list */
12133 static void
12134 cp_parser_mem_initializer_list (cp_parser* parser)
12136 tree mem_initializer_list = NULL_TREE;
12137 tree target_ctor = error_mark_node;
12138 cp_token *token = cp_lexer_peek_token (parser->lexer);
12140 /* Let the semantic analysis code know that we are starting the
12141 mem-initializer-list. */
12142 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12143 error_at (token->location,
12144 "only constructors take member initializers");
12146 /* Loop through the list. */
12147 while (true)
12149 tree mem_initializer;
12151 token = cp_lexer_peek_token (parser->lexer);
12152 /* Parse the mem-initializer. */
12153 mem_initializer = cp_parser_mem_initializer (parser);
12154 /* If the next token is a `...', we're expanding member initializers. */
12155 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12157 /* Consume the `...'. */
12158 cp_lexer_consume_token (parser->lexer);
12160 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12161 can be expanded but members cannot. */
12162 if (mem_initializer != error_mark_node
12163 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12165 error_at (token->location,
12166 "cannot expand initializer for member %<%D%>",
12167 TREE_PURPOSE (mem_initializer));
12168 mem_initializer = error_mark_node;
12171 /* Construct the pack expansion type. */
12172 if (mem_initializer != error_mark_node)
12173 mem_initializer = make_pack_expansion (mem_initializer);
12175 if (target_ctor != error_mark_node
12176 && mem_initializer != error_mark_node)
12178 error ("mem-initializer for %qD follows constructor delegation",
12179 TREE_PURPOSE (mem_initializer));
12180 mem_initializer = error_mark_node;
12182 /* Look for a target constructor. */
12183 if (mem_initializer != error_mark_node
12184 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12185 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12187 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12188 if (mem_initializer_list)
12190 error ("constructor delegation follows mem-initializer for %qD",
12191 TREE_PURPOSE (mem_initializer_list));
12192 mem_initializer = error_mark_node;
12194 target_ctor = mem_initializer;
12196 /* Add it to the list, unless it was erroneous. */
12197 if (mem_initializer != error_mark_node)
12199 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12200 mem_initializer_list = mem_initializer;
12202 /* If the next token is not a `,', we're done. */
12203 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12204 break;
12205 /* Consume the `,' token. */
12206 cp_lexer_consume_token (parser->lexer);
12209 /* Perform semantic analysis. */
12210 if (DECL_CONSTRUCTOR_P (current_function_decl))
12211 finish_mem_initializers (mem_initializer_list);
12214 /* Parse a mem-initializer.
12216 mem-initializer:
12217 mem-initializer-id ( expression-list [opt] )
12218 mem-initializer-id braced-init-list
12220 GNU extension:
12222 mem-initializer:
12223 ( expression-list [opt] )
12225 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12226 class) or FIELD_DECL (for a non-static data member) to initialize;
12227 the TREE_VALUE is the expression-list. An empty initialization
12228 list is represented by void_list_node. */
12230 static tree
12231 cp_parser_mem_initializer (cp_parser* parser)
12233 tree mem_initializer_id;
12234 tree expression_list;
12235 tree member;
12236 cp_token *token = cp_lexer_peek_token (parser->lexer);
12238 /* Find out what is being initialized. */
12239 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12241 permerror (token->location,
12242 "anachronistic old-style base class initializer");
12243 mem_initializer_id = NULL_TREE;
12245 else
12247 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12248 if (mem_initializer_id == error_mark_node)
12249 return mem_initializer_id;
12251 member = expand_member_init (mem_initializer_id);
12252 if (member && !DECL_P (member))
12253 in_base_initializer = 1;
12255 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12257 bool expr_non_constant_p;
12258 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12259 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12260 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12261 expression_list = build_tree_list (NULL_TREE, expression_list);
12263 else
12265 vec<tree, va_gc> *vec;
12266 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12267 /*cast_p=*/false,
12268 /*allow_expansion_p=*/true,
12269 /*non_constant_p=*/NULL);
12270 if (vec == NULL)
12271 return error_mark_node;
12272 expression_list = build_tree_list_vec (vec);
12273 release_tree_vector (vec);
12276 if (expression_list == error_mark_node)
12277 return error_mark_node;
12278 if (!expression_list)
12279 expression_list = void_type_node;
12281 in_base_initializer = 0;
12283 return member ? build_tree_list (member, expression_list) : error_mark_node;
12286 /* Parse a mem-initializer-id.
12288 mem-initializer-id:
12289 :: [opt] nested-name-specifier [opt] class-name
12290 identifier
12292 Returns a TYPE indicating the class to be initializer for the first
12293 production. Returns an IDENTIFIER_NODE indicating the data member
12294 to be initialized for the second production. */
12296 static tree
12297 cp_parser_mem_initializer_id (cp_parser* parser)
12299 bool global_scope_p;
12300 bool nested_name_specifier_p;
12301 bool template_p = false;
12302 tree id;
12304 cp_token *token = cp_lexer_peek_token (parser->lexer);
12306 /* `typename' is not allowed in this context ([temp.res]). */
12307 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12309 error_at (token->location,
12310 "keyword %<typename%> not allowed in this context (a qualified "
12311 "member initializer is implicitly a type)");
12312 cp_lexer_consume_token (parser->lexer);
12314 /* Look for the optional `::' operator. */
12315 global_scope_p
12316 = (cp_parser_global_scope_opt (parser,
12317 /*current_scope_valid_p=*/false)
12318 != NULL_TREE);
12319 /* Look for the optional nested-name-specifier. The simplest way to
12320 implement:
12322 [temp.res]
12324 The keyword `typename' is not permitted in a base-specifier or
12325 mem-initializer; in these contexts a qualified name that
12326 depends on a template-parameter is implicitly assumed to be a
12327 type name.
12329 is to assume that we have seen the `typename' keyword at this
12330 point. */
12331 nested_name_specifier_p
12332 = (cp_parser_nested_name_specifier_opt (parser,
12333 /*typename_keyword_p=*/true,
12334 /*check_dependency_p=*/true,
12335 /*type_p=*/true,
12336 /*is_declaration=*/true)
12337 != NULL_TREE);
12338 if (nested_name_specifier_p)
12339 template_p = cp_parser_optional_template_keyword (parser);
12340 /* If there is a `::' operator or a nested-name-specifier, then we
12341 are definitely looking for a class-name. */
12342 if (global_scope_p || nested_name_specifier_p)
12343 return cp_parser_class_name (parser,
12344 /*typename_keyword_p=*/true,
12345 /*template_keyword_p=*/template_p,
12346 typename_type,
12347 /*check_dependency_p=*/true,
12348 /*class_head_p=*/false,
12349 /*is_declaration=*/true);
12350 /* Otherwise, we could also be looking for an ordinary identifier. */
12351 cp_parser_parse_tentatively (parser);
12352 /* Try a class-name. */
12353 id = cp_parser_class_name (parser,
12354 /*typename_keyword_p=*/true,
12355 /*template_keyword_p=*/false,
12356 none_type,
12357 /*check_dependency_p=*/true,
12358 /*class_head_p=*/false,
12359 /*is_declaration=*/true);
12360 /* If we found one, we're done. */
12361 if (cp_parser_parse_definitely (parser))
12362 return id;
12363 /* Otherwise, look for an ordinary identifier. */
12364 return cp_parser_identifier (parser);
12367 /* Overloading [gram.over] */
12369 /* Parse an operator-function-id.
12371 operator-function-id:
12372 operator operator
12374 Returns an IDENTIFIER_NODE for the operator which is a
12375 human-readable spelling of the identifier, e.g., `operator +'. */
12377 static tree
12378 cp_parser_operator_function_id (cp_parser* parser)
12380 /* Look for the `operator' keyword. */
12381 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12382 return error_mark_node;
12383 /* And then the name of the operator itself. */
12384 return cp_parser_operator (parser);
12387 /* Return an identifier node for a user-defined literal operator.
12388 The suffix identifier is chained to the operator name identifier. */
12390 static tree
12391 cp_literal_operator_id (const char* name)
12393 tree identifier;
12394 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12395 + strlen (name) + 10);
12396 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12397 identifier = get_identifier (buffer);
12399 return identifier;
12402 /* Parse an operator.
12404 operator:
12405 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12406 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12407 || ++ -- , ->* -> () []
12409 GNU Extensions:
12411 operator:
12412 <? >? <?= >?=
12414 Returns an IDENTIFIER_NODE for the operator which is a
12415 human-readable spelling of the identifier, e.g., `operator +'. */
12417 static tree
12418 cp_parser_operator (cp_parser* parser)
12420 tree id = NULL_TREE;
12421 cp_token *token;
12422 bool bad_encoding_prefix = false;
12424 /* Peek at the next token. */
12425 token = cp_lexer_peek_token (parser->lexer);
12426 /* Figure out which operator we have. */
12427 switch (token->type)
12429 case CPP_KEYWORD:
12431 enum tree_code op;
12433 /* The keyword should be either `new' or `delete'. */
12434 if (token->keyword == RID_NEW)
12435 op = NEW_EXPR;
12436 else if (token->keyword == RID_DELETE)
12437 op = DELETE_EXPR;
12438 else
12439 break;
12441 /* Consume the `new' or `delete' token. */
12442 cp_lexer_consume_token (parser->lexer);
12444 /* Peek at the next token. */
12445 token = cp_lexer_peek_token (parser->lexer);
12446 /* If it's a `[' token then this is the array variant of the
12447 operator. */
12448 if (token->type == CPP_OPEN_SQUARE)
12450 /* Consume the `[' token. */
12451 cp_lexer_consume_token (parser->lexer);
12452 /* Look for the `]' token. */
12453 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12454 id = ansi_opname (op == NEW_EXPR
12455 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12457 /* Otherwise, we have the non-array variant. */
12458 else
12459 id = ansi_opname (op);
12461 return id;
12464 case CPP_PLUS:
12465 id = ansi_opname (PLUS_EXPR);
12466 break;
12468 case CPP_MINUS:
12469 id = ansi_opname (MINUS_EXPR);
12470 break;
12472 case CPP_MULT:
12473 id = ansi_opname (MULT_EXPR);
12474 break;
12476 case CPP_DIV:
12477 id = ansi_opname (TRUNC_DIV_EXPR);
12478 break;
12480 case CPP_MOD:
12481 id = ansi_opname (TRUNC_MOD_EXPR);
12482 break;
12484 case CPP_XOR:
12485 id = ansi_opname (BIT_XOR_EXPR);
12486 break;
12488 case CPP_AND:
12489 id = ansi_opname (BIT_AND_EXPR);
12490 break;
12492 case CPP_OR:
12493 id = ansi_opname (BIT_IOR_EXPR);
12494 break;
12496 case CPP_COMPL:
12497 id = ansi_opname (BIT_NOT_EXPR);
12498 break;
12500 case CPP_NOT:
12501 id = ansi_opname (TRUTH_NOT_EXPR);
12502 break;
12504 case CPP_EQ:
12505 id = ansi_assopname (NOP_EXPR);
12506 break;
12508 case CPP_LESS:
12509 id = ansi_opname (LT_EXPR);
12510 break;
12512 case CPP_GREATER:
12513 id = ansi_opname (GT_EXPR);
12514 break;
12516 case CPP_PLUS_EQ:
12517 id = ansi_assopname (PLUS_EXPR);
12518 break;
12520 case CPP_MINUS_EQ:
12521 id = ansi_assopname (MINUS_EXPR);
12522 break;
12524 case CPP_MULT_EQ:
12525 id = ansi_assopname (MULT_EXPR);
12526 break;
12528 case CPP_DIV_EQ:
12529 id = ansi_assopname (TRUNC_DIV_EXPR);
12530 break;
12532 case CPP_MOD_EQ:
12533 id = ansi_assopname (TRUNC_MOD_EXPR);
12534 break;
12536 case CPP_XOR_EQ:
12537 id = ansi_assopname (BIT_XOR_EXPR);
12538 break;
12540 case CPP_AND_EQ:
12541 id = ansi_assopname (BIT_AND_EXPR);
12542 break;
12544 case CPP_OR_EQ:
12545 id = ansi_assopname (BIT_IOR_EXPR);
12546 break;
12548 case CPP_LSHIFT:
12549 id = ansi_opname (LSHIFT_EXPR);
12550 break;
12552 case CPP_RSHIFT:
12553 id = ansi_opname (RSHIFT_EXPR);
12554 break;
12556 case CPP_LSHIFT_EQ:
12557 id = ansi_assopname (LSHIFT_EXPR);
12558 break;
12560 case CPP_RSHIFT_EQ:
12561 id = ansi_assopname (RSHIFT_EXPR);
12562 break;
12564 case CPP_EQ_EQ:
12565 id = ansi_opname (EQ_EXPR);
12566 break;
12568 case CPP_NOT_EQ:
12569 id = ansi_opname (NE_EXPR);
12570 break;
12572 case CPP_LESS_EQ:
12573 id = ansi_opname (LE_EXPR);
12574 break;
12576 case CPP_GREATER_EQ:
12577 id = ansi_opname (GE_EXPR);
12578 break;
12580 case CPP_AND_AND:
12581 id = ansi_opname (TRUTH_ANDIF_EXPR);
12582 break;
12584 case CPP_OR_OR:
12585 id = ansi_opname (TRUTH_ORIF_EXPR);
12586 break;
12588 case CPP_PLUS_PLUS:
12589 id = ansi_opname (POSTINCREMENT_EXPR);
12590 break;
12592 case CPP_MINUS_MINUS:
12593 id = ansi_opname (PREDECREMENT_EXPR);
12594 break;
12596 case CPP_COMMA:
12597 id = ansi_opname (COMPOUND_EXPR);
12598 break;
12600 case CPP_DEREF_STAR:
12601 id = ansi_opname (MEMBER_REF);
12602 break;
12604 case CPP_DEREF:
12605 id = ansi_opname (COMPONENT_REF);
12606 break;
12608 case CPP_OPEN_PAREN:
12609 /* Consume the `('. */
12610 cp_lexer_consume_token (parser->lexer);
12611 /* Look for the matching `)'. */
12612 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12613 return ansi_opname (CALL_EXPR);
12615 case CPP_OPEN_SQUARE:
12616 /* Consume the `['. */
12617 cp_lexer_consume_token (parser->lexer);
12618 /* Look for the matching `]'. */
12619 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12620 return ansi_opname (ARRAY_REF);
12622 case CPP_WSTRING:
12623 case CPP_STRING16:
12624 case CPP_STRING32:
12625 case CPP_UTF8STRING:
12626 bad_encoding_prefix = true;
12627 /* Fall through. */
12629 case CPP_STRING:
12630 if (cxx_dialect == cxx98)
12631 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12632 if (bad_encoding_prefix)
12634 error ("invalid encoding prefix in literal operator");
12635 return error_mark_node;
12637 if (TREE_STRING_LENGTH (token->u.value) > 2)
12639 error ("expected empty string after %<operator%> keyword");
12640 return error_mark_node;
12642 /* Consume the string. */
12643 cp_lexer_consume_token (parser->lexer);
12644 /* Look for the suffix identifier. */
12645 token = cp_lexer_peek_token (parser->lexer);
12646 if (token->type == CPP_NAME)
12648 id = cp_parser_identifier (parser);
12649 if (id != error_mark_node)
12651 const char *name = IDENTIFIER_POINTER (id);
12652 return cp_literal_operator_id (name);
12655 else if (token->type == CPP_KEYWORD)
12657 error ("unexpected keyword;"
12658 " remove space between quotes and suffix identifier");
12659 return error_mark_node;
12661 else
12663 error ("expected suffix identifier");
12664 return error_mark_node;
12667 case CPP_WSTRING_USERDEF:
12668 case CPP_STRING16_USERDEF:
12669 case CPP_STRING32_USERDEF:
12670 case CPP_UTF8STRING_USERDEF:
12671 bad_encoding_prefix = true;
12672 /* Fall through. */
12674 case CPP_STRING_USERDEF:
12675 if (cxx_dialect == cxx98)
12676 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12677 if (bad_encoding_prefix)
12679 error ("invalid encoding prefix in literal operator");
12680 return error_mark_node;
12683 tree string_tree = USERDEF_LITERAL_VALUE (token->u.value);
12684 if (TREE_STRING_LENGTH (string_tree) > 2)
12686 error ("expected empty string after %<operator%> keyword");
12687 return error_mark_node;
12689 id = USERDEF_LITERAL_SUFFIX_ID (token->u.value);
12690 /* Consume the user-defined string literal. */
12691 cp_lexer_consume_token (parser->lexer);
12692 if (id != error_mark_node)
12694 const char *name = IDENTIFIER_POINTER (id);
12695 return cp_literal_operator_id (name);
12697 else
12698 return error_mark_node;
12701 default:
12702 /* Anything else is an error. */
12703 break;
12706 /* If we have selected an identifier, we need to consume the
12707 operator token. */
12708 if (id)
12709 cp_lexer_consume_token (parser->lexer);
12710 /* Otherwise, no valid operator name was present. */
12711 else
12713 cp_parser_error (parser, "expected operator");
12714 id = error_mark_node;
12717 return id;
12720 /* Parse a template-declaration.
12722 template-declaration:
12723 export [opt] template < template-parameter-list > declaration
12725 If MEMBER_P is TRUE, this template-declaration occurs within a
12726 class-specifier.
12728 The grammar rule given by the standard isn't correct. What
12729 is really meant is:
12731 template-declaration:
12732 export [opt] template-parameter-list-seq
12733 decl-specifier-seq [opt] init-declarator [opt] ;
12734 export [opt] template-parameter-list-seq
12735 function-definition
12737 template-parameter-list-seq:
12738 template-parameter-list-seq [opt]
12739 template < template-parameter-list > */
12741 static void
12742 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12744 /* Check for `export'. */
12745 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12747 /* Consume the `export' token. */
12748 cp_lexer_consume_token (parser->lexer);
12749 /* Warn that we do not support `export'. */
12750 warning (0, "keyword %<export%> not implemented, and will be ignored");
12753 cp_parser_template_declaration_after_export (parser, member_p);
12756 /* Parse a template-parameter-list.
12758 template-parameter-list:
12759 template-parameter
12760 template-parameter-list , template-parameter
12762 Returns a TREE_LIST. Each node represents a template parameter.
12763 The nodes are connected via their TREE_CHAINs. */
12765 static tree
12766 cp_parser_template_parameter_list (cp_parser* parser)
12768 tree parameter_list = NULL_TREE;
12770 begin_template_parm_list ();
12772 /* The loop below parses the template parms. We first need to know
12773 the total number of template parms to be able to compute proper
12774 canonical types of each dependent type. So after the loop, when
12775 we know the total number of template parms,
12776 end_template_parm_list computes the proper canonical types and
12777 fixes up the dependent types accordingly. */
12778 while (true)
12780 tree parameter;
12781 bool is_non_type;
12782 bool is_parameter_pack;
12783 location_t parm_loc;
12785 /* Parse the template-parameter. */
12786 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12787 parameter = cp_parser_template_parameter (parser,
12788 &is_non_type,
12789 &is_parameter_pack);
12790 /* Add it to the list. */
12791 if (parameter != error_mark_node)
12792 parameter_list = process_template_parm (parameter_list,
12793 parm_loc,
12794 parameter,
12795 is_non_type,
12796 is_parameter_pack);
12797 else
12799 tree err_parm = build_tree_list (parameter, parameter);
12800 parameter_list = chainon (parameter_list, err_parm);
12803 /* If the next token is not a `,', we're done. */
12804 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12805 break;
12806 /* Otherwise, consume the `,' token. */
12807 cp_lexer_consume_token (parser->lexer);
12810 return end_template_parm_list (parameter_list);
12813 /* Parse a template-parameter.
12815 template-parameter:
12816 type-parameter
12817 parameter-declaration
12819 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12820 the parameter. The TREE_PURPOSE is the default value, if any.
12821 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12822 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12823 set to true iff this parameter is a parameter pack. */
12825 static tree
12826 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12827 bool *is_parameter_pack)
12829 cp_token *token;
12830 cp_parameter_declarator *parameter_declarator;
12831 cp_declarator *id_declarator;
12832 tree parm;
12834 /* Assume it is a type parameter or a template parameter. */
12835 *is_non_type = false;
12836 /* Assume it not a parameter pack. */
12837 *is_parameter_pack = false;
12838 /* Peek at the next token. */
12839 token = cp_lexer_peek_token (parser->lexer);
12840 /* If it is `class' or `template', we have a type-parameter. */
12841 if (token->keyword == RID_TEMPLATE)
12842 return cp_parser_type_parameter (parser, is_parameter_pack);
12843 /* If it is `class' or `typename' we do not know yet whether it is a
12844 type parameter or a non-type parameter. Consider:
12846 template <typename T, typename T::X X> ...
12850 template <class C, class D*> ...
12852 Here, the first parameter is a type parameter, and the second is
12853 a non-type parameter. We can tell by looking at the token after
12854 the identifier -- if it is a `,', `=', or `>' then we have a type
12855 parameter. */
12856 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12858 /* Peek at the token after `class' or `typename'. */
12859 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12860 /* If it's an ellipsis, we have a template type parameter
12861 pack. */
12862 if (token->type == CPP_ELLIPSIS)
12863 return cp_parser_type_parameter (parser, is_parameter_pack);
12864 /* If it's an identifier, skip it. */
12865 if (token->type == CPP_NAME)
12866 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12867 /* Now, see if the token looks like the end of a template
12868 parameter. */
12869 if (token->type == CPP_COMMA
12870 || token->type == CPP_EQ
12871 || token->type == CPP_GREATER)
12872 return cp_parser_type_parameter (parser, is_parameter_pack);
12875 /* Otherwise, it is a non-type parameter.
12877 [temp.param]
12879 When parsing a default template-argument for a non-type
12880 template-parameter, the first non-nested `>' is taken as the end
12881 of the template parameter-list rather than a greater-than
12882 operator. */
12883 *is_non_type = true;
12884 parameter_declarator
12885 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12886 /*parenthesized_p=*/NULL);
12888 /* If the parameter declaration is marked as a parameter pack, set
12889 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12890 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12891 grokdeclarator. */
12892 if (parameter_declarator
12893 && parameter_declarator->declarator
12894 && parameter_declarator->declarator->parameter_pack_p)
12896 *is_parameter_pack = true;
12897 parameter_declarator->declarator->parameter_pack_p = false;
12900 if (parameter_declarator
12901 && parameter_declarator->default_argument)
12903 /* Can happen in some cases of erroneous input (c++/34892). */
12904 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12905 /* Consume the `...' for better error recovery. */
12906 cp_lexer_consume_token (parser->lexer);
12908 /* If the next token is an ellipsis, and we don't already have it
12909 marked as a parameter pack, then we have a parameter pack (that
12910 has no declarator). */
12911 else if (!*is_parameter_pack
12912 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12913 && (declarator_can_be_parameter_pack
12914 (parameter_declarator->declarator)))
12916 /* Consume the `...'. */
12917 cp_lexer_consume_token (parser->lexer);
12918 maybe_warn_variadic_templates ();
12920 *is_parameter_pack = true;
12922 /* We might end up with a pack expansion as the type of the non-type
12923 template parameter, in which case this is a non-type template
12924 parameter pack. */
12925 else if (parameter_declarator
12926 && parameter_declarator->decl_specifiers.type
12927 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12929 *is_parameter_pack = true;
12930 parameter_declarator->decl_specifiers.type =
12931 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12934 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12936 /* Parameter packs cannot have default arguments. However, a
12937 user may try to do so, so we'll parse them and give an
12938 appropriate diagnostic here. */
12940 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12942 /* Find the name of the parameter pack. */
12943 id_declarator = parameter_declarator->declarator;
12944 while (id_declarator && id_declarator->kind != cdk_id)
12945 id_declarator = id_declarator->declarator;
12947 if (id_declarator && id_declarator->kind == cdk_id)
12948 error_at (start_token->location,
12949 "template parameter pack %qD cannot have a default argument",
12950 id_declarator->u.id.unqualified_name);
12951 else
12952 error_at (start_token->location,
12953 "template parameter pack cannot have a default argument");
12955 /* Parse the default argument, but throw away the result. */
12956 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12959 parm = grokdeclarator (parameter_declarator->declarator,
12960 &parameter_declarator->decl_specifiers,
12961 TPARM, /*initialized=*/0,
12962 /*attrlist=*/NULL);
12963 if (parm == error_mark_node)
12964 return error_mark_node;
12966 return build_tree_list (parameter_declarator->default_argument, parm);
12969 /* Parse a type-parameter.
12971 type-parameter:
12972 class identifier [opt]
12973 class identifier [opt] = type-id
12974 typename identifier [opt]
12975 typename identifier [opt] = type-id
12976 template < template-parameter-list > class identifier [opt]
12977 template < template-parameter-list > class identifier [opt]
12978 = id-expression
12980 GNU Extension (variadic templates):
12982 type-parameter:
12983 class ... identifier [opt]
12984 typename ... identifier [opt]
12986 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12987 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12988 the declaration of the parameter.
12990 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12992 static tree
12993 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12995 cp_token *token;
12996 tree parameter;
12998 /* Look for a keyword to tell us what kind of parameter this is. */
12999 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13000 if (!token)
13001 return error_mark_node;
13003 switch (token->keyword)
13005 case RID_CLASS:
13006 case RID_TYPENAME:
13008 tree identifier;
13009 tree default_argument;
13011 /* If the next token is an ellipsis, we have a template
13012 argument pack. */
13013 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13015 /* Consume the `...' token. */
13016 cp_lexer_consume_token (parser->lexer);
13017 maybe_warn_variadic_templates ();
13019 *is_parameter_pack = true;
13022 /* If the next token is an identifier, then it names the
13023 parameter. */
13024 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13025 identifier = cp_parser_identifier (parser);
13026 else
13027 identifier = NULL_TREE;
13029 /* Create the parameter. */
13030 parameter = finish_template_type_parm (class_type_node, identifier);
13032 /* If the next token is an `=', we have a default argument. */
13033 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13035 /* Consume the `=' token. */
13036 cp_lexer_consume_token (parser->lexer);
13037 /* Parse the default-argument. */
13038 push_deferring_access_checks (dk_no_deferred);
13039 default_argument = cp_parser_type_id (parser);
13041 /* Template parameter packs cannot have default
13042 arguments. */
13043 if (*is_parameter_pack)
13045 if (identifier)
13046 error_at (token->location,
13047 "template parameter pack %qD cannot have a "
13048 "default argument", identifier);
13049 else
13050 error_at (token->location,
13051 "template parameter packs cannot have "
13052 "default arguments");
13053 default_argument = NULL_TREE;
13055 pop_deferring_access_checks ();
13057 else
13058 default_argument = NULL_TREE;
13060 /* Create the combined representation of the parameter and the
13061 default argument. */
13062 parameter = build_tree_list (default_argument, parameter);
13064 break;
13066 case RID_TEMPLATE:
13068 tree identifier;
13069 tree default_argument;
13071 /* Look for the `<'. */
13072 cp_parser_require (parser, CPP_LESS, RT_LESS);
13073 /* Parse the template-parameter-list. */
13074 cp_parser_template_parameter_list (parser);
13075 /* Look for the `>'. */
13076 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13077 /* Look for the `class' keyword. */
13078 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
13079 /* If the next token is an ellipsis, we have a template
13080 argument pack. */
13081 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13083 /* Consume the `...' token. */
13084 cp_lexer_consume_token (parser->lexer);
13085 maybe_warn_variadic_templates ();
13087 *is_parameter_pack = true;
13089 /* If the next token is an `=', then there is a
13090 default-argument. If the next token is a `>', we are at
13091 the end of the parameter-list. If the next token is a `,',
13092 then we are at the end of this parameter. */
13093 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13094 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13095 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13097 identifier = cp_parser_identifier (parser);
13098 /* Treat invalid names as if the parameter were nameless. */
13099 if (identifier == error_mark_node)
13100 identifier = NULL_TREE;
13102 else
13103 identifier = NULL_TREE;
13105 /* Create the template parameter. */
13106 parameter = finish_template_template_parm (class_type_node,
13107 identifier);
13109 /* If the next token is an `=', then there is a
13110 default-argument. */
13111 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13113 bool is_template;
13115 /* Consume the `='. */
13116 cp_lexer_consume_token (parser->lexer);
13117 /* Parse the id-expression. */
13118 push_deferring_access_checks (dk_no_deferred);
13119 /* save token before parsing the id-expression, for error
13120 reporting */
13121 token = cp_lexer_peek_token (parser->lexer);
13122 default_argument
13123 = cp_parser_id_expression (parser,
13124 /*template_keyword_p=*/false,
13125 /*check_dependency_p=*/true,
13126 /*template_p=*/&is_template,
13127 /*declarator_p=*/false,
13128 /*optional_p=*/false);
13129 if (TREE_CODE (default_argument) == TYPE_DECL)
13130 /* If the id-expression was a template-id that refers to
13131 a template-class, we already have the declaration here,
13132 so no further lookup is needed. */
13134 else
13135 /* Look up the name. */
13136 default_argument
13137 = cp_parser_lookup_name (parser, default_argument,
13138 none_type,
13139 /*is_template=*/is_template,
13140 /*is_namespace=*/false,
13141 /*check_dependency=*/true,
13142 /*ambiguous_decls=*/NULL,
13143 token->location);
13144 /* See if the default argument is valid. */
13145 default_argument
13146 = check_template_template_default_arg (default_argument);
13148 /* Template parameter packs cannot have default
13149 arguments. */
13150 if (*is_parameter_pack)
13152 if (identifier)
13153 error_at (token->location,
13154 "template parameter pack %qD cannot "
13155 "have a default argument",
13156 identifier);
13157 else
13158 error_at (token->location, "template parameter packs cannot "
13159 "have default arguments");
13160 default_argument = NULL_TREE;
13162 pop_deferring_access_checks ();
13164 else
13165 default_argument = NULL_TREE;
13167 /* Create the combined representation of the parameter and the
13168 default argument. */
13169 parameter = build_tree_list (default_argument, parameter);
13171 break;
13173 default:
13174 gcc_unreachable ();
13175 break;
13178 return parameter;
13181 /* Parse a template-id.
13183 template-id:
13184 template-name < template-argument-list [opt] >
13186 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13187 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13188 returned. Otherwise, if the template-name names a function, or set
13189 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13190 names a class, returns a TYPE_DECL for the specialization.
13192 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13193 uninstantiated templates. */
13195 static tree
13196 cp_parser_template_id (cp_parser *parser,
13197 bool template_keyword_p,
13198 bool check_dependency_p,
13199 enum tag_types tag_type,
13200 bool is_declaration)
13202 int i;
13203 tree templ;
13204 tree arguments;
13205 tree template_id;
13206 cp_token_position start_of_id = 0;
13207 deferred_access_check *chk;
13208 vec<deferred_access_check, va_gc> *access_check;
13209 cp_token *next_token = NULL, *next_token_2 = NULL;
13210 bool is_identifier;
13212 /* If the next token corresponds to a template-id, there is no need
13213 to reparse it. */
13214 next_token = cp_lexer_peek_token (parser->lexer);
13215 if (next_token->type == CPP_TEMPLATE_ID)
13217 struct tree_check *check_value;
13219 /* Get the stored value. */
13220 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13221 /* Perform any access checks that were deferred. */
13222 access_check = check_value->checks;
13223 if (access_check)
13225 FOR_EACH_VEC_ELT (*access_check, i, chk)
13226 perform_or_defer_access_check (chk->binfo,
13227 chk->decl,
13228 chk->diag_decl,
13229 tf_warning_or_error);
13231 /* Return the stored value. */
13232 return check_value->value;
13235 /* Avoid performing name lookup if there is no possibility of
13236 finding a template-id. */
13237 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13238 || (next_token->type == CPP_NAME
13239 && !cp_parser_nth_token_starts_template_argument_list_p
13240 (parser, 2)))
13242 cp_parser_error (parser, "expected template-id");
13243 return error_mark_node;
13246 /* Remember where the template-id starts. */
13247 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13248 start_of_id = cp_lexer_token_position (parser->lexer, false);
13250 push_deferring_access_checks (dk_deferred);
13252 /* Parse the template-name. */
13253 is_identifier = false;
13254 templ = cp_parser_template_name (parser, template_keyword_p,
13255 check_dependency_p,
13256 is_declaration,
13257 tag_type,
13258 &is_identifier);
13259 if (templ == error_mark_node || is_identifier)
13261 pop_deferring_access_checks ();
13262 return templ;
13265 /* If we find the sequence `[:' after a template-name, it's probably
13266 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13267 parse correctly the argument list. */
13268 next_token = cp_lexer_peek_token (parser->lexer);
13269 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13270 if (next_token->type == CPP_OPEN_SQUARE
13271 && next_token->flags & DIGRAPH
13272 && next_token_2->type == CPP_COLON
13273 && !(next_token_2->flags & PREV_WHITE))
13275 cp_parser_parse_tentatively (parser);
13276 /* Change `:' into `::'. */
13277 next_token_2->type = CPP_SCOPE;
13278 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13279 CPP_LESS. */
13280 cp_lexer_consume_token (parser->lexer);
13282 /* Parse the arguments. */
13283 arguments = cp_parser_enclosed_template_argument_list (parser);
13284 if (!cp_parser_parse_definitely (parser))
13286 /* If we couldn't parse an argument list, then we revert our changes
13287 and return simply an error. Maybe this is not a template-id
13288 after all. */
13289 next_token_2->type = CPP_COLON;
13290 cp_parser_error (parser, "expected %<<%>");
13291 pop_deferring_access_checks ();
13292 return error_mark_node;
13294 /* Otherwise, emit an error about the invalid digraph, but continue
13295 parsing because we got our argument list. */
13296 if (permerror (next_token->location,
13297 "%<<::%> cannot begin a template-argument list"))
13299 static bool hint = false;
13300 inform (next_token->location,
13301 "%<<:%> is an alternate spelling for %<[%>."
13302 " Insert whitespace between %<<%> and %<::%>");
13303 if (!hint && !flag_permissive)
13305 inform (next_token->location, "(if you use %<-fpermissive%> "
13306 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13307 "accept your code)");
13308 hint = true;
13312 else
13314 /* Look for the `<' that starts the template-argument-list. */
13315 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13317 pop_deferring_access_checks ();
13318 return error_mark_node;
13320 /* Parse the arguments. */
13321 arguments = cp_parser_enclosed_template_argument_list (parser);
13324 /* Build a representation of the specialization. */
13325 if (identifier_p (templ))
13326 template_id = build_min_nt_loc (next_token->location,
13327 TEMPLATE_ID_EXPR,
13328 templ, arguments);
13329 else if (DECL_TYPE_TEMPLATE_P (templ)
13330 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13332 bool entering_scope;
13333 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13334 template (rather than some instantiation thereof) only if
13335 is not nested within some other construct. For example, in
13336 "template <typename T> void f(T) { A<T>::", A<T> is just an
13337 instantiation of A. */
13338 entering_scope = (template_parm_scope_p ()
13339 && cp_lexer_next_token_is (parser->lexer,
13340 CPP_SCOPE));
13341 template_id
13342 = finish_template_type (templ, arguments, entering_scope);
13344 else
13346 /* If it's not a class-template or a template-template, it should be
13347 a function-template. */
13348 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13349 || TREE_CODE (templ) == OVERLOAD
13350 || BASELINK_P (templ)));
13352 template_id = lookup_template_function (templ, arguments);
13355 /* If parsing tentatively, replace the sequence of tokens that makes
13356 up the template-id with a CPP_TEMPLATE_ID token. That way,
13357 should we re-parse the token stream, we will not have to repeat
13358 the effort required to do the parse, nor will we issue duplicate
13359 error messages about problems during instantiation of the
13360 template. */
13361 if (start_of_id)
13363 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13365 /* Reset the contents of the START_OF_ID token. */
13366 token->type = CPP_TEMPLATE_ID;
13367 /* Retrieve any deferred checks. Do not pop this access checks yet
13368 so the memory will not be reclaimed during token replacing below. */
13369 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
13370 token->u.tree_check_value->value = template_id;
13371 token->u.tree_check_value->checks = get_deferred_access_checks ();
13372 token->keyword = RID_MAX;
13374 /* Purge all subsequent tokens. */
13375 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13377 /* ??? Can we actually assume that, if template_id ==
13378 error_mark_node, we will have issued a diagnostic to the
13379 user, as opposed to simply marking the tentative parse as
13380 failed? */
13381 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13382 error_at (token->location, "parse error in template argument list");
13385 pop_to_parent_deferring_access_checks ();
13386 return template_id;
13389 /* Parse a template-name.
13391 template-name:
13392 identifier
13394 The standard should actually say:
13396 template-name:
13397 identifier
13398 operator-function-id
13400 A defect report has been filed about this issue.
13402 A conversion-function-id cannot be a template name because they cannot
13403 be part of a template-id. In fact, looking at this code:
13405 a.operator K<int>()
13407 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13408 It is impossible to call a templated conversion-function-id with an
13409 explicit argument list, since the only allowed template parameter is
13410 the type to which it is converting.
13412 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13413 `template' keyword, in a construction like:
13415 T::template f<3>()
13417 In that case `f' is taken to be a template-name, even though there
13418 is no way of knowing for sure.
13420 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13421 name refers to a set of overloaded functions, at least one of which
13422 is a template, or an IDENTIFIER_NODE with the name of the template,
13423 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13424 names are looked up inside uninstantiated templates. */
13426 static tree
13427 cp_parser_template_name (cp_parser* parser,
13428 bool template_keyword_p,
13429 bool check_dependency_p,
13430 bool is_declaration,
13431 enum tag_types tag_type,
13432 bool *is_identifier)
13434 tree identifier;
13435 tree decl;
13436 tree fns;
13437 cp_token *token = cp_lexer_peek_token (parser->lexer);
13439 /* If the next token is `operator', then we have either an
13440 operator-function-id or a conversion-function-id. */
13441 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13443 /* We don't know whether we're looking at an
13444 operator-function-id or a conversion-function-id. */
13445 cp_parser_parse_tentatively (parser);
13446 /* Try an operator-function-id. */
13447 identifier = cp_parser_operator_function_id (parser);
13448 /* If that didn't work, try a conversion-function-id. */
13449 if (!cp_parser_parse_definitely (parser))
13451 cp_parser_error (parser, "expected template-name");
13452 return error_mark_node;
13455 /* Look for the identifier. */
13456 else
13457 identifier = cp_parser_identifier (parser);
13459 /* If we didn't find an identifier, we don't have a template-id. */
13460 if (identifier == error_mark_node)
13461 return error_mark_node;
13463 /* If the name immediately followed the `template' keyword, then it
13464 is a template-name. However, if the next token is not `<', then
13465 we do not treat it as a template-name, since it is not being used
13466 as part of a template-id. This enables us to handle constructs
13467 like:
13469 template <typename T> struct S { S(); };
13470 template <typename T> S<T>::S();
13472 correctly. We would treat `S' as a template -- if it were `S<T>'
13473 -- but we do not if there is no `<'. */
13475 if (processing_template_decl
13476 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13478 /* In a declaration, in a dependent context, we pretend that the
13479 "template" keyword was present in order to improve error
13480 recovery. For example, given:
13482 template <typename T> void f(T::X<int>);
13484 we want to treat "X<int>" as a template-id. */
13485 if (is_declaration
13486 && !template_keyword_p
13487 && parser->scope && TYPE_P (parser->scope)
13488 && check_dependency_p
13489 && dependent_scope_p (parser->scope)
13490 /* Do not do this for dtors (or ctors), since they never
13491 need the template keyword before their name. */
13492 && !constructor_name_p (identifier, parser->scope))
13494 cp_token_position start = 0;
13496 /* Explain what went wrong. */
13497 error_at (token->location, "non-template %qD used as template",
13498 identifier);
13499 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13500 parser->scope, identifier);
13501 /* If parsing tentatively, find the location of the "<" token. */
13502 if (cp_parser_simulate_error (parser))
13503 start = cp_lexer_token_position (parser->lexer, true);
13504 /* Parse the template arguments so that we can issue error
13505 messages about them. */
13506 cp_lexer_consume_token (parser->lexer);
13507 cp_parser_enclosed_template_argument_list (parser);
13508 /* Skip tokens until we find a good place from which to
13509 continue parsing. */
13510 cp_parser_skip_to_closing_parenthesis (parser,
13511 /*recovering=*/true,
13512 /*or_comma=*/true,
13513 /*consume_paren=*/false);
13514 /* If parsing tentatively, permanently remove the
13515 template argument list. That will prevent duplicate
13516 error messages from being issued about the missing
13517 "template" keyword. */
13518 if (start)
13519 cp_lexer_purge_tokens_after (parser->lexer, start);
13520 if (is_identifier)
13521 *is_identifier = true;
13522 return identifier;
13525 /* If the "template" keyword is present, then there is generally
13526 no point in doing name-lookup, so we just return IDENTIFIER.
13527 But, if the qualifying scope is non-dependent then we can
13528 (and must) do name-lookup normally. */
13529 if (template_keyword_p
13530 && (!parser->scope
13531 || (TYPE_P (parser->scope)
13532 && dependent_type_p (parser->scope))))
13533 return identifier;
13536 /* Look up the name. */
13537 decl = cp_parser_lookup_name (parser, identifier,
13538 tag_type,
13539 /*is_template=*/true,
13540 /*is_namespace=*/false,
13541 check_dependency_p,
13542 /*ambiguous_decls=*/NULL,
13543 token->location);
13545 /* If DECL is a template, then the name was a template-name. */
13546 if (TREE_CODE (decl) == TEMPLATE_DECL)
13548 else
13550 tree fn = NULL_TREE;
13552 /* The standard does not explicitly indicate whether a name that
13553 names a set of overloaded declarations, some of which are
13554 templates, is a template-name. However, such a name should
13555 be a template-name; otherwise, there is no way to form a
13556 template-id for the overloaded templates. */
13557 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13558 if (TREE_CODE (fns) == OVERLOAD)
13559 for (fn = fns; fn; fn = OVL_NEXT (fn))
13560 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13561 break;
13563 if (!fn)
13565 /* The name does not name a template. */
13566 cp_parser_error (parser, "expected template-name");
13567 return error_mark_node;
13571 /* If DECL is dependent, and refers to a function, then just return
13572 its name; we will look it up again during template instantiation. */
13573 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13575 tree scope = ovl_scope (decl);
13576 if (TYPE_P (scope) && dependent_type_p (scope))
13577 return identifier;
13580 return decl;
13583 /* Parse a template-argument-list.
13585 template-argument-list:
13586 template-argument ... [opt]
13587 template-argument-list , template-argument ... [opt]
13589 Returns a TREE_VEC containing the arguments. */
13591 static tree
13592 cp_parser_template_argument_list (cp_parser* parser)
13594 tree fixed_args[10];
13595 unsigned n_args = 0;
13596 unsigned alloced = 10;
13597 tree *arg_ary = fixed_args;
13598 tree vec;
13599 bool saved_in_template_argument_list_p;
13600 bool saved_ice_p;
13601 bool saved_non_ice_p;
13603 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13604 parser->in_template_argument_list_p = true;
13605 /* Even if the template-id appears in an integral
13606 constant-expression, the contents of the argument list do
13607 not. */
13608 saved_ice_p = parser->integral_constant_expression_p;
13609 parser->integral_constant_expression_p = false;
13610 saved_non_ice_p = parser->non_integral_constant_expression_p;
13611 parser->non_integral_constant_expression_p = false;
13613 /* Parse the arguments. */
13616 tree argument;
13618 if (n_args)
13619 /* Consume the comma. */
13620 cp_lexer_consume_token (parser->lexer);
13622 /* Parse the template-argument. */
13623 argument = cp_parser_template_argument (parser);
13625 /* If the next token is an ellipsis, we're expanding a template
13626 argument pack. */
13627 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13629 if (argument == error_mark_node)
13631 cp_token *token = cp_lexer_peek_token (parser->lexer);
13632 error_at (token->location,
13633 "expected parameter pack before %<...%>");
13635 /* Consume the `...' token. */
13636 cp_lexer_consume_token (parser->lexer);
13638 /* Make the argument into a TYPE_PACK_EXPANSION or
13639 EXPR_PACK_EXPANSION. */
13640 argument = make_pack_expansion (argument);
13643 if (n_args == alloced)
13645 alloced *= 2;
13647 if (arg_ary == fixed_args)
13649 arg_ary = XNEWVEC (tree, alloced);
13650 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13652 else
13653 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13655 arg_ary[n_args++] = argument;
13657 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13659 vec = make_tree_vec (n_args);
13661 while (n_args--)
13662 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13664 if (arg_ary != fixed_args)
13665 free (arg_ary);
13666 parser->non_integral_constant_expression_p = saved_non_ice_p;
13667 parser->integral_constant_expression_p = saved_ice_p;
13668 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13669 #ifdef ENABLE_CHECKING
13670 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13671 #endif
13672 return vec;
13675 /* Parse a template-argument.
13677 template-argument:
13678 assignment-expression
13679 type-id
13680 id-expression
13682 The representation is that of an assignment-expression, type-id, or
13683 id-expression -- except that the qualified id-expression is
13684 evaluated, so that the value returned is either a DECL or an
13685 OVERLOAD.
13687 Although the standard says "assignment-expression", it forbids
13688 throw-expressions or assignments in the template argument.
13689 Therefore, we use "conditional-expression" instead. */
13691 static tree
13692 cp_parser_template_argument (cp_parser* parser)
13694 tree argument;
13695 bool template_p;
13696 bool address_p;
13697 bool maybe_type_id = false;
13698 cp_token *token = NULL, *argument_start_token = NULL;
13699 location_t loc = 0;
13700 cp_id_kind idk;
13702 /* There's really no way to know what we're looking at, so we just
13703 try each alternative in order.
13705 [temp.arg]
13707 In a template-argument, an ambiguity between a type-id and an
13708 expression is resolved to a type-id, regardless of the form of
13709 the corresponding template-parameter.
13711 Therefore, we try a type-id first. */
13712 cp_parser_parse_tentatively (parser);
13713 argument = cp_parser_template_type_arg (parser);
13714 /* If there was no error parsing the type-id but the next token is a
13715 '>>', our behavior depends on which dialect of C++ we're
13716 parsing. In C++98, we probably found a typo for '> >'. But there
13717 are type-id which are also valid expressions. For instance:
13719 struct X { int operator >> (int); };
13720 template <int V> struct Foo {};
13721 Foo<X () >> 5> r;
13723 Here 'X()' is a valid type-id of a function type, but the user just
13724 wanted to write the expression "X() >> 5". Thus, we remember that we
13725 found a valid type-id, but we still try to parse the argument as an
13726 expression to see what happens.
13728 In C++0x, the '>>' will be considered two separate '>'
13729 tokens. */
13730 if (!cp_parser_error_occurred (parser)
13731 && cxx_dialect == cxx98
13732 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13734 maybe_type_id = true;
13735 cp_parser_abort_tentative_parse (parser);
13737 else
13739 /* If the next token isn't a `,' or a `>', then this argument wasn't
13740 really finished. This means that the argument is not a valid
13741 type-id. */
13742 if (!cp_parser_next_token_ends_template_argument_p (parser))
13743 cp_parser_error (parser, "expected template-argument");
13744 /* If that worked, we're done. */
13745 if (cp_parser_parse_definitely (parser))
13746 return argument;
13748 /* We're still not sure what the argument will be. */
13749 cp_parser_parse_tentatively (parser);
13750 /* Try a template. */
13751 argument_start_token = cp_lexer_peek_token (parser->lexer);
13752 argument = cp_parser_id_expression (parser,
13753 /*template_keyword_p=*/false,
13754 /*check_dependency_p=*/true,
13755 &template_p,
13756 /*declarator_p=*/false,
13757 /*optional_p=*/false);
13758 /* If the next token isn't a `,' or a `>', then this argument wasn't
13759 really finished. */
13760 if (!cp_parser_next_token_ends_template_argument_p (parser))
13761 cp_parser_error (parser, "expected template-argument");
13762 if (!cp_parser_error_occurred (parser))
13764 /* Figure out what is being referred to. If the id-expression
13765 was for a class template specialization, then we will have a
13766 TYPE_DECL at this point. There is no need to do name lookup
13767 at this point in that case. */
13768 if (TREE_CODE (argument) != TYPE_DECL)
13769 argument = cp_parser_lookup_name (parser, argument,
13770 none_type,
13771 /*is_template=*/template_p,
13772 /*is_namespace=*/false,
13773 /*check_dependency=*/true,
13774 /*ambiguous_decls=*/NULL,
13775 argument_start_token->location);
13776 if (TREE_CODE (argument) != TEMPLATE_DECL
13777 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13778 cp_parser_error (parser, "expected template-name");
13780 if (cp_parser_parse_definitely (parser))
13781 return argument;
13782 /* It must be a non-type argument. There permitted cases are given
13783 in [temp.arg.nontype]:
13785 -- an integral constant-expression of integral or enumeration
13786 type; or
13788 -- the name of a non-type template-parameter; or
13790 -- the name of an object or function with external linkage...
13792 -- the address of an object or function with external linkage...
13794 -- a pointer to member... */
13795 /* Look for a non-type template parameter. */
13796 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13798 cp_parser_parse_tentatively (parser);
13799 argument = cp_parser_primary_expression (parser,
13800 /*address_p=*/false,
13801 /*cast_p=*/false,
13802 /*template_arg_p=*/true,
13803 &idk);
13804 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13805 || !cp_parser_next_token_ends_template_argument_p (parser))
13806 cp_parser_simulate_error (parser);
13807 if (cp_parser_parse_definitely (parser))
13808 return argument;
13811 /* If the next token is "&", the argument must be the address of an
13812 object or function with external linkage. */
13813 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13814 if (address_p)
13816 loc = cp_lexer_peek_token (parser->lexer)->location;
13817 cp_lexer_consume_token (parser->lexer);
13819 /* See if we might have an id-expression. */
13820 token = cp_lexer_peek_token (parser->lexer);
13821 if (token->type == CPP_NAME
13822 || token->keyword == RID_OPERATOR
13823 || token->type == CPP_SCOPE
13824 || token->type == CPP_TEMPLATE_ID
13825 || token->type == CPP_NESTED_NAME_SPECIFIER)
13827 cp_parser_parse_tentatively (parser);
13828 argument = cp_parser_primary_expression (parser,
13829 address_p,
13830 /*cast_p=*/false,
13831 /*template_arg_p=*/true,
13832 &idk);
13833 if (cp_parser_error_occurred (parser)
13834 || !cp_parser_next_token_ends_template_argument_p (parser))
13835 cp_parser_abort_tentative_parse (parser);
13836 else
13838 tree probe;
13840 if (INDIRECT_REF_P (argument))
13842 gcc_assert (REFERENCE_REF_P (argument));
13843 argument = TREE_OPERAND (argument, 0);
13846 /* If we're in a template, we represent a qualified-id referring
13847 to a static data member as a SCOPE_REF even if the scope isn't
13848 dependent so that we can check access control later. */
13849 probe = argument;
13850 if (TREE_CODE (probe) == SCOPE_REF)
13851 probe = TREE_OPERAND (probe, 1);
13852 if (VAR_P (probe))
13854 /* A variable without external linkage might still be a
13855 valid constant-expression, so no error is issued here
13856 if the external-linkage check fails. */
13857 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13858 cp_parser_simulate_error (parser);
13860 else if (is_overloaded_fn (argument))
13861 /* All overloaded functions are allowed; if the external
13862 linkage test does not pass, an error will be issued
13863 later. */
13865 else if (address_p
13866 && (TREE_CODE (argument) == OFFSET_REF
13867 || TREE_CODE (argument) == SCOPE_REF))
13868 /* A pointer-to-member. */
13870 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13872 else
13873 cp_parser_simulate_error (parser);
13875 if (cp_parser_parse_definitely (parser))
13877 if (address_p)
13878 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13879 tf_warning_or_error);
13880 return argument;
13884 /* If the argument started with "&", there are no other valid
13885 alternatives at this point. */
13886 if (address_p)
13888 cp_parser_error (parser, "invalid non-type template argument");
13889 return error_mark_node;
13892 /* If the argument wasn't successfully parsed as a type-id followed
13893 by '>>', the argument can only be a constant expression now.
13894 Otherwise, we try parsing the constant-expression tentatively,
13895 because the argument could really be a type-id. */
13896 if (maybe_type_id)
13897 cp_parser_parse_tentatively (parser);
13898 argument = cp_parser_constant_expression (parser,
13899 /*allow_non_constant_p=*/false,
13900 /*non_constant_p=*/NULL);
13901 if (!maybe_type_id)
13902 return argument;
13903 if (!cp_parser_next_token_ends_template_argument_p (parser))
13904 cp_parser_error (parser, "expected template-argument");
13905 if (cp_parser_parse_definitely (parser))
13906 return argument;
13907 /* We did our best to parse the argument as a non type-id, but that
13908 was the only alternative that matched (albeit with a '>' after
13909 it). We can assume it's just a typo from the user, and a
13910 diagnostic will then be issued. */
13911 return cp_parser_template_type_arg (parser);
13914 /* Parse an explicit-instantiation.
13916 explicit-instantiation:
13917 template declaration
13919 Although the standard says `declaration', what it really means is:
13921 explicit-instantiation:
13922 template decl-specifier-seq [opt] declarator [opt] ;
13924 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13925 supposed to be allowed. A defect report has been filed about this
13926 issue.
13928 GNU Extension:
13930 explicit-instantiation:
13931 storage-class-specifier template
13932 decl-specifier-seq [opt] declarator [opt] ;
13933 function-specifier template
13934 decl-specifier-seq [opt] declarator [opt] ; */
13936 static void
13937 cp_parser_explicit_instantiation (cp_parser* parser)
13939 int declares_class_or_enum;
13940 cp_decl_specifier_seq decl_specifiers;
13941 tree extension_specifier = NULL_TREE;
13943 timevar_push (TV_TEMPLATE_INST);
13945 /* Look for an (optional) storage-class-specifier or
13946 function-specifier. */
13947 if (cp_parser_allow_gnu_extensions_p (parser))
13949 extension_specifier
13950 = cp_parser_storage_class_specifier_opt (parser);
13951 if (!extension_specifier)
13952 extension_specifier
13953 = cp_parser_function_specifier_opt (parser,
13954 /*decl_specs=*/NULL);
13957 /* Look for the `template' keyword. */
13958 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13959 /* Let the front end know that we are processing an explicit
13960 instantiation. */
13961 begin_explicit_instantiation ();
13962 /* [temp.explicit] says that we are supposed to ignore access
13963 control while processing explicit instantiation directives. */
13964 push_deferring_access_checks (dk_no_check);
13965 /* Parse a decl-specifier-seq. */
13966 cp_parser_decl_specifier_seq (parser,
13967 CP_PARSER_FLAGS_OPTIONAL,
13968 &decl_specifiers,
13969 &declares_class_or_enum);
13970 /* If there was exactly one decl-specifier, and it declared a class,
13971 and there's no declarator, then we have an explicit type
13972 instantiation. */
13973 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13975 tree type;
13977 type = check_tag_decl (&decl_specifiers,
13978 /*explicit_type_instantiation_p=*/true);
13979 /* Turn access control back on for names used during
13980 template instantiation. */
13981 pop_deferring_access_checks ();
13982 if (type)
13983 do_type_instantiation (type, extension_specifier,
13984 /*complain=*/tf_error);
13986 else
13988 cp_declarator *declarator;
13989 tree decl;
13991 /* Parse the declarator. */
13992 declarator
13993 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13994 /*ctor_dtor_or_conv_p=*/NULL,
13995 /*parenthesized_p=*/NULL,
13996 /*member_p=*/false);
13997 if (declares_class_or_enum & 2)
13998 cp_parser_check_for_definition_in_return_type (declarator,
13999 decl_specifiers.type,
14000 decl_specifiers.locations[ds_type_spec]);
14001 if (declarator != cp_error_declarator)
14003 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14004 permerror (decl_specifiers.locations[ds_inline],
14005 "explicit instantiation shall not use"
14006 " %<inline%> specifier");
14007 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14008 permerror (decl_specifiers.locations[ds_constexpr],
14009 "explicit instantiation shall not use"
14010 " %<constexpr%> specifier");
14012 decl = grokdeclarator (declarator, &decl_specifiers,
14013 NORMAL, 0, &decl_specifiers.attributes);
14014 /* Turn access control back on for names used during
14015 template instantiation. */
14016 pop_deferring_access_checks ();
14017 /* Do the explicit instantiation. */
14018 do_decl_instantiation (decl, extension_specifier);
14020 else
14022 pop_deferring_access_checks ();
14023 /* Skip the body of the explicit instantiation. */
14024 cp_parser_skip_to_end_of_statement (parser);
14027 /* We're done with the instantiation. */
14028 end_explicit_instantiation ();
14030 cp_parser_consume_semicolon_at_end_of_statement (parser);
14032 timevar_pop (TV_TEMPLATE_INST);
14035 /* Parse an explicit-specialization.
14037 explicit-specialization:
14038 template < > declaration
14040 Although the standard says `declaration', what it really means is:
14042 explicit-specialization:
14043 template <> decl-specifier [opt] init-declarator [opt] ;
14044 template <> function-definition
14045 template <> explicit-specialization
14046 template <> template-declaration */
14048 static void
14049 cp_parser_explicit_specialization (cp_parser* parser)
14051 bool need_lang_pop;
14052 cp_token *token = cp_lexer_peek_token (parser->lexer);
14054 /* Look for the `template' keyword. */
14055 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14056 /* Look for the `<'. */
14057 cp_parser_require (parser, CPP_LESS, RT_LESS);
14058 /* Look for the `>'. */
14059 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14060 /* We have processed another parameter list. */
14061 ++parser->num_template_parameter_lists;
14062 /* [temp]
14064 A template ... explicit specialization ... shall not have C
14065 linkage. */
14066 if (current_lang_name == lang_name_c)
14068 error_at (token->location, "template specialization with C linkage");
14069 /* Give it C++ linkage to avoid confusing other parts of the
14070 front end. */
14071 push_lang_context (lang_name_cplusplus);
14072 need_lang_pop = true;
14074 else
14075 need_lang_pop = false;
14076 /* Let the front end know that we are beginning a specialization. */
14077 if (!begin_specialization ())
14079 end_specialization ();
14080 return;
14083 /* If the next keyword is `template', we need to figure out whether
14084 or not we're looking a template-declaration. */
14085 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14087 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14088 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14089 cp_parser_template_declaration_after_export (parser,
14090 /*member_p=*/false);
14091 else
14092 cp_parser_explicit_specialization (parser);
14094 else
14095 /* Parse the dependent declaration. */
14096 cp_parser_single_declaration (parser,
14097 /*checks=*/NULL,
14098 /*member_p=*/false,
14099 /*explicit_specialization_p=*/true,
14100 /*friend_p=*/NULL);
14101 /* We're done with the specialization. */
14102 end_specialization ();
14103 /* For the erroneous case of a template with C linkage, we pushed an
14104 implicit C++ linkage scope; exit that scope now. */
14105 if (need_lang_pop)
14106 pop_lang_context ();
14107 /* We're done with this parameter list. */
14108 --parser->num_template_parameter_lists;
14111 /* Parse a type-specifier.
14113 type-specifier:
14114 simple-type-specifier
14115 class-specifier
14116 enum-specifier
14117 elaborated-type-specifier
14118 cv-qualifier
14120 GNU Extension:
14122 type-specifier:
14123 __complex__
14125 Returns a representation of the type-specifier. For a
14126 class-specifier, enum-specifier, or elaborated-type-specifier, a
14127 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14129 The parser flags FLAGS is used to control type-specifier parsing.
14131 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14132 in a decl-specifier-seq.
14134 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14135 class-specifier, enum-specifier, or elaborated-type-specifier, then
14136 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14137 if a type is declared; 2 if it is defined. Otherwise, it is set to
14138 zero.
14140 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14141 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14142 is set to FALSE. */
14144 static tree
14145 cp_parser_type_specifier (cp_parser* parser,
14146 cp_parser_flags flags,
14147 cp_decl_specifier_seq *decl_specs,
14148 bool is_declaration,
14149 int* declares_class_or_enum,
14150 bool* is_cv_qualifier)
14152 tree type_spec = NULL_TREE;
14153 cp_token *token;
14154 enum rid keyword;
14155 cp_decl_spec ds = ds_last;
14157 /* Assume this type-specifier does not declare a new type. */
14158 if (declares_class_or_enum)
14159 *declares_class_or_enum = 0;
14160 /* And that it does not specify a cv-qualifier. */
14161 if (is_cv_qualifier)
14162 *is_cv_qualifier = false;
14163 /* Peek at the next token. */
14164 token = cp_lexer_peek_token (parser->lexer);
14166 /* If we're looking at a keyword, we can use that to guide the
14167 production we choose. */
14168 keyword = token->keyword;
14169 switch (keyword)
14171 case RID_ENUM:
14172 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14173 goto elaborated_type_specifier;
14175 /* Look for the enum-specifier. */
14176 type_spec = cp_parser_enum_specifier (parser);
14177 /* If that worked, we're done. */
14178 if (type_spec)
14180 if (declares_class_or_enum)
14181 *declares_class_or_enum = 2;
14182 if (decl_specs)
14183 cp_parser_set_decl_spec_type (decl_specs,
14184 type_spec,
14185 token,
14186 /*type_definition_p=*/true);
14187 return type_spec;
14189 else
14190 goto elaborated_type_specifier;
14192 /* Any of these indicate either a class-specifier, or an
14193 elaborated-type-specifier. */
14194 case RID_CLASS:
14195 case RID_STRUCT:
14196 case RID_UNION:
14197 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14198 goto elaborated_type_specifier;
14200 /* Parse tentatively so that we can back up if we don't find a
14201 class-specifier. */
14202 cp_parser_parse_tentatively (parser);
14203 /* Look for the class-specifier. */
14204 type_spec = cp_parser_class_specifier (parser);
14205 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14206 /* If that worked, we're done. */
14207 if (cp_parser_parse_definitely (parser))
14209 if (declares_class_or_enum)
14210 *declares_class_or_enum = 2;
14211 if (decl_specs)
14212 cp_parser_set_decl_spec_type (decl_specs,
14213 type_spec,
14214 token,
14215 /*type_definition_p=*/true);
14216 return type_spec;
14219 /* Fall through. */
14220 elaborated_type_specifier:
14221 /* We're declaring (not defining) a class or enum. */
14222 if (declares_class_or_enum)
14223 *declares_class_or_enum = 1;
14225 /* Fall through. */
14226 case RID_TYPENAME:
14227 /* Look for an elaborated-type-specifier. */
14228 type_spec
14229 = (cp_parser_elaborated_type_specifier
14230 (parser,
14231 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14232 is_declaration));
14233 if (decl_specs)
14234 cp_parser_set_decl_spec_type (decl_specs,
14235 type_spec,
14236 token,
14237 /*type_definition_p=*/false);
14238 return type_spec;
14240 case RID_CONST:
14241 ds = ds_const;
14242 if (is_cv_qualifier)
14243 *is_cv_qualifier = true;
14244 break;
14246 case RID_VOLATILE:
14247 ds = ds_volatile;
14248 if (is_cv_qualifier)
14249 *is_cv_qualifier = true;
14250 break;
14252 case RID_RESTRICT:
14253 ds = ds_restrict;
14254 if (is_cv_qualifier)
14255 *is_cv_qualifier = true;
14256 break;
14258 case RID_COMPLEX:
14259 /* The `__complex__' keyword is a GNU extension. */
14260 ds = ds_complex;
14261 break;
14263 default:
14264 break;
14267 /* Handle simple keywords. */
14268 if (ds != ds_last)
14270 if (decl_specs)
14272 set_and_check_decl_spec_loc (decl_specs, ds, token);
14273 decl_specs->any_specifiers_p = true;
14275 return cp_lexer_consume_token (parser->lexer)->u.value;
14278 /* If we do not already have a type-specifier, assume we are looking
14279 at a simple-type-specifier. */
14280 type_spec = cp_parser_simple_type_specifier (parser,
14281 decl_specs,
14282 flags);
14284 /* If we didn't find a type-specifier, and a type-specifier was not
14285 optional in this context, issue an error message. */
14286 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14288 cp_parser_error (parser, "expected type specifier");
14289 return error_mark_node;
14292 return type_spec;
14295 /* Parse a simple-type-specifier.
14297 simple-type-specifier:
14298 :: [opt] nested-name-specifier [opt] type-name
14299 :: [opt] nested-name-specifier template template-id
14300 char
14301 wchar_t
14302 bool
14303 short
14305 long
14306 signed
14307 unsigned
14308 float
14309 double
14310 void
14312 C++0x Extension:
14314 simple-type-specifier:
14315 auto
14316 decltype ( expression )
14317 char16_t
14318 char32_t
14319 __underlying_type ( type-id )
14321 GNU Extension:
14323 simple-type-specifier:
14324 __int128
14325 __typeof__ unary-expression
14326 __typeof__ ( type-id )
14327 __typeof__ ( type-id ) { initializer-list , [opt] }
14329 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14330 appropriately updated. */
14332 static tree
14333 cp_parser_simple_type_specifier (cp_parser* parser,
14334 cp_decl_specifier_seq *decl_specs,
14335 cp_parser_flags flags)
14337 tree type = NULL_TREE;
14338 cp_token *token;
14340 /* Peek at the next token. */
14341 token = cp_lexer_peek_token (parser->lexer);
14343 /* If we're looking at a keyword, things are easy. */
14344 switch (token->keyword)
14346 case RID_CHAR:
14347 if (decl_specs)
14348 decl_specs->explicit_char_p = true;
14349 type = char_type_node;
14350 break;
14351 case RID_CHAR16:
14352 type = char16_type_node;
14353 break;
14354 case RID_CHAR32:
14355 type = char32_type_node;
14356 break;
14357 case RID_WCHAR:
14358 type = wchar_type_node;
14359 break;
14360 case RID_BOOL:
14361 type = boolean_type_node;
14362 break;
14363 case RID_SHORT:
14364 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14365 type = short_integer_type_node;
14366 break;
14367 case RID_INT:
14368 if (decl_specs)
14369 decl_specs->explicit_int_p = true;
14370 type = integer_type_node;
14371 break;
14372 case RID_INT128:
14373 if (!int128_integer_type_node)
14374 break;
14375 if (decl_specs)
14376 decl_specs->explicit_int128_p = true;
14377 type = int128_integer_type_node;
14378 break;
14379 case RID_LONG:
14380 if (decl_specs)
14381 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14382 type = long_integer_type_node;
14383 break;
14384 case RID_SIGNED:
14385 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14386 type = integer_type_node;
14387 break;
14388 case RID_UNSIGNED:
14389 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14390 type = unsigned_type_node;
14391 break;
14392 case RID_FLOAT:
14393 type = float_type_node;
14394 break;
14395 case RID_DOUBLE:
14396 type = double_type_node;
14397 break;
14398 case RID_VOID:
14399 type = void_type_node;
14400 break;
14402 case RID_AUTO:
14403 maybe_warn_cpp0x (CPP0X_AUTO);
14404 type = make_auto ();
14405 break;
14407 case RID_DECLTYPE:
14408 /* Since DR 743, decltype can either be a simple-type-specifier by
14409 itself or begin a nested-name-specifier. Parsing it will replace
14410 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14411 handling below decide what to do. */
14412 cp_parser_decltype (parser);
14413 cp_lexer_set_token_position (parser->lexer, token);
14414 break;
14416 case RID_TYPEOF:
14417 /* Consume the `typeof' token. */
14418 cp_lexer_consume_token (parser->lexer);
14419 /* Parse the operand to `typeof'. */
14420 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14421 /* If it is not already a TYPE, take its type. */
14422 if (!TYPE_P (type))
14423 type = finish_typeof (type);
14425 if (decl_specs)
14426 cp_parser_set_decl_spec_type (decl_specs, type,
14427 token,
14428 /*type_definition_p=*/false);
14430 return type;
14432 case RID_UNDERLYING_TYPE:
14433 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14434 if (decl_specs)
14435 cp_parser_set_decl_spec_type (decl_specs, type,
14436 token,
14437 /*type_definition_p=*/false);
14439 return type;
14441 case RID_BASES:
14442 case RID_DIRECT_BASES:
14443 type = cp_parser_trait_expr (parser, token->keyword);
14444 if (decl_specs)
14445 cp_parser_set_decl_spec_type (decl_specs, type,
14446 token,
14447 /*type_definition_p=*/false);
14448 return type;
14449 default:
14450 break;
14453 /* If token is an already-parsed decltype not followed by ::,
14454 it's a simple-type-specifier. */
14455 if (token->type == CPP_DECLTYPE
14456 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14458 type = token->u.value;
14459 if (decl_specs)
14460 cp_parser_set_decl_spec_type (decl_specs, type,
14461 token,
14462 /*type_definition_p=*/false);
14463 cp_lexer_consume_token (parser->lexer);
14464 return type;
14467 /* If the type-specifier was for a built-in type, we're done. */
14468 if (type)
14470 /* Record the type. */
14471 if (decl_specs
14472 && (token->keyword != RID_SIGNED
14473 && token->keyword != RID_UNSIGNED
14474 && token->keyword != RID_SHORT
14475 && token->keyword != RID_LONG))
14476 cp_parser_set_decl_spec_type (decl_specs,
14477 type,
14478 token,
14479 /*type_definition_p=*/false);
14480 if (decl_specs)
14481 decl_specs->any_specifiers_p = true;
14483 /* Consume the token. */
14484 cp_lexer_consume_token (parser->lexer);
14486 /* There is no valid C++ program where a non-template type is
14487 followed by a "<". That usually indicates that the user thought
14488 that the type was a template. */
14489 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14490 token->location);
14492 return TYPE_NAME (type);
14495 /* The type-specifier must be a user-defined type. */
14496 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14498 bool qualified_p;
14499 bool global_p;
14501 /* Don't gobble tokens or issue error messages if this is an
14502 optional type-specifier. */
14503 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14504 cp_parser_parse_tentatively (parser);
14506 /* Look for the optional `::' operator. */
14507 global_p
14508 = (cp_parser_global_scope_opt (parser,
14509 /*current_scope_valid_p=*/false)
14510 != NULL_TREE);
14511 /* Look for the nested-name specifier. */
14512 qualified_p
14513 = (cp_parser_nested_name_specifier_opt (parser,
14514 /*typename_keyword_p=*/false,
14515 /*check_dependency_p=*/true,
14516 /*type_p=*/false,
14517 /*is_declaration=*/false)
14518 != NULL_TREE);
14519 token = cp_lexer_peek_token (parser->lexer);
14520 /* If we have seen a nested-name-specifier, and the next token
14521 is `template', then we are using the template-id production. */
14522 if (parser->scope
14523 && cp_parser_optional_template_keyword (parser))
14525 /* Look for the template-id. */
14526 type = cp_parser_template_id (parser,
14527 /*template_keyword_p=*/true,
14528 /*check_dependency_p=*/true,
14529 none_type,
14530 /*is_declaration=*/false);
14531 /* If the template-id did not name a type, we are out of
14532 luck. */
14533 if (TREE_CODE (type) != TYPE_DECL)
14535 cp_parser_error (parser, "expected template-id for type");
14536 type = NULL_TREE;
14539 /* Otherwise, look for a type-name. */
14540 else
14541 type = cp_parser_type_name (parser);
14542 /* Keep track of all name-lookups performed in class scopes. */
14543 if (type
14544 && !global_p
14545 && !qualified_p
14546 && TREE_CODE (type) == TYPE_DECL
14547 && identifier_p (DECL_NAME (type)))
14548 maybe_note_name_used_in_class (DECL_NAME (type), type);
14549 /* If it didn't work out, we don't have a TYPE. */
14550 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14551 && !cp_parser_parse_definitely (parser))
14552 type = NULL_TREE;
14553 if (type && decl_specs)
14554 cp_parser_set_decl_spec_type (decl_specs, type,
14555 token,
14556 /*type_definition_p=*/false);
14559 /* If we didn't get a type-name, issue an error message. */
14560 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14562 cp_parser_error (parser, "expected type-name");
14563 return error_mark_node;
14566 if (type && type != error_mark_node)
14568 /* See if TYPE is an Objective-C type, and if so, parse and
14569 accept any protocol references following it. Do this before
14570 the cp_parser_check_for_invalid_template_id() call, because
14571 Objective-C types can be followed by '<...>' which would
14572 enclose protocol names rather than template arguments, and so
14573 everything is fine. */
14574 if (c_dialect_objc () && !parser->scope
14575 && (objc_is_id (type) || objc_is_class_name (type)))
14577 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14578 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14580 /* Clobber the "unqualified" type previously entered into
14581 DECL_SPECS with the new, improved protocol-qualified version. */
14582 if (decl_specs)
14583 decl_specs->type = qual_type;
14585 return qual_type;
14588 /* There is no valid C++ program where a non-template type is
14589 followed by a "<". That usually indicates that the user
14590 thought that the type was a template. */
14591 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14592 none_type,
14593 token->location);
14596 return type;
14599 /* Parse a type-name.
14601 type-name:
14602 class-name
14603 enum-name
14604 typedef-name
14605 simple-template-id [in c++0x]
14607 enum-name:
14608 identifier
14610 typedef-name:
14611 identifier
14613 Returns a TYPE_DECL for the type. */
14615 static tree
14616 cp_parser_type_name (cp_parser* parser)
14618 tree type_decl;
14620 /* We can't know yet whether it is a class-name or not. */
14621 cp_parser_parse_tentatively (parser);
14622 /* Try a class-name. */
14623 type_decl = cp_parser_class_name (parser,
14624 /*typename_keyword_p=*/false,
14625 /*template_keyword_p=*/false,
14626 none_type,
14627 /*check_dependency_p=*/true,
14628 /*class_head_p=*/false,
14629 /*is_declaration=*/false);
14630 /* If it's not a class-name, keep looking. */
14631 if (!cp_parser_parse_definitely (parser))
14633 if (cxx_dialect < cxx11)
14634 /* It must be a typedef-name or an enum-name. */
14635 return cp_parser_nonclass_name (parser);
14637 cp_parser_parse_tentatively (parser);
14638 /* It is either a simple-template-id representing an
14639 instantiation of an alias template... */
14640 type_decl = cp_parser_template_id (parser,
14641 /*template_keyword_p=*/false,
14642 /*check_dependency_p=*/false,
14643 none_type,
14644 /*is_declaration=*/false);
14645 /* Note that this must be an instantiation of an alias template
14646 because [temp.names]/6 says:
14648 A template-id that names an alias template specialization
14649 is a type-name.
14651 Whereas [temp.names]/7 says:
14653 A simple-template-id that names a class template
14654 specialization is a class-name. */
14655 if (type_decl != NULL_TREE
14656 && TREE_CODE (type_decl) == TYPE_DECL
14657 && TYPE_DECL_ALIAS_P (type_decl))
14658 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14659 else
14660 cp_parser_simulate_error (parser);
14662 if (!cp_parser_parse_definitely (parser))
14663 /* ... Or a typedef-name or an enum-name. */
14664 return cp_parser_nonclass_name (parser);
14667 return type_decl;
14670 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14672 enum-name:
14673 identifier
14675 typedef-name:
14676 identifier
14678 Returns a TYPE_DECL for the type. */
14680 static tree
14681 cp_parser_nonclass_name (cp_parser* parser)
14683 tree type_decl;
14684 tree identifier;
14686 cp_token *token = cp_lexer_peek_token (parser->lexer);
14687 identifier = cp_parser_identifier (parser);
14688 if (identifier == error_mark_node)
14689 return error_mark_node;
14691 /* Look up the type-name. */
14692 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14694 if (TREE_CODE (type_decl) == USING_DECL)
14696 if (!DECL_DEPENDENT_P (type_decl))
14697 type_decl = strip_using_decl (type_decl);
14698 else if (USING_DECL_TYPENAME_P (type_decl))
14700 /* We have found a type introduced by a using
14701 declaration at class scope that refers to a dependent
14702 type.
14704 using typename :: [opt] nested-name-specifier unqualified-id ;
14706 type_decl = make_typename_type (TREE_TYPE (type_decl),
14707 DECL_NAME (type_decl),
14708 typename_type, tf_error);
14709 if (type_decl != error_mark_node)
14710 type_decl = TYPE_NAME (type_decl);
14714 if (TREE_CODE (type_decl) != TYPE_DECL
14715 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14717 /* See if this is an Objective-C type. */
14718 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14719 tree type = objc_get_protocol_qualified_type (identifier, protos);
14720 if (type)
14721 type_decl = TYPE_NAME (type);
14724 /* Issue an error if we did not find a type-name. */
14725 if (TREE_CODE (type_decl) != TYPE_DECL
14726 /* In Objective-C, we have the complication that class names are
14727 normally type names and start declarations (eg, the
14728 "NSObject" in "NSObject *object;"), but can be used in an
14729 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14730 is an expression. So, a classname followed by a dot is not a
14731 valid type-name. */
14732 || (objc_is_class_name (TREE_TYPE (type_decl))
14733 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14735 if (!cp_parser_simulate_error (parser))
14736 cp_parser_name_lookup_error (parser, identifier, type_decl,
14737 NLE_TYPE, token->location);
14738 return error_mark_node;
14740 /* Remember that the name was used in the definition of the
14741 current class so that we can check later to see if the
14742 meaning would have been different after the class was
14743 entirely defined. */
14744 else if (type_decl != error_mark_node
14745 && !parser->scope)
14746 maybe_note_name_used_in_class (identifier, type_decl);
14748 return type_decl;
14751 /* Parse an elaborated-type-specifier. Note that the grammar given
14752 here incorporates the resolution to DR68.
14754 elaborated-type-specifier:
14755 class-key :: [opt] nested-name-specifier [opt] identifier
14756 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14757 enum-key :: [opt] nested-name-specifier [opt] identifier
14758 typename :: [opt] nested-name-specifier identifier
14759 typename :: [opt] nested-name-specifier template [opt]
14760 template-id
14762 GNU extension:
14764 elaborated-type-specifier:
14765 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14766 class-key attributes :: [opt] nested-name-specifier [opt]
14767 template [opt] template-id
14768 enum attributes :: [opt] nested-name-specifier [opt] identifier
14770 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14771 declared `friend'. If IS_DECLARATION is TRUE, then this
14772 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14773 something is being declared.
14775 Returns the TYPE specified. */
14777 static tree
14778 cp_parser_elaborated_type_specifier (cp_parser* parser,
14779 bool is_friend,
14780 bool is_declaration)
14782 enum tag_types tag_type;
14783 tree identifier;
14784 tree type = NULL_TREE;
14785 tree attributes = NULL_TREE;
14786 tree globalscope;
14787 cp_token *token = NULL;
14789 /* See if we're looking at the `enum' keyword. */
14790 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14792 /* Consume the `enum' token. */
14793 cp_lexer_consume_token (parser->lexer);
14794 /* Remember that it's an enumeration type. */
14795 tag_type = enum_type;
14796 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14797 enums) is used here. */
14798 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14799 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14801 pedwarn (input_location, 0, "elaborated-type-specifier "
14802 "for a scoped enum must not use the %<%D%> keyword",
14803 cp_lexer_peek_token (parser->lexer)->u.value);
14804 /* Consume the `struct' or `class' and parse it anyway. */
14805 cp_lexer_consume_token (parser->lexer);
14807 /* Parse the attributes. */
14808 attributes = cp_parser_attributes_opt (parser);
14810 /* Or, it might be `typename'. */
14811 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14812 RID_TYPENAME))
14814 /* Consume the `typename' token. */
14815 cp_lexer_consume_token (parser->lexer);
14816 /* Remember that it's a `typename' type. */
14817 tag_type = typename_type;
14819 /* Otherwise it must be a class-key. */
14820 else
14822 tag_type = cp_parser_class_key (parser);
14823 if (tag_type == none_type)
14824 return error_mark_node;
14825 /* Parse the attributes. */
14826 attributes = cp_parser_attributes_opt (parser);
14829 /* Look for the `::' operator. */
14830 globalscope = cp_parser_global_scope_opt (parser,
14831 /*current_scope_valid_p=*/false);
14832 /* Look for the nested-name-specifier. */
14833 if (tag_type == typename_type && !globalscope)
14835 if (!cp_parser_nested_name_specifier (parser,
14836 /*typename_keyword_p=*/true,
14837 /*check_dependency_p=*/true,
14838 /*type_p=*/true,
14839 is_declaration))
14840 return error_mark_node;
14842 else
14843 /* Even though `typename' is not present, the proposed resolution
14844 to Core Issue 180 says that in `class A<T>::B', `B' should be
14845 considered a type-name, even if `A<T>' is dependent. */
14846 cp_parser_nested_name_specifier_opt (parser,
14847 /*typename_keyword_p=*/true,
14848 /*check_dependency_p=*/true,
14849 /*type_p=*/true,
14850 is_declaration);
14851 /* For everything but enumeration types, consider a template-id.
14852 For an enumeration type, consider only a plain identifier. */
14853 if (tag_type != enum_type)
14855 bool template_p = false;
14856 tree decl;
14858 /* Allow the `template' keyword. */
14859 template_p = cp_parser_optional_template_keyword (parser);
14860 /* If we didn't see `template', we don't know if there's a
14861 template-id or not. */
14862 if (!template_p)
14863 cp_parser_parse_tentatively (parser);
14864 /* Parse the template-id. */
14865 token = cp_lexer_peek_token (parser->lexer);
14866 decl = cp_parser_template_id (parser, template_p,
14867 /*check_dependency_p=*/true,
14868 tag_type,
14869 is_declaration);
14870 /* If we didn't find a template-id, look for an ordinary
14871 identifier. */
14872 if (!template_p && !cp_parser_parse_definitely (parser))
14874 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14875 in effect, then we must assume that, upon instantiation, the
14876 template will correspond to a class. */
14877 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14878 && tag_type == typename_type)
14879 type = make_typename_type (parser->scope, decl,
14880 typename_type,
14881 /*complain=*/tf_error);
14882 /* If the `typename' keyword is in effect and DECL is not a type
14883 decl, then type is non existent. */
14884 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14886 else if (TREE_CODE (decl) == TYPE_DECL)
14887 type = check_elaborated_type_specifier (tag_type, decl,
14888 /*allow_template_p=*/true);
14889 else if (decl == error_mark_node)
14890 type = error_mark_node;
14893 if (!type)
14895 token = cp_lexer_peek_token (parser->lexer);
14896 identifier = cp_parser_identifier (parser);
14898 if (identifier == error_mark_node)
14900 parser->scope = NULL_TREE;
14901 return error_mark_node;
14904 /* For a `typename', we needn't call xref_tag. */
14905 if (tag_type == typename_type
14906 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14907 return cp_parser_make_typename_type (parser, parser->scope,
14908 identifier,
14909 token->location);
14910 /* Look up a qualified name in the usual way. */
14911 if (parser->scope)
14913 tree decl;
14914 tree ambiguous_decls;
14916 decl = cp_parser_lookup_name (parser, identifier,
14917 tag_type,
14918 /*is_template=*/false,
14919 /*is_namespace=*/false,
14920 /*check_dependency=*/true,
14921 &ambiguous_decls,
14922 token->location);
14924 /* If the lookup was ambiguous, an error will already have been
14925 issued. */
14926 if (ambiguous_decls)
14927 return error_mark_node;
14929 /* If we are parsing friend declaration, DECL may be a
14930 TEMPLATE_DECL tree node here. However, we need to check
14931 whether this TEMPLATE_DECL results in valid code. Consider
14932 the following example:
14934 namespace N {
14935 template <class T> class C {};
14937 class X {
14938 template <class T> friend class N::C; // #1, valid code
14940 template <class T> class Y {
14941 friend class N::C; // #2, invalid code
14944 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14945 name lookup of `N::C'. We see that friend declaration must
14946 be template for the code to be valid. Note that
14947 processing_template_decl does not work here since it is
14948 always 1 for the above two cases. */
14950 decl = (cp_parser_maybe_treat_template_as_class
14951 (decl, /*tag_name_p=*/is_friend
14952 && parser->num_template_parameter_lists));
14954 if (TREE_CODE (decl) != TYPE_DECL)
14956 cp_parser_diagnose_invalid_type_name (parser,
14957 parser->scope,
14958 identifier,
14959 token->location);
14960 return error_mark_node;
14963 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14965 bool allow_template = (parser->num_template_parameter_lists
14966 || DECL_SELF_REFERENCE_P (decl));
14967 type = check_elaborated_type_specifier (tag_type, decl,
14968 allow_template);
14970 if (type == error_mark_node)
14971 return error_mark_node;
14974 /* Forward declarations of nested types, such as
14976 class C1::C2;
14977 class C1::C2::C3;
14979 are invalid unless all components preceding the final '::'
14980 are complete. If all enclosing types are complete, these
14981 declarations become merely pointless.
14983 Invalid forward declarations of nested types are errors
14984 caught elsewhere in parsing. Those that are pointless arrive
14985 here. */
14987 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14988 && !is_friend && !processing_explicit_instantiation)
14989 warning (0, "declaration %qD does not declare anything", decl);
14991 type = TREE_TYPE (decl);
14993 else
14995 /* An elaborated-type-specifier sometimes introduces a new type and
14996 sometimes names an existing type. Normally, the rule is that it
14997 introduces a new type only if there is not an existing type of
14998 the same name already in scope. For example, given:
15000 struct S {};
15001 void f() { struct S s; }
15003 the `struct S' in the body of `f' is the same `struct S' as in
15004 the global scope; the existing definition is used. However, if
15005 there were no global declaration, this would introduce a new
15006 local class named `S'.
15008 An exception to this rule applies to the following code:
15010 namespace N { struct S; }
15012 Here, the elaborated-type-specifier names a new type
15013 unconditionally; even if there is already an `S' in the
15014 containing scope this declaration names a new type.
15015 This exception only applies if the elaborated-type-specifier
15016 forms the complete declaration:
15018 [class.name]
15020 A declaration consisting solely of `class-key identifier ;' is
15021 either a redeclaration of the name in the current scope or a
15022 forward declaration of the identifier as a class name. It
15023 introduces the name into the current scope.
15025 We are in this situation precisely when the next token is a `;'.
15027 An exception to the exception is that a `friend' declaration does
15028 *not* name a new type; i.e., given:
15030 struct S { friend struct T; };
15032 `T' is not a new type in the scope of `S'.
15034 Also, `new struct S' or `sizeof (struct S)' never results in the
15035 definition of a new type; a new type can only be declared in a
15036 declaration context. */
15038 tag_scope ts;
15039 bool template_p;
15041 if (is_friend)
15042 /* Friends have special name lookup rules. */
15043 ts = ts_within_enclosing_non_class;
15044 else if (is_declaration
15045 && cp_lexer_next_token_is (parser->lexer,
15046 CPP_SEMICOLON))
15047 /* This is a `class-key identifier ;' */
15048 ts = ts_current;
15049 else
15050 ts = ts_global;
15052 template_p =
15053 (parser->num_template_parameter_lists
15054 && (cp_parser_next_token_starts_class_definition_p (parser)
15055 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15056 /* An unqualified name was used to reference this type, so
15057 there were no qualifying templates. */
15058 if (!cp_parser_check_template_parameters (parser,
15059 /*num_templates=*/0,
15060 token->location,
15061 /*declarator=*/NULL))
15062 return error_mark_node;
15063 type = xref_tag (tag_type, identifier, ts, template_p);
15067 if (type == error_mark_node)
15068 return error_mark_node;
15070 /* Allow attributes on forward declarations of classes. */
15071 if (attributes)
15073 if (TREE_CODE (type) == TYPENAME_TYPE)
15074 warning (OPT_Wattributes,
15075 "attributes ignored on uninstantiated type");
15076 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15077 && ! processing_explicit_instantiation)
15078 warning (OPT_Wattributes,
15079 "attributes ignored on template instantiation");
15080 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15081 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15082 else
15083 warning (OPT_Wattributes,
15084 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15087 if (tag_type != enum_type)
15089 /* Indicate whether this class was declared as a `class' or as a
15090 `struct'. */
15091 if (TREE_CODE (type) == RECORD_TYPE)
15092 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15093 cp_parser_check_class_key (tag_type, type);
15096 /* A "<" cannot follow an elaborated type specifier. If that
15097 happens, the user was probably trying to form a template-id. */
15098 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15099 token->location);
15101 return type;
15104 /* Parse an enum-specifier.
15106 enum-specifier:
15107 enum-head { enumerator-list [opt] }
15108 enum-head { enumerator-list , } [C++0x]
15110 enum-head:
15111 enum-key identifier [opt] enum-base [opt]
15112 enum-key nested-name-specifier identifier enum-base [opt]
15114 enum-key:
15115 enum
15116 enum class [C++0x]
15117 enum struct [C++0x]
15119 enum-base: [C++0x]
15120 : type-specifier-seq
15122 opaque-enum-specifier:
15123 enum-key identifier enum-base [opt] ;
15125 GNU Extensions:
15126 enum-key attributes[opt] identifier [opt] enum-base [opt]
15127 { enumerator-list [opt] }attributes[opt]
15128 enum-key attributes[opt] identifier [opt] enum-base [opt]
15129 { enumerator-list, }attributes[opt] [C++0x]
15131 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15132 if the token stream isn't an enum-specifier after all. */
15134 static tree
15135 cp_parser_enum_specifier (cp_parser* parser)
15137 tree identifier;
15138 tree type = NULL_TREE;
15139 tree prev_scope;
15140 tree nested_name_specifier = NULL_TREE;
15141 tree attributes;
15142 bool scoped_enum_p = false;
15143 bool has_underlying_type = false;
15144 bool nested_being_defined = false;
15145 bool new_value_list = false;
15146 bool is_new_type = false;
15147 bool is_anonymous = false;
15148 tree underlying_type = NULL_TREE;
15149 cp_token *type_start_token = NULL;
15150 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15152 parser->colon_corrects_to_scope_p = false;
15154 /* Parse tentatively so that we can back up if we don't find a
15155 enum-specifier. */
15156 cp_parser_parse_tentatively (parser);
15158 /* Caller guarantees that the current token is 'enum', an identifier
15159 possibly follows, and the token after that is an opening brace.
15160 If we don't have an identifier, fabricate an anonymous name for
15161 the enumeration being defined. */
15162 cp_lexer_consume_token (parser->lexer);
15164 /* Parse the "class" or "struct", which indicates a scoped
15165 enumeration type in C++0x. */
15166 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15167 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15169 if (cxx_dialect < cxx11)
15170 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15172 /* Consume the `struct' or `class' token. */
15173 cp_lexer_consume_token (parser->lexer);
15175 scoped_enum_p = true;
15178 attributes = cp_parser_attributes_opt (parser);
15180 /* Clear the qualification. */
15181 parser->scope = NULL_TREE;
15182 parser->qualifying_scope = NULL_TREE;
15183 parser->object_scope = NULL_TREE;
15185 /* Figure out in what scope the declaration is being placed. */
15186 prev_scope = current_scope ();
15188 type_start_token = cp_lexer_peek_token (parser->lexer);
15190 push_deferring_access_checks (dk_no_check);
15191 nested_name_specifier
15192 = cp_parser_nested_name_specifier_opt (parser,
15193 /*typename_keyword_p=*/true,
15194 /*check_dependency_p=*/false,
15195 /*type_p=*/false,
15196 /*is_declaration=*/false);
15198 if (nested_name_specifier)
15200 tree name;
15202 identifier = cp_parser_identifier (parser);
15203 name = cp_parser_lookup_name (parser, identifier,
15204 enum_type,
15205 /*is_template=*/false,
15206 /*is_namespace=*/false,
15207 /*check_dependency=*/true,
15208 /*ambiguous_decls=*/NULL,
15209 input_location);
15210 if (name && name != error_mark_node)
15212 type = TREE_TYPE (name);
15213 if (TREE_CODE (type) == TYPENAME_TYPE)
15215 /* Are template enums allowed in ISO? */
15216 if (template_parm_scope_p ())
15217 pedwarn (type_start_token->location, OPT_Wpedantic,
15218 "%qD is an enumeration template", name);
15219 /* ignore a typename reference, for it will be solved by name
15220 in start_enum. */
15221 type = NULL_TREE;
15224 else if (nested_name_specifier == error_mark_node)
15225 /* We already issued an error. */;
15226 else
15227 error_at (type_start_token->location,
15228 "%qD is not an enumerator-name", identifier);
15230 else
15232 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15233 identifier = cp_parser_identifier (parser);
15234 else
15236 identifier = make_anon_name ();
15237 is_anonymous = true;
15238 if (scoped_enum_p)
15239 error_at (type_start_token->location,
15240 "anonymous scoped enum is not allowed");
15243 pop_deferring_access_checks ();
15245 /* Check for the `:' that denotes a specified underlying type in C++0x.
15246 Note that a ':' could also indicate a bitfield width, however. */
15247 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15249 cp_decl_specifier_seq type_specifiers;
15251 /* Consume the `:'. */
15252 cp_lexer_consume_token (parser->lexer);
15254 /* Parse the type-specifier-seq. */
15255 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15256 /*is_trailing_return=*/false,
15257 &type_specifiers);
15259 /* At this point this is surely not elaborated type specifier. */
15260 if (!cp_parser_parse_definitely (parser))
15261 return NULL_TREE;
15263 if (cxx_dialect < cxx11)
15264 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15266 has_underlying_type = true;
15268 /* If that didn't work, stop. */
15269 if (type_specifiers.type != error_mark_node)
15271 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15272 /*initialized=*/0, NULL);
15273 if (underlying_type == error_mark_node)
15274 underlying_type = NULL_TREE;
15278 /* Look for the `{' but don't consume it yet. */
15279 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15281 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15283 cp_parser_error (parser, "expected %<{%>");
15284 if (has_underlying_type)
15286 type = NULL_TREE;
15287 goto out;
15290 /* An opaque-enum-specifier must have a ';' here. */
15291 if ((scoped_enum_p || underlying_type)
15292 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15294 cp_parser_error (parser, "expected %<;%> or %<{%>");
15295 if (has_underlying_type)
15297 type = NULL_TREE;
15298 goto out;
15303 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15304 return NULL_TREE;
15306 if (nested_name_specifier)
15308 if (CLASS_TYPE_P (nested_name_specifier))
15310 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15311 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15312 push_scope (nested_name_specifier);
15314 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15316 push_nested_namespace (nested_name_specifier);
15320 /* Issue an error message if type-definitions are forbidden here. */
15321 if (!cp_parser_check_type_definition (parser))
15322 type = error_mark_node;
15323 else
15324 /* Create the new type. We do this before consuming the opening
15325 brace so the enum will be recorded as being on the line of its
15326 tag (or the 'enum' keyword, if there is no tag). */
15327 type = start_enum (identifier, type, underlying_type,
15328 scoped_enum_p, &is_new_type);
15330 /* If the next token is not '{' it is an opaque-enum-specifier or an
15331 elaborated-type-specifier. */
15332 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15334 timevar_push (TV_PARSE_ENUM);
15335 if (nested_name_specifier
15336 && nested_name_specifier != error_mark_node)
15338 /* The following catches invalid code such as:
15339 enum class S<int>::E { A, B, C }; */
15340 if (!processing_specialization
15341 && CLASS_TYPE_P (nested_name_specifier)
15342 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15343 error_at (type_start_token->location, "cannot add an enumerator "
15344 "list to a template instantiation");
15346 /* If that scope does not contain the scope in which the
15347 class was originally declared, the program is invalid. */
15348 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
15350 if (at_namespace_scope_p ())
15351 error_at (type_start_token->location,
15352 "declaration of %qD in namespace %qD which does not "
15353 "enclose %qD",
15354 type, prev_scope, nested_name_specifier);
15355 else
15356 error_at (type_start_token->location,
15357 "declaration of %qD in %qD which does not enclose %qD",
15358 type, prev_scope, nested_name_specifier);
15359 type = error_mark_node;
15363 if (scoped_enum_p)
15364 begin_scope (sk_scoped_enum, type);
15366 /* Consume the opening brace. */
15367 cp_lexer_consume_token (parser->lexer);
15369 if (type == error_mark_node)
15370 ; /* Nothing to add */
15371 else if (OPAQUE_ENUM_P (type)
15372 || (cxx_dialect > cxx98 && processing_specialization))
15374 new_value_list = true;
15375 SET_OPAQUE_ENUM_P (type, false);
15376 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15378 else
15380 error_at (type_start_token->location, "multiple definition of %q#T", type);
15381 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15382 "previous definition here");
15383 type = error_mark_node;
15386 if (type == error_mark_node)
15387 cp_parser_skip_to_end_of_block_or_statement (parser);
15388 /* If the next token is not '}', then there are some enumerators. */
15389 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15391 if (is_anonymous && !scoped_enum_p)
15392 pedwarn (type_start_token->location, OPT_Wpedantic,
15393 "ISO C++ forbids empty anonymous enum");
15395 else
15396 cp_parser_enumerator_list (parser, type);
15398 /* Consume the final '}'. */
15399 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15401 if (scoped_enum_p)
15402 finish_scope ();
15403 timevar_pop (TV_PARSE_ENUM);
15405 else
15407 /* If a ';' follows, then it is an opaque-enum-specifier
15408 and additional restrictions apply. */
15409 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15411 if (is_anonymous)
15412 error_at (type_start_token->location,
15413 "opaque-enum-specifier without name");
15414 else if (nested_name_specifier)
15415 error_at (type_start_token->location,
15416 "opaque-enum-specifier must use a simple identifier");
15420 /* Look for trailing attributes to apply to this enumeration, and
15421 apply them if appropriate. */
15422 if (cp_parser_allow_gnu_extensions_p (parser))
15424 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15425 trailing_attr = chainon (trailing_attr, attributes);
15426 cplus_decl_attributes (&type,
15427 trailing_attr,
15428 (int) ATTR_FLAG_TYPE_IN_PLACE);
15431 /* Finish up the enumeration. */
15432 if (type != error_mark_node)
15434 if (new_value_list)
15435 finish_enum_value_list (type);
15436 if (is_new_type)
15437 finish_enum (type);
15440 if (nested_name_specifier)
15442 if (CLASS_TYPE_P (nested_name_specifier))
15444 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15445 pop_scope (nested_name_specifier);
15447 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15449 pop_nested_namespace (nested_name_specifier);
15452 out:
15453 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15454 return type;
15457 /* Parse an enumerator-list. The enumerators all have the indicated
15458 TYPE.
15460 enumerator-list:
15461 enumerator-definition
15462 enumerator-list , enumerator-definition */
15464 static void
15465 cp_parser_enumerator_list (cp_parser* parser, tree type)
15467 while (true)
15469 /* Parse an enumerator-definition. */
15470 cp_parser_enumerator_definition (parser, type);
15472 /* If the next token is not a ',', we've reached the end of
15473 the list. */
15474 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15475 break;
15476 /* Otherwise, consume the `,' and keep going. */
15477 cp_lexer_consume_token (parser->lexer);
15478 /* If the next token is a `}', there is a trailing comma. */
15479 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15481 if (cxx_dialect < cxx11 && !in_system_header)
15482 pedwarn (input_location, OPT_Wpedantic,
15483 "comma at end of enumerator list");
15484 break;
15489 /* Parse an enumerator-definition. The enumerator has the indicated
15490 TYPE.
15492 enumerator-definition:
15493 enumerator
15494 enumerator = constant-expression
15496 enumerator:
15497 identifier */
15499 static void
15500 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15502 tree identifier;
15503 tree value;
15504 location_t loc;
15506 /* Save the input location because we are interested in the location
15507 of the identifier and not the location of the explicit value. */
15508 loc = cp_lexer_peek_token (parser->lexer)->location;
15510 /* Look for the identifier. */
15511 identifier = cp_parser_identifier (parser);
15512 if (identifier == error_mark_node)
15513 return;
15515 /* If the next token is an '=', then there is an explicit value. */
15516 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15518 /* Consume the `=' token. */
15519 cp_lexer_consume_token (parser->lexer);
15520 /* Parse the value. */
15521 value = cp_parser_constant_expression (parser,
15522 /*allow_non_constant_p=*/false,
15523 NULL);
15525 else
15526 value = NULL_TREE;
15528 /* If we are processing a template, make sure the initializer of the
15529 enumerator doesn't contain any bare template parameter pack. */
15530 if (check_for_bare_parameter_packs (value))
15531 value = error_mark_node;
15533 /* integral_constant_value will pull out this expression, so make sure
15534 it's folded as appropriate. */
15535 value = fold_non_dependent_expr (value);
15537 /* Create the enumerator. */
15538 build_enumerator (identifier, value, type, loc);
15541 /* Parse a namespace-name.
15543 namespace-name:
15544 original-namespace-name
15545 namespace-alias
15547 Returns the NAMESPACE_DECL for the namespace. */
15549 static tree
15550 cp_parser_namespace_name (cp_parser* parser)
15552 tree identifier;
15553 tree namespace_decl;
15555 cp_token *token = cp_lexer_peek_token (parser->lexer);
15557 /* Get the name of the namespace. */
15558 identifier = cp_parser_identifier (parser);
15559 if (identifier == error_mark_node)
15560 return error_mark_node;
15562 /* Look up the identifier in the currently active scope. Look only
15563 for namespaces, due to:
15565 [basic.lookup.udir]
15567 When looking up a namespace-name in a using-directive or alias
15568 definition, only namespace names are considered.
15570 And:
15572 [basic.lookup.qual]
15574 During the lookup of a name preceding the :: scope resolution
15575 operator, object, function, and enumerator names are ignored.
15577 (Note that cp_parser_qualifying_entity only calls this
15578 function if the token after the name is the scope resolution
15579 operator.) */
15580 namespace_decl = cp_parser_lookup_name (parser, identifier,
15581 none_type,
15582 /*is_template=*/false,
15583 /*is_namespace=*/true,
15584 /*check_dependency=*/true,
15585 /*ambiguous_decls=*/NULL,
15586 token->location);
15587 /* If it's not a namespace, issue an error. */
15588 if (namespace_decl == error_mark_node
15589 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15591 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15592 error_at (token->location, "%qD is not a namespace-name", identifier);
15593 cp_parser_error (parser, "expected namespace-name");
15594 namespace_decl = error_mark_node;
15597 return namespace_decl;
15600 /* Parse a namespace-definition.
15602 namespace-definition:
15603 named-namespace-definition
15604 unnamed-namespace-definition
15606 named-namespace-definition:
15607 original-namespace-definition
15608 extension-namespace-definition
15610 original-namespace-definition:
15611 namespace identifier { namespace-body }
15613 extension-namespace-definition:
15614 namespace original-namespace-name { namespace-body }
15616 unnamed-namespace-definition:
15617 namespace { namespace-body } */
15619 static void
15620 cp_parser_namespace_definition (cp_parser* parser)
15622 tree identifier, attribs;
15623 bool has_visibility;
15624 bool is_inline;
15626 cp_ensure_no_omp_declare_simd (parser);
15627 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15629 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15630 is_inline = true;
15631 cp_lexer_consume_token (parser->lexer);
15633 else
15634 is_inline = false;
15636 /* Look for the `namespace' keyword. */
15637 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15639 /* Get the name of the namespace. We do not attempt to distinguish
15640 between an original-namespace-definition and an
15641 extension-namespace-definition at this point. The semantic
15642 analysis routines are responsible for that. */
15643 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15644 identifier = cp_parser_identifier (parser);
15645 else
15646 identifier = NULL_TREE;
15648 /* Parse any specified attributes. */
15649 attribs = cp_parser_attributes_opt (parser);
15651 /* Look for the `{' to start the namespace. */
15652 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15653 /* Start the namespace. */
15654 push_namespace (identifier);
15656 /* "inline namespace" is equivalent to a stub namespace definition
15657 followed by a strong using directive. */
15658 if (is_inline)
15660 tree name_space = current_namespace;
15661 /* Set up namespace association. */
15662 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15663 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15664 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15665 /* Import the contents of the inline namespace. */
15666 pop_namespace ();
15667 do_using_directive (name_space);
15668 push_namespace (identifier);
15671 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15673 /* Parse the body of the namespace. */
15674 cp_parser_namespace_body (parser);
15676 if (has_visibility)
15677 pop_visibility (1);
15679 /* Finish the namespace. */
15680 pop_namespace ();
15681 /* Look for the final `}'. */
15682 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15685 /* Parse a namespace-body.
15687 namespace-body:
15688 declaration-seq [opt] */
15690 static void
15691 cp_parser_namespace_body (cp_parser* parser)
15693 cp_parser_declaration_seq_opt (parser);
15696 /* Parse a namespace-alias-definition.
15698 namespace-alias-definition:
15699 namespace identifier = qualified-namespace-specifier ; */
15701 static void
15702 cp_parser_namespace_alias_definition (cp_parser* parser)
15704 tree identifier;
15705 tree namespace_specifier;
15707 cp_token *token = cp_lexer_peek_token (parser->lexer);
15709 /* Look for the `namespace' keyword. */
15710 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15711 /* Look for the identifier. */
15712 identifier = cp_parser_identifier (parser);
15713 if (identifier == error_mark_node)
15714 return;
15715 /* Look for the `=' token. */
15716 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15717 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15719 error_at (token->location, "%<namespace%> definition is not allowed here");
15720 /* Skip the definition. */
15721 cp_lexer_consume_token (parser->lexer);
15722 if (cp_parser_skip_to_closing_brace (parser))
15723 cp_lexer_consume_token (parser->lexer);
15724 return;
15726 cp_parser_require (parser, CPP_EQ, RT_EQ);
15727 /* Look for the qualified-namespace-specifier. */
15728 namespace_specifier
15729 = cp_parser_qualified_namespace_specifier (parser);
15730 /* Look for the `;' token. */
15731 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15733 /* Register the alias in the symbol table. */
15734 do_namespace_alias (identifier, namespace_specifier);
15737 /* Parse a qualified-namespace-specifier.
15739 qualified-namespace-specifier:
15740 :: [opt] nested-name-specifier [opt] namespace-name
15742 Returns a NAMESPACE_DECL corresponding to the specified
15743 namespace. */
15745 static tree
15746 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15748 /* Look for the optional `::'. */
15749 cp_parser_global_scope_opt (parser,
15750 /*current_scope_valid_p=*/false);
15752 /* Look for the optional nested-name-specifier. */
15753 cp_parser_nested_name_specifier_opt (parser,
15754 /*typename_keyword_p=*/false,
15755 /*check_dependency_p=*/true,
15756 /*type_p=*/false,
15757 /*is_declaration=*/true);
15759 return cp_parser_namespace_name (parser);
15762 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15763 access declaration.
15765 using-declaration:
15766 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15767 using :: unqualified-id ;
15769 access-declaration:
15770 qualified-id ;
15774 static bool
15775 cp_parser_using_declaration (cp_parser* parser,
15776 bool access_declaration_p)
15778 cp_token *token;
15779 bool typename_p = false;
15780 bool global_scope_p;
15781 tree decl;
15782 tree identifier;
15783 tree qscope;
15784 int oldcount = errorcount;
15785 cp_token *diag_token = NULL;
15787 if (access_declaration_p)
15789 diag_token = cp_lexer_peek_token (parser->lexer);
15790 cp_parser_parse_tentatively (parser);
15792 else
15794 /* Look for the `using' keyword. */
15795 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15797 /* Peek at the next token. */
15798 token = cp_lexer_peek_token (parser->lexer);
15799 /* See if it's `typename'. */
15800 if (token->keyword == RID_TYPENAME)
15802 /* Remember that we've seen it. */
15803 typename_p = true;
15804 /* Consume the `typename' token. */
15805 cp_lexer_consume_token (parser->lexer);
15809 /* Look for the optional global scope qualification. */
15810 global_scope_p
15811 = (cp_parser_global_scope_opt (parser,
15812 /*current_scope_valid_p=*/false)
15813 != NULL_TREE);
15815 /* If we saw `typename', or didn't see `::', then there must be a
15816 nested-name-specifier present. */
15817 if (typename_p || !global_scope_p)
15818 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15819 /*check_dependency_p=*/true,
15820 /*type_p=*/false,
15821 /*is_declaration=*/true);
15822 /* Otherwise, we could be in either of the two productions. In that
15823 case, treat the nested-name-specifier as optional. */
15824 else
15825 qscope = cp_parser_nested_name_specifier_opt (parser,
15826 /*typename_keyword_p=*/false,
15827 /*check_dependency_p=*/true,
15828 /*type_p=*/false,
15829 /*is_declaration=*/true);
15830 if (!qscope)
15831 qscope = global_namespace;
15833 if (access_declaration_p && cp_parser_error_occurred (parser))
15834 /* Something has already gone wrong; there's no need to parse
15835 further. Since an error has occurred, the return value of
15836 cp_parser_parse_definitely will be false, as required. */
15837 return cp_parser_parse_definitely (parser);
15839 token = cp_lexer_peek_token (parser->lexer);
15840 /* Parse the unqualified-id. */
15841 identifier = cp_parser_unqualified_id (parser,
15842 /*template_keyword_p=*/false,
15843 /*check_dependency_p=*/true,
15844 /*declarator_p=*/true,
15845 /*optional_p=*/false);
15847 if (access_declaration_p)
15849 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15850 cp_parser_simulate_error (parser);
15851 if (!cp_parser_parse_definitely (parser))
15852 return false;
15855 /* The function we call to handle a using-declaration is different
15856 depending on what scope we are in. */
15857 if (qscope == error_mark_node || identifier == error_mark_node)
15859 else if (!identifier_p (identifier)
15860 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15861 /* [namespace.udecl]
15863 A using declaration shall not name a template-id. */
15864 error_at (token->location,
15865 "a template-id may not appear in a using-declaration");
15866 else
15868 if (at_class_scope_p ())
15870 /* Create the USING_DECL. */
15871 decl = do_class_using_decl (parser->scope, identifier);
15873 if (decl && typename_p)
15874 USING_DECL_TYPENAME_P (decl) = 1;
15876 if (check_for_bare_parameter_packs (decl))
15877 return false;
15878 else
15879 /* Add it to the list of members in this class. */
15880 finish_member_declaration (decl);
15882 else
15884 decl = cp_parser_lookup_name_simple (parser,
15885 identifier,
15886 token->location);
15887 if (decl == error_mark_node)
15888 cp_parser_name_lookup_error (parser, identifier,
15889 decl, NLE_NULL,
15890 token->location);
15891 else if (check_for_bare_parameter_packs (decl))
15892 return false;
15893 else if (!at_namespace_scope_p ())
15894 do_local_using_decl (decl, qscope, identifier);
15895 else
15896 do_toplevel_using_decl (decl, qscope, identifier);
15900 /* Look for the final `;'. */
15901 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15903 if (access_declaration_p && errorcount == oldcount)
15904 warning_at (diag_token->location, OPT_Wdeprecated,
15905 "access declarations are deprecated "
15906 "in favour of using-declarations; "
15907 "suggestion: add the %<using%> keyword");
15909 return true;
15912 /* Parse an alias-declaration.
15914 alias-declaration:
15915 using identifier attribute-specifier-seq [opt] = type-id */
15917 static tree
15918 cp_parser_alias_declaration (cp_parser* parser)
15920 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15921 location_t id_location;
15922 cp_declarator *declarator;
15923 cp_decl_specifier_seq decl_specs;
15924 bool member_p;
15925 const char *saved_message = NULL;
15927 /* Look for the `using' keyword. */
15928 cp_token *using_token
15929 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
15930 if (using_token == NULL)
15931 return error_mark_node;
15933 id_location = cp_lexer_peek_token (parser->lexer)->location;
15934 id = cp_parser_identifier (parser);
15935 if (id == error_mark_node)
15936 return error_mark_node;
15938 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
15939 attributes = cp_parser_attributes_opt (parser);
15940 if (attributes == error_mark_node)
15941 return error_mark_node;
15943 cp_parser_require (parser, CPP_EQ, RT_EQ);
15945 if (cp_parser_error_occurred (parser))
15946 return error_mark_node;
15948 cp_parser_commit_to_tentative_parse (parser);
15950 /* Now we are going to parse the type-id of the declaration. */
15953 [dcl.type]/3 says:
15955 "A type-specifier-seq shall not define a class or enumeration
15956 unless it appears in the type-id of an alias-declaration (7.1.3) that
15957 is not the declaration of a template-declaration."
15959 In other words, if we currently are in an alias template, the
15960 type-id should not define a type.
15962 So let's set parser->type_definition_forbidden_message in that
15963 case; cp_parser_check_type_definition (called by
15964 cp_parser_class_specifier) will then emit an error if a type is
15965 defined in the type-id. */
15966 if (parser->num_template_parameter_lists)
15968 saved_message = parser->type_definition_forbidden_message;
15969 parser->type_definition_forbidden_message =
15970 G_("types may not be defined in alias template declarations");
15973 type = cp_parser_type_id (parser);
15975 /* Restore the error message if need be. */
15976 if (parser->num_template_parameter_lists)
15977 parser->type_definition_forbidden_message = saved_message;
15979 if (type == error_mark_node)
15981 cp_parser_skip_to_end_of_block_or_statement (parser);
15982 return error_mark_node;
15985 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15987 if (cp_parser_error_occurred (parser))
15989 cp_parser_skip_to_end_of_block_or_statement (parser);
15990 return error_mark_node;
15993 /* A typedef-name can also be introduced by an alias-declaration. The
15994 identifier following the using keyword becomes a typedef-name. It has
15995 the same semantics as if it were introduced by the typedef
15996 specifier. In particular, it does not define a new type and it shall
15997 not appear in the type-id. */
15999 clear_decl_specs (&decl_specs);
16000 decl_specs.type = type;
16001 if (attributes != NULL_TREE)
16003 decl_specs.attributes = attributes;
16004 set_and_check_decl_spec_loc (&decl_specs,
16005 ds_attribute,
16006 attrs_token);
16008 set_and_check_decl_spec_loc (&decl_specs,
16009 ds_typedef,
16010 using_token);
16011 set_and_check_decl_spec_loc (&decl_specs,
16012 ds_alias,
16013 using_token);
16015 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16016 declarator->id_loc = id_location;
16018 member_p = at_class_scope_p ();
16019 if (member_p)
16020 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16021 NULL_TREE, attributes);
16022 else
16023 decl = start_decl (declarator, &decl_specs, 0,
16024 attributes, NULL_TREE, &pushed_scope);
16025 if (decl == error_mark_node)
16026 return decl;
16028 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16030 if (pushed_scope)
16031 pop_scope (pushed_scope);
16033 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16034 added into the symbol table; otherwise, return the TYPE_DECL. */
16035 if (DECL_LANG_SPECIFIC (decl)
16036 && DECL_TEMPLATE_INFO (decl)
16037 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16039 decl = DECL_TI_TEMPLATE (decl);
16040 if (member_p)
16041 check_member_template (decl);
16044 return decl;
16047 /* Parse a using-directive.
16049 using-directive:
16050 using namespace :: [opt] nested-name-specifier [opt]
16051 namespace-name ; */
16053 static void
16054 cp_parser_using_directive (cp_parser* parser)
16056 tree namespace_decl;
16057 tree attribs;
16059 /* Look for the `using' keyword. */
16060 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16061 /* And the `namespace' keyword. */
16062 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16063 /* Look for the optional `::' operator. */
16064 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16065 /* And the optional nested-name-specifier. */
16066 cp_parser_nested_name_specifier_opt (parser,
16067 /*typename_keyword_p=*/false,
16068 /*check_dependency_p=*/true,
16069 /*type_p=*/false,
16070 /*is_declaration=*/true);
16071 /* Get the namespace being used. */
16072 namespace_decl = cp_parser_namespace_name (parser);
16073 /* And any specified attributes. */
16074 attribs = cp_parser_attributes_opt (parser);
16075 /* Update the symbol table. */
16076 parse_using_directive (namespace_decl, attribs);
16077 /* Look for the final `;'. */
16078 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16081 /* Parse an asm-definition.
16083 asm-definition:
16084 asm ( string-literal ) ;
16086 GNU Extension:
16088 asm-definition:
16089 asm volatile [opt] ( string-literal ) ;
16090 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16091 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16092 : asm-operand-list [opt] ) ;
16093 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16094 : asm-operand-list [opt]
16095 : asm-clobber-list [opt] ) ;
16096 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16097 : asm-clobber-list [opt]
16098 : asm-goto-list ) ; */
16100 static void
16101 cp_parser_asm_definition (cp_parser* parser)
16103 tree string;
16104 tree outputs = NULL_TREE;
16105 tree inputs = NULL_TREE;
16106 tree clobbers = NULL_TREE;
16107 tree labels = NULL_TREE;
16108 tree asm_stmt;
16109 bool volatile_p = false;
16110 bool extended_p = false;
16111 bool invalid_inputs_p = false;
16112 bool invalid_outputs_p = false;
16113 bool goto_p = false;
16114 required_token missing = RT_NONE;
16116 /* Look for the `asm' keyword. */
16117 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16118 /* See if the next token is `volatile'. */
16119 if (cp_parser_allow_gnu_extensions_p (parser)
16120 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16122 /* Remember that we saw the `volatile' keyword. */
16123 volatile_p = true;
16124 /* Consume the token. */
16125 cp_lexer_consume_token (parser->lexer);
16127 if (cp_parser_allow_gnu_extensions_p (parser)
16128 && parser->in_function_body
16129 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16131 /* Remember that we saw the `goto' keyword. */
16132 goto_p = true;
16133 /* Consume the token. */
16134 cp_lexer_consume_token (parser->lexer);
16136 /* Look for the opening `('. */
16137 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16138 return;
16139 /* Look for the string. */
16140 string = cp_parser_string_literal (parser, false, false);
16141 if (string == error_mark_node)
16143 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16144 /*consume_paren=*/true);
16145 return;
16148 /* If we're allowing GNU extensions, check for the extended assembly
16149 syntax. Unfortunately, the `:' tokens need not be separated by
16150 a space in C, and so, for compatibility, we tolerate that here
16151 too. Doing that means that we have to treat the `::' operator as
16152 two `:' tokens. */
16153 if (cp_parser_allow_gnu_extensions_p (parser)
16154 && parser->in_function_body
16155 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16156 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16158 bool inputs_p = false;
16159 bool clobbers_p = false;
16160 bool labels_p = false;
16162 /* The extended syntax was used. */
16163 extended_p = true;
16165 /* Look for outputs. */
16166 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16168 /* Consume the `:'. */
16169 cp_lexer_consume_token (parser->lexer);
16170 /* Parse the output-operands. */
16171 if (cp_lexer_next_token_is_not (parser->lexer,
16172 CPP_COLON)
16173 && cp_lexer_next_token_is_not (parser->lexer,
16174 CPP_SCOPE)
16175 && cp_lexer_next_token_is_not (parser->lexer,
16176 CPP_CLOSE_PAREN)
16177 && !goto_p)
16178 outputs = cp_parser_asm_operand_list (parser);
16180 if (outputs == error_mark_node)
16181 invalid_outputs_p = true;
16183 /* If the next token is `::', there are no outputs, and the
16184 next token is the beginning of the inputs. */
16185 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16186 /* The inputs are coming next. */
16187 inputs_p = true;
16189 /* Look for inputs. */
16190 if (inputs_p
16191 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16193 /* Consume the `:' or `::'. */
16194 cp_lexer_consume_token (parser->lexer);
16195 /* Parse the output-operands. */
16196 if (cp_lexer_next_token_is_not (parser->lexer,
16197 CPP_COLON)
16198 && cp_lexer_next_token_is_not (parser->lexer,
16199 CPP_SCOPE)
16200 && cp_lexer_next_token_is_not (parser->lexer,
16201 CPP_CLOSE_PAREN))
16202 inputs = cp_parser_asm_operand_list (parser);
16204 if (inputs == error_mark_node)
16205 invalid_inputs_p = true;
16207 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16208 /* The clobbers are coming next. */
16209 clobbers_p = true;
16211 /* Look for clobbers. */
16212 if (clobbers_p
16213 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16215 clobbers_p = true;
16216 /* Consume the `:' or `::'. */
16217 cp_lexer_consume_token (parser->lexer);
16218 /* Parse the clobbers. */
16219 if (cp_lexer_next_token_is_not (parser->lexer,
16220 CPP_COLON)
16221 && cp_lexer_next_token_is_not (parser->lexer,
16222 CPP_CLOSE_PAREN))
16223 clobbers = cp_parser_asm_clobber_list (parser);
16225 else if (goto_p
16226 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16227 /* The labels are coming next. */
16228 labels_p = true;
16230 /* Look for labels. */
16231 if (labels_p
16232 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16234 labels_p = true;
16235 /* Consume the `:' or `::'. */
16236 cp_lexer_consume_token (parser->lexer);
16237 /* Parse the labels. */
16238 labels = cp_parser_asm_label_list (parser);
16241 if (goto_p && !labels_p)
16242 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16244 else if (goto_p)
16245 missing = RT_COLON_SCOPE;
16247 /* Look for the closing `)'. */
16248 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16249 missing ? missing : RT_CLOSE_PAREN))
16250 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16251 /*consume_paren=*/true);
16252 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16254 if (!invalid_inputs_p && !invalid_outputs_p)
16256 /* Create the ASM_EXPR. */
16257 if (parser->in_function_body)
16259 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16260 inputs, clobbers, labels);
16261 /* If the extended syntax was not used, mark the ASM_EXPR. */
16262 if (!extended_p)
16264 tree temp = asm_stmt;
16265 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16266 temp = TREE_OPERAND (temp, 0);
16268 ASM_INPUT_P (temp) = 1;
16271 else
16272 add_asm_node (string);
16276 /* Declarators [gram.dcl.decl] */
16278 /* Parse an init-declarator.
16280 init-declarator:
16281 declarator initializer [opt]
16283 GNU Extension:
16285 init-declarator:
16286 declarator asm-specification [opt] attributes [opt] initializer [opt]
16288 function-definition:
16289 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16290 function-body
16291 decl-specifier-seq [opt] declarator function-try-block
16293 GNU Extension:
16295 function-definition:
16296 __extension__ function-definition
16298 TM Extension:
16300 function-definition:
16301 decl-specifier-seq [opt] declarator function-transaction-block
16303 The DECL_SPECIFIERS apply to this declarator. Returns a
16304 representation of the entity declared. If MEMBER_P is TRUE, then
16305 this declarator appears in a class scope. The new DECL created by
16306 this declarator is returned.
16308 The CHECKS are access checks that should be performed once we know
16309 what entity is being declared (and, therefore, what classes have
16310 befriended it).
16312 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16313 for a function-definition here as well. If the declarator is a
16314 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16315 be TRUE upon return. By that point, the function-definition will
16316 have been completely parsed.
16318 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16319 is FALSE.
16321 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16322 parsed declaration if it is an uninitialized single declarator not followed
16323 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16324 if present, will not be consumed. If returned, this declarator will be
16325 created with SD_INITIALIZED but will not call cp_finish_decl. */
16327 static tree
16328 cp_parser_init_declarator (cp_parser* parser,
16329 cp_decl_specifier_seq *decl_specifiers,
16330 vec<deferred_access_check, va_gc> *checks,
16331 bool function_definition_allowed_p,
16332 bool member_p,
16333 int declares_class_or_enum,
16334 bool* function_definition_p,
16335 tree* maybe_range_for_decl)
16337 cp_token *token = NULL, *asm_spec_start_token = NULL,
16338 *attributes_start_token = NULL;
16339 cp_declarator *declarator;
16340 tree prefix_attributes;
16341 tree attributes = NULL;
16342 tree asm_specification;
16343 tree initializer;
16344 tree decl = NULL_TREE;
16345 tree scope;
16346 int is_initialized;
16347 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16348 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16349 "(...)". */
16350 enum cpp_ttype initialization_kind;
16351 bool is_direct_init = false;
16352 bool is_non_constant_init;
16353 int ctor_dtor_or_conv_p;
16354 bool friend_p;
16355 tree pushed_scope = NULL_TREE;
16356 bool range_for_decl_p = false;
16357 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16359 /* Gather the attributes that were provided with the
16360 decl-specifiers. */
16361 prefix_attributes = decl_specifiers->attributes;
16363 /* Assume that this is not the declarator for a function
16364 definition. */
16365 if (function_definition_p)
16366 *function_definition_p = false;
16368 /* Default arguments are only permitted for function parameters. */
16369 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16370 parser->default_arg_ok_p = false;
16372 /* Defer access checks while parsing the declarator; we cannot know
16373 what names are accessible until we know what is being
16374 declared. */
16375 resume_deferring_access_checks ();
16377 /* Parse the declarator. */
16378 token = cp_lexer_peek_token (parser->lexer);
16379 declarator
16380 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16381 &ctor_dtor_or_conv_p,
16382 /*parenthesized_p=*/NULL,
16383 member_p);
16384 /* Gather up the deferred checks. */
16385 stop_deferring_access_checks ();
16387 parser->default_arg_ok_p = saved_default_arg_ok_p;
16389 /* If the DECLARATOR was erroneous, there's no need to go
16390 further. */
16391 if (declarator == cp_error_declarator)
16392 return error_mark_node;
16394 /* Check that the number of template-parameter-lists is OK. */
16395 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16396 token->location))
16397 return error_mark_node;
16399 if (declares_class_or_enum & 2)
16400 cp_parser_check_for_definition_in_return_type (declarator,
16401 decl_specifiers->type,
16402 decl_specifiers->locations[ds_type_spec]);
16404 /* Figure out what scope the entity declared by the DECLARATOR is
16405 located in. `grokdeclarator' sometimes changes the scope, so
16406 we compute it now. */
16407 scope = get_scope_of_declarator (declarator);
16409 /* Perform any lookups in the declared type which were thought to be
16410 dependent, but are not in the scope of the declarator. */
16411 decl_specifiers->type
16412 = maybe_update_decl_type (decl_specifiers->type, scope);
16414 /* If we're allowing GNU extensions, look for an
16415 asm-specification. */
16416 if (cp_parser_allow_gnu_extensions_p (parser))
16418 /* Look for an asm-specification. */
16419 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16420 asm_specification = cp_parser_asm_specification_opt (parser);
16422 else
16423 asm_specification = NULL_TREE;
16425 /* Look for attributes. */
16426 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16427 attributes = cp_parser_attributes_opt (parser);
16429 /* Peek at the next token. */
16430 token = cp_lexer_peek_token (parser->lexer);
16432 if (function_declarator_p (declarator))
16434 /* Check to see if the token indicates the start of a
16435 function-definition. */
16436 if (cp_parser_token_starts_function_definition_p (token))
16438 if (!function_definition_allowed_p)
16440 /* If a function-definition should not appear here, issue an
16441 error message. */
16442 cp_parser_error (parser,
16443 "a function-definition is not allowed here");
16444 return error_mark_node;
16447 location_t func_brace_location
16448 = cp_lexer_peek_token (parser->lexer)->location;
16450 /* Neither attributes nor an asm-specification are allowed
16451 on a function-definition. */
16452 if (asm_specification)
16453 error_at (asm_spec_start_token->location,
16454 "an asm-specification is not allowed "
16455 "on a function-definition");
16456 if (attributes)
16457 error_at (attributes_start_token->location,
16458 "attributes are not allowed "
16459 "on a function-definition");
16460 /* This is a function-definition. */
16461 *function_definition_p = true;
16463 /* Parse the function definition. */
16464 if (member_p)
16465 decl = cp_parser_save_member_function_body (parser,
16466 decl_specifiers,
16467 declarator,
16468 prefix_attributes);
16469 else
16470 decl =
16471 (cp_parser_function_definition_from_specifiers_and_declarator
16472 (parser, decl_specifiers, prefix_attributes, declarator));
16474 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16476 /* This is where the prologue starts... */
16477 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16478 = func_brace_location;
16481 return decl;
16485 /* [dcl.dcl]
16487 Only in function declarations for constructors, destructors, and
16488 type conversions can the decl-specifier-seq be omitted.
16490 We explicitly postpone this check past the point where we handle
16491 function-definitions because we tolerate function-definitions
16492 that are missing their return types in some modes. */
16493 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16495 cp_parser_error (parser,
16496 "expected constructor, destructor, or type conversion");
16497 return error_mark_node;
16500 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16501 if (token->type == CPP_EQ
16502 || token->type == CPP_OPEN_PAREN
16503 || token->type == CPP_OPEN_BRACE)
16505 is_initialized = SD_INITIALIZED;
16506 initialization_kind = token->type;
16507 if (maybe_range_for_decl)
16508 *maybe_range_for_decl = error_mark_node;
16510 if (token->type == CPP_EQ
16511 && function_declarator_p (declarator))
16513 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16514 if (t2->keyword == RID_DEFAULT)
16515 is_initialized = SD_DEFAULTED;
16516 else if (t2->keyword == RID_DELETE)
16517 is_initialized = SD_DELETED;
16520 else
16522 /* If the init-declarator isn't initialized and isn't followed by a
16523 `,' or `;', it's not a valid init-declarator. */
16524 if (token->type != CPP_COMMA
16525 && token->type != CPP_SEMICOLON)
16527 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16528 range_for_decl_p = true;
16529 else
16531 cp_parser_error (parser, "expected initializer");
16532 return error_mark_node;
16535 is_initialized = SD_UNINITIALIZED;
16536 initialization_kind = CPP_EOF;
16539 /* Because start_decl has side-effects, we should only call it if we
16540 know we're going ahead. By this point, we know that we cannot
16541 possibly be looking at any other construct. */
16542 cp_parser_commit_to_tentative_parse (parser);
16544 /* If the decl specifiers were bad, issue an error now that we're
16545 sure this was intended to be a declarator. Then continue
16546 declaring the variable(s), as int, to try to cut down on further
16547 errors. */
16548 if (decl_specifiers->any_specifiers_p
16549 && decl_specifiers->type == error_mark_node)
16551 cp_parser_error (parser, "invalid type in declaration");
16552 decl_specifiers->type = integer_type_node;
16555 /* Check to see whether or not this declaration is a friend. */
16556 friend_p = cp_parser_friend_p (decl_specifiers);
16558 /* Enter the newly declared entry in the symbol table. If we're
16559 processing a declaration in a class-specifier, we wait until
16560 after processing the initializer. */
16561 if (!member_p)
16563 if (parser->in_unbraced_linkage_specification_p)
16564 decl_specifiers->storage_class = sc_extern;
16565 decl = start_decl (declarator, decl_specifiers,
16566 range_for_decl_p? SD_INITIALIZED : is_initialized,
16567 attributes, prefix_attributes, &pushed_scope);
16568 cp_finalize_omp_declare_simd (parser, decl);
16569 /* Adjust location of decl if declarator->id_loc is more appropriate:
16570 set, and decl wasn't merged with another decl, in which case its
16571 location would be different from input_location, and more accurate. */
16572 if (DECL_P (decl)
16573 && declarator->id_loc != UNKNOWN_LOCATION
16574 && DECL_SOURCE_LOCATION (decl) == input_location)
16575 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16577 else if (scope)
16578 /* Enter the SCOPE. That way unqualified names appearing in the
16579 initializer will be looked up in SCOPE. */
16580 pushed_scope = push_scope (scope);
16582 /* Perform deferred access control checks, now that we know in which
16583 SCOPE the declared entity resides. */
16584 if (!member_p && decl)
16586 tree saved_current_function_decl = NULL_TREE;
16588 /* If the entity being declared is a function, pretend that we
16589 are in its scope. If it is a `friend', it may have access to
16590 things that would not otherwise be accessible. */
16591 if (TREE_CODE (decl) == FUNCTION_DECL)
16593 saved_current_function_decl = current_function_decl;
16594 current_function_decl = decl;
16597 /* Perform access checks for template parameters. */
16598 cp_parser_perform_template_parameter_access_checks (checks);
16600 /* Perform the access control checks for the declarator and the
16601 decl-specifiers. */
16602 perform_deferred_access_checks (tf_warning_or_error);
16604 /* Restore the saved value. */
16605 if (TREE_CODE (decl) == FUNCTION_DECL)
16606 current_function_decl = saved_current_function_decl;
16609 /* Parse the initializer. */
16610 initializer = NULL_TREE;
16611 is_direct_init = false;
16612 is_non_constant_init = true;
16613 if (is_initialized)
16615 if (function_declarator_p (declarator))
16617 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16618 if (initialization_kind == CPP_EQ)
16619 initializer = cp_parser_pure_specifier (parser);
16620 else
16622 /* If the declaration was erroneous, we don't really
16623 know what the user intended, so just silently
16624 consume the initializer. */
16625 if (decl != error_mark_node)
16626 error_at (initializer_start_token->location,
16627 "initializer provided for function");
16628 cp_parser_skip_to_closing_parenthesis (parser,
16629 /*recovering=*/true,
16630 /*or_comma=*/false,
16631 /*consume_paren=*/true);
16634 else
16636 /* We want to record the extra mangling scope for in-class
16637 initializers of class members and initializers of static data
16638 member templates. The former involves deferring
16639 parsing of the initializer until end of class as with default
16640 arguments. So right here we only handle the latter. */
16641 if (!member_p && processing_template_decl)
16642 start_lambda_scope (decl);
16643 initializer = cp_parser_initializer (parser,
16644 &is_direct_init,
16645 &is_non_constant_init);
16646 if (!member_p && processing_template_decl)
16647 finish_lambda_scope ();
16648 if (initializer == error_mark_node)
16649 cp_parser_skip_to_end_of_statement (parser);
16653 /* The old parser allows attributes to appear after a parenthesized
16654 initializer. Mark Mitchell proposed removing this functionality
16655 on the GCC mailing lists on 2002-08-13. This parser accepts the
16656 attributes -- but ignores them. */
16657 if (cp_parser_allow_gnu_extensions_p (parser)
16658 && initialization_kind == CPP_OPEN_PAREN)
16659 if (cp_parser_attributes_opt (parser))
16660 warning (OPT_Wattributes,
16661 "attributes after parenthesized initializer ignored");
16663 /* For an in-class declaration, use `grokfield' to create the
16664 declaration. */
16665 if (member_p)
16667 if (pushed_scope)
16669 pop_scope (pushed_scope);
16670 pushed_scope = NULL_TREE;
16672 decl = grokfield (declarator, decl_specifiers,
16673 initializer, !is_non_constant_init,
16674 /*asmspec=*/NULL_TREE,
16675 chainon (attributes, prefix_attributes));
16676 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16677 cp_parser_save_default_args (parser, decl);
16678 cp_finalize_omp_declare_simd (parser, decl);
16681 /* Finish processing the declaration. But, skip member
16682 declarations. */
16683 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16685 cp_finish_decl (decl,
16686 initializer, !is_non_constant_init,
16687 asm_specification,
16688 /* If the initializer is in parentheses, then this is
16689 a direct-initialization, which means that an
16690 `explicit' constructor is OK. Otherwise, an
16691 `explicit' constructor cannot be used. */
16692 ((is_direct_init || !is_initialized)
16693 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16695 else if ((cxx_dialect != cxx98) && friend_p
16696 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16697 /* Core issue #226 (C++0x only): A default template-argument
16698 shall not be specified in a friend class template
16699 declaration. */
16700 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16701 /*is_partial=*/false, /*is_friend_decl=*/1);
16703 if (!friend_p && pushed_scope)
16704 pop_scope (pushed_scope);
16706 if (function_declarator_p (declarator)
16707 && parser->fully_implicit_function_template_p)
16709 if (member_p)
16710 decl = finish_fully_implicit_template (parser, decl);
16711 else
16712 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16715 return decl;
16718 /* Parse a declarator.
16720 declarator:
16721 direct-declarator
16722 ptr-operator declarator
16724 abstract-declarator:
16725 ptr-operator abstract-declarator [opt]
16726 direct-abstract-declarator
16728 GNU Extensions:
16730 declarator:
16731 attributes [opt] direct-declarator
16732 attributes [opt] ptr-operator declarator
16734 abstract-declarator:
16735 attributes [opt] ptr-operator abstract-declarator [opt]
16736 attributes [opt] direct-abstract-declarator
16738 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16739 detect constructor, destructor or conversion operators. It is set
16740 to -1 if the declarator is a name, and +1 if it is a
16741 function. Otherwise it is set to zero. Usually you just want to
16742 test for >0, but internally the negative value is used.
16744 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16745 a decl-specifier-seq unless it declares a constructor, destructor,
16746 or conversion. It might seem that we could check this condition in
16747 semantic analysis, rather than parsing, but that makes it difficult
16748 to handle something like `f()'. We want to notice that there are
16749 no decl-specifiers, and therefore realize that this is an
16750 expression, not a declaration.)
16752 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16753 the declarator is a direct-declarator of the form "(...)".
16755 MEMBER_P is true iff this declarator is a member-declarator. */
16757 static cp_declarator *
16758 cp_parser_declarator (cp_parser* parser,
16759 cp_parser_declarator_kind dcl_kind,
16760 int* ctor_dtor_or_conv_p,
16761 bool* parenthesized_p,
16762 bool member_p)
16764 cp_declarator *declarator;
16765 enum tree_code code;
16766 cp_cv_quals cv_quals;
16767 tree class_type;
16768 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16770 /* Assume this is not a constructor, destructor, or type-conversion
16771 operator. */
16772 if (ctor_dtor_or_conv_p)
16773 *ctor_dtor_or_conv_p = 0;
16775 if (cp_parser_allow_gnu_extensions_p (parser))
16776 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16778 /* Check for the ptr-operator production. */
16779 cp_parser_parse_tentatively (parser);
16780 /* Parse the ptr-operator. */
16781 code = cp_parser_ptr_operator (parser,
16782 &class_type,
16783 &cv_quals,
16784 &std_attributes);
16786 /* If that worked, then we have a ptr-operator. */
16787 if (cp_parser_parse_definitely (parser))
16789 /* If a ptr-operator was found, then this declarator was not
16790 parenthesized. */
16791 if (parenthesized_p)
16792 *parenthesized_p = true;
16793 /* The dependent declarator is optional if we are parsing an
16794 abstract-declarator. */
16795 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16796 cp_parser_parse_tentatively (parser);
16798 /* Parse the dependent declarator. */
16799 declarator = cp_parser_declarator (parser, dcl_kind,
16800 /*ctor_dtor_or_conv_p=*/NULL,
16801 /*parenthesized_p=*/NULL,
16802 /*member_p=*/false);
16804 /* If we are parsing an abstract-declarator, we must handle the
16805 case where the dependent declarator is absent. */
16806 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16807 && !cp_parser_parse_definitely (parser))
16808 declarator = NULL;
16810 declarator = cp_parser_make_indirect_declarator
16811 (code, class_type, cv_quals, declarator, std_attributes);
16813 /* Everything else is a direct-declarator. */
16814 else
16816 if (parenthesized_p)
16817 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
16818 CPP_OPEN_PAREN);
16819 declarator = cp_parser_direct_declarator (parser, dcl_kind,
16820 ctor_dtor_or_conv_p,
16821 member_p);
16824 if (gnu_attributes && declarator && declarator != cp_error_declarator)
16825 declarator->attributes = gnu_attributes;
16826 return declarator;
16829 /* Parse a direct-declarator or direct-abstract-declarator.
16831 direct-declarator:
16832 declarator-id
16833 direct-declarator ( parameter-declaration-clause )
16834 cv-qualifier-seq [opt]
16835 ref-qualifier [opt]
16836 exception-specification [opt]
16837 direct-declarator [ constant-expression [opt] ]
16838 ( declarator )
16840 direct-abstract-declarator:
16841 direct-abstract-declarator [opt]
16842 ( parameter-declaration-clause )
16843 cv-qualifier-seq [opt]
16844 ref-qualifier [opt]
16845 exception-specification [opt]
16846 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16847 ( abstract-declarator )
16849 Returns a representation of the declarator. DCL_KIND is
16850 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16851 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16852 we are parsing a direct-declarator. It is
16853 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16854 of ambiguity we prefer an abstract declarator, as per
16855 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16856 cp_parser_declarator. */
16858 static cp_declarator *
16859 cp_parser_direct_declarator (cp_parser* parser,
16860 cp_parser_declarator_kind dcl_kind,
16861 int* ctor_dtor_or_conv_p,
16862 bool member_p)
16864 cp_token *token;
16865 cp_declarator *declarator = NULL;
16866 tree scope = NULL_TREE;
16867 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16868 bool saved_in_declarator_p = parser->in_declarator_p;
16869 bool first = true;
16870 tree pushed_scope = NULL_TREE;
16872 while (true)
16874 /* Peek at the next token. */
16875 token = cp_lexer_peek_token (parser->lexer);
16876 if (token->type == CPP_OPEN_PAREN)
16878 /* This is either a parameter-declaration-clause, or a
16879 parenthesized declarator. When we know we are parsing a
16880 named declarator, it must be a parenthesized declarator
16881 if FIRST is true. For instance, `(int)' is a
16882 parameter-declaration-clause, with an omitted
16883 direct-abstract-declarator. But `((*))', is a
16884 parenthesized abstract declarator. Finally, when T is a
16885 template parameter `(T)' is a
16886 parameter-declaration-clause, and not a parenthesized
16887 named declarator.
16889 We first try and parse a parameter-declaration-clause,
16890 and then try a nested declarator (if FIRST is true).
16892 It is not an error for it not to be a
16893 parameter-declaration-clause, even when FIRST is
16894 false. Consider,
16896 int i (int);
16897 int i (3);
16899 The first is the declaration of a function while the
16900 second is the definition of a variable, including its
16901 initializer.
16903 Having seen only the parenthesis, we cannot know which of
16904 these two alternatives should be selected. Even more
16905 complex are examples like:
16907 int i (int (a));
16908 int i (int (3));
16910 The former is a function-declaration; the latter is a
16911 variable initialization.
16913 Thus again, we try a parameter-declaration-clause, and if
16914 that fails, we back out and return. */
16916 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16918 tree params;
16919 unsigned saved_num_template_parameter_lists;
16920 bool is_declarator = false;
16922 /* In a member-declarator, the only valid interpretation
16923 of a parenthesis is the start of a
16924 parameter-declaration-clause. (It is invalid to
16925 initialize a static data member with a parenthesized
16926 initializer; only the "=" form of initialization is
16927 permitted.) */
16928 if (!member_p)
16929 cp_parser_parse_tentatively (parser);
16931 /* Consume the `('. */
16932 cp_lexer_consume_token (parser->lexer);
16933 if (first)
16935 /* If this is going to be an abstract declarator, we're
16936 in a declarator and we can't have default args. */
16937 parser->default_arg_ok_p = false;
16938 parser->in_declarator_p = true;
16941 /* Inside the function parameter list, surrounding
16942 template-parameter-lists do not apply. */
16943 saved_num_template_parameter_lists
16944 = parser->num_template_parameter_lists;
16945 parser->num_template_parameter_lists = 0;
16947 begin_scope (sk_function_parms, NULL_TREE);
16949 /* Parse the parameter-declaration-clause. */
16950 params = cp_parser_parameter_declaration_clause (parser);
16952 /* Restore saved template parameter lists accounting for implicit
16953 template parameters. */
16954 parser->num_template_parameter_lists
16955 += saved_num_template_parameter_lists;
16957 /* Consume the `)'. */
16958 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16960 /* If all went well, parse the cv-qualifier-seq,
16961 ref-qualifier and the exception-specification. */
16962 if (member_p || cp_parser_parse_definitely (parser))
16964 cp_cv_quals cv_quals;
16965 cp_virt_specifiers virt_specifiers;
16966 cp_ref_qualifier ref_qual;
16967 tree exception_specification;
16968 tree late_return;
16969 tree attrs;
16970 bool memfn = (member_p || (pushed_scope
16971 && CLASS_TYPE_P (pushed_scope)));
16973 is_declarator = true;
16975 if (ctor_dtor_or_conv_p)
16976 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16977 first = false;
16979 /* Parse the cv-qualifier-seq. */
16980 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16981 /* Parse the ref-qualifier. */
16982 ref_qual = cp_parser_ref_qualifier_opt (parser);
16983 /* And the exception-specification. */
16984 exception_specification
16985 = cp_parser_exception_specification_opt (parser);
16987 attrs = cp_parser_std_attribute_spec_seq (parser);
16989 late_return = (cp_parser_late_return_type_opt
16990 (parser, declarator,
16991 memfn ? cv_quals : -1));
16994 /* Parse the virt-specifier-seq. */
16995 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16997 /* Create the function-declarator. */
16998 declarator = make_call_declarator (declarator,
16999 params,
17000 cv_quals,
17001 virt_specifiers,
17002 ref_qual,
17003 exception_specification,
17004 late_return);
17005 declarator->std_attributes = attrs;
17006 /* Any subsequent parameter lists are to do with
17007 return type, so are not those of the declared
17008 function. */
17009 parser->default_arg_ok_p = false;
17012 /* Remove the function parms from scope. */
17013 pop_bindings_and_leave_scope ();
17015 if (is_declarator)
17016 /* Repeat the main loop. */
17017 continue;
17020 /* If this is the first, we can try a parenthesized
17021 declarator. */
17022 if (first)
17024 bool saved_in_type_id_in_expr_p;
17026 parser->default_arg_ok_p = saved_default_arg_ok_p;
17027 parser->in_declarator_p = saved_in_declarator_p;
17029 /* Consume the `('. */
17030 cp_lexer_consume_token (parser->lexer);
17031 /* Parse the nested declarator. */
17032 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17033 parser->in_type_id_in_expr_p = true;
17034 declarator
17035 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17036 /*parenthesized_p=*/NULL,
17037 member_p);
17038 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17039 first = false;
17040 /* Expect a `)'. */
17041 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17042 declarator = cp_error_declarator;
17043 if (declarator == cp_error_declarator)
17044 break;
17046 goto handle_declarator;
17048 /* Otherwise, we must be done. */
17049 else
17050 break;
17052 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17053 && token->type == CPP_OPEN_SQUARE
17054 && !cp_next_tokens_can_be_attribute_p (parser))
17056 /* Parse an array-declarator. */
17057 tree bounds, attrs;
17059 if (ctor_dtor_or_conv_p)
17060 *ctor_dtor_or_conv_p = 0;
17062 first = false;
17063 parser->default_arg_ok_p = false;
17064 parser->in_declarator_p = true;
17065 /* Consume the `['. */
17066 cp_lexer_consume_token (parser->lexer);
17067 /* Peek at the next token. */
17068 token = cp_lexer_peek_token (parser->lexer);
17069 /* If the next token is `]', then there is no
17070 constant-expression. */
17071 if (token->type != CPP_CLOSE_SQUARE)
17073 bool non_constant_p;
17074 bounds
17075 = cp_parser_constant_expression (parser,
17076 /*allow_non_constant=*/true,
17077 &non_constant_p);
17078 if (!non_constant_p)
17079 /* OK */;
17080 else if (error_operand_p (bounds))
17081 /* Already gave an error. */;
17082 else if (!parser->in_function_body
17083 || current_binding_level->kind == sk_function_parms)
17085 /* Normally, the array bound must be an integral constant
17086 expression. However, as an extension, we allow VLAs
17087 in function scopes as long as they aren't part of a
17088 parameter declaration. */
17089 cp_parser_error (parser,
17090 "array bound is not an integer constant");
17091 bounds = error_mark_node;
17093 else if (processing_template_decl)
17095 /* Remember this wasn't a constant-expression. */
17096 bounds = build_nop (TREE_TYPE (bounds), bounds);
17097 TREE_SIDE_EFFECTS (bounds) = 1;
17100 else
17101 bounds = NULL_TREE;
17102 /* Look for the closing `]'. */
17103 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17105 declarator = cp_error_declarator;
17106 break;
17109 attrs = cp_parser_std_attribute_spec_seq (parser);
17110 declarator = make_array_declarator (declarator, bounds);
17111 declarator->std_attributes = attrs;
17113 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17116 tree qualifying_scope;
17117 tree unqualified_name;
17118 tree attrs;
17119 special_function_kind sfk;
17120 bool abstract_ok;
17121 bool pack_expansion_p = false;
17122 cp_token *declarator_id_start_token;
17124 /* Parse a declarator-id */
17125 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17126 if (abstract_ok)
17128 cp_parser_parse_tentatively (parser);
17130 /* If we see an ellipsis, we should be looking at a
17131 parameter pack. */
17132 if (token->type == CPP_ELLIPSIS)
17134 /* Consume the `...' */
17135 cp_lexer_consume_token (parser->lexer);
17137 pack_expansion_p = true;
17141 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17142 unqualified_name
17143 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17144 qualifying_scope = parser->scope;
17145 if (abstract_ok)
17147 bool okay = false;
17149 if (!unqualified_name && pack_expansion_p)
17151 /* Check whether an error occurred. */
17152 okay = !cp_parser_error_occurred (parser);
17154 /* We already consumed the ellipsis to mark a
17155 parameter pack, but we have no way to report it,
17156 so abort the tentative parse. We will be exiting
17157 immediately anyway. */
17158 cp_parser_abort_tentative_parse (parser);
17160 else
17161 okay = cp_parser_parse_definitely (parser);
17163 if (!okay)
17164 unqualified_name = error_mark_node;
17165 else if (unqualified_name
17166 && (qualifying_scope
17167 || (!identifier_p (unqualified_name))))
17169 cp_parser_error (parser, "expected unqualified-id");
17170 unqualified_name = error_mark_node;
17174 if (!unqualified_name)
17175 return NULL;
17176 if (unqualified_name == error_mark_node)
17178 declarator = cp_error_declarator;
17179 pack_expansion_p = false;
17180 declarator->parameter_pack_p = false;
17181 break;
17184 attrs = cp_parser_std_attribute_spec_seq (parser);
17186 if (qualifying_scope && at_namespace_scope_p ()
17187 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17189 /* In the declaration of a member of a template class
17190 outside of the class itself, the SCOPE will sometimes
17191 be a TYPENAME_TYPE. For example, given:
17193 template <typename T>
17194 int S<T>::R::i = 3;
17196 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17197 this context, we must resolve S<T>::R to an ordinary
17198 type, rather than a typename type.
17200 The reason we normally avoid resolving TYPENAME_TYPEs
17201 is that a specialization of `S' might render
17202 `S<T>::R' not a type. However, if `S' is
17203 specialized, then this `i' will not be used, so there
17204 is no harm in resolving the types here. */
17205 tree type;
17207 /* Resolve the TYPENAME_TYPE. */
17208 type = resolve_typename_type (qualifying_scope,
17209 /*only_current_p=*/false);
17210 /* If that failed, the declarator is invalid. */
17211 if (TREE_CODE (type) == TYPENAME_TYPE)
17213 if (typedef_variant_p (type))
17214 error_at (declarator_id_start_token->location,
17215 "cannot define member of dependent typedef "
17216 "%qT", type);
17217 else
17218 error_at (declarator_id_start_token->location,
17219 "%<%T::%E%> is not a type",
17220 TYPE_CONTEXT (qualifying_scope),
17221 TYPE_IDENTIFIER (qualifying_scope));
17223 qualifying_scope = type;
17226 sfk = sfk_none;
17228 if (unqualified_name)
17230 tree class_type;
17232 if (qualifying_scope
17233 && CLASS_TYPE_P (qualifying_scope))
17234 class_type = qualifying_scope;
17235 else
17236 class_type = current_class_type;
17238 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17240 tree name_type = TREE_TYPE (unqualified_name);
17241 if (class_type && same_type_p (name_type, class_type))
17243 if (qualifying_scope
17244 && CLASSTYPE_USE_TEMPLATE (name_type))
17246 error_at (declarator_id_start_token->location,
17247 "invalid use of constructor as a template");
17248 inform (declarator_id_start_token->location,
17249 "use %<%T::%D%> instead of %<%T::%D%> to "
17250 "name the constructor in a qualified name",
17251 class_type,
17252 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17253 class_type, name_type);
17254 declarator = cp_error_declarator;
17255 break;
17257 else
17258 unqualified_name = constructor_name (class_type);
17260 else
17262 /* We do not attempt to print the declarator
17263 here because we do not have enough
17264 information about its original syntactic
17265 form. */
17266 cp_parser_error (parser, "invalid declarator");
17267 declarator = cp_error_declarator;
17268 break;
17272 if (class_type)
17274 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17275 sfk = sfk_destructor;
17276 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17277 sfk = sfk_conversion;
17278 else if (/* There's no way to declare a constructor
17279 for an anonymous type, even if the type
17280 got a name for linkage purposes. */
17281 !TYPE_WAS_ANONYMOUS (class_type)
17282 && constructor_name_p (unqualified_name,
17283 class_type))
17285 unqualified_name = constructor_name (class_type);
17286 sfk = sfk_constructor;
17288 else if (is_overloaded_fn (unqualified_name)
17289 && DECL_CONSTRUCTOR_P (get_first_fn
17290 (unqualified_name)))
17291 sfk = sfk_constructor;
17293 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17294 *ctor_dtor_or_conv_p = -1;
17297 declarator = make_id_declarator (qualifying_scope,
17298 unqualified_name,
17299 sfk);
17300 declarator->std_attributes = attrs;
17301 declarator->id_loc = token->location;
17302 declarator->parameter_pack_p = pack_expansion_p;
17304 if (pack_expansion_p)
17305 maybe_warn_variadic_templates ();
17308 handle_declarator:;
17309 scope = get_scope_of_declarator (declarator);
17310 if (scope)
17312 /* Any names that appear after the declarator-id for a
17313 member are looked up in the containing scope. */
17314 if (at_function_scope_p ())
17316 /* But declarations with qualified-ids can't appear in a
17317 function. */
17318 cp_parser_error (parser, "qualified-id in declaration");
17319 break;
17321 pushed_scope = push_scope (scope);
17323 parser->in_declarator_p = true;
17324 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17325 || (declarator && declarator->kind == cdk_id))
17326 /* Default args are only allowed on function
17327 declarations. */
17328 parser->default_arg_ok_p = saved_default_arg_ok_p;
17329 else
17330 parser->default_arg_ok_p = false;
17332 first = false;
17334 /* We're done. */
17335 else
17336 break;
17339 /* For an abstract declarator, we might wind up with nothing at this
17340 point. That's an error; the declarator is not optional. */
17341 if (!declarator)
17342 cp_parser_error (parser, "expected declarator");
17344 /* If we entered a scope, we must exit it now. */
17345 if (pushed_scope)
17346 pop_scope (pushed_scope);
17348 parser->default_arg_ok_p = saved_default_arg_ok_p;
17349 parser->in_declarator_p = saved_in_declarator_p;
17351 return declarator;
17354 /* Parse a ptr-operator.
17356 ptr-operator:
17357 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17358 * cv-qualifier-seq [opt]
17360 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17361 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17363 GNU Extension:
17365 ptr-operator:
17366 & cv-qualifier-seq [opt]
17368 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17369 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17370 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17371 filled in with the TYPE containing the member. *CV_QUALS is
17372 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17373 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17374 Note that the tree codes returned by this function have nothing
17375 to do with the types of trees that will be eventually be created
17376 to represent the pointer or reference type being parsed. They are
17377 just constants with suggestive names. */
17378 static enum tree_code
17379 cp_parser_ptr_operator (cp_parser* parser,
17380 tree* type,
17381 cp_cv_quals *cv_quals,
17382 tree *attributes)
17384 enum tree_code code = ERROR_MARK;
17385 cp_token *token;
17386 tree attrs = NULL_TREE;
17388 /* Assume that it's not a pointer-to-member. */
17389 *type = NULL_TREE;
17390 /* And that there are no cv-qualifiers. */
17391 *cv_quals = TYPE_UNQUALIFIED;
17393 /* Peek at the next token. */
17394 token = cp_lexer_peek_token (parser->lexer);
17396 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17397 if (token->type == CPP_MULT)
17398 code = INDIRECT_REF;
17399 else if (token->type == CPP_AND)
17400 code = ADDR_EXPR;
17401 else if ((cxx_dialect != cxx98) &&
17402 token->type == CPP_AND_AND) /* C++0x only */
17403 code = NON_LVALUE_EXPR;
17405 if (code != ERROR_MARK)
17407 /* Consume the `*', `&' or `&&'. */
17408 cp_lexer_consume_token (parser->lexer);
17410 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17411 `&', if we are allowing GNU extensions. (The only qualifier
17412 that can legally appear after `&' is `restrict', but that is
17413 enforced during semantic analysis. */
17414 if (code == INDIRECT_REF
17415 || cp_parser_allow_gnu_extensions_p (parser))
17416 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17418 attrs = cp_parser_std_attribute_spec_seq (parser);
17419 if (attributes != NULL)
17420 *attributes = attrs;
17422 else
17424 /* Try the pointer-to-member case. */
17425 cp_parser_parse_tentatively (parser);
17426 /* Look for the optional `::' operator. */
17427 cp_parser_global_scope_opt (parser,
17428 /*current_scope_valid_p=*/false);
17429 /* Look for the nested-name specifier. */
17430 token = cp_lexer_peek_token (parser->lexer);
17431 cp_parser_nested_name_specifier (parser,
17432 /*typename_keyword_p=*/false,
17433 /*check_dependency_p=*/true,
17434 /*type_p=*/false,
17435 /*is_declaration=*/false);
17436 /* If we found it, and the next token is a `*', then we are
17437 indeed looking at a pointer-to-member operator. */
17438 if (!cp_parser_error_occurred (parser)
17439 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17441 /* Indicate that the `*' operator was used. */
17442 code = INDIRECT_REF;
17444 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17445 error_at (token->location, "%qD is a namespace", parser->scope);
17446 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17447 error_at (token->location, "cannot form pointer to member of "
17448 "non-class %q#T", parser->scope);
17449 else
17451 /* The type of which the member is a member is given by the
17452 current SCOPE. */
17453 *type = parser->scope;
17454 /* The next name will not be qualified. */
17455 parser->scope = NULL_TREE;
17456 parser->qualifying_scope = NULL_TREE;
17457 parser->object_scope = NULL_TREE;
17458 /* Look for optional c++11 attributes. */
17459 attrs = cp_parser_std_attribute_spec_seq (parser);
17460 if (attributes != NULL)
17461 *attributes = attrs;
17462 /* Look for the optional cv-qualifier-seq. */
17463 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17466 /* If that didn't work we don't have a ptr-operator. */
17467 if (!cp_parser_parse_definitely (parser))
17468 cp_parser_error (parser, "expected ptr-operator");
17471 return code;
17474 /* Parse an (optional) cv-qualifier-seq.
17476 cv-qualifier-seq:
17477 cv-qualifier cv-qualifier-seq [opt]
17479 cv-qualifier:
17480 const
17481 volatile
17483 GNU Extension:
17485 cv-qualifier:
17486 __restrict__
17488 Returns a bitmask representing the cv-qualifiers. */
17490 static cp_cv_quals
17491 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17493 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17495 while (true)
17497 cp_token *token;
17498 cp_cv_quals cv_qualifier;
17500 /* Peek at the next token. */
17501 token = cp_lexer_peek_token (parser->lexer);
17502 /* See if it's a cv-qualifier. */
17503 switch (token->keyword)
17505 case RID_CONST:
17506 cv_qualifier = TYPE_QUAL_CONST;
17507 break;
17509 case RID_VOLATILE:
17510 cv_qualifier = TYPE_QUAL_VOLATILE;
17511 break;
17513 case RID_RESTRICT:
17514 cv_qualifier = TYPE_QUAL_RESTRICT;
17515 break;
17517 default:
17518 cv_qualifier = TYPE_UNQUALIFIED;
17519 break;
17522 if (!cv_qualifier)
17523 break;
17525 if (cv_quals & cv_qualifier)
17527 error_at (token->location, "duplicate cv-qualifier");
17528 cp_lexer_purge_token (parser->lexer);
17530 else
17532 cp_lexer_consume_token (parser->lexer);
17533 cv_quals |= cv_qualifier;
17537 return cv_quals;
17540 /* Parse an (optional) ref-qualifier
17542 ref-qualifier:
17546 Returns cp_ref_qualifier representing ref-qualifier. */
17548 static cp_ref_qualifier
17549 cp_parser_ref_qualifier_opt (cp_parser* parser)
17551 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17553 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17554 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17555 return ref_qual;
17557 while (true)
17559 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17560 cp_token *token = cp_lexer_peek_token (parser->lexer);
17562 switch (token->type)
17564 case CPP_AND:
17565 curr_ref_qual = REF_QUAL_LVALUE;
17566 break;
17568 case CPP_AND_AND:
17569 curr_ref_qual = REF_QUAL_RVALUE;
17570 break;
17572 default:
17573 curr_ref_qual = REF_QUAL_NONE;
17574 break;
17577 if (!curr_ref_qual)
17578 break;
17579 else if (ref_qual)
17581 error_at (token->location, "multiple ref-qualifiers");
17582 cp_lexer_purge_token (parser->lexer);
17584 else
17586 ref_qual = curr_ref_qual;
17587 cp_lexer_consume_token (parser->lexer);
17591 return ref_qual;
17594 /* Parse an (optional) virt-specifier-seq.
17596 virt-specifier-seq:
17597 virt-specifier virt-specifier-seq [opt]
17599 virt-specifier:
17600 override
17601 final
17603 Returns a bitmask representing the virt-specifiers. */
17605 static cp_virt_specifiers
17606 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17608 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17610 while (true)
17612 cp_token *token;
17613 cp_virt_specifiers virt_specifier;
17615 /* Peek at the next token. */
17616 token = cp_lexer_peek_token (parser->lexer);
17617 /* See if it's a virt-specifier-qualifier. */
17618 if (token->type != CPP_NAME)
17619 break;
17620 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17622 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17623 virt_specifier = VIRT_SPEC_OVERRIDE;
17625 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17627 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17628 virt_specifier = VIRT_SPEC_FINAL;
17630 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17632 virt_specifier = VIRT_SPEC_FINAL;
17634 else
17635 break;
17637 if (virt_specifiers & virt_specifier)
17639 error_at (token->location, "duplicate virt-specifier");
17640 cp_lexer_purge_token (parser->lexer);
17642 else
17644 cp_lexer_consume_token (parser->lexer);
17645 virt_specifiers |= virt_specifier;
17648 return virt_specifiers;
17651 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17652 is in scope even though it isn't real. */
17654 static void
17655 inject_this_parameter (tree ctype, cp_cv_quals quals)
17657 tree this_parm;
17659 if (current_class_ptr)
17661 /* We don't clear this between NSDMIs. Is it already what we want? */
17662 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17663 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17664 && cp_type_quals (type) == quals)
17665 return;
17668 this_parm = build_this_parm (ctype, quals);
17669 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17670 current_class_ptr = NULL_TREE;
17671 current_class_ref
17672 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17673 current_class_ptr = this_parm;
17676 /* Return true iff our current scope is a non-static data member
17677 initializer. */
17679 bool
17680 parsing_nsdmi (void)
17682 /* We recognize NSDMI context by the context-less 'this' pointer set up
17683 by the function above. */
17684 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
17685 return true;
17686 return false;
17689 /* Parse a late-specified return type, if any. This is not a separate
17690 non-terminal, but part of a function declarator, which looks like
17692 -> trailing-type-specifier-seq abstract-declarator(opt)
17694 Returns the type indicated by the type-id.
17696 In addition to this this parses any queued up omp declare simd
17697 clauses.
17699 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17700 function. */
17702 static tree
17703 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
17704 cp_cv_quals quals)
17706 cp_token *token;
17707 tree type = NULL_TREE;
17708 bool declare_simd_p = (parser->omp_declare_simd
17709 && declarator
17710 && declarator->kind == cdk_id);
17712 /* Peek at the next token. */
17713 token = cp_lexer_peek_token (parser->lexer);
17714 /* A late-specified return type is indicated by an initial '->'. */
17715 if (token->type != CPP_DEREF && !declare_simd_p)
17716 return NULL_TREE;
17718 tree save_ccp = current_class_ptr;
17719 tree save_ccr = current_class_ref;
17720 if (quals >= 0)
17722 /* DR 1207: 'this' is in scope in the trailing return type. */
17723 inject_this_parameter (current_class_type, quals);
17726 if (token->type == CPP_DEREF)
17728 /* Consume the ->. */
17729 cp_lexer_consume_token (parser->lexer);
17731 type = cp_parser_trailing_type_id (parser);
17734 if (declare_simd_p)
17735 declarator->std_attributes
17736 = cp_parser_late_parsing_omp_declare_simd (parser,
17737 declarator->std_attributes);
17739 if (quals >= 0)
17741 current_class_ptr = save_ccp;
17742 current_class_ref = save_ccr;
17745 return type;
17748 /* Parse a declarator-id.
17750 declarator-id:
17751 id-expression
17752 :: [opt] nested-name-specifier [opt] type-name
17754 In the `id-expression' case, the value returned is as for
17755 cp_parser_id_expression if the id-expression was an unqualified-id.
17756 If the id-expression was a qualified-id, then a SCOPE_REF is
17757 returned. The first operand is the scope (either a NAMESPACE_DECL
17758 or TREE_TYPE), but the second is still just a representation of an
17759 unqualified-id. */
17761 static tree
17762 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
17764 tree id;
17765 /* The expression must be an id-expression. Assume that qualified
17766 names are the names of types so that:
17768 template <class T>
17769 int S<T>::R::i = 3;
17771 will work; we must treat `S<T>::R' as the name of a type.
17772 Similarly, assume that qualified names are templates, where
17773 required, so that:
17775 template <class T>
17776 int S<T>::R<T>::i = 3;
17778 will work, too. */
17779 id = cp_parser_id_expression (parser,
17780 /*template_keyword_p=*/false,
17781 /*check_dependency_p=*/false,
17782 /*template_p=*/NULL,
17783 /*declarator_p=*/true,
17784 optional_p);
17785 if (id && BASELINK_P (id))
17786 id = BASELINK_FUNCTIONS (id);
17787 return id;
17790 /* Parse a type-id.
17792 type-id:
17793 type-specifier-seq abstract-declarator [opt]
17795 Returns the TYPE specified. */
17797 static tree
17798 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17799 bool is_trailing_return)
17801 cp_decl_specifier_seq type_specifier_seq;
17802 cp_declarator *abstract_declarator;
17804 /* Parse the type-specifier-seq. */
17805 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17806 is_trailing_return,
17807 &type_specifier_seq);
17808 if (type_specifier_seq.type == error_mark_node)
17809 return error_mark_node;
17811 /* There might or might not be an abstract declarator. */
17812 cp_parser_parse_tentatively (parser);
17813 /* Look for the declarator. */
17814 abstract_declarator
17815 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
17816 /*parenthesized_p=*/NULL,
17817 /*member_p=*/false);
17818 /* Check to see if there really was a declarator. */
17819 if (!cp_parser_parse_definitely (parser))
17820 abstract_declarator = NULL;
17822 if (type_specifier_seq.type
17823 && cxx_dialect < cxx1y
17824 && type_uses_auto (type_specifier_seq.type))
17826 /* A type-id with type 'auto' is only ok if the abstract declarator
17827 is a function declarator with a late-specified return type. */
17828 if (abstract_declarator
17829 && abstract_declarator->kind == cdk_function
17830 && abstract_declarator->u.function.late_return_type)
17831 /* OK */;
17832 else
17834 error ("invalid use of %<auto%>");
17835 return error_mark_node;
17839 return groktypename (&type_specifier_seq, abstract_declarator,
17840 is_template_arg);
17843 static tree cp_parser_type_id (cp_parser *parser)
17845 return cp_parser_type_id_1 (parser, false, false);
17848 static tree cp_parser_template_type_arg (cp_parser *parser)
17850 tree r;
17851 const char *saved_message = parser->type_definition_forbidden_message;
17852 parser->type_definition_forbidden_message
17853 = G_("types may not be defined in template arguments");
17854 r = cp_parser_type_id_1 (parser, true, false);
17855 parser->type_definition_forbidden_message = saved_message;
17856 return r;
17859 static tree cp_parser_trailing_type_id (cp_parser *parser)
17861 return cp_parser_type_id_1 (parser, false, true);
17864 /* Parse a type-specifier-seq.
17866 type-specifier-seq:
17867 type-specifier type-specifier-seq [opt]
17869 GNU extension:
17871 type-specifier-seq:
17872 attributes type-specifier-seq [opt]
17874 If IS_DECLARATION is true, we are at the start of a "condition" or
17875 exception-declaration, so we might be followed by a declarator-id.
17877 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17878 i.e. we've just seen "->".
17880 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17882 static void
17883 cp_parser_type_specifier_seq (cp_parser* parser,
17884 bool is_declaration,
17885 bool is_trailing_return,
17886 cp_decl_specifier_seq *type_specifier_seq)
17888 bool seen_type_specifier = false;
17889 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
17890 cp_token *start_token = NULL;
17892 /* Clear the TYPE_SPECIFIER_SEQ. */
17893 clear_decl_specs (type_specifier_seq);
17895 /* In the context of a trailing return type, enum E { } is an
17896 elaborated-type-specifier followed by a function-body, not an
17897 enum-specifier. */
17898 if (is_trailing_return)
17899 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
17901 /* Parse the type-specifiers and attributes. */
17902 while (true)
17904 tree type_specifier;
17905 bool is_cv_qualifier;
17907 /* Check for attributes first. */
17908 if (cp_next_tokens_can_be_attribute_p (parser))
17910 type_specifier_seq->attributes =
17911 chainon (type_specifier_seq->attributes,
17912 cp_parser_attributes_opt (parser));
17913 continue;
17916 /* record the token of the beginning of the type specifier seq,
17917 for error reporting purposes*/
17918 if (!start_token)
17919 start_token = cp_lexer_peek_token (parser->lexer);
17921 /* Look for the type-specifier. */
17922 type_specifier = cp_parser_type_specifier (parser,
17923 flags,
17924 type_specifier_seq,
17925 /*is_declaration=*/false,
17926 NULL,
17927 &is_cv_qualifier);
17928 if (!type_specifier)
17930 /* If the first type-specifier could not be found, this is not a
17931 type-specifier-seq at all. */
17932 if (!seen_type_specifier)
17934 cp_parser_error (parser, "expected type-specifier");
17935 type_specifier_seq->type = error_mark_node;
17936 return;
17938 /* If subsequent type-specifiers could not be found, the
17939 type-specifier-seq is complete. */
17940 break;
17943 seen_type_specifier = true;
17944 /* The standard says that a condition can be:
17946 type-specifier-seq declarator = assignment-expression
17948 However, given:
17950 struct S {};
17951 if (int S = ...)
17953 we should treat the "S" as a declarator, not as a
17954 type-specifier. The standard doesn't say that explicitly for
17955 type-specifier-seq, but it does say that for
17956 decl-specifier-seq in an ordinary declaration. Perhaps it
17957 would be clearer just to allow a decl-specifier-seq here, and
17958 then add a semantic restriction that if any decl-specifiers
17959 that are not type-specifiers appear, the program is invalid. */
17960 if (is_declaration && !is_cv_qualifier)
17961 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
17965 /* Parse a parameter-declaration-clause.
17967 parameter-declaration-clause:
17968 parameter-declaration-list [opt] ... [opt]
17969 parameter-declaration-list , ...
17971 Returns a representation for the parameter declarations. A return
17972 value of NULL indicates a parameter-declaration-clause consisting
17973 only of an ellipsis. */
17975 static tree
17976 cp_parser_parameter_declaration_clause (cp_parser* parser)
17978 tree parameters;
17979 cp_token *token;
17980 bool ellipsis_p;
17981 bool is_error;
17983 /* Peek at the next token. */
17984 token = cp_lexer_peek_token (parser->lexer);
17985 /* Check for trivial parameter-declaration-clauses. */
17986 if (token->type == CPP_ELLIPSIS)
17988 /* Consume the `...' token. */
17989 cp_lexer_consume_token (parser->lexer);
17990 return NULL_TREE;
17992 else if (token->type == CPP_CLOSE_PAREN)
17993 /* There are no parameters. */
17995 #ifndef NO_IMPLICIT_EXTERN_C
17996 if (in_system_header && current_class_type == NULL
17997 && current_lang_name == lang_name_c)
17998 return NULL_TREE;
17999 else
18000 #endif
18001 return void_list_node;
18003 /* Check for `(void)', too, which is a special case. */
18004 else if (token->keyword == RID_VOID
18005 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18006 == CPP_CLOSE_PAREN))
18008 /* Consume the `void' token. */
18009 cp_lexer_consume_token (parser->lexer);
18010 /* There are no parameters. */
18011 return void_list_node;
18014 /* Parse the parameter-declaration-list. */
18015 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18016 /* If a parse error occurred while parsing the
18017 parameter-declaration-list, then the entire
18018 parameter-declaration-clause is erroneous. */
18019 if (is_error)
18020 return NULL;
18022 /* Peek at the next token. */
18023 token = cp_lexer_peek_token (parser->lexer);
18024 /* If it's a `,', the clause should terminate with an ellipsis. */
18025 if (token->type == CPP_COMMA)
18027 /* Consume the `,'. */
18028 cp_lexer_consume_token (parser->lexer);
18029 /* Expect an ellipsis. */
18030 ellipsis_p
18031 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18033 /* It might also be `...' if the optional trailing `,' was
18034 omitted. */
18035 else if (token->type == CPP_ELLIPSIS)
18037 /* Consume the `...' token. */
18038 cp_lexer_consume_token (parser->lexer);
18039 /* And remember that we saw it. */
18040 ellipsis_p = true;
18042 else
18043 ellipsis_p = false;
18045 /* Finish the parameter list. */
18046 if (!ellipsis_p)
18047 parameters = chainon (parameters, void_list_node);
18049 return parameters;
18052 /* Parse a parameter-declaration-list.
18054 parameter-declaration-list:
18055 parameter-declaration
18056 parameter-declaration-list , parameter-declaration
18058 Returns a representation of the parameter-declaration-list, as for
18059 cp_parser_parameter_declaration_clause. However, the
18060 `void_list_node' is never appended to the list. Upon return,
18061 *IS_ERROR will be true iff an error occurred. */
18063 static tree
18064 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18066 tree parameters = NULL_TREE;
18067 tree *tail = &parameters;
18068 bool saved_in_unbraced_linkage_specification_p;
18069 int index = 0;
18070 int implicit_template_parms = 0;
18072 /* Assume all will go well. */
18073 *is_error = false;
18074 /* The special considerations that apply to a function within an
18075 unbraced linkage specifications do not apply to the parameters
18076 to the function. */
18077 saved_in_unbraced_linkage_specification_p
18078 = parser->in_unbraced_linkage_specification_p;
18079 parser->in_unbraced_linkage_specification_p = false;
18081 /* Look for more parameters. */
18082 while (true)
18084 cp_parameter_declarator *parameter;
18085 tree decl = error_mark_node;
18086 bool parenthesized_p = false;
18087 /* Parse the parameter. */
18088 parameter
18089 = cp_parser_parameter_declaration (parser,
18090 /*template_parm_p=*/false,
18091 &parenthesized_p);
18093 /* We don't know yet if the enclosing context is deprecated, so wait
18094 and warn in grokparms if appropriate. */
18095 deprecated_state = DEPRECATED_SUPPRESS;
18097 if (parameter)
18099 decl = grokdeclarator (parameter->declarator,
18100 &parameter->decl_specifiers,
18101 PARM,
18102 parameter->default_argument != NULL_TREE,
18103 &parameter->decl_specifiers.attributes);
18105 if (TREE_TYPE (decl) != error_mark_node
18106 && parameter->decl_specifiers.type
18107 && is_auto_or_concept (parameter->decl_specifiers.type))
18108 ++implicit_template_parms;
18111 deprecated_state = DEPRECATED_NORMAL;
18113 /* If a parse error occurred parsing the parameter declaration,
18114 then the entire parameter-declaration-list is erroneous. */
18115 if (decl == error_mark_node)
18117 *is_error = true;
18118 parameters = error_mark_node;
18119 break;
18122 if (parameter->decl_specifiers.attributes)
18123 cplus_decl_attributes (&decl,
18124 parameter->decl_specifiers.attributes,
18126 if (DECL_NAME (decl))
18127 decl = pushdecl (decl);
18129 if (decl != error_mark_node)
18131 retrofit_lang_decl (decl);
18132 DECL_PARM_INDEX (decl) = ++index;
18133 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18136 /* Add the new parameter to the list. */
18137 *tail = build_tree_list (parameter->default_argument, decl);
18138 tail = &TREE_CHAIN (*tail);
18140 /* Peek at the next token. */
18141 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18142 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18143 /* These are for Objective-C++ */
18144 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18145 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18146 /* The parameter-declaration-list is complete. */
18147 break;
18148 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18150 cp_token *token;
18152 /* Peek at the next token. */
18153 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18154 /* If it's an ellipsis, then the list is complete. */
18155 if (token->type == CPP_ELLIPSIS)
18156 break;
18157 /* Otherwise, there must be more parameters. Consume the
18158 `,'. */
18159 cp_lexer_consume_token (parser->lexer);
18160 /* When parsing something like:
18162 int i(float f, double d)
18164 we can tell after seeing the declaration for "f" that we
18165 are not looking at an initialization of a variable "i",
18166 but rather at the declaration of a function "i".
18168 Due to the fact that the parsing of template arguments
18169 (as specified to a template-id) requires backtracking we
18170 cannot use this technique when inside a template argument
18171 list. */
18172 if (!parser->in_template_argument_list_p
18173 && !parser->in_type_id_in_expr_p
18174 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18175 /* However, a parameter-declaration of the form
18176 "float(f)" (which is a valid declaration of a
18177 parameter "f") can also be interpreted as an
18178 expression (the conversion of "f" to "float"). */
18179 && !parenthesized_p)
18180 cp_parser_commit_to_tentative_parse (parser);
18182 else
18184 cp_parser_error (parser, "expected %<,%> or %<...%>");
18185 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18186 cp_parser_skip_to_closing_parenthesis (parser,
18187 /*recovering=*/true,
18188 /*or_comma=*/false,
18189 /*consume_paren=*/false);
18190 break;
18194 parser->in_unbraced_linkage_specification_p
18195 = saved_in_unbraced_linkage_specification_p;
18197 if (parameters != error_mark_node && implicit_template_parms)
18198 parameters = add_implicit_template_parms (parser,
18199 implicit_template_parms,
18200 parameters);
18202 return parameters;
18205 /* Parse a parameter declaration.
18207 parameter-declaration:
18208 decl-specifier-seq ... [opt] declarator
18209 decl-specifier-seq declarator = assignment-expression
18210 decl-specifier-seq ... [opt] abstract-declarator [opt]
18211 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18213 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18214 declares a template parameter. (In that case, a non-nested `>'
18215 token encountered during the parsing of the assignment-expression
18216 is not interpreted as a greater-than operator.)
18218 Returns a representation of the parameter, or NULL if an error
18219 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18220 true iff the declarator is of the form "(p)". */
18222 static cp_parameter_declarator *
18223 cp_parser_parameter_declaration (cp_parser *parser,
18224 bool template_parm_p,
18225 bool *parenthesized_p)
18227 int declares_class_or_enum;
18228 cp_decl_specifier_seq decl_specifiers;
18229 cp_declarator *declarator;
18230 tree default_argument;
18231 cp_token *token = NULL, *declarator_token_start = NULL;
18232 const char *saved_message;
18234 /* In a template parameter, `>' is not an operator.
18236 [temp.param]
18238 When parsing a default template-argument for a non-type
18239 template-parameter, the first non-nested `>' is taken as the end
18240 of the template parameter-list rather than a greater-than
18241 operator. */
18243 /* Type definitions may not appear in parameter types. */
18244 saved_message = parser->type_definition_forbidden_message;
18245 parser->type_definition_forbidden_message
18246 = G_("types may not be defined in parameter types");
18248 /* Parse the declaration-specifiers. */
18249 cp_parser_decl_specifier_seq (parser,
18250 CP_PARSER_FLAGS_NONE,
18251 &decl_specifiers,
18252 &declares_class_or_enum);
18254 /* Complain about missing 'typename' or other invalid type names. */
18255 if (!decl_specifiers.any_type_specifiers_p
18256 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18257 decl_specifiers.type = error_mark_node;
18259 /* If an error occurred, there's no reason to attempt to parse the
18260 rest of the declaration. */
18261 if (cp_parser_error_occurred (parser))
18263 parser->type_definition_forbidden_message = saved_message;
18264 return NULL;
18267 /* Peek at the next token. */
18268 token = cp_lexer_peek_token (parser->lexer);
18270 /* If the next token is a `)', `,', `=', `>', or `...', then there
18271 is no declarator. However, when variadic templates are enabled,
18272 there may be a declarator following `...'. */
18273 if (token->type == CPP_CLOSE_PAREN
18274 || token->type == CPP_COMMA
18275 || token->type == CPP_EQ
18276 || token->type == CPP_GREATER)
18278 declarator = NULL;
18279 if (parenthesized_p)
18280 *parenthesized_p = false;
18282 /* Otherwise, there should be a declarator. */
18283 else
18285 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18286 parser->default_arg_ok_p = false;
18288 /* After seeing a decl-specifier-seq, if the next token is not a
18289 "(", there is no possibility that the code is a valid
18290 expression. Therefore, if parsing tentatively, we commit at
18291 this point. */
18292 if (!parser->in_template_argument_list_p
18293 /* In an expression context, having seen:
18295 (int((char ...
18297 we cannot be sure whether we are looking at a
18298 function-type (taking a "char" as a parameter) or a cast
18299 of some object of type "char" to "int". */
18300 && !parser->in_type_id_in_expr_p
18301 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18302 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18303 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18304 cp_parser_commit_to_tentative_parse (parser);
18305 /* Parse the declarator. */
18306 declarator_token_start = token;
18307 declarator = cp_parser_declarator (parser,
18308 CP_PARSER_DECLARATOR_EITHER,
18309 /*ctor_dtor_or_conv_p=*/NULL,
18310 parenthesized_p,
18311 /*member_p=*/false);
18312 parser->default_arg_ok_p = saved_default_arg_ok_p;
18313 /* After the declarator, allow more attributes. */
18314 decl_specifiers.attributes
18315 = chainon (decl_specifiers.attributes,
18316 cp_parser_attributes_opt (parser));
18319 /* If the next token is an ellipsis, and we have not seen a
18320 declarator name, and the type of the declarator contains parameter
18321 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18322 a parameter pack expansion expression. Otherwise, leave the
18323 ellipsis for a C-style variadic function. */
18324 token = cp_lexer_peek_token (parser->lexer);
18325 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18327 tree type = decl_specifiers.type;
18329 if (type && DECL_P (type))
18330 type = TREE_TYPE (type);
18332 if (type
18333 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18334 && declarator_can_be_parameter_pack (declarator)
18335 && (!declarator || !declarator->parameter_pack_p)
18336 && uses_parameter_packs (type))
18338 /* Consume the `...'. */
18339 cp_lexer_consume_token (parser->lexer);
18340 maybe_warn_variadic_templates ();
18342 /* Build a pack expansion type */
18343 if (declarator)
18344 declarator->parameter_pack_p = true;
18345 else
18346 decl_specifiers.type = make_pack_expansion (type);
18350 /* The restriction on defining new types applies only to the type
18351 of the parameter, not to the default argument. */
18352 parser->type_definition_forbidden_message = saved_message;
18354 /* If the next token is `=', then process a default argument. */
18355 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18357 token = cp_lexer_peek_token (parser->lexer);
18358 /* If we are defining a class, then the tokens that make up the
18359 default argument must be saved and processed later. */
18360 if (!template_parm_p && at_class_scope_p ()
18361 && TYPE_BEING_DEFINED (current_class_type)
18362 && !LAMBDA_TYPE_P (current_class_type))
18363 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18364 /* Outside of a class definition, we can just parse the
18365 assignment-expression. */
18366 else
18367 default_argument
18368 = cp_parser_default_argument (parser, template_parm_p);
18370 if (!parser->default_arg_ok_p)
18372 if (flag_permissive)
18373 warning (0, "deprecated use of default argument for parameter of non-function");
18374 else
18376 error_at (token->location,
18377 "default arguments are only "
18378 "permitted for function parameters");
18379 default_argument = NULL_TREE;
18382 else if ((declarator && declarator->parameter_pack_p)
18383 || (decl_specifiers.type
18384 && PACK_EXPANSION_P (decl_specifiers.type)))
18386 /* Find the name of the parameter pack. */
18387 cp_declarator *id_declarator = declarator;
18388 while (id_declarator && id_declarator->kind != cdk_id)
18389 id_declarator = id_declarator->declarator;
18391 if (id_declarator && id_declarator->kind == cdk_id)
18392 error_at (declarator_token_start->location,
18393 template_parm_p
18394 ? G_("template parameter pack %qD "
18395 "cannot have a default argument")
18396 : G_("parameter pack %qD cannot have "
18397 "a default argument"),
18398 id_declarator->u.id.unqualified_name);
18399 else
18400 error_at (declarator_token_start->location,
18401 template_parm_p
18402 ? G_("template parameter pack cannot have "
18403 "a default argument")
18404 : G_("parameter pack cannot have a "
18405 "default argument"));
18407 default_argument = NULL_TREE;
18410 else
18411 default_argument = NULL_TREE;
18413 return make_parameter_declarator (&decl_specifiers,
18414 declarator,
18415 default_argument);
18418 /* Parse a default argument and return it.
18420 TEMPLATE_PARM_P is true if this is a default argument for a
18421 non-type template parameter. */
18422 static tree
18423 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18425 tree default_argument = NULL_TREE;
18426 bool saved_greater_than_is_operator_p;
18427 bool saved_local_variables_forbidden_p;
18428 bool non_constant_p, is_direct_init;
18430 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18431 set correctly. */
18432 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18433 parser->greater_than_is_operator_p = !template_parm_p;
18434 /* Local variable names (and the `this' keyword) may not
18435 appear in a default argument. */
18436 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18437 parser->local_variables_forbidden_p = true;
18438 /* Parse the assignment-expression. */
18439 if (template_parm_p)
18440 push_deferring_access_checks (dk_no_deferred);
18441 default_argument
18442 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18443 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18444 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18445 if (template_parm_p)
18446 pop_deferring_access_checks ();
18447 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18448 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18450 return default_argument;
18453 /* Parse a function-body.
18455 function-body:
18456 compound_statement */
18458 static void
18459 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18461 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18464 /* Parse a ctor-initializer-opt followed by a function-body. Return
18465 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18466 is true we are parsing a function-try-block. */
18468 static bool
18469 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18470 bool in_function_try_block)
18472 tree body, list;
18473 bool ctor_initializer_p;
18474 const bool check_body_p =
18475 DECL_CONSTRUCTOR_P (current_function_decl)
18476 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18477 tree last = NULL;
18479 /* Begin the function body. */
18480 body = begin_function_body ();
18481 /* Parse the optional ctor-initializer. */
18482 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18484 /* If we're parsing a constexpr constructor definition, we need
18485 to check that the constructor body is indeed empty. However,
18486 before we get to cp_parser_function_body lot of junk has been
18487 generated, so we can't just check that we have an empty block.
18488 Rather we take a snapshot of the outermost block, and check whether
18489 cp_parser_function_body changed its state. */
18490 if (check_body_p)
18492 list = cur_stmt_list;
18493 if (STATEMENT_LIST_TAIL (list))
18494 last = STATEMENT_LIST_TAIL (list)->stmt;
18496 /* Parse the function-body. */
18497 cp_parser_function_body (parser, in_function_try_block);
18498 if (check_body_p)
18499 check_constexpr_ctor_body (last, list);
18500 /* Finish the function body. */
18501 finish_function_body (body);
18503 return ctor_initializer_p;
18506 /* Parse an initializer.
18508 initializer:
18509 = initializer-clause
18510 ( expression-list )
18512 Returns an expression representing the initializer. If no
18513 initializer is present, NULL_TREE is returned.
18515 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18516 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18517 set to TRUE if there is no initializer present. If there is an
18518 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18519 is set to true; otherwise it is set to false. */
18521 static tree
18522 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
18523 bool* non_constant_p)
18525 cp_token *token;
18526 tree init;
18528 /* Peek at the next token. */
18529 token = cp_lexer_peek_token (parser->lexer);
18531 /* Let our caller know whether or not this initializer was
18532 parenthesized. */
18533 *is_direct_init = (token->type != CPP_EQ);
18534 /* Assume that the initializer is constant. */
18535 *non_constant_p = false;
18537 if (token->type == CPP_EQ)
18539 /* Consume the `='. */
18540 cp_lexer_consume_token (parser->lexer);
18541 /* Parse the initializer-clause. */
18542 init = cp_parser_initializer_clause (parser, non_constant_p);
18544 else if (token->type == CPP_OPEN_PAREN)
18546 vec<tree, va_gc> *vec;
18547 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18548 /*cast_p=*/false,
18549 /*allow_expansion_p=*/true,
18550 non_constant_p);
18551 if (vec == NULL)
18552 return error_mark_node;
18553 init = build_tree_list_vec (vec);
18554 release_tree_vector (vec);
18556 else if (token->type == CPP_OPEN_BRACE)
18558 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18559 init = cp_parser_braced_list (parser, non_constant_p);
18560 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
18562 else
18564 /* Anything else is an error. */
18565 cp_parser_error (parser, "expected initializer");
18566 init = error_mark_node;
18569 return init;
18572 /* Parse an initializer-clause.
18574 initializer-clause:
18575 assignment-expression
18576 braced-init-list
18578 Returns an expression representing the initializer.
18580 If the `assignment-expression' production is used the value
18581 returned is simply a representation for the expression.
18583 Otherwise, calls cp_parser_braced_list. */
18585 static tree
18586 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
18588 tree initializer;
18590 /* Assume the expression is constant. */
18591 *non_constant_p = false;
18593 /* If it is not a `{', then we are looking at an
18594 assignment-expression. */
18595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
18597 initializer
18598 = cp_parser_constant_expression (parser,
18599 /*allow_non_constant_p=*/true,
18600 non_constant_p);
18602 else
18603 initializer = cp_parser_braced_list (parser, non_constant_p);
18605 return initializer;
18608 /* Parse a brace-enclosed initializer list.
18610 braced-init-list:
18611 { initializer-list , [opt] }
18614 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18615 the elements of the initializer-list (or NULL, if the last
18616 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18617 NULL_TREE. There is no way to detect whether or not the optional
18618 trailing `,' was provided. NON_CONSTANT_P is as for
18619 cp_parser_initializer. */
18621 static tree
18622 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
18624 tree initializer;
18626 /* Consume the `{' token. */
18627 cp_lexer_consume_token (parser->lexer);
18628 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18629 initializer = make_node (CONSTRUCTOR);
18630 /* If it's not a `}', then there is a non-trivial initializer. */
18631 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
18633 /* Parse the initializer list. */
18634 CONSTRUCTOR_ELTS (initializer)
18635 = cp_parser_initializer_list (parser, non_constant_p);
18636 /* A trailing `,' token is allowed. */
18637 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18638 cp_lexer_consume_token (parser->lexer);
18640 else
18641 *non_constant_p = false;
18642 /* Now, there should be a trailing `}'. */
18643 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18644 TREE_TYPE (initializer) = init_list_type_node;
18645 return initializer;
18648 /* Parse an initializer-list.
18650 initializer-list:
18651 initializer-clause ... [opt]
18652 initializer-list , initializer-clause ... [opt]
18654 GNU Extension:
18656 initializer-list:
18657 designation initializer-clause ...[opt]
18658 initializer-list , designation initializer-clause ...[opt]
18660 designation:
18661 . identifier =
18662 identifier :
18663 [ constant-expression ] =
18665 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18666 for the initializer. If the INDEX of the elt is non-NULL, it is the
18667 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18668 as for cp_parser_initializer. */
18670 static vec<constructor_elt, va_gc> *
18671 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
18673 vec<constructor_elt, va_gc> *v = NULL;
18675 /* Assume all of the expressions are constant. */
18676 *non_constant_p = false;
18678 /* Parse the rest of the list. */
18679 while (true)
18681 cp_token *token;
18682 tree designator;
18683 tree initializer;
18684 bool clause_non_constant_p;
18686 /* If the next token is an identifier and the following one is a
18687 colon, we are looking at the GNU designated-initializer
18688 syntax. */
18689 if (cp_parser_allow_gnu_extensions_p (parser)
18690 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
18691 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
18693 /* Warn the user that they are using an extension. */
18694 pedwarn (input_location, OPT_Wpedantic,
18695 "ISO C++ does not allow designated initializers");
18696 /* Consume the identifier. */
18697 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18698 /* Consume the `:'. */
18699 cp_lexer_consume_token (parser->lexer);
18701 /* Also handle the C99 syntax, '. id ='. */
18702 else if (cp_parser_allow_gnu_extensions_p (parser)
18703 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
18704 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
18705 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
18707 /* Warn the user that they are using an extension. */
18708 pedwarn (input_location, OPT_Wpedantic,
18709 "ISO C++ does not allow C99 designated initializers");
18710 /* Consume the `.'. */
18711 cp_lexer_consume_token (parser->lexer);
18712 /* Consume the identifier. */
18713 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18714 /* Consume the `='. */
18715 cp_lexer_consume_token (parser->lexer);
18717 /* Also handle C99 array designators, '[ const ] ='. */
18718 else if (cp_parser_allow_gnu_extensions_p (parser)
18719 && !c_dialect_objc ()
18720 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18722 /* In C++11, [ could start a lambda-introducer. */
18723 bool non_const = false;
18725 cp_parser_parse_tentatively (parser);
18726 cp_lexer_consume_token (parser->lexer);
18727 designator = cp_parser_constant_expression (parser, true, &non_const);
18728 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18729 cp_parser_require (parser, CPP_EQ, RT_EQ);
18730 if (!cp_parser_parse_definitely (parser))
18731 designator = NULL_TREE;
18732 else if (non_const)
18733 require_potential_rvalue_constant_expression (designator);
18735 else
18736 designator = NULL_TREE;
18738 /* Parse the initializer. */
18739 initializer = cp_parser_initializer_clause (parser,
18740 &clause_non_constant_p);
18741 /* If any clause is non-constant, so is the entire initializer. */
18742 if (clause_non_constant_p)
18743 *non_constant_p = true;
18745 /* If we have an ellipsis, this is an initializer pack
18746 expansion. */
18747 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18749 /* Consume the `...'. */
18750 cp_lexer_consume_token (parser->lexer);
18752 /* Turn the initializer into an initializer expansion. */
18753 initializer = make_pack_expansion (initializer);
18756 /* Add it to the vector. */
18757 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
18759 /* If the next token is not a comma, we have reached the end of
18760 the list. */
18761 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18762 break;
18764 /* Peek at the next token. */
18765 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18766 /* If the next token is a `}', then we're still done. An
18767 initializer-clause can have a trailing `,' after the
18768 initializer-list and before the closing `}'. */
18769 if (token->type == CPP_CLOSE_BRACE)
18770 break;
18772 /* Consume the `,' token. */
18773 cp_lexer_consume_token (parser->lexer);
18776 return v;
18779 /* Classes [gram.class] */
18781 /* Parse a class-name.
18783 class-name:
18784 identifier
18785 template-id
18787 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
18788 to indicate that names looked up in dependent types should be
18789 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
18790 keyword has been used to indicate that the name that appears next
18791 is a template. TAG_TYPE indicates the explicit tag given before
18792 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
18793 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
18794 is the class being defined in a class-head.
18796 Returns the TYPE_DECL representing the class. */
18798 static tree
18799 cp_parser_class_name (cp_parser *parser,
18800 bool typename_keyword_p,
18801 bool template_keyword_p,
18802 enum tag_types tag_type,
18803 bool check_dependency_p,
18804 bool class_head_p,
18805 bool is_declaration)
18807 tree decl;
18808 tree scope;
18809 bool typename_p;
18810 cp_token *token;
18811 tree identifier = NULL_TREE;
18813 /* All class-names start with an identifier. */
18814 token = cp_lexer_peek_token (parser->lexer);
18815 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
18817 cp_parser_error (parser, "expected class-name");
18818 return error_mark_node;
18821 /* PARSER->SCOPE can be cleared when parsing the template-arguments
18822 to a template-id, so we save it here. */
18823 scope = parser->scope;
18824 if (scope == error_mark_node)
18825 return error_mark_node;
18827 /* Any name names a type if we're following the `typename' keyword
18828 in a qualified name where the enclosing scope is type-dependent. */
18829 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
18830 && dependent_type_p (scope));
18831 /* Handle the common case (an identifier, but not a template-id)
18832 efficiently. */
18833 if (token->type == CPP_NAME
18834 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
18836 cp_token *identifier_token;
18837 bool ambiguous_p;
18839 /* Look for the identifier. */
18840 identifier_token = cp_lexer_peek_token (parser->lexer);
18841 ambiguous_p = identifier_token->ambiguous_p;
18842 identifier = cp_parser_identifier (parser);
18843 /* If the next token isn't an identifier, we are certainly not
18844 looking at a class-name. */
18845 if (identifier == error_mark_node)
18846 decl = error_mark_node;
18847 /* If we know this is a type-name, there's no need to look it
18848 up. */
18849 else if (typename_p)
18850 decl = identifier;
18851 else
18853 tree ambiguous_decls;
18854 /* If we already know that this lookup is ambiguous, then
18855 we've already issued an error message; there's no reason
18856 to check again. */
18857 if (ambiguous_p)
18859 cp_parser_simulate_error (parser);
18860 return error_mark_node;
18862 /* If the next token is a `::', then the name must be a type
18863 name.
18865 [basic.lookup.qual]
18867 During the lookup for a name preceding the :: scope
18868 resolution operator, object, function, and enumerator
18869 names are ignored. */
18870 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18871 tag_type = typename_type;
18872 /* Look up the name. */
18873 decl = cp_parser_lookup_name (parser, identifier,
18874 tag_type,
18875 /*is_template=*/false,
18876 /*is_namespace=*/false,
18877 check_dependency_p,
18878 &ambiguous_decls,
18879 identifier_token->location);
18880 if (ambiguous_decls)
18882 if (cp_parser_parsing_tentatively (parser))
18883 cp_parser_simulate_error (parser);
18884 return error_mark_node;
18888 else
18890 /* Try a template-id. */
18891 decl = cp_parser_template_id (parser, template_keyword_p,
18892 check_dependency_p,
18893 tag_type,
18894 is_declaration);
18895 if (decl == error_mark_node)
18896 return error_mark_node;
18899 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
18901 /* If this is a typename, create a TYPENAME_TYPE. */
18902 if (typename_p && decl != error_mark_node)
18904 decl = make_typename_type (scope, decl, typename_type,
18905 /*complain=*/tf_error);
18906 if (decl != error_mark_node)
18907 decl = TYPE_NAME (decl);
18910 decl = strip_using_decl (decl);
18912 /* Check to see that it is really the name of a class. */
18913 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18914 && identifier_p (TREE_OPERAND (decl, 0))
18915 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18916 /* Situations like this:
18918 template <typename T> struct A {
18919 typename T::template X<int>::I i;
18922 are problematic. Is `T::template X<int>' a class-name? The
18923 standard does not seem to be definitive, but there is no other
18924 valid interpretation of the following `::'. Therefore, those
18925 names are considered class-names. */
18927 decl = make_typename_type (scope, decl, tag_type, tf_error);
18928 if (decl != error_mark_node)
18929 decl = TYPE_NAME (decl);
18931 else if (TREE_CODE (decl) != TYPE_DECL
18932 || TREE_TYPE (decl) == error_mark_node
18933 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
18934 /* In Objective-C 2.0, a classname followed by '.' starts a
18935 dot-syntax expression, and it's not a type-name. */
18936 || (c_dialect_objc ()
18937 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
18938 && objc_is_class_name (decl)))
18939 decl = error_mark_node;
18941 if (decl == error_mark_node)
18942 cp_parser_error (parser, "expected class-name");
18943 else if (identifier && !parser->scope)
18944 maybe_note_name_used_in_class (identifier, decl);
18946 return decl;
18949 /* Parse a class-specifier.
18951 class-specifier:
18952 class-head { member-specification [opt] }
18954 Returns the TREE_TYPE representing the class. */
18956 static tree
18957 cp_parser_class_specifier_1 (cp_parser* parser)
18959 tree type;
18960 tree attributes = NULL_TREE;
18961 bool nested_name_specifier_p;
18962 unsigned saved_num_template_parameter_lists;
18963 bool saved_in_function_body;
18964 unsigned char in_statement;
18965 bool in_switch_statement_p;
18966 bool saved_in_unbraced_linkage_specification_p;
18967 tree old_scope = NULL_TREE;
18968 tree scope = NULL_TREE;
18969 cp_token *closing_brace;
18971 push_deferring_access_checks (dk_no_deferred);
18973 /* Parse the class-head. */
18974 type = cp_parser_class_head (parser,
18975 &nested_name_specifier_p);
18976 /* If the class-head was a semantic disaster, skip the entire body
18977 of the class. */
18978 if (!type)
18980 cp_parser_skip_to_end_of_block_or_statement (parser);
18981 pop_deferring_access_checks ();
18982 return error_mark_node;
18985 /* Look for the `{'. */
18986 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
18988 pop_deferring_access_checks ();
18989 return error_mark_node;
18992 cp_ensure_no_omp_declare_simd (parser);
18994 /* Issue an error message if type-definitions are forbidden here. */
18995 cp_parser_check_type_definition (parser);
18996 /* Remember that we are defining one more class. */
18997 ++parser->num_classes_being_defined;
18998 /* Inside the class, surrounding template-parameter-lists do not
18999 apply. */
19000 saved_num_template_parameter_lists
19001 = parser->num_template_parameter_lists;
19002 parser->num_template_parameter_lists = 0;
19003 /* We are not in a function body. */
19004 saved_in_function_body = parser->in_function_body;
19005 parser->in_function_body = false;
19006 /* Or in a loop. */
19007 in_statement = parser->in_statement;
19008 parser->in_statement = 0;
19009 /* Or in a switch. */
19010 in_switch_statement_p = parser->in_switch_statement_p;
19011 parser->in_switch_statement_p = false;
19012 /* We are not immediately inside an extern "lang" block. */
19013 saved_in_unbraced_linkage_specification_p
19014 = parser->in_unbraced_linkage_specification_p;
19015 parser->in_unbraced_linkage_specification_p = false;
19017 /* Start the class. */
19018 if (nested_name_specifier_p)
19020 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19021 old_scope = push_inner_scope (scope);
19023 type = begin_class_definition (type);
19025 if (type == error_mark_node)
19026 /* If the type is erroneous, skip the entire body of the class. */
19027 cp_parser_skip_to_closing_brace (parser);
19028 else
19029 /* Parse the member-specification. */
19030 cp_parser_member_specification_opt (parser);
19032 /* Look for the trailing `}'. */
19033 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19034 /* Look for trailing attributes to apply to this class. */
19035 if (cp_parser_allow_gnu_extensions_p (parser))
19036 attributes = cp_parser_gnu_attributes_opt (parser);
19037 if (type != error_mark_node)
19038 type = finish_struct (type, attributes);
19039 if (nested_name_specifier_p)
19040 pop_inner_scope (old_scope, scope);
19042 /* We've finished a type definition. Check for the common syntax
19043 error of forgetting a semicolon after the definition. We need to
19044 be careful, as we can't just check for not-a-semicolon and be done
19045 with it; the user might have typed:
19047 class X { } c = ...;
19048 class X { } *p = ...;
19050 and so forth. Instead, enumerate all the possible tokens that
19051 might follow this production; if we don't see one of them, then
19052 complain and silently insert the semicolon. */
19054 cp_token *token = cp_lexer_peek_token (parser->lexer);
19055 bool want_semicolon = true;
19057 if (cp_next_tokens_can_be_std_attribute_p (parser))
19058 /* Don't try to parse c++11 attributes here. As per the
19059 grammar, that should be a task for
19060 cp_parser_decl_specifier_seq. */
19061 want_semicolon = false;
19063 switch (token->type)
19065 case CPP_NAME:
19066 case CPP_SEMICOLON:
19067 case CPP_MULT:
19068 case CPP_AND:
19069 case CPP_OPEN_PAREN:
19070 case CPP_CLOSE_PAREN:
19071 case CPP_COMMA:
19072 want_semicolon = false;
19073 break;
19075 /* While it's legal for type qualifiers and storage class
19076 specifiers to follow type definitions in the grammar, only
19077 compiler testsuites contain code like that. Assume that if
19078 we see such code, then what we're really seeing is a case
19079 like:
19081 class X { }
19082 const <type> var = ...;
19086 class Y { }
19087 static <type> func (...) ...
19089 i.e. the qualifier or specifier applies to the next
19090 declaration. To do so, however, we need to look ahead one
19091 more token to see if *that* token is a type specifier.
19093 This code could be improved to handle:
19095 class Z { }
19096 static const <type> var = ...; */
19097 case CPP_KEYWORD:
19098 if (keyword_is_decl_specifier (token->keyword))
19100 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19102 /* Handling user-defined types here would be nice, but very
19103 tricky. */
19104 want_semicolon
19105 = (lookahead->type == CPP_KEYWORD
19106 && keyword_begins_type_specifier (lookahead->keyword));
19108 break;
19109 default:
19110 break;
19113 /* If we don't have a type, then something is very wrong and we
19114 shouldn't try to do anything clever. Likewise for not seeing the
19115 closing brace. */
19116 if (closing_brace && TYPE_P (type) && want_semicolon)
19118 cp_token_position prev
19119 = cp_lexer_previous_token_position (parser->lexer);
19120 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19121 location_t loc = prev_token->location;
19123 if (CLASSTYPE_DECLARED_CLASS (type))
19124 error_at (loc, "expected %<;%> after class definition");
19125 else if (TREE_CODE (type) == RECORD_TYPE)
19126 error_at (loc, "expected %<;%> after struct definition");
19127 else if (TREE_CODE (type) == UNION_TYPE)
19128 error_at (loc, "expected %<;%> after union definition");
19129 else
19130 gcc_unreachable ();
19132 /* Unget one token and smash it to look as though we encountered
19133 a semicolon in the input stream. */
19134 cp_lexer_set_token_position (parser->lexer, prev);
19135 token = cp_lexer_peek_token (parser->lexer);
19136 token->type = CPP_SEMICOLON;
19137 token->keyword = RID_MAX;
19141 /* If this class is not itself within the scope of another class,
19142 then we need to parse the bodies of all of the queued function
19143 definitions. Note that the queued functions defined in a class
19144 are not always processed immediately following the
19145 class-specifier for that class. Consider:
19147 struct A {
19148 struct B { void f() { sizeof (A); } };
19151 If `f' were processed before the processing of `A' were
19152 completed, there would be no way to compute the size of `A'.
19153 Note that the nesting we are interested in here is lexical --
19154 not the semantic nesting given by TYPE_CONTEXT. In particular,
19155 for:
19157 struct A { struct B; };
19158 struct A::B { void f() { } };
19160 there is no need to delay the parsing of `A::B::f'. */
19161 if (--parser->num_classes_being_defined == 0)
19163 tree decl;
19164 tree class_type = NULL_TREE;
19165 tree pushed_scope = NULL_TREE;
19166 unsigned ix;
19167 cp_default_arg_entry *e;
19168 tree save_ccp, save_ccr;
19170 /* In a first pass, parse default arguments to the functions.
19171 Then, in a second pass, parse the bodies of the functions.
19172 This two-phased approach handles cases like:
19174 struct S {
19175 void f() { g(); }
19176 void g(int i = 3);
19180 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19182 decl = e->decl;
19183 /* If there are default arguments that have not yet been processed,
19184 take care of them now. */
19185 if (class_type != e->class_type)
19187 if (pushed_scope)
19188 pop_scope (pushed_scope);
19189 class_type = e->class_type;
19190 pushed_scope = push_scope (class_type);
19192 /* Make sure that any template parameters are in scope. */
19193 maybe_begin_member_template_processing (decl);
19194 /* Parse the default argument expressions. */
19195 cp_parser_late_parsing_default_args (parser, decl);
19196 /* Remove any template parameters from the symbol table. */
19197 maybe_end_member_template_processing ();
19199 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19200 /* Now parse any NSDMIs. */
19201 save_ccp = current_class_ptr;
19202 save_ccr = current_class_ref;
19203 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19205 if (class_type != DECL_CONTEXT (decl))
19207 if (pushed_scope)
19208 pop_scope (pushed_scope);
19209 class_type = DECL_CONTEXT (decl);
19210 pushed_scope = push_scope (class_type);
19212 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19213 cp_parser_late_parsing_nsdmi (parser, decl);
19215 vec_safe_truncate (unparsed_nsdmis, 0);
19216 current_class_ptr = save_ccp;
19217 current_class_ref = save_ccr;
19218 if (pushed_scope)
19219 pop_scope (pushed_scope);
19220 /* Now parse the body of the functions. */
19221 if (flag_openmp)
19223 /* OpenMP UDRs need to be parsed before all other functions. */
19224 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19225 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19226 cp_parser_late_parsing_for_member (parser, decl);
19227 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19228 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19229 cp_parser_late_parsing_for_member (parser, decl);
19231 else
19232 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19233 cp_parser_late_parsing_for_member (parser, decl);
19234 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19237 /* Put back any saved access checks. */
19238 pop_deferring_access_checks ();
19240 /* Restore saved state. */
19241 parser->in_switch_statement_p = in_switch_statement_p;
19242 parser->in_statement = in_statement;
19243 parser->in_function_body = saved_in_function_body;
19244 parser->num_template_parameter_lists
19245 = saved_num_template_parameter_lists;
19246 parser->in_unbraced_linkage_specification_p
19247 = saved_in_unbraced_linkage_specification_p;
19249 return type;
19252 static tree
19253 cp_parser_class_specifier (cp_parser* parser)
19255 tree ret;
19256 timevar_push (TV_PARSE_STRUCT);
19257 ret = cp_parser_class_specifier_1 (parser);
19258 timevar_pop (TV_PARSE_STRUCT);
19259 return ret;
19262 /* Parse a class-head.
19264 class-head:
19265 class-key identifier [opt] base-clause [opt]
19266 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19267 class-key nested-name-specifier [opt] template-id
19268 base-clause [opt]
19270 class-virt-specifier:
19271 final
19273 GNU Extensions:
19274 class-key attributes identifier [opt] base-clause [opt]
19275 class-key attributes nested-name-specifier identifier base-clause [opt]
19276 class-key attributes nested-name-specifier [opt] template-id
19277 base-clause [opt]
19279 Upon return BASES is initialized to the list of base classes (or
19280 NULL, if there are none) in the same form returned by
19281 cp_parser_base_clause.
19283 Returns the TYPE of the indicated class. Sets
19284 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19285 involving a nested-name-specifier was used, and FALSE otherwise.
19287 Returns error_mark_node if this is not a class-head.
19289 Returns NULL_TREE if the class-head is syntactically valid, but
19290 semantically invalid in a way that means we should skip the entire
19291 body of the class. */
19293 static tree
19294 cp_parser_class_head (cp_parser* parser,
19295 bool* nested_name_specifier_p)
19297 tree nested_name_specifier;
19298 enum tag_types class_key;
19299 tree id = NULL_TREE;
19300 tree type = NULL_TREE;
19301 tree attributes;
19302 tree bases;
19303 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19304 bool template_id_p = false;
19305 bool qualified_p = false;
19306 bool invalid_nested_name_p = false;
19307 bool invalid_explicit_specialization_p = false;
19308 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19309 tree pushed_scope = NULL_TREE;
19310 unsigned num_templates;
19311 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19312 /* Assume no nested-name-specifier will be present. */
19313 *nested_name_specifier_p = false;
19314 /* Assume no template parameter lists will be used in defining the
19315 type. */
19316 num_templates = 0;
19317 parser->colon_corrects_to_scope_p = false;
19319 /* Look for the class-key. */
19320 class_key = cp_parser_class_key (parser);
19321 if (class_key == none_type)
19322 return error_mark_node;
19324 /* Parse the attributes. */
19325 attributes = cp_parser_attributes_opt (parser);
19327 /* If the next token is `::', that is invalid -- but sometimes
19328 people do try to write:
19330 struct ::S {};
19332 Handle this gracefully by accepting the extra qualifier, and then
19333 issuing an error about it later if this really is a
19334 class-head. If it turns out just to be an elaborated type
19335 specifier, remain silent. */
19336 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19337 qualified_p = true;
19339 push_deferring_access_checks (dk_no_check);
19341 /* Determine the name of the class. Begin by looking for an
19342 optional nested-name-specifier. */
19343 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19344 nested_name_specifier
19345 = cp_parser_nested_name_specifier_opt (parser,
19346 /*typename_keyword_p=*/false,
19347 /*check_dependency_p=*/false,
19348 /*type_p=*/true,
19349 /*is_declaration=*/false);
19350 /* If there was a nested-name-specifier, then there *must* be an
19351 identifier. */
19352 if (nested_name_specifier)
19354 type_start_token = cp_lexer_peek_token (parser->lexer);
19355 /* Although the grammar says `identifier', it really means
19356 `class-name' or `template-name'. You are only allowed to
19357 define a class that has already been declared with this
19358 syntax.
19360 The proposed resolution for Core Issue 180 says that wherever
19361 you see `class T::X' you should treat `X' as a type-name.
19363 It is OK to define an inaccessible class; for example:
19365 class A { class B; };
19366 class A::B {};
19368 We do not know if we will see a class-name, or a
19369 template-name. We look for a class-name first, in case the
19370 class-name is a template-id; if we looked for the
19371 template-name first we would stop after the template-name. */
19372 cp_parser_parse_tentatively (parser);
19373 type = cp_parser_class_name (parser,
19374 /*typename_keyword_p=*/false,
19375 /*template_keyword_p=*/false,
19376 class_type,
19377 /*check_dependency_p=*/false,
19378 /*class_head_p=*/true,
19379 /*is_declaration=*/false);
19380 /* If that didn't work, ignore the nested-name-specifier. */
19381 if (!cp_parser_parse_definitely (parser))
19383 invalid_nested_name_p = true;
19384 type_start_token = cp_lexer_peek_token (parser->lexer);
19385 id = cp_parser_identifier (parser);
19386 if (id == error_mark_node)
19387 id = NULL_TREE;
19389 /* If we could not find a corresponding TYPE, treat this
19390 declaration like an unqualified declaration. */
19391 if (type == error_mark_node)
19392 nested_name_specifier = NULL_TREE;
19393 /* Otherwise, count the number of templates used in TYPE and its
19394 containing scopes. */
19395 else
19397 tree scope;
19399 for (scope = TREE_TYPE (type);
19400 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19401 scope = get_containing_scope (scope))
19402 if (TYPE_P (scope)
19403 && CLASS_TYPE_P (scope)
19404 && CLASSTYPE_TEMPLATE_INFO (scope)
19405 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19406 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19407 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19408 ++num_templates;
19411 /* Otherwise, the identifier is optional. */
19412 else
19414 /* We don't know whether what comes next is a template-id,
19415 an identifier, or nothing at all. */
19416 cp_parser_parse_tentatively (parser);
19417 /* Check for a template-id. */
19418 type_start_token = cp_lexer_peek_token (parser->lexer);
19419 id = cp_parser_template_id (parser,
19420 /*template_keyword_p=*/false,
19421 /*check_dependency_p=*/true,
19422 class_key,
19423 /*is_declaration=*/true);
19424 /* If that didn't work, it could still be an identifier. */
19425 if (!cp_parser_parse_definitely (parser))
19427 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19429 type_start_token = cp_lexer_peek_token (parser->lexer);
19430 id = cp_parser_identifier (parser);
19432 else
19433 id = NULL_TREE;
19435 else
19437 template_id_p = true;
19438 ++num_templates;
19442 pop_deferring_access_checks ();
19444 if (id)
19446 cp_parser_check_for_invalid_template_id (parser, id,
19447 class_key,
19448 type_start_token->location);
19450 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19452 /* If it's not a `:' or a `{' then we can't really be looking at a
19453 class-head, since a class-head only appears as part of a
19454 class-specifier. We have to detect this situation before calling
19455 xref_tag, since that has irreversible side-effects. */
19456 if (!cp_parser_next_token_starts_class_definition_p (parser))
19458 cp_parser_error (parser, "expected %<{%> or %<:%>");
19459 type = error_mark_node;
19460 goto out;
19463 /* At this point, we're going ahead with the class-specifier, even
19464 if some other problem occurs. */
19465 cp_parser_commit_to_tentative_parse (parser);
19466 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19468 cp_parser_error (parser,
19469 "cannot specify %<override%> for a class");
19470 type = error_mark_node;
19471 goto out;
19473 /* Issue the error about the overly-qualified name now. */
19474 if (qualified_p)
19476 cp_parser_error (parser,
19477 "global qualification of class name is invalid");
19478 type = error_mark_node;
19479 goto out;
19481 else if (invalid_nested_name_p)
19483 cp_parser_error (parser,
19484 "qualified name does not name a class");
19485 type = error_mark_node;
19486 goto out;
19488 else if (nested_name_specifier)
19490 tree scope;
19492 /* Reject typedef-names in class heads. */
19493 if (!DECL_IMPLICIT_TYPEDEF_P (type))
19495 error_at (type_start_token->location,
19496 "invalid class name in declaration of %qD",
19497 type);
19498 type = NULL_TREE;
19499 goto done;
19502 /* Figure out in what scope the declaration is being placed. */
19503 scope = current_scope ();
19504 /* If that scope does not contain the scope in which the
19505 class was originally declared, the program is invalid. */
19506 if (scope && !is_ancestor (scope, nested_name_specifier))
19508 if (at_namespace_scope_p ())
19509 error_at (type_start_token->location,
19510 "declaration of %qD in namespace %qD which does not "
19511 "enclose %qD",
19512 type, scope, nested_name_specifier);
19513 else
19514 error_at (type_start_token->location,
19515 "declaration of %qD in %qD which does not enclose %qD",
19516 type, scope, nested_name_specifier);
19517 type = NULL_TREE;
19518 goto done;
19520 /* [dcl.meaning]
19522 A declarator-id shall not be qualified except for the
19523 definition of a ... nested class outside of its class
19524 ... [or] the definition or explicit instantiation of a
19525 class member of a namespace outside of its namespace. */
19526 if (scope == nested_name_specifier)
19528 permerror (nested_name_specifier_token_start->location,
19529 "extra qualification not allowed");
19530 nested_name_specifier = NULL_TREE;
19531 num_templates = 0;
19534 /* An explicit-specialization must be preceded by "template <>". If
19535 it is not, try to recover gracefully. */
19536 if (at_namespace_scope_p ()
19537 && parser->num_template_parameter_lists == 0
19538 && template_id_p)
19540 error_at (type_start_token->location,
19541 "an explicit specialization must be preceded by %<template <>%>");
19542 invalid_explicit_specialization_p = true;
19543 /* Take the same action that would have been taken by
19544 cp_parser_explicit_specialization. */
19545 ++parser->num_template_parameter_lists;
19546 begin_specialization ();
19548 /* There must be no "return" statements between this point and the
19549 end of this function; set "type "to the correct return value and
19550 use "goto done;" to return. */
19551 /* Make sure that the right number of template parameters were
19552 present. */
19553 if (!cp_parser_check_template_parameters (parser, num_templates,
19554 type_start_token->location,
19555 /*declarator=*/NULL))
19557 /* If something went wrong, there is no point in even trying to
19558 process the class-definition. */
19559 type = NULL_TREE;
19560 goto done;
19563 /* Look up the type. */
19564 if (template_id_p)
19566 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
19567 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
19568 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
19570 error_at (type_start_token->location,
19571 "function template %qD redeclared as a class template", id);
19572 type = error_mark_node;
19574 else
19576 type = TREE_TYPE (id);
19577 type = maybe_process_partial_specialization (type);
19579 if (nested_name_specifier)
19580 pushed_scope = push_scope (nested_name_specifier);
19582 else if (nested_name_specifier)
19584 tree class_type;
19586 /* Given:
19588 template <typename T> struct S { struct T };
19589 template <typename T> struct S<T>::T { };
19591 we will get a TYPENAME_TYPE when processing the definition of
19592 `S::T'. We need to resolve it to the actual type before we
19593 try to define it. */
19594 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
19596 class_type = resolve_typename_type (TREE_TYPE (type),
19597 /*only_current_p=*/false);
19598 if (TREE_CODE (class_type) != TYPENAME_TYPE)
19599 type = TYPE_NAME (class_type);
19600 else
19602 cp_parser_error (parser, "could not resolve typename type");
19603 type = error_mark_node;
19607 if (maybe_process_partial_specialization (TREE_TYPE (type))
19608 == error_mark_node)
19610 type = NULL_TREE;
19611 goto done;
19614 class_type = current_class_type;
19615 /* Enter the scope indicated by the nested-name-specifier. */
19616 pushed_scope = push_scope (nested_name_specifier);
19617 /* Get the canonical version of this type. */
19618 type = TYPE_MAIN_DECL (TREE_TYPE (type));
19619 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
19620 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
19622 type = push_template_decl (type);
19623 if (type == error_mark_node)
19625 type = NULL_TREE;
19626 goto done;
19630 type = TREE_TYPE (type);
19631 *nested_name_specifier_p = true;
19633 else /* The name is not a nested name. */
19635 /* If the class was unnamed, create a dummy name. */
19636 if (!id)
19637 id = make_anon_name ();
19638 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
19639 parser->num_template_parameter_lists);
19642 /* Indicate whether this class was declared as a `class' or as a
19643 `struct'. */
19644 if (TREE_CODE (type) == RECORD_TYPE)
19645 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
19646 cp_parser_check_class_key (class_key, type);
19648 /* If this type was already complete, and we see another definition,
19649 that's an error. */
19650 if (type != error_mark_node && COMPLETE_TYPE_P (type))
19652 error_at (type_start_token->location, "redefinition of %q#T",
19653 type);
19654 error_at (type_start_token->location, "previous definition of %q+#T",
19655 type);
19656 type = NULL_TREE;
19657 goto done;
19659 else if (type == error_mark_node)
19660 type = NULL_TREE;
19662 if (type)
19664 /* Apply attributes now, before any use of the class as a template
19665 argument in its base list. */
19666 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19667 fixup_attribute_variants (type);
19670 /* We will have entered the scope containing the class; the names of
19671 base classes should be looked up in that context. For example:
19673 struct A { struct B {}; struct C; };
19674 struct A::C : B {};
19676 is valid. */
19678 /* Get the list of base-classes, if there is one. */
19679 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19680 bases = cp_parser_base_clause (parser);
19681 else
19682 bases = NULL_TREE;
19684 /* If we're really defining a class, process the base classes.
19685 If they're invalid, fail. */
19686 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19687 && !xref_basetypes (type, bases))
19688 type = NULL_TREE;
19690 done:
19691 /* Leave the scope given by the nested-name-specifier. We will
19692 enter the class scope itself while processing the members. */
19693 if (pushed_scope)
19694 pop_scope (pushed_scope);
19696 if (invalid_explicit_specialization_p)
19698 end_specialization ();
19699 --parser->num_template_parameter_lists;
19702 if (type)
19703 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
19704 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
19705 CLASSTYPE_FINAL (type) = 1;
19706 out:
19707 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19708 return type;
19711 /* Parse a class-key.
19713 class-key:
19714 class
19715 struct
19716 union
19718 Returns the kind of class-key specified, or none_type to indicate
19719 error. */
19721 static enum tag_types
19722 cp_parser_class_key (cp_parser* parser)
19724 cp_token *token;
19725 enum tag_types tag_type;
19727 /* Look for the class-key. */
19728 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
19729 if (!token)
19730 return none_type;
19732 /* Check to see if the TOKEN is a class-key. */
19733 tag_type = cp_parser_token_is_class_key (token);
19734 if (!tag_type)
19735 cp_parser_error (parser, "expected class-key");
19736 return tag_type;
19739 /* Parse an (optional) member-specification.
19741 member-specification:
19742 member-declaration member-specification [opt]
19743 access-specifier : member-specification [opt] */
19745 static void
19746 cp_parser_member_specification_opt (cp_parser* parser)
19748 while (true)
19750 cp_token *token;
19751 enum rid keyword;
19753 /* Peek at the next token. */
19754 token = cp_lexer_peek_token (parser->lexer);
19755 /* If it's a `}', or EOF then we've seen all the members. */
19756 if (token->type == CPP_CLOSE_BRACE
19757 || token->type == CPP_EOF
19758 || token->type == CPP_PRAGMA_EOL)
19759 break;
19761 /* See if this token is a keyword. */
19762 keyword = token->keyword;
19763 switch (keyword)
19765 case RID_PUBLIC:
19766 case RID_PROTECTED:
19767 case RID_PRIVATE:
19768 /* Consume the access-specifier. */
19769 cp_lexer_consume_token (parser->lexer);
19770 /* Remember which access-specifier is active. */
19771 current_access_specifier = token->u.value;
19772 /* Look for the `:'. */
19773 cp_parser_require (parser, CPP_COLON, RT_COLON);
19774 break;
19776 default:
19777 /* Accept #pragmas at class scope. */
19778 if (token->type == CPP_PRAGMA)
19780 cp_parser_pragma (parser, pragma_member);
19781 break;
19784 /* Otherwise, the next construction must be a
19785 member-declaration. */
19786 cp_parser_member_declaration (parser);
19791 /* Parse a member-declaration.
19793 member-declaration:
19794 decl-specifier-seq [opt] member-declarator-list [opt] ;
19795 function-definition ; [opt]
19796 :: [opt] nested-name-specifier template [opt] unqualified-id ;
19797 using-declaration
19798 template-declaration
19799 alias-declaration
19801 member-declarator-list:
19802 member-declarator
19803 member-declarator-list , member-declarator
19805 member-declarator:
19806 declarator pure-specifier [opt]
19807 declarator constant-initializer [opt]
19808 identifier [opt] : constant-expression
19810 GNU Extensions:
19812 member-declaration:
19813 __extension__ member-declaration
19815 member-declarator:
19816 declarator attributes [opt] pure-specifier [opt]
19817 declarator attributes [opt] constant-initializer [opt]
19818 identifier [opt] attributes [opt] : constant-expression
19820 C++0x Extensions:
19822 member-declaration:
19823 static_assert-declaration */
19825 static void
19826 cp_parser_member_declaration (cp_parser* parser)
19828 cp_decl_specifier_seq decl_specifiers;
19829 tree prefix_attributes;
19830 tree decl;
19831 int declares_class_or_enum;
19832 bool friend_p;
19833 cp_token *token = NULL;
19834 cp_token *decl_spec_token_start = NULL;
19835 cp_token *initializer_token_start = NULL;
19836 int saved_pedantic;
19837 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19839 /* Check for the `__extension__' keyword. */
19840 if (cp_parser_extension_opt (parser, &saved_pedantic))
19842 /* Recurse. */
19843 cp_parser_member_declaration (parser);
19844 /* Restore the old value of the PEDANTIC flag. */
19845 pedantic = saved_pedantic;
19847 return;
19850 /* Check for a template-declaration. */
19851 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19853 /* An explicit specialization here is an error condition, and we
19854 expect the specialization handler to detect and report this. */
19855 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19856 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
19857 cp_parser_explicit_specialization (parser);
19858 else
19859 cp_parser_template_declaration (parser, /*member_p=*/true);
19861 return;
19864 /* Check for a using-declaration. */
19865 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
19867 if (cxx_dialect < cxx11)
19869 /* Parse the using-declaration. */
19870 cp_parser_using_declaration (parser,
19871 /*access_declaration_p=*/false);
19872 return;
19874 else
19876 tree decl;
19877 bool alias_decl_expected;
19878 cp_parser_parse_tentatively (parser);
19879 decl = cp_parser_alias_declaration (parser);
19880 /* Note that if we actually see the '=' token after the
19881 identifier, cp_parser_alias_declaration commits the
19882 tentative parse. In that case, we really expects an
19883 alias-declaration. Otherwise, we expect a using
19884 declaration. */
19885 alias_decl_expected =
19886 !cp_parser_uncommitted_to_tentative_parse_p (parser);
19887 cp_parser_parse_definitely (parser);
19889 if (alias_decl_expected)
19890 finish_member_declaration (decl);
19891 else
19892 cp_parser_using_declaration (parser,
19893 /*access_declaration_p=*/false);
19894 return;
19898 /* Check for @defs. */
19899 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
19901 tree ivar, member;
19902 tree ivar_chains = cp_parser_objc_defs_expression (parser);
19903 ivar = ivar_chains;
19904 while (ivar)
19906 member = ivar;
19907 ivar = TREE_CHAIN (member);
19908 TREE_CHAIN (member) = NULL_TREE;
19909 finish_member_declaration (member);
19911 return;
19914 /* If the next token is `static_assert' we have a static assertion. */
19915 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
19917 cp_parser_static_assert (parser, /*member_p=*/true);
19918 return;
19921 parser->colon_corrects_to_scope_p = false;
19923 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
19924 goto out;
19926 /* Parse the decl-specifier-seq. */
19927 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19928 cp_parser_decl_specifier_seq (parser,
19929 CP_PARSER_FLAGS_OPTIONAL,
19930 &decl_specifiers,
19931 &declares_class_or_enum);
19932 /* Check for an invalid type-name. */
19933 if (!decl_specifiers.any_type_specifiers_p
19934 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19935 goto out;
19936 /* If there is no declarator, then the decl-specifier-seq should
19937 specify a type. */
19938 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19940 /* If there was no decl-specifier-seq, and the next token is a
19941 `;', then we have something like:
19943 struct S { ; };
19945 [class.mem]
19947 Each member-declaration shall declare at least one member
19948 name of the class. */
19949 if (!decl_specifiers.any_specifiers_p)
19951 cp_token *token = cp_lexer_peek_token (parser->lexer);
19952 if (!in_system_header_at (token->location))
19953 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
19955 else
19957 tree type;
19959 /* See if this declaration is a friend. */
19960 friend_p = cp_parser_friend_p (&decl_specifiers);
19961 /* If there were decl-specifiers, check to see if there was
19962 a class-declaration. */
19963 type = check_tag_decl (&decl_specifiers,
19964 /*explicit_type_instantiation_p=*/false);
19965 /* Nested classes have already been added to the class, but
19966 a `friend' needs to be explicitly registered. */
19967 if (friend_p)
19969 /* If the `friend' keyword was present, the friend must
19970 be introduced with a class-key. */
19971 if (!declares_class_or_enum && cxx_dialect < cxx11)
19972 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
19973 "in C++03 a class-key must be used "
19974 "when declaring a friend");
19975 /* In this case:
19977 template <typename T> struct A {
19978 friend struct A<T>::B;
19981 A<T>::B will be represented by a TYPENAME_TYPE, and
19982 therefore not recognized by check_tag_decl. */
19983 if (!type)
19985 type = decl_specifiers.type;
19986 if (type && TREE_CODE (type) == TYPE_DECL)
19987 type = TREE_TYPE (type);
19989 if (!type || !TYPE_P (type))
19990 error_at (decl_spec_token_start->location,
19991 "friend declaration does not name a class or "
19992 "function");
19993 else
19994 make_friend_class (current_class_type, type,
19995 /*complain=*/true);
19997 /* If there is no TYPE, an error message will already have
19998 been issued. */
19999 else if (!type || type == error_mark_node)
20001 /* An anonymous aggregate has to be handled specially; such
20002 a declaration really declares a data member (with a
20003 particular type), as opposed to a nested class. */
20004 else if (ANON_AGGR_TYPE_P (type))
20006 /* C++11 9.5/6. */
20007 if (decl_specifiers.storage_class != sc_none)
20008 error_at (decl_spec_token_start->location,
20009 "a storage class on an anonymous aggregate "
20010 "in class scope is not allowed");
20012 /* Remove constructors and such from TYPE, now that we
20013 know it is an anonymous aggregate. */
20014 fixup_anonymous_aggr (type);
20015 /* And make the corresponding data member. */
20016 decl = build_decl (decl_spec_token_start->location,
20017 FIELD_DECL, NULL_TREE, type);
20018 /* Add it to the class. */
20019 finish_member_declaration (decl);
20021 else
20022 cp_parser_check_access_in_redeclaration
20023 (TYPE_NAME (type),
20024 decl_spec_token_start->location);
20027 else
20029 bool assume_semicolon = false;
20031 /* Clear attributes from the decl_specifiers but keep them
20032 around as prefix attributes that apply them to the entity
20033 being declared. */
20034 prefix_attributes = decl_specifiers.attributes;
20035 decl_specifiers.attributes = NULL_TREE;
20037 /* See if these declarations will be friends. */
20038 friend_p = cp_parser_friend_p (&decl_specifiers);
20040 /* Keep going until we hit the `;' at the end of the
20041 declaration. */
20042 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20044 tree attributes = NULL_TREE;
20045 tree first_attribute;
20047 /* Peek at the next token. */
20048 token = cp_lexer_peek_token (parser->lexer);
20050 /* Check for a bitfield declaration. */
20051 if (token->type == CPP_COLON
20052 || (token->type == CPP_NAME
20053 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20054 == CPP_COLON))
20056 tree identifier;
20057 tree width;
20059 /* Get the name of the bitfield. Note that we cannot just
20060 check TOKEN here because it may have been invalidated by
20061 the call to cp_lexer_peek_nth_token above. */
20062 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20063 identifier = cp_parser_identifier (parser);
20064 else
20065 identifier = NULL_TREE;
20067 /* Consume the `:' token. */
20068 cp_lexer_consume_token (parser->lexer);
20069 /* Get the width of the bitfield. */
20070 width
20071 = cp_parser_constant_expression (parser,
20072 /*allow_non_constant=*/false,
20073 NULL);
20075 /* Look for attributes that apply to the bitfield. */
20076 attributes = cp_parser_attributes_opt (parser);
20077 /* Remember which attributes are prefix attributes and
20078 which are not. */
20079 first_attribute = attributes;
20080 /* Combine the attributes. */
20081 attributes = chainon (prefix_attributes, attributes);
20083 /* Create the bitfield declaration. */
20084 decl = grokbitfield (identifier
20085 ? make_id_declarator (NULL_TREE,
20086 identifier,
20087 sfk_none)
20088 : NULL,
20089 &decl_specifiers,
20090 width,
20091 attributes);
20093 else
20095 cp_declarator *declarator;
20096 tree initializer;
20097 tree asm_specification;
20098 int ctor_dtor_or_conv_p;
20100 /* Parse the declarator. */
20101 declarator
20102 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20103 &ctor_dtor_or_conv_p,
20104 /*parenthesized_p=*/NULL,
20105 /*member_p=*/true);
20107 /* If something went wrong parsing the declarator, make sure
20108 that we at least consume some tokens. */
20109 if (declarator == cp_error_declarator)
20111 /* Skip to the end of the statement. */
20112 cp_parser_skip_to_end_of_statement (parser);
20113 /* If the next token is not a semicolon, that is
20114 probably because we just skipped over the body of
20115 a function. So, we consume a semicolon if
20116 present, but do not issue an error message if it
20117 is not present. */
20118 if (cp_lexer_next_token_is (parser->lexer,
20119 CPP_SEMICOLON))
20120 cp_lexer_consume_token (parser->lexer);
20121 goto out;
20124 if (declares_class_or_enum & 2)
20125 cp_parser_check_for_definition_in_return_type
20126 (declarator, decl_specifiers.type,
20127 decl_specifiers.locations[ds_type_spec]);
20129 /* Look for an asm-specification. */
20130 asm_specification = cp_parser_asm_specification_opt (parser);
20131 /* Look for attributes that apply to the declaration. */
20132 attributes = cp_parser_attributes_opt (parser);
20133 /* Remember which attributes are prefix attributes and
20134 which are not. */
20135 first_attribute = attributes;
20136 /* Combine the attributes. */
20137 attributes = chainon (prefix_attributes, attributes);
20139 /* If it's an `=', then we have a constant-initializer or a
20140 pure-specifier. It is not correct to parse the
20141 initializer before registering the member declaration
20142 since the member declaration should be in scope while
20143 its initializer is processed. However, the rest of the
20144 front end does not yet provide an interface that allows
20145 us to handle this correctly. */
20146 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20148 /* In [class.mem]:
20150 A pure-specifier shall be used only in the declaration of
20151 a virtual function.
20153 A member-declarator can contain a constant-initializer
20154 only if it declares a static member of integral or
20155 enumeration type.
20157 Therefore, if the DECLARATOR is for a function, we look
20158 for a pure-specifier; otherwise, we look for a
20159 constant-initializer. When we call `grokfield', it will
20160 perform more stringent semantics checks. */
20161 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20162 if (function_declarator_p (declarator)
20163 || (decl_specifiers.type
20164 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20165 && declarator->kind == cdk_id
20166 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20167 == FUNCTION_TYPE)))
20168 initializer = cp_parser_pure_specifier (parser);
20169 else if (decl_specifiers.storage_class != sc_static)
20170 initializer = cp_parser_save_nsdmi (parser);
20171 else if (cxx_dialect >= cxx11)
20173 bool nonconst;
20174 /* Don't require a constant rvalue in C++11, since we
20175 might want a reference constant. We'll enforce
20176 constancy later. */
20177 cp_lexer_consume_token (parser->lexer);
20178 /* Parse the initializer. */
20179 initializer = cp_parser_initializer_clause (parser,
20180 &nonconst);
20182 else
20183 /* Parse the initializer. */
20184 initializer = cp_parser_constant_initializer (parser);
20186 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20187 && !function_declarator_p (declarator))
20189 bool x;
20190 if (decl_specifiers.storage_class != sc_static)
20191 initializer = cp_parser_save_nsdmi (parser);
20192 else
20193 initializer = cp_parser_initializer (parser, &x, &x);
20195 /* Otherwise, there is no initializer. */
20196 else
20197 initializer = NULL_TREE;
20199 /* See if we are probably looking at a function
20200 definition. We are certainly not looking at a
20201 member-declarator. Calling `grokfield' has
20202 side-effects, so we must not do it unless we are sure
20203 that we are looking at a member-declarator. */
20204 if (cp_parser_token_starts_function_definition_p
20205 (cp_lexer_peek_token (parser->lexer)))
20207 /* The grammar does not allow a pure-specifier to be
20208 used when a member function is defined. (It is
20209 possible that this fact is an oversight in the
20210 standard, since a pure function may be defined
20211 outside of the class-specifier. */
20212 if (initializer && initializer_token_start)
20213 error_at (initializer_token_start->location,
20214 "pure-specifier on function-definition");
20215 decl = cp_parser_save_member_function_body (parser,
20216 &decl_specifiers,
20217 declarator,
20218 attributes);
20219 /* If the member was not a friend, declare it here. */
20220 if (!friend_p)
20222 if (parser->fully_implicit_function_template_p)
20223 decl = finish_fully_implicit_template (parser, decl);
20224 finish_member_declaration (decl);
20226 /* Peek at the next token. */
20227 token = cp_lexer_peek_token (parser->lexer);
20228 /* If the next token is a semicolon, consume it. */
20229 if (token->type == CPP_SEMICOLON)
20230 cp_lexer_consume_token (parser->lexer);
20231 goto out;
20233 else
20234 if (declarator->kind == cdk_function)
20235 declarator->id_loc = token->location;
20236 /* Create the declaration. */
20237 decl = grokfield (declarator, &decl_specifiers,
20238 initializer, /*init_const_expr_p=*/true,
20239 asm_specification, attributes);
20240 if (parser->fully_implicit_function_template_p)
20241 decl = finish_fully_implicit_template (parser, decl);
20244 cp_finalize_omp_declare_simd (parser, decl);
20246 /* Reset PREFIX_ATTRIBUTES. */
20247 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20248 attributes = TREE_CHAIN (attributes);
20249 if (attributes)
20250 TREE_CHAIN (attributes) = NULL_TREE;
20252 /* If there is any qualification still in effect, clear it
20253 now; we will be starting fresh with the next declarator. */
20254 parser->scope = NULL_TREE;
20255 parser->qualifying_scope = NULL_TREE;
20256 parser->object_scope = NULL_TREE;
20257 /* If it's a `,', then there are more declarators. */
20258 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20260 cp_lexer_consume_token (parser->lexer);
20261 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20263 cp_token *token = cp_lexer_previous_token (parser->lexer);
20264 error_at (token->location,
20265 "stray %<,%> at end of member declaration");
20268 /* If the next token isn't a `;', then we have a parse error. */
20269 else if (cp_lexer_next_token_is_not (parser->lexer,
20270 CPP_SEMICOLON))
20272 /* The next token might be a ways away from where the
20273 actual semicolon is missing. Find the previous token
20274 and use that for our error position. */
20275 cp_token *token = cp_lexer_previous_token (parser->lexer);
20276 error_at (token->location,
20277 "expected %<;%> at end of member declaration");
20279 /* Assume that the user meant to provide a semicolon. If
20280 we were to cp_parser_skip_to_end_of_statement, we might
20281 skip to a semicolon inside a member function definition
20282 and issue nonsensical error messages. */
20283 assume_semicolon = true;
20286 if (decl)
20288 /* Add DECL to the list of members. */
20289 if (!friend_p)
20290 finish_member_declaration (decl);
20292 if (TREE_CODE (decl) == FUNCTION_DECL)
20293 cp_parser_save_default_args (parser, decl);
20294 else if (TREE_CODE (decl) == FIELD_DECL
20295 && !DECL_C_BIT_FIELD (decl)
20296 && DECL_INITIAL (decl))
20297 /* Add DECL to the queue of NSDMI to be parsed later. */
20298 vec_safe_push (unparsed_nsdmis, decl);
20301 if (assume_semicolon)
20302 goto out;
20306 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20307 out:
20308 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20311 /* Parse a pure-specifier.
20313 pure-specifier:
20316 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20317 Otherwise, ERROR_MARK_NODE is returned. */
20319 static tree
20320 cp_parser_pure_specifier (cp_parser* parser)
20322 cp_token *token;
20324 /* Look for the `=' token. */
20325 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20326 return error_mark_node;
20327 /* Look for the `0' token. */
20328 token = cp_lexer_peek_token (parser->lexer);
20330 if (token->type == CPP_EOF
20331 || token->type == CPP_PRAGMA_EOL)
20332 return error_mark_node;
20334 cp_lexer_consume_token (parser->lexer);
20336 /* Accept = default or = delete in c++0x mode. */
20337 if (token->keyword == RID_DEFAULT
20338 || token->keyword == RID_DELETE)
20340 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20341 return token->u.value;
20344 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20345 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20347 cp_parser_error (parser,
20348 "invalid pure specifier (only %<= 0%> is allowed)");
20349 cp_parser_skip_to_end_of_statement (parser);
20350 return error_mark_node;
20352 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20354 error_at (token->location, "templates may not be %<virtual%>");
20355 return error_mark_node;
20358 return integer_zero_node;
20361 /* Parse a constant-initializer.
20363 constant-initializer:
20364 = constant-expression
20366 Returns a representation of the constant-expression. */
20368 static tree
20369 cp_parser_constant_initializer (cp_parser* parser)
20371 /* Look for the `=' token. */
20372 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20373 return error_mark_node;
20375 /* It is invalid to write:
20377 struct S { static const int i = { 7 }; };
20380 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20382 cp_parser_error (parser,
20383 "a brace-enclosed initializer is not allowed here");
20384 /* Consume the opening brace. */
20385 cp_lexer_consume_token (parser->lexer);
20386 /* Skip the initializer. */
20387 cp_parser_skip_to_closing_brace (parser);
20388 /* Look for the trailing `}'. */
20389 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20391 return error_mark_node;
20394 return cp_parser_constant_expression (parser,
20395 /*allow_non_constant=*/false,
20396 NULL);
20399 /* Derived classes [gram.class.derived] */
20401 /* Parse a base-clause.
20403 base-clause:
20404 : base-specifier-list
20406 base-specifier-list:
20407 base-specifier ... [opt]
20408 base-specifier-list , base-specifier ... [opt]
20410 Returns a TREE_LIST representing the base-classes, in the order in
20411 which they were declared. The representation of each node is as
20412 described by cp_parser_base_specifier.
20414 In the case that no bases are specified, this function will return
20415 NULL_TREE, not ERROR_MARK_NODE. */
20417 static tree
20418 cp_parser_base_clause (cp_parser* parser)
20420 tree bases = NULL_TREE;
20422 /* Look for the `:' that begins the list. */
20423 cp_parser_require (parser, CPP_COLON, RT_COLON);
20425 /* Scan the base-specifier-list. */
20426 while (true)
20428 cp_token *token;
20429 tree base;
20430 bool pack_expansion_p = false;
20432 /* Look for the base-specifier. */
20433 base = cp_parser_base_specifier (parser);
20434 /* Look for the (optional) ellipsis. */
20435 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20437 /* Consume the `...'. */
20438 cp_lexer_consume_token (parser->lexer);
20440 pack_expansion_p = true;
20443 /* Add BASE to the front of the list. */
20444 if (base && base != error_mark_node)
20446 if (pack_expansion_p)
20447 /* Make this a pack expansion type. */
20448 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
20450 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
20452 TREE_CHAIN (base) = bases;
20453 bases = base;
20456 /* Peek at the next token. */
20457 token = cp_lexer_peek_token (parser->lexer);
20458 /* If it's not a comma, then the list is complete. */
20459 if (token->type != CPP_COMMA)
20460 break;
20461 /* Consume the `,'. */
20462 cp_lexer_consume_token (parser->lexer);
20465 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20466 base class had a qualified name. However, the next name that
20467 appears is certainly not qualified. */
20468 parser->scope = NULL_TREE;
20469 parser->qualifying_scope = NULL_TREE;
20470 parser->object_scope = NULL_TREE;
20472 return nreverse (bases);
20475 /* Parse a base-specifier.
20477 base-specifier:
20478 :: [opt] nested-name-specifier [opt] class-name
20479 virtual access-specifier [opt] :: [opt] nested-name-specifier
20480 [opt] class-name
20481 access-specifier virtual [opt] :: [opt] nested-name-specifier
20482 [opt] class-name
20484 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20485 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20486 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20487 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20489 static tree
20490 cp_parser_base_specifier (cp_parser* parser)
20492 cp_token *token;
20493 bool done = false;
20494 bool virtual_p = false;
20495 bool duplicate_virtual_error_issued_p = false;
20496 bool duplicate_access_error_issued_p = false;
20497 bool class_scope_p, template_p;
20498 tree access = access_default_node;
20499 tree type;
20501 /* Process the optional `virtual' and `access-specifier'. */
20502 while (!done)
20504 /* Peek at the next token. */
20505 token = cp_lexer_peek_token (parser->lexer);
20506 /* Process `virtual'. */
20507 switch (token->keyword)
20509 case RID_VIRTUAL:
20510 /* If `virtual' appears more than once, issue an error. */
20511 if (virtual_p && !duplicate_virtual_error_issued_p)
20513 cp_parser_error (parser,
20514 "%<virtual%> specified more than once in base-specified");
20515 duplicate_virtual_error_issued_p = true;
20518 virtual_p = true;
20520 /* Consume the `virtual' token. */
20521 cp_lexer_consume_token (parser->lexer);
20523 break;
20525 case RID_PUBLIC:
20526 case RID_PROTECTED:
20527 case RID_PRIVATE:
20528 /* If more than one access specifier appears, issue an
20529 error. */
20530 if (access != access_default_node
20531 && !duplicate_access_error_issued_p)
20533 cp_parser_error (parser,
20534 "more than one access specifier in base-specified");
20535 duplicate_access_error_issued_p = true;
20538 access = ridpointers[(int) token->keyword];
20540 /* Consume the access-specifier. */
20541 cp_lexer_consume_token (parser->lexer);
20543 break;
20545 default:
20546 done = true;
20547 break;
20550 /* It is not uncommon to see programs mechanically, erroneously, use
20551 the 'typename' keyword to denote (dependent) qualified types
20552 as base classes. */
20553 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
20555 token = cp_lexer_peek_token (parser->lexer);
20556 if (!processing_template_decl)
20557 error_at (token->location,
20558 "keyword %<typename%> not allowed outside of templates");
20559 else
20560 error_at (token->location,
20561 "keyword %<typename%> not allowed in this context "
20562 "(the base class is implicitly a type)");
20563 cp_lexer_consume_token (parser->lexer);
20566 /* Look for the optional `::' operator. */
20567 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20568 /* Look for the nested-name-specifier. The simplest way to
20569 implement:
20571 [temp.res]
20573 The keyword `typename' is not permitted in a base-specifier or
20574 mem-initializer; in these contexts a qualified name that
20575 depends on a template-parameter is implicitly assumed to be a
20576 type name.
20578 is to pretend that we have seen the `typename' keyword at this
20579 point. */
20580 cp_parser_nested_name_specifier_opt (parser,
20581 /*typename_keyword_p=*/true,
20582 /*check_dependency_p=*/true,
20583 typename_type,
20584 /*is_declaration=*/true);
20585 /* If the base class is given by a qualified name, assume that names
20586 we see are type names or templates, as appropriate. */
20587 class_scope_p = (parser->scope && TYPE_P (parser->scope));
20588 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
20590 if (!parser->scope
20591 && cp_lexer_next_token_is_decltype (parser->lexer))
20592 /* DR 950 allows decltype as a base-specifier. */
20593 type = cp_parser_decltype (parser);
20594 else
20596 /* Otherwise, look for the class-name. */
20597 type = cp_parser_class_name (parser,
20598 class_scope_p,
20599 template_p,
20600 typename_type,
20601 /*check_dependency_p=*/true,
20602 /*class_head_p=*/false,
20603 /*is_declaration=*/true);
20604 type = TREE_TYPE (type);
20607 if (type == error_mark_node)
20608 return error_mark_node;
20610 return finish_base_specifier (type, access, virtual_p);
20613 /* Exception handling [gram.exception] */
20615 /* Parse an (optional) noexcept-specification.
20617 noexcept-specification:
20618 noexcept ( constant-expression ) [opt]
20620 If no noexcept-specification is present, returns NULL_TREE.
20621 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20622 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20623 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20624 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20625 in which case a boolean condition is returned instead. */
20627 static tree
20628 cp_parser_noexcept_specification_opt (cp_parser* parser,
20629 bool require_constexpr,
20630 bool* consumed_expr,
20631 bool return_cond)
20633 cp_token *token;
20634 const char *saved_message;
20636 /* Peek at the next token. */
20637 token = cp_lexer_peek_token (parser->lexer);
20639 /* Is it a noexcept-specification? */
20640 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
20642 tree expr;
20643 cp_lexer_consume_token (parser->lexer);
20645 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
20647 cp_lexer_consume_token (parser->lexer);
20649 if (require_constexpr)
20651 /* Types may not be defined in an exception-specification. */
20652 saved_message = parser->type_definition_forbidden_message;
20653 parser->type_definition_forbidden_message
20654 = G_("types may not be defined in an exception-specification");
20656 expr = cp_parser_constant_expression (parser, false, NULL);
20658 /* Restore the saved message. */
20659 parser->type_definition_forbidden_message = saved_message;
20661 else
20663 expr = cp_parser_expression (parser, false, NULL);
20664 *consumed_expr = true;
20667 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20669 else
20671 expr = boolean_true_node;
20672 if (!require_constexpr)
20673 *consumed_expr = false;
20676 /* We cannot build a noexcept-spec right away because this will check
20677 that expr is a constexpr. */
20678 if (!return_cond)
20679 return build_noexcept_spec (expr, tf_warning_or_error);
20680 else
20681 return expr;
20683 else
20684 return NULL_TREE;
20687 /* Parse an (optional) exception-specification.
20689 exception-specification:
20690 throw ( type-id-list [opt] )
20692 Returns a TREE_LIST representing the exception-specification. The
20693 TREE_VALUE of each node is a type. */
20695 static tree
20696 cp_parser_exception_specification_opt (cp_parser* parser)
20698 cp_token *token;
20699 tree type_id_list;
20700 const char *saved_message;
20702 /* Peek at the next token. */
20703 token = cp_lexer_peek_token (parser->lexer);
20705 /* Is it a noexcept-specification? */
20706 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
20707 false);
20708 if (type_id_list != NULL_TREE)
20709 return type_id_list;
20711 /* If it's not `throw', then there's no exception-specification. */
20712 if (!cp_parser_is_keyword (token, RID_THROW))
20713 return NULL_TREE;
20715 #if 0
20716 /* Enable this once a lot of code has transitioned to noexcept? */
20717 if (cxx_dialect >= cxx11 && !in_system_header)
20718 warning (OPT_Wdeprecated, "dynamic exception specifications are "
20719 "deprecated in C++0x; use %<noexcept%> instead");
20720 #endif
20722 /* Consume the `throw'. */
20723 cp_lexer_consume_token (parser->lexer);
20725 /* Look for the `('. */
20726 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20728 /* Peek at the next token. */
20729 token = cp_lexer_peek_token (parser->lexer);
20730 /* If it's not a `)', then there is a type-id-list. */
20731 if (token->type != CPP_CLOSE_PAREN)
20733 /* Types may not be defined in an exception-specification. */
20734 saved_message = parser->type_definition_forbidden_message;
20735 parser->type_definition_forbidden_message
20736 = G_("types may not be defined in an exception-specification");
20737 /* Parse the type-id-list. */
20738 type_id_list = cp_parser_type_id_list (parser);
20739 /* Restore the saved message. */
20740 parser->type_definition_forbidden_message = saved_message;
20742 else
20743 type_id_list = empty_except_spec;
20745 /* Look for the `)'. */
20746 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20748 return type_id_list;
20751 /* Parse an (optional) type-id-list.
20753 type-id-list:
20754 type-id ... [opt]
20755 type-id-list , type-id ... [opt]
20757 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
20758 in the order that the types were presented. */
20760 static tree
20761 cp_parser_type_id_list (cp_parser* parser)
20763 tree types = NULL_TREE;
20765 while (true)
20767 cp_token *token;
20768 tree type;
20770 /* Get the next type-id. */
20771 type = cp_parser_type_id (parser);
20772 /* Parse the optional ellipsis. */
20773 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20775 /* Consume the `...'. */
20776 cp_lexer_consume_token (parser->lexer);
20778 /* Turn the type into a pack expansion expression. */
20779 type = make_pack_expansion (type);
20781 /* Add it to the list. */
20782 types = add_exception_specifier (types, type, /*complain=*/1);
20783 /* Peek at the next token. */
20784 token = cp_lexer_peek_token (parser->lexer);
20785 /* If it is not a `,', we are done. */
20786 if (token->type != CPP_COMMA)
20787 break;
20788 /* Consume the `,'. */
20789 cp_lexer_consume_token (parser->lexer);
20792 return nreverse (types);
20795 /* Parse a try-block.
20797 try-block:
20798 try compound-statement handler-seq */
20800 static tree
20801 cp_parser_try_block (cp_parser* parser)
20803 tree try_block;
20805 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
20806 try_block = begin_try_block ();
20807 cp_parser_compound_statement (parser, NULL, true, false);
20808 finish_try_block (try_block);
20809 cp_parser_handler_seq (parser);
20810 finish_handler_sequence (try_block);
20812 return try_block;
20815 /* Parse a function-try-block.
20817 function-try-block:
20818 try ctor-initializer [opt] function-body handler-seq */
20820 static bool
20821 cp_parser_function_try_block (cp_parser* parser)
20823 tree compound_stmt;
20824 tree try_block;
20825 bool ctor_initializer_p;
20827 /* Look for the `try' keyword. */
20828 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
20829 return false;
20830 /* Let the rest of the front end know where we are. */
20831 try_block = begin_function_try_block (&compound_stmt);
20832 /* Parse the function-body. */
20833 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
20834 (parser, /*in_function_try_block=*/true);
20835 /* We're done with the `try' part. */
20836 finish_function_try_block (try_block);
20837 /* Parse the handlers. */
20838 cp_parser_handler_seq (parser);
20839 /* We're done with the handlers. */
20840 finish_function_handler_sequence (try_block, compound_stmt);
20842 return ctor_initializer_p;
20845 /* Parse a handler-seq.
20847 handler-seq:
20848 handler handler-seq [opt] */
20850 static void
20851 cp_parser_handler_seq (cp_parser* parser)
20853 while (true)
20855 cp_token *token;
20857 /* Parse the handler. */
20858 cp_parser_handler (parser);
20859 /* Peek at the next token. */
20860 token = cp_lexer_peek_token (parser->lexer);
20861 /* If it's not `catch' then there are no more handlers. */
20862 if (!cp_parser_is_keyword (token, RID_CATCH))
20863 break;
20867 /* Parse a handler.
20869 handler:
20870 catch ( exception-declaration ) compound-statement */
20872 static void
20873 cp_parser_handler (cp_parser* parser)
20875 tree handler;
20876 tree declaration;
20878 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
20879 handler = begin_handler ();
20880 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20881 declaration = cp_parser_exception_declaration (parser);
20882 finish_handler_parms (declaration, handler);
20883 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20884 cp_parser_compound_statement (parser, NULL, false, false);
20885 finish_handler (handler);
20888 /* Parse an exception-declaration.
20890 exception-declaration:
20891 type-specifier-seq declarator
20892 type-specifier-seq abstract-declarator
20893 type-specifier-seq
20896 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20897 ellipsis variant is used. */
20899 static tree
20900 cp_parser_exception_declaration (cp_parser* parser)
20902 cp_decl_specifier_seq type_specifiers;
20903 cp_declarator *declarator;
20904 const char *saved_message;
20906 /* If it's an ellipsis, it's easy to handle. */
20907 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20909 /* Consume the `...' token. */
20910 cp_lexer_consume_token (parser->lexer);
20911 return NULL_TREE;
20914 /* Types may not be defined in exception-declarations. */
20915 saved_message = parser->type_definition_forbidden_message;
20916 parser->type_definition_forbidden_message
20917 = G_("types may not be defined in exception-declarations");
20919 /* Parse the type-specifier-seq. */
20920 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
20921 /*is_trailing_return=*/false,
20922 &type_specifiers);
20923 /* If it's a `)', then there is no declarator. */
20924 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
20925 declarator = NULL;
20926 else
20927 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
20928 /*ctor_dtor_or_conv_p=*/NULL,
20929 /*parenthesized_p=*/NULL,
20930 /*member_p=*/false);
20932 /* Restore the saved message. */
20933 parser->type_definition_forbidden_message = saved_message;
20935 if (!type_specifiers.any_specifiers_p)
20936 return error_mark_node;
20938 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
20941 /* Parse a throw-expression.
20943 throw-expression:
20944 throw assignment-expression [opt]
20946 Returns a THROW_EXPR representing the throw-expression. */
20948 static tree
20949 cp_parser_throw_expression (cp_parser* parser)
20951 tree expression;
20952 cp_token* token;
20954 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
20955 token = cp_lexer_peek_token (parser->lexer);
20956 /* Figure out whether or not there is an assignment-expression
20957 following the "throw" keyword. */
20958 if (token->type == CPP_COMMA
20959 || token->type == CPP_SEMICOLON
20960 || token->type == CPP_CLOSE_PAREN
20961 || token->type == CPP_CLOSE_SQUARE
20962 || token->type == CPP_CLOSE_BRACE
20963 || token->type == CPP_COLON)
20964 expression = NULL_TREE;
20965 else
20966 expression = cp_parser_assignment_expression (parser,
20967 /*cast_p=*/false, NULL);
20969 return build_throw (expression);
20972 /* GNU Extensions */
20974 /* Parse an (optional) asm-specification.
20976 asm-specification:
20977 asm ( string-literal )
20979 If the asm-specification is present, returns a STRING_CST
20980 corresponding to the string-literal. Otherwise, returns
20981 NULL_TREE. */
20983 static tree
20984 cp_parser_asm_specification_opt (cp_parser* parser)
20986 cp_token *token;
20987 tree asm_specification;
20989 /* Peek at the next token. */
20990 token = cp_lexer_peek_token (parser->lexer);
20991 /* If the next token isn't the `asm' keyword, then there's no
20992 asm-specification. */
20993 if (!cp_parser_is_keyword (token, RID_ASM))
20994 return NULL_TREE;
20996 /* Consume the `asm' token. */
20997 cp_lexer_consume_token (parser->lexer);
20998 /* Look for the `('. */
20999 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21001 /* Look for the string-literal. */
21002 asm_specification = cp_parser_string_literal (parser, false, false);
21004 /* Look for the `)'. */
21005 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21007 return asm_specification;
21010 /* Parse an asm-operand-list.
21012 asm-operand-list:
21013 asm-operand
21014 asm-operand-list , asm-operand
21016 asm-operand:
21017 string-literal ( expression )
21018 [ string-literal ] string-literal ( expression )
21020 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21021 each node is the expression. The TREE_PURPOSE is itself a
21022 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21023 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21024 is a STRING_CST for the string literal before the parenthesis. Returns
21025 ERROR_MARK_NODE if any of the operands are invalid. */
21027 static tree
21028 cp_parser_asm_operand_list (cp_parser* parser)
21030 tree asm_operands = NULL_TREE;
21031 bool invalid_operands = false;
21033 while (true)
21035 tree string_literal;
21036 tree expression;
21037 tree name;
21039 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21041 /* Consume the `[' token. */
21042 cp_lexer_consume_token (parser->lexer);
21043 /* Read the operand name. */
21044 name = cp_parser_identifier (parser);
21045 if (name != error_mark_node)
21046 name = build_string (IDENTIFIER_LENGTH (name),
21047 IDENTIFIER_POINTER (name));
21048 /* Look for the closing `]'. */
21049 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21051 else
21052 name = NULL_TREE;
21053 /* Look for the string-literal. */
21054 string_literal = cp_parser_string_literal (parser, false, false);
21056 /* Look for the `('. */
21057 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21058 /* Parse the expression. */
21059 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
21060 /* Look for the `)'. */
21061 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21063 if (name == error_mark_node
21064 || string_literal == error_mark_node
21065 || expression == error_mark_node)
21066 invalid_operands = true;
21068 /* Add this operand to the list. */
21069 asm_operands = tree_cons (build_tree_list (name, string_literal),
21070 expression,
21071 asm_operands);
21072 /* If the next token is not a `,', there are no more
21073 operands. */
21074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21075 break;
21076 /* Consume the `,'. */
21077 cp_lexer_consume_token (parser->lexer);
21080 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21083 /* Parse an asm-clobber-list.
21085 asm-clobber-list:
21086 string-literal
21087 asm-clobber-list , string-literal
21089 Returns a TREE_LIST, indicating the clobbers in the order that they
21090 appeared. The TREE_VALUE of each node is a STRING_CST. */
21092 static tree
21093 cp_parser_asm_clobber_list (cp_parser* parser)
21095 tree clobbers = NULL_TREE;
21097 while (true)
21099 tree string_literal;
21101 /* Look for the string literal. */
21102 string_literal = cp_parser_string_literal (parser, false, false);
21103 /* Add it to the list. */
21104 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21105 /* If the next token is not a `,', then the list is
21106 complete. */
21107 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21108 break;
21109 /* Consume the `,' token. */
21110 cp_lexer_consume_token (parser->lexer);
21113 return clobbers;
21116 /* Parse an asm-label-list.
21118 asm-label-list:
21119 identifier
21120 asm-label-list , identifier
21122 Returns a TREE_LIST, indicating the labels in the order that they
21123 appeared. The TREE_VALUE of each node is a label. */
21125 static tree
21126 cp_parser_asm_label_list (cp_parser* parser)
21128 tree labels = NULL_TREE;
21130 while (true)
21132 tree identifier, label, name;
21134 /* Look for the identifier. */
21135 identifier = cp_parser_identifier (parser);
21136 if (!error_operand_p (identifier))
21138 label = lookup_label (identifier);
21139 if (TREE_CODE (label) == LABEL_DECL)
21141 TREE_USED (label) = 1;
21142 check_goto (label);
21143 name = build_string (IDENTIFIER_LENGTH (identifier),
21144 IDENTIFIER_POINTER (identifier));
21145 labels = tree_cons (name, label, labels);
21148 /* If the next token is not a `,', then the list is
21149 complete. */
21150 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21151 break;
21152 /* Consume the `,' token. */
21153 cp_lexer_consume_token (parser->lexer);
21156 return nreverse (labels);
21159 /* Return TRUE iff the next tokens in the stream are possibly the
21160 beginning of a GNU extension attribute. */
21162 static bool
21163 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21165 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21168 /* Return TRUE iff the next tokens in the stream are possibly the
21169 beginning of a standard C++-11 attribute specifier. */
21171 static bool
21172 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21174 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21177 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21178 beginning of a standard C++-11 attribute specifier. */
21180 static bool
21181 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21183 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21185 return (cxx_dialect >= cxx11
21186 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21187 || (token->type == CPP_OPEN_SQUARE
21188 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21189 && token->type == CPP_OPEN_SQUARE)));
21192 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21193 beginning of a GNU extension attribute. */
21195 static bool
21196 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21198 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21200 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21203 /* Return true iff the next tokens can be the beginning of either a
21204 GNU attribute list, or a standard C++11 attribute sequence. */
21206 static bool
21207 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21209 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21210 || cp_next_tokens_can_be_std_attribute_p (parser));
21213 /* Return true iff the next Nth tokens can be the beginning of either
21214 a GNU attribute list, or a standard C++11 attribute sequence. */
21216 static bool
21217 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21219 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21220 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21223 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21224 of GNU attributes, or return NULL. */
21226 static tree
21227 cp_parser_attributes_opt (cp_parser *parser)
21229 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21230 return cp_parser_gnu_attributes_opt (parser);
21231 return cp_parser_std_attribute_spec_seq (parser);
21234 /* Parse an (optional) series of attributes.
21236 attributes:
21237 attributes attribute
21239 attribute:
21240 __attribute__ (( attribute-list [opt] ))
21242 The return value is as for cp_parser_gnu_attribute_list. */
21244 static tree
21245 cp_parser_gnu_attributes_opt (cp_parser* parser)
21247 tree attributes = NULL_TREE;
21249 while (true)
21251 cp_token *token;
21252 tree attribute_list;
21253 bool ok = true;
21255 /* Peek at the next token. */
21256 token = cp_lexer_peek_token (parser->lexer);
21257 /* If it's not `__attribute__', then we're done. */
21258 if (token->keyword != RID_ATTRIBUTE)
21259 break;
21261 /* Consume the `__attribute__' keyword. */
21262 cp_lexer_consume_token (parser->lexer);
21263 /* Look for the two `(' tokens. */
21264 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21265 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21267 /* Peek at the next token. */
21268 token = cp_lexer_peek_token (parser->lexer);
21269 if (token->type != CPP_CLOSE_PAREN)
21270 /* Parse the attribute-list. */
21271 attribute_list = cp_parser_gnu_attribute_list (parser);
21272 else
21273 /* If the next token is a `)', then there is no attribute
21274 list. */
21275 attribute_list = NULL;
21277 /* Look for the two `)' tokens. */
21278 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21279 ok = false;
21280 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21281 ok = false;
21282 if (!ok)
21283 cp_parser_skip_to_end_of_statement (parser);
21285 /* Add these new attributes to the list. */
21286 attributes = chainon (attributes, attribute_list);
21289 return attributes;
21292 /* Parse a GNU attribute-list.
21294 attribute-list:
21295 attribute
21296 attribute-list , attribute
21298 attribute:
21299 identifier
21300 identifier ( identifier )
21301 identifier ( identifier , expression-list )
21302 identifier ( expression-list )
21304 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21305 to an attribute. The TREE_PURPOSE of each node is the identifier
21306 indicating which attribute is in use. The TREE_VALUE represents
21307 the arguments, if any. */
21309 static tree
21310 cp_parser_gnu_attribute_list (cp_parser* parser)
21312 tree attribute_list = NULL_TREE;
21313 bool save_translate_strings_p = parser->translate_strings_p;
21315 parser->translate_strings_p = false;
21316 while (true)
21318 cp_token *token;
21319 tree identifier;
21320 tree attribute;
21322 /* Look for the identifier. We also allow keywords here; for
21323 example `__attribute__ ((const))' is legal. */
21324 token = cp_lexer_peek_token (parser->lexer);
21325 if (token->type == CPP_NAME
21326 || token->type == CPP_KEYWORD)
21328 tree arguments = NULL_TREE;
21330 /* Consume the token. */
21331 token = cp_lexer_consume_token (parser->lexer);
21333 /* Save away the identifier that indicates which attribute
21334 this is. */
21335 identifier = (token->type == CPP_KEYWORD)
21336 /* For keywords, use the canonical spelling, not the
21337 parsed identifier. */
21338 ? ridpointers[(int) token->keyword]
21339 : token->u.value;
21341 attribute = build_tree_list (identifier, NULL_TREE);
21343 /* Peek at the next token. */
21344 token = cp_lexer_peek_token (parser->lexer);
21345 /* If it's an `(', then parse the attribute arguments. */
21346 if (token->type == CPP_OPEN_PAREN)
21348 vec<tree, va_gc> *vec;
21349 int attr_flag = (attribute_takes_identifier_p (identifier)
21350 ? id_attr : normal_attr);
21351 vec = cp_parser_parenthesized_expression_list
21352 (parser, attr_flag, /*cast_p=*/false,
21353 /*allow_expansion_p=*/false,
21354 /*non_constant_p=*/NULL);
21355 if (vec == NULL)
21356 arguments = error_mark_node;
21357 else
21359 arguments = build_tree_list_vec (vec);
21360 release_tree_vector (vec);
21362 /* Save the arguments away. */
21363 TREE_VALUE (attribute) = arguments;
21366 if (arguments != error_mark_node)
21368 /* Add this attribute to the list. */
21369 TREE_CHAIN (attribute) = attribute_list;
21370 attribute_list = attribute;
21373 token = cp_lexer_peek_token (parser->lexer);
21375 /* Now, look for more attributes. If the next token isn't a
21376 `,', we're done. */
21377 if (token->type != CPP_COMMA)
21378 break;
21380 /* Consume the comma and keep going. */
21381 cp_lexer_consume_token (parser->lexer);
21383 parser->translate_strings_p = save_translate_strings_p;
21385 /* We built up the list in reverse order. */
21386 return nreverse (attribute_list);
21389 /* Parse a standard C++11 attribute.
21391 The returned representation is a TREE_LIST which TREE_PURPOSE is
21392 the scoped name of the attribute, and the TREE_VALUE is its
21393 arguments list.
21395 Note that the scoped name of the attribute is itself a TREE_LIST
21396 which TREE_PURPOSE is the namespace of the attribute, and
21397 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
21398 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21399 and which TREE_PURPOSE is directly the attribute name.
21401 Clients of the attribute code should use get_attribute_namespace
21402 and get_attribute_name to get the actual namespace and name of
21403 attributes, regardless of their being GNU or C++11 attributes.
21405 attribute:
21406 attribute-token attribute-argument-clause [opt]
21408 attribute-token:
21409 identifier
21410 attribute-scoped-token
21412 attribute-scoped-token:
21413 attribute-namespace :: identifier
21415 attribute-namespace:
21416 identifier
21418 attribute-argument-clause:
21419 ( balanced-token-seq )
21421 balanced-token-seq:
21422 balanced-token [opt]
21423 balanced-token-seq balanced-token
21425 balanced-token:
21426 ( balanced-token-seq )
21427 [ balanced-token-seq ]
21428 { balanced-token-seq }. */
21430 static tree
21431 cp_parser_std_attribute (cp_parser *parser)
21433 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
21434 cp_token *token;
21436 /* First, parse name of the the attribute, a.k.a
21437 attribute-token. */
21439 token = cp_lexer_peek_token (parser->lexer);
21440 if (token->type == CPP_NAME)
21441 attr_id = token->u.value;
21442 else if (token->type == CPP_KEYWORD)
21443 attr_id = ridpointers[(int) token->keyword];
21444 else if (token->flags & NAMED_OP)
21445 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
21447 if (attr_id == NULL_TREE)
21448 return NULL_TREE;
21450 cp_lexer_consume_token (parser->lexer);
21452 token = cp_lexer_peek_token (parser->lexer);
21453 if (token->type == CPP_SCOPE)
21455 /* We are seeing a scoped attribute token. */
21457 cp_lexer_consume_token (parser->lexer);
21458 attr_ns = attr_id;
21460 token = cp_lexer_consume_token (parser->lexer);
21461 if (token->type == CPP_NAME)
21462 attr_id = token->u.value;
21463 else if (token->type == CPP_KEYWORD)
21464 attr_id = ridpointers[(int) token->keyword];
21465 else
21467 error_at (token->location,
21468 "expected an identifier for the attribute name");
21469 return error_mark_node;
21471 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
21472 NULL_TREE);
21473 token = cp_lexer_peek_token (parser->lexer);
21475 else
21477 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
21478 NULL_TREE);
21479 /* C++11 noreturn attribute is equivalent to GNU's. */
21480 if (is_attribute_p ("noreturn", attr_id))
21481 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21482 /* C++14 deprecated attribute is equivalent to GNU's. */
21483 else if (cxx_dialect >= cxx1y && is_attribute_p ("deprecated", attr_id))
21484 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21487 /* Now parse the optional argument clause of the attribute. */
21489 if (token->type != CPP_OPEN_PAREN)
21490 return attribute;
21493 vec<tree, va_gc> *vec;
21494 int attr_flag = normal_attr;
21496 if (attr_ns == get_identifier ("gnu")
21497 && attribute_takes_identifier_p (attr_id))
21498 /* A GNU attribute that takes an identifier in parameter. */
21499 attr_flag = id_attr;
21501 vec = cp_parser_parenthesized_expression_list
21502 (parser, attr_flag, /*cast_p=*/false,
21503 /*allow_expansion_p=*/true,
21504 /*non_constant_p=*/NULL);
21505 if (vec == NULL)
21506 arguments = error_mark_node;
21507 else
21509 arguments = build_tree_list_vec (vec);
21510 release_tree_vector (vec);
21513 if (arguments == error_mark_node)
21514 attribute = error_mark_node;
21515 else
21516 TREE_VALUE (attribute) = arguments;
21519 return attribute;
21522 /* Parse a list of standard C++-11 attributes.
21524 attribute-list:
21525 attribute [opt]
21526 attribute-list , attribute[opt]
21527 attribute ...
21528 attribute-list , attribute ...
21531 static tree
21532 cp_parser_std_attribute_list (cp_parser *parser)
21534 tree attributes = NULL_TREE, attribute = NULL_TREE;
21535 cp_token *token = NULL;
21537 while (true)
21539 attribute = cp_parser_std_attribute (parser);
21540 if (attribute == error_mark_node)
21541 break;
21542 if (attribute != NULL_TREE)
21544 TREE_CHAIN (attribute) = attributes;
21545 attributes = attribute;
21547 token = cp_lexer_peek_token (parser->lexer);
21548 if (token->type != CPP_COMMA)
21549 break;
21550 cp_lexer_consume_token (parser->lexer);
21552 attributes = nreverse (attributes);
21553 return attributes;
21556 /* Parse a standard C++-11 attribute specifier.
21558 attribute-specifier:
21559 [ [ attribute-list ] ]
21560 alignment-specifier
21562 alignment-specifier:
21563 alignas ( type-id ... [opt] )
21564 alignas ( alignment-expression ... [opt] ). */
21566 static tree
21567 cp_parser_std_attribute_spec (cp_parser *parser)
21569 tree attributes = NULL_TREE;
21570 cp_token *token = cp_lexer_peek_token (parser->lexer);
21572 if (token->type == CPP_OPEN_SQUARE
21573 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
21575 cp_lexer_consume_token (parser->lexer);
21576 cp_lexer_consume_token (parser->lexer);
21578 attributes = cp_parser_std_attribute_list (parser);
21580 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
21581 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21582 cp_parser_skip_to_end_of_statement (parser);
21583 else
21584 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21585 when we are sure that we have actually parsed them. */
21586 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21588 else
21590 tree alignas_expr;
21592 /* Look for an alignment-specifier. */
21594 token = cp_lexer_peek_token (parser->lexer);
21596 if (token->type != CPP_KEYWORD
21597 || token->keyword != RID_ALIGNAS)
21598 return NULL_TREE;
21600 cp_lexer_consume_token (parser->lexer);
21601 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21603 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
21605 cp_parser_error (parser, "expected %<(%>");
21606 return error_mark_node;
21609 cp_parser_parse_tentatively (parser);
21610 alignas_expr = cp_parser_type_id (parser);
21612 if (!cp_parser_parse_definitely (parser))
21614 gcc_assert (alignas_expr == error_mark_node
21615 || alignas_expr == NULL_TREE);
21617 alignas_expr =
21618 cp_parser_assignment_expression (parser, /*cast_p=*/false,
21619 /**cp_id_kind=*/NULL);
21620 if (alignas_expr == error_mark_node)
21621 cp_parser_skip_to_end_of_statement (parser);
21622 if (alignas_expr == NULL_TREE
21623 || alignas_expr == error_mark_node)
21624 return alignas_expr;
21627 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
21629 cp_parser_error (parser, "expected %<)%>");
21630 return error_mark_node;
21633 alignas_expr = cxx_alignas_expr (alignas_expr);
21635 /* Build the C++-11 representation of an 'aligned'
21636 attribute. */
21637 attributes =
21638 build_tree_list (build_tree_list (get_identifier ("gnu"),
21639 get_identifier ("aligned")),
21640 build_tree_list (NULL_TREE, alignas_expr));
21643 return attributes;
21646 /* Parse a standard C++-11 attribute-specifier-seq.
21648 attribute-specifier-seq:
21649 attribute-specifier-seq [opt] attribute-specifier
21652 static tree
21653 cp_parser_std_attribute_spec_seq (cp_parser *parser)
21655 tree attr_specs = NULL;
21657 while (true)
21659 tree attr_spec = cp_parser_std_attribute_spec (parser);
21660 if (attr_spec == NULL_TREE)
21661 break;
21662 if (attr_spec == error_mark_node)
21663 return error_mark_node;
21665 TREE_CHAIN (attr_spec) = attr_specs;
21666 attr_specs = attr_spec;
21669 attr_specs = nreverse (attr_specs);
21670 return attr_specs;
21673 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
21674 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
21675 current value of the PEDANTIC flag, regardless of whether or not
21676 the `__extension__' keyword is present. The caller is responsible
21677 for restoring the value of the PEDANTIC flag. */
21679 static bool
21680 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
21682 /* Save the old value of the PEDANTIC flag. */
21683 *saved_pedantic = pedantic;
21685 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
21687 /* Consume the `__extension__' token. */
21688 cp_lexer_consume_token (parser->lexer);
21689 /* We're not being pedantic while the `__extension__' keyword is
21690 in effect. */
21691 pedantic = 0;
21693 return true;
21696 return false;
21699 /* Parse a label declaration.
21701 label-declaration:
21702 __label__ label-declarator-seq ;
21704 label-declarator-seq:
21705 identifier , label-declarator-seq
21706 identifier */
21708 static void
21709 cp_parser_label_declaration (cp_parser* parser)
21711 /* Look for the `__label__' keyword. */
21712 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
21714 while (true)
21716 tree identifier;
21718 /* Look for an identifier. */
21719 identifier = cp_parser_identifier (parser);
21720 /* If we failed, stop. */
21721 if (identifier == error_mark_node)
21722 break;
21723 /* Declare it as a label. */
21724 finish_label_decl (identifier);
21725 /* If the next token is a `;', stop. */
21726 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21727 break;
21728 /* Look for the `,' separating the label declarations. */
21729 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
21732 /* Look for the final `;'. */
21733 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21736 /* Support Functions */
21738 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
21739 NAME should have one of the representations used for an
21740 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
21741 is returned. If PARSER->SCOPE is a dependent type, then a
21742 SCOPE_REF is returned.
21744 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
21745 returned; the name was already resolved when the TEMPLATE_ID_EXPR
21746 was formed. Abstractly, such entities should not be passed to this
21747 function, because they do not need to be looked up, but it is
21748 simpler to check for this special case here, rather than at the
21749 call-sites.
21751 In cases not explicitly covered above, this function returns a
21752 DECL, OVERLOAD, or baselink representing the result of the lookup.
21753 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
21754 is returned.
21756 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
21757 (e.g., "struct") that was used. In that case bindings that do not
21758 refer to types are ignored.
21760 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
21761 ignored.
21763 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
21764 are ignored.
21766 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
21767 types.
21769 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
21770 TREE_LIST of candidates if name-lookup results in an ambiguity, and
21771 NULL_TREE otherwise. */
21773 static tree
21774 cp_parser_lookup_name (cp_parser *parser, tree name,
21775 enum tag_types tag_type,
21776 bool is_template,
21777 bool is_namespace,
21778 bool check_dependency,
21779 tree *ambiguous_decls,
21780 location_t name_location)
21782 tree decl;
21783 tree object_type = parser->context->object_type;
21785 /* Assume that the lookup will be unambiguous. */
21786 if (ambiguous_decls)
21787 *ambiguous_decls = NULL_TREE;
21789 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
21790 no longer valid. Note that if we are parsing tentatively, and
21791 the parse fails, OBJECT_TYPE will be automatically restored. */
21792 parser->context->object_type = NULL_TREE;
21794 if (name == error_mark_node)
21795 return error_mark_node;
21797 /* A template-id has already been resolved; there is no lookup to
21798 do. */
21799 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
21800 return name;
21801 if (BASELINK_P (name))
21803 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
21804 == TEMPLATE_ID_EXPR);
21805 return name;
21808 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
21809 it should already have been checked to make sure that the name
21810 used matches the type being destroyed. */
21811 if (TREE_CODE (name) == BIT_NOT_EXPR)
21813 tree type;
21815 /* Figure out to which type this destructor applies. */
21816 if (parser->scope)
21817 type = parser->scope;
21818 else if (object_type)
21819 type = object_type;
21820 else
21821 type = current_class_type;
21822 /* If that's not a class type, there is no destructor. */
21823 if (!type || !CLASS_TYPE_P (type))
21824 return error_mark_node;
21825 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
21826 lazily_declare_fn (sfk_destructor, type);
21827 if (!CLASSTYPE_DESTRUCTORS (type))
21828 return error_mark_node;
21829 /* If it was a class type, return the destructor. */
21830 return CLASSTYPE_DESTRUCTORS (type);
21833 /* By this point, the NAME should be an ordinary identifier. If
21834 the id-expression was a qualified name, the qualifying scope is
21835 stored in PARSER->SCOPE at this point. */
21836 gcc_assert (identifier_p (name));
21838 /* Perform the lookup. */
21839 if (parser->scope)
21841 bool dependent_p;
21843 if (parser->scope == error_mark_node)
21844 return error_mark_node;
21846 /* If the SCOPE is dependent, the lookup must be deferred until
21847 the template is instantiated -- unless we are explicitly
21848 looking up names in uninstantiated templates. Even then, we
21849 cannot look up the name if the scope is not a class type; it
21850 might, for example, be a template type parameter. */
21851 dependent_p = (TYPE_P (parser->scope)
21852 && dependent_scope_p (parser->scope));
21853 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
21854 && dependent_p)
21855 /* Defer lookup. */
21856 decl = error_mark_node;
21857 else
21859 tree pushed_scope = NULL_TREE;
21861 /* If PARSER->SCOPE is a dependent type, then it must be a
21862 class type, and we must not be checking dependencies;
21863 otherwise, we would have processed this lookup above. So
21864 that PARSER->SCOPE is not considered a dependent base by
21865 lookup_member, we must enter the scope here. */
21866 if (dependent_p)
21867 pushed_scope = push_scope (parser->scope);
21869 /* If the PARSER->SCOPE is a template specialization, it
21870 may be instantiated during name lookup. In that case,
21871 errors may be issued. Even if we rollback the current
21872 tentative parse, those errors are valid. */
21873 decl = lookup_qualified_name (parser->scope, name,
21874 tag_type != none_type,
21875 /*complain=*/true);
21877 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21878 lookup result and the nested-name-specifier nominates a class C:
21879 * if the name specified after the nested-name-specifier, when
21880 looked up in C, is the injected-class-name of C (Clause 9), or
21881 * if the name specified after the nested-name-specifier is the
21882 same as the identifier or the simple-template-id's template-
21883 name in the last component of the nested-name-specifier,
21884 the name is instead considered to name the constructor of
21885 class C. [ Note: for example, the constructor is not an
21886 acceptable lookup result in an elaborated-type-specifier so
21887 the constructor would not be used in place of the
21888 injected-class-name. --end note ] Such a constructor name
21889 shall be used only in the declarator-id of a declaration that
21890 names a constructor or in a using-declaration. */
21891 if (tag_type == none_type
21892 && DECL_SELF_REFERENCE_P (decl)
21893 && same_type_p (DECL_CONTEXT (decl), parser->scope))
21894 decl = lookup_qualified_name (parser->scope, ctor_identifier,
21895 tag_type != none_type,
21896 /*complain=*/true);
21898 /* If we have a single function from a using decl, pull it out. */
21899 if (TREE_CODE (decl) == OVERLOAD
21900 && !really_overloaded_fn (decl))
21901 decl = OVL_FUNCTION (decl);
21903 if (pushed_scope)
21904 pop_scope (pushed_scope);
21907 /* If the scope is a dependent type and either we deferred lookup or
21908 we did lookup but didn't find the name, rememeber the name. */
21909 if (decl == error_mark_node && TYPE_P (parser->scope)
21910 && dependent_type_p (parser->scope))
21912 if (tag_type)
21914 tree type;
21916 /* The resolution to Core Issue 180 says that `struct
21917 A::B' should be considered a type-name, even if `A'
21918 is dependent. */
21919 type = make_typename_type (parser->scope, name, tag_type,
21920 /*complain=*/tf_error);
21921 if (type != error_mark_node)
21922 decl = TYPE_NAME (type);
21924 else if (is_template
21925 && (cp_parser_next_token_ends_template_argument_p (parser)
21926 || cp_lexer_next_token_is (parser->lexer,
21927 CPP_CLOSE_PAREN)))
21928 decl = make_unbound_class_template (parser->scope,
21929 name, NULL_TREE,
21930 /*complain=*/tf_error);
21931 else
21932 decl = build_qualified_name (/*type=*/NULL_TREE,
21933 parser->scope, name,
21934 is_template);
21936 parser->qualifying_scope = parser->scope;
21937 parser->object_scope = NULL_TREE;
21939 else if (object_type)
21941 /* Look up the name in the scope of the OBJECT_TYPE, unless the
21942 OBJECT_TYPE is not a class. */
21943 if (CLASS_TYPE_P (object_type))
21944 /* If the OBJECT_TYPE is a template specialization, it may
21945 be instantiated during name lookup. In that case, errors
21946 may be issued. Even if we rollback the current tentative
21947 parse, those errors are valid. */
21948 decl = lookup_member (object_type,
21949 name,
21950 /*protect=*/0,
21951 tag_type != none_type,
21952 tf_warning_or_error);
21953 else
21954 decl = NULL_TREE;
21956 if (!decl)
21957 /* Look it up in the enclosing context. */
21958 decl = lookup_name_real (name, tag_type != none_type,
21959 /*nonclass=*/0,
21960 /*block_p=*/true, is_namespace, 0);
21961 parser->object_scope = object_type;
21962 parser->qualifying_scope = NULL_TREE;
21964 else
21966 decl = lookup_name_real (name, tag_type != none_type,
21967 /*nonclass=*/0,
21968 /*block_p=*/true, is_namespace, 0);
21969 parser->qualifying_scope = NULL_TREE;
21970 parser->object_scope = NULL_TREE;
21973 /* If the lookup failed, let our caller know. */
21974 if (!decl || decl == error_mark_node)
21975 return error_mark_node;
21977 /* Pull out the template from an injected-class-name (or multiple). */
21978 if (is_template)
21979 decl = maybe_get_template_decl_from_type_decl (decl);
21981 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
21982 if (TREE_CODE (decl) == TREE_LIST)
21984 if (ambiguous_decls)
21985 *ambiguous_decls = decl;
21986 /* The error message we have to print is too complicated for
21987 cp_parser_error, so we incorporate its actions directly. */
21988 if (!cp_parser_simulate_error (parser))
21990 error_at (name_location, "reference to %qD is ambiguous",
21991 name);
21992 print_candidates (decl);
21994 return error_mark_node;
21997 gcc_assert (DECL_P (decl)
21998 || TREE_CODE (decl) == OVERLOAD
21999 || TREE_CODE (decl) == SCOPE_REF
22000 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22001 || BASELINK_P (decl));
22003 /* If we have resolved the name of a member declaration, check to
22004 see if the declaration is accessible. When the name resolves to
22005 set of overloaded functions, accessibility is checked when
22006 overload resolution is done.
22008 During an explicit instantiation, access is not checked at all,
22009 as per [temp.explicit]. */
22010 if (DECL_P (decl))
22011 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22013 maybe_record_typedef_use (decl);
22015 return decl;
22018 /* Like cp_parser_lookup_name, but for use in the typical case where
22019 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22020 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22022 static tree
22023 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22025 return cp_parser_lookup_name (parser, name,
22026 none_type,
22027 /*is_template=*/false,
22028 /*is_namespace=*/false,
22029 /*check_dependency=*/true,
22030 /*ambiguous_decls=*/NULL,
22031 location);
22034 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22035 the current context, return the TYPE_DECL. If TAG_NAME_P is
22036 true, the DECL indicates the class being defined in a class-head,
22037 or declared in an elaborated-type-specifier.
22039 Otherwise, return DECL. */
22041 static tree
22042 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22044 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22045 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22047 struct A {
22048 template <typename T> struct B;
22051 template <typename T> struct A::B {};
22053 Similarly, in an elaborated-type-specifier:
22055 namespace N { struct X{}; }
22057 struct A {
22058 template <typename T> friend struct N::X;
22061 However, if the DECL refers to a class type, and we are in
22062 the scope of the class, then the name lookup automatically
22063 finds the TYPE_DECL created by build_self_reference rather
22064 than a TEMPLATE_DECL. For example, in:
22066 template <class T> struct S {
22067 S s;
22070 there is no need to handle such case. */
22072 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22073 return DECL_TEMPLATE_RESULT (decl);
22075 return decl;
22078 /* If too many, or too few, template-parameter lists apply to the
22079 declarator, issue an error message. Returns TRUE if all went well,
22080 and FALSE otherwise. */
22082 static bool
22083 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22084 cp_declarator *declarator,
22085 location_t declarator_location)
22087 switch (declarator->kind)
22089 case cdk_id:
22091 unsigned num_templates = 0;
22092 tree scope = declarator->u.id.qualifying_scope;
22094 if (scope)
22095 num_templates = num_template_headers_for_class (scope);
22096 else if (TREE_CODE (declarator->u.id.unqualified_name)
22097 == TEMPLATE_ID_EXPR)
22098 /* If the DECLARATOR has the form `X<y>' then it uses one
22099 additional level of template parameters. */
22100 ++num_templates;
22102 return cp_parser_check_template_parameters
22103 (parser, num_templates, declarator_location, declarator);
22106 case cdk_function:
22107 case cdk_array:
22108 case cdk_pointer:
22109 case cdk_reference:
22110 case cdk_ptrmem:
22111 return (cp_parser_check_declarator_template_parameters
22112 (parser, declarator->declarator, declarator_location));
22114 case cdk_error:
22115 return true;
22117 default:
22118 gcc_unreachable ();
22120 return false;
22123 /* NUM_TEMPLATES were used in the current declaration. If that is
22124 invalid, return FALSE and issue an error messages. Otherwise,
22125 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22126 declarator and we can print more accurate diagnostics. */
22128 static bool
22129 cp_parser_check_template_parameters (cp_parser* parser,
22130 unsigned num_templates,
22131 location_t location,
22132 cp_declarator *declarator)
22134 /* If there are the same number of template classes and parameter
22135 lists, that's OK. */
22136 if (parser->num_template_parameter_lists == num_templates)
22137 return true;
22138 /* If there are more, but only one more, then we are referring to a
22139 member template. That's OK too. */
22140 if (parser->num_template_parameter_lists == num_templates + 1)
22141 return true;
22142 /* If there are more template classes than parameter lists, we have
22143 something like:
22145 template <class T> void S<T>::R<T>::f (); */
22146 if (parser->num_template_parameter_lists < num_templates)
22148 if (declarator && !current_function_decl)
22149 error_at (location, "specializing member %<%T::%E%> "
22150 "requires %<template<>%> syntax",
22151 declarator->u.id.qualifying_scope,
22152 declarator->u.id.unqualified_name);
22153 else if (declarator)
22154 error_at (location, "invalid declaration of %<%T::%E%>",
22155 declarator->u.id.qualifying_scope,
22156 declarator->u.id.unqualified_name);
22157 else
22158 error_at (location, "too few template-parameter-lists");
22159 return false;
22161 /* Otherwise, there are too many template parameter lists. We have
22162 something like:
22164 template <class T> template <class U> void S::f(); */
22165 error_at (location, "too many template-parameter-lists");
22166 return false;
22169 /* Parse an optional `::' token indicating that the following name is
22170 from the global namespace. If so, PARSER->SCOPE is set to the
22171 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22172 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22173 Returns the new value of PARSER->SCOPE, if the `::' token is
22174 present, and NULL_TREE otherwise. */
22176 static tree
22177 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22179 cp_token *token;
22181 /* Peek at the next token. */
22182 token = cp_lexer_peek_token (parser->lexer);
22183 /* If we're looking at a `::' token then we're starting from the
22184 global namespace, not our current location. */
22185 if (token->type == CPP_SCOPE)
22187 /* Consume the `::' token. */
22188 cp_lexer_consume_token (parser->lexer);
22189 /* Set the SCOPE so that we know where to start the lookup. */
22190 parser->scope = global_namespace;
22191 parser->qualifying_scope = global_namespace;
22192 parser->object_scope = NULL_TREE;
22194 return parser->scope;
22196 else if (!current_scope_valid_p)
22198 parser->scope = NULL_TREE;
22199 parser->qualifying_scope = NULL_TREE;
22200 parser->object_scope = NULL_TREE;
22203 return NULL_TREE;
22206 /* Returns TRUE if the upcoming token sequence is the start of a
22207 constructor declarator. If FRIEND_P is true, the declarator is
22208 preceded by the `friend' specifier. */
22210 static bool
22211 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22213 bool constructor_p;
22214 tree nested_name_specifier;
22215 cp_token *next_token;
22217 /* The common case is that this is not a constructor declarator, so
22218 try to avoid doing lots of work if at all possible. It's not
22219 valid declare a constructor at function scope. */
22220 if (parser->in_function_body)
22221 return false;
22222 /* And only certain tokens can begin a constructor declarator. */
22223 next_token = cp_lexer_peek_token (parser->lexer);
22224 if (next_token->type != CPP_NAME
22225 && next_token->type != CPP_SCOPE
22226 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22227 && next_token->type != CPP_TEMPLATE_ID)
22228 return false;
22230 /* Parse tentatively; we are going to roll back all of the tokens
22231 consumed here. */
22232 cp_parser_parse_tentatively (parser);
22233 /* Assume that we are looking at a constructor declarator. */
22234 constructor_p = true;
22236 /* Look for the optional `::' operator. */
22237 cp_parser_global_scope_opt (parser,
22238 /*current_scope_valid_p=*/false);
22239 /* Look for the nested-name-specifier. */
22240 nested_name_specifier
22241 = (cp_parser_nested_name_specifier_opt (parser,
22242 /*typename_keyword_p=*/false,
22243 /*check_dependency_p=*/false,
22244 /*type_p=*/false,
22245 /*is_declaration=*/false));
22246 /* Outside of a class-specifier, there must be a
22247 nested-name-specifier. */
22248 if (!nested_name_specifier &&
22249 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
22250 || friend_p))
22251 constructor_p = false;
22252 else if (nested_name_specifier == error_mark_node)
22253 constructor_p = false;
22255 /* If we have a class scope, this is easy; DR 147 says that S::S always
22256 names the constructor, and no other qualified name could. */
22257 if (constructor_p && nested_name_specifier
22258 && CLASS_TYPE_P (nested_name_specifier))
22260 tree id = cp_parser_unqualified_id (parser,
22261 /*template_keyword_p=*/false,
22262 /*check_dependency_p=*/false,
22263 /*declarator_p=*/true,
22264 /*optional_p=*/false);
22265 if (is_overloaded_fn (id))
22266 id = DECL_NAME (get_first_fn (id));
22267 if (!constructor_name_p (id, nested_name_specifier))
22268 constructor_p = false;
22270 /* If we still think that this might be a constructor-declarator,
22271 look for a class-name. */
22272 else if (constructor_p)
22274 /* If we have:
22276 template <typename T> struct S {
22277 S();
22280 we must recognize that the nested `S' names a class. */
22281 tree type_decl;
22282 type_decl = cp_parser_class_name (parser,
22283 /*typename_keyword_p=*/false,
22284 /*template_keyword_p=*/false,
22285 none_type,
22286 /*check_dependency_p=*/false,
22287 /*class_head_p=*/false,
22288 /*is_declaration=*/false);
22289 /* If there was no class-name, then this is not a constructor. */
22290 constructor_p = !cp_parser_error_occurred (parser);
22292 /* If we're still considering a constructor, we have to see a `(',
22293 to begin the parameter-declaration-clause, followed by either a
22294 `)', an `...', or a decl-specifier. We need to check for a
22295 type-specifier to avoid being fooled into thinking that:
22297 S (f) (int);
22299 is a constructor. (It is actually a function named `f' that
22300 takes one parameter (of type `int') and returns a value of type
22301 `S'. */
22302 if (constructor_p
22303 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22304 constructor_p = false;
22306 if (constructor_p
22307 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
22308 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
22309 /* A parameter declaration begins with a decl-specifier,
22310 which is either the "attribute" keyword, a storage class
22311 specifier, or (usually) a type-specifier. */
22312 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
22314 tree type;
22315 tree pushed_scope = NULL_TREE;
22316 unsigned saved_num_template_parameter_lists;
22318 /* Names appearing in the type-specifier should be looked up
22319 in the scope of the class. */
22320 if (current_class_type)
22321 type = NULL_TREE;
22322 else
22324 type = TREE_TYPE (type_decl);
22325 if (TREE_CODE (type) == TYPENAME_TYPE)
22327 type = resolve_typename_type (type,
22328 /*only_current_p=*/false);
22329 if (TREE_CODE (type) == TYPENAME_TYPE)
22331 cp_parser_abort_tentative_parse (parser);
22332 return false;
22335 pushed_scope = push_scope (type);
22338 /* Inside the constructor parameter list, surrounding
22339 template-parameter-lists do not apply. */
22340 saved_num_template_parameter_lists
22341 = parser->num_template_parameter_lists;
22342 parser->num_template_parameter_lists = 0;
22344 /* Look for the type-specifier. */
22345 cp_parser_type_specifier (parser,
22346 CP_PARSER_FLAGS_NONE,
22347 /*decl_specs=*/NULL,
22348 /*is_declarator=*/true,
22349 /*declares_class_or_enum=*/NULL,
22350 /*is_cv_qualifier=*/NULL);
22352 parser->num_template_parameter_lists
22353 = saved_num_template_parameter_lists;
22355 /* Leave the scope of the class. */
22356 if (pushed_scope)
22357 pop_scope (pushed_scope);
22359 constructor_p = !cp_parser_error_occurred (parser);
22363 /* We did not really want to consume any tokens. */
22364 cp_parser_abort_tentative_parse (parser);
22366 return constructor_p;
22369 /* Parse the definition of the function given by the DECL_SPECIFIERS,
22370 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
22371 they must be performed once we are in the scope of the function.
22373 Returns the function defined. */
22375 static tree
22376 cp_parser_function_definition_from_specifiers_and_declarator
22377 (cp_parser* parser,
22378 cp_decl_specifier_seq *decl_specifiers,
22379 tree attributes,
22380 const cp_declarator *declarator)
22382 tree fn;
22383 bool success_p;
22385 /* Begin the function-definition. */
22386 success_p = start_function (decl_specifiers, declarator, attributes);
22388 /* The things we're about to see are not directly qualified by any
22389 template headers we've seen thus far. */
22390 reset_specialization ();
22392 /* If there were names looked up in the decl-specifier-seq that we
22393 did not check, check them now. We must wait until we are in the
22394 scope of the function to perform the checks, since the function
22395 might be a friend. */
22396 perform_deferred_access_checks (tf_warning_or_error);
22398 if (success_p)
22400 cp_finalize_omp_declare_simd (parser, current_function_decl);
22401 parser->omp_declare_simd = NULL;
22404 if (!success_p)
22406 /* Skip the entire function. */
22407 cp_parser_skip_to_end_of_block_or_statement (parser);
22408 fn = error_mark_node;
22410 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
22412 /* Seen already, skip it. An error message has already been output. */
22413 cp_parser_skip_to_end_of_block_or_statement (parser);
22414 fn = current_function_decl;
22415 current_function_decl = NULL_TREE;
22416 /* If this is a function from a class, pop the nested class. */
22417 if (current_class_name)
22418 pop_nested_class ();
22420 else
22422 timevar_id_t tv;
22423 if (DECL_DECLARED_INLINE_P (current_function_decl))
22424 tv = TV_PARSE_INLINE;
22425 else
22426 tv = TV_PARSE_FUNC;
22427 timevar_push (tv);
22428 fn = cp_parser_function_definition_after_declarator (parser,
22429 /*inline_p=*/false);
22430 timevar_pop (tv);
22433 return fn;
22436 /* Parse the part of a function-definition that follows the
22437 declarator. INLINE_P is TRUE iff this function is an inline
22438 function defined within a class-specifier.
22440 Returns the function defined. */
22442 static tree
22443 cp_parser_function_definition_after_declarator (cp_parser* parser,
22444 bool inline_p)
22446 tree fn;
22447 bool ctor_initializer_p = false;
22448 bool saved_in_unbraced_linkage_specification_p;
22449 bool saved_in_function_body;
22450 unsigned saved_num_template_parameter_lists;
22451 cp_token *token;
22453 saved_in_function_body = parser->in_function_body;
22454 parser->in_function_body = true;
22455 /* If the next token is `return', then the code may be trying to
22456 make use of the "named return value" extension that G++ used to
22457 support. */
22458 token = cp_lexer_peek_token (parser->lexer);
22459 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
22461 /* Consume the `return' keyword. */
22462 cp_lexer_consume_token (parser->lexer);
22463 /* Look for the identifier that indicates what value is to be
22464 returned. */
22465 cp_parser_identifier (parser);
22466 /* Issue an error message. */
22467 error_at (token->location,
22468 "named return values are no longer supported");
22469 /* Skip tokens until we reach the start of the function body. */
22470 while (true)
22472 cp_token *token = cp_lexer_peek_token (parser->lexer);
22473 if (token->type == CPP_OPEN_BRACE
22474 || token->type == CPP_EOF
22475 || token->type == CPP_PRAGMA_EOL)
22476 break;
22477 cp_lexer_consume_token (parser->lexer);
22480 /* The `extern' in `extern "C" void f () { ... }' does not apply to
22481 anything declared inside `f'. */
22482 saved_in_unbraced_linkage_specification_p
22483 = parser->in_unbraced_linkage_specification_p;
22484 parser->in_unbraced_linkage_specification_p = false;
22485 /* Inside the function, surrounding template-parameter-lists do not
22486 apply. */
22487 saved_num_template_parameter_lists
22488 = parser->num_template_parameter_lists;
22489 parser->num_template_parameter_lists = 0;
22491 start_lambda_scope (current_function_decl);
22493 /* If the next token is `try', `__transaction_atomic', or
22494 `__transaction_relaxed`, then we are looking at either function-try-block
22495 or function-transaction-block. Note that all of these include the
22496 function-body. */
22497 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
22498 ctor_initializer_p = cp_parser_function_transaction (parser,
22499 RID_TRANSACTION_ATOMIC);
22500 else if (cp_lexer_next_token_is_keyword (parser->lexer,
22501 RID_TRANSACTION_RELAXED))
22502 ctor_initializer_p = cp_parser_function_transaction (parser,
22503 RID_TRANSACTION_RELAXED);
22504 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
22505 ctor_initializer_p = cp_parser_function_try_block (parser);
22506 else
22507 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22508 (parser, /*in_function_try_block=*/false);
22510 finish_lambda_scope ();
22512 /* Finish the function. */
22513 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
22514 (inline_p ? 2 : 0));
22515 /* Generate code for it, if necessary. */
22516 expand_or_defer_fn (fn);
22517 /* Restore the saved values. */
22518 parser->in_unbraced_linkage_specification_p
22519 = saved_in_unbraced_linkage_specification_p;
22520 parser->num_template_parameter_lists
22521 = saved_num_template_parameter_lists;
22522 parser->in_function_body = saved_in_function_body;
22524 if (parser->fully_implicit_function_template_p)
22525 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22527 return fn;
22530 /* Parse a template-declaration, assuming that the `export' (and
22531 `extern') keywords, if present, has already been scanned. MEMBER_P
22532 is as for cp_parser_template_declaration. */
22534 static void
22535 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
22537 tree decl = NULL_TREE;
22538 vec<deferred_access_check, va_gc> *checks;
22539 tree parameter_list;
22540 bool friend_p = false;
22541 bool need_lang_pop;
22542 cp_token *token;
22544 /* Look for the `template' keyword. */
22545 token = cp_lexer_peek_token (parser->lexer);
22546 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
22547 return;
22549 /* And the `<'. */
22550 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
22551 return;
22552 if (at_class_scope_p () && current_function_decl)
22554 /* 14.5.2.2 [temp.mem]
22556 A local class shall not have member templates. */
22557 error_at (token->location,
22558 "invalid declaration of member template in local class");
22559 cp_parser_skip_to_end_of_block_or_statement (parser);
22560 return;
22562 /* [temp]
22564 A template ... shall not have C linkage. */
22565 if (current_lang_name == lang_name_c)
22567 error_at (token->location, "template with C linkage");
22568 /* Give it C++ linkage to avoid confusing other parts of the
22569 front end. */
22570 push_lang_context (lang_name_cplusplus);
22571 need_lang_pop = true;
22573 else
22574 need_lang_pop = false;
22576 /* We cannot perform access checks on the template parameter
22577 declarations until we know what is being declared, just as we
22578 cannot check the decl-specifier list. */
22579 push_deferring_access_checks (dk_deferred);
22581 /* If the next token is `>', then we have an invalid
22582 specialization. Rather than complain about an invalid template
22583 parameter, issue an error message here. */
22584 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
22586 cp_parser_error (parser, "invalid explicit specialization");
22587 begin_specialization ();
22588 parameter_list = NULL_TREE;
22590 else
22592 /* Parse the template parameters. */
22593 parameter_list = cp_parser_template_parameter_list (parser);
22596 /* Get the deferred access checks from the parameter list. These
22597 will be checked once we know what is being declared, as for a
22598 member template the checks must be performed in the scope of the
22599 class containing the member. */
22600 checks = get_deferred_access_checks ();
22602 /* Look for the `>'. */
22603 cp_parser_skip_to_end_of_template_parameter_list (parser);
22604 /* We just processed one more parameter list. */
22605 ++parser->num_template_parameter_lists;
22606 /* If the next token is `template', there are more template
22607 parameters. */
22608 if (cp_lexer_next_token_is_keyword (parser->lexer,
22609 RID_TEMPLATE))
22610 cp_parser_template_declaration_after_export (parser, member_p);
22611 else if (cxx_dialect >= cxx11
22612 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22613 decl = cp_parser_alias_declaration (parser);
22614 else
22616 /* There are no access checks when parsing a template, as we do not
22617 know if a specialization will be a friend. */
22618 push_deferring_access_checks (dk_no_check);
22619 token = cp_lexer_peek_token (parser->lexer);
22620 decl = cp_parser_single_declaration (parser,
22621 checks,
22622 member_p,
22623 /*explicit_specialization_p=*/false,
22624 &friend_p);
22625 pop_deferring_access_checks ();
22627 /* If this is a member template declaration, let the front
22628 end know. */
22629 if (member_p && !friend_p && decl)
22631 if (TREE_CODE (decl) == TYPE_DECL)
22632 cp_parser_check_access_in_redeclaration (decl, token->location);
22634 decl = finish_member_template_decl (decl);
22636 else if (friend_p && decl
22637 && DECL_DECLARES_TYPE_P (decl))
22638 make_friend_class (current_class_type, TREE_TYPE (decl),
22639 /*complain=*/true);
22641 /* We are done with the current parameter list. */
22642 --parser->num_template_parameter_lists;
22644 pop_deferring_access_checks ();
22646 /* Finish up. */
22647 finish_template_decl (parameter_list);
22649 /* Check the template arguments for a literal operator template. */
22650 if (decl
22651 && DECL_DECLARES_FUNCTION_P (decl)
22652 && UDLIT_OPER_P (DECL_NAME (decl)))
22654 bool ok = true;
22655 if (parameter_list == NULL_TREE)
22656 ok = false;
22657 else
22659 int num_parms = TREE_VEC_LENGTH (parameter_list);
22660 if (num_parms == 1)
22662 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
22663 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
22664 if (TREE_TYPE (parm) != char_type_node
22665 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
22666 ok = false;
22668 else if (num_parms == 2 && cxx_dialect >= cxx1y)
22670 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
22671 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
22672 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
22673 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
22674 if (TREE_TYPE (parm) != TREE_TYPE (type)
22675 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
22676 ok = false;
22678 else
22679 ok = false;
22681 if (!ok)
22682 error ("literal operator template %qD has invalid parameter list."
22683 " Expected non-type template argument pack <char...>"
22684 " or <typename CharT, CharT...>",
22685 decl);
22687 /* Register member declarations. */
22688 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
22689 finish_member_declaration (decl);
22690 /* For the erroneous case of a template with C linkage, we pushed an
22691 implicit C++ linkage scope; exit that scope now. */
22692 if (need_lang_pop)
22693 pop_lang_context ();
22694 /* If DECL is a function template, we must return to parse it later.
22695 (Even though there is no definition, there might be default
22696 arguments that need handling.) */
22697 if (member_p && decl
22698 && DECL_DECLARES_FUNCTION_P (decl))
22699 vec_safe_push (unparsed_funs_with_definitions, decl);
22702 /* Perform the deferred access checks from a template-parameter-list.
22703 CHECKS is a TREE_LIST of access checks, as returned by
22704 get_deferred_access_checks. */
22706 static void
22707 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
22709 ++processing_template_parmlist;
22710 perform_access_checks (checks, tf_warning_or_error);
22711 --processing_template_parmlist;
22714 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
22715 `function-definition' sequence that follows a template header.
22716 If MEMBER_P is true, this declaration appears in a class scope.
22718 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
22719 *FRIEND_P is set to TRUE iff the declaration is a friend. */
22721 static tree
22722 cp_parser_single_declaration (cp_parser* parser,
22723 vec<deferred_access_check, va_gc> *checks,
22724 bool member_p,
22725 bool explicit_specialization_p,
22726 bool* friend_p)
22728 int declares_class_or_enum;
22729 tree decl = NULL_TREE;
22730 cp_decl_specifier_seq decl_specifiers;
22731 bool function_definition_p = false;
22732 cp_token *decl_spec_token_start;
22734 /* This function is only used when processing a template
22735 declaration. */
22736 gcc_assert (innermost_scope_kind () == sk_template_parms
22737 || innermost_scope_kind () == sk_template_spec);
22739 /* Defer access checks until we know what is being declared. */
22740 push_deferring_access_checks (dk_deferred);
22742 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
22743 alternative. */
22744 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22745 cp_parser_decl_specifier_seq (parser,
22746 CP_PARSER_FLAGS_OPTIONAL,
22747 &decl_specifiers,
22748 &declares_class_or_enum);
22749 if (friend_p)
22750 *friend_p = cp_parser_friend_p (&decl_specifiers);
22752 /* There are no template typedefs. */
22753 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
22755 error_at (decl_spec_token_start->location,
22756 "template declaration of %<typedef%>");
22757 decl = error_mark_node;
22760 /* Gather up the access checks that occurred the
22761 decl-specifier-seq. */
22762 stop_deferring_access_checks ();
22764 /* Check for the declaration of a template class. */
22765 if (declares_class_or_enum)
22767 if (cp_parser_declares_only_class_p (parser))
22769 decl = shadow_tag (&decl_specifiers);
22771 /* In this case:
22773 struct C {
22774 friend template <typename T> struct A<T>::B;
22777 A<T>::B will be represented by a TYPENAME_TYPE, and
22778 therefore not recognized by shadow_tag. */
22779 if (friend_p && *friend_p
22780 && !decl
22781 && decl_specifiers.type
22782 && TYPE_P (decl_specifiers.type))
22783 decl = decl_specifiers.type;
22785 if (decl && decl != error_mark_node)
22786 decl = TYPE_NAME (decl);
22787 else
22788 decl = error_mark_node;
22790 /* Perform access checks for template parameters. */
22791 cp_parser_perform_template_parameter_access_checks (checks);
22795 /* Complain about missing 'typename' or other invalid type names. */
22796 if (!decl_specifiers.any_type_specifiers_p
22797 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22799 /* cp_parser_parse_and_diagnose_invalid_type_name calls
22800 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
22801 the rest of this declaration. */
22802 decl = error_mark_node;
22803 goto out;
22806 /* If it's not a template class, try for a template function. If
22807 the next token is a `;', then this declaration does not declare
22808 anything. But, if there were errors in the decl-specifiers, then
22809 the error might well have come from an attempted class-specifier.
22810 In that case, there's no need to warn about a missing declarator. */
22811 if (!decl
22812 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
22813 || decl_specifiers.type != error_mark_node))
22815 decl = cp_parser_init_declarator (parser,
22816 &decl_specifiers,
22817 checks,
22818 /*function_definition_allowed_p=*/true,
22819 member_p,
22820 declares_class_or_enum,
22821 &function_definition_p,
22822 NULL);
22824 /* 7.1.1-1 [dcl.stc]
22826 A storage-class-specifier shall not be specified in an explicit
22827 specialization... */
22828 if (decl
22829 && explicit_specialization_p
22830 && decl_specifiers.storage_class != sc_none)
22832 error_at (decl_spec_token_start->location,
22833 "explicit template specialization cannot have a storage class");
22834 decl = error_mark_node;
22837 if (decl && VAR_P (decl))
22838 check_template_variable (decl);
22841 /* Look for a trailing `;' after the declaration. */
22842 if (!function_definition_p
22843 && (decl == error_mark_node
22844 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
22845 cp_parser_skip_to_end_of_block_or_statement (parser);
22847 out:
22848 pop_deferring_access_checks ();
22850 /* Clear any current qualification; whatever comes next is the start
22851 of something new. */
22852 parser->scope = NULL_TREE;
22853 parser->qualifying_scope = NULL_TREE;
22854 parser->object_scope = NULL_TREE;
22856 return decl;
22859 /* Parse a cast-expression that is not the operand of a unary "&". */
22861 static tree
22862 cp_parser_simple_cast_expression (cp_parser *parser)
22864 return cp_parser_cast_expression (parser, /*address_p=*/false,
22865 /*cast_p=*/false, /*decltype*/false, NULL);
22868 /* Parse a functional cast to TYPE. Returns an expression
22869 representing the cast. */
22871 static tree
22872 cp_parser_functional_cast (cp_parser* parser, tree type)
22874 vec<tree, va_gc> *vec;
22875 tree expression_list;
22876 tree cast;
22877 bool nonconst_p;
22879 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22881 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22882 expression_list = cp_parser_braced_list (parser, &nonconst_p);
22883 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
22884 if (TREE_CODE (type) == TYPE_DECL)
22885 type = TREE_TYPE (type);
22886 return finish_compound_literal (type, expression_list,
22887 tf_warning_or_error);
22891 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22892 /*cast_p=*/true,
22893 /*allow_expansion_p=*/true,
22894 /*non_constant_p=*/NULL);
22895 if (vec == NULL)
22896 expression_list = error_mark_node;
22897 else
22899 expression_list = build_tree_list_vec (vec);
22900 release_tree_vector (vec);
22903 cast = build_functional_cast (type, expression_list,
22904 tf_warning_or_error);
22905 /* [expr.const]/1: In an integral constant expression "only type
22906 conversions to integral or enumeration type can be used". */
22907 if (TREE_CODE (type) == TYPE_DECL)
22908 type = TREE_TYPE (type);
22909 if (cast != error_mark_node
22910 && !cast_valid_in_integral_constant_expression_p (type)
22911 && cp_parser_non_integral_constant_expression (parser,
22912 NIC_CONSTRUCTOR))
22913 return error_mark_node;
22914 return cast;
22917 /* Save the tokens that make up the body of a member function defined
22918 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
22919 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
22920 specifiers applied to the declaration. Returns the FUNCTION_DECL
22921 for the member function. */
22923 static tree
22924 cp_parser_save_member_function_body (cp_parser* parser,
22925 cp_decl_specifier_seq *decl_specifiers,
22926 cp_declarator *declarator,
22927 tree attributes)
22929 cp_token *first;
22930 cp_token *last;
22931 tree fn;
22933 /* Create the FUNCTION_DECL. */
22934 fn = grokmethod (decl_specifiers, declarator, attributes);
22935 cp_finalize_omp_declare_simd (parser, fn);
22936 /* If something went badly wrong, bail out now. */
22937 if (fn == error_mark_node)
22939 /* If there's a function-body, skip it. */
22940 if (cp_parser_token_starts_function_definition_p
22941 (cp_lexer_peek_token (parser->lexer)))
22942 cp_parser_skip_to_end_of_block_or_statement (parser);
22943 return error_mark_node;
22946 /* Remember it, if there default args to post process. */
22947 cp_parser_save_default_args (parser, fn);
22949 /* Save away the tokens that make up the body of the
22950 function. */
22951 first = parser->lexer->next_token;
22952 /* Handle function try blocks. */
22953 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
22954 cp_lexer_consume_token (parser->lexer);
22955 /* We can have braced-init-list mem-initializers before the fn body. */
22956 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22958 cp_lexer_consume_token (parser->lexer);
22959 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22961 /* cache_group will stop after an un-nested { } pair, too. */
22962 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
22963 break;
22965 /* variadic mem-inits have ... after the ')'. */
22966 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22967 cp_lexer_consume_token (parser->lexer);
22970 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22971 /* Handle function try blocks. */
22972 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
22973 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22974 last = parser->lexer->next_token;
22976 /* Save away the inline definition; we will process it when the
22977 class is complete. */
22978 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
22979 DECL_PENDING_INLINE_P (fn) = 1;
22981 /* We need to know that this was defined in the class, so that
22982 friend templates are handled correctly. */
22983 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
22985 /* Add FN to the queue of functions to be parsed later. */
22986 vec_safe_push (unparsed_funs_with_definitions, fn);
22988 return fn;
22991 /* Save the tokens that make up the in-class initializer for a non-static
22992 data member. Returns a DEFAULT_ARG. */
22994 static tree
22995 cp_parser_save_nsdmi (cp_parser* parser)
22997 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23000 /* Parse a template-argument-list, as well as the trailing ">" (but
23001 not the opening "<"). See cp_parser_template_argument_list for the
23002 return value. */
23004 static tree
23005 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23007 tree arguments;
23008 tree saved_scope;
23009 tree saved_qualifying_scope;
23010 tree saved_object_scope;
23011 bool saved_greater_than_is_operator_p;
23012 int saved_unevaluated_operand;
23013 int saved_inhibit_evaluation_warnings;
23015 /* [temp.names]
23017 When parsing a template-id, the first non-nested `>' is taken as
23018 the end of the template-argument-list rather than a greater-than
23019 operator. */
23020 saved_greater_than_is_operator_p
23021 = parser->greater_than_is_operator_p;
23022 parser->greater_than_is_operator_p = false;
23023 /* Parsing the argument list may modify SCOPE, so we save it
23024 here. */
23025 saved_scope = parser->scope;
23026 saved_qualifying_scope = parser->qualifying_scope;
23027 saved_object_scope = parser->object_scope;
23028 /* We need to evaluate the template arguments, even though this
23029 template-id may be nested within a "sizeof". */
23030 saved_unevaluated_operand = cp_unevaluated_operand;
23031 cp_unevaluated_operand = 0;
23032 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23033 c_inhibit_evaluation_warnings = 0;
23034 /* Parse the template-argument-list itself. */
23035 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23036 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23037 arguments = NULL_TREE;
23038 else
23039 arguments = cp_parser_template_argument_list (parser);
23040 /* Look for the `>' that ends the template-argument-list. If we find
23041 a '>>' instead, it's probably just a typo. */
23042 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23044 if (cxx_dialect != cxx98)
23046 /* In C++0x, a `>>' in a template argument list or cast
23047 expression is considered to be two separate `>'
23048 tokens. So, change the current token to a `>', but don't
23049 consume it: it will be consumed later when the outer
23050 template argument list (or cast expression) is parsed.
23051 Note that this replacement of `>' for `>>' is necessary
23052 even if we are parsing tentatively: in the tentative
23053 case, after calling
23054 cp_parser_enclosed_template_argument_list we will always
23055 throw away all of the template arguments and the first
23056 closing `>', either because the template argument list
23057 was erroneous or because we are replacing those tokens
23058 with a CPP_TEMPLATE_ID token. The second `>' (which will
23059 not have been thrown away) is needed either to close an
23060 outer template argument list or to complete a new-style
23061 cast. */
23062 cp_token *token = cp_lexer_peek_token (parser->lexer);
23063 token->type = CPP_GREATER;
23065 else if (!saved_greater_than_is_operator_p)
23067 /* If we're in a nested template argument list, the '>>' has
23068 to be a typo for '> >'. We emit the error message, but we
23069 continue parsing and we push a '>' as next token, so that
23070 the argument list will be parsed correctly. Note that the
23071 global source location is still on the token before the
23072 '>>', so we need to say explicitly where we want it. */
23073 cp_token *token = cp_lexer_peek_token (parser->lexer);
23074 error_at (token->location, "%<>>%> should be %<> >%> "
23075 "within a nested template argument list");
23077 token->type = CPP_GREATER;
23079 else
23081 /* If this is not a nested template argument list, the '>>'
23082 is a typo for '>'. Emit an error message and continue.
23083 Same deal about the token location, but here we can get it
23084 right by consuming the '>>' before issuing the diagnostic. */
23085 cp_token *token = cp_lexer_consume_token (parser->lexer);
23086 error_at (token->location,
23087 "spurious %<>>%>, use %<>%> to terminate "
23088 "a template argument list");
23091 else
23092 cp_parser_skip_to_end_of_template_parameter_list (parser);
23093 /* The `>' token might be a greater-than operator again now. */
23094 parser->greater_than_is_operator_p
23095 = saved_greater_than_is_operator_p;
23096 /* Restore the SAVED_SCOPE. */
23097 parser->scope = saved_scope;
23098 parser->qualifying_scope = saved_qualifying_scope;
23099 parser->object_scope = saved_object_scope;
23100 cp_unevaluated_operand = saved_unevaluated_operand;
23101 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23103 return arguments;
23106 /* MEMBER_FUNCTION is a member function, or a friend. If default
23107 arguments, or the body of the function have not yet been parsed,
23108 parse them now. */
23110 static void
23111 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23113 timevar_push (TV_PARSE_INMETH);
23114 /* If this member is a template, get the underlying
23115 FUNCTION_DECL. */
23116 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23117 member_function = DECL_TEMPLATE_RESULT (member_function);
23119 /* There should not be any class definitions in progress at this
23120 point; the bodies of members are only parsed outside of all class
23121 definitions. */
23122 gcc_assert (parser->num_classes_being_defined == 0);
23123 /* While we're parsing the member functions we might encounter more
23124 classes. We want to handle them right away, but we don't want
23125 them getting mixed up with functions that are currently in the
23126 queue. */
23127 push_unparsed_function_queues (parser);
23129 /* Make sure that any template parameters are in scope. */
23130 maybe_begin_member_template_processing (member_function);
23132 /* If the body of the function has not yet been parsed, parse it
23133 now. */
23134 if (DECL_PENDING_INLINE_P (member_function))
23136 tree function_scope;
23137 cp_token_cache *tokens;
23139 /* The function is no longer pending; we are processing it. */
23140 tokens = DECL_PENDING_INLINE_INFO (member_function);
23141 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23142 DECL_PENDING_INLINE_P (member_function) = 0;
23144 /* If this is a local class, enter the scope of the containing
23145 function. */
23146 function_scope = current_function_decl;
23147 if (function_scope)
23148 push_function_context ();
23150 /* Push the body of the function onto the lexer stack. */
23151 cp_parser_push_lexer_for_tokens (parser, tokens);
23153 /* Let the front end know that we going to be defining this
23154 function. */
23155 start_preparsed_function (member_function, NULL_TREE,
23156 SF_PRE_PARSED | SF_INCLASS_INLINE);
23158 /* Don't do access checking if it is a templated function. */
23159 if (processing_template_decl)
23160 push_deferring_access_checks (dk_no_check);
23162 /* #pragma omp declare reduction needs special parsing. */
23163 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23165 parser->lexer->in_pragma = true;
23166 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23167 finish_function (0);
23168 cp_check_omp_declare_reduction (member_function);
23170 else
23171 /* Now, parse the body of the function. */
23172 cp_parser_function_definition_after_declarator (parser,
23173 /*inline_p=*/true);
23175 if (processing_template_decl)
23176 pop_deferring_access_checks ();
23178 /* Leave the scope of the containing function. */
23179 if (function_scope)
23180 pop_function_context ();
23181 cp_parser_pop_lexer (parser);
23184 /* Remove any template parameters from the symbol table. */
23185 maybe_end_member_template_processing ();
23187 /* Restore the queue. */
23188 pop_unparsed_function_queues (parser);
23189 timevar_pop (TV_PARSE_INMETH);
23192 /* If DECL contains any default args, remember it on the unparsed
23193 functions queue. */
23195 static void
23196 cp_parser_save_default_args (cp_parser* parser, tree decl)
23198 tree probe;
23200 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23201 probe;
23202 probe = TREE_CHAIN (probe))
23203 if (TREE_PURPOSE (probe))
23205 cp_default_arg_entry entry = {current_class_type, decl};
23206 vec_safe_push (unparsed_funs_with_default_args, entry);
23207 break;
23211 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23212 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23213 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23214 from the parameter-type-list. */
23216 static tree
23217 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23218 tree default_arg, tree parmtype)
23220 cp_token_cache *tokens;
23221 tree parsed_arg;
23222 bool dummy;
23224 if (default_arg == error_mark_node)
23225 return error_mark_node;
23227 /* Push the saved tokens for the default argument onto the parser's
23228 lexer stack. */
23229 tokens = DEFARG_TOKENS (default_arg);
23230 cp_parser_push_lexer_for_tokens (parser, tokens);
23232 start_lambda_scope (decl);
23234 /* Parse the default argument. */
23235 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
23236 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
23237 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23239 finish_lambda_scope ();
23241 if (parsed_arg == error_mark_node)
23242 cp_parser_skip_to_end_of_statement (parser);
23244 if (!processing_template_decl)
23246 /* In a non-template class, check conversions now. In a template,
23247 we'll wait and instantiate these as needed. */
23248 if (TREE_CODE (decl) == PARM_DECL)
23249 parsed_arg = check_default_argument (parmtype, parsed_arg,
23250 tf_warning_or_error);
23251 else
23253 int flags = LOOKUP_IMPLICIT;
23254 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
23255 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
23256 flags = LOOKUP_NORMAL;
23257 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
23258 if (TREE_CODE (parsed_arg) == TARGET_EXPR)
23259 /* This represents the whole initialization. */
23260 TARGET_EXPR_DIRECT_INIT_P (parsed_arg) = true;
23264 /* If the token stream has not been completely used up, then
23265 there was extra junk after the end of the default
23266 argument. */
23267 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23269 if (TREE_CODE (decl) == PARM_DECL)
23270 cp_parser_error (parser, "expected %<,%>");
23271 else
23272 cp_parser_error (parser, "expected %<;%>");
23275 /* Revert to the main lexer. */
23276 cp_parser_pop_lexer (parser);
23278 return parsed_arg;
23281 /* FIELD is a non-static data member with an initializer which we saved for
23282 later; parse it now. */
23284 static void
23285 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
23287 tree def;
23289 push_unparsed_function_queues (parser);
23290 def = cp_parser_late_parse_one_default_arg (parser, field,
23291 DECL_INITIAL (field),
23292 NULL_TREE);
23293 pop_unparsed_function_queues (parser);
23295 DECL_INITIAL (field) = def;
23298 /* FN is a FUNCTION_DECL which may contains a parameter with an
23299 unparsed DEFAULT_ARG. Parse the default args now. This function
23300 assumes that the current scope is the scope in which the default
23301 argument should be processed. */
23303 static void
23304 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
23306 bool saved_local_variables_forbidden_p;
23307 tree parm, parmdecl;
23309 /* While we're parsing the default args, we might (due to the
23310 statement expression extension) encounter more classes. We want
23311 to handle them right away, but we don't want them getting mixed
23312 up with default args that are currently in the queue. */
23313 push_unparsed_function_queues (parser);
23315 /* Local variable names (and the `this' keyword) may not appear
23316 in a default argument. */
23317 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23318 parser->local_variables_forbidden_p = true;
23320 push_defarg_context (fn);
23322 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
23323 parmdecl = DECL_ARGUMENTS (fn);
23324 parm && parm != void_list_node;
23325 parm = TREE_CHAIN (parm),
23326 parmdecl = DECL_CHAIN (parmdecl))
23328 tree default_arg = TREE_PURPOSE (parm);
23329 tree parsed_arg;
23330 vec<tree, va_gc> *insts;
23331 tree copy;
23332 unsigned ix;
23334 if (!default_arg)
23335 continue;
23337 if (TREE_CODE (default_arg) != DEFAULT_ARG)
23338 /* This can happen for a friend declaration for a function
23339 already declared with default arguments. */
23340 continue;
23342 parsed_arg
23343 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
23344 default_arg,
23345 TREE_VALUE (parm));
23346 if (parsed_arg == error_mark_node)
23348 continue;
23351 TREE_PURPOSE (parm) = parsed_arg;
23353 /* Update any instantiations we've already created. */
23354 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
23355 vec_safe_iterate (insts, ix, &copy); ix++)
23356 TREE_PURPOSE (copy) = parsed_arg;
23359 pop_defarg_context ();
23361 /* Make sure no default arg is missing. */
23362 check_default_args (fn);
23364 /* Restore the state of local_variables_forbidden_p. */
23365 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
23367 /* Restore the queue. */
23368 pop_unparsed_function_queues (parser);
23371 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
23373 sizeof ... ( identifier )
23375 where the 'sizeof' token has already been consumed. */
23377 static tree
23378 cp_parser_sizeof_pack (cp_parser *parser)
23380 /* Consume the `...'. */
23381 cp_lexer_consume_token (parser->lexer);
23382 maybe_warn_variadic_templates ();
23384 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
23385 if (paren)
23386 cp_lexer_consume_token (parser->lexer);
23387 else
23388 permerror (cp_lexer_peek_token (parser->lexer)->location,
23389 "%<sizeof...%> argument must be surrounded by parentheses");
23391 cp_token *token = cp_lexer_peek_token (parser->lexer);
23392 tree name = cp_parser_identifier (parser);
23393 if (name == error_mark_node)
23394 return error_mark_node;
23395 /* The name is not qualified. */
23396 parser->scope = NULL_TREE;
23397 parser->qualifying_scope = NULL_TREE;
23398 parser->object_scope = NULL_TREE;
23399 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
23400 if (expr == error_mark_node)
23401 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
23402 token->location);
23403 if (TREE_CODE (expr) == TYPE_DECL)
23404 expr = TREE_TYPE (expr);
23405 else if (TREE_CODE (expr) == CONST_DECL)
23406 expr = DECL_INITIAL (expr);
23407 expr = make_pack_expansion (expr);
23409 if (paren)
23410 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23412 return expr;
23415 /* Parse the operand of `sizeof' (or a similar operator). Returns
23416 either a TYPE or an expression, depending on the form of the
23417 input. The KEYWORD indicates which kind of expression we have
23418 encountered. */
23420 static tree
23421 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
23423 tree expr = NULL_TREE;
23424 const char *saved_message;
23425 char *tmp;
23426 bool saved_integral_constant_expression_p;
23427 bool saved_non_integral_constant_expression_p;
23429 /* If it's a `...', then we are computing the length of a parameter
23430 pack. */
23431 if (keyword == RID_SIZEOF
23432 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23433 return cp_parser_sizeof_pack (parser);
23435 /* Types cannot be defined in a `sizeof' expression. Save away the
23436 old message. */
23437 saved_message = parser->type_definition_forbidden_message;
23438 /* And create the new one. */
23439 tmp = concat ("types may not be defined in %<",
23440 IDENTIFIER_POINTER (ridpointers[keyword]),
23441 "%> expressions", NULL);
23442 parser->type_definition_forbidden_message = tmp;
23444 /* The restrictions on constant-expressions do not apply inside
23445 sizeof expressions. */
23446 saved_integral_constant_expression_p
23447 = parser->integral_constant_expression_p;
23448 saved_non_integral_constant_expression_p
23449 = parser->non_integral_constant_expression_p;
23450 parser->integral_constant_expression_p = false;
23452 /* Do not actually evaluate the expression. */
23453 ++cp_unevaluated_operand;
23454 ++c_inhibit_evaluation_warnings;
23455 /* If it's a `(', then we might be looking at the type-id
23456 construction. */
23457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23459 tree type = NULL_TREE;
23460 bool compound_literal_p;
23462 /* We can't be sure yet whether we're looking at a type-id or an
23463 expression. */
23464 cp_parser_parse_tentatively (parser);
23465 /* Consume the `('. */
23466 cp_lexer_consume_token (parser->lexer);
23467 /* Note: as a GNU Extension, compound literals are considered
23468 postfix-expressions as they are in C99, so they are valid
23469 arguments to sizeof. See comment in cp_parser_cast_expression
23470 for details. */
23471 cp_lexer_save_tokens (parser->lexer);
23472 /* Skip tokens until the next token is a closing parenthesis.
23473 If we find the closing `)', and the next token is a `{', then
23474 we are looking at a compound-literal. */
23475 compound_literal_p
23476 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
23477 /*consume_paren=*/true)
23478 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
23479 /* Roll back the tokens we skipped. */
23480 cp_lexer_rollback_tokens (parser->lexer);
23481 /* If we were looking at a compound-literal, simulate an error
23482 so that the call to cp_parser_parse_definitely below will
23483 fail. */
23484 if (compound_literal_p)
23485 cp_parser_simulate_error (parser);
23486 else
23488 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23489 parser->in_type_id_in_expr_p = true;
23490 /* Look for the type-id. */
23491 type = cp_parser_type_id (parser);
23492 /* Look for the closing `)'. */
23493 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23494 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23497 /* If all went well, then we're done. */
23498 if (cp_parser_parse_definitely (parser))
23500 cp_decl_specifier_seq decl_specs;
23502 /* Build a trivial decl-specifier-seq. */
23503 clear_decl_specs (&decl_specs);
23504 decl_specs.type = type;
23506 /* Call grokdeclarator to figure out what type this is. */
23507 expr = grokdeclarator (NULL,
23508 &decl_specs,
23509 TYPENAME,
23510 /*initialized=*/0,
23511 /*attrlist=*/NULL);
23515 /* If the type-id production did not work out, then we must be
23516 looking at the unary-expression production. */
23517 if (!expr)
23518 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
23519 /*cast_p=*/false, NULL);
23521 /* Go back to evaluating expressions. */
23522 --cp_unevaluated_operand;
23523 --c_inhibit_evaluation_warnings;
23525 /* Free the message we created. */
23526 free (tmp);
23527 /* And restore the old one. */
23528 parser->type_definition_forbidden_message = saved_message;
23529 parser->integral_constant_expression_p
23530 = saved_integral_constant_expression_p;
23531 parser->non_integral_constant_expression_p
23532 = saved_non_integral_constant_expression_p;
23534 return expr;
23537 /* If the current declaration has no declarator, return true. */
23539 static bool
23540 cp_parser_declares_only_class_p (cp_parser *parser)
23542 /* If the next token is a `;' or a `,' then there is no
23543 declarator. */
23544 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23545 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23548 /* Update the DECL_SPECS to reflect the storage class indicated by
23549 KEYWORD. */
23551 static void
23552 cp_parser_set_storage_class (cp_parser *parser,
23553 cp_decl_specifier_seq *decl_specs,
23554 enum rid keyword,
23555 cp_token *token)
23557 cp_storage_class storage_class;
23559 if (parser->in_unbraced_linkage_specification_p)
23561 error_at (token->location, "invalid use of %qD in linkage specification",
23562 ridpointers[keyword]);
23563 return;
23565 else if (decl_specs->storage_class != sc_none)
23567 decl_specs->conflicting_specifiers_p = true;
23568 return;
23571 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
23572 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
23573 && decl_specs->gnu_thread_keyword_p)
23575 pedwarn (decl_specs->locations[ds_thread], 0,
23576 "%<__thread%> before %qD", ridpointers[keyword]);
23579 switch (keyword)
23581 case RID_AUTO:
23582 storage_class = sc_auto;
23583 break;
23584 case RID_REGISTER:
23585 storage_class = sc_register;
23586 break;
23587 case RID_STATIC:
23588 storage_class = sc_static;
23589 break;
23590 case RID_EXTERN:
23591 storage_class = sc_extern;
23592 break;
23593 case RID_MUTABLE:
23594 storage_class = sc_mutable;
23595 break;
23596 default:
23597 gcc_unreachable ();
23599 decl_specs->storage_class = storage_class;
23600 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
23602 /* A storage class specifier cannot be applied alongside a typedef
23603 specifier. If there is a typedef specifier present then set
23604 conflicting_specifiers_p which will trigger an error later
23605 on in grokdeclarator. */
23606 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
23607 decl_specs->conflicting_specifiers_p = true;
23610 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
23611 is true, the type is a class or enum definition. */
23613 static void
23614 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
23615 tree type_spec,
23616 cp_token *token,
23617 bool type_definition_p)
23619 decl_specs->any_specifiers_p = true;
23621 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
23622 (with, for example, in "typedef int wchar_t;") we remember that
23623 this is what happened. In system headers, we ignore these
23624 declarations so that G++ can work with system headers that are not
23625 C++-safe. */
23626 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
23627 && !type_definition_p
23628 && (type_spec == boolean_type_node
23629 || type_spec == char16_type_node
23630 || type_spec == char32_type_node
23631 || type_spec == wchar_type_node)
23632 && (decl_specs->type
23633 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
23634 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
23635 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
23636 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
23638 decl_specs->redefined_builtin_type = type_spec;
23639 set_and_check_decl_spec_loc (decl_specs,
23640 ds_redefined_builtin_type_spec,
23641 token);
23642 if (!decl_specs->type)
23644 decl_specs->type = type_spec;
23645 decl_specs->type_definition_p = false;
23646 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
23649 else if (decl_specs->type)
23650 decl_specs->multiple_types_p = true;
23651 else
23653 decl_specs->type = type_spec;
23654 decl_specs->type_definition_p = type_definition_p;
23655 decl_specs->redefined_builtin_type = NULL_TREE;
23656 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
23660 /* True iff TOKEN is the GNU keyword __thread. */
23662 static bool
23663 token_is__thread (cp_token *token)
23665 gcc_assert (token->keyword == RID_THREAD);
23666 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
23669 /* Set the location for a declarator specifier and check if it is
23670 duplicated.
23672 DECL_SPECS is the sequence of declarator specifiers onto which to
23673 set the location.
23675 DS is the single declarator specifier to set which location is to
23676 be set onto the existing sequence of declarators.
23678 LOCATION is the location for the declarator specifier to
23679 consider. */
23681 static void
23682 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
23683 cp_decl_spec ds, cp_token *token)
23685 gcc_assert (ds < ds_last);
23687 if (decl_specs == NULL)
23688 return;
23690 source_location location = token->location;
23692 if (decl_specs->locations[ds] == 0)
23694 decl_specs->locations[ds] = location;
23695 if (ds == ds_thread)
23696 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
23698 else
23700 if (ds == ds_long)
23702 if (decl_specs->locations[ds_long_long] != 0)
23703 error_at (location,
23704 "%<long long long%> is too long for GCC");
23705 else
23707 decl_specs->locations[ds_long_long] = location;
23708 pedwarn_cxx98 (location,
23709 OPT_Wlong_long,
23710 "ISO C++ 1998 does not support %<long long%>");
23713 else if (ds == ds_thread)
23715 bool gnu = token_is__thread (token);
23716 if (gnu != decl_specs->gnu_thread_keyword_p)
23717 error_at (location,
23718 "both %<__thread%> and %<thread_local%> specified");
23719 else
23720 error_at (location, "duplicate %qD", token->u.value);
23722 else
23724 static const char *const decl_spec_names[] = {
23725 "signed",
23726 "unsigned",
23727 "short",
23728 "long",
23729 "const",
23730 "volatile",
23731 "restrict",
23732 "inline",
23733 "virtual",
23734 "explicit",
23735 "friend",
23736 "typedef",
23737 "using",
23738 "constexpr",
23739 "__complex"
23741 error_at (location,
23742 "duplicate %qs", decl_spec_names[ds]);
23747 /* Return true iff the declarator specifier DS is present in the
23748 sequence of declarator specifiers DECL_SPECS. */
23750 bool
23751 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
23752 cp_decl_spec ds)
23754 gcc_assert (ds < ds_last);
23756 if (decl_specs == NULL)
23757 return false;
23759 return decl_specs->locations[ds] != 0;
23762 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
23763 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
23765 static bool
23766 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
23768 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
23771 /* Issue an error message indicating that TOKEN_DESC was expected.
23772 If KEYWORD is true, it indicated this function is called by
23773 cp_parser_require_keword and the required token can only be
23774 a indicated keyword. */
23776 static void
23777 cp_parser_required_error (cp_parser *parser,
23778 required_token token_desc,
23779 bool keyword)
23781 switch (token_desc)
23783 case RT_NEW:
23784 cp_parser_error (parser, "expected %<new%>");
23785 return;
23786 case RT_DELETE:
23787 cp_parser_error (parser, "expected %<delete%>");
23788 return;
23789 case RT_RETURN:
23790 cp_parser_error (parser, "expected %<return%>");
23791 return;
23792 case RT_WHILE:
23793 cp_parser_error (parser, "expected %<while%>");
23794 return;
23795 case RT_EXTERN:
23796 cp_parser_error (parser, "expected %<extern%>");
23797 return;
23798 case RT_STATIC_ASSERT:
23799 cp_parser_error (parser, "expected %<static_assert%>");
23800 return;
23801 case RT_DECLTYPE:
23802 cp_parser_error (parser, "expected %<decltype%>");
23803 return;
23804 case RT_OPERATOR:
23805 cp_parser_error (parser, "expected %<operator%>");
23806 return;
23807 case RT_CLASS:
23808 cp_parser_error (parser, "expected %<class%>");
23809 return;
23810 case RT_TEMPLATE:
23811 cp_parser_error (parser, "expected %<template%>");
23812 return;
23813 case RT_NAMESPACE:
23814 cp_parser_error (parser, "expected %<namespace%>");
23815 return;
23816 case RT_USING:
23817 cp_parser_error (parser, "expected %<using%>");
23818 return;
23819 case RT_ASM:
23820 cp_parser_error (parser, "expected %<asm%>");
23821 return;
23822 case RT_TRY:
23823 cp_parser_error (parser, "expected %<try%>");
23824 return;
23825 case RT_CATCH:
23826 cp_parser_error (parser, "expected %<catch%>");
23827 return;
23828 case RT_THROW:
23829 cp_parser_error (parser, "expected %<throw%>");
23830 return;
23831 case RT_LABEL:
23832 cp_parser_error (parser, "expected %<__label__%>");
23833 return;
23834 case RT_AT_TRY:
23835 cp_parser_error (parser, "expected %<@try%>");
23836 return;
23837 case RT_AT_SYNCHRONIZED:
23838 cp_parser_error (parser, "expected %<@synchronized%>");
23839 return;
23840 case RT_AT_THROW:
23841 cp_parser_error (parser, "expected %<@throw%>");
23842 return;
23843 case RT_TRANSACTION_ATOMIC:
23844 cp_parser_error (parser, "expected %<__transaction_atomic%>");
23845 return;
23846 case RT_TRANSACTION_RELAXED:
23847 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
23848 return;
23849 default:
23850 break;
23852 if (!keyword)
23854 switch (token_desc)
23856 case RT_SEMICOLON:
23857 cp_parser_error (parser, "expected %<;%>");
23858 return;
23859 case RT_OPEN_PAREN:
23860 cp_parser_error (parser, "expected %<(%>");
23861 return;
23862 case RT_CLOSE_BRACE:
23863 cp_parser_error (parser, "expected %<}%>");
23864 return;
23865 case RT_OPEN_BRACE:
23866 cp_parser_error (parser, "expected %<{%>");
23867 return;
23868 case RT_CLOSE_SQUARE:
23869 cp_parser_error (parser, "expected %<]%>");
23870 return;
23871 case RT_OPEN_SQUARE:
23872 cp_parser_error (parser, "expected %<[%>");
23873 return;
23874 case RT_COMMA:
23875 cp_parser_error (parser, "expected %<,%>");
23876 return;
23877 case RT_SCOPE:
23878 cp_parser_error (parser, "expected %<::%>");
23879 return;
23880 case RT_LESS:
23881 cp_parser_error (parser, "expected %<<%>");
23882 return;
23883 case RT_GREATER:
23884 cp_parser_error (parser, "expected %<>%>");
23885 return;
23886 case RT_EQ:
23887 cp_parser_error (parser, "expected %<=%>");
23888 return;
23889 case RT_ELLIPSIS:
23890 cp_parser_error (parser, "expected %<...%>");
23891 return;
23892 case RT_MULT:
23893 cp_parser_error (parser, "expected %<*%>");
23894 return;
23895 case RT_COMPL:
23896 cp_parser_error (parser, "expected %<~%>");
23897 return;
23898 case RT_COLON:
23899 cp_parser_error (parser, "expected %<:%>");
23900 return;
23901 case RT_COLON_SCOPE:
23902 cp_parser_error (parser, "expected %<:%> or %<::%>");
23903 return;
23904 case RT_CLOSE_PAREN:
23905 cp_parser_error (parser, "expected %<)%>");
23906 return;
23907 case RT_COMMA_CLOSE_PAREN:
23908 cp_parser_error (parser, "expected %<,%> or %<)%>");
23909 return;
23910 case RT_PRAGMA_EOL:
23911 cp_parser_error (parser, "expected end of line");
23912 return;
23913 case RT_NAME:
23914 cp_parser_error (parser, "expected identifier");
23915 return;
23916 case RT_SELECT:
23917 cp_parser_error (parser, "expected selection-statement");
23918 return;
23919 case RT_INTERATION:
23920 cp_parser_error (parser, "expected iteration-statement");
23921 return;
23922 case RT_JUMP:
23923 cp_parser_error (parser, "expected jump-statement");
23924 return;
23925 case RT_CLASS_KEY:
23926 cp_parser_error (parser, "expected class-key");
23927 return;
23928 case RT_CLASS_TYPENAME_TEMPLATE:
23929 cp_parser_error (parser,
23930 "expected %<class%>, %<typename%>, or %<template%>");
23931 return;
23932 default:
23933 gcc_unreachable ();
23936 else
23937 gcc_unreachable ();
23942 /* If the next token is of the indicated TYPE, consume it. Otherwise,
23943 issue an error message indicating that TOKEN_DESC was expected.
23945 Returns the token consumed, if the token had the appropriate type.
23946 Otherwise, returns NULL. */
23948 static cp_token *
23949 cp_parser_require (cp_parser* parser,
23950 enum cpp_ttype type,
23951 required_token token_desc)
23953 if (cp_lexer_next_token_is (parser->lexer, type))
23954 return cp_lexer_consume_token (parser->lexer);
23955 else
23957 /* Output the MESSAGE -- unless we're parsing tentatively. */
23958 if (!cp_parser_simulate_error (parser))
23959 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
23960 return NULL;
23964 /* An error message is produced if the next token is not '>'.
23965 All further tokens are skipped until the desired token is
23966 found or '{', '}', ';' or an unbalanced ')' or ']'. */
23968 static void
23969 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
23971 /* Current level of '< ... >'. */
23972 unsigned level = 0;
23973 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
23974 unsigned nesting_depth = 0;
23976 /* Are we ready, yet? If not, issue error message. */
23977 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
23978 return;
23980 /* Skip tokens until the desired token is found. */
23981 while (true)
23983 /* Peek at the next token. */
23984 switch (cp_lexer_peek_token (parser->lexer)->type)
23986 case CPP_LESS:
23987 if (!nesting_depth)
23988 ++level;
23989 break;
23991 case CPP_RSHIFT:
23992 if (cxx_dialect == cxx98)
23993 /* C++0x views the `>>' operator as two `>' tokens, but
23994 C++98 does not. */
23995 break;
23996 else if (!nesting_depth && level-- == 0)
23998 /* We've hit a `>>' where the first `>' closes the
23999 template argument list, and the second `>' is
24000 spurious. Just consume the `>>' and stop; we've
24001 already produced at least one error. */
24002 cp_lexer_consume_token (parser->lexer);
24003 return;
24005 /* Fall through for C++0x, so we handle the second `>' in
24006 the `>>'. */
24008 case CPP_GREATER:
24009 if (!nesting_depth && level-- == 0)
24011 /* We've reached the token we want, consume it and stop. */
24012 cp_lexer_consume_token (parser->lexer);
24013 return;
24015 break;
24017 case CPP_OPEN_PAREN:
24018 case CPP_OPEN_SQUARE:
24019 ++nesting_depth;
24020 break;
24022 case CPP_CLOSE_PAREN:
24023 case CPP_CLOSE_SQUARE:
24024 if (nesting_depth-- == 0)
24025 return;
24026 break;
24028 case CPP_EOF:
24029 case CPP_PRAGMA_EOL:
24030 case CPP_SEMICOLON:
24031 case CPP_OPEN_BRACE:
24032 case CPP_CLOSE_BRACE:
24033 /* The '>' was probably forgotten, don't look further. */
24034 return;
24036 default:
24037 break;
24040 /* Consume this token. */
24041 cp_lexer_consume_token (parser->lexer);
24045 /* If the next token is the indicated keyword, consume it. Otherwise,
24046 issue an error message indicating that TOKEN_DESC was expected.
24048 Returns the token consumed, if the token had the appropriate type.
24049 Otherwise, returns NULL. */
24051 static cp_token *
24052 cp_parser_require_keyword (cp_parser* parser,
24053 enum rid keyword,
24054 required_token token_desc)
24056 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24058 if (token && token->keyword != keyword)
24060 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24061 return NULL;
24064 return token;
24067 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24068 function-definition. */
24070 static bool
24071 cp_parser_token_starts_function_definition_p (cp_token* token)
24073 return (/* An ordinary function-body begins with an `{'. */
24074 token->type == CPP_OPEN_BRACE
24075 /* A ctor-initializer begins with a `:'. */
24076 || token->type == CPP_COLON
24077 /* A function-try-block begins with `try'. */
24078 || token->keyword == RID_TRY
24079 /* A function-transaction-block begins with `__transaction_atomic'
24080 or `__transaction_relaxed'. */
24081 || token->keyword == RID_TRANSACTION_ATOMIC
24082 || token->keyword == RID_TRANSACTION_RELAXED
24083 /* The named return value extension begins with `return'. */
24084 || token->keyword == RID_RETURN);
24087 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24088 definition. */
24090 static bool
24091 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24093 cp_token *token;
24095 token = cp_lexer_peek_token (parser->lexer);
24096 return (token->type == CPP_OPEN_BRACE
24097 || (token->type == CPP_COLON
24098 && !parser->colon_doesnt_start_class_def_p));
24101 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24102 C++0x) ending a template-argument. */
24104 static bool
24105 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24107 cp_token *token;
24109 token = cp_lexer_peek_token (parser->lexer);
24110 return (token->type == CPP_COMMA
24111 || token->type == CPP_GREATER
24112 || token->type == CPP_ELLIPSIS
24113 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24116 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24117 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24119 static bool
24120 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24121 size_t n)
24123 cp_token *token;
24125 token = cp_lexer_peek_nth_token (parser->lexer, n);
24126 if (token->type == CPP_LESS)
24127 return true;
24128 /* Check for the sequence `<::' in the original code. It would be lexed as
24129 `[:', where `[' is a digraph, and there is no whitespace before
24130 `:'. */
24131 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24133 cp_token *token2;
24134 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24135 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24136 return true;
24138 return false;
24141 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24142 or none_type otherwise. */
24144 static enum tag_types
24145 cp_parser_token_is_class_key (cp_token* token)
24147 switch (token->keyword)
24149 case RID_CLASS:
24150 return class_type;
24151 case RID_STRUCT:
24152 return record_type;
24153 case RID_UNION:
24154 return union_type;
24156 default:
24157 return none_type;
24161 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24163 static void
24164 cp_parser_check_class_key (enum tag_types class_key, tree type)
24166 if (type == error_mark_node)
24167 return;
24168 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24170 if (permerror (input_location, "%qs tag used in naming %q#T",
24171 class_key == union_type ? "union"
24172 : class_key == record_type ? "struct" : "class",
24173 type))
24174 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24175 "%q#T was previously declared here", type);
24179 /* Issue an error message if DECL is redeclared with different
24180 access than its original declaration [class.access.spec/3].
24181 This applies to nested classes and nested class templates.
24182 [class.mem/1]. */
24184 static void
24185 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24187 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24188 return;
24190 if ((TREE_PRIVATE (decl)
24191 != (current_access_specifier == access_private_node))
24192 || (TREE_PROTECTED (decl)
24193 != (current_access_specifier == access_protected_node)))
24194 error_at (location, "%qD redeclared with different access", decl);
24197 /* Look for the `template' keyword, as a syntactic disambiguator.
24198 Return TRUE iff it is present, in which case it will be
24199 consumed. */
24201 static bool
24202 cp_parser_optional_template_keyword (cp_parser *parser)
24204 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24206 /* In C++98 the `template' keyword can only be used within templates;
24207 outside templates the parser can always figure out what is a
24208 template and what is not. In C++11, per the resolution of DR 468,
24209 `template' is allowed in cases where it is not strictly necessary. */
24210 if (!processing_template_decl
24211 && pedantic && cxx_dialect == cxx98)
24213 cp_token *token = cp_lexer_peek_token (parser->lexer);
24214 pedwarn (token->location, OPT_Wpedantic,
24215 "in C++98 %<template%> (as a disambiguator) is only "
24216 "allowed within templates");
24217 /* If this part of the token stream is rescanned, the same
24218 error message would be generated. So, we purge the token
24219 from the stream. */
24220 cp_lexer_purge_token (parser->lexer);
24221 return false;
24223 else
24225 /* Consume the `template' keyword. */
24226 cp_lexer_consume_token (parser->lexer);
24227 return true;
24230 return false;
24233 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24234 set PARSER->SCOPE, and perform other related actions. */
24236 static void
24237 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
24239 int i;
24240 struct tree_check *check_value;
24241 deferred_access_check *chk;
24242 vec<deferred_access_check, va_gc> *checks;
24244 /* Get the stored value. */
24245 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
24246 /* Perform any access checks that were deferred. */
24247 checks = check_value->checks;
24248 if (checks)
24250 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
24251 perform_or_defer_access_check (chk->binfo,
24252 chk->decl,
24253 chk->diag_decl, tf_warning_or_error);
24255 /* Set the scope from the stored value. */
24256 parser->scope = check_value->value;
24257 parser->qualifying_scope = check_value->qualifying_scope;
24258 parser->object_scope = NULL_TREE;
24261 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24262 encounter the end of a block before what we were looking for. */
24264 static bool
24265 cp_parser_cache_group (cp_parser *parser,
24266 enum cpp_ttype end,
24267 unsigned depth)
24269 while (true)
24271 cp_token *token = cp_lexer_peek_token (parser->lexer);
24273 /* Abort a parenthesized expression if we encounter a semicolon. */
24274 if ((end == CPP_CLOSE_PAREN || depth == 0)
24275 && token->type == CPP_SEMICOLON)
24276 return true;
24277 /* If we've reached the end of the file, stop. */
24278 if (token->type == CPP_EOF
24279 || (end != CPP_PRAGMA_EOL
24280 && token->type == CPP_PRAGMA_EOL))
24281 return true;
24282 if (token->type == CPP_CLOSE_BRACE && depth == 0)
24283 /* We've hit the end of an enclosing block, so there's been some
24284 kind of syntax error. */
24285 return true;
24287 /* Consume the token. */
24288 cp_lexer_consume_token (parser->lexer);
24289 /* See if it starts a new group. */
24290 if (token->type == CPP_OPEN_BRACE)
24292 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
24293 /* In theory this should probably check end == '}', but
24294 cp_parser_save_member_function_body needs it to exit
24295 after either '}' or ')' when called with ')'. */
24296 if (depth == 0)
24297 return false;
24299 else if (token->type == CPP_OPEN_PAREN)
24301 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
24302 if (depth == 0 && end == CPP_CLOSE_PAREN)
24303 return false;
24305 else if (token->type == CPP_PRAGMA)
24306 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
24307 else if (token->type == end)
24308 return false;
24312 /* Like above, for caching a default argument or NSDMI. Both of these are
24313 terminated by a non-nested comma, but it can be unclear whether or not a
24314 comma is nested in a template argument list unless we do more parsing.
24315 In order to handle this ambiguity, when we encounter a ',' after a '<'
24316 we try to parse what follows as a parameter-declaration-list (in the
24317 case of a default argument) or a member-declarator (in the case of an
24318 NSDMI). If that succeeds, then we stop caching. */
24320 static tree
24321 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
24323 unsigned depth = 0;
24324 int maybe_template_id = 0;
24325 cp_token *first_token;
24326 cp_token *token;
24327 tree default_argument;
24329 /* Add tokens until we have processed the entire default
24330 argument. We add the range [first_token, token). */
24331 first_token = cp_lexer_peek_token (parser->lexer);
24332 if (first_token->type == CPP_OPEN_BRACE)
24334 /* For list-initialization, this is straightforward. */
24335 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24336 token = cp_lexer_peek_token (parser->lexer);
24338 else while (true)
24340 bool done = false;
24342 /* Peek at the next token. */
24343 token = cp_lexer_peek_token (parser->lexer);
24344 /* What we do depends on what token we have. */
24345 switch (token->type)
24347 /* In valid code, a default argument must be
24348 immediately followed by a `,' `)', or `...'. */
24349 case CPP_COMMA:
24350 if (depth == 0 && maybe_template_id)
24352 /* If we've seen a '<', we might be in a
24353 template-argument-list. Until Core issue 325 is
24354 resolved, we don't know how this situation ought
24355 to be handled, so try to DTRT. We check whether
24356 what comes after the comma is a valid parameter
24357 declaration list. If it is, then the comma ends
24358 the default argument; otherwise the default
24359 argument continues. */
24360 bool error = false;
24362 /* Set ITALP so cp_parser_parameter_declaration_list
24363 doesn't decide to commit to this parse. */
24364 bool saved_italp = parser->in_template_argument_list_p;
24365 parser->in_template_argument_list_p = true;
24367 cp_parser_parse_tentatively (parser);
24368 cp_lexer_consume_token (parser->lexer);
24370 if (nsdmi)
24372 int ctor_dtor_or_conv_p;
24373 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24374 &ctor_dtor_or_conv_p,
24375 /*parenthesized_p=*/NULL,
24376 /*member_p=*/true);
24378 else
24380 begin_scope (sk_function_parms, NULL_TREE);
24381 cp_parser_parameter_declaration_list (parser, &error);
24382 pop_bindings_and_leave_scope ();
24384 if (!cp_parser_error_occurred (parser) && !error)
24385 done = true;
24386 cp_parser_abort_tentative_parse (parser);
24388 parser->in_template_argument_list_p = saved_italp;
24389 break;
24391 case CPP_CLOSE_PAREN:
24392 case CPP_ELLIPSIS:
24393 /* If we run into a non-nested `;', `}', or `]',
24394 then the code is invalid -- but the default
24395 argument is certainly over. */
24396 case CPP_SEMICOLON:
24397 case CPP_CLOSE_BRACE:
24398 case CPP_CLOSE_SQUARE:
24399 if (depth == 0
24400 /* Handle correctly int n = sizeof ... ( p ); */
24401 && !(nsdmi && token->type == CPP_ELLIPSIS))
24402 done = true;
24403 /* Update DEPTH, if necessary. */
24404 else if (token->type == CPP_CLOSE_PAREN
24405 || token->type == CPP_CLOSE_BRACE
24406 || token->type == CPP_CLOSE_SQUARE)
24407 --depth;
24408 break;
24410 case CPP_OPEN_PAREN:
24411 case CPP_OPEN_SQUARE:
24412 case CPP_OPEN_BRACE:
24413 ++depth;
24414 break;
24416 case CPP_LESS:
24417 if (depth == 0)
24418 /* This might be the comparison operator, or it might
24419 start a template argument list. */
24420 ++maybe_template_id;
24421 break;
24423 case CPP_RSHIFT:
24424 if (cxx_dialect == cxx98)
24425 break;
24426 /* Fall through for C++0x, which treats the `>>'
24427 operator like two `>' tokens in certain
24428 cases. */
24430 case CPP_GREATER:
24431 if (depth == 0)
24433 /* This might be an operator, or it might close a
24434 template argument list. But if a previous '<'
24435 started a template argument list, this will have
24436 closed it, so we can't be in one anymore. */
24437 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
24438 if (maybe_template_id < 0)
24439 maybe_template_id = 0;
24441 break;
24443 /* If we run out of tokens, issue an error message. */
24444 case CPP_EOF:
24445 case CPP_PRAGMA_EOL:
24446 error_at (token->location, "file ends in default argument");
24447 done = true;
24448 break;
24450 case CPP_NAME:
24451 case CPP_SCOPE:
24452 /* In these cases, we should look for template-ids.
24453 For example, if the default argument is
24454 `X<int, double>()', we need to do name lookup to
24455 figure out whether or not `X' is a template; if
24456 so, the `,' does not end the default argument.
24458 That is not yet done. */
24459 break;
24461 default:
24462 break;
24465 /* If we've reached the end, stop. */
24466 if (done)
24467 break;
24469 /* Add the token to the token block. */
24470 token = cp_lexer_consume_token (parser->lexer);
24473 /* Create a DEFAULT_ARG to represent the unparsed default
24474 argument. */
24475 default_argument = make_node (DEFAULT_ARG);
24476 DEFARG_TOKENS (default_argument)
24477 = cp_token_cache_new (first_token, token);
24478 DEFARG_INSTANTIATIONS (default_argument) = NULL;
24480 return default_argument;
24483 /* Begin parsing tentatively. We always save tokens while parsing
24484 tentatively so that if the tentative parsing fails we can restore the
24485 tokens. */
24487 static void
24488 cp_parser_parse_tentatively (cp_parser* parser)
24490 /* Enter a new parsing context. */
24491 parser->context = cp_parser_context_new (parser->context);
24492 /* Begin saving tokens. */
24493 cp_lexer_save_tokens (parser->lexer);
24494 /* In order to avoid repetitive access control error messages,
24495 access checks are queued up until we are no longer parsing
24496 tentatively. */
24497 push_deferring_access_checks (dk_deferred);
24500 /* Commit to the currently active tentative parse. */
24502 static void
24503 cp_parser_commit_to_tentative_parse (cp_parser* parser)
24505 cp_parser_context *context;
24506 cp_lexer *lexer;
24508 /* Mark all of the levels as committed. */
24509 lexer = parser->lexer;
24510 for (context = parser->context; context->next; context = context->next)
24512 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24513 break;
24514 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 /* Commit to the topmost currently active tentative parse.
24523 Note that this function shouldn't be called when there are
24524 irreversible side-effects while in a tentative state. For
24525 example, we shouldn't create a permanent entry in the symbol
24526 table, or issue an error message that might not apply if the
24527 tentative parse is aborted. */
24529 static void
24530 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
24532 cp_parser_context *context = parser->context;
24533 cp_lexer *lexer = parser->lexer;
24535 if (context)
24537 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24538 return;
24539 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24541 while (!cp_lexer_saving_tokens (lexer))
24542 lexer = lexer->next;
24543 cp_lexer_commit_tokens (lexer);
24547 /* Abort the currently active tentative parse. All consumed tokens
24548 will be rolled back, and no diagnostics will be issued. */
24550 static void
24551 cp_parser_abort_tentative_parse (cp_parser* parser)
24553 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
24554 || errorcount > 0);
24555 cp_parser_simulate_error (parser);
24556 /* Now, pretend that we want to see if the construct was
24557 successfully parsed. */
24558 cp_parser_parse_definitely (parser);
24561 /* Stop parsing tentatively. If a parse error has occurred, restore the
24562 token stream. Otherwise, commit to the tokens we have consumed.
24563 Returns true if no error occurred; false otherwise. */
24565 static bool
24566 cp_parser_parse_definitely (cp_parser* parser)
24568 bool error_occurred;
24569 cp_parser_context *context;
24571 /* Remember whether or not an error occurred, since we are about to
24572 destroy that information. */
24573 error_occurred = cp_parser_error_occurred (parser);
24574 /* Remove the topmost context from the stack. */
24575 context = parser->context;
24576 parser->context = context->next;
24577 /* If no parse errors occurred, commit to the tentative parse. */
24578 if (!error_occurred)
24580 /* Commit to the tokens read tentatively, unless that was
24581 already done. */
24582 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
24583 cp_lexer_commit_tokens (parser->lexer);
24585 pop_to_parent_deferring_access_checks ();
24587 /* Otherwise, if errors occurred, roll back our state so that things
24588 are just as they were before we began the tentative parse. */
24589 else
24591 cp_lexer_rollback_tokens (parser->lexer);
24592 pop_deferring_access_checks ();
24594 /* Add the context to the front of the free list. */
24595 context->next = cp_parser_context_free_list;
24596 cp_parser_context_free_list = context;
24598 return !error_occurred;
24601 /* Returns true if we are parsing tentatively and are not committed to
24602 this tentative parse. */
24604 static bool
24605 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
24607 return (cp_parser_parsing_tentatively (parser)
24608 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
24611 /* Returns nonzero iff an error has occurred during the most recent
24612 tentative parse. */
24614 static bool
24615 cp_parser_error_occurred (cp_parser* parser)
24617 return (cp_parser_parsing_tentatively (parser)
24618 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
24621 /* Returns nonzero if GNU extensions are allowed. */
24623 static bool
24624 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
24626 return parser->allow_gnu_extensions_p;
24629 /* Objective-C++ Productions */
24632 /* Parse an Objective-C expression, which feeds into a primary-expression
24633 above.
24635 objc-expression:
24636 objc-message-expression
24637 objc-string-literal
24638 objc-encode-expression
24639 objc-protocol-expression
24640 objc-selector-expression
24642 Returns a tree representation of the expression. */
24644 static tree
24645 cp_parser_objc_expression (cp_parser* parser)
24647 /* Try to figure out what kind of declaration is present. */
24648 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24650 switch (kwd->type)
24652 case CPP_OPEN_SQUARE:
24653 return cp_parser_objc_message_expression (parser);
24655 case CPP_OBJC_STRING:
24656 kwd = cp_lexer_consume_token (parser->lexer);
24657 return objc_build_string_object (kwd->u.value);
24659 case CPP_KEYWORD:
24660 switch (kwd->keyword)
24662 case RID_AT_ENCODE:
24663 return cp_parser_objc_encode_expression (parser);
24665 case RID_AT_PROTOCOL:
24666 return cp_parser_objc_protocol_expression (parser);
24668 case RID_AT_SELECTOR:
24669 return cp_parser_objc_selector_expression (parser);
24671 default:
24672 break;
24674 default:
24675 error_at (kwd->location,
24676 "misplaced %<@%D%> Objective-C++ construct",
24677 kwd->u.value);
24678 cp_parser_skip_to_end_of_block_or_statement (parser);
24681 return error_mark_node;
24684 /* Parse an Objective-C message expression.
24686 objc-message-expression:
24687 [ objc-message-receiver objc-message-args ]
24689 Returns a representation of an Objective-C message. */
24691 static tree
24692 cp_parser_objc_message_expression (cp_parser* parser)
24694 tree receiver, messageargs;
24696 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
24697 receiver = cp_parser_objc_message_receiver (parser);
24698 messageargs = cp_parser_objc_message_args (parser);
24699 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24701 return objc_build_message_expr (receiver, messageargs);
24704 /* Parse an objc-message-receiver.
24706 objc-message-receiver:
24707 expression
24708 simple-type-specifier
24710 Returns a representation of the type or expression. */
24712 static tree
24713 cp_parser_objc_message_receiver (cp_parser* parser)
24715 tree rcv;
24717 /* An Objective-C message receiver may be either (1) a type
24718 or (2) an expression. */
24719 cp_parser_parse_tentatively (parser);
24720 rcv = cp_parser_expression (parser, false, NULL);
24722 if (cp_parser_parse_definitely (parser))
24723 return rcv;
24725 rcv = cp_parser_simple_type_specifier (parser,
24726 /*decl_specs=*/NULL,
24727 CP_PARSER_FLAGS_NONE);
24729 return objc_get_class_reference (rcv);
24732 /* Parse the arguments and selectors comprising an Objective-C message.
24734 objc-message-args:
24735 objc-selector
24736 objc-selector-args
24737 objc-selector-args , objc-comma-args
24739 objc-selector-args:
24740 objc-selector [opt] : assignment-expression
24741 objc-selector-args objc-selector [opt] : assignment-expression
24743 objc-comma-args:
24744 assignment-expression
24745 objc-comma-args , assignment-expression
24747 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
24748 selector arguments and TREE_VALUE containing a list of comma
24749 arguments. */
24751 static tree
24752 cp_parser_objc_message_args (cp_parser* parser)
24754 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
24755 bool maybe_unary_selector_p = true;
24756 cp_token *token = cp_lexer_peek_token (parser->lexer);
24758 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24760 tree selector = NULL_TREE, arg;
24762 if (token->type != CPP_COLON)
24763 selector = cp_parser_objc_selector (parser);
24765 /* Detect if we have a unary selector. */
24766 if (maybe_unary_selector_p
24767 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24768 return build_tree_list (selector, NULL_TREE);
24770 maybe_unary_selector_p = false;
24771 cp_parser_require (parser, CPP_COLON, RT_COLON);
24772 arg = cp_parser_assignment_expression (parser, false, NULL);
24774 sel_args
24775 = chainon (sel_args,
24776 build_tree_list (selector, arg));
24778 token = cp_lexer_peek_token (parser->lexer);
24781 /* Handle non-selector arguments, if any. */
24782 while (token->type == CPP_COMMA)
24784 tree arg;
24786 cp_lexer_consume_token (parser->lexer);
24787 arg = cp_parser_assignment_expression (parser, false, NULL);
24789 addl_args
24790 = chainon (addl_args,
24791 build_tree_list (NULL_TREE, arg));
24793 token = cp_lexer_peek_token (parser->lexer);
24796 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
24798 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
24799 return build_tree_list (error_mark_node, error_mark_node);
24802 return build_tree_list (sel_args, addl_args);
24805 /* Parse an Objective-C encode expression.
24807 objc-encode-expression:
24808 @encode objc-typename
24810 Returns an encoded representation of the type argument. */
24812 static tree
24813 cp_parser_objc_encode_expression (cp_parser* parser)
24815 tree type;
24816 cp_token *token;
24818 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
24819 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24820 token = cp_lexer_peek_token (parser->lexer);
24821 type = complete_type (cp_parser_type_id (parser));
24822 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24824 if (!type)
24826 error_at (token->location,
24827 "%<@encode%> must specify a type as an argument");
24828 return error_mark_node;
24831 /* This happens if we find @encode(T) (where T is a template
24832 typename or something dependent on a template typename) when
24833 parsing a template. In that case, we can't compile it
24834 immediately, but we rather create an AT_ENCODE_EXPR which will
24835 need to be instantiated when the template is used.
24837 if (dependent_type_p (type))
24839 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
24840 TREE_READONLY (value) = 1;
24841 return value;
24844 return objc_build_encode_expr (type);
24847 /* Parse an Objective-C @defs expression. */
24849 static tree
24850 cp_parser_objc_defs_expression (cp_parser *parser)
24852 tree name;
24854 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
24855 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24856 name = cp_parser_identifier (parser);
24857 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24859 return objc_get_class_ivars (name);
24862 /* Parse an Objective-C protocol expression.
24864 objc-protocol-expression:
24865 @protocol ( identifier )
24867 Returns a representation of the protocol expression. */
24869 static tree
24870 cp_parser_objc_protocol_expression (cp_parser* parser)
24872 tree proto;
24874 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24875 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24876 proto = cp_parser_identifier (parser);
24877 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24879 return objc_build_protocol_expr (proto);
24882 /* Parse an Objective-C selector expression.
24884 objc-selector-expression:
24885 @selector ( objc-method-signature )
24887 objc-method-signature:
24888 objc-selector
24889 objc-selector-seq
24891 objc-selector-seq:
24892 objc-selector :
24893 objc-selector-seq objc-selector :
24895 Returns a representation of the method selector. */
24897 static tree
24898 cp_parser_objc_selector_expression (cp_parser* parser)
24900 tree sel_seq = NULL_TREE;
24901 bool maybe_unary_selector_p = true;
24902 cp_token *token;
24903 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24905 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
24906 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24907 token = cp_lexer_peek_token (parser->lexer);
24909 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
24910 || token->type == CPP_SCOPE)
24912 tree selector = NULL_TREE;
24914 if (token->type != CPP_COLON
24915 || token->type == CPP_SCOPE)
24916 selector = cp_parser_objc_selector (parser);
24918 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
24919 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
24921 /* Detect if we have a unary selector. */
24922 if (maybe_unary_selector_p)
24924 sel_seq = selector;
24925 goto finish_selector;
24927 else
24929 cp_parser_error (parser, "expected %<:%>");
24932 maybe_unary_selector_p = false;
24933 token = cp_lexer_consume_token (parser->lexer);
24935 if (token->type == CPP_SCOPE)
24937 sel_seq
24938 = chainon (sel_seq,
24939 build_tree_list (selector, NULL_TREE));
24940 sel_seq
24941 = chainon (sel_seq,
24942 build_tree_list (NULL_TREE, NULL_TREE));
24944 else
24945 sel_seq
24946 = chainon (sel_seq,
24947 build_tree_list (selector, NULL_TREE));
24949 token = cp_lexer_peek_token (parser->lexer);
24952 finish_selector:
24953 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24955 return objc_build_selector_expr (loc, sel_seq);
24958 /* Parse a list of identifiers.
24960 objc-identifier-list:
24961 identifier
24962 objc-identifier-list , identifier
24964 Returns a TREE_LIST of identifier nodes. */
24966 static tree
24967 cp_parser_objc_identifier_list (cp_parser* parser)
24969 tree identifier;
24970 tree list;
24971 cp_token *sep;
24973 identifier = cp_parser_identifier (parser);
24974 if (identifier == error_mark_node)
24975 return error_mark_node;
24977 list = build_tree_list (NULL_TREE, identifier);
24978 sep = cp_lexer_peek_token (parser->lexer);
24980 while (sep->type == CPP_COMMA)
24982 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24983 identifier = cp_parser_identifier (parser);
24984 if (identifier == error_mark_node)
24985 return list;
24987 list = chainon (list, build_tree_list (NULL_TREE,
24988 identifier));
24989 sep = cp_lexer_peek_token (parser->lexer);
24992 return list;
24995 /* Parse an Objective-C alias declaration.
24997 objc-alias-declaration:
24998 @compatibility_alias identifier identifier ;
25000 This function registers the alias mapping with the Objective-C front end.
25001 It returns nothing. */
25003 static void
25004 cp_parser_objc_alias_declaration (cp_parser* parser)
25006 tree alias, orig;
25008 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25009 alias = cp_parser_identifier (parser);
25010 orig = cp_parser_identifier (parser);
25011 objc_declare_alias (alias, orig);
25012 cp_parser_consume_semicolon_at_end_of_statement (parser);
25015 /* Parse an Objective-C class forward-declaration.
25017 objc-class-declaration:
25018 @class objc-identifier-list ;
25020 The function registers the forward declarations with the Objective-C
25021 front end. It returns nothing. */
25023 static void
25024 cp_parser_objc_class_declaration (cp_parser* parser)
25026 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25027 while (true)
25029 tree id;
25031 id = cp_parser_identifier (parser);
25032 if (id == error_mark_node)
25033 break;
25035 objc_declare_class (id);
25037 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25038 cp_lexer_consume_token (parser->lexer);
25039 else
25040 break;
25042 cp_parser_consume_semicolon_at_end_of_statement (parser);
25045 /* Parse a list of Objective-C protocol references.
25047 objc-protocol-refs-opt:
25048 objc-protocol-refs [opt]
25050 objc-protocol-refs:
25051 < objc-identifier-list >
25053 Returns a TREE_LIST of identifiers, if any. */
25055 static tree
25056 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25058 tree protorefs = NULL_TREE;
25060 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25062 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25063 protorefs = cp_parser_objc_identifier_list (parser);
25064 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25067 return protorefs;
25070 /* Parse a Objective-C visibility specification. */
25072 static void
25073 cp_parser_objc_visibility_spec (cp_parser* parser)
25075 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25077 switch (vis->keyword)
25079 case RID_AT_PRIVATE:
25080 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25081 break;
25082 case RID_AT_PROTECTED:
25083 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25084 break;
25085 case RID_AT_PUBLIC:
25086 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25087 break;
25088 case RID_AT_PACKAGE:
25089 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25090 break;
25091 default:
25092 return;
25095 /* Eat '@private'/'@protected'/'@public'. */
25096 cp_lexer_consume_token (parser->lexer);
25099 /* Parse an Objective-C method type. Return 'true' if it is a class
25100 (+) method, and 'false' if it is an instance (-) method. */
25102 static inline bool
25103 cp_parser_objc_method_type (cp_parser* parser)
25105 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25106 return true;
25107 else
25108 return false;
25111 /* Parse an Objective-C protocol qualifier. */
25113 static tree
25114 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25116 tree quals = NULL_TREE, node;
25117 cp_token *token = cp_lexer_peek_token (parser->lexer);
25119 node = token->u.value;
25121 while (node && identifier_p (node)
25122 && (node == ridpointers [(int) RID_IN]
25123 || node == ridpointers [(int) RID_OUT]
25124 || node == ridpointers [(int) RID_INOUT]
25125 || node == ridpointers [(int) RID_BYCOPY]
25126 || node == ridpointers [(int) RID_BYREF]
25127 || node == ridpointers [(int) RID_ONEWAY]))
25129 quals = tree_cons (NULL_TREE, node, quals);
25130 cp_lexer_consume_token (parser->lexer);
25131 token = cp_lexer_peek_token (parser->lexer);
25132 node = token->u.value;
25135 return quals;
25138 /* Parse an Objective-C typename. */
25140 static tree
25141 cp_parser_objc_typename (cp_parser* parser)
25143 tree type_name = NULL_TREE;
25145 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25147 tree proto_quals, cp_type = NULL_TREE;
25149 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25150 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25152 /* An ObjC type name may consist of just protocol qualifiers, in which
25153 case the type shall default to 'id'. */
25154 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25156 cp_type = cp_parser_type_id (parser);
25158 /* If the type could not be parsed, an error has already
25159 been produced. For error recovery, behave as if it had
25160 not been specified, which will use the default type
25161 'id'. */
25162 if (cp_type == error_mark_node)
25164 cp_type = NULL_TREE;
25165 /* We need to skip to the closing parenthesis as
25166 cp_parser_type_id() does not seem to do it for
25167 us. */
25168 cp_parser_skip_to_closing_parenthesis (parser,
25169 /*recovering=*/true,
25170 /*or_comma=*/false,
25171 /*consume_paren=*/false);
25175 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25176 type_name = build_tree_list (proto_quals, cp_type);
25179 return type_name;
25182 /* Check to see if TYPE refers to an Objective-C selector name. */
25184 static bool
25185 cp_parser_objc_selector_p (enum cpp_ttype type)
25187 return (type == CPP_NAME || type == CPP_KEYWORD
25188 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25189 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25190 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25191 || type == CPP_XOR || type == CPP_XOR_EQ);
25194 /* Parse an Objective-C selector. */
25196 static tree
25197 cp_parser_objc_selector (cp_parser* parser)
25199 cp_token *token = cp_lexer_consume_token (parser->lexer);
25201 if (!cp_parser_objc_selector_p (token->type))
25203 error_at (token->location, "invalid Objective-C++ selector name");
25204 return error_mark_node;
25207 /* C++ operator names are allowed to appear in ObjC selectors. */
25208 switch (token->type)
25210 case CPP_AND_AND: return get_identifier ("and");
25211 case CPP_AND_EQ: return get_identifier ("and_eq");
25212 case CPP_AND: return get_identifier ("bitand");
25213 case CPP_OR: return get_identifier ("bitor");
25214 case CPP_COMPL: return get_identifier ("compl");
25215 case CPP_NOT: return get_identifier ("not");
25216 case CPP_NOT_EQ: return get_identifier ("not_eq");
25217 case CPP_OR_OR: return get_identifier ("or");
25218 case CPP_OR_EQ: return get_identifier ("or_eq");
25219 case CPP_XOR: return get_identifier ("xor");
25220 case CPP_XOR_EQ: return get_identifier ("xor_eq");
25221 default: return token->u.value;
25225 /* Parse an Objective-C params list. */
25227 static tree
25228 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
25230 tree params = NULL_TREE;
25231 bool maybe_unary_selector_p = true;
25232 cp_token *token = cp_lexer_peek_token (parser->lexer);
25234 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25236 tree selector = NULL_TREE, type_name, identifier;
25237 tree parm_attr = NULL_TREE;
25239 if (token->keyword == RID_ATTRIBUTE)
25240 break;
25242 if (token->type != CPP_COLON)
25243 selector = cp_parser_objc_selector (parser);
25245 /* Detect if we have a unary selector. */
25246 if (maybe_unary_selector_p
25247 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25249 params = selector; /* Might be followed by attributes. */
25250 break;
25253 maybe_unary_selector_p = false;
25254 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25256 /* Something went quite wrong. There should be a colon
25257 here, but there is not. Stop parsing parameters. */
25258 break;
25260 type_name = cp_parser_objc_typename (parser);
25261 /* New ObjC allows attributes on parameters too. */
25262 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
25263 parm_attr = cp_parser_attributes_opt (parser);
25264 identifier = cp_parser_identifier (parser);
25266 params
25267 = chainon (params,
25268 objc_build_keyword_decl (selector,
25269 type_name,
25270 identifier,
25271 parm_attr));
25273 token = cp_lexer_peek_token (parser->lexer);
25276 if (params == NULL_TREE)
25278 cp_parser_error (parser, "objective-c++ method declaration is expected");
25279 return error_mark_node;
25282 /* We allow tail attributes for the method. */
25283 if (token->keyword == RID_ATTRIBUTE)
25285 *attributes = cp_parser_attributes_opt (parser);
25286 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25287 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25288 return params;
25289 cp_parser_error (parser,
25290 "method attributes must be specified at the end");
25291 return error_mark_node;
25294 if (params == NULL_TREE)
25296 cp_parser_error (parser, "objective-c++ method declaration is expected");
25297 return error_mark_node;
25299 return params;
25302 /* Parse the non-keyword Objective-C params. */
25304 static tree
25305 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
25306 tree* attributes)
25308 tree params = make_node (TREE_LIST);
25309 cp_token *token = cp_lexer_peek_token (parser->lexer);
25310 *ellipsisp = false; /* Initially, assume no ellipsis. */
25312 while (token->type == CPP_COMMA)
25314 cp_parameter_declarator *parmdecl;
25315 tree parm;
25317 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25318 token = cp_lexer_peek_token (parser->lexer);
25320 if (token->type == CPP_ELLIPSIS)
25322 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
25323 *ellipsisp = true;
25324 token = cp_lexer_peek_token (parser->lexer);
25325 break;
25328 /* TODO: parse attributes for tail parameters. */
25329 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
25330 parm = grokdeclarator (parmdecl->declarator,
25331 &parmdecl->decl_specifiers,
25332 PARM, /*initialized=*/0,
25333 /*attrlist=*/NULL);
25335 chainon (params, build_tree_list (NULL_TREE, parm));
25336 token = cp_lexer_peek_token (parser->lexer);
25339 /* We allow tail attributes for the method. */
25340 if (token->keyword == RID_ATTRIBUTE)
25342 if (*attributes == NULL_TREE)
25344 *attributes = cp_parser_attributes_opt (parser);
25345 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25346 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25347 return params;
25349 else
25350 /* We have an error, but parse the attributes, so that we can
25351 carry on. */
25352 *attributes = cp_parser_attributes_opt (parser);
25354 cp_parser_error (parser,
25355 "method attributes must be specified at the end");
25356 return error_mark_node;
25359 return params;
25362 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
25364 static void
25365 cp_parser_objc_interstitial_code (cp_parser* parser)
25367 cp_token *token = cp_lexer_peek_token (parser->lexer);
25369 /* If the next token is `extern' and the following token is a string
25370 literal, then we have a linkage specification. */
25371 if (token->keyword == RID_EXTERN
25372 && cp_parser_is_pure_string_literal
25373 (cp_lexer_peek_nth_token (parser->lexer, 2)))
25374 cp_parser_linkage_specification (parser);
25375 /* Handle #pragma, if any. */
25376 else if (token->type == CPP_PRAGMA)
25377 cp_parser_pragma (parser, pragma_objc_icode);
25378 /* Allow stray semicolons. */
25379 else if (token->type == CPP_SEMICOLON)
25380 cp_lexer_consume_token (parser->lexer);
25381 /* Mark methods as optional or required, when building protocols. */
25382 else if (token->keyword == RID_AT_OPTIONAL)
25384 cp_lexer_consume_token (parser->lexer);
25385 objc_set_method_opt (true);
25387 else if (token->keyword == RID_AT_REQUIRED)
25389 cp_lexer_consume_token (parser->lexer);
25390 objc_set_method_opt (false);
25392 else if (token->keyword == RID_NAMESPACE)
25393 cp_parser_namespace_definition (parser);
25394 /* Other stray characters must generate errors. */
25395 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
25397 cp_lexer_consume_token (parser->lexer);
25398 error ("stray %qs between Objective-C++ methods",
25399 token->type == CPP_OPEN_BRACE ? "{" : "}");
25401 /* Finally, try to parse a block-declaration, or a function-definition. */
25402 else
25403 cp_parser_block_declaration (parser, /*statement_p=*/false);
25406 /* Parse a method signature. */
25408 static tree
25409 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
25411 tree rettype, kwdparms, optparms;
25412 bool ellipsis = false;
25413 bool is_class_method;
25415 is_class_method = cp_parser_objc_method_type (parser);
25416 rettype = cp_parser_objc_typename (parser);
25417 *attributes = NULL_TREE;
25418 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
25419 if (kwdparms == error_mark_node)
25420 return error_mark_node;
25421 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
25422 if (optparms == error_mark_node)
25423 return error_mark_node;
25425 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
25428 static bool
25429 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
25431 tree tattr;
25432 cp_lexer_save_tokens (parser->lexer);
25433 tattr = cp_parser_attributes_opt (parser);
25434 gcc_assert (tattr) ;
25436 /* If the attributes are followed by a method introducer, this is not allowed.
25437 Dump the attributes and flag the situation. */
25438 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
25439 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
25440 return true;
25442 /* Otherwise, the attributes introduce some interstitial code, possibly so
25443 rewind to allow that check. */
25444 cp_lexer_rollback_tokens (parser->lexer);
25445 return false;
25448 /* Parse an Objective-C method prototype list. */
25450 static void
25451 cp_parser_objc_method_prototype_list (cp_parser* parser)
25453 cp_token *token = cp_lexer_peek_token (parser->lexer);
25455 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25457 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25459 tree attributes, sig;
25460 bool is_class_method;
25461 if (token->type == CPP_PLUS)
25462 is_class_method = true;
25463 else
25464 is_class_method = false;
25465 sig = cp_parser_objc_method_signature (parser, &attributes);
25466 if (sig == error_mark_node)
25468 cp_parser_skip_to_end_of_block_or_statement (parser);
25469 token = cp_lexer_peek_token (parser->lexer);
25470 continue;
25472 objc_add_method_declaration (is_class_method, sig, attributes);
25473 cp_parser_consume_semicolon_at_end_of_statement (parser);
25475 else if (token->keyword == RID_AT_PROPERTY)
25476 cp_parser_objc_at_property_declaration (parser);
25477 else if (token->keyword == RID_ATTRIBUTE
25478 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25479 warning_at (cp_lexer_peek_token (parser->lexer)->location,
25480 OPT_Wattributes,
25481 "prefix attributes are ignored for methods");
25482 else
25483 /* Allow for interspersed non-ObjC++ code. */
25484 cp_parser_objc_interstitial_code (parser);
25486 token = cp_lexer_peek_token (parser->lexer);
25489 if (token->type != CPP_EOF)
25490 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25491 else
25492 cp_parser_error (parser, "expected %<@end%>");
25494 objc_finish_interface ();
25497 /* Parse an Objective-C method definition list. */
25499 static void
25500 cp_parser_objc_method_definition_list (cp_parser* parser)
25502 cp_token *token = cp_lexer_peek_token (parser->lexer);
25504 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25506 tree meth;
25508 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25510 cp_token *ptk;
25511 tree sig, attribute;
25512 bool is_class_method;
25513 if (token->type == CPP_PLUS)
25514 is_class_method = true;
25515 else
25516 is_class_method = false;
25517 push_deferring_access_checks (dk_deferred);
25518 sig = cp_parser_objc_method_signature (parser, &attribute);
25519 if (sig == error_mark_node)
25521 cp_parser_skip_to_end_of_block_or_statement (parser);
25522 token = cp_lexer_peek_token (parser->lexer);
25523 continue;
25525 objc_start_method_definition (is_class_method, sig, attribute,
25526 NULL_TREE);
25528 /* For historical reasons, we accept an optional semicolon. */
25529 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25530 cp_lexer_consume_token (parser->lexer);
25532 ptk = cp_lexer_peek_token (parser->lexer);
25533 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
25534 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
25536 perform_deferred_access_checks (tf_warning_or_error);
25537 stop_deferring_access_checks ();
25538 meth = cp_parser_function_definition_after_declarator (parser,
25539 false);
25540 pop_deferring_access_checks ();
25541 objc_finish_method_definition (meth);
25544 /* The following case will be removed once @synthesize is
25545 completely implemented. */
25546 else if (token->keyword == RID_AT_PROPERTY)
25547 cp_parser_objc_at_property_declaration (parser);
25548 else if (token->keyword == RID_AT_SYNTHESIZE)
25549 cp_parser_objc_at_synthesize_declaration (parser);
25550 else if (token->keyword == RID_AT_DYNAMIC)
25551 cp_parser_objc_at_dynamic_declaration (parser);
25552 else if (token->keyword == RID_ATTRIBUTE
25553 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25554 warning_at (token->location, OPT_Wattributes,
25555 "prefix attributes are ignored for methods");
25556 else
25557 /* Allow for interspersed non-ObjC++ code. */
25558 cp_parser_objc_interstitial_code (parser);
25560 token = cp_lexer_peek_token (parser->lexer);
25563 if (token->type != CPP_EOF)
25564 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25565 else
25566 cp_parser_error (parser, "expected %<@end%>");
25568 objc_finish_implementation ();
25571 /* Parse Objective-C ivars. */
25573 static void
25574 cp_parser_objc_class_ivars (cp_parser* parser)
25576 cp_token *token = cp_lexer_peek_token (parser->lexer);
25578 if (token->type != CPP_OPEN_BRACE)
25579 return; /* No ivars specified. */
25581 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
25582 token = cp_lexer_peek_token (parser->lexer);
25584 while (token->type != CPP_CLOSE_BRACE
25585 && token->keyword != RID_AT_END && token->type != CPP_EOF)
25587 cp_decl_specifier_seq declspecs;
25588 int decl_class_or_enum_p;
25589 tree prefix_attributes;
25591 cp_parser_objc_visibility_spec (parser);
25593 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25594 break;
25596 cp_parser_decl_specifier_seq (parser,
25597 CP_PARSER_FLAGS_OPTIONAL,
25598 &declspecs,
25599 &decl_class_or_enum_p);
25601 /* auto, register, static, extern, mutable. */
25602 if (declspecs.storage_class != sc_none)
25604 cp_parser_error (parser, "invalid type for instance variable");
25605 declspecs.storage_class = sc_none;
25608 /* thread_local. */
25609 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
25611 cp_parser_error (parser, "invalid type for instance variable");
25612 declspecs.locations[ds_thread] = 0;
25615 /* typedef. */
25616 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
25618 cp_parser_error (parser, "invalid type for instance variable");
25619 declspecs.locations[ds_typedef] = 0;
25622 prefix_attributes = declspecs.attributes;
25623 declspecs.attributes = NULL_TREE;
25625 /* Keep going until we hit the `;' at the end of the
25626 declaration. */
25627 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25629 tree width = NULL_TREE, attributes, first_attribute, decl;
25630 cp_declarator *declarator = NULL;
25631 int ctor_dtor_or_conv_p;
25633 /* Check for a (possibly unnamed) bitfield declaration. */
25634 token = cp_lexer_peek_token (parser->lexer);
25635 if (token->type == CPP_COLON)
25636 goto eat_colon;
25638 if (token->type == CPP_NAME
25639 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25640 == CPP_COLON))
25642 /* Get the name of the bitfield. */
25643 declarator = make_id_declarator (NULL_TREE,
25644 cp_parser_identifier (parser),
25645 sfk_none);
25647 eat_colon:
25648 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
25649 /* Get the width of the bitfield. */
25650 width
25651 = cp_parser_constant_expression (parser,
25652 /*allow_non_constant=*/false,
25653 NULL);
25655 else
25657 /* Parse the declarator. */
25658 declarator
25659 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25660 &ctor_dtor_or_conv_p,
25661 /*parenthesized_p=*/NULL,
25662 /*member_p=*/false);
25665 /* Look for attributes that apply to the ivar. */
25666 attributes = cp_parser_attributes_opt (parser);
25667 /* Remember which attributes are prefix attributes and
25668 which are not. */
25669 first_attribute = attributes;
25670 /* Combine the attributes. */
25671 attributes = chainon (prefix_attributes, attributes);
25673 if (width)
25674 /* Create the bitfield declaration. */
25675 decl = grokbitfield (declarator, &declspecs,
25676 width,
25677 attributes);
25678 else
25679 decl = grokfield (declarator, &declspecs,
25680 NULL_TREE, /*init_const_expr_p=*/false,
25681 NULL_TREE, attributes);
25683 /* Add the instance variable. */
25684 if (decl != error_mark_node && decl != NULL_TREE)
25685 objc_add_instance_variable (decl);
25687 /* Reset PREFIX_ATTRIBUTES. */
25688 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25689 attributes = TREE_CHAIN (attributes);
25690 if (attributes)
25691 TREE_CHAIN (attributes) = NULL_TREE;
25693 token = cp_lexer_peek_token (parser->lexer);
25695 if (token->type == CPP_COMMA)
25697 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25698 continue;
25700 break;
25703 cp_parser_consume_semicolon_at_end_of_statement (parser);
25704 token = cp_lexer_peek_token (parser->lexer);
25707 if (token->keyword == RID_AT_END)
25708 cp_parser_error (parser, "expected %<}%>");
25710 /* Do not consume the RID_AT_END, so it will be read again as terminating
25711 the @interface of @implementation. */
25712 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
25713 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
25715 /* For historical reasons, we accept an optional semicolon. */
25716 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25717 cp_lexer_consume_token (parser->lexer);
25720 /* Parse an Objective-C protocol declaration. */
25722 static void
25723 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
25725 tree proto, protorefs;
25726 cp_token *tok;
25728 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25729 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
25731 tok = cp_lexer_peek_token (parser->lexer);
25732 error_at (tok->location, "identifier expected after %<@protocol%>");
25733 cp_parser_consume_semicolon_at_end_of_statement (parser);
25734 return;
25737 /* See if we have a forward declaration or a definition. */
25738 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
25740 /* Try a forward declaration first. */
25741 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
25743 while (true)
25745 tree id;
25747 id = cp_parser_identifier (parser);
25748 if (id == error_mark_node)
25749 break;
25751 objc_declare_protocol (id, attributes);
25753 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25754 cp_lexer_consume_token (parser->lexer);
25755 else
25756 break;
25758 cp_parser_consume_semicolon_at_end_of_statement (parser);
25761 /* Ok, we got a full-fledged definition (or at least should). */
25762 else
25764 proto = cp_parser_identifier (parser);
25765 protorefs = cp_parser_objc_protocol_refs_opt (parser);
25766 objc_start_protocol (proto, protorefs, attributes);
25767 cp_parser_objc_method_prototype_list (parser);
25771 /* Parse an Objective-C superclass or category. */
25773 static void
25774 cp_parser_objc_superclass_or_category (cp_parser *parser,
25775 bool iface_p,
25776 tree *super,
25777 tree *categ, bool *is_class_extension)
25779 cp_token *next = cp_lexer_peek_token (parser->lexer);
25781 *super = *categ = NULL_TREE;
25782 *is_class_extension = false;
25783 if (next->type == CPP_COLON)
25785 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
25786 *super = cp_parser_identifier (parser);
25788 else if (next->type == CPP_OPEN_PAREN)
25790 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25792 /* If there is no category name, and this is an @interface, we
25793 have a class extension. */
25794 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25796 *categ = NULL_TREE;
25797 *is_class_extension = true;
25799 else
25800 *categ = cp_parser_identifier (parser);
25802 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25806 /* Parse an Objective-C class interface. */
25808 static void
25809 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
25811 tree name, super, categ, protos;
25812 bool is_class_extension;
25814 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
25815 name = cp_parser_identifier (parser);
25816 if (name == error_mark_node)
25818 /* It's hard to recover because even if valid @interface stuff
25819 is to follow, we can't compile it (or validate it) if we
25820 don't even know which class it refers to. Let's assume this
25821 was a stray '@interface' token in the stream and skip it.
25823 return;
25825 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
25826 &is_class_extension);
25827 protos = cp_parser_objc_protocol_refs_opt (parser);
25829 /* We have either a class or a category on our hands. */
25830 if (categ || is_class_extension)
25831 objc_start_category_interface (name, categ, protos, attributes);
25832 else
25834 objc_start_class_interface (name, super, protos, attributes);
25835 /* Handle instance variable declarations, if any. */
25836 cp_parser_objc_class_ivars (parser);
25837 objc_continue_interface ();
25840 cp_parser_objc_method_prototype_list (parser);
25843 /* Parse an Objective-C class implementation. */
25845 static void
25846 cp_parser_objc_class_implementation (cp_parser* parser)
25848 tree name, super, categ;
25849 bool is_class_extension;
25851 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
25852 name = cp_parser_identifier (parser);
25853 if (name == error_mark_node)
25855 /* It's hard to recover because even if valid @implementation
25856 stuff is to follow, we can't compile it (or validate it) if
25857 we don't even know which class it refers to. Let's assume
25858 this was a stray '@implementation' token in the stream and
25859 skip it.
25861 return;
25863 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
25864 &is_class_extension);
25866 /* We have either a class or a category on our hands. */
25867 if (categ)
25868 objc_start_category_implementation (name, categ);
25869 else
25871 objc_start_class_implementation (name, super);
25872 /* Handle instance variable declarations, if any. */
25873 cp_parser_objc_class_ivars (parser);
25874 objc_continue_implementation ();
25877 cp_parser_objc_method_definition_list (parser);
25880 /* Consume the @end token and finish off the implementation. */
25882 static void
25883 cp_parser_objc_end_implementation (cp_parser* parser)
25885 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25886 objc_finish_implementation ();
25889 /* Parse an Objective-C declaration. */
25891 static void
25892 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
25894 /* Try to figure out what kind of declaration is present. */
25895 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25897 if (attributes)
25898 switch (kwd->keyword)
25900 case RID_AT_ALIAS:
25901 case RID_AT_CLASS:
25902 case RID_AT_END:
25903 error_at (kwd->location, "attributes may not be specified before"
25904 " the %<@%D%> Objective-C++ keyword",
25905 kwd->u.value);
25906 attributes = NULL;
25907 break;
25908 case RID_AT_IMPLEMENTATION:
25909 warning_at (kwd->location, OPT_Wattributes,
25910 "prefix attributes are ignored before %<@%D%>",
25911 kwd->u.value);
25912 attributes = NULL;
25913 default:
25914 break;
25917 switch (kwd->keyword)
25919 case RID_AT_ALIAS:
25920 cp_parser_objc_alias_declaration (parser);
25921 break;
25922 case RID_AT_CLASS:
25923 cp_parser_objc_class_declaration (parser);
25924 break;
25925 case RID_AT_PROTOCOL:
25926 cp_parser_objc_protocol_declaration (parser, attributes);
25927 break;
25928 case RID_AT_INTERFACE:
25929 cp_parser_objc_class_interface (parser, attributes);
25930 break;
25931 case RID_AT_IMPLEMENTATION:
25932 cp_parser_objc_class_implementation (parser);
25933 break;
25934 case RID_AT_END:
25935 cp_parser_objc_end_implementation (parser);
25936 break;
25937 default:
25938 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25939 kwd->u.value);
25940 cp_parser_skip_to_end_of_block_or_statement (parser);
25944 /* Parse an Objective-C try-catch-finally statement.
25946 objc-try-catch-finally-stmt:
25947 @try compound-statement objc-catch-clause-seq [opt]
25948 objc-finally-clause [opt]
25950 objc-catch-clause-seq:
25951 objc-catch-clause objc-catch-clause-seq [opt]
25953 objc-catch-clause:
25954 @catch ( objc-exception-declaration ) compound-statement
25956 objc-finally-clause:
25957 @finally compound-statement
25959 objc-exception-declaration:
25960 parameter-declaration
25961 '...'
25963 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
25965 Returns NULL_TREE.
25967 PS: This function is identical to c_parser_objc_try_catch_finally_statement
25968 for C. Keep them in sync. */
25970 static tree
25971 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
25973 location_t location;
25974 tree stmt;
25976 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
25977 location = cp_lexer_peek_token (parser->lexer)->location;
25978 objc_maybe_warn_exceptions (location);
25979 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
25980 node, lest it get absorbed into the surrounding block. */
25981 stmt = push_stmt_list ();
25982 cp_parser_compound_statement (parser, NULL, false, false);
25983 objc_begin_try_stmt (location, pop_stmt_list (stmt));
25985 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
25987 cp_parameter_declarator *parm;
25988 tree parameter_declaration = error_mark_node;
25989 bool seen_open_paren = false;
25991 cp_lexer_consume_token (parser->lexer);
25992 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25993 seen_open_paren = true;
25994 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25996 /* We have "@catch (...)" (where the '...' are literally
25997 what is in the code). Skip the '...'.
25998 parameter_declaration is set to NULL_TREE, and
25999 objc_being_catch_clauses() knows that that means
26000 '...'. */
26001 cp_lexer_consume_token (parser->lexer);
26002 parameter_declaration = NULL_TREE;
26004 else
26006 /* We have "@catch (NSException *exception)" or something
26007 like that. Parse the parameter declaration. */
26008 parm = cp_parser_parameter_declaration (parser, false, NULL);
26009 if (parm == NULL)
26010 parameter_declaration = error_mark_node;
26011 else
26012 parameter_declaration = grokdeclarator (parm->declarator,
26013 &parm->decl_specifiers,
26014 PARM, /*initialized=*/0,
26015 /*attrlist=*/NULL);
26017 if (seen_open_paren)
26018 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26019 else
26021 /* If there was no open parenthesis, we are recovering from
26022 an error, and we are trying to figure out what mistake
26023 the user has made. */
26025 /* If there is an immediate closing parenthesis, the user
26026 probably forgot the opening one (ie, they typed "@catch
26027 NSException *e)". Parse the closing parenthesis and keep
26028 going. */
26029 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26030 cp_lexer_consume_token (parser->lexer);
26032 /* If these is no immediate closing parenthesis, the user
26033 probably doesn't know that parenthesis are required at
26034 all (ie, they typed "@catch NSException *e"). So, just
26035 forget about the closing parenthesis and keep going. */
26037 objc_begin_catch_clause (parameter_declaration);
26038 cp_parser_compound_statement (parser, NULL, false, false);
26039 objc_finish_catch_clause ();
26041 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26043 cp_lexer_consume_token (parser->lexer);
26044 location = cp_lexer_peek_token (parser->lexer)->location;
26045 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26046 node, lest it get absorbed into the surrounding block. */
26047 stmt = push_stmt_list ();
26048 cp_parser_compound_statement (parser, NULL, false, false);
26049 objc_build_finally_clause (location, pop_stmt_list (stmt));
26052 return objc_finish_try_stmt ();
26055 /* Parse an Objective-C synchronized statement.
26057 objc-synchronized-stmt:
26058 @synchronized ( expression ) compound-statement
26060 Returns NULL_TREE. */
26062 static tree
26063 cp_parser_objc_synchronized_statement (cp_parser *parser)
26065 location_t location;
26066 tree lock, stmt;
26068 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26070 location = cp_lexer_peek_token (parser->lexer)->location;
26071 objc_maybe_warn_exceptions (location);
26072 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26073 lock = cp_parser_expression (parser, false, NULL);
26074 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26076 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26077 node, lest it get absorbed into the surrounding block. */
26078 stmt = push_stmt_list ();
26079 cp_parser_compound_statement (parser, NULL, false, false);
26081 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26084 /* Parse an Objective-C throw statement.
26086 objc-throw-stmt:
26087 @throw assignment-expression [opt] ;
26089 Returns a constructed '@throw' statement. */
26091 static tree
26092 cp_parser_objc_throw_statement (cp_parser *parser)
26094 tree expr = NULL_TREE;
26095 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26097 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26099 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26100 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26102 cp_parser_consume_semicolon_at_end_of_statement (parser);
26104 return objc_build_throw_stmt (loc, expr);
26107 /* Parse an Objective-C statement. */
26109 static tree
26110 cp_parser_objc_statement (cp_parser * parser)
26112 /* Try to figure out what kind of declaration is present. */
26113 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26115 switch (kwd->keyword)
26117 case RID_AT_TRY:
26118 return cp_parser_objc_try_catch_finally_statement (parser);
26119 case RID_AT_SYNCHRONIZED:
26120 return cp_parser_objc_synchronized_statement (parser);
26121 case RID_AT_THROW:
26122 return cp_parser_objc_throw_statement (parser);
26123 default:
26124 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26125 kwd->u.value);
26126 cp_parser_skip_to_end_of_block_or_statement (parser);
26129 return error_mark_node;
26132 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26133 look ahead to see if an objc keyword follows the attributes. This
26134 is to detect the use of prefix attributes on ObjC @interface and
26135 @protocol. */
26137 static bool
26138 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26140 cp_lexer_save_tokens (parser->lexer);
26141 *attrib = cp_parser_attributes_opt (parser);
26142 gcc_assert (*attrib);
26143 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26145 cp_lexer_commit_tokens (parser->lexer);
26146 return true;
26148 cp_lexer_rollback_tokens (parser->lexer);
26149 return false;
26152 /* This routine is a minimal replacement for
26153 c_parser_struct_declaration () used when parsing the list of
26154 types/names or ObjC++ properties. For example, when parsing the
26155 code
26157 @property (readonly) int a, b, c;
26159 this function is responsible for parsing "int a, int b, int c" and
26160 returning the declarations as CHAIN of DECLs.
26162 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26163 similar parsing. */
26164 static tree
26165 cp_parser_objc_struct_declaration (cp_parser *parser)
26167 tree decls = NULL_TREE;
26168 cp_decl_specifier_seq declspecs;
26169 int decl_class_or_enum_p;
26170 tree prefix_attributes;
26172 cp_parser_decl_specifier_seq (parser,
26173 CP_PARSER_FLAGS_NONE,
26174 &declspecs,
26175 &decl_class_or_enum_p);
26177 if (declspecs.type == error_mark_node)
26178 return error_mark_node;
26180 /* auto, register, static, extern, mutable. */
26181 if (declspecs.storage_class != sc_none)
26183 cp_parser_error (parser, "invalid type for property");
26184 declspecs.storage_class = sc_none;
26187 /* thread_local. */
26188 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26190 cp_parser_error (parser, "invalid type for property");
26191 declspecs.locations[ds_thread] = 0;
26194 /* typedef. */
26195 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26197 cp_parser_error (parser, "invalid type for property");
26198 declspecs.locations[ds_typedef] = 0;
26201 prefix_attributes = declspecs.attributes;
26202 declspecs.attributes = NULL_TREE;
26204 /* Keep going until we hit the `;' at the end of the declaration. */
26205 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26207 tree attributes, first_attribute, decl;
26208 cp_declarator *declarator;
26209 cp_token *token;
26211 /* Parse the declarator. */
26212 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26213 NULL, NULL, false);
26215 /* Look for attributes that apply to the ivar. */
26216 attributes = cp_parser_attributes_opt (parser);
26217 /* Remember which attributes are prefix attributes and
26218 which are not. */
26219 first_attribute = attributes;
26220 /* Combine the attributes. */
26221 attributes = chainon (prefix_attributes, attributes);
26223 decl = grokfield (declarator, &declspecs,
26224 NULL_TREE, /*init_const_expr_p=*/false,
26225 NULL_TREE, attributes);
26227 if (decl == error_mark_node || decl == NULL_TREE)
26228 return error_mark_node;
26230 /* Reset PREFIX_ATTRIBUTES. */
26231 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26232 attributes = TREE_CHAIN (attributes);
26233 if (attributes)
26234 TREE_CHAIN (attributes) = NULL_TREE;
26236 DECL_CHAIN (decl) = decls;
26237 decls = decl;
26239 token = cp_lexer_peek_token (parser->lexer);
26240 if (token->type == CPP_COMMA)
26242 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26243 continue;
26245 else
26246 break;
26248 return decls;
26251 /* Parse an Objective-C @property declaration. The syntax is:
26253 objc-property-declaration:
26254 '@property' objc-property-attributes[opt] struct-declaration ;
26256 objc-property-attributes:
26257 '(' objc-property-attribute-list ')'
26259 objc-property-attribute-list:
26260 objc-property-attribute
26261 objc-property-attribute-list, objc-property-attribute
26263 objc-property-attribute
26264 'getter' = identifier
26265 'setter' = identifier
26266 'readonly'
26267 'readwrite'
26268 'assign'
26269 'retain'
26270 'copy'
26271 'nonatomic'
26273 For example:
26274 @property NSString *name;
26275 @property (readonly) id object;
26276 @property (retain, nonatomic, getter=getTheName) id name;
26277 @property int a, b, c;
26279 PS: This function is identical to
26280 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26281 static void
26282 cp_parser_objc_at_property_declaration (cp_parser *parser)
26284 /* The following variables hold the attributes of the properties as
26285 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26286 seen. When we see an attribute, we set them to 'true' (if they
26287 are boolean properties) or to the identifier (if they have an
26288 argument, ie, for getter and setter). Note that here we only
26289 parse the list of attributes, check the syntax and accumulate the
26290 attributes that we find. objc_add_property_declaration() will
26291 then process the information. */
26292 bool property_assign = false;
26293 bool property_copy = false;
26294 tree property_getter_ident = NULL_TREE;
26295 bool property_nonatomic = false;
26296 bool property_readonly = false;
26297 bool property_readwrite = false;
26298 bool property_retain = false;
26299 tree property_setter_ident = NULL_TREE;
26301 /* 'properties' is the list of properties that we read. Usually a
26302 single one, but maybe more (eg, in "@property int a, b, c;" there
26303 are three). */
26304 tree properties;
26305 location_t loc;
26307 loc = cp_lexer_peek_token (parser->lexer)->location;
26309 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
26311 /* Parse the optional attribute list... */
26312 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26314 /* Eat the '('. */
26315 cp_lexer_consume_token (parser->lexer);
26317 while (true)
26319 bool syntax_error = false;
26320 cp_token *token = cp_lexer_peek_token (parser->lexer);
26321 enum rid keyword;
26323 if (token->type != CPP_NAME)
26325 cp_parser_error (parser, "expected identifier");
26326 break;
26328 keyword = C_RID_CODE (token->u.value);
26329 cp_lexer_consume_token (parser->lexer);
26330 switch (keyword)
26332 case RID_ASSIGN: property_assign = true; break;
26333 case RID_COPY: property_copy = true; break;
26334 case RID_NONATOMIC: property_nonatomic = true; break;
26335 case RID_READONLY: property_readonly = true; break;
26336 case RID_READWRITE: property_readwrite = true; break;
26337 case RID_RETAIN: property_retain = true; break;
26339 case RID_GETTER:
26340 case RID_SETTER:
26341 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26343 if (keyword == RID_GETTER)
26344 cp_parser_error (parser,
26345 "missing %<=%> (after %<getter%> attribute)");
26346 else
26347 cp_parser_error (parser,
26348 "missing %<=%> (after %<setter%> attribute)");
26349 syntax_error = true;
26350 break;
26352 cp_lexer_consume_token (parser->lexer); /* eat the = */
26353 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
26355 cp_parser_error (parser, "expected identifier");
26356 syntax_error = true;
26357 break;
26359 if (keyword == RID_SETTER)
26361 if (property_setter_ident != NULL_TREE)
26363 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
26364 cp_lexer_consume_token (parser->lexer);
26366 else
26367 property_setter_ident = cp_parser_objc_selector (parser);
26368 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26369 cp_parser_error (parser, "setter name must terminate with %<:%>");
26370 else
26371 cp_lexer_consume_token (parser->lexer);
26373 else
26375 if (property_getter_ident != NULL_TREE)
26377 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
26378 cp_lexer_consume_token (parser->lexer);
26380 else
26381 property_getter_ident = cp_parser_objc_selector (parser);
26383 break;
26384 default:
26385 cp_parser_error (parser, "unknown property attribute");
26386 syntax_error = true;
26387 break;
26390 if (syntax_error)
26391 break;
26393 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26394 cp_lexer_consume_token (parser->lexer);
26395 else
26396 break;
26399 /* FIXME: "@property (setter, assign);" will generate a spurious
26400 "error: expected ‘)’ before ‘,’ token". This is because
26401 cp_parser_require, unlike the C counterpart, will produce an
26402 error even if we are in error recovery. */
26403 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26405 cp_parser_skip_to_closing_parenthesis (parser,
26406 /*recovering=*/true,
26407 /*or_comma=*/false,
26408 /*consume_paren=*/true);
26412 /* ... and the property declaration(s). */
26413 properties = cp_parser_objc_struct_declaration (parser);
26415 if (properties == error_mark_node)
26417 cp_parser_skip_to_end_of_statement (parser);
26418 /* If the next token is now a `;', consume it. */
26419 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26420 cp_lexer_consume_token (parser->lexer);
26421 return;
26424 if (properties == NULL_TREE)
26425 cp_parser_error (parser, "expected identifier");
26426 else
26428 /* Comma-separated properties are chained together in
26429 reverse order; add them one by one. */
26430 properties = nreverse (properties);
26432 for (; properties; properties = TREE_CHAIN (properties))
26433 objc_add_property_declaration (loc, copy_node (properties),
26434 property_readonly, property_readwrite,
26435 property_assign, property_retain,
26436 property_copy, property_nonatomic,
26437 property_getter_ident, property_setter_ident);
26440 cp_parser_consume_semicolon_at_end_of_statement (parser);
26443 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
26445 objc-synthesize-declaration:
26446 @synthesize objc-synthesize-identifier-list ;
26448 objc-synthesize-identifier-list:
26449 objc-synthesize-identifier
26450 objc-synthesize-identifier-list, objc-synthesize-identifier
26452 objc-synthesize-identifier
26453 identifier
26454 identifier = identifier
26456 For example:
26457 @synthesize MyProperty;
26458 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
26460 PS: This function is identical to c_parser_objc_at_synthesize_declaration
26461 for C. Keep them in sync.
26463 static void
26464 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
26466 tree list = NULL_TREE;
26467 location_t loc;
26468 loc = cp_lexer_peek_token (parser->lexer)->location;
26470 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
26471 while (true)
26473 tree property, ivar;
26474 property = cp_parser_identifier (parser);
26475 if (property == error_mark_node)
26477 cp_parser_consume_semicolon_at_end_of_statement (parser);
26478 return;
26480 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26482 cp_lexer_consume_token (parser->lexer);
26483 ivar = cp_parser_identifier (parser);
26484 if (ivar == error_mark_node)
26486 cp_parser_consume_semicolon_at_end_of_statement (parser);
26487 return;
26490 else
26491 ivar = NULL_TREE;
26492 list = chainon (list, build_tree_list (ivar, property));
26493 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26494 cp_lexer_consume_token (parser->lexer);
26495 else
26496 break;
26498 cp_parser_consume_semicolon_at_end_of_statement (parser);
26499 objc_add_synthesize_declaration (loc, list);
26502 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
26504 objc-dynamic-declaration:
26505 @dynamic identifier-list ;
26507 For example:
26508 @dynamic MyProperty;
26509 @dynamic MyProperty, AnotherProperty;
26511 PS: This function is identical to c_parser_objc_at_dynamic_declaration
26512 for C. Keep them in sync.
26514 static void
26515 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
26517 tree list = NULL_TREE;
26518 location_t loc;
26519 loc = cp_lexer_peek_token (parser->lexer)->location;
26521 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
26522 while (true)
26524 tree property;
26525 property = cp_parser_identifier (parser);
26526 if (property == error_mark_node)
26528 cp_parser_consume_semicolon_at_end_of_statement (parser);
26529 return;
26531 list = chainon (list, build_tree_list (NULL, property));
26532 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26533 cp_lexer_consume_token (parser->lexer);
26534 else
26535 break;
26537 cp_parser_consume_semicolon_at_end_of_statement (parser);
26538 objc_add_dynamic_declaration (loc, list);
26542 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
26544 /* Returns name of the next clause.
26545 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26546 the token is not consumed. Otherwise appropriate pragma_omp_clause is
26547 returned and the token is consumed. */
26549 static pragma_omp_clause
26550 cp_parser_omp_clause_name (cp_parser *parser)
26552 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
26554 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
26555 result = PRAGMA_OMP_CLAUSE_IF;
26556 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
26557 result = PRAGMA_OMP_CLAUSE_DEFAULT;
26558 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
26559 result = PRAGMA_OMP_CLAUSE_PRIVATE;
26560 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26561 result = PRAGMA_OMP_CLAUSE_FOR;
26562 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26564 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26565 const char *p = IDENTIFIER_POINTER (id);
26567 switch (p[0])
26569 case 'a':
26570 if (!strcmp ("aligned", p))
26571 result = PRAGMA_OMP_CLAUSE_ALIGNED;
26572 break;
26573 case 'c':
26574 if (!strcmp ("collapse", p))
26575 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
26576 else if (!strcmp ("copyin", p))
26577 result = PRAGMA_OMP_CLAUSE_COPYIN;
26578 else if (!strcmp ("copyprivate", p))
26579 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
26580 break;
26581 case 'd':
26582 if (!strcmp ("depend", p))
26583 result = PRAGMA_OMP_CLAUSE_DEPEND;
26584 else if (!strcmp ("device", p))
26585 result = PRAGMA_OMP_CLAUSE_DEVICE;
26586 else if (!strcmp ("dist_schedule", p))
26587 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
26588 break;
26589 case 'f':
26590 if (!strcmp ("final", p))
26591 result = PRAGMA_OMP_CLAUSE_FINAL;
26592 else if (!strcmp ("firstprivate", p))
26593 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
26594 else if (!strcmp ("from", p))
26595 result = PRAGMA_OMP_CLAUSE_FROM;
26596 break;
26597 case 'i':
26598 if (!strcmp ("inbranch", p))
26599 result = PRAGMA_OMP_CLAUSE_INBRANCH;
26600 break;
26601 case 'l':
26602 if (!strcmp ("lastprivate", p))
26603 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
26604 else if (!strcmp ("linear", p))
26605 result = PRAGMA_OMP_CLAUSE_LINEAR;
26606 break;
26607 case 'm':
26608 if (!strcmp ("map", p))
26609 result = PRAGMA_OMP_CLAUSE_MAP;
26610 else if (!strcmp ("mergeable", p))
26611 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
26612 break;
26613 case 'n':
26614 if (!strcmp ("notinbranch", p))
26615 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
26616 else if (!strcmp ("nowait", p))
26617 result = PRAGMA_OMP_CLAUSE_NOWAIT;
26618 else if (!strcmp ("num_teams", p))
26619 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
26620 else if (!strcmp ("num_threads", p))
26621 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
26622 break;
26623 case 'o':
26624 if (!strcmp ("ordered", p))
26625 result = PRAGMA_OMP_CLAUSE_ORDERED;
26626 break;
26627 case 'p':
26628 if (!strcmp ("parallel", p))
26629 result = PRAGMA_OMP_CLAUSE_PARALLEL;
26630 else if (!strcmp ("proc_bind", p))
26631 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
26632 break;
26633 case 'r':
26634 if (!strcmp ("reduction", p))
26635 result = PRAGMA_OMP_CLAUSE_REDUCTION;
26636 break;
26637 case 's':
26638 if (!strcmp ("safelen", p))
26639 result = PRAGMA_OMP_CLAUSE_SAFELEN;
26640 else if (!strcmp ("schedule", p))
26641 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
26642 else if (!strcmp ("sections", p))
26643 result = PRAGMA_OMP_CLAUSE_SECTIONS;
26644 else if (!strcmp ("shared", p))
26645 result = PRAGMA_OMP_CLAUSE_SHARED;
26646 else if (!strcmp ("simdlen", p))
26647 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
26648 break;
26649 case 't':
26650 if (!strcmp ("taskgroup", p))
26651 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
26652 else if (!strcmp ("thread_limit", p))
26653 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
26654 else if (!strcmp ("to", p))
26655 result = PRAGMA_OMP_CLAUSE_TO;
26656 break;
26657 case 'u':
26658 if (!strcmp ("uniform", p))
26659 result = PRAGMA_OMP_CLAUSE_UNIFORM;
26660 else if (!strcmp ("untied", p))
26661 result = PRAGMA_OMP_CLAUSE_UNTIED;
26662 break;
26666 if (result != PRAGMA_OMP_CLAUSE_NONE)
26667 cp_lexer_consume_token (parser->lexer);
26669 return result;
26672 /* Validate that a clause of the given type does not already exist. */
26674 static void
26675 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
26676 const char *name, location_t location)
26678 tree c;
26680 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26681 if (OMP_CLAUSE_CODE (c) == code)
26683 error_at (location, "too many %qs clauses", name);
26684 break;
26688 /* OpenMP 2.5:
26689 variable-list:
26690 identifier
26691 variable-list , identifier
26693 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
26694 colon). An opening parenthesis will have been consumed by the caller.
26696 If KIND is nonzero, create the appropriate node and install the decl
26697 in OMP_CLAUSE_DECL and add the node to the head of the list.
26699 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
26700 return the list created.
26702 COLON can be NULL if only closing parenthesis should end the list,
26703 or pointer to bool which will receive false if the list is terminated
26704 by closing parenthesis or true if the list is terminated by colon. */
26706 static tree
26707 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
26708 tree list, bool *colon)
26710 cp_token *token;
26711 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26712 if (colon)
26714 parser->colon_corrects_to_scope_p = false;
26715 *colon = false;
26717 while (1)
26719 tree name, decl;
26721 token = cp_lexer_peek_token (parser->lexer);
26722 name = cp_parser_id_expression (parser, /*template_p=*/false,
26723 /*check_dependency_p=*/true,
26724 /*template_p=*/NULL,
26725 /*declarator_p=*/false,
26726 /*optional_p=*/false);
26727 if (name == error_mark_node)
26728 goto skip_comma;
26730 decl = cp_parser_lookup_name_simple (parser, name, token->location);
26731 if (decl == error_mark_node)
26732 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
26733 token->location);
26734 else if (kind != 0)
26736 switch (kind)
26738 case OMP_CLAUSE_MAP:
26739 case OMP_CLAUSE_FROM:
26740 case OMP_CLAUSE_TO:
26741 case OMP_CLAUSE_DEPEND:
26742 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
26744 tree low_bound = NULL_TREE, length = NULL_TREE;
26746 parser->colon_corrects_to_scope_p = false;
26747 cp_lexer_consume_token (parser->lexer);
26748 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26749 low_bound = cp_parser_expression (parser, /*cast_p=*/false,
26750 NULL);
26751 if (!colon)
26752 parser->colon_corrects_to_scope_p
26753 = saved_colon_corrects_to_scope_p;
26754 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
26755 length = integer_one_node;
26756 else
26758 /* Look for `:'. */
26759 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26760 goto skip_comma;
26761 if (!cp_lexer_next_token_is (parser->lexer,
26762 CPP_CLOSE_SQUARE))
26763 length = cp_parser_expression (parser,
26764 /*cast_p=*/false,
26765 NULL);
26767 /* Look for the closing `]'. */
26768 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
26769 RT_CLOSE_SQUARE))
26770 goto skip_comma;
26771 decl = tree_cons (low_bound, length, decl);
26773 break;
26774 default:
26775 break;
26778 tree u = build_omp_clause (token->location, kind);
26779 OMP_CLAUSE_DECL (u) = decl;
26780 OMP_CLAUSE_CHAIN (u) = list;
26781 list = u;
26783 else
26784 list = tree_cons (decl, NULL_TREE, list);
26786 get_comma:
26787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
26788 break;
26789 cp_lexer_consume_token (parser->lexer);
26792 if (colon)
26793 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
26795 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26797 *colon = true;
26798 cp_parser_require (parser, CPP_COLON, RT_COLON);
26799 return list;
26802 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26804 int ending;
26806 /* Try to resync to an unnested comma. Copied from
26807 cp_parser_parenthesized_expression_list. */
26808 skip_comma:
26809 if (colon)
26810 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
26811 ending = cp_parser_skip_to_closing_parenthesis (parser,
26812 /*recovering=*/true,
26813 /*or_comma=*/true,
26814 /*consume_paren=*/true);
26815 if (ending < 0)
26816 goto get_comma;
26819 return list;
26822 /* Similarly, but expect leading and trailing parenthesis. This is a very
26823 common case for omp clauses. */
26825 static tree
26826 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
26828 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26829 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
26830 return list;
26833 /* OpenMP 3.0:
26834 collapse ( constant-expression ) */
26836 static tree
26837 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
26839 tree c, num;
26840 location_t loc;
26841 HOST_WIDE_INT n;
26843 loc = cp_lexer_peek_token (parser->lexer)->location;
26844 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26845 return list;
26847 num = cp_parser_constant_expression (parser, false, NULL);
26849 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26850 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26851 /*or_comma=*/false,
26852 /*consume_paren=*/true);
26854 if (num == error_mark_node)
26855 return list;
26856 num = fold_non_dependent_expr (num);
26857 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
26858 || !host_integerp (num, 0)
26859 || (n = tree_low_cst (num, 0)) <= 0
26860 || (int) n != n)
26862 error_at (loc, "collapse argument needs positive constant integer expression");
26863 return list;
26866 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
26867 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
26868 OMP_CLAUSE_CHAIN (c) = list;
26869 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
26871 return c;
26874 /* OpenMP 2.5:
26875 default ( shared | none ) */
26877 static tree
26878 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
26880 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
26881 tree c;
26883 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26884 return list;
26885 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26887 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26888 const char *p = IDENTIFIER_POINTER (id);
26890 switch (p[0])
26892 case 'n':
26893 if (strcmp ("none", p) != 0)
26894 goto invalid_kind;
26895 kind = OMP_CLAUSE_DEFAULT_NONE;
26896 break;
26898 case 's':
26899 if (strcmp ("shared", p) != 0)
26900 goto invalid_kind;
26901 kind = OMP_CLAUSE_DEFAULT_SHARED;
26902 break;
26904 default:
26905 goto invalid_kind;
26908 cp_lexer_consume_token (parser->lexer);
26910 else
26912 invalid_kind:
26913 cp_parser_error (parser, "expected %<none%> or %<shared%>");
26916 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26917 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26918 /*or_comma=*/false,
26919 /*consume_paren=*/true);
26921 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
26922 return list;
26924 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
26925 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
26926 OMP_CLAUSE_CHAIN (c) = list;
26927 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
26929 return c;
26932 /* OpenMP 3.1:
26933 final ( expression ) */
26935 static tree
26936 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
26938 tree t, c;
26940 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26941 return list;
26943 t = cp_parser_condition (parser);
26945 if (t == error_mark_node
26946 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26947 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26948 /*or_comma=*/false,
26949 /*consume_paren=*/true);
26951 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
26953 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
26954 OMP_CLAUSE_FINAL_EXPR (c) = t;
26955 OMP_CLAUSE_CHAIN (c) = list;
26957 return c;
26960 /* OpenMP 2.5:
26961 if ( expression ) */
26963 static tree
26964 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
26966 tree t, c;
26968 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26969 return list;
26971 t = cp_parser_condition (parser);
26973 if (t == error_mark_node
26974 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26975 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26976 /*or_comma=*/false,
26977 /*consume_paren=*/true);
26979 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
26981 c = build_omp_clause (location, OMP_CLAUSE_IF);
26982 OMP_CLAUSE_IF_EXPR (c) = t;
26983 OMP_CLAUSE_CHAIN (c) = list;
26985 return c;
26988 /* OpenMP 3.1:
26989 mergeable */
26991 static tree
26992 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
26993 tree list, location_t location)
26995 tree c;
26997 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
26998 location);
27000 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27001 OMP_CLAUSE_CHAIN (c) = list;
27002 return c;
27005 /* OpenMP 2.5:
27006 nowait */
27008 static tree
27009 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27010 tree list, location_t location)
27012 tree c;
27014 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27016 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27017 OMP_CLAUSE_CHAIN (c) = list;
27018 return c;
27021 /* OpenMP 2.5:
27022 num_threads ( expression ) */
27024 static tree
27025 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27026 location_t location)
27028 tree t, c;
27030 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27031 return list;
27033 t = cp_parser_expression (parser, false, NULL);
27035 if (t == error_mark_node
27036 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27037 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27038 /*or_comma=*/false,
27039 /*consume_paren=*/true);
27041 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27042 "num_threads", location);
27044 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27045 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27046 OMP_CLAUSE_CHAIN (c) = list;
27048 return c;
27051 /* OpenMP 2.5:
27052 ordered */
27054 static tree
27055 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27056 tree list, location_t location)
27058 tree c;
27060 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27061 "ordered", location);
27063 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27064 OMP_CLAUSE_CHAIN (c) = list;
27065 return c;
27068 /* OpenMP 2.5:
27069 reduction ( reduction-operator : variable-list )
27071 reduction-operator:
27072 One of: + * - & ^ | && ||
27074 OpenMP 3.1:
27076 reduction-operator:
27077 One of: + * - & ^ | && || min max
27079 OpenMP 4.0:
27081 reduction-operator:
27082 One of: + * - & ^ | && ||
27083 id-expression */
27085 static tree
27086 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27088 enum tree_code code = ERROR_MARK;
27089 tree nlist, c, id = NULL_TREE;
27091 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27092 return list;
27094 switch (cp_lexer_peek_token (parser->lexer)->type)
27096 case CPP_PLUS: code = PLUS_EXPR; break;
27097 case CPP_MULT: code = MULT_EXPR; break;
27098 case CPP_MINUS: code = MINUS_EXPR; break;
27099 case CPP_AND: code = BIT_AND_EXPR; break;
27100 case CPP_XOR: code = BIT_XOR_EXPR; break;
27101 case CPP_OR: code = BIT_IOR_EXPR; break;
27102 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27103 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27104 default: break;
27107 if (code != ERROR_MARK)
27108 cp_lexer_consume_token (parser->lexer);
27109 else
27111 bool saved_colon_corrects_to_scope_p;
27112 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27113 parser->colon_corrects_to_scope_p = false;
27114 id = cp_parser_id_expression (parser, /*template_p=*/false,
27115 /*check_dependency_p=*/true,
27116 /*template_p=*/NULL,
27117 /*declarator_p=*/false,
27118 /*optional_p=*/false);
27119 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27120 if (identifier_p (id))
27122 const char *p = IDENTIFIER_POINTER (id);
27124 if (strcmp (p, "min") == 0)
27125 code = MIN_EXPR;
27126 else if (strcmp (p, "max") == 0)
27127 code = MAX_EXPR;
27128 else if (id == ansi_opname (PLUS_EXPR))
27129 code = PLUS_EXPR;
27130 else if (id == ansi_opname (MULT_EXPR))
27131 code = MULT_EXPR;
27132 else if (id == ansi_opname (MINUS_EXPR))
27133 code = MINUS_EXPR;
27134 else if (id == ansi_opname (BIT_AND_EXPR))
27135 code = BIT_AND_EXPR;
27136 else if (id == ansi_opname (BIT_IOR_EXPR))
27137 code = BIT_IOR_EXPR;
27138 else if (id == ansi_opname (BIT_XOR_EXPR))
27139 code = BIT_XOR_EXPR;
27140 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27141 code = TRUTH_ANDIF_EXPR;
27142 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27143 code = TRUTH_ORIF_EXPR;
27144 id = omp_reduction_id (code, id, NULL_TREE);
27145 tree scope = parser->scope;
27146 if (scope)
27147 id = build_qualified_name (NULL_TREE, scope, id, false);
27148 parser->scope = NULL_TREE;
27149 parser->qualifying_scope = NULL_TREE;
27150 parser->object_scope = NULL_TREE;
27152 else
27154 error ("invalid reduction-identifier");
27155 resync_fail:
27156 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27157 /*or_comma=*/false,
27158 /*consume_paren=*/true);
27159 return list;
27163 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27164 goto resync_fail;
27166 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27167 NULL);
27168 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27170 OMP_CLAUSE_REDUCTION_CODE (c) = code;
27171 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27174 return nlist;
27177 /* OpenMP 2.5:
27178 schedule ( schedule-kind )
27179 schedule ( schedule-kind , expression )
27181 schedule-kind:
27182 static | dynamic | guided | runtime | auto */
27184 static tree
27185 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27187 tree c, t;
27189 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27190 return list;
27192 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27194 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27196 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27197 const char *p = IDENTIFIER_POINTER (id);
27199 switch (p[0])
27201 case 'd':
27202 if (strcmp ("dynamic", p) != 0)
27203 goto invalid_kind;
27204 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27205 break;
27207 case 'g':
27208 if (strcmp ("guided", p) != 0)
27209 goto invalid_kind;
27210 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27211 break;
27213 case 'r':
27214 if (strcmp ("runtime", p) != 0)
27215 goto invalid_kind;
27216 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27217 break;
27219 default:
27220 goto invalid_kind;
27223 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27224 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27225 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27226 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27227 else
27228 goto invalid_kind;
27229 cp_lexer_consume_token (parser->lexer);
27231 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27233 cp_token *token;
27234 cp_lexer_consume_token (parser->lexer);
27236 token = cp_lexer_peek_token (parser->lexer);
27237 t = cp_parser_assignment_expression (parser, false, NULL);
27239 if (t == error_mark_node)
27240 goto resync_fail;
27241 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
27242 error_at (token->location, "schedule %<runtime%> does not take "
27243 "a %<chunk_size%> parameter");
27244 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
27245 error_at (token->location, "schedule %<auto%> does not take "
27246 "a %<chunk_size%> parameter");
27247 else
27248 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
27250 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27251 goto resync_fail;
27253 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27254 goto resync_fail;
27256 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
27257 OMP_CLAUSE_CHAIN (c) = list;
27258 return c;
27260 invalid_kind:
27261 cp_parser_error (parser, "invalid schedule kind");
27262 resync_fail:
27263 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27264 /*or_comma=*/false,
27265 /*consume_paren=*/true);
27266 return list;
27269 /* OpenMP 3.0:
27270 untied */
27272 static tree
27273 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
27274 tree list, location_t location)
27276 tree c;
27278 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
27280 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
27281 OMP_CLAUSE_CHAIN (c) = list;
27282 return c;
27285 /* OpenMP 4.0:
27286 inbranch
27287 notinbranch */
27289 static tree
27290 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
27291 tree list, location_t location)
27293 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
27294 tree c = build_omp_clause (location, code);
27295 OMP_CLAUSE_CHAIN (c) = list;
27296 return c;
27299 /* OpenMP 4.0:
27300 parallel
27302 sections
27303 taskgroup */
27305 static tree
27306 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
27307 enum omp_clause_code code,
27308 tree list, location_t location)
27310 tree c = build_omp_clause (location, code);
27311 OMP_CLAUSE_CHAIN (c) = list;
27312 return c;
27315 /* OpenMP 4.0:
27316 num_teams ( expression ) */
27318 static tree
27319 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
27320 location_t location)
27322 tree t, c;
27324 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27325 return list;
27327 t = cp_parser_expression (parser, false, NULL);
27329 if (t == error_mark_node
27330 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27331 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27332 /*or_comma=*/false,
27333 /*consume_paren=*/true);
27335 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
27336 "num_teams", location);
27338 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
27339 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
27340 OMP_CLAUSE_CHAIN (c) = list;
27342 return c;
27345 /* OpenMP 4.0:
27346 thread_limit ( expression ) */
27348 static tree
27349 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
27350 location_t location)
27352 tree t, c;
27354 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27355 return list;
27357 t = cp_parser_expression (parser, false, NULL);
27359 if (t == error_mark_node
27360 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27361 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27362 /*or_comma=*/false,
27363 /*consume_paren=*/true);
27365 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
27366 "thread_limit", location);
27368 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
27369 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
27370 OMP_CLAUSE_CHAIN (c) = list;
27372 return c;
27375 /* OpenMP 4.0:
27376 aligned ( variable-list )
27377 aligned ( variable-list : constant-expression ) */
27379 static tree
27380 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
27382 tree nlist, c, alignment = NULL_TREE;
27383 bool colon;
27385 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27386 return list;
27388 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
27389 &colon);
27391 if (colon)
27393 alignment = cp_parser_constant_expression (parser, false, NULL);
27395 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27396 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27397 /*or_comma=*/false,
27398 /*consume_paren=*/true);
27400 if (alignment == error_mark_node)
27401 alignment = NULL_TREE;
27404 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27405 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
27407 return nlist;
27410 /* OpenMP 4.0:
27411 linear ( variable-list )
27412 linear ( variable-list : expression ) */
27414 static tree
27415 cp_parser_omp_clause_linear (cp_parser *parser, tree list)
27417 tree nlist, c, step = integer_one_node;
27418 bool colon;
27420 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27421 return list;
27423 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
27424 &colon);
27426 if (colon)
27428 step = cp_parser_expression (parser, false, NULL);
27430 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27431 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27432 /*or_comma=*/false,
27433 /*consume_paren=*/true);
27435 if (step == error_mark_node)
27436 return list;
27439 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27440 OMP_CLAUSE_LINEAR_STEP (c) = step;
27442 return nlist;
27445 /* OpenMP 4.0:
27446 safelen ( constant-expression ) */
27448 static tree
27449 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
27450 location_t location)
27452 tree t, c;
27454 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27455 return list;
27457 t = cp_parser_constant_expression (parser, false, NULL);
27459 if (t == error_mark_node
27460 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27461 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27462 /*or_comma=*/false,
27463 /*consume_paren=*/true);
27465 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
27467 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
27468 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
27469 OMP_CLAUSE_CHAIN (c) = list;
27471 return c;
27474 /* OpenMP 4.0:
27475 simdlen ( constant-expression ) */
27477 static tree
27478 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
27479 location_t location)
27481 tree t, c;
27483 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27484 return list;
27486 t = cp_parser_constant_expression (parser, false, NULL);
27488 if (t == error_mark_node
27489 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27490 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27491 /*or_comma=*/false,
27492 /*consume_paren=*/true);
27494 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
27496 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
27497 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
27498 OMP_CLAUSE_CHAIN (c) = list;
27500 return c;
27503 /* OpenMP 4.0:
27504 depend ( depend-kind : variable-list )
27506 depend-kind:
27507 in | out | inout */
27509 static tree
27510 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
27512 tree nlist, c;
27513 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
27515 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27516 return list;
27518 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27520 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27521 const char *p = IDENTIFIER_POINTER (id);
27523 if (strcmp ("in", p) == 0)
27524 kind = OMP_CLAUSE_DEPEND_IN;
27525 else if (strcmp ("inout", p) == 0)
27526 kind = OMP_CLAUSE_DEPEND_INOUT;
27527 else if (strcmp ("out", p) == 0)
27528 kind = OMP_CLAUSE_DEPEND_OUT;
27529 else
27530 goto invalid_kind;
27532 else
27533 goto invalid_kind;
27535 cp_lexer_consume_token (parser->lexer);
27536 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27537 goto resync_fail;
27539 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
27540 NULL);
27542 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27543 OMP_CLAUSE_DEPEND_KIND (c) = kind;
27545 return nlist;
27547 invalid_kind:
27548 cp_parser_error (parser, "invalid depend kind");
27549 resync_fail:
27550 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27551 /*or_comma=*/false,
27552 /*consume_paren=*/true);
27553 return list;
27556 /* OpenMP 4.0:
27557 map ( map-kind : variable-list )
27558 map ( variable-list )
27560 map-kind:
27561 alloc | to | from | tofrom */
27563 static tree
27564 cp_parser_omp_clause_map (cp_parser *parser, tree list)
27566 tree nlist, c;
27567 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
27569 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27570 return list;
27572 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
27573 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
27575 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27576 const char *p = IDENTIFIER_POINTER (id);
27578 if (strcmp ("alloc", p) == 0)
27579 kind = OMP_CLAUSE_MAP_ALLOC;
27580 else if (strcmp ("to", p) == 0)
27581 kind = OMP_CLAUSE_MAP_TO;
27582 else if (strcmp ("from", p) == 0)
27583 kind = OMP_CLAUSE_MAP_FROM;
27584 else if (strcmp ("tofrom", p) == 0)
27585 kind = OMP_CLAUSE_MAP_TOFROM;
27586 else
27588 cp_parser_error (parser, "invalid map kind");
27589 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27590 /*or_comma=*/false,
27591 /*consume_paren=*/true);
27592 return list;
27594 cp_lexer_consume_token (parser->lexer);
27595 cp_lexer_consume_token (parser->lexer);
27598 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
27599 NULL);
27601 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27602 OMP_CLAUSE_MAP_KIND (c) = kind;
27604 return nlist;
27607 /* OpenMP 4.0:
27608 device ( expression ) */
27610 static tree
27611 cp_parser_omp_clause_device (cp_parser *parser, tree list,
27612 location_t location)
27614 tree t, c;
27616 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27617 return list;
27619 t = cp_parser_expression (parser, false, NULL);
27621 if (t == error_mark_node
27622 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27623 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27624 /*or_comma=*/false,
27625 /*consume_paren=*/true);
27627 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
27628 "device", location);
27630 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
27631 OMP_CLAUSE_DEVICE_ID (c) = t;
27632 OMP_CLAUSE_CHAIN (c) = list;
27634 return c;
27637 /* OpenMP 4.0:
27638 dist_schedule ( static )
27639 dist_schedule ( static , expression ) */
27641 static tree
27642 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
27643 location_t location)
27645 tree c, t;
27647 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27648 return list;
27650 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
27652 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27653 goto invalid_kind;
27654 cp_lexer_consume_token (parser->lexer);
27656 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27658 cp_lexer_consume_token (parser->lexer);
27660 t = cp_parser_assignment_expression (parser, false, NULL);
27662 if (t == error_mark_node)
27663 goto resync_fail;
27664 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
27666 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27667 goto resync_fail;
27669 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27670 goto resync_fail;
27672 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
27673 location);
27674 OMP_CLAUSE_CHAIN (c) = list;
27675 return c;
27677 invalid_kind:
27678 cp_parser_error (parser, "invalid dist_schedule kind");
27679 resync_fail:
27680 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27681 /*or_comma=*/false,
27682 /*consume_paren=*/true);
27683 return list;
27686 /* OpenMP 4.0:
27687 proc_bind ( proc-bind-kind )
27689 proc-bind-kind:
27690 master | close | spread */
27692 static tree
27693 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
27694 location_t location)
27696 tree c;
27697 enum omp_clause_proc_bind_kind kind;
27699 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27700 return list;
27702 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27704 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27705 const char *p = IDENTIFIER_POINTER (id);
27707 if (strcmp ("master", p) == 0)
27708 kind = OMP_CLAUSE_PROC_BIND_MASTER;
27709 else if (strcmp ("close", p) == 0)
27710 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
27711 else if (strcmp ("spread", p) == 0)
27712 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
27713 else
27714 goto invalid_kind;
27716 else
27717 goto invalid_kind;
27719 cp_lexer_consume_token (parser->lexer);
27720 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27721 goto resync_fail;
27723 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
27724 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
27725 location);
27726 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
27727 OMP_CLAUSE_CHAIN (c) = list;
27728 return c;
27730 invalid_kind:
27731 cp_parser_error (parser, "invalid depend kind");
27732 resync_fail:
27733 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27734 /*or_comma=*/false,
27735 /*consume_paren=*/true);
27736 return list;
27739 /* Parse all OpenMP clauses. The set clauses allowed by the directive
27740 is a bitmask in MASK. Return the list of clauses found; the result
27741 of clause default goes in *pdefault. */
27743 static tree
27744 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
27745 const char *where, cp_token *pragma_tok,
27746 bool finish_p = true)
27748 tree clauses = NULL;
27749 bool first = true;
27750 cp_token *token = NULL;
27752 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
27754 pragma_omp_clause c_kind;
27755 const char *c_name;
27756 tree prev = clauses;
27758 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27759 cp_lexer_consume_token (parser->lexer);
27761 token = cp_lexer_peek_token (parser->lexer);
27762 c_kind = cp_parser_omp_clause_name (parser);
27764 switch (c_kind)
27766 case PRAGMA_OMP_CLAUSE_COLLAPSE:
27767 clauses = cp_parser_omp_clause_collapse (parser, clauses,
27768 token->location);
27769 c_name = "collapse";
27770 break;
27771 case PRAGMA_OMP_CLAUSE_COPYIN:
27772 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
27773 c_name = "copyin";
27774 break;
27775 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
27776 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
27777 clauses);
27778 c_name = "copyprivate";
27779 break;
27780 case PRAGMA_OMP_CLAUSE_DEFAULT:
27781 clauses = cp_parser_omp_clause_default (parser, clauses,
27782 token->location);
27783 c_name = "default";
27784 break;
27785 case PRAGMA_OMP_CLAUSE_FINAL:
27786 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
27787 c_name = "final";
27788 break;
27789 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
27790 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
27791 clauses);
27792 c_name = "firstprivate";
27793 break;
27794 case PRAGMA_OMP_CLAUSE_IF:
27795 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
27796 c_name = "if";
27797 break;
27798 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
27799 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
27800 clauses);
27801 c_name = "lastprivate";
27802 break;
27803 case PRAGMA_OMP_CLAUSE_MERGEABLE:
27804 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
27805 token->location);
27806 c_name = "mergeable";
27807 break;
27808 case PRAGMA_OMP_CLAUSE_NOWAIT:
27809 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
27810 c_name = "nowait";
27811 break;
27812 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
27813 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
27814 token->location);
27815 c_name = "num_threads";
27816 break;
27817 case PRAGMA_OMP_CLAUSE_ORDERED:
27818 clauses = cp_parser_omp_clause_ordered (parser, clauses,
27819 token->location);
27820 c_name = "ordered";
27821 break;
27822 case PRAGMA_OMP_CLAUSE_PRIVATE:
27823 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
27824 clauses);
27825 c_name = "private";
27826 break;
27827 case PRAGMA_OMP_CLAUSE_REDUCTION:
27828 clauses = cp_parser_omp_clause_reduction (parser, clauses);
27829 c_name = "reduction";
27830 break;
27831 case PRAGMA_OMP_CLAUSE_SCHEDULE:
27832 clauses = cp_parser_omp_clause_schedule (parser, clauses,
27833 token->location);
27834 c_name = "schedule";
27835 break;
27836 case PRAGMA_OMP_CLAUSE_SHARED:
27837 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
27838 clauses);
27839 c_name = "shared";
27840 break;
27841 case PRAGMA_OMP_CLAUSE_UNTIED:
27842 clauses = cp_parser_omp_clause_untied (parser, clauses,
27843 token->location);
27844 c_name = "untied";
27845 break;
27846 case PRAGMA_OMP_CLAUSE_INBRANCH:
27847 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
27848 clauses, token->location);
27849 c_name = "inbranch";
27850 break;
27851 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
27852 clauses = cp_parser_omp_clause_branch (parser,
27853 OMP_CLAUSE_NOTINBRANCH,
27854 clauses, token->location);
27855 c_name = "notinbranch";
27856 break;
27857 case PRAGMA_OMP_CLAUSE_PARALLEL:
27858 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
27859 clauses, token->location);
27860 c_name = "parallel";
27861 if (!first)
27863 clause_not_first:
27864 error_at (token->location, "%qs must be the first clause of %qs",
27865 c_name, where);
27866 clauses = prev;
27868 break;
27869 case PRAGMA_OMP_CLAUSE_FOR:
27870 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
27871 clauses, token->location);
27872 c_name = "for";
27873 if (!first)
27874 goto clause_not_first;
27875 break;
27876 case PRAGMA_OMP_CLAUSE_SECTIONS:
27877 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
27878 clauses, token->location);
27879 c_name = "sections";
27880 if (!first)
27881 goto clause_not_first;
27882 break;
27883 case PRAGMA_OMP_CLAUSE_TASKGROUP:
27884 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
27885 clauses, token->location);
27886 c_name = "taskgroup";
27887 if (!first)
27888 goto clause_not_first;
27889 break;
27890 case PRAGMA_OMP_CLAUSE_TO:
27891 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
27892 clauses);
27893 c_name = "to";
27894 break;
27895 case PRAGMA_OMP_CLAUSE_FROM:
27896 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
27897 clauses);
27898 c_name = "from";
27899 break;
27900 case PRAGMA_OMP_CLAUSE_UNIFORM:
27901 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
27902 clauses);
27903 c_name = "uniform";
27904 break;
27905 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
27906 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
27907 token->location);
27908 c_name = "num_teams";
27909 break;
27910 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
27911 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
27912 token->location);
27913 c_name = "thread_limit";
27914 break;
27915 case PRAGMA_OMP_CLAUSE_ALIGNED:
27916 clauses = cp_parser_omp_clause_aligned (parser, clauses);
27917 c_name = "aligned";
27918 break;
27919 case PRAGMA_OMP_CLAUSE_LINEAR:
27920 clauses = cp_parser_omp_clause_linear (parser, clauses);
27921 c_name = "linear";
27922 break;
27923 case PRAGMA_OMP_CLAUSE_DEPEND:
27924 clauses = cp_parser_omp_clause_depend (parser, clauses);
27925 c_name = "depend";
27926 break;
27927 case PRAGMA_OMP_CLAUSE_MAP:
27928 clauses = cp_parser_omp_clause_map (parser, clauses);
27929 c_name = "map";
27930 break;
27931 case PRAGMA_OMP_CLAUSE_DEVICE:
27932 clauses = cp_parser_omp_clause_device (parser, clauses,
27933 token->location);
27934 c_name = "device";
27935 break;
27936 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
27937 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
27938 token->location);
27939 c_name = "dist_schedule";
27940 break;
27941 case PRAGMA_OMP_CLAUSE_PROC_BIND:
27942 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
27943 token->location);
27944 c_name = "proc_bind";
27945 break;
27946 case PRAGMA_OMP_CLAUSE_SAFELEN:
27947 clauses = cp_parser_omp_clause_safelen (parser, clauses,
27948 token->location);
27949 c_name = "safelen";
27950 break;
27951 case PRAGMA_OMP_CLAUSE_SIMDLEN:
27952 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
27953 token->location);
27954 c_name = "simdlen";
27955 break;
27956 default:
27957 cp_parser_error (parser, "expected %<#pragma omp%> clause");
27958 goto saw_error;
27961 first = false;
27963 if (((mask >> c_kind) & 1) == 0)
27965 /* Remove the invalid clause(s) from the list to avoid
27966 confusing the rest of the compiler. */
27967 clauses = prev;
27968 error_at (token->location, "%qs is not valid for %qs", c_name, where);
27971 saw_error:
27972 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27973 if (finish_p)
27974 return finish_omp_clauses (clauses);
27975 return clauses;
27978 /* OpenMP 2.5:
27979 structured-block:
27980 statement
27982 In practice, we're also interested in adding the statement to an
27983 outer node. So it is convenient if we work around the fact that
27984 cp_parser_statement calls add_stmt. */
27986 static unsigned
27987 cp_parser_begin_omp_structured_block (cp_parser *parser)
27989 unsigned save = parser->in_statement;
27991 /* Only move the values to IN_OMP_BLOCK if they weren't false.
27992 This preserves the "not within loop or switch" style error messages
27993 for nonsense cases like
27994 void foo() {
27995 #pragma omp single
27996 break;
27999 if (parser->in_statement)
28000 parser->in_statement = IN_OMP_BLOCK;
28002 return save;
28005 static void
28006 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28008 parser->in_statement = save;
28011 static tree
28012 cp_parser_omp_structured_block (cp_parser *parser)
28014 tree stmt = begin_omp_structured_block ();
28015 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28017 cp_parser_statement (parser, NULL_TREE, false, NULL);
28019 cp_parser_end_omp_structured_block (parser, save);
28020 return finish_omp_structured_block (stmt);
28023 /* OpenMP 2.5:
28024 # pragma omp atomic new-line
28025 expression-stmt
28027 expression-stmt:
28028 x binop= expr | x++ | ++x | x-- | --x
28029 binop:
28030 +, *, -, /, &, ^, |, <<, >>
28032 where x is an lvalue expression with scalar type.
28034 OpenMP 3.1:
28035 # pragma omp atomic new-line
28036 update-stmt
28038 # pragma omp atomic read new-line
28039 read-stmt
28041 # pragma omp atomic write new-line
28042 write-stmt
28044 # pragma omp atomic update new-line
28045 update-stmt
28047 # pragma omp atomic capture new-line
28048 capture-stmt
28050 # pragma omp atomic capture new-line
28051 capture-block
28053 read-stmt:
28054 v = x
28055 write-stmt:
28056 x = expr
28057 update-stmt:
28058 expression-stmt | x = x binop expr
28059 capture-stmt:
28060 v = expression-stmt
28061 capture-block:
28062 { v = x; update-stmt; } | { update-stmt; v = x; }
28064 OpenMP 4.0:
28065 update-stmt:
28066 expression-stmt | x = x binop expr | x = expr binop x
28067 capture-stmt:
28068 v = update-stmt
28069 capture-block:
28070 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28072 where x and v are lvalue expressions with scalar type. */
28074 static void
28075 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28077 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28078 tree rhs1 = NULL_TREE, orig_lhs;
28079 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28080 bool structured_block = false;
28081 bool seq_cst = false;
28083 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28085 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28086 const char *p = IDENTIFIER_POINTER (id);
28088 if (!strcmp (p, "read"))
28089 code = OMP_ATOMIC_READ;
28090 else if (!strcmp (p, "write"))
28091 code = NOP_EXPR;
28092 else if (!strcmp (p, "update"))
28093 code = OMP_ATOMIC;
28094 else if (!strcmp (p, "capture"))
28095 code = OMP_ATOMIC_CAPTURE_NEW;
28096 else
28097 p = NULL;
28098 if (p)
28099 cp_lexer_consume_token (parser->lexer);
28102 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28104 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28105 const char *p = IDENTIFIER_POINTER (id);
28107 if (!strcmp (p, "seq_cst"))
28109 seq_cst = true;
28110 cp_lexer_consume_token (parser->lexer);
28113 cp_parser_require_pragma_eol (parser, pragma_tok);
28115 switch (code)
28117 case OMP_ATOMIC_READ:
28118 case NOP_EXPR: /* atomic write */
28119 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28120 /*cast_p=*/false, NULL);
28121 if (v == error_mark_node)
28122 goto saw_error;
28123 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28124 goto saw_error;
28125 if (code == NOP_EXPR)
28126 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28127 else
28128 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28129 /*cast_p=*/false, NULL);
28130 if (lhs == error_mark_node)
28131 goto saw_error;
28132 if (code == NOP_EXPR)
28134 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28135 opcode. */
28136 code = OMP_ATOMIC;
28137 rhs = lhs;
28138 lhs = v;
28139 v = NULL_TREE;
28141 goto done;
28142 case OMP_ATOMIC_CAPTURE_NEW:
28143 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28145 cp_lexer_consume_token (parser->lexer);
28146 structured_block = true;
28148 else
28150 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28151 /*cast_p=*/false, NULL);
28152 if (v == error_mark_node)
28153 goto saw_error;
28154 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28155 goto saw_error;
28157 default:
28158 break;
28161 restart:
28162 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28163 /*cast_p=*/false, NULL);
28164 orig_lhs = lhs;
28165 switch (TREE_CODE (lhs))
28167 case ERROR_MARK:
28168 goto saw_error;
28170 case POSTINCREMENT_EXPR:
28171 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28172 code = OMP_ATOMIC_CAPTURE_OLD;
28173 /* FALLTHROUGH */
28174 case PREINCREMENT_EXPR:
28175 lhs = TREE_OPERAND (lhs, 0);
28176 opcode = PLUS_EXPR;
28177 rhs = integer_one_node;
28178 break;
28180 case POSTDECREMENT_EXPR:
28181 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28182 code = OMP_ATOMIC_CAPTURE_OLD;
28183 /* FALLTHROUGH */
28184 case PREDECREMENT_EXPR:
28185 lhs = TREE_OPERAND (lhs, 0);
28186 opcode = MINUS_EXPR;
28187 rhs = integer_one_node;
28188 break;
28190 case COMPOUND_EXPR:
28191 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
28192 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
28193 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
28194 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
28195 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28196 (TREE_OPERAND (lhs, 1), 0), 0)))
28197 == BOOLEAN_TYPE)
28198 /* Undo effects of boolean_increment for post {in,de}crement. */
28199 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
28200 /* FALLTHRU */
28201 case MODIFY_EXPR:
28202 if (TREE_CODE (lhs) == MODIFY_EXPR
28203 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
28205 /* Undo effects of boolean_increment. */
28206 if (integer_onep (TREE_OPERAND (lhs, 1)))
28208 /* This is pre or post increment. */
28209 rhs = TREE_OPERAND (lhs, 1);
28210 lhs = TREE_OPERAND (lhs, 0);
28211 opcode = NOP_EXPR;
28212 if (code == OMP_ATOMIC_CAPTURE_NEW
28213 && !structured_block
28214 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
28215 code = OMP_ATOMIC_CAPTURE_OLD;
28216 break;
28219 /* FALLTHRU */
28220 default:
28221 switch (cp_lexer_peek_token (parser->lexer)->type)
28223 case CPP_MULT_EQ:
28224 opcode = MULT_EXPR;
28225 break;
28226 case CPP_DIV_EQ:
28227 opcode = TRUNC_DIV_EXPR;
28228 break;
28229 case CPP_PLUS_EQ:
28230 opcode = PLUS_EXPR;
28231 break;
28232 case CPP_MINUS_EQ:
28233 opcode = MINUS_EXPR;
28234 break;
28235 case CPP_LSHIFT_EQ:
28236 opcode = LSHIFT_EXPR;
28237 break;
28238 case CPP_RSHIFT_EQ:
28239 opcode = RSHIFT_EXPR;
28240 break;
28241 case CPP_AND_EQ:
28242 opcode = BIT_AND_EXPR;
28243 break;
28244 case CPP_OR_EQ:
28245 opcode = BIT_IOR_EXPR;
28246 break;
28247 case CPP_XOR_EQ:
28248 opcode = BIT_XOR_EXPR;
28249 break;
28250 case CPP_EQ:
28251 enum cp_parser_prec oprec;
28252 cp_token *token;
28253 cp_lexer_consume_token (parser->lexer);
28254 cp_parser_parse_tentatively (parser);
28255 rhs1 = cp_parser_simple_cast_expression (parser);
28256 if (rhs1 == error_mark_node)
28258 cp_parser_abort_tentative_parse (parser);
28259 cp_parser_simple_cast_expression (parser);
28260 goto saw_error;
28262 token = cp_lexer_peek_token (parser->lexer);
28263 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
28265 cp_parser_abort_tentative_parse (parser);
28266 cp_parser_parse_tentatively (parser);
28267 rhs = cp_parser_binary_expression (parser, false, true,
28268 PREC_NOT_OPERATOR, NULL);
28269 if (rhs == error_mark_node)
28271 cp_parser_abort_tentative_parse (parser);
28272 cp_parser_binary_expression (parser, false, true,
28273 PREC_NOT_OPERATOR, NULL);
28274 goto saw_error;
28276 switch (TREE_CODE (rhs))
28278 case MULT_EXPR:
28279 case TRUNC_DIV_EXPR:
28280 case PLUS_EXPR:
28281 case MINUS_EXPR:
28282 case LSHIFT_EXPR:
28283 case RSHIFT_EXPR:
28284 case BIT_AND_EXPR:
28285 case BIT_IOR_EXPR:
28286 case BIT_XOR_EXPR:
28287 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
28289 if (cp_parser_parse_definitely (parser))
28291 opcode = TREE_CODE (rhs);
28292 rhs1 = TREE_OPERAND (rhs, 0);
28293 rhs = TREE_OPERAND (rhs, 1);
28294 goto stmt_done;
28296 else
28297 goto saw_error;
28299 break;
28300 default:
28301 break;
28303 cp_parser_abort_tentative_parse (parser);
28304 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
28306 rhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28307 if (rhs == error_mark_node)
28308 goto saw_error;
28309 opcode = NOP_EXPR;
28310 rhs1 = NULL_TREE;
28311 goto stmt_done;
28313 cp_parser_error (parser,
28314 "invalid form of %<#pragma omp atomic%>");
28315 goto saw_error;
28317 if (!cp_parser_parse_definitely (parser))
28318 goto saw_error;
28319 switch (token->type)
28321 case CPP_SEMICOLON:
28322 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28324 code = OMP_ATOMIC_CAPTURE_OLD;
28325 v = lhs;
28326 lhs = NULL_TREE;
28327 lhs1 = rhs1;
28328 rhs1 = NULL_TREE;
28329 cp_lexer_consume_token (parser->lexer);
28330 goto restart;
28332 else if (structured_block)
28334 opcode = NOP_EXPR;
28335 rhs = rhs1;
28336 rhs1 = NULL_TREE;
28337 goto stmt_done;
28339 cp_parser_error (parser,
28340 "invalid form of %<#pragma omp atomic%>");
28341 goto saw_error;
28342 case CPP_MULT:
28343 opcode = MULT_EXPR;
28344 break;
28345 case CPP_DIV:
28346 opcode = TRUNC_DIV_EXPR;
28347 break;
28348 case CPP_PLUS:
28349 opcode = PLUS_EXPR;
28350 break;
28351 case CPP_MINUS:
28352 opcode = MINUS_EXPR;
28353 break;
28354 case CPP_LSHIFT:
28355 opcode = LSHIFT_EXPR;
28356 break;
28357 case CPP_RSHIFT:
28358 opcode = RSHIFT_EXPR;
28359 break;
28360 case CPP_AND:
28361 opcode = BIT_AND_EXPR;
28362 break;
28363 case CPP_OR:
28364 opcode = BIT_IOR_EXPR;
28365 break;
28366 case CPP_XOR:
28367 opcode = BIT_XOR_EXPR;
28368 break;
28369 default:
28370 cp_parser_error (parser,
28371 "invalid operator for %<#pragma omp atomic%>");
28372 goto saw_error;
28374 oprec = TOKEN_PRECEDENCE (token);
28375 gcc_assert (oprec != PREC_NOT_OPERATOR);
28376 if (commutative_tree_code (opcode))
28377 oprec = (enum cp_parser_prec) (oprec - 1);
28378 cp_lexer_consume_token (parser->lexer);
28379 rhs = cp_parser_binary_expression (parser, false, false,
28380 oprec, NULL);
28381 if (rhs == error_mark_node)
28382 goto saw_error;
28383 goto stmt_done;
28384 /* FALLTHROUGH */
28385 default:
28386 cp_parser_error (parser,
28387 "invalid operator for %<#pragma omp atomic%>");
28388 goto saw_error;
28390 cp_lexer_consume_token (parser->lexer);
28392 rhs = cp_parser_expression (parser, false, NULL);
28393 if (rhs == error_mark_node)
28394 goto saw_error;
28395 break;
28397 stmt_done:
28398 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28400 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
28401 goto saw_error;
28402 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28403 /*cast_p=*/false, NULL);
28404 if (v == error_mark_node)
28405 goto saw_error;
28406 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28407 goto saw_error;
28408 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
28409 /*cast_p=*/false, NULL);
28410 if (lhs1 == error_mark_node)
28411 goto saw_error;
28413 if (structured_block)
28415 cp_parser_consume_semicolon_at_end_of_statement (parser);
28416 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
28418 done:
28419 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
28420 if (!structured_block)
28421 cp_parser_consume_semicolon_at_end_of_statement (parser);
28422 return;
28424 saw_error:
28425 cp_parser_skip_to_end_of_block_or_statement (parser);
28426 if (structured_block)
28428 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28429 cp_lexer_consume_token (parser->lexer);
28430 else if (code == OMP_ATOMIC_CAPTURE_NEW)
28432 cp_parser_skip_to_end_of_block_or_statement (parser);
28433 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28434 cp_lexer_consume_token (parser->lexer);
28440 /* OpenMP 2.5:
28441 # pragma omp barrier new-line */
28443 static void
28444 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
28446 cp_parser_require_pragma_eol (parser, pragma_tok);
28447 finish_omp_barrier ();
28450 /* OpenMP 2.5:
28451 # pragma omp critical [(name)] new-line
28452 structured-block */
28454 static tree
28455 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
28457 tree stmt, name = NULL;
28459 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28461 cp_lexer_consume_token (parser->lexer);
28463 name = cp_parser_identifier (parser);
28465 if (name == error_mark_node
28466 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28467 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28468 /*or_comma=*/false,
28469 /*consume_paren=*/true);
28470 if (name == error_mark_node)
28471 name = NULL;
28473 cp_parser_require_pragma_eol (parser, pragma_tok);
28475 stmt = cp_parser_omp_structured_block (parser);
28476 return c_finish_omp_critical (input_location, stmt, name);
28479 /* OpenMP 2.5:
28480 # pragma omp flush flush-vars[opt] new-line
28482 flush-vars:
28483 ( variable-list ) */
28485 static void
28486 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
28488 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28489 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28490 cp_parser_require_pragma_eol (parser, pragma_tok);
28492 finish_omp_flush ();
28495 /* Helper function, to parse omp for increment expression. */
28497 static tree
28498 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
28500 tree cond = cp_parser_binary_expression (parser, false, true,
28501 PREC_NOT_OPERATOR, NULL);
28502 if (cond == error_mark_node
28503 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28505 cp_parser_skip_to_end_of_statement (parser);
28506 return error_mark_node;
28509 switch (TREE_CODE (cond))
28511 case GT_EXPR:
28512 case GE_EXPR:
28513 case LT_EXPR:
28514 case LE_EXPR:
28515 break;
28516 default:
28517 return error_mark_node;
28520 /* If decl is an iterator, preserve LHS and RHS of the relational
28521 expr until finish_omp_for. */
28522 if (decl
28523 && (type_dependent_expression_p (decl)
28524 || CLASS_TYPE_P (TREE_TYPE (decl))))
28525 return cond;
28527 return build_x_binary_op (input_location, TREE_CODE (cond),
28528 TREE_OPERAND (cond, 0), ERROR_MARK,
28529 TREE_OPERAND (cond, 1), ERROR_MARK,
28530 /*overload=*/NULL, tf_warning_or_error);
28533 /* Helper function, to parse omp for increment expression. */
28535 static tree
28536 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
28538 cp_token *token = cp_lexer_peek_token (parser->lexer);
28539 enum tree_code op;
28540 tree lhs, rhs;
28541 cp_id_kind idk;
28542 bool decl_first;
28544 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
28546 op = (token->type == CPP_PLUS_PLUS
28547 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
28548 cp_lexer_consume_token (parser->lexer);
28549 lhs = cp_parser_simple_cast_expression (parser);
28550 if (lhs != decl)
28551 return error_mark_node;
28552 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
28555 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
28556 if (lhs != decl)
28557 return error_mark_node;
28559 token = cp_lexer_peek_token (parser->lexer);
28560 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
28562 op = (token->type == CPP_PLUS_PLUS
28563 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
28564 cp_lexer_consume_token (parser->lexer);
28565 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
28568 op = cp_parser_assignment_operator_opt (parser);
28569 if (op == ERROR_MARK)
28570 return error_mark_node;
28572 if (op != NOP_EXPR)
28574 rhs = cp_parser_assignment_expression (parser, false, NULL);
28575 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
28576 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
28579 lhs = cp_parser_binary_expression (parser, false, false,
28580 PREC_ADDITIVE_EXPRESSION, NULL);
28581 token = cp_lexer_peek_token (parser->lexer);
28582 decl_first = lhs == decl;
28583 if (decl_first)
28584 lhs = NULL_TREE;
28585 if (token->type != CPP_PLUS
28586 && token->type != CPP_MINUS)
28587 return error_mark_node;
28591 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
28592 cp_lexer_consume_token (parser->lexer);
28593 rhs = cp_parser_binary_expression (parser, false, false,
28594 PREC_ADDITIVE_EXPRESSION, NULL);
28595 token = cp_lexer_peek_token (parser->lexer);
28596 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
28598 if (lhs == NULL_TREE)
28600 if (op == PLUS_EXPR)
28601 lhs = rhs;
28602 else
28603 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
28604 tf_warning_or_error);
28606 else
28607 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
28608 ERROR_MARK, NULL, tf_warning_or_error);
28611 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
28613 if (!decl_first)
28615 if (rhs != decl || op == MINUS_EXPR)
28616 return error_mark_node;
28617 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
28619 else
28620 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
28622 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
28625 /* Parse the restricted form of the for statement allowed by OpenMP. */
28627 static tree
28628 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
28629 tree *cclauses)
28631 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
28632 tree real_decl, initv, condv, incrv, declv;
28633 tree this_pre_body, cl;
28634 location_t loc_first;
28635 bool collapse_err = false;
28636 int i, collapse = 1, nbraces = 0;
28637 vec<tree, va_gc> *for_block = make_tree_vector ();
28639 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
28640 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
28641 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
28643 gcc_assert (collapse >= 1);
28645 declv = make_tree_vec (collapse);
28646 initv = make_tree_vec (collapse);
28647 condv = make_tree_vec (collapse);
28648 incrv = make_tree_vec (collapse);
28650 loc_first = cp_lexer_peek_token (parser->lexer)->location;
28652 for (i = 0; i < collapse; i++)
28654 int bracecount = 0;
28655 bool add_private_clause = false;
28656 location_t loc;
28658 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
28660 cp_parser_error (parser, "for statement expected");
28661 return NULL;
28663 loc = cp_lexer_consume_token (parser->lexer)->location;
28665 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28666 return NULL;
28668 init = decl = real_decl = NULL;
28669 this_pre_body = push_stmt_list ();
28670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28672 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
28674 init-expr:
28675 var = lb
28676 integer-type var = lb
28677 random-access-iterator-type var = lb
28678 pointer-type var = lb
28680 cp_decl_specifier_seq type_specifiers;
28682 /* First, try to parse as an initialized declaration. See
28683 cp_parser_condition, from whence the bulk of this is copied. */
28685 cp_parser_parse_tentatively (parser);
28686 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
28687 /*is_trailing_return=*/false,
28688 &type_specifiers);
28689 if (cp_parser_parse_definitely (parser))
28691 /* If parsing a type specifier seq succeeded, then this
28692 MUST be a initialized declaration. */
28693 tree asm_specification, attributes;
28694 cp_declarator *declarator;
28696 declarator = cp_parser_declarator (parser,
28697 CP_PARSER_DECLARATOR_NAMED,
28698 /*ctor_dtor_or_conv_p=*/NULL,
28699 /*parenthesized_p=*/NULL,
28700 /*member_p=*/false);
28701 attributes = cp_parser_attributes_opt (parser);
28702 asm_specification = cp_parser_asm_specification_opt (parser);
28704 if (declarator == cp_error_declarator)
28705 cp_parser_skip_to_end_of_statement (parser);
28707 else
28709 tree pushed_scope, auto_node;
28711 decl = start_decl (declarator, &type_specifiers,
28712 SD_INITIALIZED, attributes,
28713 /*prefix_attributes=*/NULL_TREE,
28714 &pushed_scope);
28716 auto_node = type_uses_auto (TREE_TYPE (decl));
28717 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
28719 if (cp_lexer_next_token_is (parser->lexer,
28720 CPP_OPEN_PAREN))
28721 error ("parenthesized initialization is not allowed in "
28722 "OpenMP %<for%> loop");
28723 else
28724 /* Trigger an error. */
28725 cp_parser_require (parser, CPP_EQ, RT_EQ);
28727 init = error_mark_node;
28728 cp_parser_skip_to_end_of_statement (parser);
28730 else if (CLASS_TYPE_P (TREE_TYPE (decl))
28731 || type_dependent_expression_p (decl)
28732 || auto_node)
28734 bool is_direct_init, is_non_constant_init;
28736 init = cp_parser_initializer (parser,
28737 &is_direct_init,
28738 &is_non_constant_init);
28740 if (auto_node)
28742 TREE_TYPE (decl)
28743 = do_auto_deduction (TREE_TYPE (decl), init,
28744 auto_node);
28746 if (!CLASS_TYPE_P (TREE_TYPE (decl))
28747 && !type_dependent_expression_p (decl))
28748 goto non_class;
28751 cp_finish_decl (decl, init, !is_non_constant_init,
28752 asm_specification,
28753 LOOKUP_ONLYCONVERTING);
28754 if (CLASS_TYPE_P (TREE_TYPE (decl)))
28756 vec_safe_push (for_block, this_pre_body);
28757 init = NULL_TREE;
28759 else
28760 init = pop_stmt_list (this_pre_body);
28761 this_pre_body = NULL_TREE;
28763 else
28765 /* Consume '='. */
28766 cp_lexer_consume_token (parser->lexer);
28767 init = cp_parser_assignment_expression (parser, false, NULL);
28769 non_class:
28770 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
28771 init = error_mark_node;
28772 else
28773 cp_finish_decl (decl, NULL_TREE,
28774 /*init_const_expr_p=*/false,
28775 asm_specification,
28776 LOOKUP_ONLYCONVERTING);
28779 if (pushed_scope)
28780 pop_scope (pushed_scope);
28783 else
28785 cp_id_kind idk;
28786 /* If parsing a type specifier sequence failed, then
28787 this MUST be a simple expression. */
28788 cp_parser_parse_tentatively (parser);
28789 decl = cp_parser_primary_expression (parser, false, false,
28790 false, &idk);
28791 if (!cp_parser_error_occurred (parser)
28792 && decl
28793 && DECL_P (decl)
28794 && CLASS_TYPE_P (TREE_TYPE (decl)))
28796 tree rhs;
28798 cp_parser_parse_definitely (parser);
28799 cp_parser_require (parser, CPP_EQ, RT_EQ);
28800 rhs = cp_parser_assignment_expression (parser, false, NULL);
28801 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
28802 decl, NOP_EXPR,
28803 rhs,
28804 tf_warning_or_error));
28805 add_private_clause = true;
28807 else
28809 decl = NULL;
28810 cp_parser_abort_tentative_parse (parser);
28811 init = cp_parser_expression (parser, false, NULL);
28812 if (init)
28814 if (TREE_CODE (init) == MODIFY_EXPR
28815 || TREE_CODE (init) == MODOP_EXPR)
28816 real_decl = TREE_OPERAND (init, 0);
28821 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28822 if (this_pre_body)
28824 this_pre_body = pop_stmt_list (this_pre_body);
28825 if (pre_body)
28827 tree t = pre_body;
28828 pre_body = push_stmt_list ();
28829 add_stmt (t);
28830 add_stmt (this_pre_body);
28831 pre_body = pop_stmt_list (pre_body);
28833 else
28834 pre_body = this_pre_body;
28837 if (decl)
28838 real_decl = decl;
28839 if (cclauses != NULL
28840 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
28841 && real_decl != NULL_TREE)
28843 tree *c;
28844 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
28845 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
28846 && OMP_CLAUSE_DECL (*c) == real_decl)
28848 error_at (loc, "iteration variable %qD"
28849 " should not be firstprivate", real_decl);
28850 *c = OMP_CLAUSE_CHAIN (*c);
28852 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
28853 && OMP_CLAUSE_DECL (*c) == real_decl)
28855 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
28856 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
28857 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
28858 OMP_CLAUSE_DECL (l) = real_decl;
28859 OMP_CLAUSE_CHAIN (l) = clauses;
28860 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
28861 clauses = l;
28862 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
28863 CP_OMP_CLAUSE_INFO (*c) = NULL;
28864 add_private_clause = false;
28866 else
28868 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
28869 && OMP_CLAUSE_DECL (*c) == real_decl)
28870 add_private_clause = false;
28871 c = &OMP_CLAUSE_CHAIN (*c);
28875 if (add_private_clause)
28877 tree c;
28878 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
28880 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
28881 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
28882 && OMP_CLAUSE_DECL (c) == decl)
28883 break;
28884 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
28885 && OMP_CLAUSE_DECL (c) == decl)
28886 error_at (loc, "iteration variable %qD "
28887 "should not be firstprivate",
28888 decl);
28889 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
28890 && OMP_CLAUSE_DECL (c) == decl)
28891 error_at (loc, "iteration variable %qD should not be reduction",
28892 decl);
28894 if (c == NULL)
28896 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
28897 OMP_CLAUSE_DECL (c) = decl;
28898 c = finish_omp_clauses (c);
28899 if (c)
28901 OMP_CLAUSE_CHAIN (c) = clauses;
28902 clauses = c;
28907 cond = NULL;
28908 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28909 cond = cp_parser_omp_for_cond (parser, decl);
28910 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
28912 incr = NULL;
28913 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28915 /* If decl is an iterator, preserve the operator on decl
28916 until finish_omp_for. */
28917 if (real_decl
28918 && ((processing_template_decl
28919 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
28920 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
28921 incr = cp_parser_omp_for_incr (parser, real_decl);
28922 else
28923 incr = cp_parser_expression (parser, false, NULL);
28924 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
28925 SET_EXPR_LOCATION (incr, input_location);
28928 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28929 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28930 /*or_comma=*/false,
28931 /*consume_paren=*/true);
28933 TREE_VEC_ELT (declv, i) = decl;
28934 TREE_VEC_ELT (initv, i) = init;
28935 TREE_VEC_ELT (condv, i) = cond;
28936 TREE_VEC_ELT (incrv, i) = incr;
28938 if (i == collapse - 1)
28939 break;
28941 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
28942 in between the collapsed for loops to be still considered perfectly
28943 nested. Hopefully the final version clarifies this.
28944 For now handle (multiple) {'s and empty statements. */
28945 cp_parser_parse_tentatively (parser);
28948 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
28949 break;
28950 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28952 cp_lexer_consume_token (parser->lexer);
28953 bracecount++;
28955 else if (bracecount
28956 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28957 cp_lexer_consume_token (parser->lexer);
28958 else
28960 loc = cp_lexer_peek_token (parser->lexer)->location;
28961 error_at (loc, "not enough collapsed for loops");
28962 collapse_err = true;
28963 cp_parser_abort_tentative_parse (parser);
28964 declv = NULL_TREE;
28965 break;
28968 while (1);
28970 if (declv)
28972 cp_parser_parse_definitely (parser);
28973 nbraces += bracecount;
28977 /* Note that we saved the original contents of this flag when we entered
28978 the structured block, and so we don't need to re-save it here. */
28979 parser->in_statement = IN_OMP_FOR;
28981 /* Note that the grammar doesn't call for a structured block here,
28982 though the loop as a whole is a structured block. */
28983 body = push_stmt_list ();
28984 cp_parser_statement (parser, NULL_TREE, false, NULL);
28985 body = pop_stmt_list (body);
28987 if (declv == NULL_TREE)
28988 ret = NULL_TREE;
28989 else
28990 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
28991 pre_body, clauses);
28993 while (nbraces)
28995 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28997 cp_lexer_consume_token (parser->lexer);
28998 nbraces--;
29000 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29001 cp_lexer_consume_token (parser->lexer);
29002 else
29004 if (!collapse_err)
29006 error_at (cp_lexer_peek_token (parser->lexer)->location,
29007 "collapsed loops not perfectly nested");
29009 collapse_err = true;
29010 cp_parser_statement_seq_opt (parser, NULL);
29011 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29012 break;
29016 while (!for_block->is_empty ())
29017 add_stmt (pop_stmt_list (for_block->pop ()));
29018 release_tree_vector (for_block);
29020 return ret;
29023 /* Helper function for OpenMP parsing, split clauses and call
29024 finish_omp_clauses on each of the set of clauses afterwards. */
29026 static void
29027 cp_omp_split_clauses (location_t loc, enum tree_code code,
29028 omp_clause_mask mask, tree clauses, tree *cclauses)
29030 int i;
29031 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29032 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29033 if (cclauses[i])
29034 cclauses[i] = finish_omp_clauses (cclauses[i]);
29037 /* OpenMP 4.0:
29038 #pragma omp simd simd-clause[optseq] new-line
29039 for-loop */
29041 #define OMP_SIMD_CLAUSE_MASK \
29042 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29050 static tree
29051 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29052 char *p_name, omp_clause_mask mask, tree *cclauses)
29054 tree clauses, sb, ret;
29055 unsigned int save;
29056 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29058 strcat (p_name, " simd");
29059 mask |= OMP_SIMD_CLAUSE_MASK;
29060 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29062 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29063 cclauses == NULL);
29064 if (cclauses)
29066 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29067 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29070 sb = begin_omp_structured_block ();
29071 save = cp_parser_begin_omp_structured_block (parser);
29073 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29075 cp_parser_end_omp_structured_block (parser, save);
29076 add_stmt (finish_omp_structured_block (sb));
29078 return ret;
29081 /* OpenMP 2.5:
29082 #pragma omp for for-clause[optseq] new-line
29083 for-loop
29085 OpenMP 4.0:
29086 #pragma omp for simd for-simd-clause[optseq] new-line
29087 for-loop */
29089 #define OMP_FOR_CLAUSE_MASK \
29090 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29099 static tree
29100 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29101 char *p_name, omp_clause_mask mask, tree *cclauses)
29103 tree clauses, sb, ret;
29104 unsigned int save;
29105 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29107 strcat (p_name, " for");
29108 mask |= OMP_FOR_CLAUSE_MASK;
29109 if (cclauses)
29110 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29112 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29114 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29115 const char *p = IDENTIFIER_POINTER (id);
29117 if (strcmp (p, "simd") == 0)
29119 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29120 if (cclauses == NULL)
29121 cclauses = cclauses_buf;
29123 cp_lexer_consume_token (parser->lexer);
29124 sb = begin_omp_structured_block ();
29125 save = cp_parser_begin_omp_structured_block (parser);
29126 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29127 cclauses);
29128 cp_parser_end_omp_structured_block (parser, save);
29129 tree body = finish_omp_structured_block (sb);
29130 if (ret == NULL)
29131 return ret;
29132 ret = make_node (OMP_FOR);
29133 TREE_TYPE (ret) = void_type_node;
29134 OMP_FOR_BODY (ret) = body;
29135 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29136 SET_EXPR_LOCATION (ret, loc);
29137 add_stmt (ret);
29138 return ret;
29142 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29143 cclauses == NULL);
29144 if (cclauses)
29146 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
29147 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29150 sb = begin_omp_structured_block ();
29151 save = cp_parser_begin_omp_structured_block (parser);
29153 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
29155 cp_parser_end_omp_structured_block (parser, save);
29156 add_stmt (finish_omp_structured_block (sb));
29158 return ret;
29161 /* OpenMP 2.5:
29162 # pragma omp master new-line
29163 structured-block */
29165 static tree
29166 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
29168 cp_parser_require_pragma_eol (parser, pragma_tok);
29169 return c_finish_omp_master (input_location,
29170 cp_parser_omp_structured_block (parser));
29173 /* OpenMP 2.5:
29174 # pragma omp ordered new-line
29175 structured-block */
29177 static tree
29178 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
29180 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29181 cp_parser_require_pragma_eol (parser, pragma_tok);
29182 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
29185 /* OpenMP 2.5:
29187 section-scope:
29188 { section-sequence }
29190 section-sequence:
29191 section-directive[opt] structured-block
29192 section-sequence section-directive structured-block */
29194 static tree
29195 cp_parser_omp_sections_scope (cp_parser *parser)
29197 tree stmt, substmt;
29198 bool error_suppress = false;
29199 cp_token *tok;
29201 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
29202 return NULL_TREE;
29204 stmt = push_stmt_list ();
29206 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
29208 substmt = cp_parser_omp_structured_block (parser);
29209 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29210 add_stmt (substmt);
29213 while (1)
29215 tok = cp_lexer_peek_token (parser->lexer);
29216 if (tok->type == CPP_CLOSE_BRACE)
29217 break;
29218 if (tok->type == CPP_EOF)
29219 break;
29221 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
29223 cp_lexer_consume_token (parser->lexer);
29224 cp_parser_require_pragma_eol (parser, tok);
29225 error_suppress = false;
29227 else if (!error_suppress)
29229 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
29230 error_suppress = true;
29233 substmt = cp_parser_omp_structured_block (parser);
29234 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29235 add_stmt (substmt);
29237 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29239 substmt = pop_stmt_list (stmt);
29241 stmt = make_node (OMP_SECTIONS);
29242 TREE_TYPE (stmt) = void_type_node;
29243 OMP_SECTIONS_BODY (stmt) = substmt;
29245 add_stmt (stmt);
29246 return stmt;
29249 /* OpenMP 2.5:
29250 # pragma omp sections sections-clause[optseq] newline
29251 sections-scope */
29253 #define OMP_SECTIONS_CLAUSE_MASK \
29254 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29260 static tree
29261 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
29262 char *p_name, omp_clause_mask mask, tree *cclauses)
29264 tree clauses, ret;
29265 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29267 strcat (p_name, " sections");
29268 mask |= OMP_SECTIONS_CLAUSE_MASK;
29269 if (cclauses)
29270 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29272 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29273 cclauses == NULL);
29274 if (cclauses)
29276 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
29277 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
29280 ret = cp_parser_omp_sections_scope (parser);
29281 if (ret)
29282 OMP_SECTIONS_CLAUSES (ret) = clauses;
29284 return ret;
29287 /* OpenMP 2.5:
29288 # pragma parallel parallel-clause new-line
29289 # pragma parallel for parallel-for-clause new-line
29290 # pragma parallel sections parallel-sections-clause new-line
29292 OpenMP 4.0:
29293 # pragma parallel for simd parallel-for-simd-clause new-line */
29295 #define OMP_PARALLEL_CLAUSE_MASK \
29296 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
29302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
29304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
29306 static tree
29307 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
29308 char *p_name, omp_clause_mask mask, tree *cclauses)
29310 tree stmt, clauses, block;
29311 unsigned int save;
29312 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29314 strcat (p_name, " parallel");
29315 mask |= OMP_PARALLEL_CLAUSE_MASK;
29317 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29319 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29320 if (cclauses == NULL)
29321 cclauses = cclauses_buf;
29323 cp_lexer_consume_token (parser->lexer);
29324 block = begin_omp_parallel ();
29325 save = cp_parser_begin_omp_structured_block (parser);
29326 cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29327 cp_parser_end_omp_structured_block (parser, save);
29328 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29329 block);
29330 OMP_PARALLEL_COMBINED (stmt) = 1;
29331 return stmt;
29333 else if (cclauses)
29335 error_at (loc, "expected %<for%> after %qs", p_name);
29336 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29337 return NULL_TREE;
29339 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29341 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29342 const char *p = IDENTIFIER_POINTER (id);
29343 if (strcmp (p, "sections") == 0)
29345 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29346 cclauses = cclauses_buf;
29348 cp_lexer_consume_token (parser->lexer);
29349 block = begin_omp_parallel ();
29350 save = cp_parser_begin_omp_structured_block (parser);
29351 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
29352 cp_parser_end_omp_structured_block (parser, save);
29353 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29354 block);
29355 OMP_PARALLEL_COMBINED (stmt) = 1;
29356 return stmt;
29360 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
29362 block = begin_omp_parallel ();
29363 save = cp_parser_begin_omp_structured_block (parser);
29364 cp_parser_statement (parser, NULL_TREE, false, NULL);
29365 cp_parser_end_omp_structured_block (parser, save);
29366 stmt = finish_omp_parallel (clauses, block);
29367 return stmt;
29370 /* OpenMP 2.5:
29371 # pragma omp single single-clause[optseq] new-line
29372 structured-block */
29374 #define OMP_SINGLE_CLAUSE_MASK \
29375 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
29378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29380 static tree
29381 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
29383 tree stmt = make_node (OMP_SINGLE);
29384 TREE_TYPE (stmt) = void_type_node;
29386 OMP_SINGLE_CLAUSES (stmt)
29387 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
29388 "#pragma omp single", pragma_tok);
29389 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
29391 return add_stmt (stmt);
29394 /* OpenMP 3.0:
29395 # pragma omp task task-clause[optseq] new-line
29396 structured-block */
29398 #define OMP_TASK_CLAUSE_MASK \
29399 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
29401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
29406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
29407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
29409 static tree
29410 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
29412 tree clauses, block;
29413 unsigned int save;
29415 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
29416 "#pragma omp task", pragma_tok);
29417 block = begin_omp_task ();
29418 save = cp_parser_begin_omp_structured_block (parser);
29419 cp_parser_statement (parser, NULL_TREE, false, NULL);
29420 cp_parser_end_omp_structured_block (parser, save);
29421 return finish_omp_task (clauses, block);
29424 /* OpenMP 3.0:
29425 # pragma omp taskwait new-line */
29427 static void
29428 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
29430 cp_parser_require_pragma_eol (parser, pragma_tok);
29431 finish_omp_taskwait ();
29434 /* OpenMP 3.1:
29435 # pragma omp taskyield new-line */
29437 static void
29438 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
29440 cp_parser_require_pragma_eol (parser, pragma_tok);
29441 finish_omp_taskyield ();
29444 /* OpenMP 4.0:
29445 # pragma omp taskgroup new-line
29446 structured-block */
29448 static tree
29449 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
29451 cp_parser_require_pragma_eol (parser, pragma_tok);
29452 return c_finish_omp_taskgroup (input_location,
29453 cp_parser_omp_structured_block (parser));
29457 /* OpenMP 2.5:
29458 # pragma omp threadprivate (variable-list) */
29460 static void
29461 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
29463 tree vars;
29465 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29466 cp_parser_require_pragma_eol (parser, pragma_tok);
29468 finish_omp_threadprivate (vars);
29471 /* OpenMP 4.0:
29472 # pragma omp cancel cancel-clause[optseq] new-line */
29474 #define OMP_CANCEL_CLAUSE_MASK \
29475 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
29479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29481 static void
29482 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
29484 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
29485 "#pragma omp cancel", pragma_tok);
29486 finish_omp_cancel (clauses);
29489 /* OpenMP 4.0:
29490 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
29492 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
29493 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
29498 static void
29499 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
29501 tree clauses;
29502 bool point_seen = false;
29504 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29506 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29507 const char *p = IDENTIFIER_POINTER (id);
29509 if (strcmp (p, "point") == 0)
29511 cp_lexer_consume_token (parser->lexer);
29512 point_seen = true;
29515 if (!point_seen)
29517 cp_parser_error (parser, "expected %<point%>");
29518 cp_parser_require_pragma_eol (parser, pragma_tok);
29519 return;
29522 clauses = cp_parser_omp_all_clauses (parser,
29523 OMP_CANCELLATION_POINT_CLAUSE_MASK,
29524 "#pragma omp cancellation point",
29525 pragma_tok);
29526 finish_omp_cancellation_point (clauses);
29529 /* OpenMP 4.0:
29530 #pragma omp distribute distribute-clause[optseq] new-line
29531 for-loop */
29533 #define OMP_DISTRIBUTE_CLAUSE_MASK \
29534 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
29537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29539 static tree
29540 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
29541 char *p_name, omp_clause_mask mask, tree *cclauses)
29543 tree clauses, sb, ret;
29544 unsigned int save;
29545 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29547 strcat (p_name, " distribute");
29548 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
29550 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29552 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29553 const char *p = IDENTIFIER_POINTER (id);
29554 bool simd = false;
29555 bool parallel = false;
29557 if (strcmp (p, "simd") == 0)
29558 simd = true;
29559 else
29560 parallel = strcmp (p, "parallel") == 0;
29561 if (parallel || simd)
29563 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29564 if (cclauses == NULL)
29565 cclauses = cclauses_buf;
29566 cp_lexer_consume_token (parser->lexer);
29567 sb = begin_omp_structured_block ();
29568 save = cp_parser_begin_omp_structured_block (parser);
29569 if (simd)
29570 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29571 cclauses);
29572 else
29573 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
29574 cclauses);
29575 cp_parser_end_omp_structured_block (parser, save);
29576 tree body = finish_omp_structured_block (sb);
29577 if (ret == NULL)
29578 return ret;
29579 ret = make_node (OMP_DISTRIBUTE);
29580 TREE_TYPE (ret) = void_type_node;
29581 OMP_FOR_BODY (ret) = body;
29582 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
29583 SET_EXPR_LOCATION (ret, loc);
29584 add_stmt (ret);
29585 return ret;
29589 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29590 cclauses == NULL);
29591 if (cclauses)
29593 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
29594 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
29597 sb = begin_omp_structured_block ();
29598 save = cp_parser_begin_omp_structured_block (parser);
29600 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
29602 cp_parser_end_omp_structured_block (parser, save);
29603 add_stmt (finish_omp_structured_block (sb));
29605 return ret;
29608 /* OpenMP 4.0:
29609 # pragma omp teams teams-clause[optseq] new-line
29610 structured-block */
29612 #define OMP_TEAMS_CLAUSE_MASK \
29613 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
29618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
29619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
29621 static tree
29622 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
29623 char *p_name, omp_clause_mask mask, tree *cclauses)
29625 tree clauses, sb, ret;
29626 unsigned int save;
29627 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29629 strcat (p_name, " teams");
29630 mask |= OMP_TEAMS_CLAUSE_MASK;
29632 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29634 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29635 const char *p = IDENTIFIER_POINTER (id);
29636 if (strcmp (p, "distribute") == 0)
29638 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29639 if (cclauses == NULL)
29640 cclauses = cclauses_buf;
29642 cp_lexer_consume_token (parser->lexer);
29643 sb = begin_omp_structured_block ();
29644 save = cp_parser_begin_omp_structured_block (parser);
29645 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
29646 cclauses);
29647 cp_parser_end_omp_structured_block (parser, save);
29648 tree body = finish_omp_structured_block (sb);
29649 if (ret == NULL)
29650 return ret;
29651 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
29652 ret = make_node (OMP_TEAMS);
29653 TREE_TYPE (ret) = void_type_node;
29654 OMP_TEAMS_CLAUSES (ret) = clauses;
29655 OMP_TEAMS_BODY (ret) = body;
29656 return add_stmt (ret);
29660 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29661 cclauses == NULL);
29662 if (cclauses)
29664 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
29665 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
29668 tree stmt = make_node (OMP_TEAMS);
29669 TREE_TYPE (stmt) = void_type_node;
29670 OMP_TEAMS_CLAUSES (stmt) = clauses;
29671 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
29673 return add_stmt (stmt);
29676 /* OpenMP 4.0:
29677 # pragma omp target data target-data-clause[optseq] new-line
29678 structured-block */
29680 #define OMP_TARGET_DATA_CLAUSE_MASK \
29681 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
29683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29685 static tree
29686 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
29688 tree stmt = make_node (OMP_TARGET_DATA);
29689 TREE_TYPE (stmt) = void_type_node;
29691 OMP_TARGET_DATA_CLAUSES (stmt)
29692 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
29693 "#pragma omp target data", pragma_tok);
29694 keep_next_level (true);
29695 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
29697 SET_EXPR_LOCATION (stmt, pragma_tok->location);
29698 return add_stmt (stmt);
29701 /* OpenMP 4.0:
29702 # pragma omp target update target-update-clause[optseq] new-line */
29704 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
29705 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
29706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
29707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29710 static bool
29711 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
29712 enum pragma_context context)
29714 if (context == pragma_stmt)
29716 error_at (pragma_tok->location,
29717 "%<#pragma omp target update%> may only be "
29718 "used in compound statements");
29719 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29720 return false;
29723 tree clauses
29724 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
29725 "#pragma omp target update", pragma_tok);
29726 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
29727 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
29729 error_at (pragma_tok->location,
29730 "%<#pragma omp target update must contain at least one "
29731 "%<from%> or %<to%> clauses");
29732 return false;
29735 tree stmt = make_node (OMP_TARGET_UPDATE);
29736 TREE_TYPE (stmt) = void_type_node;
29737 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
29738 SET_EXPR_LOCATION (stmt, pragma_tok->location);
29739 add_stmt (stmt);
29740 return false;
29743 /* OpenMP 4.0:
29744 # pragma omp target target-clause[optseq] new-line
29745 structured-block */
29747 #define OMP_TARGET_CLAUSE_MASK \
29748 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
29749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
29750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29752 static bool
29753 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
29754 enum pragma_context context)
29756 if (context != pragma_stmt && context != pragma_compound)
29758 cp_parser_error (parser, "expected declaration specifiers");
29759 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29760 return false;
29763 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29765 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29766 const char *p = IDENTIFIER_POINTER (id);
29768 if (strcmp (p, "data") == 0)
29770 cp_lexer_consume_token (parser->lexer);
29771 cp_parser_omp_target_data (parser, pragma_tok);
29772 return true;
29774 else if (strcmp (p, "update") == 0)
29776 cp_lexer_consume_token (parser->lexer);
29777 return cp_parser_omp_target_update (parser, pragma_tok, context);
29779 else if (strcmp (p, "teams") == 0)
29781 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
29782 char p_name[sizeof ("#pragma omp target teams distribute "
29783 "parallel for simd")];
29785 cp_lexer_consume_token (parser->lexer);
29786 strcpy (p_name, "#pragma omp target");
29787 keep_next_level (true);
29788 tree sb = begin_omp_structured_block ();
29789 unsigned save = cp_parser_begin_omp_structured_block (parser);
29790 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
29791 OMP_TARGET_CLAUSE_MASK, cclauses);
29792 cp_parser_end_omp_structured_block (parser, save);
29793 tree body = finish_omp_structured_block (sb);
29794 if (ret == NULL)
29795 return ret;
29796 tree stmt = make_node (OMP_TARGET);
29797 TREE_TYPE (stmt) = void_type_node;
29798 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
29799 OMP_TARGET_BODY (stmt) = body;
29800 add_stmt (stmt);
29801 return true;
29805 tree stmt = make_node (OMP_TARGET);
29806 TREE_TYPE (stmt) = void_type_node;
29808 OMP_TARGET_CLAUSES (stmt)
29809 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
29810 "#pragma omp target", pragma_tok);
29811 keep_next_level (true);
29812 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
29814 SET_EXPR_LOCATION (stmt, pragma_tok->location);
29815 add_stmt (stmt);
29816 return true;
29819 /* OpenMP 4.0:
29820 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
29822 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
29823 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
29824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
29827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
29828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
29830 static void
29831 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
29832 enum pragma_context context)
29834 bool first_p = parser->omp_declare_simd == NULL;
29835 cp_omp_declare_simd_data data;
29836 if (first_p)
29838 data.error_seen = false;
29839 data.fndecl_seen = false;
29840 data.tokens = vNULL;
29841 parser->omp_declare_simd = &data;
29843 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
29844 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
29845 cp_lexer_consume_token (parser->lexer);
29846 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
29847 parser->omp_declare_simd->error_seen = true;
29848 cp_parser_require_pragma_eol (parser, pragma_tok);
29849 struct cp_token_cache *cp
29850 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
29851 parser->omp_declare_simd->tokens.safe_push (cp);
29852 if (first_p)
29854 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
29855 cp_parser_pragma (parser, context);
29856 switch (context)
29858 case pragma_external:
29859 cp_parser_declaration (parser);
29860 break;
29861 case pragma_member:
29862 cp_parser_member_declaration (parser);
29863 break;
29864 case pragma_objc_icode:
29865 cp_parser_block_declaration (parser, /*statement_p=*/false);
29866 break;
29867 default:
29868 cp_parser_declaration_statement (parser);
29869 break;
29871 if (parser->omp_declare_simd
29872 && !parser->omp_declare_simd->error_seen
29873 && !parser->omp_declare_simd->fndecl_seen)
29874 error_at (pragma_tok->location,
29875 "%<#pragma omp declare simd%> not immediately followed by "
29876 "function declaration or definition");
29877 data.tokens.release ();
29878 parser->omp_declare_simd = NULL;
29882 /* Finalize #pragma omp declare simd clauses after direct declarator has
29883 been parsed, and put that into "omp declare simd" attribute. */
29885 static tree
29886 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
29888 struct cp_token_cache *ce;
29889 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
29890 int i;
29892 if (!data->error_seen && data->fndecl_seen)
29894 error ("%<#pragma omp declare simd%> not immediately followed by "
29895 "a single function declaration or definition");
29896 data->error_seen = true;
29897 return attrs;
29899 if (data->error_seen)
29900 return attrs;
29902 FOR_EACH_VEC_ELT (data->tokens, i, ce)
29904 tree c, cl;
29906 cp_parser_push_lexer_for_tokens (parser, ce);
29907 parser->lexer->in_pragma = true;
29908 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
29909 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
29910 cp_lexer_consume_token (parser->lexer);
29911 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
29912 "#pragma omp declare simd", pragma_tok);
29913 cp_parser_pop_lexer (parser);
29914 if (cl)
29915 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
29916 c = build_tree_list (get_identifier ("omp declare simd"), cl);
29917 TREE_CHAIN (c) = attrs;
29918 if (processing_template_decl)
29919 ATTR_IS_DEPENDENT (c) = 1;
29920 attrs = c;
29923 data->fndecl_seen = true;
29924 return attrs;
29928 /* OpenMP 4.0:
29929 # pragma omp declare target new-line
29930 declarations and definitions
29931 # pragma omp end declare target new-line */
29933 static void
29934 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
29936 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29937 scope_chain->omp_declare_target_attribute++;
29940 static void
29941 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
29943 const char *p = "";
29944 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29946 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29947 p = IDENTIFIER_POINTER (id);
29949 if (strcmp (p, "declare") == 0)
29951 cp_lexer_consume_token (parser->lexer);
29952 p = "";
29953 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29955 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29956 p = IDENTIFIER_POINTER (id);
29958 if (strcmp (p, "target") == 0)
29959 cp_lexer_consume_token (parser->lexer);
29960 else
29962 cp_parser_error (parser, "expected %<target%>");
29963 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29964 return;
29967 else
29969 cp_parser_error (parser, "expected %<declare%>");
29970 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29971 return;
29973 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29974 if (!scope_chain->omp_declare_target_attribute)
29975 error_at (pragma_tok->location,
29976 "%<#pragma omp end declare target%> without corresponding "
29977 "%<#pragma omp declare target%>");
29978 else
29979 scope_chain->omp_declare_target_attribute--;
29982 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
29983 expression and optional initializer clause of
29984 #pragma omp declare reduction. We store the expression(s) as
29985 either 3, 6 or 7 special statements inside of the artificial function's
29986 body. The first two statements are DECL_EXPRs for the artificial
29987 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
29988 expression that uses those variables.
29989 If there was any INITIALIZER clause, this is followed by further statements,
29990 the fourth and fifth statements are DECL_EXPRs for the artificial
29991 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
29992 constructor variant (first token after open paren is not omp_priv),
29993 then the sixth statement is a statement with the function call expression
29994 that uses the OMP_PRIV and optionally OMP_ORIG variable.
29995 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
29996 to initialize the OMP_PRIV artificial variable and there is seventh
29997 statement, a DECL_EXPR of the OMP_PRIV statement again. */
29999 static bool
30000 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
30002 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
30003 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
30004 type = TREE_TYPE (type);
30005 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
30006 DECL_ARTIFICIAL (omp_out) = 1;
30007 pushdecl (omp_out);
30008 add_decl_expr (omp_out);
30009 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
30010 DECL_ARTIFICIAL (omp_in) = 1;
30011 pushdecl (omp_in);
30012 add_decl_expr (omp_in);
30013 tree combiner;
30014 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
30016 keep_next_level (true);
30017 tree block = begin_omp_structured_block ();
30018 combiner = cp_parser_expression (parser, false, NULL);
30019 finish_expr_stmt (combiner);
30020 block = finish_omp_structured_block (block);
30021 add_stmt (block);
30023 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30024 return false;
30026 const char *p = "";
30027 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30029 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30030 p = IDENTIFIER_POINTER (id);
30033 if (strcmp (p, "initializer") == 0)
30035 cp_lexer_consume_token (parser->lexer);
30036 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30037 return false;
30039 p = "";
30040 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30042 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30043 p = IDENTIFIER_POINTER (id);
30046 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
30047 DECL_ARTIFICIAL (omp_priv) = 1;
30048 pushdecl (omp_priv);
30049 add_decl_expr (omp_priv);
30050 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
30051 DECL_ARTIFICIAL (omp_orig) = 1;
30052 pushdecl (omp_orig);
30053 add_decl_expr (omp_orig);
30055 keep_next_level (true);
30056 block = begin_omp_structured_block ();
30058 bool ctor = false;
30059 if (strcmp (p, "omp_priv") == 0)
30061 bool is_direct_init, is_non_constant_init;
30062 ctor = true;
30063 cp_lexer_consume_token (parser->lexer);
30064 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30065 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
30066 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30067 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
30068 == CPP_CLOSE_PAREN
30069 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
30070 == CPP_CLOSE_PAREN))
30072 finish_omp_structured_block (block);
30073 error ("invalid initializer clause");
30074 return false;
30076 initializer = cp_parser_initializer (parser, &is_direct_init,
30077 &is_non_constant_init);
30078 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
30079 NULL_TREE, LOOKUP_ONLYCONVERTING);
30081 else
30083 cp_parser_parse_tentatively (parser);
30084 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
30085 /*check_dependency_p=*/true,
30086 /*template_p=*/NULL,
30087 /*declarator_p=*/false,
30088 /*optional_p=*/false);
30089 vec<tree, va_gc> *args;
30090 if (fn_name == error_mark_node
30091 || cp_parser_error_occurred (parser)
30092 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30093 || ((args = cp_parser_parenthesized_expression_list
30094 (parser, non_attr, /*cast_p=*/false,
30095 /*allow_expansion_p=*/true,
30096 /*non_constant_p=*/NULL)),
30097 cp_parser_error_occurred (parser)))
30099 finish_omp_structured_block (block);
30100 cp_parser_abort_tentative_parse (parser);
30101 cp_parser_error (parser, "expected id-expression (arguments)");
30102 return false;
30104 unsigned int i;
30105 tree arg;
30106 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
30107 if (arg == omp_priv
30108 || (TREE_CODE (arg) == ADDR_EXPR
30109 && TREE_OPERAND (arg, 0) == omp_priv))
30110 break;
30111 cp_parser_abort_tentative_parse (parser);
30112 if (arg == NULL_TREE)
30113 error ("one of the initializer call arguments should be %<omp_priv%>"
30114 " or %<&omp_priv%>");
30115 initializer = cp_parser_postfix_expression (parser, false, false, false,
30116 false, NULL);
30117 finish_expr_stmt (initializer);
30120 block = finish_omp_structured_block (block);
30121 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
30122 finish_expr_stmt (block);
30124 if (ctor)
30125 add_decl_expr (omp_orig);
30127 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30128 return false;
30131 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
30132 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
30134 return true;
30137 /* OpenMP 4.0
30138 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30139 initializer-clause[opt] new-line
30141 initializer-clause:
30142 initializer (omp_priv initializer)
30143 initializer (function-name (argument-list)) */
30145 static void
30146 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
30147 enum pragma_context)
30149 vec<tree> types = vNULL;
30150 enum tree_code reduc_code = ERROR_MARK;
30151 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
30152 unsigned int i;
30153 cp_token *first_token;
30154 cp_token_cache *cp;
30155 int errs;
30157 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30158 goto fail;
30160 switch (cp_lexer_peek_token (parser->lexer)->type)
30162 case CPP_PLUS:
30163 reduc_code = PLUS_EXPR;
30164 break;
30165 case CPP_MULT:
30166 reduc_code = MULT_EXPR;
30167 break;
30168 case CPP_MINUS:
30169 reduc_code = MINUS_EXPR;
30170 break;
30171 case CPP_AND:
30172 reduc_code = BIT_AND_EXPR;
30173 break;
30174 case CPP_XOR:
30175 reduc_code = BIT_XOR_EXPR;
30176 break;
30177 case CPP_OR:
30178 reduc_code = BIT_IOR_EXPR;
30179 break;
30180 case CPP_AND_AND:
30181 reduc_code = TRUTH_ANDIF_EXPR;
30182 break;
30183 case CPP_OR_OR:
30184 reduc_code = TRUTH_ORIF_EXPR;
30185 break;
30186 case CPP_NAME:
30187 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
30188 break;
30189 default:
30190 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
30191 "%<|%>, %<&&%>, %<||%> or identifier");
30192 goto fail;
30195 if (reduc_code != ERROR_MARK)
30196 cp_lexer_consume_token (parser->lexer);
30198 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
30199 if (reduc_id == error_mark_node)
30200 goto fail;
30202 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30203 goto fail;
30205 /* Types may not be defined in declare reduction type list. */
30206 const char *saved_message;
30207 saved_message = parser->type_definition_forbidden_message;
30208 parser->type_definition_forbidden_message
30209 = G_("types may not be defined in declare reduction type list");
30210 bool saved_colon_corrects_to_scope_p;
30211 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30212 parser->colon_corrects_to_scope_p = false;
30213 bool saved_colon_doesnt_start_class_def_p;
30214 saved_colon_doesnt_start_class_def_p
30215 = parser->colon_doesnt_start_class_def_p;
30216 parser->colon_doesnt_start_class_def_p = true;
30218 while (true)
30220 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30221 type = cp_parser_type_id (parser);
30222 if (type == error_mark_node)
30224 else if (ARITHMETIC_TYPE_P (type)
30225 && (orig_reduc_id == NULL_TREE
30226 || (TREE_CODE (type) != COMPLEX_TYPE
30227 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30228 "min") == 0
30229 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30230 "max") == 0))))
30231 error_at (loc, "predeclared arithmetic type %qT in "
30232 "%<#pragma omp declare reduction%>", type);
30233 else if (TREE_CODE (type) == FUNCTION_TYPE
30234 || TREE_CODE (type) == METHOD_TYPE
30235 || TREE_CODE (type) == ARRAY_TYPE)
30236 error_at (loc, "function or array type %qT in "
30237 "%<#pragma omp declare reduction%>", type);
30238 else if (TREE_CODE (type) == REFERENCE_TYPE)
30239 error_at (loc, "reference type %qT in "
30240 "%<#pragma omp declare reduction%>", type);
30241 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
30242 error_at (loc, "const, volatile or __restrict qualified type %qT in "
30243 "%<#pragma omp declare reduction%>", type);
30244 else
30245 types.safe_push (type);
30247 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30248 cp_lexer_consume_token (parser->lexer);
30249 else
30250 break;
30253 /* Restore the saved message. */
30254 parser->type_definition_forbidden_message = saved_message;
30255 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30256 parser->colon_doesnt_start_class_def_p
30257 = saved_colon_doesnt_start_class_def_p;
30259 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
30260 || types.is_empty ())
30262 fail:
30263 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30264 types.release ();
30265 return;
30268 first_token = cp_lexer_peek_token (parser->lexer);
30269 cp = NULL;
30270 errs = errorcount;
30271 FOR_EACH_VEC_ELT (types, i, type)
30273 tree fntype
30274 = build_function_type_list (void_type_node,
30275 cp_build_reference_type (type, false),
30276 NULL_TREE);
30277 tree this_reduc_id = reduc_id;
30278 if (!dependent_type_p (type))
30279 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
30280 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
30281 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
30282 DECL_ARTIFICIAL (fndecl) = 1;
30283 DECL_EXTERNAL (fndecl) = 1;
30284 DECL_DECLARED_INLINE_P (fndecl) = 1;
30285 DECL_IGNORED_P (fndecl) = 1;
30286 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
30287 DECL_ATTRIBUTES (fndecl)
30288 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
30289 DECL_ATTRIBUTES (fndecl));
30290 if (processing_template_decl)
30291 fndecl = push_template_decl (fndecl);
30292 bool block_scope = false;
30293 tree block = NULL_TREE;
30294 if (current_function_decl)
30296 block_scope = true;
30297 DECL_CONTEXT (fndecl) = global_namespace;
30298 if (!processing_template_decl)
30299 pushdecl (fndecl);
30301 else if (current_class_type)
30303 if (cp == NULL)
30305 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30306 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30307 cp_lexer_consume_token (parser->lexer);
30308 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30309 goto fail;
30310 cp = cp_token_cache_new (first_token,
30311 cp_lexer_peek_nth_token (parser->lexer,
30312 2));
30314 DECL_STATIC_FUNCTION_P (fndecl) = 1;
30315 finish_member_declaration (fndecl);
30316 DECL_PENDING_INLINE_INFO (fndecl) = cp;
30317 DECL_PENDING_INLINE_P (fndecl) = 1;
30318 vec_safe_push (unparsed_funs_with_definitions, fndecl);
30319 continue;
30321 else
30323 DECL_CONTEXT (fndecl) = current_namespace;
30324 pushdecl (fndecl);
30326 if (!block_scope)
30327 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
30328 else
30329 block = begin_omp_structured_block ();
30330 if (cp)
30332 cp_parser_push_lexer_for_tokens (parser, cp);
30333 parser->lexer->in_pragma = true;
30335 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
30337 if (!block_scope)
30338 finish_function (0);
30339 else
30340 DECL_CONTEXT (fndecl) = current_function_decl;
30341 if (cp)
30342 cp_parser_pop_lexer (parser);
30343 goto fail;
30345 if (cp)
30346 cp_parser_pop_lexer (parser);
30347 if (!block_scope)
30348 finish_function (0);
30349 else
30351 DECL_CONTEXT (fndecl) = current_function_decl;
30352 block = finish_omp_structured_block (block);
30353 if (TREE_CODE (block) == BIND_EXPR)
30354 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
30355 else if (TREE_CODE (block) == STATEMENT_LIST)
30356 DECL_SAVED_TREE (fndecl) = block;
30357 if (processing_template_decl)
30358 add_decl_expr (fndecl);
30360 cp_check_omp_declare_reduction (fndecl);
30361 if (cp == NULL && types.length () > 1)
30362 cp = cp_token_cache_new (first_token,
30363 cp_lexer_peek_nth_token (parser->lexer, 2));
30364 if (errs != errorcount)
30365 break;
30368 cp_parser_require_pragma_eol (parser, pragma_tok);
30369 types.release ();
30372 /* OpenMP 4.0
30373 #pragma omp declare simd declare-simd-clauses[optseq] new-line
30374 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30375 initializer-clause[opt] new-line
30376 #pragma omp declare target new-line */
30378 static void
30379 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
30380 enum pragma_context context)
30382 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30384 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30385 const char *p = IDENTIFIER_POINTER (id);
30387 if (strcmp (p, "simd") == 0)
30389 cp_lexer_consume_token (parser->lexer);
30390 cp_parser_omp_declare_simd (parser, pragma_tok,
30391 context);
30392 return;
30394 cp_ensure_no_omp_declare_simd (parser);
30395 if (strcmp (p, "reduction") == 0)
30397 cp_lexer_consume_token (parser->lexer);
30398 cp_parser_omp_declare_reduction (parser, pragma_tok,
30399 context);
30400 return;
30402 if (strcmp (p, "target") == 0)
30404 cp_lexer_consume_token (parser->lexer);
30405 cp_parser_omp_declare_target (parser, pragma_tok);
30406 return;
30409 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
30410 "or %<target%>");
30411 cp_parser_require_pragma_eol (parser, pragma_tok);
30414 /* Main entry point to OpenMP statement pragmas. */
30416 static void
30417 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
30419 tree stmt;
30420 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
30421 omp_clause_mask mask (0);
30423 switch (pragma_tok->pragma_kind)
30425 case PRAGMA_OMP_ATOMIC:
30426 cp_parser_omp_atomic (parser, pragma_tok);
30427 return;
30428 case PRAGMA_OMP_CRITICAL:
30429 stmt = cp_parser_omp_critical (parser, pragma_tok);
30430 break;
30431 case PRAGMA_OMP_DISTRIBUTE:
30432 strcpy (p_name, "#pragma omp");
30433 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
30434 break;
30435 case PRAGMA_OMP_FOR:
30436 strcpy (p_name, "#pragma omp");
30437 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
30438 break;
30439 case PRAGMA_OMP_MASTER:
30440 stmt = cp_parser_omp_master (parser, pragma_tok);
30441 break;
30442 case PRAGMA_OMP_ORDERED:
30443 stmt = cp_parser_omp_ordered (parser, pragma_tok);
30444 break;
30445 case PRAGMA_OMP_PARALLEL:
30446 strcpy (p_name, "#pragma omp");
30447 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
30448 break;
30449 case PRAGMA_OMP_SECTIONS:
30450 strcpy (p_name, "#pragma omp");
30451 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
30452 break;
30453 case PRAGMA_OMP_SIMD:
30454 strcpy (p_name, "#pragma omp");
30455 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
30456 break;
30457 case PRAGMA_OMP_SINGLE:
30458 stmt = cp_parser_omp_single (parser, pragma_tok);
30459 break;
30460 case PRAGMA_OMP_TASK:
30461 stmt = cp_parser_omp_task (parser, pragma_tok);
30462 break;
30463 case PRAGMA_OMP_TASKGROUP:
30464 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
30465 break;
30466 case PRAGMA_OMP_TEAMS:
30467 strcpy (p_name, "#pragma omp");
30468 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
30469 break;
30470 default:
30471 gcc_unreachable ();
30474 if (stmt)
30475 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30478 /* Transactional Memory parsing routines. */
30480 /* Parse a transaction attribute.
30482 txn-attribute:
30483 attribute
30484 [ [ identifier ] ]
30486 ??? Simplify this when C++0x bracket attributes are
30487 implemented properly. */
30489 static tree
30490 cp_parser_txn_attribute_opt (cp_parser *parser)
30492 cp_token *token;
30493 tree attr_name, attr = NULL;
30495 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30496 return cp_parser_attributes_opt (parser);
30498 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
30499 return NULL_TREE;
30500 cp_lexer_consume_token (parser->lexer);
30501 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
30502 goto error1;
30504 token = cp_lexer_peek_token (parser->lexer);
30505 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
30507 token = cp_lexer_consume_token (parser->lexer);
30509 attr_name = (token->type == CPP_KEYWORD
30510 /* For keywords, use the canonical spelling,
30511 not the parsed identifier. */
30512 ? ridpointers[(int) token->keyword]
30513 : token->u.value);
30514 attr = build_tree_list (attr_name, NULL_TREE);
30516 else
30517 cp_parser_error (parser, "expected identifier");
30519 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30520 error1:
30521 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30522 return attr;
30525 /* Parse a __transaction_atomic or __transaction_relaxed statement.
30527 transaction-statement:
30528 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
30529 compound-statement
30530 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
30533 static tree
30534 cp_parser_transaction (cp_parser *parser, enum rid keyword)
30536 unsigned char old_in = parser->in_transaction;
30537 unsigned char this_in = 1, new_in;
30538 cp_token *token;
30539 tree stmt, attrs, noex;
30541 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
30542 || keyword == RID_TRANSACTION_RELAXED);
30543 token = cp_parser_require_keyword (parser, keyword,
30544 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
30545 : RT_TRANSACTION_RELAXED));
30546 gcc_assert (token != NULL);
30548 if (keyword == RID_TRANSACTION_RELAXED)
30549 this_in |= TM_STMT_ATTR_RELAXED;
30550 else
30552 attrs = cp_parser_txn_attribute_opt (parser);
30553 if (attrs)
30554 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
30557 /* Parse a noexcept specification. */
30558 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
30560 /* Keep track if we're in the lexical scope of an outer transaction. */
30561 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
30563 stmt = begin_transaction_stmt (token->location, NULL, this_in);
30565 parser->in_transaction = new_in;
30566 cp_parser_compound_statement (parser, NULL, false, false);
30567 parser->in_transaction = old_in;
30569 finish_transaction_stmt (stmt, NULL, this_in, noex);
30571 return stmt;
30574 /* Parse a __transaction_atomic or __transaction_relaxed expression.
30576 transaction-expression:
30577 __transaction_atomic txn-noexcept-spec[opt] ( expression )
30578 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
30581 static tree
30582 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
30584 unsigned char old_in = parser->in_transaction;
30585 unsigned char this_in = 1;
30586 cp_token *token;
30587 tree expr, noex;
30588 bool noex_expr;
30590 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
30591 || keyword == RID_TRANSACTION_RELAXED);
30593 if (!flag_tm)
30594 error (keyword == RID_TRANSACTION_RELAXED
30595 ? G_("%<__transaction_relaxed%> without transactional memory "
30596 "support enabled")
30597 : G_("%<__transaction_atomic%> without transactional memory "
30598 "support enabled"));
30600 token = cp_parser_require_keyword (parser, keyword,
30601 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
30602 : RT_TRANSACTION_RELAXED));
30603 gcc_assert (token != NULL);
30605 if (keyword == RID_TRANSACTION_RELAXED)
30606 this_in |= TM_STMT_ATTR_RELAXED;
30608 /* Set this early. This might mean that we allow transaction_cancel in
30609 an expression that we find out later actually has to be a constexpr.
30610 However, we expect that cxx_constant_value will be able to deal with
30611 this; also, if the noexcept has no constexpr, then what we parse next
30612 really is a transaction's body. */
30613 parser->in_transaction = this_in;
30615 /* Parse a noexcept specification. */
30616 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
30617 true);
30619 if (!noex || !noex_expr
30620 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
30622 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30624 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
30625 expr = finish_parenthesized_expr (expr);
30627 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30629 else
30631 /* The only expression that is available got parsed for the noexcept
30632 already. noexcept is true then. */
30633 expr = noex;
30634 noex = boolean_true_node;
30637 expr = build_transaction_expr (token->location, expr, this_in, noex);
30638 parser->in_transaction = old_in;
30640 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
30641 return error_mark_node;
30643 return (flag_tm ? expr : error_mark_node);
30646 /* Parse a function-transaction-block.
30648 function-transaction-block:
30649 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
30650 function-body
30651 __transaction_atomic txn-attribute[opt] function-try-block
30652 __transaction_relaxed ctor-initializer[opt] function-body
30653 __transaction_relaxed function-try-block
30656 static bool
30657 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
30659 unsigned char old_in = parser->in_transaction;
30660 unsigned char new_in = 1;
30661 tree compound_stmt, stmt, attrs;
30662 bool ctor_initializer_p;
30663 cp_token *token;
30665 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
30666 || keyword == RID_TRANSACTION_RELAXED);
30667 token = cp_parser_require_keyword (parser, keyword,
30668 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
30669 : RT_TRANSACTION_RELAXED));
30670 gcc_assert (token != NULL);
30672 if (keyword == RID_TRANSACTION_RELAXED)
30673 new_in |= TM_STMT_ATTR_RELAXED;
30674 else
30676 attrs = cp_parser_txn_attribute_opt (parser);
30677 if (attrs)
30678 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
30681 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
30683 parser->in_transaction = new_in;
30685 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
30686 ctor_initializer_p = cp_parser_function_try_block (parser);
30687 else
30688 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
30689 (parser, /*in_function_try_block=*/false);
30691 parser->in_transaction = old_in;
30693 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
30695 return ctor_initializer_p;
30698 /* Parse a __transaction_cancel statement.
30700 cancel-statement:
30701 __transaction_cancel txn-attribute[opt] ;
30702 __transaction_cancel txn-attribute[opt] throw-expression ;
30704 ??? Cancel and throw is not yet implemented. */
30706 static tree
30707 cp_parser_transaction_cancel (cp_parser *parser)
30709 cp_token *token;
30710 bool is_outer = false;
30711 tree stmt, attrs;
30713 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
30714 RT_TRANSACTION_CANCEL);
30715 gcc_assert (token != NULL);
30717 attrs = cp_parser_txn_attribute_opt (parser);
30718 if (attrs)
30719 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
30721 /* ??? Parse cancel-and-throw here. */
30723 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
30725 if (!flag_tm)
30727 error_at (token->location, "%<__transaction_cancel%> without "
30728 "transactional memory support enabled");
30729 return error_mark_node;
30731 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
30733 error_at (token->location, "%<__transaction_cancel%> within a "
30734 "%<__transaction_relaxed%>");
30735 return error_mark_node;
30737 else if (is_outer)
30739 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
30740 && !is_tm_may_cancel_outer (current_function_decl))
30742 error_at (token->location, "outer %<__transaction_cancel%> not "
30743 "within outer %<__transaction_atomic%>");
30744 error_at (token->location,
30745 " or a %<transaction_may_cancel_outer%> function");
30746 return error_mark_node;
30749 else if (parser->in_transaction == 0)
30751 error_at (token->location, "%<__transaction_cancel%> not within "
30752 "%<__transaction_atomic%>");
30753 return error_mark_node;
30756 stmt = build_tm_abort_call (token->location, is_outer);
30757 add_stmt (stmt);
30759 return stmt;
30762 /* The parser. */
30764 static GTY (()) cp_parser *the_parser;
30767 /* Special handling for the first token or line in the file. The first
30768 thing in the file might be #pragma GCC pch_preprocess, which loads a
30769 PCH file, which is a GC collection point. So we need to handle this
30770 first pragma without benefit of an existing lexer structure.
30772 Always returns one token to the caller in *FIRST_TOKEN. This is
30773 either the true first token of the file, or the first token after
30774 the initial pragma. */
30776 static void
30777 cp_parser_initial_pragma (cp_token *first_token)
30779 tree name = NULL;
30781 cp_lexer_get_preprocessor_token (NULL, first_token);
30782 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
30783 return;
30785 cp_lexer_get_preprocessor_token (NULL, first_token);
30786 if (first_token->type == CPP_STRING)
30788 name = first_token->u.value;
30790 cp_lexer_get_preprocessor_token (NULL, first_token);
30791 if (first_token->type != CPP_PRAGMA_EOL)
30792 error_at (first_token->location,
30793 "junk at end of %<#pragma GCC pch_preprocess%>");
30795 else
30796 error_at (first_token->location, "expected string literal");
30798 /* Skip to the end of the pragma. */
30799 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
30800 cp_lexer_get_preprocessor_token (NULL, first_token);
30802 /* Now actually load the PCH file. */
30803 if (name)
30804 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
30806 /* Read one more token to return to our caller. We have to do this
30807 after reading the PCH file in, since its pointers have to be
30808 live. */
30809 cp_lexer_get_preprocessor_token (NULL, first_token);
30812 /* Normal parsing of a pragma token. Here we can (and must) use the
30813 regular lexer. */
30815 static bool
30816 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
30818 cp_token *pragma_tok;
30819 unsigned int id;
30821 pragma_tok = cp_lexer_consume_token (parser->lexer);
30822 gcc_assert (pragma_tok->type == CPP_PRAGMA);
30823 parser->lexer->in_pragma = true;
30825 id = pragma_tok->pragma_kind;
30826 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
30827 cp_ensure_no_omp_declare_simd (parser);
30828 switch (id)
30830 case PRAGMA_GCC_PCH_PREPROCESS:
30831 error_at (pragma_tok->location,
30832 "%<#pragma GCC pch_preprocess%> must be first");
30833 break;
30835 case PRAGMA_OMP_BARRIER:
30836 switch (context)
30838 case pragma_compound:
30839 cp_parser_omp_barrier (parser, pragma_tok);
30840 return false;
30841 case pragma_stmt:
30842 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
30843 "used in compound statements");
30844 break;
30845 default:
30846 goto bad_stmt;
30848 break;
30850 case PRAGMA_OMP_FLUSH:
30851 switch (context)
30853 case pragma_compound:
30854 cp_parser_omp_flush (parser, pragma_tok);
30855 return false;
30856 case pragma_stmt:
30857 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
30858 "used in compound statements");
30859 break;
30860 default:
30861 goto bad_stmt;
30863 break;
30865 case PRAGMA_OMP_TASKWAIT:
30866 switch (context)
30868 case pragma_compound:
30869 cp_parser_omp_taskwait (parser, pragma_tok);
30870 return false;
30871 case pragma_stmt:
30872 error_at (pragma_tok->location,
30873 "%<#pragma omp taskwait%> may only be "
30874 "used in compound statements");
30875 break;
30876 default:
30877 goto bad_stmt;
30879 break;
30881 case PRAGMA_OMP_TASKYIELD:
30882 switch (context)
30884 case pragma_compound:
30885 cp_parser_omp_taskyield (parser, pragma_tok);
30886 return false;
30887 case pragma_stmt:
30888 error_at (pragma_tok->location,
30889 "%<#pragma omp taskyield%> may only be "
30890 "used in compound statements");
30891 break;
30892 default:
30893 goto bad_stmt;
30895 break;
30897 case PRAGMA_OMP_CANCEL:
30898 switch (context)
30900 case pragma_compound:
30901 cp_parser_omp_cancel (parser, pragma_tok);
30902 return false;
30903 case pragma_stmt:
30904 error_at (pragma_tok->location,
30905 "%<#pragma omp cancel%> may only be "
30906 "used in compound statements");
30907 break;
30908 default:
30909 goto bad_stmt;
30911 break;
30913 case PRAGMA_OMP_CANCELLATION_POINT:
30914 switch (context)
30916 case pragma_compound:
30917 cp_parser_omp_cancellation_point (parser, pragma_tok);
30918 return false;
30919 case pragma_stmt:
30920 error_at (pragma_tok->location,
30921 "%<#pragma omp cancellation point%> may only be "
30922 "used in compound statements");
30923 break;
30924 default:
30925 goto bad_stmt;
30927 break;
30929 case PRAGMA_OMP_THREADPRIVATE:
30930 cp_parser_omp_threadprivate (parser, pragma_tok);
30931 return false;
30933 case PRAGMA_OMP_DECLARE_REDUCTION:
30934 cp_parser_omp_declare (parser, pragma_tok, context);
30935 return false;
30937 case PRAGMA_OMP_ATOMIC:
30938 case PRAGMA_OMP_CRITICAL:
30939 case PRAGMA_OMP_DISTRIBUTE:
30940 case PRAGMA_OMP_FOR:
30941 case PRAGMA_OMP_MASTER:
30942 case PRAGMA_OMP_ORDERED:
30943 case PRAGMA_OMP_PARALLEL:
30944 case PRAGMA_OMP_SECTIONS:
30945 case PRAGMA_OMP_SIMD:
30946 case PRAGMA_OMP_SINGLE:
30947 case PRAGMA_OMP_TASK:
30948 case PRAGMA_OMP_TASKGROUP:
30949 case PRAGMA_OMP_TEAMS:
30950 if (context != pragma_stmt && context != pragma_compound)
30951 goto bad_stmt;
30952 cp_parser_omp_construct (parser, pragma_tok);
30953 return true;
30955 case PRAGMA_OMP_TARGET:
30956 return cp_parser_omp_target (parser, pragma_tok, context);
30958 case PRAGMA_OMP_END_DECLARE_TARGET:
30959 cp_parser_omp_end_declare_target (parser, pragma_tok);
30960 return false;
30962 case PRAGMA_OMP_SECTION:
30963 error_at (pragma_tok->location,
30964 "%<#pragma omp section%> may only be used in "
30965 "%<#pragma omp sections%> construct");
30966 break;
30968 case PRAGMA_IVDEP:
30970 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30971 cp_token *tok;
30972 tok = cp_lexer_peek_token (the_parser->lexer);
30973 if (tok->type != CPP_KEYWORD
30974 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
30975 && tok->keyword != RID_DO))
30977 cp_parser_error (parser, "for, while or do statement expected");
30978 return false;
30980 cp_parser_iteration_statement (parser, true);
30981 return true;
30984 default:
30985 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
30986 c_invoke_pragma_handler (id);
30987 break;
30989 bad_stmt:
30990 cp_parser_error (parser, "expected declaration specifiers");
30991 break;
30994 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30995 return false;
30998 /* The interface the pragma parsers have to the lexer. */
31000 enum cpp_ttype
31001 pragma_lex (tree *value)
31003 cp_token *tok;
31004 enum cpp_ttype ret;
31006 tok = cp_lexer_peek_token (the_parser->lexer);
31008 ret = tok->type;
31009 *value = tok->u.value;
31011 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
31012 ret = CPP_EOF;
31013 else if (ret == CPP_STRING)
31014 *value = cp_parser_string_literal (the_parser, false, false);
31015 else
31017 cp_lexer_consume_token (the_parser->lexer);
31018 if (ret == CPP_KEYWORD)
31019 ret = CPP_NAME;
31022 return ret;
31026 /* External interface. */
31028 /* Parse one entire translation unit. */
31030 void
31031 c_parse_file (void)
31033 static bool already_called = false;
31035 if (already_called)
31037 sorry ("inter-module optimizations not implemented for C++");
31038 return;
31040 already_called = true;
31042 the_parser = cp_parser_new ();
31043 push_deferring_access_checks (flag_access_control
31044 ? dk_no_deferred : dk_no_check);
31045 cp_parser_translation_unit (the_parser);
31046 the_parser = NULL;
31049 /* Create an identifier for a generic parameter type (a synthesized
31050 template parameter implied by `auto' or a concept identifier). */
31052 static GTY(()) int generic_parm_count;
31053 static tree
31054 make_generic_type_name ()
31056 char buf[32];
31057 sprintf (buf, "<auto%d>", ++generic_parm_count);
31058 return get_identifier (buf);
31061 /* Predicate that behaves as is_auto_or_concept but matches the parent
31062 node of the generic type rather than the generic type itself. This
31063 allows for type transformation in add_implicit_template_parms. */
31065 static inline bool
31066 tree_type_is_auto_or_concept (const_tree t)
31068 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
31071 /* Add EXPECT_COUNT implicit template parameters gleaned from the generic
31072 type parameters in PARAMETERS to the CURRENT_TEMPLATE_PARMS (creating a new
31073 template parameter list if necessary). Returns PARAMETERS suitably rewritten
31074 to reference the newly created types or ERROR_MARK_NODE on failure. */
31076 tree
31077 add_implicit_template_parms (cp_parser *parser, size_t expect_count,
31078 tree parameters)
31080 gcc_assert (current_binding_level->kind == sk_function_parms);
31082 cp_binding_level *fn_parms_scope = current_binding_level;
31084 bool become_template =
31085 fn_parms_scope->level_chain->kind != sk_template_parms;
31087 size_t synth_count = 0;
31089 /* Roll back a scope level and either introduce a new template parameter list
31090 or update an existing one. The function scope is added back after template
31091 parameter synthesis below. */
31092 current_binding_level = fn_parms_scope->level_chain;
31094 /* TPARMS tracks the function's template parameter list. This is either a new
31095 chain in the case of a fully implicit function template or an extension of
31096 the function's explicitly specified template parameter list. */
31097 tree tparms = NULL_TREE;
31099 if (become_template)
31101 push_deferring_access_checks (dk_deferred);
31102 begin_template_parm_list ();
31104 parser->fully_implicit_function_template_p = true;
31105 ++parser->num_template_parameter_lists;
31107 else
31109 /* Roll back the innermost template parameter list such that it may be
31110 extended in the loop below as if it were being explicitly declared. */
31112 gcc_assert (current_template_parms);
31114 /* Pop the innermost template parms into TPARMS. */
31115 tree inner_vec = INNERMOST_TEMPLATE_PARMS (current_template_parms);
31116 current_template_parms = TREE_CHAIN (current_template_parms);
31118 size_t inner_vec_len = TREE_VEC_LENGTH (inner_vec);
31119 if (inner_vec_len != 0)
31121 tree t = tparms = TREE_VEC_ELT (inner_vec, 0);
31122 for (size_t n = 1; n < inner_vec_len; ++n)
31123 t = TREE_CHAIN (t) = TREE_VEC_ELT (inner_vec, n);
31126 ++processing_template_parmlist;
31129 for (tree p = parameters; p && synth_count < expect_count; p = TREE_CHAIN (p))
31131 tree generic_type_ptr
31132 = find_type_usage (TREE_VALUE (p), tree_type_is_auto_or_concept);
31134 if (!generic_type_ptr)
31135 continue;
31137 ++synth_count;
31139 tree synth_id = make_generic_type_name ();
31140 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
31141 synth_id);
31142 tparms = process_template_parm (tparms, DECL_SOURCE_LOCATION (TREE_VALUE
31143 (p)),
31144 build_tree_list (NULL_TREE,
31145 synth_tmpl_parm),
31146 /*non_type=*/false,
31147 /*param_pack=*/false);
31149 /* Rewrite the type of P to be the template_parm added above (getdecls is
31150 used to retrieve it since it is the most recent declaration in this
31151 scope). Qualifiers need to be preserved also. */
31153 tree& cur_type = TREE_TYPE (generic_type_ptr);
31154 tree new_type = TREE_TYPE (getdecls ());
31156 if (TYPE_QUALS (cur_type))
31157 cur_type = cp_build_qualified_type (new_type, TYPE_QUALS (cur_type));
31158 else
31159 cur_type = new_type;
31162 gcc_assert (synth_count == expect_count);
31164 push_binding_level (fn_parms_scope);
31166 end_template_parm_list (tparms);
31168 return parameters;
31171 /* Finish the declaration of a fully implicit function template. Such a
31172 template has no explicit template parameter list so has not been through the
31173 normal template head and tail processing. add_implicit_template_parms tries
31174 to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
31175 provided if the declaration is a class member such that its template
31176 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
31177 form is returned. Otherwise NULL_TREE is returned. */
31179 tree
31180 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
31182 gcc_assert (parser->fully_implicit_function_template_p);
31184 if (member_decl_opt && member_decl_opt != error_mark_node
31185 && DECL_VIRTUAL_P (member_decl_opt))
31187 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
31188 "implicit templates may not be %<virtual%>");
31189 DECL_VIRTUAL_P (member_decl_opt) = false;
31192 pop_deferring_access_checks ();
31193 if (member_decl_opt)
31194 member_decl_opt = finish_member_template_decl (member_decl_opt);
31195 end_template_decl ();
31197 parser->fully_implicit_function_template_p = false;
31198 --parser->num_template_parameter_lists;
31200 return member_decl_opt;
31203 #include "gt-cp-parser.h"