* parser.h (struct cp_token): Rename ambiguous_p to error_reported.
[official-gcc.git] / gcc / cp / parser.c
blobbb59e3bcdd19b3d99c06665b85d1ba445434b112
1 /* C++ Parser.
2 Copyright (C) 2000-2014 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 "print-tree.h"
29 #include "stringpool.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "cp-tree.h"
33 #include "intl.h"
34 #include "c-family/c-pragma.h"
35 #include "decl.h"
36 #include "flags.h"
37 #include "diagnostic-core.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "plugin.h"
43 #include "tree-pretty-print.h"
44 #include "parser.h"
45 #include "type-utils.h"
46 #include "omp-low.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 typedef enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
116 } non_integral_constant;
118 /* The various kinds of errors about name-lookup failing. */
119 typedef enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
128 } name_lookup_error;
130 /* The various kinds of required token */
131 typedef enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_INTERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
184 } required_token;
186 /* Prototypes. */
188 static cp_lexer *cp_lexer_new_main
189 (void);
190 static cp_lexer *cp_lexer_new_from_tokens
191 (cp_token_cache *tokens);
192 static void cp_lexer_destroy
193 (cp_lexer *);
194 static int cp_lexer_saving_tokens
195 (const cp_lexer *);
196 static cp_token *cp_lexer_token_at
197 (cp_lexer *, cp_token_position);
198 static void cp_lexer_get_preprocessor_token
199 (cp_lexer *, cp_token *);
200 static inline cp_token *cp_lexer_peek_token
201 (cp_lexer *);
202 static cp_token *cp_lexer_peek_nth_token
203 (cp_lexer *, size_t);
204 static inline bool cp_lexer_next_token_is
205 (cp_lexer *, enum cpp_ttype);
206 static bool cp_lexer_next_token_is_not
207 (cp_lexer *, enum cpp_ttype);
208 static bool cp_lexer_next_token_is_keyword
209 (cp_lexer *, enum rid);
210 static cp_token *cp_lexer_consume_token
211 (cp_lexer *);
212 static void cp_lexer_purge_token
213 (cp_lexer *);
214 static void cp_lexer_purge_tokens_after
215 (cp_lexer *, cp_token_position);
216 static void cp_lexer_save_tokens
217 (cp_lexer *);
218 static void cp_lexer_commit_tokens
219 (cp_lexer *);
220 static void cp_lexer_rollback_tokens
221 (cp_lexer *);
222 static void cp_lexer_print_token
223 (FILE *, cp_token *);
224 static inline bool cp_lexer_debugging_p
225 (cp_lexer *);
226 static void cp_lexer_start_debugging
227 (cp_lexer *) ATTRIBUTE_UNUSED;
228 static void cp_lexer_stop_debugging
229 (cp_lexer *) ATTRIBUTE_UNUSED;
231 static cp_token_cache *cp_token_cache_new
232 (cp_token *, cp_token *);
234 static void cp_parser_initial_pragma
235 (cp_token *);
237 static tree cp_literal_operator_id
238 (const char *);
240 static void cp_parser_cilk_simd
241 (cp_parser *, cp_token *);
242 static bool cp_parser_omp_declare_reduction_exprs
243 (tree, cp_parser *);
244 static tree cp_parser_cilk_simd_vectorlength
245 (cp_parser *, tree, bool);
247 /* Manifest constants. */
248 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
249 #define CP_SAVED_TOKEN_STACK 5
251 /* Variables. */
253 /* The stream to which debugging output should be written. */
254 static FILE *cp_lexer_debug_stream;
256 /* Nonzero if we are parsing an unevaluated operand: an operand to
257 sizeof, typeof, or alignof. */
258 int cp_unevaluated_operand;
260 /* Dump up to NUM tokens in BUFFER to FILE starting with token
261 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
262 first token in BUFFER. If NUM is 0, dump all the tokens. If
263 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
264 highlighted by surrounding it in [[ ]]. */
266 static void
267 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
268 cp_token *start_token, unsigned num,
269 cp_token *curr_token)
271 unsigned i, nprinted;
272 cp_token *token;
273 bool do_print;
275 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
277 if (buffer == NULL)
278 return;
280 if (num == 0)
281 num = buffer->length ();
283 if (start_token == NULL)
284 start_token = buffer->address ();
286 if (start_token > buffer->address ())
288 cp_lexer_print_token (file, &(*buffer)[0]);
289 fprintf (file, " ... ");
292 do_print = false;
293 nprinted = 0;
294 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
296 if (token == start_token)
297 do_print = true;
299 if (!do_print)
300 continue;
302 nprinted++;
303 if (token == curr_token)
304 fprintf (file, "[[");
306 cp_lexer_print_token (file, token);
308 if (token == curr_token)
309 fprintf (file, "]]");
311 switch (token->type)
313 case CPP_SEMICOLON:
314 case CPP_OPEN_BRACE:
315 case CPP_CLOSE_BRACE:
316 case CPP_EOF:
317 fputc ('\n', file);
318 break;
320 default:
321 fputc (' ', file);
325 if (i == num && i < buffer->length ())
327 fprintf (file, " ... ");
328 cp_lexer_print_token (file, &buffer->last ());
331 fprintf (file, "\n");
335 /* Dump all tokens in BUFFER to stderr. */
337 void
338 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
340 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
343 DEBUG_FUNCTION void
344 debug (vec<cp_token, va_gc> &ref)
346 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
349 DEBUG_FUNCTION void
350 debug (vec<cp_token, va_gc> *ptr)
352 if (ptr)
353 debug (*ptr);
354 else
355 fprintf (stderr, "<nil>\n");
359 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
360 description for T. */
362 static void
363 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
365 if (t)
367 fprintf (file, "%s: ", desc);
368 print_node_brief (file, "", t, 0);
373 /* Dump parser context C to FILE. */
375 static void
376 cp_debug_print_context (FILE *file, cp_parser_context *c)
378 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
379 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
380 print_node_brief (file, "", c->object_type, 0);
381 fprintf (file, "}\n");
385 /* Print the stack of parsing contexts to FILE starting with FIRST. */
387 static void
388 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
390 unsigned i;
391 cp_parser_context *c;
393 fprintf (file, "Parsing context stack:\n");
394 for (i = 0, c = first; c; c = c->next, i++)
396 fprintf (file, "\t#%u: ", i);
397 cp_debug_print_context (file, c);
402 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
404 static void
405 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
407 if (flag)
408 fprintf (file, "%s: true\n", desc);
412 /* Print an unparsed function entry UF to FILE. */
414 static void
415 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
417 unsigned i;
418 cp_default_arg_entry *default_arg_fn;
419 tree fn;
421 fprintf (file, "\tFunctions with default args:\n");
422 for (i = 0;
423 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
424 i++)
426 fprintf (file, "\t\tClass type: ");
427 print_node_brief (file, "", default_arg_fn->class_type, 0);
428 fprintf (file, "\t\tDeclaration: ");
429 print_node_brief (file, "", default_arg_fn->decl, 0);
430 fprintf (file, "\n");
433 fprintf (file, "\n\tFunctions with definitions that require "
434 "post-processing\n\t\t");
435 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
437 print_node_brief (file, "", fn, 0);
438 fprintf (file, " ");
440 fprintf (file, "\n");
442 fprintf (file, "\n\tNon-static data members with initializers that require "
443 "post-processing\n\t\t");
444 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
446 print_node_brief (file, "", fn, 0);
447 fprintf (file, " ");
449 fprintf (file, "\n");
453 /* Print the stack of unparsed member functions S to FILE. */
455 static void
456 cp_debug_print_unparsed_queues (FILE *file,
457 vec<cp_unparsed_functions_entry, va_gc> *s)
459 unsigned i;
460 cp_unparsed_functions_entry *uf;
462 fprintf (file, "Unparsed functions\n");
463 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
465 fprintf (file, "#%u:\n", i);
466 cp_debug_print_unparsed_function (file, uf);
471 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
472 the given PARSER. If FILE is NULL, the output is printed on stderr. */
474 static void
475 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
477 cp_token *next_token, *first_token, *start_token;
479 if (file == NULL)
480 file = stderr;
482 next_token = parser->lexer->next_token;
483 first_token = parser->lexer->buffer->address ();
484 start_token = (next_token > first_token + window_size / 2)
485 ? next_token - window_size / 2
486 : first_token;
487 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
488 next_token);
492 /* Dump debugging information for the given PARSER. If FILE is NULL,
493 the output is printed on stderr. */
495 void
496 cp_debug_parser (FILE *file, cp_parser *parser)
498 const size_t window_size = 20;
499 cp_token *token;
500 expanded_location eloc;
502 if (file == NULL)
503 file = stderr;
505 fprintf (file, "Parser state\n\n");
506 fprintf (file, "Number of tokens: %u\n",
507 vec_safe_length (parser->lexer->buffer));
508 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
509 cp_debug_print_tree_if_set (file, "Object scope",
510 parser->object_scope);
511 cp_debug_print_tree_if_set (file, "Qualifying scope",
512 parser->qualifying_scope);
513 cp_debug_print_context_stack (file, parser->context);
514 cp_debug_print_flag (file, "Allow GNU extensions",
515 parser->allow_gnu_extensions_p);
516 cp_debug_print_flag (file, "'>' token is greater-than",
517 parser->greater_than_is_operator_p);
518 cp_debug_print_flag (file, "Default args allowed in current "
519 "parameter list", parser->default_arg_ok_p);
520 cp_debug_print_flag (file, "Parsing integral constant-expression",
521 parser->integral_constant_expression_p);
522 cp_debug_print_flag (file, "Allow non-constant expression in current "
523 "constant-expression",
524 parser->allow_non_integral_constant_expression_p);
525 cp_debug_print_flag (file, "Seen non-constant expression",
526 parser->non_integral_constant_expression_p);
527 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
528 "current context",
529 parser->local_variables_forbidden_p);
530 cp_debug_print_flag (file, "In unbraced linkage specification",
531 parser->in_unbraced_linkage_specification_p);
532 cp_debug_print_flag (file, "Parsing a declarator",
533 parser->in_declarator_p);
534 cp_debug_print_flag (file, "In template argument list",
535 parser->in_template_argument_list_p);
536 cp_debug_print_flag (file, "Parsing an iteration statement",
537 parser->in_statement & IN_ITERATION_STMT);
538 cp_debug_print_flag (file, "Parsing a switch statement",
539 parser->in_statement & IN_SWITCH_STMT);
540 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
541 parser->in_statement & IN_OMP_BLOCK);
542 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
543 parser->in_statement & IN_CILK_SIMD_FOR);
544 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
545 parser->in_statement & IN_OMP_FOR);
546 cp_debug_print_flag (file, "Parsing an if statement",
547 parser->in_statement & IN_IF_STMT);
548 cp_debug_print_flag (file, "Parsing a type-id in an expression "
549 "context", parser->in_type_id_in_expr_p);
550 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
551 parser->implicit_extern_c);
552 cp_debug_print_flag (file, "String expressions should be translated "
553 "to execution character set",
554 parser->translate_strings_p);
555 cp_debug_print_flag (file, "Parsing function body outside of a "
556 "local class", parser->in_function_body);
557 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
558 parser->colon_corrects_to_scope_p);
559 cp_debug_print_flag (file, "Colon doesn't start a class definition",
560 parser->colon_doesnt_start_class_def_p);
561 if (parser->type_definition_forbidden_message)
562 fprintf (file, "Error message for forbidden type definitions: %s\n",
563 parser->type_definition_forbidden_message);
564 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
565 fprintf (file, "Number of class definitions in progress: %u\n",
566 parser->num_classes_being_defined);
567 fprintf (file, "Number of template parameter lists for the current "
568 "declaration: %u\n", parser->num_template_parameter_lists);
569 cp_debug_parser_tokens (file, parser, window_size);
570 token = parser->lexer->next_token;
571 fprintf (file, "Next token to parse:\n");
572 fprintf (file, "\tToken: ");
573 cp_lexer_print_token (file, token);
574 eloc = expand_location (token->location);
575 fprintf (file, "\n\tFile: %s\n", eloc.file);
576 fprintf (file, "\tLine: %d\n", eloc.line);
577 fprintf (file, "\tColumn: %d\n", eloc.column);
580 DEBUG_FUNCTION void
581 debug (cp_parser &ref)
583 cp_debug_parser (stderr, &ref);
586 DEBUG_FUNCTION void
587 debug (cp_parser *ptr)
589 if (ptr)
590 debug (*ptr);
591 else
592 fprintf (stderr, "<nil>\n");
595 /* Allocate memory for a new lexer object and return it. */
597 static cp_lexer *
598 cp_lexer_alloc (void)
600 cp_lexer *lexer;
602 c_common_no_more_pch ();
604 /* Allocate the memory. */
605 lexer = ggc_alloc_cleared_cp_lexer ();
607 /* Initially we are not debugging. */
608 lexer->debugging_p = false;
610 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
612 /* Create the buffer. */
613 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
615 return lexer;
619 /* Create a new main C++ lexer, the lexer that gets tokens from the
620 preprocessor. */
622 static cp_lexer *
623 cp_lexer_new_main (void)
625 cp_lexer *lexer;
626 cp_token token;
628 /* It's possible that parsing the first pragma will load a PCH file,
629 which is a GC collection point. So we have to do that before
630 allocating any memory. */
631 cp_parser_initial_pragma (&token);
633 lexer = cp_lexer_alloc ();
635 /* Put the first token in the buffer. */
636 lexer->buffer->quick_push (token);
638 /* Get the remaining tokens from the preprocessor. */
639 while (token.type != CPP_EOF)
641 cp_lexer_get_preprocessor_token (lexer, &token);
642 vec_safe_push (lexer->buffer, token);
645 lexer->last_token = lexer->buffer->address ()
646 + lexer->buffer->length ()
647 - 1;
648 lexer->next_token = lexer->buffer->length ()
649 ? lexer->buffer->address ()
650 : &eof_token;
652 /* Subsequent preprocessor diagnostics should use compiler
653 diagnostic functions to get the compiler source location. */
654 done_lexing = true;
656 gcc_assert (!lexer->next_token->purged_p);
657 return lexer;
660 /* Create a new lexer whose token stream is primed with the tokens in
661 CACHE. When these tokens are exhausted, no new tokens will be read. */
663 static cp_lexer *
664 cp_lexer_new_from_tokens (cp_token_cache *cache)
666 cp_token *first = cache->first;
667 cp_token *last = cache->last;
668 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
670 /* We do not own the buffer. */
671 lexer->buffer = NULL;
672 lexer->next_token = first == last ? &eof_token : first;
673 lexer->last_token = last;
675 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
677 /* Initially we are not debugging. */
678 lexer->debugging_p = false;
680 gcc_assert (!lexer->next_token->purged_p);
681 return lexer;
684 /* Frees all resources associated with LEXER. */
686 static void
687 cp_lexer_destroy (cp_lexer *lexer)
689 vec_free (lexer->buffer);
690 lexer->saved_tokens.release ();
691 ggc_free (lexer);
694 /* Returns nonzero if debugging information should be output. */
696 static inline bool
697 cp_lexer_debugging_p (cp_lexer *lexer)
699 return lexer->debugging_p;
703 static inline cp_token_position
704 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
706 gcc_assert (!previous_p || lexer->next_token != &eof_token);
708 return lexer->next_token - previous_p;
711 static inline cp_token *
712 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
714 return pos;
717 static inline void
718 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
720 lexer->next_token = cp_lexer_token_at (lexer, pos);
723 static inline cp_token_position
724 cp_lexer_previous_token_position (cp_lexer *lexer)
726 if (lexer->next_token == &eof_token)
727 return lexer->last_token - 1;
728 else
729 return cp_lexer_token_position (lexer, true);
732 static inline cp_token *
733 cp_lexer_previous_token (cp_lexer *lexer)
735 cp_token_position tp = cp_lexer_previous_token_position (lexer);
737 return cp_lexer_token_at (lexer, tp);
740 /* nonzero if we are presently saving tokens. */
742 static inline int
743 cp_lexer_saving_tokens (const cp_lexer* lexer)
745 return lexer->saved_tokens.length () != 0;
748 /* Store the next token from the preprocessor in *TOKEN. Return true
749 if we reach EOF. If LEXER is NULL, assume we are handling an
750 initial #pragma pch_preprocess, and thus want the lexer to return
751 processed strings. */
753 static void
754 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
756 static int is_extern_c = 0;
758 /* Get a new token from the preprocessor. */
759 token->type
760 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
761 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
762 token->keyword = RID_MAX;
763 token->pragma_kind = PRAGMA_NONE;
764 token->purged_p = false;
765 token->error_reported = false;
767 /* On some systems, some header files are surrounded by an
768 implicit extern "C" block. Set a flag in the token if it
769 comes from such a header. */
770 is_extern_c += pending_lang_change;
771 pending_lang_change = 0;
772 token->implicit_extern_c = is_extern_c > 0;
774 /* Check to see if this token is a keyword. */
775 if (token->type == CPP_NAME)
777 if (C_IS_RESERVED_WORD (token->u.value))
779 /* Mark this token as a keyword. */
780 token->type = CPP_KEYWORD;
781 /* Record which keyword. */
782 token->keyword = C_RID_CODE (token->u.value);
784 else
786 if (warn_cxx0x_compat
787 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
788 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
790 /* Warn about the C++0x keyword (but still treat it as
791 an identifier). */
792 warning (OPT_Wc__0x_compat,
793 "identifier %qE is a keyword in C++11",
794 token->u.value);
796 /* Clear out the C_RID_CODE so we don't warn about this
797 particular identifier-turned-keyword again. */
798 C_SET_RID_CODE (token->u.value, RID_MAX);
801 token->keyword = RID_MAX;
804 else if (token->type == CPP_AT_NAME)
806 /* This only happens in Objective-C++; it must be a keyword. */
807 token->type = CPP_KEYWORD;
808 switch (C_RID_CODE (token->u.value))
810 /* Replace 'class' with '@class', 'private' with '@private',
811 etc. This prevents confusion with the C++ keyword
812 'class', and makes the tokens consistent with other
813 Objective-C 'AT' keywords. For example '@class' is
814 reported as RID_AT_CLASS which is consistent with
815 '@synchronized', which is reported as
816 RID_AT_SYNCHRONIZED.
818 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
819 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
820 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
821 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
822 case RID_THROW: token->keyword = RID_AT_THROW; break;
823 case RID_TRY: token->keyword = RID_AT_TRY; break;
824 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
825 default: token->keyword = C_RID_CODE (token->u.value);
828 else if (token->type == CPP_PRAGMA)
830 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
831 token->pragma_kind = ((enum pragma_kind)
832 TREE_INT_CST_LOW (token->u.value));
833 token->u.value = NULL_TREE;
837 /* Update the globals input_location and the input file stack from TOKEN. */
838 static inline void
839 cp_lexer_set_source_position_from_token (cp_token *token)
841 if (token->type != CPP_EOF)
843 input_location = token->location;
847 /* Update the globals input_location and the input file stack from LEXER. */
848 static inline void
849 cp_lexer_set_source_position (cp_lexer *lexer)
851 cp_token *token = cp_lexer_peek_token (lexer);
852 cp_lexer_set_source_position_from_token (token);
855 /* Return a pointer to the next token in the token stream, but do not
856 consume it. */
858 static inline cp_token *
859 cp_lexer_peek_token (cp_lexer *lexer)
861 if (cp_lexer_debugging_p (lexer))
863 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
864 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
865 putc ('\n', cp_lexer_debug_stream);
867 return lexer->next_token;
870 /* Return true if the next token has the indicated TYPE. */
872 static inline bool
873 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
875 return cp_lexer_peek_token (lexer)->type == type;
878 /* Return true if the next token does not have the indicated TYPE. */
880 static inline bool
881 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
883 return !cp_lexer_next_token_is (lexer, type);
886 /* Return true if the next token is the indicated KEYWORD. */
888 static inline bool
889 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
891 return cp_lexer_peek_token (lexer)->keyword == keyword;
894 static inline bool
895 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
897 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
900 /* Return true if the next token is not the indicated KEYWORD. */
902 static inline bool
903 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
905 return cp_lexer_peek_token (lexer)->keyword != keyword;
908 /* Return true if the next token is a keyword for a decl-specifier. */
910 static bool
911 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
913 cp_token *token;
915 token = cp_lexer_peek_token (lexer);
916 switch (token->keyword)
918 /* auto specifier: storage-class-specifier in C++,
919 simple-type-specifier in C++0x. */
920 case RID_AUTO:
921 /* Storage classes. */
922 case RID_REGISTER:
923 case RID_STATIC:
924 case RID_EXTERN:
925 case RID_MUTABLE:
926 case RID_THREAD:
927 /* Elaborated type specifiers. */
928 case RID_ENUM:
929 case RID_CLASS:
930 case RID_STRUCT:
931 case RID_UNION:
932 case RID_TYPENAME:
933 /* Simple type specifiers. */
934 case RID_CHAR:
935 case RID_CHAR16:
936 case RID_CHAR32:
937 case RID_WCHAR:
938 case RID_BOOL:
939 case RID_SHORT:
940 case RID_INT:
941 case RID_LONG:
942 case RID_INT128:
943 case RID_SIGNED:
944 case RID_UNSIGNED:
945 case RID_FLOAT:
946 case RID_DOUBLE:
947 case RID_VOID:
948 /* GNU extensions. */
949 case RID_ATTRIBUTE:
950 case RID_TYPEOF:
951 /* C++0x extensions. */
952 case RID_DECLTYPE:
953 case RID_UNDERLYING_TYPE:
954 return true;
956 default:
957 return false;
961 /* Returns TRUE iff the token T begins a decltype type. */
963 static bool
964 token_is_decltype (cp_token *t)
966 return (t->keyword == RID_DECLTYPE
967 || t->type == CPP_DECLTYPE);
970 /* Returns TRUE iff the next token begins a decltype type. */
972 static bool
973 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
975 cp_token *t = cp_lexer_peek_token (lexer);
976 return token_is_decltype (t);
979 /* Return a pointer to the Nth token in the token stream. If N is 1,
980 then this is precisely equivalent to cp_lexer_peek_token (except
981 that it is not inline). One would like to disallow that case, but
982 there is one case (cp_parser_nth_token_starts_template_id) where
983 the caller passes a variable for N and it might be 1. */
985 static cp_token *
986 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
988 cp_token *token;
990 /* N is 1-based, not zero-based. */
991 gcc_assert (n > 0);
993 if (cp_lexer_debugging_p (lexer))
994 fprintf (cp_lexer_debug_stream,
995 "cp_lexer: peeking ahead %ld at token: ", (long)n);
997 --n;
998 token = lexer->next_token;
999 gcc_assert (!n || token != &eof_token);
1000 while (n != 0)
1002 ++token;
1003 if (token == lexer->last_token)
1005 token = &eof_token;
1006 break;
1009 if (!token->purged_p)
1010 --n;
1013 if (cp_lexer_debugging_p (lexer))
1015 cp_lexer_print_token (cp_lexer_debug_stream, token);
1016 putc ('\n', cp_lexer_debug_stream);
1019 return token;
1022 /* Return the next token, and advance the lexer's next_token pointer
1023 to point to the next non-purged token. */
1025 static cp_token *
1026 cp_lexer_consume_token (cp_lexer* lexer)
1028 cp_token *token = lexer->next_token;
1030 gcc_assert (token != &eof_token);
1031 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1035 lexer->next_token++;
1036 if (lexer->next_token == lexer->last_token)
1038 lexer->next_token = &eof_token;
1039 break;
1043 while (lexer->next_token->purged_p);
1045 cp_lexer_set_source_position_from_token (token);
1047 /* Provide debugging output. */
1048 if (cp_lexer_debugging_p (lexer))
1050 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1051 cp_lexer_print_token (cp_lexer_debug_stream, token);
1052 putc ('\n', cp_lexer_debug_stream);
1055 return token;
1058 /* Permanently remove the next token from the token stream, and
1059 advance the next_token pointer to refer to the next non-purged
1060 token. */
1062 static void
1063 cp_lexer_purge_token (cp_lexer *lexer)
1065 cp_token *tok = lexer->next_token;
1067 gcc_assert (tok != &eof_token);
1068 tok->purged_p = true;
1069 tok->location = UNKNOWN_LOCATION;
1070 tok->u.value = NULL_TREE;
1071 tok->keyword = RID_MAX;
1075 tok++;
1076 if (tok == lexer->last_token)
1078 tok = &eof_token;
1079 break;
1082 while (tok->purged_p);
1083 lexer->next_token = tok;
1086 /* Permanently remove all tokens after TOK, up to, but not
1087 including, the token that will be returned next by
1088 cp_lexer_peek_token. */
1090 static void
1091 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1093 cp_token *peek = lexer->next_token;
1095 if (peek == &eof_token)
1096 peek = lexer->last_token;
1098 gcc_assert (tok < peek);
1100 for ( tok += 1; tok != peek; tok += 1)
1102 tok->purged_p = true;
1103 tok->location = UNKNOWN_LOCATION;
1104 tok->u.value = NULL_TREE;
1105 tok->keyword = RID_MAX;
1109 /* Begin saving tokens. All tokens consumed after this point will be
1110 preserved. */
1112 static void
1113 cp_lexer_save_tokens (cp_lexer* lexer)
1115 /* Provide debugging output. */
1116 if (cp_lexer_debugging_p (lexer))
1117 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1119 lexer->saved_tokens.safe_push (lexer->next_token);
1122 /* Commit to the portion of the token stream most recently saved. */
1124 static void
1125 cp_lexer_commit_tokens (cp_lexer* lexer)
1127 /* Provide debugging output. */
1128 if (cp_lexer_debugging_p (lexer))
1129 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1131 lexer->saved_tokens.pop ();
1134 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1135 to the token stream. Stop saving tokens. */
1137 static void
1138 cp_lexer_rollback_tokens (cp_lexer* lexer)
1140 /* Provide debugging output. */
1141 if (cp_lexer_debugging_p (lexer))
1142 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1144 lexer->next_token = lexer->saved_tokens.pop ();
1147 /* Print a representation of the TOKEN on the STREAM. */
1149 static void
1150 cp_lexer_print_token (FILE * stream, cp_token *token)
1152 /* We don't use cpp_type2name here because the parser defines
1153 a few tokens of its own. */
1154 static const char *const token_names[] = {
1155 /* cpplib-defined token types */
1156 #define OP(e, s) #e,
1157 #define TK(e, s) #e,
1158 TTYPE_TABLE
1159 #undef OP
1160 #undef TK
1161 /* C++ parser token types - see "Manifest constants", above. */
1162 "KEYWORD",
1163 "TEMPLATE_ID",
1164 "NESTED_NAME_SPECIFIER",
1167 /* For some tokens, print the associated data. */
1168 switch (token->type)
1170 case CPP_KEYWORD:
1171 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1172 For example, `struct' is mapped to an INTEGER_CST. */
1173 if (!identifier_p (token->u.value))
1174 break;
1175 /* else fall through */
1176 case CPP_NAME:
1177 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1178 break;
1180 case CPP_STRING:
1181 case CPP_STRING16:
1182 case CPP_STRING32:
1183 case CPP_WSTRING:
1184 case CPP_UTF8STRING:
1185 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1186 break;
1188 case CPP_NUMBER:
1189 print_generic_expr (stream, token->u.value, 0);
1190 break;
1192 default:
1193 /* If we have a name for the token, print it out. Otherwise, we
1194 simply give the numeric code. */
1195 if (token->type < ARRAY_SIZE(token_names))
1196 fputs (token_names[token->type], stream);
1197 else
1198 fprintf (stream, "[%d]", token->type);
1199 break;
1203 DEBUG_FUNCTION void
1204 debug (cp_token &ref)
1206 cp_lexer_print_token (stderr, &ref);
1207 fprintf (stderr, "\n");
1210 DEBUG_FUNCTION void
1211 debug (cp_token *ptr)
1213 if (ptr)
1214 debug (*ptr);
1215 else
1216 fprintf (stderr, "<nil>\n");
1220 /* Start emitting debugging information. */
1222 static void
1223 cp_lexer_start_debugging (cp_lexer* lexer)
1225 lexer->debugging_p = true;
1226 cp_lexer_debug_stream = stderr;
1229 /* Stop emitting debugging information. */
1231 static void
1232 cp_lexer_stop_debugging (cp_lexer* lexer)
1234 lexer->debugging_p = false;
1235 cp_lexer_debug_stream = NULL;
1238 /* Create a new cp_token_cache, representing a range of tokens. */
1240 static cp_token_cache *
1241 cp_token_cache_new (cp_token *first, cp_token *last)
1243 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1244 cache->first = first;
1245 cache->last = last;
1246 return cache;
1249 /* Diagnose if #pragma omp declare simd isn't followed immediately
1250 by function declaration or definition. */
1252 static inline void
1253 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1255 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1257 error ("%<#pragma omp declare simd%> not immediately followed by "
1258 "function declaration or definition");
1259 parser->omp_declare_simd = NULL;
1263 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1264 and put that into "omp declare simd" attribute. */
1266 static inline void
1267 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1269 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1271 if (fndecl == error_mark_node)
1273 parser->omp_declare_simd = NULL;
1274 return;
1276 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1278 cp_ensure_no_omp_declare_simd (parser);
1279 return;
1284 /* Decl-specifiers. */
1286 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1288 static void
1289 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1291 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1294 /* Declarators. */
1296 /* Nothing other than the parser should be creating declarators;
1297 declarators are a semi-syntactic representation of C++ entities.
1298 Other parts of the front end that need to create entities (like
1299 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1301 static cp_declarator *make_call_declarator
1302 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1303 static cp_declarator *make_array_declarator
1304 (cp_declarator *, tree);
1305 static cp_declarator *make_pointer_declarator
1306 (cp_cv_quals, cp_declarator *, tree);
1307 static cp_declarator *make_reference_declarator
1308 (cp_cv_quals, cp_declarator *, bool, tree);
1309 static cp_parameter_declarator *make_parameter_declarator
1310 (cp_decl_specifier_seq *, cp_declarator *, tree);
1311 static cp_declarator *make_ptrmem_declarator
1312 (cp_cv_quals, tree, cp_declarator *, tree);
1314 /* An erroneous declarator. */
1315 static cp_declarator *cp_error_declarator;
1317 /* The obstack on which declarators and related data structures are
1318 allocated. */
1319 static struct obstack declarator_obstack;
1321 /* Alloc BYTES from the declarator memory pool. */
1323 static inline void *
1324 alloc_declarator (size_t bytes)
1326 return obstack_alloc (&declarator_obstack, bytes);
1329 /* Allocate a declarator of the indicated KIND. Clear fields that are
1330 common to all declarators. */
1332 static cp_declarator *
1333 make_declarator (cp_declarator_kind kind)
1335 cp_declarator *declarator;
1337 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1338 declarator->kind = kind;
1339 declarator->attributes = NULL_TREE;
1340 declarator->std_attributes = NULL_TREE;
1341 declarator->declarator = NULL;
1342 declarator->parameter_pack_p = false;
1343 declarator->id_loc = UNKNOWN_LOCATION;
1345 return declarator;
1348 /* Make a declarator for a generalized identifier. If
1349 QUALIFYING_SCOPE is non-NULL, the identifier is
1350 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1351 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1352 is, if any. */
1354 static cp_declarator *
1355 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1356 special_function_kind sfk)
1358 cp_declarator *declarator;
1360 /* It is valid to write:
1362 class C { void f(); };
1363 typedef C D;
1364 void D::f();
1366 The standard is not clear about whether `typedef const C D' is
1367 legal; as of 2002-09-15 the committee is considering that
1368 question. EDG 3.0 allows that syntax. Therefore, we do as
1369 well. */
1370 if (qualifying_scope && TYPE_P (qualifying_scope))
1371 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1373 gcc_assert (identifier_p (unqualified_name)
1374 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1375 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1377 declarator = make_declarator (cdk_id);
1378 declarator->u.id.qualifying_scope = qualifying_scope;
1379 declarator->u.id.unqualified_name = unqualified_name;
1380 declarator->u.id.sfk = sfk;
1382 return declarator;
1385 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1386 of modifiers such as const or volatile to apply to the pointer
1387 type, represented as identifiers. ATTRIBUTES represent the attributes that
1388 appertain to the pointer or reference. */
1390 cp_declarator *
1391 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1392 tree attributes)
1394 cp_declarator *declarator;
1396 declarator = make_declarator (cdk_pointer);
1397 declarator->declarator = target;
1398 declarator->u.pointer.qualifiers = cv_qualifiers;
1399 declarator->u.pointer.class_type = NULL_TREE;
1400 if (target)
1402 declarator->id_loc = target->id_loc;
1403 declarator->parameter_pack_p = target->parameter_pack_p;
1404 target->parameter_pack_p = false;
1406 else
1407 declarator->parameter_pack_p = false;
1409 declarator->std_attributes = attributes;
1411 return declarator;
1414 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1415 represent the attributes that appertain to the pointer or
1416 reference. */
1418 cp_declarator *
1419 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1420 bool rvalue_ref, tree attributes)
1422 cp_declarator *declarator;
1424 declarator = make_declarator (cdk_reference);
1425 declarator->declarator = target;
1426 declarator->u.reference.qualifiers = cv_qualifiers;
1427 declarator->u.reference.rvalue_ref = rvalue_ref;
1428 if (target)
1430 declarator->id_loc = target->id_loc;
1431 declarator->parameter_pack_p = target->parameter_pack_p;
1432 target->parameter_pack_p = false;
1434 else
1435 declarator->parameter_pack_p = false;
1437 declarator->std_attributes = attributes;
1439 return declarator;
1442 /* Like make_pointer_declarator -- but for a pointer to a non-static
1443 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1444 appertain to the pointer or reference. */
1446 cp_declarator *
1447 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1448 cp_declarator *pointee,
1449 tree attributes)
1451 cp_declarator *declarator;
1453 declarator = make_declarator (cdk_ptrmem);
1454 declarator->declarator = pointee;
1455 declarator->u.pointer.qualifiers = cv_qualifiers;
1456 declarator->u.pointer.class_type = class_type;
1458 if (pointee)
1460 declarator->parameter_pack_p = pointee->parameter_pack_p;
1461 pointee->parameter_pack_p = false;
1463 else
1464 declarator->parameter_pack_p = false;
1466 declarator->std_attributes = attributes;
1468 return declarator;
1471 /* Make a declarator for the function given by TARGET, with the
1472 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1473 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1474 indicates what exceptions can be thrown. */
1476 cp_declarator *
1477 make_call_declarator (cp_declarator *target,
1478 tree parms,
1479 cp_cv_quals cv_qualifiers,
1480 cp_virt_specifiers virt_specifiers,
1481 cp_ref_qualifier ref_qualifier,
1482 tree exception_specification,
1483 tree late_return_type)
1485 cp_declarator *declarator;
1487 declarator = make_declarator (cdk_function);
1488 declarator->declarator = target;
1489 declarator->u.function.parameters = parms;
1490 declarator->u.function.qualifiers = cv_qualifiers;
1491 declarator->u.function.virt_specifiers = virt_specifiers;
1492 declarator->u.function.ref_qualifier = ref_qualifier;
1493 declarator->u.function.exception_specification = exception_specification;
1494 declarator->u.function.late_return_type = late_return_type;
1495 if (target)
1497 declarator->id_loc = target->id_loc;
1498 declarator->parameter_pack_p = target->parameter_pack_p;
1499 target->parameter_pack_p = false;
1501 else
1502 declarator->parameter_pack_p = false;
1504 return declarator;
1507 /* Make a declarator for an array of BOUNDS elements, each of which is
1508 defined by ELEMENT. */
1510 cp_declarator *
1511 make_array_declarator (cp_declarator *element, tree bounds)
1513 cp_declarator *declarator;
1515 declarator = make_declarator (cdk_array);
1516 declarator->declarator = element;
1517 declarator->u.array.bounds = bounds;
1518 if (element)
1520 declarator->id_loc = element->id_loc;
1521 declarator->parameter_pack_p = element->parameter_pack_p;
1522 element->parameter_pack_p = false;
1524 else
1525 declarator->parameter_pack_p = false;
1527 return declarator;
1530 /* Determine whether the declarator we've seen so far can be a
1531 parameter pack, when followed by an ellipsis. */
1532 static bool
1533 declarator_can_be_parameter_pack (cp_declarator *declarator)
1535 /* Search for a declarator name, or any other declarator that goes
1536 after the point where the ellipsis could appear in a parameter
1537 pack. If we find any of these, then this declarator can not be
1538 made into a parameter pack. */
1539 bool found = false;
1540 while (declarator && !found)
1542 switch ((int)declarator->kind)
1544 case cdk_id:
1545 case cdk_array:
1546 found = true;
1547 break;
1549 case cdk_error:
1550 return true;
1552 default:
1553 declarator = declarator->declarator;
1554 break;
1558 return !found;
1561 cp_parameter_declarator *no_parameters;
1563 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1564 DECLARATOR and DEFAULT_ARGUMENT. */
1566 cp_parameter_declarator *
1567 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1568 cp_declarator *declarator,
1569 tree default_argument)
1571 cp_parameter_declarator *parameter;
1573 parameter = ((cp_parameter_declarator *)
1574 alloc_declarator (sizeof (cp_parameter_declarator)));
1575 parameter->next = NULL;
1576 if (decl_specifiers)
1577 parameter->decl_specifiers = *decl_specifiers;
1578 else
1579 clear_decl_specs (&parameter->decl_specifiers);
1580 parameter->declarator = declarator;
1581 parameter->default_argument = default_argument;
1582 parameter->ellipsis_p = false;
1584 return parameter;
1587 /* Returns true iff DECLARATOR is a declaration for a function. */
1589 static bool
1590 function_declarator_p (const cp_declarator *declarator)
1592 while (declarator)
1594 if (declarator->kind == cdk_function
1595 && declarator->declarator->kind == cdk_id)
1596 return true;
1597 if (declarator->kind == cdk_id
1598 || declarator->kind == cdk_error)
1599 return false;
1600 declarator = declarator->declarator;
1602 return false;
1605 /* The parser. */
1607 /* Overview
1608 --------
1610 A cp_parser parses the token stream as specified by the C++
1611 grammar. Its job is purely parsing, not semantic analysis. For
1612 example, the parser breaks the token stream into declarators,
1613 expressions, statements, and other similar syntactic constructs.
1614 It does not check that the types of the expressions on either side
1615 of an assignment-statement are compatible, or that a function is
1616 not declared with a parameter of type `void'.
1618 The parser invokes routines elsewhere in the compiler to perform
1619 semantic analysis and to build up the abstract syntax tree for the
1620 code processed.
1622 The parser (and the template instantiation code, which is, in a
1623 way, a close relative of parsing) are the only parts of the
1624 compiler that should be calling push_scope and pop_scope, or
1625 related functions. The parser (and template instantiation code)
1626 keeps track of what scope is presently active; everything else
1627 should simply honor that. (The code that generates static
1628 initializers may also need to set the scope, in order to check
1629 access control correctly when emitting the initializers.)
1631 Methodology
1632 -----------
1634 The parser is of the standard recursive-descent variety. Upcoming
1635 tokens in the token stream are examined in order to determine which
1636 production to use when parsing a non-terminal. Some C++ constructs
1637 require arbitrary look ahead to disambiguate. For example, it is
1638 impossible, in the general case, to tell whether a statement is an
1639 expression or declaration without scanning the entire statement.
1640 Therefore, the parser is capable of "parsing tentatively." When the
1641 parser is not sure what construct comes next, it enters this mode.
1642 Then, while we attempt to parse the construct, the parser queues up
1643 error messages, rather than issuing them immediately, and saves the
1644 tokens it consumes. If the construct is parsed successfully, the
1645 parser "commits", i.e., it issues any queued error messages and
1646 the tokens that were being preserved are permanently discarded.
1647 If, however, the construct is not parsed successfully, the parser
1648 rolls back its state completely so that it can resume parsing using
1649 a different alternative.
1651 Future Improvements
1652 -------------------
1654 The performance of the parser could probably be improved substantially.
1655 We could often eliminate the need to parse tentatively by looking ahead
1656 a little bit. In some places, this approach might not entirely eliminate
1657 the need to parse tentatively, but it might still speed up the average
1658 case. */
1660 /* Flags that are passed to some parsing functions. These values can
1661 be bitwise-ored together. */
1663 enum
1665 /* No flags. */
1666 CP_PARSER_FLAGS_NONE = 0x0,
1667 /* The construct is optional. If it is not present, then no error
1668 should be issued. */
1669 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1670 /* When parsing a type-specifier, treat user-defined type-names
1671 as non-type identifiers. */
1672 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1673 /* When parsing a type-specifier, do not try to parse a class-specifier
1674 or enum-specifier. */
1675 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1676 /* When parsing a decl-specifier-seq, only allow type-specifier or
1677 constexpr. */
1678 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1681 /* This type is used for parameters and variables which hold
1682 combinations of the above flags. */
1683 typedef int cp_parser_flags;
1685 /* The different kinds of declarators we want to parse. */
1687 typedef enum cp_parser_declarator_kind
1689 /* We want an abstract declarator. */
1690 CP_PARSER_DECLARATOR_ABSTRACT,
1691 /* We want a named declarator. */
1692 CP_PARSER_DECLARATOR_NAMED,
1693 /* We don't mind, but the name must be an unqualified-id. */
1694 CP_PARSER_DECLARATOR_EITHER
1695 } cp_parser_declarator_kind;
1697 /* The precedence values used to parse binary expressions. The minimum value
1698 of PREC must be 1, because zero is reserved to quickly discriminate
1699 binary operators from other tokens. */
1701 enum cp_parser_prec
1703 PREC_NOT_OPERATOR,
1704 PREC_LOGICAL_OR_EXPRESSION,
1705 PREC_LOGICAL_AND_EXPRESSION,
1706 PREC_INCLUSIVE_OR_EXPRESSION,
1707 PREC_EXCLUSIVE_OR_EXPRESSION,
1708 PREC_AND_EXPRESSION,
1709 PREC_EQUALITY_EXPRESSION,
1710 PREC_RELATIONAL_EXPRESSION,
1711 PREC_SHIFT_EXPRESSION,
1712 PREC_ADDITIVE_EXPRESSION,
1713 PREC_MULTIPLICATIVE_EXPRESSION,
1714 PREC_PM_EXPRESSION,
1715 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1718 /* A mapping from a token type to a corresponding tree node type, with a
1719 precedence value. */
1721 typedef struct cp_parser_binary_operations_map_node
1723 /* The token type. */
1724 enum cpp_ttype token_type;
1725 /* The corresponding tree code. */
1726 enum tree_code tree_type;
1727 /* The precedence of this operator. */
1728 enum cp_parser_prec prec;
1729 } cp_parser_binary_operations_map_node;
1731 typedef struct cp_parser_expression_stack_entry
1733 /* Left hand side of the binary operation we are currently
1734 parsing. */
1735 tree lhs;
1736 /* Original tree code for left hand side, if it was a binary
1737 expression itself (used for -Wparentheses). */
1738 enum tree_code lhs_type;
1739 /* Tree code for the binary operation we are parsing. */
1740 enum tree_code tree_type;
1741 /* Precedence of the binary operation we are parsing. */
1742 enum cp_parser_prec prec;
1743 /* Location of the binary operation we are parsing. */
1744 location_t loc;
1745 } cp_parser_expression_stack_entry;
1747 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1748 entries because precedence levels on the stack are monotonically
1749 increasing. */
1750 typedef struct cp_parser_expression_stack_entry
1751 cp_parser_expression_stack[NUM_PREC_VALUES];
1753 /* Prototypes. */
1755 /* Constructors and destructors. */
1757 static cp_parser_context *cp_parser_context_new
1758 (cp_parser_context *);
1760 /* Class variables. */
1762 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1764 /* The operator-precedence table used by cp_parser_binary_expression.
1765 Transformed into an associative array (binops_by_token) by
1766 cp_parser_new. */
1768 static const cp_parser_binary_operations_map_node binops[] = {
1769 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1770 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1772 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1773 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1774 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1776 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1777 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1779 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1780 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1782 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1783 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1784 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1785 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1787 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1788 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1790 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1792 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1794 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1796 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1798 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1801 /* The same as binops, but initialized by cp_parser_new so that
1802 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1803 for speed. */
1804 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1806 /* Constructors and destructors. */
1808 /* Construct a new context. The context below this one on the stack
1809 is given by NEXT. */
1811 static cp_parser_context *
1812 cp_parser_context_new (cp_parser_context* next)
1814 cp_parser_context *context;
1816 /* Allocate the storage. */
1817 if (cp_parser_context_free_list != NULL)
1819 /* Pull the first entry from the free list. */
1820 context = cp_parser_context_free_list;
1821 cp_parser_context_free_list = context->next;
1822 memset (context, 0, sizeof (*context));
1824 else
1825 context = ggc_alloc_cleared_cp_parser_context ();
1827 /* No errors have occurred yet in this context. */
1828 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1829 /* If this is not the bottommost context, copy information that we
1830 need from the previous context. */
1831 if (next)
1833 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1834 expression, then we are parsing one in this context, too. */
1835 context->object_type = next->object_type;
1836 /* Thread the stack. */
1837 context->next = next;
1840 return context;
1843 /* Managing the unparsed function queues. */
1845 #define unparsed_funs_with_default_args \
1846 parser->unparsed_queues->last ().funs_with_default_args
1847 #define unparsed_funs_with_definitions \
1848 parser->unparsed_queues->last ().funs_with_definitions
1849 #define unparsed_nsdmis \
1850 parser->unparsed_queues->last ().nsdmis
1852 static void
1853 push_unparsed_function_queues (cp_parser *parser)
1855 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1856 vec_safe_push (parser->unparsed_queues, e);
1859 static void
1860 pop_unparsed_function_queues (cp_parser *parser)
1862 release_tree_vector (unparsed_funs_with_definitions);
1863 parser->unparsed_queues->pop ();
1866 /* Prototypes. */
1868 /* Constructors and destructors. */
1870 static cp_parser *cp_parser_new
1871 (void);
1873 /* Routines to parse various constructs.
1875 Those that return `tree' will return the error_mark_node (rather
1876 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1877 Sometimes, they will return an ordinary node if error-recovery was
1878 attempted, even though a parse error occurred. So, to check
1879 whether or not a parse error occurred, you should always use
1880 cp_parser_error_occurred. If the construct is optional (indicated
1881 either by an `_opt' in the name of the function that does the
1882 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1883 the construct is not present. */
1885 /* Lexical conventions [gram.lex] */
1887 static tree cp_parser_identifier
1888 (cp_parser *);
1889 static tree cp_parser_string_literal
1890 (cp_parser *, bool, bool);
1891 static tree cp_parser_userdef_char_literal
1892 (cp_parser *);
1893 static tree cp_parser_userdef_string_literal
1894 (cp_token *);
1895 static tree cp_parser_userdef_numeric_literal
1896 (cp_parser *);
1898 /* Basic concepts [gram.basic] */
1900 static bool cp_parser_translation_unit
1901 (cp_parser *);
1903 /* Expressions [gram.expr] */
1905 static tree cp_parser_primary_expression
1906 (cp_parser *, bool, bool, bool, cp_id_kind *);
1907 static tree cp_parser_id_expression
1908 (cp_parser *, bool, bool, bool *, bool, bool);
1909 static tree cp_parser_unqualified_id
1910 (cp_parser *, bool, bool, bool, bool);
1911 static tree cp_parser_nested_name_specifier_opt
1912 (cp_parser *, bool, bool, bool, bool);
1913 static tree cp_parser_nested_name_specifier
1914 (cp_parser *, bool, bool, bool, bool);
1915 static tree cp_parser_qualifying_entity
1916 (cp_parser *, bool, bool, bool, bool, bool);
1917 static tree cp_parser_postfix_expression
1918 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1919 static tree cp_parser_postfix_open_square_expression
1920 (cp_parser *, tree, bool, bool);
1921 static tree cp_parser_postfix_dot_deref_expression
1922 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1923 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1924 (cp_parser *, int, bool, bool, bool *);
1925 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1926 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1927 static void cp_parser_pseudo_destructor_name
1928 (cp_parser *, tree, tree *, tree *);
1929 static tree cp_parser_unary_expression
1930 (cp_parser *, bool, bool, cp_id_kind *);
1931 static enum tree_code cp_parser_unary_operator
1932 (cp_token *);
1933 static tree cp_parser_new_expression
1934 (cp_parser *);
1935 static vec<tree, va_gc> *cp_parser_new_placement
1936 (cp_parser *);
1937 static tree cp_parser_new_type_id
1938 (cp_parser *, tree *);
1939 static cp_declarator *cp_parser_new_declarator_opt
1940 (cp_parser *);
1941 static cp_declarator *cp_parser_direct_new_declarator
1942 (cp_parser *);
1943 static vec<tree, va_gc> *cp_parser_new_initializer
1944 (cp_parser *);
1945 static tree cp_parser_delete_expression
1946 (cp_parser *);
1947 static tree cp_parser_cast_expression
1948 (cp_parser *, bool, bool, bool, cp_id_kind *);
1949 static tree cp_parser_binary_expression
1950 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1951 static tree cp_parser_question_colon_clause
1952 (cp_parser *, tree);
1953 static tree cp_parser_assignment_expression
1954 (cp_parser *, bool, cp_id_kind *);
1955 static enum tree_code cp_parser_assignment_operator_opt
1956 (cp_parser *);
1957 static tree cp_parser_expression
1958 (cp_parser *, bool, cp_id_kind *);
1959 static tree cp_parser_expression
1960 (cp_parser *, bool, bool, cp_id_kind *);
1961 static tree cp_parser_constant_expression
1962 (cp_parser *, bool, bool *);
1963 static tree cp_parser_builtin_offsetof
1964 (cp_parser *);
1965 static tree cp_parser_lambda_expression
1966 (cp_parser *);
1967 static void cp_parser_lambda_introducer
1968 (cp_parser *, tree);
1969 static bool cp_parser_lambda_declarator_opt
1970 (cp_parser *, tree);
1971 static void cp_parser_lambda_body
1972 (cp_parser *, tree);
1974 /* Statements [gram.stmt.stmt] */
1976 static void cp_parser_statement
1977 (cp_parser *, tree, bool, bool *);
1978 static void cp_parser_label_for_labeled_statement
1979 (cp_parser *, tree);
1980 static tree cp_parser_expression_statement
1981 (cp_parser *, tree);
1982 static tree cp_parser_compound_statement
1983 (cp_parser *, tree, bool, bool);
1984 static void cp_parser_statement_seq_opt
1985 (cp_parser *, tree);
1986 static tree cp_parser_selection_statement
1987 (cp_parser *, bool *);
1988 static tree cp_parser_condition
1989 (cp_parser *);
1990 static tree cp_parser_iteration_statement
1991 (cp_parser *, bool);
1992 static bool cp_parser_for_init_statement
1993 (cp_parser *, tree *decl);
1994 static tree cp_parser_for
1995 (cp_parser *, bool);
1996 static tree cp_parser_c_for
1997 (cp_parser *, tree, tree, bool);
1998 static tree cp_parser_range_for
1999 (cp_parser *, tree, tree, tree, bool);
2000 static void do_range_for_auto_deduction
2001 (tree, tree);
2002 static tree cp_parser_perform_range_for_lookup
2003 (tree, tree *, tree *);
2004 static tree cp_parser_range_for_member_function
2005 (tree, tree);
2006 static tree cp_parser_jump_statement
2007 (cp_parser *);
2008 static void cp_parser_declaration_statement
2009 (cp_parser *);
2011 static tree cp_parser_implicitly_scoped_statement
2012 (cp_parser *, bool *);
2013 static void cp_parser_already_scoped_statement
2014 (cp_parser *);
2016 /* Declarations [gram.dcl.dcl] */
2018 static void cp_parser_declaration_seq_opt
2019 (cp_parser *);
2020 static void cp_parser_declaration
2021 (cp_parser *);
2022 static void cp_parser_block_declaration
2023 (cp_parser *, bool);
2024 static void cp_parser_simple_declaration
2025 (cp_parser *, bool, tree *);
2026 static void cp_parser_decl_specifier_seq
2027 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2028 static tree cp_parser_storage_class_specifier_opt
2029 (cp_parser *);
2030 static tree cp_parser_function_specifier_opt
2031 (cp_parser *, cp_decl_specifier_seq *);
2032 static tree cp_parser_type_specifier
2033 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2034 int *, bool *);
2035 static tree cp_parser_simple_type_specifier
2036 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2037 static tree cp_parser_type_name
2038 (cp_parser *);
2039 static tree cp_parser_nonclass_name
2040 (cp_parser* parser);
2041 static tree cp_parser_elaborated_type_specifier
2042 (cp_parser *, bool, bool);
2043 static tree cp_parser_enum_specifier
2044 (cp_parser *);
2045 static void cp_parser_enumerator_list
2046 (cp_parser *, tree);
2047 static void cp_parser_enumerator_definition
2048 (cp_parser *, tree);
2049 static tree cp_parser_namespace_name
2050 (cp_parser *);
2051 static void cp_parser_namespace_definition
2052 (cp_parser *);
2053 static void cp_parser_namespace_body
2054 (cp_parser *);
2055 static tree cp_parser_qualified_namespace_specifier
2056 (cp_parser *);
2057 static void cp_parser_namespace_alias_definition
2058 (cp_parser *);
2059 static bool cp_parser_using_declaration
2060 (cp_parser *, bool);
2061 static void cp_parser_using_directive
2062 (cp_parser *);
2063 static tree cp_parser_alias_declaration
2064 (cp_parser *);
2065 static void cp_parser_asm_definition
2066 (cp_parser *);
2067 static void cp_parser_linkage_specification
2068 (cp_parser *);
2069 static void cp_parser_static_assert
2070 (cp_parser *, bool);
2071 static tree cp_parser_decltype
2072 (cp_parser *);
2074 /* Declarators [gram.dcl.decl] */
2076 static tree cp_parser_init_declarator
2077 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2078 static cp_declarator *cp_parser_declarator
2079 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
2080 static cp_declarator *cp_parser_direct_declarator
2081 (cp_parser *, cp_parser_declarator_kind, int *, bool);
2082 static enum tree_code cp_parser_ptr_operator
2083 (cp_parser *, tree *, cp_cv_quals *, tree *);
2084 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2085 (cp_parser *);
2086 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2087 (cp_parser *);
2088 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2089 (cp_parser *);
2090 static tree cp_parser_late_return_type_opt
2091 (cp_parser *, cp_declarator *, cp_cv_quals);
2092 static tree cp_parser_declarator_id
2093 (cp_parser *, bool);
2094 static tree cp_parser_type_id
2095 (cp_parser *);
2096 static tree cp_parser_template_type_arg
2097 (cp_parser *);
2098 static tree cp_parser_trailing_type_id (cp_parser *);
2099 static tree cp_parser_type_id_1
2100 (cp_parser *, bool, bool);
2101 static void cp_parser_type_specifier_seq
2102 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2103 static tree cp_parser_parameter_declaration_clause
2104 (cp_parser *);
2105 static tree cp_parser_parameter_declaration_list
2106 (cp_parser *, bool *);
2107 static cp_parameter_declarator *cp_parser_parameter_declaration
2108 (cp_parser *, bool, bool *);
2109 static tree cp_parser_default_argument
2110 (cp_parser *, bool);
2111 static void cp_parser_function_body
2112 (cp_parser *, bool);
2113 static tree cp_parser_initializer
2114 (cp_parser *, bool *, bool *);
2115 static tree cp_parser_initializer_clause
2116 (cp_parser *, bool *);
2117 static tree cp_parser_braced_list
2118 (cp_parser*, bool*);
2119 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2120 (cp_parser *, bool *);
2122 static bool cp_parser_ctor_initializer_opt_and_function_body
2123 (cp_parser *, bool);
2125 static tree cp_parser_late_parsing_omp_declare_simd
2126 (cp_parser *, tree);
2128 static tree cp_parser_late_parsing_cilk_simd_fn_info
2129 (cp_parser *, tree);
2131 static tree synthesize_implicit_template_parm
2132 (cp_parser *);
2133 static tree finish_fully_implicit_template
2134 (cp_parser *, tree);
2136 /* Classes [gram.class] */
2138 static tree cp_parser_class_name
2139 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2140 static tree cp_parser_class_specifier
2141 (cp_parser *);
2142 static tree cp_parser_class_head
2143 (cp_parser *, bool *);
2144 static enum tag_types cp_parser_class_key
2145 (cp_parser *);
2146 static void cp_parser_member_specification_opt
2147 (cp_parser *);
2148 static void cp_parser_member_declaration
2149 (cp_parser *);
2150 static tree cp_parser_pure_specifier
2151 (cp_parser *);
2152 static tree cp_parser_constant_initializer
2153 (cp_parser *);
2155 /* Derived classes [gram.class.derived] */
2157 static tree cp_parser_base_clause
2158 (cp_parser *);
2159 static tree cp_parser_base_specifier
2160 (cp_parser *);
2162 /* Special member functions [gram.special] */
2164 static tree cp_parser_conversion_function_id
2165 (cp_parser *);
2166 static tree cp_parser_conversion_type_id
2167 (cp_parser *);
2168 static cp_declarator *cp_parser_conversion_declarator_opt
2169 (cp_parser *);
2170 static bool cp_parser_ctor_initializer_opt
2171 (cp_parser *);
2172 static void cp_parser_mem_initializer_list
2173 (cp_parser *);
2174 static tree cp_parser_mem_initializer
2175 (cp_parser *);
2176 static tree cp_parser_mem_initializer_id
2177 (cp_parser *);
2179 /* Overloading [gram.over] */
2181 static tree cp_parser_operator_function_id
2182 (cp_parser *);
2183 static tree cp_parser_operator
2184 (cp_parser *);
2186 /* Templates [gram.temp] */
2188 static void cp_parser_template_declaration
2189 (cp_parser *, bool);
2190 static tree cp_parser_template_parameter_list
2191 (cp_parser *);
2192 static tree cp_parser_template_parameter
2193 (cp_parser *, bool *, bool *);
2194 static tree cp_parser_type_parameter
2195 (cp_parser *, bool *);
2196 static tree cp_parser_template_id
2197 (cp_parser *, bool, bool, enum tag_types, bool);
2198 static tree cp_parser_template_name
2199 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2200 static tree cp_parser_template_argument_list
2201 (cp_parser *);
2202 static tree cp_parser_template_argument
2203 (cp_parser *);
2204 static void cp_parser_explicit_instantiation
2205 (cp_parser *);
2206 static void cp_parser_explicit_specialization
2207 (cp_parser *);
2209 /* Exception handling [gram.exception] */
2211 static tree cp_parser_try_block
2212 (cp_parser *);
2213 static bool cp_parser_function_try_block
2214 (cp_parser *);
2215 static void cp_parser_handler_seq
2216 (cp_parser *);
2217 static void cp_parser_handler
2218 (cp_parser *);
2219 static tree cp_parser_exception_declaration
2220 (cp_parser *);
2221 static tree cp_parser_throw_expression
2222 (cp_parser *);
2223 static tree cp_parser_exception_specification_opt
2224 (cp_parser *);
2225 static tree cp_parser_type_id_list
2226 (cp_parser *);
2228 /* GNU Extensions */
2230 static tree cp_parser_asm_specification_opt
2231 (cp_parser *);
2232 static tree cp_parser_asm_operand_list
2233 (cp_parser *);
2234 static tree cp_parser_asm_clobber_list
2235 (cp_parser *);
2236 static tree cp_parser_asm_label_list
2237 (cp_parser *);
2238 static bool cp_next_tokens_can_be_attribute_p
2239 (cp_parser *);
2240 static bool cp_next_tokens_can_be_gnu_attribute_p
2241 (cp_parser *);
2242 static bool cp_next_tokens_can_be_std_attribute_p
2243 (cp_parser *);
2244 static bool cp_nth_tokens_can_be_std_attribute_p
2245 (cp_parser *, size_t);
2246 static bool cp_nth_tokens_can_be_gnu_attribute_p
2247 (cp_parser *, size_t);
2248 static bool cp_nth_tokens_can_be_attribute_p
2249 (cp_parser *, size_t);
2250 static tree cp_parser_attributes_opt
2251 (cp_parser *);
2252 static tree cp_parser_gnu_attributes_opt
2253 (cp_parser *);
2254 static tree cp_parser_gnu_attribute_list
2255 (cp_parser *);
2256 static tree cp_parser_std_attribute
2257 (cp_parser *);
2258 static tree cp_parser_std_attribute_spec
2259 (cp_parser *);
2260 static tree cp_parser_std_attribute_spec_seq
2261 (cp_parser *);
2262 static bool cp_parser_extension_opt
2263 (cp_parser *, int *);
2264 static void cp_parser_label_declaration
2265 (cp_parser *);
2267 /* Transactional Memory Extensions */
2269 static tree cp_parser_transaction
2270 (cp_parser *, enum rid);
2271 static tree cp_parser_transaction_expression
2272 (cp_parser *, enum rid);
2273 static bool cp_parser_function_transaction
2274 (cp_parser *, enum rid);
2275 static tree cp_parser_transaction_cancel
2276 (cp_parser *);
2278 enum pragma_context {
2279 pragma_external,
2280 pragma_member,
2281 pragma_objc_icode,
2282 pragma_stmt,
2283 pragma_compound
2285 static bool cp_parser_pragma
2286 (cp_parser *, enum pragma_context);
2288 /* Objective-C++ Productions */
2290 static tree cp_parser_objc_message_receiver
2291 (cp_parser *);
2292 static tree cp_parser_objc_message_args
2293 (cp_parser *);
2294 static tree cp_parser_objc_message_expression
2295 (cp_parser *);
2296 static tree cp_parser_objc_encode_expression
2297 (cp_parser *);
2298 static tree cp_parser_objc_defs_expression
2299 (cp_parser *);
2300 static tree cp_parser_objc_protocol_expression
2301 (cp_parser *);
2302 static tree cp_parser_objc_selector_expression
2303 (cp_parser *);
2304 static tree cp_parser_objc_expression
2305 (cp_parser *);
2306 static bool cp_parser_objc_selector_p
2307 (enum cpp_ttype);
2308 static tree cp_parser_objc_selector
2309 (cp_parser *);
2310 static tree cp_parser_objc_protocol_refs_opt
2311 (cp_parser *);
2312 static void cp_parser_objc_declaration
2313 (cp_parser *, tree);
2314 static tree cp_parser_objc_statement
2315 (cp_parser *);
2316 static bool cp_parser_objc_valid_prefix_attributes
2317 (cp_parser *, tree *);
2318 static void cp_parser_objc_at_property_declaration
2319 (cp_parser *) ;
2320 static void cp_parser_objc_at_synthesize_declaration
2321 (cp_parser *) ;
2322 static void cp_parser_objc_at_dynamic_declaration
2323 (cp_parser *) ;
2324 static tree cp_parser_objc_struct_declaration
2325 (cp_parser *) ;
2327 /* Utility Routines */
2329 static tree cp_parser_lookup_name
2330 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2331 static tree cp_parser_lookup_name_simple
2332 (cp_parser *, tree, location_t);
2333 static tree cp_parser_maybe_treat_template_as_class
2334 (tree, bool);
2335 static bool cp_parser_check_declarator_template_parameters
2336 (cp_parser *, cp_declarator *, location_t);
2337 static bool cp_parser_check_template_parameters
2338 (cp_parser *, unsigned, location_t, cp_declarator *);
2339 static tree cp_parser_simple_cast_expression
2340 (cp_parser *);
2341 static tree cp_parser_global_scope_opt
2342 (cp_parser *, bool);
2343 static bool cp_parser_constructor_declarator_p
2344 (cp_parser *, bool);
2345 static tree cp_parser_function_definition_from_specifiers_and_declarator
2346 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2347 static tree cp_parser_function_definition_after_declarator
2348 (cp_parser *, bool);
2349 static void cp_parser_template_declaration_after_export
2350 (cp_parser *, bool);
2351 static void cp_parser_perform_template_parameter_access_checks
2352 (vec<deferred_access_check, va_gc> *);
2353 static tree cp_parser_single_declaration
2354 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2355 static tree cp_parser_functional_cast
2356 (cp_parser *, tree);
2357 static tree cp_parser_save_member_function_body
2358 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2359 static tree cp_parser_save_nsdmi
2360 (cp_parser *);
2361 static tree cp_parser_enclosed_template_argument_list
2362 (cp_parser *);
2363 static void cp_parser_save_default_args
2364 (cp_parser *, tree);
2365 static void cp_parser_late_parsing_for_member
2366 (cp_parser *, tree);
2367 static tree cp_parser_late_parse_one_default_arg
2368 (cp_parser *, tree, tree, tree);
2369 static void cp_parser_late_parsing_nsdmi
2370 (cp_parser *, tree);
2371 static void cp_parser_late_parsing_default_args
2372 (cp_parser *, tree);
2373 static tree cp_parser_sizeof_operand
2374 (cp_parser *, enum rid);
2375 static tree cp_parser_trait_expr
2376 (cp_parser *, enum rid);
2377 static bool cp_parser_declares_only_class_p
2378 (cp_parser *);
2379 static void cp_parser_set_storage_class
2380 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2381 static void cp_parser_set_decl_spec_type
2382 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2383 static void set_and_check_decl_spec_loc
2384 (cp_decl_specifier_seq *decl_specs,
2385 cp_decl_spec ds, cp_token *);
2386 static bool cp_parser_friend_p
2387 (const cp_decl_specifier_seq *);
2388 static void cp_parser_required_error
2389 (cp_parser *, required_token, bool);
2390 static cp_token *cp_parser_require
2391 (cp_parser *, enum cpp_ttype, required_token);
2392 static cp_token *cp_parser_require_keyword
2393 (cp_parser *, enum rid, required_token);
2394 static bool cp_parser_token_starts_function_definition_p
2395 (cp_token *);
2396 static bool cp_parser_next_token_starts_class_definition_p
2397 (cp_parser *);
2398 static bool cp_parser_next_token_ends_template_argument_p
2399 (cp_parser *);
2400 static bool cp_parser_nth_token_starts_template_argument_list_p
2401 (cp_parser *, size_t);
2402 static enum tag_types cp_parser_token_is_class_key
2403 (cp_token *);
2404 static void cp_parser_check_class_key
2405 (enum tag_types, tree type);
2406 static void cp_parser_check_access_in_redeclaration
2407 (tree type, location_t location);
2408 static bool cp_parser_optional_template_keyword
2409 (cp_parser *);
2410 static void cp_parser_pre_parsed_nested_name_specifier
2411 (cp_parser *);
2412 static bool cp_parser_cache_group
2413 (cp_parser *, enum cpp_ttype, unsigned);
2414 static tree cp_parser_cache_defarg
2415 (cp_parser *parser, bool nsdmi);
2416 static void cp_parser_parse_tentatively
2417 (cp_parser *);
2418 static void cp_parser_commit_to_tentative_parse
2419 (cp_parser *);
2420 static void cp_parser_commit_to_topmost_tentative_parse
2421 (cp_parser *);
2422 static void cp_parser_abort_tentative_parse
2423 (cp_parser *);
2424 static bool cp_parser_parse_definitely
2425 (cp_parser *);
2426 static inline bool cp_parser_parsing_tentatively
2427 (cp_parser *);
2428 static bool cp_parser_uncommitted_to_tentative_parse_p
2429 (cp_parser *);
2430 static void cp_parser_error
2431 (cp_parser *, const char *);
2432 static void cp_parser_name_lookup_error
2433 (cp_parser *, tree, tree, name_lookup_error, location_t);
2434 static bool cp_parser_simulate_error
2435 (cp_parser *);
2436 static bool cp_parser_check_type_definition
2437 (cp_parser *);
2438 static void cp_parser_check_for_definition_in_return_type
2439 (cp_declarator *, tree, location_t type_location);
2440 static void cp_parser_check_for_invalid_template_id
2441 (cp_parser *, tree, enum tag_types, location_t location);
2442 static bool cp_parser_non_integral_constant_expression
2443 (cp_parser *, non_integral_constant);
2444 static void cp_parser_diagnose_invalid_type_name
2445 (cp_parser *, tree, tree, location_t);
2446 static bool cp_parser_parse_and_diagnose_invalid_type_name
2447 (cp_parser *);
2448 static int cp_parser_skip_to_closing_parenthesis
2449 (cp_parser *, bool, bool, bool);
2450 static void cp_parser_skip_to_end_of_statement
2451 (cp_parser *);
2452 static void cp_parser_consume_semicolon_at_end_of_statement
2453 (cp_parser *);
2454 static void cp_parser_skip_to_end_of_block_or_statement
2455 (cp_parser *);
2456 static bool cp_parser_skip_to_closing_brace
2457 (cp_parser *);
2458 static void cp_parser_skip_to_end_of_template_parameter_list
2459 (cp_parser *);
2460 static void cp_parser_skip_to_pragma_eol
2461 (cp_parser*, cp_token *);
2462 static bool cp_parser_error_occurred
2463 (cp_parser *);
2464 static bool cp_parser_allow_gnu_extensions_p
2465 (cp_parser *);
2466 static bool cp_parser_is_pure_string_literal
2467 (cp_token *);
2468 static bool cp_parser_is_string_literal
2469 (cp_token *);
2470 static bool cp_parser_is_keyword
2471 (cp_token *, enum rid);
2472 static tree cp_parser_make_typename_type
2473 (cp_parser *, tree, tree, location_t location);
2474 static cp_declarator * cp_parser_make_indirect_declarator
2475 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2477 /* Returns nonzero if we are parsing tentatively. */
2479 static inline bool
2480 cp_parser_parsing_tentatively (cp_parser* parser)
2482 return parser->context->next != NULL;
2485 /* Returns nonzero if TOKEN is a string literal. */
2487 static bool
2488 cp_parser_is_pure_string_literal (cp_token* token)
2490 return (token->type == CPP_STRING ||
2491 token->type == CPP_STRING16 ||
2492 token->type == CPP_STRING32 ||
2493 token->type == CPP_WSTRING ||
2494 token->type == CPP_UTF8STRING);
2497 /* Returns nonzero if TOKEN is a string literal
2498 of a user-defined string literal. */
2500 static bool
2501 cp_parser_is_string_literal (cp_token* token)
2503 return (cp_parser_is_pure_string_literal (token) ||
2504 token->type == CPP_STRING_USERDEF ||
2505 token->type == CPP_STRING16_USERDEF ||
2506 token->type == CPP_STRING32_USERDEF ||
2507 token->type == CPP_WSTRING_USERDEF ||
2508 token->type == CPP_UTF8STRING_USERDEF);
2511 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2513 static bool
2514 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2516 return token->keyword == keyword;
2519 /* If not parsing tentatively, issue a diagnostic of the form
2520 FILE:LINE: MESSAGE before TOKEN
2521 where TOKEN is the next token in the input stream. MESSAGE
2522 (specified by the caller) is usually of the form "expected
2523 OTHER-TOKEN". */
2525 static void
2526 cp_parser_error (cp_parser* parser, const char* gmsgid)
2528 if (!cp_parser_simulate_error (parser))
2530 cp_token *token = cp_lexer_peek_token (parser->lexer);
2531 /* This diagnostic makes more sense if it is tagged to the line
2532 of the token we just peeked at. */
2533 cp_lexer_set_source_position_from_token (token);
2535 if (token->type == CPP_PRAGMA)
2537 error_at (token->location,
2538 "%<#pragma%> is not allowed here");
2539 cp_parser_skip_to_pragma_eol (parser, token);
2540 return;
2543 c_parse_error (gmsgid,
2544 /* Because c_parser_error does not understand
2545 CPP_KEYWORD, keywords are treated like
2546 identifiers. */
2547 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2548 token->u.value, token->flags);
2552 /* Issue an error about name-lookup failing. NAME is the
2553 IDENTIFIER_NODE DECL is the result of
2554 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2555 the thing that we hoped to find. */
2557 static void
2558 cp_parser_name_lookup_error (cp_parser* parser,
2559 tree name,
2560 tree decl,
2561 name_lookup_error desired,
2562 location_t location)
2564 /* If name lookup completely failed, tell the user that NAME was not
2565 declared. */
2566 if (decl == error_mark_node)
2568 if (parser->scope && parser->scope != global_namespace)
2569 error_at (location, "%<%E::%E%> has not been declared",
2570 parser->scope, name);
2571 else if (parser->scope == global_namespace)
2572 error_at (location, "%<::%E%> has not been declared", name);
2573 else if (parser->object_scope
2574 && !CLASS_TYPE_P (parser->object_scope))
2575 error_at (location, "request for member %qE in non-class type %qT",
2576 name, parser->object_scope);
2577 else if (parser->object_scope)
2578 error_at (location, "%<%T::%E%> has not been declared",
2579 parser->object_scope, name);
2580 else
2581 error_at (location, "%qE has not been declared", name);
2583 else if (parser->scope && parser->scope != global_namespace)
2585 switch (desired)
2587 case NLE_TYPE:
2588 error_at (location, "%<%E::%E%> is not a type",
2589 parser->scope, name);
2590 break;
2591 case NLE_CXX98:
2592 error_at (location, "%<%E::%E%> is not a class or namespace",
2593 parser->scope, name);
2594 break;
2595 case NLE_NOT_CXX98:
2596 error_at (location,
2597 "%<%E::%E%> is not a class, namespace, or enumeration",
2598 parser->scope, name);
2599 break;
2600 default:
2601 gcc_unreachable ();
2605 else if (parser->scope == global_namespace)
2607 switch (desired)
2609 case NLE_TYPE:
2610 error_at (location, "%<::%E%> is not a type", name);
2611 break;
2612 case NLE_CXX98:
2613 error_at (location, "%<::%E%> is not a class or namespace", name);
2614 break;
2615 case NLE_NOT_CXX98:
2616 error_at (location,
2617 "%<::%E%> is not a class, namespace, or enumeration",
2618 name);
2619 break;
2620 default:
2621 gcc_unreachable ();
2624 else
2626 switch (desired)
2628 case NLE_TYPE:
2629 error_at (location, "%qE is not a type", name);
2630 break;
2631 case NLE_CXX98:
2632 error_at (location, "%qE is not a class or namespace", name);
2633 break;
2634 case NLE_NOT_CXX98:
2635 error_at (location,
2636 "%qE is not a class, namespace, or enumeration", name);
2637 break;
2638 default:
2639 gcc_unreachable ();
2644 /* If we are parsing tentatively, remember that an error has occurred
2645 during this tentative parse. Returns true if the error was
2646 simulated; false if a message should be issued by the caller. */
2648 static bool
2649 cp_parser_simulate_error (cp_parser* parser)
2651 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2653 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2654 return true;
2656 return false;
2659 /* This function is called when a type is defined. If type
2660 definitions are forbidden at this point, an error message is
2661 issued. */
2663 static bool
2664 cp_parser_check_type_definition (cp_parser* parser)
2666 /* If types are forbidden here, issue a message. */
2667 if (parser->type_definition_forbidden_message)
2669 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2670 in the message need to be interpreted. */
2671 error (parser->type_definition_forbidden_message);
2672 return false;
2674 return true;
2677 /* This function is called when the DECLARATOR is processed. The TYPE
2678 was a type defined in the decl-specifiers. If it is invalid to
2679 define a type in the decl-specifiers for DECLARATOR, an error is
2680 issued. TYPE_LOCATION is the location of TYPE and is used
2681 for error reporting. */
2683 static void
2684 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2685 tree type, location_t type_location)
2687 /* [dcl.fct] forbids type definitions in return types.
2688 Unfortunately, it's not easy to know whether or not we are
2689 processing a return type until after the fact. */
2690 while (declarator
2691 && (declarator->kind == cdk_pointer
2692 || declarator->kind == cdk_reference
2693 || declarator->kind == cdk_ptrmem))
2694 declarator = declarator->declarator;
2695 if (declarator
2696 && declarator->kind == cdk_function)
2698 error_at (type_location,
2699 "new types may not be defined in a return type");
2700 inform (type_location,
2701 "(perhaps a semicolon is missing after the definition of %qT)",
2702 type);
2706 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2707 "<" in any valid C++ program. If the next token is indeed "<",
2708 issue a message warning the user about what appears to be an
2709 invalid attempt to form a template-id. LOCATION is the location
2710 of the type-specifier (TYPE) */
2712 static void
2713 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2714 tree type,
2715 enum tag_types tag_type,
2716 location_t location)
2718 cp_token_position start = 0;
2720 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2722 if (TYPE_P (type))
2723 error_at (location, "%qT is not a template", type);
2724 else if (identifier_p (type))
2726 if (tag_type != none_type)
2727 error_at (location, "%qE is not a class template", type);
2728 else
2729 error_at (location, "%qE is not a template", type);
2731 else
2732 error_at (location, "invalid template-id");
2733 /* Remember the location of the invalid "<". */
2734 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2735 start = cp_lexer_token_position (parser->lexer, true);
2736 /* Consume the "<". */
2737 cp_lexer_consume_token (parser->lexer);
2738 /* Parse the template arguments. */
2739 cp_parser_enclosed_template_argument_list (parser);
2740 /* Permanently remove the invalid template arguments so that
2741 this error message is not issued again. */
2742 if (start)
2743 cp_lexer_purge_tokens_after (parser->lexer, start);
2747 /* If parsing an integral constant-expression, issue an error message
2748 about the fact that THING appeared and return true. Otherwise,
2749 return false. In either case, set
2750 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2752 static bool
2753 cp_parser_non_integral_constant_expression (cp_parser *parser,
2754 non_integral_constant thing)
2756 parser->non_integral_constant_expression_p = true;
2757 if (parser->integral_constant_expression_p)
2759 if (!parser->allow_non_integral_constant_expression_p)
2761 const char *msg = NULL;
2762 switch (thing)
2764 case NIC_FLOAT:
2765 error ("floating-point literal "
2766 "cannot appear in a constant-expression");
2767 return true;
2768 case NIC_CAST:
2769 error ("a cast to a type other than an integral or "
2770 "enumeration type cannot appear in a "
2771 "constant-expression");
2772 return true;
2773 case NIC_TYPEID:
2774 error ("%<typeid%> operator "
2775 "cannot appear in a constant-expression");
2776 return true;
2777 case NIC_NCC:
2778 error ("non-constant compound literals "
2779 "cannot appear in a constant-expression");
2780 return true;
2781 case NIC_FUNC_CALL:
2782 error ("a function call "
2783 "cannot appear in a constant-expression");
2784 return true;
2785 case NIC_INC:
2786 error ("an increment "
2787 "cannot appear in a constant-expression");
2788 return true;
2789 case NIC_DEC:
2790 error ("an decrement "
2791 "cannot appear in a constant-expression");
2792 return true;
2793 case NIC_ARRAY_REF:
2794 error ("an array reference "
2795 "cannot appear in a constant-expression");
2796 return true;
2797 case NIC_ADDR_LABEL:
2798 error ("the address of a label "
2799 "cannot appear in a constant-expression");
2800 return true;
2801 case NIC_OVERLOADED:
2802 error ("calls to overloaded operators "
2803 "cannot appear in a constant-expression");
2804 return true;
2805 case NIC_ASSIGNMENT:
2806 error ("an assignment cannot appear in a constant-expression");
2807 return true;
2808 case NIC_COMMA:
2809 error ("a comma operator "
2810 "cannot appear in a constant-expression");
2811 return true;
2812 case NIC_CONSTRUCTOR:
2813 error ("a call to a constructor "
2814 "cannot appear in a constant-expression");
2815 return true;
2816 case NIC_TRANSACTION:
2817 error ("a transaction expression "
2818 "cannot appear in a constant-expression");
2819 return true;
2820 case NIC_THIS:
2821 msg = "this";
2822 break;
2823 case NIC_FUNC_NAME:
2824 msg = "__FUNCTION__";
2825 break;
2826 case NIC_PRETTY_FUNC:
2827 msg = "__PRETTY_FUNCTION__";
2828 break;
2829 case NIC_C99_FUNC:
2830 msg = "__func__";
2831 break;
2832 case NIC_VA_ARG:
2833 msg = "va_arg";
2834 break;
2835 case NIC_ARROW:
2836 msg = "->";
2837 break;
2838 case NIC_POINT:
2839 msg = ".";
2840 break;
2841 case NIC_STAR:
2842 msg = "*";
2843 break;
2844 case NIC_ADDR:
2845 msg = "&";
2846 break;
2847 case NIC_PREINCREMENT:
2848 msg = "++";
2849 break;
2850 case NIC_PREDECREMENT:
2851 msg = "--";
2852 break;
2853 case NIC_NEW:
2854 msg = "new";
2855 break;
2856 case NIC_DEL:
2857 msg = "delete";
2858 break;
2859 default:
2860 gcc_unreachable ();
2862 if (msg)
2863 error ("%qs cannot appear in a constant-expression", msg);
2864 return true;
2867 return false;
2870 /* Emit a diagnostic for an invalid type name. SCOPE is the
2871 qualifying scope (or NULL, if none) for ID. This function commits
2872 to the current active tentative parse, if any. (Otherwise, the
2873 problematic construct might be encountered again later, resulting
2874 in duplicate error messages.) LOCATION is the location of ID. */
2876 static void
2877 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2878 tree scope, tree id,
2879 location_t location)
2881 tree decl, old_scope;
2882 cp_parser_commit_to_tentative_parse (parser);
2883 /* Try to lookup the identifier. */
2884 old_scope = parser->scope;
2885 parser->scope = scope;
2886 decl = cp_parser_lookup_name_simple (parser, id, location);
2887 parser->scope = old_scope;
2888 /* If the lookup found a template-name, it means that the user forgot
2889 to specify an argument list. Emit a useful error message. */
2890 if (TREE_CODE (decl) == TEMPLATE_DECL)
2891 error_at (location,
2892 "invalid use of template-name %qE without an argument list",
2893 decl);
2894 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2895 error_at (location, "invalid use of destructor %qD as a type", id);
2896 else if (TREE_CODE (decl) == TYPE_DECL)
2897 /* Something like 'unsigned A a;' */
2898 error_at (location, "invalid combination of multiple type-specifiers");
2899 else if (!parser->scope)
2901 /* Issue an error message. */
2902 error_at (location, "%qE does not name a type", id);
2903 /* If we're in a template class, it's possible that the user was
2904 referring to a type from a base class. For example:
2906 template <typename T> struct A { typedef T X; };
2907 template <typename T> struct B : public A<T> { X x; };
2909 The user should have said "typename A<T>::X". */
2910 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2911 inform (location, "C++11 %<constexpr%> only available with "
2912 "-std=c++11 or -std=gnu++11");
2913 else if (processing_template_decl && current_class_type
2914 && TYPE_BINFO (current_class_type))
2916 tree b;
2918 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2920 b = TREE_CHAIN (b))
2922 tree base_type = BINFO_TYPE (b);
2923 if (CLASS_TYPE_P (base_type)
2924 && dependent_type_p (base_type))
2926 tree field;
2927 /* Go from a particular instantiation of the
2928 template (which will have an empty TYPE_FIELDs),
2929 to the main version. */
2930 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2931 for (field = TYPE_FIELDS (base_type);
2932 field;
2933 field = DECL_CHAIN (field))
2934 if (TREE_CODE (field) == TYPE_DECL
2935 && DECL_NAME (field) == id)
2937 inform (location,
2938 "(perhaps %<typename %T::%E%> was intended)",
2939 BINFO_TYPE (b), id);
2940 break;
2942 if (field)
2943 break;
2948 /* Here we diagnose qualified-ids where the scope is actually correct,
2949 but the identifier does not resolve to a valid type name. */
2950 else if (parser->scope != error_mark_node)
2952 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2954 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2955 error_at (location_of (id),
2956 "%qE in namespace %qE does not name a template type",
2957 id, parser->scope);
2958 else
2959 error_at (location_of (id),
2960 "%qE in namespace %qE does not name a type",
2961 id, parser->scope);
2963 else if (CLASS_TYPE_P (parser->scope)
2964 && constructor_name_p (id, parser->scope))
2966 /* A<T>::A<T>() */
2967 error_at (location, "%<%T::%E%> names the constructor, not"
2968 " the type", parser->scope, id);
2969 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2970 error_at (location, "and %qT has no template constructors",
2971 parser->scope);
2973 else if (TYPE_P (parser->scope)
2974 && dependent_scope_p (parser->scope))
2975 error_at (location, "need %<typename%> before %<%T::%E%> because "
2976 "%qT is a dependent scope",
2977 parser->scope, id, parser->scope);
2978 else if (TYPE_P (parser->scope))
2980 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2981 error_at (location_of (id),
2982 "%qE in %q#T does not name a template type",
2983 id, parser->scope);
2984 else
2985 error_at (location_of (id),
2986 "%qE in %q#T does not name a type",
2987 id, parser->scope);
2989 else
2990 gcc_unreachable ();
2994 /* Check for a common situation where a type-name should be present,
2995 but is not, and issue a sensible error message. Returns true if an
2996 invalid type-name was detected.
2998 The situation handled by this function are variable declarations of the
2999 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3000 Usually, `ID' should name a type, but if we got here it means that it
3001 does not. We try to emit the best possible error message depending on
3002 how exactly the id-expression looks like. */
3004 static bool
3005 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3007 tree id;
3008 cp_token *token = cp_lexer_peek_token (parser->lexer);
3010 /* Avoid duplicate error about ambiguous lookup. */
3011 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3013 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3014 if (next->type == CPP_NAME && next->error_reported)
3015 goto out;
3018 cp_parser_parse_tentatively (parser);
3019 id = cp_parser_id_expression (parser,
3020 /*template_keyword_p=*/false,
3021 /*check_dependency_p=*/true,
3022 /*template_p=*/NULL,
3023 /*declarator_p=*/true,
3024 /*optional_p=*/false);
3025 /* If the next token is a (, this is a function with no explicit return
3026 type, i.e. constructor, destructor or conversion op. */
3027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3028 || TREE_CODE (id) == TYPE_DECL)
3030 cp_parser_abort_tentative_parse (parser);
3031 return false;
3033 if (!cp_parser_parse_definitely (parser))
3034 return false;
3036 /* Emit a diagnostic for the invalid type. */
3037 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
3038 id, token->location);
3039 out:
3040 /* If we aren't in the middle of a declarator (i.e. in a
3041 parameter-declaration-clause), skip to the end of the declaration;
3042 there's no point in trying to process it. */
3043 if (!parser->in_declarator_p)
3044 cp_parser_skip_to_end_of_block_or_statement (parser);
3045 return true;
3048 /* Consume tokens up to, and including, the next non-nested closing `)'.
3049 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3050 are doing error recovery. Returns -1 if OR_COMMA is true and we
3051 found an unnested comma. */
3053 static int
3054 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3055 bool recovering,
3056 bool or_comma,
3057 bool consume_paren)
3059 unsigned paren_depth = 0;
3060 unsigned brace_depth = 0;
3061 unsigned square_depth = 0;
3063 if (recovering && !or_comma
3064 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3065 return 0;
3067 while (true)
3069 cp_token * token = cp_lexer_peek_token (parser->lexer);
3071 switch (token->type)
3073 case CPP_EOF:
3074 case CPP_PRAGMA_EOL:
3075 /* If we've run out of tokens, then there is no closing `)'. */
3076 return 0;
3078 /* This is good for lambda expression capture-lists. */
3079 case CPP_OPEN_SQUARE:
3080 ++square_depth;
3081 break;
3082 case CPP_CLOSE_SQUARE:
3083 if (!square_depth--)
3084 return 0;
3085 break;
3087 case CPP_SEMICOLON:
3088 /* This matches the processing in skip_to_end_of_statement. */
3089 if (!brace_depth)
3090 return 0;
3091 break;
3093 case CPP_OPEN_BRACE:
3094 ++brace_depth;
3095 break;
3096 case CPP_CLOSE_BRACE:
3097 if (!brace_depth--)
3098 return 0;
3099 break;
3101 case CPP_COMMA:
3102 if (recovering && or_comma && !brace_depth && !paren_depth
3103 && !square_depth)
3104 return -1;
3105 break;
3107 case CPP_OPEN_PAREN:
3108 if (!brace_depth)
3109 ++paren_depth;
3110 break;
3112 case CPP_CLOSE_PAREN:
3113 if (!brace_depth && !paren_depth--)
3115 if (consume_paren)
3116 cp_lexer_consume_token (parser->lexer);
3117 return 1;
3119 break;
3121 default:
3122 break;
3125 /* Consume the token. */
3126 cp_lexer_consume_token (parser->lexer);
3130 /* Consume tokens until we reach the end of the current statement.
3131 Normally, that will be just before consuming a `;'. However, if a
3132 non-nested `}' comes first, then we stop before consuming that. */
3134 static void
3135 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3137 unsigned nesting_depth = 0;
3139 /* Unwind generic function template scope if necessary. */
3140 if (parser->fully_implicit_function_template_p)
3141 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3143 while (true)
3145 cp_token *token = cp_lexer_peek_token (parser->lexer);
3147 switch (token->type)
3149 case CPP_EOF:
3150 case CPP_PRAGMA_EOL:
3151 /* If we've run out of tokens, stop. */
3152 return;
3154 case CPP_SEMICOLON:
3155 /* If the next token is a `;', we have reached the end of the
3156 statement. */
3157 if (!nesting_depth)
3158 return;
3159 break;
3161 case CPP_CLOSE_BRACE:
3162 /* If this is a non-nested '}', stop before consuming it.
3163 That way, when confronted with something like:
3165 { 3 + }
3167 we stop before consuming the closing '}', even though we
3168 have not yet reached a `;'. */
3169 if (nesting_depth == 0)
3170 return;
3172 /* If it is the closing '}' for a block that we have
3173 scanned, stop -- but only after consuming the token.
3174 That way given:
3176 void f g () { ... }
3177 typedef int I;
3179 we will stop after the body of the erroneously declared
3180 function, but before consuming the following `typedef'
3181 declaration. */
3182 if (--nesting_depth == 0)
3184 cp_lexer_consume_token (parser->lexer);
3185 return;
3188 case CPP_OPEN_BRACE:
3189 ++nesting_depth;
3190 break;
3192 default:
3193 break;
3196 /* Consume the token. */
3197 cp_lexer_consume_token (parser->lexer);
3201 /* This function is called at the end of a statement or declaration.
3202 If the next token is a semicolon, it is consumed; otherwise, error
3203 recovery is attempted. */
3205 static void
3206 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3208 /* Look for the trailing `;'. */
3209 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3211 /* If there is additional (erroneous) input, skip to the end of
3212 the statement. */
3213 cp_parser_skip_to_end_of_statement (parser);
3214 /* If the next token is now a `;', consume it. */
3215 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3216 cp_lexer_consume_token (parser->lexer);
3220 /* Skip tokens until we have consumed an entire block, or until we
3221 have consumed a non-nested `;'. */
3223 static void
3224 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3226 int nesting_depth = 0;
3228 /* Unwind generic function template scope if necessary. */
3229 if (parser->fully_implicit_function_template_p)
3230 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3232 while (nesting_depth >= 0)
3234 cp_token *token = cp_lexer_peek_token (parser->lexer);
3236 switch (token->type)
3238 case CPP_EOF:
3239 case CPP_PRAGMA_EOL:
3240 /* If we've run out of tokens, stop. */
3241 return;
3243 case CPP_SEMICOLON:
3244 /* Stop if this is an unnested ';'. */
3245 if (!nesting_depth)
3246 nesting_depth = -1;
3247 break;
3249 case CPP_CLOSE_BRACE:
3250 /* Stop if this is an unnested '}', or closes the outermost
3251 nesting level. */
3252 nesting_depth--;
3253 if (nesting_depth < 0)
3254 return;
3255 if (!nesting_depth)
3256 nesting_depth = -1;
3257 break;
3259 case CPP_OPEN_BRACE:
3260 /* Nest. */
3261 nesting_depth++;
3262 break;
3264 default:
3265 break;
3268 /* Consume the token. */
3269 cp_lexer_consume_token (parser->lexer);
3273 /* Skip tokens until a non-nested closing curly brace is the next
3274 token, or there are no more tokens. Return true in the first case,
3275 false otherwise. */
3277 static bool
3278 cp_parser_skip_to_closing_brace (cp_parser *parser)
3280 unsigned nesting_depth = 0;
3282 while (true)
3284 cp_token *token = cp_lexer_peek_token (parser->lexer);
3286 switch (token->type)
3288 case CPP_EOF:
3289 case CPP_PRAGMA_EOL:
3290 /* If we've run out of tokens, stop. */
3291 return false;
3293 case CPP_CLOSE_BRACE:
3294 /* If the next token is a non-nested `}', then we have reached
3295 the end of the current block. */
3296 if (nesting_depth-- == 0)
3297 return true;
3298 break;
3300 case CPP_OPEN_BRACE:
3301 /* If it the next token is a `{', then we are entering a new
3302 block. Consume the entire block. */
3303 ++nesting_depth;
3304 break;
3306 default:
3307 break;
3310 /* Consume the token. */
3311 cp_lexer_consume_token (parser->lexer);
3315 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3316 parameter is the PRAGMA token, allowing us to purge the entire pragma
3317 sequence. */
3319 static void
3320 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3322 cp_token *token;
3324 parser->lexer->in_pragma = false;
3327 token = cp_lexer_consume_token (parser->lexer);
3328 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3330 /* Ensure that the pragma is not parsed again. */
3331 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3334 /* Require pragma end of line, resyncing with it as necessary. The
3335 arguments are as for cp_parser_skip_to_pragma_eol. */
3337 static void
3338 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3340 parser->lexer->in_pragma = false;
3341 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3342 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3345 /* This is a simple wrapper around make_typename_type. When the id is
3346 an unresolved identifier node, we can provide a superior diagnostic
3347 using cp_parser_diagnose_invalid_type_name. */
3349 static tree
3350 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3351 tree id, location_t id_location)
3353 tree result;
3354 if (identifier_p (id))
3356 result = make_typename_type (scope, id, typename_type,
3357 /*complain=*/tf_none);
3358 if (result == error_mark_node)
3359 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3360 return result;
3362 return make_typename_type (scope, id, typename_type, tf_error);
3365 /* This is a wrapper around the
3366 make_{pointer,ptrmem,reference}_declarator functions that decides
3367 which one to call based on the CODE and CLASS_TYPE arguments. The
3368 CODE argument should be one of the values returned by
3369 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3370 appertain to the pointer or reference. */
3372 static cp_declarator *
3373 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3374 cp_cv_quals cv_qualifiers,
3375 cp_declarator *target,
3376 tree attributes)
3378 if (code == ERROR_MARK)
3379 return cp_error_declarator;
3381 if (code == INDIRECT_REF)
3382 if (class_type == NULL_TREE)
3383 return make_pointer_declarator (cv_qualifiers, target, attributes);
3384 else
3385 return make_ptrmem_declarator (cv_qualifiers, class_type,
3386 target, attributes);
3387 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3388 return make_reference_declarator (cv_qualifiers, target,
3389 false, attributes);
3390 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3391 return make_reference_declarator (cv_qualifiers, target,
3392 true, attributes);
3393 gcc_unreachable ();
3396 /* Create a new C++ parser. */
3398 static cp_parser *
3399 cp_parser_new (void)
3401 cp_parser *parser;
3402 cp_lexer *lexer;
3403 unsigned i;
3405 /* cp_lexer_new_main is called before doing GC allocation because
3406 cp_lexer_new_main might load a PCH file. */
3407 lexer = cp_lexer_new_main ();
3409 /* Initialize the binops_by_token so that we can get the tree
3410 directly from the token. */
3411 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3412 binops_by_token[binops[i].token_type] = binops[i];
3414 parser = ggc_alloc_cleared_cp_parser ();
3415 parser->lexer = lexer;
3416 parser->context = cp_parser_context_new (NULL);
3418 /* For now, we always accept GNU extensions. */
3419 parser->allow_gnu_extensions_p = 1;
3421 /* The `>' token is a greater-than operator, not the end of a
3422 template-id. */
3423 parser->greater_than_is_operator_p = true;
3425 parser->default_arg_ok_p = true;
3427 /* We are not parsing a constant-expression. */
3428 parser->integral_constant_expression_p = false;
3429 parser->allow_non_integral_constant_expression_p = false;
3430 parser->non_integral_constant_expression_p = false;
3432 /* Local variable names are not forbidden. */
3433 parser->local_variables_forbidden_p = false;
3435 /* We are not processing an `extern "C"' declaration. */
3436 parser->in_unbraced_linkage_specification_p = false;
3438 /* We are not processing a declarator. */
3439 parser->in_declarator_p = false;
3441 /* We are not processing a template-argument-list. */
3442 parser->in_template_argument_list_p = false;
3444 /* We are not in an iteration statement. */
3445 parser->in_statement = 0;
3447 /* We are not in a switch statement. */
3448 parser->in_switch_statement_p = false;
3450 /* We are not parsing a type-id inside an expression. */
3451 parser->in_type_id_in_expr_p = false;
3453 /* Declarations aren't implicitly extern "C". */
3454 parser->implicit_extern_c = false;
3456 /* String literals should be translated to the execution character set. */
3457 parser->translate_strings_p = true;
3459 /* We are not parsing a function body. */
3460 parser->in_function_body = false;
3462 /* We can correct until told otherwise. */
3463 parser->colon_corrects_to_scope_p = true;
3465 /* The unparsed function queue is empty. */
3466 push_unparsed_function_queues (parser);
3468 /* There are no classes being defined. */
3469 parser->num_classes_being_defined = 0;
3471 /* No template parameters apply. */
3472 parser->num_template_parameter_lists = 0;
3474 /* Not declaring an implicit function template. */
3475 parser->auto_is_implicit_function_template_parm_p = false;
3476 parser->fully_implicit_function_template_p = false;
3477 parser->implicit_template_parms = 0;
3478 parser->implicit_template_scope = 0;
3480 return parser;
3483 /* Create a cp_lexer structure which will emit the tokens in CACHE
3484 and push it onto the parser's lexer stack. This is used for delayed
3485 parsing of in-class method bodies and default arguments, and should
3486 not be confused with tentative parsing. */
3487 static void
3488 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3490 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3491 lexer->next = parser->lexer;
3492 parser->lexer = lexer;
3494 /* Move the current source position to that of the first token in the
3495 new lexer. */
3496 cp_lexer_set_source_position_from_token (lexer->next_token);
3499 /* Pop the top lexer off the parser stack. This is never used for the
3500 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3501 static void
3502 cp_parser_pop_lexer (cp_parser *parser)
3504 cp_lexer *lexer = parser->lexer;
3505 parser->lexer = lexer->next;
3506 cp_lexer_destroy (lexer);
3508 /* Put the current source position back where it was before this
3509 lexer was pushed. */
3510 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3513 /* Lexical conventions [gram.lex] */
3515 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3516 identifier. */
3518 static tree
3519 cp_parser_identifier (cp_parser* parser)
3521 cp_token *token;
3523 /* Look for the identifier. */
3524 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3525 /* Return the value. */
3526 return token ? token->u.value : error_mark_node;
3529 /* Parse a sequence of adjacent string constants. Returns a
3530 TREE_STRING representing the combined, nul-terminated string
3531 constant. If TRANSLATE is true, translate the string to the
3532 execution character set. If WIDE_OK is true, a wide string is
3533 invalid here.
3535 C++98 [lex.string] says that if a narrow string literal token is
3536 adjacent to a wide string literal token, the behavior is undefined.
3537 However, C99 6.4.5p4 says that this results in a wide string literal.
3538 We follow C99 here, for consistency with the C front end.
3540 This code is largely lifted from lex_string() in c-lex.c.
3542 FUTURE: ObjC++ will need to handle @-strings here. */
3543 static tree
3544 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3546 tree value;
3547 size_t count;
3548 struct obstack str_ob;
3549 cpp_string str, istr, *strs;
3550 cp_token *tok;
3551 enum cpp_ttype type, curr_type;
3552 int have_suffix_p = 0;
3553 tree string_tree;
3554 tree suffix_id = NULL_TREE;
3555 bool curr_tok_is_userdef_p = false;
3557 tok = cp_lexer_peek_token (parser->lexer);
3558 if (!cp_parser_is_string_literal (tok))
3560 cp_parser_error (parser, "expected string-literal");
3561 return error_mark_node;
3564 if (cpp_userdef_string_p (tok->type))
3566 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3567 curr_type = cpp_userdef_string_remove_type (tok->type);
3568 curr_tok_is_userdef_p = true;
3570 else
3572 string_tree = tok->u.value;
3573 curr_type = tok->type;
3575 type = curr_type;
3577 /* Try to avoid the overhead of creating and destroying an obstack
3578 for the common case of just one string. */
3579 if (!cp_parser_is_string_literal
3580 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3582 cp_lexer_consume_token (parser->lexer);
3584 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3585 str.len = TREE_STRING_LENGTH (string_tree);
3586 count = 1;
3588 if (curr_tok_is_userdef_p)
3590 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3591 have_suffix_p = 1;
3592 curr_type = cpp_userdef_string_remove_type (tok->type);
3594 else
3595 curr_type = tok->type;
3597 strs = &str;
3599 else
3601 gcc_obstack_init (&str_ob);
3602 count = 0;
3606 cp_lexer_consume_token (parser->lexer);
3607 count++;
3608 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3609 str.len = TREE_STRING_LENGTH (string_tree);
3611 if (curr_tok_is_userdef_p)
3613 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3614 if (have_suffix_p == 0)
3616 suffix_id = curr_suffix_id;
3617 have_suffix_p = 1;
3619 else if (have_suffix_p == 1
3620 && curr_suffix_id != suffix_id)
3622 error ("inconsistent user-defined literal suffixes"
3623 " %qD and %qD in string literal",
3624 suffix_id, curr_suffix_id);
3625 have_suffix_p = -1;
3627 curr_type = cpp_userdef_string_remove_type (tok->type);
3629 else
3630 curr_type = tok->type;
3632 if (type != curr_type)
3634 if (type == CPP_STRING)
3635 type = curr_type;
3636 else if (curr_type != CPP_STRING)
3637 error_at (tok->location,
3638 "unsupported non-standard concatenation "
3639 "of string literals");
3642 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3644 tok = cp_lexer_peek_token (parser->lexer);
3645 if (cpp_userdef_string_p (tok->type))
3647 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3648 curr_type = cpp_userdef_string_remove_type (tok->type);
3649 curr_tok_is_userdef_p = true;
3651 else
3653 string_tree = tok->u.value;
3654 curr_type = tok->type;
3655 curr_tok_is_userdef_p = false;
3658 while (cp_parser_is_string_literal (tok));
3660 strs = (cpp_string *) obstack_finish (&str_ob);
3663 if (type != CPP_STRING && !wide_ok)
3665 cp_parser_error (parser, "a wide string is invalid in this context");
3666 type = CPP_STRING;
3669 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3670 (parse_in, strs, count, &istr, type))
3672 value = build_string (istr.len, (const char *)istr.text);
3673 free (CONST_CAST (unsigned char *, istr.text));
3675 switch (type)
3677 default:
3678 case CPP_STRING:
3679 case CPP_UTF8STRING:
3680 TREE_TYPE (value) = char_array_type_node;
3681 break;
3682 case CPP_STRING16:
3683 TREE_TYPE (value) = char16_array_type_node;
3684 break;
3685 case CPP_STRING32:
3686 TREE_TYPE (value) = char32_array_type_node;
3687 break;
3688 case CPP_WSTRING:
3689 TREE_TYPE (value) = wchar_array_type_node;
3690 break;
3693 value = fix_string_type (value);
3695 if (have_suffix_p)
3697 tree literal = build_userdef_literal (suffix_id, value,
3698 OT_NONE, NULL_TREE);
3699 tok->u.value = literal;
3700 return cp_parser_userdef_string_literal (tok);
3703 else
3704 /* cpp_interpret_string has issued an error. */
3705 value = error_mark_node;
3707 if (count > 1)
3708 obstack_free (&str_ob, 0);
3710 return value;
3713 /* Look up a literal operator with the name and the exact arguments. */
3715 static tree
3716 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3718 tree decl, fns;
3719 decl = lookup_name (name);
3720 if (!decl || !is_overloaded_fn (decl))
3721 return error_mark_node;
3723 for (fns = decl; fns; fns = OVL_NEXT (fns))
3725 unsigned int ix;
3726 bool found = true;
3727 tree fn = OVL_CURRENT (fns);
3728 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3729 if (parmtypes != NULL_TREE)
3731 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3732 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3734 tree tparm = TREE_VALUE (parmtypes);
3735 tree targ = TREE_TYPE ((*args)[ix]);
3736 bool ptr = TYPE_PTR_P (tparm);
3737 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3738 if ((ptr || arr || !same_type_p (tparm, targ))
3739 && (!ptr || !arr
3740 || !same_type_p (TREE_TYPE (tparm),
3741 TREE_TYPE (targ))))
3742 found = false;
3744 if (found
3745 && ix == vec_safe_length (args)
3746 /* May be this should be sufficient_parms_p instead,
3747 depending on how exactly should user-defined literals
3748 work in presence of default arguments on the literal
3749 operator parameters. */
3750 && parmtypes == void_list_node)
3751 return fn;
3755 return error_mark_node;
3758 /* Parse a user-defined char constant. Returns a call to a user-defined
3759 literal operator taking the character as an argument. */
3761 static tree
3762 cp_parser_userdef_char_literal (cp_parser *parser)
3764 cp_token *token = cp_lexer_consume_token (parser->lexer);
3765 tree literal = token->u.value;
3766 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3767 tree value = USERDEF_LITERAL_VALUE (literal);
3768 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3769 tree decl, result;
3771 /* Build up a call to the user-defined operator */
3772 /* Lookup the name we got back from the id-expression. */
3773 vec<tree, va_gc> *args = make_tree_vector ();
3774 vec_safe_push (args, value);
3775 decl = lookup_literal_operator (name, args);
3776 if (!decl || decl == error_mark_node)
3778 error ("unable to find character literal operator %qD with %qT argument",
3779 name, TREE_TYPE (value));
3780 release_tree_vector (args);
3781 return error_mark_node;
3783 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3784 release_tree_vector (args);
3785 if (result != error_mark_node)
3786 return result;
3788 error ("unable to find character literal operator %qD with %qT argument",
3789 name, TREE_TYPE (value));
3790 return error_mark_node;
3793 /* A subroutine of cp_parser_userdef_numeric_literal to
3794 create a char... template parameter pack from a string node. */
3796 static tree
3797 make_char_string_pack (tree value)
3799 tree charvec;
3800 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3801 const char *str = TREE_STRING_POINTER (value);
3802 int i, len = TREE_STRING_LENGTH (value) - 1;
3803 tree argvec = make_tree_vec (1);
3805 /* Fill in CHARVEC with all of the parameters. */
3806 charvec = make_tree_vec (len);
3807 for (i = 0; i < len; ++i)
3808 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3810 /* Build the argument packs. */
3811 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3812 TREE_TYPE (argpack) = char_type_node;
3814 TREE_VEC_ELT (argvec, 0) = argpack;
3816 return argvec;
3819 /* A subroutine of cp_parser_userdef_numeric_literal to
3820 create a char... template parameter pack from a string node. */
3822 static tree
3823 make_string_pack (tree value)
3825 tree charvec;
3826 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3827 const unsigned char *str
3828 = (const unsigned char *) TREE_STRING_POINTER (value);
3829 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3830 int len = TREE_STRING_LENGTH (value) / sz - 1;
3831 tree argvec = make_tree_vec (2);
3833 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3834 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3836 /* First template parm is character type. */
3837 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3839 /* Fill in CHARVEC with all of the parameters. */
3840 charvec = make_tree_vec (len);
3841 for (int i = 0; i < len; ++i)
3842 TREE_VEC_ELT (charvec, i)
3843 = double_int_to_tree (str_char_type_node,
3844 double_int::from_buffer (str + i * sz, sz));
3846 /* Build the argument packs. */
3847 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3848 TREE_TYPE (argpack) = str_char_type_node;
3850 TREE_VEC_ELT (argvec, 1) = argpack;
3852 return argvec;
3855 /* Parse a user-defined numeric constant. returns a call to a user-defined
3856 literal operator. */
3858 static tree
3859 cp_parser_userdef_numeric_literal (cp_parser *parser)
3861 cp_token *token = cp_lexer_consume_token (parser->lexer);
3862 tree literal = token->u.value;
3863 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3864 tree value = USERDEF_LITERAL_VALUE (literal);
3865 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3866 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3867 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3868 tree decl, result;
3869 vec<tree, va_gc> *args;
3871 /* Look for a literal operator taking the exact type of numeric argument
3872 as the literal value. */
3873 args = make_tree_vector ();
3874 vec_safe_push (args, value);
3875 decl = lookup_literal_operator (name, args);
3876 if (decl && decl != error_mark_node)
3878 result = finish_call_expr (decl, &args, false, true, tf_none);
3879 if (result != error_mark_node)
3881 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3882 warning_at (token->location, OPT_Woverflow,
3883 "integer literal exceeds range of %qT type",
3884 long_long_unsigned_type_node);
3885 else
3887 if (overflow > 0)
3888 warning_at (token->location, OPT_Woverflow,
3889 "floating literal exceeds range of %qT type",
3890 long_double_type_node);
3891 else if (overflow < 0)
3892 warning_at (token->location, OPT_Woverflow,
3893 "floating literal truncated to zero");
3895 release_tree_vector (args);
3896 return result;
3899 release_tree_vector (args);
3901 /* If the numeric argument didn't work, look for a raw literal
3902 operator taking a const char* argument consisting of the number
3903 in string format. */
3904 args = make_tree_vector ();
3905 vec_safe_push (args, num_string);
3906 decl = lookup_literal_operator (name, args);
3907 if (decl && decl != error_mark_node)
3909 result = finish_call_expr (decl, &args, false, true, tf_none);
3910 if (result != error_mark_node)
3912 release_tree_vector (args);
3913 return result;
3916 release_tree_vector (args);
3918 /* If the raw literal didn't work, look for a non-type template
3919 function with parameter pack char.... Call the function with
3920 template parameter characters representing the number. */
3921 args = make_tree_vector ();
3922 decl = lookup_literal_operator (name, args);
3923 if (decl && decl != error_mark_node)
3925 tree tmpl_args = make_char_string_pack (num_string);
3926 decl = lookup_template_function (decl, tmpl_args);
3927 result = finish_call_expr (decl, &args, false, true, tf_none);
3928 if (result != error_mark_node)
3930 release_tree_vector (args);
3931 return result;
3934 release_tree_vector (args);
3936 error ("unable to find numeric literal operator %qD", name);
3937 if (!cpp_get_options (parse_in)->ext_numeric_literals)
3938 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
3939 "to enable more built-in suffixes");
3940 return error_mark_node;
3943 /* Parse a user-defined string constant. Returns a call to a user-defined
3944 literal operator taking a character pointer and the length of the string
3945 as arguments. */
3947 static tree
3948 cp_parser_userdef_string_literal (cp_token *token)
3950 tree literal = token->u.value;
3951 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3952 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3953 tree value = USERDEF_LITERAL_VALUE (literal);
3954 int len = TREE_STRING_LENGTH (value)
3955 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3956 tree decl, result;
3957 vec<tree, va_gc> *args;
3959 /* Look for a template function with typename parameter CharT
3960 and parameter pack CharT... Call the function with
3961 template parameter characters representing the string. */
3962 args = make_tree_vector ();
3963 decl = lookup_literal_operator (name, args);
3964 if (decl && decl != error_mark_node)
3966 tree tmpl_args = make_string_pack (value);
3967 decl = lookup_template_function (decl, tmpl_args);
3968 result = finish_call_expr (decl, &args, false, true, tf_none);
3969 if (result != error_mark_node)
3971 release_tree_vector (args);
3972 return result;
3975 release_tree_vector (args);
3977 /* Build up a call to the user-defined operator */
3978 /* Lookup the name we got back from the id-expression. */
3979 args = make_tree_vector ();
3980 vec_safe_push (args, value);
3981 vec_safe_push (args, build_int_cst (size_type_node, len));
3982 decl = lookup_name (name);
3983 if (!decl || decl == error_mark_node)
3985 error ("unable to find string literal operator %qD", name);
3986 release_tree_vector (args);
3987 return error_mark_node;
3989 result = finish_call_expr (decl, &args, false, true, tf_none);
3990 release_tree_vector (args);
3991 if (result != error_mark_node)
3992 return result;
3994 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3995 name, TREE_TYPE (value), size_type_node);
3996 return error_mark_node;
4000 /* Basic concepts [gram.basic] */
4002 /* Parse a translation-unit.
4004 translation-unit:
4005 declaration-seq [opt]
4007 Returns TRUE if all went well. */
4009 static bool
4010 cp_parser_translation_unit (cp_parser* parser)
4012 /* The address of the first non-permanent object on the declarator
4013 obstack. */
4014 static void *declarator_obstack_base;
4016 bool success;
4018 /* Create the declarator obstack, if necessary. */
4019 if (!cp_error_declarator)
4021 gcc_obstack_init (&declarator_obstack);
4022 /* Create the error declarator. */
4023 cp_error_declarator = make_declarator (cdk_error);
4024 /* Create the empty parameter list. */
4025 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4026 /* Remember where the base of the declarator obstack lies. */
4027 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4030 cp_parser_declaration_seq_opt (parser);
4032 /* If there are no tokens left then all went well. */
4033 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4035 /* Get rid of the token array; we don't need it any more. */
4036 cp_lexer_destroy (parser->lexer);
4037 parser->lexer = NULL;
4039 /* This file might have been a context that's implicitly extern
4040 "C". If so, pop the lang context. (Only relevant for PCH.) */
4041 if (parser->implicit_extern_c)
4043 pop_lang_context ();
4044 parser->implicit_extern_c = false;
4047 /* Finish up. */
4048 finish_translation_unit ();
4050 success = true;
4052 else
4054 cp_parser_error (parser, "expected declaration");
4055 success = false;
4058 /* Make sure the declarator obstack was fully cleaned up. */
4059 gcc_assert (obstack_next_free (&declarator_obstack)
4060 == declarator_obstack_base);
4062 /* All went well. */
4063 return success;
4066 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4067 decltype context. */
4069 static inline tsubst_flags_t
4070 complain_flags (bool decltype_p)
4072 tsubst_flags_t complain = tf_warning_or_error;
4073 if (decltype_p)
4074 complain |= tf_decltype;
4075 return complain;
4078 /* Expressions [gram.expr] */
4080 /* Parse a primary-expression.
4082 primary-expression:
4083 literal
4084 this
4085 ( expression )
4086 id-expression
4088 GNU Extensions:
4090 primary-expression:
4091 ( compound-statement )
4092 __builtin_va_arg ( assignment-expression , type-id )
4093 __builtin_offsetof ( type-id , offsetof-expression )
4095 C++ Extensions:
4096 __has_nothrow_assign ( type-id )
4097 __has_nothrow_constructor ( type-id )
4098 __has_nothrow_copy ( type-id )
4099 __has_trivial_assign ( type-id )
4100 __has_trivial_constructor ( type-id )
4101 __has_trivial_copy ( type-id )
4102 __has_trivial_destructor ( type-id )
4103 __has_virtual_destructor ( type-id )
4104 __is_abstract ( type-id )
4105 __is_base_of ( type-id , type-id )
4106 __is_class ( type-id )
4107 __is_convertible_to ( type-id , type-id )
4108 __is_empty ( type-id )
4109 __is_enum ( type-id )
4110 __is_final ( type-id )
4111 __is_literal_type ( type-id )
4112 __is_pod ( type-id )
4113 __is_polymorphic ( type-id )
4114 __is_std_layout ( type-id )
4115 __is_trivial ( type-id )
4116 __is_union ( type-id )
4118 Objective-C++ Extension:
4120 primary-expression:
4121 objc-expression
4123 literal:
4124 __null
4126 ADDRESS_P is true iff this expression was immediately preceded by
4127 "&" and therefore might denote a pointer-to-member. CAST_P is true
4128 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4129 true iff this expression is a template argument.
4131 Returns a representation of the expression. Upon return, *IDK
4132 indicates what kind of id-expression (if any) was present. */
4134 static tree
4135 cp_parser_primary_expression (cp_parser *parser,
4136 bool address_p,
4137 bool cast_p,
4138 bool template_arg_p,
4139 bool decltype_p,
4140 cp_id_kind *idk)
4142 cp_token *token = NULL;
4144 /* Assume the primary expression is not an id-expression. */
4145 *idk = CP_ID_KIND_NONE;
4147 /* Peek at the next token. */
4148 token = cp_lexer_peek_token (parser->lexer);
4149 switch (token->type)
4151 /* literal:
4152 integer-literal
4153 character-literal
4154 floating-literal
4155 string-literal
4156 boolean-literal
4157 pointer-literal
4158 user-defined-literal */
4159 case CPP_CHAR:
4160 case CPP_CHAR16:
4161 case CPP_CHAR32:
4162 case CPP_WCHAR:
4163 case CPP_NUMBER:
4164 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4165 return cp_parser_userdef_numeric_literal (parser);
4166 token = cp_lexer_consume_token (parser->lexer);
4167 if (TREE_CODE (token->u.value) == FIXED_CST)
4169 error_at (token->location,
4170 "fixed-point types not supported in C++");
4171 return error_mark_node;
4173 /* Floating-point literals are only allowed in an integral
4174 constant expression if they are cast to an integral or
4175 enumeration type. */
4176 if (TREE_CODE (token->u.value) == REAL_CST
4177 && parser->integral_constant_expression_p
4178 && pedantic)
4180 /* CAST_P will be set even in invalid code like "int(2.7 +
4181 ...)". Therefore, we have to check that the next token
4182 is sure to end the cast. */
4183 if (cast_p)
4185 cp_token *next_token;
4187 next_token = cp_lexer_peek_token (parser->lexer);
4188 if (/* The comma at the end of an
4189 enumerator-definition. */
4190 next_token->type != CPP_COMMA
4191 /* The curly brace at the end of an enum-specifier. */
4192 && next_token->type != CPP_CLOSE_BRACE
4193 /* The end of a statement. */
4194 && next_token->type != CPP_SEMICOLON
4195 /* The end of the cast-expression. */
4196 && next_token->type != CPP_CLOSE_PAREN
4197 /* The end of an array bound. */
4198 && next_token->type != CPP_CLOSE_SQUARE
4199 /* The closing ">" in a template-argument-list. */
4200 && (next_token->type != CPP_GREATER
4201 || parser->greater_than_is_operator_p)
4202 /* C++0x only: A ">>" treated like two ">" tokens,
4203 in a template-argument-list. */
4204 && (next_token->type != CPP_RSHIFT
4205 || (cxx_dialect == cxx98)
4206 || parser->greater_than_is_operator_p))
4207 cast_p = false;
4210 /* If we are within a cast, then the constraint that the
4211 cast is to an integral or enumeration type will be
4212 checked at that point. If we are not within a cast, then
4213 this code is invalid. */
4214 if (!cast_p)
4215 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4217 return token->u.value;
4219 case CPP_CHAR_USERDEF:
4220 case CPP_CHAR16_USERDEF:
4221 case CPP_CHAR32_USERDEF:
4222 case CPP_WCHAR_USERDEF:
4223 return cp_parser_userdef_char_literal (parser);
4225 case CPP_STRING:
4226 case CPP_STRING16:
4227 case CPP_STRING32:
4228 case CPP_WSTRING:
4229 case CPP_UTF8STRING:
4230 case CPP_STRING_USERDEF:
4231 case CPP_STRING16_USERDEF:
4232 case CPP_STRING32_USERDEF:
4233 case CPP_WSTRING_USERDEF:
4234 case CPP_UTF8STRING_USERDEF:
4235 /* ??? Should wide strings be allowed when parser->translate_strings_p
4236 is false (i.e. in attributes)? If not, we can kill the third
4237 argument to cp_parser_string_literal. */
4238 return cp_parser_string_literal (parser,
4239 parser->translate_strings_p,
4240 true);
4242 case CPP_OPEN_PAREN:
4244 tree expr;
4245 bool saved_greater_than_is_operator_p;
4247 /* Consume the `('. */
4248 cp_lexer_consume_token (parser->lexer);
4249 /* Within a parenthesized expression, a `>' token is always
4250 the greater-than operator. */
4251 saved_greater_than_is_operator_p
4252 = parser->greater_than_is_operator_p;
4253 parser->greater_than_is_operator_p = true;
4254 /* If we see `( { ' then we are looking at the beginning of
4255 a GNU statement-expression. */
4256 if (cp_parser_allow_gnu_extensions_p (parser)
4257 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4259 /* Statement-expressions are not allowed by the standard. */
4260 pedwarn (token->location, OPT_Wpedantic,
4261 "ISO C++ forbids braced-groups within expressions");
4263 /* And they're not allowed outside of a function-body; you
4264 cannot, for example, write:
4266 int i = ({ int j = 3; j + 1; });
4268 at class or namespace scope. */
4269 if (!parser->in_function_body
4270 || parser->in_template_argument_list_p)
4272 error_at (token->location,
4273 "statement-expressions are not allowed outside "
4274 "functions nor in template-argument lists");
4275 cp_parser_skip_to_end_of_block_or_statement (parser);
4276 expr = error_mark_node;
4278 else
4280 /* Start the statement-expression. */
4281 expr = begin_stmt_expr ();
4282 /* Parse the compound-statement. */
4283 cp_parser_compound_statement (parser, expr, false, false);
4284 /* Finish up. */
4285 expr = finish_stmt_expr (expr, false);
4288 else
4290 /* Parse the parenthesized expression. */
4291 expr = cp_parser_expression (parser, cast_p, decltype_p, idk);
4292 /* Let the front end know that this expression was
4293 enclosed in parentheses. This matters in case, for
4294 example, the expression is of the form `A::B', since
4295 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4296 not. */
4297 expr = finish_parenthesized_expr (expr);
4298 /* DR 705: Wrapping an unqualified name in parentheses
4299 suppresses arg-dependent lookup. We want to pass back
4300 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4301 (c++/37862), but none of the others. */
4302 if (*idk != CP_ID_KIND_QUALIFIED)
4303 *idk = CP_ID_KIND_NONE;
4305 /* The `>' token might be the end of a template-id or
4306 template-parameter-list now. */
4307 parser->greater_than_is_operator_p
4308 = saved_greater_than_is_operator_p;
4309 /* Consume the `)'. */
4310 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4311 cp_parser_skip_to_end_of_statement (parser);
4313 return expr;
4316 case CPP_OPEN_SQUARE:
4317 if (c_dialect_objc ())
4318 /* We have an Objective-C++ message. */
4319 return cp_parser_objc_expression (parser);
4321 tree lam = cp_parser_lambda_expression (parser);
4322 /* Don't warn about a failed tentative parse. */
4323 if (cp_parser_error_occurred (parser))
4324 return error_mark_node;
4325 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4326 return lam;
4329 case CPP_OBJC_STRING:
4330 if (c_dialect_objc ())
4331 /* We have an Objective-C++ string literal. */
4332 return cp_parser_objc_expression (parser);
4333 cp_parser_error (parser, "expected primary-expression");
4334 return error_mark_node;
4336 case CPP_KEYWORD:
4337 switch (token->keyword)
4339 /* These two are the boolean literals. */
4340 case RID_TRUE:
4341 cp_lexer_consume_token (parser->lexer);
4342 return boolean_true_node;
4343 case RID_FALSE:
4344 cp_lexer_consume_token (parser->lexer);
4345 return boolean_false_node;
4347 /* The `__null' literal. */
4348 case RID_NULL:
4349 cp_lexer_consume_token (parser->lexer);
4350 return null_node;
4352 /* The `nullptr' literal. */
4353 case RID_NULLPTR:
4354 cp_lexer_consume_token (parser->lexer);
4355 return nullptr_node;
4357 /* Recognize the `this' keyword. */
4358 case RID_THIS:
4359 cp_lexer_consume_token (parser->lexer);
4360 if (parser->local_variables_forbidden_p)
4362 error_at (token->location,
4363 "%<this%> may not be used in this context");
4364 return error_mark_node;
4366 /* Pointers cannot appear in constant-expressions. */
4367 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4368 return error_mark_node;
4369 return finish_this_expr ();
4371 /* The `operator' keyword can be the beginning of an
4372 id-expression. */
4373 case RID_OPERATOR:
4374 goto id_expression;
4376 case RID_FUNCTION_NAME:
4377 case RID_PRETTY_FUNCTION_NAME:
4378 case RID_C99_FUNCTION_NAME:
4380 non_integral_constant name;
4382 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4383 __func__ are the names of variables -- but they are
4384 treated specially. Therefore, they are handled here,
4385 rather than relying on the generic id-expression logic
4386 below. Grammatically, these names are id-expressions.
4388 Consume the token. */
4389 token = cp_lexer_consume_token (parser->lexer);
4391 switch (token->keyword)
4393 case RID_FUNCTION_NAME:
4394 name = NIC_FUNC_NAME;
4395 break;
4396 case RID_PRETTY_FUNCTION_NAME:
4397 name = NIC_PRETTY_FUNC;
4398 break;
4399 case RID_C99_FUNCTION_NAME:
4400 name = NIC_C99_FUNC;
4401 break;
4402 default:
4403 gcc_unreachable ();
4406 if (cp_parser_non_integral_constant_expression (parser, name))
4407 return error_mark_node;
4409 /* Look up the name. */
4410 return finish_fname (token->u.value);
4413 case RID_VA_ARG:
4415 tree expression;
4416 tree type;
4417 source_location type_location;
4419 /* The `__builtin_va_arg' construct is used to handle
4420 `va_arg'. Consume the `__builtin_va_arg' token. */
4421 cp_lexer_consume_token (parser->lexer);
4422 /* Look for the opening `('. */
4423 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4424 /* Now, parse the assignment-expression. */
4425 expression = cp_parser_assignment_expression (parser,
4426 /*cast_p=*/false, NULL);
4427 /* Look for the `,'. */
4428 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4429 type_location = cp_lexer_peek_token (parser->lexer)->location;
4430 /* Parse the type-id. */
4431 type = cp_parser_type_id (parser);
4432 /* Look for the closing `)'. */
4433 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4434 /* Using `va_arg' in a constant-expression is not
4435 allowed. */
4436 if (cp_parser_non_integral_constant_expression (parser,
4437 NIC_VA_ARG))
4438 return error_mark_node;
4439 return build_x_va_arg (type_location, expression, type);
4442 case RID_OFFSETOF:
4443 return cp_parser_builtin_offsetof (parser);
4445 case RID_HAS_NOTHROW_ASSIGN:
4446 case RID_HAS_NOTHROW_CONSTRUCTOR:
4447 case RID_HAS_NOTHROW_COPY:
4448 case RID_HAS_TRIVIAL_ASSIGN:
4449 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4450 case RID_HAS_TRIVIAL_COPY:
4451 case RID_HAS_TRIVIAL_DESTRUCTOR:
4452 case RID_HAS_VIRTUAL_DESTRUCTOR:
4453 case RID_IS_ABSTRACT:
4454 case RID_IS_BASE_OF:
4455 case RID_IS_CLASS:
4456 case RID_IS_CONVERTIBLE_TO:
4457 case RID_IS_EMPTY:
4458 case RID_IS_ENUM:
4459 case RID_IS_FINAL:
4460 case RID_IS_LITERAL_TYPE:
4461 case RID_IS_POD:
4462 case RID_IS_POLYMORPHIC:
4463 case RID_IS_STD_LAYOUT:
4464 case RID_IS_TRIVIAL:
4465 case RID_IS_UNION:
4466 return cp_parser_trait_expr (parser, token->keyword);
4468 /* Objective-C++ expressions. */
4469 case RID_AT_ENCODE:
4470 case RID_AT_PROTOCOL:
4471 case RID_AT_SELECTOR:
4472 return cp_parser_objc_expression (parser);
4474 case RID_TEMPLATE:
4475 if (parser->in_function_body
4476 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4477 == CPP_LESS))
4479 error_at (token->location,
4480 "a template declaration cannot appear at block scope");
4481 cp_parser_skip_to_end_of_block_or_statement (parser);
4482 return error_mark_node;
4484 default:
4485 cp_parser_error (parser, "expected primary-expression");
4486 return error_mark_node;
4489 /* An id-expression can start with either an identifier, a
4490 `::' as the beginning of a qualified-id, or the "operator"
4491 keyword. */
4492 case CPP_NAME:
4493 case CPP_SCOPE:
4494 case CPP_TEMPLATE_ID:
4495 case CPP_NESTED_NAME_SPECIFIER:
4497 tree id_expression;
4498 tree decl;
4499 const char *error_msg;
4500 bool template_p;
4501 bool done;
4502 cp_token *id_expr_token;
4504 id_expression:
4505 /* Parse the id-expression. */
4506 id_expression
4507 = cp_parser_id_expression (parser,
4508 /*template_keyword_p=*/false,
4509 /*check_dependency_p=*/true,
4510 &template_p,
4511 /*declarator_p=*/false,
4512 /*optional_p=*/false);
4513 if (id_expression == error_mark_node)
4514 return error_mark_node;
4515 id_expr_token = token;
4516 token = cp_lexer_peek_token (parser->lexer);
4517 done = (token->type != CPP_OPEN_SQUARE
4518 && token->type != CPP_OPEN_PAREN
4519 && token->type != CPP_DOT
4520 && token->type != CPP_DEREF
4521 && token->type != CPP_PLUS_PLUS
4522 && token->type != CPP_MINUS_MINUS);
4523 /* If we have a template-id, then no further lookup is
4524 required. If the template-id was for a template-class, we
4525 will sometimes have a TYPE_DECL at this point. */
4526 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4527 || TREE_CODE (id_expression) == TYPE_DECL)
4528 decl = id_expression;
4529 /* Look up the name. */
4530 else
4532 tree ambiguous_decls;
4534 /* If we already know that this lookup is ambiguous, then
4535 we've already issued an error message; there's no reason
4536 to check again. */
4537 if (id_expr_token->type == CPP_NAME
4538 && id_expr_token->error_reported)
4540 cp_parser_simulate_error (parser);
4541 return error_mark_node;
4544 decl = cp_parser_lookup_name (parser, id_expression,
4545 none_type,
4546 template_p,
4547 /*is_namespace=*/false,
4548 /*check_dependency=*/true,
4549 &ambiguous_decls,
4550 id_expr_token->location);
4551 /* If the lookup was ambiguous, an error will already have
4552 been issued. */
4553 if (ambiguous_decls)
4554 return error_mark_node;
4556 /* In Objective-C++, we may have an Objective-C 2.0
4557 dot-syntax for classes here. */
4558 if (c_dialect_objc ()
4559 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4560 && TREE_CODE (decl) == TYPE_DECL
4561 && objc_is_class_name (decl))
4563 tree component;
4564 cp_lexer_consume_token (parser->lexer);
4565 component = cp_parser_identifier (parser);
4566 if (component == error_mark_node)
4567 return error_mark_node;
4569 return objc_build_class_component_ref (id_expression, component);
4572 /* In Objective-C++, an instance variable (ivar) may be preferred
4573 to whatever cp_parser_lookup_name() found. */
4574 decl = objc_lookup_ivar (decl, id_expression);
4576 /* If name lookup gives us a SCOPE_REF, then the
4577 qualifying scope was dependent. */
4578 if (TREE_CODE (decl) == SCOPE_REF)
4580 /* At this point, we do not know if DECL is a valid
4581 integral constant expression. We assume that it is
4582 in fact such an expression, so that code like:
4584 template <int N> struct A {
4585 int a[B<N>::i];
4588 is accepted. At template-instantiation time, we
4589 will check that B<N>::i is actually a constant. */
4590 return decl;
4592 /* Check to see if DECL is a local variable in a context
4593 where that is forbidden. */
4594 if (parser->local_variables_forbidden_p
4595 && local_variable_p (decl))
4597 /* It might be that we only found DECL because we are
4598 trying to be generous with pre-ISO scoping rules.
4599 For example, consider:
4601 int i;
4602 void g() {
4603 for (int i = 0; i < 10; ++i) {}
4604 extern void f(int j = i);
4607 Here, name look up will originally find the out
4608 of scope `i'. We need to issue a warning message,
4609 but then use the global `i'. */
4610 decl = check_for_out_of_scope_variable (decl);
4611 if (local_variable_p (decl))
4613 error_at (id_expr_token->location,
4614 "local variable %qD may not appear in this context",
4615 decl);
4616 return error_mark_node;
4621 decl = (finish_id_expression
4622 (id_expression, decl, parser->scope,
4623 idk,
4624 parser->integral_constant_expression_p,
4625 parser->allow_non_integral_constant_expression_p,
4626 &parser->non_integral_constant_expression_p,
4627 template_p, done, address_p,
4628 template_arg_p,
4629 &error_msg,
4630 id_expr_token->location));
4631 if (error_msg)
4632 cp_parser_error (parser, error_msg);
4633 return decl;
4636 /* Anything else is an error. */
4637 default:
4638 cp_parser_error (parser, "expected primary-expression");
4639 return error_mark_node;
4643 static inline tree
4644 cp_parser_primary_expression (cp_parser *parser,
4645 bool address_p,
4646 bool cast_p,
4647 bool template_arg_p,
4648 cp_id_kind *idk)
4650 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4651 /*decltype*/false, idk);
4654 /* Parse an id-expression.
4656 id-expression:
4657 unqualified-id
4658 qualified-id
4660 qualified-id:
4661 :: [opt] nested-name-specifier template [opt] unqualified-id
4662 :: identifier
4663 :: operator-function-id
4664 :: template-id
4666 Return a representation of the unqualified portion of the
4667 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4668 a `::' or nested-name-specifier.
4670 Often, if the id-expression was a qualified-id, the caller will
4671 want to make a SCOPE_REF to represent the qualified-id. This
4672 function does not do this in order to avoid wastefully creating
4673 SCOPE_REFs when they are not required.
4675 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4676 `template' keyword.
4678 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4679 uninstantiated templates.
4681 If *TEMPLATE_P is non-NULL, it is set to true iff the
4682 `template' keyword is used to explicitly indicate that the entity
4683 named is a template.
4685 If DECLARATOR_P is true, the id-expression is appearing as part of
4686 a declarator, rather than as part of an expression. */
4688 static tree
4689 cp_parser_id_expression (cp_parser *parser,
4690 bool template_keyword_p,
4691 bool check_dependency_p,
4692 bool *template_p,
4693 bool declarator_p,
4694 bool optional_p)
4696 bool global_scope_p;
4697 bool nested_name_specifier_p;
4699 /* Assume the `template' keyword was not used. */
4700 if (template_p)
4701 *template_p = template_keyword_p;
4703 /* Look for the optional `::' operator. */
4704 global_scope_p
4705 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4706 != NULL_TREE);
4707 /* Look for the optional nested-name-specifier. */
4708 nested_name_specifier_p
4709 = (cp_parser_nested_name_specifier_opt (parser,
4710 /*typename_keyword_p=*/false,
4711 check_dependency_p,
4712 /*type_p=*/false,
4713 declarator_p)
4714 != NULL_TREE);
4715 /* If there is a nested-name-specifier, then we are looking at
4716 the first qualified-id production. */
4717 if (nested_name_specifier_p)
4719 tree saved_scope;
4720 tree saved_object_scope;
4721 tree saved_qualifying_scope;
4722 tree unqualified_id;
4723 bool is_template;
4725 /* See if the next token is the `template' keyword. */
4726 if (!template_p)
4727 template_p = &is_template;
4728 *template_p = cp_parser_optional_template_keyword (parser);
4729 /* Name lookup we do during the processing of the
4730 unqualified-id might obliterate SCOPE. */
4731 saved_scope = parser->scope;
4732 saved_object_scope = parser->object_scope;
4733 saved_qualifying_scope = parser->qualifying_scope;
4734 /* Process the final unqualified-id. */
4735 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4736 check_dependency_p,
4737 declarator_p,
4738 /*optional_p=*/false);
4739 /* Restore the SAVED_SCOPE for our caller. */
4740 parser->scope = saved_scope;
4741 parser->object_scope = saved_object_scope;
4742 parser->qualifying_scope = saved_qualifying_scope;
4744 return unqualified_id;
4746 /* Otherwise, if we are in global scope, then we are looking at one
4747 of the other qualified-id productions. */
4748 else if (global_scope_p)
4750 cp_token *token;
4751 tree id;
4753 /* Peek at the next token. */
4754 token = cp_lexer_peek_token (parser->lexer);
4756 /* If it's an identifier, and the next token is not a "<", then
4757 we can avoid the template-id case. This is an optimization
4758 for this common case. */
4759 if (token->type == CPP_NAME
4760 && !cp_parser_nth_token_starts_template_argument_list_p
4761 (parser, 2))
4762 return cp_parser_identifier (parser);
4764 cp_parser_parse_tentatively (parser);
4765 /* Try a template-id. */
4766 id = cp_parser_template_id (parser,
4767 /*template_keyword_p=*/false,
4768 /*check_dependency_p=*/true,
4769 none_type,
4770 declarator_p);
4771 /* If that worked, we're done. */
4772 if (cp_parser_parse_definitely (parser))
4773 return id;
4775 /* Peek at the next token. (Changes in the token buffer may
4776 have invalidated the pointer obtained above.) */
4777 token = cp_lexer_peek_token (parser->lexer);
4779 switch (token->type)
4781 case CPP_NAME:
4782 return cp_parser_identifier (parser);
4784 case CPP_KEYWORD:
4785 if (token->keyword == RID_OPERATOR)
4786 return cp_parser_operator_function_id (parser);
4787 /* Fall through. */
4789 default:
4790 cp_parser_error (parser, "expected id-expression");
4791 return error_mark_node;
4794 else
4795 return cp_parser_unqualified_id (parser, template_keyword_p,
4796 /*check_dependency_p=*/true,
4797 declarator_p,
4798 optional_p);
4801 /* Parse an unqualified-id.
4803 unqualified-id:
4804 identifier
4805 operator-function-id
4806 conversion-function-id
4807 ~ class-name
4808 template-id
4810 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4811 keyword, in a construct like `A::template ...'.
4813 Returns a representation of unqualified-id. For the `identifier'
4814 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4815 production a BIT_NOT_EXPR is returned; the operand of the
4816 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4817 other productions, see the documentation accompanying the
4818 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4819 names are looked up in uninstantiated templates. If DECLARATOR_P
4820 is true, the unqualified-id is appearing as part of a declarator,
4821 rather than as part of an expression. */
4823 static tree
4824 cp_parser_unqualified_id (cp_parser* parser,
4825 bool template_keyword_p,
4826 bool check_dependency_p,
4827 bool declarator_p,
4828 bool optional_p)
4830 cp_token *token;
4832 /* Peek at the next token. */
4833 token = cp_lexer_peek_token (parser->lexer);
4835 switch (token->type)
4837 case CPP_NAME:
4839 tree id;
4841 /* We don't know yet whether or not this will be a
4842 template-id. */
4843 cp_parser_parse_tentatively (parser);
4844 /* Try a template-id. */
4845 id = cp_parser_template_id (parser, template_keyword_p,
4846 check_dependency_p,
4847 none_type,
4848 declarator_p);
4849 /* If it worked, we're done. */
4850 if (cp_parser_parse_definitely (parser))
4851 return id;
4852 /* Otherwise, it's an ordinary identifier. */
4853 return cp_parser_identifier (parser);
4856 case CPP_TEMPLATE_ID:
4857 return cp_parser_template_id (parser, template_keyword_p,
4858 check_dependency_p,
4859 none_type,
4860 declarator_p);
4862 case CPP_COMPL:
4864 tree type_decl;
4865 tree qualifying_scope;
4866 tree object_scope;
4867 tree scope;
4868 bool done;
4870 /* Consume the `~' token. */
4871 cp_lexer_consume_token (parser->lexer);
4872 /* Parse the class-name. The standard, as written, seems to
4873 say that:
4875 template <typename T> struct S { ~S (); };
4876 template <typename T> S<T>::~S() {}
4878 is invalid, since `~' must be followed by a class-name, but
4879 `S<T>' is dependent, and so not known to be a class.
4880 That's not right; we need to look in uninstantiated
4881 templates. A further complication arises from:
4883 template <typename T> void f(T t) {
4884 t.T::~T();
4887 Here, it is not possible to look up `T' in the scope of `T'
4888 itself. We must look in both the current scope, and the
4889 scope of the containing complete expression.
4891 Yet another issue is:
4893 struct S {
4894 int S;
4895 ~S();
4898 S::~S() {}
4900 The standard does not seem to say that the `S' in `~S'
4901 should refer to the type `S' and not the data member
4902 `S::S'. */
4904 /* DR 244 says that we look up the name after the "~" in the
4905 same scope as we looked up the qualifying name. That idea
4906 isn't fully worked out; it's more complicated than that. */
4907 scope = parser->scope;
4908 object_scope = parser->object_scope;
4909 qualifying_scope = parser->qualifying_scope;
4911 /* Check for invalid scopes. */
4912 if (scope == error_mark_node)
4914 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4915 cp_lexer_consume_token (parser->lexer);
4916 return error_mark_node;
4918 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4920 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4921 error_at (token->location,
4922 "scope %qT before %<~%> is not a class-name",
4923 scope);
4924 cp_parser_simulate_error (parser);
4925 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4926 cp_lexer_consume_token (parser->lexer);
4927 return error_mark_node;
4929 gcc_assert (!scope || TYPE_P (scope));
4931 /* If the name is of the form "X::~X" it's OK even if X is a
4932 typedef. */
4933 token = cp_lexer_peek_token (parser->lexer);
4934 if (scope
4935 && token->type == CPP_NAME
4936 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4937 != CPP_LESS)
4938 && (token->u.value == TYPE_IDENTIFIER (scope)
4939 || (CLASS_TYPE_P (scope)
4940 && constructor_name_p (token->u.value, scope))))
4942 cp_lexer_consume_token (parser->lexer);
4943 return build_nt (BIT_NOT_EXPR, scope);
4946 /* ~auto means the destructor of whatever the object is. */
4947 if (cp_parser_is_keyword (token, RID_AUTO))
4949 if (cxx_dialect < cxx1y)
4950 pedwarn (input_location, 0,
4951 "%<~auto%> only available with "
4952 "-std=c++1y or -std=gnu++1y");
4953 cp_lexer_consume_token (parser->lexer);
4954 return build_nt (BIT_NOT_EXPR, make_auto ());
4957 /* If there was an explicit qualification (S::~T), first look
4958 in the scope given by the qualification (i.e., S).
4960 Note: in the calls to cp_parser_class_name below we pass
4961 typename_type so that lookup finds the injected-class-name
4962 rather than the constructor. */
4963 done = false;
4964 type_decl = NULL_TREE;
4965 if (scope)
4967 cp_parser_parse_tentatively (parser);
4968 type_decl = cp_parser_class_name (parser,
4969 /*typename_keyword_p=*/false,
4970 /*template_keyword_p=*/false,
4971 typename_type,
4972 /*check_dependency=*/false,
4973 /*class_head_p=*/false,
4974 declarator_p);
4975 if (cp_parser_parse_definitely (parser))
4976 done = true;
4978 /* In "N::S::~S", look in "N" as well. */
4979 if (!done && scope && qualifying_scope)
4981 cp_parser_parse_tentatively (parser);
4982 parser->scope = qualifying_scope;
4983 parser->object_scope = NULL_TREE;
4984 parser->qualifying_scope = NULL_TREE;
4985 type_decl
4986 = cp_parser_class_name (parser,
4987 /*typename_keyword_p=*/false,
4988 /*template_keyword_p=*/false,
4989 typename_type,
4990 /*check_dependency=*/false,
4991 /*class_head_p=*/false,
4992 declarator_p);
4993 if (cp_parser_parse_definitely (parser))
4994 done = true;
4996 /* In "p->S::~T", look in the scope given by "*p" as well. */
4997 else if (!done && object_scope)
4999 cp_parser_parse_tentatively (parser);
5000 parser->scope = object_scope;
5001 parser->object_scope = NULL_TREE;
5002 parser->qualifying_scope = NULL_TREE;
5003 type_decl
5004 = cp_parser_class_name (parser,
5005 /*typename_keyword_p=*/false,
5006 /*template_keyword_p=*/false,
5007 typename_type,
5008 /*check_dependency=*/false,
5009 /*class_head_p=*/false,
5010 declarator_p);
5011 if (cp_parser_parse_definitely (parser))
5012 done = true;
5014 /* Look in the surrounding context. */
5015 if (!done)
5017 parser->scope = NULL_TREE;
5018 parser->object_scope = NULL_TREE;
5019 parser->qualifying_scope = NULL_TREE;
5020 if (processing_template_decl)
5021 cp_parser_parse_tentatively (parser);
5022 type_decl
5023 = cp_parser_class_name (parser,
5024 /*typename_keyword_p=*/false,
5025 /*template_keyword_p=*/false,
5026 typename_type,
5027 /*check_dependency=*/false,
5028 /*class_head_p=*/false,
5029 declarator_p);
5030 if (processing_template_decl
5031 && ! cp_parser_parse_definitely (parser))
5033 /* We couldn't find a type with this name, so just accept
5034 it and check for a match at instantiation time. */
5035 type_decl = cp_parser_identifier (parser);
5036 if (type_decl != error_mark_node)
5037 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5038 return type_decl;
5041 /* If an error occurred, assume that the name of the
5042 destructor is the same as the name of the qualifying
5043 class. That allows us to keep parsing after running
5044 into ill-formed destructor names. */
5045 if (type_decl == error_mark_node && scope)
5046 return build_nt (BIT_NOT_EXPR, scope);
5047 else if (type_decl == error_mark_node)
5048 return error_mark_node;
5050 /* Check that destructor name and scope match. */
5051 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5053 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5054 error_at (token->location,
5055 "declaration of %<~%T%> as member of %qT",
5056 type_decl, scope);
5057 cp_parser_simulate_error (parser);
5058 return error_mark_node;
5061 /* [class.dtor]
5063 A typedef-name that names a class shall not be used as the
5064 identifier in the declarator for a destructor declaration. */
5065 if (declarator_p
5066 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5067 && !DECL_SELF_REFERENCE_P (type_decl)
5068 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5069 error_at (token->location,
5070 "typedef-name %qD used as destructor declarator",
5071 type_decl);
5073 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5076 case CPP_KEYWORD:
5077 if (token->keyword == RID_OPERATOR)
5079 tree id;
5081 /* This could be a template-id, so we try that first. */
5082 cp_parser_parse_tentatively (parser);
5083 /* Try a template-id. */
5084 id = cp_parser_template_id (parser, template_keyword_p,
5085 /*check_dependency_p=*/true,
5086 none_type,
5087 declarator_p);
5088 /* If that worked, we're done. */
5089 if (cp_parser_parse_definitely (parser))
5090 return id;
5091 /* We still don't know whether we're looking at an
5092 operator-function-id or a conversion-function-id. */
5093 cp_parser_parse_tentatively (parser);
5094 /* Try an operator-function-id. */
5095 id = cp_parser_operator_function_id (parser);
5096 /* If that didn't work, try a conversion-function-id. */
5097 if (!cp_parser_parse_definitely (parser))
5098 id = cp_parser_conversion_function_id (parser);
5099 else if (UDLIT_OPER_P (id))
5101 /* 17.6.3.3.5 */
5102 const char *name = UDLIT_OP_SUFFIX (id);
5103 if (name[0] != '_' && !in_system_header_at (input_location)
5104 && declarator_p)
5105 warning (0, "literal operator suffixes not preceded by %<_%>"
5106 " are reserved for future standardization");
5109 return id;
5111 /* Fall through. */
5113 default:
5114 if (optional_p)
5115 return NULL_TREE;
5116 cp_parser_error (parser, "expected unqualified-id");
5117 return error_mark_node;
5121 /* Parse an (optional) nested-name-specifier.
5123 nested-name-specifier: [C++98]
5124 class-or-namespace-name :: nested-name-specifier [opt]
5125 class-or-namespace-name :: template nested-name-specifier [opt]
5127 nested-name-specifier: [C++0x]
5128 type-name ::
5129 namespace-name ::
5130 nested-name-specifier identifier ::
5131 nested-name-specifier template [opt] simple-template-id ::
5133 PARSER->SCOPE should be set appropriately before this function is
5134 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5135 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5136 in name lookups.
5138 Sets PARSER->SCOPE to the class (TYPE) or namespace
5139 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5140 it unchanged if there is no nested-name-specifier. Returns the new
5141 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5143 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5144 part of a declaration and/or decl-specifier. */
5146 static tree
5147 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5148 bool typename_keyword_p,
5149 bool check_dependency_p,
5150 bool type_p,
5151 bool is_declaration)
5153 bool success = false;
5154 cp_token_position start = 0;
5155 cp_token *token;
5157 /* Remember where the nested-name-specifier starts. */
5158 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5160 start = cp_lexer_token_position (parser->lexer, false);
5161 push_deferring_access_checks (dk_deferred);
5164 while (true)
5166 tree new_scope;
5167 tree old_scope;
5168 tree saved_qualifying_scope;
5169 bool template_keyword_p;
5171 /* Spot cases that cannot be the beginning of a
5172 nested-name-specifier. */
5173 token = cp_lexer_peek_token (parser->lexer);
5175 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5176 the already parsed nested-name-specifier. */
5177 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5179 /* Grab the nested-name-specifier and continue the loop. */
5180 cp_parser_pre_parsed_nested_name_specifier (parser);
5181 /* If we originally encountered this nested-name-specifier
5182 with IS_DECLARATION set to false, we will not have
5183 resolved TYPENAME_TYPEs, so we must do so here. */
5184 if (is_declaration
5185 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5187 new_scope = resolve_typename_type (parser->scope,
5188 /*only_current_p=*/false);
5189 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5190 parser->scope = new_scope;
5192 success = true;
5193 continue;
5196 /* Spot cases that cannot be the beginning of a
5197 nested-name-specifier. On the second and subsequent times
5198 through the loop, we look for the `template' keyword. */
5199 if (success && token->keyword == RID_TEMPLATE)
5201 /* A template-id can start a nested-name-specifier. */
5202 else if (token->type == CPP_TEMPLATE_ID)
5204 /* DR 743: decltype can be used in a nested-name-specifier. */
5205 else if (token_is_decltype (token))
5207 else
5209 /* If the next token is not an identifier, then it is
5210 definitely not a type-name or namespace-name. */
5211 if (token->type != CPP_NAME)
5212 break;
5213 /* If the following token is neither a `<' (to begin a
5214 template-id), nor a `::', then we are not looking at a
5215 nested-name-specifier. */
5216 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5218 if (token->type == CPP_COLON
5219 && parser->colon_corrects_to_scope_p
5220 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5222 error_at (token->location,
5223 "found %<:%> in nested-name-specifier, expected %<::%>");
5224 token->type = CPP_SCOPE;
5227 if (token->type != CPP_SCOPE
5228 && !cp_parser_nth_token_starts_template_argument_list_p
5229 (parser, 2))
5230 break;
5233 /* The nested-name-specifier is optional, so we parse
5234 tentatively. */
5235 cp_parser_parse_tentatively (parser);
5237 /* Look for the optional `template' keyword, if this isn't the
5238 first time through the loop. */
5239 if (success)
5240 template_keyword_p = cp_parser_optional_template_keyword (parser);
5241 else
5242 template_keyword_p = false;
5244 /* Save the old scope since the name lookup we are about to do
5245 might destroy it. */
5246 old_scope = parser->scope;
5247 saved_qualifying_scope = parser->qualifying_scope;
5248 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5249 look up names in "X<T>::I" in order to determine that "Y" is
5250 a template. So, if we have a typename at this point, we make
5251 an effort to look through it. */
5252 if (is_declaration
5253 && !typename_keyword_p
5254 && parser->scope
5255 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5256 parser->scope = resolve_typename_type (parser->scope,
5257 /*only_current_p=*/false);
5258 /* Parse the qualifying entity. */
5259 new_scope
5260 = cp_parser_qualifying_entity (parser,
5261 typename_keyword_p,
5262 template_keyword_p,
5263 check_dependency_p,
5264 type_p,
5265 is_declaration);
5266 /* Look for the `::' token. */
5267 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5269 /* If we found what we wanted, we keep going; otherwise, we're
5270 done. */
5271 if (!cp_parser_parse_definitely (parser))
5273 bool error_p = false;
5275 /* Restore the OLD_SCOPE since it was valid before the
5276 failed attempt at finding the last
5277 class-or-namespace-name. */
5278 parser->scope = old_scope;
5279 parser->qualifying_scope = saved_qualifying_scope;
5281 /* If the next token is a decltype, and the one after that is a
5282 `::', then the decltype has failed to resolve to a class or
5283 enumeration type. Give this error even when parsing
5284 tentatively since it can't possibly be valid--and we're going
5285 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5286 won't get another chance.*/
5287 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5288 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5289 == CPP_SCOPE))
5291 token = cp_lexer_consume_token (parser->lexer);
5292 error_at (token->location, "decltype evaluates to %qT, "
5293 "which is not a class or enumeration type",
5294 token->u.value);
5295 parser->scope = error_mark_node;
5296 error_p = true;
5297 /* As below. */
5298 success = true;
5299 cp_lexer_consume_token (parser->lexer);
5302 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5303 break;
5304 /* If the next token is an identifier, and the one after
5305 that is a `::', then any valid interpretation would have
5306 found a class-or-namespace-name. */
5307 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5308 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5309 == CPP_SCOPE)
5310 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5311 != CPP_COMPL))
5313 token = cp_lexer_consume_token (parser->lexer);
5314 if (!error_p)
5316 if (!token->error_reported)
5318 tree decl;
5319 tree ambiguous_decls;
5321 decl = cp_parser_lookup_name (parser, token->u.value,
5322 none_type,
5323 /*is_template=*/false,
5324 /*is_namespace=*/false,
5325 /*check_dependency=*/true,
5326 &ambiguous_decls,
5327 token->location);
5328 if (TREE_CODE (decl) == TEMPLATE_DECL)
5329 error_at (token->location,
5330 "%qD used without template parameters",
5331 decl);
5332 else if (ambiguous_decls)
5334 // cp_parser_lookup_name has the same diagnostic,
5335 // thus make sure to emit it at most once.
5336 if (cp_parser_uncommitted_to_tentative_parse_p
5337 (parser))
5339 error_at (token->location,
5340 "reference to %qD is ambiguous",
5341 token->u.value);
5342 print_candidates (ambiguous_decls);
5344 decl = error_mark_node;
5346 else
5348 if (cxx_dialect != cxx98)
5349 cp_parser_name_lookup_error
5350 (parser, token->u.value, decl, NLE_NOT_CXX98,
5351 token->location);
5352 else
5353 cp_parser_name_lookup_error
5354 (parser, token->u.value, decl, NLE_CXX98,
5355 token->location);
5358 parser->scope = error_mark_node;
5359 error_p = true;
5360 /* Treat this as a successful nested-name-specifier
5361 due to:
5363 [basic.lookup.qual]
5365 If the name found is not a class-name (clause
5366 _class_) or namespace-name (_namespace.def_), the
5367 program is ill-formed. */
5368 success = true;
5370 cp_lexer_consume_token (parser->lexer);
5372 break;
5374 /* We've found one valid nested-name-specifier. */
5375 success = true;
5376 /* Name lookup always gives us a DECL. */
5377 if (TREE_CODE (new_scope) == TYPE_DECL)
5378 new_scope = TREE_TYPE (new_scope);
5379 /* Uses of "template" must be followed by actual templates. */
5380 if (template_keyword_p
5381 && !(CLASS_TYPE_P (new_scope)
5382 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5383 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5384 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5385 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5386 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5387 == TEMPLATE_ID_EXPR)))
5388 permerror (input_location, TYPE_P (new_scope)
5389 ? G_("%qT is not a template")
5390 : G_("%qD is not a template"),
5391 new_scope);
5392 /* If it is a class scope, try to complete it; we are about to
5393 be looking up names inside the class. */
5394 if (TYPE_P (new_scope)
5395 /* Since checking types for dependency can be expensive,
5396 avoid doing it if the type is already complete. */
5397 && !COMPLETE_TYPE_P (new_scope)
5398 /* Do not try to complete dependent types. */
5399 && !dependent_type_p (new_scope))
5401 new_scope = complete_type (new_scope);
5402 /* If it is a typedef to current class, use the current
5403 class instead, as the typedef won't have any names inside
5404 it yet. */
5405 if (!COMPLETE_TYPE_P (new_scope)
5406 && currently_open_class (new_scope))
5407 new_scope = TYPE_MAIN_VARIANT (new_scope);
5409 /* Make sure we look in the right scope the next time through
5410 the loop. */
5411 parser->scope = new_scope;
5414 /* If parsing tentatively, replace the sequence of tokens that makes
5415 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5416 token. That way, should we re-parse the token stream, we will
5417 not have to repeat the effort required to do the parse, nor will
5418 we issue duplicate error messages. */
5419 if (success && start)
5421 cp_token *token;
5423 token = cp_lexer_token_at (parser->lexer, start);
5424 /* Reset the contents of the START token. */
5425 token->type = CPP_NESTED_NAME_SPECIFIER;
5426 /* Retrieve any deferred checks. Do not pop this access checks yet
5427 so the memory will not be reclaimed during token replacing below. */
5428 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5429 token->u.tree_check_value->value = parser->scope;
5430 token->u.tree_check_value->checks = get_deferred_access_checks ();
5431 token->u.tree_check_value->qualifying_scope =
5432 parser->qualifying_scope;
5433 token->keyword = RID_MAX;
5435 /* Purge all subsequent tokens. */
5436 cp_lexer_purge_tokens_after (parser->lexer, start);
5439 if (start)
5440 pop_to_parent_deferring_access_checks ();
5442 return success ? parser->scope : NULL_TREE;
5445 /* Parse a nested-name-specifier. See
5446 cp_parser_nested_name_specifier_opt for details. This function
5447 behaves identically, except that it will an issue an error if no
5448 nested-name-specifier is present. */
5450 static tree
5451 cp_parser_nested_name_specifier (cp_parser *parser,
5452 bool typename_keyword_p,
5453 bool check_dependency_p,
5454 bool type_p,
5455 bool is_declaration)
5457 tree scope;
5459 /* Look for the nested-name-specifier. */
5460 scope = cp_parser_nested_name_specifier_opt (parser,
5461 typename_keyword_p,
5462 check_dependency_p,
5463 type_p,
5464 is_declaration);
5465 /* If it was not present, issue an error message. */
5466 if (!scope)
5468 cp_parser_error (parser, "expected nested-name-specifier");
5469 parser->scope = NULL_TREE;
5472 return scope;
5475 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5476 this is either a class-name or a namespace-name (which corresponds
5477 to the class-or-namespace-name production in the grammar). For
5478 C++0x, it can also be a type-name that refers to an enumeration
5479 type or a simple-template-id.
5481 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5482 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5483 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5484 TYPE_P is TRUE iff the next name should be taken as a class-name,
5485 even the same name is declared to be another entity in the same
5486 scope.
5488 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5489 specified by the class-or-namespace-name. If neither is found the
5490 ERROR_MARK_NODE is returned. */
5492 static tree
5493 cp_parser_qualifying_entity (cp_parser *parser,
5494 bool typename_keyword_p,
5495 bool template_keyword_p,
5496 bool check_dependency_p,
5497 bool type_p,
5498 bool is_declaration)
5500 tree saved_scope;
5501 tree saved_qualifying_scope;
5502 tree saved_object_scope;
5503 tree scope;
5504 bool only_class_p;
5505 bool successful_parse_p;
5507 /* DR 743: decltype can appear in a nested-name-specifier. */
5508 if (cp_lexer_next_token_is_decltype (parser->lexer))
5510 scope = cp_parser_decltype (parser);
5511 if (TREE_CODE (scope) != ENUMERAL_TYPE
5512 && !MAYBE_CLASS_TYPE_P (scope))
5514 cp_parser_simulate_error (parser);
5515 return error_mark_node;
5517 if (TYPE_NAME (scope))
5518 scope = TYPE_NAME (scope);
5519 return scope;
5522 /* Before we try to parse the class-name, we must save away the
5523 current PARSER->SCOPE since cp_parser_class_name will destroy
5524 it. */
5525 saved_scope = parser->scope;
5526 saved_qualifying_scope = parser->qualifying_scope;
5527 saved_object_scope = parser->object_scope;
5528 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5529 there is no need to look for a namespace-name. */
5530 only_class_p = template_keyword_p
5531 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5532 if (!only_class_p)
5533 cp_parser_parse_tentatively (parser);
5534 scope = cp_parser_class_name (parser,
5535 typename_keyword_p,
5536 template_keyword_p,
5537 type_p ? class_type : none_type,
5538 check_dependency_p,
5539 /*class_head_p=*/false,
5540 is_declaration);
5541 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5542 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5543 if (!only_class_p
5544 && cxx_dialect != cxx98
5545 && !successful_parse_p)
5547 /* Restore the saved scope. */
5548 parser->scope = saved_scope;
5549 parser->qualifying_scope = saved_qualifying_scope;
5550 parser->object_scope = saved_object_scope;
5552 /* Parse tentatively. */
5553 cp_parser_parse_tentatively (parser);
5555 /* Parse a type-name */
5556 scope = cp_parser_type_name (parser);
5558 /* "If the name found does not designate a namespace or a class,
5559 enumeration, or dependent type, the program is ill-formed."
5561 We cover classes and dependent types above and namespaces below,
5562 so this code is only looking for enums. */
5563 if (!scope || TREE_CODE (scope) != TYPE_DECL
5564 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5565 cp_parser_simulate_error (parser);
5567 successful_parse_p = cp_parser_parse_definitely (parser);
5569 /* If that didn't work, try for a namespace-name. */
5570 if (!only_class_p && !successful_parse_p)
5572 /* Restore the saved scope. */
5573 parser->scope = saved_scope;
5574 parser->qualifying_scope = saved_qualifying_scope;
5575 parser->object_scope = saved_object_scope;
5576 /* If we are not looking at an identifier followed by the scope
5577 resolution operator, then this is not part of a
5578 nested-name-specifier. (Note that this function is only used
5579 to parse the components of a nested-name-specifier.) */
5580 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5581 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5582 return error_mark_node;
5583 scope = cp_parser_namespace_name (parser);
5586 return scope;
5589 /* Parse a postfix-expression.
5591 postfix-expression:
5592 primary-expression
5593 postfix-expression [ expression ]
5594 postfix-expression ( expression-list [opt] )
5595 simple-type-specifier ( expression-list [opt] )
5596 typename :: [opt] nested-name-specifier identifier
5597 ( expression-list [opt] )
5598 typename :: [opt] nested-name-specifier template [opt] template-id
5599 ( expression-list [opt] )
5600 postfix-expression . template [opt] id-expression
5601 postfix-expression -> template [opt] id-expression
5602 postfix-expression . pseudo-destructor-name
5603 postfix-expression -> pseudo-destructor-name
5604 postfix-expression ++
5605 postfix-expression --
5606 dynamic_cast < type-id > ( expression )
5607 static_cast < type-id > ( expression )
5608 reinterpret_cast < type-id > ( expression )
5609 const_cast < type-id > ( expression )
5610 typeid ( expression )
5611 typeid ( type-id )
5613 GNU Extension:
5615 postfix-expression:
5616 ( type-id ) { initializer-list , [opt] }
5618 This extension is a GNU version of the C99 compound-literal
5619 construct. (The C99 grammar uses `type-name' instead of `type-id',
5620 but they are essentially the same concept.)
5622 If ADDRESS_P is true, the postfix expression is the operand of the
5623 `&' operator. CAST_P is true if this expression is the target of a
5624 cast.
5626 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5627 class member access expressions [expr.ref].
5629 Returns a representation of the expression. */
5631 static tree
5632 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5633 bool member_access_only_p, bool decltype_p,
5634 cp_id_kind * pidk_return)
5636 cp_token *token;
5637 location_t loc;
5638 enum rid keyword;
5639 cp_id_kind idk = CP_ID_KIND_NONE;
5640 tree postfix_expression = NULL_TREE;
5641 bool is_member_access = false;
5642 int saved_in_statement = -1;
5644 /* Peek at the next token. */
5645 token = cp_lexer_peek_token (parser->lexer);
5646 loc = token->location;
5647 /* Some of the productions are determined by keywords. */
5648 keyword = token->keyword;
5649 switch (keyword)
5651 case RID_DYNCAST:
5652 case RID_STATCAST:
5653 case RID_REINTCAST:
5654 case RID_CONSTCAST:
5656 tree type;
5657 tree expression;
5658 const char *saved_message;
5659 bool saved_in_type_id_in_expr_p;
5661 /* All of these can be handled in the same way from the point
5662 of view of parsing. Begin by consuming the token
5663 identifying the cast. */
5664 cp_lexer_consume_token (parser->lexer);
5666 /* New types cannot be defined in the cast. */
5667 saved_message = parser->type_definition_forbidden_message;
5668 parser->type_definition_forbidden_message
5669 = G_("types may not be defined in casts");
5671 /* Look for the opening `<'. */
5672 cp_parser_require (parser, CPP_LESS, RT_LESS);
5673 /* Parse the type to which we are casting. */
5674 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5675 parser->in_type_id_in_expr_p = true;
5676 type = cp_parser_type_id (parser);
5677 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5678 /* Look for the closing `>'. */
5679 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5680 /* Restore the old message. */
5681 parser->type_definition_forbidden_message = saved_message;
5683 bool saved_greater_than_is_operator_p
5684 = parser->greater_than_is_operator_p;
5685 parser->greater_than_is_operator_p = true;
5687 /* And the expression which is being cast. */
5688 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5689 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5690 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5692 parser->greater_than_is_operator_p
5693 = saved_greater_than_is_operator_p;
5695 /* Only type conversions to integral or enumeration types
5696 can be used in constant-expressions. */
5697 if (!cast_valid_in_integral_constant_expression_p (type)
5698 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5699 return error_mark_node;
5701 switch (keyword)
5703 case RID_DYNCAST:
5704 postfix_expression
5705 = build_dynamic_cast (type, expression, tf_warning_or_error);
5706 break;
5707 case RID_STATCAST:
5708 postfix_expression
5709 = build_static_cast (type, expression, tf_warning_or_error);
5710 break;
5711 case RID_REINTCAST:
5712 postfix_expression
5713 = build_reinterpret_cast (type, expression,
5714 tf_warning_or_error);
5715 break;
5716 case RID_CONSTCAST:
5717 postfix_expression
5718 = build_const_cast (type, expression, tf_warning_or_error);
5719 break;
5720 default:
5721 gcc_unreachable ();
5724 break;
5726 case RID_TYPEID:
5728 tree type;
5729 const char *saved_message;
5730 bool saved_in_type_id_in_expr_p;
5732 /* Consume the `typeid' token. */
5733 cp_lexer_consume_token (parser->lexer);
5734 /* Look for the `(' token. */
5735 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5736 /* Types cannot be defined in a `typeid' expression. */
5737 saved_message = parser->type_definition_forbidden_message;
5738 parser->type_definition_forbidden_message
5739 = G_("types may not be defined in a %<typeid%> expression");
5740 /* We can't be sure yet whether we're looking at a type-id or an
5741 expression. */
5742 cp_parser_parse_tentatively (parser);
5743 /* Try a type-id first. */
5744 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5745 parser->in_type_id_in_expr_p = true;
5746 type = cp_parser_type_id (parser);
5747 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5748 /* Look for the `)' token. Otherwise, we can't be sure that
5749 we're not looking at an expression: consider `typeid (int
5750 (3))', for example. */
5751 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5752 /* If all went well, simply lookup the type-id. */
5753 if (cp_parser_parse_definitely (parser))
5754 postfix_expression = get_typeid (type, tf_warning_or_error);
5755 /* Otherwise, fall back to the expression variant. */
5756 else
5758 tree expression;
5760 /* Look for an expression. */
5761 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5762 /* Compute its typeid. */
5763 postfix_expression = build_typeid (expression, tf_warning_or_error);
5764 /* Look for the `)' token. */
5765 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5767 /* Restore the saved message. */
5768 parser->type_definition_forbidden_message = saved_message;
5769 /* `typeid' may not appear in an integral constant expression. */
5770 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5771 return error_mark_node;
5773 break;
5775 case RID_TYPENAME:
5777 tree type;
5778 /* The syntax permitted here is the same permitted for an
5779 elaborated-type-specifier. */
5780 type = cp_parser_elaborated_type_specifier (parser,
5781 /*is_friend=*/false,
5782 /*is_declaration=*/false);
5783 postfix_expression = cp_parser_functional_cast (parser, type);
5785 break;
5787 case RID_CILK_SPAWN:
5789 cp_lexer_consume_token (parser->lexer);
5790 token = cp_lexer_peek_token (parser->lexer);
5791 if (token->type == CPP_SEMICOLON)
5793 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5794 "an expression");
5795 postfix_expression = error_mark_node;
5796 break;
5798 else if (!current_function_decl)
5800 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5801 "inside a function");
5802 postfix_expression = error_mark_node;
5803 break;
5805 else
5807 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5808 saved_in_statement = parser->in_statement;
5809 parser->in_statement |= IN_CILK_SPAWN;
5811 cfun->calls_cilk_spawn = 1;
5812 postfix_expression =
5813 cp_parser_postfix_expression (parser, false, false,
5814 false, false, &idk);
5815 if (!flag_cilkplus)
5817 error_at (token->location, "-fcilkplus must be enabled to use"
5818 " %<_Cilk_spawn%>");
5819 cfun->calls_cilk_spawn = 0;
5821 else if (saved_in_statement & IN_CILK_SPAWN)
5823 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5824 "are not permitted");
5825 postfix_expression = error_mark_node;
5826 cfun->calls_cilk_spawn = 0;
5828 else
5830 postfix_expression = build_cilk_spawn (token->location,
5831 postfix_expression);
5832 if (postfix_expression != error_mark_node)
5833 SET_EXPR_LOCATION (postfix_expression, input_location);
5834 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5836 break;
5839 case RID_CILK_SYNC:
5840 if (flag_cilkplus)
5842 tree sync_expr = build_cilk_sync ();
5843 SET_EXPR_LOCATION (sync_expr,
5844 cp_lexer_peek_token (parser->lexer)->location);
5845 finish_expr_stmt (sync_expr);
5847 else
5848 error_at (token->location, "-fcilkplus must be enabled to use"
5849 " %<_Cilk_sync%>");
5850 cp_lexer_consume_token (parser->lexer);
5851 break;
5853 case RID_BUILTIN_SHUFFLE:
5855 vec<tree, va_gc> *vec;
5856 unsigned int i;
5857 tree p;
5859 cp_lexer_consume_token (parser->lexer);
5860 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5861 /*cast_p=*/false, /*allow_expansion_p=*/true,
5862 /*non_constant_p=*/NULL);
5863 if (vec == NULL)
5864 return error_mark_node;
5866 FOR_EACH_VEC_ELT (*vec, i, p)
5867 mark_exp_read (p);
5869 if (vec->length () == 2)
5870 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5871 tf_warning_or_error);
5872 else if (vec->length () == 3)
5873 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5874 tf_warning_or_error);
5875 else
5877 error_at (loc, "wrong number of arguments to "
5878 "%<__builtin_shuffle%>");
5879 return error_mark_node;
5881 break;
5884 default:
5886 tree type;
5888 /* If the next thing is a simple-type-specifier, we may be
5889 looking at a functional cast. We could also be looking at
5890 an id-expression. So, we try the functional cast, and if
5891 that doesn't work we fall back to the primary-expression. */
5892 cp_parser_parse_tentatively (parser);
5893 /* Look for the simple-type-specifier. */
5894 type = cp_parser_simple_type_specifier (parser,
5895 /*decl_specs=*/NULL,
5896 CP_PARSER_FLAGS_NONE);
5897 /* Parse the cast itself. */
5898 if (!cp_parser_error_occurred (parser))
5899 postfix_expression
5900 = cp_parser_functional_cast (parser, type);
5901 /* If that worked, we're done. */
5902 if (cp_parser_parse_definitely (parser))
5903 break;
5905 /* If the functional-cast didn't work out, try a
5906 compound-literal. */
5907 if (cp_parser_allow_gnu_extensions_p (parser)
5908 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5910 tree initializer = NULL_TREE;
5911 bool compound_literal_p;
5913 cp_parser_parse_tentatively (parser);
5914 /* Consume the `('. */
5915 cp_lexer_consume_token (parser->lexer);
5917 /* Avoid calling cp_parser_type_id pointlessly, see comment
5918 in cp_parser_cast_expression about c++/29234. */
5919 cp_lexer_save_tokens (parser->lexer);
5921 compound_literal_p
5922 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5923 /*consume_paren=*/true)
5924 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5926 /* Roll back the tokens we skipped. */
5927 cp_lexer_rollback_tokens (parser->lexer);
5929 if (!compound_literal_p)
5930 cp_parser_simulate_error (parser);
5931 else
5933 /* Parse the type. */
5934 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5935 parser->in_type_id_in_expr_p = true;
5936 type = cp_parser_type_id (parser);
5937 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5938 /* Look for the `)'. */
5939 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5942 /* If things aren't going well, there's no need to
5943 keep going. */
5944 if (!cp_parser_error_occurred (parser))
5946 bool non_constant_p;
5947 /* Parse the brace-enclosed initializer list. */
5948 initializer = cp_parser_braced_list (parser,
5949 &non_constant_p);
5951 /* If that worked, we're definitely looking at a
5952 compound-literal expression. */
5953 if (cp_parser_parse_definitely (parser))
5955 /* Warn the user that a compound literal is not
5956 allowed in standard C++. */
5957 pedwarn (input_location, OPT_Wpedantic,
5958 "ISO C++ forbids compound-literals");
5959 /* For simplicity, we disallow compound literals in
5960 constant-expressions. We could
5961 allow compound literals of integer type, whose
5962 initializer was a constant, in constant
5963 expressions. Permitting that usage, as a further
5964 extension, would not change the meaning of any
5965 currently accepted programs. (Of course, as
5966 compound literals are not part of ISO C++, the
5967 standard has nothing to say.) */
5968 if (cp_parser_non_integral_constant_expression (parser,
5969 NIC_NCC))
5971 postfix_expression = error_mark_node;
5972 break;
5974 /* Form the representation of the compound-literal. */
5975 postfix_expression
5976 = finish_compound_literal (type, initializer,
5977 tf_warning_or_error);
5978 break;
5982 /* It must be a primary-expression. */
5983 postfix_expression
5984 = cp_parser_primary_expression (parser, address_p, cast_p,
5985 /*template_arg_p=*/false,
5986 decltype_p,
5987 &idk);
5989 break;
5992 /* Note that we don't need to worry about calling build_cplus_new on a
5993 class-valued CALL_EXPR in decltype when it isn't the end of the
5994 postfix-expression; unary_complex_lvalue will take care of that for
5995 all these cases. */
5997 /* Keep looping until the postfix-expression is complete. */
5998 while (true)
6000 if (idk == CP_ID_KIND_UNQUALIFIED
6001 && identifier_p (postfix_expression)
6002 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6003 /* It is not a Koenig lookup function call. */
6004 postfix_expression
6005 = unqualified_name_lookup_error (postfix_expression);
6007 /* Peek at the next token. */
6008 token = cp_lexer_peek_token (parser->lexer);
6010 switch (token->type)
6012 case CPP_OPEN_SQUARE:
6013 if (cp_next_tokens_can_be_std_attribute_p (parser))
6015 cp_parser_error (parser,
6016 "two consecutive %<[%> shall "
6017 "only introduce an attribute");
6018 return error_mark_node;
6020 postfix_expression
6021 = cp_parser_postfix_open_square_expression (parser,
6022 postfix_expression,
6023 false,
6024 decltype_p);
6025 idk = CP_ID_KIND_NONE;
6026 is_member_access = false;
6027 break;
6029 case CPP_OPEN_PAREN:
6030 /* postfix-expression ( expression-list [opt] ) */
6032 bool koenig_p;
6033 bool is_builtin_constant_p;
6034 bool saved_integral_constant_expression_p = false;
6035 bool saved_non_integral_constant_expression_p = false;
6036 tsubst_flags_t complain = complain_flags (decltype_p);
6037 vec<tree, va_gc> *args;
6039 is_member_access = false;
6041 is_builtin_constant_p
6042 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6043 if (is_builtin_constant_p)
6045 /* The whole point of __builtin_constant_p is to allow
6046 non-constant expressions to appear as arguments. */
6047 saved_integral_constant_expression_p
6048 = parser->integral_constant_expression_p;
6049 saved_non_integral_constant_expression_p
6050 = parser->non_integral_constant_expression_p;
6051 parser->integral_constant_expression_p = false;
6053 args = (cp_parser_parenthesized_expression_list
6054 (parser, non_attr,
6055 /*cast_p=*/false, /*allow_expansion_p=*/true,
6056 /*non_constant_p=*/NULL));
6057 if (is_builtin_constant_p)
6059 parser->integral_constant_expression_p
6060 = saved_integral_constant_expression_p;
6061 parser->non_integral_constant_expression_p
6062 = saved_non_integral_constant_expression_p;
6065 if (args == NULL)
6067 postfix_expression = error_mark_node;
6068 break;
6071 /* Function calls are not permitted in
6072 constant-expressions. */
6073 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6074 && cp_parser_non_integral_constant_expression (parser,
6075 NIC_FUNC_CALL))
6077 postfix_expression = error_mark_node;
6078 release_tree_vector (args);
6079 break;
6082 koenig_p = false;
6083 if (idk == CP_ID_KIND_UNQUALIFIED
6084 || idk == CP_ID_KIND_TEMPLATE_ID)
6086 if (identifier_p (postfix_expression))
6088 if (!args->is_empty ())
6090 koenig_p = true;
6091 if (!any_type_dependent_arguments_p (args))
6092 postfix_expression
6093 = perform_koenig_lookup (postfix_expression, args,
6094 complain);
6096 else
6097 postfix_expression
6098 = unqualified_fn_lookup_error (postfix_expression);
6100 /* We do not perform argument-dependent lookup if
6101 normal lookup finds a non-function, in accordance
6102 with the expected resolution of DR 218. */
6103 else if (!args->is_empty ()
6104 && is_overloaded_fn (postfix_expression))
6106 tree fn = get_first_fn (postfix_expression);
6107 fn = STRIP_TEMPLATE (fn);
6109 /* Do not do argument dependent lookup if regular
6110 lookup finds a member function or a block-scope
6111 function declaration. [basic.lookup.argdep]/3 */
6112 if (!DECL_FUNCTION_MEMBER_P (fn)
6113 && !DECL_LOCAL_FUNCTION_P (fn))
6115 koenig_p = true;
6116 if (!any_type_dependent_arguments_p (args))
6117 postfix_expression
6118 = perform_koenig_lookup (postfix_expression, args,
6119 complain);
6124 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6126 tree instance = TREE_OPERAND (postfix_expression, 0);
6127 tree fn = TREE_OPERAND (postfix_expression, 1);
6129 if (processing_template_decl
6130 && (type_dependent_expression_p (instance)
6131 || (!BASELINK_P (fn)
6132 && TREE_CODE (fn) != FIELD_DECL)
6133 || type_dependent_expression_p (fn)
6134 || any_type_dependent_arguments_p (args)))
6136 postfix_expression
6137 = build_nt_call_vec (postfix_expression, args);
6138 release_tree_vector (args);
6139 break;
6142 if (BASELINK_P (fn))
6144 postfix_expression
6145 = (build_new_method_call
6146 (instance, fn, &args, NULL_TREE,
6147 (idk == CP_ID_KIND_QUALIFIED
6148 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6149 : LOOKUP_NORMAL),
6150 /*fn_p=*/NULL,
6151 complain));
6153 else
6154 postfix_expression
6155 = finish_call_expr (postfix_expression, &args,
6156 /*disallow_virtual=*/false,
6157 /*koenig_p=*/false,
6158 complain);
6160 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6161 || TREE_CODE (postfix_expression) == MEMBER_REF
6162 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6163 postfix_expression = (build_offset_ref_call_from_tree
6164 (postfix_expression, &args,
6165 complain));
6166 else if (idk == CP_ID_KIND_QUALIFIED)
6167 /* A call to a static class member, or a namespace-scope
6168 function. */
6169 postfix_expression
6170 = finish_call_expr (postfix_expression, &args,
6171 /*disallow_virtual=*/true,
6172 koenig_p,
6173 complain);
6174 else
6175 /* All other function calls. */
6176 postfix_expression
6177 = finish_call_expr (postfix_expression, &args,
6178 /*disallow_virtual=*/false,
6179 koenig_p,
6180 complain);
6182 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6183 idk = CP_ID_KIND_NONE;
6185 release_tree_vector (args);
6187 break;
6189 case CPP_DOT:
6190 case CPP_DEREF:
6191 /* postfix-expression . template [opt] id-expression
6192 postfix-expression . pseudo-destructor-name
6193 postfix-expression -> template [opt] id-expression
6194 postfix-expression -> pseudo-destructor-name */
6196 /* Consume the `.' or `->' operator. */
6197 cp_lexer_consume_token (parser->lexer);
6199 postfix_expression
6200 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6201 postfix_expression,
6202 false, &idk, loc);
6204 is_member_access = true;
6205 break;
6207 case CPP_PLUS_PLUS:
6208 /* postfix-expression ++ */
6209 /* Consume the `++' token. */
6210 cp_lexer_consume_token (parser->lexer);
6211 /* Generate a representation for the complete expression. */
6212 postfix_expression
6213 = finish_increment_expr (postfix_expression,
6214 POSTINCREMENT_EXPR);
6215 /* Increments may not appear in constant-expressions. */
6216 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6217 postfix_expression = error_mark_node;
6218 idk = CP_ID_KIND_NONE;
6219 is_member_access = false;
6220 break;
6222 case CPP_MINUS_MINUS:
6223 /* postfix-expression -- */
6224 /* Consume the `--' token. */
6225 cp_lexer_consume_token (parser->lexer);
6226 /* Generate a representation for the complete expression. */
6227 postfix_expression
6228 = finish_increment_expr (postfix_expression,
6229 POSTDECREMENT_EXPR);
6230 /* Decrements may not appear in constant-expressions. */
6231 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6232 postfix_expression = error_mark_node;
6233 idk = CP_ID_KIND_NONE;
6234 is_member_access = false;
6235 break;
6237 default:
6238 if (pidk_return != NULL)
6239 * pidk_return = idk;
6240 if (member_access_only_p)
6241 return is_member_access? postfix_expression : error_mark_node;
6242 else
6243 return postfix_expression;
6247 /* We should never get here. */
6248 gcc_unreachable ();
6249 return error_mark_node;
6252 /* This function parses Cilk Plus array notations. If a normal array expr. is
6253 parsed then the array index is passed back to the caller through *INIT_INDEX
6254 and the function returns a NULL_TREE. If array notation expr. is parsed,
6255 then *INIT_INDEX is ignored by the caller and the function returns
6256 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6257 error_mark_node. */
6259 static tree
6260 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6261 tree array_value)
6263 cp_token *token = NULL;
6264 tree length_index, stride = NULL_TREE, value_tree, array_type;
6265 if (!array_value || array_value == error_mark_node)
6267 cp_parser_skip_to_end_of_statement (parser);
6268 return error_mark_node;
6271 array_type = TREE_TYPE (array_value);
6273 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6274 parser->colon_corrects_to_scope_p = false;
6275 token = cp_lexer_peek_token (parser->lexer);
6277 if (!token)
6279 cp_parser_error (parser, "expected %<:%> or numeral");
6280 return error_mark_node;
6282 else if (token->type == CPP_COLON)
6284 /* Consume the ':'. */
6285 cp_lexer_consume_token (parser->lexer);
6287 /* If we are here, then we have a case like this A[:]. */
6288 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6290 cp_parser_error (parser, "expected %<]%>");
6291 cp_parser_skip_to_end_of_statement (parser);
6292 return error_mark_node;
6294 *init_index = NULL_TREE;
6295 stride = NULL_TREE;
6296 length_index = NULL_TREE;
6298 else
6300 /* If we are here, then there are three valid possibilities:
6301 1. ARRAY [ EXP ]
6302 2. ARRAY [ EXP : EXP ]
6303 3. ARRAY [ EXP : EXP : EXP ] */
6305 *init_index = cp_parser_expression (parser, false, NULL);
6306 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6308 /* This indicates that we have a normal array expression. */
6309 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6310 return NULL_TREE;
6313 /* Consume the ':'. */
6314 cp_lexer_consume_token (parser->lexer);
6315 length_index = cp_parser_expression (parser, false, NULL);
6316 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6318 cp_lexer_consume_token (parser->lexer);
6319 stride = cp_parser_expression (parser, false, NULL);
6322 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6324 if (*init_index == error_mark_node || length_index == error_mark_node
6325 || stride == error_mark_node)
6327 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6328 cp_lexer_consume_token (parser->lexer);
6329 return error_mark_node;
6331 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6333 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6334 length_index, stride, array_type);
6335 return value_tree;
6338 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6339 by cp_parser_builtin_offsetof. We're looking for
6341 postfix-expression [ expression ]
6342 postfix-expression [ braced-init-list ] (C++11)
6344 FOR_OFFSETOF is set if we're being called in that context, which
6345 changes how we deal with integer constant expressions. */
6347 static tree
6348 cp_parser_postfix_open_square_expression (cp_parser *parser,
6349 tree postfix_expression,
6350 bool for_offsetof,
6351 bool decltype_p)
6353 tree index = NULL_TREE;
6354 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6355 bool saved_greater_than_is_operator_p;
6357 /* Consume the `[' token. */
6358 cp_lexer_consume_token (parser->lexer);
6360 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6361 parser->greater_than_is_operator_p = true;
6363 /* Parse the index expression. */
6364 /* ??? For offsetof, there is a question of what to allow here. If
6365 offsetof is not being used in an integral constant expression context,
6366 then we *could* get the right answer by computing the value at runtime.
6367 If we are in an integral constant expression context, then we might
6368 could accept any constant expression; hard to say without analysis.
6369 Rather than open the barn door too wide right away, allow only integer
6370 constant expressions here. */
6371 if (for_offsetof)
6372 index = cp_parser_constant_expression (parser, false, NULL);
6373 else
6375 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6377 bool expr_nonconst_p;
6378 cp_lexer_set_source_position (parser->lexer);
6379 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6380 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6381 if (flag_cilkplus
6382 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6384 error_at (cp_lexer_peek_token (parser->lexer)->location,
6385 "braced list index is not allowed with array "
6386 "notation");
6387 cp_parser_skip_to_end_of_statement (parser);
6388 return error_mark_node;
6391 else if (flag_cilkplus)
6393 /* Here are have these two options:
6394 ARRAY[EXP : EXP] - Array notation expr with default
6395 stride of 1.
6396 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6397 stride. */
6398 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6399 postfix_expression);
6400 if (an_exp)
6401 return an_exp;
6403 else
6404 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6407 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6409 /* Look for the closing `]'. */
6410 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6412 /* Build the ARRAY_REF. */
6413 postfix_expression = grok_array_decl (loc, postfix_expression,
6414 index, decltype_p);
6416 /* When not doing offsetof, array references are not permitted in
6417 constant-expressions. */
6418 if (!for_offsetof
6419 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6420 postfix_expression = error_mark_node;
6422 return postfix_expression;
6425 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6426 by cp_parser_builtin_offsetof. We're looking for
6428 postfix-expression . template [opt] id-expression
6429 postfix-expression . pseudo-destructor-name
6430 postfix-expression -> template [opt] id-expression
6431 postfix-expression -> pseudo-destructor-name
6433 FOR_OFFSETOF is set if we're being called in that context. That sorta
6434 limits what of the above we'll actually accept, but nevermind.
6435 TOKEN_TYPE is the "." or "->" token, which will already have been
6436 removed from the stream. */
6438 static tree
6439 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6440 enum cpp_ttype token_type,
6441 tree postfix_expression,
6442 bool for_offsetof, cp_id_kind *idk,
6443 location_t location)
6445 tree name;
6446 bool dependent_p;
6447 bool pseudo_destructor_p;
6448 tree scope = NULL_TREE;
6450 /* If this is a `->' operator, dereference the pointer. */
6451 if (token_type == CPP_DEREF)
6452 postfix_expression = build_x_arrow (location, postfix_expression,
6453 tf_warning_or_error);
6454 /* Check to see whether or not the expression is type-dependent. */
6455 dependent_p = type_dependent_expression_p (postfix_expression);
6456 /* The identifier following the `->' or `.' is not qualified. */
6457 parser->scope = NULL_TREE;
6458 parser->qualifying_scope = NULL_TREE;
6459 parser->object_scope = NULL_TREE;
6460 *idk = CP_ID_KIND_NONE;
6462 /* Enter the scope corresponding to the type of the object
6463 given by the POSTFIX_EXPRESSION. */
6464 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6466 scope = TREE_TYPE (postfix_expression);
6467 /* According to the standard, no expression should ever have
6468 reference type. Unfortunately, we do not currently match
6469 the standard in this respect in that our internal representation
6470 of an expression may have reference type even when the standard
6471 says it does not. Therefore, we have to manually obtain the
6472 underlying type here. */
6473 scope = non_reference (scope);
6474 /* The type of the POSTFIX_EXPRESSION must be complete. */
6475 if (scope == unknown_type_node)
6477 error_at (location, "%qE does not have class type",
6478 postfix_expression);
6479 scope = NULL_TREE;
6481 /* Unlike the object expression in other contexts, *this is not
6482 required to be of complete type for purposes of class member
6483 access (5.2.5) outside the member function body. */
6484 else if (postfix_expression != current_class_ref
6485 && !(processing_template_decl && scope == current_class_type))
6486 scope = complete_type_or_else (scope, NULL_TREE);
6487 /* Let the name lookup machinery know that we are processing a
6488 class member access expression. */
6489 parser->context->object_type = scope;
6490 /* If something went wrong, we want to be able to discern that case,
6491 as opposed to the case where there was no SCOPE due to the type
6492 of expression being dependent. */
6493 if (!scope)
6494 scope = error_mark_node;
6495 /* If the SCOPE was erroneous, make the various semantic analysis
6496 functions exit quickly -- and without issuing additional error
6497 messages. */
6498 if (scope == error_mark_node)
6499 postfix_expression = error_mark_node;
6502 /* Assume this expression is not a pseudo-destructor access. */
6503 pseudo_destructor_p = false;
6505 /* If the SCOPE is a scalar type, then, if this is a valid program,
6506 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6507 is type dependent, it can be pseudo-destructor-name or something else.
6508 Try to parse it as pseudo-destructor-name first. */
6509 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6511 tree s;
6512 tree type;
6514 cp_parser_parse_tentatively (parser);
6515 /* Parse the pseudo-destructor-name. */
6516 s = NULL_TREE;
6517 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6518 &s, &type);
6519 if (dependent_p
6520 && (cp_parser_error_occurred (parser)
6521 || !SCALAR_TYPE_P (type)))
6522 cp_parser_abort_tentative_parse (parser);
6523 else if (cp_parser_parse_definitely (parser))
6525 pseudo_destructor_p = true;
6526 postfix_expression
6527 = finish_pseudo_destructor_expr (postfix_expression,
6528 s, type, location);
6532 if (!pseudo_destructor_p)
6534 /* If the SCOPE is not a scalar type, we are looking at an
6535 ordinary class member access expression, rather than a
6536 pseudo-destructor-name. */
6537 bool template_p;
6538 cp_token *token = cp_lexer_peek_token (parser->lexer);
6539 /* Parse the id-expression. */
6540 name = (cp_parser_id_expression
6541 (parser,
6542 cp_parser_optional_template_keyword (parser),
6543 /*check_dependency_p=*/true,
6544 &template_p,
6545 /*declarator_p=*/false,
6546 /*optional_p=*/false));
6547 /* In general, build a SCOPE_REF if the member name is qualified.
6548 However, if the name was not dependent and has already been
6549 resolved; there is no need to build the SCOPE_REF. For example;
6551 struct X { void f(); };
6552 template <typename T> void f(T* t) { t->X::f(); }
6554 Even though "t" is dependent, "X::f" is not and has been resolved
6555 to a BASELINK; there is no need to include scope information. */
6557 /* But we do need to remember that there was an explicit scope for
6558 virtual function calls. */
6559 if (parser->scope)
6560 *idk = CP_ID_KIND_QUALIFIED;
6562 /* If the name is a template-id that names a type, we will get a
6563 TYPE_DECL here. That is invalid code. */
6564 if (TREE_CODE (name) == TYPE_DECL)
6566 error_at (token->location, "invalid use of %qD", name);
6567 postfix_expression = error_mark_node;
6569 else
6571 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6573 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6575 error_at (token->location, "%<%D::%D%> is not a class member",
6576 parser->scope, name);
6577 postfix_expression = error_mark_node;
6579 else
6580 name = build_qualified_name (/*type=*/NULL_TREE,
6581 parser->scope,
6582 name,
6583 template_p);
6584 parser->scope = NULL_TREE;
6585 parser->qualifying_scope = NULL_TREE;
6586 parser->object_scope = NULL_TREE;
6588 if (parser->scope && name && BASELINK_P (name))
6589 adjust_result_of_qualified_name_lookup
6590 (name, parser->scope, scope);
6591 postfix_expression
6592 = finish_class_member_access_expr (postfix_expression, name,
6593 template_p,
6594 tf_warning_or_error);
6598 /* We no longer need to look up names in the scope of the object on
6599 the left-hand side of the `.' or `->' operator. */
6600 parser->context->object_type = NULL_TREE;
6602 /* Outside of offsetof, these operators may not appear in
6603 constant-expressions. */
6604 if (!for_offsetof
6605 && (cp_parser_non_integral_constant_expression
6606 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6607 postfix_expression = error_mark_node;
6609 return postfix_expression;
6612 /* Parse a parenthesized expression-list.
6614 expression-list:
6615 assignment-expression
6616 expression-list, assignment-expression
6618 attribute-list:
6619 expression-list
6620 identifier
6621 identifier, expression-list
6623 CAST_P is true if this expression is the target of a cast.
6625 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6626 argument pack.
6628 Returns a vector of trees. Each element is a representation of an
6629 assignment-expression. NULL is returned if the ( and or ) are
6630 missing. An empty, but allocated, vector is returned on no
6631 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6632 if we are parsing an attribute list for an attribute that wants a
6633 plain identifier argument, normal_attr for an attribute that wants
6634 an expression, or non_attr if we aren't parsing an attribute list. If
6635 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6636 not all of the expressions in the list were constant. */
6638 static vec<tree, va_gc> *
6639 cp_parser_parenthesized_expression_list (cp_parser* parser,
6640 int is_attribute_list,
6641 bool cast_p,
6642 bool allow_expansion_p,
6643 bool *non_constant_p)
6645 vec<tree, va_gc> *expression_list;
6646 bool fold_expr_p = is_attribute_list != non_attr;
6647 tree identifier = NULL_TREE;
6648 bool saved_greater_than_is_operator_p;
6650 /* Assume all the expressions will be constant. */
6651 if (non_constant_p)
6652 *non_constant_p = false;
6654 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6655 return NULL;
6657 expression_list = make_tree_vector ();
6659 /* Within a parenthesized expression, a `>' token is always
6660 the greater-than operator. */
6661 saved_greater_than_is_operator_p
6662 = parser->greater_than_is_operator_p;
6663 parser->greater_than_is_operator_p = true;
6665 /* Consume expressions until there are no more. */
6666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6667 while (true)
6669 tree expr;
6671 /* At the beginning of attribute lists, check to see if the
6672 next token is an identifier. */
6673 if (is_attribute_list == id_attr
6674 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6676 cp_token *token;
6678 /* Consume the identifier. */
6679 token = cp_lexer_consume_token (parser->lexer);
6680 /* Save the identifier. */
6681 identifier = token->u.value;
6683 else
6685 bool expr_non_constant_p;
6687 /* Parse the next assignment-expression. */
6688 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6690 /* A braced-init-list. */
6691 cp_lexer_set_source_position (parser->lexer);
6692 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6693 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6694 if (non_constant_p && expr_non_constant_p)
6695 *non_constant_p = true;
6697 else if (non_constant_p)
6699 expr = (cp_parser_constant_expression
6700 (parser, /*allow_non_constant_p=*/true,
6701 &expr_non_constant_p));
6702 if (expr_non_constant_p)
6703 *non_constant_p = true;
6705 else
6706 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6708 if (fold_expr_p)
6709 expr = fold_non_dependent_expr (expr);
6711 /* If we have an ellipsis, then this is an expression
6712 expansion. */
6713 if (allow_expansion_p
6714 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6716 /* Consume the `...'. */
6717 cp_lexer_consume_token (parser->lexer);
6719 /* Build the argument pack. */
6720 expr = make_pack_expansion (expr);
6723 /* Add it to the list. We add error_mark_node
6724 expressions to the list, so that we can still tell if
6725 the correct form for a parenthesized expression-list
6726 is found. That gives better errors. */
6727 vec_safe_push (expression_list, expr);
6729 if (expr == error_mark_node)
6730 goto skip_comma;
6733 /* After the first item, attribute lists look the same as
6734 expression lists. */
6735 is_attribute_list = non_attr;
6737 get_comma:;
6738 /* If the next token isn't a `,', then we are done. */
6739 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6740 break;
6742 /* Otherwise, consume the `,' and keep going. */
6743 cp_lexer_consume_token (parser->lexer);
6746 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6748 int ending;
6750 skip_comma:;
6751 /* We try and resync to an unnested comma, as that will give the
6752 user better diagnostics. */
6753 ending = cp_parser_skip_to_closing_parenthesis (parser,
6754 /*recovering=*/true,
6755 /*or_comma=*/true,
6756 /*consume_paren=*/true);
6757 if (ending < 0)
6758 goto get_comma;
6759 if (!ending)
6761 parser->greater_than_is_operator_p
6762 = saved_greater_than_is_operator_p;
6763 return NULL;
6767 parser->greater_than_is_operator_p
6768 = saved_greater_than_is_operator_p;
6770 if (identifier)
6771 vec_safe_insert (expression_list, 0, identifier);
6773 return expression_list;
6776 /* Parse a pseudo-destructor-name.
6778 pseudo-destructor-name:
6779 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6780 :: [opt] nested-name-specifier template template-id :: ~ type-name
6781 :: [opt] nested-name-specifier [opt] ~ type-name
6783 If either of the first two productions is used, sets *SCOPE to the
6784 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6785 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6786 or ERROR_MARK_NODE if the parse fails. */
6788 static void
6789 cp_parser_pseudo_destructor_name (cp_parser* parser,
6790 tree object,
6791 tree* scope,
6792 tree* type)
6794 bool nested_name_specifier_p;
6796 /* Handle ~auto. */
6797 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6798 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6799 && !type_dependent_expression_p (object))
6801 if (cxx_dialect < cxx1y)
6802 pedwarn (input_location, 0,
6803 "%<~auto%> only available with "
6804 "-std=c++1y or -std=gnu++1y");
6805 cp_lexer_consume_token (parser->lexer);
6806 cp_lexer_consume_token (parser->lexer);
6807 *scope = NULL_TREE;
6808 *type = TREE_TYPE (object);
6809 return;
6812 /* Assume that things will not work out. */
6813 *type = error_mark_node;
6815 /* Look for the optional `::' operator. */
6816 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6817 /* Look for the optional nested-name-specifier. */
6818 nested_name_specifier_p
6819 = (cp_parser_nested_name_specifier_opt (parser,
6820 /*typename_keyword_p=*/false,
6821 /*check_dependency_p=*/true,
6822 /*type_p=*/false,
6823 /*is_declaration=*/false)
6824 != NULL_TREE);
6825 /* Now, if we saw a nested-name-specifier, we might be doing the
6826 second production. */
6827 if (nested_name_specifier_p
6828 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6830 /* Consume the `template' keyword. */
6831 cp_lexer_consume_token (parser->lexer);
6832 /* Parse the template-id. */
6833 cp_parser_template_id (parser,
6834 /*template_keyword_p=*/true,
6835 /*check_dependency_p=*/false,
6836 class_type,
6837 /*is_declaration=*/true);
6838 /* Look for the `::' token. */
6839 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6841 /* If the next token is not a `~', then there might be some
6842 additional qualification. */
6843 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6845 /* At this point, we're looking for "type-name :: ~". The type-name
6846 must not be a class-name, since this is a pseudo-destructor. So,
6847 it must be either an enum-name, or a typedef-name -- both of which
6848 are just identifiers. So, we peek ahead to check that the "::"
6849 and "~" tokens are present; if they are not, then we can avoid
6850 calling type_name. */
6851 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6852 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6853 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6855 cp_parser_error (parser, "non-scalar type");
6856 return;
6859 /* Look for the type-name. */
6860 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6861 if (*scope == error_mark_node)
6862 return;
6864 /* Look for the `::' token. */
6865 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6867 else
6868 *scope = NULL_TREE;
6870 /* Look for the `~'. */
6871 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6873 /* Once we see the ~, this has to be a pseudo-destructor. */
6874 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6875 cp_parser_commit_to_topmost_tentative_parse (parser);
6877 /* Look for the type-name again. We are not responsible for
6878 checking that it matches the first type-name. */
6879 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6882 /* Parse a unary-expression.
6884 unary-expression:
6885 postfix-expression
6886 ++ cast-expression
6887 -- cast-expression
6888 unary-operator cast-expression
6889 sizeof unary-expression
6890 sizeof ( type-id )
6891 alignof ( type-id ) [C++0x]
6892 new-expression
6893 delete-expression
6895 GNU Extensions:
6897 unary-expression:
6898 __extension__ cast-expression
6899 __alignof__ unary-expression
6900 __alignof__ ( type-id )
6901 alignof unary-expression [C++0x]
6902 __real__ cast-expression
6903 __imag__ cast-expression
6904 && identifier
6905 sizeof ( type-id ) { initializer-list , [opt] }
6906 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
6907 __alignof__ ( type-id ) { initializer-list , [opt] }
6909 ADDRESS_P is true iff the unary-expression is appearing as the
6910 operand of the `&' operator. CAST_P is true if this expression is
6911 the target of a cast.
6913 Returns a representation of the expression. */
6915 static tree
6916 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6917 bool decltype_p, cp_id_kind * pidk)
6919 cp_token *token;
6920 enum tree_code unary_operator;
6922 /* Peek at the next token. */
6923 token = cp_lexer_peek_token (parser->lexer);
6924 /* Some keywords give away the kind of expression. */
6925 if (token->type == CPP_KEYWORD)
6927 enum rid keyword = token->keyword;
6929 switch (keyword)
6931 case RID_ALIGNOF:
6932 case RID_SIZEOF:
6934 tree operand, ret;
6935 enum tree_code op;
6936 location_t first_loc;
6938 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6939 /* Consume the token. */
6940 cp_lexer_consume_token (parser->lexer);
6941 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6942 /* Parse the operand. */
6943 operand = cp_parser_sizeof_operand (parser, keyword);
6945 if (TYPE_P (operand))
6946 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6947 else
6949 /* ISO C++ defines alignof only with types, not with
6950 expressions. So pedwarn if alignof is used with a non-
6951 type expression. However, __alignof__ is ok. */
6952 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6953 pedwarn (token->location, OPT_Wpedantic,
6954 "ISO C++ does not allow %<alignof%> "
6955 "with a non-type");
6957 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6959 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6960 SIZEOF_EXPR with the original operand. */
6961 if (op == SIZEOF_EXPR && ret != error_mark_node)
6963 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6965 if (!processing_template_decl && TYPE_P (operand))
6967 ret = build_min (SIZEOF_EXPR, size_type_node,
6968 build1 (NOP_EXPR, operand,
6969 error_mark_node));
6970 SIZEOF_EXPR_TYPE_P (ret) = 1;
6972 else
6973 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6974 TREE_SIDE_EFFECTS (ret) = 0;
6975 TREE_READONLY (ret) = 1;
6977 SET_EXPR_LOCATION (ret, first_loc);
6979 return ret;
6982 case RID_NEW:
6983 return cp_parser_new_expression (parser);
6985 case RID_DELETE:
6986 return cp_parser_delete_expression (parser);
6988 case RID_EXTENSION:
6990 /* The saved value of the PEDANTIC flag. */
6991 int saved_pedantic;
6992 tree expr;
6994 /* Save away the PEDANTIC flag. */
6995 cp_parser_extension_opt (parser, &saved_pedantic);
6996 /* Parse the cast-expression. */
6997 expr = cp_parser_simple_cast_expression (parser);
6998 /* Restore the PEDANTIC flag. */
6999 pedantic = saved_pedantic;
7001 return expr;
7004 case RID_REALPART:
7005 case RID_IMAGPART:
7007 tree expression;
7009 /* Consume the `__real__' or `__imag__' token. */
7010 cp_lexer_consume_token (parser->lexer);
7011 /* Parse the cast-expression. */
7012 expression = cp_parser_simple_cast_expression (parser);
7013 /* Create the complete representation. */
7014 return build_x_unary_op (token->location,
7015 (keyword == RID_REALPART
7016 ? REALPART_EXPR : IMAGPART_EXPR),
7017 expression,
7018 tf_warning_or_error);
7020 break;
7022 case RID_TRANSACTION_ATOMIC:
7023 case RID_TRANSACTION_RELAXED:
7024 return cp_parser_transaction_expression (parser, keyword);
7026 case RID_NOEXCEPT:
7028 tree expr;
7029 const char *saved_message;
7030 bool saved_integral_constant_expression_p;
7031 bool saved_non_integral_constant_expression_p;
7032 bool saved_greater_than_is_operator_p;
7034 cp_lexer_consume_token (parser->lexer);
7035 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7037 saved_message = parser->type_definition_forbidden_message;
7038 parser->type_definition_forbidden_message
7039 = G_("types may not be defined in %<noexcept%> expressions");
7041 saved_integral_constant_expression_p
7042 = parser->integral_constant_expression_p;
7043 saved_non_integral_constant_expression_p
7044 = parser->non_integral_constant_expression_p;
7045 parser->integral_constant_expression_p = false;
7047 saved_greater_than_is_operator_p
7048 = parser->greater_than_is_operator_p;
7049 parser->greater_than_is_operator_p = true;
7051 ++cp_unevaluated_operand;
7052 ++c_inhibit_evaluation_warnings;
7053 expr = cp_parser_expression (parser, false, NULL);
7054 --c_inhibit_evaluation_warnings;
7055 --cp_unevaluated_operand;
7057 parser->greater_than_is_operator_p
7058 = saved_greater_than_is_operator_p;
7060 parser->integral_constant_expression_p
7061 = saved_integral_constant_expression_p;
7062 parser->non_integral_constant_expression_p
7063 = saved_non_integral_constant_expression_p;
7065 parser->type_definition_forbidden_message = saved_message;
7067 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7068 return finish_noexcept_expr (expr, tf_warning_or_error);
7071 default:
7072 break;
7076 /* Look for the `:: new' and `:: delete', which also signal the
7077 beginning of a new-expression, or delete-expression,
7078 respectively. If the next token is `::', then it might be one of
7079 these. */
7080 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7082 enum rid keyword;
7084 /* See if the token after the `::' is one of the keywords in
7085 which we're interested. */
7086 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7087 /* If it's `new', we have a new-expression. */
7088 if (keyword == RID_NEW)
7089 return cp_parser_new_expression (parser);
7090 /* Similarly, for `delete'. */
7091 else if (keyword == RID_DELETE)
7092 return cp_parser_delete_expression (parser);
7095 /* Look for a unary operator. */
7096 unary_operator = cp_parser_unary_operator (token);
7097 /* The `++' and `--' operators can be handled similarly, even though
7098 they are not technically unary-operators in the grammar. */
7099 if (unary_operator == ERROR_MARK)
7101 if (token->type == CPP_PLUS_PLUS)
7102 unary_operator = PREINCREMENT_EXPR;
7103 else if (token->type == CPP_MINUS_MINUS)
7104 unary_operator = PREDECREMENT_EXPR;
7105 /* Handle the GNU address-of-label extension. */
7106 else if (cp_parser_allow_gnu_extensions_p (parser)
7107 && token->type == CPP_AND_AND)
7109 tree identifier;
7110 tree expression;
7111 location_t loc = token->location;
7113 /* Consume the '&&' token. */
7114 cp_lexer_consume_token (parser->lexer);
7115 /* Look for the identifier. */
7116 identifier = cp_parser_identifier (parser);
7117 /* Create an expression representing the address. */
7118 expression = finish_label_address_expr (identifier, loc);
7119 if (cp_parser_non_integral_constant_expression (parser,
7120 NIC_ADDR_LABEL))
7121 expression = error_mark_node;
7122 return expression;
7125 if (unary_operator != ERROR_MARK)
7127 tree cast_expression;
7128 tree expression = error_mark_node;
7129 non_integral_constant non_constant_p = NIC_NONE;
7130 location_t loc = token->location;
7131 tsubst_flags_t complain = complain_flags (decltype_p);
7133 /* Consume the operator token. */
7134 token = cp_lexer_consume_token (parser->lexer);
7135 /* Parse the cast-expression. */
7136 cast_expression
7137 = cp_parser_cast_expression (parser,
7138 unary_operator == ADDR_EXPR,
7139 /*cast_p=*/false,
7140 /*decltype*/false,
7141 pidk);
7142 /* Now, build an appropriate representation. */
7143 switch (unary_operator)
7145 case INDIRECT_REF:
7146 non_constant_p = NIC_STAR;
7147 expression = build_x_indirect_ref (loc, cast_expression,
7148 RO_UNARY_STAR,
7149 complain);
7150 break;
7152 case ADDR_EXPR:
7153 non_constant_p = NIC_ADDR;
7154 /* Fall through. */
7155 case BIT_NOT_EXPR:
7156 expression = build_x_unary_op (loc, unary_operator,
7157 cast_expression,
7158 complain);
7159 break;
7161 case PREINCREMENT_EXPR:
7162 case PREDECREMENT_EXPR:
7163 non_constant_p = unary_operator == PREINCREMENT_EXPR
7164 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7165 /* Fall through. */
7166 case UNARY_PLUS_EXPR:
7167 case NEGATE_EXPR:
7168 case TRUTH_NOT_EXPR:
7169 expression = finish_unary_op_expr (loc, unary_operator,
7170 cast_expression, complain);
7171 break;
7173 default:
7174 gcc_unreachable ();
7177 if (non_constant_p != NIC_NONE
7178 && cp_parser_non_integral_constant_expression (parser,
7179 non_constant_p))
7180 expression = error_mark_node;
7182 return expression;
7185 return cp_parser_postfix_expression (parser, address_p, cast_p,
7186 /*member_access_only_p=*/false,
7187 decltype_p,
7188 pidk);
7191 static inline tree
7192 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7193 cp_id_kind * pidk)
7195 return cp_parser_unary_expression (parser, address_p, cast_p,
7196 /*decltype*/false, pidk);
7199 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7200 unary-operator, the corresponding tree code is returned. */
7202 static enum tree_code
7203 cp_parser_unary_operator (cp_token* token)
7205 switch (token->type)
7207 case CPP_MULT:
7208 return INDIRECT_REF;
7210 case CPP_AND:
7211 return ADDR_EXPR;
7213 case CPP_PLUS:
7214 return UNARY_PLUS_EXPR;
7216 case CPP_MINUS:
7217 return NEGATE_EXPR;
7219 case CPP_NOT:
7220 return TRUTH_NOT_EXPR;
7222 case CPP_COMPL:
7223 return BIT_NOT_EXPR;
7225 default:
7226 return ERROR_MARK;
7230 /* Parse a new-expression.
7232 new-expression:
7233 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7234 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7236 Returns a representation of the expression. */
7238 static tree
7239 cp_parser_new_expression (cp_parser* parser)
7241 bool global_scope_p;
7242 vec<tree, va_gc> *placement;
7243 tree type;
7244 vec<tree, va_gc> *initializer;
7245 tree nelts = NULL_TREE;
7246 tree ret;
7248 /* Look for the optional `::' operator. */
7249 global_scope_p
7250 = (cp_parser_global_scope_opt (parser,
7251 /*current_scope_valid_p=*/false)
7252 != NULL_TREE);
7253 /* Look for the `new' operator. */
7254 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7255 /* There's no easy way to tell a new-placement from the
7256 `( type-id )' construct. */
7257 cp_parser_parse_tentatively (parser);
7258 /* Look for a new-placement. */
7259 placement = cp_parser_new_placement (parser);
7260 /* If that didn't work out, there's no new-placement. */
7261 if (!cp_parser_parse_definitely (parser))
7263 if (placement != NULL)
7264 release_tree_vector (placement);
7265 placement = NULL;
7268 /* If the next token is a `(', then we have a parenthesized
7269 type-id. */
7270 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7272 cp_token *token;
7273 const char *saved_message = parser->type_definition_forbidden_message;
7275 /* Consume the `('. */
7276 cp_lexer_consume_token (parser->lexer);
7278 /* Parse the type-id. */
7279 parser->type_definition_forbidden_message
7280 = G_("types may not be defined in a new-expression");
7281 type = cp_parser_type_id (parser);
7282 parser->type_definition_forbidden_message = saved_message;
7284 /* Look for the closing `)'. */
7285 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7286 token = cp_lexer_peek_token (parser->lexer);
7287 /* There should not be a direct-new-declarator in this production,
7288 but GCC used to allowed this, so we check and emit a sensible error
7289 message for this case. */
7290 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7292 error_at (token->location,
7293 "array bound forbidden after parenthesized type-id");
7294 inform (token->location,
7295 "try removing the parentheses around the type-id");
7296 cp_parser_direct_new_declarator (parser);
7299 /* Otherwise, there must be a new-type-id. */
7300 else
7301 type = cp_parser_new_type_id (parser, &nelts);
7303 /* If the next token is a `(' or '{', then we have a new-initializer. */
7304 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7305 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7306 initializer = cp_parser_new_initializer (parser);
7307 else
7308 initializer = NULL;
7310 /* A new-expression may not appear in an integral constant
7311 expression. */
7312 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7313 ret = error_mark_node;
7314 else
7316 /* Create a representation of the new-expression. */
7317 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7318 tf_warning_or_error);
7321 if (placement != NULL)
7322 release_tree_vector (placement);
7323 if (initializer != NULL)
7324 release_tree_vector (initializer);
7326 return ret;
7329 /* Parse a new-placement.
7331 new-placement:
7332 ( expression-list )
7334 Returns the same representation as for an expression-list. */
7336 static vec<tree, va_gc> *
7337 cp_parser_new_placement (cp_parser* parser)
7339 vec<tree, va_gc> *expression_list;
7341 /* Parse the expression-list. */
7342 expression_list = (cp_parser_parenthesized_expression_list
7343 (parser, non_attr, /*cast_p=*/false,
7344 /*allow_expansion_p=*/true,
7345 /*non_constant_p=*/NULL));
7347 return expression_list;
7350 /* Parse a new-type-id.
7352 new-type-id:
7353 type-specifier-seq new-declarator [opt]
7355 Returns the TYPE allocated. If the new-type-id indicates an array
7356 type, *NELTS is set to the number of elements in the last array
7357 bound; the TYPE will not include the last array bound. */
7359 static tree
7360 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7362 cp_decl_specifier_seq type_specifier_seq;
7363 cp_declarator *new_declarator;
7364 cp_declarator *declarator;
7365 cp_declarator *outer_declarator;
7366 const char *saved_message;
7368 /* The type-specifier sequence must not contain type definitions.
7369 (It cannot contain declarations of new types either, but if they
7370 are not definitions we will catch that because they are not
7371 complete.) */
7372 saved_message = parser->type_definition_forbidden_message;
7373 parser->type_definition_forbidden_message
7374 = G_("types may not be defined in a new-type-id");
7375 /* Parse the type-specifier-seq. */
7376 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7377 /*is_trailing_return=*/false,
7378 &type_specifier_seq);
7379 /* Restore the old message. */
7380 parser->type_definition_forbidden_message = saved_message;
7382 if (type_specifier_seq.type == error_mark_node)
7383 return error_mark_node;
7385 /* Parse the new-declarator. */
7386 new_declarator = cp_parser_new_declarator_opt (parser);
7388 /* Determine the number of elements in the last array dimension, if
7389 any. */
7390 *nelts = NULL_TREE;
7391 /* Skip down to the last array dimension. */
7392 declarator = new_declarator;
7393 outer_declarator = NULL;
7394 while (declarator && (declarator->kind == cdk_pointer
7395 || declarator->kind == cdk_ptrmem))
7397 outer_declarator = declarator;
7398 declarator = declarator->declarator;
7400 while (declarator
7401 && declarator->kind == cdk_array
7402 && declarator->declarator
7403 && declarator->declarator->kind == cdk_array)
7405 outer_declarator = declarator;
7406 declarator = declarator->declarator;
7409 if (declarator && declarator->kind == cdk_array)
7411 *nelts = declarator->u.array.bounds;
7412 if (*nelts == error_mark_node)
7413 *nelts = integer_one_node;
7415 if (outer_declarator)
7416 outer_declarator->declarator = declarator->declarator;
7417 else
7418 new_declarator = NULL;
7421 return groktypename (&type_specifier_seq, new_declarator, false);
7424 /* Parse an (optional) new-declarator.
7426 new-declarator:
7427 ptr-operator new-declarator [opt]
7428 direct-new-declarator
7430 Returns the declarator. */
7432 static cp_declarator *
7433 cp_parser_new_declarator_opt (cp_parser* parser)
7435 enum tree_code code;
7436 tree type, std_attributes = NULL_TREE;
7437 cp_cv_quals cv_quals;
7439 /* We don't know if there's a ptr-operator next, or not. */
7440 cp_parser_parse_tentatively (parser);
7441 /* Look for a ptr-operator. */
7442 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7443 /* If that worked, look for more new-declarators. */
7444 if (cp_parser_parse_definitely (parser))
7446 cp_declarator *declarator;
7448 /* Parse another optional declarator. */
7449 declarator = cp_parser_new_declarator_opt (parser);
7451 declarator = cp_parser_make_indirect_declarator
7452 (code, type, cv_quals, declarator, std_attributes);
7454 return declarator;
7457 /* If the next token is a `[', there is a direct-new-declarator. */
7458 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7459 return cp_parser_direct_new_declarator (parser);
7461 return NULL;
7464 /* Parse a direct-new-declarator.
7466 direct-new-declarator:
7467 [ expression ]
7468 direct-new-declarator [constant-expression]
7472 static cp_declarator *
7473 cp_parser_direct_new_declarator (cp_parser* parser)
7475 cp_declarator *declarator = NULL;
7477 while (true)
7479 tree expression;
7480 cp_token *token;
7482 /* Look for the opening `['. */
7483 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7485 token = cp_lexer_peek_token (parser->lexer);
7486 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7487 /* The standard requires that the expression have integral
7488 type. DR 74 adds enumeration types. We believe that the
7489 real intent is that these expressions be handled like the
7490 expression in a `switch' condition, which also allows
7491 classes with a single conversion to integral or
7492 enumeration type. */
7493 if (!processing_template_decl)
7495 expression
7496 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7497 expression,
7498 /*complain=*/true);
7499 if (!expression)
7501 error_at (token->location,
7502 "expression in new-declarator must have integral "
7503 "or enumeration type");
7504 expression = error_mark_node;
7508 /* Look for the closing `]'. */
7509 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7511 /* Add this bound to the declarator. */
7512 declarator = make_array_declarator (declarator, expression);
7514 /* If the next token is not a `[', then there are no more
7515 bounds. */
7516 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7517 break;
7520 return declarator;
7523 /* Parse a new-initializer.
7525 new-initializer:
7526 ( expression-list [opt] )
7527 braced-init-list
7529 Returns a representation of the expression-list. */
7531 static vec<tree, va_gc> *
7532 cp_parser_new_initializer (cp_parser* parser)
7534 vec<tree, va_gc> *expression_list;
7536 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7538 tree t;
7539 bool expr_non_constant_p;
7540 cp_lexer_set_source_position (parser->lexer);
7541 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7542 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7543 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7544 expression_list = make_tree_vector_single (t);
7546 else
7547 expression_list = (cp_parser_parenthesized_expression_list
7548 (parser, non_attr, /*cast_p=*/false,
7549 /*allow_expansion_p=*/true,
7550 /*non_constant_p=*/NULL));
7552 return expression_list;
7555 /* Parse a delete-expression.
7557 delete-expression:
7558 :: [opt] delete cast-expression
7559 :: [opt] delete [ ] cast-expression
7561 Returns a representation of the expression. */
7563 static tree
7564 cp_parser_delete_expression (cp_parser* parser)
7566 bool global_scope_p;
7567 bool array_p;
7568 tree expression;
7570 /* Look for the optional `::' operator. */
7571 global_scope_p
7572 = (cp_parser_global_scope_opt (parser,
7573 /*current_scope_valid_p=*/false)
7574 != NULL_TREE);
7575 /* Look for the `delete' keyword. */
7576 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7577 /* See if the array syntax is in use. */
7578 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7580 /* Consume the `[' token. */
7581 cp_lexer_consume_token (parser->lexer);
7582 /* Look for the `]' token. */
7583 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7584 /* Remember that this is the `[]' construct. */
7585 array_p = true;
7587 else
7588 array_p = false;
7590 /* Parse the cast-expression. */
7591 expression = cp_parser_simple_cast_expression (parser);
7593 /* A delete-expression may not appear in an integral constant
7594 expression. */
7595 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7596 return error_mark_node;
7598 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7599 tf_warning_or_error);
7602 /* Returns true if TOKEN may start a cast-expression and false
7603 otherwise. */
7605 static bool
7606 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7608 cp_token *token = cp_lexer_peek_token (parser->lexer);
7609 switch (token->type)
7611 case CPP_COMMA:
7612 case CPP_SEMICOLON:
7613 case CPP_QUERY:
7614 case CPP_COLON:
7615 case CPP_CLOSE_SQUARE:
7616 case CPP_CLOSE_PAREN:
7617 case CPP_CLOSE_BRACE:
7618 case CPP_OPEN_BRACE:
7619 case CPP_DOT:
7620 case CPP_DOT_STAR:
7621 case CPP_DEREF:
7622 case CPP_DEREF_STAR:
7623 case CPP_DIV:
7624 case CPP_MOD:
7625 case CPP_LSHIFT:
7626 case CPP_RSHIFT:
7627 case CPP_LESS:
7628 case CPP_GREATER:
7629 case CPP_LESS_EQ:
7630 case CPP_GREATER_EQ:
7631 case CPP_EQ_EQ:
7632 case CPP_NOT_EQ:
7633 case CPP_EQ:
7634 case CPP_MULT_EQ:
7635 case CPP_DIV_EQ:
7636 case CPP_MOD_EQ:
7637 case CPP_PLUS_EQ:
7638 case CPP_MINUS_EQ:
7639 case CPP_RSHIFT_EQ:
7640 case CPP_LSHIFT_EQ:
7641 case CPP_AND_EQ:
7642 case CPP_XOR_EQ:
7643 case CPP_OR_EQ:
7644 case CPP_XOR:
7645 case CPP_OR:
7646 case CPP_OR_OR:
7647 case CPP_EOF:
7648 return false;
7650 case CPP_OPEN_PAREN:
7651 /* In ((type ()) () the last () isn't a valid cast-expression,
7652 so the whole must be parsed as postfix-expression. */
7653 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7654 != CPP_CLOSE_PAREN;
7656 /* '[' may start a primary-expression in obj-c++. */
7657 case CPP_OPEN_SQUARE:
7658 return c_dialect_objc ();
7660 default:
7661 return true;
7665 /* Parse a cast-expression.
7667 cast-expression:
7668 unary-expression
7669 ( type-id ) cast-expression
7671 ADDRESS_P is true iff the unary-expression is appearing as the
7672 operand of the `&' operator. CAST_P is true if this expression is
7673 the target of a cast.
7675 Returns a representation of the expression. */
7677 static tree
7678 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7679 bool decltype_p, cp_id_kind * pidk)
7681 /* If it's a `(', then we might be looking at a cast. */
7682 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7684 tree type = NULL_TREE;
7685 tree expr = NULL_TREE;
7686 bool cast_expression_p;
7687 const char *saved_message;
7689 /* There's no way to know yet whether or not this is a cast.
7690 For example, `(int (3))' is a unary-expression, while `(int)
7691 3' is a cast. So, we resort to parsing tentatively. */
7692 cp_parser_parse_tentatively (parser);
7693 /* Types may not be defined in a cast. */
7694 saved_message = parser->type_definition_forbidden_message;
7695 parser->type_definition_forbidden_message
7696 = G_("types may not be defined in casts");
7697 /* Consume the `('. */
7698 cp_lexer_consume_token (parser->lexer);
7699 /* A very tricky bit is that `(struct S) { 3 }' is a
7700 compound-literal (which we permit in C++ as an extension).
7701 But, that construct is not a cast-expression -- it is a
7702 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7703 is legal; if the compound-literal were a cast-expression,
7704 you'd need an extra set of parentheses.) But, if we parse
7705 the type-id, and it happens to be a class-specifier, then we
7706 will commit to the parse at that point, because we cannot
7707 undo the action that is done when creating a new class. So,
7708 then we cannot back up and do a postfix-expression.
7709 Another tricky case is the following (c++/29234):
7711 struct S { void operator () (); };
7713 void foo ()
7715 ( S()() );
7718 As a type-id we parse the parenthesized S()() as a function
7719 returning a function, groktypename complains and we cannot
7720 back up in this case either.
7722 Therefore, we scan ahead to the closing `)', and check to see
7723 if the tokens after the `)' can start a cast-expression. Otherwise
7724 we are dealing with an unary-expression, a postfix-expression
7725 or something else.
7727 Save tokens so that we can put them back. */
7728 cp_lexer_save_tokens (parser->lexer);
7730 /* We may be looking at a cast-expression. */
7731 cast_expression_p
7732 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7733 /*consume_paren=*/true)
7734 && cp_parser_tokens_start_cast_expression (parser));
7736 /* Roll back the tokens we skipped. */
7737 cp_lexer_rollback_tokens (parser->lexer);
7738 /* If we aren't looking at a cast-expression, simulate an error so
7739 that the call to cp_parser_parse_definitely below will fail. */
7740 if (!cast_expression_p)
7741 cp_parser_simulate_error (parser);
7742 else
7744 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7745 parser->in_type_id_in_expr_p = true;
7746 /* Look for the type-id. */
7747 type = cp_parser_type_id (parser);
7748 /* Look for the closing `)'. */
7749 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7750 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7753 /* Restore the saved message. */
7754 parser->type_definition_forbidden_message = saved_message;
7756 /* At this point this can only be either a cast or a
7757 parenthesized ctor such as `(T ())' that looks like a cast to
7758 function returning T. */
7759 if (!cp_parser_error_occurred (parser))
7761 cp_parser_parse_definitely (parser);
7762 expr = cp_parser_cast_expression (parser,
7763 /*address_p=*/false,
7764 /*cast_p=*/true,
7765 /*decltype_p=*/false,
7766 pidk);
7768 /* Warn about old-style casts, if so requested. */
7769 if (warn_old_style_cast
7770 && !in_system_header_at (input_location)
7771 && !VOID_TYPE_P (type)
7772 && current_lang_name != lang_name_c)
7773 warning (OPT_Wold_style_cast, "use of old-style cast");
7775 /* Only type conversions to integral or enumeration types
7776 can be used in constant-expressions. */
7777 if (!cast_valid_in_integral_constant_expression_p (type)
7778 && cp_parser_non_integral_constant_expression (parser,
7779 NIC_CAST))
7780 return error_mark_node;
7782 /* Perform the cast. */
7783 expr = build_c_cast (input_location, type, expr);
7784 return expr;
7786 else
7787 cp_parser_abort_tentative_parse (parser);
7790 /* If we get here, then it's not a cast, so it must be a
7791 unary-expression. */
7792 return cp_parser_unary_expression (parser, address_p, cast_p,
7793 decltype_p, pidk);
7796 /* Parse a binary expression of the general form:
7798 pm-expression:
7799 cast-expression
7800 pm-expression .* cast-expression
7801 pm-expression ->* cast-expression
7803 multiplicative-expression:
7804 pm-expression
7805 multiplicative-expression * pm-expression
7806 multiplicative-expression / pm-expression
7807 multiplicative-expression % pm-expression
7809 additive-expression:
7810 multiplicative-expression
7811 additive-expression + multiplicative-expression
7812 additive-expression - multiplicative-expression
7814 shift-expression:
7815 additive-expression
7816 shift-expression << additive-expression
7817 shift-expression >> additive-expression
7819 relational-expression:
7820 shift-expression
7821 relational-expression < shift-expression
7822 relational-expression > shift-expression
7823 relational-expression <= shift-expression
7824 relational-expression >= shift-expression
7826 GNU Extension:
7828 relational-expression:
7829 relational-expression <? shift-expression
7830 relational-expression >? shift-expression
7832 equality-expression:
7833 relational-expression
7834 equality-expression == relational-expression
7835 equality-expression != relational-expression
7837 and-expression:
7838 equality-expression
7839 and-expression & equality-expression
7841 exclusive-or-expression:
7842 and-expression
7843 exclusive-or-expression ^ and-expression
7845 inclusive-or-expression:
7846 exclusive-or-expression
7847 inclusive-or-expression | exclusive-or-expression
7849 logical-and-expression:
7850 inclusive-or-expression
7851 logical-and-expression && inclusive-or-expression
7853 logical-or-expression:
7854 logical-and-expression
7855 logical-or-expression || logical-and-expression
7857 All these are implemented with a single function like:
7859 binary-expression:
7860 simple-cast-expression
7861 binary-expression <token> binary-expression
7863 CAST_P is true if this expression is the target of a cast.
7865 The binops_by_token map is used to get the tree codes for each <token> type.
7866 binary-expressions are associated according to a precedence table. */
7868 #define TOKEN_PRECEDENCE(token) \
7869 (((token->type == CPP_GREATER \
7870 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7871 && !parser->greater_than_is_operator_p) \
7872 ? PREC_NOT_OPERATOR \
7873 : binops_by_token[token->type].prec)
7875 static tree
7876 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7877 bool no_toplevel_fold_p,
7878 bool decltype_p,
7879 enum cp_parser_prec prec,
7880 cp_id_kind * pidk)
7882 cp_parser_expression_stack stack;
7883 cp_parser_expression_stack_entry *sp = &stack[0];
7884 cp_parser_expression_stack_entry current;
7885 tree rhs;
7886 cp_token *token;
7887 enum tree_code rhs_type;
7888 enum cp_parser_prec new_prec, lookahead_prec;
7889 tree overload;
7891 /* Parse the first expression. */
7892 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7893 cast_p, decltype_p, pidk);
7894 current.lhs_type = ERROR_MARK;
7895 current.prec = prec;
7897 if (cp_parser_error_occurred (parser))
7898 return error_mark_node;
7900 for (;;)
7902 /* Get an operator token. */
7903 token = cp_lexer_peek_token (parser->lexer);
7905 if (warn_cxx0x_compat
7906 && token->type == CPP_RSHIFT
7907 && !parser->greater_than_is_operator_p)
7909 if (warning_at (token->location, OPT_Wc__0x_compat,
7910 "%<>>%> operator is treated"
7911 " as two right angle brackets in C++11"))
7912 inform (token->location,
7913 "suggest parentheses around %<>>%> expression");
7916 new_prec = TOKEN_PRECEDENCE (token);
7918 /* Popping an entry off the stack means we completed a subexpression:
7919 - either we found a token which is not an operator (`>' where it is not
7920 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7921 will happen repeatedly;
7922 - or, we found an operator which has lower priority. This is the case
7923 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7924 parsing `3 * 4'. */
7925 if (new_prec <= current.prec)
7927 if (sp == stack)
7928 break;
7929 else
7930 goto pop;
7933 get_rhs:
7934 current.tree_type = binops_by_token[token->type].tree_type;
7935 current.loc = token->location;
7937 /* We used the operator token. */
7938 cp_lexer_consume_token (parser->lexer);
7940 /* For "false && x" or "true || x", x will never be executed;
7941 disable warnings while evaluating it. */
7942 if (current.tree_type == TRUTH_ANDIF_EXPR)
7943 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7944 else if (current.tree_type == TRUTH_ORIF_EXPR)
7945 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7947 /* Extract another operand. It may be the RHS of this expression
7948 or the LHS of a new, higher priority expression. */
7949 rhs = cp_parser_simple_cast_expression (parser);
7950 rhs_type = ERROR_MARK;
7952 /* Get another operator token. Look up its precedence to avoid
7953 building a useless (immediately popped) stack entry for common
7954 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7955 token = cp_lexer_peek_token (parser->lexer);
7956 lookahead_prec = TOKEN_PRECEDENCE (token);
7957 if (lookahead_prec > new_prec)
7959 /* ... and prepare to parse the RHS of the new, higher priority
7960 expression. Since precedence levels on the stack are
7961 monotonically increasing, we do not have to care about
7962 stack overflows. */
7963 *sp = current;
7964 ++sp;
7965 current.lhs = rhs;
7966 current.lhs_type = rhs_type;
7967 current.prec = new_prec;
7968 new_prec = lookahead_prec;
7969 goto get_rhs;
7971 pop:
7972 lookahead_prec = new_prec;
7973 /* If the stack is not empty, we have parsed into LHS the right side
7974 (`4' in the example above) of an expression we had suspended.
7975 We can use the information on the stack to recover the LHS (`3')
7976 from the stack together with the tree code (`MULT_EXPR'), and
7977 the precedence of the higher level subexpression
7978 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7979 which will be used to actually build the additive expression. */
7980 rhs = current.lhs;
7981 rhs_type = current.lhs_type;
7982 --sp;
7983 current = *sp;
7986 /* Undo the disabling of warnings done above. */
7987 if (current.tree_type == TRUTH_ANDIF_EXPR)
7988 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7989 else if (current.tree_type == TRUTH_ORIF_EXPR)
7990 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7992 overload = NULL;
7993 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7994 ERROR_MARK for everything that is not a binary expression.
7995 This makes warn_about_parentheses miss some warnings that
7996 involve unary operators. For unary expressions we should
7997 pass the correct tree_code unless the unary expression was
7998 surrounded by parentheses.
8000 if (no_toplevel_fold_p
8001 && lookahead_prec <= current.prec
8002 && sp == stack)
8003 current.lhs = build2 (current.tree_type,
8004 TREE_CODE_CLASS (current.tree_type)
8005 == tcc_comparison
8006 ? boolean_type_node : TREE_TYPE (current.lhs),
8007 current.lhs, rhs);
8008 else
8009 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8010 current.lhs, current.lhs_type,
8011 rhs, rhs_type, &overload,
8012 complain_flags (decltype_p));
8013 current.lhs_type = current.tree_type;
8014 if (EXPR_P (current.lhs))
8015 SET_EXPR_LOCATION (current.lhs, current.loc);
8017 /* If the binary operator required the use of an overloaded operator,
8018 then this expression cannot be an integral constant-expression.
8019 An overloaded operator can be used even if both operands are
8020 otherwise permissible in an integral constant-expression if at
8021 least one of the operands is of enumeration type. */
8023 if (overload
8024 && cp_parser_non_integral_constant_expression (parser,
8025 NIC_OVERLOADED))
8026 return error_mark_node;
8029 return current.lhs;
8032 static tree
8033 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8034 bool no_toplevel_fold_p,
8035 enum cp_parser_prec prec,
8036 cp_id_kind * pidk)
8038 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8039 /*decltype*/false, prec, pidk);
8042 /* Parse the `? expression : assignment-expression' part of a
8043 conditional-expression. The LOGICAL_OR_EXPR is the
8044 logical-or-expression that started the conditional-expression.
8045 Returns a representation of the entire conditional-expression.
8047 This routine is used by cp_parser_assignment_expression.
8049 ? expression : assignment-expression
8051 GNU Extensions:
8053 ? : assignment-expression */
8055 static tree
8056 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8058 tree expr;
8059 tree assignment_expr;
8060 struct cp_token *token;
8061 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8063 /* Consume the `?' token. */
8064 cp_lexer_consume_token (parser->lexer);
8065 token = cp_lexer_peek_token (parser->lexer);
8066 if (cp_parser_allow_gnu_extensions_p (parser)
8067 && token->type == CPP_COLON)
8069 pedwarn (token->location, OPT_Wpedantic,
8070 "ISO C++ does not allow ?: with omitted middle operand");
8071 /* Implicit true clause. */
8072 expr = NULL_TREE;
8073 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8074 warn_for_omitted_condop (token->location, logical_or_expr);
8076 else
8078 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8079 parser->colon_corrects_to_scope_p = false;
8080 /* Parse the expression. */
8081 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8082 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8083 c_inhibit_evaluation_warnings +=
8084 ((logical_or_expr == truthvalue_true_node)
8085 - (logical_or_expr == truthvalue_false_node));
8086 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8089 /* The next token should be a `:'. */
8090 cp_parser_require (parser, CPP_COLON, RT_COLON);
8091 /* Parse the assignment-expression. */
8092 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8093 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8095 /* Build the conditional-expression. */
8096 return build_x_conditional_expr (loc, logical_or_expr,
8097 expr,
8098 assignment_expr,
8099 tf_warning_or_error);
8102 /* Parse an assignment-expression.
8104 assignment-expression:
8105 conditional-expression
8106 logical-or-expression assignment-operator assignment_expression
8107 throw-expression
8109 CAST_P is true if this expression is the target of a cast.
8110 DECLTYPE_P is true if this expression is the operand of decltype.
8112 Returns a representation for the expression. */
8114 static tree
8115 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8116 bool decltype_p, cp_id_kind * pidk)
8118 tree expr;
8120 /* If the next token is the `throw' keyword, then we're looking at
8121 a throw-expression. */
8122 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8123 expr = cp_parser_throw_expression (parser);
8124 /* Otherwise, it must be that we are looking at a
8125 logical-or-expression. */
8126 else
8128 /* Parse the binary expressions (logical-or-expression). */
8129 expr = cp_parser_binary_expression (parser, cast_p, false,
8130 decltype_p,
8131 PREC_NOT_OPERATOR, pidk);
8132 /* If the next token is a `?' then we're actually looking at a
8133 conditional-expression. */
8134 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8135 return cp_parser_question_colon_clause (parser, expr);
8136 else
8138 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8140 /* If it's an assignment-operator, we're using the second
8141 production. */
8142 enum tree_code assignment_operator
8143 = cp_parser_assignment_operator_opt (parser);
8144 if (assignment_operator != ERROR_MARK)
8146 bool non_constant_p;
8147 location_t saved_input_location;
8149 /* Parse the right-hand side of the assignment. */
8150 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8152 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8153 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8155 /* An assignment may not appear in a
8156 constant-expression. */
8157 if (cp_parser_non_integral_constant_expression (parser,
8158 NIC_ASSIGNMENT))
8159 return error_mark_node;
8160 /* Build the assignment expression. Its default
8161 location is the location of the '=' token. */
8162 saved_input_location = input_location;
8163 input_location = loc;
8164 expr = build_x_modify_expr (loc, expr,
8165 assignment_operator,
8166 rhs,
8167 complain_flags (decltype_p));
8168 input_location = saved_input_location;
8173 return expr;
8176 static tree
8177 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8178 cp_id_kind * pidk)
8180 return cp_parser_assignment_expression (parser, cast_p,
8181 /*decltype*/false, pidk);
8184 /* Parse an (optional) assignment-operator.
8186 assignment-operator: one of
8187 = *= /= %= += -= >>= <<= &= ^= |=
8189 GNU Extension:
8191 assignment-operator: one of
8192 <?= >?=
8194 If the next token is an assignment operator, the corresponding tree
8195 code is returned, and the token is consumed. For example, for
8196 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8197 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8198 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8199 operator, ERROR_MARK is returned. */
8201 static enum tree_code
8202 cp_parser_assignment_operator_opt (cp_parser* parser)
8204 enum tree_code op;
8205 cp_token *token;
8207 /* Peek at the next token. */
8208 token = cp_lexer_peek_token (parser->lexer);
8210 switch (token->type)
8212 case CPP_EQ:
8213 op = NOP_EXPR;
8214 break;
8216 case CPP_MULT_EQ:
8217 op = MULT_EXPR;
8218 break;
8220 case CPP_DIV_EQ:
8221 op = TRUNC_DIV_EXPR;
8222 break;
8224 case CPP_MOD_EQ:
8225 op = TRUNC_MOD_EXPR;
8226 break;
8228 case CPP_PLUS_EQ:
8229 op = PLUS_EXPR;
8230 break;
8232 case CPP_MINUS_EQ:
8233 op = MINUS_EXPR;
8234 break;
8236 case CPP_RSHIFT_EQ:
8237 op = RSHIFT_EXPR;
8238 break;
8240 case CPP_LSHIFT_EQ:
8241 op = LSHIFT_EXPR;
8242 break;
8244 case CPP_AND_EQ:
8245 op = BIT_AND_EXPR;
8246 break;
8248 case CPP_XOR_EQ:
8249 op = BIT_XOR_EXPR;
8250 break;
8252 case CPP_OR_EQ:
8253 op = BIT_IOR_EXPR;
8254 break;
8256 default:
8257 /* Nothing else is an assignment operator. */
8258 op = ERROR_MARK;
8261 /* If it was an assignment operator, consume it. */
8262 if (op != ERROR_MARK)
8263 cp_lexer_consume_token (parser->lexer);
8265 return op;
8268 /* Parse an expression.
8270 expression:
8271 assignment-expression
8272 expression , assignment-expression
8274 CAST_P is true if this expression is the target of a cast.
8275 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8276 except possibly parenthesized or on the RHS of a comma (N3276).
8278 Returns a representation of the expression. */
8280 static tree
8281 cp_parser_expression (cp_parser* parser, bool cast_p, bool decltype_p,
8282 cp_id_kind * pidk)
8284 tree expression = NULL_TREE;
8285 location_t loc = UNKNOWN_LOCATION;
8287 while (true)
8289 tree assignment_expression;
8291 /* Parse the next assignment-expression. */
8292 assignment_expression
8293 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8295 /* We don't create a temporary for a call that is the immediate operand
8296 of decltype or on the RHS of a comma. But when we see a comma, we
8297 need to create a temporary for a call on the LHS. */
8298 if (decltype_p && !processing_template_decl
8299 && TREE_CODE (assignment_expression) == CALL_EXPR
8300 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8301 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8302 assignment_expression
8303 = build_cplus_new (TREE_TYPE (assignment_expression),
8304 assignment_expression, tf_warning_or_error);
8306 /* If this is the first assignment-expression, we can just
8307 save it away. */
8308 if (!expression)
8309 expression = assignment_expression;
8310 else
8311 expression = build_x_compound_expr (loc, expression,
8312 assignment_expression,
8313 complain_flags (decltype_p));
8314 /* If the next token is not a comma, then we are done with the
8315 expression. */
8316 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8317 break;
8318 /* Consume the `,'. */
8319 loc = cp_lexer_peek_token (parser->lexer)->location;
8320 cp_lexer_consume_token (parser->lexer);
8321 /* A comma operator cannot appear in a constant-expression. */
8322 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8323 expression = error_mark_node;
8326 return expression;
8329 static inline tree
8330 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
8332 return cp_parser_expression (parser, cast_p, /*decltype*/false, pidk);
8335 /* Parse a constant-expression.
8337 constant-expression:
8338 conditional-expression
8340 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8341 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8342 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8343 is false, NON_CONSTANT_P should be NULL. */
8345 static tree
8346 cp_parser_constant_expression (cp_parser* parser,
8347 bool allow_non_constant_p,
8348 bool *non_constant_p)
8350 bool saved_integral_constant_expression_p;
8351 bool saved_allow_non_integral_constant_expression_p;
8352 bool saved_non_integral_constant_expression_p;
8353 tree expression;
8355 /* It might seem that we could simply parse the
8356 conditional-expression, and then check to see if it were
8357 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8358 one that the compiler can figure out is constant, possibly after
8359 doing some simplifications or optimizations. The standard has a
8360 precise definition of constant-expression, and we must honor
8361 that, even though it is somewhat more restrictive.
8363 For example:
8365 int i[(2, 3)];
8367 is not a legal declaration, because `(2, 3)' is not a
8368 constant-expression. The `,' operator is forbidden in a
8369 constant-expression. However, GCC's constant-folding machinery
8370 will fold this operation to an INTEGER_CST for `3'. */
8372 /* Save the old settings. */
8373 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8374 saved_allow_non_integral_constant_expression_p
8375 = parser->allow_non_integral_constant_expression_p;
8376 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8377 /* We are now parsing a constant-expression. */
8378 parser->integral_constant_expression_p = true;
8379 parser->allow_non_integral_constant_expression_p
8380 = (allow_non_constant_p || cxx_dialect >= cxx11);
8381 parser->non_integral_constant_expression_p = false;
8382 /* Although the grammar says "conditional-expression", we parse an
8383 "assignment-expression", which also permits "throw-expression"
8384 and the use of assignment operators. In the case that
8385 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8386 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8387 actually essential that we look for an assignment-expression.
8388 For example, cp_parser_initializer_clauses uses this function to
8389 determine whether a particular assignment-expression is in fact
8390 constant. */
8391 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8392 /* Restore the old settings. */
8393 parser->integral_constant_expression_p
8394 = saved_integral_constant_expression_p;
8395 parser->allow_non_integral_constant_expression_p
8396 = saved_allow_non_integral_constant_expression_p;
8397 if (cxx_dialect >= cxx11)
8399 /* Require an rvalue constant expression here; that's what our
8400 callers expect. Reference constant expressions are handled
8401 separately in e.g. cp_parser_template_argument. */
8402 bool is_const = potential_rvalue_constant_expression (expression);
8403 parser->non_integral_constant_expression_p = !is_const;
8404 if (!is_const && !allow_non_constant_p)
8405 require_potential_rvalue_constant_expression (expression);
8407 if (allow_non_constant_p)
8408 *non_constant_p = parser->non_integral_constant_expression_p;
8409 parser->non_integral_constant_expression_p
8410 = saved_non_integral_constant_expression_p;
8412 return expression;
8415 /* Parse __builtin_offsetof.
8417 offsetof-expression:
8418 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8420 offsetof-member-designator:
8421 id-expression
8422 | offsetof-member-designator "." id-expression
8423 | offsetof-member-designator "[" expression "]"
8424 | offsetof-member-designator "->" id-expression */
8426 static tree
8427 cp_parser_builtin_offsetof (cp_parser *parser)
8429 int save_ice_p, save_non_ice_p;
8430 tree type, expr;
8431 cp_id_kind dummy;
8432 cp_token *token;
8434 /* We're about to accept non-integral-constant things, but will
8435 definitely yield an integral constant expression. Save and
8436 restore these values around our local parsing. */
8437 save_ice_p = parser->integral_constant_expression_p;
8438 save_non_ice_p = parser->non_integral_constant_expression_p;
8440 /* Consume the "__builtin_offsetof" token. */
8441 cp_lexer_consume_token (parser->lexer);
8442 /* Consume the opening `('. */
8443 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8444 /* Parse the type-id. */
8445 type = cp_parser_type_id (parser);
8446 /* Look for the `,'. */
8447 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8448 token = cp_lexer_peek_token (parser->lexer);
8450 /* Build the (type *)null that begins the traditional offsetof macro. */
8451 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8452 tf_warning_or_error);
8454 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8455 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8456 true, &dummy, token->location);
8457 while (true)
8459 token = cp_lexer_peek_token (parser->lexer);
8460 switch (token->type)
8462 case CPP_OPEN_SQUARE:
8463 /* offsetof-member-designator "[" expression "]" */
8464 expr = cp_parser_postfix_open_square_expression (parser, expr,
8465 true, false);
8466 break;
8468 case CPP_DEREF:
8469 /* offsetof-member-designator "->" identifier */
8470 expr = grok_array_decl (token->location, expr,
8471 integer_zero_node, false);
8472 /* FALLTHRU */
8474 case CPP_DOT:
8475 /* offsetof-member-designator "." identifier */
8476 cp_lexer_consume_token (parser->lexer);
8477 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8478 expr, true, &dummy,
8479 token->location);
8480 break;
8482 case CPP_CLOSE_PAREN:
8483 /* Consume the ")" token. */
8484 cp_lexer_consume_token (parser->lexer);
8485 goto success;
8487 default:
8488 /* Error. We know the following require will fail, but
8489 that gives the proper error message. */
8490 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8491 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8492 expr = error_mark_node;
8493 goto failure;
8497 success:
8498 /* If we're processing a template, we can't finish the semantics yet.
8499 Otherwise we can fold the entire expression now. */
8500 if (processing_template_decl)
8501 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8502 else
8503 expr = finish_offsetof (expr);
8505 failure:
8506 parser->integral_constant_expression_p = save_ice_p;
8507 parser->non_integral_constant_expression_p = save_non_ice_p;
8509 return expr;
8512 /* Parse a trait expression.
8514 Returns a representation of the expression, the underlying type
8515 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8517 static tree
8518 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8520 cp_trait_kind kind;
8521 tree type1, type2 = NULL_TREE;
8522 bool binary = false;
8523 cp_decl_specifier_seq decl_specs;
8525 switch (keyword)
8527 case RID_HAS_NOTHROW_ASSIGN:
8528 kind = CPTK_HAS_NOTHROW_ASSIGN;
8529 break;
8530 case RID_HAS_NOTHROW_CONSTRUCTOR:
8531 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8532 break;
8533 case RID_HAS_NOTHROW_COPY:
8534 kind = CPTK_HAS_NOTHROW_COPY;
8535 break;
8536 case RID_HAS_TRIVIAL_ASSIGN:
8537 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8538 break;
8539 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8540 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8541 break;
8542 case RID_HAS_TRIVIAL_COPY:
8543 kind = CPTK_HAS_TRIVIAL_COPY;
8544 break;
8545 case RID_HAS_TRIVIAL_DESTRUCTOR:
8546 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8547 break;
8548 case RID_HAS_VIRTUAL_DESTRUCTOR:
8549 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8550 break;
8551 case RID_IS_ABSTRACT:
8552 kind = CPTK_IS_ABSTRACT;
8553 break;
8554 case RID_IS_BASE_OF:
8555 kind = CPTK_IS_BASE_OF;
8556 binary = true;
8557 break;
8558 case RID_IS_CLASS:
8559 kind = CPTK_IS_CLASS;
8560 break;
8561 case RID_IS_CONVERTIBLE_TO:
8562 kind = CPTK_IS_CONVERTIBLE_TO;
8563 binary = true;
8564 break;
8565 case RID_IS_EMPTY:
8566 kind = CPTK_IS_EMPTY;
8567 break;
8568 case RID_IS_ENUM:
8569 kind = CPTK_IS_ENUM;
8570 break;
8571 case RID_IS_FINAL:
8572 kind = CPTK_IS_FINAL;
8573 break;
8574 case RID_IS_LITERAL_TYPE:
8575 kind = CPTK_IS_LITERAL_TYPE;
8576 break;
8577 case RID_IS_POD:
8578 kind = CPTK_IS_POD;
8579 break;
8580 case RID_IS_POLYMORPHIC:
8581 kind = CPTK_IS_POLYMORPHIC;
8582 break;
8583 case RID_IS_STD_LAYOUT:
8584 kind = CPTK_IS_STD_LAYOUT;
8585 break;
8586 case RID_IS_TRIVIAL:
8587 kind = CPTK_IS_TRIVIAL;
8588 break;
8589 case RID_IS_UNION:
8590 kind = CPTK_IS_UNION;
8591 break;
8592 case RID_UNDERLYING_TYPE:
8593 kind = CPTK_UNDERLYING_TYPE;
8594 break;
8595 case RID_BASES:
8596 kind = CPTK_BASES;
8597 break;
8598 case RID_DIRECT_BASES:
8599 kind = CPTK_DIRECT_BASES;
8600 break;
8601 default:
8602 gcc_unreachable ();
8605 /* Consume the token. */
8606 cp_lexer_consume_token (parser->lexer);
8608 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8610 type1 = cp_parser_type_id (parser);
8612 if (type1 == error_mark_node)
8613 return error_mark_node;
8615 /* Build a trivial decl-specifier-seq. */
8616 clear_decl_specs (&decl_specs);
8617 decl_specs.type = type1;
8619 /* Call grokdeclarator to figure out what type this is. */
8620 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8621 /*initialized=*/0, /*attrlist=*/NULL);
8623 if (binary)
8625 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8627 type2 = cp_parser_type_id (parser);
8629 if (type2 == error_mark_node)
8630 return error_mark_node;
8632 /* Build a trivial decl-specifier-seq. */
8633 clear_decl_specs (&decl_specs);
8634 decl_specs.type = type2;
8636 /* Call grokdeclarator to figure out what type this is. */
8637 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8638 /*initialized=*/0, /*attrlist=*/NULL);
8641 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8643 /* Complete the trait expression, which may mean either processing
8644 the trait expr now or saving it for template instantiation. */
8645 switch(kind)
8647 case CPTK_UNDERLYING_TYPE:
8648 return finish_underlying_type (type1);
8649 case CPTK_BASES:
8650 return finish_bases (type1, false);
8651 case CPTK_DIRECT_BASES:
8652 return finish_bases (type1, true);
8653 default:
8654 return finish_trait_expr (kind, type1, type2);
8658 /* Lambdas that appear in variable initializer or default argument scope
8659 get that in their mangling, so we need to record it. We might as well
8660 use the count for function and namespace scopes as well. */
8661 static GTY(()) tree lambda_scope;
8662 static GTY(()) int lambda_count;
8663 typedef struct GTY(()) tree_int
8665 tree t;
8666 int i;
8667 } tree_int;
8668 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8670 static void
8671 start_lambda_scope (tree decl)
8673 tree_int ti;
8674 gcc_assert (decl);
8675 /* Once we're inside a function, we ignore other scopes and just push
8676 the function again so that popping works properly. */
8677 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8678 decl = current_function_decl;
8679 ti.t = lambda_scope;
8680 ti.i = lambda_count;
8681 vec_safe_push (lambda_scope_stack, ti);
8682 if (lambda_scope != decl)
8684 /* Don't reset the count if we're still in the same function. */
8685 lambda_scope = decl;
8686 lambda_count = 0;
8690 static void
8691 record_lambda_scope (tree lambda)
8693 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8694 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8697 static void
8698 finish_lambda_scope (void)
8700 tree_int *p = &lambda_scope_stack->last ();
8701 if (lambda_scope != p->t)
8703 lambda_scope = p->t;
8704 lambda_count = p->i;
8706 lambda_scope_stack->pop ();
8709 /* Parse a lambda expression.
8711 lambda-expression:
8712 lambda-introducer lambda-declarator [opt] compound-statement
8714 Returns a representation of the expression. */
8716 static tree
8717 cp_parser_lambda_expression (cp_parser* parser)
8719 tree lambda_expr = build_lambda_expr ();
8720 tree type;
8721 bool ok = true;
8722 cp_token *token = cp_lexer_peek_token (parser->lexer);
8724 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8726 if (cp_unevaluated_operand)
8728 if (!token->error_reported)
8730 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8731 "lambda-expression in unevaluated context");
8732 token->error_reported = true;
8734 ok = false;
8737 /* We may be in the middle of deferred access check. Disable
8738 it now. */
8739 push_deferring_access_checks (dk_no_deferred);
8741 cp_parser_lambda_introducer (parser, lambda_expr);
8743 type = begin_lambda_type (lambda_expr);
8744 if (type == error_mark_node)
8745 return error_mark_node;
8747 record_lambda_scope (lambda_expr);
8749 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8750 determine_visibility (TYPE_NAME (type));
8752 /* Now that we've started the type, add the capture fields for any
8753 explicit captures. */
8754 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8757 /* Inside the class, surrounding template-parameter-lists do not apply. */
8758 unsigned int saved_num_template_parameter_lists
8759 = parser->num_template_parameter_lists;
8760 unsigned char in_statement = parser->in_statement;
8761 bool in_switch_statement_p = parser->in_switch_statement_p;
8762 bool fully_implicit_function_template_p
8763 = parser->fully_implicit_function_template_p;
8764 tree implicit_template_parms = parser->implicit_template_parms;
8765 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8766 bool auto_is_implicit_function_template_parm_p
8767 = parser->auto_is_implicit_function_template_parm_p;
8769 parser->num_template_parameter_lists = 0;
8770 parser->in_statement = 0;
8771 parser->in_switch_statement_p = false;
8772 parser->fully_implicit_function_template_p = false;
8773 parser->implicit_template_parms = 0;
8774 parser->implicit_template_scope = 0;
8775 parser->auto_is_implicit_function_template_parm_p = false;
8777 /* By virtue of defining a local class, a lambda expression has access to
8778 the private variables of enclosing classes. */
8780 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
8782 if (ok)
8783 cp_parser_lambda_body (parser, lambda_expr);
8784 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8786 if (cp_parser_skip_to_closing_brace (parser))
8787 cp_lexer_consume_token (parser->lexer);
8790 /* The capture list was built up in reverse order; fix that now. */
8791 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8792 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8794 if (ok)
8795 maybe_add_lambda_conv_op (type);
8797 type = finish_struct (type, /*attributes=*/NULL_TREE);
8799 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8800 parser->in_statement = in_statement;
8801 parser->in_switch_statement_p = in_switch_statement_p;
8802 parser->fully_implicit_function_template_p
8803 = fully_implicit_function_template_p;
8804 parser->implicit_template_parms = implicit_template_parms;
8805 parser->implicit_template_scope = implicit_template_scope;
8806 parser->auto_is_implicit_function_template_parm_p
8807 = auto_is_implicit_function_template_parm_p;
8810 pop_deferring_access_checks ();
8812 /* This field is only used during parsing of the lambda. */
8813 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8815 /* This lambda shouldn't have any proxies left at this point. */
8816 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8817 /* And now that we're done, push proxies for an enclosing lambda. */
8818 insert_pending_capture_proxies ();
8820 if (ok)
8821 return build_lambda_object (lambda_expr);
8822 else
8823 return error_mark_node;
8826 /* Parse the beginning of a lambda expression.
8828 lambda-introducer:
8829 [ lambda-capture [opt] ]
8831 LAMBDA_EXPR is the current representation of the lambda expression. */
8833 static void
8834 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8836 /* Need commas after the first capture. */
8837 bool first = true;
8839 /* Eat the leading `['. */
8840 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8842 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8843 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8844 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8845 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8846 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8847 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8849 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8851 cp_lexer_consume_token (parser->lexer);
8852 first = false;
8855 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8857 cp_token* capture_token;
8858 tree capture_id;
8859 tree capture_init_expr;
8860 cp_id_kind idk = CP_ID_KIND_NONE;
8861 bool explicit_init_p = false;
8863 enum capture_kind_type
8865 BY_COPY,
8866 BY_REFERENCE
8868 enum capture_kind_type capture_kind = BY_COPY;
8870 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8872 error ("expected end of capture-list");
8873 return;
8876 if (first)
8877 first = false;
8878 else
8879 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8881 /* Possibly capture `this'. */
8882 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8884 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8885 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8886 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8887 "with by-copy capture default");
8888 cp_lexer_consume_token (parser->lexer);
8889 add_capture (lambda_expr,
8890 /*id=*/this_identifier,
8891 /*initializer=*/finish_this_expr(),
8892 /*by_reference_p=*/false,
8893 explicit_init_p);
8894 continue;
8897 /* Remember whether we want to capture as a reference or not. */
8898 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8900 capture_kind = BY_REFERENCE;
8901 cp_lexer_consume_token (parser->lexer);
8904 /* Get the identifier. */
8905 capture_token = cp_lexer_peek_token (parser->lexer);
8906 capture_id = cp_parser_identifier (parser);
8908 if (capture_id == error_mark_node)
8909 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8910 delimiters, but I modified this to stop on unnested ']' as well. It
8911 was already changed to stop on unnested '}', so the
8912 "closing_parenthesis" name is no more misleading with my change. */
8914 cp_parser_skip_to_closing_parenthesis (parser,
8915 /*recovering=*/true,
8916 /*or_comma=*/true,
8917 /*consume_paren=*/true);
8918 break;
8921 /* Find the initializer for this capture. */
8922 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
8923 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
8924 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8926 bool direct, non_constant;
8927 /* An explicit initializer exists. */
8928 if (cxx_dialect < cxx1y)
8929 pedwarn (input_location, 0,
8930 "lambda capture initializers "
8931 "only available with -std=c++1y or -std=gnu++1y");
8932 capture_init_expr = cp_parser_initializer (parser, &direct,
8933 &non_constant);
8934 explicit_init_p = true;
8935 if (capture_init_expr == NULL_TREE)
8937 error ("empty initializer for lambda init-capture");
8938 capture_init_expr = error_mark_node;
8941 else
8943 const char* error_msg;
8945 /* Turn the identifier into an id-expression. */
8946 capture_init_expr
8947 = cp_parser_lookup_name_simple (parser, capture_id,
8948 capture_token->location);
8950 if (capture_init_expr == error_mark_node)
8952 unqualified_name_lookup_error (capture_id);
8953 continue;
8955 else if (DECL_P (capture_init_expr)
8956 && (!VAR_P (capture_init_expr)
8957 && TREE_CODE (capture_init_expr) != PARM_DECL))
8959 error_at (capture_token->location,
8960 "capture of non-variable %qD ",
8961 capture_init_expr);
8962 inform (0, "%q+#D declared here", capture_init_expr);
8963 continue;
8965 if (VAR_P (capture_init_expr)
8966 && decl_storage_duration (capture_init_expr) != dk_auto)
8968 if (pedwarn (capture_token->location, 0, "capture of variable "
8969 "%qD with non-automatic storage duration",
8970 capture_init_expr))
8971 inform (0, "%q+#D declared here", capture_init_expr);
8972 continue;
8975 capture_init_expr
8976 = finish_id_expression
8977 (capture_id,
8978 capture_init_expr,
8979 parser->scope,
8980 &idk,
8981 /*integral_constant_expression_p=*/false,
8982 /*allow_non_integral_constant_expression_p=*/false,
8983 /*non_integral_constant_expression_p=*/NULL,
8984 /*template_p=*/false,
8985 /*done=*/true,
8986 /*address_p=*/false,
8987 /*template_arg_p=*/false,
8988 &error_msg,
8989 capture_token->location);
8991 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8993 cp_lexer_consume_token (parser->lexer);
8994 capture_init_expr = make_pack_expansion (capture_init_expr);
8996 else
8997 check_for_bare_parameter_packs (capture_init_expr);
9000 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9001 && !explicit_init_p)
9003 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9004 && capture_kind == BY_COPY)
9005 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9006 "of %qD redundant with by-copy capture default",
9007 capture_id);
9008 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9009 && capture_kind == BY_REFERENCE)
9010 pedwarn (capture_token->location, 0, "explicit by-reference "
9011 "capture of %qD redundant with by-reference capture "
9012 "default", capture_id);
9015 add_capture (lambda_expr,
9016 capture_id,
9017 capture_init_expr,
9018 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9019 explicit_init_p);
9022 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9025 /* Parse the (optional) middle of a lambda expression.
9027 lambda-declarator:
9028 < template-parameter-list [opt] >
9029 ( parameter-declaration-clause [opt] )
9030 attribute-specifier [opt]
9031 mutable [opt]
9032 exception-specification [opt]
9033 lambda-return-type-clause [opt]
9035 LAMBDA_EXPR is the current representation of the lambda expression. */
9037 static bool
9038 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9040 /* 5.1.1.4 of the standard says:
9041 If a lambda-expression does not include a lambda-declarator, it is as if
9042 the lambda-declarator were ().
9043 This means an empty parameter list, no attributes, and no exception
9044 specification. */
9045 tree param_list = void_list_node;
9046 tree attributes = NULL_TREE;
9047 tree exception_spec = NULL_TREE;
9048 tree template_param_list = NULL_TREE;
9050 /* The template-parameter-list is optional, but must begin with
9051 an opening angle if present. */
9052 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9054 if (cxx_dialect < cxx1y)
9055 pedwarn (parser->lexer->next_token->location, 0,
9056 "lambda templates are only available with "
9057 "-std=c++1y or -std=gnu++1y");
9059 cp_lexer_consume_token (parser->lexer);
9061 template_param_list = cp_parser_template_parameter_list (parser);
9063 cp_parser_skip_to_end_of_template_parameter_list (parser);
9065 /* We just processed one more parameter list. */
9066 ++parser->num_template_parameter_lists;
9069 /* The parameter-declaration-clause is optional (unless
9070 template-parameter-list was given), but must begin with an
9071 opening parenthesis if present. */
9072 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9074 cp_lexer_consume_token (parser->lexer);
9076 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9078 /* Parse parameters. */
9079 param_list = cp_parser_parameter_declaration_clause (parser);
9081 /* Default arguments shall not be specified in the
9082 parameter-declaration-clause of a lambda-declarator. */
9083 for (tree t = param_list; t; t = TREE_CHAIN (t))
9084 if (TREE_PURPOSE (t))
9085 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9086 "default argument specified for lambda parameter");
9088 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9090 attributes = cp_parser_attributes_opt (parser);
9092 /* Parse optional `mutable' keyword. */
9093 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9095 cp_lexer_consume_token (parser->lexer);
9096 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9099 /* Parse optional exception specification. */
9100 exception_spec = cp_parser_exception_specification_opt (parser);
9102 /* Parse optional trailing return type. */
9103 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9105 cp_lexer_consume_token (parser->lexer);
9106 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9107 = cp_parser_trailing_type_id (parser);
9110 /* The function parameters must be in scope all the way until after the
9111 trailing-return-type in case of decltype. */
9112 pop_bindings_and_leave_scope ();
9114 else if (template_param_list != NULL_TREE) // generate diagnostic
9115 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9117 /* Create the function call operator.
9119 Messing with declarators like this is no uglier than building up the
9120 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9121 other code. */
9123 cp_decl_specifier_seq return_type_specs;
9124 cp_declarator* declarator;
9125 tree fco;
9126 int quals;
9127 void *p;
9129 clear_decl_specs (&return_type_specs);
9130 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9131 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9132 else
9133 /* Maybe we will deduce the return type later. */
9134 return_type_specs.type = make_auto ();
9136 p = obstack_alloc (&declarator_obstack, 0);
9138 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9139 sfk_none);
9141 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9142 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9143 declarator = make_call_declarator (declarator, param_list, quals,
9144 VIRT_SPEC_UNSPECIFIED,
9145 REF_QUAL_NONE,
9146 exception_spec,
9147 /*late_return_type=*/NULL_TREE);
9148 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9150 fco = grokmethod (&return_type_specs,
9151 declarator,
9152 attributes);
9153 if (fco != error_mark_node)
9155 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9156 DECL_ARTIFICIAL (fco) = 1;
9157 /* Give the object parameter a different name. */
9158 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9160 if (template_param_list)
9162 fco = finish_member_template_decl (fco);
9163 finish_template_decl (template_param_list);
9164 --parser->num_template_parameter_lists;
9166 else if (parser->fully_implicit_function_template_p)
9167 fco = finish_fully_implicit_template (parser, fco);
9169 finish_member_declaration (fco);
9171 obstack_free (&declarator_obstack, p);
9173 return (fco != error_mark_node);
9177 /* Parse the body of a lambda expression, which is simply
9179 compound-statement
9181 but which requires special handling.
9182 LAMBDA_EXPR is the current representation of the lambda expression. */
9184 static void
9185 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9187 bool nested = (current_function_decl != NULL_TREE);
9188 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9189 if (nested)
9190 push_function_context ();
9191 else
9192 /* Still increment function_depth so that we don't GC in the
9193 middle of an expression. */
9194 ++function_depth;
9195 /* Clear this in case we're in the middle of a default argument. */
9196 parser->local_variables_forbidden_p = false;
9198 /* Finish the function call operator
9199 - class_specifier
9200 + late_parsing_for_member
9201 + function_definition_after_declarator
9202 + ctor_initializer_opt_and_function_body */
9204 tree fco = lambda_function (lambda_expr);
9205 tree body;
9206 bool done = false;
9207 tree compound_stmt;
9208 tree cap;
9210 /* Let the front end know that we are going to be defining this
9211 function. */
9212 start_preparsed_function (fco,
9213 NULL_TREE,
9214 SF_PRE_PARSED | SF_INCLASS_INLINE);
9216 start_lambda_scope (fco);
9217 body = begin_function_body ();
9219 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9220 goto out;
9222 /* Push the proxies for any explicit captures. */
9223 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9224 cap = TREE_CHAIN (cap))
9225 build_capture_proxy (TREE_PURPOSE (cap));
9227 compound_stmt = begin_compound_stmt (0);
9229 /* 5.1.1.4 of the standard says:
9230 If a lambda-expression does not include a trailing-return-type, it
9231 is as if the trailing-return-type denotes the following type:
9232 * if the compound-statement is of the form
9233 { return attribute-specifier [opt] expression ; }
9234 the type of the returned expression after lvalue-to-rvalue
9235 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9236 (_conv.array_ 4.2), and function-to-pointer conversion
9237 (_conv.func_ 4.3);
9238 * otherwise, void. */
9240 /* In a lambda that has neither a lambda-return-type-clause
9241 nor a deducible form, errors should be reported for return statements
9242 in the body. Since we used void as the placeholder return type, parsing
9243 the body as usual will give such desired behavior. */
9244 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9245 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9246 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9248 tree expr = NULL_TREE;
9249 cp_id_kind idk = CP_ID_KIND_NONE;
9251 /* Parse tentatively in case there's more after the initial return
9252 statement. */
9253 cp_parser_parse_tentatively (parser);
9255 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9257 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
9259 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9260 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9262 if (cp_parser_parse_definitely (parser))
9264 if (!processing_template_decl)
9265 apply_deduced_return_type (fco, lambda_return_type (expr));
9267 /* Will get error here if type not deduced yet. */
9268 finish_return_stmt (expr);
9270 done = true;
9274 if (!done)
9276 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9277 cp_parser_label_declaration (parser);
9278 cp_parser_statement_seq_opt (parser, NULL_TREE);
9279 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9282 finish_compound_stmt (compound_stmt);
9284 out:
9285 finish_function_body (body);
9286 finish_lambda_scope ();
9288 /* Finish the function and generate code for it if necessary. */
9289 tree fn = finish_function (/*inline*/2);
9291 /* Only expand if the call op is not a template. */
9292 if (!DECL_TEMPLATE_INFO (fco))
9293 expand_or_defer_fn (fn);
9296 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9297 if (nested)
9298 pop_function_context();
9299 else
9300 --function_depth;
9303 /* Statements [gram.stmt.stmt] */
9305 /* Parse a statement.
9307 statement:
9308 labeled-statement
9309 expression-statement
9310 compound-statement
9311 selection-statement
9312 iteration-statement
9313 jump-statement
9314 declaration-statement
9315 try-block
9317 C++11:
9319 statement:
9320 labeled-statement
9321 attribute-specifier-seq (opt) expression-statement
9322 attribute-specifier-seq (opt) compound-statement
9323 attribute-specifier-seq (opt) selection-statement
9324 attribute-specifier-seq (opt) iteration-statement
9325 attribute-specifier-seq (opt) jump-statement
9326 declaration-statement
9327 attribute-specifier-seq (opt) try-block
9329 TM Extension:
9331 statement:
9332 atomic-statement
9334 IN_COMPOUND is true when the statement is nested inside a
9335 cp_parser_compound_statement; this matters for certain pragmas.
9337 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9338 is a (possibly labeled) if statement which is not enclosed in braces
9339 and has an else clause. This is used to implement -Wparentheses. */
9341 static void
9342 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9343 bool in_compound, bool *if_p)
9345 tree statement, std_attrs = NULL_TREE;
9346 cp_token *token;
9347 location_t statement_location, attrs_location;
9349 restart:
9350 if (if_p != NULL)
9351 *if_p = false;
9352 /* There is no statement yet. */
9353 statement = NULL_TREE;
9355 cp_lexer_save_tokens (parser->lexer);
9356 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9357 if (c_dialect_objc ())
9358 /* In obj-c++, seeing '[[' might be the either the beginning of
9359 c++11 attributes, or a nested objc-message-expression. So
9360 let's parse the c++11 attributes tentatively. */
9361 cp_parser_parse_tentatively (parser);
9362 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9363 if (c_dialect_objc ())
9365 if (!cp_parser_parse_definitely (parser))
9366 std_attrs = NULL_TREE;
9369 /* Peek at the next token. */
9370 token = cp_lexer_peek_token (parser->lexer);
9371 /* Remember the location of the first token in the statement. */
9372 statement_location = token->location;
9373 /* If this is a keyword, then that will often determine what kind of
9374 statement we have. */
9375 if (token->type == CPP_KEYWORD)
9377 enum rid keyword = token->keyword;
9379 switch (keyword)
9381 case RID_CASE:
9382 case RID_DEFAULT:
9383 /* Looks like a labeled-statement with a case label.
9384 Parse the label, and then use tail recursion to parse
9385 the statement. */
9386 cp_parser_label_for_labeled_statement (parser, std_attrs);
9387 goto restart;
9389 case RID_IF:
9390 case RID_SWITCH:
9391 statement = cp_parser_selection_statement (parser, if_p);
9392 break;
9394 case RID_WHILE:
9395 case RID_DO:
9396 case RID_FOR:
9397 statement = cp_parser_iteration_statement (parser, false);
9398 break;
9400 case RID_BREAK:
9401 case RID_CONTINUE:
9402 case RID_RETURN:
9403 case RID_GOTO:
9404 statement = cp_parser_jump_statement (parser);
9405 break;
9407 /* Objective-C++ exception-handling constructs. */
9408 case RID_AT_TRY:
9409 case RID_AT_CATCH:
9410 case RID_AT_FINALLY:
9411 case RID_AT_SYNCHRONIZED:
9412 case RID_AT_THROW:
9413 statement = cp_parser_objc_statement (parser);
9414 break;
9416 case RID_TRY:
9417 statement = cp_parser_try_block (parser);
9418 break;
9420 case RID_NAMESPACE:
9421 /* This must be a namespace alias definition. */
9422 cp_parser_declaration_statement (parser);
9423 return;
9425 case RID_TRANSACTION_ATOMIC:
9426 case RID_TRANSACTION_RELAXED:
9427 statement = cp_parser_transaction (parser, keyword);
9428 break;
9429 case RID_TRANSACTION_CANCEL:
9430 statement = cp_parser_transaction_cancel (parser);
9431 break;
9433 default:
9434 /* It might be a keyword like `int' that can start a
9435 declaration-statement. */
9436 break;
9439 else if (token->type == CPP_NAME)
9441 /* If the next token is a `:', then we are looking at a
9442 labeled-statement. */
9443 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9444 if (token->type == CPP_COLON)
9446 /* Looks like a labeled-statement with an ordinary label.
9447 Parse the label, and then use tail recursion to parse
9448 the statement. */
9450 cp_parser_label_for_labeled_statement (parser, std_attrs);
9451 goto restart;
9454 /* Anything that starts with a `{' must be a compound-statement. */
9455 else if (token->type == CPP_OPEN_BRACE)
9456 statement = cp_parser_compound_statement (parser, NULL, false, false);
9457 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9458 a statement all its own. */
9459 else if (token->type == CPP_PRAGMA)
9461 /* Only certain OpenMP pragmas are attached to statements, and thus
9462 are considered statements themselves. All others are not. In
9463 the context of a compound, accept the pragma as a "statement" and
9464 return so that we can check for a close brace. Otherwise we
9465 require a real statement and must go back and read one. */
9466 if (in_compound)
9467 cp_parser_pragma (parser, pragma_compound);
9468 else if (!cp_parser_pragma (parser, pragma_stmt))
9469 goto restart;
9470 return;
9472 else if (token->type == CPP_EOF)
9474 cp_parser_error (parser, "expected statement");
9475 return;
9478 /* Everything else must be a declaration-statement or an
9479 expression-statement. Try for the declaration-statement
9480 first, unless we are looking at a `;', in which case we know that
9481 we have an expression-statement. */
9482 if (!statement)
9484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9486 if (std_attrs != NULL_TREE)
9488 /* Attributes should be parsed as part of the the
9489 declaration, so let's un-parse them. */
9490 cp_lexer_rollback_tokens (parser->lexer);
9491 std_attrs = NULL_TREE;
9494 cp_parser_parse_tentatively (parser);
9495 /* Try to parse the declaration-statement. */
9496 cp_parser_declaration_statement (parser);
9497 /* If that worked, we're done. */
9498 if (cp_parser_parse_definitely (parser))
9499 return;
9501 /* Look for an expression-statement instead. */
9502 statement = cp_parser_expression_statement (parser, in_statement_expr);
9505 /* Set the line number for the statement. */
9506 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9507 SET_EXPR_LOCATION (statement, statement_location);
9509 /* Note that for now, we don't do anything with c++11 statements
9510 parsed at this level. */
9511 if (std_attrs != NULL_TREE)
9512 warning_at (attrs_location,
9513 OPT_Wattributes,
9514 "attributes at the beginning of statement are ignored");
9517 /* Parse the label for a labeled-statement, i.e.
9519 identifier :
9520 case constant-expression :
9521 default :
9523 GNU Extension:
9524 case constant-expression ... constant-expression : statement
9526 When a label is parsed without errors, the label is added to the
9527 parse tree by the finish_* functions, so this function doesn't
9528 have to return the label. */
9530 static void
9531 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9533 cp_token *token;
9534 tree label = NULL_TREE;
9535 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9537 /* The next token should be an identifier. */
9538 token = cp_lexer_peek_token (parser->lexer);
9539 if (token->type != CPP_NAME
9540 && token->type != CPP_KEYWORD)
9542 cp_parser_error (parser, "expected labeled-statement");
9543 return;
9546 parser->colon_corrects_to_scope_p = false;
9547 switch (token->keyword)
9549 case RID_CASE:
9551 tree expr, expr_hi;
9552 cp_token *ellipsis;
9554 /* Consume the `case' token. */
9555 cp_lexer_consume_token (parser->lexer);
9556 /* Parse the constant-expression. */
9557 expr = cp_parser_constant_expression (parser,
9558 /*allow_non_constant_p=*/false,
9559 NULL);
9561 ellipsis = cp_lexer_peek_token (parser->lexer);
9562 if (ellipsis->type == CPP_ELLIPSIS)
9564 /* Consume the `...' token. */
9565 cp_lexer_consume_token (parser->lexer);
9566 expr_hi =
9567 cp_parser_constant_expression (parser,
9568 /*allow_non_constant_p=*/false,
9569 NULL);
9570 /* We don't need to emit warnings here, as the common code
9571 will do this for us. */
9573 else
9574 expr_hi = NULL_TREE;
9576 if (parser->in_switch_statement_p)
9577 finish_case_label (token->location, expr, expr_hi);
9578 else
9579 error_at (token->location,
9580 "case label %qE not within a switch statement",
9581 expr);
9583 break;
9585 case RID_DEFAULT:
9586 /* Consume the `default' token. */
9587 cp_lexer_consume_token (parser->lexer);
9589 if (parser->in_switch_statement_p)
9590 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9591 else
9592 error_at (token->location, "case label not within a switch statement");
9593 break;
9595 default:
9596 /* Anything else must be an ordinary label. */
9597 label = finish_label_stmt (cp_parser_identifier (parser));
9598 break;
9601 /* Require the `:' token. */
9602 cp_parser_require (parser, CPP_COLON, RT_COLON);
9604 /* An ordinary label may optionally be followed by attributes.
9605 However, this is only permitted if the attributes are then
9606 followed by a semicolon. This is because, for backward
9607 compatibility, when parsing
9608 lab: __attribute__ ((unused)) int i;
9609 we want the attribute to attach to "i", not "lab". */
9610 if (label != NULL_TREE
9611 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9613 tree attrs;
9614 cp_parser_parse_tentatively (parser);
9615 attrs = cp_parser_gnu_attributes_opt (parser);
9616 if (attrs == NULL_TREE
9617 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9618 cp_parser_abort_tentative_parse (parser);
9619 else if (!cp_parser_parse_definitely (parser))
9621 else
9622 attributes = chainon (attributes, attrs);
9625 if (attributes != NULL_TREE)
9626 cplus_decl_attributes (&label, attributes, 0);
9628 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9631 /* Parse an expression-statement.
9633 expression-statement:
9634 expression [opt] ;
9636 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9637 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9638 indicates whether this expression-statement is part of an
9639 expression statement. */
9641 static tree
9642 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9644 tree statement = NULL_TREE;
9645 cp_token *token = cp_lexer_peek_token (parser->lexer);
9647 /* If the next token is a ';', then there is no expression
9648 statement. */
9649 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9651 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9652 if (statement == error_mark_node
9653 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9655 cp_parser_skip_to_end_of_block_or_statement (parser);
9656 return error_mark_node;
9660 /* Give a helpful message for "A<T>::type t;" and the like. */
9661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9662 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9664 if (TREE_CODE (statement) == SCOPE_REF)
9665 error_at (token->location, "need %<typename%> before %qE because "
9666 "%qT is a dependent scope",
9667 statement, TREE_OPERAND (statement, 0));
9668 else if (is_overloaded_fn (statement)
9669 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9671 /* A::A a; */
9672 tree fn = get_first_fn (statement);
9673 error_at (token->location,
9674 "%<%T::%D%> names the constructor, not the type",
9675 DECL_CONTEXT (fn), DECL_NAME (fn));
9679 /* Consume the final `;'. */
9680 cp_parser_consume_semicolon_at_end_of_statement (parser);
9682 if (in_statement_expr
9683 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9684 /* This is the final expression statement of a statement
9685 expression. */
9686 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9687 else if (statement)
9688 statement = finish_expr_stmt (statement);
9690 return statement;
9693 /* Parse a compound-statement.
9695 compound-statement:
9696 { statement-seq [opt] }
9698 GNU extension:
9700 compound-statement:
9701 { label-declaration-seq [opt] statement-seq [opt] }
9703 label-declaration-seq:
9704 label-declaration
9705 label-declaration-seq label-declaration
9707 Returns a tree representing the statement. */
9709 static tree
9710 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9711 bool in_try, bool function_body)
9713 tree compound_stmt;
9715 /* Consume the `{'. */
9716 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9717 return error_mark_node;
9718 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9719 && !function_body)
9720 pedwarn (input_location, OPT_Wpedantic,
9721 "compound-statement in constexpr function");
9722 /* Begin the compound-statement. */
9723 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9724 /* If the next keyword is `__label__' we have a label declaration. */
9725 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9726 cp_parser_label_declaration (parser);
9727 /* Parse an (optional) statement-seq. */
9728 cp_parser_statement_seq_opt (parser, in_statement_expr);
9729 /* Finish the compound-statement. */
9730 finish_compound_stmt (compound_stmt);
9731 /* Consume the `}'. */
9732 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9734 return compound_stmt;
9737 /* Parse an (optional) statement-seq.
9739 statement-seq:
9740 statement
9741 statement-seq [opt] statement */
9743 static void
9744 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9746 /* Scan statements until there aren't any more. */
9747 while (true)
9749 cp_token *token = cp_lexer_peek_token (parser->lexer);
9751 /* If we are looking at a `}', then we have run out of
9752 statements; the same is true if we have reached the end
9753 of file, or have stumbled upon a stray '@end'. */
9754 if (token->type == CPP_CLOSE_BRACE
9755 || token->type == CPP_EOF
9756 || token->type == CPP_PRAGMA_EOL
9757 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9758 break;
9760 /* If we are in a compound statement and find 'else' then
9761 something went wrong. */
9762 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9764 if (parser->in_statement & IN_IF_STMT)
9765 break;
9766 else
9768 token = cp_lexer_consume_token (parser->lexer);
9769 error_at (token->location, "%<else%> without a previous %<if%>");
9773 /* Parse the statement. */
9774 cp_parser_statement (parser, in_statement_expr, true, NULL);
9778 /* Parse a selection-statement.
9780 selection-statement:
9781 if ( condition ) statement
9782 if ( condition ) statement else statement
9783 switch ( condition ) statement
9785 Returns the new IF_STMT or SWITCH_STMT.
9787 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9788 is a (possibly labeled) if statement which is not enclosed in
9789 braces and has an else clause. This is used to implement
9790 -Wparentheses. */
9792 static tree
9793 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9795 cp_token *token;
9796 enum rid keyword;
9798 if (if_p != NULL)
9799 *if_p = false;
9801 /* Peek at the next token. */
9802 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9804 /* See what kind of keyword it is. */
9805 keyword = token->keyword;
9806 switch (keyword)
9808 case RID_IF:
9809 case RID_SWITCH:
9811 tree statement;
9812 tree condition;
9814 /* Look for the `('. */
9815 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9817 cp_parser_skip_to_end_of_statement (parser);
9818 return error_mark_node;
9821 /* Begin the selection-statement. */
9822 if (keyword == RID_IF)
9823 statement = begin_if_stmt ();
9824 else
9825 statement = begin_switch_stmt ();
9827 /* Parse the condition. */
9828 condition = cp_parser_condition (parser);
9829 /* Look for the `)'. */
9830 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9831 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9832 /*consume_paren=*/true);
9834 if (keyword == RID_IF)
9836 bool nested_if;
9837 unsigned char in_statement;
9839 /* Add the condition. */
9840 finish_if_stmt_cond (condition, statement);
9842 /* Parse the then-clause. */
9843 in_statement = parser->in_statement;
9844 parser->in_statement |= IN_IF_STMT;
9845 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9847 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9848 add_stmt (build_empty_stmt (loc));
9849 cp_lexer_consume_token (parser->lexer);
9850 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9851 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9852 "empty body in an %<if%> statement");
9853 nested_if = false;
9855 else
9856 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9857 parser->in_statement = in_statement;
9859 finish_then_clause (statement);
9861 /* If the next token is `else', parse the else-clause. */
9862 if (cp_lexer_next_token_is_keyword (parser->lexer,
9863 RID_ELSE))
9865 /* Consume the `else' keyword. */
9866 cp_lexer_consume_token (parser->lexer);
9867 begin_else_clause (statement);
9868 /* Parse the else-clause. */
9869 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9871 location_t loc;
9872 loc = cp_lexer_peek_token (parser->lexer)->location;
9873 warning_at (loc,
9874 OPT_Wempty_body, "suggest braces around "
9875 "empty body in an %<else%> statement");
9876 add_stmt (build_empty_stmt (loc));
9877 cp_lexer_consume_token (parser->lexer);
9879 else
9880 cp_parser_implicitly_scoped_statement (parser, NULL);
9882 finish_else_clause (statement);
9884 /* If we are currently parsing a then-clause, then
9885 IF_P will not be NULL. We set it to true to
9886 indicate that this if statement has an else clause.
9887 This may trigger the Wparentheses warning below
9888 when we get back up to the parent if statement. */
9889 if (if_p != NULL)
9890 *if_p = true;
9892 else
9894 /* This if statement does not have an else clause. If
9895 NESTED_IF is true, then the then-clause is an if
9896 statement which does have an else clause. We warn
9897 about the potential ambiguity. */
9898 if (nested_if)
9899 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9900 "suggest explicit braces to avoid ambiguous"
9901 " %<else%>");
9904 /* Now we're all done with the if-statement. */
9905 finish_if_stmt (statement);
9907 else
9909 bool in_switch_statement_p;
9910 unsigned char in_statement;
9912 /* Add the condition. */
9913 finish_switch_cond (condition, statement);
9915 /* Parse the body of the switch-statement. */
9916 in_switch_statement_p = parser->in_switch_statement_p;
9917 in_statement = parser->in_statement;
9918 parser->in_switch_statement_p = true;
9919 parser->in_statement |= IN_SWITCH_STMT;
9920 cp_parser_implicitly_scoped_statement (parser, NULL);
9921 parser->in_switch_statement_p = in_switch_statement_p;
9922 parser->in_statement = in_statement;
9924 /* Now we're all done with the switch-statement. */
9925 finish_switch_stmt (statement);
9928 return statement;
9930 break;
9932 default:
9933 cp_parser_error (parser, "expected selection-statement");
9934 return error_mark_node;
9938 /* Parse a condition.
9940 condition:
9941 expression
9942 type-specifier-seq declarator = initializer-clause
9943 type-specifier-seq declarator braced-init-list
9945 GNU Extension:
9947 condition:
9948 type-specifier-seq declarator asm-specification [opt]
9949 attributes [opt] = assignment-expression
9951 Returns the expression that should be tested. */
9953 static tree
9954 cp_parser_condition (cp_parser* parser)
9956 cp_decl_specifier_seq type_specifiers;
9957 const char *saved_message;
9958 int declares_class_or_enum;
9960 /* Try the declaration first. */
9961 cp_parser_parse_tentatively (parser);
9962 /* New types are not allowed in the type-specifier-seq for a
9963 condition. */
9964 saved_message = parser->type_definition_forbidden_message;
9965 parser->type_definition_forbidden_message
9966 = G_("types may not be defined in conditions");
9967 /* Parse the type-specifier-seq. */
9968 cp_parser_decl_specifier_seq (parser,
9969 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9970 &type_specifiers,
9971 &declares_class_or_enum);
9972 /* Restore the saved message. */
9973 parser->type_definition_forbidden_message = saved_message;
9974 /* If all is well, we might be looking at a declaration. */
9975 if (!cp_parser_error_occurred (parser))
9977 tree decl;
9978 tree asm_specification;
9979 tree attributes;
9980 cp_declarator *declarator;
9981 tree initializer = NULL_TREE;
9983 /* Parse the declarator. */
9984 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9985 /*ctor_dtor_or_conv_p=*/NULL,
9986 /*parenthesized_p=*/NULL,
9987 /*member_p=*/false);
9988 /* Parse the attributes. */
9989 attributes = cp_parser_attributes_opt (parser);
9990 /* Parse the asm-specification. */
9991 asm_specification = cp_parser_asm_specification_opt (parser);
9992 /* If the next token is not an `=' or '{', then we might still be
9993 looking at an expression. For example:
9995 if (A(a).x)
9997 looks like a decl-specifier-seq and a declarator -- but then
9998 there is no `=', so this is an expression. */
9999 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10000 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10001 cp_parser_simulate_error (parser);
10003 /* If we did see an `=' or '{', then we are looking at a declaration
10004 for sure. */
10005 if (cp_parser_parse_definitely (parser))
10007 tree pushed_scope;
10008 bool non_constant_p;
10009 bool flags = LOOKUP_ONLYCONVERTING;
10011 /* Create the declaration. */
10012 decl = start_decl (declarator, &type_specifiers,
10013 /*initialized_p=*/true,
10014 attributes, /*prefix_attributes=*/NULL_TREE,
10015 &pushed_scope);
10017 /* Parse the initializer. */
10018 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10020 initializer = cp_parser_braced_list (parser, &non_constant_p);
10021 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10022 flags = 0;
10024 else
10026 /* Consume the `='. */
10027 cp_parser_require (parser, CPP_EQ, RT_EQ);
10028 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10030 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10031 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10033 /* Process the initializer. */
10034 cp_finish_decl (decl,
10035 initializer, !non_constant_p,
10036 asm_specification,
10037 flags);
10039 if (pushed_scope)
10040 pop_scope (pushed_scope);
10042 return convert_from_reference (decl);
10045 /* If we didn't even get past the declarator successfully, we are
10046 definitely not looking at a declaration. */
10047 else
10048 cp_parser_abort_tentative_parse (parser);
10050 /* Otherwise, we are looking at an expression. */
10051 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
10054 /* Parses a for-statement or range-for-statement until the closing ')',
10055 not included. */
10057 static tree
10058 cp_parser_for (cp_parser *parser, bool ivdep)
10060 tree init, scope, decl;
10061 bool is_range_for;
10063 /* Begin the for-statement. */
10064 scope = begin_for_scope (&init);
10066 /* Parse the initialization. */
10067 is_range_for = cp_parser_for_init_statement (parser, &decl);
10069 if (is_range_for)
10070 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10071 else
10072 return cp_parser_c_for (parser, scope, init, ivdep);
10075 static tree
10076 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10078 /* Normal for loop */
10079 tree condition = NULL_TREE;
10080 tree expression = NULL_TREE;
10081 tree stmt;
10083 stmt = begin_for_stmt (scope, init);
10084 /* The for-init-statement has already been parsed in
10085 cp_parser_for_init_statement, so no work is needed here. */
10086 finish_for_init_stmt (stmt);
10088 /* If there's a condition, process it. */
10089 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10090 condition = cp_parser_condition (parser);
10091 else if (ivdep)
10093 cp_parser_error (parser, "missing loop condition in loop with "
10094 "%<GCC ivdep%> pragma");
10095 condition = error_mark_node;
10097 finish_for_cond (condition, stmt, ivdep);
10098 /* Look for the `;'. */
10099 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10101 /* If there's an expression, process it. */
10102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10103 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10104 finish_for_expr (expression, stmt);
10106 return stmt;
10109 /* Tries to parse a range-based for-statement:
10111 range-based-for:
10112 decl-specifier-seq declarator : expression
10114 The decl-specifier-seq declarator and the `:' are already parsed by
10115 cp_parser_for_init_statement. If processing_template_decl it returns a
10116 newly created RANGE_FOR_STMT; if not, it is converted to a
10117 regular FOR_STMT. */
10119 static tree
10120 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10121 bool ivdep)
10123 tree stmt, range_expr;
10125 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10127 bool expr_non_constant_p;
10128 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10130 else
10131 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10133 /* If in template, STMT is converted to a normal for-statement
10134 at instantiation. If not, it is done just ahead. */
10135 if (processing_template_decl)
10137 if (check_for_bare_parameter_packs (range_expr))
10138 range_expr = error_mark_node;
10139 stmt = begin_range_for_stmt (scope, init);
10140 if (ivdep)
10141 RANGE_FOR_IVDEP (stmt) = 1;
10142 finish_range_for_decl (stmt, range_decl, range_expr);
10143 if (!type_dependent_expression_p (range_expr)
10144 /* do_auto_deduction doesn't mess with template init-lists. */
10145 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10146 do_range_for_auto_deduction (range_decl, range_expr);
10148 else
10150 stmt = begin_for_stmt (scope, init);
10151 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10153 return stmt;
10156 /* Subroutine of cp_convert_range_for: given the initializer expression,
10157 builds up the range temporary. */
10159 static tree
10160 build_range_temp (tree range_expr)
10162 tree range_type, range_temp;
10164 /* Find out the type deduced by the declaration
10165 `auto &&__range = range_expr'. */
10166 range_type = cp_build_reference_type (make_auto (), true);
10167 range_type = do_auto_deduction (range_type, range_expr,
10168 type_uses_auto (range_type));
10170 /* Create the __range variable. */
10171 range_temp = build_decl (input_location, VAR_DECL,
10172 get_identifier ("__for_range"), range_type);
10173 TREE_USED (range_temp) = 1;
10174 DECL_ARTIFICIAL (range_temp) = 1;
10176 return range_temp;
10179 /* Used by cp_parser_range_for in template context: we aren't going to
10180 do a full conversion yet, but we still need to resolve auto in the
10181 type of the for-range-declaration if present. This is basically
10182 a shortcut version of cp_convert_range_for. */
10184 static void
10185 do_range_for_auto_deduction (tree decl, tree range_expr)
10187 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10188 if (auto_node)
10190 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10191 range_temp = convert_from_reference (build_range_temp (range_expr));
10192 iter_type = (cp_parser_perform_range_for_lookup
10193 (range_temp, &begin_dummy, &end_dummy));
10194 if (iter_type)
10196 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10197 iter_type);
10198 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10199 tf_warning_or_error);
10200 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10201 iter_decl, auto_node);
10206 /* Converts a range-based for-statement into a normal
10207 for-statement, as per the definition.
10209 for (RANGE_DECL : RANGE_EXPR)
10210 BLOCK
10212 should be equivalent to:
10215 auto &&__range = RANGE_EXPR;
10216 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10217 __begin != __end;
10218 ++__begin)
10220 RANGE_DECL = *__begin;
10221 BLOCK
10225 If RANGE_EXPR is an array:
10226 BEGIN_EXPR = __range
10227 END_EXPR = __range + ARRAY_SIZE(__range)
10228 Else if RANGE_EXPR has a member 'begin' or 'end':
10229 BEGIN_EXPR = __range.begin()
10230 END_EXPR = __range.end()
10231 Else:
10232 BEGIN_EXPR = begin(__range)
10233 END_EXPR = end(__range);
10235 If __range has a member 'begin' but not 'end', or vice versa, we must
10236 still use the second alternative (it will surely fail, however).
10237 When calling begin()/end() in the third alternative we must use
10238 argument dependent lookup, but always considering 'std' as an associated
10239 namespace. */
10241 tree
10242 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10243 bool ivdep)
10245 tree begin, end;
10246 tree iter_type, begin_expr, end_expr;
10247 tree condition, expression;
10249 if (range_decl == error_mark_node || range_expr == error_mark_node)
10250 /* If an error happened previously do nothing or else a lot of
10251 unhelpful errors would be issued. */
10252 begin_expr = end_expr = iter_type = error_mark_node;
10253 else
10255 tree range_temp;
10257 if (TREE_CODE (range_expr) == VAR_DECL
10258 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10259 /* Can't bind a reference to an array of runtime bound. */
10260 range_temp = range_expr;
10261 else
10263 range_temp = build_range_temp (range_expr);
10264 pushdecl (range_temp);
10265 cp_finish_decl (range_temp, range_expr,
10266 /*is_constant_init*/false, NULL_TREE,
10267 LOOKUP_ONLYCONVERTING);
10268 range_temp = convert_from_reference (range_temp);
10270 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10271 &begin_expr, &end_expr);
10274 /* The new for initialization statement. */
10275 begin = build_decl (input_location, VAR_DECL,
10276 get_identifier ("__for_begin"), iter_type);
10277 TREE_USED (begin) = 1;
10278 DECL_ARTIFICIAL (begin) = 1;
10279 pushdecl (begin);
10280 cp_finish_decl (begin, begin_expr,
10281 /*is_constant_init*/false, NULL_TREE,
10282 LOOKUP_ONLYCONVERTING);
10284 end = build_decl (input_location, VAR_DECL,
10285 get_identifier ("__for_end"), iter_type);
10286 TREE_USED (end) = 1;
10287 DECL_ARTIFICIAL (end) = 1;
10288 pushdecl (end);
10289 cp_finish_decl (end, end_expr,
10290 /*is_constant_init*/false, NULL_TREE,
10291 LOOKUP_ONLYCONVERTING);
10293 finish_for_init_stmt (statement);
10295 /* The new for condition. */
10296 condition = build_x_binary_op (input_location, NE_EXPR,
10297 begin, ERROR_MARK,
10298 end, ERROR_MARK,
10299 NULL, tf_warning_or_error);
10300 finish_for_cond (condition, statement, ivdep);
10302 /* The new increment expression. */
10303 expression = finish_unary_op_expr (input_location,
10304 PREINCREMENT_EXPR, begin,
10305 tf_warning_or_error);
10306 finish_for_expr (expression, statement);
10308 /* The declaration is initialized with *__begin inside the loop body. */
10309 cp_finish_decl (range_decl,
10310 build_x_indirect_ref (input_location, begin, RO_NULL,
10311 tf_warning_or_error),
10312 /*is_constant_init*/false, NULL_TREE,
10313 LOOKUP_ONLYCONVERTING);
10315 return statement;
10318 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10319 We need to solve both at the same time because the method used
10320 depends on the existence of members begin or end.
10321 Returns the type deduced for the iterator expression. */
10323 static tree
10324 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10326 if (error_operand_p (range))
10328 *begin = *end = error_mark_node;
10329 return error_mark_node;
10332 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10334 error ("range-based %<for%> expression of type %qT "
10335 "has incomplete type", TREE_TYPE (range));
10336 *begin = *end = error_mark_node;
10337 return error_mark_node;
10339 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10341 /* If RANGE is an array, we will use pointer arithmetic. */
10342 *begin = range;
10343 *end = build_binary_op (input_location, PLUS_EXPR,
10344 range,
10345 array_type_nelts_top (TREE_TYPE (range)),
10347 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10349 else
10351 /* If it is not an array, we must do a bit of magic. */
10352 tree id_begin, id_end;
10353 tree member_begin, member_end;
10355 *begin = *end = error_mark_node;
10357 id_begin = get_identifier ("begin");
10358 id_end = get_identifier ("end");
10359 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10360 /*protect=*/2, /*want_type=*/false,
10361 tf_warning_or_error);
10362 member_end = lookup_member (TREE_TYPE (range), id_end,
10363 /*protect=*/2, /*want_type=*/false,
10364 tf_warning_or_error);
10366 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10368 /* Use the member functions. */
10369 if (member_begin != NULL_TREE)
10370 *begin = cp_parser_range_for_member_function (range, id_begin);
10371 else
10372 error ("range-based %<for%> expression of type %qT has an "
10373 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10375 if (member_end != NULL_TREE)
10376 *end = cp_parser_range_for_member_function (range, id_end);
10377 else
10378 error ("range-based %<for%> expression of type %qT has a "
10379 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10381 else
10383 /* Use global functions with ADL. */
10384 vec<tree, va_gc> *vec;
10385 vec = make_tree_vector ();
10387 vec_safe_push (vec, range);
10389 member_begin = perform_koenig_lookup (id_begin, vec,
10390 tf_warning_or_error);
10391 *begin = finish_call_expr (member_begin, &vec, false, true,
10392 tf_warning_or_error);
10393 member_end = perform_koenig_lookup (id_end, vec,
10394 tf_warning_or_error);
10395 *end = finish_call_expr (member_end, &vec, false, true,
10396 tf_warning_or_error);
10398 release_tree_vector (vec);
10401 /* Last common checks. */
10402 if (*begin == error_mark_node || *end == error_mark_node)
10404 /* If one of the expressions is an error do no more checks. */
10405 *begin = *end = error_mark_node;
10406 return error_mark_node;
10408 else if (type_dependent_expression_p (*begin)
10409 || type_dependent_expression_p (*end))
10410 /* Can happen, when, eg, in a template context, Koenig lookup
10411 can't resolve begin/end (c++/58503). */
10412 return NULL_TREE;
10413 else
10415 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10416 /* The unqualified type of the __begin and __end temporaries should
10417 be the same, as required by the multiple auto declaration. */
10418 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10419 error ("inconsistent begin/end types in range-based %<for%> "
10420 "statement: %qT and %qT",
10421 TREE_TYPE (*begin), TREE_TYPE (*end));
10422 return iter_type;
10427 /* Helper function for cp_parser_perform_range_for_lookup.
10428 Builds a tree for RANGE.IDENTIFIER(). */
10430 static tree
10431 cp_parser_range_for_member_function (tree range, tree identifier)
10433 tree member, res;
10434 vec<tree, va_gc> *vec;
10436 member = finish_class_member_access_expr (range, identifier,
10437 false, tf_warning_or_error);
10438 if (member == error_mark_node)
10439 return error_mark_node;
10441 vec = make_tree_vector ();
10442 res = finish_call_expr (member, &vec,
10443 /*disallow_virtual=*/false,
10444 /*koenig_p=*/false,
10445 tf_warning_or_error);
10446 release_tree_vector (vec);
10447 return res;
10450 /* Parse an iteration-statement.
10452 iteration-statement:
10453 while ( condition ) statement
10454 do statement while ( expression ) ;
10455 for ( for-init-statement condition [opt] ; expression [opt] )
10456 statement
10458 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10460 static tree
10461 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10463 cp_token *token;
10464 enum rid keyword;
10465 tree statement;
10466 unsigned char in_statement;
10468 /* Peek at the next token. */
10469 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10470 if (!token)
10471 return error_mark_node;
10473 /* Remember whether or not we are already within an iteration
10474 statement. */
10475 in_statement = parser->in_statement;
10477 /* See what kind of keyword it is. */
10478 keyword = token->keyword;
10479 switch (keyword)
10481 case RID_WHILE:
10483 tree condition;
10485 /* Begin the while-statement. */
10486 statement = begin_while_stmt ();
10487 /* Look for the `('. */
10488 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10489 /* Parse the condition. */
10490 condition = cp_parser_condition (parser);
10491 finish_while_stmt_cond (condition, statement, ivdep);
10492 /* Look for the `)'. */
10493 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10494 /* Parse the dependent statement. */
10495 parser->in_statement = IN_ITERATION_STMT;
10496 cp_parser_already_scoped_statement (parser);
10497 parser->in_statement = in_statement;
10498 /* We're done with the while-statement. */
10499 finish_while_stmt (statement);
10501 break;
10503 case RID_DO:
10505 tree expression;
10507 /* Begin the do-statement. */
10508 statement = begin_do_stmt ();
10509 /* Parse the body of the do-statement. */
10510 parser->in_statement = IN_ITERATION_STMT;
10511 cp_parser_implicitly_scoped_statement (parser, NULL);
10512 parser->in_statement = in_statement;
10513 finish_do_body (statement);
10514 /* Look for the `while' keyword. */
10515 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10516 /* Look for the `('. */
10517 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10518 /* Parse the expression. */
10519 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10520 /* We're done with the do-statement. */
10521 finish_do_stmt (expression, statement, ivdep);
10522 /* Look for the `)'. */
10523 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10524 /* Look for the `;'. */
10525 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10527 break;
10529 case RID_FOR:
10531 /* Look for the `('. */
10532 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10534 statement = cp_parser_for (parser, ivdep);
10536 /* Look for the `)'. */
10537 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10539 /* Parse the body of the for-statement. */
10540 parser->in_statement = IN_ITERATION_STMT;
10541 cp_parser_already_scoped_statement (parser);
10542 parser->in_statement = in_statement;
10544 /* We're done with the for-statement. */
10545 finish_for_stmt (statement);
10547 break;
10549 default:
10550 cp_parser_error (parser, "expected iteration-statement");
10551 statement = error_mark_node;
10552 break;
10555 return statement;
10558 /* Parse a for-init-statement or the declarator of a range-based-for.
10559 Returns true if a range-based-for declaration is seen.
10561 for-init-statement:
10562 expression-statement
10563 simple-declaration */
10565 static bool
10566 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10568 /* If the next token is a `;', then we have an empty
10569 expression-statement. Grammatically, this is also a
10570 simple-declaration, but an invalid one, because it does not
10571 declare anything. Therefore, if we did not handle this case
10572 specially, we would issue an error message about an invalid
10573 declaration. */
10574 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10576 bool is_range_for = false;
10577 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10579 parser->colon_corrects_to_scope_p = false;
10581 /* We're going to speculatively look for a declaration, falling back
10582 to an expression, if necessary. */
10583 cp_parser_parse_tentatively (parser);
10584 /* Parse the declaration. */
10585 cp_parser_simple_declaration (parser,
10586 /*function_definition_allowed_p=*/false,
10587 decl);
10588 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10589 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10591 /* It is a range-for, consume the ':' */
10592 cp_lexer_consume_token (parser->lexer);
10593 is_range_for = true;
10594 if (cxx_dialect < cxx11)
10596 error_at (cp_lexer_peek_token (parser->lexer)->location,
10597 "range-based %<for%> loops are not allowed "
10598 "in C++98 mode");
10599 *decl = error_mark_node;
10602 else
10603 /* The ';' is not consumed yet because we told
10604 cp_parser_simple_declaration not to. */
10605 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10607 if (cp_parser_parse_definitely (parser))
10608 return is_range_for;
10609 /* If the tentative parse failed, then we shall need to look for an
10610 expression-statement. */
10612 /* If we are here, it is an expression-statement. */
10613 cp_parser_expression_statement (parser, NULL_TREE);
10614 return false;
10617 /* Parse a jump-statement.
10619 jump-statement:
10620 break ;
10621 continue ;
10622 return expression [opt] ;
10623 return braced-init-list ;
10624 goto identifier ;
10626 GNU extension:
10628 jump-statement:
10629 goto * expression ;
10631 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10633 static tree
10634 cp_parser_jump_statement (cp_parser* parser)
10636 tree statement = error_mark_node;
10637 cp_token *token;
10638 enum rid keyword;
10639 unsigned char in_statement;
10641 /* Peek at the next token. */
10642 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10643 if (!token)
10644 return error_mark_node;
10646 /* See what kind of keyword it is. */
10647 keyword = token->keyword;
10648 switch (keyword)
10650 case RID_BREAK:
10651 in_statement = parser->in_statement & ~IN_IF_STMT;
10652 switch (in_statement)
10654 case 0:
10655 error_at (token->location, "break statement not within loop or switch");
10656 break;
10657 default:
10658 gcc_assert ((in_statement & IN_SWITCH_STMT)
10659 || in_statement == IN_ITERATION_STMT);
10660 statement = finish_break_stmt ();
10661 if (in_statement == IN_ITERATION_STMT)
10662 break_maybe_infinite_loop ();
10663 break;
10664 case IN_OMP_BLOCK:
10665 error_at (token->location, "invalid exit from OpenMP structured block");
10666 break;
10667 case IN_OMP_FOR:
10668 error_at (token->location, "break statement used with OpenMP for loop");
10669 break;
10670 case IN_CILK_SIMD_FOR:
10671 error_at (token->location, "break statement used with Cilk Plus for loop");
10672 break;
10674 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10675 break;
10677 case RID_CONTINUE:
10678 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10680 case 0:
10681 error_at (token->location, "continue statement not within a loop");
10682 break;
10683 case IN_CILK_SIMD_FOR:
10684 error_at (token->location,
10685 "continue statement within %<#pragma simd%> loop body");
10686 /* Fall through. */
10687 case IN_ITERATION_STMT:
10688 case IN_OMP_FOR:
10689 statement = finish_continue_stmt ();
10690 break;
10691 case IN_OMP_BLOCK:
10692 error_at (token->location, "invalid exit from OpenMP structured block");
10693 break;
10694 default:
10695 gcc_unreachable ();
10697 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10698 break;
10700 case RID_RETURN:
10702 tree expr;
10703 bool expr_non_constant_p;
10705 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10707 cp_lexer_set_source_position (parser->lexer);
10708 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10709 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10711 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10712 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10713 else
10714 /* If the next token is a `;', then there is no
10715 expression. */
10716 expr = NULL_TREE;
10717 /* Build the return-statement. */
10718 statement = finish_return_stmt (expr);
10719 /* Look for the final `;'. */
10720 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10722 break;
10724 case RID_GOTO:
10725 /* Create the goto-statement. */
10726 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10728 /* Issue a warning about this use of a GNU extension. */
10729 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10730 /* Consume the '*' token. */
10731 cp_lexer_consume_token (parser->lexer);
10732 /* Parse the dependent expression. */
10733 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10735 else
10736 finish_goto_stmt (cp_parser_identifier (parser));
10737 /* Look for the final `;'. */
10738 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10739 break;
10741 default:
10742 cp_parser_error (parser, "expected jump-statement");
10743 break;
10746 return statement;
10749 /* Parse a declaration-statement.
10751 declaration-statement:
10752 block-declaration */
10754 static void
10755 cp_parser_declaration_statement (cp_parser* parser)
10757 void *p;
10759 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10760 p = obstack_alloc (&declarator_obstack, 0);
10762 /* Parse the block-declaration. */
10763 cp_parser_block_declaration (parser, /*statement_p=*/true);
10765 /* Free any declarators allocated. */
10766 obstack_free (&declarator_obstack, p);
10769 /* Some dependent statements (like `if (cond) statement'), are
10770 implicitly in their own scope. In other words, if the statement is
10771 a single statement (as opposed to a compound-statement), it is
10772 none-the-less treated as if it were enclosed in braces. Any
10773 declarations appearing in the dependent statement are out of scope
10774 after control passes that point. This function parses a statement,
10775 but ensures that is in its own scope, even if it is not a
10776 compound-statement.
10778 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10779 is a (possibly labeled) if statement which is not enclosed in
10780 braces and has an else clause. This is used to implement
10781 -Wparentheses.
10783 Returns the new statement. */
10785 static tree
10786 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10788 tree statement;
10790 if (if_p != NULL)
10791 *if_p = false;
10793 /* Mark if () ; with a special NOP_EXPR. */
10794 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10796 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10797 cp_lexer_consume_token (parser->lexer);
10798 statement = add_stmt (build_empty_stmt (loc));
10800 /* if a compound is opened, we simply parse the statement directly. */
10801 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10802 statement = cp_parser_compound_statement (parser, NULL, false, false);
10803 /* If the token is not a `{', then we must take special action. */
10804 else
10806 /* Create a compound-statement. */
10807 statement = begin_compound_stmt (0);
10808 /* Parse the dependent-statement. */
10809 cp_parser_statement (parser, NULL_TREE, false, if_p);
10810 /* Finish the dummy compound-statement. */
10811 finish_compound_stmt (statement);
10814 /* Return the statement. */
10815 return statement;
10818 /* For some dependent statements (like `while (cond) statement'), we
10819 have already created a scope. Therefore, even if the dependent
10820 statement is a compound-statement, we do not want to create another
10821 scope. */
10823 static void
10824 cp_parser_already_scoped_statement (cp_parser* parser)
10826 /* If the token is a `{', then we must take special action. */
10827 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10828 cp_parser_statement (parser, NULL_TREE, false, NULL);
10829 else
10831 /* Avoid calling cp_parser_compound_statement, so that we
10832 don't create a new scope. Do everything else by hand. */
10833 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10834 /* If the next keyword is `__label__' we have a label declaration. */
10835 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10836 cp_parser_label_declaration (parser);
10837 /* Parse an (optional) statement-seq. */
10838 cp_parser_statement_seq_opt (parser, NULL_TREE);
10839 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10843 /* Declarations [gram.dcl.dcl] */
10845 /* Parse an optional declaration-sequence.
10847 declaration-seq:
10848 declaration
10849 declaration-seq declaration */
10851 static void
10852 cp_parser_declaration_seq_opt (cp_parser* parser)
10854 while (true)
10856 cp_token *token;
10858 token = cp_lexer_peek_token (parser->lexer);
10860 if (token->type == CPP_CLOSE_BRACE
10861 || token->type == CPP_EOF
10862 || token->type == CPP_PRAGMA_EOL)
10863 break;
10865 if (token->type == CPP_SEMICOLON)
10867 /* A declaration consisting of a single semicolon is
10868 invalid. Allow it unless we're being pedantic. */
10869 cp_lexer_consume_token (parser->lexer);
10870 if (!in_system_header_at (input_location))
10871 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10872 continue;
10875 /* If we're entering or exiting a region that's implicitly
10876 extern "C", modify the lang context appropriately. */
10877 if (!parser->implicit_extern_c && token->implicit_extern_c)
10879 push_lang_context (lang_name_c);
10880 parser->implicit_extern_c = true;
10882 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10884 pop_lang_context ();
10885 parser->implicit_extern_c = false;
10888 if (token->type == CPP_PRAGMA)
10890 /* A top-level declaration can consist solely of a #pragma.
10891 A nested declaration cannot, so this is done here and not
10892 in cp_parser_declaration. (A #pragma at block scope is
10893 handled in cp_parser_statement.) */
10894 cp_parser_pragma (parser, pragma_external);
10895 continue;
10898 /* Parse the declaration itself. */
10899 cp_parser_declaration (parser);
10903 /* Parse a declaration.
10905 declaration:
10906 block-declaration
10907 function-definition
10908 template-declaration
10909 explicit-instantiation
10910 explicit-specialization
10911 linkage-specification
10912 namespace-definition
10914 GNU extension:
10916 declaration:
10917 __extension__ declaration */
10919 static void
10920 cp_parser_declaration (cp_parser* parser)
10922 cp_token token1;
10923 cp_token token2;
10924 int saved_pedantic;
10925 void *p;
10926 tree attributes = NULL_TREE;
10928 /* Check for the `__extension__' keyword. */
10929 if (cp_parser_extension_opt (parser, &saved_pedantic))
10931 /* Parse the qualified declaration. */
10932 cp_parser_declaration (parser);
10933 /* Restore the PEDANTIC flag. */
10934 pedantic = saved_pedantic;
10936 return;
10939 /* Try to figure out what kind of declaration is present. */
10940 token1 = *cp_lexer_peek_token (parser->lexer);
10942 if (token1.type != CPP_EOF)
10943 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10944 else
10946 token2.type = CPP_EOF;
10947 token2.keyword = RID_MAX;
10950 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10951 p = obstack_alloc (&declarator_obstack, 0);
10953 /* If the next token is `extern' and the following token is a string
10954 literal, then we have a linkage specification. */
10955 if (token1.keyword == RID_EXTERN
10956 && cp_parser_is_pure_string_literal (&token2))
10957 cp_parser_linkage_specification (parser);
10958 /* If the next token is `template', then we have either a template
10959 declaration, an explicit instantiation, or an explicit
10960 specialization. */
10961 else if (token1.keyword == RID_TEMPLATE)
10963 /* `template <>' indicates a template specialization. */
10964 if (token2.type == CPP_LESS
10965 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10966 cp_parser_explicit_specialization (parser);
10967 /* `template <' indicates a template declaration. */
10968 else if (token2.type == CPP_LESS)
10969 cp_parser_template_declaration (parser, /*member_p=*/false);
10970 /* Anything else must be an explicit instantiation. */
10971 else
10972 cp_parser_explicit_instantiation (parser);
10974 /* If the next token is `export', then we have a template
10975 declaration. */
10976 else if (token1.keyword == RID_EXPORT)
10977 cp_parser_template_declaration (parser, /*member_p=*/false);
10978 /* If the next token is `extern', 'static' or 'inline' and the one
10979 after that is `template', we have a GNU extended explicit
10980 instantiation directive. */
10981 else if (cp_parser_allow_gnu_extensions_p (parser)
10982 && (token1.keyword == RID_EXTERN
10983 || token1.keyword == RID_STATIC
10984 || token1.keyword == RID_INLINE)
10985 && token2.keyword == RID_TEMPLATE)
10986 cp_parser_explicit_instantiation (parser);
10987 /* If the next token is `namespace', check for a named or unnamed
10988 namespace definition. */
10989 else if (token1.keyword == RID_NAMESPACE
10990 && (/* A named namespace definition. */
10991 (token2.type == CPP_NAME
10992 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10993 != CPP_EQ))
10994 /* An unnamed namespace definition. */
10995 || token2.type == CPP_OPEN_BRACE
10996 || token2.keyword == RID_ATTRIBUTE))
10997 cp_parser_namespace_definition (parser);
10998 /* An inline (associated) namespace definition. */
10999 else if (token1.keyword == RID_INLINE
11000 && token2.keyword == RID_NAMESPACE)
11001 cp_parser_namespace_definition (parser);
11002 /* Objective-C++ declaration/definition. */
11003 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11004 cp_parser_objc_declaration (parser, NULL_TREE);
11005 else if (c_dialect_objc ()
11006 && token1.keyword == RID_ATTRIBUTE
11007 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11008 cp_parser_objc_declaration (parser, attributes);
11009 /* We must have either a block declaration or a function
11010 definition. */
11011 else
11012 /* Try to parse a block-declaration, or a function-definition. */
11013 cp_parser_block_declaration (parser, /*statement_p=*/false);
11015 /* Free any declarators allocated. */
11016 obstack_free (&declarator_obstack, p);
11019 /* Parse a block-declaration.
11021 block-declaration:
11022 simple-declaration
11023 asm-definition
11024 namespace-alias-definition
11025 using-declaration
11026 using-directive
11028 GNU Extension:
11030 block-declaration:
11031 __extension__ block-declaration
11033 C++0x Extension:
11035 block-declaration:
11036 static_assert-declaration
11038 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11039 part of a declaration-statement. */
11041 static void
11042 cp_parser_block_declaration (cp_parser *parser,
11043 bool statement_p)
11045 cp_token *token1;
11046 int saved_pedantic;
11048 /* Check for the `__extension__' keyword. */
11049 if (cp_parser_extension_opt (parser, &saved_pedantic))
11051 /* Parse the qualified declaration. */
11052 cp_parser_block_declaration (parser, statement_p);
11053 /* Restore the PEDANTIC flag. */
11054 pedantic = saved_pedantic;
11056 return;
11059 /* Peek at the next token to figure out which kind of declaration is
11060 present. */
11061 token1 = cp_lexer_peek_token (parser->lexer);
11063 /* If the next keyword is `asm', we have an asm-definition. */
11064 if (token1->keyword == RID_ASM)
11066 if (statement_p)
11067 cp_parser_commit_to_tentative_parse (parser);
11068 cp_parser_asm_definition (parser);
11070 /* If the next keyword is `namespace', we have a
11071 namespace-alias-definition. */
11072 else if (token1->keyword == RID_NAMESPACE)
11073 cp_parser_namespace_alias_definition (parser);
11074 /* If the next keyword is `using', we have a
11075 using-declaration, a using-directive, or an alias-declaration. */
11076 else if (token1->keyword == RID_USING)
11078 cp_token *token2;
11080 if (statement_p)
11081 cp_parser_commit_to_tentative_parse (parser);
11082 /* If the token after `using' is `namespace', then we have a
11083 using-directive. */
11084 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11085 if (token2->keyword == RID_NAMESPACE)
11086 cp_parser_using_directive (parser);
11087 /* If the second token after 'using' is '=', then we have an
11088 alias-declaration. */
11089 else if (cxx_dialect >= cxx11
11090 && token2->type == CPP_NAME
11091 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11092 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11093 cp_parser_alias_declaration (parser);
11094 /* Otherwise, it's a using-declaration. */
11095 else
11096 cp_parser_using_declaration (parser,
11097 /*access_declaration_p=*/false);
11099 /* If the next keyword is `__label__' we have a misplaced label
11100 declaration. */
11101 else if (token1->keyword == RID_LABEL)
11103 cp_lexer_consume_token (parser->lexer);
11104 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11105 cp_parser_skip_to_end_of_statement (parser);
11106 /* If the next token is now a `;', consume it. */
11107 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11108 cp_lexer_consume_token (parser->lexer);
11110 /* If the next token is `static_assert' we have a static assertion. */
11111 else if (token1->keyword == RID_STATIC_ASSERT)
11112 cp_parser_static_assert (parser, /*member_p=*/false);
11113 /* Anything else must be a simple-declaration. */
11114 else
11115 cp_parser_simple_declaration (parser, !statement_p,
11116 /*maybe_range_for_decl*/NULL);
11119 /* Parse a simple-declaration.
11121 simple-declaration:
11122 decl-specifier-seq [opt] init-declarator-list [opt] ;
11124 init-declarator-list:
11125 init-declarator
11126 init-declarator-list , init-declarator
11128 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11129 function-definition as a simple-declaration.
11131 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11132 parsed declaration if it is an uninitialized single declarator not followed
11133 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11134 if present, will not be consumed. */
11136 static void
11137 cp_parser_simple_declaration (cp_parser* parser,
11138 bool function_definition_allowed_p,
11139 tree *maybe_range_for_decl)
11141 cp_decl_specifier_seq decl_specifiers;
11142 int declares_class_or_enum;
11143 bool saw_declarator;
11145 if (maybe_range_for_decl)
11146 *maybe_range_for_decl = NULL_TREE;
11148 /* Defer access checks until we know what is being declared; the
11149 checks for names appearing in the decl-specifier-seq should be
11150 done as if we were in the scope of the thing being declared. */
11151 push_deferring_access_checks (dk_deferred);
11153 /* Parse the decl-specifier-seq. We have to keep track of whether
11154 or not the decl-specifier-seq declares a named class or
11155 enumeration type, since that is the only case in which the
11156 init-declarator-list is allowed to be empty.
11158 [dcl.dcl]
11160 In a simple-declaration, the optional init-declarator-list can be
11161 omitted only when declaring a class or enumeration, that is when
11162 the decl-specifier-seq contains either a class-specifier, an
11163 elaborated-type-specifier, or an enum-specifier. */
11164 cp_parser_decl_specifier_seq (parser,
11165 CP_PARSER_FLAGS_OPTIONAL,
11166 &decl_specifiers,
11167 &declares_class_or_enum);
11168 /* We no longer need to defer access checks. */
11169 stop_deferring_access_checks ();
11171 /* In a block scope, a valid declaration must always have a
11172 decl-specifier-seq. By not trying to parse declarators, we can
11173 resolve the declaration/expression ambiguity more quickly. */
11174 if (!function_definition_allowed_p
11175 && !decl_specifiers.any_specifiers_p)
11177 cp_parser_error (parser, "expected declaration");
11178 goto done;
11181 /* If the next two tokens are both identifiers, the code is
11182 erroneous. The usual cause of this situation is code like:
11184 T t;
11186 where "T" should name a type -- but does not. */
11187 if (!decl_specifiers.any_type_specifiers_p
11188 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11190 /* If parsing tentatively, we should commit; we really are
11191 looking at a declaration. */
11192 cp_parser_commit_to_tentative_parse (parser);
11193 /* Give up. */
11194 goto done;
11197 /* If we have seen at least one decl-specifier, and the next token
11198 is not a parenthesis, then we must be looking at a declaration.
11199 (After "int (" we might be looking at a functional cast.) */
11200 if (decl_specifiers.any_specifiers_p
11201 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11202 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11203 && !cp_parser_error_occurred (parser))
11204 cp_parser_commit_to_tentative_parse (parser);
11206 /* Keep going until we hit the `;' at the end of the simple
11207 declaration. */
11208 saw_declarator = false;
11209 while (cp_lexer_next_token_is_not (parser->lexer,
11210 CPP_SEMICOLON))
11212 cp_token *token;
11213 bool function_definition_p;
11214 tree decl;
11216 if (saw_declarator)
11218 /* If we are processing next declarator, coma is expected */
11219 token = cp_lexer_peek_token (parser->lexer);
11220 gcc_assert (token->type == CPP_COMMA);
11221 cp_lexer_consume_token (parser->lexer);
11222 if (maybe_range_for_decl)
11223 *maybe_range_for_decl = error_mark_node;
11225 else
11226 saw_declarator = true;
11228 /* Parse the init-declarator. */
11229 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11230 /*checks=*/NULL,
11231 function_definition_allowed_p,
11232 /*member_p=*/false,
11233 declares_class_or_enum,
11234 &function_definition_p,
11235 maybe_range_for_decl);
11236 /* If an error occurred while parsing tentatively, exit quickly.
11237 (That usually happens when in the body of a function; each
11238 statement is treated as a declaration-statement until proven
11239 otherwise.) */
11240 if (cp_parser_error_occurred (parser))
11241 goto done;
11242 /* Handle function definitions specially. */
11243 if (function_definition_p)
11245 /* If the next token is a `,', then we are probably
11246 processing something like:
11248 void f() {}, *p;
11250 which is erroneous. */
11251 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11253 cp_token *token = cp_lexer_peek_token (parser->lexer);
11254 error_at (token->location,
11255 "mixing"
11256 " declarations and function-definitions is forbidden");
11258 /* Otherwise, we're done with the list of declarators. */
11259 else
11261 pop_deferring_access_checks ();
11262 return;
11265 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11266 *maybe_range_for_decl = decl;
11267 /* The next token should be either a `,' or a `;'. */
11268 token = cp_lexer_peek_token (parser->lexer);
11269 /* If it's a `,', there are more declarators to come. */
11270 if (token->type == CPP_COMMA)
11271 /* will be consumed next time around */;
11272 /* If it's a `;', we are done. */
11273 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11274 break;
11275 /* Anything else is an error. */
11276 else
11278 /* If we have already issued an error message we don't need
11279 to issue another one. */
11280 if (decl != error_mark_node
11281 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11282 cp_parser_error (parser, "expected %<,%> or %<;%>");
11283 /* Skip tokens until we reach the end of the statement. */
11284 cp_parser_skip_to_end_of_statement (parser);
11285 /* If the next token is now a `;', consume it. */
11286 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11287 cp_lexer_consume_token (parser->lexer);
11288 goto done;
11290 /* After the first time around, a function-definition is not
11291 allowed -- even if it was OK at first. For example:
11293 int i, f() {}
11295 is not valid. */
11296 function_definition_allowed_p = false;
11299 /* Issue an error message if no declarators are present, and the
11300 decl-specifier-seq does not itself declare a class or
11301 enumeration: [dcl.dcl]/3. */
11302 if (!saw_declarator)
11304 if (cp_parser_declares_only_class_p (parser))
11306 if (!declares_class_or_enum
11307 && decl_specifiers.type
11308 && OVERLOAD_TYPE_P (decl_specifiers.type))
11309 /* Ensure an error is issued anyway when finish_decltype_type,
11310 called via cp_parser_decl_specifier_seq, returns a class or
11311 an enumeration (c++/51786). */
11312 decl_specifiers.type = NULL_TREE;
11313 shadow_tag (&decl_specifiers);
11315 /* Perform any deferred access checks. */
11316 perform_deferred_access_checks (tf_warning_or_error);
11319 /* Consume the `;'. */
11320 if (!maybe_range_for_decl)
11321 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11323 done:
11324 pop_deferring_access_checks ();
11327 /* Parse a decl-specifier-seq.
11329 decl-specifier-seq:
11330 decl-specifier-seq [opt] decl-specifier
11331 decl-specifier attribute-specifier-seq [opt] (C++11)
11333 decl-specifier:
11334 storage-class-specifier
11335 type-specifier
11336 function-specifier
11337 friend
11338 typedef
11340 GNU Extension:
11342 decl-specifier:
11343 attributes
11345 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11347 The parser flags FLAGS is used to control type-specifier parsing.
11349 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11350 flags:
11352 1: one of the decl-specifiers is an elaborated-type-specifier
11353 (i.e., a type declaration)
11354 2: one of the decl-specifiers is an enum-specifier or a
11355 class-specifier (i.e., a type definition)
11359 static void
11360 cp_parser_decl_specifier_seq (cp_parser* parser,
11361 cp_parser_flags flags,
11362 cp_decl_specifier_seq *decl_specs,
11363 int* declares_class_or_enum)
11365 bool constructor_possible_p = !parser->in_declarator_p;
11366 bool found_decl_spec = false;
11367 cp_token *start_token = NULL;
11368 cp_decl_spec ds;
11370 /* Clear DECL_SPECS. */
11371 clear_decl_specs (decl_specs);
11373 /* Assume no class or enumeration type is declared. */
11374 *declares_class_or_enum = 0;
11376 /* Keep reading specifiers until there are no more to read. */
11377 while (true)
11379 bool constructor_p;
11380 cp_token *token;
11381 ds = ds_last;
11383 /* Peek at the next token. */
11384 token = cp_lexer_peek_token (parser->lexer);
11386 /* Save the first token of the decl spec list for error
11387 reporting. */
11388 if (!start_token)
11389 start_token = token;
11390 /* Handle attributes. */
11391 if (cp_next_tokens_can_be_attribute_p (parser))
11393 /* Parse the attributes. */
11394 tree attrs = cp_parser_attributes_opt (parser);
11396 /* In a sequence of declaration specifiers, c++11 attributes
11397 appertain to the type that precede them. In that case
11398 [dcl.spec]/1 says:
11400 The attribute-specifier-seq affects the type only for
11401 the declaration it appears in, not other declarations
11402 involving the same type.
11404 But for now let's force the user to position the
11405 attribute either at the beginning of the declaration or
11406 after the declarator-id, which would clearly mean that it
11407 applies to the declarator. */
11408 if (cxx11_attribute_p (attrs))
11410 if (!found_decl_spec)
11411 /* The c++11 attribute is at the beginning of the
11412 declaration. It appertains to the entity being
11413 declared. */;
11414 else
11416 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11418 /* This is an attribute following a
11419 class-specifier. */
11420 if (decl_specs->type_definition_p)
11421 warn_misplaced_attr_for_class_type (token->location,
11422 decl_specs->type);
11423 attrs = NULL_TREE;
11425 else
11427 decl_specs->std_attributes
11428 = chainon (decl_specs->std_attributes,
11429 attrs);
11430 if (decl_specs->locations[ds_std_attribute] == 0)
11431 decl_specs->locations[ds_std_attribute] = token->location;
11433 continue;
11437 decl_specs->attributes
11438 = chainon (decl_specs->attributes,
11439 attrs);
11440 if (decl_specs->locations[ds_attribute] == 0)
11441 decl_specs->locations[ds_attribute] = token->location;
11442 continue;
11444 /* Assume we will find a decl-specifier keyword. */
11445 found_decl_spec = true;
11446 /* If the next token is an appropriate keyword, we can simply
11447 add it to the list. */
11448 switch (token->keyword)
11450 /* decl-specifier:
11451 friend
11452 constexpr */
11453 case RID_FRIEND:
11454 if (!at_class_scope_p ())
11456 error_at (token->location, "%<friend%> used outside of class");
11457 cp_lexer_purge_token (parser->lexer);
11459 else
11461 ds = ds_friend;
11462 /* Consume the token. */
11463 cp_lexer_consume_token (parser->lexer);
11465 break;
11467 case RID_CONSTEXPR:
11468 ds = ds_constexpr;
11469 cp_lexer_consume_token (parser->lexer);
11470 break;
11472 /* function-specifier:
11473 inline
11474 virtual
11475 explicit */
11476 case RID_INLINE:
11477 case RID_VIRTUAL:
11478 case RID_EXPLICIT:
11479 cp_parser_function_specifier_opt (parser, decl_specs);
11480 break;
11482 /* decl-specifier:
11483 typedef */
11484 case RID_TYPEDEF:
11485 ds = ds_typedef;
11486 /* Consume the token. */
11487 cp_lexer_consume_token (parser->lexer);
11488 /* A constructor declarator cannot appear in a typedef. */
11489 constructor_possible_p = false;
11490 /* The "typedef" keyword can only occur in a declaration; we
11491 may as well commit at this point. */
11492 cp_parser_commit_to_tentative_parse (parser);
11494 if (decl_specs->storage_class != sc_none)
11495 decl_specs->conflicting_specifiers_p = true;
11496 break;
11498 /* storage-class-specifier:
11499 auto
11500 register
11501 static
11502 extern
11503 mutable
11505 GNU Extension:
11506 thread */
11507 case RID_AUTO:
11508 if (cxx_dialect == cxx98)
11510 /* Consume the token. */
11511 cp_lexer_consume_token (parser->lexer);
11513 /* Complain about `auto' as a storage specifier, if
11514 we're complaining about C++0x compatibility. */
11515 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11516 " changes meaning in C++11; please remove it");
11518 /* Set the storage class anyway. */
11519 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11520 token);
11522 else
11523 /* C++0x auto type-specifier. */
11524 found_decl_spec = false;
11525 break;
11527 case RID_REGISTER:
11528 case RID_STATIC:
11529 case RID_EXTERN:
11530 case RID_MUTABLE:
11531 /* Consume the token. */
11532 cp_lexer_consume_token (parser->lexer);
11533 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11534 token);
11535 break;
11536 case RID_THREAD:
11537 /* Consume the token. */
11538 ds = ds_thread;
11539 cp_lexer_consume_token (parser->lexer);
11540 break;
11542 default:
11543 /* We did not yet find a decl-specifier yet. */
11544 found_decl_spec = false;
11545 break;
11548 if (found_decl_spec
11549 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11550 && token->keyword != RID_CONSTEXPR)
11551 error ("decl-specifier invalid in condition");
11553 if (ds != ds_last)
11554 set_and_check_decl_spec_loc (decl_specs, ds, token);
11556 /* Constructors are a special case. The `S' in `S()' is not a
11557 decl-specifier; it is the beginning of the declarator. */
11558 constructor_p
11559 = (!found_decl_spec
11560 && constructor_possible_p
11561 && (cp_parser_constructor_declarator_p
11562 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11564 /* If we don't have a DECL_SPEC yet, then we must be looking at
11565 a type-specifier. */
11566 if (!found_decl_spec && !constructor_p)
11568 int decl_spec_declares_class_or_enum;
11569 bool is_cv_qualifier;
11570 tree type_spec;
11572 type_spec
11573 = cp_parser_type_specifier (parser, flags,
11574 decl_specs,
11575 /*is_declaration=*/true,
11576 &decl_spec_declares_class_or_enum,
11577 &is_cv_qualifier);
11578 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11580 /* If this type-specifier referenced a user-defined type
11581 (a typedef, class-name, etc.), then we can't allow any
11582 more such type-specifiers henceforth.
11584 [dcl.spec]
11586 The longest sequence of decl-specifiers that could
11587 possibly be a type name is taken as the
11588 decl-specifier-seq of a declaration. The sequence shall
11589 be self-consistent as described below.
11591 [dcl.type]
11593 As a general rule, at most one type-specifier is allowed
11594 in the complete decl-specifier-seq of a declaration. The
11595 only exceptions are the following:
11597 -- const or volatile can be combined with any other
11598 type-specifier.
11600 -- signed or unsigned can be combined with char, long,
11601 short, or int.
11603 -- ..
11605 Example:
11607 typedef char* Pc;
11608 void g (const int Pc);
11610 Here, Pc is *not* part of the decl-specifier seq; it's
11611 the declarator. Therefore, once we see a type-specifier
11612 (other than a cv-qualifier), we forbid any additional
11613 user-defined types. We *do* still allow things like `int
11614 int' to be considered a decl-specifier-seq, and issue the
11615 error message later. */
11616 if (type_spec && !is_cv_qualifier)
11617 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11618 /* A constructor declarator cannot follow a type-specifier. */
11619 if (type_spec)
11621 constructor_possible_p = false;
11622 found_decl_spec = true;
11623 if (!is_cv_qualifier)
11624 decl_specs->any_type_specifiers_p = true;
11628 /* If we still do not have a DECL_SPEC, then there are no more
11629 decl-specifiers. */
11630 if (!found_decl_spec)
11631 break;
11633 decl_specs->any_specifiers_p = true;
11634 /* After we see one decl-specifier, further decl-specifiers are
11635 always optional. */
11636 flags |= CP_PARSER_FLAGS_OPTIONAL;
11639 /* Don't allow a friend specifier with a class definition. */
11640 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11641 && (*declares_class_or_enum & 2))
11642 error_at (decl_specs->locations[ds_friend],
11643 "class definition may not be declared a friend");
11646 /* Parse an (optional) storage-class-specifier.
11648 storage-class-specifier:
11649 auto
11650 register
11651 static
11652 extern
11653 mutable
11655 GNU Extension:
11657 storage-class-specifier:
11658 thread
11660 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11662 static tree
11663 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11665 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11667 case RID_AUTO:
11668 if (cxx_dialect != cxx98)
11669 return NULL_TREE;
11670 /* Fall through for C++98. */
11672 case RID_REGISTER:
11673 case RID_STATIC:
11674 case RID_EXTERN:
11675 case RID_MUTABLE:
11676 case RID_THREAD:
11677 /* Consume the token. */
11678 return cp_lexer_consume_token (parser->lexer)->u.value;
11680 default:
11681 return NULL_TREE;
11685 /* Parse an (optional) function-specifier.
11687 function-specifier:
11688 inline
11689 virtual
11690 explicit
11692 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11693 Updates DECL_SPECS, if it is non-NULL. */
11695 static tree
11696 cp_parser_function_specifier_opt (cp_parser* parser,
11697 cp_decl_specifier_seq *decl_specs)
11699 cp_token *token = cp_lexer_peek_token (parser->lexer);
11700 switch (token->keyword)
11702 case RID_INLINE:
11703 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11704 break;
11706 case RID_VIRTUAL:
11707 /* 14.5.2.3 [temp.mem]
11709 A member function template shall not be virtual. */
11710 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11711 error_at (token->location, "templates may not be %<virtual%>");
11712 else
11713 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11714 break;
11716 case RID_EXPLICIT:
11717 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11718 break;
11720 default:
11721 return NULL_TREE;
11724 /* Consume the token. */
11725 return cp_lexer_consume_token (parser->lexer)->u.value;
11728 /* Parse a linkage-specification.
11730 linkage-specification:
11731 extern string-literal { declaration-seq [opt] }
11732 extern string-literal declaration */
11734 static void
11735 cp_parser_linkage_specification (cp_parser* parser)
11737 tree linkage;
11739 /* Look for the `extern' keyword. */
11740 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11742 /* Look for the string-literal. */
11743 linkage = cp_parser_string_literal (parser, false, false);
11745 /* Transform the literal into an identifier. If the literal is a
11746 wide-character string, or contains embedded NULs, then we can't
11747 handle it as the user wants. */
11748 if (strlen (TREE_STRING_POINTER (linkage))
11749 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11751 cp_parser_error (parser, "invalid linkage-specification");
11752 /* Assume C++ linkage. */
11753 linkage = lang_name_cplusplus;
11755 else
11756 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11758 /* We're now using the new linkage. */
11759 push_lang_context (linkage);
11761 /* If the next token is a `{', then we're using the first
11762 production. */
11763 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11765 cp_ensure_no_omp_declare_simd (parser);
11767 /* Consume the `{' token. */
11768 cp_lexer_consume_token (parser->lexer);
11769 /* Parse the declarations. */
11770 cp_parser_declaration_seq_opt (parser);
11771 /* Look for the closing `}'. */
11772 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11774 /* Otherwise, there's just one declaration. */
11775 else
11777 bool saved_in_unbraced_linkage_specification_p;
11779 saved_in_unbraced_linkage_specification_p
11780 = parser->in_unbraced_linkage_specification_p;
11781 parser->in_unbraced_linkage_specification_p = true;
11782 cp_parser_declaration (parser);
11783 parser->in_unbraced_linkage_specification_p
11784 = saved_in_unbraced_linkage_specification_p;
11787 /* We're done with the linkage-specification. */
11788 pop_lang_context ();
11791 /* Parse a static_assert-declaration.
11793 static_assert-declaration:
11794 static_assert ( constant-expression , string-literal ) ;
11796 If MEMBER_P, this static_assert is a class member. */
11798 static void
11799 cp_parser_static_assert(cp_parser *parser, bool member_p)
11801 tree condition;
11802 tree message;
11803 cp_token *token;
11804 location_t saved_loc;
11805 bool dummy;
11807 /* Peek at the `static_assert' token so we can keep track of exactly
11808 where the static assertion started. */
11809 token = cp_lexer_peek_token (parser->lexer);
11810 saved_loc = token->location;
11812 /* Look for the `static_assert' keyword. */
11813 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11814 RT_STATIC_ASSERT))
11815 return;
11817 /* We know we are in a static assertion; commit to any tentative
11818 parse. */
11819 if (cp_parser_parsing_tentatively (parser))
11820 cp_parser_commit_to_tentative_parse (parser);
11822 /* Parse the `(' starting the static assertion condition. */
11823 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11825 /* Parse the constant-expression. Allow a non-constant expression
11826 here in order to give better diagnostics in finish_static_assert. */
11827 condition =
11828 cp_parser_constant_expression (parser,
11829 /*allow_non_constant_p=*/true,
11830 /*non_constant_p=*/&dummy);
11832 /* Parse the separating `,'. */
11833 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11835 /* Parse the string-literal message. */
11836 message = cp_parser_string_literal (parser,
11837 /*translate=*/false,
11838 /*wide_ok=*/true);
11840 /* A `)' completes the static assertion. */
11841 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11842 cp_parser_skip_to_closing_parenthesis (parser,
11843 /*recovering=*/true,
11844 /*or_comma=*/false,
11845 /*consume_paren=*/true);
11847 /* A semicolon terminates the declaration. */
11848 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11850 /* Complete the static assertion, which may mean either processing
11851 the static assert now or saving it for template instantiation. */
11852 finish_static_assert (condition, message, saved_loc, member_p);
11855 /* Parse the expression in decltype ( expression ). */
11857 static tree
11858 cp_parser_decltype_expr (cp_parser *parser,
11859 bool &id_expression_or_member_access_p)
11861 cp_token *id_expr_start_token;
11862 tree expr;
11864 /* First, try parsing an id-expression. */
11865 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11866 cp_parser_parse_tentatively (parser);
11867 expr = cp_parser_id_expression (parser,
11868 /*template_keyword_p=*/false,
11869 /*check_dependency_p=*/true,
11870 /*template_p=*/NULL,
11871 /*declarator_p=*/false,
11872 /*optional_p=*/false);
11874 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11876 bool non_integral_constant_expression_p = false;
11877 tree id_expression = expr;
11878 cp_id_kind idk;
11879 const char *error_msg;
11881 if (identifier_p (expr))
11882 /* Lookup the name we got back from the id-expression. */
11883 expr = cp_parser_lookup_name_simple (parser, expr,
11884 id_expr_start_token->location);
11886 if (expr
11887 && expr != error_mark_node
11888 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11889 && TREE_CODE (expr) != TYPE_DECL
11890 && (TREE_CODE (expr) != BIT_NOT_EXPR
11891 || !TYPE_P (TREE_OPERAND (expr, 0)))
11892 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11894 /* Complete lookup of the id-expression. */
11895 expr = (finish_id_expression
11896 (id_expression, expr, parser->scope, &idk,
11897 /*integral_constant_expression_p=*/false,
11898 /*allow_non_integral_constant_expression_p=*/true,
11899 &non_integral_constant_expression_p,
11900 /*template_p=*/false,
11901 /*done=*/true,
11902 /*address_p=*/false,
11903 /*template_arg_p=*/false,
11904 &error_msg,
11905 id_expr_start_token->location));
11907 if (expr == error_mark_node)
11908 /* We found an id-expression, but it was something that we
11909 should not have found. This is an error, not something
11910 we can recover from, so note that we found an
11911 id-expression and we'll recover as gracefully as
11912 possible. */
11913 id_expression_or_member_access_p = true;
11916 if (expr
11917 && expr != error_mark_node
11918 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11919 /* We have an id-expression. */
11920 id_expression_or_member_access_p = true;
11923 if (!id_expression_or_member_access_p)
11925 /* Abort the id-expression parse. */
11926 cp_parser_abort_tentative_parse (parser);
11928 /* Parsing tentatively, again. */
11929 cp_parser_parse_tentatively (parser);
11931 /* Parse a class member access. */
11932 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11933 /*cast_p=*/false, /*decltype*/true,
11934 /*member_access_only_p=*/true, NULL);
11936 if (expr
11937 && expr != error_mark_node
11938 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11939 /* We have an id-expression. */
11940 id_expression_or_member_access_p = true;
11943 if (id_expression_or_member_access_p)
11944 /* We have parsed the complete id-expression or member access. */
11945 cp_parser_parse_definitely (parser);
11946 else
11948 /* Abort our attempt to parse an id-expression or member access
11949 expression. */
11950 cp_parser_abort_tentative_parse (parser);
11952 /* Parse a full expression. */
11953 expr = cp_parser_expression (parser, /*cast_p=*/false,
11954 /*decltype*/true, NULL);
11957 return expr;
11960 /* Parse a `decltype' type. Returns the type.
11962 simple-type-specifier:
11963 decltype ( expression )
11964 C++14 proposal:
11965 decltype ( auto ) */
11967 static tree
11968 cp_parser_decltype (cp_parser *parser)
11970 tree expr;
11971 bool id_expression_or_member_access_p = false;
11972 const char *saved_message;
11973 bool saved_integral_constant_expression_p;
11974 bool saved_non_integral_constant_expression_p;
11975 bool saved_greater_than_is_operator_p;
11976 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11978 if (start_token->type == CPP_DECLTYPE)
11980 /* Already parsed. */
11981 cp_lexer_consume_token (parser->lexer);
11982 return start_token->u.value;
11985 /* Look for the `decltype' token. */
11986 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11987 return error_mark_node;
11989 /* Parse the opening `('. */
11990 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11991 return error_mark_node;
11993 /* decltype (auto) */
11994 if (cxx_dialect >= cxx1y
11995 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
11997 cp_lexer_consume_token (parser->lexer);
11998 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11999 return error_mark_node;
12000 expr = make_decltype_auto ();
12001 AUTO_IS_DECLTYPE (expr) = true;
12002 goto rewrite;
12005 /* Types cannot be defined in a `decltype' expression. Save away the
12006 old message. */
12007 saved_message = parser->type_definition_forbidden_message;
12009 /* And create the new one. */
12010 parser->type_definition_forbidden_message
12011 = G_("types may not be defined in %<decltype%> expressions");
12013 /* The restrictions on constant-expressions do not apply inside
12014 decltype expressions. */
12015 saved_integral_constant_expression_p
12016 = parser->integral_constant_expression_p;
12017 saved_non_integral_constant_expression_p
12018 = parser->non_integral_constant_expression_p;
12019 parser->integral_constant_expression_p = false;
12021 /* Within a parenthesized expression, a `>' token is always
12022 the greater-than operator. */
12023 saved_greater_than_is_operator_p
12024 = parser->greater_than_is_operator_p;
12025 parser->greater_than_is_operator_p = true;
12027 /* Do not actually evaluate the expression. */
12028 ++cp_unevaluated_operand;
12030 /* Do not warn about problems with the expression. */
12031 ++c_inhibit_evaluation_warnings;
12033 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12035 /* Go back to evaluating expressions. */
12036 --cp_unevaluated_operand;
12037 --c_inhibit_evaluation_warnings;
12039 /* The `>' token might be the end of a template-id or
12040 template-parameter-list now. */
12041 parser->greater_than_is_operator_p
12042 = saved_greater_than_is_operator_p;
12044 /* Restore the old message and the integral constant expression
12045 flags. */
12046 parser->type_definition_forbidden_message = saved_message;
12047 parser->integral_constant_expression_p
12048 = saved_integral_constant_expression_p;
12049 parser->non_integral_constant_expression_p
12050 = saved_non_integral_constant_expression_p;
12052 /* Parse to the closing `)'. */
12053 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12055 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12056 /*consume_paren=*/true);
12057 return error_mark_node;
12060 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12061 tf_warning_or_error);
12063 rewrite:
12064 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12065 it again. */
12066 start_token->type = CPP_DECLTYPE;
12067 start_token->u.value = expr;
12068 start_token->keyword = RID_MAX;
12069 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12071 return expr;
12074 /* Special member functions [gram.special] */
12076 /* Parse a conversion-function-id.
12078 conversion-function-id:
12079 operator conversion-type-id
12081 Returns an IDENTIFIER_NODE representing the operator. */
12083 static tree
12084 cp_parser_conversion_function_id (cp_parser* parser)
12086 tree type;
12087 tree saved_scope;
12088 tree saved_qualifying_scope;
12089 tree saved_object_scope;
12090 tree pushed_scope = NULL_TREE;
12092 /* Look for the `operator' token. */
12093 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12094 return error_mark_node;
12095 /* When we parse the conversion-type-id, the current scope will be
12096 reset. However, we need that information in able to look up the
12097 conversion function later, so we save it here. */
12098 saved_scope = parser->scope;
12099 saved_qualifying_scope = parser->qualifying_scope;
12100 saved_object_scope = parser->object_scope;
12101 /* We must enter the scope of the class so that the names of
12102 entities declared within the class are available in the
12103 conversion-type-id. For example, consider:
12105 struct S {
12106 typedef int I;
12107 operator I();
12110 S::operator I() { ... }
12112 In order to see that `I' is a type-name in the definition, we
12113 must be in the scope of `S'. */
12114 if (saved_scope)
12115 pushed_scope = push_scope (saved_scope);
12116 /* Parse the conversion-type-id. */
12117 type = cp_parser_conversion_type_id (parser);
12118 /* Leave the scope of the class, if any. */
12119 if (pushed_scope)
12120 pop_scope (pushed_scope);
12121 /* Restore the saved scope. */
12122 parser->scope = saved_scope;
12123 parser->qualifying_scope = saved_qualifying_scope;
12124 parser->object_scope = saved_object_scope;
12125 /* If the TYPE is invalid, indicate failure. */
12126 if (type == error_mark_node)
12127 return error_mark_node;
12128 return mangle_conv_op_name_for_type (type);
12131 /* Parse a conversion-type-id:
12133 conversion-type-id:
12134 type-specifier-seq conversion-declarator [opt]
12136 Returns the TYPE specified. */
12138 static tree
12139 cp_parser_conversion_type_id (cp_parser* parser)
12141 tree attributes;
12142 cp_decl_specifier_seq type_specifiers;
12143 cp_declarator *declarator;
12144 tree type_specified;
12145 const char *saved_message;
12147 /* Parse the attributes. */
12148 attributes = cp_parser_attributes_opt (parser);
12150 saved_message = parser->type_definition_forbidden_message;
12151 parser->type_definition_forbidden_message
12152 = G_("types may not be defined in a conversion-type-id");
12154 /* Parse the type-specifiers. */
12155 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12156 /*is_trailing_return=*/false,
12157 &type_specifiers);
12159 parser->type_definition_forbidden_message = saved_message;
12161 /* If that didn't work, stop. */
12162 if (type_specifiers.type == error_mark_node)
12163 return error_mark_node;
12164 /* Parse the conversion-declarator. */
12165 declarator = cp_parser_conversion_declarator_opt (parser);
12167 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12168 /*initialized=*/0, &attributes);
12169 if (attributes)
12170 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12172 /* Don't give this error when parsing tentatively. This happens to
12173 work because we always parse this definitively once. */
12174 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12175 && type_uses_auto (type_specified))
12177 if (cxx_dialect < cxx1y)
12179 error ("invalid use of %<auto%> in conversion operator");
12180 return error_mark_node;
12182 else if (template_parm_scope_p ())
12183 warning (0, "use of %<auto%> in member template "
12184 "conversion operator can never be deduced");
12187 return type_specified;
12190 /* Parse an (optional) conversion-declarator.
12192 conversion-declarator:
12193 ptr-operator conversion-declarator [opt]
12197 static cp_declarator *
12198 cp_parser_conversion_declarator_opt (cp_parser* parser)
12200 enum tree_code code;
12201 tree class_type, std_attributes = NULL_TREE;
12202 cp_cv_quals cv_quals;
12204 /* We don't know if there's a ptr-operator next, or not. */
12205 cp_parser_parse_tentatively (parser);
12206 /* Try the ptr-operator. */
12207 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12208 &std_attributes);
12209 /* If it worked, look for more conversion-declarators. */
12210 if (cp_parser_parse_definitely (parser))
12212 cp_declarator *declarator;
12214 /* Parse another optional declarator. */
12215 declarator = cp_parser_conversion_declarator_opt (parser);
12217 declarator = cp_parser_make_indirect_declarator
12218 (code, class_type, cv_quals, declarator, std_attributes);
12220 return declarator;
12223 return NULL;
12226 /* Parse an (optional) ctor-initializer.
12228 ctor-initializer:
12229 : mem-initializer-list
12231 Returns TRUE iff the ctor-initializer was actually present. */
12233 static bool
12234 cp_parser_ctor_initializer_opt (cp_parser* parser)
12236 /* If the next token is not a `:', then there is no
12237 ctor-initializer. */
12238 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12240 /* Do default initialization of any bases and members. */
12241 if (DECL_CONSTRUCTOR_P (current_function_decl))
12242 finish_mem_initializers (NULL_TREE);
12244 return false;
12247 /* Consume the `:' token. */
12248 cp_lexer_consume_token (parser->lexer);
12249 /* And the mem-initializer-list. */
12250 cp_parser_mem_initializer_list (parser);
12252 return true;
12255 /* Parse a mem-initializer-list.
12257 mem-initializer-list:
12258 mem-initializer ... [opt]
12259 mem-initializer ... [opt] , mem-initializer-list */
12261 static void
12262 cp_parser_mem_initializer_list (cp_parser* parser)
12264 tree mem_initializer_list = NULL_TREE;
12265 tree target_ctor = error_mark_node;
12266 cp_token *token = cp_lexer_peek_token (parser->lexer);
12268 /* Let the semantic analysis code know that we are starting the
12269 mem-initializer-list. */
12270 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12271 error_at (token->location,
12272 "only constructors take member initializers");
12274 /* Loop through the list. */
12275 while (true)
12277 tree mem_initializer;
12279 token = cp_lexer_peek_token (parser->lexer);
12280 /* Parse the mem-initializer. */
12281 mem_initializer = cp_parser_mem_initializer (parser);
12282 /* If the next token is a `...', we're expanding member initializers. */
12283 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12285 /* Consume the `...'. */
12286 cp_lexer_consume_token (parser->lexer);
12288 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12289 can be expanded but members cannot. */
12290 if (mem_initializer != error_mark_node
12291 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12293 error_at (token->location,
12294 "cannot expand initializer for member %<%D%>",
12295 TREE_PURPOSE (mem_initializer));
12296 mem_initializer = error_mark_node;
12299 /* Construct the pack expansion type. */
12300 if (mem_initializer != error_mark_node)
12301 mem_initializer = make_pack_expansion (mem_initializer);
12303 if (target_ctor != error_mark_node
12304 && mem_initializer != error_mark_node)
12306 error ("mem-initializer for %qD follows constructor delegation",
12307 TREE_PURPOSE (mem_initializer));
12308 mem_initializer = error_mark_node;
12310 /* Look for a target constructor. */
12311 if (mem_initializer != error_mark_node
12312 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12313 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12315 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12316 if (mem_initializer_list)
12318 error ("constructor delegation follows mem-initializer for %qD",
12319 TREE_PURPOSE (mem_initializer_list));
12320 mem_initializer = error_mark_node;
12322 target_ctor = mem_initializer;
12324 /* Add it to the list, unless it was erroneous. */
12325 if (mem_initializer != error_mark_node)
12327 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12328 mem_initializer_list = mem_initializer;
12330 /* If the next token is not a `,', we're done. */
12331 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12332 break;
12333 /* Consume the `,' token. */
12334 cp_lexer_consume_token (parser->lexer);
12337 /* Perform semantic analysis. */
12338 if (DECL_CONSTRUCTOR_P (current_function_decl))
12339 finish_mem_initializers (mem_initializer_list);
12342 /* Parse a mem-initializer.
12344 mem-initializer:
12345 mem-initializer-id ( expression-list [opt] )
12346 mem-initializer-id braced-init-list
12348 GNU extension:
12350 mem-initializer:
12351 ( expression-list [opt] )
12353 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12354 class) or FIELD_DECL (for a non-static data member) to initialize;
12355 the TREE_VALUE is the expression-list. An empty initialization
12356 list is represented by void_list_node. */
12358 static tree
12359 cp_parser_mem_initializer (cp_parser* parser)
12361 tree mem_initializer_id;
12362 tree expression_list;
12363 tree member;
12364 cp_token *token = cp_lexer_peek_token (parser->lexer);
12366 /* Find out what is being initialized. */
12367 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12369 permerror (token->location,
12370 "anachronistic old-style base class initializer");
12371 mem_initializer_id = NULL_TREE;
12373 else
12375 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12376 if (mem_initializer_id == error_mark_node)
12377 return mem_initializer_id;
12379 member = expand_member_init (mem_initializer_id);
12380 if (member && !DECL_P (member))
12381 in_base_initializer = 1;
12383 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12385 bool expr_non_constant_p;
12386 cp_lexer_set_source_position (parser->lexer);
12387 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12388 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12389 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12390 expression_list = build_tree_list (NULL_TREE, expression_list);
12392 else
12394 vec<tree, va_gc> *vec;
12395 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12396 /*cast_p=*/false,
12397 /*allow_expansion_p=*/true,
12398 /*non_constant_p=*/NULL);
12399 if (vec == NULL)
12400 return error_mark_node;
12401 expression_list = build_tree_list_vec (vec);
12402 release_tree_vector (vec);
12405 if (expression_list == error_mark_node)
12406 return error_mark_node;
12407 if (!expression_list)
12408 expression_list = void_type_node;
12410 in_base_initializer = 0;
12412 return member ? build_tree_list (member, expression_list) : error_mark_node;
12415 /* Parse a mem-initializer-id.
12417 mem-initializer-id:
12418 :: [opt] nested-name-specifier [opt] class-name
12419 identifier
12421 Returns a TYPE indicating the class to be initializer for the first
12422 production. Returns an IDENTIFIER_NODE indicating the data member
12423 to be initialized for the second production. */
12425 static tree
12426 cp_parser_mem_initializer_id (cp_parser* parser)
12428 bool global_scope_p;
12429 bool nested_name_specifier_p;
12430 bool template_p = false;
12431 tree id;
12433 cp_token *token = cp_lexer_peek_token (parser->lexer);
12435 /* `typename' is not allowed in this context ([temp.res]). */
12436 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12438 error_at (token->location,
12439 "keyword %<typename%> not allowed in this context (a qualified "
12440 "member initializer is implicitly a type)");
12441 cp_lexer_consume_token (parser->lexer);
12443 /* Look for the optional `::' operator. */
12444 global_scope_p
12445 = (cp_parser_global_scope_opt (parser,
12446 /*current_scope_valid_p=*/false)
12447 != NULL_TREE);
12448 /* Look for the optional nested-name-specifier. The simplest way to
12449 implement:
12451 [temp.res]
12453 The keyword `typename' is not permitted in a base-specifier or
12454 mem-initializer; in these contexts a qualified name that
12455 depends on a template-parameter is implicitly assumed to be a
12456 type name.
12458 is to assume that we have seen the `typename' keyword at this
12459 point. */
12460 nested_name_specifier_p
12461 = (cp_parser_nested_name_specifier_opt (parser,
12462 /*typename_keyword_p=*/true,
12463 /*check_dependency_p=*/true,
12464 /*type_p=*/true,
12465 /*is_declaration=*/true)
12466 != NULL_TREE);
12467 if (nested_name_specifier_p)
12468 template_p = cp_parser_optional_template_keyword (parser);
12469 /* If there is a `::' operator or a nested-name-specifier, then we
12470 are definitely looking for a class-name. */
12471 if (global_scope_p || nested_name_specifier_p)
12472 return cp_parser_class_name (parser,
12473 /*typename_keyword_p=*/true,
12474 /*template_keyword_p=*/template_p,
12475 typename_type,
12476 /*check_dependency_p=*/true,
12477 /*class_head_p=*/false,
12478 /*is_declaration=*/true);
12479 /* Otherwise, we could also be looking for an ordinary identifier. */
12480 cp_parser_parse_tentatively (parser);
12481 /* Try a class-name. */
12482 id = cp_parser_class_name (parser,
12483 /*typename_keyword_p=*/true,
12484 /*template_keyword_p=*/false,
12485 none_type,
12486 /*check_dependency_p=*/true,
12487 /*class_head_p=*/false,
12488 /*is_declaration=*/true);
12489 /* If we found one, we're done. */
12490 if (cp_parser_parse_definitely (parser))
12491 return id;
12492 /* Otherwise, look for an ordinary identifier. */
12493 return cp_parser_identifier (parser);
12496 /* Overloading [gram.over] */
12498 /* Parse an operator-function-id.
12500 operator-function-id:
12501 operator operator
12503 Returns an IDENTIFIER_NODE for the operator which is a
12504 human-readable spelling of the identifier, e.g., `operator +'. */
12506 static tree
12507 cp_parser_operator_function_id (cp_parser* parser)
12509 /* Look for the `operator' keyword. */
12510 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12511 return error_mark_node;
12512 /* And then the name of the operator itself. */
12513 return cp_parser_operator (parser);
12516 /* Return an identifier node for a user-defined literal operator.
12517 The suffix identifier is chained to the operator name identifier. */
12519 static tree
12520 cp_literal_operator_id (const char* name)
12522 tree identifier;
12523 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12524 + strlen (name) + 10);
12525 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12526 identifier = get_identifier (buffer);
12528 return identifier;
12531 /* Parse an operator.
12533 operator:
12534 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12535 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12536 || ++ -- , ->* -> () []
12538 GNU Extensions:
12540 operator:
12541 <? >? <?= >?=
12543 Returns an IDENTIFIER_NODE for the operator which is a
12544 human-readable spelling of the identifier, e.g., `operator +'. */
12546 static tree
12547 cp_parser_operator (cp_parser* parser)
12549 tree id = NULL_TREE;
12550 cp_token *token;
12551 bool bad_encoding_prefix = false;
12553 /* Peek at the next token. */
12554 token = cp_lexer_peek_token (parser->lexer);
12555 /* Figure out which operator we have. */
12556 switch (token->type)
12558 case CPP_KEYWORD:
12560 enum tree_code op;
12562 /* The keyword should be either `new' or `delete'. */
12563 if (token->keyword == RID_NEW)
12564 op = NEW_EXPR;
12565 else if (token->keyword == RID_DELETE)
12566 op = DELETE_EXPR;
12567 else
12568 break;
12570 /* Consume the `new' or `delete' token. */
12571 cp_lexer_consume_token (parser->lexer);
12573 /* Peek at the next token. */
12574 token = cp_lexer_peek_token (parser->lexer);
12575 /* If it's a `[' token then this is the array variant of the
12576 operator. */
12577 if (token->type == CPP_OPEN_SQUARE)
12579 /* Consume the `[' token. */
12580 cp_lexer_consume_token (parser->lexer);
12581 /* Look for the `]' token. */
12582 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12583 id = ansi_opname (op == NEW_EXPR
12584 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12586 /* Otherwise, we have the non-array variant. */
12587 else
12588 id = ansi_opname (op);
12590 return id;
12593 case CPP_PLUS:
12594 id = ansi_opname (PLUS_EXPR);
12595 break;
12597 case CPP_MINUS:
12598 id = ansi_opname (MINUS_EXPR);
12599 break;
12601 case CPP_MULT:
12602 id = ansi_opname (MULT_EXPR);
12603 break;
12605 case CPP_DIV:
12606 id = ansi_opname (TRUNC_DIV_EXPR);
12607 break;
12609 case CPP_MOD:
12610 id = ansi_opname (TRUNC_MOD_EXPR);
12611 break;
12613 case CPP_XOR:
12614 id = ansi_opname (BIT_XOR_EXPR);
12615 break;
12617 case CPP_AND:
12618 id = ansi_opname (BIT_AND_EXPR);
12619 break;
12621 case CPP_OR:
12622 id = ansi_opname (BIT_IOR_EXPR);
12623 break;
12625 case CPP_COMPL:
12626 id = ansi_opname (BIT_NOT_EXPR);
12627 break;
12629 case CPP_NOT:
12630 id = ansi_opname (TRUTH_NOT_EXPR);
12631 break;
12633 case CPP_EQ:
12634 id = ansi_assopname (NOP_EXPR);
12635 break;
12637 case CPP_LESS:
12638 id = ansi_opname (LT_EXPR);
12639 break;
12641 case CPP_GREATER:
12642 id = ansi_opname (GT_EXPR);
12643 break;
12645 case CPP_PLUS_EQ:
12646 id = ansi_assopname (PLUS_EXPR);
12647 break;
12649 case CPP_MINUS_EQ:
12650 id = ansi_assopname (MINUS_EXPR);
12651 break;
12653 case CPP_MULT_EQ:
12654 id = ansi_assopname (MULT_EXPR);
12655 break;
12657 case CPP_DIV_EQ:
12658 id = ansi_assopname (TRUNC_DIV_EXPR);
12659 break;
12661 case CPP_MOD_EQ:
12662 id = ansi_assopname (TRUNC_MOD_EXPR);
12663 break;
12665 case CPP_XOR_EQ:
12666 id = ansi_assopname (BIT_XOR_EXPR);
12667 break;
12669 case CPP_AND_EQ:
12670 id = ansi_assopname (BIT_AND_EXPR);
12671 break;
12673 case CPP_OR_EQ:
12674 id = ansi_assopname (BIT_IOR_EXPR);
12675 break;
12677 case CPP_LSHIFT:
12678 id = ansi_opname (LSHIFT_EXPR);
12679 break;
12681 case CPP_RSHIFT:
12682 id = ansi_opname (RSHIFT_EXPR);
12683 break;
12685 case CPP_LSHIFT_EQ:
12686 id = ansi_assopname (LSHIFT_EXPR);
12687 break;
12689 case CPP_RSHIFT_EQ:
12690 id = ansi_assopname (RSHIFT_EXPR);
12691 break;
12693 case CPP_EQ_EQ:
12694 id = ansi_opname (EQ_EXPR);
12695 break;
12697 case CPP_NOT_EQ:
12698 id = ansi_opname (NE_EXPR);
12699 break;
12701 case CPP_LESS_EQ:
12702 id = ansi_opname (LE_EXPR);
12703 break;
12705 case CPP_GREATER_EQ:
12706 id = ansi_opname (GE_EXPR);
12707 break;
12709 case CPP_AND_AND:
12710 id = ansi_opname (TRUTH_ANDIF_EXPR);
12711 break;
12713 case CPP_OR_OR:
12714 id = ansi_opname (TRUTH_ORIF_EXPR);
12715 break;
12717 case CPP_PLUS_PLUS:
12718 id = ansi_opname (POSTINCREMENT_EXPR);
12719 break;
12721 case CPP_MINUS_MINUS:
12722 id = ansi_opname (PREDECREMENT_EXPR);
12723 break;
12725 case CPP_COMMA:
12726 id = ansi_opname (COMPOUND_EXPR);
12727 break;
12729 case CPP_DEREF_STAR:
12730 id = ansi_opname (MEMBER_REF);
12731 break;
12733 case CPP_DEREF:
12734 id = ansi_opname (COMPONENT_REF);
12735 break;
12737 case CPP_OPEN_PAREN:
12738 /* Consume the `('. */
12739 cp_lexer_consume_token (parser->lexer);
12740 /* Look for the matching `)'. */
12741 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12742 return ansi_opname (CALL_EXPR);
12744 case CPP_OPEN_SQUARE:
12745 /* Consume the `['. */
12746 cp_lexer_consume_token (parser->lexer);
12747 /* Look for the matching `]'. */
12748 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12749 return ansi_opname (ARRAY_REF);
12751 case CPP_WSTRING:
12752 case CPP_STRING16:
12753 case CPP_STRING32:
12754 case CPP_UTF8STRING:
12755 bad_encoding_prefix = true;
12756 /* Fall through. */
12758 case CPP_STRING:
12759 if (cxx_dialect == cxx98)
12760 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12761 if (bad_encoding_prefix)
12763 error ("invalid encoding prefix in literal operator");
12764 return error_mark_node;
12766 if (TREE_STRING_LENGTH (token->u.value) > 2)
12768 error ("expected empty string after %<operator%> keyword");
12769 return error_mark_node;
12771 /* Consume the string. */
12772 cp_lexer_consume_token (parser->lexer);
12773 /* Look for the suffix identifier. */
12774 token = cp_lexer_peek_token (parser->lexer);
12775 if (token->type == CPP_NAME)
12777 id = cp_parser_identifier (parser);
12778 if (id != error_mark_node)
12780 const char *name = IDENTIFIER_POINTER (id);
12781 return cp_literal_operator_id (name);
12784 else if (token->type == CPP_KEYWORD)
12786 error ("unexpected keyword;"
12787 " remove space between quotes and suffix identifier");
12788 return error_mark_node;
12790 else
12792 error ("expected suffix identifier");
12793 return error_mark_node;
12796 case CPP_WSTRING_USERDEF:
12797 case CPP_STRING16_USERDEF:
12798 case CPP_STRING32_USERDEF:
12799 case CPP_UTF8STRING_USERDEF:
12800 bad_encoding_prefix = true;
12801 /* Fall through. */
12803 case CPP_STRING_USERDEF:
12804 if (cxx_dialect == cxx98)
12805 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12806 if (bad_encoding_prefix)
12808 error ("invalid encoding prefix in literal operator");
12809 return error_mark_node;
12812 tree string_tree = USERDEF_LITERAL_VALUE (token->u.value);
12813 if (TREE_STRING_LENGTH (string_tree) > 2)
12815 error ("expected empty string after %<operator%> keyword");
12816 return error_mark_node;
12818 id = USERDEF_LITERAL_SUFFIX_ID (token->u.value);
12819 /* Consume the user-defined string literal. */
12820 cp_lexer_consume_token (parser->lexer);
12821 if (id != error_mark_node)
12823 const char *name = IDENTIFIER_POINTER (id);
12824 return cp_literal_operator_id (name);
12826 else
12827 return error_mark_node;
12830 default:
12831 /* Anything else is an error. */
12832 break;
12835 /* If we have selected an identifier, we need to consume the
12836 operator token. */
12837 if (id)
12838 cp_lexer_consume_token (parser->lexer);
12839 /* Otherwise, no valid operator name was present. */
12840 else
12842 cp_parser_error (parser, "expected operator");
12843 id = error_mark_node;
12846 return id;
12849 /* Parse a template-declaration.
12851 template-declaration:
12852 export [opt] template < template-parameter-list > declaration
12854 If MEMBER_P is TRUE, this template-declaration occurs within a
12855 class-specifier.
12857 The grammar rule given by the standard isn't correct. What
12858 is really meant is:
12860 template-declaration:
12861 export [opt] template-parameter-list-seq
12862 decl-specifier-seq [opt] init-declarator [opt] ;
12863 export [opt] template-parameter-list-seq
12864 function-definition
12866 template-parameter-list-seq:
12867 template-parameter-list-seq [opt]
12868 template < template-parameter-list > */
12870 static void
12871 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12873 /* Check for `export'. */
12874 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12876 /* Consume the `export' token. */
12877 cp_lexer_consume_token (parser->lexer);
12878 /* Warn that we do not support `export'. */
12879 warning (0, "keyword %<export%> not implemented, and will be ignored");
12882 cp_parser_template_declaration_after_export (parser, member_p);
12885 /* Parse a template-parameter-list.
12887 template-parameter-list:
12888 template-parameter
12889 template-parameter-list , template-parameter
12891 Returns a TREE_LIST. Each node represents a template parameter.
12892 The nodes are connected via their TREE_CHAINs. */
12894 static tree
12895 cp_parser_template_parameter_list (cp_parser* parser)
12897 tree parameter_list = NULL_TREE;
12899 begin_template_parm_list ();
12901 /* The loop below parses the template parms. We first need to know
12902 the total number of template parms to be able to compute proper
12903 canonical types of each dependent type. So after the loop, when
12904 we know the total number of template parms,
12905 end_template_parm_list computes the proper canonical types and
12906 fixes up the dependent types accordingly. */
12907 while (true)
12909 tree parameter;
12910 bool is_non_type;
12911 bool is_parameter_pack;
12912 location_t parm_loc;
12914 /* Parse the template-parameter. */
12915 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12916 parameter = cp_parser_template_parameter (parser,
12917 &is_non_type,
12918 &is_parameter_pack);
12919 /* Add it to the list. */
12920 if (parameter != error_mark_node)
12921 parameter_list = process_template_parm (parameter_list,
12922 parm_loc,
12923 parameter,
12924 is_non_type,
12925 is_parameter_pack);
12926 else
12928 tree err_parm = build_tree_list (parameter, parameter);
12929 parameter_list = chainon (parameter_list, err_parm);
12932 /* If the next token is not a `,', we're done. */
12933 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12934 break;
12935 /* Otherwise, consume the `,' token. */
12936 cp_lexer_consume_token (parser->lexer);
12939 return end_template_parm_list (parameter_list);
12942 /* Parse a template-parameter.
12944 template-parameter:
12945 type-parameter
12946 parameter-declaration
12948 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12949 the parameter. The TREE_PURPOSE is the default value, if any.
12950 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12951 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12952 set to true iff this parameter is a parameter pack. */
12954 static tree
12955 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12956 bool *is_parameter_pack)
12958 cp_token *token;
12959 cp_parameter_declarator *parameter_declarator;
12960 cp_declarator *id_declarator;
12961 tree parm;
12963 /* Assume it is a type parameter or a template parameter. */
12964 *is_non_type = false;
12965 /* Assume it not a parameter pack. */
12966 *is_parameter_pack = false;
12967 /* Peek at the next token. */
12968 token = cp_lexer_peek_token (parser->lexer);
12969 /* If it is `class' or `template', we have a type-parameter. */
12970 if (token->keyword == RID_TEMPLATE)
12971 return cp_parser_type_parameter (parser, is_parameter_pack);
12972 /* If it is `class' or `typename' we do not know yet whether it is a
12973 type parameter or a non-type parameter. Consider:
12975 template <typename T, typename T::X X> ...
12979 template <class C, class D*> ...
12981 Here, the first parameter is a type parameter, and the second is
12982 a non-type parameter. We can tell by looking at the token after
12983 the identifier -- if it is a `,', `=', or `>' then we have a type
12984 parameter. */
12985 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12987 /* Peek at the token after `class' or `typename'. */
12988 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12989 /* If it's an ellipsis, we have a template type parameter
12990 pack. */
12991 if (token->type == CPP_ELLIPSIS)
12992 return cp_parser_type_parameter (parser, is_parameter_pack);
12993 /* If it's an identifier, skip it. */
12994 if (token->type == CPP_NAME)
12995 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12996 /* Now, see if the token looks like the end of a template
12997 parameter. */
12998 if (token->type == CPP_COMMA
12999 || token->type == CPP_EQ
13000 || token->type == CPP_GREATER)
13001 return cp_parser_type_parameter (parser, is_parameter_pack);
13004 /* Otherwise, it is a non-type parameter.
13006 [temp.param]
13008 When parsing a default template-argument for a non-type
13009 template-parameter, the first non-nested `>' is taken as the end
13010 of the template parameter-list rather than a greater-than
13011 operator. */
13012 *is_non_type = true;
13013 parameter_declarator
13014 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13015 /*parenthesized_p=*/NULL);
13017 if (!parameter_declarator)
13018 return error_mark_node;
13020 /* If the parameter declaration is marked as a parameter pack, set
13021 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13022 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13023 grokdeclarator. */
13024 if (parameter_declarator->declarator
13025 && parameter_declarator->declarator->parameter_pack_p)
13027 *is_parameter_pack = true;
13028 parameter_declarator->declarator->parameter_pack_p = false;
13031 if (parameter_declarator->default_argument)
13033 /* Can happen in some cases of erroneous input (c++/34892). */
13034 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13035 /* Consume the `...' for better error recovery. */
13036 cp_lexer_consume_token (parser->lexer);
13038 /* If the next token is an ellipsis, and we don't already have it
13039 marked as a parameter pack, then we have a parameter pack (that
13040 has no declarator). */
13041 else if (!*is_parameter_pack
13042 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13043 && (declarator_can_be_parameter_pack
13044 (parameter_declarator->declarator)))
13046 /* Consume the `...'. */
13047 cp_lexer_consume_token (parser->lexer);
13048 maybe_warn_variadic_templates ();
13050 *is_parameter_pack = true;
13052 /* We might end up with a pack expansion as the type of the non-type
13053 template parameter, in which case this is a non-type template
13054 parameter pack. */
13055 else if (parameter_declarator->decl_specifiers.type
13056 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13058 *is_parameter_pack = true;
13059 parameter_declarator->decl_specifiers.type =
13060 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13063 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13065 /* Parameter packs cannot have default arguments. However, a
13066 user may try to do so, so we'll parse them and give an
13067 appropriate diagnostic here. */
13069 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13071 /* Find the name of the parameter pack. */
13072 id_declarator = parameter_declarator->declarator;
13073 while (id_declarator && id_declarator->kind != cdk_id)
13074 id_declarator = id_declarator->declarator;
13076 if (id_declarator && id_declarator->kind == cdk_id)
13077 error_at (start_token->location,
13078 "template parameter pack %qD cannot have a default argument",
13079 id_declarator->u.id.unqualified_name);
13080 else
13081 error_at (start_token->location,
13082 "template parameter pack cannot have a default argument");
13084 /* Parse the default argument, but throw away the result. */
13085 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13088 parm = grokdeclarator (parameter_declarator->declarator,
13089 &parameter_declarator->decl_specifiers,
13090 TPARM, /*initialized=*/0,
13091 /*attrlist=*/NULL);
13092 if (parm == error_mark_node)
13093 return error_mark_node;
13095 return build_tree_list (parameter_declarator->default_argument, parm);
13098 /* Parse a type-parameter.
13100 type-parameter:
13101 class identifier [opt]
13102 class identifier [opt] = type-id
13103 typename identifier [opt]
13104 typename identifier [opt] = type-id
13105 template < template-parameter-list > class identifier [opt]
13106 template < template-parameter-list > class identifier [opt]
13107 = id-expression
13109 GNU Extension (variadic templates):
13111 type-parameter:
13112 class ... identifier [opt]
13113 typename ... identifier [opt]
13115 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13116 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13117 the declaration of the parameter.
13119 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13121 static tree
13122 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13124 cp_token *token;
13125 tree parameter;
13127 /* Look for a keyword to tell us what kind of parameter this is. */
13128 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13129 if (!token)
13130 return error_mark_node;
13132 switch (token->keyword)
13134 case RID_CLASS:
13135 case RID_TYPENAME:
13137 tree identifier;
13138 tree default_argument;
13140 /* If the next token is an ellipsis, we have a template
13141 argument pack. */
13142 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13144 /* Consume the `...' token. */
13145 cp_lexer_consume_token (parser->lexer);
13146 maybe_warn_variadic_templates ();
13148 *is_parameter_pack = true;
13151 /* If the next token is an identifier, then it names the
13152 parameter. */
13153 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13154 identifier = cp_parser_identifier (parser);
13155 else
13156 identifier = NULL_TREE;
13158 /* Create the parameter. */
13159 parameter = finish_template_type_parm (class_type_node, identifier);
13161 /* If the next token is an `=', we have a default argument. */
13162 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13164 /* Consume the `=' token. */
13165 cp_lexer_consume_token (parser->lexer);
13166 /* Parse the default-argument. */
13167 push_deferring_access_checks (dk_no_deferred);
13168 default_argument = cp_parser_type_id (parser);
13170 /* Template parameter packs cannot have default
13171 arguments. */
13172 if (*is_parameter_pack)
13174 if (identifier)
13175 error_at (token->location,
13176 "template parameter pack %qD cannot have a "
13177 "default argument", identifier);
13178 else
13179 error_at (token->location,
13180 "template parameter packs cannot have "
13181 "default arguments");
13182 default_argument = NULL_TREE;
13184 pop_deferring_access_checks ();
13186 else
13187 default_argument = NULL_TREE;
13189 /* Create the combined representation of the parameter and the
13190 default argument. */
13191 parameter = build_tree_list (default_argument, parameter);
13193 break;
13195 case RID_TEMPLATE:
13197 tree identifier;
13198 tree default_argument;
13200 /* Look for the `<'. */
13201 cp_parser_require (parser, CPP_LESS, RT_LESS);
13202 /* Parse the template-parameter-list. */
13203 cp_parser_template_parameter_list (parser);
13204 /* Look for the `>'. */
13205 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13206 /* Look for the `class' keyword. */
13207 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
13208 /* If the next token is an ellipsis, we have a template
13209 argument pack. */
13210 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13212 /* Consume the `...' token. */
13213 cp_lexer_consume_token (parser->lexer);
13214 maybe_warn_variadic_templates ();
13216 *is_parameter_pack = true;
13218 /* If the next token is an `=', then there is a
13219 default-argument. If the next token is a `>', we are at
13220 the end of the parameter-list. If the next token is a `,',
13221 then we are at the end of this parameter. */
13222 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13223 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13224 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13226 identifier = cp_parser_identifier (parser);
13227 /* Treat invalid names as if the parameter were nameless. */
13228 if (identifier == error_mark_node)
13229 identifier = NULL_TREE;
13231 else
13232 identifier = NULL_TREE;
13234 /* Create the template parameter. */
13235 parameter = finish_template_template_parm (class_type_node,
13236 identifier);
13238 /* If the next token is an `=', then there is a
13239 default-argument. */
13240 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13242 bool is_template;
13244 /* Consume the `='. */
13245 cp_lexer_consume_token (parser->lexer);
13246 /* Parse the id-expression. */
13247 push_deferring_access_checks (dk_no_deferred);
13248 /* save token before parsing the id-expression, for error
13249 reporting */
13250 token = cp_lexer_peek_token (parser->lexer);
13251 default_argument
13252 = cp_parser_id_expression (parser,
13253 /*template_keyword_p=*/false,
13254 /*check_dependency_p=*/true,
13255 /*template_p=*/&is_template,
13256 /*declarator_p=*/false,
13257 /*optional_p=*/false);
13258 if (TREE_CODE (default_argument) == TYPE_DECL)
13259 /* If the id-expression was a template-id that refers to
13260 a template-class, we already have the declaration here,
13261 so no further lookup is needed. */
13263 else
13264 /* Look up the name. */
13265 default_argument
13266 = cp_parser_lookup_name (parser, default_argument,
13267 none_type,
13268 /*is_template=*/is_template,
13269 /*is_namespace=*/false,
13270 /*check_dependency=*/true,
13271 /*ambiguous_decls=*/NULL,
13272 token->location);
13273 /* See if the default argument is valid. */
13274 default_argument
13275 = check_template_template_default_arg (default_argument);
13277 /* Template parameter packs cannot have default
13278 arguments. */
13279 if (*is_parameter_pack)
13281 if (identifier)
13282 error_at (token->location,
13283 "template parameter pack %qD cannot "
13284 "have a default argument",
13285 identifier);
13286 else
13287 error_at (token->location, "template parameter packs cannot "
13288 "have default arguments");
13289 default_argument = NULL_TREE;
13291 pop_deferring_access_checks ();
13293 else
13294 default_argument = NULL_TREE;
13296 /* Create the combined representation of the parameter and the
13297 default argument. */
13298 parameter = build_tree_list (default_argument, parameter);
13300 break;
13302 default:
13303 gcc_unreachable ();
13304 break;
13307 return parameter;
13310 /* Parse a template-id.
13312 template-id:
13313 template-name < template-argument-list [opt] >
13315 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13316 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13317 returned. Otherwise, if the template-name names a function, or set
13318 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13319 names a class, returns a TYPE_DECL for the specialization.
13321 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13322 uninstantiated templates. */
13324 static tree
13325 cp_parser_template_id (cp_parser *parser,
13326 bool template_keyword_p,
13327 bool check_dependency_p,
13328 enum tag_types tag_type,
13329 bool is_declaration)
13331 int i;
13332 tree templ;
13333 tree arguments;
13334 tree template_id;
13335 cp_token_position start_of_id = 0;
13336 deferred_access_check *chk;
13337 vec<deferred_access_check, va_gc> *access_check;
13338 cp_token *next_token = NULL, *next_token_2 = NULL;
13339 bool is_identifier;
13341 /* If the next token corresponds to a template-id, there is no need
13342 to reparse it. */
13343 next_token = cp_lexer_peek_token (parser->lexer);
13344 if (next_token->type == CPP_TEMPLATE_ID)
13346 struct tree_check *check_value;
13348 /* Get the stored value. */
13349 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13350 /* Perform any access checks that were deferred. */
13351 access_check = check_value->checks;
13352 if (access_check)
13354 FOR_EACH_VEC_ELT (*access_check, i, chk)
13355 perform_or_defer_access_check (chk->binfo,
13356 chk->decl,
13357 chk->diag_decl,
13358 tf_warning_or_error);
13360 /* Return the stored value. */
13361 return check_value->value;
13364 /* Avoid performing name lookup if there is no possibility of
13365 finding a template-id. */
13366 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13367 || (next_token->type == CPP_NAME
13368 && !cp_parser_nth_token_starts_template_argument_list_p
13369 (parser, 2)))
13371 cp_parser_error (parser, "expected template-id");
13372 return error_mark_node;
13375 /* Remember where the template-id starts. */
13376 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13377 start_of_id = cp_lexer_token_position (parser->lexer, false);
13379 push_deferring_access_checks (dk_deferred);
13381 /* Parse the template-name. */
13382 is_identifier = false;
13383 templ = cp_parser_template_name (parser, template_keyword_p,
13384 check_dependency_p,
13385 is_declaration,
13386 tag_type,
13387 &is_identifier);
13388 if (templ == error_mark_node || is_identifier)
13390 pop_deferring_access_checks ();
13391 return templ;
13394 /* If we find the sequence `[:' after a template-name, it's probably
13395 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13396 parse correctly the argument list. */
13397 next_token = cp_lexer_peek_token (parser->lexer);
13398 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13399 if (next_token->type == CPP_OPEN_SQUARE
13400 && next_token->flags & DIGRAPH
13401 && next_token_2->type == CPP_COLON
13402 && !(next_token_2->flags & PREV_WHITE))
13404 cp_parser_parse_tentatively (parser);
13405 /* Change `:' into `::'. */
13406 next_token_2->type = CPP_SCOPE;
13407 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13408 CPP_LESS. */
13409 cp_lexer_consume_token (parser->lexer);
13411 /* Parse the arguments. */
13412 arguments = cp_parser_enclosed_template_argument_list (parser);
13413 if (!cp_parser_parse_definitely (parser))
13415 /* If we couldn't parse an argument list, then we revert our changes
13416 and return simply an error. Maybe this is not a template-id
13417 after all. */
13418 next_token_2->type = CPP_COLON;
13419 cp_parser_error (parser, "expected %<<%>");
13420 pop_deferring_access_checks ();
13421 return error_mark_node;
13423 /* Otherwise, emit an error about the invalid digraph, but continue
13424 parsing because we got our argument list. */
13425 if (permerror (next_token->location,
13426 "%<<::%> cannot begin a template-argument list"))
13428 static bool hint = false;
13429 inform (next_token->location,
13430 "%<<:%> is an alternate spelling for %<[%>."
13431 " Insert whitespace between %<<%> and %<::%>");
13432 if (!hint && !flag_permissive)
13434 inform (next_token->location, "(if you use %<-fpermissive%> "
13435 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13436 "accept your code)");
13437 hint = true;
13441 else
13443 /* Look for the `<' that starts the template-argument-list. */
13444 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13446 pop_deferring_access_checks ();
13447 return error_mark_node;
13449 /* Parse the arguments. */
13450 arguments = cp_parser_enclosed_template_argument_list (parser);
13453 /* Build a representation of the specialization. */
13454 if (identifier_p (templ))
13455 template_id = build_min_nt_loc (next_token->location,
13456 TEMPLATE_ID_EXPR,
13457 templ, arguments);
13458 else if (DECL_TYPE_TEMPLATE_P (templ)
13459 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13461 bool entering_scope;
13462 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13463 template (rather than some instantiation thereof) only if
13464 is not nested within some other construct. For example, in
13465 "template <typename T> void f(T) { A<T>::", A<T> is just an
13466 instantiation of A. */
13467 entering_scope = (template_parm_scope_p ()
13468 && cp_lexer_next_token_is (parser->lexer,
13469 CPP_SCOPE));
13470 template_id
13471 = finish_template_type (templ, arguments, entering_scope);
13473 else
13475 /* If it's not a class-template or a template-template, it should be
13476 a function-template. */
13477 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13478 || TREE_CODE (templ) == OVERLOAD
13479 || BASELINK_P (templ)));
13481 template_id = lookup_template_function (templ, arguments);
13484 /* If parsing tentatively, replace the sequence of tokens that makes
13485 up the template-id with a CPP_TEMPLATE_ID token. That way,
13486 should we re-parse the token stream, we will not have to repeat
13487 the effort required to do the parse, nor will we issue duplicate
13488 error messages about problems during instantiation of the
13489 template. */
13490 if (start_of_id
13491 /* Don't do this if we had a parse error in a declarator; re-parsing
13492 might succeed if a name changes meaning (60361). */
13493 && !(cp_parser_error_occurred (parser)
13494 && cp_parser_parsing_tentatively (parser)
13495 && parser->in_declarator_p))
13497 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13499 /* Reset the contents of the START_OF_ID token. */
13500 token->type = CPP_TEMPLATE_ID;
13501 /* Retrieve any deferred checks. Do not pop this access checks yet
13502 so the memory will not be reclaimed during token replacing below. */
13503 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
13504 token->u.tree_check_value->value = template_id;
13505 token->u.tree_check_value->checks = get_deferred_access_checks ();
13506 token->keyword = RID_MAX;
13508 /* Purge all subsequent tokens. */
13509 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13511 /* ??? Can we actually assume that, if template_id ==
13512 error_mark_node, we will have issued a diagnostic to the
13513 user, as opposed to simply marking the tentative parse as
13514 failed? */
13515 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13516 error_at (token->location, "parse error in template argument list");
13519 pop_to_parent_deferring_access_checks ();
13520 return template_id;
13523 /* Parse a template-name.
13525 template-name:
13526 identifier
13528 The standard should actually say:
13530 template-name:
13531 identifier
13532 operator-function-id
13534 A defect report has been filed about this issue.
13536 A conversion-function-id cannot be a template name because they cannot
13537 be part of a template-id. In fact, looking at this code:
13539 a.operator K<int>()
13541 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13542 It is impossible to call a templated conversion-function-id with an
13543 explicit argument list, since the only allowed template parameter is
13544 the type to which it is converting.
13546 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13547 `template' keyword, in a construction like:
13549 T::template f<3>()
13551 In that case `f' is taken to be a template-name, even though there
13552 is no way of knowing for sure.
13554 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13555 name refers to a set of overloaded functions, at least one of which
13556 is a template, or an IDENTIFIER_NODE with the name of the template,
13557 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13558 names are looked up inside uninstantiated templates. */
13560 static tree
13561 cp_parser_template_name (cp_parser* parser,
13562 bool template_keyword_p,
13563 bool check_dependency_p,
13564 bool is_declaration,
13565 enum tag_types tag_type,
13566 bool *is_identifier)
13568 tree identifier;
13569 tree decl;
13570 tree fns;
13571 cp_token *token = cp_lexer_peek_token (parser->lexer);
13573 /* If the next token is `operator', then we have either an
13574 operator-function-id or a conversion-function-id. */
13575 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13577 /* We don't know whether we're looking at an
13578 operator-function-id or a conversion-function-id. */
13579 cp_parser_parse_tentatively (parser);
13580 /* Try an operator-function-id. */
13581 identifier = cp_parser_operator_function_id (parser);
13582 /* If that didn't work, try a conversion-function-id. */
13583 if (!cp_parser_parse_definitely (parser))
13585 cp_parser_error (parser, "expected template-name");
13586 return error_mark_node;
13589 /* Look for the identifier. */
13590 else
13591 identifier = cp_parser_identifier (parser);
13593 /* If we didn't find an identifier, we don't have a template-id. */
13594 if (identifier == error_mark_node)
13595 return error_mark_node;
13597 /* If the name immediately followed the `template' keyword, then it
13598 is a template-name. However, if the next token is not `<', then
13599 we do not treat it as a template-name, since it is not being used
13600 as part of a template-id. This enables us to handle constructs
13601 like:
13603 template <typename T> struct S { S(); };
13604 template <typename T> S<T>::S();
13606 correctly. We would treat `S' as a template -- if it were `S<T>'
13607 -- but we do not if there is no `<'. */
13609 if (processing_template_decl
13610 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13612 /* In a declaration, in a dependent context, we pretend that the
13613 "template" keyword was present in order to improve error
13614 recovery. For example, given:
13616 template <typename T> void f(T::X<int>);
13618 we want to treat "X<int>" as a template-id. */
13619 if (is_declaration
13620 && !template_keyword_p
13621 && parser->scope && TYPE_P (parser->scope)
13622 && check_dependency_p
13623 && dependent_scope_p (parser->scope)
13624 /* Do not do this for dtors (or ctors), since they never
13625 need the template keyword before their name. */
13626 && !constructor_name_p (identifier, parser->scope))
13628 cp_token_position start = 0;
13630 /* Explain what went wrong. */
13631 error_at (token->location, "non-template %qD used as template",
13632 identifier);
13633 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13634 parser->scope, identifier);
13635 /* If parsing tentatively, find the location of the "<" token. */
13636 if (cp_parser_simulate_error (parser))
13637 start = cp_lexer_token_position (parser->lexer, true);
13638 /* Parse the template arguments so that we can issue error
13639 messages about them. */
13640 cp_lexer_consume_token (parser->lexer);
13641 cp_parser_enclosed_template_argument_list (parser);
13642 /* Skip tokens until we find a good place from which to
13643 continue parsing. */
13644 cp_parser_skip_to_closing_parenthesis (parser,
13645 /*recovering=*/true,
13646 /*or_comma=*/true,
13647 /*consume_paren=*/false);
13648 /* If parsing tentatively, permanently remove the
13649 template argument list. That will prevent duplicate
13650 error messages from being issued about the missing
13651 "template" keyword. */
13652 if (start)
13653 cp_lexer_purge_tokens_after (parser->lexer, start);
13654 if (is_identifier)
13655 *is_identifier = true;
13656 return identifier;
13659 /* If the "template" keyword is present, then there is generally
13660 no point in doing name-lookup, so we just return IDENTIFIER.
13661 But, if the qualifying scope is non-dependent then we can
13662 (and must) do name-lookup normally. */
13663 if (template_keyword_p
13664 && (!parser->scope
13665 || (TYPE_P (parser->scope)
13666 && dependent_type_p (parser->scope))))
13667 return identifier;
13670 /* Look up the name. */
13671 decl = cp_parser_lookup_name (parser, identifier,
13672 tag_type,
13673 /*is_template=*/true,
13674 /*is_namespace=*/false,
13675 check_dependency_p,
13676 /*ambiguous_decls=*/NULL,
13677 token->location);
13679 /* If DECL is a template, then the name was a template-name. */
13680 if (TREE_CODE (decl) == TEMPLATE_DECL)
13682 else
13684 tree fn = NULL_TREE;
13686 /* The standard does not explicitly indicate whether a name that
13687 names a set of overloaded declarations, some of which are
13688 templates, is a template-name. However, such a name should
13689 be a template-name; otherwise, there is no way to form a
13690 template-id for the overloaded templates. */
13691 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13692 if (TREE_CODE (fns) == OVERLOAD)
13693 for (fn = fns; fn; fn = OVL_NEXT (fn))
13694 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13695 break;
13697 if (!fn)
13699 /* The name does not name a template. */
13700 cp_parser_error (parser, "expected template-name");
13701 return error_mark_node;
13705 /* If DECL is dependent, and refers to a function, then just return
13706 its name; we will look it up again during template instantiation. */
13707 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13709 tree scope = ovl_scope (decl);
13710 if (TYPE_P (scope) && dependent_type_p (scope))
13711 return identifier;
13714 return decl;
13717 /* Parse a template-argument-list.
13719 template-argument-list:
13720 template-argument ... [opt]
13721 template-argument-list , template-argument ... [opt]
13723 Returns a TREE_VEC containing the arguments. */
13725 static tree
13726 cp_parser_template_argument_list (cp_parser* parser)
13728 tree fixed_args[10];
13729 unsigned n_args = 0;
13730 unsigned alloced = 10;
13731 tree *arg_ary = fixed_args;
13732 tree vec;
13733 bool saved_in_template_argument_list_p;
13734 bool saved_ice_p;
13735 bool saved_non_ice_p;
13737 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13738 parser->in_template_argument_list_p = true;
13739 /* Even if the template-id appears in an integral
13740 constant-expression, the contents of the argument list do
13741 not. */
13742 saved_ice_p = parser->integral_constant_expression_p;
13743 parser->integral_constant_expression_p = false;
13744 saved_non_ice_p = parser->non_integral_constant_expression_p;
13745 parser->non_integral_constant_expression_p = false;
13747 /* Parse the arguments. */
13750 tree argument;
13752 if (n_args)
13753 /* Consume the comma. */
13754 cp_lexer_consume_token (parser->lexer);
13756 /* Parse the template-argument. */
13757 argument = cp_parser_template_argument (parser);
13759 /* If the next token is an ellipsis, we're expanding a template
13760 argument pack. */
13761 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13763 if (argument == error_mark_node)
13765 cp_token *token = cp_lexer_peek_token (parser->lexer);
13766 error_at (token->location,
13767 "expected parameter pack before %<...%>");
13769 /* Consume the `...' token. */
13770 cp_lexer_consume_token (parser->lexer);
13772 /* Make the argument into a TYPE_PACK_EXPANSION or
13773 EXPR_PACK_EXPANSION. */
13774 argument = make_pack_expansion (argument);
13777 if (n_args == alloced)
13779 alloced *= 2;
13781 if (arg_ary == fixed_args)
13783 arg_ary = XNEWVEC (tree, alloced);
13784 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13786 else
13787 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13789 arg_ary[n_args++] = argument;
13791 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13793 vec = make_tree_vec (n_args);
13795 while (n_args--)
13796 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13798 if (arg_ary != fixed_args)
13799 free (arg_ary);
13800 parser->non_integral_constant_expression_p = saved_non_ice_p;
13801 parser->integral_constant_expression_p = saved_ice_p;
13802 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13803 #ifdef ENABLE_CHECKING
13804 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13805 #endif
13806 return vec;
13809 /* Parse a template-argument.
13811 template-argument:
13812 assignment-expression
13813 type-id
13814 id-expression
13816 The representation is that of an assignment-expression, type-id, or
13817 id-expression -- except that the qualified id-expression is
13818 evaluated, so that the value returned is either a DECL or an
13819 OVERLOAD.
13821 Although the standard says "assignment-expression", it forbids
13822 throw-expressions or assignments in the template argument.
13823 Therefore, we use "conditional-expression" instead. */
13825 static tree
13826 cp_parser_template_argument (cp_parser* parser)
13828 tree argument;
13829 bool template_p;
13830 bool address_p;
13831 bool maybe_type_id = false;
13832 cp_token *token = NULL, *argument_start_token = NULL;
13833 location_t loc = 0;
13834 cp_id_kind idk;
13836 /* There's really no way to know what we're looking at, so we just
13837 try each alternative in order.
13839 [temp.arg]
13841 In a template-argument, an ambiguity between a type-id and an
13842 expression is resolved to a type-id, regardless of the form of
13843 the corresponding template-parameter.
13845 Therefore, we try a type-id first. */
13846 cp_parser_parse_tentatively (parser);
13847 argument = cp_parser_template_type_arg (parser);
13848 /* If there was no error parsing the type-id but the next token is a
13849 '>>', our behavior depends on which dialect of C++ we're
13850 parsing. In C++98, we probably found a typo for '> >'. But there
13851 are type-id which are also valid expressions. For instance:
13853 struct X { int operator >> (int); };
13854 template <int V> struct Foo {};
13855 Foo<X () >> 5> r;
13857 Here 'X()' is a valid type-id of a function type, but the user just
13858 wanted to write the expression "X() >> 5". Thus, we remember that we
13859 found a valid type-id, but we still try to parse the argument as an
13860 expression to see what happens.
13862 In C++0x, the '>>' will be considered two separate '>'
13863 tokens. */
13864 if (!cp_parser_error_occurred (parser)
13865 && cxx_dialect == cxx98
13866 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13868 maybe_type_id = true;
13869 cp_parser_abort_tentative_parse (parser);
13871 else
13873 /* If the next token isn't a `,' or a `>', then this argument wasn't
13874 really finished. This means that the argument is not a valid
13875 type-id. */
13876 if (!cp_parser_next_token_ends_template_argument_p (parser))
13877 cp_parser_error (parser, "expected template-argument");
13878 /* If that worked, we're done. */
13879 if (cp_parser_parse_definitely (parser))
13880 return argument;
13882 /* We're still not sure what the argument will be. */
13883 cp_parser_parse_tentatively (parser);
13884 /* Try a template. */
13885 argument_start_token = cp_lexer_peek_token (parser->lexer);
13886 argument = cp_parser_id_expression (parser,
13887 /*template_keyword_p=*/false,
13888 /*check_dependency_p=*/true,
13889 &template_p,
13890 /*declarator_p=*/false,
13891 /*optional_p=*/false);
13892 /* If the next token isn't a `,' or a `>', then this argument wasn't
13893 really finished. */
13894 if (!cp_parser_next_token_ends_template_argument_p (parser))
13895 cp_parser_error (parser, "expected template-argument");
13896 if (!cp_parser_error_occurred (parser))
13898 /* Figure out what is being referred to. If the id-expression
13899 was for a class template specialization, then we will have a
13900 TYPE_DECL at this point. There is no need to do name lookup
13901 at this point in that case. */
13902 if (TREE_CODE (argument) != TYPE_DECL)
13903 argument = cp_parser_lookup_name (parser, argument,
13904 none_type,
13905 /*is_template=*/template_p,
13906 /*is_namespace=*/false,
13907 /*check_dependency=*/true,
13908 /*ambiguous_decls=*/NULL,
13909 argument_start_token->location);
13910 if (TREE_CODE (argument) != TEMPLATE_DECL
13911 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13912 cp_parser_error (parser, "expected template-name");
13914 if (cp_parser_parse_definitely (parser))
13915 return argument;
13916 /* It must be a non-type argument. There permitted cases are given
13917 in [temp.arg.nontype]:
13919 -- an integral constant-expression of integral or enumeration
13920 type; or
13922 -- the name of a non-type template-parameter; or
13924 -- the name of an object or function with external linkage...
13926 -- the address of an object or function with external linkage...
13928 -- a pointer to member... */
13929 /* Look for a non-type template parameter. */
13930 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13932 cp_parser_parse_tentatively (parser);
13933 argument = cp_parser_primary_expression (parser,
13934 /*address_p=*/false,
13935 /*cast_p=*/false,
13936 /*template_arg_p=*/true,
13937 &idk);
13938 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13939 || !cp_parser_next_token_ends_template_argument_p (parser))
13940 cp_parser_simulate_error (parser);
13941 if (cp_parser_parse_definitely (parser))
13942 return argument;
13945 /* If the next token is "&", the argument must be the address of an
13946 object or function with external linkage. */
13947 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13948 if (address_p)
13950 loc = cp_lexer_peek_token (parser->lexer)->location;
13951 cp_lexer_consume_token (parser->lexer);
13953 /* See if we might have an id-expression. */
13954 token = cp_lexer_peek_token (parser->lexer);
13955 if (token->type == CPP_NAME
13956 || token->keyword == RID_OPERATOR
13957 || token->type == CPP_SCOPE
13958 || token->type == CPP_TEMPLATE_ID
13959 || token->type == CPP_NESTED_NAME_SPECIFIER)
13961 cp_parser_parse_tentatively (parser);
13962 argument = cp_parser_primary_expression (parser,
13963 address_p,
13964 /*cast_p=*/false,
13965 /*template_arg_p=*/true,
13966 &idk);
13967 if (cp_parser_error_occurred (parser)
13968 || !cp_parser_next_token_ends_template_argument_p (parser))
13969 cp_parser_abort_tentative_parse (parser);
13970 else
13972 tree probe;
13974 if (INDIRECT_REF_P (argument))
13976 /* Strip the dereference temporarily. */
13977 gcc_assert (REFERENCE_REF_P (argument));
13978 argument = TREE_OPERAND (argument, 0);
13981 /* If we're in a template, we represent a qualified-id referring
13982 to a static data member as a SCOPE_REF even if the scope isn't
13983 dependent so that we can check access control later. */
13984 probe = argument;
13985 if (TREE_CODE (probe) == SCOPE_REF)
13986 probe = TREE_OPERAND (probe, 1);
13987 if (VAR_P (probe))
13989 /* A variable without external linkage might still be a
13990 valid constant-expression, so no error is issued here
13991 if the external-linkage check fails. */
13992 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13993 cp_parser_simulate_error (parser);
13995 else if (is_overloaded_fn (argument))
13996 /* All overloaded functions are allowed; if the external
13997 linkage test does not pass, an error will be issued
13998 later. */
14000 else if (address_p
14001 && (TREE_CODE (argument) == OFFSET_REF
14002 || TREE_CODE (argument) == SCOPE_REF))
14003 /* A pointer-to-member. */
14005 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14007 else
14008 cp_parser_simulate_error (parser);
14010 if (cp_parser_parse_definitely (parser))
14012 if (address_p)
14013 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14014 tf_warning_or_error);
14015 else
14016 argument = convert_from_reference (argument);
14017 return argument;
14021 /* If the argument started with "&", there are no other valid
14022 alternatives at this point. */
14023 if (address_p)
14025 cp_parser_error (parser, "invalid non-type template argument");
14026 return error_mark_node;
14029 /* If the argument wasn't successfully parsed as a type-id followed
14030 by '>>', the argument can only be a constant expression now.
14031 Otherwise, we try parsing the constant-expression tentatively,
14032 because the argument could really be a type-id. */
14033 if (maybe_type_id)
14034 cp_parser_parse_tentatively (parser);
14035 argument = cp_parser_constant_expression (parser,
14036 /*allow_non_constant_p=*/false,
14037 /*non_constant_p=*/NULL);
14038 if (!maybe_type_id)
14039 return argument;
14040 if (!cp_parser_next_token_ends_template_argument_p (parser))
14041 cp_parser_error (parser, "expected template-argument");
14042 if (cp_parser_parse_definitely (parser))
14043 return argument;
14044 /* We did our best to parse the argument as a non type-id, but that
14045 was the only alternative that matched (albeit with a '>' after
14046 it). We can assume it's just a typo from the user, and a
14047 diagnostic will then be issued. */
14048 return cp_parser_template_type_arg (parser);
14051 /* Parse an explicit-instantiation.
14053 explicit-instantiation:
14054 template declaration
14056 Although the standard says `declaration', what it really means is:
14058 explicit-instantiation:
14059 template decl-specifier-seq [opt] declarator [opt] ;
14061 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14062 supposed to be allowed. A defect report has been filed about this
14063 issue.
14065 GNU Extension:
14067 explicit-instantiation:
14068 storage-class-specifier template
14069 decl-specifier-seq [opt] declarator [opt] ;
14070 function-specifier template
14071 decl-specifier-seq [opt] declarator [opt] ; */
14073 static void
14074 cp_parser_explicit_instantiation (cp_parser* parser)
14076 int declares_class_or_enum;
14077 cp_decl_specifier_seq decl_specifiers;
14078 tree extension_specifier = NULL_TREE;
14080 timevar_push (TV_TEMPLATE_INST);
14082 /* Look for an (optional) storage-class-specifier or
14083 function-specifier. */
14084 if (cp_parser_allow_gnu_extensions_p (parser))
14086 extension_specifier
14087 = cp_parser_storage_class_specifier_opt (parser);
14088 if (!extension_specifier)
14089 extension_specifier
14090 = cp_parser_function_specifier_opt (parser,
14091 /*decl_specs=*/NULL);
14094 /* Look for the `template' keyword. */
14095 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14096 /* Let the front end know that we are processing an explicit
14097 instantiation. */
14098 begin_explicit_instantiation ();
14099 /* [temp.explicit] says that we are supposed to ignore access
14100 control while processing explicit instantiation directives. */
14101 push_deferring_access_checks (dk_no_check);
14102 /* Parse a decl-specifier-seq. */
14103 cp_parser_decl_specifier_seq (parser,
14104 CP_PARSER_FLAGS_OPTIONAL,
14105 &decl_specifiers,
14106 &declares_class_or_enum);
14107 /* If there was exactly one decl-specifier, and it declared a class,
14108 and there's no declarator, then we have an explicit type
14109 instantiation. */
14110 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14112 tree type;
14114 type = check_tag_decl (&decl_specifiers,
14115 /*explicit_type_instantiation_p=*/true);
14116 /* Turn access control back on for names used during
14117 template instantiation. */
14118 pop_deferring_access_checks ();
14119 if (type)
14120 do_type_instantiation (type, extension_specifier,
14121 /*complain=*/tf_error);
14123 else
14125 cp_declarator *declarator;
14126 tree decl;
14128 /* Parse the declarator. */
14129 declarator
14130 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14131 /*ctor_dtor_or_conv_p=*/NULL,
14132 /*parenthesized_p=*/NULL,
14133 /*member_p=*/false);
14134 if (declares_class_or_enum & 2)
14135 cp_parser_check_for_definition_in_return_type (declarator,
14136 decl_specifiers.type,
14137 decl_specifiers.locations[ds_type_spec]);
14138 if (declarator != cp_error_declarator)
14140 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14141 permerror (decl_specifiers.locations[ds_inline],
14142 "explicit instantiation shall not use"
14143 " %<inline%> specifier");
14144 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14145 permerror (decl_specifiers.locations[ds_constexpr],
14146 "explicit instantiation shall not use"
14147 " %<constexpr%> specifier");
14149 decl = grokdeclarator (declarator, &decl_specifiers,
14150 NORMAL, 0, &decl_specifiers.attributes);
14151 /* Turn access control back on for names used during
14152 template instantiation. */
14153 pop_deferring_access_checks ();
14154 /* Do the explicit instantiation. */
14155 do_decl_instantiation (decl, extension_specifier);
14157 else
14159 pop_deferring_access_checks ();
14160 /* Skip the body of the explicit instantiation. */
14161 cp_parser_skip_to_end_of_statement (parser);
14164 /* We're done with the instantiation. */
14165 end_explicit_instantiation ();
14167 cp_parser_consume_semicolon_at_end_of_statement (parser);
14169 timevar_pop (TV_TEMPLATE_INST);
14172 /* Parse an explicit-specialization.
14174 explicit-specialization:
14175 template < > declaration
14177 Although the standard says `declaration', what it really means is:
14179 explicit-specialization:
14180 template <> decl-specifier [opt] init-declarator [opt] ;
14181 template <> function-definition
14182 template <> explicit-specialization
14183 template <> template-declaration */
14185 static void
14186 cp_parser_explicit_specialization (cp_parser* parser)
14188 bool need_lang_pop;
14189 cp_token *token = cp_lexer_peek_token (parser->lexer);
14191 /* Look for the `template' keyword. */
14192 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14193 /* Look for the `<'. */
14194 cp_parser_require (parser, CPP_LESS, RT_LESS);
14195 /* Look for the `>'. */
14196 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14197 /* We have processed another parameter list. */
14198 ++parser->num_template_parameter_lists;
14199 /* [temp]
14201 A template ... explicit specialization ... shall not have C
14202 linkage. */
14203 if (current_lang_name == lang_name_c)
14205 error_at (token->location, "template specialization with C linkage");
14206 /* Give it C++ linkage to avoid confusing other parts of the
14207 front end. */
14208 push_lang_context (lang_name_cplusplus);
14209 need_lang_pop = true;
14211 else
14212 need_lang_pop = false;
14213 /* Let the front end know that we are beginning a specialization. */
14214 if (!begin_specialization ())
14216 end_specialization ();
14217 return;
14220 /* If the next keyword is `template', we need to figure out whether
14221 or not we're looking a template-declaration. */
14222 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14224 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14225 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14226 cp_parser_template_declaration_after_export (parser,
14227 /*member_p=*/false);
14228 else
14229 cp_parser_explicit_specialization (parser);
14231 else
14232 /* Parse the dependent declaration. */
14233 cp_parser_single_declaration (parser,
14234 /*checks=*/NULL,
14235 /*member_p=*/false,
14236 /*explicit_specialization_p=*/true,
14237 /*friend_p=*/NULL);
14238 /* We're done with the specialization. */
14239 end_specialization ();
14240 /* For the erroneous case of a template with C linkage, we pushed an
14241 implicit C++ linkage scope; exit that scope now. */
14242 if (need_lang_pop)
14243 pop_lang_context ();
14244 /* We're done with this parameter list. */
14245 --parser->num_template_parameter_lists;
14248 /* Parse a type-specifier.
14250 type-specifier:
14251 simple-type-specifier
14252 class-specifier
14253 enum-specifier
14254 elaborated-type-specifier
14255 cv-qualifier
14257 GNU Extension:
14259 type-specifier:
14260 __complex__
14262 Returns a representation of the type-specifier. For a
14263 class-specifier, enum-specifier, or elaborated-type-specifier, a
14264 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14266 The parser flags FLAGS is used to control type-specifier parsing.
14268 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14269 in a decl-specifier-seq.
14271 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14272 class-specifier, enum-specifier, or elaborated-type-specifier, then
14273 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14274 if a type is declared; 2 if it is defined. Otherwise, it is set to
14275 zero.
14277 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14278 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14279 is set to FALSE. */
14281 static tree
14282 cp_parser_type_specifier (cp_parser* parser,
14283 cp_parser_flags flags,
14284 cp_decl_specifier_seq *decl_specs,
14285 bool is_declaration,
14286 int* declares_class_or_enum,
14287 bool* is_cv_qualifier)
14289 tree type_spec = NULL_TREE;
14290 cp_token *token;
14291 enum rid keyword;
14292 cp_decl_spec ds = ds_last;
14294 /* Assume this type-specifier does not declare a new type. */
14295 if (declares_class_or_enum)
14296 *declares_class_or_enum = 0;
14297 /* And that it does not specify a cv-qualifier. */
14298 if (is_cv_qualifier)
14299 *is_cv_qualifier = false;
14300 /* Peek at the next token. */
14301 token = cp_lexer_peek_token (parser->lexer);
14303 /* If we're looking at a keyword, we can use that to guide the
14304 production we choose. */
14305 keyword = token->keyword;
14306 switch (keyword)
14308 case RID_ENUM:
14309 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14310 goto elaborated_type_specifier;
14312 /* Look for the enum-specifier. */
14313 type_spec = cp_parser_enum_specifier (parser);
14314 /* If that worked, we're done. */
14315 if (type_spec)
14317 if (declares_class_or_enum)
14318 *declares_class_or_enum = 2;
14319 if (decl_specs)
14320 cp_parser_set_decl_spec_type (decl_specs,
14321 type_spec,
14322 token,
14323 /*type_definition_p=*/true);
14324 return type_spec;
14326 else
14327 goto elaborated_type_specifier;
14329 /* Any of these indicate either a class-specifier, or an
14330 elaborated-type-specifier. */
14331 case RID_CLASS:
14332 case RID_STRUCT:
14333 case RID_UNION:
14334 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14335 goto elaborated_type_specifier;
14337 /* Parse tentatively so that we can back up if we don't find a
14338 class-specifier. */
14339 cp_parser_parse_tentatively (parser);
14340 /* Look for the class-specifier. */
14341 type_spec = cp_parser_class_specifier (parser);
14342 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14343 /* If that worked, we're done. */
14344 if (cp_parser_parse_definitely (parser))
14346 if (declares_class_or_enum)
14347 *declares_class_or_enum = 2;
14348 if (decl_specs)
14349 cp_parser_set_decl_spec_type (decl_specs,
14350 type_spec,
14351 token,
14352 /*type_definition_p=*/true);
14353 return type_spec;
14356 /* Fall through. */
14357 elaborated_type_specifier:
14358 /* We're declaring (not defining) a class or enum. */
14359 if (declares_class_or_enum)
14360 *declares_class_or_enum = 1;
14362 /* Fall through. */
14363 case RID_TYPENAME:
14364 /* Look for an elaborated-type-specifier. */
14365 type_spec
14366 = (cp_parser_elaborated_type_specifier
14367 (parser,
14368 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14369 is_declaration));
14370 if (decl_specs)
14371 cp_parser_set_decl_spec_type (decl_specs,
14372 type_spec,
14373 token,
14374 /*type_definition_p=*/false);
14375 return type_spec;
14377 case RID_CONST:
14378 ds = ds_const;
14379 if (is_cv_qualifier)
14380 *is_cv_qualifier = true;
14381 break;
14383 case RID_VOLATILE:
14384 ds = ds_volatile;
14385 if (is_cv_qualifier)
14386 *is_cv_qualifier = true;
14387 break;
14389 case RID_RESTRICT:
14390 ds = ds_restrict;
14391 if (is_cv_qualifier)
14392 *is_cv_qualifier = true;
14393 break;
14395 case RID_COMPLEX:
14396 /* The `__complex__' keyword is a GNU extension. */
14397 ds = ds_complex;
14398 break;
14400 default:
14401 break;
14404 /* Handle simple keywords. */
14405 if (ds != ds_last)
14407 if (decl_specs)
14409 set_and_check_decl_spec_loc (decl_specs, ds, token);
14410 decl_specs->any_specifiers_p = true;
14412 return cp_lexer_consume_token (parser->lexer)->u.value;
14415 /* If we do not already have a type-specifier, assume we are looking
14416 at a simple-type-specifier. */
14417 type_spec = cp_parser_simple_type_specifier (parser,
14418 decl_specs,
14419 flags);
14421 /* If we didn't find a type-specifier, and a type-specifier was not
14422 optional in this context, issue an error message. */
14423 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14425 cp_parser_error (parser, "expected type specifier");
14426 return error_mark_node;
14429 return type_spec;
14432 /* Parse a simple-type-specifier.
14434 simple-type-specifier:
14435 :: [opt] nested-name-specifier [opt] type-name
14436 :: [opt] nested-name-specifier template template-id
14437 char
14438 wchar_t
14439 bool
14440 short
14442 long
14443 signed
14444 unsigned
14445 float
14446 double
14447 void
14449 C++0x Extension:
14451 simple-type-specifier:
14452 auto
14453 decltype ( expression )
14454 char16_t
14455 char32_t
14456 __underlying_type ( type-id )
14458 GNU Extension:
14460 simple-type-specifier:
14461 __int128
14462 __typeof__ unary-expression
14463 __typeof__ ( type-id )
14464 __typeof__ ( type-id ) { initializer-list , [opt] }
14466 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14467 appropriately updated. */
14469 static tree
14470 cp_parser_simple_type_specifier (cp_parser* parser,
14471 cp_decl_specifier_seq *decl_specs,
14472 cp_parser_flags flags)
14474 tree type = NULL_TREE;
14475 cp_token *token;
14477 /* Peek at the next token. */
14478 token = cp_lexer_peek_token (parser->lexer);
14480 /* If we're looking at a keyword, things are easy. */
14481 switch (token->keyword)
14483 case RID_CHAR:
14484 if (decl_specs)
14485 decl_specs->explicit_char_p = true;
14486 type = char_type_node;
14487 break;
14488 case RID_CHAR16:
14489 type = char16_type_node;
14490 break;
14491 case RID_CHAR32:
14492 type = char32_type_node;
14493 break;
14494 case RID_WCHAR:
14495 type = wchar_type_node;
14496 break;
14497 case RID_BOOL:
14498 type = boolean_type_node;
14499 break;
14500 case RID_SHORT:
14501 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14502 type = short_integer_type_node;
14503 break;
14504 case RID_INT:
14505 if (decl_specs)
14506 decl_specs->explicit_int_p = true;
14507 type = integer_type_node;
14508 break;
14509 case RID_INT128:
14510 if (!int128_integer_type_node)
14511 break;
14512 if (decl_specs)
14513 decl_specs->explicit_int128_p = true;
14514 type = int128_integer_type_node;
14515 break;
14516 case RID_LONG:
14517 if (decl_specs)
14518 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14519 type = long_integer_type_node;
14520 break;
14521 case RID_SIGNED:
14522 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14523 type = integer_type_node;
14524 break;
14525 case RID_UNSIGNED:
14526 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14527 type = unsigned_type_node;
14528 break;
14529 case RID_FLOAT:
14530 type = float_type_node;
14531 break;
14532 case RID_DOUBLE:
14533 type = double_type_node;
14534 break;
14535 case RID_VOID:
14536 type = void_type_node;
14537 break;
14539 case RID_AUTO:
14540 maybe_warn_cpp0x (CPP0X_AUTO);
14541 if (parser->auto_is_implicit_function_template_parm_p)
14543 type = synthesize_implicit_template_parm (parser);
14545 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14547 if (cxx_dialect < cxx1y)
14548 pedwarn (location_of (type), 0,
14549 "use of %<auto%> in lambda parameter declaration "
14550 "only available with "
14551 "-std=c++1y or -std=gnu++1y");
14553 else if (cxx_dialect < cxx1y)
14554 pedwarn (location_of (type), 0,
14555 "use of %<auto%> in parameter declaration "
14556 "only available with "
14557 "-std=c++1y or -std=gnu++1y");
14558 else
14559 pedwarn (location_of (type), OPT_Wpedantic,
14560 "ISO C++ forbids use of %<auto%> in parameter "
14561 "declaration");
14563 else
14564 type = make_auto ();
14565 break;
14567 case RID_DECLTYPE:
14568 /* Since DR 743, decltype can either be a simple-type-specifier by
14569 itself or begin a nested-name-specifier. Parsing it will replace
14570 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14571 handling below decide what to do. */
14572 cp_parser_decltype (parser);
14573 cp_lexer_set_token_position (parser->lexer, token);
14574 break;
14576 case RID_TYPEOF:
14577 /* Consume the `typeof' token. */
14578 cp_lexer_consume_token (parser->lexer);
14579 /* Parse the operand to `typeof'. */
14580 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14581 /* If it is not already a TYPE, take its type. */
14582 if (!TYPE_P (type))
14583 type = finish_typeof (type);
14585 if (decl_specs)
14586 cp_parser_set_decl_spec_type (decl_specs, type,
14587 token,
14588 /*type_definition_p=*/false);
14590 return type;
14592 case RID_UNDERLYING_TYPE:
14593 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14594 if (decl_specs)
14595 cp_parser_set_decl_spec_type (decl_specs, type,
14596 token,
14597 /*type_definition_p=*/false);
14599 return type;
14601 case RID_BASES:
14602 case RID_DIRECT_BASES:
14603 type = cp_parser_trait_expr (parser, token->keyword);
14604 if (decl_specs)
14605 cp_parser_set_decl_spec_type (decl_specs, type,
14606 token,
14607 /*type_definition_p=*/false);
14608 return type;
14609 default:
14610 break;
14613 /* If token is an already-parsed decltype not followed by ::,
14614 it's a simple-type-specifier. */
14615 if (token->type == CPP_DECLTYPE
14616 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14618 type = token->u.value;
14619 if (decl_specs)
14620 cp_parser_set_decl_spec_type (decl_specs, type,
14621 token,
14622 /*type_definition_p=*/false);
14623 cp_lexer_consume_token (parser->lexer);
14624 return type;
14627 /* If the type-specifier was for a built-in type, we're done. */
14628 if (type)
14630 /* Record the type. */
14631 if (decl_specs
14632 && (token->keyword != RID_SIGNED
14633 && token->keyword != RID_UNSIGNED
14634 && token->keyword != RID_SHORT
14635 && token->keyword != RID_LONG))
14636 cp_parser_set_decl_spec_type (decl_specs,
14637 type,
14638 token,
14639 /*type_definition_p=*/false);
14640 if (decl_specs)
14641 decl_specs->any_specifiers_p = true;
14643 /* Consume the token. */
14644 cp_lexer_consume_token (parser->lexer);
14646 /* There is no valid C++ program where a non-template type is
14647 followed by a "<". That usually indicates that the user thought
14648 that the type was a template. */
14649 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14650 token->location);
14652 return TYPE_NAME (type);
14655 /* The type-specifier must be a user-defined type. */
14656 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14658 bool qualified_p;
14659 bool global_p;
14661 /* Don't gobble tokens or issue error messages if this is an
14662 optional type-specifier. */
14663 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14664 cp_parser_parse_tentatively (parser);
14666 /* Look for the optional `::' operator. */
14667 global_p
14668 = (cp_parser_global_scope_opt (parser,
14669 /*current_scope_valid_p=*/false)
14670 != NULL_TREE);
14671 /* Look for the nested-name specifier. */
14672 qualified_p
14673 = (cp_parser_nested_name_specifier_opt (parser,
14674 /*typename_keyword_p=*/false,
14675 /*check_dependency_p=*/true,
14676 /*type_p=*/false,
14677 /*is_declaration=*/false)
14678 != NULL_TREE);
14679 token = cp_lexer_peek_token (parser->lexer);
14680 /* If we have seen a nested-name-specifier, and the next token
14681 is `template', then we are using the template-id production. */
14682 if (parser->scope
14683 && cp_parser_optional_template_keyword (parser))
14685 /* Look for the template-id. */
14686 type = cp_parser_template_id (parser,
14687 /*template_keyword_p=*/true,
14688 /*check_dependency_p=*/true,
14689 none_type,
14690 /*is_declaration=*/false);
14691 /* If the template-id did not name a type, we are out of
14692 luck. */
14693 if (TREE_CODE (type) != TYPE_DECL)
14695 cp_parser_error (parser, "expected template-id for type");
14696 type = NULL_TREE;
14699 /* Otherwise, look for a type-name. */
14700 else
14701 type = cp_parser_type_name (parser);
14702 /* Keep track of all name-lookups performed in class scopes. */
14703 if (type
14704 && !global_p
14705 && !qualified_p
14706 && TREE_CODE (type) == TYPE_DECL
14707 && identifier_p (DECL_NAME (type)))
14708 maybe_note_name_used_in_class (DECL_NAME (type), type);
14709 /* If it didn't work out, we don't have a TYPE. */
14710 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14711 && !cp_parser_parse_definitely (parser))
14712 type = NULL_TREE;
14713 if (type && decl_specs)
14714 cp_parser_set_decl_spec_type (decl_specs, type,
14715 token,
14716 /*type_definition_p=*/false);
14719 /* If we didn't get a type-name, issue an error message. */
14720 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14722 cp_parser_error (parser, "expected type-name");
14723 return error_mark_node;
14726 if (type && type != error_mark_node)
14728 /* See if TYPE is an Objective-C type, and if so, parse and
14729 accept any protocol references following it. Do this before
14730 the cp_parser_check_for_invalid_template_id() call, because
14731 Objective-C types can be followed by '<...>' which would
14732 enclose protocol names rather than template arguments, and so
14733 everything is fine. */
14734 if (c_dialect_objc () && !parser->scope
14735 && (objc_is_id (type) || objc_is_class_name (type)))
14737 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14738 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14740 /* Clobber the "unqualified" type previously entered into
14741 DECL_SPECS with the new, improved protocol-qualified version. */
14742 if (decl_specs)
14743 decl_specs->type = qual_type;
14745 return qual_type;
14748 /* There is no valid C++ program where a non-template type is
14749 followed by a "<". That usually indicates that the user
14750 thought that the type was a template. */
14751 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14752 none_type,
14753 token->location);
14756 return type;
14759 /* Parse a type-name.
14761 type-name:
14762 class-name
14763 enum-name
14764 typedef-name
14765 simple-template-id [in c++0x]
14767 enum-name:
14768 identifier
14770 typedef-name:
14771 identifier
14773 Returns a TYPE_DECL for the type. */
14775 static tree
14776 cp_parser_type_name (cp_parser* parser)
14778 tree type_decl;
14780 /* We can't know yet whether it is a class-name or not. */
14781 cp_parser_parse_tentatively (parser);
14782 /* Try a class-name. */
14783 type_decl = cp_parser_class_name (parser,
14784 /*typename_keyword_p=*/false,
14785 /*template_keyword_p=*/false,
14786 none_type,
14787 /*check_dependency_p=*/true,
14788 /*class_head_p=*/false,
14789 /*is_declaration=*/false);
14790 /* If it's not a class-name, keep looking. */
14791 if (!cp_parser_parse_definitely (parser))
14793 if (cxx_dialect < cxx11)
14794 /* It must be a typedef-name or an enum-name. */
14795 return cp_parser_nonclass_name (parser);
14797 cp_parser_parse_tentatively (parser);
14798 /* It is either a simple-template-id representing an
14799 instantiation of an alias template... */
14800 type_decl = cp_parser_template_id (parser,
14801 /*template_keyword_p=*/false,
14802 /*check_dependency_p=*/true,
14803 none_type,
14804 /*is_declaration=*/false);
14805 /* Note that this must be an instantiation of an alias template
14806 because [temp.names]/6 says:
14808 A template-id that names an alias template specialization
14809 is a type-name.
14811 Whereas [temp.names]/7 says:
14813 A simple-template-id that names a class template
14814 specialization is a class-name. */
14815 if (type_decl != NULL_TREE
14816 && TREE_CODE (type_decl) == TYPE_DECL
14817 && TYPE_DECL_ALIAS_P (type_decl))
14818 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14819 else
14820 cp_parser_simulate_error (parser);
14822 if (!cp_parser_parse_definitely (parser))
14823 /* ... Or a typedef-name or an enum-name. */
14824 return cp_parser_nonclass_name (parser);
14827 return type_decl;
14830 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14832 enum-name:
14833 identifier
14835 typedef-name:
14836 identifier
14838 Returns a TYPE_DECL for the type. */
14840 static tree
14841 cp_parser_nonclass_name (cp_parser* parser)
14843 tree type_decl;
14844 tree identifier;
14846 cp_token *token = cp_lexer_peek_token (parser->lexer);
14847 identifier = cp_parser_identifier (parser);
14848 if (identifier == error_mark_node)
14849 return error_mark_node;
14851 /* Look up the type-name. */
14852 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14854 type_decl = strip_using_decl (type_decl);
14856 if (TREE_CODE (type_decl) != TYPE_DECL
14857 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14859 /* See if this is an Objective-C type. */
14860 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14861 tree type = objc_get_protocol_qualified_type (identifier, protos);
14862 if (type)
14863 type_decl = TYPE_NAME (type);
14866 /* Issue an error if we did not find a type-name. */
14867 if (TREE_CODE (type_decl) != TYPE_DECL
14868 /* In Objective-C, we have the complication that class names are
14869 normally type names and start declarations (eg, the
14870 "NSObject" in "NSObject *object;"), but can be used in an
14871 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14872 is an expression. So, a classname followed by a dot is not a
14873 valid type-name. */
14874 || (objc_is_class_name (TREE_TYPE (type_decl))
14875 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14877 if (!cp_parser_simulate_error (parser))
14878 cp_parser_name_lookup_error (parser, identifier, type_decl,
14879 NLE_TYPE, token->location);
14880 return error_mark_node;
14882 /* Remember that the name was used in the definition of the
14883 current class so that we can check later to see if the
14884 meaning would have been different after the class was
14885 entirely defined. */
14886 else if (type_decl != error_mark_node
14887 && !parser->scope)
14888 maybe_note_name_used_in_class (identifier, type_decl);
14890 return type_decl;
14893 /* Parse an elaborated-type-specifier. Note that the grammar given
14894 here incorporates the resolution to DR68.
14896 elaborated-type-specifier:
14897 class-key :: [opt] nested-name-specifier [opt] identifier
14898 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14899 enum-key :: [opt] nested-name-specifier [opt] identifier
14900 typename :: [opt] nested-name-specifier identifier
14901 typename :: [opt] nested-name-specifier template [opt]
14902 template-id
14904 GNU extension:
14906 elaborated-type-specifier:
14907 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14908 class-key attributes :: [opt] nested-name-specifier [opt]
14909 template [opt] template-id
14910 enum attributes :: [opt] nested-name-specifier [opt] identifier
14912 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14913 declared `friend'. If IS_DECLARATION is TRUE, then this
14914 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14915 something is being declared.
14917 Returns the TYPE specified. */
14919 static tree
14920 cp_parser_elaborated_type_specifier (cp_parser* parser,
14921 bool is_friend,
14922 bool is_declaration)
14924 enum tag_types tag_type;
14925 tree identifier;
14926 tree type = NULL_TREE;
14927 tree attributes = NULL_TREE;
14928 tree globalscope;
14929 cp_token *token = NULL;
14931 /* See if we're looking at the `enum' keyword. */
14932 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14934 /* Consume the `enum' token. */
14935 cp_lexer_consume_token (parser->lexer);
14936 /* Remember that it's an enumeration type. */
14937 tag_type = enum_type;
14938 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14939 enums) is used here. */
14940 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14941 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14943 pedwarn (input_location, 0, "elaborated-type-specifier "
14944 "for a scoped enum must not use the %<%D%> keyword",
14945 cp_lexer_peek_token (parser->lexer)->u.value);
14946 /* Consume the `struct' or `class' and parse it anyway. */
14947 cp_lexer_consume_token (parser->lexer);
14949 /* Parse the attributes. */
14950 attributes = cp_parser_attributes_opt (parser);
14952 /* Or, it might be `typename'. */
14953 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14954 RID_TYPENAME))
14956 /* Consume the `typename' token. */
14957 cp_lexer_consume_token (parser->lexer);
14958 /* Remember that it's a `typename' type. */
14959 tag_type = typename_type;
14961 /* Otherwise it must be a class-key. */
14962 else
14964 tag_type = cp_parser_class_key (parser);
14965 if (tag_type == none_type)
14966 return error_mark_node;
14967 /* Parse the attributes. */
14968 attributes = cp_parser_attributes_opt (parser);
14971 /* Look for the `::' operator. */
14972 globalscope = cp_parser_global_scope_opt (parser,
14973 /*current_scope_valid_p=*/false);
14974 /* Look for the nested-name-specifier. */
14975 if (tag_type == typename_type && !globalscope)
14977 if (!cp_parser_nested_name_specifier (parser,
14978 /*typename_keyword_p=*/true,
14979 /*check_dependency_p=*/true,
14980 /*type_p=*/true,
14981 is_declaration))
14982 return error_mark_node;
14984 else
14985 /* Even though `typename' is not present, the proposed resolution
14986 to Core Issue 180 says that in `class A<T>::B', `B' should be
14987 considered a type-name, even if `A<T>' is dependent. */
14988 cp_parser_nested_name_specifier_opt (parser,
14989 /*typename_keyword_p=*/true,
14990 /*check_dependency_p=*/true,
14991 /*type_p=*/true,
14992 is_declaration);
14993 /* For everything but enumeration types, consider a template-id.
14994 For an enumeration type, consider only a plain identifier. */
14995 if (tag_type != enum_type)
14997 bool template_p = false;
14998 tree decl;
15000 /* Allow the `template' keyword. */
15001 template_p = cp_parser_optional_template_keyword (parser);
15002 /* If we didn't see `template', we don't know if there's a
15003 template-id or not. */
15004 if (!template_p)
15005 cp_parser_parse_tentatively (parser);
15006 /* Parse the template-id. */
15007 token = cp_lexer_peek_token (parser->lexer);
15008 decl = cp_parser_template_id (parser, template_p,
15009 /*check_dependency_p=*/true,
15010 tag_type,
15011 is_declaration);
15012 /* If we didn't find a template-id, look for an ordinary
15013 identifier. */
15014 if (!template_p && !cp_parser_parse_definitely (parser))
15016 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15017 in effect, then we must assume that, upon instantiation, the
15018 template will correspond to a class. */
15019 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15020 && tag_type == typename_type)
15021 type = make_typename_type (parser->scope, decl,
15022 typename_type,
15023 /*complain=*/tf_error);
15024 /* If the `typename' keyword is in effect and DECL is not a type
15025 decl, then type is non existent. */
15026 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15028 else if (TREE_CODE (decl) == TYPE_DECL)
15029 type = check_elaborated_type_specifier (tag_type, decl,
15030 /*allow_template_p=*/true);
15031 else if (decl == error_mark_node)
15032 type = error_mark_node;
15035 if (!type)
15037 token = cp_lexer_peek_token (parser->lexer);
15038 identifier = cp_parser_identifier (parser);
15040 if (identifier == error_mark_node)
15042 parser->scope = NULL_TREE;
15043 return error_mark_node;
15046 /* For a `typename', we needn't call xref_tag. */
15047 if (tag_type == typename_type
15048 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15049 return cp_parser_make_typename_type (parser, parser->scope,
15050 identifier,
15051 token->location);
15052 /* Look up a qualified name in the usual way. */
15053 if (parser->scope)
15055 tree decl;
15056 tree ambiguous_decls;
15058 decl = cp_parser_lookup_name (parser, identifier,
15059 tag_type,
15060 /*is_template=*/false,
15061 /*is_namespace=*/false,
15062 /*check_dependency=*/true,
15063 &ambiguous_decls,
15064 token->location);
15066 /* If the lookup was ambiguous, an error will already have been
15067 issued. */
15068 if (ambiguous_decls)
15069 return error_mark_node;
15071 /* If we are parsing friend declaration, DECL may be a
15072 TEMPLATE_DECL tree node here. However, we need to check
15073 whether this TEMPLATE_DECL results in valid code. Consider
15074 the following example:
15076 namespace N {
15077 template <class T> class C {};
15079 class X {
15080 template <class T> friend class N::C; // #1, valid code
15082 template <class T> class Y {
15083 friend class N::C; // #2, invalid code
15086 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15087 name lookup of `N::C'. We see that friend declaration must
15088 be template for the code to be valid. Note that
15089 processing_template_decl does not work here since it is
15090 always 1 for the above two cases. */
15092 decl = (cp_parser_maybe_treat_template_as_class
15093 (decl, /*tag_name_p=*/is_friend
15094 && parser->num_template_parameter_lists));
15096 if (TREE_CODE (decl) != TYPE_DECL)
15098 cp_parser_diagnose_invalid_type_name (parser,
15099 parser->scope,
15100 identifier,
15101 token->location);
15102 return error_mark_node;
15105 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15107 bool allow_template = (parser->num_template_parameter_lists
15108 || DECL_SELF_REFERENCE_P (decl));
15109 type = check_elaborated_type_specifier (tag_type, decl,
15110 allow_template);
15112 if (type == error_mark_node)
15113 return error_mark_node;
15116 /* Forward declarations of nested types, such as
15118 class C1::C2;
15119 class C1::C2::C3;
15121 are invalid unless all components preceding the final '::'
15122 are complete. If all enclosing types are complete, these
15123 declarations become merely pointless.
15125 Invalid forward declarations of nested types are errors
15126 caught elsewhere in parsing. Those that are pointless arrive
15127 here. */
15129 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15130 && !is_friend && !processing_explicit_instantiation)
15131 warning (0, "declaration %qD does not declare anything", decl);
15133 type = TREE_TYPE (decl);
15135 else
15137 /* An elaborated-type-specifier sometimes introduces a new type and
15138 sometimes names an existing type. Normally, the rule is that it
15139 introduces a new type only if there is not an existing type of
15140 the same name already in scope. For example, given:
15142 struct S {};
15143 void f() { struct S s; }
15145 the `struct S' in the body of `f' is the same `struct S' as in
15146 the global scope; the existing definition is used. However, if
15147 there were no global declaration, this would introduce a new
15148 local class named `S'.
15150 An exception to this rule applies to the following code:
15152 namespace N { struct S; }
15154 Here, the elaborated-type-specifier names a new type
15155 unconditionally; even if there is already an `S' in the
15156 containing scope this declaration names a new type.
15157 This exception only applies if the elaborated-type-specifier
15158 forms the complete declaration:
15160 [class.name]
15162 A declaration consisting solely of `class-key identifier ;' is
15163 either a redeclaration of the name in the current scope or a
15164 forward declaration of the identifier as a class name. It
15165 introduces the name into the current scope.
15167 We are in this situation precisely when the next token is a `;'.
15169 An exception to the exception is that a `friend' declaration does
15170 *not* name a new type; i.e., given:
15172 struct S { friend struct T; };
15174 `T' is not a new type in the scope of `S'.
15176 Also, `new struct S' or `sizeof (struct S)' never results in the
15177 definition of a new type; a new type can only be declared in a
15178 declaration context. */
15180 tag_scope ts;
15181 bool template_p;
15183 if (is_friend)
15184 /* Friends have special name lookup rules. */
15185 ts = ts_within_enclosing_non_class;
15186 else if (is_declaration
15187 && cp_lexer_next_token_is (parser->lexer,
15188 CPP_SEMICOLON))
15189 /* This is a `class-key identifier ;' */
15190 ts = ts_current;
15191 else
15192 ts = ts_global;
15194 template_p =
15195 (parser->num_template_parameter_lists
15196 && (cp_parser_next_token_starts_class_definition_p (parser)
15197 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15198 /* An unqualified name was used to reference this type, so
15199 there were no qualifying templates. */
15200 if (!cp_parser_check_template_parameters (parser,
15201 /*num_templates=*/0,
15202 token->location,
15203 /*declarator=*/NULL))
15204 return error_mark_node;
15205 type = xref_tag (tag_type, identifier, ts, template_p);
15209 if (type == error_mark_node)
15210 return error_mark_node;
15212 /* Allow attributes on forward declarations of classes. */
15213 if (attributes)
15215 if (TREE_CODE (type) == TYPENAME_TYPE)
15216 warning (OPT_Wattributes,
15217 "attributes ignored on uninstantiated type");
15218 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15219 && ! processing_explicit_instantiation)
15220 warning (OPT_Wattributes,
15221 "attributes ignored on template instantiation");
15222 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15223 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15224 else
15225 warning (OPT_Wattributes,
15226 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15229 if (tag_type != enum_type)
15231 /* Indicate whether this class was declared as a `class' or as a
15232 `struct'. */
15233 if (TREE_CODE (type) == RECORD_TYPE)
15234 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15235 cp_parser_check_class_key (tag_type, type);
15238 /* A "<" cannot follow an elaborated type specifier. If that
15239 happens, the user was probably trying to form a template-id. */
15240 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15241 token->location);
15243 return type;
15246 /* Parse an enum-specifier.
15248 enum-specifier:
15249 enum-head { enumerator-list [opt] }
15250 enum-head { enumerator-list , } [C++0x]
15252 enum-head:
15253 enum-key identifier [opt] enum-base [opt]
15254 enum-key nested-name-specifier identifier enum-base [opt]
15256 enum-key:
15257 enum
15258 enum class [C++0x]
15259 enum struct [C++0x]
15261 enum-base: [C++0x]
15262 : type-specifier-seq
15264 opaque-enum-specifier:
15265 enum-key identifier enum-base [opt] ;
15267 GNU Extensions:
15268 enum-key attributes[opt] identifier [opt] enum-base [opt]
15269 { enumerator-list [opt] }attributes[opt]
15270 enum-key attributes[opt] identifier [opt] enum-base [opt]
15271 { enumerator-list, }attributes[opt] [C++0x]
15273 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15274 if the token stream isn't an enum-specifier after all. */
15276 static tree
15277 cp_parser_enum_specifier (cp_parser* parser)
15279 tree identifier;
15280 tree type = NULL_TREE;
15281 tree prev_scope;
15282 tree nested_name_specifier = NULL_TREE;
15283 tree attributes;
15284 bool scoped_enum_p = false;
15285 bool has_underlying_type = false;
15286 bool nested_being_defined = false;
15287 bool new_value_list = false;
15288 bool is_new_type = false;
15289 bool is_anonymous = false;
15290 tree underlying_type = NULL_TREE;
15291 cp_token *type_start_token = NULL;
15292 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15294 parser->colon_corrects_to_scope_p = false;
15296 /* Parse tentatively so that we can back up if we don't find a
15297 enum-specifier. */
15298 cp_parser_parse_tentatively (parser);
15300 /* Caller guarantees that the current token is 'enum', an identifier
15301 possibly follows, and the token after that is an opening brace.
15302 If we don't have an identifier, fabricate an anonymous name for
15303 the enumeration being defined. */
15304 cp_lexer_consume_token (parser->lexer);
15306 /* Parse the "class" or "struct", which indicates a scoped
15307 enumeration type in C++0x. */
15308 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15309 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15311 if (cxx_dialect < cxx11)
15312 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15314 /* Consume the `struct' or `class' token. */
15315 cp_lexer_consume_token (parser->lexer);
15317 scoped_enum_p = true;
15320 attributes = cp_parser_attributes_opt (parser);
15322 /* Clear the qualification. */
15323 parser->scope = NULL_TREE;
15324 parser->qualifying_scope = NULL_TREE;
15325 parser->object_scope = NULL_TREE;
15327 /* Figure out in what scope the declaration is being placed. */
15328 prev_scope = current_scope ();
15330 type_start_token = cp_lexer_peek_token (parser->lexer);
15332 push_deferring_access_checks (dk_no_check);
15333 nested_name_specifier
15334 = cp_parser_nested_name_specifier_opt (parser,
15335 /*typename_keyword_p=*/true,
15336 /*check_dependency_p=*/false,
15337 /*type_p=*/false,
15338 /*is_declaration=*/false);
15340 if (nested_name_specifier)
15342 tree name;
15344 identifier = cp_parser_identifier (parser);
15345 name = cp_parser_lookup_name (parser, identifier,
15346 enum_type,
15347 /*is_template=*/false,
15348 /*is_namespace=*/false,
15349 /*check_dependency=*/true,
15350 /*ambiguous_decls=*/NULL,
15351 input_location);
15352 if (name && name != error_mark_node)
15354 type = TREE_TYPE (name);
15355 if (TREE_CODE (type) == TYPENAME_TYPE)
15357 /* Are template enums allowed in ISO? */
15358 if (template_parm_scope_p ())
15359 pedwarn (type_start_token->location, OPT_Wpedantic,
15360 "%qD is an enumeration template", name);
15361 /* ignore a typename reference, for it will be solved by name
15362 in start_enum. */
15363 type = NULL_TREE;
15366 else if (nested_name_specifier == error_mark_node)
15367 /* We already issued an error. */;
15368 else
15369 error_at (type_start_token->location,
15370 "%qD is not an enumerator-name", identifier);
15372 else
15374 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15375 identifier = cp_parser_identifier (parser);
15376 else
15378 identifier = make_anon_name ();
15379 is_anonymous = true;
15380 if (scoped_enum_p)
15381 error_at (type_start_token->location,
15382 "anonymous scoped enum is not allowed");
15385 pop_deferring_access_checks ();
15387 /* Check for the `:' that denotes a specified underlying type in C++0x.
15388 Note that a ':' could also indicate a bitfield width, however. */
15389 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15391 cp_decl_specifier_seq type_specifiers;
15393 /* Consume the `:'. */
15394 cp_lexer_consume_token (parser->lexer);
15396 /* Parse the type-specifier-seq. */
15397 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15398 /*is_trailing_return=*/false,
15399 &type_specifiers);
15401 /* At this point this is surely not elaborated type specifier. */
15402 if (!cp_parser_parse_definitely (parser))
15403 return NULL_TREE;
15405 if (cxx_dialect < cxx11)
15406 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15408 has_underlying_type = true;
15410 /* If that didn't work, stop. */
15411 if (type_specifiers.type != error_mark_node)
15413 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15414 /*initialized=*/0, NULL);
15415 if (underlying_type == error_mark_node
15416 || check_for_bare_parameter_packs (underlying_type))
15417 underlying_type = NULL_TREE;
15421 /* Look for the `{' but don't consume it yet. */
15422 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15424 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15426 cp_parser_error (parser, "expected %<{%>");
15427 if (has_underlying_type)
15429 type = NULL_TREE;
15430 goto out;
15433 /* An opaque-enum-specifier must have a ';' here. */
15434 if ((scoped_enum_p || underlying_type)
15435 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15437 cp_parser_error (parser, "expected %<;%> or %<{%>");
15438 if (has_underlying_type)
15440 type = NULL_TREE;
15441 goto out;
15446 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15447 return NULL_TREE;
15449 if (nested_name_specifier)
15451 if (CLASS_TYPE_P (nested_name_specifier))
15453 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15454 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15455 push_scope (nested_name_specifier);
15457 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15459 push_nested_namespace (nested_name_specifier);
15463 /* Issue an error message if type-definitions are forbidden here. */
15464 if (!cp_parser_check_type_definition (parser))
15465 type = error_mark_node;
15466 else
15467 /* Create the new type. We do this before consuming the opening
15468 brace so the enum will be recorded as being on the line of its
15469 tag (or the 'enum' keyword, if there is no tag). */
15470 type = start_enum (identifier, type, underlying_type,
15471 scoped_enum_p, &is_new_type);
15473 /* If the next token is not '{' it is an opaque-enum-specifier or an
15474 elaborated-type-specifier. */
15475 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15477 timevar_push (TV_PARSE_ENUM);
15478 if (nested_name_specifier
15479 && nested_name_specifier != error_mark_node)
15481 /* The following catches invalid code such as:
15482 enum class S<int>::E { A, B, C }; */
15483 if (!processing_specialization
15484 && CLASS_TYPE_P (nested_name_specifier)
15485 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15486 error_at (type_start_token->location, "cannot add an enumerator "
15487 "list to a template instantiation");
15489 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15491 error_at (type_start_token->location,
15492 "%<%T::%E%> has not been declared",
15493 TYPE_CONTEXT (nested_name_specifier),
15494 nested_name_specifier);
15495 type = error_mark_node;
15497 /* If that scope does not contain the scope in which the
15498 class was originally declared, the program is invalid. */
15499 else if (prev_scope && !is_ancestor (prev_scope,
15500 nested_name_specifier))
15502 if (at_namespace_scope_p ())
15503 error_at (type_start_token->location,
15504 "declaration of %qD in namespace %qD which does not "
15505 "enclose %qD",
15506 type, prev_scope, nested_name_specifier);
15507 else
15508 error_at (type_start_token->location,
15509 "declaration of %qD in %qD which does not "
15510 "enclose %qD",
15511 type, prev_scope, nested_name_specifier);
15512 type = error_mark_node;
15516 if (scoped_enum_p)
15517 begin_scope (sk_scoped_enum, type);
15519 /* Consume the opening brace. */
15520 cp_lexer_consume_token (parser->lexer);
15522 if (type == error_mark_node)
15523 ; /* Nothing to add */
15524 else if (OPAQUE_ENUM_P (type)
15525 || (cxx_dialect > cxx98 && processing_specialization))
15527 new_value_list = true;
15528 SET_OPAQUE_ENUM_P (type, false);
15529 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15531 else
15533 error_at (type_start_token->location, "multiple definition of %q#T", type);
15534 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15535 "previous definition here");
15536 type = error_mark_node;
15539 if (type == error_mark_node)
15540 cp_parser_skip_to_end_of_block_or_statement (parser);
15541 /* If the next token is not '}', then there are some enumerators. */
15542 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15544 if (is_anonymous && !scoped_enum_p)
15545 pedwarn (type_start_token->location, OPT_Wpedantic,
15546 "ISO C++ forbids empty anonymous enum");
15548 else
15549 cp_parser_enumerator_list (parser, type);
15551 /* Consume the final '}'. */
15552 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15554 if (scoped_enum_p)
15555 finish_scope ();
15556 timevar_pop (TV_PARSE_ENUM);
15558 else
15560 /* If a ';' follows, then it is an opaque-enum-specifier
15561 and additional restrictions apply. */
15562 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15564 if (is_anonymous)
15565 error_at (type_start_token->location,
15566 "opaque-enum-specifier without name");
15567 else if (nested_name_specifier)
15568 error_at (type_start_token->location,
15569 "opaque-enum-specifier must use a simple identifier");
15573 /* Look for trailing attributes to apply to this enumeration, and
15574 apply them if appropriate. */
15575 if (cp_parser_allow_gnu_extensions_p (parser))
15577 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15578 trailing_attr = chainon (trailing_attr, attributes);
15579 cplus_decl_attributes (&type,
15580 trailing_attr,
15581 (int) ATTR_FLAG_TYPE_IN_PLACE);
15584 /* Finish up the enumeration. */
15585 if (type != error_mark_node)
15587 if (new_value_list)
15588 finish_enum_value_list (type);
15589 if (is_new_type)
15590 finish_enum (type);
15593 if (nested_name_specifier)
15595 if (CLASS_TYPE_P (nested_name_specifier))
15597 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15598 pop_scope (nested_name_specifier);
15600 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15602 pop_nested_namespace (nested_name_specifier);
15605 out:
15606 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15607 return type;
15610 /* Parse an enumerator-list. The enumerators all have the indicated
15611 TYPE.
15613 enumerator-list:
15614 enumerator-definition
15615 enumerator-list , enumerator-definition */
15617 static void
15618 cp_parser_enumerator_list (cp_parser* parser, tree type)
15620 while (true)
15622 /* Parse an enumerator-definition. */
15623 cp_parser_enumerator_definition (parser, type);
15625 /* If the next token is not a ',', we've reached the end of
15626 the list. */
15627 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15628 break;
15629 /* Otherwise, consume the `,' and keep going. */
15630 cp_lexer_consume_token (parser->lexer);
15631 /* If the next token is a `}', there is a trailing comma. */
15632 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15634 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15635 pedwarn (input_location, OPT_Wpedantic,
15636 "comma at end of enumerator list");
15637 break;
15642 /* Parse an enumerator-definition. The enumerator has the indicated
15643 TYPE.
15645 enumerator-definition:
15646 enumerator
15647 enumerator = constant-expression
15649 enumerator:
15650 identifier */
15652 static void
15653 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15655 tree identifier;
15656 tree value;
15657 location_t loc;
15659 /* Save the input location because we are interested in the location
15660 of the identifier and not the location of the explicit value. */
15661 loc = cp_lexer_peek_token (parser->lexer)->location;
15663 /* Look for the identifier. */
15664 identifier = cp_parser_identifier (parser);
15665 if (identifier == error_mark_node)
15666 return;
15668 /* If the next token is an '=', then there is an explicit value. */
15669 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15671 /* Consume the `=' token. */
15672 cp_lexer_consume_token (parser->lexer);
15673 /* Parse the value. */
15674 value = cp_parser_constant_expression (parser,
15675 /*allow_non_constant_p=*/false,
15676 NULL);
15678 else
15679 value = NULL_TREE;
15681 /* If we are processing a template, make sure the initializer of the
15682 enumerator doesn't contain any bare template parameter pack. */
15683 if (check_for_bare_parameter_packs (value))
15684 value = error_mark_node;
15686 /* integral_constant_value will pull out this expression, so make sure
15687 it's folded as appropriate. */
15688 value = fold_non_dependent_expr (value);
15690 /* Create the enumerator. */
15691 build_enumerator (identifier, value, type, loc);
15694 /* Parse a namespace-name.
15696 namespace-name:
15697 original-namespace-name
15698 namespace-alias
15700 Returns the NAMESPACE_DECL for the namespace. */
15702 static tree
15703 cp_parser_namespace_name (cp_parser* parser)
15705 tree identifier;
15706 tree namespace_decl;
15708 cp_token *token = cp_lexer_peek_token (parser->lexer);
15710 /* Get the name of the namespace. */
15711 identifier = cp_parser_identifier (parser);
15712 if (identifier == error_mark_node)
15713 return error_mark_node;
15715 /* Look up the identifier in the currently active scope. Look only
15716 for namespaces, due to:
15718 [basic.lookup.udir]
15720 When looking up a namespace-name in a using-directive or alias
15721 definition, only namespace names are considered.
15723 And:
15725 [basic.lookup.qual]
15727 During the lookup of a name preceding the :: scope resolution
15728 operator, object, function, and enumerator names are ignored.
15730 (Note that cp_parser_qualifying_entity only calls this
15731 function if the token after the name is the scope resolution
15732 operator.) */
15733 namespace_decl = cp_parser_lookup_name (parser, identifier,
15734 none_type,
15735 /*is_template=*/false,
15736 /*is_namespace=*/true,
15737 /*check_dependency=*/true,
15738 /*ambiguous_decls=*/NULL,
15739 token->location);
15740 /* If it's not a namespace, issue an error. */
15741 if (namespace_decl == error_mark_node
15742 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15744 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15745 error_at (token->location, "%qD is not a namespace-name", identifier);
15746 cp_parser_error (parser, "expected namespace-name");
15747 namespace_decl = error_mark_node;
15750 return namespace_decl;
15753 /* Parse a namespace-definition.
15755 namespace-definition:
15756 named-namespace-definition
15757 unnamed-namespace-definition
15759 named-namespace-definition:
15760 original-namespace-definition
15761 extension-namespace-definition
15763 original-namespace-definition:
15764 namespace identifier { namespace-body }
15766 extension-namespace-definition:
15767 namespace original-namespace-name { namespace-body }
15769 unnamed-namespace-definition:
15770 namespace { namespace-body } */
15772 static void
15773 cp_parser_namespace_definition (cp_parser* parser)
15775 tree identifier, attribs;
15776 bool has_visibility;
15777 bool is_inline;
15779 cp_ensure_no_omp_declare_simd (parser);
15780 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15782 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15783 is_inline = true;
15784 cp_lexer_consume_token (parser->lexer);
15786 else
15787 is_inline = false;
15789 /* Look for the `namespace' keyword. */
15790 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15792 /* Get the name of the namespace. We do not attempt to distinguish
15793 between an original-namespace-definition and an
15794 extension-namespace-definition at this point. The semantic
15795 analysis routines are responsible for that. */
15796 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15797 identifier = cp_parser_identifier (parser);
15798 else
15799 identifier = NULL_TREE;
15801 /* Parse any specified attributes. */
15802 attribs = cp_parser_attributes_opt (parser);
15804 /* Look for the `{' to start the namespace. */
15805 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15806 /* Start the namespace. */
15807 push_namespace (identifier);
15809 /* "inline namespace" is equivalent to a stub namespace definition
15810 followed by a strong using directive. */
15811 if (is_inline)
15813 tree name_space = current_namespace;
15814 /* Set up namespace association. */
15815 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15816 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15817 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15818 /* Import the contents of the inline namespace. */
15819 pop_namespace ();
15820 do_using_directive (name_space);
15821 push_namespace (identifier);
15824 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15826 /* Parse the body of the namespace. */
15827 cp_parser_namespace_body (parser);
15829 if (has_visibility)
15830 pop_visibility (1);
15832 /* Finish the namespace. */
15833 pop_namespace ();
15834 /* Look for the final `}'. */
15835 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15838 /* Parse a namespace-body.
15840 namespace-body:
15841 declaration-seq [opt] */
15843 static void
15844 cp_parser_namespace_body (cp_parser* parser)
15846 cp_parser_declaration_seq_opt (parser);
15849 /* Parse a namespace-alias-definition.
15851 namespace-alias-definition:
15852 namespace identifier = qualified-namespace-specifier ; */
15854 static void
15855 cp_parser_namespace_alias_definition (cp_parser* parser)
15857 tree identifier;
15858 tree namespace_specifier;
15860 cp_token *token = cp_lexer_peek_token (parser->lexer);
15862 /* Look for the `namespace' keyword. */
15863 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15864 /* Look for the identifier. */
15865 identifier = cp_parser_identifier (parser);
15866 if (identifier == error_mark_node)
15867 return;
15868 /* Look for the `=' token. */
15869 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15870 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15872 error_at (token->location, "%<namespace%> definition is not allowed here");
15873 /* Skip the definition. */
15874 cp_lexer_consume_token (parser->lexer);
15875 if (cp_parser_skip_to_closing_brace (parser))
15876 cp_lexer_consume_token (parser->lexer);
15877 return;
15879 cp_parser_require (parser, CPP_EQ, RT_EQ);
15880 /* Look for the qualified-namespace-specifier. */
15881 namespace_specifier
15882 = cp_parser_qualified_namespace_specifier (parser);
15883 /* Look for the `;' token. */
15884 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15886 /* Register the alias in the symbol table. */
15887 do_namespace_alias (identifier, namespace_specifier);
15890 /* Parse a qualified-namespace-specifier.
15892 qualified-namespace-specifier:
15893 :: [opt] nested-name-specifier [opt] namespace-name
15895 Returns a NAMESPACE_DECL corresponding to the specified
15896 namespace. */
15898 static tree
15899 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15901 /* Look for the optional `::'. */
15902 cp_parser_global_scope_opt (parser,
15903 /*current_scope_valid_p=*/false);
15905 /* Look for the optional nested-name-specifier. */
15906 cp_parser_nested_name_specifier_opt (parser,
15907 /*typename_keyword_p=*/false,
15908 /*check_dependency_p=*/true,
15909 /*type_p=*/false,
15910 /*is_declaration=*/true);
15912 return cp_parser_namespace_name (parser);
15915 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15916 access declaration.
15918 using-declaration:
15919 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15920 using :: unqualified-id ;
15922 access-declaration:
15923 qualified-id ;
15927 static bool
15928 cp_parser_using_declaration (cp_parser* parser,
15929 bool access_declaration_p)
15931 cp_token *token;
15932 bool typename_p = false;
15933 bool global_scope_p;
15934 tree decl;
15935 tree identifier;
15936 tree qscope;
15937 int oldcount = errorcount;
15938 cp_token *diag_token = NULL;
15940 if (access_declaration_p)
15942 diag_token = cp_lexer_peek_token (parser->lexer);
15943 cp_parser_parse_tentatively (parser);
15945 else
15947 /* Look for the `using' keyword. */
15948 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15950 /* Peek at the next token. */
15951 token = cp_lexer_peek_token (parser->lexer);
15952 /* See if it's `typename'. */
15953 if (token->keyword == RID_TYPENAME)
15955 /* Remember that we've seen it. */
15956 typename_p = true;
15957 /* Consume the `typename' token. */
15958 cp_lexer_consume_token (parser->lexer);
15962 /* Look for the optional global scope qualification. */
15963 global_scope_p
15964 = (cp_parser_global_scope_opt (parser,
15965 /*current_scope_valid_p=*/false)
15966 != NULL_TREE);
15968 /* If we saw `typename', or didn't see `::', then there must be a
15969 nested-name-specifier present. */
15970 if (typename_p || !global_scope_p)
15972 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15973 /*check_dependency_p=*/true,
15974 /*type_p=*/false,
15975 /*is_declaration=*/true);
15976 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
15978 cp_parser_skip_to_end_of_block_or_statement (parser);
15979 return false;
15982 /* Otherwise, we could be in either of the two productions. In that
15983 case, treat the nested-name-specifier as optional. */
15984 else
15985 qscope = cp_parser_nested_name_specifier_opt (parser,
15986 /*typename_keyword_p=*/false,
15987 /*check_dependency_p=*/true,
15988 /*type_p=*/false,
15989 /*is_declaration=*/true);
15990 if (!qscope)
15991 qscope = global_namespace;
15993 if (access_declaration_p && cp_parser_error_occurred (parser))
15994 /* Something has already gone wrong; there's no need to parse
15995 further. Since an error has occurred, the return value of
15996 cp_parser_parse_definitely will be false, as required. */
15997 return cp_parser_parse_definitely (parser);
15999 token = cp_lexer_peek_token (parser->lexer);
16000 /* Parse the unqualified-id. */
16001 identifier = cp_parser_unqualified_id (parser,
16002 /*template_keyword_p=*/false,
16003 /*check_dependency_p=*/true,
16004 /*declarator_p=*/true,
16005 /*optional_p=*/false);
16007 if (access_declaration_p)
16009 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16010 cp_parser_simulate_error (parser);
16011 if (!cp_parser_parse_definitely (parser))
16012 return false;
16015 /* The function we call to handle a using-declaration is different
16016 depending on what scope we are in. */
16017 if (qscope == error_mark_node || identifier == error_mark_node)
16019 else if (!identifier_p (identifier)
16020 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16021 /* [namespace.udecl]
16023 A using declaration shall not name a template-id. */
16024 error_at (token->location,
16025 "a template-id may not appear in a using-declaration");
16026 else
16028 if (at_class_scope_p ())
16030 /* Create the USING_DECL. */
16031 decl = do_class_using_decl (parser->scope, identifier);
16033 if (decl && typename_p)
16034 USING_DECL_TYPENAME_P (decl) = 1;
16036 if (check_for_bare_parameter_packs (decl))
16038 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16039 return false;
16041 else
16042 /* Add it to the list of members in this class. */
16043 finish_member_declaration (decl);
16045 else
16047 decl = cp_parser_lookup_name_simple (parser,
16048 identifier,
16049 token->location);
16050 if (decl == error_mark_node)
16051 cp_parser_name_lookup_error (parser, identifier,
16052 decl, NLE_NULL,
16053 token->location);
16054 else if (check_for_bare_parameter_packs (decl))
16056 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16057 return false;
16059 else if (!at_namespace_scope_p ())
16060 do_local_using_decl (decl, qscope, identifier);
16061 else
16062 do_toplevel_using_decl (decl, qscope, identifier);
16066 /* Look for the final `;'. */
16067 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16069 if (access_declaration_p && errorcount == oldcount)
16070 warning_at (diag_token->location, OPT_Wdeprecated,
16071 "access declarations are deprecated "
16072 "in favour of using-declarations; "
16073 "suggestion: add the %<using%> keyword");
16075 return true;
16078 /* Parse an alias-declaration.
16080 alias-declaration:
16081 using identifier attribute-specifier-seq [opt] = type-id */
16083 static tree
16084 cp_parser_alias_declaration (cp_parser* parser)
16086 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16087 location_t id_location;
16088 cp_declarator *declarator;
16089 cp_decl_specifier_seq decl_specs;
16090 bool member_p;
16091 const char *saved_message = NULL;
16093 /* Look for the `using' keyword. */
16094 cp_token *using_token
16095 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16096 if (using_token == NULL)
16097 return error_mark_node;
16099 id_location = cp_lexer_peek_token (parser->lexer)->location;
16100 id = cp_parser_identifier (parser);
16101 if (id == error_mark_node)
16102 return error_mark_node;
16104 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16105 attributes = cp_parser_attributes_opt (parser);
16106 if (attributes == error_mark_node)
16107 return error_mark_node;
16109 cp_parser_require (parser, CPP_EQ, RT_EQ);
16111 if (cp_parser_error_occurred (parser))
16112 return error_mark_node;
16114 cp_parser_commit_to_tentative_parse (parser);
16116 /* Now we are going to parse the type-id of the declaration. */
16119 [dcl.type]/3 says:
16121 "A type-specifier-seq shall not define a class or enumeration
16122 unless it appears in the type-id of an alias-declaration (7.1.3) that
16123 is not the declaration of a template-declaration."
16125 In other words, if we currently are in an alias template, the
16126 type-id should not define a type.
16128 So let's set parser->type_definition_forbidden_message in that
16129 case; cp_parser_check_type_definition (called by
16130 cp_parser_class_specifier) will then emit an error if a type is
16131 defined in the type-id. */
16132 if (parser->num_template_parameter_lists)
16134 saved_message = parser->type_definition_forbidden_message;
16135 parser->type_definition_forbidden_message =
16136 G_("types may not be defined in alias template declarations");
16139 type = cp_parser_type_id (parser);
16141 /* Restore the error message if need be. */
16142 if (parser->num_template_parameter_lists)
16143 parser->type_definition_forbidden_message = saved_message;
16145 if (type == error_mark_node)
16147 cp_parser_skip_to_end_of_block_or_statement (parser);
16148 return error_mark_node;
16151 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16153 if (cp_parser_error_occurred (parser))
16155 cp_parser_skip_to_end_of_block_or_statement (parser);
16156 return error_mark_node;
16159 /* A typedef-name can also be introduced by an alias-declaration. The
16160 identifier following the using keyword becomes a typedef-name. It has
16161 the same semantics as if it were introduced by the typedef
16162 specifier. In particular, it does not define a new type and it shall
16163 not appear in the type-id. */
16165 clear_decl_specs (&decl_specs);
16166 decl_specs.type = type;
16167 if (attributes != NULL_TREE)
16169 decl_specs.attributes = attributes;
16170 set_and_check_decl_spec_loc (&decl_specs,
16171 ds_attribute,
16172 attrs_token);
16174 set_and_check_decl_spec_loc (&decl_specs,
16175 ds_typedef,
16176 using_token);
16177 set_and_check_decl_spec_loc (&decl_specs,
16178 ds_alias,
16179 using_token);
16181 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16182 declarator->id_loc = id_location;
16184 member_p = at_class_scope_p ();
16185 if (member_p)
16186 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16187 NULL_TREE, attributes);
16188 else
16189 decl = start_decl (declarator, &decl_specs, 0,
16190 attributes, NULL_TREE, &pushed_scope);
16191 if (decl == error_mark_node)
16192 return decl;
16194 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16196 if (pushed_scope)
16197 pop_scope (pushed_scope);
16199 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16200 added into the symbol table; otherwise, return the TYPE_DECL. */
16201 if (DECL_LANG_SPECIFIC (decl)
16202 && DECL_TEMPLATE_INFO (decl)
16203 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16205 decl = DECL_TI_TEMPLATE (decl);
16206 if (member_p)
16207 check_member_template (decl);
16210 return decl;
16213 /* Parse a using-directive.
16215 using-directive:
16216 using namespace :: [opt] nested-name-specifier [opt]
16217 namespace-name ; */
16219 static void
16220 cp_parser_using_directive (cp_parser* parser)
16222 tree namespace_decl;
16223 tree attribs;
16225 /* Look for the `using' keyword. */
16226 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16227 /* And the `namespace' keyword. */
16228 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16229 /* Look for the optional `::' operator. */
16230 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16231 /* And the optional nested-name-specifier. */
16232 cp_parser_nested_name_specifier_opt (parser,
16233 /*typename_keyword_p=*/false,
16234 /*check_dependency_p=*/true,
16235 /*type_p=*/false,
16236 /*is_declaration=*/true);
16237 /* Get the namespace being used. */
16238 namespace_decl = cp_parser_namespace_name (parser);
16239 /* And any specified attributes. */
16240 attribs = cp_parser_attributes_opt (parser);
16241 /* Update the symbol table. */
16242 parse_using_directive (namespace_decl, attribs);
16243 /* Look for the final `;'. */
16244 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16247 /* Parse an asm-definition.
16249 asm-definition:
16250 asm ( string-literal ) ;
16252 GNU Extension:
16254 asm-definition:
16255 asm volatile [opt] ( string-literal ) ;
16256 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16257 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16258 : asm-operand-list [opt] ) ;
16259 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16260 : asm-operand-list [opt]
16261 : asm-clobber-list [opt] ) ;
16262 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16263 : asm-clobber-list [opt]
16264 : asm-goto-list ) ; */
16266 static void
16267 cp_parser_asm_definition (cp_parser* parser)
16269 tree string;
16270 tree outputs = NULL_TREE;
16271 tree inputs = NULL_TREE;
16272 tree clobbers = NULL_TREE;
16273 tree labels = NULL_TREE;
16274 tree asm_stmt;
16275 bool volatile_p = false;
16276 bool extended_p = false;
16277 bool invalid_inputs_p = false;
16278 bool invalid_outputs_p = false;
16279 bool goto_p = false;
16280 required_token missing = RT_NONE;
16282 /* Look for the `asm' keyword. */
16283 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16284 /* See if the next token is `volatile'. */
16285 if (cp_parser_allow_gnu_extensions_p (parser)
16286 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16288 /* Remember that we saw the `volatile' keyword. */
16289 volatile_p = true;
16290 /* Consume the token. */
16291 cp_lexer_consume_token (parser->lexer);
16293 if (cp_parser_allow_gnu_extensions_p (parser)
16294 && parser->in_function_body
16295 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16297 /* Remember that we saw the `goto' keyword. */
16298 goto_p = true;
16299 /* Consume the token. */
16300 cp_lexer_consume_token (parser->lexer);
16302 /* Look for the opening `('. */
16303 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16304 return;
16305 /* Look for the string. */
16306 string = cp_parser_string_literal (parser, false, false);
16307 if (string == error_mark_node)
16309 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16310 /*consume_paren=*/true);
16311 return;
16314 /* If we're allowing GNU extensions, check for the extended assembly
16315 syntax. Unfortunately, the `:' tokens need not be separated by
16316 a space in C, and so, for compatibility, we tolerate that here
16317 too. Doing that means that we have to treat the `::' operator as
16318 two `:' tokens. */
16319 if (cp_parser_allow_gnu_extensions_p (parser)
16320 && parser->in_function_body
16321 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16322 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16324 bool inputs_p = false;
16325 bool clobbers_p = false;
16326 bool labels_p = false;
16328 /* The extended syntax was used. */
16329 extended_p = true;
16331 /* Look for outputs. */
16332 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16334 /* Consume the `:'. */
16335 cp_lexer_consume_token (parser->lexer);
16336 /* Parse the output-operands. */
16337 if (cp_lexer_next_token_is_not (parser->lexer,
16338 CPP_COLON)
16339 && cp_lexer_next_token_is_not (parser->lexer,
16340 CPP_SCOPE)
16341 && cp_lexer_next_token_is_not (parser->lexer,
16342 CPP_CLOSE_PAREN)
16343 && !goto_p)
16344 outputs = cp_parser_asm_operand_list (parser);
16346 if (outputs == error_mark_node)
16347 invalid_outputs_p = true;
16349 /* If the next token is `::', there are no outputs, and the
16350 next token is the beginning of the inputs. */
16351 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16352 /* The inputs are coming next. */
16353 inputs_p = true;
16355 /* Look for inputs. */
16356 if (inputs_p
16357 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16359 /* Consume the `:' or `::'. */
16360 cp_lexer_consume_token (parser->lexer);
16361 /* Parse the output-operands. */
16362 if (cp_lexer_next_token_is_not (parser->lexer,
16363 CPP_COLON)
16364 && cp_lexer_next_token_is_not (parser->lexer,
16365 CPP_SCOPE)
16366 && cp_lexer_next_token_is_not (parser->lexer,
16367 CPP_CLOSE_PAREN))
16368 inputs = cp_parser_asm_operand_list (parser);
16370 if (inputs == error_mark_node)
16371 invalid_inputs_p = true;
16373 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16374 /* The clobbers are coming next. */
16375 clobbers_p = true;
16377 /* Look for clobbers. */
16378 if (clobbers_p
16379 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16381 clobbers_p = true;
16382 /* Consume the `:' or `::'. */
16383 cp_lexer_consume_token (parser->lexer);
16384 /* Parse the clobbers. */
16385 if (cp_lexer_next_token_is_not (parser->lexer,
16386 CPP_COLON)
16387 && cp_lexer_next_token_is_not (parser->lexer,
16388 CPP_CLOSE_PAREN))
16389 clobbers = cp_parser_asm_clobber_list (parser);
16391 else if (goto_p
16392 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16393 /* The labels are coming next. */
16394 labels_p = true;
16396 /* Look for labels. */
16397 if (labels_p
16398 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16400 labels_p = true;
16401 /* Consume the `:' or `::'. */
16402 cp_lexer_consume_token (parser->lexer);
16403 /* Parse the labels. */
16404 labels = cp_parser_asm_label_list (parser);
16407 if (goto_p && !labels_p)
16408 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16410 else if (goto_p)
16411 missing = RT_COLON_SCOPE;
16413 /* Look for the closing `)'. */
16414 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16415 missing ? missing : RT_CLOSE_PAREN))
16416 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16417 /*consume_paren=*/true);
16418 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16420 if (!invalid_inputs_p && !invalid_outputs_p)
16422 /* Create the ASM_EXPR. */
16423 if (parser->in_function_body)
16425 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16426 inputs, clobbers, labels);
16427 /* If the extended syntax was not used, mark the ASM_EXPR. */
16428 if (!extended_p)
16430 tree temp = asm_stmt;
16431 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16432 temp = TREE_OPERAND (temp, 0);
16434 ASM_INPUT_P (temp) = 1;
16437 else
16438 add_asm_node (string);
16442 /* Declarators [gram.dcl.decl] */
16444 /* Parse an init-declarator.
16446 init-declarator:
16447 declarator initializer [opt]
16449 GNU Extension:
16451 init-declarator:
16452 declarator asm-specification [opt] attributes [opt] initializer [opt]
16454 function-definition:
16455 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16456 function-body
16457 decl-specifier-seq [opt] declarator function-try-block
16459 GNU Extension:
16461 function-definition:
16462 __extension__ function-definition
16464 TM Extension:
16466 function-definition:
16467 decl-specifier-seq [opt] declarator function-transaction-block
16469 The DECL_SPECIFIERS apply to this declarator. Returns a
16470 representation of the entity declared. If MEMBER_P is TRUE, then
16471 this declarator appears in a class scope. The new DECL created by
16472 this declarator is returned.
16474 The CHECKS are access checks that should be performed once we know
16475 what entity is being declared (and, therefore, what classes have
16476 befriended it).
16478 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16479 for a function-definition here as well. If the declarator is a
16480 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16481 be TRUE upon return. By that point, the function-definition will
16482 have been completely parsed.
16484 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16485 is FALSE.
16487 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16488 parsed declaration if it is an uninitialized single declarator not followed
16489 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16490 if present, will not be consumed. If returned, this declarator will be
16491 created with SD_INITIALIZED but will not call cp_finish_decl. */
16493 static tree
16494 cp_parser_init_declarator (cp_parser* parser,
16495 cp_decl_specifier_seq *decl_specifiers,
16496 vec<deferred_access_check, va_gc> *checks,
16497 bool function_definition_allowed_p,
16498 bool member_p,
16499 int declares_class_or_enum,
16500 bool* function_definition_p,
16501 tree* maybe_range_for_decl)
16503 cp_token *token = NULL, *asm_spec_start_token = NULL,
16504 *attributes_start_token = NULL;
16505 cp_declarator *declarator;
16506 tree prefix_attributes;
16507 tree attributes = NULL;
16508 tree asm_specification;
16509 tree initializer;
16510 tree decl = NULL_TREE;
16511 tree scope;
16512 int is_initialized;
16513 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16514 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16515 "(...)". */
16516 enum cpp_ttype initialization_kind;
16517 bool is_direct_init = false;
16518 bool is_non_constant_init;
16519 int ctor_dtor_or_conv_p;
16520 bool friend_p;
16521 tree pushed_scope = NULL_TREE;
16522 bool range_for_decl_p = false;
16523 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16525 /* Gather the attributes that were provided with the
16526 decl-specifiers. */
16527 prefix_attributes = decl_specifiers->attributes;
16529 /* Assume that this is not the declarator for a function
16530 definition. */
16531 if (function_definition_p)
16532 *function_definition_p = false;
16534 /* Default arguments are only permitted for function parameters. */
16535 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16536 parser->default_arg_ok_p = false;
16538 /* Defer access checks while parsing the declarator; we cannot know
16539 what names are accessible until we know what is being
16540 declared. */
16541 resume_deferring_access_checks ();
16543 /* Parse the declarator. */
16544 token = cp_lexer_peek_token (parser->lexer);
16545 declarator
16546 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16547 &ctor_dtor_or_conv_p,
16548 /*parenthesized_p=*/NULL,
16549 member_p);
16550 /* Gather up the deferred checks. */
16551 stop_deferring_access_checks ();
16553 parser->default_arg_ok_p = saved_default_arg_ok_p;
16555 /* If the DECLARATOR was erroneous, there's no need to go
16556 further. */
16557 if (declarator == cp_error_declarator)
16558 return error_mark_node;
16560 /* Check that the number of template-parameter-lists is OK. */
16561 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16562 token->location))
16563 return error_mark_node;
16565 if (declares_class_or_enum & 2)
16566 cp_parser_check_for_definition_in_return_type (declarator,
16567 decl_specifiers->type,
16568 decl_specifiers->locations[ds_type_spec]);
16570 /* Figure out what scope the entity declared by the DECLARATOR is
16571 located in. `grokdeclarator' sometimes changes the scope, so
16572 we compute it now. */
16573 scope = get_scope_of_declarator (declarator);
16575 /* Perform any lookups in the declared type which were thought to be
16576 dependent, but are not in the scope of the declarator. */
16577 decl_specifiers->type
16578 = maybe_update_decl_type (decl_specifiers->type, scope);
16580 /* If we're allowing GNU extensions, look for an
16581 asm-specification. */
16582 if (cp_parser_allow_gnu_extensions_p (parser))
16584 /* Look for an asm-specification. */
16585 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16586 asm_specification = cp_parser_asm_specification_opt (parser);
16588 else
16589 asm_specification = NULL_TREE;
16591 /* Look for attributes. */
16592 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16593 attributes = cp_parser_attributes_opt (parser);
16595 /* Peek at the next token. */
16596 token = cp_lexer_peek_token (parser->lexer);
16598 if (function_declarator_p (declarator))
16600 /* Check to see if the token indicates the start of a
16601 function-definition. */
16602 if (cp_parser_token_starts_function_definition_p (token))
16604 if (!function_definition_allowed_p)
16606 /* If a function-definition should not appear here, issue an
16607 error message. */
16608 cp_parser_error (parser,
16609 "a function-definition is not allowed here");
16610 return error_mark_node;
16613 location_t func_brace_location
16614 = cp_lexer_peek_token (parser->lexer)->location;
16616 /* Neither attributes nor an asm-specification are allowed
16617 on a function-definition. */
16618 if (asm_specification)
16619 error_at (asm_spec_start_token->location,
16620 "an asm-specification is not allowed "
16621 "on a function-definition");
16622 if (attributes)
16623 error_at (attributes_start_token->location,
16624 "attributes are not allowed "
16625 "on a function-definition");
16626 /* This is a function-definition. */
16627 *function_definition_p = true;
16629 /* Parse the function definition. */
16630 if (member_p)
16631 decl = cp_parser_save_member_function_body (parser,
16632 decl_specifiers,
16633 declarator,
16634 prefix_attributes);
16635 else
16636 decl =
16637 (cp_parser_function_definition_from_specifiers_and_declarator
16638 (parser, decl_specifiers, prefix_attributes, declarator));
16640 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16642 /* This is where the prologue starts... */
16643 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16644 = func_brace_location;
16647 return decl;
16651 /* [dcl.dcl]
16653 Only in function declarations for constructors, destructors, and
16654 type conversions can the decl-specifier-seq be omitted.
16656 We explicitly postpone this check past the point where we handle
16657 function-definitions because we tolerate function-definitions
16658 that are missing their return types in some modes. */
16659 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16661 cp_parser_error (parser,
16662 "expected constructor, destructor, or type conversion");
16663 return error_mark_node;
16666 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16667 if (token->type == CPP_EQ
16668 || token->type == CPP_OPEN_PAREN
16669 || token->type == CPP_OPEN_BRACE)
16671 is_initialized = SD_INITIALIZED;
16672 initialization_kind = token->type;
16673 if (maybe_range_for_decl)
16674 *maybe_range_for_decl = error_mark_node;
16676 if (token->type == CPP_EQ
16677 && function_declarator_p (declarator))
16679 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16680 if (t2->keyword == RID_DEFAULT)
16681 is_initialized = SD_DEFAULTED;
16682 else if (t2->keyword == RID_DELETE)
16683 is_initialized = SD_DELETED;
16686 else
16688 /* If the init-declarator isn't initialized and isn't followed by a
16689 `,' or `;', it's not a valid init-declarator. */
16690 if (token->type != CPP_COMMA
16691 && token->type != CPP_SEMICOLON)
16693 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16694 range_for_decl_p = true;
16695 else
16697 cp_parser_error (parser, "expected initializer");
16698 return error_mark_node;
16701 is_initialized = SD_UNINITIALIZED;
16702 initialization_kind = CPP_EOF;
16705 /* Because start_decl has side-effects, we should only call it if we
16706 know we're going ahead. By this point, we know that we cannot
16707 possibly be looking at any other construct. */
16708 cp_parser_commit_to_tentative_parse (parser);
16710 /* If the decl specifiers were bad, issue an error now that we're
16711 sure this was intended to be a declarator. Then continue
16712 declaring the variable(s), as int, to try to cut down on further
16713 errors. */
16714 if (decl_specifiers->any_specifiers_p
16715 && decl_specifiers->type == error_mark_node)
16717 cp_parser_error (parser, "invalid type in declaration");
16718 decl_specifiers->type = integer_type_node;
16721 /* Check to see whether or not this declaration is a friend. */
16722 friend_p = cp_parser_friend_p (decl_specifiers);
16724 /* Enter the newly declared entry in the symbol table. If we're
16725 processing a declaration in a class-specifier, we wait until
16726 after processing the initializer. */
16727 if (!member_p)
16729 if (parser->in_unbraced_linkage_specification_p)
16730 decl_specifiers->storage_class = sc_extern;
16731 decl = start_decl (declarator, decl_specifiers,
16732 range_for_decl_p? SD_INITIALIZED : is_initialized,
16733 attributes, prefix_attributes, &pushed_scope);
16734 cp_finalize_omp_declare_simd (parser, decl);
16735 /* Adjust location of decl if declarator->id_loc is more appropriate:
16736 set, and decl wasn't merged with another decl, in which case its
16737 location would be different from input_location, and more accurate. */
16738 if (DECL_P (decl)
16739 && declarator->id_loc != UNKNOWN_LOCATION
16740 && DECL_SOURCE_LOCATION (decl) == input_location)
16741 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16743 else if (scope)
16744 /* Enter the SCOPE. That way unqualified names appearing in the
16745 initializer will be looked up in SCOPE. */
16746 pushed_scope = push_scope (scope);
16748 /* Perform deferred access control checks, now that we know in which
16749 SCOPE the declared entity resides. */
16750 if (!member_p && decl)
16752 tree saved_current_function_decl = NULL_TREE;
16754 /* If the entity being declared is a function, pretend that we
16755 are in its scope. If it is a `friend', it may have access to
16756 things that would not otherwise be accessible. */
16757 if (TREE_CODE (decl) == FUNCTION_DECL)
16759 saved_current_function_decl = current_function_decl;
16760 current_function_decl = decl;
16763 /* Perform access checks for template parameters. */
16764 cp_parser_perform_template_parameter_access_checks (checks);
16766 /* Perform the access control checks for the declarator and the
16767 decl-specifiers. */
16768 perform_deferred_access_checks (tf_warning_or_error);
16770 /* Restore the saved value. */
16771 if (TREE_CODE (decl) == FUNCTION_DECL)
16772 current_function_decl = saved_current_function_decl;
16775 /* Parse the initializer. */
16776 initializer = NULL_TREE;
16777 is_direct_init = false;
16778 is_non_constant_init = true;
16779 if (is_initialized)
16781 if (function_declarator_p (declarator))
16783 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16784 if (initialization_kind == CPP_EQ)
16785 initializer = cp_parser_pure_specifier (parser);
16786 else
16788 /* If the declaration was erroneous, we don't really
16789 know what the user intended, so just silently
16790 consume the initializer. */
16791 if (decl != error_mark_node)
16792 error_at (initializer_start_token->location,
16793 "initializer provided for function");
16794 cp_parser_skip_to_closing_parenthesis (parser,
16795 /*recovering=*/true,
16796 /*or_comma=*/false,
16797 /*consume_paren=*/true);
16800 else
16802 /* We want to record the extra mangling scope for in-class
16803 initializers of class members and initializers of static data
16804 member templates. The former involves deferring
16805 parsing of the initializer until end of class as with default
16806 arguments. So right here we only handle the latter. */
16807 if (!member_p && processing_template_decl)
16808 start_lambda_scope (decl);
16809 initializer = cp_parser_initializer (parser,
16810 &is_direct_init,
16811 &is_non_constant_init);
16812 if (!member_p && processing_template_decl)
16813 finish_lambda_scope ();
16814 if (initializer == error_mark_node)
16815 cp_parser_skip_to_end_of_statement (parser);
16819 /* The old parser allows attributes to appear after a parenthesized
16820 initializer. Mark Mitchell proposed removing this functionality
16821 on the GCC mailing lists on 2002-08-13. This parser accepts the
16822 attributes -- but ignores them. */
16823 if (cp_parser_allow_gnu_extensions_p (parser)
16824 && initialization_kind == CPP_OPEN_PAREN)
16825 if (cp_parser_attributes_opt (parser))
16826 warning (OPT_Wattributes,
16827 "attributes after parenthesized initializer ignored");
16829 /* A non-template declaration involving a function parameter list containing
16830 an implicit template parameter will have been made into a template. If it
16831 turns out that the resulting declaration is not an actual function then
16832 finish the template declaration here. An error message will already have
16833 been issued. */
16834 if (parser->fully_implicit_function_template_p)
16835 if (!function_declarator_p (declarator))
16837 if (pushed_scope)
16839 pop_scope (pushed_scope);
16840 pushed_scope = 0;
16842 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16845 /* For an in-class declaration, use `grokfield' to create the
16846 declaration. */
16847 if (member_p)
16849 if (pushed_scope)
16851 pop_scope (pushed_scope);
16852 pushed_scope = NULL_TREE;
16854 decl = grokfield (declarator, decl_specifiers,
16855 initializer, !is_non_constant_init,
16856 /*asmspec=*/NULL_TREE,
16857 chainon (attributes, prefix_attributes));
16858 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16859 cp_parser_save_default_args (parser, decl);
16860 cp_finalize_omp_declare_simd (parser, decl);
16863 /* Finish processing the declaration. But, skip member
16864 declarations. */
16865 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16867 cp_finish_decl (decl,
16868 initializer, !is_non_constant_init,
16869 asm_specification,
16870 /* If the initializer is in parentheses, then this is
16871 a direct-initialization, which means that an
16872 `explicit' constructor is OK. Otherwise, an
16873 `explicit' constructor cannot be used. */
16874 ((is_direct_init || !is_initialized)
16875 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16877 else if ((cxx_dialect != cxx98) && friend_p
16878 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16879 /* Core issue #226 (C++0x only): A default template-argument
16880 shall not be specified in a friend class template
16881 declaration. */
16882 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16883 /*is_partial=*/false, /*is_friend_decl=*/1);
16885 if (!friend_p && pushed_scope)
16886 pop_scope (pushed_scope);
16888 if (function_declarator_p (declarator)
16889 && parser->fully_implicit_function_template_p)
16891 if (member_p)
16892 decl = finish_fully_implicit_template (parser, decl);
16893 else
16894 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
16897 return decl;
16900 /* Parse a declarator.
16902 declarator:
16903 direct-declarator
16904 ptr-operator declarator
16906 abstract-declarator:
16907 ptr-operator abstract-declarator [opt]
16908 direct-abstract-declarator
16910 GNU Extensions:
16912 declarator:
16913 attributes [opt] direct-declarator
16914 attributes [opt] ptr-operator declarator
16916 abstract-declarator:
16917 attributes [opt] ptr-operator abstract-declarator [opt]
16918 attributes [opt] direct-abstract-declarator
16920 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16921 detect constructor, destructor or conversion operators. It is set
16922 to -1 if the declarator is a name, and +1 if it is a
16923 function. Otherwise it is set to zero. Usually you just want to
16924 test for >0, but internally the negative value is used.
16926 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16927 a decl-specifier-seq unless it declares a constructor, destructor,
16928 or conversion. It might seem that we could check this condition in
16929 semantic analysis, rather than parsing, but that makes it difficult
16930 to handle something like `f()'. We want to notice that there are
16931 no decl-specifiers, and therefore realize that this is an
16932 expression, not a declaration.)
16934 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16935 the declarator is a direct-declarator of the form "(...)".
16937 MEMBER_P is true iff this declarator is a member-declarator. */
16939 static cp_declarator *
16940 cp_parser_declarator (cp_parser* parser,
16941 cp_parser_declarator_kind dcl_kind,
16942 int* ctor_dtor_or_conv_p,
16943 bool* parenthesized_p,
16944 bool member_p)
16946 cp_declarator *declarator;
16947 enum tree_code code;
16948 cp_cv_quals cv_quals;
16949 tree class_type;
16950 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16952 /* Assume this is not a constructor, destructor, or type-conversion
16953 operator. */
16954 if (ctor_dtor_or_conv_p)
16955 *ctor_dtor_or_conv_p = 0;
16957 if (cp_parser_allow_gnu_extensions_p (parser))
16958 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16960 /* Check for the ptr-operator production. */
16961 cp_parser_parse_tentatively (parser);
16962 /* Parse the ptr-operator. */
16963 code = cp_parser_ptr_operator (parser,
16964 &class_type,
16965 &cv_quals,
16966 &std_attributes);
16968 /* If that worked, then we have a ptr-operator. */
16969 if (cp_parser_parse_definitely (parser))
16971 /* If a ptr-operator was found, then this declarator was not
16972 parenthesized. */
16973 if (parenthesized_p)
16974 *parenthesized_p = true;
16975 /* The dependent declarator is optional if we are parsing an
16976 abstract-declarator. */
16977 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16978 cp_parser_parse_tentatively (parser);
16980 /* Parse the dependent declarator. */
16981 declarator = cp_parser_declarator (parser, dcl_kind,
16982 /*ctor_dtor_or_conv_p=*/NULL,
16983 /*parenthesized_p=*/NULL,
16984 /*member_p=*/false);
16986 /* If we are parsing an abstract-declarator, we must handle the
16987 case where the dependent declarator is absent. */
16988 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16989 && !cp_parser_parse_definitely (parser))
16990 declarator = NULL;
16992 declarator = cp_parser_make_indirect_declarator
16993 (code, class_type, cv_quals, declarator, std_attributes);
16995 /* Everything else is a direct-declarator. */
16996 else
16998 if (parenthesized_p)
16999 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17000 CPP_OPEN_PAREN);
17001 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17002 ctor_dtor_or_conv_p,
17003 member_p);
17006 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17007 declarator->attributes = gnu_attributes;
17008 return declarator;
17011 /* Parse a direct-declarator or direct-abstract-declarator.
17013 direct-declarator:
17014 declarator-id
17015 direct-declarator ( parameter-declaration-clause )
17016 cv-qualifier-seq [opt]
17017 ref-qualifier [opt]
17018 exception-specification [opt]
17019 direct-declarator [ constant-expression [opt] ]
17020 ( declarator )
17022 direct-abstract-declarator:
17023 direct-abstract-declarator [opt]
17024 ( parameter-declaration-clause )
17025 cv-qualifier-seq [opt]
17026 ref-qualifier [opt]
17027 exception-specification [opt]
17028 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17029 ( abstract-declarator )
17031 Returns a representation of the declarator. DCL_KIND is
17032 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17033 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17034 we are parsing a direct-declarator. It is
17035 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17036 of ambiguity we prefer an abstract declarator, as per
17037 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
17038 cp_parser_declarator. */
17040 static cp_declarator *
17041 cp_parser_direct_declarator (cp_parser* parser,
17042 cp_parser_declarator_kind dcl_kind,
17043 int* ctor_dtor_or_conv_p,
17044 bool member_p)
17046 cp_token *token;
17047 cp_declarator *declarator = NULL;
17048 tree scope = NULL_TREE;
17049 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17050 bool saved_in_declarator_p = parser->in_declarator_p;
17051 bool first = true;
17052 tree pushed_scope = NULL_TREE;
17054 while (true)
17056 /* Peek at the next token. */
17057 token = cp_lexer_peek_token (parser->lexer);
17058 if (token->type == CPP_OPEN_PAREN)
17060 /* This is either a parameter-declaration-clause, or a
17061 parenthesized declarator. When we know we are parsing a
17062 named declarator, it must be a parenthesized declarator
17063 if FIRST is true. For instance, `(int)' is a
17064 parameter-declaration-clause, with an omitted
17065 direct-abstract-declarator. But `((*))', is a
17066 parenthesized abstract declarator. Finally, when T is a
17067 template parameter `(T)' is a
17068 parameter-declaration-clause, and not a parenthesized
17069 named declarator.
17071 We first try and parse a parameter-declaration-clause,
17072 and then try a nested declarator (if FIRST is true).
17074 It is not an error for it not to be a
17075 parameter-declaration-clause, even when FIRST is
17076 false. Consider,
17078 int i (int);
17079 int i (3);
17081 The first is the declaration of a function while the
17082 second is the definition of a variable, including its
17083 initializer.
17085 Having seen only the parenthesis, we cannot know which of
17086 these two alternatives should be selected. Even more
17087 complex are examples like:
17089 int i (int (a));
17090 int i (int (3));
17092 The former is a function-declaration; the latter is a
17093 variable initialization.
17095 Thus again, we try a parameter-declaration-clause, and if
17096 that fails, we back out and return. */
17098 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17100 tree params;
17101 bool is_declarator = false;
17103 /* In a member-declarator, the only valid interpretation
17104 of a parenthesis is the start of a
17105 parameter-declaration-clause. (It is invalid to
17106 initialize a static data member with a parenthesized
17107 initializer; only the "=" form of initialization is
17108 permitted.) */
17109 if (!member_p)
17110 cp_parser_parse_tentatively (parser);
17112 /* Consume the `('. */
17113 cp_lexer_consume_token (parser->lexer);
17114 if (first)
17116 /* If this is going to be an abstract declarator, we're
17117 in a declarator and we can't have default args. */
17118 parser->default_arg_ok_p = false;
17119 parser->in_declarator_p = true;
17122 begin_scope (sk_function_parms, NULL_TREE);
17124 /* Parse the parameter-declaration-clause. */
17125 params = cp_parser_parameter_declaration_clause (parser);
17127 /* Consume the `)'. */
17128 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17130 /* If all went well, parse the cv-qualifier-seq,
17131 ref-qualifier and the exception-specification. */
17132 if (member_p || cp_parser_parse_definitely (parser))
17134 cp_cv_quals cv_quals;
17135 cp_virt_specifiers virt_specifiers;
17136 cp_ref_qualifier ref_qual;
17137 tree exception_specification;
17138 tree late_return;
17139 tree attrs;
17140 bool memfn = (member_p || (pushed_scope
17141 && CLASS_TYPE_P (pushed_scope)));
17143 is_declarator = true;
17145 if (ctor_dtor_or_conv_p)
17146 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17147 first = false;
17149 /* Parse the cv-qualifier-seq. */
17150 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17151 /* Parse the ref-qualifier. */
17152 ref_qual = cp_parser_ref_qualifier_opt (parser);
17153 /* And the exception-specification. */
17154 exception_specification
17155 = cp_parser_exception_specification_opt (parser);
17157 attrs = cp_parser_std_attribute_spec_seq (parser);
17159 /* In here, we handle cases where attribute is used after
17160 the function declaration. For example:
17161 void func (int x) __attribute__((vector(..))); */
17162 if (flag_cilkplus
17163 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17165 cp_parser_parse_tentatively (parser);
17166 tree attr = cp_parser_gnu_attributes_opt (parser);
17167 if (cp_lexer_next_token_is_not (parser->lexer,
17168 CPP_SEMICOLON)
17169 && cp_lexer_next_token_is_not (parser->lexer,
17170 CPP_OPEN_BRACE))
17171 cp_parser_abort_tentative_parse (parser);
17172 else if (!cp_parser_parse_definitely (parser))
17174 else
17175 attrs = chainon (attr, attrs);
17177 late_return = (cp_parser_late_return_type_opt
17178 (parser, declarator,
17179 memfn ? cv_quals : -1));
17182 /* Parse the virt-specifier-seq. */
17183 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17185 /* Create the function-declarator. */
17186 declarator = make_call_declarator (declarator,
17187 params,
17188 cv_quals,
17189 virt_specifiers,
17190 ref_qual,
17191 exception_specification,
17192 late_return);
17193 declarator->std_attributes = attrs;
17194 /* Any subsequent parameter lists are to do with
17195 return type, so are not those of the declared
17196 function. */
17197 parser->default_arg_ok_p = false;
17200 /* Remove the function parms from scope. */
17201 pop_bindings_and_leave_scope ();
17203 if (is_declarator)
17204 /* Repeat the main loop. */
17205 continue;
17208 /* If this is the first, we can try a parenthesized
17209 declarator. */
17210 if (first)
17212 bool saved_in_type_id_in_expr_p;
17214 parser->default_arg_ok_p = saved_default_arg_ok_p;
17215 parser->in_declarator_p = saved_in_declarator_p;
17217 /* Consume the `('. */
17218 cp_lexer_consume_token (parser->lexer);
17219 /* Parse the nested declarator. */
17220 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17221 parser->in_type_id_in_expr_p = true;
17222 declarator
17223 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17224 /*parenthesized_p=*/NULL,
17225 member_p);
17226 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17227 first = false;
17228 /* Expect a `)'. */
17229 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17230 declarator = cp_error_declarator;
17231 if (declarator == cp_error_declarator)
17232 break;
17234 goto handle_declarator;
17236 /* Otherwise, we must be done. */
17237 else
17238 break;
17240 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17241 && token->type == CPP_OPEN_SQUARE
17242 && !cp_next_tokens_can_be_attribute_p (parser))
17244 /* Parse an array-declarator. */
17245 tree bounds, attrs;
17247 if (ctor_dtor_or_conv_p)
17248 *ctor_dtor_or_conv_p = 0;
17250 first = false;
17251 parser->default_arg_ok_p = false;
17252 parser->in_declarator_p = true;
17253 /* Consume the `['. */
17254 cp_lexer_consume_token (parser->lexer);
17255 /* Peek at the next token. */
17256 token = cp_lexer_peek_token (parser->lexer);
17257 /* If the next token is `]', then there is no
17258 constant-expression. */
17259 if (token->type != CPP_CLOSE_SQUARE)
17261 bool non_constant_p;
17262 bounds
17263 = cp_parser_constant_expression (parser,
17264 /*allow_non_constant=*/true,
17265 &non_constant_p);
17266 if (!non_constant_p)
17267 /* OK */;
17268 else if (error_operand_p (bounds))
17269 /* Already gave an error. */;
17270 else if (!parser->in_function_body
17271 || current_binding_level->kind == sk_function_parms)
17273 /* Normally, the array bound must be an integral constant
17274 expression. However, as an extension, we allow VLAs
17275 in function scopes as long as they aren't part of a
17276 parameter declaration. */
17277 cp_parser_error (parser,
17278 "array bound is not an integer constant");
17279 bounds = error_mark_node;
17281 else if (processing_template_decl
17282 && !type_dependent_expression_p (bounds))
17284 /* Remember this wasn't a constant-expression. */
17285 bounds = build_nop (TREE_TYPE (bounds), bounds);
17286 TREE_SIDE_EFFECTS (bounds) = 1;
17289 else
17290 bounds = NULL_TREE;
17291 /* Look for the closing `]'. */
17292 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17294 declarator = cp_error_declarator;
17295 break;
17298 attrs = cp_parser_std_attribute_spec_seq (parser);
17299 declarator = make_array_declarator (declarator, bounds);
17300 declarator->std_attributes = attrs;
17302 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17305 tree qualifying_scope;
17306 tree unqualified_name;
17307 tree attrs;
17308 special_function_kind sfk;
17309 bool abstract_ok;
17310 bool pack_expansion_p = false;
17311 cp_token *declarator_id_start_token;
17313 /* Parse a declarator-id */
17314 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17315 if (abstract_ok)
17317 cp_parser_parse_tentatively (parser);
17319 /* If we see an ellipsis, we should be looking at a
17320 parameter pack. */
17321 if (token->type == CPP_ELLIPSIS)
17323 /* Consume the `...' */
17324 cp_lexer_consume_token (parser->lexer);
17326 pack_expansion_p = true;
17330 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17331 unqualified_name
17332 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17333 qualifying_scope = parser->scope;
17334 if (abstract_ok)
17336 bool okay = false;
17338 if (!unqualified_name && pack_expansion_p)
17340 /* Check whether an error occurred. */
17341 okay = !cp_parser_error_occurred (parser);
17343 /* We already consumed the ellipsis to mark a
17344 parameter pack, but we have no way to report it,
17345 so abort the tentative parse. We will be exiting
17346 immediately anyway. */
17347 cp_parser_abort_tentative_parse (parser);
17349 else
17350 okay = cp_parser_parse_definitely (parser);
17352 if (!okay)
17353 unqualified_name = error_mark_node;
17354 else if (unqualified_name
17355 && (qualifying_scope
17356 || (!identifier_p (unqualified_name))))
17358 cp_parser_error (parser, "expected unqualified-id");
17359 unqualified_name = error_mark_node;
17363 if (!unqualified_name)
17364 return NULL;
17365 if (unqualified_name == error_mark_node)
17367 declarator = cp_error_declarator;
17368 pack_expansion_p = false;
17369 declarator->parameter_pack_p = false;
17370 break;
17373 attrs = cp_parser_std_attribute_spec_seq (parser);
17375 if (qualifying_scope && at_namespace_scope_p ()
17376 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17378 /* In the declaration of a member of a template class
17379 outside of the class itself, the SCOPE will sometimes
17380 be a TYPENAME_TYPE. For example, given:
17382 template <typename T>
17383 int S<T>::R::i = 3;
17385 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17386 this context, we must resolve S<T>::R to an ordinary
17387 type, rather than a typename type.
17389 The reason we normally avoid resolving TYPENAME_TYPEs
17390 is that a specialization of `S' might render
17391 `S<T>::R' not a type. However, if `S' is
17392 specialized, then this `i' will not be used, so there
17393 is no harm in resolving the types here. */
17394 tree type;
17396 /* Resolve the TYPENAME_TYPE. */
17397 type = resolve_typename_type (qualifying_scope,
17398 /*only_current_p=*/false);
17399 /* If that failed, the declarator is invalid. */
17400 if (TREE_CODE (type) == TYPENAME_TYPE)
17402 if (typedef_variant_p (type))
17403 error_at (declarator_id_start_token->location,
17404 "cannot define member of dependent typedef "
17405 "%qT", type);
17406 else
17407 error_at (declarator_id_start_token->location,
17408 "%<%T::%E%> is not a type",
17409 TYPE_CONTEXT (qualifying_scope),
17410 TYPE_IDENTIFIER (qualifying_scope));
17412 qualifying_scope = type;
17415 sfk = sfk_none;
17417 if (unqualified_name)
17419 tree class_type;
17421 if (qualifying_scope
17422 && CLASS_TYPE_P (qualifying_scope))
17423 class_type = qualifying_scope;
17424 else
17425 class_type = current_class_type;
17427 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17429 tree name_type = TREE_TYPE (unqualified_name);
17430 if (class_type && same_type_p (name_type, class_type))
17432 if (qualifying_scope
17433 && CLASSTYPE_USE_TEMPLATE (name_type))
17435 error_at (declarator_id_start_token->location,
17436 "invalid use of constructor as a template");
17437 inform (declarator_id_start_token->location,
17438 "use %<%T::%D%> instead of %<%T::%D%> to "
17439 "name the constructor in a qualified name",
17440 class_type,
17441 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17442 class_type, name_type);
17443 declarator = cp_error_declarator;
17444 break;
17446 else
17447 unqualified_name = constructor_name (class_type);
17449 else
17451 /* We do not attempt to print the declarator
17452 here because we do not have enough
17453 information about its original syntactic
17454 form. */
17455 cp_parser_error (parser, "invalid declarator");
17456 declarator = cp_error_declarator;
17457 break;
17461 if (class_type)
17463 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17464 sfk = sfk_destructor;
17465 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17466 sfk = sfk_conversion;
17467 else if (/* There's no way to declare a constructor
17468 for an anonymous type, even if the type
17469 got a name for linkage purposes. */
17470 !TYPE_WAS_ANONYMOUS (class_type)
17471 && constructor_name_p (unqualified_name,
17472 class_type))
17474 unqualified_name = constructor_name (class_type);
17475 sfk = sfk_constructor;
17477 else if (is_overloaded_fn (unqualified_name)
17478 && DECL_CONSTRUCTOR_P (get_first_fn
17479 (unqualified_name)))
17480 sfk = sfk_constructor;
17482 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17483 *ctor_dtor_or_conv_p = -1;
17486 declarator = make_id_declarator (qualifying_scope,
17487 unqualified_name,
17488 sfk);
17489 declarator->std_attributes = attrs;
17490 declarator->id_loc = token->location;
17491 declarator->parameter_pack_p = pack_expansion_p;
17493 if (pack_expansion_p)
17494 maybe_warn_variadic_templates ();
17497 handle_declarator:;
17498 scope = get_scope_of_declarator (declarator);
17499 if (scope)
17501 /* Any names that appear after the declarator-id for a
17502 member are looked up in the containing scope. */
17503 if (at_function_scope_p ())
17505 /* But declarations with qualified-ids can't appear in a
17506 function. */
17507 cp_parser_error (parser, "qualified-id in declaration");
17508 declarator = cp_error_declarator;
17509 break;
17511 pushed_scope = push_scope (scope);
17513 parser->in_declarator_p = true;
17514 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17515 || (declarator && declarator->kind == cdk_id))
17516 /* Default args are only allowed on function
17517 declarations. */
17518 parser->default_arg_ok_p = saved_default_arg_ok_p;
17519 else
17520 parser->default_arg_ok_p = false;
17522 first = false;
17524 /* We're done. */
17525 else
17526 break;
17529 /* For an abstract declarator, we might wind up with nothing at this
17530 point. That's an error; the declarator is not optional. */
17531 if (!declarator)
17532 cp_parser_error (parser, "expected declarator");
17534 /* If we entered a scope, we must exit it now. */
17535 if (pushed_scope)
17536 pop_scope (pushed_scope);
17538 parser->default_arg_ok_p = saved_default_arg_ok_p;
17539 parser->in_declarator_p = saved_in_declarator_p;
17541 return declarator;
17544 /* Parse a ptr-operator.
17546 ptr-operator:
17547 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17548 * cv-qualifier-seq [opt]
17550 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17551 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17553 GNU Extension:
17555 ptr-operator:
17556 & cv-qualifier-seq [opt]
17558 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17559 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17560 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17561 filled in with the TYPE containing the member. *CV_QUALS is
17562 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17563 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17564 Note that the tree codes returned by this function have nothing
17565 to do with the types of trees that will be eventually be created
17566 to represent the pointer or reference type being parsed. They are
17567 just constants with suggestive names. */
17568 static enum tree_code
17569 cp_parser_ptr_operator (cp_parser* parser,
17570 tree* type,
17571 cp_cv_quals *cv_quals,
17572 tree *attributes)
17574 enum tree_code code = ERROR_MARK;
17575 cp_token *token;
17576 tree attrs = NULL_TREE;
17578 /* Assume that it's not a pointer-to-member. */
17579 *type = NULL_TREE;
17580 /* And that there are no cv-qualifiers. */
17581 *cv_quals = TYPE_UNQUALIFIED;
17583 /* Peek at the next token. */
17584 token = cp_lexer_peek_token (parser->lexer);
17586 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17587 if (token->type == CPP_MULT)
17588 code = INDIRECT_REF;
17589 else if (token->type == CPP_AND)
17590 code = ADDR_EXPR;
17591 else if ((cxx_dialect != cxx98) &&
17592 token->type == CPP_AND_AND) /* C++0x only */
17593 code = NON_LVALUE_EXPR;
17595 if (code != ERROR_MARK)
17597 /* Consume the `*', `&' or `&&'. */
17598 cp_lexer_consume_token (parser->lexer);
17600 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17601 `&', if we are allowing GNU extensions. (The only qualifier
17602 that can legally appear after `&' is `restrict', but that is
17603 enforced during semantic analysis. */
17604 if (code == INDIRECT_REF
17605 || cp_parser_allow_gnu_extensions_p (parser))
17606 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17608 attrs = cp_parser_std_attribute_spec_seq (parser);
17609 if (attributes != NULL)
17610 *attributes = attrs;
17612 else
17614 /* Try the pointer-to-member case. */
17615 cp_parser_parse_tentatively (parser);
17616 /* Look for the optional `::' operator. */
17617 cp_parser_global_scope_opt (parser,
17618 /*current_scope_valid_p=*/false);
17619 /* Look for the nested-name specifier. */
17620 token = cp_lexer_peek_token (parser->lexer);
17621 cp_parser_nested_name_specifier (parser,
17622 /*typename_keyword_p=*/false,
17623 /*check_dependency_p=*/true,
17624 /*type_p=*/false,
17625 /*is_declaration=*/false);
17626 /* If we found it, and the next token is a `*', then we are
17627 indeed looking at a pointer-to-member operator. */
17628 if (!cp_parser_error_occurred (parser)
17629 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17631 /* Indicate that the `*' operator was used. */
17632 code = INDIRECT_REF;
17634 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17635 error_at (token->location, "%qD is a namespace", parser->scope);
17636 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17637 error_at (token->location, "cannot form pointer to member of "
17638 "non-class %q#T", parser->scope);
17639 else
17641 /* The type of which the member is a member is given by the
17642 current SCOPE. */
17643 *type = parser->scope;
17644 /* The next name will not be qualified. */
17645 parser->scope = NULL_TREE;
17646 parser->qualifying_scope = NULL_TREE;
17647 parser->object_scope = NULL_TREE;
17648 /* Look for optional c++11 attributes. */
17649 attrs = cp_parser_std_attribute_spec_seq (parser);
17650 if (attributes != NULL)
17651 *attributes = attrs;
17652 /* Look for the optional cv-qualifier-seq. */
17653 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17656 /* If that didn't work we don't have a ptr-operator. */
17657 if (!cp_parser_parse_definitely (parser))
17658 cp_parser_error (parser, "expected ptr-operator");
17661 return code;
17664 /* Parse an (optional) cv-qualifier-seq.
17666 cv-qualifier-seq:
17667 cv-qualifier cv-qualifier-seq [opt]
17669 cv-qualifier:
17670 const
17671 volatile
17673 GNU Extension:
17675 cv-qualifier:
17676 __restrict__
17678 Returns a bitmask representing the cv-qualifiers. */
17680 static cp_cv_quals
17681 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17683 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17685 while (true)
17687 cp_token *token;
17688 cp_cv_quals cv_qualifier;
17690 /* Peek at the next token. */
17691 token = cp_lexer_peek_token (parser->lexer);
17692 /* See if it's a cv-qualifier. */
17693 switch (token->keyword)
17695 case RID_CONST:
17696 cv_qualifier = TYPE_QUAL_CONST;
17697 break;
17699 case RID_VOLATILE:
17700 cv_qualifier = TYPE_QUAL_VOLATILE;
17701 break;
17703 case RID_RESTRICT:
17704 cv_qualifier = TYPE_QUAL_RESTRICT;
17705 break;
17707 default:
17708 cv_qualifier = TYPE_UNQUALIFIED;
17709 break;
17712 if (!cv_qualifier)
17713 break;
17715 if (cv_quals & cv_qualifier)
17717 error_at (token->location, "duplicate cv-qualifier");
17718 cp_lexer_purge_token (parser->lexer);
17720 else
17722 cp_lexer_consume_token (parser->lexer);
17723 cv_quals |= cv_qualifier;
17727 return cv_quals;
17730 /* Parse an (optional) ref-qualifier
17732 ref-qualifier:
17736 Returns cp_ref_qualifier representing ref-qualifier. */
17738 static cp_ref_qualifier
17739 cp_parser_ref_qualifier_opt (cp_parser* parser)
17741 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17743 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17744 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17745 return ref_qual;
17747 while (true)
17749 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17750 cp_token *token = cp_lexer_peek_token (parser->lexer);
17752 switch (token->type)
17754 case CPP_AND:
17755 curr_ref_qual = REF_QUAL_LVALUE;
17756 break;
17758 case CPP_AND_AND:
17759 curr_ref_qual = REF_QUAL_RVALUE;
17760 break;
17762 default:
17763 curr_ref_qual = REF_QUAL_NONE;
17764 break;
17767 if (!curr_ref_qual)
17768 break;
17769 else if (ref_qual)
17771 error_at (token->location, "multiple ref-qualifiers");
17772 cp_lexer_purge_token (parser->lexer);
17774 else
17776 ref_qual = curr_ref_qual;
17777 cp_lexer_consume_token (parser->lexer);
17781 return ref_qual;
17784 /* Parse an (optional) virt-specifier-seq.
17786 virt-specifier-seq:
17787 virt-specifier virt-specifier-seq [opt]
17789 virt-specifier:
17790 override
17791 final
17793 Returns a bitmask representing the virt-specifiers. */
17795 static cp_virt_specifiers
17796 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
17798 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17800 while (true)
17802 cp_token *token;
17803 cp_virt_specifiers virt_specifier;
17805 /* Peek at the next token. */
17806 token = cp_lexer_peek_token (parser->lexer);
17807 /* See if it's a virt-specifier-qualifier. */
17808 if (token->type != CPP_NAME)
17809 break;
17810 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
17812 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17813 virt_specifier = VIRT_SPEC_OVERRIDE;
17815 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
17817 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
17818 virt_specifier = VIRT_SPEC_FINAL;
17820 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
17822 virt_specifier = VIRT_SPEC_FINAL;
17824 else
17825 break;
17827 if (virt_specifiers & virt_specifier)
17829 error_at (token->location, "duplicate virt-specifier");
17830 cp_lexer_purge_token (parser->lexer);
17832 else
17834 cp_lexer_consume_token (parser->lexer);
17835 virt_specifiers |= virt_specifier;
17838 return virt_specifiers;
17841 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
17842 is in scope even though it isn't real. */
17844 static void
17845 inject_this_parameter (tree ctype, cp_cv_quals quals)
17847 tree this_parm;
17849 if (current_class_ptr)
17851 /* We don't clear this between NSDMIs. Is it already what we want? */
17852 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
17853 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
17854 && cp_type_quals (type) == quals)
17855 return;
17858 this_parm = build_this_parm (ctype, quals);
17859 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
17860 current_class_ptr = NULL_TREE;
17861 current_class_ref
17862 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
17863 current_class_ptr = this_parm;
17866 /* Return true iff our current scope is a non-static data member
17867 initializer. */
17869 bool
17870 parsing_nsdmi (void)
17872 /* We recognize NSDMI context by the context-less 'this' pointer set up
17873 by the function above. */
17874 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
17875 return true;
17876 return false;
17879 /* Parse a late-specified return type, if any. This is not a separate
17880 non-terminal, but part of a function declarator, which looks like
17882 -> trailing-type-specifier-seq abstract-declarator(opt)
17884 Returns the type indicated by the type-id.
17886 In addition to this this parses any queued up omp declare simd
17887 clauses and Cilk Plus SIMD-enabled function's vector attributes.
17889 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
17890 function. */
17892 static tree
17893 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
17894 cp_cv_quals quals)
17896 cp_token *token;
17897 tree type = NULL_TREE;
17898 bool declare_simd_p = (parser->omp_declare_simd
17899 && declarator
17900 && declarator->kind == cdk_id);
17902 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
17903 && declarator && declarator->kind == cdk_id);
17905 /* Peek at the next token. */
17906 token = cp_lexer_peek_token (parser->lexer);
17907 /* A late-specified return type is indicated by an initial '->'. */
17908 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
17909 return NULL_TREE;
17911 tree save_ccp = current_class_ptr;
17912 tree save_ccr = current_class_ref;
17913 if (quals >= 0)
17915 /* DR 1207: 'this' is in scope in the trailing return type. */
17916 inject_this_parameter (current_class_type, quals);
17919 if (token->type == CPP_DEREF)
17921 /* Consume the ->. */
17922 cp_lexer_consume_token (parser->lexer);
17924 type = cp_parser_trailing_type_id (parser);
17927 if (cilk_simd_fn_vector_p)
17928 declarator->std_attributes
17929 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
17930 declarator->std_attributes);
17931 if (declare_simd_p)
17932 declarator->std_attributes
17933 = cp_parser_late_parsing_omp_declare_simd (parser,
17934 declarator->std_attributes);
17936 if (quals >= 0)
17938 current_class_ptr = save_ccp;
17939 current_class_ref = save_ccr;
17942 return type;
17945 /* Parse a declarator-id.
17947 declarator-id:
17948 id-expression
17949 :: [opt] nested-name-specifier [opt] type-name
17951 In the `id-expression' case, the value returned is as for
17952 cp_parser_id_expression if the id-expression was an unqualified-id.
17953 If the id-expression was a qualified-id, then a SCOPE_REF is
17954 returned. The first operand is the scope (either a NAMESPACE_DECL
17955 or TREE_TYPE), but the second is still just a representation of an
17956 unqualified-id. */
17958 static tree
17959 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
17961 tree id;
17962 /* The expression must be an id-expression. Assume that qualified
17963 names are the names of types so that:
17965 template <class T>
17966 int S<T>::R::i = 3;
17968 will work; we must treat `S<T>::R' as the name of a type.
17969 Similarly, assume that qualified names are templates, where
17970 required, so that:
17972 template <class T>
17973 int S<T>::R<T>::i = 3;
17975 will work, too. */
17976 id = cp_parser_id_expression (parser,
17977 /*template_keyword_p=*/false,
17978 /*check_dependency_p=*/false,
17979 /*template_p=*/NULL,
17980 /*declarator_p=*/true,
17981 optional_p);
17982 if (id && BASELINK_P (id))
17983 id = BASELINK_FUNCTIONS (id);
17984 return id;
17987 /* Parse a type-id.
17989 type-id:
17990 type-specifier-seq abstract-declarator [opt]
17992 Returns the TYPE specified. */
17994 static tree
17995 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17996 bool is_trailing_return)
17998 cp_decl_specifier_seq type_specifier_seq;
17999 cp_declarator *abstract_declarator;
18001 /* Parse the type-specifier-seq. */
18002 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18003 is_trailing_return,
18004 &type_specifier_seq);
18005 if (type_specifier_seq.type == error_mark_node)
18006 return error_mark_node;
18008 /* There might or might not be an abstract declarator. */
18009 cp_parser_parse_tentatively (parser);
18010 /* Look for the declarator. */
18011 abstract_declarator
18012 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18013 /*parenthesized_p=*/NULL,
18014 /*member_p=*/false);
18015 /* Check to see if there really was a declarator. */
18016 if (!cp_parser_parse_definitely (parser))
18017 abstract_declarator = NULL;
18019 if (type_specifier_seq.type
18020 /* None of the valid uses of 'auto' in C++14 involve the type-id
18021 nonterminal, but it is valid in a trailing-return-type. */
18022 && !(cxx_dialect >= cxx1y && is_trailing_return)
18023 && type_uses_auto (type_specifier_seq.type))
18025 /* A type-id with type 'auto' is only ok if the abstract declarator
18026 is a function declarator with a late-specified return type. */
18027 if (abstract_declarator
18028 && abstract_declarator->kind == cdk_function
18029 && abstract_declarator->u.function.late_return_type)
18030 /* OK */;
18031 else
18033 error ("invalid use of %<auto%>");
18034 return error_mark_node;
18038 return groktypename (&type_specifier_seq, abstract_declarator,
18039 is_template_arg);
18042 static tree cp_parser_type_id (cp_parser *parser)
18044 return cp_parser_type_id_1 (parser, false, false);
18047 static tree cp_parser_template_type_arg (cp_parser *parser)
18049 tree r;
18050 const char *saved_message = parser->type_definition_forbidden_message;
18051 parser->type_definition_forbidden_message
18052 = G_("types may not be defined in template arguments");
18053 r = cp_parser_type_id_1 (parser, true, false);
18054 parser->type_definition_forbidden_message = saved_message;
18055 if (cxx_dialect >= cxx1y && type_uses_auto (r))
18057 error ("invalid use of %<auto%> in template argument");
18058 r = error_mark_node;
18060 return r;
18063 static tree cp_parser_trailing_type_id (cp_parser *parser)
18065 return cp_parser_type_id_1 (parser, false, true);
18068 /* Parse a type-specifier-seq.
18070 type-specifier-seq:
18071 type-specifier type-specifier-seq [opt]
18073 GNU extension:
18075 type-specifier-seq:
18076 attributes type-specifier-seq [opt]
18078 If IS_DECLARATION is true, we are at the start of a "condition" or
18079 exception-declaration, so we might be followed by a declarator-id.
18081 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18082 i.e. we've just seen "->".
18084 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18086 static void
18087 cp_parser_type_specifier_seq (cp_parser* parser,
18088 bool is_declaration,
18089 bool is_trailing_return,
18090 cp_decl_specifier_seq *type_specifier_seq)
18092 bool seen_type_specifier = false;
18093 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18094 cp_token *start_token = NULL;
18096 /* Clear the TYPE_SPECIFIER_SEQ. */
18097 clear_decl_specs (type_specifier_seq);
18099 /* In the context of a trailing return type, enum E { } is an
18100 elaborated-type-specifier followed by a function-body, not an
18101 enum-specifier. */
18102 if (is_trailing_return)
18103 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18105 /* Parse the type-specifiers and attributes. */
18106 while (true)
18108 tree type_specifier;
18109 bool is_cv_qualifier;
18111 /* Check for attributes first. */
18112 if (cp_next_tokens_can_be_attribute_p (parser))
18114 type_specifier_seq->attributes =
18115 chainon (type_specifier_seq->attributes,
18116 cp_parser_attributes_opt (parser));
18117 continue;
18120 /* record the token of the beginning of the type specifier seq,
18121 for error reporting purposes*/
18122 if (!start_token)
18123 start_token = cp_lexer_peek_token (parser->lexer);
18125 /* Look for the type-specifier. */
18126 type_specifier = cp_parser_type_specifier (parser,
18127 flags,
18128 type_specifier_seq,
18129 /*is_declaration=*/false,
18130 NULL,
18131 &is_cv_qualifier);
18132 if (!type_specifier)
18134 /* If the first type-specifier could not be found, this is not a
18135 type-specifier-seq at all. */
18136 if (!seen_type_specifier)
18138 /* Set in_declarator_p to avoid skipping to the semicolon. */
18139 int in_decl = parser->in_declarator_p;
18140 parser->in_declarator_p = true;
18142 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18143 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18144 cp_parser_error (parser, "expected type-specifier");
18146 parser->in_declarator_p = in_decl;
18148 type_specifier_seq->type = error_mark_node;
18149 return;
18151 /* If subsequent type-specifiers could not be found, the
18152 type-specifier-seq is complete. */
18153 break;
18156 seen_type_specifier = true;
18157 /* The standard says that a condition can be:
18159 type-specifier-seq declarator = assignment-expression
18161 However, given:
18163 struct S {};
18164 if (int S = ...)
18166 we should treat the "S" as a declarator, not as a
18167 type-specifier. The standard doesn't say that explicitly for
18168 type-specifier-seq, but it does say that for
18169 decl-specifier-seq in an ordinary declaration. Perhaps it
18170 would be clearer just to allow a decl-specifier-seq here, and
18171 then add a semantic restriction that if any decl-specifiers
18172 that are not type-specifiers appear, the program is invalid. */
18173 if (is_declaration && !is_cv_qualifier)
18174 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18178 /* Return whether the function currently being declared has an associated
18179 template parameter list. */
18181 static bool
18182 function_being_declared_is_template_p (cp_parser* parser)
18184 if (!current_template_parms || processing_template_parmlist)
18185 return false;
18187 if (parser->implicit_template_scope)
18188 return true;
18190 if (at_class_scope_p ()
18191 && TYPE_BEING_DEFINED (current_class_type))
18192 return parser->num_template_parameter_lists != 0;
18194 return ((int) parser->num_template_parameter_lists > template_class_depth
18195 (current_class_type));
18198 /* Parse a parameter-declaration-clause.
18200 parameter-declaration-clause:
18201 parameter-declaration-list [opt] ... [opt]
18202 parameter-declaration-list , ...
18204 Returns a representation for the parameter declarations. A return
18205 value of NULL indicates a parameter-declaration-clause consisting
18206 only of an ellipsis. */
18208 static tree
18209 cp_parser_parameter_declaration_clause (cp_parser* parser)
18211 tree parameters;
18212 cp_token *token;
18213 bool ellipsis_p;
18214 bool is_error;
18216 struct cleanup {
18217 cp_parser* parser;
18218 int auto_is_implicit_function_template_parm_p;
18219 ~cleanup() {
18220 parser->auto_is_implicit_function_template_parm_p
18221 = auto_is_implicit_function_template_parm_p;
18223 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18225 (void) cleanup;
18227 if (!processing_specialization
18228 && !processing_template_parmlist
18229 && !processing_explicit_instantiation)
18230 if (!current_function_decl
18231 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18232 parser->auto_is_implicit_function_template_parm_p = true;
18234 /* Peek at the next token. */
18235 token = cp_lexer_peek_token (parser->lexer);
18236 /* Check for trivial parameter-declaration-clauses. */
18237 if (token->type == CPP_ELLIPSIS)
18239 /* Consume the `...' token. */
18240 cp_lexer_consume_token (parser->lexer);
18241 return NULL_TREE;
18243 else if (token->type == CPP_CLOSE_PAREN)
18244 /* There are no parameters. */
18246 #ifndef NO_IMPLICIT_EXTERN_C
18247 if (in_system_header_at (input_location)
18248 && current_class_type == NULL
18249 && current_lang_name == lang_name_c)
18250 return NULL_TREE;
18251 else
18252 #endif
18253 return void_list_node;
18255 /* Check for `(void)', too, which is a special case. */
18256 else if (token->keyword == RID_VOID
18257 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18258 == CPP_CLOSE_PAREN))
18260 /* Consume the `void' token. */
18261 cp_lexer_consume_token (parser->lexer);
18262 /* There are no parameters. */
18263 return void_list_node;
18266 /* Parse the parameter-declaration-list. */
18267 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18268 /* If a parse error occurred while parsing the
18269 parameter-declaration-list, then the entire
18270 parameter-declaration-clause is erroneous. */
18271 if (is_error)
18272 return NULL;
18274 /* Peek at the next token. */
18275 token = cp_lexer_peek_token (parser->lexer);
18276 /* If it's a `,', the clause should terminate with an ellipsis. */
18277 if (token->type == CPP_COMMA)
18279 /* Consume the `,'. */
18280 cp_lexer_consume_token (parser->lexer);
18281 /* Expect an ellipsis. */
18282 ellipsis_p
18283 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18285 /* It might also be `...' if the optional trailing `,' was
18286 omitted. */
18287 else if (token->type == CPP_ELLIPSIS)
18289 /* Consume the `...' token. */
18290 cp_lexer_consume_token (parser->lexer);
18291 /* And remember that we saw it. */
18292 ellipsis_p = true;
18294 else
18295 ellipsis_p = false;
18297 /* Finish the parameter list. */
18298 if (!ellipsis_p)
18299 parameters = chainon (parameters, void_list_node);
18301 return parameters;
18304 /* Parse a parameter-declaration-list.
18306 parameter-declaration-list:
18307 parameter-declaration
18308 parameter-declaration-list , parameter-declaration
18310 Returns a representation of the parameter-declaration-list, as for
18311 cp_parser_parameter_declaration_clause. However, the
18312 `void_list_node' is never appended to the list. Upon return,
18313 *IS_ERROR will be true iff an error occurred. */
18315 static tree
18316 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18318 tree parameters = NULL_TREE;
18319 tree *tail = &parameters;
18320 bool saved_in_unbraced_linkage_specification_p;
18321 int index = 0;
18323 /* Assume all will go well. */
18324 *is_error = false;
18325 /* The special considerations that apply to a function within an
18326 unbraced linkage specifications do not apply to the parameters
18327 to the function. */
18328 saved_in_unbraced_linkage_specification_p
18329 = parser->in_unbraced_linkage_specification_p;
18330 parser->in_unbraced_linkage_specification_p = false;
18332 /* Look for more parameters. */
18333 while (true)
18335 cp_parameter_declarator *parameter;
18336 tree decl = error_mark_node;
18337 bool parenthesized_p = false;
18338 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18339 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18340 (current_template_parms)) : 0);
18342 /* Parse the parameter. */
18343 parameter
18344 = cp_parser_parameter_declaration (parser,
18345 /*template_parm_p=*/false,
18346 &parenthesized_p);
18348 /* We don't know yet if the enclosing context is deprecated, so wait
18349 and warn in grokparms if appropriate. */
18350 deprecated_state = DEPRECATED_SUPPRESS;
18352 if (parameter)
18354 /* If a function parameter pack was specified and an implicit template
18355 parameter was introduced during cp_parser_parameter_declaration,
18356 change any implicit parameters introduced into packs. */
18357 if (parser->implicit_template_parms
18358 && parameter->declarator
18359 && parameter->declarator->parameter_pack_p)
18361 int latest_template_parm_idx = TREE_VEC_LENGTH
18362 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18364 if (latest_template_parm_idx != template_parm_idx)
18365 parameter->decl_specifiers.type = convert_generic_types_to_packs
18366 (parameter->decl_specifiers.type,
18367 template_parm_idx, latest_template_parm_idx);
18370 decl = grokdeclarator (parameter->declarator,
18371 &parameter->decl_specifiers,
18372 PARM,
18373 parameter->default_argument != NULL_TREE,
18374 &parameter->decl_specifiers.attributes);
18377 deprecated_state = DEPRECATED_NORMAL;
18379 /* If a parse error occurred parsing the parameter declaration,
18380 then the entire parameter-declaration-list is erroneous. */
18381 if (decl == error_mark_node)
18383 *is_error = true;
18384 parameters = error_mark_node;
18385 break;
18388 if (parameter->decl_specifiers.attributes)
18389 cplus_decl_attributes (&decl,
18390 parameter->decl_specifiers.attributes,
18392 if (DECL_NAME (decl))
18393 decl = pushdecl (decl);
18395 if (decl != error_mark_node)
18397 retrofit_lang_decl (decl);
18398 DECL_PARM_INDEX (decl) = ++index;
18399 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18402 /* Add the new parameter to the list. */
18403 *tail = build_tree_list (parameter->default_argument, decl);
18404 tail = &TREE_CHAIN (*tail);
18406 /* Peek at the next token. */
18407 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18408 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18409 /* These are for Objective-C++ */
18410 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18411 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18412 /* The parameter-declaration-list is complete. */
18413 break;
18414 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18416 cp_token *token;
18418 /* Peek at the next token. */
18419 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18420 /* If it's an ellipsis, then the list is complete. */
18421 if (token->type == CPP_ELLIPSIS)
18422 break;
18423 /* Otherwise, there must be more parameters. Consume the
18424 `,'. */
18425 cp_lexer_consume_token (parser->lexer);
18426 /* When parsing something like:
18428 int i(float f, double d)
18430 we can tell after seeing the declaration for "f" that we
18431 are not looking at an initialization of a variable "i",
18432 but rather at the declaration of a function "i".
18434 Due to the fact that the parsing of template arguments
18435 (as specified to a template-id) requires backtracking we
18436 cannot use this technique when inside a template argument
18437 list. */
18438 if (!parser->in_template_argument_list_p
18439 && !parser->in_type_id_in_expr_p
18440 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18441 /* However, a parameter-declaration of the form
18442 "float(f)" (which is a valid declaration of a
18443 parameter "f") can also be interpreted as an
18444 expression (the conversion of "f" to "float"). */
18445 && !parenthesized_p)
18446 cp_parser_commit_to_tentative_parse (parser);
18448 else
18450 cp_parser_error (parser, "expected %<,%> or %<...%>");
18451 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18452 cp_parser_skip_to_closing_parenthesis (parser,
18453 /*recovering=*/true,
18454 /*or_comma=*/false,
18455 /*consume_paren=*/false);
18456 break;
18460 parser->in_unbraced_linkage_specification_p
18461 = saved_in_unbraced_linkage_specification_p;
18463 /* Reset implicit_template_scope if we are about to leave the function
18464 parameter list that introduced it. Note that for out-of-line member
18465 definitions, there will be one or more class scopes before we get to
18466 the template parameter scope. */
18468 if (cp_binding_level *its = parser->implicit_template_scope)
18469 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18471 while (maybe_its->kind == sk_class)
18472 maybe_its = maybe_its->level_chain;
18473 if (maybe_its == its)
18475 parser->implicit_template_parms = 0;
18476 parser->implicit_template_scope = 0;
18480 return parameters;
18483 /* Parse a parameter declaration.
18485 parameter-declaration:
18486 decl-specifier-seq ... [opt] declarator
18487 decl-specifier-seq declarator = assignment-expression
18488 decl-specifier-seq ... [opt] abstract-declarator [opt]
18489 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18491 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18492 declares a template parameter. (In that case, a non-nested `>'
18493 token encountered during the parsing of the assignment-expression
18494 is not interpreted as a greater-than operator.)
18496 Returns a representation of the parameter, or NULL if an error
18497 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18498 true iff the declarator is of the form "(p)". */
18500 static cp_parameter_declarator *
18501 cp_parser_parameter_declaration (cp_parser *parser,
18502 bool template_parm_p,
18503 bool *parenthesized_p)
18505 int declares_class_or_enum;
18506 cp_decl_specifier_seq decl_specifiers;
18507 cp_declarator *declarator;
18508 tree default_argument;
18509 cp_token *token = NULL, *declarator_token_start = NULL;
18510 const char *saved_message;
18512 /* In a template parameter, `>' is not an operator.
18514 [temp.param]
18516 When parsing a default template-argument for a non-type
18517 template-parameter, the first non-nested `>' is taken as the end
18518 of the template parameter-list rather than a greater-than
18519 operator. */
18521 /* Type definitions may not appear in parameter types. */
18522 saved_message = parser->type_definition_forbidden_message;
18523 parser->type_definition_forbidden_message
18524 = G_("types may not be defined in parameter types");
18526 /* Parse the declaration-specifiers. */
18527 cp_parser_decl_specifier_seq (parser,
18528 CP_PARSER_FLAGS_NONE,
18529 &decl_specifiers,
18530 &declares_class_or_enum);
18532 /* Complain about missing 'typename' or other invalid type names. */
18533 if (!decl_specifiers.any_type_specifiers_p
18534 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18535 decl_specifiers.type = error_mark_node;
18537 /* If an error occurred, there's no reason to attempt to parse the
18538 rest of the declaration. */
18539 if (cp_parser_error_occurred (parser))
18541 parser->type_definition_forbidden_message = saved_message;
18542 return NULL;
18545 /* Peek at the next token. */
18546 token = cp_lexer_peek_token (parser->lexer);
18548 /* If the next token is a `)', `,', `=', `>', or `...', then there
18549 is no declarator. However, when variadic templates are enabled,
18550 there may be a declarator following `...'. */
18551 if (token->type == CPP_CLOSE_PAREN
18552 || token->type == CPP_COMMA
18553 || token->type == CPP_EQ
18554 || token->type == CPP_GREATER)
18556 declarator = NULL;
18557 if (parenthesized_p)
18558 *parenthesized_p = false;
18560 /* Otherwise, there should be a declarator. */
18561 else
18563 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18564 parser->default_arg_ok_p = false;
18566 /* After seeing a decl-specifier-seq, if the next token is not a
18567 "(", there is no possibility that the code is a valid
18568 expression. Therefore, if parsing tentatively, we commit at
18569 this point. */
18570 if (!parser->in_template_argument_list_p
18571 /* In an expression context, having seen:
18573 (int((char ...
18575 we cannot be sure whether we are looking at a
18576 function-type (taking a "char" as a parameter) or a cast
18577 of some object of type "char" to "int". */
18578 && !parser->in_type_id_in_expr_p
18579 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18580 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18581 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18582 cp_parser_commit_to_tentative_parse (parser);
18583 /* Parse the declarator. */
18584 declarator_token_start = token;
18585 declarator = cp_parser_declarator (parser,
18586 CP_PARSER_DECLARATOR_EITHER,
18587 /*ctor_dtor_or_conv_p=*/NULL,
18588 parenthesized_p,
18589 /*member_p=*/false);
18590 parser->default_arg_ok_p = saved_default_arg_ok_p;
18591 /* After the declarator, allow more attributes. */
18592 decl_specifiers.attributes
18593 = chainon (decl_specifiers.attributes,
18594 cp_parser_attributes_opt (parser));
18597 /* If the next token is an ellipsis, and we have not seen a
18598 declarator name, and the type of the declarator contains parameter
18599 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18600 a parameter pack expansion expression. Otherwise, leave the
18601 ellipsis for a C-style variadic function. */
18602 token = cp_lexer_peek_token (parser->lexer);
18603 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18605 tree type = decl_specifiers.type;
18607 if (type && DECL_P (type))
18608 type = TREE_TYPE (type);
18610 if (type
18611 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18612 && declarator_can_be_parameter_pack (declarator)
18613 && (!declarator || !declarator->parameter_pack_p)
18614 && uses_parameter_packs (type))
18616 /* Consume the `...'. */
18617 cp_lexer_consume_token (parser->lexer);
18618 maybe_warn_variadic_templates ();
18620 /* Build a pack expansion type */
18621 if (declarator)
18622 declarator->parameter_pack_p = true;
18623 else
18624 decl_specifiers.type = make_pack_expansion (type);
18628 /* The restriction on defining new types applies only to the type
18629 of the parameter, not to the default argument. */
18630 parser->type_definition_forbidden_message = saved_message;
18632 /* If the next token is `=', then process a default argument. */
18633 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18635 token = cp_lexer_peek_token (parser->lexer);
18636 /* If we are defining a class, then the tokens that make up the
18637 default argument must be saved and processed later. */
18638 if (!template_parm_p && at_class_scope_p ()
18639 && TYPE_BEING_DEFINED (current_class_type)
18640 && !LAMBDA_TYPE_P (current_class_type))
18641 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18642 /* Outside of a class definition, we can just parse the
18643 assignment-expression. */
18644 else
18645 default_argument
18646 = cp_parser_default_argument (parser, template_parm_p);
18648 if (!parser->default_arg_ok_p)
18650 if (flag_permissive)
18651 warning (0, "deprecated use of default argument for parameter of non-function");
18652 else
18654 error_at (token->location,
18655 "default arguments are only "
18656 "permitted for function parameters");
18657 default_argument = NULL_TREE;
18660 else if ((declarator && declarator->parameter_pack_p)
18661 || (decl_specifiers.type
18662 && PACK_EXPANSION_P (decl_specifiers.type)))
18664 /* Find the name of the parameter pack. */
18665 cp_declarator *id_declarator = declarator;
18666 while (id_declarator && id_declarator->kind != cdk_id)
18667 id_declarator = id_declarator->declarator;
18669 if (id_declarator && id_declarator->kind == cdk_id)
18670 error_at (declarator_token_start->location,
18671 template_parm_p
18672 ? G_("template parameter pack %qD "
18673 "cannot have a default argument")
18674 : G_("parameter pack %qD cannot have "
18675 "a default argument"),
18676 id_declarator->u.id.unqualified_name);
18677 else
18678 error_at (declarator_token_start->location,
18679 template_parm_p
18680 ? G_("template parameter pack cannot have "
18681 "a default argument")
18682 : G_("parameter pack cannot have a "
18683 "default argument"));
18685 default_argument = NULL_TREE;
18688 else
18689 default_argument = NULL_TREE;
18691 return make_parameter_declarator (&decl_specifiers,
18692 declarator,
18693 default_argument);
18696 /* Parse a default argument and return it.
18698 TEMPLATE_PARM_P is true if this is a default argument for a
18699 non-type template parameter. */
18700 static tree
18701 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18703 tree default_argument = NULL_TREE;
18704 bool saved_greater_than_is_operator_p;
18705 bool saved_local_variables_forbidden_p;
18706 bool non_constant_p, is_direct_init;
18708 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18709 set correctly. */
18710 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18711 parser->greater_than_is_operator_p = !template_parm_p;
18712 /* Local variable names (and the `this' keyword) may not
18713 appear in a default argument. */
18714 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18715 parser->local_variables_forbidden_p = true;
18716 /* Parse the assignment-expression. */
18717 if (template_parm_p)
18718 push_deferring_access_checks (dk_no_deferred);
18719 tree saved_class_ptr = NULL_TREE;
18720 tree saved_class_ref = NULL_TREE;
18721 /* The "this" pointer is not valid in a default argument. */
18722 if (cfun)
18724 saved_class_ptr = current_class_ptr;
18725 cp_function_chain->x_current_class_ptr = NULL_TREE;
18726 saved_class_ref = current_class_ref;
18727 cp_function_chain->x_current_class_ref = NULL_TREE;
18729 default_argument
18730 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18731 /* Restore the "this" pointer. */
18732 if (cfun)
18734 cp_function_chain->x_current_class_ptr = saved_class_ptr;
18735 cp_function_chain->x_current_class_ref = saved_class_ref;
18737 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18738 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18739 if (template_parm_p)
18740 pop_deferring_access_checks ();
18741 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18742 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18744 return default_argument;
18747 /* Parse a function-body.
18749 function-body:
18750 compound_statement */
18752 static void
18753 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18755 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18758 /* Parse a ctor-initializer-opt followed by a function-body. Return
18759 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18760 is true we are parsing a function-try-block. */
18762 static bool
18763 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18764 bool in_function_try_block)
18766 tree body, list;
18767 bool ctor_initializer_p;
18768 const bool check_body_p =
18769 DECL_CONSTRUCTOR_P (current_function_decl)
18770 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18771 tree last = NULL;
18773 /* Begin the function body. */
18774 body = begin_function_body ();
18775 /* Parse the optional ctor-initializer. */
18776 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18778 /* If we're parsing a constexpr constructor definition, we need
18779 to check that the constructor body is indeed empty. However,
18780 before we get to cp_parser_function_body lot of junk has been
18781 generated, so we can't just check that we have an empty block.
18782 Rather we take a snapshot of the outermost block, and check whether
18783 cp_parser_function_body changed its state. */
18784 if (check_body_p)
18786 list = cur_stmt_list;
18787 if (STATEMENT_LIST_TAIL (list))
18788 last = STATEMENT_LIST_TAIL (list)->stmt;
18790 /* Parse the function-body. */
18791 cp_parser_function_body (parser, in_function_try_block);
18792 if (check_body_p)
18793 check_constexpr_ctor_body (last, list);
18794 /* Finish the function body. */
18795 finish_function_body (body);
18797 return ctor_initializer_p;
18800 /* Parse an initializer.
18802 initializer:
18803 = initializer-clause
18804 ( expression-list )
18806 Returns an expression representing the initializer. If no
18807 initializer is present, NULL_TREE is returned.
18809 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
18810 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
18811 set to TRUE if there is no initializer present. If there is an
18812 initializer, and it is not a constant-expression, *NON_CONSTANT_P
18813 is set to true; otherwise it is set to false. */
18815 static tree
18816 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
18817 bool* non_constant_p)
18819 cp_token *token;
18820 tree init;
18822 /* Peek at the next token. */
18823 token = cp_lexer_peek_token (parser->lexer);
18825 /* Let our caller know whether or not this initializer was
18826 parenthesized. */
18827 *is_direct_init = (token->type != CPP_EQ);
18828 /* Assume that the initializer is constant. */
18829 *non_constant_p = false;
18831 if (token->type == CPP_EQ)
18833 /* Consume the `='. */
18834 cp_lexer_consume_token (parser->lexer);
18835 /* Parse the initializer-clause. */
18836 init = cp_parser_initializer_clause (parser, non_constant_p);
18838 else if (token->type == CPP_OPEN_PAREN)
18840 vec<tree, va_gc> *vec;
18841 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
18842 /*cast_p=*/false,
18843 /*allow_expansion_p=*/true,
18844 non_constant_p);
18845 if (vec == NULL)
18846 return error_mark_node;
18847 init = build_tree_list_vec (vec);
18848 release_tree_vector (vec);
18850 else if (token->type == CPP_OPEN_BRACE)
18852 cp_lexer_set_source_position (parser->lexer);
18853 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18854 init = cp_parser_braced_list (parser, non_constant_p);
18855 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
18857 else
18859 /* Anything else is an error. */
18860 cp_parser_error (parser, "expected initializer");
18861 init = error_mark_node;
18864 return init;
18867 /* Parse an initializer-clause.
18869 initializer-clause:
18870 assignment-expression
18871 braced-init-list
18873 Returns an expression representing the initializer.
18875 If the `assignment-expression' production is used the value
18876 returned is simply a representation for the expression.
18878 Otherwise, calls cp_parser_braced_list. */
18880 static tree
18881 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
18883 tree initializer;
18885 /* Assume the expression is constant. */
18886 *non_constant_p = false;
18888 /* If it is not a `{', then we are looking at an
18889 assignment-expression. */
18890 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
18892 initializer
18893 = cp_parser_constant_expression (parser,
18894 /*allow_non_constant_p=*/true,
18895 non_constant_p);
18897 else
18898 initializer = cp_parser_braced_list (parser, non_constant_p);
18900 return initializer;
18903 /* Parse a brace-enclosed initializer list.
18905 braced-init-list:
18906 { initializer-list , [opt] }
18909 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
18910 the elements of the initializer-list (or NULL, if the last
18911 production is used). The TREE_TYPE for the CONSTRUCTOR will be
18912 NULL_TREE. There is no way to detect whether or not the optional
18913 trailing `,' was provided. NON_CONSTANT_P is as for
18914 cp_parser_initializer. */
18916 static tree
18917 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
18919 tree initializer;
18921 /* Consume the `{' token. */
18922 cp_lexer_consume_token (parser->lexer);
18923 /* Create a CONSTRUCTOR to represent the braced-initializer. */
18924 initializer = make_node (CONSTRUCTOR);
18925 /* If it's not a `}', then there is a non-trivial initializer. */
18926 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
18928 /* Parse the initializer list. */
18929 CONSTRUCTOR_ELTS (initializer)
18930 = cp_parser_initializer_list (parser, non_constant_p);
18931 /* A trailing `,' token is allowed. */
18932 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18933 cp_lexer_consume_token (parser->lexer);
18935 else
18936 *non_constant_p = false;
18937 /* Now, there should be a trailing `}'. */
18938 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18939 TREE_TYPE (initializer) = init_list_type_node;
18940 return initializer;
18943 /* Parse an initializer-list.
18945 initializer-list:
18946 initializer-clause ... [opt]
18947 initializer-list , initializer-clause ... [opt]
18949 GNU Extension:
18951 initializer-list:
18952 designation initializer-clause ...[opt]
18953 initializer-list , designation initializer-clause ...[opt]
18955 designation:
18956 . identifier =
18957 identifier :
18958 [ constant-expression ] =
18960 Returns a vec of constructor_elt. The VALUE of each elt is an expression
18961 for the initializer. If the INDEX of the elt is non-NULL, it is the
18962 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
18963 as for cp_parser_initializer. */
18965 static vec<constructor_elt, va_gc> *
18966 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
18968 vec<constructor_elt, va_gc> *v = NULL;
18970 /* Assume all of the expressions are constant. */
18971 *non_constant_p = false;
18973 /* Parse the rest of the list. */
18974 while (true)
18976 cp_token *token;
18977 tree designator;
18978 tree initializer;
18979 bool clause_non_constant_p;
18981 /* If the next token is an identifier and the following one is a
18982 colon, we are looking at the GNU designated-initializer
18983 syntax. */
18984 if (cp_parser_allow_gnu_extensions_p (parser)
18985 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
18986 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
18988 /* Warn the user that they are using an extension. */
18989 pedwarn (input_location, OPT_Wpedantic,
18990 "ISO C++ does not allow designated initializers");
18991 /* Consume the identifier. */
18992 designator = cp_lexer_consume_token (parser->lexer)->u.value;
18993 /* Consume the `:'. */
18994 cp_lexer_consume_token (parser->lexer);
18996 /* Also handle the C99 syntax, '. id ='. */
18997 else if (cp_parser_allow_gnu_extensions_p (parser)
18998 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
18999 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19000 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19002 /* Warn the user that they are using an extension. */
19003 pedwarn (input_location, OPT_Wpedantic,
19004 "ISO C++ does not allow C99 designated initializers");
19005 /* Consume the `.'. */
19006 cp_lexer_consume_token (parser->lexer);
19007 /* Consume the identifier. */
19008 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19009 /* Consume the `='. */
19010 cp_lexer_consume_token (parser->lexer);
19012 /* Also handle C99 array designators, '[ const ] ='. */
19013 else if (cp_parser_allow_gnu_extensions_p (parser)
19014 && !c_dialect_objc ()
19015 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19017 /* In C++11, [ could start a lambda-introducer. */
19018 bool non_const = false;
19020 cp_parser_parse_tentatively (parser);
19021 cp_lexer_consume_token (parser->lexer);
19022 designator = cp_parser_constant_expression (parser, true, &non_const);
19023 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19024 cp_parser_require (parser, CPP_EQ, RT_EQ);
19025 if (!cp_parser_parse_definitely (parser))
19026 designator = NULL_TREE;
19027 else if (non_const)
19028 require_potential_rvalue_constant_expression (designator);
19030 else
19031 designator = NULL_TREE;
19033 /* Parse the initializer. */
19034 initializer = cp_parser_initializer_clause (parser,
19035 &clause_non_constant_p);
19036 /* If any clause is non-constant, so is the entire initializer. */
19037 if (clause_non_constant_p)
19038 *non_constant_p = true;
19040 /* If we have an ellipsis, this is an initializer pack
19041 expansion. */
19042 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19044 /* Consume the `...'. */
19045 cp_lexer_consume_token (parser->lexer);
19047 /* Turn the initializer into an initializer expansion. */
19048 initializer = make_pack_expansion (initializer);
19051 /* Add it to the vector. */
19052 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19054 /* If the next token is not a comma, we have reached the end of
19055 the list. */
19056 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19057 break;
19059 /* Peek at the next token. */
19060 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19061 /* If the next token is a `}', then we're still done. An
19062 initializer-clause can have a trailing `,' after the
19063 initializer-list and before the closing `}'. */
19064 if (token->type == CPP_CLOSE_BRACE)
19065 break;
19067 /* Consume the `,' token. */
19068 cp_lexer_consume_token (parser->lexer);
19071 return v;
19074 /* Classes [gram.class] */
19076 /* Parse a class-name.
19078 class-name:
19079 identifier
19080 template-id
19082 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19083 to indicate that names looked up in dependent types should be
19084 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19085 keyword has been used to indicate that the name that appears next
19086 is a template. TAG_TYPE indicates the explicit tag given before
19087 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19088 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19089 is the class being defined in a class-head.
19091 Returns the TYPE_DECL representing the class. */
19093 static tree
19094 cp_parser_class_name (cp_parser *parser,
19095 bool typename_keyword_p,
19096 bool template_keyword_p,
19097 enum tag_types tag_type,
19098 bool check_dependency_p,
19099 bool class_head_p,
19100 bool is_declaration)
19102 tree decl;
19103 tree scope;
19104 bool typename_p;
19105 cp_token *token;
19106 tree identifier = NULL_TREE;
19108 /* All class-names start with an identifier. */
19109 token = cp_lexer_peek_token (parser->lexer);
19110 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19112 cp_parser_error (parser, "expected class-name");
19113 return error_mark_node;
19116 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19117 to a template-id, so we save it here. */
19118 scope = parser->scope;
19119 if (scope == error_mark_node)
19120 return error_mark_node;
19122 /* Any name names a type if we're following the `typename' keyword
19123 in a qualified name where the enclosing scope is type-dependent. */
19124 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19125 && dependent_type_p (scope));
19126 /* Handle the common case (an identifier, but not a template-id)
19127 efficiently. */
19128 if (token->type == CPP_NAME
19129 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19131 cp_token *identifier_token;
19132 bool ambiguous_p;
19134 /* Look for the identifier. */
19135 identifier_token = cp_lexer_peek_token (parser->lexer);
19136 ambiguous_p = identifier_token->error_reported;
19137 identifier = cp_parser_identifier (parser);
19138 /* If the next token isn't an identifier, we are certainly not
19139 looking at a class-name. */
19140 if (identifier == error_mark_node)
19141 decl = error_mark_node;
19142 /* If we know this is a type-name, there's no need to look it
19143 up. */
19144 else if (typename_p)
19145 decl = identifier;
19146 else
19148 tree ambiguous_decls;
19149 /* If we already know that this lookup is ambiguous, then
19150 we've already issued an error message; there's no reason
19151 to check again. */
19152 if (ambiguous_p)
19154 cp_parser_simulate_error (parser);
19155 return error_mark_node;
19157 /* If the next token is a `::', then the name must be a type
19158 name.
19160 [basic.lookup.qual]
19162 During the lookup for a name preceding the :: scope
19163 resolution operator, object, function, and enumerator
19164 names are ignored. */
19165 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19166 tag_type = typename_type;
19167 /* Look up the name. */
19168 decl = cp_parser_lookup_name (parser, identifier,
19169 tag_type,
19170 /*is_template=*/false,
19171 /*is_namespace=*/false,
19172 check_dependency_p,
19173 &ambiguous_decls,
19174 identifier_token->location);
19175 if (ambiguous_decls)
19177 if (cp_parser_parsing_tentatively (parser))
19178 cp_parser_simulate_error (parser);
19179 return error_mark_node;
19183 else
19185 /* Try a template-id. */
19186 decl = cp_parser_template_id (parser, template_keyword_p,
19187 check_dependency_p,
19188 tag_type,
19189 is_declaration);
19190 if (decl == error_mark_node)
19191 return error_mark_node;
19194 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19196 /* If this is a typename, create a TYPENAME_TYPE. */
19197 if (typename_p && decl != error_mark_node)
19199 decl = make_typename_type (scope, decl, typename_type,
19200 /*complain=*/tf_error);
19201 if (decl != error_mark_node)
19202 decl = TYPE_NAME (decl);
19205 decl = strip_using_decl (decl);
19207 /* Check to see that it is really the name of a class. */
19208 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19209 && identifier_p (TREE_OPERAND (decl, 0))
19210 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19211 /* Situations like this:
19213 template <typename T> struct A {
19214 typename T::template X<int>::I i;
19217 are problematic. Is `T::template X<int>' a class-name? The
19218 standard does not seem to be definitive, but there is no other
19219 valid interpretation of the following `::'. Therefore, those
19220 names are considered class-names. */
19222 decl = make_typename_type (scope, decl, tag_type, tf_error);
19223 if (decl != error_mark_node)
19224 decl = TYPE_NAME (decl);
19226 else if (TREE_CODE (decl) != TYPE_DECL
19227 || TREE_TYPE (decl) == error_mark_node
19228 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19229 /* In Objective-C 2.0, a classname followed by '.' starts a
19230 dot-syntax expression, and it's not a type-name. */
19231 || (c_dialect_objc ()
19232 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19233 && objc_is_class_name (decl)))
19234 decl = error_mark_node;
19236 if (decl == error_mark_node)
19237 cp_parser_error (parser, "expected class-name");
19238 else if (identifier && !parser->scope)
19239 maybe_note_name_used_in_class (identifier, decl);
19241 return decl;
19244 /* Parse a class-specifier.
19246 class-specifier:
19247 class-head { member-specification [opt] }
19249 Returns the TREE_TYPE representing the class. */
19251 static tree
19252 cp_parser_class_specifier_1 (cp_parser* parser)
19254 tree type;
19255 tree attributes = NULL_TREE;
19256 bool nested_name_specifier_p;
19257 unsigned saved_num_template_parameter_lists;
19258 bool saved_in_function_body;
19259 unsigned char in_statement;
19260 bool in_switch_statement_p;
19261 bool saved_in_unbraced_linkage_specification_p;
19262 tree old_scope = NULL_TREE;
19263 tree scope = NULL_TREE;
19264 cp_token *closing_brace;
19266 push_deferring_access_checks (dk_no_deferred);
19268 /* Parse the class-head. */
19269 type = cp_parser_class_head (parser,
19270 &nested_name_specifier_p);
19271 /* If the class-head was a semantic disaster, skip the entire body
19272 of the class. */
19273 if (!type)
19275 cp_parser_skip_to_end_of_block_or_statement (parser);
19276 pop_deferring_access_checks ();
19277 return error_mark_node;
19280 /* Look for the `{'. */
19281 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19283 pop_deferring_access_checks ();
19284 return error_mark_node;
19287 cp_ensure_no_omp_declare_simd (parser);
19289 /* Issue an error message if type-definitions are forbidden here. */
19290 cp_parser_check_type_definition (parser);
19291 /* Remember that we are defining one more class. */
19292 ++parser->num_classes_being_defined;
19293 /* Inside the class, surrounding template-parameter-lists do not
19294 apply. */
19295 saved_num_template_parameter_lists
19296 = parser->num_template_parameter_lists;
19297 parser->num_template_parameter_lists = 0;
19298 /* We are not in a function body. */
19299 saved_in_function_body = parser->in_function_body;
19300 parser->in_function_body = false;
19301 /* Or in a loop. */
19302 in_statement = parser->in_statement;
19303 parser->in_statement = 0;
19304 /* Or in a switch. */
19305 in_switch_statement_p = parser->in_switch_statement_p;
19306 parser->in_switch_statement_p = false;
19307 /* We are not immediately inside an extern "lang" block. */
19308 saved_in_unbraced_linkage_specification_p
19309 = parser->in_unbraced_linkage_specification_p;
19310 parser->in_unbraced_linkage_specification_p = false;
19312 /* Start the class. */
19313 if (nested_name_specifier_p)
19315 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19316 old_scope = push_inner_scope (scope);
19318 type = begin_class_definition (type);
19320 if (type == error_mark_node)
19321 /* If the type is erroneous, skip the entire body of the class. */
19322 cp_parser_skip_to_closing_brace (parser);
19323 else
19324 /* Parse the member-specification. */
19325 cp_parser_member_specification_opt (parser);
19327 /* Look for the trailing `}'. */
19328 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19329 /* Look for trailing attributes to apply to this class. */
19330 if (cp_parser_allow_gnu_extensions_p (parser))
19331 attributes = cp_parser_gnu_attributes_opt (parser);
19332 if (type != error_mark_node)
19333 type = finish_struct (type, attributes);
19334 if (nested_name_specifier_p)
19335 pop_inner_scope (old_scope, scope);
19337 /* We've finished a type definition. Check for the common syntax
19338 error of forgetting a semicolon after the definition. We need to
19339 be careful, as we can't just check for not-a-semicolon and be done
19340 with it; the user might have typed:
19342 class X { } c = ...;
19343 class X { } *p = ...;
19345 and so forth. Instead, enumerate all the possible tokens that
19346 might follow this production; if we don't see one of them, then
19347 complain and silently insert the semicolon. */
19349 cp_token *token = cp_lexer_peek_token (parser->lexer);
19350 bool want_semicolon = true;
19352 if (cp_next_tokens_can_be_std_attribute_p (parser))
19353 /* Don't try to parse c++11 attributes here. As per the
19354 grammar, that should be a task for
19355 cp_parser_decl_specifier_seq. */
19356 want_semicolon = false;
19358 switch (token->type)
19360 case CPP_NAME:
19361 case CPP_SEMICOLON:
19362 case CPP_MULT:
19363 case CPP_AND:
19364 case CPP_OPEN_PAREN:
19365 case CPP_CLOSE_PAREN:
19366 case CPP_COMMA:
19367 want_semicolon = false;
19368 break;
19370 /* While it's legal for type qualifiers and storage class
19371 specifiers to follow type definitions in the grammar, only
19372 compiler testsuites contain code like that. Assume that if
19373 we see such code, then what we're really seeing is a case
19374 like:
19376 class X { }
19377 const <type> var = ...;
19381 class Y { }
19382 static <type> func (...) ...
19384 i.e. the qualifier or specifier applies to the next
19385 declaration. To do so, however, we need to look ahead one
19386 more token to see if *that* token is a type specifier.
19388 This code could be improved to handle:
19390 class Z { }
19391 static const <type> var = ...; */
19392 case CPP_KEYWORD:
19393 if (keyword_is_decl_specifier (token->keyword))
19395 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19397 /* Handling user-defined types here would be nice, but very
19398 tricky. */
19399 want_semicolon
19400 = (lookahead->type == CPP_KEYWORD
19401 && keyword_begins_type_specifier (lookahead->keyword));
19403 break;
19404 default:
19405 break;
19408 /* If we don't have a type, then something is very wrong and we
19409 shouldn't try to do anything clever. Likewise for not seeing the
19410 closing brace. */
19411 if (closing_brace && TYPE_P (type) && want_semicolon)
19413 cp_token_position prev
19414 = cp_lexer_previous_token_position (parser->lexer);
19415 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19416 location_t loc = prev_token->location;
19418 if (CLASSTYPE_DECLARED_CLASS (type))
19419 error_at (loc, "expected %<;%> after class definition");
19420 else if (TREE_CODE (type) == RECORD_TYPE)
19421 error_at (loc, "expected %<;%> after struct definition");
19422 else if (TREE_CODE (type) == UNION_TYPE)
19423 error_at (loc, "expected %<;%> after union definition");
19424 else
19425 gcc_unreachable ();
19427 /* Unget one token and smash it to look as though we encountered
19428 a semicolon in the input stream. */
19429 cp_lexer_set_token_position (parser->lexer, prev);
19430 token = cp_lexer_peek_token (parser->lexer);
19431 token->type = CPP_SEMICOLON;
19432 token->keyword = RID_MAX;
19436 /* If this class is not itself within the scope of another class,
19437 then we need to parse the bodies of all of the queued function
19438 definitions. Note that the queued functions defined in a class
19439 are not always processed immediately following the
19440 class-specifier for that class. Consider:
19442 struct A {
19443 struct B { void f() { sizeof (A); } };
19446 If `f' were processed before the processing of `A' were
19447 completed, there would be no way to compute the size of `A'.
19448 Note that the nesting we are interested in here is lexical --
19449 not the semantic nesting given by TYPE_CONTEXT. In particular,
19450 for:
19452 struct A { struct B; };
19453 struct A::B { void f() { } };
19455 there is no need to delay the parsing of `A::B::f'. */
19456 if (--parser->num_classes_being_defined == 0)
19458 tree decl;
19459 tree class_type = NULL_TREE;
19460 tree pushed_scope = NULL_TREE;
19461 unsigned ix;
19462 cp_default_arg_entry *e;
19463 tree save_ccp, save_ccr;
19465 /* In a first pass, parse default arguments to the functions.
19466 Then, in a second pass, parse the bodies of the functions.
19467 This two-phased approach handles cases like:
19469 struct S {
19470 void f() { g(); }
19471 void g(int i = 3);
19475 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19477 decl = e->decl;
19478 /* If there are default arguments that have not yet been processed,
19479 take care of them now. */
19480 if (class_type != e->class_type)
19482 if (pushed_scope)
19483 pop_scope (pushed_scope);
19484 class_type = e->class_type;
19485 pushed_scope = push_scope (class_type);
19487 /* Make sure that any template parameters are in scope. */
19488 maybe_begin_member_template_processing (decl);
19489 /* Parse the default argument expressions. */
19490 cp_parser_late_parsing_default_args (parser, decl);
19491 /* Remove any template parameters from the symbol table. */
19492 maybe_end_member_template_processing ();
19494 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19495 /* Now parse any NSDMIs. */
19496 save_ccp = current_class_ptr;
19497 save_ccr = current_class_ref;
19498 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19500 if (class_type != DECL_CONTEXT (decl))
19502 if (pushed_scope)
19503 pop_scope (pushed_scope);
19504 class_type = DECL_CONTEXT (decl);
19505 pushed_scope = push_scope (class_type);
19507 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19508 cp_parser_late_parsing_nsdmi (parser, decl);
19510 vec_safe_truncate (unparsed_nsdmis, 0);
19511 current_class_ptr = save_ccp;
19512 current_class_ref = save_ccr;
19513 if (pushed_scope)
19514 pop_scope (pushed_scope);
19515 /* Now parse the body of the functions. */
19516 if (flag_openmp)
19518 /* OpenMP UDRs need to be parsed before all other functions. */
19519 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19520 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19521 cp_parser_late_parsing_for_member (parser, decl);
19522 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19523 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19524 cp_parser_late_parsing_for_member (parser, decl);
19526 else
19527 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19528 cp_parser_late_parsing_for_member (parser, decl);
19529 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19532 /* Put back any saved access checks. */
19533 pop_deferring_access_checks ();
19535 /* Restore saved state. */
19536 parser->in_switch_statement_p = in_switch_statement_p;
19537 parser->in_statement = in_statement;
19538 parser->in_function_body = saved_in_function_body;
19539 parser->num_template_parameter_lists
19540 = saved_num_template_parameter_lists;
19541 parser->in_unbraced_linkage_specification_p
19542 = saved_in_unbraced_linkage_specification_p;
19544 return type;
19547 static tree
19548 cp_parser_class_specifier (cp_parser* parser)
19550 tree ret;
19551 timevar_push (TV_PARSE_STRUCT);
19552 ret = cp_parser_class_specifier_1 (parser);
19553 timevar_pop (TV_PARSE_STRUCT);
19554 return ret;
19557 /* Parse a class-head.
19559 class-head:
19560 class-key identifier [opt] base-clause [opt]
19561 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19562 class-key nested-name-specifier [opt] template-id
19563 base-clause [opt]
19565 class-virt-specifier:
19566 final
19568 GNU Extensions:
19569 class-key attributes identifier [opt] base-clause [opt]
19570 class-key attributes nested-name-specifier identifier base-clause [opt]
19571 class-key attributes nested-name-specifier [opt] template-id
19572 base-clause [opt]
19574 Upon return BASES is initialized to the list of base classes (or
19575 NULL, if there are none) in the same form returned by
19576 cp_parser_base_clause.
19578 Returns the TYPE of the indicated class. Sets
19579 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19580 involving a nested-name-specifier was used, and FALSE otherwise.
19582 Returns error_mark_node if this is not a class-head.
19584 Returns NULL_TREE if the class-head is syntactically valid, but
19585 semantically invalid in a way that means we should skip the entire
19586 body of the class. */
19588 static tree
19589 cp_parser_class_head (cp_parser* parser,
19590 bool* nested_name_specifier_p)
19592 tree nested_name_specifier;
19593 enum tag_types class_key;
19594 tree id = NULL_TREE;
19595 tree type = NULL_TREE;
19596 tree attributes;
19597 tree bases;
19598 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19599 bool template_id_p = false;
19600 bool qualified_p = false;
19601 bool invalid_nested_name_p = false;
19602 bool invalid_explicit_specialization_p = false;
19603 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19604 tree pushed_scope = NULL_TREE;
19605 unsigned num_templates;
19606 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19607 /* Assume no nested-name-specifier will be present. */
19608 *nested_name_specifier_p = false;
19609 /* Assume no template parameter lists will be used in defining the
19610 type. */
19611 num_templates = 0;
19612 parser->colon_corrects_to_scope_p = false;
19614 /* Look for the class-key. */
19615 class_key = cp_parser_class_key (parser);
19616 if (class_key == none_type)
19617 return error_mark_node;
19619 /* Parse the attributes. */
19620 attributes = cp_parser_attributes_opt (parser);
19622 /* If the next token is `::', that is invalid -- but sometimes
19623 people do try to write:
19625 struct ::S {};
19627 Handle this gracefully by accepting the extra qualifier, and then
19628 issuing an error about it later if this really is a
19629 class-head. If it turns out just to be an elaborated type
19630 specifier, remain silent. */
19631 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19632 qualified_p = true;
19634 push_deferring_access_checks (dk_no_check);
19636 /* Determine the name of the class. Begin by looking for an
19637 optional nested-name-specifier. */
19638 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19639 nested_name_specifier
19640 = cp_parser_nested_name_specifier_opt (parser,
19641 /*typename_keyword_p=*/false,
19642 /*check_dependency_p=*/false,
19643 /*type_p=*/true,
19644 /*is_declaration=*/false);
19645 /* If there was a nested-name-specifier, then there *must* be an
19646 identifier. */
19647 if (nested_name_specifier)
19649 type_start_token = cp_lexer_peek_token (parser->lexer);
19650 /* Although the grammar says `identifier', it really means
19651 `class-name' or `template-name'. You are only allowed to
19652 define a class that has already been declared with this
19653 syntax.
19655 The proposed resolution for Core Issue 180 says that wherever
19656 you see `class T::X' you should treat `X' as a type-name.
19658 It is OK to define an inaccessible class; for example:
19660 class A { class B; };
19661 class A::B {};
19663 We do not know if we will see a class-name, or a
19664 template-name. We look for a class-name first, in case the
19665 class-name is a template-id; if we looked for the
19666 template-name first we would stop after the template-name. */
19667 cp_parser_parse_tentatively (parser);
19668 type = cp_parser_class_name (parser,
19669 /*typename_keyword_p=*/false,
19670 /*template_keyword_p=*/false,
19671 class_type,
19672 /*check_dependency_p=*/false,
19673 /*class_head_p=*/true,
19674 /*is_declaration=*/false);
19675 /* If that didn't work, ignore the nested-name-specifier. */
19676 if (!cp_parser_parse_definitely (parser))
19678 invalid_nested_name_p = true;
19679 type_start_token = cp_lexer_peek_token (parser->lexer);
19680 id = cp_parser_identifier (parser);
19681 if (id == error_mark_node)
19682 id = NULL_TREE;
19684 /* If we could not find a corresponding TYPE, treat this
19685 declaration like an unqualified declaration. */
19686 if (type == error_mark_node)
19687 nested_name_specifier = NULL_TREE;
19688 /* Otherwise, count the number of templates used in TYPE and its
19689 containing scopes. */
19690 else
19692 tree scope;
19694 for (scope = TREE_TYPE (type);
19695 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19696 scope = get_containing_scope (scope))
19697 if (TYPE_P (scope)
19698 && CLASS_TYPE_P (scope)
19699 && CLASSTYPE_TEMPLATE_INFO (scope)
19700 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19701 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19702 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19703 ++num_templates;
19706 /* Otherwise, the identifier is optional. */
19707 else
19709 /* We don't know whether what comes next is a template-id,
19710 an identifier, or nothing at all. */
19711 cp_parser_parse_tentatively (parser);
19712 /* Check for a template-id. */
19713 type_start_token = cp_lexer_peek_token (parser->lexer);
19714 id = cp_parser_template_id (parser,
19715 /*template_keyword_p=*/false,
19716 /*check_dependency_p=*/true,
19717 class_key,
19718 /*is_declaration=*/true);
19719 /* If that didn't work, it could still be an identifier. */
19720 if (!cp_parser_parse_definitely (parser))
19722 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19724 type_start_token = cp_lexer_peek_token (parser->lexer);
19725 id = cp_parser_identifier (parser);
19727 else
19728 id = NULL_TREE;
19730 else
19732 template_id_p = true;
19733 ++num_templates;
19737 pop_deferring_access_checks ();
19739 if (id)
19741 cp_parser_check_for_invalid_template_id (parser, id,
19742 class_key,
19743 type_start_token->location);
19745 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19747 /* If it's not a `:' or a `{' then we can't really be looking at a
19748 class-head, since a class-head only appears as part of a
19749 class-specifier. We have to detect this situation before calling
19750 xref_tag, since that has irreversible side-effects. */
19751 if (!cp_parser_next_token_starts_class_definition_p (parser))
19753 cp_parser_error (parser, "expected %<{%> or %<:%>");
19754 type = error_mark_node;
19755 goto out;
19758 /* At this point, we're going ahead with the class-specifier, even
19759 if some other problem occurs. */
19760 cp_parser_commit_to_tentative_parse (parser);
19761 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19763 cp_parser_error (parser,
19764 "cannot specify %<override%> for a class");
19765 type = error_mark_node;
19766 goto out;
19768 /* Issue the error about the overly-qualified name now. */
19769 if (qualified_p)
19771 cp_parser_error (parser,
19772 "global qualification of class name is invalid");
19773 type = error_mark_node;
19774 goto out;
19776 else if (invalid_nested_name_p)
19778 cp_parser_error (parser,
19779 "qualified name does not name a class");
19780 type = error_mark_node;
19781 goto out;
19783 else if (nested_name_specifier)
19785 tree scope;
19787 /* Reject typedef-names in class heads. */
19788 if (!DECL_IMPLICIT_TYPEDEF_P (type))
19790 error_at (type_start_token->location,
19791 "invalid class name in declaration of %qD",
19792 type);
19793 type = NULL_TREE;
19794 goto done;
19797 /* Figure out in what scope the declaration is being placed. */
19798 scope = current_scope ();
19799 /* If that scope does not contain the scope in which the
19800 class was originally declared, the program is invalid. */
19801 if (scope && !is_ancestor (scope, nested_name_specifier))
19803 if (at_namespace_scope_p ())
19804 error_at (type_start_token->location,
19805 "declaration of %qD in namespace %qD which does not "
19806 "enclose %qD",
19807 type, scope, nested_name_specifier);
19808 else
19809 error_at (type_start_token->location,
19810 "declaration of %qD in %qD which does not enclose %qD",
19811 type, scope, nested_name_specifier);
19812 type = NULL_TREE;
19813 goto done;
19815 /* [dcl.meaning]
19817 A declarator-id shall not be qualified except for the
19818 definition of a ... nested class outside of its class
19819 ... [or] the definition or explicit instantiation of a
19820 class member of a namespace outside of its namespace. */
19821 if (scope == nested_name_specifier)
19823 permerror (nested_name_specifier_token_start->location,
19824 "extra qualification not allowed");
19825 nested_name_specifier = NULL_TREE;
19826 num_templates = 0;
19829 /* An explicit-specialization must be preceded by "template <>". If
19830 it is not, try to recover gracefully. */
19831 if (at_namespace_scope_p ()
19832 && parser->num_template_parameter_lists == 0
19833 && template_id_p)
19835 error_at (type_start_token->location,
19836 "an explicit specialization must be preceded by %<template <>%>");
19837 invalid_explicit_specialization_p = true;
19838 /* Take the same action that would have been taken by
19839 cp_parser_explicit_specialization. */
19840 ++parser->num_template_parameter_lists;
19841 begin_specialization ();
19843 /* There must be no "return" statements between this point and the
19844 end of this function; set "type "to the correct return value and
19845 use "goto done;" to return. */
19846 /* Make sure that the right number of template parameters were
19847 present. */
19848 if (!cp_parser_check_template_parameters (parser, num_templates,
19849 type_start_token->location,
19850 /*declarator=*/NULL))
19852 /* If something went wrong, there is no point in even trying to
19853 process the class-definition. */
19854 type = NULL_TREE;
19855 goto done;
19858 /* Look up the type. */
19859 if (template_id_p)
19861 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
19862 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
19863 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
19865 error_at (type_start_token->location,
19866 "function template %qD redeclared as a class template", id);
19867 type = error_mark_node;
19869 else
19871 type = TREE_TYPE (id);
19872 type = maybe_process_partial_specialization (type);
19874 if (nested_name_specifier)
19875 pushed_scope = push_scope (nested_name_specifier);
19877 else if (nested_name_specifier)
19879 tree class_type;
19881 /* Given:
19883 template <typename T> struct S { struct T };
19884 template <typename T> struct S<T>::T { };
19886 we will get a TYPENAME_TYPE when processing the definition of
19887 `S::T'. We need to resolve it to the actual type before we
19888 try to define it. */
19889 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
19891 class_type = resolve_typename_type (TREE_TYPE (type),
19892 /*only_current_p=*/false);
19893 if (TREE_CODE (class_type) != TYPENAME_TYPE)
19894 type = TYPE_NAME (class_type);
19895 else
19897 cp_parser_error (parser, "could not resolve typename type");
19898 type = error_mark_node;
19902 if (maybe_process_partial_specialization (TREE_TYPE (type))
19903 == error_mark_node)
19905 type = NULL_TREE;
19906 goto done;
19909 class_type = current_class_type;
19910 /* Enter the scope indicated by the nested-name-specifier. */
19911 pushed_scope = push_scope (nested_name_specifier);
19912 /* Get the canonical version of this type. */
19913 type = TYPE_MAIN_DECL (TREE_TYPE (type));
19914 /* Call push_template_decl if it seems like we should be defining a
19915 template either from the template headers or the type we're
19916 defining, so that we diagnose both extra and missing headers. */
19917 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
19918 || (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type))
19919 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE
19920 (TREE_TYPE (type)))))
19921 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
19923 type = push_template_decl (type);
19924 if (type == error_mark_node)
19926 type = NULL_TREE;
19927 goto done;
19931 type = TREE_TYPE (type);
19932 *nested_name_specifier_p = true;
19934 else /* The name is not a nested name. */
19936 /* If the class was unnamed, create a dummy name. */
19937 if (!id)
19938 id = make_anon_name ();
19939 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
19940 parser->num_template_parameter_lists);
19943 /* Indicate whether this class was declared as a `class' or as a
19944 `struct'. */
19945 if (TREE_CODE (type) == RECORD_TYPE)
19946 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
19947 cp_parser_check_class_key (class_key, type);
19949 /* If this type was already complete, and we see another definition,
19950 that's an error. */
19951 if (type != error_mark_node && COMPLETE_TYPE_P (type))
19953 error_at (type_start_token->location, "redefinition of %q#T",
19954 type);
19955 error_at (type_start_token->location, "previous definition of %q+#T",
19956 type);
19957 type = NULL_TREE;
19958 goto done;
19960 else if (type == error_mark_node)
19961 type = NULL_TREE;
19963 if (type)
19965 /* Apply attributes now, before any use of the class as a template
19966 argument in its base list. */
19967 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
19968 fixup_attribute_variants (type);
19971 /* We will have entered the scope containing the class; the names of
19972 base classes should be looked up in that context. For example:
19974 struct A { struct B {}; struct C; };
19975 struct A::C : B {};
19977 is valid. */
19979 /* Get the list of base-classes, if there is one. */
19980 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19982 /* PR59482: enter the class scope so that base-specifiers are looked
19983 up correctly. */
19984 if (type)
19985 pushclass (type);
19986 bases = cp_parser_base_clause (parser);
19987 /* PR59482: get out of the previously pushed class scope so that the
19988 subsequent pops pop the right thing. */
19989 if (type)
19990 popclass ();
19992 else
19993 bases = NULL_TREE;
19995 /* If we're really defining a class, process the base classes.
19996 If they're invalid, fail. */
19997 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19998 && !xref_basetypes (type, bases))
19999 type = NULL_TREE;
20001 done:
20002 /* Leave the scope given by the nested-name-specifier. We will
20003 enter the class scope itself while processing the members. */
20004 if (pushed_scope)
20005 pop_scope (pushed_scope);
20007 if (invalid_explicit_specialization_p)
20009 end_specialization ();
20010 --parser->num_template_parameter_lists;
20013 if (type)
20014 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20015 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20016 CLASSTYPE_FINAL (type) = 1;
20017 out:
20018 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20019 return type;
20022 /* Parse a class-key.
20024 class-key:
20025 class
20026 struct
20027 union
20029 Returns the kind of class-key specified, or none_type to indicate
20030 error. */
20032 static enum tag_types
20033 cp_parser_class_key (cp_parser* parser)
20035 cp_token *token;
20036 enum tag_types tag_type;
20038 /* Look for the class-key. */
20039 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20040 if (!token)
20041 return none_type;
20043 /* Check to see if the TOKEN is a class-key. */
20044 tag_type = cp_parser_token_is_class_key (token);
20045 if (!tag_type)
20046 cp_parser_error (parser, "expected class-key");
20047 return tag_type;
20050 /* Parse an (optional) member-specification.
20052 member-specification:
20053 member-declaration member-specification [opt]
20054 access-specifier : member-specification [opt] */
20056 static void
20057 cp_parser_member_specification_opt (cp_parser* parser)
20059 while (true)
20061 cp_token *token;
20062 enum rid keyword;
20064 /* Peek at the next token. */
20065 token = cp_lexer_peek_token (parser->lexer);
20066 /* If it's a `}', or EOF then we've seen all the members. */
20067 if (token->type == CPP_CLOSE_BRACE
20068 || token->type == CPP_EOF
20069 || token->type == CPP_PRAGMA_EOL)
20070 break;
20072 /* See if this token is a keyword. */
20073 keyword = token->keyword;
20074 switch (keyword)
20076 case RID_PUBLIC:
20077 case RID_PROTECTED:
20078 case RID_PRIVATE:
20079 /* Consume the access-specifier. */
20080 cp_lexer_consume_token (parser->lexer);
20081 /* Remember which access-specifier is active. */
20082 current_access_specifier = token->u.value;
20083 /* Look for the `:'. */
20084 cp_parser_require (parser, CPP_COLON, RT_COLON);
20085 break;
20087 default:
20088 /* Accept #pragmas at class scope. */
20089 if (token->type == CPP_PRAGMA)
20091 cp_parser_pragma (parser, pragma_member);
20092 break;
20095 /* Otherwise, the next construction must be a
20096 member-declaration. */
20097 cp_parser_member_declaration (parser);
20102 /* Parse a member-declaration.
20104 member-declaration:
20105 decl-specifier-seq [opt] member-declarator-list [opt] ;
20106 function-definition ; [opt]
20107 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20108 using-declaration
20109 template-declaration
20110 alias-declaration
20112 member-declarator-list:
20113 member-declarator
20114 member-declarator-list , member-declarator
20116 member-declarator:
20117 declarator pure-specifier [opt]
20118 declarator constant-initializer [opt]
20119 identifier [opt] : constant-expression
20121 GNU Extensions:
20123 member-declaration:
20124 __extension__ member-declaration
20126 member-declarator:
20127 declarator attributes [opt] pure-specifier [opt]
20128 declarator attributes [opt] constant-initializer [opt]
20129 identifier [opt] attributes [opt] : constant-expression
20131 C++0x Extensions:
20133 member-declaration:
20134 static_assert-declaration */
20136 static void
20137 cp_parser_member_declaration (cp_parser* parser)
20139 cp_decl_specifier_seq decl_specifiers;
20140 tree prefix_attributes;
20141 tree decl;
20142 int declares_class_or_enum;
20143 bool friend_p;
20144 cp_token *token = NULL;
20145 cp_token *decl_spec_token_start = NULL;
20146 cp_token *initializer_token_start = NULL;
20147 int saved_pedantic;
20148 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20150 /* Check for the `__extension__' keyword. */
20151 if (cp_parser_extension_opt (parser, &saved_pedantic))
20153 /* Recurse. */
20154 cp_parser_member_declaration (parser);
20155 /* Restore the old value of the PEDANTIC flag. */
20156 pedantic = saved_pedantic;
20158 return;
20161 /* Check for a template-declaration. */
20162 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20164 /* An explicit specialization here is an error condition, and we
20165 expect the specialization handler to detect and report this. */
20166 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20167 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20168 cp_parser_explicit_specialization (parser);
20169 else
20170 cp_parser_template_declaration (parser, /*member_p=*/true);
20172 return;
20175 /* Check for a using-declaration. */
20176 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20178 if (cxx_dialect < cxx11)
20180 /* Parse the using-declaration. */
20181 cp_parser_using_declaration (parser,
20182 /*access_declaration_p=*/false);
20183 return;
20185 else
20187 tree decl;
20188 bool alias_decl_expected;
20189 cp_parser_parse_tentatively (parser);
20190 decl = cp_parser_alias_declaration (parser);
20191 /* Note that if we actually see the '=' token after the
20192 identifier, cp_parser_alias_declaration commits the
20193 tentative parse. In that case, we really expects an
20194 alias-declaration. Otherwise, we expect a using
20195 declaration. */
20196 alias_decl_expected =
20197 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20198 cp_parser_parse_definitely (parser);
20200 if (alias_decl_expected)
20201 finish_member_declaration (decl);
20202 else
20203 cp_parser_using_declaration (parser,
20204 /*access_declaration_p=*/false);
20205 return;
20209 /* Check for @defs. */
20210 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20212 tree ivar, member;
20213 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20214 ivar = ivar_chains;
20215 while (ivar)
20217 member = ivar;
20218 ivar = TREE_CHAIN (member);
20219 TREE_CHAIN (member) = NULL_TREE;
20220 finish_member_declaration (member);
20222 return;
20225 /* If the next token is `static_assert' we have a static assertion. */
20226 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20228 cp_parser_static_assert (parser, /*member_p=*/true);
20229 return;
20232 parser->colon_corrects_to_scope_p = false;
20234 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20235 goto out;
20237 /* Parse the decl-specifier-seq. */
20238 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20239 cp_parser_decl_specifier_seq (parser,
20240 CP_PARSER_FLAGS_OPTIONAL,
20241 &decl_specifiers,
20242 &declares_class_or_enum);
20243 /* Check for an invalid type-name. */
20244 if (!decl_specifiers.any_type_specifiers_p
20245 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20246 goto out;
20247 /* If there is no declarator, then the decl-specifier-seq should
20248 specify a type. */
20249 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20251 /* If there was no decl-specifier-seq, and the next token is a
20252 `;', then we have something like:
20254 struct S { ; };
20256 [class.mem]
20258 Each member-declaration shall declare at least one member
20259 name of the class. */
20260 if (!decl_specifiers.any_specifiers_p)
20262 cp_token *token = cp_lexer_peek_token (parser->lexer);
20263 if (!in_system_header_at (token->location))
20264 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20266 else
20268 tree type;
20270 /* See if this declaration is a friend. */
20271 friend_p = cp_parser_friend_p (&decl_specifiers);
20272 /* If there were decl-specifiers, check to see if there was
20273 a class-declaration. */
20274 type = check_tag_decl (&decl_specifiers,
20275 /*explicit_type_instantiation_p=*/false);
20276 /* Nested classes have already been added to the class, but
20277 a `friend' needs to be explicitly registered. */
20278 if (friend_p)
20280 /* If the `friend' keyword was present, the friend must
20281 be introduced with a class-key. */
20282 if (!declares_class_or_enum && cxx_dialect < cxx11)
20283 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20284 "in C++03 a class-key must be used "
20285 "when declaring a friend");
20286 /* In this case:
20288 template <typename T> struct A {
20289 friend struct A<T>::B;
20292 A<T>::B will be represented by a TYPENAME_TYPE, and
20293 therefore not recognized by check_tag_decl. */
20294 if (!type)
20296 type = decl_specifiers.type;
20297 if (type && TREE_CODE (type) == TYPE_DECL)
20298 type = TREE_TYPE (type);
20300 if (!type || !TYPE_P (type))
20301 error_at (decl_spec_token_start->location,
20302 "friend declaration does not name a class or "
20303 "function");
20304 else
20305 make_friend_class (current_class_type, type,
20306 /*complain=*/true);
20308 /* If there is no TYPE, an error message will already have
20309 been issued. */
20310 else if (!type || type == error_mark_node)
20312 /* An anonymous aggregate has to be handled specially; such
20313 a declaration really declares a data member (with a
20314 particular type), as opposed to a nested class. */
20315 else if (ANON_AGGR_TYPE_P (type))
20317 /* C++11 9.5/6. */
20318 if (decl_specifiers.storage_class != sc_none)
20319 error_at (decl_spec_token_start->location,
20320 "a storage class on an anonymous aggregate "
20321 "in class scope is not allowed");
20323 /* Remove constructors and such from TYPE, now that we
20324 know it is an anonymous aggregate. */
20325 fixup_anonymous_aggr (type);
20326 /* And make the corresponding data member. */
20327 decl = build_decl (decl_spec_token_start->location,
20328 FIELD_DECL, NULL_TREE, type);
20329 /* Add it to the class. */
20330 finish_member_declaration (decl);
20332 else
20333 cp_parser_check_access_in_redeclaration
20334 (TYPE_NAME (type),
20335 decl_spec_token_start->location);
20338 else
20340 bool assume_semicolon = false;
20342 /* Clear attributes from the decl_specifiers but keep them
20343 around as prefix attributes that apply them to the entity
20344 being declared. */
20345 prefix_attributes = decl_specifiers.attributes;
20346 decl_specifiers.attributes = NULL_TREE;
20348 /* See if these declarations will be friends. */
20349 friend_p = cp_parser_friend_p (&decl_specifiers);
20351 /* Keep going until we hit the `;' at the end of the
20352 declaration. */
20353 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20355 tree attributes = NULL_TREE;
20356 tree first_attribute;
20358 /* Peek at the next token. */
20359 token = cp_lexer_peek_token (parser->lexer);
20361 /* Check for a bitfield declaration. */
20362 if (token->type == CPP_COLON
20363 || (token->type == CPP_NAME
20364 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20365 == CPP_COLON))
20367 tree identifier;
20368 tree width;
20370 /* Get the name of the bitfield. Note that we cannot just
20371 check TOKEN here because it may have been invalidated by
20372 the call to cp_lexer_peek_nth_token above. */
20373 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20374 identifier = cp_parser_identifier (parser);
20375 else
20376 identifier = NULL_TREE;
20378 /* Consume the `:' token. */
20379 cp_lexer_consume_token (parser->lexer);
20380 /* Get the width of the bitfield. */
20381 width
20382 = cp_parser_constant_expression (parser,
20383 /*allow_non_constant=*/false,
20384 NULL);
20386 /* Look for attributes that apply to the bitfield. */
20387 attributes = cp_parser_attributes_opt (parser);
20388 /* Remember which attributes are prefix attributes and
20389 which are not. */
20390 first_attribute = attributes;
20391 /* Combine the attributes. */
20392 attributes = chainon (prefix_attributes, attributes);
20394 /* Create the bitfield declaration. */
20395 decl = grokbitfield (identifier
20396 ? make_id_declarator (NULL_TREE,
20397 identifier,
20398 sfk_none)
20399 : NULL,
20400 &decl_specifiers,
20401 width,
20402 attributes);
20404 else
20406 cp_declarator *declarator;
20407 tree initializer;
20408 tree asm_specification;
20409 int ctor_dtor_or_conv_p;
20411 /* Parse the declarator. */
20412 declarator
20413 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20414 &ctor_dtor_or_conv_p,
20415 /*parenthesized_p=*/NULL,
20416 /*member_p=*/true);
20418 /* If something went wrong parsing the declarator, make sure
20419 that we at least consume some tokens. */
20420 if (declarator == cp_error_declarator)
20422 /* Skip to the end of the statement. */
20423 cp_parser_skip_to_end_of_statement (parser);
20424 /* If the next token is not a semicolon, that is
20425 probably because we just skipped over the body of
20426 a function. So, we consume a semicolon if
20427 present, but do not issue an error message if it
20428 is not present. */
20429 if (cp_lexer_next_token_is (parser->lexer,
20430 CPP_SEMICOLON))
20431 cp_lexer_consume_token (parser->lexer);
20432 goto out;
20435 if (declares_class_or_enum & 2)
20436 cp_parser_check_for_definition_in_return_type
20437 (declarator, decl_specifiers.type,
20438 decl_specifiers.locations[ds_type_spec]);
20440 /* Look for an asm-specification. */
20441 asm_specification = cp_parser_asm_specification_opt (parser);
20442 /* Look for attributes that apply to the declaration. */
20443 attributes = cp_parser_attributes_opt (parser);
20444 /* Remember which attributes are prefix attributes and
20445 which are not. */
20446 first_attribute = attributes;
20447 /* Combine the attributes. */
20448 attributes = chainon (prefix_attributes, attributes);
20450 /* If it's an `=', then we have a constant-initializer or a
20451 pure-specifier. It is not correct to parse the
20452 initializer before registering the member declaration
20453 since the member declaration should be in scope while
20454 its initializer is processed. However, the rest of the
20455 front end does not yet provide an interface that allows
20456 us to handle this correctly. */
20457 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20459 /* In [class.mem]:
20461 A pure-specifier shall be used only in the declaration of
20462 a virtual function.
20464 A member-declarator can contain a constant-initializer
20465 only if it declares a static member of integral or
20466 enumeration type.
20468 Therefore, if the DECLARATOR is for a function, we look
20469 for a pure-specifier; otherwise, we look for a
20470 constant-initializer. When we call `grokfield', it will
20471 perform more stringent semantics checks. */
20472 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20473 if (function_declarator_p (declarator)
20474 || (decl_specifiers.type
20475 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20476 && declarator->kind == cdk_id
20477 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20478 == FUNCTION_TYPE)))
20479 initializer = cp_parser_pure_specifier (parser);
20480 else if (decl_specifiers.storage_class != sc_static)
20481 initializer = cp_parser_save_nsdmi (parser);
20482 else if (cxx_dialect >= cxx11)
20484 bool nonconst;
20485 /* Don't require a constant rvalue in C++11, since we
20486 might want a reference constant. We'll enforce
20487 constancy later. */
20488 cp_lexer_consume_token (parser->lexer);
20489 /* Parse the initializer. */
20490 initializer = cp_parser_initializer_clause (parser,
20491 &nonconst);
20493 else
20494 /* Parse the initializer. */
20495 initializer = cp_parser_constant_initializer (parser);
20497 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20498 && !function_declarator_p (declarator))
20500 bool x;
20501 if (decl_specifiers.storage_class != sc_static)
20502 initializer = cp_parser_save_nsdmi (parser);
20503 else
20504 initializer = cp_parser_initializer (parser, &x, &x);
20506 /* Otherwise, there is no initializer. */
20507 else
20508 initializer = NULL_TREE;
20510 /* See if we are probably looking at a function
20511 definition. We are certainly not looking at a
20512 member-declarator. Calling `grokfield' has
20513 side-effects, so we must not do it unless we are sure
20514 that we are looking at a member-declarator. */
20515 if (cp_parser_token_starts_function_definition_p
20516 (cp_lexer_peek_token (parser->lexer)))
20518 /* The grammar does not allow a pure-specifier to be
20519 used when a member function is defined. (It is
20520 possible that this fact is an oversight in the
20521 standard, since a pure function may be defined
20522 outside of the class-specifier. */
20523 if (initializer && initializer_token_start)
20524 error_at (initializer_token_start->location,
20525 "pure-specifier on function-definition");
20526 decl = cp_parser_save_member_function_body (parser,
20527 &decl_specifiers,
20528 declarator,
20529 attributes);
20530 if (parser->fully_implicit_function_template_p)
20531 decl = finish_fully_implicit_template (parser, decl);
20532 /* If the member was not a friend, declare it here. */
20533 if (!friend_p)
20534 finish_member_declaration (decl);
20535 /* Peek at the next token. */
20536 token = cp_lexer_peek_token (parser->lexer);
20537 /* If the next token is a semicolon, consume it. */
20538 if (token->type == CPP_SEMICOLON)
20539 cp_lexer_consume_token (parser->lexer);
20540 goto out;
20542 else
20543 if (declarator->kind == cdk_function)
20544 declarator->id_loc = token->location;
20545 /* Create the declaration. */
20546 decl = grokfield (declarator, &decl_specifiers,
20547 initializer, /*init_const_expr_p=*/true,
20548 asm_specification, attributes);
20549 if (parser->fully_implicit_function_template_p)
20551 if (friend_p)
20552 finish_fully_implicit_template (parser, 0);
20553 else
20554 decl = finish_fully_implicit_template (parser, decl);
20558 cp_finalize_omp_declare_simd (parser, decl);
20560 /* Reset PREFIX_ATTRIBUTES. */
20561 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20562 attributes = TREE_CHAIN (attributes);
20563 if (attributes)
20564 TREE_CHAIN (attributes) = NULL_TREE;
20566 /* If there is any qualification still in effect, clear it
20567 now; we will be starting fresh with the next declarator. */
20568 parser->scope = NULL_TREE;
20569 parser->qualifying_scope = NULL_TREE;
20570 parser->object_scope = NULL_TREE;
20571 /* If it's a `,', then there are more declarators. */
20572 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20574 cp_lexer_consume_token (parser->lexer);
20575 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20577 cp_token *token = cp_lexer_previous_token (parser->lexer);
20578 error_at (token->location,
20579 "stray %<,%> at end of member declaration");
20582 /* If the next token isn't a `;', then we have a parse error. */
20583 else if (cp_lexer_next_token_is_not (parser->lexer,
20584 CPP_SEMICOLON))
20586 /* The next token might be a ways away from where the
20587 actual semicolon is missing. Find the previous token
20588 and use that for our error position. */
20589 cp_token *token = cp_lexer_previous_token (parser->lexer);
20590 error_at (token->location,
20591 "expected %<;%> at end of member declaration");
20593 /* Assume that the user meant to provide a semicolon. If
20594 we were to cp_parser_skip_to_end_of_statement, we might
20595 skip to a semicolon inside a member function definition
20596 and issue nonsensical error messages. */
20597 assume_semicolon = true;
20600 if (decl)
20602 /* Add DECL to the list of members. */
20603 if (!friend_p)
20604 finish_member_declaration (decl);
20606 if (TREE_CODE (decl) == FUNCTION_DECL)
20607 cp_parser_save_default_args (parser, decl);
20608 else if (TREE_CODE (decl) == FIELD_DECL
20609 && !DECL_C_BIT_FIELD (decl)
20610 && DECL_INITIAL (decl))
20611 /* Add DECL to the queue of NSDMI to be parsed later. */
20612 vec_safe_push (unparsed_nsdmis, decl);
20615 if (assume_semicolon)
20616 goto out;
20620 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20621 out:
20622 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20625 /* Parse a pure-specifier.
20627 pure-specifier:
20630 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20631 Otherwise, ERROR_MARK_NODE is returned. */
20633 static tree
20634 cp_parser_pure_specifier (cp_parser* parser)
20636 cp_token *token;
20638 /* Look for the `=' token. */
20639 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20640 return error_mark_node;
20641 /* Look for the `0' token. */
20642 token = cp_lexer_peek_token (parser->lexer);
20644 if (token->type == CPP_EOF
20645 || token->type == CPP_PRAGMA_EOL)
20646 return error_mark_node;
20648 cp_lexer_consume_token (parser->lexer);
20650 /* Accept = default or = delete in c++0x mode. */
20651 if (token->keyword == RID_DEFAULT
20652 || token->keyword == RID_DELETE)
20654 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20655 return token->u.value;
20658 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20659 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20661 cp_parser_error (parser,
20662 "invalid pure specifier (only %<= 0%> is allowed)");
20663 cp_parser_skip_to_end_of_statement (parser);
20664 return error_mark_node;
20666 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20668 error_at (token->location, "templates may not be %<virtual%>");
20669 return error_mark_node;
20672 return integer_zero_node;
20675 /* Parse a constant-initializer.
20677 constant-initializer:
20678 = constant-expression
20680 Returns a representation of the constant-expression. */
20682 static tree
20683 cp_parser_constant_initializer (cp_parser* parser)
20685 /* Look for the `=' token. */
20686 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20687 return error_mark_node;
20689 /* It is invalid to write:
20691 struct S { static const int i = { 7 }; };
20694 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20696 cp_parser_error (parser,
20697 "a brace-enclosed initializer is not allowed here");
20698 /* Consume the opening brace. */
20699 cp_lexer_consume_token (parser->lexer);
20700 /* Skip the initializer. */
20701 cp_parser_skip_to_closing_brace (parser);
20702 /* Look for the trailing `}'. */
20703 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20705 return error_mark_node;
20708 return cp_parser_constant_expression (parser,
20709 /*allow_non_constant=*/false,
20710 NULL);
20713 /* Derived classes [gram.class.derived] */
20715 /* Parse a base-clause.
20717 base-clause:
20718 : base-specifier-list
20720 base-specifier-list:
20721 base-specifier ... [opt]
20722 base-specifier-list , base-specifier ... [opt]
20724 Returns a TREE_LIST representing the base-classes, in the order in
20725 which they were declared. The representation of each node is as
20726 described by cp_parser_base_specifier.
20728 In the case that no bases are specified, this function will return
20729 NULL_TREE, not ERROR_MARK_NODE. */
20731 static tree
20732 cp_parser_base_clause (cp_parser* parser)
20734 tree bases = NULL_TREE;
20736 /* Look for the `:' that begins the list. */
20737 cp_parser_require (parser, CPP_COLON, RT_COLON);
20739 /* Scan the base-specifier-list. */
20740 while (true)
20742 cp_token *token;
20743 tree base;
20744 bool pack_expansion_p = false;
20746 /* Look for the base-specifier. */
20747 base = cp_parser_base_specifier (parser);
20748 /* Look for the (optional) ellipsis. */
20749 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20751 /* Consume the `...'. */
20752 cp_lexer_consume_token (parser->lexer);
20754 pack_expansion_p = true;
20757 /* Add BASE to the front of the list. */
20758 if (base && base != error_mark_node)
20760 if (pack_expansion_p)
20761 /* Make this a pack expansion type. */
20762 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
20764 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
20766 TREE_CHAIN (base) = bases;
20767 bases = base;
20770 /* Peek at the next token. */
20771 token = cp_lexer_peek_token (parser->lexer);
20772 /* If it's not a comma, then the list is complete. */
20773 if (token->type != CPP_COMMA)
20774 break;
20775 /* Consume the `,'. */
20776 cp_lexer_consume_token (parser->lexer);
20779 /* PARSER->SCOPE may still be non-NULL at this point, if the last
20780 base class had a qualified name. However, the next name that
20781 appears is certainly not qualified. */
20782 parser->scope = NULL_TREE;
20783 parser->qualifying_scope = NULL_TREE;
20784 parser->object_scope = NULL_TREE;
20786 return nreverse (bases);
20789 /* Parse a base-specifier.
20791 base-specifier:
20792 :: [opt] nested-name-specifier [opt] class-name
20793 virtual access-specifier [opt] :: [opt] nested-name-specifier
20794 [opt] class-name
20795 access-specifier virtual [opt] :: [opt] nested-name-specifier
20796 [opt] class-name
20798 Returns a TREE_LIST. The TREE_PURPOSE will be one of
20799 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
20800 indicate the specifiers provided. The TREE_VALUE will be a TYPE
20801 (or the ERROR_MARK_NODE) indicating the type that was specified. */
20803 static tree
20804 cp_parser_base_specifier (cp_parser* parser)
20806 cp_token *token;
20807 bool done = false;
20808 bool virtual_p = false;
20809 bool duplicate_virtual_error_issued_p = false;
20810 bool duplicate_access_error_issued_p = false;
20811 bool class_scope_p, template_p;
20812 tree access = access_default_node;
20813 tree type;
20815 /* Process the optional `virtual' and `access-specifier'. */
20816 while (!done)
20818 /* Peek at the next token. */
20819 token = cp_lexer_peek_token (parser->lexer);
20820 /* Process `virtual'. */
20821 switch (token->keyword)
20823 case RID_VIRTUAL:
20824 /* If `virtual' appears more than once, issue an error. */
20825 if (virtual_p && !duplicate_virtual_error_issued_p)
20827 cp_parser_error (parser,
20828 "%<virtual%> specified more than once in base-specified");
20829 duplicate_virtual_error_issued_p = true;
20832 virtual_p = true;
20834 /* Consume the `virtual' token. */
20835 cp_lexer_consume_token (parser->lexer);
20837 break;
20839 case RID_PUBLIC:
20840 case RID_PROTECTED:
20841 case RID_PRIVATE:
20842 /* If more than one access specifier appears, issue an
20843 error. */
20844 if (access != access_default_node
20845 && !duplicate_access_error_issued_p)
20847 cp_parser_error (parser,
20848 "more than one access specifier in base-specified");
20849 duplicate_access_error_issued_p = true;
20852 access = ridpointers[(int) token->keyword];
20854 /* Consume the access-specifier. */
20855 cp_lexer_consume_token (parser->lexer);
20857 break;
20859 default:
20860 done = true;
20861 break;
20864 /* It is not uncommon to see programs mechanically, erroneously, use
20865 the 'typename' keyword to denote (dependent) qualified types
20866 as base classes. */
20867 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
20869 token = cp_lexer_peek_token (parser->lexer);
20870 if (!processing_template_decl)
20871 error_at (token->location,
20872 "keyword %<typename%> not allowed outside of templates");
20873 else
20874 error_at (token->location,
20875 "keyword %<typename%> not allowed in this context "
20876 "(the base class is implicitly a type)");
20877 cp_lexer_consume_token (parser->lexer);
20880 /* Look for the optional `::' operator. */
20881 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
20882 /* Look for the nested-name-specifier. The simplest way to
20883 implement:
20885 [temp.res]
20887 The keyword `typename' is not permitted in a base-specifier or
20888 mem-initializer; in these contexts a qualified name that
20889 depends on a template-parameter is implicitly assumed to be a
20890 type name.
20892 is to pretend that we have seen the `typename' keyword at this
20893 point. */
20894 cp_parser_nested_name_specifier_opt (parser,
20895 /*typename_keyword_p=*/true,
20896 /*check_dependency_p=*/true,
20897 typename_type,
20898 /*is_declaration=*/true);
20899 /* If the base class is given by a qualified name, assume that names
20900 we see are type names or templates, as appropriate. */
20901 class_scope_p = (parser->scope && TYPE_P (parser->scope));
20902 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
20904 if (!parser->scope
20905 && cp_lexer_next_token_is_decltype (parser->lexer))
20906 /* DR 950 allows decltype as a base-specifier. */
20907 type = cp_parser_decltype (parser);
20908 else
20910 /* Otherwise, look for the class-name. */
20911 type = cp_parser_class_name (parser,
20912 class_scope_p,
20913 template_p,
20914 typename_type,
20915 /*check_dependency_p=*/true,
20916 /*class_head_p=*/false,
20917 /*is_declaration=*/true);
20918 type = TREE_TYPE (type);
20921 if (type == error_mark_node)
20922 return error_mark_node;
20924 return finish_base_specifier (type, access, virtual_p);
20927 /* Exception handling [gram.exception] */
20929 /* Parse an (optional) noexcept-specification.
20931 noexcept-specification:
20932 noexcept ( constant-expression ) [opt]
20934 If no noexcept-specification is present, returns NULL_TREE.
20935 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
20936 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
20937 there are no parentheses. CONSUMED_EXPR will be set accordingly.
20938 Otherwise, returns a noexcept specification unless RETURN_COND is true,
20939 in which case a boolean condition is returned instead. */
20941 static tree
20942 cp_parser_noexcept_specification_opt (cp_parser* parser,
20943 bool require_constexpr,
20944 bool* consumed_expr,
20945 bool return_cond)
20947 cp_token *token;
20948 const char *saved_message;
20950 /* Peek at the next token. */
20951 token = cp_lexer_peek_token (parser->lexer);
20953 /* Is it a noexcept-specification? */
20954 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
20956 tree expr;
20957 cp_lexer_consume_token (parser->lexer);
20959 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
20961 cp_lexer_consume_token (parser->lexer);
20963 if (require_constexpr)
20965 /* Types may not be defined in an exception-specification. */
20966 saved_message = parser->type_definition_forbidden_message;
20967 parser->type_definition_forbidden_message
20968 = G_("types may not be defined in an exception-specification");
20970 expr = cp_parser_constant_expression (parser, false, NULL);
20972 /* Restore the saved message. */
20973 parser->type_definition_forbidden_message = saved_message;
20975 else
20977 expr = cp_parser_expression (parser, false, NULL);
20978 *consumed_expr = true;
20981 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20983 else
20985 expr = boolean_true_node;
20986 if (!require_constexpr)
20987 *consumed_expr = false;
20990 /* We cannot build a noexcept-spec right away because this will check
20991 that expr is a constexpr. */
20992 if (!return_cond)
20993 return build_noexcept_spec (expr, tf_warning_or_error);
20994 else
20995 return expr;
20997 else
20998 return NULL_TREE;
21001 /* Parse an (optional) exception-specification.
21003 exception-specification:
21004 throw ( type-id-list [opt] )
21006 Returns a TREE_LIST representing the exception-specification. The
21007 TREE_VALUE of each node is a type. */
21009 static tree
21010 cp_parser_exception_specification_opt (cp_parser* parser)
21012 cp_token *token;
21013 tree type_id_list;
21014 const char *saved_message;
21016 /* Peek at the next token. */
21017 token = cp_lexer_peek_token (parser->lexer);
21019 /* Is it a noexcept-specification? */
21020 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21021 false);
21022 if (type_id_list != NULL_TREE)
21023 return type_id_list;
21025 /* If it's not `throw', then there's no exception-specification. */
21026 if (!cp_parser_is_keyword (token, RID_THROW))
21027 return NULL_TREE;
21029 #if 0
21030 /* Enable this once a lot of code has transitioned to noexcept? */
21031 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21032 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21033 "deprecated in C++0x; use %<noexcept%> instead");
21034 #endif
21036 /* Consume the `throw'. */
21037 cp_lexer_consume_token (parser->lexer);
21039 /* Look for the `('. */
21040 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21042 /* Peek at the next token. */
21043 token = cp_lexer_peek_token (parser->lexer);
21044 /* If it's not a `)', then there is a type-id-list. */
21045 if (token->type != CPP_CLOSE_PAREN)
21047 /* Types may not be defined in an exception-specification. */
21048 saved_message = parser->type_definition_forbidden_message;
21049 parser->type_definition_forbidden_message
21050 = G_("types may not be defined in an exception-specification");
21051 /* Parse the type-id-list. */
21052 type_id_list = cp_parser_type_id_list (parser);
21053 /* Restore the saved message. */
21054 parser->type_definition_forbidden_message = saved_message;
21056 else
21057 type_id_list = empty_except_spec;
21059 /* Look for the `)'. */
21060 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21062 return type_id_list;
21065 /* Parse an (optional) type-id-list.
21067 type-id-list:
21068 type-id ... [opt]
21069 type-id-list , type-id ... [opt]
21071 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21072 in the order that the types were presented. */
21074 static tree
21075 cp_parser_type_id_list (cp_parser* parser)
21077 tree types = NULL_TREE;
21079 while (true)
21081 cp_token *token;
21082 tree type;
21084 /* Get the next type-id. */
21085 type = cp_parser_type_id (parser);
21086 /* Parse the optional ellipsis. */
21087 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21089 /* Consume the `...'. */
21090 cp_lexer_consume_token (parser->lexer);
21092 /* Turn the type into a pack expansion expression. */
21093 type = make_pack_expansion (type);
21095 /* Add it to the list. */
21096 types = add_exception_specifier (types, type, /*complain=*/1);
21097 /* Peek at the next token. */
21098 token = cp_lexer_peek_token (parser->lexer);
21099 /* If it is not a `,', we are done. */
21100 if (token->type != CPP_COMMA)
21101 break;
21102 /* Consume the `,'. */
21103 cp_lexer_consume_token (parser->lexer);
21106 return nreverse (types);
21109 /* Parse a try-block.
21111 try-block:
21112 try compound-statement handler-seq */
21114 static tree
21115 cp_parser_try_block (cp_parser* parser)
21117 tree try_block;
21119 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21120 try_block = begin_try_block ();
21121 cp_parser_compound_statement (parser, NULL, true, false);
21122 finish_try_block (try_block);
21123 cp_parser_handler_seq (parser);
21124 finish_handler_sequence (try_block);
21126 return try_block;
21129 /* Parse a function-try-block.
21131 function-try-block:
21132 try ctor-initializer [opt] function-body handler-seq */
21134 static bool
21135 cp_parser_function_try_block (cp_parser* parser)
21137 tree compound_stmt;
21138 tree try_block;
21139 bool ctor_initializer_p;
21141 /* Look for the `try' keyword. */
21142 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21143 return false;
21144 /* Let the rest of the front end know where we are. */
21145 try_block = begin_function_try_block (&compound_stmt);
21146 /* Parse the function-body. */
21147 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21148 (parser, /*in_function_try_block=*/true);
21149 /* We're done with the `try' part. */
21150 finish_function_try_block (try_block);
21151 /* Parse the handlers. */
21152 cp_parser_handler_seq (parser);
21153 /* We're done with the handlers. */
21154 finish_function_handler_sequence (try_block, compound_stmt);
21156 return ctor_initializer_p;
21159 /* Parse a handler-seq.
21161 handler-seq:
21162 handler handler-seq [opt] */
21164 static void
21165 cp_parser_handler_seq (cp_parser* parser)
21167 while (true)
21169 cp_token *token;
21171 /* Parse the handler. */
21172 cp_parser_handler (parser);
21173 /* Peek at the next token. */
21174 token = cp_lexer_peek_token (parser->lexer);
21175 /* If it's not `catch' then there are no more handlers. */
21176 if (!cp_parser_is_keyword (token, RID_CATCH))
21177 break;
21181 /* Parse a handler.
21183 handler:
21184 catch ( exception-declaration ) compound-statement */
21186 static void
21187 cp_parser_handler (cp_parser* parser)
21189 tree handler;
21190 tree declaration;
21192 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21193 handler = begin_handler ();
21194 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21195 declaration = cp_parser_exception_declaration (parser);
21196 finish_handler_parms (declaration, handler);
21197 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21198 cp_parser_compound_statement (parser, NULL, false, false);
21199 finish_handler (handler);
21202 /* Parse an exception-declaration.
21204 exception-declaration:
21205 type-specifier-seq declarator
21206 type-specifier-seq abstract-declarator
21207 type-specifier-seq
21210 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21211 ellipsis variant is used. */
21213 static tree
21214 cp_parser_exception_declaration (cp_parser* parser)
21216 cp_decl_specifier_seq type_specifiers;
21217 cp_declarator *declarator;
21218 const char *saved_message;
21220 /* If it's an ellipsis, it's easy to handle. */
21221 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21223 /* Consume the `...' token. */
21224 cp_lexer_consume_token (parser->lexer);
21225 return NULL_TREE;
21228 /* Types may not be defined in exception-declarations. */
21229 saved_message = parser->type_definition_forbidden_message;
21230 parser->type_definition_forbidden_message
21231 = G_("types may not be defined in exception-declarations");
21233 /* Parse the type-specifier-seq. */
21234 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21235 /*is_trailing_return=*/false,
21236 &type_specifiers);
21237 /* If it's a `)', then there is no declarator. */
21238 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21239 declarator = NULL;
21240 else
21241 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21242 /*ctor_dtor_or_conv_p=*/NULL,
21243 /*parenthesized_p=*/NULL,
21244 /*member_p=*/false);
21246 /* Restore the saved message. */
21247 parser->type_definition_forbidden_message = saved_message;
21249 if (!type_specifiers.any_specifiers_p)
21250 return error_mark_node;
21252 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21255 /* Parse a throw-expression.
21257 throw-expression:
21258 throw assignment-expression [opt]
21260 Returns a THROW_EXPR representing the throw-expression. */
21262 static tree
21263 cp_parser_throw_expression (cp_parser* parser)
21265 tree expression;
21266 cp_token* token;
21268 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21269 token = cp_lexer_peek_token (parser->lexer);
21270 /* Figure out whether or not there is an assignment-expression
21271 following the "throw" keyword. */
21272 if (token->type == CPP_COMMA
21273 || token->type == CPP_SEMICOLON
21274 || token->type == CPP_CLOSE_PAREN
21275 || token->type == CPP_CLOSE_SQUARE
21276 || token->type == CPP_CLOSE_BRACE
21277 || token->type == CPP_COLON)
21278 expression = NULL_TREE;
21279 else
21280 expression = cp_parser_assignment_expression (parser,
21281 /*cast_p=*/false, NULL);
21283 return build_throw (expression);
21286 /* GNU Extensions */
21288 /* Parse an (optional) asm-specification.
21290 asm-specification:
21291 asm ( string-literal )
21293 If the asm-specification is present, returns a STRING_CST
21294 corresponding to the string-literal. Otherwise, returns
21295 NULL_TREE. */
21297 static tree
21298 cp_parser_asm_specification_opt (cp_parser* parser)
21300 cp_token *token;
21301 tree asm_specification;
21303 /* Peek at the next token. */
21304 token = cp_lexer_peek_token (parser->lexer);
21305 /* If the next token isn't the `asm' keyword, then there's no
21306 asm-specification. */
21307 if (!cp_parser_is_keyword (token, RID_ASM))
21308 return NULL_TREE;
21310 /* Consume the `asm' token. */
21311 cp_lexer_consume_token (parser->lexer);
21312 /* Look for the `('. */
21313 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21315 /* Look for the string-literal. */
21316 asm_specification = cp_parser_string_literal (parser, false, false);
21318 /* Look for the `)'. */
21319 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21321 return asm_specification;
21324 /* Parse an asm-operand-list.
21326 asm-operand-list:
21327 asm-operand
21328 asm-operand-list , asm-operand
21330 asm-operand:
21331 string-literal ( expression )
21332 [ string-literal ] string-literal ( expression )
21334 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21335 each node is the expression. The TREE_PURPOSE is itself a
21336 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21337 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21338 is a STRING_CST for the string literal before the parenthesis. Returns
21339 ERROR_MARK_NODE if any of the operands are invalid. */
21341 static tree
21342 cp_parser_asm_operand_list (cp_parser* parser)
21344 tree asm_operands = NULL_TREE;
21345 bool invalid_operands = false;
21347 while (true)
21349 tree string_literal;
21350 tree expression;
21351 tree name;
21353 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21355 /* Consume the `[' token. */
21356 cp_lexer_consume_token (parser->lexer);
21357 /* Read the operand name. */
21358 name = cp_parser_identifier (parser);
21359 if (name != error_mark_node)
21360 name = build_string (IDENTIFIER_LENGTH (name),
21361 IDENTIFIER_POINTER (name));
21362 /* Look for the closing `]'. */
21363 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21365 else
21366 name = NULL_TREE;
21367 /* Look for the string-literal. */
21368 string_literal = cp_parser_string_literal (parser, false, false);
21370 /* Look for the `('. */
21371 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21372 /* Parse the expression. */
21373 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
21374 /* Look for the `)'. */
21375 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21377 if (name == error_mark_node
21378 || string_literal == error_mark_node
21379 || expression == error_mark_node)
21380 invalid_operands = true;
21382 /* Add this operand to the list. */
21383 asm_operands = tree_cons (build_tree_list (name, string_literal),
21384 expression,
21385 asm_operands);
21386 /* If the next token is not a `,', there are no more
21387 operands. */
21388 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21389 break;
21390 /* Consume the `,'. */
21391 cp_lexer_consume_token (parser->lexer);
21394 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21397 /* Parse an asm-clobber-list.
21399 asm-clobber-list:
21400 string-literal
21401 asm-clobber-list , string-literal
21403 Returns a TREE_LIST, indicating the clobbers in the order that they
21404 appeared. The TREE_VALUE of each node is a STRING_CST. */
21406 static tree
21407 cp_parser_asm_clobber_list (cp_parser* parser)
21409 tree clobbers = NULL_TREE;
21411 while (true)
21413 tree string_literal;
21415 /* Look for the string literal. */
21416 string_literal = cp_parser_string_literal (parser, false, false);
21417 /* Add it to the list. */
21418 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21419 /* If the next token is not a `,', then the list is
21420 complete. */
21421 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21422 break;
21423 /* Consume the `,' token. */
21424 cp_lexer_consume_token (parser->lexer);
21427 return clobbers;
21430 /* Parse an asm-label-list.
21432 asm-label-list:
21433 identifier
21434 asm-label-list , identifier
21436 Returns a TREE_LIST, indicating the labels in the order that they
21437 appeared. The TREE_VALUE of each node is a label. */
21439 static tree
21440 cp_parser_asm_label_list (cp_parser* parser)
21442 tree labels = NULL_TREE;
21444 while (true)
21446 tree identifier, label, name;
21448 /* Look for the identifier. */
21449 identifier = cp_parser_identifier (parser);
21450 if (!error_operand_p (identifier))
21452 label = lookup_label (identifier);
21453 if (TREE_CODE (label) == LABEL_DECL)
21455 TREE_USED (label) = 1;
21456 check_goto (label);
21457 name = build_string (IDENTIFIER_LENGTH (identifier),
21458 IDENTIFIER_POINTER (identifier));
21459 labels = tree_cons (name, label, labels);
21462 /* If the next token is not a `,', then the list is
21463 complete. */
21464 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21465 break;
21466 /* Consume the `,' token. */
21467 cp_lexer_consume_token (parser->lexer);
21470 return nreverse (labels);
21473 /* Return TRUE iff the next tokens in the stream are possibly the
21474 beginning of a GNU extension attribute. */
21476 static bool
21477 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21479 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21482 /* Return TRUE iff the next tokens in the stream are possibly the
21483 beginning of a standard C++-11 attribute specifier. */
21485 static bool
21486 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21488 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21491 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21492 beginning of a standard C++-11 attribute specifier. */
21494 static bool
21495 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21497 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21499 return (cxx_dialect >= cxx11
21500 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21501 || (token->type == CPP_OPEN_SQUARE
21502 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21503 && token->type == CPP_OPEN_SQUARE)));
21506 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21507 beginning of a GNU extension attribute. */
21509 static bool
21510 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21512 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21514 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21517 /* Return true iff the next tokens can be the beginning of either a
21518 GNU attribute list, or a standard C++11 attribute sequence. */
21520 static bool
21521 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21523 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21524 || cp_next_tokens_can_be_std_attribute_p (parser));
21527 /* Return true iff the next Nth tokens can be the beginning of either
21528 a GNU attribute list, or a standard C++11 attribute sequence. */
21530 static bool
21531 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21533 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21534 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21537 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21538 of GNU attributes, or return NULL. */
21540 static tree
21541 cp_parser_attributes_opt (cp_parser *parser)
21543 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21544 return cp_parser_gnu_attributes_opt (parser);
21545 return cp_parser_std_attribute_spec_seq (parser);
21548 #define CILK_SIMD_FN_CLAUSE_MASK \
21549 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
21550 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
21551 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
21552 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
21553 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21555 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
21556 vector [(<clauses>)] */
21558 static void
21559 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
21561 bool first_p = parser->cilk_simd_fn_info == NULL;
21562 cp_token *token = v_token;
21563 if (first_p)
21565 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
21566 parser->cilk_simd_fn_info->error_seen = false;
21567 parser->cilk_simd_fn_info->fndecl_seen = false;
21568 parser->cilk_simd_fn_info->tokens = vNULL;
21570 int paren_scope = 0;
21571 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21573 cp_lexer_consume_token (parser->lexer);
21574 v_token = cp_lexer_peek_token (parser->lexer);
21575 paren_scope++;
21577 while (paren_scope > 0)
21579 token = cp_lexer_peek_token (parser->lexer);
21580 if (token->type == CPP_OPEN_PAREN)
21581 paren_scope++;
21582 else if (token->type == CPP_CLOSE_PAREN)
21583 paren_scope--;
21584 /* Do not push the last ')' */
21585 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
21586 cp_lexer_consume_token (parser->lexer);
21589 token->type = CPP_PRAGMA_EOL;
21590 parser->lexer->next_token = token;
21591 cp_lexer_consume_token (parser->lexer);
21593 struct cp_token_cache *cp
21594 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
21595 parser->cilk_simd_fn_info->tokens.safe_push (cp);
21598 /* Parse an (optional) series of attributes.
21600 attributes:
21601 attributes attribute
21603 attribute:
21604 __attribute__ (( attribute-list [opt] ))
21606 The return value is as for cp_parser_gnu_attribute_list. */
21608 static tree
21609 cp_parser_gnu_attributes_opt (cp_parser* parser)
21611 tree attributes = NULL_TREE;
21613 while (true)
21615 cp_token *token;
21616 tree attribute_list;
21617 bool ok = true;
21619 /* Peek at the next token. */
21620 token = cp_lexer_peek_token (parser->lexer);
21621 /* If it's not `__attribute__', then we're done. */
21622 if (token->keyword != RID_ATTRIBUTE)
21623 break;
21625 /* Consume the `__attribute__' keyword. */
21626 cp_lexer_consume_token (parser->lexer);
21627 /* Look for the two `(' tokens. */
21628 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21629 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21631 /* Peek at the next token. */
21632 token = cp_lexer_peek_token (parser->lexer);
21633 if (token->type != CPP_CLOSE_PAREN)
21634 /* Parse the attribute-list. */
21635 attribute_list = cp_parser_gnu_attribute_list (parser);
21636 else
21637 /* If the next token is a `)', then there is no attribute
21638 list. */
21639 attribute_list = NULL;
21641 /* Look for the two `)' tokens. */
21642 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21643 ok = false;
21644 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21645 ok = false;
21646 if (!ok)
21647 cp_parser_skip_to_end_of_statement (parser);
21649 /* Add these new attributes to the list. */
21650 attributes = chainon (attributes, attribute_list);
21653 return attributes;
21656 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
21657 "__vector" or "__vector__." */
21659 static inline bool
21660 is_cilkplus_vector_p (tree name)
21662 if (flag_cilkplus && is_attribute_p ("vector", name))
21663 return true;
21664 return false;
21667 /* Parse a GNU attribute-list.
21669 attribute-list:
21670 attribute
21671 attribute-list , attribute
21673 attribute:
21674 identifier
21675 identifier ( identifier )
21676 identifier ( identifier , expression-list )
21677 identifier ( expression-list )
21679 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21680 to an attribute. The TREE_PURPOSE of each node is the identifier
21681 indicating which attribute is in use. The TREE_VALUE represents
21682 the arguments, if any. */
21684 static tree
21685 cp_parser_gnu_attribute_list (cp_parser* parser)
21687 tree attribute_list = NULL_TREE;
21688 bool save_translate_strings_p = parser->translate_strings_p;
21690 parser->translate_strings_p = false;
21691 while (true)
21693 cp_token *token;
21694 tree identifier;
21695 tree attribute;
21697 /* Look for the identifier. We also allow keywords here; for
21698 example `__attribute__ ((const))' is legal. */
21699 token = cp_lexer_peek_token (parser->lexer);
21700 if (token->type == CPP_NAME
21701 || token->type == CPP_KEYWORD)
21703 tree arguments = NULL_TREE;
21705 /* Consume the token, but save it since we need it for the
21706 SIMD enabled function parsing. */
21707 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
21709 /* Save away the identifier that indicates which attribute
21710 this is. */
21711 identifier = (token->type == CPP_KEYWORD)
21712 /* For keywords, use the canonical spelling, not the
21713 parsed identifier. */
21714 ? ridpointers[(int) token->keyword]
21715 : id_token->u.value;
21717 attribute = build_tree_list (identifier, NULL_TREE);
21719 /* Peek at the next token. */
21720 token = cp_lexer_peek_token (parser->lexer);
21721 /* If it's an `(', then parse the attribute arguments. */
21722 if (token->type == CPP_OPEN_PAREN)
21724 vec<tree, va_gc> *vec;
21725 int attr_flag = (attribute_takes_identifier_p (identifier)
21726 ? id_attr : normal_attr);
21727 if (is_cilkplus_vector_p (identifier))
21729 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21730 continue;
21732 else
21733 vec = cp_parser_parenthesized_expression_list
21734 (parser, attr_flag, /*cast_p=*/false,
21735 /*allow_expansion_p=*/false,
21736 /*non_constant_p=*/NULL);
21737 if (vec == NULL)
21738 arguments = error_mark_node;
21739 else
21741 arguments = build_tree_list_vec (vec);
21742 release_tree_vector (vec);
21744 /* Save the arguments away. */
21745 TREE_VALUE (attribute) = arguments;
21747 else if (is_cilkplus_vector_p (identifier))
21749 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21750 continue;
21753 if (arguments != error_mark_node)
21755 /* Add this attribute to the list. */
21756 TREE_CHAIN (attribute) = attribute_list;
21757 attribute_list = attribute;
21760 token = cp_lexer_peek_token (parser->lexer);
21762 /* Now, look for more attributes. If the next token isn't a
21763 `,', we're done. */
21764 if (token->type != CPP_COMMA)
21765 break;
21767 /* Consume the comma and keep going. */
21768 cp_lexer_consume_token (parser->lexer);
21770 parser->translate_strings_p = save_translate_strings_p;
21772 /* We built up the list in reverse order. */
21773 return nreverse (attribute_list);
21776 /* Parse a standard C++11 attribute.
21778 The returned representation is a TREE_LIST which TREE_PURPOSE is
21779 the scoped name of the attribute, and the TREE_VALUE is its
21780 arguments list.
21782 Note that the scoped name of the attribute is itself a TREE_LIST
21783 which TREE_PURPOSE is the namespace of the attribute, and
21784 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
21785 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
21786 and which TREE_PURPOSE is directly the attribute name.
21788 Clients of the attribute code should use get_attribute_namespace
21789 and get_attribute_name to get the actual namespace and name of
21790 attributes, regardless of their being GNU or C++11 attributes.
21792 attribute:
21793 attribute-token attribute-argument-clause [opt]
21795 attribute-token:
21796 identifier
21797 attribute-scoped-token
21799 attribute-scoped-token:
21800 attribute-namespace :: identifier
21802 attribute-namespace:
21803 identifier
21805 attribute-argument-clause:
21806 ( balanced-token-seq )
21808 balanced-token-seq:
21809 balanced-token [opt]
21810 balanced-token-seq balanced-token
21812 balanced-token:
21813 ( balanced-token-seq )
21814 [ balanced-token-seq ]
21815 { balanced-token-seq }. */
21817 static tree
21818 cp_parser_std_attribute (cp_parser *parser)
21820 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
21821 cp_token *token;
21823 /* First, parse name of the the attribute, a.k.a
21824 attribute-token. */
21826 token = cp_lexer_peek_token (parser->lexer);
21827 if (token->type == CPP_NAME)
21828 attr_id = token->u.value;
21829 else if (token->type == CPP_KEYWORD)
21830 attr_id = ridpointers[(int) token->keyword];
21831 else if (token->flags & NAMED_OP)
21832 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
21834 if (attr_id == NULL_TREE)
21835 return NULL_TREE;
21837 cp_lexer_consume_token (parser->lexer);
21839 token = cp_lexer_peek_token (parser->lexer);
21840 if (token->type == CPP_SCOPE)
21842 /* We are seeing a scoped attribute token. */
21844 cp_lexer_consume_token (parser->lexer);
21845 attr_ns = attr_id;
21847 token = cp_lexer_consume_token (parser->lexer);
21848 if (token->type == CPP_NAME)
21849 attr_id = token->u.value;
21850 else if (token->type == CPP_KEYWORD)
21851 attr_id = ridpointers[(int) token->keyword];
21852 else
21854 error_at (token->location,
21855 "expected an identifier for the attribute name");
21856 return error_mark_node;
21858 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
21859 NULL_TREE);
21860 token = cp_lexer_peek_token (parser->lexer);
21862 else
21864 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
21865 NULL_TREE);
21866 /* C++11 noreturn attribute is equivalent to GNU's. */
21867 if (is_attribute_p ("noreturn", attr_id))
21868 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21869 /* C++14 deprecated attribute is equivalent to GNU's. */
21870 else if (cxx_dialect >= cxx1y && is_attribute_p ("deprecated", attr_id))
21871 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
21874 /* Now parse the optional argument clause of the attribute. */
21876 if (token->type != CPP_OPEN_PAREN)
21877 return attribute;
21880 vec<tree, va_gc> *vec;
21881 int attr_flag = normal_attr;
21883 if (attr_ns == get_identifier ("gnu")
21884 && attribute_takes_identifier_p (attr_id))
21885 /* A GNU attribute that takes an identifier in parameter. */
21886 attr_flag = id_attr;
21888 vec = cp_parser_parenthesized_expression_list
21889 (parser, attr_flag, /*cast_p=*/false,
21890 /*allow_expansion_p=*/true,
21891 /*non_constant_p=*/NULL);
21892 if (vec == NULL)
21893 arguments = error_mark_node;
21894 else
21896 arguments = build_tree_list_vec (vec);
21897 release_tree_vector (vec);
21900 if (arguments == error_mark_node)
21901 attribute = error_mark_node;
21902 else
21903 TREE_VALUE (attribute) = arguments;
21906 return attribute;
21909 /* Parse a list of standard C++-11 attributes.
21911 attribute-list:
21912 attribute [opt]
21913 attribute-list , attribute[opt]
21914 attribute ...
21915 attribute-list , attribute ...
21918 static tree
21919 cp_parser_std_attribute_list (cp_parser *parser)
21921 tree attributes = NULL_TREE, attribute = NULL_TREE;
21922 cp_token *token = NULL;
21924 while (true)
21926 attribute = cp_parser_std_attribute (parser);
21927 if (attribute == error_mark_node)
21928 break;
21929 if (attribute != NULL_TREE)
21931 TREE_CHAIN (attribute) = attributes;
21932 attributes = attribute;
21934 token = cp_lexer_peek_token (parser->lexer);
21935 if (token->type != CPP_COMMA)
21936 break;
21937 cp_lexer_consume_token (parser->lexer);
21939 attributes = nreverse (attributes);
21940 return attributes;
21943 /* Parse a standard C++-11 attribute specifier.
21945 attribute-specifier:
21946 [ [ attribute-list ] ]
21947 alignment-specifier
21949 alignment-specifier:
21950 alignas ( type-id ... [opt] )
21951 alignas ( alignment-expression ... [opt] ). */
21953 static tree
21954 cp_parser_std_attribute_spec (cp_parser *parser)
21956 tree attributes = NULL_TREE;
21957 cp_token *token = cp_lexer_peek_token (parser->lexer);
21959 if (token->type == CPP_OPEN_SQUARE
21960 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
21962 cp_lexer_consume_token (parser->lexer);
21963 cp_lexer_consume_token (parser->lexer);
21965 attributes = cp_parser_std_attribute_list (parser);
21967 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
21968 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
21969 cp_parser_skip_to_end_of_statement (parser);
21970 else
21971 /* Warn about parsing c++11 attribute in non-c++1 mode, only
21972 when we are sure that we have actually parsed them. */
21973 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21975 else
21977 tree alignas_expr;
21979 /* Look for an alignment-specifier. */
21981 token = cp_lexer_peek_token (parser->lexer);
21983 if (token->type != CPP_KEYWORD
21984 || token->keyword != RID_ALIGNAS)
21985 return NULL_TREE;
21987 cp_lexer_consume_token (parser->lexer);
21988 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
21990 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
21992 cp_parser_error (parser, "expected %<(%>");
21993 return error_mark_node;
21996 cp_parser_parse_tentatively (parser);
21997 alignas_expr = cp_parser_type_id (parser);
21999 if (!cp_parser_parse_definitely (parser))
22001 gcc_assert (alignas_expr == error_mark_node
22002 || alignas_expr == NULL_TREE);
22004 alignas_expr =
22005 cp_parser_assignment_expression (parser, /*cast_p=*/false,
22006 /**cp_id_kind=*/NULL);
22007 if (alignas_expr == error_mark_node)
22008 cp_parser_skip_to_end_of_statement (parser);
22009 if (alignas_expr == NULL_TREE
22010 || alignas_expr == error_mark_node)
22011 return alignas_expr;
22014 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22016 cp_parser_error (parser, "expected %<)%>");
22017 return error_mark_node;
22020 alignas_expr = cxx_alignas_expr (alignas_expr);
22022 /* Build the C++-11 representation of an 'aligned'
22023 attribute. */
22024 attributes =
22025 build_tree_list (build_tree_list (get_identifier ("gnu"),
22026 get_identifier ("aligned")),
22027 build_tree_list (NULL_TREE, alignas_expr));
22030 return attributes;
22033 /* Parse a standard C++-11 attribute-specifier-seq.
22035 attribute-specifier-seq:
22036 attribute-specifier-seq [opt] attribute-specifier
22039 static tree
22040 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22042 tree attr_specs = NULL;
22044 while (true)
22046 tree attr_spec = cp_parser_std_attribute_spec (parser);
22047 if (attr_spec == NULL_TREE)
22048 break;
22049 if (attr_spec == error_mark_node)
22050 return error_mark_node;
22052 TREE_CHAIN (attr_spec) = attr_specs;
22053 attr_specs = attr_spec;
22056 attr_specs = nreverse (attr_specs);
22057 return attr_specs;
22060 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22061 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22062 current value of the PEDANTIC flag, regardless of whether or not
22063 the `__extension__' keyword is present. The caller is responsible
22064 for restoring the value of the PEDANTIC flag. */
22066 static bool
22067 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22069 /* Save the old value of the PEDANTIC flag. */
22070 *saved_pedantic = pedantic;
22072 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22074 /* Consume the `__extension__' token. */
22075 cp_lexer_consume_token (parser->lexer);
22076 /* We're not being pedantic while the `__extension__' keyword is
22077 in effect. */
22078 pedantic = 0;
22080 return true;
22083 return false;
22086 /* Parse a label declaration.
22088 label-declaration:
22089 __label__ label-declarator-seq ;
22091 label-declarator-seq:
22092 identifier , label-declarator-seq
22093 identifier */
22095 static void
22096 cp_parser_label_declaration (cp_parser* parser)
22098 /* Look for the `__label__' keyword. */
22099 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22101 while (true)
22103 tree identifier;
22105 /* Look for an identifier. */
22106 identifier = cp_parser_identifier (parser);
22107 /* If we failed, stop. */
22108 if (identifier == error_mark_node)
22109 break;
22110 /* Declare it as a label. */
22111 finish_label_decl (identifier);
22112 /* If the next token is a `;', stop. */
22113 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22114 break;
22115 /* Look for the `,' separating the label declarations. */
22116 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22119 /* Look for the final `;'. */
22120 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22123 /* Support Functions */
22125 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22126 NAME should have one of the representations used for an
22127 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22128 is returned. If PARSER->SCOPE is a dependent type, then a
22129 SCOPE_REF is returned.
22131 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22132 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22133 was formed. Abstractly, such entities should not be passed to this
22134 function, because they do not need to be looked up, but it is
22135 simpler to check for this special case here, rather than at the
22136 call-sites.
22138 In cases not explicitly covered above, this function returns a
22139 DECL, OVERLOAD, or baselink representing the result of the lookup.
22140 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22141 is returned.
22143 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22144 (e.g., "struct") that was used. In that case bindings that do not
22145 refer to types are ignored.
22147 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22148 ignored.
22150 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22151 are ignored.
22153 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22154 types.
22156 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22157 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22158 NULL_TREE otherwise. */
22160 static tree
22161 cp_parser_lookup_name (cp_parser *parser, tree name,
22162 enum tag_types tag_type,
22163 bool is_template,
22164 bool is_namespace,
22165 bool check_dependency,
22166 tree *ambiguous_decls,
22167 location_t name_location)
22169 tree decl;
22170 tree object_type = parser->context->object_type;
22172 /* Assume that the lookup will be unambiguous. */
22173 if (ambiguous_decls)
22174 *ambiguous_decls = NULL_TREE;
22176 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22177 no longer valid. Note that if we are parsing tentatively, and
22178 the parse fails, OBJECT_TYPE will be automatically restored. */
22179 parser->context->object_type = NULL_TREE;
22181 if (name == error_mark_node)
22182 return error_mark_node;
22184 /* A template-id has already been resolved; there is no lookup to
22185 do. */
22186 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22187 return name;
22188 if (BASELINK_P (name))
22190 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22191 == TEMPLATE_ID_EXPR);
22192 return name;
22195 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22196 it should already have been checked to make sure that the name
22197 used matches the type being destroyed. */
22198 if (TREE_CODE (name) == BIT_NOT_EXPR)
22200 tree type;
22202 /* Figure out to which type this destructor applies. */
22203 if (parser->scope)
22204 type = parser->scope;
22205 else if (object_type)
22206 type = object_type;
22207 else
22208 type = current_class_type;
22209 /* If that's not a class type, there is no destructor. */
22210 if (!type || !CLASS_TYPE_P (type))
22211 return error_mark_node;
22212 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22213 lazily_declare_fn (sfk_destructor, type);
22214 if (!CLASSTYPE_DESTRUCTORS (type))
22215 return error_mark_node;
22216 /* If it was a class type, return the destructor. */
22217 return CLASSTYPE_DESTRUCTORS (type);
22220 /* By this point, the NAME should be an ordinary identifier. If
22221 the id-expression was a qualified name, the qualifying scope is
22222 stored in PARSER->SCOPE at this point. */
22223 gcc_assert (identifier_p (name));
22225 /* Perform the lookup. */
22226 if (parser->scope)
22228 bool dependent_p;
22230 if (parser->scope == error_mark_node)
22231 return error_mark_node;
22233 /* If the SCOPE is dependent, the lookup must be deferred until
22234 the template is instantiated -- unless we are explicitly
22235 looking up names in uninstantiated templates. Even then, we
22236 cannot look up the name if the scope is not a class type; it
22237 might, for example, be a template type parameter. */
22238 dependent_p = (TYPE_P (parser->scope)
22239 && dependent_scope_p (parser->scope));
22240 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22241 && dependent_p)
22242 /* Defer lookup. */
22243 decl = error_mark_node;
22244 else
22246 tree pushed_scope = NULL_TREE;
22248 /* If PARSER->SCOPE is a dependent type, then it must be a
22249 class type, and we must not be checking dependencies;
22250 otherwise, we would have processed this lookup above. So
22251 that PARSER->SCOPE is not considered a dependent base by
22252 lookup_member, we must enter the scope here. */
22253 if (dependent_p)
22254 pushed_scope = push_scope (parser->scope);
22256 /* If the PARSER->SCOPE is a template specialization, it
22257 may be instantiated during name lookup. In that case,
22258 errors may be issued. Even if we rollback the current
22259 tentative parse, those errors are valid. */
22260 decl = lookup_qualified_name (parser->scope, name,
22261 tag_type != none_type,
22262 /*complain=*/true);
22264 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22265 lookup result and the nested-name-specifier nominates a class C:
22266 * if the name specified after the nested-name-specifier, when
22267 looked up in C, is the injected-class-name of C (Clause 9), or
22268 * if the name specified after the nested-name-specifier is the
22269 same as the identifier or the simple-template-id's template-
22270 name in the last component of the nested-name-specifier,
22271 the name is instead considered to name the constructor of
22272 class C. [ Note: for example, the constructor is not an
22273 acceptable lookup result in an elaborated-type-specifier so
22274 the constructor would not be used in place of the
22275 injected-class-name. --end note ] Such a constructor name
22276 shall be used only in the declarator-id of a declaration that
22277 names a constructor or in a using-declaration. */
22278 if (tag_type == none_type
22279 && DECL_SELF_REFERENCE_P (decl)
22280 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22281 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22282 tag_type != none_type,
22283 /*complain=*/true);
22285 /* If we have a single function from a using decl, pull it out. */
22286 if (TREE_CODE (decl) == OVERLOAD
22287 && !really_overloaded_fn (decl))
22288 decl = OVL_FUNCTION (decl);
22290 if (pushed_scope)
22291 pop_scope (pushed_scope);
22294 /* If the scope is a dependent type and either we deferred lookup or
22295 we did lookup but didn't find the name, rememeber the name. */
22296 if (decl == error_mark_node && TYPE_P (parser->scope)
22297 && dependent_type_p (parser->scope))
22299 if (tag_type)
22301 tree type;
22303 /* The resolution to Core Issue 180 says that `struct
22304 A::B' should be considered a type-name, even if `A'
22305 is dependent. */
22306 type = make_typename_type (parser->scope, name, tag_type,
22307 /*complain=*/tf_error);
22308 if (type != error_mark_node)
22309 decl = TYPE_NAME (type);
22311 else if (is_template
22312 && (cp_parser_next_token_ends_template_argument_p (parser)
22313 || cp_lexer_next_token_is (parser->lexer,
22314 CPP_CLOSE_PAREN)))
22315 decl = make_unbound_class_template (parser->scope,
22316 name, NULL_TREE,
22317 /*complain=*/tf_error);
22318 else
22319 decl = build_qualified_name (/*type=*/NULL_TREE,
22320 parser->scope, name,
22321 is_template);
22323 parser->qualifying_scope = parser->scope;
22324 parser->object_scope = NULL_TREE;
22326 else if (object_type)
22328 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22329 OBJECT_TYPE is not a class. */
22330 if (CLASS_TYPE_P (object_type))
22331 /* If the OBJECT_TYPE is a template specialization, it may
22332 be instantiated during name lookup. In that case, errors
22333 may be issued. Even if we rollback the current tentative
22334 parse, those errors are valid. */
22335 decl = lookup_member (object_type,
22336 name,
22337 /*protect=*/0,
22338 tag_type != none_type,
22339 tf_warning_or_error);
22340 else
22341 decl = NULL_TREE;
22343 if (!decl)
22344 /* Look it up in the enclosing context. */
22345 decl = lookup_name_real (name, tag_type != none_type,
22346 /*nonclass=*/0,
22347 /*block_p=*/true, is_namespace, 0);
22348 parser->object_scope = object_type;
22349 parser->qualifying_scope = NULL_TREE;
22351 else
22353 decl = lookup_name_real (name, tag_type != none_type,
22354 /*nonclass=*/0,
22355 /*block_p=*/true, is_namespace, 0);
22356 parser->qualifying_scope = NULL_TREE;
22357 parser->object_scope = NULL_TREE;
22360 /* If the lookup failed, let our caller know. */
22361 if (!decl || decl == error_mark_node)
22362 return error_mark_node;
22364 /* Pull out the template from an injected-class-name (or multiple). */
22365 if (is_template)
22366 decl = maybe_get_template_decl_from_type_decl (decl);
22368 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22369 if (TREE_CODE (decl) == TREE_LIST)
22371 if (ambiguous_decls)
22372 *ambiguous_decls = decl;
22373 /* The error message we have to print is too complicated for
22374 cp_parser_error, so we incorporate its actions directly. */
22375 if (!cp_parser_simulate_error (parser))
22377 error_at (name_location, "reference to %qD is ambiguous",
22378 name);
22379 print_candidates (decl);
22381 return error_mark_node;
22384 gcc_assert (DECL_P (decl)
22385 || TREE_CODE (decl) == OVERLOAD
22386 || TREE_CODE (decl) == SCOPE_REF
22387 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22388 || BASELINK_P (decl));
22390 /* If we have resolved the name of a member declaration, check to
22391 see if the declaration is accessible. When the name resolves to
22392 set of overloaded functions, accessibility is checked when
22393 overload resolution is done.
22395 During an explicit instantiation, access is not checked at all,
22396 as per [temp.explicit]. */
22397 if (DECL_P (decl))
22398 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22400 maybe_record_typedef_use (decl);
22402 return decl;
22405 /* Like cp_parser_lookup_name, but for use in the typical case where
22406 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22407 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22409 static tree
22410 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22412 return cp_parser_lookup_name (parser, name,
22413 none_type,
22414 /*is_template=*/false,
22415 /*is_namespace=*/false,
22416 /*check_dependency=*/true,
22417 /*ambiguous_decls=*/NULL,
22418 location);
22421 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22422 the current context, return the TYPE_DECL. If TAG_NAME_P is
22423 true, the DECL indicates the class being defined in a class-head,
22424 or declared in an elaborated-type-specifier.
22426 Otherwise, return DECL. */
22428 static tree
22429 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22431 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22432 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22434 struct A {
22435 template <typename T> struct B;
22438 template <typename T> struct A::B {};
22440 Similarly, in an elaborated-type-specifier:
22442 namespace N { struct X{}; }
22444 struct A {
22445 template <typename T> friend struct N::X;
22448 However, if the DECL refers to a class type, and we are in
22449 the scope of the class, then the name lookup automatically
22450 finds the TYPE_DECL created by build_self_reference rather
22451 than a TEMPLATE_DECL. For example, in:
22453 template <class T> struct S {
22454 S s;
22457 there is no need to handle such case. */
22459 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22460 return DECL_TEMPLATE_RESULT (decl);
22462 return decl;
22465 /* If too many, or too few, template-parameter lists apply to the
22466 declarator, issue an error message. Returns TRUE if all went well,
22467 and FALSE otherwise. */
22469 static bool
22470 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22471 cp_declarator *declarator,
22472 location_t declarator_location)
22474 switch (declarator->kind)
22476 case cdk_id:
22478 unsigned num_templates = 0;
22479 tree scope = declarator->u.id.qualifying_scope;
22481 if (scope)
22482 num_templates = num_template_headers_for_class (scope);
22483 else if (TREE_CODE (declarator->u.id.unqualified_name)
22484 == TEMPLATE_ID_EXPR)
22485 /* If the DECLARATOR has the form `X<y>' then it uses one
22486 additional level of template parameters. */
22487 ++num_templates;
22489 return cp_parser_check_template_parameters
22490 (parser, num_templates, declarator_location, declarator);
22493 case cdk_function:
22494 case cdk_array:
22495 case cdk_pointer:
22496 case cdk_reference:
22497 case cdk_ptrmem:
22498 return (cp_parser_check_declarator_template_parameters
22499 (parser, declarator->declarator, declarator_location));
22501 case cdk_error:
22502 return true;
22504 default:
22505 gcc_unreachable ();
22507 return false;
22510 /* NUM_TEMPLATES were used in the current declaration. If that is
22511 invalid, return FALSE and issue an error messages. Otherwise,
22512 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22513 declarator and we can print more accurate diagnostics. */
22515 static bool
22516 cp_parser_check_template_parameters (cp_parser* parser,
22517 unsigned num_templates,
22518 location_t location,
22519 cp_declarator *declarator)
22521 /* If there are the same number of template classes and parameter
22522 lists, that's OK. */
22523 if (parser->num_template_parameter_lists == num_templates)
22524 return true;
22525 /* If there are more, but only one more, then we are referring to a
22526 member template. That's OK too. */
22527 if (parser->num_template_parameter_lists == num_templates + 1)
22528 return true;
22529 /* If there are more template classes than parameter lists, we have
22530 something like:
22532 template <class T> void S<T>::R<T>::f (); */
22533 if (parser->num_template_parameter_lists < num_templates)
22535 if (declarator && !current_function_decl)
22536 error_at (location, "specializing member %<%T::%E%> "
22537 "requires %<template<>%> syntax",
22538 declarator->u.id.qualifying_scope,
22539 declarator->u.id.unqualified_name);
22540 else if (declarator)
22541 error_at (location, "invalid declaration of %<%T::%E%>",
22542 declarator->u.id.qualifying_scope,
22543 declarator->u.id.unqualified_name);
22544 else
22545 error_at (location, "too few template-parameter-lists");
22546 return false;
22548 /* Otherwise, there are too many template parameter lists. We have
22549 something like:
22551 template <class T> template <class U> void S::f(); */
22552 error_at (location, "too many template-parameter-lists");
22553 return false;
22556 /* Parse an optional `::' token indicating that the following name is
22557 from the global namespace. If so, PARSER->SCOPE is set to the
22558 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22559 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22560 Returns the new value of PARSER->SCOPE, if the `::' token is
22561 present, and NULL_TREE otherwise. */
22563 static tree
22564 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22566 cp_token *token;
22568 /* Peek at the next token. */
22569 token = cp_lexer_peek_token (parser->lexer);
22570 /* If we're looking at a `::' token then we're starting from the
22571 global namespace, not our current location. */
22572 if (token->type == CPP_SCOPE)
22574 /* Consume the `::' token. */
22575 cp_lexer_consume_token (parser->lexer);
22576 /* Set the SCOPE so that we know where to start the lookup. */
22577 parser->scope = global_namespace;
22578 parser->qualifying_scope = global_namespace;
22579 parser->object_scope = NULL_TREE;
22581 return parser->scope;
22583 else if (!current_scope_valid_p)
22585 parser->scope = NULL_TREE;
22586 parser->qualifying_scope = NULL_TREE;
22587 parser->object_scope = NULL_TREE;
22590 return NULL_TREE;
22593 /* Returns TRUE if the upcoming token sequence is the start of a
22594 constructor declarator. If FRIEND_P is true, the declarator is
22595 preceded by the `friend' specifier. */
22597 static bool
22598 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22600 bool constructor_p;
22601 bool outside_class_specifier_p;
22602 tree nested_name_specifier;
22603 cp_token *next_token;
22605 /* The common case is that this is not a constructor declarator, so
22606 try to avoid doing lots of work if at all possible. It's not
22607 valid declare a constructor at function scope. */
22608 if (parser->in_function_body)
22609 return false;
22610 /* And only certain tokens can begin a constructor declarator. */
22611 next_token = cp_lexer_peek_token (parser->lexer);
22612 if (next_token->type != CPP_NAME
22613 && next_token->type != CPP_SCOPE
22614 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22615 && next_token->type != CPP_TEMPLATE_ID)
22616 return false;
22618 /* Parse tentatively; we are going to roll back all of the tokens
22619 consumed here. */
22620 cp_parser_parse_tentatively (parser);
22621 /* Assume that we are looking at a constructor declarator. */
22622 constructor_p = true;
22624 /* Look for the optional `::' operator. */
22625 cp_parser_global_scope_opt (parser,
22626 /*current_scope_valid_p=*/false);
22627 /* Look for the nested-name-specifier. */
22628 nested_name_specifier
22629 = (cp_parser_nested_name_specifier_opt (parser,
22630 /*typename_keyword_p=*/false,
22631 /*check_dependency_p=*/false,
22632 /*type_p=*/false,
22633 /*is_declaration=*/false));
22635 outside_class_specifier_p = (!at_class_scope_p ()
22636 || !TYPE_BEING_DEFINED (current_class_type)
22637 || friend_p);
22639 /* Outside of a class-specifier, there must be a
22640 nested-name-specifier. */
22641 if (!nested_name_specifier && outside_class_specifier_p)
22642 constructor_p = false;
22643 else if (nested_name_specifier == error_mark_node)
22644 constructor_p = false;
22646 /* If we have a class scope, this is easy; DR 147 says that S::S always
22647 names the constructor, and no other qualified name could. */
22648 if (constructor_p && nested_name_specifier
22649 && CLASS_TYPE_P (nested_name_specifier))
22651 tree id = cp_parser_unqualified_id (parser,
22652 /*template_keyword_p=*/false,
22653 /*check_dependency_p=*/false,
22654 /*declarator_p=*/true,
22655 /*optional_p=*/false);
22656 if (is_overloaded_fn (id))
22657 id = DECL_NAME (get_first_fn (id));
22658 if (!constructor_name_p (id, nested_name_specifier))
22659 constructor_p = false;
22661 /* If we still think that this might be a constructor-declarator,
22662 look for a class-name. */
22663 else if (constructor_p)
22665 /* If we have:
22667 template <typename T> struct S {
22668 S();
22671 we must recognize that the nested `S' names a class. */
22672 tree type_decl;
22673 type_decl = cp_parser_class_name (parser,
22674 /*typename_keyword_p=*/false,
22675 /*template_keyword_p=*/false,
22676 none_type,
22677 /*check_dependency_p=*/false,
22678 /*class_head_p=*/false,
22679 /*is_declaration=*/false);
22680 /* If there was no class-name, then this is not a constructor.
22681 Otherwise, if we are in a class-specifier and we aren't
22682 handling a friend declaration, check that its type matches
22683 current_class_type (c++/38313). Note: error_mark_node
22684 is left alone for error recovery purposes. */
22685 constructor_p = (!cp_parser_error_occurred (parser)
22686 && (outside_class_specifier_p
22687 || type_decl == error_mark_node
22688 || same_type_p (current_class_type,
22689 TREE_TYPE (type_decl))));
22691 /* If we're still considering a constructor, we have to see a `(',
22692 to begin the parameter-declaration-clause, followed by either a
22693 `)', an `...', or a decl-specifier. We need to check for a
22694 type-specifier to avoid being fooled into thinking that:
22696 S (f) (int);
22698 is a constructor. (It is actually a function named `f' that
22699 takes one parameter (of type `int') and returns a value of type
22700 `S'. */
22701 if (constructor_p
22702 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22703 constructor_p = false;
22705 if (constructor_p
22706 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
22707 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
22708 /* A parameter declaration begins with a decl-specifier,
22709 which is either the "attribute" keyword, a storage class
22710 specifier, or (usually) a type-specifier. */
22711 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
22713 tree type;
22714 tree pushed_scope = NULL_TREE;
22715 unsigned saved_num_template_parameter_lists;
22717 /* Names appearing in the type-specifier should be looked up
22718 in the scope of the class. */
22719 if (current_class_type)
22720 type = NULL_TREE;
22721 else
22723 type = TREE_TYPE (type_decl);
22724 if (TREE_CODE (type) == TYPENAME_TYPE)
22726 type = resolve_typename_type (type,
22727 /*only_current_p=*/false);
22728 if (TREE_CODE (type) == TYPENAME_TYPE)
22730 cp_parser_abort_tentative_parse (parser);
22731 return false;
22734 pushed_scope = push_scope (type);
22737 /* Inside the constructor parameter list, surrounding
22738 template-parameter-lists do not apply. */
22739 saved_num_template_parameter_lists
22740 = parser->num_template_parameter_lists;
22741 parser->num_template_parameter_lists = 0;
22743 /* Look for the type-specifier. */
22744 cp_parser_type_specifier (parser,
22745 CP_PARSER_FLAGS_NONE,
22746 /*decl_specs=*/NULL,
22747 /*is_declarator=*/true,
22748 /*declares_class_or_enum=*/NULL,
22749 /*is_cv_qualifier=*/NULL);
22751 parser->num_template_parameter_lists
22752 = saved_num_template_parameter_lists;
22754 /* Leave the scope of the class. */
22755 if (pushed_scope)
22756 pop_scope (pushed_scope);
22758 constructor_p = !cp_parser_error_occurred (parser);
22762 /* We did not really want to consume any tokens. */
22763 cp_parser_abort_tentative_parse (parser);
22765 return constructor_p;
22768 /* Parse the definition of the function given by the DECL_SPECIFIERS,
22769 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
22770 they must be performed once we are in the scope of the function.
22772 Returns the function defined. */
22774 static tree
22775 cp_parser_function_definition_from_specifiers_and_declarator
22776 (cp_parser* parser,
22777 cp_decl_specifier_seq *decl_specifiers,
22778 tree attributes,
22779 const cp_declarator *declarator)
22781 tree fn;
22782 bool success_p;
22784 /* Begin the function-definition. */
22785 success_p = start_function (decl_specifiers, declarator, attributes);
22787 /* The things we're about to see are not directly qualified by any
22788 template headers we've seen thus far. */
22789 reset_specialization ();
22791 /* If there were names looked up in the decl-specifier-seq that we
22792 did not check, check them now. We must wait until we are in the
22793 scope of the function to perform the checks, since the function
22794 might be a friend. */
22795 perform_deferred_access_checks (tf_warning_or_error);
22797 if (success_p)
22799 cp_finalize_omp_declare_simd (parser, current_function_decl);
22800 parser->omp_declare_simd = NULL;
22803 if (!success_p)
22805 /* Skip the entire function. */
22806 cp_parser_skip_to_end_of_block_or_statement (parser);
22807 fn = error_mark_node;
22809 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
22811 /* Seen already, skip it. An error message has already been output. */
22812 cp_parser_skip_to_end_of_block_or_statement (parser);
22813 fn = current_function_decl;
22814 current_function_decl = NULL_TREE;
22815 /* If this is a function from a class, pop the nested class. */
22816 if (current_class_name)
22817 pop_nested_class ();
22819 else
22821 timevar_id_t tv;
22822 if (DECL_DECLARED_INLINE_P (current_function_decl))
22823 tv = TV_PARSE_INLINE;
22824 else
22825 tv = TV_PARSE_FUNC;
22826 timevar_push (tv);
22827 fn = cp_parser_function_definition_after_declarator (parser,
22828 /*inline_p=*/false);
22829 timevar_pop (tv);
22832 return fn;
22835 /* Parse the part of a function-definition that follows the
22836 declarator. INLINE_P is TRUE iff this function is an inline
22837 function defined within a class-specifier.
22839 Returns the function defined. */
22841 static tree
22842 cp_parser_function_definition_after_declarator (cp_parser* parser,
22843 bool inline_p)
22845 tree fn;
22846 bool ctor_initializer_p = false;
22847 bool saved_in_unbraced_linkage_specification_p;
22848 bool saved_in_function_body;
22849 unsigned saved_num_template_parameter_lists;
22850 cp_token *token;
22851 bool fully_implicit_function_template_p
22852 = parser->fully_implicit_function_template_p;
22853 parser->fully_implicit_function_template_p = false;
22854 tree implicit_template_parms
22855 = parser->implicit_template_parms;
22856 parser->implicit_template_parms = 0;
22857 cp_binding_level* implicit_template_scope
22858 = parser->implicit_template_scope;
22859 parser->implicit_template_scope = 0;
22861 saved_in_function_body = parser->in_function_body;
22862 parser->in_function_body = true;
22863 /* If the next token is `return', then the code may be trying to
22864 make use of the "named return value" extension that G++ used to
22865 support. */
22866 token = cp_lexer_peek_token (parser->lexer);
22867 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
22869 /* Consume the `return' keyword. */
22870 cp_lexer_consume_token (parser->lexer);
22871 /* Look for the identifier that indicates what value is to be
22872 returned. */
22873 cp_parser_identifier (parser);
22874 /* Issue an error message. */
22875 error_at (token->location,
22876 "named return values are no longer supported");
22877 /* Skip tokens until we reach the start of the function body. */
22878 while (true)
22880 cp_token *token = cp_lexer_peek_token (parser->lexer);
22881 if (token->type == CPP_OPEN_BRACE
22882 || token->type == CPP_EOF
22883 || token->type == CPP_PRAGMA_EOL)
22884 break;
22885 cp_lexer_consume_token (parser->lexer);
22888 /* The `extern' in `extern "C" void f () { ... }' does not apply to
22889 anything declared inside `f'. */
22890 saved_in_unbraced_linkage_specification_p
22891 = parser->in_unbraced_linkage_specification_p;
22892 parser->in_unbraced_linkage_specification_p = false;
22893 /* Inside the function, surrounding template-parameter-lists do not
22894 apply. */
22895 saved_num_template_parameter_lists
22896 = parser->num_template_parameter_lists;
22897 parser->num_template_parameter_lists = 0;
22899 start_lambda_scope (current_function_decl);
22901 /* If the next token is `try', `__transaction_atomic', or
22902 `__transaction_relaxed`, then we are looking at either function-try-block
22903 or function-transaction-block. Note that all of these include the
22904 function-body. */
22905 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
22906 ctor_initializer_p = cp_parser_function_transaction (parser,
22907 RID_TRANSACTION_ATOMIC);
22908 else if (cp_lexer_next_token_is_keyword (parser->lexer,
22909 RID_TRANSACTION_RELAXED))
22910 ctor_initializer_p = cp_parser_function_transaction (parser,
22911 RID_TRANSACTION_RELAXED);
22912 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
22913 ctor_initializer_p = cp_parser_function_try_block (parser);
22914 else
22915 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
22916 (parser, /*in_function_try_block=*/false);
22918 finish_lambda_scope ();
22920 /* Finish the function. */
22921 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
22922 (inline_p ? 2 : 0));
22923 /* Generate code for it, if necessary. */
22924 expand_or_defer_fn (fn);
22925 /* Restore the saved values. */
22926 parser->in_unbraced_linkage_specification_p
22927 = saved_in_unbraced_linkage_specification_p;
22928 parser->num_template_parameter_lists
22929 = saved_num_template_parameter_lists;
22930 parser->in_function_body = saved_in_function_body;
22932 parser->fully_implicit_function_template_p
22933 = fully_implicit_function_template_p;
22934 parser->implicit_template_parms
22935 = implicit_template_parms;
22936 parser->implicit_template_scope
22937 = implicit_template_scope;
22939 if (parser->fully_implicit_function_template_p)
22940 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22942 return fn;
22945 /* Parse a template-declaration, assuming that the `export' (and
22946 `extern') keywords, if present, has already been scanned. MEMBER_P
22947 is as for cp_parser_template_declaration. */
22949 static void
22950 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
22952 tree decl = NULL_TREE;
22953 vec<deferred_access_check, va_gc> *checks;
22954 tree parameter_list;
22955 bool friend_p = false;
22956 bool need_lang_pop;
22957 cp_token *token;
22959 /* Look for the `template' keyword. */
22960 token = cp_lexer_peek_token (parser->lexer);
22961 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
22962 return;
22964 /* And the `<'. */
22965 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
22966 return;
22967 if (at_class_scope_p () && current_function_decl)
22969 /* 14.5.2.2 [temp.mem]
22971 A local class shall not have member templates. */
22972 error_at (token->location,
22973 "invalid declaration of member template in local class");
22974 cp_parser_skip_to_end_of_block_or_statement (parser);
22975 return;
22977 /* [temp]
22979 A template ... shall not have C linkage. */
22980 if (current_lang_name == lang_name_c)
22982 error_at (token->location, "template with C linkage");
22983 /* Give it C++ linkage to avoid confusing other parts of the
22984 front end. */
22985 push_lang_context (lang_name_cplusplus);
22986 need_lang_pop = true;
22988 else
22989 need_lang_pop = false;
22991 /* We cannot perform access checks on the template parameter
22992 declarations until we know what is being declared, just as we
22993 cannot check the decl-specifier list. */
22994 push_deferring_access_checks (dk_deferred);
22996 /* If the next token is `>', then we have an invalid
22997 specialization. Rather than complain about an invalid template
22998 parameter, issue an error message here. */
22999 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23001 cp_parser_error (parser, "invalid explicit specialization");
23002 begin_specialization ();
23003 parameter_list = NULL_TREE;
23005 else
23007 /* Parse the template parameters. */
23008 parameter_list = cp_parser_template_parameter_list (parser);
23011 /* Get the deferred access checks from the parameter list. These
23012 will be checked once we know what is being declared, as for a
23013 member template the checks must be performed in the scope of the
23014 class containing the member. */
23015 checks = get_deferred_access_checks ();
23017 /* Look for the `>'. */
23018 cp_parser_skip_to_end_of_template_parameter_list (parser);
23019 /* We just processed one more parameter list. */
23020 ++parser->num_template_parameter_lists;
23021 /* If the next token is `template', there are more template
23022 parameters. */
23023 if (cp_lexer_next_token_is_keyword (parser->lexer,
23024 RID_TEMPLATE))
23025 cp_parser_template_declaration_after_export (parser, member_p);
23026 else if (cxx_dialect >= cxx11
23027 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23028 decl = cp_parser_alias_declaration (parser);
23029 else
23031 /* There are no access checks when parsing a template, as we do not
23032 know if a specialization will be a friend. */
23033 push_deferring_access_checks (dk_no_check);
23034 token = cp_lexer_peek_token (parser->lexer);
23035 decl = cp_parser_single_declaration (parser,
23036 checks,
23037 member_p,
23038 /*explicit_specialization_p=*/false,
23039 &friend_p);
23040 pop_deferring_access_checks ();
23042 /* If this is a member template declaration, let the front
23043 end know. */
23044 if (member_p && !friend_p && decl)
23046 if (TREE_CODE (decl) == TYPE_DECL)
23047 cp_parser_check_access_in_redeclaration (decl, token->location);
23049 decl = finish_member_template_decl (decl);
23051 else if (friend_p && decl
23052 && DECL_DECLARES_TYPE_P (decl))
23053 make_friend_class (current_class_type, TREE_TYPE (decl),
23054 /*complain=*/true);
23056 /* We are done with the current parameter list. */
23057 --parser->num_template_parameter_lists;
23059 pop_deferring_access_checks ();
23061 /* Finish up. */
23062 finish_template_decl (parameter_list);
23064 /* Check the template arguments for a literal operator template. */
23065 if (decl
23066 && DECL_DECLARES_FUNCTION_P (decl)
23067 && UDLIT_OPER_P (DECL_NAME (decl)))
23069 bool ok = true;
23070 if (parameter_list == NULL_TREE)
23071 ok = false;
23072 else
23074 int num_parms = TREE_VEC_LENGTH (parameter_list);
23075 if (num_parms == 1)
23077 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23078 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23079 if (TREE_TYPE (parm) != char_type_node
23080 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23081 ok = false;
23083 else if (num_parms == 2 && cxx_dialect >= cxx1y)
23085 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23086 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23087 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23088 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23089 if (TREE_TYPE (parm) != TREE_TYPE (type)
23090 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23091 ok = false;
23093 else
23094 ok = false;
23096 if (!ok)
23097 error ("literal operator template %qD has invalid parameter list."
23098 " Expected non-type template argument pack <char...>"
23099 " or <typename CharT, CharT...>",
23100 decl);
23102 /* Register member declarations. */
23103 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23104 finish_member_declaration (decl);
23105 /* For the erroneous case of a template with C linkage, we pushed an
23106 implicit C++ linkage scope; exit that scope now. */
23107 if (need_lang_pop)
23108 pop_lang_context ();
23109 /* If DECL is a function template, we must return to parse it later.
23110 (Even though there is no definition, there might be default
23111 arguments that need handling.) */
23112 if (member_p && decl
23113 && DECL_DECLARES_FUNCTION_P (decl))
23114 vec_safe_push (unparsed_funs_with_definitions, decl);
23117 /* Perform the deferred access checks from a template-parameter-list.
23118 CHECKS is a TREE_LIST of access checks, as returned by
23119 get_deferred_access_checks. */
23121 static void
23122 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23124 ++processing_template_parmlist;
23125 perform_access_checks (checks, tf_warning_or_error);
23126 --processing_template_parmlist;
23129 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23130 `function-definition' sequence that follows a template header.
23131 If MEMBER_P is true, this declaration appears in a class scope.
23133 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23134 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23136 static tree
23137 cp_parser_single_declaration (cp_parser* parser,
23138 vec<deferred_access_check, va_gc> *checks,
23139 bool member_p,
23140 bool explicit_specialization_p,
23141 bool* friend_p)
23143 int declares_class_or_enum;
23144 tree decl = NULL_TREE;
23145 cp_decl_specifier_seq decl_specifiers;
23146 bool function_definition_p = false;
23147 cp_token *decl_spec_token_start;
23149 /* This function is only used when processing a template
23150 declaration. */
23151 gcc_assert (innermost_scope_kind () == sk_template_parms
23152 || innermost_scope_kind () == sk_template_spec);
23154 /* Defer access checks until we know what is being declared. */
23155 push_deferring_access_checks (dk_deferred);
23157 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23158 alternative. */
23159 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23160 cp_parser_decl_specifier_seq (parser,
23161 CP_PARSER_FLAGS_OPTIONAL,
23162 &decl_specifiers,
23163 &declares_class_or_enum);
23164 if (friend_p)
23165 *friend_p = cp_parser_friend_p (&decl_specifiers);
23167 /* There are no template typedefs. */
23168 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23170 error_at (decl_spec_token_start->location,
23171 "template declaration of %<typedef%>");
23172 decl = error_mark_node;
23175 /* Gather up the access checks that occurred the
23176 decl-specifier-seq. */
23177 stop_deferring_access_checks ();
23179 /* Check for the declaration of a template class. */
23180 if (declares_class_or_enum)
23182 if (cp_parser_declares_only_class_p (parser))
23184 decl = shadow_tag (&decl_specifiers);
23186 /* In this case:
23188 struct C {
23189 friend template <typename T> struct A<T>::B;
23192 A<T>::B will be represented by a TYPENAME_TYPE, and
23193 therefore not recognized by shadow_tag. */
23194 if (friend_p && *friend_p
23195 && !decl
23196 && decl_specifiers.type
23197 && TYPE_P (decl_specifiers.type))
23198 decl = decl_specifiers.type;
23200 if (decl && decl != error_mark_node)
23201 decl = TYPE_NAME (decl);
23202 else
23203 decl = error_mark_node;
23205 /* Perform access checks for template parameters. */
23206 cp_parser_perform_template_parameter_access_checks (checks);
23210 /* Complain about missing 'typename' or other invalid type names. */
23211 if (!decl_specifiers.any_type_specifiers_p
23212 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23214 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23215 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23216 the rest of this declaration. */
23217 decl = error_mark_node;
23218 goto out;
23221 /* If it's not a template class, try for a template function. If
23222 the next token is a `;', then this declaration does not declare
23223 anything. But, if there were errors in the decl-specifiers, then
23224 the error might well have come from an attempted class-specifier.
23225 In that case, there's no need to warn about a missing declarator. */
23226 if (!decl
23227 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23228 || decl_specifiers.type != error_mark_node))
23230 decl = cp_parser_init_declarator (parser,
23231 &decl_specifiers,
23232 checks,
23233 /*function_definition_allowed_p=*/true,
23234 member_p,
23235 declares_class_or_enum,
23236 &function_definition_p,
23237 NULL);
23239 /* 7.1.1-1 [dcl.stc]
23241 A storage-class-specifier shall not be specified in an explicit
23242 specialization... */
23243 if (decl
23244 && explicit_specialization_p
23245 && decl_specifiers.storage_class != sc_none)
23247 error_at (decl_spec_token_start->location,
23248 "explicit template specialization cannot have a storage class");
23249 decl = error_mark_node;
23252 if (decl && VAR_P (decl))
23253 check_template_variable (decl);
23256 /* Look for a trailing `;' after the declaration. */
23257 if (!function_definition_p
23258 && (decl == error_mark_node
23259 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23260 cp_parser_skip_to_end_of_block_or_statement (parser);
23262 out:
23263 pop_deferring_access_checks ();
23265 /* Clear any current qualification; whatever comes next is the start
23266 of something new. */
23267 parser->scope = NULL_TREE;
23268 parser->qualifying_scope = NULL_TREE;
23269 parser->object_scope = NULL_TREE;
23271 return decl;
23274 /* Parse a cast-expression that is not the operand of a unary "&". */
23276 static tree
23277 cp_parser_simple_cast_expression (cp_parser *parser)
23279 return cp_parser_cast_expression (parser, /*address_p=*/false,
23280 /*cast_p=*/false, /*decltype*/false, NULL);
23283 /* Parse a functional cast to TYPE. Returns an expression
23284 representing the cast. */
23286 static tree
23287 cp_parser_functional_cast (cp_parser* parser, tree type)
23289 vec<tree, va_gc> *vec;
23290 tree expression_list;
23291 tree cast;
23292 bool nonconst_p;
23294 if (!type)
23295 type = error_mark_node;
23297 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23299 cp_lexer_set_source_position (parser->lexer);
23300 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23301 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23302 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23303 if (TREE_CODE (type) == TYPE_DECL)
23304 type = TREE_TYPE (type);
23305 return finish_compound_literal (type, expression_list,
23306 tf_warning_or_error);
23310 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23311 /*cast_p=*/true,
23312 /*allow_expansion_p=*/true,
23313 /*non_constant_p=*/NULL);
23314 if (vec == NULL)
23315 expression_list = error_mark_node;
23316 else
23318 expression_list = build_tree_list_vec (vec);
23319 release_tree_vector (vec);
23322 cast = build_functional_cast (type, expression_list,
23323 tf_warning_or_error);
23324 /* [expr.const]/1: In an integral constant expression "only type
23325 conversions to integral or enumeration type can be used". */
23326 if (TREE_CODE (type) == TYPE_DECL)
23327 type = TREE_TYPE (type);
23328 if (cast != error_mark_node
23329 && !cast_valid_in_integral_constant_expression_p (type)
23330 && cp_parser_non_integral_constant_expression (parser,
23331 NIC_CONSTRUCTOR))
23332 return error_mark_node;
23333 return cast;
23336 /* Save the tokens that make up the body of a member function defined
23337 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23338 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23339 specifiers applied to the declaration. Returns the FUNCTION_DECL
23340 for the member function. */
23342 static tree
23343 cp_parser_save_member_function_body (cp_parser* parser,
23344 cp_decl_specifier_seq *decl_specifiers,
23345 cp_declarator *declarator,
23346 tree attributes)
23348 cp_token *first;
23349 cp_token *last;
23350 tree fn;
23352 /* Create the FUNCTION_DECL. */
23353 fn = grokmethod (decl_specifiers, declarator, attributes);
23354 cp_finalize_omp_declare_simd (parser, fn);
23355 /* If something went badly wrong, bail out now. */
23356 if (fn == error_mark_node)
23358 /* If there's a function-body, skip it. */
23359 if (cp_parser_token_starts_function_definition_p
23360 (cp_lexer_peek_token (parser->lexer)))
23361 cp_parser_skip_to_end_of_block_or_statement (parser);
23362 return error_mark_node;
23365 /* Remember it, if there default args to post process. */
23366 cp_parser_save_default_args (parser, fn);
23368 /* Save away the tokens that make up the body of the
23369 function. */
23370 first = parser->lexer->next_token;
23371 /* Handle function try blocks. */
23372 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23373 cp_lexer_consume_token (parser->lexer);
23374 /* We can have braced-init-list mem-initializers before the fn body. */
23375 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23377 cp_lexer_consume_token (parser->lexer);
23378 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23380 /* cache_group will stop after an un-nested { } pair, too. */
23381 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23382 break;
23384 /* variadic mem-inits have ... after the ')'. */
23385 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23386 cp_lexer_consume_token (parser->lexer);
23389 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23390 /* Handle function try blocks. */
23391 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23392 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23393 last = parser->lexer->next_token;
23395 /* Save away the inline definition; we will process it when the
23396 class is complete. */
23397 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23398 DECL_PENDING_INLINE_P (fn) = 1;
23400 /* We need to know that this was defined in the class, so that
23401 friend templates are handled correctly. */
23402 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23404 /* Add FN to the queue of functions to be parsed later. */
23405 vec_safe_push (unparsed_funs_with_definitions, fn);
23407 return fn;
23410 /* Save the tokens that make up the in-class initializer for a non-static
23411 data member. Returns a DEFAULT_ARG. */
23413 static tree
23414 cp_parser_save_nsdmi (cp_parser* parser)
23416 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23419 /* Parse a template-argument-list, as well as the trailing ">" (but
23420 not the opening "<"). See cp_parser_template_argument_list for the
23421 return value. */
23423 static tree
23424 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23426 tree arguments;
23427 tree saved_scope;
23428 tree saved_qualifying_scope;
23429 tree saved_object_scope;
23430 bool saved_greater_than_is_operator_p;
23431 int saved_unevaluated_operand;
23432 int saved_inhibit_evaluation_warnings;
23434 /* [temp.names]
23436 When parsing a template-id, the first non-nested `>' is taken as
23437 the end of the template-argument-list rather than a greater-than
23438 operator. */
23439 saved_greater_than_is_operator_p
23440 = parser->greater_than_is_operator_p;
23441 parser->greater_than_is_operator_p = false;
23442 /* Parsing the argument list may modify SCOPE, so we save it
23443 here. */
23444 saved_scope = parser->scope;
23445 saved_qualifying_scope = parser->qualifying_scope;
23446 saved_object_scope = parser->object_scope;
23447 /* We need to evaluate the template arguments, even though this
23448 template-id may be nested within a "sizeof". */
23449 saved_unevaluated_operand = cp_unevaluated_operand;
23450 cp_unevaluated_operand = 0;
23451 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23452 c_inhibit_evaluation_warnings = 0;
23453 /* Parse the template-argument-list itself. */
23454 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23455 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23456 arguments = NULL_TREE;
23457 else
23458 arguments = cp_parser_template_argument_list (parser);
23459 /* Look for the `>' that ends the template-argument-list. If we find
23460 a '>>' instead, it's probably just a typo. */
23461 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23463 if (cxx_dialect != cxx98)
23465 /* In C++0x, a `>>' in a template argument list or cast
23466 expression is considered to be two separate `>'
23467 tokens. So, change the current token to a `>', but don't
23468 consume it: it will be consumed later when the outer
23469 template argument list (or cast expression) is parsed.
23470 Note that this replacement of `>' for `>>' is necessary
23471 even if we are parsing tentatively: in the tentative
23472 case, after calling
23473 cp_parser_enclosed_template_argument_list we will always
23474 throw away all of the template arguments and the first
23475 closing `>', either because the template argument list
23476 was erroneous or because we are replacing those tokens
23477 with a CPP_TEMPLATE_ID token. The second `>' (which will
23478 not have been thrown away) is needed either to close an
23479 outer template argument list or to complete a new-style
23480 cast. */
23481 cp_token *token = cp_lexer_peek_token (parser->lexer);
23482 token->type = CPP_GREATER;
23484 else if (!saved_greater_than_is_operator_p)
23486 /* If we're in a nested template argument list, the '>>' has
23487 to be a typo for '> >'. We emit the error message, but we
23488 continue parsing and we push a '>' as next token, so that
23489 the argument list will be parsed correctly. Note that the
23490 global source location is still on the token before the
23491 '>>', so we need to say explicitly where we want it. */
23492 cp_token *token = cp_lexer_peek_token (parser->lexer);
23493 error_at (token->location, "%<>>%> should be %<> >%> "
23494 "within a nested template argument list");
23496 token->type = CPP_GREATER;
23498 else
23500 /* If this is not a nested template argument list, the '>>'
23501 is a typo for '>'. Emit an error message and continue.
23502 Same deal about the token location, but here we can get it
23503 right by consuming the '>>' before issuing the diagnostic. */
23504 cp_token *token = cp_lexer_consume_token (parser->lexer);
23505 error_at (token->location,
23506 "spurious %<>>%>, use %<>%> to terminate "
23507 "a template argument list");
23510 else
23511 cp_parser_skip_to_end_of_template_parameter_list (parser);
23512 /* The `>' token might be a greater-than operator again now. */
23513 parser->greater_than_is_operator_p
23514 = saved_greater_than_is_operator_p;
23515 /* Restore the SAVED_SCOPE. */
23516 parser->scope = saved_scope;
23517 parser->qualifying_scope = saved_qualifying_scope;
23518 parser->object_scope = saved_object_scope;
23519 cp_unevaluated_operand = saved_unevaluated_operand;
23520 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23522 return arguments;
23525 /* MEMBER_FUNCTION is a member function, or a friend. If default
23526 arguments, or the body of the function have not yet been parsed,
23527 parse them now. */
23529 static void
23530 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23532 timevar_push (TV_PARSE_INMETH);
23533 /* If this member is a template, get the underlying
23534 FUNCTION_DECL. */
23535 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23536 member_function = DECL_TEMPLATE_RESULT (member_function);
23538 /* There should not be any class definitions in progress at this
23539 point; the bodies of members are only parsed outside of all class
23540 definitions. */
23541 gcc_assert (parser->num_classes_being_defined == 0);
23542 /* While we're parsing the member functions we might encounter more
23543 classes. We want to handle them right away, but we don't want
23544 them getting mixed up with functions that are currently in the
23545 queue. */
23546 push_unparsed_function_queues (parser);
23548 /* Make sure that any template parameters are in scope. */
23549 maybe_begin_member_template_processing (member_function);
23551 /* If the body of the function has not yet been parsed, parse it
23552 now. */
23553 if (DECL_PENDING_INLINE_P (member_function))
23555 tree function_scope;
23556 cp_token_cache *tokens;
23558 /* The function is no longer pending; we are processing it. */
23559 tokens = DECL_PENDING_INLINE_INFO (member_function);
23560 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23561 DECL_PENDING_INLINE_P (member_function) = 0;
23563 /* If this is a local class, enter the scope of the containing
23564 function. */
23565 function_scope = current_function_decl;
23566 if (function_scope)
23567 push_function_context ();
23569 /* Push the body of the function onto the lexer stack. */
23570 cp_parser_push_lexer_for_tokens (parser, tokens);
23572 /* Let the front end know that we going to be defining this
23573 function. */
23574 start_preparsed_function (member_function, NULL_TREE,
23575 SF_PRE_PARSED | SF_INCLASS_INLINE);
23577 /* Don't do access checking if it is a templated function. */
23578 if (processing_template_decl)
23579 push_deferring_access_checks (dk_no_check);
23581 /* #pragma omp declare reduction needs special parsing. */
23582 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23584 parser->lexer->in_pragma = true;
23585 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23586 finish_function (/*inline*/2);
23587 cp_check_omp_declare_reduction (member_function);
23589 else
23590 /* Now, parse the body of the function. */
23591 cp_parser_function_definition_after_declarator (parser,
23592 /*inline_p=*/true);
23594 if (processing_template_decl)
23595 pop_deferring_access_checks ();
23597 /* Leave the scope of the containing function. */
23598 if (function_scope)
23599 pop_function_context ();
23600 cp_parser_pop_lexer (parser);
23603 /* Remove any template parameters from the symbol table. */
23604 maybe_end_member_template_processing ();
23606 /* Restore the queue. */
23607 pop_unparsed_function_queues (parser);
23608 timevar_pop (TV_PARSE_INMETH);
23611 /* If DECL contains any default args, remember it on the unparsed
23612 functions queue. */
23614 static void
23615 cp_parser_save_default_args (cp_parser* parser, tree decl)
23617 tree probe;
23619 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23620 probe;
23621 probe = TREE_CHAIN (probe))
23622 if (TREE_PURPOSE (probe))
23624 cp_default_arg_entry entry = {current_class_type, decl};
23625 vec_safe_push (unparsed_funs_with_default_args, entry);
23626 break;
23630 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23631 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23632 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23633 from the parameter-type-list. */
23635 static tree
23636 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23637 tree default_arg, tree parmtype)
23639 cp_token_cache *tokens;
23640 tree parsed_arg;
23641 bool dummy;
23643 if (default_arg == error_mark_node)
23644 return error_mark_node;
23646 /* Push the saved tokens for the default argument onto the parser's
23647 lexer stack. */
23648 tokens = DEFARG_TOKENS (default_arg);
23649 cp_parser_push_lexer_for_tokens (parser, tokens);
23651 start_lambda_scope (decl);
23653 /* Parse the default argument. */
23654 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
23655 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
23656 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23658 finish_lambda_scope ();
23660 if (parsed_arg == error_mark_node)
23661 cp_parser_skip_to_end_of_statement (parser);
23663 if (!processing_template_decl)
23665 /* In a non-template class, check conversions now. In a template,
23666 we'll wait and instantiate these as needed. */
23667 if (TREE_CODE (decl) == PARM_DECL)
23668 parsed_arg = check_default_argument (parmtype, parsed_arg,
23669 tf_warning_or_error);
23670 else
23672 int flags = LOOKUP_IMPLICIT;
23673 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
23674 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
23675 flags = LOOKUP_NORMAL;
23676 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
23677 if (TREE_CODE (parsed_arg) == TARGET_EXPR)
23678 /* This represents the whole initialization. */
23679 TARGET_EXPR_DIRECT_INIT_P (parsed_arg) = true;
23683 /* If the token stream has not been completely used up, then
23684 there was extra junk after the end of the default
23685 argument. */
23686 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23688 if (TREE_CODE (decl) == PARM_DECL)
23689 cp_parser_error (parser, "expected %<,%>");
23690 else
23691 cp_parser_error (parser, "expected %<;%>");
23694 /* Revert to the main lexer. */
23695 cp_parser_pop_lexer (parser);
23697 return parsed_arg;
23700 /* FIELD is a non-static data member with an initializer which we saved for
23701 later; parse it now. */
23703 static void
23704 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
23706 tree def;
23708 maybe_begin_member_template_processing (field);
23710 push_unparsed_function_queues (parser);
23711 def = cp_parser_late_parse_one_default_arg (parser, field,
23712 DECL_INITIAL (field),
23713 NULL_TREE);
23714 pop_unparsed_function_queues (parser);
23716 maybe_end_member_template_processing ();
23718 DECL_INITIAL (field) = def;
23721 /* FN is a FUNCTION_DECL which may contains a parameter with an
23722 unparsed DEFAULT_ARG. Parse the default args now. This function
23723 assumes that the current scope is the scope in which the default
23724 argument should be processed. */
23726 static void
23727 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
23729 bool saved_local_variables_forbidden_p;
23730 tree parm, parmdecl;
23732 /* While we're parsing the default args, we might (due to the
23733 statement expression extension) encounter more classes. We want
23734 to handle them right away, but we don't want them getting mixed
23735 up with default args that are currently in the queue. */
23736 push_unparsed_function_queues (parser);
23738 /* Local variable names (and the `this' keyword) may not appear
23739 in a default argument. */
23740 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23741 parser->local_variables_forbidden_p = true;
23743 push_defarg_context (fn);
23745 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
23746 parmdecl = DECL_ARGUMENTS (fn);
23747 parm && parm != void_list_node;
23748 parm = TREE_CHAIN (parm),
23749 parmdecl = DECL_CHAIN (parmdecl))
23751 tree default_arg = TREE_PURPOSE (parm);
23752 tree parsed_arg;
23753 vec<tree, va_gc> *insts;
23754 tree copy;
23755 unsigned ix;
23757 if (!default_arg)
23758 continue;
23760 if (TREE_CODE (default_arg) != DEFAULT_ARG)
23761 /* This can happen for a friend declaration for a function
23762 already declared with default arguments. */
23763 continue;
23765 parsed_arg
23766 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
23767 default_arg,
23768 TREE_VALUE (parm));
23769 if (parsed_arg == error_mark_node)
23771 continue;
23774 TREE_PURPOSE (parm) = parsed_arg;
23776 /* Update any instantiations we've already created. */
23777 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
23778 vec_safe_iterate (insts, ix, &copy); ix++)
23779 TREE_PURPOSE (copy) = parsed_arg;
23782 pop_defarg_context ();
23784 /* Make sure no default arg is missing. */
23785 check_default_args (fn);
23787 /* Restore the state of local_variables_forbidden_p. */
23788 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
23790 /* Restore the queue. */
23791 pop_unparsed_function_queues (parser);
23794 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
23796 sizeof ... ( identifier )
23798 where the 'sizeof' token has already been consumed. */
23800 static tree
23801 cp_parser_sizeof_pack (cp_parser *parser)
23803 /* Consume the `...'. */
23804 cp_lexer_consume_token (parser->lexer);
23805 maybe_warn_variadic_templates ();
23807 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
23808 if (paren)
23809 cp_lexer_consume_token (parser->lexer);
23810 else
23811 permerror (cp_lexer_peek_token (parser->lexer)->location,
23812 "%<sizeof...%> argument must be surrounded by parentheses");
23814 cp_token *token = cp_lexer_peek_token (parser->lexer);
23815 tree name = cp_parser_identifier (parser);
23816 if (name == error_mark_node)
23817 return error_mark_node;
23818 /* The name is not qualified. */
23819 parser->scope = NULL_TREE;
23820 parser->qualifying_scope = NULL_TREE;
23821 parser->object_scope = NULL_TREE;
23822 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
23823 if (expr == error_mark_node)
23824 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
23825 token->location);
23826 if (TREE_CODE (expr) == TYPE_DECL)
23827 expr = TREE_TYPE (expr);
23828 else if (TREE_CODE (expr) == CONST_DECL)
23829 expr = DECL_INITIAL (expr);
23830 expr = make_pack_expansion (expr);
23832 if (paren)
23833 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23835 return expr;
23838 /* Parse the operand of `sizeof' (or a similar operator). Returns
23839 either a TYPE or an expression, depending on the form of the
23840 input. The KEYWORD indicates which kind of expression we have
23841 encountered. */
23843 static tree
23844 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
23846 tree expr = NULL_TREE;
23847 const char *saved_message;
23848 char *tmp;
23849 bool saved_integral_constant_expression_p;
23850 bool saved_non_integral_constant_expression_p;
23852 /* If it's a `...', then we are computing the length of a parameter
23853 pack. */
23854 if (keyword == RID_SIZEOF
23855 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23856 return cp_parser_sizeof_pack (parser);
23858 /* Types cannot be defined in a `sizeof' expression. Save away the
23859 old message. */
23860 saved_message = parser->type_definition_forbidden_message;
23861 /* And create the new one. */
23862 tmp = concat ("types may not be defined in %<",
23863 IDENTIFIER_POINTER (ridpointers[keyword]),
23864 "%> expressions", NULL);
23865 parser->type_definition_forbidden_message = tmp;
23867 /* The restrictions on constant-expressions do not apply inside
23868 sizeof expressions. */
23869 saved_integral_constant_expression_p
23870 = parser->integral_constant_expression_p;
23871 saved_non_integral_constant_expression_p
23872 = parser->non_integral_constant_expression_p;
23873 parser->integral_constant_expression_p = false;
23875 /* Do not actually evaluate the expression. */
23876 ++cp_unevaluated_operand;
23877 ++c_inhibit_evaluation_warnings;
23878 /* If it's a `(', then we might be looking at the type-id
23879 construction. */
23880 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23882 tree type = NULL_TREE;
23883 bool compound_literal_p;
23885 /* We can't be sure yet whether we're looking at a type-id or an
23886 expression. */
23887 cp_parser_parse_tentatively (parser);
23888 /* Consume the `('. */
23889 cp_lexer_consume_token (parser->lexer);
23890 /* Note: as a GNU Extension, compound literals are considered
23891 postfix-expressions as they are in C99, so they are valid
23892 arguments to sizeof. See comment in cp_parser_cast_expression
23893 for details. */
23894 cp_lexer_save_tokens (parser->lexer);
23895 /* Skip tokens until the next token is a closing parenthesis.
23896 If we find the closing `)', and the next token is a `{', then
23897 we are looking at a compound-literal. */
23898 compound_literal_p
23899 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
23900 /*consume_paren=*/true)
23901 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
23902 /* Roll back the tokens we skipped. */
23903 cp_lexer_rollback_tokens (parser->lexer);
23904 /* If we were looking at a compound-literal, simulate an error
23905 so that the call to cp_parser_parse_definitely below will
23906 fail. */
23907 if (compound_literal_p)
23908 cp_parser_simulate_error (parser);
23909 else
23911 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23912 parser->in_type_id_in_expr_p = true;
23913 /* Look for the type-id. */
23914 type = cp_parser_type_id (parser);
23915 /* Look for the closing `)'. */
23916 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23917 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23920 /* If all went well, then we're done. */
23921 if (cp_parser_parse_definitely (parser))
23923 cp_decl_specifier_seq decl_specs;
23925 /* Build a trivial decl-specifier-seq. */
23926 clear_decl_specs (&decl_specs);
23927 decl_specs.type = type;
23929 /* Call grokdeclarator to figure out what type this is. */
23930 expr = grokdeclarator (NULL,
23931 &decl_specs,
23932 TYPENAME,
23933 /*initialized=*/0,
23934 /*attrlist=*/NULL);
23938 /* If the type-id production did not work out, then we must be
23939 looking at the unary-expression production. */
23940 if (!expr)
23941 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
23942 /*cast_p=*/false, NULL);
23944 /* Go back to evaluating expressions. */
23945 --cp_unevaluated_operand;
23946 --c_inhibit_evaluation_warnings;
23948 /* Free the message we created. */
23949 free (tmp);
23950 /* And restore the old one. */
23951 parser->type_definition_forbidden_message = saved_message;
23952 parser->integral_constant_expression_p
23953 = saved_integral_constant_expression_p;
23954 parser->non_integral_constant_expression_p
23955 = saved_non_integral_constant_expression_p;
23957 return expr;
23960 /* If the current declaration has no declarator, return true. */
23962 static bool
23963 cp_parser_declares_only_class_p (cp_parser *parser)
23965 /* If the next token is a `;' or a `,' then there is no
23966 declarator. */
23967 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23968 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
23971 /* Update the DECL_SPECS to reflect the storage class indicated by
23972 KEYWORD. */
23974 static void
23975 cp_parser_set_storage_class (cp_parser *parser,
23976 cp_decl_specifier_seq *decl_specs,
23977 enum rid keyword,
23978 cp_token *token)
23980 cp_storage_class storage_class;
23982 if (parser->in_unbraced_linkage_specification_p)
23984 error_at (token->location, "invalid use of %qD in linkage specification",
23985 ridpointers[keyword]);
23986 return;
23988 else if (decl_specs->storage_class != sc_none)
23990 decl_specs->conflicting_specifiers_p = true;
23991 return;
23994 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
23995 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
23996 && decl_specs->gnu_thread_keyword_p)
23998 pedwarn (decl_specs->locations[ds_thread], 0,
23999 "%<__thread%> before %qD", ridpointers[keyword]);
24002 switch (keyword)
24004 case RID_AUTO:
24005 storage_class = sc_auto;
24006 break;
24007 case RID_REGISTER:
24008 storage_class = sc_register;
24009 break;
24010 case RID_STATIC:
24011 storage_class = sc_static;
24012 break;
24013 case RID_EXTERN:
24014 storage_class = sc_extern;
24015 break;
24016 case RID_MUTABLE:
24017 storage_class = sc_mutable;
24018 break;
24019 default:
24020 gcc_unreachable ();
24022 decl_specs->storage_class = storage_class;
24023 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24025 /* A storage class specifier cannot be applied alongside a typedef
24026 specifier. If there is a typedef specifier present then set
24027 conflicting_specifiers_p which will trigger an error later
24028 on in grokdeclarator. */
24029 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24030 decl_specs->conflicting_specifiers_p = true;
24033 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24034 is true, the type is a class or enum definition. */
24036 static void
24037 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24038 tree type_spec,
24039 cp_token *token,
24040 bool type_definition_p)
24042 decl_specs->any_specifiers_p = true;
24044 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24045 (with, for example, in "typedef int wchar_t;") we remember that
24046 this is what happened. In system headers, we ignore these
24047 declarations so that G++ can work with system headers that are not
24048 C++-safe. */
24049 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24050 && !type_definition_p
24051 && (type_spec == boolean_type_node
24052 || type_spec == char16_type_node
24053 || type_spec == char32_type_node
24054 || type_spec == wchar_type_node)
24055 && (decl_specs->type
24056 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24057 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24058 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24059 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24061 decl_specs->redefined_builtin_type = type_spec;
24062 set_and_check_decl_spec_loc (decl_specs,
24063 ds_redefined_builtin_type_spec,
24064 token);
24065 if (!decl_specs->type)
24067 decl_specs->type = type_spec;
24068 decl_specs->type_definition_p = false;
24069 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24072 else if (decl_specs->type)
24073 decl_specs->multiple_types_p = true;
24074 else
24076 decl_specs->type = type_spec;
24077 decl_specs->type_definition_p = type_definition_p;
24078 decl_specs->redefined_builtin_type = NULL_TREE;
24079 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24083 /* True iff TOKEN is the GNU keyword __thread. */
24085 static bool
24086 token_is__thread (cp_token *token)
24088 gcc_assert (token->keyword == RID_THREAD);
24089 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24092 /* Set the location for a declarator specifier and check if it is
24093 duplicated.
24095 DECL_SPECS is the sequence of declarator specifiers onto which to
24096 set the location.
24098 DS is the single declarator specifier to set which location is to
24099 be set onto the existing sequence of declarators.
24101 LOCATION is the location for the declarator specifier to
24102 consider. */
24104 static void
24105 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24106 cp_decl_spec ds, cp_token *token)
24108 gcc_assert (ds < ds_last);
24110 if (decl_specs == NULL)
24111 return;
24113 source_location location = token->location;
24115 if (decl_specs->locations[ds] == 0)
24117 decl_specs->locations[ds] = location;
24118 if (ds == ds_thread)
24119 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24121 else
24123 if (ds == ds_long)
24125 if (decl_specs->locations[ds_long_long] != 0)
24126 error_at (location,
24127 "%<long long long%> is too long for GCC");
24128 else
24130 decl_specs->locations[ds_long_long] = location;
24131 pedwarn_cxx98 (location,
24132 OPT_Wlong_long,
24133 "ISO C++ 1998 does not support %<long long%>");
24136 else if (ds == ds_thread)
24138 bool gnu = token_is__thread (token);
24139 if (gnu != decl_specs->gnu_thread_keyword_p)
24140 error_at (location,
24141 "both %<__thread%> and %<thread_local%> specified");
24142 else
24143 error_at (location, "duplicate %qD", token->u.value);
24145 else
24147 static const char *const decl_spec_names[] = {
24148 "signed",
24149 "unsigned",
24150 "short",
24151 "long",
24152 "const",
24153 "volatile",
24154 "restrict",
24155 "inline",
24156 "virtual",
24157 "explicit",
24158 "friend",
24159 "typedef",
24160 "using",
24161 "constexpr",
24162 "__complex"
24164 error_at (location,
24165 "duplicate %qs", decl_spec_names[ds]);
24170 /* Return true iff the declarator specifier DS is present in the
24171 sequence of declarator specifiers DECL_SPECS. */
24173 bool
24174 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24175 cp_decl_spec ds)
24177 gcc_assert (ds < ds_last);
24179 if (decl_specs == NULL)
24180 return false;
24182 return decl_specs->locations[ds] != 0;
24185 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24186 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24188 static bool
24189 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24191 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24194 /* Issue an error message indicating that TOKEN_DESC was expected.
24195 If KEYWORD is true, it indicated this function is called by
24196 cp_parser_require_keword and the required token can only be
24197 a indicated keyword. */
24199 static void
24200 cp_parser_required_error (cp_parser *parser,
24201 required_token token_desc,
24202 bool keyword)
24204 switch (token_desc)
24206 case RT_NEW:
24207 cp_parser_error (parser, "expected %<new%>");
24208 return;
24209 case RT_DELETE:
24210 cp_parser_error (parser, "expected %<delete%>");
24211 return;
24212 case RT_RETURN:
24213 cp_parser_error (parser, "expected %<return%>");
24214 return;
24215 case RT_WHILE:
24216 cp_parser_error (parser, "expected %<while%>");
24217 return;
24218 case RT_EXTERN:
24219 cp_parser_error (parser, "expected %<extern%>");
24220 return;
24221 case RT_STATIC_ASSERT:
24222 cp_parser_error (parser, "expected %<static_assert%>");
24223 return;
24224 case RT_DECLTYPE:
24225 cp_parser_error (parser, "expected %<decltype%>");
24226 return;
24227 case RT_OPERATOR:
24228 cp_parser_error (parser, "expected %<operator%>");
24229 return;
24230 case RT_CLASS:
24231 cp_parser_error (parser, "expected %<class%>");
24232 return;
24233 case RT_TEMPLATE:
24234 cp_parser_error (parser, "expected %<template%>");
24235 return;
24236 case RT_NAMESPACE:
24237 cp_parser_error (parser, "expected %<namespace%>");
24238 return;
24239 case RT_USING:
24240 cp_parser_error (parser, "expected %<using%>");
24241 return;
24242 case RT_ASM:
24243 cp_parser_error (parser, "expected %<asm%>");
24244 return;
24245 case RT_TRY:
24246 cp_parser_error (parser, "expected %<try%>");
24247 return;
24248 case RT_CATCH:
24249 cp_parser_error (parser, "expected %<catch%>");
24250 return;
24251 case RT_THROW:
24252 cp_parser_error (parser, "expected %<throw%>");
24253 return;
24254 case RT_LABEL:
24255 cp_parser_error (parser, "expected %<__label__%>");
24256 return;
24257 case RT_AT_TRY:
24258 cp_parser_error (parser, "expected %<@try%>");
24259 return;
24260 case RT_AT_SYNCHRONIZED:
24261 cp_parser_error (parser, "expected %<@synchronized%>");
24262 return;
24263 case RT_AT_THROW:
24264 cp_parser_error (parser, "expected %<@throw%>");
24265 return;
24266 case RT_TRANSACTION_ATOMIC:
24267 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24268 return;
24269 case RT_TRANSACTION_RELAXED:
24270 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24271 return;
24272 default:
24273 break;
24275 if (!keyword)
24277 switch (token_desc)
24279 case RT_SEMICOLON:
24280 cp_parser_error (parser, "expected %<;%>");
24281 return;
24282 case RT_OPEN_PAREN:
24283 cp_parser_error (parser, "expected %<(%>");
24284 return;
24285 case RT_CLOSE_BRACE:
24286 cp_parser_error (parser, "expected %<}%>");
24287 return;
24288 case RT_OPEN_BRACE:
24289 cp_parser_error (parser, "expected %<{%>");
24290 return;
24291 case RT_CLOSE_SQUARE:
24292 cp_parser_error (parser, "expected %<]%>");
24293 return;
24294 case RT_OPEN_SQUARE:
24295 cp_parser_error (parser, "expected %<[%>");
24296 return;
24297 case RT_COMMA:
24298 cp_parser_error (parser, "expected %<,%>");
24299 return;
24300 case RT_SCOPE:
24301 cp_parser_error (parser, "expected %<::%>");
24302 return;
24303 case RT_LESS:
24304 cp_parser_error (parser, "expected %<<%>");
24305 return;
24306 case RT_GREATER:
24307 cp_parser_error (parser, "expected %<>%>");
24308 return;
24309 case RT_EQ:
24310 cp_parser_error (parser, "expected %<=%>");
24311 return;
24312 case RT_ELLIPSIS:
24313 cp_parser_error (parser, "expected %<...%>");
24314 return;
24315 case RT_MULT:
24316 cp_parser_error (parser, "expected %<*%>");
24317 return;
24318 case RT_COMPL:
24319 cp_parser_error (parser, "expected %<~%>");
24320 return;
24321 case RT_COLON:
24322 cp_parser_error (parser, "expected %<:%>");
24323 return;
24324 case RT_COLON_SCOPE:
24325 cp_parser_error (parser, "expected %<:%> or %<::%>");
24326 return;
24327 case RT_CLOSE_PAREN:
24328 cp_parser_error (parser, "expected %<)%>");
24329 return;
24330 case RT_COMMA_CLOSE_PAREN:
24331 cp_parser_error (parser, "expected %<,%> or %<)%>");
24332 return;
24333 case RT_PRAGMA_EOL:
24334 cp_parser_error (parser, "expected end of line");
24335 return;
24336 case RT_NAME:
24337 cp_parser_error (parser, "expected identifier");
24338 return;
24339 case RT_SELECT:
24340 cp_parser_error (parser, "expected selection-statement");
24341 return;
24342 case RT_INTERATION:
24343 cp_parser_error (parser, "expected iteration-statement");
24344 return;
24345 case RT_JUMP:
24346 cp_parser_error (parser, "expected jump-statement");
24347 return;
24348 case RT_CLASS_KEY:
24349 cp_parser_error (parser, "expected class-key");
24350 return;
24351 case RT_CLASS_TYPENAME_TEMPLATE:
24352 cp_parser_error (parser,
24353 "expected %<class%>, %<typename%>, or %<template%>");
24354 return;
24355 default:
24356 gcc_unreachable ();
24359 else
24360 gcc_unreachable ();
24365 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24366 issue an error message indicating that TOKEN_DESC was expected.
24368 Returns the token consumed, if the token had the appropriate type.
24369 Otherwise, returns NULL. */
24371 static cp_token *
24372 cp_parser_require (cp_parser* parser,
24373 enum cpp_ttype type,
24374 required_token token_desc)
24376 if (cp_lexer_next_token_is (parser->lexer, type))
24377 return cp_lexer_consume_token (parser->lexer);
24378 else
24380 /* Output the MESSAGE -- unless we're parsing tentatively. */
24381 if (!cp_parser_simulate_error (parser))
24382 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24383 return NULL;
24387 /* An error message is produced if the next token is not '>'.
24388 All further tokens are skipped until the desired token is
24389 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24391 static void
24392 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24394 /* Current level of '< ... >'. */
24395 unsigned level = 0;
24396 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24397 unsigned nesting_depth = 0;
24399 /* Are we ready, yet? If not, issue error message. */
24400 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24401 return;
24403 /* Skip tokens until the desired token is found. */
24404 while (true)
24406 /* Peek at the next token. */
24407 switch (cp_lexer_peek_token (parser->lexer)->type)
24409 case CPP_LESS:
24410 if (!nesting_depth)
24411 ++level;
24412 break;
24414 case CPP_RSHIFT:
24415 if (cxx_dialect == cxx98)
24416 /* C++0x views the `>>' operator as two `>' tokens, but
24417 C++98 does not. */
24418 break;
24419 else if (!nesting_depth && level-- == 0)
24421 /* We've hit a `>>' where the first `>' closes the
24422 template argument list, and the second `>' is
24423 spurious. Just consume the `>>' and stop; we've
24424 already produced at least one error. */
24425 cp_lexer_consume_token (parser->lexer);
24426 return;
24428 /* Fall through for C++0x, so we handle the second `>' in
24429 the `>>'. */
24431 case CPP_GREATER:
24432 if (!nesting_depth && level-- == 0)
24434 /* We've reached the token we want, consume it and stop. */
24435 cp_lexer_consume_token (parser->lexer);
24436 return;
24438 break;
24440 case CPP_OPEN_PAREN:
24441 case CPP_OPEN_SQUARE:
24442 ++nesting_depth;
24443 break;
24445 case CPP_CLOSE_PAREN:
24446 case CPP_CLOSE_SQUARE:
24447 if (nesting_depth-- == 0)
24448 return;
24449 break;
24451 case CPP_EOF:
24452 case CPP_PRAGMA_EOL:
24453 case CPP_SEMICOLON:
24454 case CPP_OPEN_BRACE:
24455 case CPP_CLOSE_BRACE:
24456 /* The '>' was probably forgotten, don't look further. */
24457 return;
24459 default:
24460 break;
24463 /* Consume this token. */
24464 cp_lexer_consume_token (parser->lexer);
24468 /* If the next token is the indicated keyword, consume it. Otherwise,
24469 issue an error message indicating that TOKEN_DESC was expected.
24471 Returns the token consumed, if the token had the appropriate type.
24472 Otherwise, returns NULL. */
24474 static cp_token *
24475 cp_parser_require_keyword (cp_parser* parser,
24476 enum rid keyword,
24477 required_token token_desc)
24479 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24481 if (token && token->keyword != keyword)
24483 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24484 return NULL;
24487 return token;
24490 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24491 function-definition. */
24493 static bool
24494 cp_parser_token_starts_function_definition_p (cp_token* token)
24496 return (/* An ordinary function-body begins with an `{'. */
24497 token->type == CPP_OPEN_BRACE
24498 /* A ctor-initializer begins with a `:'. */
24499 || token->type == CPP_COLON
24500 /* A function-try-block begins with `try'. */
24501 || token->keyword == RID_TRY
24502 /* A function-transaction-block begins with `__transaction_atomic'
24503 or `__transaction_relaxed'. */
24504 || token->keyword == RID_TRANSACTION_ATOMIC
24505 || token->keyword == RID_TRANSACTION_RELAXED
24506 /* The named return value extension begins with `return'. */
24507 || token->keyword == RID_RETURN);
24510 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24511 definition. */
24513 static bool
24514 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24516 cp_token *token;
24518 token = cp_lexer_peek_token (parser->lexer);
24519 return (token->type == CPP_OPEN_BRACE
24520 || (token->type == CPP_COLON
24521 && !parser->colon_doesnt_start_class_def_p));
24524 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24525 C++0x) ending a template-argument. */
24527 static bool
24528 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24530 cp_token *token;
24532 token = cp_lexer_peek_token (parser->lexer);
24533 return (token->type == CPP_COMMA
24534 || token->type == CPP_GREATER
24535 || token->type == CPP_ELLIPSIS
24536 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24539 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24540 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24542 static bool
24543 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24544 size_t n)
24546 cp_token *token;
24548 token = cp_lexer_peek_nth_token (parser->lexer, n);
24549 if (token->type == CPP_LESS)
24550 return true;
24551 /* Check for the sequence `<::' in the original code. It would be lexed as
24552 `[:', where `[' is a digraph, and there is no whitespace before
24553 `:'. */
24554 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24556 cp_token *token2;
24557 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24558 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24559 return true;
24561 return false;
24564 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24565 or none_type otherwise. */
24567 static enum tag_types
24568 cp_parser_token_is_class_key (cp_token* token)
24570 switch (token->keyword)
24572 case RID_CLASS:
24573 return class_type;
24574 case RID_STRUCT:
24575 return record_type;
24576 case RID_UNION:
24577 return union_type;
24579 default:
24580 return none_type;
24584 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24586 static void
24587 cp_parser_check_class_key (enum tag_types class_key, tree type)
24589 if (type == error_mark_node)
24590 return;
24591 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24593 if (permerror (input_location, "%qs tag used in naming %q#T",
24594 class_key == union_type ? "union"
24595 : class_key == record_type ? "struct" : "class",
24596 type))
24597 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24598 "%q#T was previously declared here", type);
24602 /* Issue an error message if DECL is redeclared with different
24603 access than its original declaration [class.access.spec/3].
24604 This applies to nested classes and nested class templates.
24605 [class.mem/1]. */
24607 static void
24608 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24610 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24611 return;
24613 if ((TREE_PRIVATE (decl)
24614 != (current_access_specifier == access_private_node))
24615 || (TREE_PROTECTED (decl)
24616 != (current_access_specifier == access_protected_node)))
24617 error_at (location, "%qD redeclared with different access", decl);
24620 /* Look for the `template' keyword, as a syntactic disambiguator.
24621 Return TRUE iff it is present, in which case it will be
24622 consumed. */
24624 static bool
24625 cp_parser_optional_template_keyword (cp_parser *parser)
24627 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24629 /* In C++98 the `template' keyword can only be used within templates;
24630 outside templates the parser can always figure out what is a
24631 template and what is not. In C++11, per the resolution of DR 468,
24632 `template' is allowed in cases where it is not strictly necessary. */
24633 if (!processing_template_decl
24634 && pedantic && cxx_dialect == cxx98)
24636 cp_token *token = cp_lexer_peek_token (parser->lexer);
24637 pedwarn (token->location, OPT_Wpedantic,
24638 "in C++98 %<template%> (as a disambiguator) is only "
24639 "allowed within templates");
24640 /* If this part of the token stream is rescanned, the same
24641 error message would be generated. So, we purge the token
24642 from the stream. */
24643 cp_lexer_purge_token (parser->lexer);
24644 return false;
24646 else
24648 /* Consume the `template' keyword. */
24649 cp_lexer_consume_token (parser->lexer);
24650 return true;
24653 return false;
24656 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24657 set PARSER->SCOPE, and perform other related actions. */
24659 static void
24660 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
24662 int i;
24663 struct tree_check *check_value;
24664 deferred_access_check *chk;
24665 vec<deferred_access_check, va_gc> *checks;
24667 /* Get the stored value. */
24668 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
24669 /* Perform any access checks that were deferred. */
24670 checks = check_value->checks;
24671 if (checks)
24673 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
24674 perform_or_defer_access_check (chk->binfo,
24675 chk->decl,
24676 chk->diag_decl, tf_warning_or_error);
24678 /* Set the scope from the stored value. */
24679 parser->scope = check_value->value;
24680 parser->qualifying_scope = check_value->qualifying_scope;
24681 parser->object_scope = NULL_TREE;
24684 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24685 encounter the end of a block before what we were looking for. */
24687 static bool
24688 cp_parser_cache_group (cp_parser *parser,
24689 enum cpp_ttype end,
24690 unsigned depth)
24692 while (true)
24694 cp_token *token = cp_lexer_peek_token (parser->lexer);
24696 /* Abort a parenthesized expression if we encounter a semicolon. */
24697 if ((end == CPP_CLOSE_PAREN || depth == 0)
24698 && token->type == CPP_SEMICOLON)
24699 return true;
24700 /* If we've reached the end of the file, stop. */
24701 if (token->type == CPP_EOF
24702 || (end != CPP_PRAGMA_EOL
24703 && token->type == CPP_PRAGMA_EOL))
24704 return true;
24705 if (token->type == CPP_CLOSE_BRACE && depth == 0)
24706 /* We've hit the end of an enclosing block, so there's been some
24707 kind of syntax error. */
24708 return true;
24710 /* Consume the token. */
24711 cp_lexer_consume_token (parser->lexer);
24712 /* See if it starts a new group. */
24713 if (token->type == CPP_OPEN_BRACE)
24715 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
24716 /* In theory this should probably check end == '}', but
24717 cp_parser_save_member_function_body needs it to exit
24718 after either '}' or ')' when called with ')'. */
24719 if (depth == 0)
24720 return false;
24722 else if (token->type == CPP_OPEN_PAREN)
24724 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
24725 if (depth == 0 && end == CPP_CLOSE_PAREN)
24726 return false;
24728 else if (token->type == CPP_PRAGMA)
24729 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
24730 else if (token->type == end)
24731 return false;
24735 /* Like above, for caching a default argument or NSDMI. Both of these are
24736 terminated by a non-nested comma, but it can be unclear whether or not a
24737 comma is nested in a template argument list unless we do more parsing.
24738 In order to handle this ambiguity, when we encounter a ',' after a '<'
24739 we try to parse what follows as a parameter-declaration-list (in the
24740 case of a default argument) or a member-declarator (in the case of an
24741 NSDMI). If that succeeds, then we stop caching. */
24743 static tree
24744 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
24746 unsigned depth = 0;
24747 int maybe_template_id = 0;
24748 cp_token *first_token;
24749 cp_token *token;
24750 tree default_argument;
24752 /* Add tokens until we have processed the entire default
24753 argument. We add the range [first_token, token). */
24754 first_token = cp_lexer_peek_token (parser->lexer);
24755 if (first_token->type == CPP_OPEN_BRACE)
24757 /* For list-initialization, this is straightforward. */
24758 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
24759 token = cp_lexer_peek_token (parser->lexer);
24761 else while (true)
24763 bool done = false;
24765 /* Peek at the next token. */
24766 token = cp_lexer_peek_token (parser->lexer);
24767 /* What we do depends on what token we have. */
24768 switch (token->type)
24770 /* In valid code, a default argument must be
24771 immediately followed by a `,' `)', or `...'. */
24772 case CPP_COMMA:
24773 if (depth == 0 && maybe_template_id)
24775 /* If we've seen a '<', we might be in a
24776 template-argument-list. Until Core issue 325 is
24777 resolved, we don't know how this situation ought
24778 to be handled, so try to DTRT. We check whether
24779 what comes after the comma is a valid parameter
24780 declaration list. If it is, then the comma ends
24781 the default argument; otherwise the default
24782 argument continues. */
24783 bool error = false;
24785 /* Set ITALP so cp_parser_parameter_declaration_list
24786 doesn't decide to commit to this parse. */
24787 bool saved_italp = parser->in_template_argument_list_p;
24788 parser->in_template_argument_list_p = true;
24790 cp_parser_parse_tentatively (parser);
24791 cp_lexer_consume_token (parser->lexer);
24793 if (nsdmi)
24795 int ctor_dtor_or_conv_p;
24796 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24797 &ctor_dtor_or_conv_p,
24798 /*parenthesized_p=*/NULL,
24799 /*member_p=*/true);
24801 else
24803 begin_scope (sk_function_parms, NULL_TREE);
24804 cp_parser_parameter_declaration_list (parser, &error);
24805 pop_bindings_and_leave_scope ();
24807 if (!cp_parser_error_occurred (parser) && !error)
24808 done = true;
24809 cp_parser_abort_tentative_parse (parser);
24811 parser->in_template_argument_list_p = saved_italp;
24812 break;
24814 case CPP_CLOSE_PAREN:
24815 case CPP_ELLIPSIS:
24816 /* If we run into a non-nested `;', `}', or `]',
24817 then the code is invalid -- but the default
24818 argument is certainly over. */
24819 case CPP_SEMICOLON:
24820 case CPP_CLOSE_BRACE:
24821 case CPP_CLOSE_SQUARE:
24822 if (depth == 0
24823 /* Handle correctly int n = sizeof ... ( p ); */
24824 && token->type != CPP_ELLIPSIS)
24825 done = true;
24826 /* Update DEPTH, if necessary. */
24827 else if (token->type == CPP_CLOSE_PAREN
24828 || token->type == CPP_CLOSE_BRACE
24829 || token->type == CPP_CLOSE_SQUARE)
24830 --depth;
24831 break;
24833 case CPP_OPEN_PAREN:
24834 case CPP_OPEN_SQUARE:
24835 case CPP_OPEN_BRACE:
24836 ++depth;
24837 break;
24839 case CPP_LESS:
24840 if (depth == 0)
24841 /* This might be the comparison operator, or it might
24842 start a template argument list. */
24843 ++maybe_template_id;
24844 break;
24846 case CPP_RSHIFT:
24847 if (cxx_dialect == cxx98)
24848 break;
24849 /* Fall through for C++0x, which treats the `>>'
24850 operator like two `>' tokens in certain
24851 cases. */
24853 case CPP_GREATER:
24854 if (depth == 0)
24856 /* This might be an operator, or it might close a
24857 template argument list. But if a previous '<'
24858 started a template argument list, this will have
24859 closed it, so we can't be in one anymore. */
24860 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
24861 if (maybe_template_id < 0)
24862 maybe_template_id = 0;
24864 break;
24866 /* If we run out of tokens, issue an error message. */
24867 case CPP_EOF:
24868 case CPP_PRAGMA_EOL:
24869 error_at (token->location, "file ends in default argument");
24870 done = true;
24871 break;
24873 case CPP_NAME:
24874 case CPP_SCOPE:
24875 /* In these cases, we should look for template-ids.
24876 For example, if the default argument is
24877 `X<int, double>()', we need to do name lookup to
24878 figure out whether or not `X' is a template; if
24879 so, the `,' does not end the default argument.
24881 That is not yet done. */
24882 break;
24884 default:
24885 break;
24888 /* If we've reached the end, stop. */
24889 if (done)
24890 break;
24892 /* Add the token to the token block. */
24893 token = cp_lexer_consume_token (parser->lexer);
24896 /* Create a DEFAULT_ARG to represent the unparsed default
24897 argument. */
24898 default_argument = make_node (DEFAULT_ARG);
24899 DEFARG_TOKENS (default_argument)
24900 = cp_token_cache_new (first_token, token);
24901 DEFARG_INSTANTIATIONS (default_argument) = NULL;
24903 return default_argument;
24906 /* Begin parsing tentatively. We always save tokens while parsing
24907 tentatively so that if the tentative parsing fails we can restore the
24908 tokens. */
24910 static void
24911 cp_parser_parse_tentatively (cp_parser* parser)
24913 /* Enter a new parsing context. */
24914 parser->context = cp_parser_context_new (parser->context);
24915 /* Begin saving tokens. */
24916 cp_lexer_save_tokens (parser->lexer);
24917 /* In order to avoid repetitive access control error messages,
24918 access checks are queued up until we are no longer parsing
24919 tentatively. */
24920 push_deferring_access_checks (dk_deferred);
24923 /* Commit to the currently active tentative parse. */
24925 static void
24926 cp_parser_commit_to_tentative_parse (cp_parser* parser)
24928 cp_parser_context *context;
24929 cp_lexer *lexer;
24931 /* Mark all of the levels as committed. */
24932 lexer = parser->lexer;
24933 for (context = parser->context; context->next; context = context->next)
24935 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24936 break;
24937 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24938 while (!cp_lexer_saving_tokens (lexer))
24939 lexer = lexer->next;
24940 cp_lexer_commit_tokens (lexer);
24944 /* Commit to the topmost currently active tentative parse.
24946 Note that this function shouldn't be called when there are
24947 irreversible side-effects while in a tentative state. For
24948 example, we shouldn't create a permanent entry in the symbol
24949 table, or issue an error message that might not apply if the
24950 tentative parse is aborted. */
24952 static void
24953 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
24955 cp_parser_context *context = parser->context;
24956 cp_lexer *lexer = parser->lexer;
24958 if (context)
24960 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
24961 return;
24962 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
24964 while (!cp_lexer_saving_tokens (lexer))
24965 lexer = lexer->next;
24966 cp_lexer_commit_tokens (lexer);
24970 /* Abort the currently active tentative parse. All consumed tokens
24971 will be rolled back, and no diagnostics will be issued. */
24973 static void
24974 cp_parser_abort_tentative_parse (cp_parser* parser)
24976 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
24977 || errorcount > 0);
24978 cp_parser_simulate_error (parser);
24979 /* Now, pretend that we want to see if the construct was
24980 successfully parsed. */
24981 cp_parser_parse_definitely (parser);
24984 /* Stop parsing tentatively. If a parse error has occurred, restore the
24985 token stream. Otherwise, commit to the tokens we have consumed.
24986 Returns true if no error occurred; false otherwise. */
24988 static bool
24989 cp_parser_parse_definitely (cp_parser* parser)
24991 bool error_occurred;
24992 cp_parser_context *context;
24994 /* Remember whether or not an error occurred, since we are about to
24995 destroy that information. */
24996 error_occurred = cp_parser_error_occurred (parser);
24997 /* Remove the topmost context from the stack. */
24998 context = parser->context;
24999 parser->context = context->next;
25000 /* If no parse errors occurred, commit to the tentative parse. */
25001 if (!error_occurred)
25003 /* Commit to the tokens read tentatively, unless that was
25004 already done. */
25005 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25006 cp_lexer_commit_tokens (parser->lexer);
25008 pop_to_parent_deferring_access_checks ();
25010 /* Otherwise, if errors occurred, roll back our state so that things
25011 are just as they were before we began the tentative parse. */
25012 else
25014 cp_lexer_rollback_tokens (parser->lexer);
25015 pop_deferring_access_checks ();
25017 /* Add the context to the front of the free list. */
25018 context->next = cp_parser_context_free_list;
25019 cp_parser_context_free_list = context;
25021 return !error_occurred;
25024 /* Returns true if we are parsing tentatively and are not committed to
25025 this tentative parse. */
25027 static bool
25028 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25030 return (cp_parser_parsing_tentatively (parser)
25031 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25034 /* Returns nonzero iff an error has occurred during the most recent
25035 tentative parse. */
25037 static bool
25038 cp_parser_error_occurred (cp_parser* parser)
25040 return (cp_parser_parsing_tentatively (parser)
25041 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25044 /* Returns nonzero if GNU extensions are allowed. */
25046 static bool
25047 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25049 return parser->allow_gnu_extensions_p;
25052 /* Objective-C++ Productions */
25055 /* Parse an Objective-C expression, which feeds into a primary-expression
25056 above.
25058 objc-expression:
25059 objc-message-expression
25060 objc-string-literal
25061 objc-encode-expression
25062 objc-protocol-expression
25063 objc-selector-expression
25065 Returns a tree representation of the expression. */
25067 static tree
25068 cp_parser_objc_expression (cp_parser* parser)
25070 /* Try to figure out what kind of declaration is present. */
25071 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25073 switch (kwd->type)
25075 case CPP_OPEN_SQUARE:
25076 return cp_parser_objc_message_expression (parser);
25078 case CPP_OBJC_STRING:
25079 kwd = cp_lexer_consume_token (parser->lexer);
25080 return objc_build_string_object (kwd->u.value);
25082 case CPP_KEYWORD:
25083 switch (kwd->keyword)
25085 case RID_AT_ENCODE:
25086 return cp_parser_objc_encode_expression (parser);
25088 case RID_AT_PROTOCOL:
25089 return cp_parser_objc_protocol_expression (parser);
25091 case RID_AT_SELECTOR:
25092 return cp_parser_objc_selector_expression (parser);
25094 default:
25095 break;
25097 default:
25098 error_at (kwd->location,
25099 "misplaced %<@%D%> Objective-C++ construct",
25100 kwd->u.value);
25101 cp_parser_skip_to_end_of_block_or_statement (parser);
25104 return error_mark_node;
25107 /* Parse an Objective-C message expression.
25109 objc-message-expression:
25110 [ objc-message-receiver objc-message-args ]
25112 Returns a representation of an Objective-C message. */
25114 static tree
25115 cp_parser_objc_message_expression (cp_parser* parser)
25117 tree receiver, messageargs;
25119 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25120 receiver = cp_parser_objc_message_receiver (parser);
25121 messageargs = cp_parser_objc_message_args (parser);
25122 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25124 return objc_build_message_expr (receiver, messageargs);
25127 /* Parse an objc-message-receiver.
25129 objc-message-receiver:
25130 expression
25131 simple-type-specifier
25133 Returns a representation of the type or expression. */
25135 static tree
25136 cp_parser_objc_message_receiver (cp_parser* parser)
25138 tree rcv;
25140 /* An Objective-C message receiver may be either (1) a type
25141 or (2) an expression. */
25142 cp_parser_parse_tentatively (parser);
25143 rcv = cp_parser_expression (parser, false, NULL);
25145 if (cp_parser_parse_definitely (parser))
25146 return rcv;
25148 rcv = cp_parser_simple_type_specifier (parser,
25149 /*decl_specs=*/NULL,
25150 CP_PARSER_FLAGS_NONE);
25152 return objc_get_class_reference (rcv);
25155 /* Parse the arguments and selectors comprising an Objective-C message.
25157 objc-message-args:
25158 objc-selector
25159 objc-selector-args
25160 objc-selector-args , objc-comma-args
25162 objc-selector-args:
25163 objc-selector [opt] : assignment-expression
25164 objc-selector-args objc-selector [opt] : assignment-expression
25166 objc-comma-args:
25167 assignment-expression
25168 objc-comma-args , assignment-expression
25170 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25171 selector arguments and TREE_VALUE containing a list of comma
25172 arguments. */
25174 static tree
25175 cp_parser_objc_message_args (cp_parser* parser)
25177 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25178 bool maybe_unary_selector_p = true;
25179 cp_token *token = cp_lexer_peek_token (parser->lexer);
25181 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25183 tree selector = NULL_TREE, arg;
25185 if (token->type != CPP_COLON)
25186 selector = cp_parser_objc_selector (parser);
25188 /* Detect if we have a unary selector. */
25189 if (maybe_unary_selector_p
25190 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25191 return build_tree_list (selector, NULL_TREE);
25193 maybe_unary_selector_p = false;
25194 cp_parser_require (parser, CPP_COLON, RT_COLON);
25195 arg = cp_parser_assignment_expression (parser, false, NULL);
25197 sel_args
25198 = chainon (sel_args,
25199 build_tree_list (selector, arg));
25201 token = cp_lexer_peek_token (parser->lexer);
25204 /* Handle non-selector arguments, if any. */
25205 while (token->type == CPP_COMMA)
25207 tree arg;
25209 cp_lexer_consume_token (parser->lexer);
25210 arg = cp_parser_assignment_expression (parser, false, NULL);
25212 addl_args
25213 = chainon (addl_args,
25214 build_tree_list (NULL_TREE, arg));
25216 token = cp_lexer_peek_token (parser->lexer);
25219 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25221 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25222 return build_tree_list (error_mark_node, error_mark_node);
25225 return build_tree_list (sel_args, addl_args);
25228 /* Parse an Objective-C encode expression.
25230 objc-encode-expression:
25231 @encode objc-typename
25233 Returns an encoded representation of the type argument. */
25235 static tree
25236 cp_parser_objc_encode_expression (cp_parser* parser)
25238 tree type;
25239 cp_token *token;
25241 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25242 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25243 token = cp_lexer_peek_token (parser->lexer);
25244 type = complete_type (cp_parser_type_id (parser));
25245 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25247 if (!type)
25249 error_at (token->location,
25250 "%<@encode%> must specify a type as an argument");
25251 return error_mark_node;
25254 /* This happens if we find @encode(T) (where T is a template
25255 typename or something dependent on a template typename) when
25256 parsing a template. In that case, we can't compile it
25257 immediately, but we rather create an AT_ENCODE_EXPR which will
25258 need to be instantiated when the template is used.
25260 if (dependent_type_p (type))
25262 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25263 TREE_READONLY (value) = 1;
25264 return value;
25267 return objc_build_encode_expr (type);
25270 /* Parse an Objective-C @defs expression. */
25272 static tree
25273 cp_parser_objc_defs_expression (cp_parser *parser)
25275 tree name;
25277 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25278 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25279 name = cp_parser_identifier (parser);
25280 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25282 return objc_get_class_ivars (name);
25285 /* Parse an Objective-C protocol expression.
25287 objc-protocol-expression:
25288 @protocol ( identifier )
25290 Returns a representation of the protocol expression. */
25292 static tree
25293 cp_parser_objc_protocol_expression (cp_parser* parser)
25295 tree proto;
25297 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25298 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25299 proto = cp_parser_identifier (parser);
25300 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25302 return objc_build_protocol_expr (proto);
25305 /* Parse an Objective-C selector expression.
25307 objc-selector-expression:
25308 @selector ( objc-method-signature )
25310 objc-method-signature:
25311 objc-selector
25312 objc-selector-seq
25314 objc-selector-seq:
25315 objc-selector :
25316 objc-selector-seq objc-selector :
25318 Returns a representation of the method selector. */
25320 static tree
25321 cp_parser_objc_selector_expression (cp_parser* parser)
25323 tree sel_seq = NULL_TREE;
25324 bool maybe_unary_selector_p = true;
25325 cp_token *token;
25326 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25328 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25329 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25330 token = cp_lexer_peek_token (parser->lexer);
25332 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25333 || token->type == CPP_SCOPE)
25335 tree selector = NULL_TREE;
25337 if (token->type != CPP_COLON
25338 || token->type == CPP_SCOPE)
25339 selector = cp_parser_objc_selector (parser);
25341 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25342 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25344 /* Detect if we have a unary selector. */
25345 if (maybe_unary_selector_p)
25347 sel_seq = selector;
25348 goto finish_selector;
25350 else
25352 cp_parser_error (parser, "expected %<:%>");
25355 maybe_unary_selector_p = false;
25356 token = cp_lexer_consume_token (parser->lexer);
25358 if (token->type == CPP_SCOPE)
25360 sel_seq
25361 = chainon (sel_seq,
25362 build_tree_list (selector, NULL_TREE));
25363 sel_seq
25364 = chainon (sel_seq,
25365 build_tree_list (NULL_TREE, NULL_TREE));
25367 else
25368 sel_seq
25369 = chainon (sel_seq,
25370 build_tree_list (selector, NULL_TREE));
25372 token = cp_lexer_peek_token (parser->lexer);
25375 finish_selector:
25376 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25378 return objc_build_selector_expr (loc, sel_seq);
25381 /* Parse a list of identifiers.
25383 objc-identifier-list:
25384 identifier
25385 objc-identifier-list , identifier
25387 Returns a TREE_LIST of identifier nodes. */
25389 static tree
25390 cp_parser_objc_identifier_list (cp_parser* parser)
25392 tree identifier;
25393 tree list;
25394 cp_token *sep;
25396 identifier = cp_parser_identifier (parser);
25397 if (identifier == error_mark_node)
25398 return error_mark_node;
25400 list = build_tree_list (NULL_TREE, identifier);
25401 sep = cp_lexer_peek_token (parser->lexer);
25403 while (sep->type == CPP_COMMA)
25405 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25406 identifier = cp_parser_identifier (parser);
25407 if (identifier == error_mark_node)
25408 return list;
25410 list = chainon (list, build_tree_list (NULL_TREE,
25411 identifier));
25412 sep = cp_lexer_peek_token (parser->lexer);
25415 return list;
25418 /* Parse an Objective-C alias declaration.
25420 objc-alias-declaration:
25421 @compatibility_alias identifier identifier ;
25423 This function registers the alias mapping with the Objective-C front end.
25424 It returns nothing. */
25426 static void
25427 cp_parser_objc_alias_declaration (cp_parser* parser)
25429 tree alias, orig;
25431 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25432 alias = cp_parser_identifier (parser);
25433 orig = cp_parser_identifier (parser);
25434 objc_declare_alias (alias, orig);
25435 cp_parser_consume_semicolon_at_end_of_statement (parser);
25438 /* Parse an Objective-C class forward-declaration.
25440 objc-class-declaration:
25441 @class objc-identifier-list ;
25443 The function registers the forward declarations with the Objective-C
25444 front end. It returns nothing. */
25446 static void
25447 cp_parser_objc_class_declaration (cp_parser* parser)
25449 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25450 while (true)
25452 tree id;
25454 id = cp_parser_identifier (parser);
25455 if (id == error_mark_node)
25456 break;
25458 objc_declare_class (id);
25460 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25461 cp_lexer_consume_token (parser->lexer);
25462 else
25463 break;
25465 cp_parser_consume_semicolon_at_end_of_statement (parser);
25468 /* Parse a list of Objective-C protocol references.
25470 objc-protocol-refs-opt:
25471 objc-protocol-refs [opt]
25473 objc-protocol-refs:
25474 < objc-identifier-list >
25476 Returns a TREE_LIST of identifiers, if any. */
25478 static tree
25479 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25481 tree protorefs = NULL_TREE;
25483 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25485 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25486 protorefs = cp_parser_objc_identifier_list (parser);
25487 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25490 return protorefs;
25493 /* Parse a Objective-C visibility specification. */
25495 static void
25496 cp_parser_objc_visibility_spec (cp_parser* parser)
25498 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25500 switch (vis->keyword)
25502 case RID_AT_PRIVATE:
25503 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25504 break;
25505 case RID_AT_PROTECTED:
25506 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25507 break;
25508 case RID_AT_PUBLIC:
25509 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25510 break;
25511 case RID_AT_PACKAGE:
25512 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25513 break;
25514 default:
25515 return;
25518 /* Eat '@private'/'@protected'/'@public'. */
25519 cp_lexer_consume_token (parser->lexer);
25522 /* Parse an Objective-C method type. Return 'true' if it is a class
25523 (+) method, and 'false' if it is an instance (-) method. */
25525 static inline bool
25526 cp_parser_objc_method_type (cp_parser* parser)
25528 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25529 return true;
25530 else
25531 return false;
25534 /* Parse an Objective-C protocol qualifier. */
25536 static tree
25537 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25539 tree quals = NULL_TREE, node;
25540 cp_token *token = cp_lexer_peek_token (parser->lexer);
25542 node = token->u.value;
25544 while (node && identifier_p (node)
25545 && (node == ridpointers [(int) RID_IN]
25546 || node == ridpointers [(int) RID_OUT]
25547 || node == ridpointers [(int) RID_INOUT]
25548 || node == ridpointers [(int) RID_BYCOPY]
25549 || node == ridpointers [(int) RID_BYREF]
25550 || node == ridpointers [(int) RID_ONEWAY]))
25552 quals = tree_cons (NULL_TREE, node, quals);
25553 cp_lexer_consume_token (parser->lexer);
25554 token = cp_lexer_peek_token (parser->lexer);
25555 node = token->u.value;
25558 return quals;
25561 /* Parse an Objective-C typename. */
25563 static tree
25564 cp_parser_objc_typename (cp_parser* parser)
25566 tree type_name = NULL_TREE;
25568 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25570 tree proto_quals, cp_type = NULL_TREE;
25572 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25573 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25575 /* An ObjC type name may consist of just protocol qualifiers, in which
25576 case the type shall default to 'id'. */
25577 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25579 cp_type = cp_parser_type_id (parser);
25581 /* If the type could not be parsed, an error has already
25582 been produced. For error recovery, behave as if it had
25583 not been specified, which will use the default type
25584 'id'. */
25585 if (cp_type == error_mark_node)
25587 cp_type = NULL_TREE;
25588 /* We need to skip to the closing parenthesis as
25589 cp_parser_type_id() does not seem to do it for
25590 us. */
25591 cp_parser_skip_to_closing_parenthesis (parser,
25592 /*recovering=*/true,
25593 /*or_comma=*/false,
25594 /*consume_paren=*/false);
25598 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25599 type_name = build_tree_list (proto_quals, cp_type);
25602 return type_name;
25605 /* Check to see if TYPE refers to an Objective-C selector name. */
25607 static bool
25608 cp_parser_objc_selector_p (enum cpp_ttype type)
25610 return (type == CPP_NAME || type == CPP_KEYWORD
25611 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25612 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25613 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25614 || type == CPP_XOR || type == CPP_XOR_EQ);
25617 /* Parse an Objective-C selector. */
25619 static tree
25620 cp_parser_objc_selector (cp_parser* parser)
25622 cp_token *token = cp_lexer_consume_token (parser->lexer);
25624 if (!cp_parser_objc_selector_p (token->type))
25626 error_at (token->location, "invalid Objective-C++ selector name");
25627 return error_mark_node;
25630 /* C++ operator names are allowed to appear in ObjC selectors. */
25631 switch (token->type)
25633 case CPP_AND_AND: return get_identifier ("and");
25634 case CPP_AND_EQ: return get_identifier ("and_eq");
25635 case CPP_AND: return get_identifier ("bitand");
25636 case CPP_OR: return get_identifier ("bitor");
25637 case CPP_COMPL: return get_identifier ("compl");
25638 case CPP_NOT: return get_identifier ("not");
25639 case CPP_NOT_EQ: return get_identifier ("not_eq");
25640 case CPP_OR_OR: return get_identifier ("or");
25641 case CPP_OR_EQ: return get_identifier ("or_eq");
25642 case CPP_XOR: return get_identifier ("xor");
25643 case CPP_XOR_EQ: return get_identifier ("xor_eq");
25644 default: return token->u.value;
25648 /* Parse an Objective-C params list. */
25650 static tree
25651 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
25653 tree params = NULL_TREE;
25654 bool maybe_unary_selector_p = true;
25655 cp_token *token = cp_lexer_peek_token (parser->lexer);
25657 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25659 tree selector = NULL_TREE, type_name, identifier;
25660 tree parm_attr = NULL_TREE;
25662 if (token->keyword == RID_ATTRIBUTE)
25663 break;
25665 if (token->type != CPP_COLON)
25666 selector = cp_parser_objc_selector (parser);
25668 /* Detect if we have a unary selector. */
25669 if (maybe_unary_selector_p
25670 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25672 params = selector; /* Might be followed by attributes. */
25673 break;
25676 maybe_unary_selector_p = false;
25677 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25679 /* Something went quite wrong. There should be a colon
25680 here, but there is not. Stop parsing parameters. */
25681 break;
25683 type_name = cp_parser_objc_typename (parser);
25684 /* New ObjC allows attributes on parameters too. */
25685 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
25686 parm_attr = cp_parser_attributes_opt (parser);
25687 identifier = cp_parser_identifier (parser);
25689 params
25690 = chainon (params,
25691 objc_build_keyword_decl (selector,
25692 type_name,
25693 identifier,
25694 parm_attr));
25696 token = cp_lexer_peek_token (parser->lexer);
25699 if (params == NULL_TREE)
25701 cp_parser_error (parser, "objective-c++ method declaration is expected");
25702 return error_mark_node;
25705 /* We allow tail attributes for the method. */
25706 if (token->keyword == RID_ATTRIBUTE)
25708 *attributes = cp_parser_attributes_opt (parser);
25709 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25710 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25711 return params;
25712 cp_parser_error (parser,
25713 "method attributes must be specified at the end");
25714 return error_mark_node;
25717 if (params == NULL_TREE)
25719 cp_parser_error (parser, "objective-c++ method declaration is expected");
25720 return error_mark_node;
25722 return params;
25725 /* Parse the non-keyword Objective-C params. */
25727 static tree
25728 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
25729 tree* attributes)
25731 tree params = make_node (TREE_LIST);
25732 cp_token *token = cp_lexer_peek_token (parser->lexer);
25733 *ellipsisp = false; /* Initially, assume no ellipsis. */
25735 while (token->type == CPP_COMMA)
25737 cp_parameter_declarator *parmdecl;
25738 tree parm;
25740 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25741 token = cp_lexer_peek_token (parser->lexer);
25743 if (token->type == CPP_ELLIPSIS)
25745 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
25746 *ellipsisp = true;
25747 token = cp_lexer_peek_token (parser->lexer);
25748 break;
25751 /* TODO: parse attributes for tail parameters. */
25752 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
25753 parm = grokdeclarator (parmdecl->declarator,
25754 &parmdecl->decl_specifiers,
25755 PARM, /*initialized=*/0,
25756 /*attrlist=*/NULL);
25758 chainon (params, build_tree_list (NULL_TREE, parm));
25759 token = cp_lexer_peek_token (parser->lexer);
25762 /* We allow tail attributes for the method. */
25763 if (token->keyword == RID_ATTRIBUTE)
25765 if (*attributes == NULL_TREE)
25767 *attributes = cp_parser_attributes_opt (parser);
25768 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25769 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25770 return params;
25772 else
25773 /* We have an error, but parse the attributes, so that we can
25774 carry on. */
25775 *attributes = cp_parser_attributes_opt (parser);
25777 cp_parser_error (parser,
25778 "method attributes must be specified at the end");
25779 return error_mark_node;
25782 return params;
25785 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
25787 static void
25788 cp_parser_objc_interstitial_code (cp_parser* parser)
25790 cp_token *token = cp_lexer_peek_token (parser->lexer);
25792 /* If the next token is `extern' and the following token is a string
25793 literal, then we have a linkage specification. */
25794 if (token->keyword == RID_EXTERN
25795 && cp_parser_is_pure_string_literal
25796 (cp_lexer_peek_nth_token (parser->lexer, 2)))
25797 cp_parser_linkage_specification (parser);
25798 /* Handle #pragma, if any. */
25799 else if (token->type == CPP_PRAGMA)
25800 cp_parser_pragma (parser, pragma_objc_icode);
25801 /* Allow stray semicolons. */
25802 else if (token->type == CPP_SEMICOLON)
25803 cp_lexer_consume_token (parser->lexer);
25804 /* Mark methods as optional or required, when building protocols. */
25805 else if (token->keyword == RID_AT_OPTIONAL)
25807 cp_lexer_consume_token (parser->lexer);
25808 objc_set_method_opt (true);
25810 else if (token->keyword == RID_AT_REQUIRED)
25812 cp_lexer_consume_token (parser->lexer);
25813 objc_set_method_opt (false);
25815 else if (token->keyword == RID_NAMESPACE)
25816 cp_parser_namespace_definition (parser);
25817 /* Other stray characters must generate errors. */
25818 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
25820 cp_lexer_consume_token (parser->lexer);
25821 error ("stray %qs between Objective-C++ methods",
25822 token->type == CPP_OPEN_BRACE ? "{" : "}");
25824 /* Finally, try to parse a block-declaration, or a function-definition. */
25825 else
25826 cp_parser_block_declaration (parser, /*statement_p=*/false);
25829 /* Parse a method signature. */
25831 static tree
25832 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
25834 tree rettype, kwdparms, optparms;
25835 bool ellipsis = false;
25836 bool is_class_method;
25838 is_class_method = cp_parser_objc_method_type (parser);
25839 rettype = cp_parser_objc_typename (parser);
25840 *attributes = NULL_TREE;
25841 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
25842 if (kwdparms == error_mark_node)
25843 return error_mark_node;
25844 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
25845 if (optparms == error_mark_node)
25846 return error_mark_node;
25848 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
25851 static bool
25852 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
25854 tree tattr;
25855 cp_lexer_save_tokens (parser->lexer);
25856 tattr = cp_parser_attributes_opt (parser);
25857 gcc_assert (tattr) ;
25859 /* If the attributes are followed by a method introducer, this is not allowed.
25860 Dump the attributes and flag the situation. */
25861 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
25862 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
25863 return true;
25865 /* Otherwise, the attributes introduce some interstitial code, possibly so
25866 rewind to allow that check. */
25867 cp_lexer_rollback_tokens (parser->lexer);
25868 return false;
25871 /* Parse an Objective-C method prototype list. */
25873 static void
25874 cp_parser_objc_method_prototype_list (cp_parser* parser)
25876 cp_token *token = cp_lexer_peek_token (parser->lexer);
25878 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25880 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25882 tree attributes, sig;
25883 bool is_class_method;
25884 if (token->type == CPP_PLUS)
25885 is_class_method = true;
25886 else
25887 is_class_method = false;
25888 sig = cp_parser_objc_method_signature (parser, &attributes);
25889 if (sig == error_mark_node)
25891 cp_parser_skip_to_end_of_block_or_statement (parser);
25892 token = cp_lexer_peek_token (parser->lexer);
25893 continue;
25895 objc_add_method_declaration (is_class_method, sig, attributes);
25896 cp_parser_consume_semicolon_at_end_of_statement (parser);
25898 else if (token->keyword == RID_AT_PROPERTY)
25899 cp_parser_objc_at_property_declaration (parser);
25900 else if (token->keyword == RID_ATTRIBUTE
25901 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25902 warning_at (cp_lexer_peek_token (parser->lexer)->location,
25903 OPT_Wattributes,
25904 "prefix attributes are ignored for methods");
25905 else
25906 /* Allow for interspersed non-ObjC++ code. */
25907 cp_parser_objc_interstitial_code (parser);
25909 token = cp_lexer_peek_token (parser->lexer);
25912 if (token->type != CPP_EOF)
25913 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25914 else
25915 cp_parser_error (parser, "expected %<@end%>");
25917 objc_finish_interface ();
25920 /* Parse an Objective-C method definition list. */
25922 static void
25923 cp_parser_objc_method_definition_list (cp_parser* parser)
25925 cp_token *token = cp_lexer_peek_token (parser->lexer);
25927 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
25929 tree meth;
25931 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
25933 cp_token *ptk;
25934 tree sig, attribute;
25935 bool is_class_method;
25936 if (token->type == CPP_PLUS)
25937 is_class_method = true;
25938 else
25939 is_class_method = false;
25940 push_deferring_access_checks (dk_deferred);
25941 sig = cp_parser_objc_method_signature (parser, &attribute);
25942 if (sig == error_mark_node)
25944 cp_parser_skip_to_end_of_block_or_statement (parser);
25945 token = cp_lexer_peek_token (parser->lexer);
25946 continue;
25948 objc_start_method_definition (is_class_method, sig, attribute,
25949 NULL_TREE);
25951 /* For historical reasons, we accept an optional semicolon. */
25952 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25953 cp_lexer_consume_token (parser->lexer);
25955 ptk = cp_lexer_peek_token (parser->lexer);
25956 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
25957 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
25959 perform_deferred_access_checks (tf_warning_or_error);
25960 stop_deferring_access_checks ();
25961 meth = cp_parser_function_definition_after_declarator (parser,
25962 false);
25963 pop_deferring_access_checks ();
25964 objc_finish_method_definition (meth);
25967 /* The following case will be removed once @synthesize is
25968 completely implemented. */
25969 else if (token->keyword == RID_AT_PROPERTY)
25970 cp_parser_objc_at_property_declaration (parser);
25971 else if (token->keyword == RID_AT_SYNTHESIZE)
25972 cp_parser_objc_at_synthesize_declaration (parser);
25973 else if (token->keyword == RID_AT_DYNAMIC)
25974 cp_parser_objc_at_dynamic_declaration (parser);
25975 else if (token->keyword == RID_ATTRIBUTE
25976 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
25977 warning_at (token->location, OPT_Wattributes,
25978 "prefix attributes are ignored for methods");
25979 else
25980 /* Allow for interspersed non-ObjC++ code. */
25981 cp_parser_objc_interstitial_code (parser);
25983 token = cp_lexer_peek_token (parser->lexer);
25986 if (token->type != CPP_EOF)
25987 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
25988 else
25989 cp_parser_error (parser, "expected %<@end%>");
25991 objc_finish_implementation ();
25994 /* Parse Objective-C ivars. */
25996 static void
25997 cp_parser_objc_class_ivars (cp_parser* parser)
25999 cp_token *token = cp_lexer_peek_token (parser->lexer);
26001 if (token->type != CPP_OPEN_BRACE)
26002 return; /* No ivars specified. */
26004 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26005 token = cp_lexer_peek_token (parser->lexer);
26007 while (token->type != CPP_CLOSE_BRACE
26008 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26010 cp_decl_specifier_seq declspecs;
26011 int decl_class_or_enum_p;
26012 tree prefix_attributes;
26014 cp_parser_objc_visibility_spec (parser);
26016 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26017 break;
26019 cp_parser_decl_specifier_seq (parser,
26020 CP_PARSER_FLAGS_OPTIONAL,
26021 &declspecs,
26022 &decl_class_or_enum_p);
26024 /* auto, register, static, extern, mutable. */
26025 if (declspecs.storage_class != sc_none)
26027 cp_parser_error (parser, "invalid type for instance variable");
26028 declspecs.storage_class = sc_none;
26031 /* thread_local. */
26032 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26034 cp_parser_error (parser, "invalid type for instance variable");
26035 declspecs.locations[ds_thread] = 0;
26038 /* typedef. */
26039 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26041 cp_parser_error (parser, "invalid type for instance variable");
26042 declspecs.locations[ds_typedef] = 0;
26045 prefix_attributes = declspecs.attributes;
26046 declspecs.attributes = NULL_TREE;
26048 /* Keep going until we hit the `;' at the end of the
26049 declaration. */
26050 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26052 tree width = NULL_TREE, attributes, first_attribute, decl;
26053 cp_declarator *declarator = NULL;
26054 int ctor_dtor_or_conv_p;
26056 /* Check for a (possibly unnamed) bitfield declaration. */
26057 token = cp_lexer_peek_token (parser->lexer);
26058 if (token->type == CPP_COLON)
26059 goto eat_colon;
26061 if (token->type == CPP_NAME
26062 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26063 == CPP_COLON))
26065 /* Get the name of the bitfield. */
26066 declarator = make_id_declarator (NULL_TREE,
26067 cp_parser_identifier (parser),
26068 sfk_none);
26070 eat_colon:
26071 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26072 /* Get the width of the bitfield. */
26073 width
26074 = cp_parser_constant_expression (parser,
26075 /*allow_non_constant=*/false,
26076 NULL);
26078 else
26080 /* Parse the declarator. */
26081 declarator
26082 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26083 &ctor_dtor_or_conv_p,
26084 /*parenthesized_p=*/NULL,
26085 /*member_p=*/false);
26088 /* Look for attributes that apply to the ivar. */
26089 attributes = cp_parser_attributes_opt (parser);
26090 /* Remember which attributes are prefix attributes and
26091 which are not. */
26092 first_attribute = attributes;
26093 /* Combine the attributes. */
26094 attributes = chainon (prefix_attributes, attributes);
26096 if (width)
26097 /* Create the bitfield declaration. */
26098 decl = grokbitfield (declarator, &declspecs,
26099 width,
26100 attributes);
26101 else
26102 decl = grokfield (declarator, &declspecs,
26103 NULL_TREE, /*init_const_expr_p=*/false,
26104 NULL_TREE, attributes);
26106 /* Add the instance variable. */
26107 if (decl != error_mark_node && decl != NULL_TREE)
26108 objc_add_instance_variable (decl);
26110 /* Reset PREFIX_ATTRIBUTES. */
26111 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26112 attributes = TREE_CHAIN (attributes);
26113 if (attributes)
26114 TREE_CHAIN (attributes) = NULL_TREE;
26116 token = cp_lexer_peek_token (parser->lexer);
26118 if (token->type == CPP_COMMA)
26120 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26121 continue;
26123 break;
26126 cp_parser_consume_semicolon_at_end_of_statement (parser);
26127 token = cp_lexer_peek_token (parser->lexer);
26130 if (token->keyword == RID_AT_END)
26131 cp_parser_error (parser, "expected %<}%>");
26133 /* Do not consume the RID_AT_END, so it will be read again as terminating
26134 the @interface of @implementation. */
26135 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26136 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26138 /* For historical reasons, we accept an optional semicolon. */
26139 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26140 cp_lexer_consume_token (parser->lexer);
26143 /* Parse an Objective-C protocol declaration. */
26145 static void
26146 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26148 tree proto, protorefs;
26149 cp_token *tok;
26151 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26154 tok = cp_lexer_peek_token (parser->lexer);
26155 error_at (tok->location, "identifier expected after %<@protocol%>");
26156 cp_parser_consume_semicolon_at_end_of_statement (parser);
26157 return;
26160 /* See if we have a forward declaration or a definition. */
26161 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26163 /* Try a forward declaration first. */
26164 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26166 while (true)
26168 tree id;
26170 id = cp_parser_identifier (parser);
26171 if (id == error_mark_node)
26172 break;
26174 objc_declare_protocol (id, attributes);
26176 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26177 cp_lexer_consume_token (parser->lexer);
26178 else
26179 break;
26181 cp_parser_consume_semicolon_at_end_of_statement (parser);
26184 /* Ok, we got a full-fledged definition (or at least should). */
26185 else
26187 proto = cp_parser_identifier (parser);
26188 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26189 objc_start_protocol (proto, protorefs, attributes);
26190 cp_parser_objc_method_prototype_list (parser);
26194 /* Parse an Objective-C superclass or category. */
26196 static void
26197 cp_parser_objc_superclass_or_category (cp_parser *parser,
26198 bool iface_p,
26199 tree *super,
26200 tree *categ, bool *is_class_extension)
26202 cp_token *next = cp_lexer_peek_token (parser->lexer);
26204 *super = *categ = NULL_TREE;
26205 *is_class_extension = false;
26206 if (next->type == CPP_COLON)
26208 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26209 *super = cp_parser_identifier (parser);
26211 else if (next->type == CPP_OPEN_PAREN)
26213 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26215 /* If there is no category name, and this is an @interface, we
26216 have a class extension. */
26217 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26219 *categ = NULL_TREE;
26220 *is_class_extension = true;
26222 else
26223 *categ = cp_parser_identifier (parser);
26225 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26229 /* Parse an Objective-C class interface. */
26231 static void
26232 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26234 tree name, super, categ, protos;
26235 bool is_class_extension;
26237 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26238 name = cp_parser_identifier (parser);
26239 if (name == error_mark_node)
26241 /* It's hard to recover because even if valid @interface stuff
26242 is to follow, we can't compile it (or validate it) if we
26243 don't even know which class it refers to. Let's assume this
26244 was a stray '@interface' token in the stream and skip it.
26246 return;
26248 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26249 &is_class_extension);
26250 protos = cp_parser_objc_protocol_refs_opt (parser);
26252 /* We have either a class or a category on our hands. */
26253 if (categ || is_class_extension)
26254 objc_start_category_interface (name, categ, protos, attributes);
26255 else
26257 objc_start_class_interface (name, super, protos, attributes);
26258 /* Handle instance variable declarations, if any. */
26259 cp_parser_objc_class_ivars (parser);
26260 objc_continue_interface ();
26263 cp_parser_objc_method_prototype_list (parser);
26266 /* Parse an Objective-C class implementation. */
26268 static void
26269 cp_parser_objc_class_implementation (cp_parser* parser)
26271 tree name, super, categ;
26272 bool is_class_extension;
26274 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26275 name = cp_parser_identifier (parser);
26276 if (name == error_mark_node)
26278 /* It's hard to recover because even if valid @implementation
26279 stuff is to follow, we can't compile it (or validate it) if
26280 we don't even know which class it refers to. Let's assume
26281 this was a stray '@implementation' token in the stream and
26282 skip it.
26284 return;
26286 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26287 &is_class_extension);
26289 /* We have either a class or a category on our hands. */
26290 if (categ)
26291 objc_start_category_implementation (name, categ);
26292 else
26294 objc_start_class_implementation (name, super);
26295 /* Handle instance variable declarations, if any. */
26296 cp_parser_objc_class_ivars (parser);
26297 objc_continue_implementation ();
26300 cp_parser_objc_method_definition_list (parser);
26303 /* Consume the @end token and finish off the implementation. */
26305 static void
26306 cp_parser_objc_end_implementation (cp_parser* parser)
26308 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26309 objc_finish_implementation ();
26312 /* Parse an Objective-C declaration. */
26314 static void
26315 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26317 /* Try to figure out what kind of declaration is present. */
26318 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26320 if (attributes)
26321 switch (kwd->keyword)
26323 case RID_AT_ALIAS:
26324 case RID_AT_CLASS:
26325 case RID_AT_END:
26326 error_at (kwd->location, "attributes may not be specified before"
26327 " the %<@%D%> Objective-C++ keyword",
26328 kwd->u.value);
26329 attributes = NULL;
26330 break;
26331 case RID_AT_IMPLEMENTATION:
26332 warning_at (kwd->location, OPT_Wattributes,
26333 "prefix attributes are ignored before %<@%D%>",
26334 kwd->u.value);
26335 attributes = NULL;
26336 default:
26337 break;
26340 switch (kwd->keyword)
26342 case RID_AT_ALIAS:
26343 cp_parser_objc_alias_declaration (parser);
26344 break;
26345 case RID_AT_CLASS:
26346 cp_parser_objc_class_declaration (parser);
26347 break;
26348 case RID_AT_PROTOCOL:
26349 cp_parser_objc_protocol_declaration (parser, attributes);
26350 break;
26351 case RID_AT_INTERFACE:
26352 cp_parser_objc_class_interface (parser, attributes);
26353 break;
26354 case RID_AT_IMPLEMENTATION:
26355 cp_parser_objc_class_implementation (parser);
26356 break;
26357 case RID_AT_END:
26358 cp_parser_objc_end_implementation (parser);
26359 break;
26360 default:
26361 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26362 kwd->u.value);
26363 cp_parser_skip_to_end_of_block_or_statement (parser);
26367 /* Parse an Objective-C try-catch-finally statement.
26369 objc-try-catch-finally-stmt:
26370 @try compound-statement objc-catch-clause-seq [opt]
26371 objc-finally-clause [opt]
26373 objc-catch-clause-seq:
26374 objc-catch-clause objc-catch-clause-seq [opt]
26376 objc-catch-clause:
26377 @catch ( objc-exception-declaration ) compound-statement
26379 objc-finally-clause:
26380 @finally compound-statement
26382 objc-exception-declaration:
26383 parameter-declaration
26384 '...'
26386 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26388 Returns NULL_TREE.
26390 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26391 for C. Keep them in sync. */
26393 static tree
26394 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26396 location_t location;
26397 tree stmt;
26399 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26400 location = cp_lexer_peek_token (parser->lexer)->location;
26401 objc_maybe_warn_exceptions (location);
26402 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26403 node, lest it get absorbed into the surrounding block. */
26404 stmt = push_stmt_list ();
26405 cp_parser_compound_statement (parser, NULL, false, false);
26406 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26408 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26410 cp_parameter_declarator *parm;
26411 tree parameter_declaration = error_mark_node;
26412 bool seen_open_paren = false;
26414 cp_lexer_consume_token (parser->lexer);
26415 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26416 seen_open_paren = true;
26417 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26419 /* We have "@catch (...)" (where the '...' are literally
26420 what is in the code). Skip the '...'.
26421 parameter_declaration is set to NULL_TREE, and
26422 objc_being_catch_clauses() knows that that means
26423 '...'. */
26424 cp_lexer_consume_token (parser->lexer);
26425 parameter_declaration = NULL_TREE;
26427 else
26429 /* We have "@catch (NSException *exception)" or something
26430 like that. Parse the parameter declaration. */
26431 parm = cp_parser_parameter_declaration (parser, false, NULL);
26432 if (parm == NULL)
26433 parameter_declaration = error_mark_node;
26434 else
26435 parameter_declaration = grokdeclarator (parm->declarator,
26436 &parm->decl_specifiers,
26437 PARM, /*initialized=*/0,
26438 /*attrlist=*/NULL);
26440 if (seen_open_paren)
26441 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26442 else
26444 /* If there was no open parenthesis, we are recovering from
26445 an error, and we are trying to figure out what mistake
26446 the user has made. */
26448 /* If there is an immediate closing parenthesis, the user
26449 probably forgot the opening one (ie, they typed "@catch
26450 NSException *e)". Parse the closing parenthesis and keep
26451 going. */
26452 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26453 cp_lexer_consume_token (parser->lexer);
26455 /* If these is no immediate closing parenthesis, the user
26456 probably doesn't know that parenthesis are required at
26457 all (ie, they typed "@catch NSException *e"). So, just
26458 forget about the closing parenthesis and keep going. */
26460 objc_begin_catch_clause (parameter_declaration);
26461 cp_parser_compound_statement (parser, NULL, false, false);
26462 objc_finish_catch_clause ();
26464 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26466 cp_lexer_consume_token (parser->lexer);
26467 location = cp_lexer_peek_token (parser->lexer)->location;
26468 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26469 node, lest it get absorbed into the surrounding block. */
26470 stmt = push_stmt_list ();
26471 cp_parser_compound_statement (parser, NULL, false, false);
26472 objc_build_finally_clause (location, pop_stmt_list (stmt));
26475 return objc_finish_try_stmt ();
26478 /* Parse an Objective-C synchronized statement.
26480 objc-synchronized-stmt:
26481 @synchronized ( expression ) compound-statement
26483 Returns NULL_TREE. */
26485 static tree
26486 cp_parser_objc_synchronized_statement (cp_parser *parser)
26488 location_t location;
26489 tree lock, stmt;
26491 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26493 location = cp_lexer_peek_token (parser->lexer)->location;
26494 objc_maybe_warn_exceptions (location);
26495 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26496 lock = cp_parser_expression (parser, false, NULL);
26497 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26499 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26500 node, lest it get absorbed into the surrounding block. */
26501 stmt = push_stmt_list ();
26502 cp_parser_compound_statement (parser, NULL, false, false);
26504 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26507 /* Parse an Objective-C throw statement.
26509 objc-throw-stmt:
26510 @throw assignment-expression [opt] ;
26512 Returns a constructed '@throw' statement. */
26514 static tree
26515 cp_parser_objc_throw_statement (cp_parser *parser)
26517 tree expr = NULL_TREE;
26518 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26520 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26522 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26523 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26525 cp_parser_consume_semicolon_at_end_of_statement (parser);
26527 return objc_build_throw_stmt (loc, expr);
26530 /* Parse an Objective-C statement. */
26532 static tree
26533 cp_parser_objc_statement (cp_parser * parser)
26535 /* Try to figure out what kind of declaration is present. */
26536 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26538 switch (kwd->keyword)
26540 case RID_AT_TRY:
26541 return cp_parser_objc_try_catch_finally_statement (parser);
26542 case RID_AT_SYNCHRONIZED:
26543 return cp_parser_objc_synchronized_statement (parser);
26544 case RID_AT_THROW:
26545 return cp_parser_objc_throw_statement (parser);
26546 default:
26547 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26548 kwd->u.value);
26549 cp_parser_skip_to_end_of_block_or_statement (parser);
26552 return error_mark_node;
26555 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26556 look ahead to see if an objc keyword follows the attributes. This
26557 is to detect the use of prefix attributes on ObjC @interface and
26558 @protocol. */
26560 static bool
26561 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26563 cp_lexer_save_tokens (parser->lexer);
26564 *attrib = cp_parser_attributes_opt (parser);
26565 gcc_assert (*attrib);
26566 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26568 cp_lexer_commit_tokens (parser->lexer);
26569 return true;
26571 cp_lexer_rollback_tokens (parser->lexer);
26572 return false;
26575 /* This routine is a minimal replacement for
26576 c_parser_struct_declaration () used when parsing the list of
26577 types/names or ObjC++ properties. For example, when parsing the
26578 code
26580 @property (readonly) int a, b, c;
26582 this function is responsible for parsing "int a, int b, int c" and
26583 returning the declarations as CHAIN of DECLs.
26585 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26586 similar parsing. */
26587 static tree
26588 cp_parser_objc_struct_declaration (cp_parser *parser)
26590 tree decls = NULL_TREE;
26591 cp_decl_specifier_seq declspecs;
26592 int decl_class_or_enum_p;
26593 tree prefix_attributes;
26595 cp_parser_decl_specifier_seq (parser,
26596 CP_PARSER_FLAGS_NONE,
26597 &declspecs,
26598 &decl_class_or_enum_p);
26600 if (declspecs.type == error_mark_node)
26601 return error_mark_node;
26603 /* auto, register, static, extern, mutable. */
26604 if (declspecs.storage_class != sc_none)
26606 cp_parser_error (parser, "invalid type for property");
26607 declspecs.storage_class = sc_none;
26610 /* thread_local. */
26611 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26613 cp_parser_error (parser, "invalid type for property");
26614 declspecs.locations[ds_thread] = 0;
26617 /* typedef. */
26618 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26620 cp_parser_error (parser, "invalid type for property");
26621 declspecs.locations[ds_typedef] = 0;
26624 prefix_attributes = declspecs.attributes;
26625 declspecs.attributes = NULL_TREE;
26627 /* Keep going until we hit the `;' at the end of the declaration. */
26628 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26630 tree attributes, first_attribute, decl;
26631 cp_declarator *declarator;
26632 cp_token *token;
26634 /* Parse the declarator. */
26635 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26636 NULL, NULL, false);
26638 /* Look for attributes that apply to the ivar. */
26639 attributes = cp_parser_attributes_opt (parser);
26640 /* Remember which attributes are prefix attributes and
26641 which are not. */
26642 first_attribute = attributes;
26643 /* Combine the attributes. */
26644 attributes = chainon (prefix_attributes, attributes);
26646 decl = grokfield (declarator, &declspecs,
26647 NULL_TREE, /*init_const_expr_p=*/false,
26648 NULL_TREE, attributes);
26650 if (decl == error_mark_node || decl == NULL_TREE)
26651 return error_mark_node;
26653 /* Reset PREFIX_ATTRIBUTES. */
26654 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26655 attributes = TREE_CHAIN (attributes);
26656 if (attributes)
26657 TREE_CHAIN (attributes) = NULL_TREE;
26659 DECL_CHAIN (decl) = decls;
26660 decls = decl;
26662 token = cp_lexer_peek_token (parser->lexer);
26663 if (token->type == CPP_COMMA)
26665 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26666 continue;
26668 else
26669 break;
26671 return decls;
26674 /* Parse an Objective-C @property declaration. The syntax is:
26676 objc-property-declaration:
26677 '@property' objc-property-attributes[opt] struct-declaration ;
26679 objc-property-attributes:
26680 '(' objc-property-attribute-list ')'
26682 objc-property-attribute-list:
26683 objc-property-attribute
26684 objc-property-attribute-list, objc-property-attribute
26686 objc-property-attribute
26687 'getter' = identifier
26688 'setter' = identifier
26689 'readonly'
26690 'readwrite'
26691 'assign'
26692 'retain'
26693 'copy'
26694 'nonatomic'
26696 For example:
26697 @property NSString *name;
26698 @property (readonly) id object;
26699 @property (retain, nonatomic, getter=getTheName) id name;
26700 @property int a, b, c;
26702 PS: This function is identical to
26703 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26704 static void
26705 cp_parser_objc_at_property_declaration (cp_parser *parser)
26707 /* The following variables hold the attributes of the properties as
26708 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26709 seen. When we see an attribute, we set them to 'true' (if they
26710 are boolean properties) or to the identifier (if they have an
26711 argument, ie, for getter and setter). Note that here we only
26712 parse the list of attributes, check the syntax and accumulate the
26713 attributes that we find. objc_add_property_declaration() will
26714 then process the information. */
26715 bool property_assign = false;
26716 bool property_copy = false;
26717 tree property_getter_ident = NULL_TREE;
26718 bool property_nonatomic = false;
26719 bool property_readonly = false;
26720 bool property_readwrite = false;
26721 bool property_retain = false;
26722 tree property_setter_ident = NULL_TREE;
26724 /* 'properties' is the list of properties that we read. Usually a
26725 single one, but maybe more (eg, in "@property int a, b, c;" there
26726 are three). */
26727 tree properties;
26728 location_t loc;
26730 loc = cp_lexer_peek_token (parser->lexer)->location;
26732 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
26734 /* Parse the optional attribute list... */
26735 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26737 /* Eat the '('. */
26738 cp_lexer_consume_token (parser->lexer);
26740 while (true)
26742 bool syntax_error = false;
26743 cp_token *token = cp_lexer_peek_token (parser->lexer);
26744 enum rid keyword;
26746 if (token->type != CPP_NAME)
26748 cp_parser_error (parser, "expected identifier");
26749 break;
26751 keyword = C_RID_CODE (token->u.value);
26752 cp_lexer_consume_token (parser->lexer);
26753 switch (keyword)
26755 case RID_ASSIGN: property_assign = true; break;
26756 case RID_COPY: property_copy = true; break;
26757 case RID_NONATOMIC: property_nonatomic = true; break;
26758 case RID_READONLY: property_readonly = true; break;
26759 case RID_READWRITE: property_readwrite = true; break;
26760 case RID_RETAIN: property_retain = true; break;
26762 case RID_GETTER:
26763 case RID_SETTER:
26764 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26766 if (keyword == RID_GETTER)
26767 cp_parser_error (parser,
26768 "missing %<=%> (after %<getter%> attribute)");
26769 else
26770 cp_parser_error (parser,
26771 "missing %<=%> (after %<setter%> attribute)");
26772 syntax_error = true;
26773 break;
26775 cp_lexer_consume_token (parser->lexer); /* eat the = */
26776 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
26778 cp_parser_error (parser, "expected identifier");
26779 syntax_error = true;
26780 break;
26782 if (keyword == RID_SETTER)
26784 if (property_setter_ident != NULL_TREE)
26786 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
26787 cp_lexer_consume_token (parser->lexer);
26789 else
26790 property_setter_ident = cp_parser_objc_selector (parser);
26791 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
26792 cp_parser_error (parser, "setter name must terminate with %<:%>");
26793 else
26794 cp_lexer_consume_token (parser->lexer);
26796 else
26798 if (property_getter_ident != NULL_TREE)
26800 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
26801 cp_lexer_consume_token (parser->lexer);
26803 else
26804 property_getter_ident = cp_parser_objc_selector (parser);
26806 break;
26807 default:
26808 cp_parser_error (parser, "unknown property attribute");
26809 syntax_error = true;
26810 break;
26813 if (syntax_error)
26814 break;
26816 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26817 cp_lexer_consume_token (parser->lexer);
26818 else
26819 break;
26822 /* FIXME: "@property (setter, assign);" will generate a spurious
26823 "error: expected ‘)’ before ‘,’ token". This is because
26824 cp_parser_require, unlike the C counterpart, will produce an
26825 error even if we are in error recovery. */
26826 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26828 cp_parser_skip_to_closing_parenthesis (parser,
26829 /*recovering=*/true,
26830 /*or_comma=*/false,
26831 /*consume_paren=*/true);
26835 /* ... and the property declaration(s). */
26836 properties = cp_parser_objc_struct_declaration (parser);
26838 if (properties == error_mark_node)
26840 cp_parser_skip_to_end_of_statement (parser);
26841 /* If the next token is now a `;', consume it. */
26842 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26843 cp_lexer_consume_token (parser->lexer);
26844 return;
26847 if (properties == NULL_TREE)
26848 cp_parser_error (parser, "expected identifier");
26849 else
26851 /* Comma-separated properties are chained together in
26852 reverse order; add them one by one. */
26853 properties = nreverse (properties);
26855 for (; properties; properties = TREE_CHAIN (properties))
26856 objc_add_property_declaration (loc, copy_node (properties),
26857 property_readonly, property_readwrite,
26858 property_assign, property_retain,
26859 property_copy, property_nonatomic,
26860 property_getter_ident, property_setter_ident);
26863 cp_parser_consume_semicolon_at_end_of_statement (parser);
26866 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
26868 objc-synthesize-declaration:
26869 @synthesize objc-synthesize-identifier-list ;
26871 objc-synthesize-identifier-list:
26872 objc-synthesize-identifier
26873 objc-synthesize-identifier-list, objc-synthesize-identifier
26875 objc-synthesize-identifier
26876 identifier
26877 identifier = identifier
26879 For example:
26880 @synthesize MyProperty;
26881 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
26883 PS: This function is identical to c_parser_objc_at_synthesize_declaration
26884 for C. Keep them in sync.
26886 static void
26887 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
26889 tree list = NULL_TREE;
26890 location_t loc;
26891 loc = cp_lexer_peek_token (parser->lexer)->location;
26893 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
26894 while (true)
26896 tree property, ivar;
26897 property = cp_parser_identifier (parser);
26898 if (property == error_mark_node)
26900 cp_parser_consume_semicolon_at_end_of_statement (parser);
26901 return;
26903 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
26905 cp_lexer_consume_token (parser->lexer);
26906 ivar = cp_parser_identifier (parser);
26907 if (ivar == error_mark_node)
26909 cp_parser_consume_semicolon_at_end_of_statement (parser);
26910 return;
26913 else
26914 ivar = NULL_TREE;
26915 list = chainon (list, build_tree_list (ivar, property));
26916 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26917 cp_lexer_consume_token (parser->lexer);
26918 else
26919 break;
26921 cp_parser_consume_semicolon_at_end_of_statement (parser);
26922 objc_add_synthesize_declaration (loc, list);
26925 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
26927 objc-dynamic-declaration:
26928 @dynamic identifier-list ;
26930 For example:
26931 @dynamic MyProperty;
26932 @dynamic MyProperty, AnotherProperty;
26934 PS: This function is identical to c_parser_objc_at_dynamic_declaration
26935 for C. Keep them in sync.
26937 static void
26938 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
26940 tree list = NULL_TREE;
26941 location_t loc;
26942 loc = cp_lexer_peek_token (parser->lexer)->location;
26944 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
26945 while (true)
26947 tree property;
26948 property = cp_parser_identifier (parser);
26949 if (property == error_mark_node)
26951 cp_parser_consume_semicolon_at_end_of_statement (parser);
26952 return;
26954 list = chainon (list, build_tree_list (NULL, property));
26955 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26956 cp_lexer_consume_token (parser->lexer);
26957 else
26958 break;
26960 cp_parser_consume_semicolon_at_end_of_statement (parser);
26961 objc_add_dynamic_declaration (loc, list);
26965 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
26967 /* Returns name of the next clause.
26968 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
26969 the token is not consumed. Otherwise appropriate pragma_omp_clause is
26970 returned and the token is consumed. */
26972 static pragma_omp_clause
26973 cp_parser_omp_clause_name (cp_parser *parser)
26975 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
26977 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
26978 result = PRAGMA_OMP_CLAUSE_IF;
26979 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
26980 result = PRAGMA_OMP_CLAUSE_DEFAULT;
26981 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
26982 result = PRAGMA_OMP_CLAUSE_PRIVATE;
26983 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26984 result = PRAGMA_OMP_CLAUSE_FOR;
26985 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26987 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26988 const char *p = IDENTIFIER_POINTER (id);
26990 switch (p[0])
26992 case 'a':
26993 if (!strcmp ("aligned", p))
26994 result = PRAGMA_OMP_CLAUSE_ALIGNED;
26995 break;
26996 case 'c':
26997 if (!strcmp ("collapse", p))
26998 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
26999 else if (!strcmp ("copyin", p))
27000 result = PRAGMA_OMP_CLAUSE_COPYIN;
27001 else if (!strcmp ("copyprivate", p))
27002 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27003 break;
27004 case 'd':
27005 if (!strcmp ("depend", p))
27006 result = PRAGMA_OMP_CLAUSE_DEPEND;
27007 else if (!strcmp ("device", p))
27008 result = PRAGMA_OMP_CLAUSE_DEVICE;
27009 else if (!strcmp ("dist_schedule", p))
27010 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27011 break;
27012 case 'f':
27013 if (!strcmp ("final", p))
27014 result = PRAGMA_OMP_CLAUSE_FINAL;
27015 else if (!strcmp ("firstprivate", p))
27016 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27017 else if (!strcmp ("from", p))
27018 result = PRAGMA_OMP_CLAUSE_FROM;
27019 break;
27020 case 'i':
27021 if (!strcmp ("inbranch", p))
27022 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27023 break;
27024 case 'l':
27025 if (!strcmp ("lastprivate", p))
27026 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27027 else if (!strcmp ("linear", p))
27028 result = PRAGMA_OMP_CLAUSE_LINEAR;
27029 break;
27030 case 'm':
27031 if (!strcmp ("map", p))
27032 result = PRAGMA_OMP_CLAUSE_MAP;
27033 else if (!strcmp ("mergeable", p))
27034 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27035 else if (flag_cilkplus && !strcmp ("mask", p))
27036 result = PRAGMA_CILK_CLAUSE_MASK;
27037 break;
27038 case 'n':
27039 if (!strcmp ("notinbranch", p))
27040 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27041 else if (!strcmp ("nowait", p))
27042 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27043 else if (flag_cilkplus && !strcmp ("nomask", p))
27044 result = PRAGMA_CILK_CLAUSE_NOMASK;
27045 else if (!strcmp ("num_teams", p))
27046 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27047 else if (!strcmp ("num_threads", p))
27048 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27049 break;
27050 case 'o':
27051 if (!strcmp ("ordered", p))
27052 result = PRAGMA_OMP_CLAUSE_ORDERED;
27053 break;
27054 case 'p':
27055 if (!strcmp ("parallel", p))
27056 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27057 else if (!strcmp ("proc_bind", p))
27058 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27059 break;
27060 case 'r':
27061 if (!strcmp ("reduction", p))
27062 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27063 break;
27064 case 's':
27065 if (!strcmp ("safelen", p))
27066 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27067 else if (!strcmp ("schedule", p))
27068 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27069 else if (!strcmp ("sections", p))
27070 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27071 else if (!strcmp ("shared", p))
27072 result = PRAGMA_OMP_CLAUSE_SHARED;
27073 else if (!strcmp ("simdlen", p))
27074 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27075 break;
27076 case 't':
27077 if (!strcmp ("taskgroup", p))
27078 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27079 else if (!strcmp ("thread_limit", p))
27080 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27081 else if (!strcmp ("to", p))
27082 result = PRAGMA_OMP_CLAUSE_TO;
27083 break;
27084 case 'u':
27085 if (!strcmp ("uniform", p))
27086 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27087 else if (!strcmp ("untied", p))
27088 result = PRAGMA_OMP_CLAUSE_UNTIED;
27089 break;
27090 case 'v':
27091 if (flag_cilkplus && !strcmp ("vectorlength", p))
27092 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27093 break;
27097 if (result != PRAGMA_OMP_CLAUSE_NONE)
27098 cp_lexer_consume_token (parser->lexer);
27100 return result;
27103 /* Validate that a clause of the given type does not already exist. */
27105 static void
27106 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27107 const char *name, location_t location)
27109 tree c;
27111 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27112 if (OMP_CLAUSE_CODE (c) == code)
27114 error_at (location, "too many %qs clauses", name);
27115 break;
27119 /* OpenMP 2.5:
27120 variable-list:
27121 identifier
27122 variable-list , identifier
27124 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27125 colon). An opening parenthesis will have been consumed by the caller.
27127 If KIND is nonzero, create the appropriate node and install the decl
27128 in OMP_CLAUSE_DECL and add the node to the head of the list.
27130 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27131 return the list created.
27133 COLON can be NULL if only closing parenthesis should end the list,
27134 or pointer to bool which will receive false if the list is terminated
27135 by closing parenthesis or true if the list is terminated by colon. */
27137 static tree
27138 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27139 tree list, bool *colon)
27141 cp_token *token;
27142 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27143 if (colon)
27145 parser->colon_corrects_to_scope_p = false;
27146 *colon = false;
27148 while (1)
27150 tree name, decl;
27152 token = cp_lexer_peek_token (parser->lexer);
27153 name = cp_parser_id_expression (parser, /*template_p=*/false,
27154 /*check_dependency_p=*/true,
27155 /*template_p=*/NULL,
27156 /*declarator_p=*/false,
27157 /*optional_p=*/false);
27158 if (name == error_mark_node)
27159 goto skip_comma;
27161 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27162 if (decl == error_mark_node)
27163 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27164 token->location);
27165 else if (kind != 0)
27167 switch (kind)
27169 case OMP_CLAUSE_MAP:
27170 case OMP_CLAUSE_FROM:
27171 case OMP_CLAUSE_TO:
27172 case OMP_CLAUSE_DEPEND:
27173 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27175 tree low_bound = NULL_TREE, length = NULL_TREE;
27177 parser->colon_corrects_to_scope_p = false;
27178 cp_lexer_consume_token (parser->lexer);
27179 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27180 low_bound = cp_parser_expression (parser, /*cast_p=*/false,
27181 NULL);
27182 if (!colon)
27183 parser->colon_corrects_to_scope_p
27184 = saved_colon_corrects_to_scope_p;
27185 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27186 length = integer_one_node;
27187 else
27189 /* Look for `:'. */
27190 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27191 goto skip_comma;
27192 if (!cp_lexer_next_token_is (parser->lexer,
27193 CPP_CLOSE_SQUARE))
27194 length = cp_parser_expression (parser,
27195 /*cast_p=*/false,
27196 NULL);
27198 /* Look for the closing `]'. */
27199 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27200 RT_CLOSE_SQUARE))
27201 goto skip_comma;
27202 decl = tree_cons (low_bound, length, decl);
27204 break;
27205 default:
27206 break;
27209 tree u = build_omp_clause (token->location, kind);
27210 OMP_CLAUSE_DECL (u) = decl;
27211 OMP_CLAUSE_CHAIN (u) = list;
27212 list = u;
27214 else
27215 list = tree_cons (decl, NULL_TREE, list);
27217 get_comma:
27218 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27219 break;
27220 cp_lexer_consume_token (parser->lexer);
27223 if (colon)
27224 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27226 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27228 *colon = true;
27229 cp_parser_require (parser, CPP_COLON, RT_COLON);
27230 return list;
27233 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27235 int ending;
27237 /* Try to resync to an unnested comma. Copied from
27238 cp_parser_parenthesized_expression_list. */
27239 skip_comma:
27240 if (colon)
27241 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27242 ending = cp_parser_skip_to_closing_parenthesis (parser,
27243 /*recovering=*/true,
27244 /*or_comma=*/true,
27245 /*consume_paren=*/true);
27246 if (ending < 0)
27247 goto get_comma;
27250 return list;
27253 /* Similarly, but expect leading and trailing parenthesis. This is a very
27254 common case for omp clauses. */
27256 static tree
27257 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27259 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27260 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27261 return list;
27264 /* OpenMP 3.0:
27265 collapse ( constant-expression ) */
27267 static tree
27268 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27270 tree c, num;
27271 location_t loc;
27272 HOST_WIDE_INT n;
27274 loc = cp_lexer_peek_token (parser->lexer)->location;
27275 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27276 return list;
27278 num = cp_parser_constant_expression (parser, false, NULL);
27280 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27281 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27282 /*or_comma=*/false,
27283 /*consume_paren=*/true);
27285 if (num == error_mark_node)
27286 return list;
27287 num = fold_non_dependent_expr (num);
27288 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27289 || !tree_fits_shwi_p (num)
27290 || (n = tree_to_shwi (num)) <= 0
27291 || (int) n != n)
27293 error_at (loc, "collapse argument needs positive constant integer expression");
27294 return list;
27297 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27298 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27299 OMP_CLAUSE_CHAIN (c) = list;
27300 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27302 return c;
27305 /* OpenMP 2.5:
27306 default ( shared | none ) */
27308 static tree
27309 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27311 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27312 tree c;
27314 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27315 return list;
27316 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27318 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27319 const char *p = IDENTIFIER_POINTER (id);
27321 switch (p[0])
27323 case 'n':
27324 if (strcmp ("none", p) != 0)
27325 goto invalid_kind;
27326 kind = OMP_CLAUSE_DEFAULT_NONE;
27327 break;
27329 case 's':
27330 if (strcmp ("shared", p) != 0)
27331 goto invalid_kind;
27332 kind = OMP_CLAUSE_DEFAULT_SHARED;
27333 break;
27335 default:
27336 goto invalid_kind;
27339 cp_lexer_consume_token (parser->lexer);
27341 else
27343 invalid_kind:
27344 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27347 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27348 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27349 /*or_comma=*/false,
27350 /*consume_paren=*/true);
27352 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27353 return list;
27355 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27356 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27357 OMP_CLAUSE_CHAIN (c) = list;
27358 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27360 return c;
27363 /* OpenMP 3.1:
27364 final ( expression ) */
27366 static tree
27367 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27369 tree t, c;
27371 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27372 return list;
27374 t = cp_parser_condition (parser);
27376 if (t == error_mark_node
27377 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27378 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27379 /*or_comma=*/false,
27380 /*consume_paren=*/true);
27382 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27384 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27385 OMP_CLAUSE_FINAL_EXPR (c) = t;
27386 OMP_CLAUSE_CHAIN (c) = list;
27388 return c;
27391 /* OpenMP 2.5:
27392 if ( expression ) */
27394 static tree
27395 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27397 tree t, c;
27399 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27400 return list;
27402 t = cp_parser_condition (parser);
27404 if (t == error_mark_node
27405 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27406 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27407 /*or_comma=*/false,
27408 /*consume_paren=*/true);
27410 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27412 c = build_omp_clause (location, OMP_CLAUSE_IF);
27413 OMP_CLAUSE_IF_EXPR (c) = t;
27414 OMP_CLAUSE_CHAIN (c) = list;
27416 return c;
27419 /* OpenMP 3.1:
27420 mergeable */
27422 static tree
27423 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27424 tree list, location_t location)
27426 tree c;
27428 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27429 location);
27431 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27432 OMP_CLAUSE_CHAIN (c) = list;
27433 return c;
27436 /* OpenMP 2.5:
27437 nowait */
27439 static tree
27440 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27441 tree list, location_t location)
27443 tree c;
27445 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27447 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27448 OMP_CLAUSE_CHAIN (c) = list;
27449 return c;
27452 /* OpenMP 2.5:
27453 num_threads ( expression ) */
27455 static tree
27456 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27457 location_t location)
27459 tree t, c;
27461 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27462 return list;
27464 t = cp_parser_expression (parser, false, NULL);
27466 if (t == error_mark_node
27467 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27468 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27469 /*or_comma=*/false,
27470 /*consume_paren=*/true);
27472 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27473 "num_threads", location);
27475 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27476 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27477 OMP_CLAUSE_CHAIN (c) = list;
27479 return c;
27482 /* OpenMP 2.5:
27483 ordered */
27485 static tree
27486 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27487 tree list, location_t location)
27489 tree c;
27491 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27492 "ordered", location);
27494 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27495 OMP_CLAUSE_CHAIN (c) = list;
27496 return c;
27499 /* OpenMP 2.5:
27500 reduction ( reduction-operator : variable-list )
27502 reduction-operator:
27503 One of: + * - & ^ | && ||
27505 OpenMP 3.1:
27507 reduction-operator:
27508 One of: + * - & ^ | && || min max
27510 OpenMP 4.0:
27512 reduction-operator:
27513 One of: + * - & ^ | && ||
27514 id-expression */
27516 static tree
27517 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27519 enum tree_code code = ERROR_MARK;
27520 tree nlist, c, id = NULL_TREE;
27522 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27523 return list;
27525 switch (cp_lexer_peek_token (parser->lexer)->type)
27527 case CPP_PLUS: code = PLUS_EXPR; break;
27528 case CPP_MULT: code = MULT_EXPR; break;
27529 case CPP_MINUS: code = MINUS_EXPR; break;
27530 case CPP_AND: code = BIT_AND_EXPR; break;
27531 case CPP_XOR: code = BIT_XOR_EXPR; break;
27532 case CPP_OR: code = BIT_IOR_EXPR; break;
27533 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27534 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27535 default: break;
27538 if (code != ERROR_MARK)
27539 cp_lexer_consume_token (parser->lexer);
27540 else
27542 bool saved_colon_corrects_to_scope_p;
27543 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27544 parser->colon_corrects_to_scope_p = false;
27545 id = cp_parser_id_expression (parser, /*template_p=*/false,
27546 /*check_dependency_p=*/true,
27547 /*template_p=*/NULL,
27548 /*declarator_p=*/false,
27549 /*optional_p=*/false);
27550 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27551 if (identifier_p (id))
27553 const char *p = IDENTIFIER_POINTER (id);
27555 if (strcmp (p, "min") == 0)
27556 code = MIN_EXPR;
27557 else if (strcmp (p, "max") == 0)
27558 code = MAX_EXPR;
27559 else if (id == ansi_opname (PLUS_EXPR))
27560 code = PLUS_EXPR;
27561 else if (id == ansi_opname (MULT_EXPR))
27562 code = MULT_EXPR;
27563 else if (id == ansi_opname (MINUS_EXPR))
27564 code = MINUS_EXPR;
27565 else if (id == ansi_opname (BIT_AND_EXPR))
27566 code = BIT_AND_EXPR;
27567 else if (id == ansi_opname (BIT_IOR_EXPR))
27568 code = BIT_IOR_EXPR;
27569 else if (id == ansi_opname (BIT_XOR_EXPR))
27570 code = BIT_XOR_EXPR;
27571 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27572 code = TRUTH_ANDIF_EXPR;
27573 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27574 code = TRUTH_ORIF_EXPR;
27575 id = omp_reduction_id (code, id, NULL_TREE);
27576 tree scope = parser->scope;
27577 if (scope)
27578 id = build_qualified_name (NULL_TREE, scope, id, false);
27579 parser->scope = NULL_TREE;
27580 parser->qualifying_scope = NULL_TREE;
27581 parser->object_scope = NULL_TREE;
27583 else
27585 error ("invalid reduction-identifier");
27586 resync_fail:
27587 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27588 /*or_comma=*/false,
27589 /*consume_paren=*/true);
27590 return list;
27594 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27595 goto resync_fail;
27597 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27598 NULL);
27599 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27601 OMP_CLAUSE_REDUCTION_CODE (c) = code;
27602 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27605 return nlist;
27608 /* OpenMP 2.5:
27609 schedule ( schedule-kind )
27610 schedule ( schedule-kind , expression )
27612 schedule-kind:
27613 static | dynamic | guided | runtime | auto */
27615 static tree
27616 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27618 tree c, t;
27620 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27621 return list;
27623 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27625 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27627 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27628 const char *p = IDENTIFIER_POINTER (id);
27630 switch (p[0])
27632 case 'd':
27633 if (strcmp ("dynamic", p) != 0)
27634 goto invalid_kind;
27635 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27636 break;
27638 case 'g':
27639 if (strcmp ("guided", p) != 0)
27640 goto invalid_kind;
27641 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27642 break;
27644 case 'r':
27645 if (strcmp ("runtime", p) != 0)
27646 goto invalid_kind;
27647 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27648 break;
27650 default:
27651 goto invalid_kind;
27654 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27655 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27656 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27657 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27658 else
27659 goto invalid_kind;
27660 cp_lexer_consume_token (parser->lexer);
27662 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27664 cp_token *token;
27665 cp_lexer_consume_token (parser->lexer);
27667 token = cp_lexer_peek_token (parser->lexer);
27668 t = cp_parser_assignment_expression (parser, false, NULL);
27670 if (t == error_mark_node)
27671 goto resync_fail;
27672 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
27673 error_at (token->location, "schedule %<runtime%> does not take "
27674 "a %<chunk_size%> parameter");
27675 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
27676 error_at (token->location, "schedule %<auto%> does not take "
27677 "a %<chunk_size%> parameter");
27678 else
27679 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
27681 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27682 goto resync_fail;
27684 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27685 goto resync_fail;
27687 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
27688 OMP_CLAUSE_CHAIN (c) = list;
27689 return c;
27691 invalid_kind:
27692 cp_parser_error (parser, "invalid schedule kind");
27693 resync_fail:
27694 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27695 /*or_comma=*/false,
27696 /*consume_paren=*/true);
27697 return list;
27700 /* OpenMP 3.0:
27701 untied */
27703 static tree
27704 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
27705 tree list, location_t location)
27707 tree c;
27709 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
27711 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
27712 OMP_CLAUSE_CHAIN (c) = list;
27713 return c;
27716 /* OpenMP 4.0:
27717 inbranch
27718 notinbranch */
27720 static tree
27721 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
27722 tree list, location_t location)
27724 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
27725 tree c = build_omp_clause (location, code);
27726 OMP_CLAUSE_CHAIN (c) = list;
27727 return c;
27730 /* OpenMP 4.0:
27731 parallel
27733 sections
27734 taskgroup */
27736 static tree
27737 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
27738 enum omp_clause_code code,
27739 tree list, location_t location)
27741 tree c = build_omp_clause (location, code);
27742 OMP_CLAUSE_CHAIN (c) = list;
27743 return c;
27746 /* OpenMP 4.0:
27747 num_teams ( expression ) */
27749 static tree
27750 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
27751 location_t location)
27753 tree t, c;
27755 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27756 return list;
27758 t = cp_parser_expression (parser, false, NULL);
27760 if (t == error_mark_node
27761 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27762 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27763 /*or_comma=*/false,
27764 /*consume_paren=*/true);
27766 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
27767 "num_teams", location);
27769 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
27770 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
27771 OMP_CLAUSE_CHAIN (c) = list;
27773 return c;
27776 /* OpenMP 4.0:
27777 thread_limit ( expression ) */
27779 static tree
27780 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
27781 location_t location)
27783 tree t, c;
27785 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27786 return list;
27788 t = cp_parser_expression (parser, false, NULL);
27790 if (t == error_mark_node
27791 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27792 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27793 /*or_comma=*/false,
27794 /*consume_paren=*/true);
27796 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
27797 "thread_limit", location);
27799 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
27800 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
27801 OMP_CLAUSE_CHAIN (c) = list;
27803 return c;
27806 /* OpenMP 4.0:
27807 aligned ( variable-list )
27808 aligned ( variable-list : constant-expression ) */
27810 static tree
27811 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
27813 tree nlist, c, alignment = NULL_TREE;
27814 bool colon;
27816 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27817 return list;
27819 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
27820 &colon);
27822 if (colon)
27824 alignment = cp_parser_constant_expression (parser, false, NULL);
27826 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27827 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27828 /*or_comma=*/false,
27829 /*consume_paren=*/true);
27831 if (alignment == error_mark_node)
27832 alignment = NULL_TREE;
27835 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27836 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
27838 return nlist;
27841 /* OpenMP 4.0:
27842 linear ( variable-list )
27843 linear ( variable-list : expression ) */
27845 static tree
27846 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
27847 bool is_cilk_simd_fn)
27849 tree nlist, c, step = integer_one_node;
27850 bool colon;
27852 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27853 return list;
27855 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
27856 &colon);
27858 if (colon)
27860 step = cp_parser_expression (parser, false, NULL);
27862 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
27864 sorry ("using parameters for %<linear%> step is not supported yet");
27865 step = integer_one_node;
27867 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27868 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27869 /*or_comma=*/false,
27870 /*consume_paren=*/true);
27872 if (step == error_mark_node)
27873 return list;
27876 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27877 OMP_CLAUSE_LINEAR_STEP (c) = step;
27879 return nlist;
27882 /* OpenMP 4.0:
27883 safelen ( constant-expression ) */
27885 static tree
27886 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
27887 location_t location)
27889 tree t, c;
27891 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27892 return list;
27894 t = cp_parser_constant_expression (parser, false, NULL);
27896 if (t == error_mark_node
27897 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27898 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27899 /*or_comma=*/false,
27900 /*consume_paren=*/true);
27902 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
27904 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
27905 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
27906 OMP_CLAUSE_CHAIN (c) = list;
27908 return c;
27911 /* OpenMP 4.0:
27912 simdlen ( constant-expression ) */
27914 static tree
27915 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
27916 location_t location)
27918 tree t, c;
27920 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27921 return list;
27923 t = cp_parser_constant_expression (parser, false, NULL);
27925 if (t == error_mark_node
27926 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27927 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27928 /*or_comma=*/false,
27929 /*consume_paren=*/true);
27931 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
27933 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
27934 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
27935 OMP_CLAUSE_CHAIN (c) = list;
27937 return c;
27940 /* OpenMP 4.0:
27941 depend ( depend-kind : variable-list )
27943 depend-kind:
27944 in | out | inout */
27946 static tree
27947 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
27949 tree nlist, c;
27950 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
27952 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27953 return list;
27955 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27957 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27958 const char *p = IDENTIFIER_POINTER (id);
27960 if (strcmp ("in", p) == 0)
27961 kind = OMP_CLAUSE_DEPEND_IN;
27962 else if (strcmp ("inout", p) == 0)
27963 kind = OMP_CLAUSE_DEPEND_INOUT;
27964 else if (strcmp ("out", p) == 0)
27965 kind = OMP_CLAUSE_DEPEND_OUT;
27966 else
27967 goto invalid_kind;
27969 else
27970 goto invalid_kind;
27972 cp_lexer_consume_token (parser->lexer);
27973 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27974 goto resync_fail;
27976 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
27977 NULL);
27979 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27980 OMP_CLAUSE_DEPEND_KIND (c) = kind;
27982 return nlist;
27984 invalid_kind:
27985 cp_parser_error (parser, "invalid depend kind");
27986 resync_fail:
27987 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27988 /*or_comma=*/false,
27989 /*consume_paren=*/true);
27990 return list;
27993 /* OpenMP 4.0:
27994 map ( map-kind : variable-list )
27995 map ( variable-list )
27997 map-kind:
27998 alloc | to | from | tofrom */
28000 static tree
28001 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28003 tree nlist, c;
28004 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28006 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28007 return list;
28009 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28010 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28012 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28013 const char *p = IDENTIFIER_POINTER (id);
28015 if (strcmp ("alloc", p) == 0)
28016 kind = OMP_CLAUSE_MAP_ALLOC;
28017 else if (strcmp ("to", p) == 0)
28018 kind = OMP_CLAUSE_MAP_TO;
28019 else if (strcmp ("from", p) == 0)
28020 kind = OMP_CLAUSE_MAP_FROM;
28021 else if (strcmp ("tofrom", p) == 0)
28022 kind = OMP_CLAUSE_MAP_TOFROM;
28023 else
28025 cp_parser_error (parser, "invalid map kind");
28026 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28027 /*or_comma=*/false,
28028 /*consume_paren=*/true);
28029 return list;
28031 cp_lexer_consume_token (parser->lexer);
28032 cp_lexer_consume_token (parser->lexer);
28035 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28036 NULL);
28038 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28039 OMP_CLAUSE_MAP_KIND (c) = kind;
28041 return nlist;
28044 /* OpenMP 4.0:
28045 device ( expression ) */
28047 static tree
28048 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28049 location_t location)
28051 tree t, c;
28053 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28054 return list;
28056 t = cp_parser_expression (parser, false, NULL);
28058 if (t == error_mark_node
28059 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28060 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28061 /*or_comma=*/false,
28062 /*consume_paren=*/true);
28064 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28065 "device", location);
28067 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28068 OMP_CLAUSE_DEVICE_ID (c) = t;
28069 OMP_CLAUSE_CHAIN (c) = list;
28071 return c;
28074 /* OpenMP 4.0:
28075 dist_schedule ( static )
28076 dist_schedule ( static , expression ) */
28078 static tree
28079 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28080 location_t location)
28082 tree c, t;
28084 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28085 return list;
28087 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28089 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28090 goto invalid_kind;
28091 cp_lexer_consume_token (parser->lexer);
28093 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28095 cp_lexer_consume_token (parser->lexer);
28097 t = cp_parser_assignment_expression (parser, false, NULL);
28099 if (t == error_mark_node)
28100 goto resync_fail;
28101 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28103 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28104 goto resync_fail;
28106 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28107 goto resync_fail;
28109 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28110 location);
28111 OMP_CLAUSE_CHAIN (c) = list;
28112 return c;
28114 invalid_kind:
28115 cp_parser_error (parser, "invalid dist_schedule kind");
28116 resync_fail:
28117 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28118 /*or_comma=*/false,
28119 /*consume_paren=*/true);
28120 return list;
28123 /* OpenMP 4.0:
28124 proc_bind ( proc-bind-kind )
28126 proc-bind-kind:
28127 master | close | spread */
28129 static tree
28130 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28131 location_t location)
28133 tree c;
28134 enum omp_clause_proc_bind_kind kind;
28136 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28137 return list;
28139 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28141 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28142 const char *p = IDENTIFIER_POINTER (id);
28144 if (strcmp ("master", p) == 0)
28145 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28146 else if (strcmp ("close", p) == 0)
28147 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28148 else if (strcmp ("spread", p) == 0)
28149 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28150 else
28151 goto invalid_kind;
28153 else
28154 goto invalid_kind;
28156 cp_lexer_consume_token (parser->lexer);
28157 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28158 goto resync_fail;
28160 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28161 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28162 location);
28163 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28164 OMP_CLAUSE_CHAIN (c) = list;
28165 return c;
28167 invalid_kind:
28168 cp_parser_error (parser, "invalid depend kind");
28169 resync_fail:
28170 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28171 /*or_comma=*/false,
28172 /*consume_paren=*/true);
28173 return list;
28176 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28177 is a bitmask in MASK. Return the list of clauses found; the result
28178 of clause default goes in *pdefault. */
28180 static tree
28181 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28182 const char *where, cp_token *pragma_tok,
28183 bool finish_p = true)
28185 tree clauses = NULL;
28186 bool first = true;
28187 cp_token *token = NULL;
28188 bool cilk_simd_fn = false;
28190 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28192 pragma_omp_clause c_kind;
28193 const char *c_name;
28194 tree prev = clauses;
28196 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28197 cp_lexer_consume_token (parser->lexer);
28199 token = cp_lexer_peek_token (parser->lexer);
28200 c_kind = cp_parser_omp_clause_name (parser);
28202 switch (c_kind)
28204 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28205 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28206 token->location);
28207 c_name = "collapse";
28208 break;
28209 case PRAGMA_OMP_CLAUSE_COPYIN:
28210 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28211 c_name = "copyin";
28212 break;
28213 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28214 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28215 clauses);
28216 c_name = "copyprivate";
28217 break;
28218 case PRAGMA_OMP_CLAUSE_DEFAULT:
28219 clauses = cp_parser_omp_clause_default (parser, clauses,
28220 token->location);
28221 c_name = "default";
28222 break;
28223 case PRAGMA_OMP_CLAUSE_FINAL:
28224 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28225 c_name = "final";
28226 break;
28227 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28228 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28229 clauses);
28230 c_name = "firstprivate";
28231 break;
28232 case PRAGMA_OMP_CLAUSE_IF:
28233 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28234 c_name = "if";
28235 break;
28236 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28237 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28238 clauses);
28239 c_name = "lastprivate";
28240 break;
28241 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28242 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28243 token->location);
28244 c_name = "mergeable";
28245 break;
28246 case PRAGMA_OMP_CLAUSE_NOWAIT:
28247 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28248 c_name = "nowait";
28249 break;
28250 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28251 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28252 token->location);
28253 c_name = "num_threads";
28254 break;
28255 case PRAGMA_OMP_CLAUSE_ORDERED:
28256 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28257 token->location);
28258 c_name = "ordered";
28259 break;
28260 case PRAGMA_OMP_CLAUSE_PRIVATE:
28261 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28262 clauses);
28263 c_name = "private";
28264 break;
28265 case PRAGMA_OMP_CLAUSE_REDUCTION:
28266 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28267 c_name = "reduction";
28268 break;
28269 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28270 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28271 token->location);
28272 c_name = "schedule";
28273 break;
28274 case PRAGMA_OMP_CLAUSE_SHARED:
28275 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28276 clauses);
28277 c_name = "shared";
28278 break;
28279 case PRAGMA_OMP_CLAUSE_UNTIED:
28280 clauses = cp_parser_omp_clause_untied (parser, clauses,
28281 token->location);
28282 c_name = "untied";
28283 break;
28284 case PRAGMA_OMP_CLAUSE_INBRANCH:
28285 case PRAGMA_CILK_CLAUSE_MASK:
28286 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28287 clauses, token->location);
28288 c_name = "inbranch";
28289 break;
28290 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28291 case PRAGMA_CILK_CLAUSE_NOMASK:
28292 clauses = cp_parser_omp_clause_branch (parser,
28293 OMP_CLAUSE_NOTINBRANCH,
28294 clauses, token->location);
28295 c_name = "notinbranch";
28296 break;
28297 case PRAGMA_OMP_CLAUSE_PARALLEL:
28298 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28299 clauses, token->location);
28300 c_name = "parallel";
28301 if (!first)
28303 clause_not_first:
28304 error_at (token->location, "%qs must be the first clause of %qs",
28305 c_name, where);
28306 clauses = prev;
28308 break;
28309 case PRAGMA_OMP_CLAUSE_FOR:
28310 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28311 clauses, token->location);
28312 c_name = "for";
28313 if (!first)
28314 goto clause_not_first;
28315 break;
28316 case PRAGMA_OMP_CLAUSE_SECTIONS:
28317 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28318 clauses, token->location);
28319 c_name = "sections";
28320 if (!first)
28321 goto clause_not_first;
28322 break;
28323 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28324 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28325 clauses, token->location);
28326 c_name = "taskgroup";
28327 if (!first)
28328 goto clause_not_first;
28329 break;
28330 case PRAGMA_OMP_CLAUSE_TO:
28331 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28332 clauses);
28333 c_name = "to";
28334 break;
28335 case PRAGMA_OMP_CLAUSE_FROM:
28336 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28337 clauses);
28338 c_name = "from";
28339 break;
28340 case PRAGMA_OMP_CLAUSE_UNIFORM:
28341 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28342 clauses);
28343 c_name = "uniform";
28344 break;
28345 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28346 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28347 token->location);
28348 c_name = "num_teams";
28349 break;
28350 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28351 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28352 token->location);
28353 c_name = "thread_limit";
28354 break;
28355 case PRAGMA_OMP_CLAUSE_ALIGNED:
28356 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28357 c_name = "aligned";
28358 break;
28359 case PRAGMA_OMP_CLAUSE_LINEAR:
28360 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28361 cilk_simd_fn = true;
28362 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28363 c_name = "linear";
28364 break;
28365 case PRAGMA_OMP_CLAUSE_DEPEND:
28366 clauses = cp_parser_omp_clause_depend (parser, clauses);
28367 c_name = "depend";
28368 break;
28369 case PRAGMA_OMP_CLAUSE_MAP:
28370 clauses = cp_parser_omp_clause_map (parser, clauses);
28371 c_name = "map";
28372 break;
28373 case PRAGMA_OMP_CLAUSE_DEVICE:
28374 clauses = cp_parser_omp_clause_device (parser, clauses,
28375 token->location);
28376 c_name = "device";
28377 break;
28378 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28379 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28380 token->location);
28381 c_name = "dist_schedule";
28382 break;
28383 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28384 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28385 token->location);
28386 c_name = "proc_bind";
28387 break;
28388 case PRAGMA_OMP_CLAUSE_SAFELEN:
28389 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28390 token->location);
28391 c_name = "safelen";
28392 break;
28393 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28394 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28395 token->location);
28396 c_name = "simdlen";
28397 break;
28398 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28399 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28400 c_name = "simdlen";
28401 break;
28402 default:
28403 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28404 goto saw_error;
28407 first = false;
28409 if (((mask >> c_kind) & 1) == 0)
28411 /* Remove the invalid clause(s) from the list to avoid
28412 confusing the rest of the compiler. */
28413 clauses = prev;
28414 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28417 saw_error:
28418 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28419 no reason to skip to the end. */
28420 if (!(flag_cilkplus && pragma_tok == NULL))
28421 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28422 if (finish_p)
28423 return finish_omp_clauses (clauses);
28424 return clauses;
28427 /* OpenMP 2.5:
28428 structured-block:
28429 statement
28431 In practice, we're also interested in adding the statement to an
28432 outer node. So it is convenient if we work around the fact that
28433 cp_parser_statement calls add_stmt. */
28435 static unsigned
28436 cp_parser_begin_omp_structured_block (cp_parser *parser)
28438 unsigned save = parser->in_statement;
28440 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28441 This preserves the "not within loop or switch" style error messages
28442 for nonsense cases like
28443 void foo() {
28444 #pragma omp single
28445 break;
28448 if (parser->in_statement)
28449 parser->in_statement = IN_OMP_BLOCK;
28451 return save;
28454 static void
28455 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28457 parser->in_statement = save;
28460 static tree
28461 cp_parser_omp_structured_block (cp_parser *parser)
28463 tree stmt = begin_omp_structured_block ();
28464 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28466 cp_parser_statement (parser, NULL_TREE, false, NULL);
28468 cp_parser_end_omp_structured_block (parser, save);
28469 return finish_omp_structured_block (stmt);
28472 /* OpenMP 2.5:
28473 # pragma omp atomic new-line
28474 expression-stmt
28476 expression-stmt:
28477 x binop= expr | x++ | ++x | x-- | --x
28478 binop:
28479 +, *, -, /, &, ^, |, <<, >>
28481 where x is an lvalue expression with scalar type.
28483 OpenMP 3.1:
28484 # pragma omp atomic new-line
28485 update-stmt
28487 # pragma omp atomic read new-line
28488 read-stmt
28490 # pragma omp atomic write new-line
28491 write-stmt
28493 # pragma omp atomic update new-line
28494 update-stmt
28496 # pragma omp atomic capture new-line
28497 capture-stmt
28499 # pragma omp atomic capture new-line
28500 capture-block
28502 read-stmt:
28503 v = x
28504 write-stmt:
28505 x = expr
28506 update-stmt:
28507 expression-stmt | x = x binop expr
28508 capture-stmt:
28509 v = expression-stmt
28510 capture-block:
28511 { v = x; update-stmt; } | { update-stmt; v = x; }
28513 OpenMP 4.0:
28514 update-stmt:
28515 expression-stmt | x = x binop expr | x = expr binop x
28516 capture-stmt:
28517 v = update-stmt
28518 capture-block:
28519 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28521 where x and v are lvalue expressions with scalar type. */
28523 static void
28524 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28526 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28527 tree rhs1 = NULL_TREE, orig_lhs;
28528 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28529 bool structured_block = false;
28530 bool seq_cst = false;
28532 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28534 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28535 const char *p = IDENTIFIER_POINTER (id);
28537 if (!strcmp (p, "read"))
28538 code = OMP_ATOMIC_READ;
28539 else if (!strcmp (p, "write"))
28540 code = NOP_EXPR;
28541 else if (!strcmp (p, "update"))
28542 code = OMP_ATOMIC;
28543 else if (!strcmp (p, "capture"))
28544 code = OMP_ATOMIC_CAPTURE_NEW;
28545 else
28546 p = NULL;
28547 if (p)
28548 cp_lexer_consume_token (parser->lexer);
28551 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28553 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28554 const char *p = IDENTIFIER_POINTER (id);
28556 if (!strcmp (p, "seq_cst"))
28558 seq_cst = true;
28559 cp_lexer_consume_token (parser->lexer);
28562 cp_parser_require_pragma_eol (parser, pragma_tok);
28564 switch (code)
28566 case OMP_ATOMIC_READ:
28567 case NOP_EXPR: /* atomic write */
28568 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28569 /*cast_p=*/false, NULL);
28570 if (v == error_mark_node)
28571 goto saw_error;
28572 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28573 goto saw_error;
28574 if (code == NOP_EXPR)
28575 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28576 else
28577 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28578 /*cast_p=*/false, NULL);
28579 if (lhs == error_mark_node)
28580 goto saw_error;
28581 if (code == NOP_EXPR)
28583 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28584 opcode. */
28585 code = OMP_ATOMIC;
28586 rhs = lhs;
28587 lhs = v;
28588 v = NULL_TREE;
28590 goto done;
28591 case OMP_ATOMIC_CAPTURE_NEW:
28592 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28594 cp_lexer_consume_token (parser->lexer);
28595 structured_block = true;
28597 else
28599 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28600 /*cast_p=*/false, NULL);
28601 if (v == error_mark_node)
28602 goto saw_error;
28603 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28604 goto saw_error;
28606 default:
28607 break;
28610 restart:
28611 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28612 /*cast_p=*/false, NULL);
28613 orig_lhs = lhs;
28614 switch (TREE_CODE (lhs))
28616 case ERROR_MARK:
28617 goto saw_error;
28619 case POSTINCREMENT_EXPR:
28620 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28621 code = OMP_ATOMIC_CAPTURE_OLD;
28622 /* FALLTHROUGH */
28623 case PREINCREMENT_EXPR:
28624 lhs = TREE_OPERAND (lhs, 0);
28625 opcode = PLUS_EXPR;
28626 rhs = integer_one_node;
28627 break;
28629 case POSTDECREMENT_EXPR:
28630 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28631 code = OMP_ATOMIC_CAPTURE_OLD;
28632 /* FALLTHROUGH */
28633 case PREDECREMENT_EXPR:
28634 lhs = TREE_OPERAND (lhs, 0);
28635 opcode = MINUS_EXPR;
28636 rhs = integer_one_node;
28637 break;
28639 case COMPOUND_EXPR:
28640 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
28641 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
28642 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
28643 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
28644 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28645 (TREE_OPERAND (lhs, 1), 0), 0)))
28646 == BOOLEAN_TYPE)
28647 /* Undo effects of boolean_increment for post {in,de}crement. */
28648 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
28649 /* FALLTHRU */
28650 case MODIFY_EXPR:
28651 if (TREE_CODE (lhs) == MODIFY_EXPR
28652 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
28654 /* Undo effects of boolean_increment. */
28655 if (integer_onep (TREE_OPERAND (lhs, 1)))
28657 /* This is pre or post increment. */
28658 rhs = TREE_OPERAND (lhs, 1);
28659 lhs = TREE_OPERAND (lhs, 0);
28660 opcode = NOP_EXPR;
28661 if (code == OMP_ATOMIC_CAPTURE_NEW
28662 && !structured_block
28663 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
28664 code = OMP_ATOMIC_CAPTURE_OLD;
28665 break;
28668 /* FALLTHRU */
28669 default:
28670 switch (cp_lexer_peek_token (parser->lexer)->type)
28672 case CPP_MULT_EQ:
28673 opcode = MULT_EXPR;
28674 break;
28675 case CPP_DIV_EQ:
28676 opcode = TRUNC_DIV_EXPR;
28677 break;
28678 case CPP_PLUS_EQ:
28679 opcode = PLUS_EXPR;
28680 break;
28681 case CPP_MINUS_EQ:
28682 opcode = MINUS_EXPR;
28683 break;
28684 case CPP_LSHIFT_EQ:
28685 opcode = LSHIFT_EXPR;
28686 break;
28687 case CPP_RSHIFT_EQ:
28688 opcode = RSHIFT_EXPR;
28689 break;
28690 case CPP_AND_EQ:
28691 opcode = BIT_AND_EXPR;
28692 break;
28693 case CPP_OR_EQ:
28694 opcode = BIT_IOR_EXPR;
28695 break;
28696 case CPP_XOR_EQ:
28697 opcode = BIT_XOR_EXPR;
28698 break;
28699 case CPP_EQ:
28700 enum cp_parser_prec oprec;
28701 cp_token *token;
28702 cp_lexer_consume_token (parser->lexer);
28703 cp_parser_parse_tentatively (parser);
28704 rhs1 = cp_parser_simple_cast_expression (parser);
28705 if (rhs1 == error_mark_node)
28707 cp_parser_abort_tentative_parse (parser);
28708 cp_parser_simple_cast_expression (parser);
28709 goto saw_error;
28711 token = cp_lexer_peek_token (parser->lexer);
28712 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
28714 cp_parser_abort_tentative_parse (parser);
28715 cp_parser_parse_tentatively (parser);
28716 rhs = cp_parser_binary_expression (parser, false, true,
28717 PREC_NOT_OPERATOR, NULL);
28718 if (rhs == error_mark_node)
28720 cp_parser_abort_tentative_parse (parser);
28721 cp_parser_binary_expression (parser, false, true,
28722 PREC_NOT_OPERATOR, NULL);
28723 goto saw_error;
28725 switch (TREE_CODE (rhs))
28727 case MULT_EXPR:
28728 case TRUNC_DIV_EXPR:
28729 case PLUS_EXPR:
28730 case MINUS_EXPR:
28731 case LSHIFT_EXPR:
28732 case RSHIFT_EXPR:
28733 case BIT_AND_EXPR:
28734 case BIT_IOR_EXPR:
28735 case BIT_XOR_EXPR:
28736 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
28738 if (cp_parser_parse_definitely (parser))
28740 opcode = TREE_CODE (rhs);
28741 rhs1 = TREE_OPERAND (rhs, 0);
28742 rhs = TREE_OPERAND (rhs, 1);
28743 goto stmt_done;
28745 else
28746 goto saw_error;
28748 break;
28749 default:
28750 break;
28752 cp_parser_abort_tentative_parse (parser);
28753 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
28755 rhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
28756 if (rhs == error_mark_node)
28757 goto saw_error;
28758 opcode = NOP_EXPR;
28759 rhs1 = NULL_TREE;
28760 goto stmt_done;
28762 cp_parser_error (parser,
28763 "invalid form of %<#pragma omp atomic%>");
28764 goto saw_error;
28766 if (!cp_parser_parse_definitely (parser))
28767 goto saw_error;
28768 switch (token->type)
28770 case CPP_SEMICOLON:
28771 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28773 code = OMP_ATOMIC_CAPTURE_OLD;
28774 v = lhs;
28775 lhs = NULL_TREE;
28776 lhs1 = rhs1;
28777 rhs1 = NULL_TREE;
28778 cp_lexer_consume_token (parser->lexer);
28779 goto restart;
28781 else if (structured_block)
28783 opcode = NOP_EXPR;
28784 rhs = rhs1;
28785 rhs1 = NULL_TREE;
28786 goto stmt_done;
28788 cp_parser_error (parser,
28789 "invalid form of %<#pragma omp atomic%>");
28790 goto saw_error;
28791 case CPP_MULT:
28792 opcode = MULT_EXPR;
28793 break;
28794 case CPP_DIV:
28795 opcode = TRUNC_DIV_EXPR;
28796 break;
28797 case CPP_PLUS:
28798 opcode = PLUS_EXPR;
28799 break;
28800 case CPP_MINUS:
28801 opcode = MINUS_EXPR;
28802 break;
28803 case CPP_LSHIFT:
28804 opcode = LSHIFT_EXPR;
28805 break;
28806 case CPP_RSHIFT:
28807 opcode = RSHIFT_EXPR;
28808 break;
28809 case CPP_AND:
28810 opcode = BIT_AND_EXPR;
28811 break;
28812 case CPP_OR:
28813 opcode = BIT_IOR_EXPR;
28814 break;
28815 case CPP_XOR:
28816 opcode = BIT_XOR_EXPR;
28817 break;
28818 default:
28819 cp_parser_error (parser,
28820 "invalid operator for %<#pragma omp atomic%>");
28821 goto saw_error;
28823 oprec = TOKEN_PRECEDENCE (token);
28824 gcc_assert (oprec != PREC_NOT_OPERATOR);
28825 if (commutative_tree_code (opcode))
28826 oprec = (enum cp_parser_prec) (oprec - 1);
28827 cp_lexer_consume_token (parser->lexer);
28828 rhs = cp_parser_binary_expression (parser, false, false,
28829 oprec, NULL);
28830 if (rhs == error_mark_node)
28831 goto saw_error;
28832 goto stmt_done;
28833 /* FALLTHROUGH */
28834 default:
28835 cp_parser_error (parser,
28836 "invalid operator for %<#pragma omp atomic%>");
28837 goto saw_error;
28839 cp_lexer_consume_token (parser->lexer);
28841 rhs = cp_parser_expression (parser, false, NULL);
28842 if (rhs == error_mark_node)
28843 goto saw_error;
28844 break;
28846 stmt_done:
28847 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
28849 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
28850 goto saw_error;
28851 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28852 /*cast_p=*/false, NULL);
28853 if (v == error_mark_node)
28854 goto saw_error;
28855 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28856 goto saw_error;
28857 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
28858 /*cast_p=*/false, NULL);
28859 if (lhs1 == error_mark_node)
28860 goto saw_error;
28862 if (structured_block)
28864 cp_parser_consume_semicolon_at_end_of_statement (parser);
28865 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
28867 done:
28868 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
28869 if (!structured_block)
28870 cp_parser_consume_semicolon_at_end_of_statement (parser);
28871 return;
28873 saw_error:
28874 cp_parser_skip_to_end_of_block_or_statement (parser);
28875 if (structured_block)
28877 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28878 cp_lexer_consume_token (parser->lexer);
28879 else if (code == OMP_ATOMIC_CAPTURE_NEW)
28881 cp_parser_skip_to_end_of_block_or_statement (parser);
28882 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28883 cp_lexer_consume_token (parser->lexer);
28889 /* OpenMP 2.5:
28890 # pragma omp barrier new-line */
28892 static void
28893 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
28895 cp_parser_require_pragma_eol (parser, pragma_tok);
28896 finish_omp_barrier ();
28899 /* OpenMP 2.5:
28900 # pragma omp critical [(name)] new-line
28901 structured-block */
28903 static tree
28904 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
28906 tree stmt, name = NULL;
28908 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28910 cp_lexer_consume_token (parser->lexer);
28912 name = cp_parser_identifier (parser);
28914 if (name == error_mark_node
28915 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28916 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28917 /*or_comma=*/false,
28918 /*consume_paren=*/true);
28919 if (name == error_mark_node)
28920 name = NULL;
28922 cp_parser_require_pragma_eol (parser, pragma_tok);
28924 stmt = cp_parser_omp_structured_block (parser);
28925 return c_finish_omp_critical (input_location, stmt, name);
28928 /* OpenMP 2.5:
28929 # pragma omp flush flush-vars[opt] new-line
28931 flush-vars:
28932 ( variable-list ) */
28934 static void
28935 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
28937 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28938 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
28939 cp_parser_require_pragma_eol (parser, pragma_tok);
28941 finish_omp_flush ();
28944 /* Helper function, to parse omp for increment expression. */
28946 static tree
28947 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
28949 tree cond = cp_parser_binary_expression (parser, false, true,
28950 PREC_NOT_OPERATOR, NULL);
28951 if (cond == error_mark_node
28952 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28954 cp_parser_skip_to_end_of_statement (parser);
28955 return error_mark_node;
28958 switch (TREE_CODE (cond))
28960 case GT_EXPR:
28961 case GE_EXPR:
28962 case LT_EXPR:
28963 case LE_EXPR:
28964 break;
28965 case NE_EXPR:
28966 if (code == CILK_SIMD)
28967 break;
28968 /* Fall through: OpenMP disallows NE_EXPR. */
28969 default:
28970 return error_mark_node;
28973 /* If decl is an iterator, preserve LHS and RHS of the relational
28974 expr until finish_omp_for. */
28975 if (decl
28976 && (type_dependent_expression_p (decl)
28977 || CLASS_TYPE_P (TREE_TYPE (decl))))
28978 return cond;
28980 return build_x_binary_op (input_location, TREE_CODE (cond),
28981 TREE_OPERAND (cond, 0), ERROR_MARK,
28982 TREE_OPERAND (cond, 1), ERROR_MARK,
28983 /*overload=*/NULL, tf_warning_or_error);
28986 /* Helper function, to parse omp for increment expression. */
28988 static tree
28989 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
28991 cp_token *token = cp_lexer_peek_token (parser->lexer);
28992 enum tree_code op;
28993 tree lhs, rhs;
28994 cp_id_kind idk;
28995 bool decl_first;
28997 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
28999 op = (token->type == CPP_PLUS_PLUS
29000 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29001 cp_lexer_consume_token (parser->lexer);
29002 lhs = cp_parser_simple_cast_expression (parser);
29003 if (lhs != decl)
29004 return error_mark_node;
29005 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29008 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29009 if (lhs != decl)
29010 return error_mark_node;
29012 token = cp_lexer_peek_token (parser->lexer);
29013 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29015 op = (token->type == CPP_PLUS_PLUS
29016 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29017 cp_lexer_consume_token (parser->lexer);
29018 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29021 op = cp_parser_assignment_operator_opt (parser);
29022 if (op == ERROR_MARK)
29023 return error_mark_node;
29025 if (op != NOP_EXPR)
29027 rhs = cp_parser_assignment_expression (parser, false, NULL);
29028 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29029 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29032 lhs = cp_parser_binary_expression (parser, false, false,
29033 PREC_ADDITIVE_EXPRESSION, NULL);
29034 token = cp_lexer_peek_token (parser->lexer);
29035 decl_first = lhs == decl;
29036 if (decl_first)
29037 lhs = NULL_TREE;
29038 if (token->type != CPP_PLUS
29039 && token->type != CPP_MINUS)
29040 return error_mark_node;
29044 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29045 cp_lexer_consume_token (parser->lexer);
29046 rhs = cp_parser_binary_expression (parser, false, false,
29047 PREC_ADDITIVE_EXPRESSION, NULL);
29048 token = cp_lexer_peek_token (parser->lexer);
29049 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29051 if (lhs == NULL_TREE)
29053 if (op == PLUS_EXPR)
29054 lhs = rhs;
29055 else
29056 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29057 tf_warning_or_error);
29059 else
29060 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29061 ERROR_MARK, NULL, tf_warning_or_error);
29064 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29066 if (!decl_first)
29068 if (rhs != decl || op == MINUS_EXPR)
29069 return error_mark_node;
29070 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29072 else
29073 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29075 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29078 /* Parse the initialization statement of either an OpenMP for loop or
29079 a Cilk Plus for loop.
29081 PARSING_OPENMP is true if parsing OpenMP, or false if parsing Cilk
29082 Plus.
29084 Return true if the resulting construct should have an
29085 OMP_CLAUSE_PRIVATE added to it. */
29087 static bool
29088 cp_parser_omp_for_loop_init (cp_parser *parser,
29089 bool parsing_openmp,
29090 tree &this_pre_body,
29091 vec<tree, va_gc> *for_block,
29092 tree &init,
29093 tree &decl,
29094 tree &real_decl)
29096 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29097 return false;
29099 bool add_private_clause = false;
29101 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29103 init-expr:
29104 var = lb
29105 integer-type var = lb
29106 random-access-iterator-type var = lb
29107 pointer-type var = lb
29109 cp_decl_specifier_seq type_specifiers;
29111 /* First, try to parse as an initialized declaration. See
29112 cp_parser_condition, from whence the bulk of this is copied. */
29114 cp_parser_parse_tentatively (parser);
29115 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29116 /*is_trailing_return=*/false,
29117 &type_specifiers);
29118 if (cp_parser_parse_definitely (parser))
29120 /* If parsing a type specifier seq succeeded, then this
29121 MUST be a initialized declaration. */
29122 tree asm_specification, attributes;
29123 cp_declarator *declarator;
29125 declarator = cp_parser_declarator (parser,
29126 CP_PARSER_DECLARATOR_NAMED,
29127 /*ctor_dtor_or_conv_p=*/NULL,
29128 /*parenthesized_p=*/NULL,
29129 /*member_p=*/false);
29130 attributes = cp_parser_attributes_opt (parser);
29131 asm_specification = cp_parser_asm_specification_opt (parser);
29133 if (declarator == cp_error_declarator)
29134 cp_parser_skip_to_end_of_statement (parser);
29136 else
29138 tree pushed_scope, auto_node;
29140 decl = start_decl (declarator, &type_specifiers,
29141 SD_INITIALIZED, attributes,
29142 /*prefix_attributes=*/NULL_TREE,
29143 &pushed_scope);
29145 auto_node = type_uses_auto (TREE_TYPE (decl));
29146 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29148 if (cp_lexer_next_token_is (parser->lexer,
29149 CPP_OPEN_PAREN))
29151 if (parsing_openmp)
29152 error ("parenthesized initialization is not allowed in "
29153 "OpenMP %<for%> loop");
29154 else
29155 error ("parenthesized initialization is "
29156 "not allowed in for-loop");
29158 else
29159 /* Trigger an error. */
29160 cp_parser_require (parser, CPP_EQ, RT_EQ);
29162 init = error_mark_node;
29163 cp_parser_skip_to_end_of_statement (parser);
29165 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29166 || type_dependent_expression_p (decl)
29167 || auto_node)
29169 bool is_direct_init, is_non_constant_init;
29171 init = cp_parser_initializer (parser,
29172 &is_direct_init,
29173 &is_non_constant_init);
29175 if (auto_node)
29177 TREE_TYPE (decl)
29178 = do_auto_deduction (TREE_TYPE (decl), init,
29179 auto_node);
29181 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29182 && !type_dependent_expression_p (decl))
29183 goto non_class;
29186 cp_finish_decl (decl, init, !is_non_constant_init,
29187 asm_specification,
29188 LOOKUP_ONLYCONVERTING);
29189 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29191 vec_safe_push (for_block, this_pre_body);
29192 init = NULL_TREE;
29194 else
29195 init = pop_stmt_list (this_pre_body);
29196 this_pre_body = NULL_TREE;
29198 else
29200 /* Consume '='. */
29201 cp_lexer_consume_token (parser->lexer);
29202 init = cp_parser_assignment_expression (parser, false, NULL);
29204 non_class:
29205 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29206 init = error_mark_node;
29207 else
29208 cp_finish_decl (decl, NULL_TREE,
29209 /*init_const_expr_p=*/false,
29210 asm_specification,
29211 LOOKUP_ONLYCONVERTING);
29214 if (pushed_scope)
29215 pop_scope (pushed_scope);
29218 else
29220 cp_id_kind idk;
29221 /* If parsing a type specifier sequence failed, then
29222 this MUST be a simple expression. */
29223 cp_parser_parse_tentatively (parser);
29224 decl = cp_parser_primary_expression (parser, false, false,
29225 false, &idk);
29226 if (!cp_parser_error_occurred (parser)
29227 && decl
29228 && DECL_P (decl)
29229 && CLASS_TYPE_P (TREE_TYPE (decl)))
29231 tree rhs;
29233 cp_parser_parse_definitely (parser);
29234 cp_parser_require (parser, CPP_EQ, RT_EQ);
29235 rhs = cp_parser_assignment_expression (parser, false, NULL);
29236 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29237 decl, NOP_EXPR,
29238 rhs,
29239 tf_warning_or_error));
29240 add_private_clause = true;
29242 else
29244 decl = NULL;
29245 cp_parser_abort_tentative_parse (parser);
29246 init = cp_parser_expression (parser, false, NULL);
29247 if (init)
29249 if (TREE_CODE (init) == MODIFY_EXPR
29250 || TREE_CODE (init) == MODOP_EXPR)
29251 real_decl = TREE_OPERAND (init, 0);
29255 return add_private_clause;
29258 /* Parse the restricted form of the for statement allowed by OpenMP. */
29260 static tree
29261 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29262 tree *cclauses)
29264 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29265 tree real_decl, initv, condv, incrv, declv;
29266 tree this_pre_body, cl;
29267 location_t loc_first;
29268 bool collapse_err = false;
29269 int i, collapse = 1, nbraces = 0;
29270 vec<tree, va_gc> *for_block = make_tree_vector ();
29272 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29273 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29274 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29276 gcc_assert (collapse >= 1);
29278 declv = make_tree_vec (collapse);
29279 initv = make_tree_vec (collapse);
29280 condv = make_tree_vec (collapse);
29281 incrv = make_tree_vec (collapse);
29283 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29285 for (i = 0; i < collapse; i++)
29287 int bracecount = 0;
29288 bool add_private_clause = false;
29289 location_t loc;
29291 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29293 cp_parser_error (parser, "for statement expected");
29294 return NULL;
29296 loc = cp_lexer_consume_token (parser->lexer)->location;
29298 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29299 return NULL;
29301 init = decl = real_decl = NULL;
29302 this_pre_body = push_stmt_list ();
29304 add_private_clause
29305 |= cp_parser_omp_for_loop_init (parser,
29306 /*parsing_openmp=*/code != CILK_SIMD,
29307 this_pre_body, for_block,
29308 init, decl, real_decl);
29310 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29311 if (this_pre_body)
29313 this_pre_body = pop_stmt_list (this_pre_body);
29314 if (pre_body)
29316 tree t = pre_body;
29317 pre_body = push_stmt_list ();
29318 add_stmt (t);
29319 add_stmt (this_pre_body);
29320 pre_body = pop_stmt_list (pre_body);
29322 else
29323 pre_body = this_pre_body;
29326 if (decl)
29327 real_decl = decl;
29328 if (cclauses != NULL
29329 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29330 && real_decl != NULL_TREE)
29332 tree *c;
29333 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29334 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29335 && OMP_CLAUSE_DECL (*c) == real_decl)
29337 error_at (loc, "iteration variable %qD"
29338 " should not be firstprivate", real_decl);
29339 *c = OMP_CLAUSE_CHAIN (*c);
29341 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29342 && OMP_CLAUSE_DECL (*c) == real_decl)
29344 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29345 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29346 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29347 OMP_CLAUSE_DECL (l) = real_decl;
29348 OMP_CLAUSE_CHAIN (l) = clauses;
29349 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29350 clauses = l;
29351 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29352 CP_OMP_CLAUSE_INFO (*c) = NULL;
29353 add_private_clause = false;
29355 else
29357 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29358 && OMP_CLAUSE_DECL (*c) == real_decl)
29359 add_private_clause = false;
29360 c = &OMP_CLAUSE_CHAIN (*c);
29364 if (add_private_clause)
29366 tree c;
29367 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29369 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29370 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29371 && OMP_CLAUSE_DECL (c) == decl)
29372 break;
29373 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29374 && OMP_CLAUSE_DECL (c) == decl)
29375 error_at (loc, "iteration variable %qD "
29376 "should not be firstprivate",
29377 decl);
29378 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29379 && OMP_CLAUSE_DECL (c) == decl)
29380 error_at (loc, "iteration variable %qD should not be reduction",
29381 decl);
29383 if (c == NULL)
29385 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29386 OMP_CLAUSE_DECL (c) = decl;
29387 c = finish_omp_clauses (c);
29388 if (c)
29390 OMP_CLAUSE_CHAIN (c) = clauses;
29391 clauses = c;
29396 cond = NULL;
29397 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29398 cond = cp_parser_omp_for_cond (parser, decl, code);
29399 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29401 incr = NULL;
29402 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29404 /* If decl is an iterator, preserve the operator on decl
29405 until finish_omp_for. */
29406 if (real_decl
29407 && ((processing_template_decl
29408 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29409 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29410 incr = cp_parser_omp_for_incr (parser, real_decl);
29411 else
29412 incr = cp_parser_expression (parser, false, NULL);
29413 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29414 SET_EXPR_LOCATION (incr, input_location);
29417 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29418 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29419 /*or_comma=*/false,
29420 /*consume_paren=*/true);
29422 TREE_VEC_ELT (declv, i) = decl;
29423 TREE_VEC_ELT (initv, i) = init;
29424 TREE_VEC_ELT (condv, i) = cond;
29425 TREE_VEC_ELT (incrv, i) = incr;
29427 if (i == collapse - 1)
29428 break;
29430 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29431 in between the collapsed for loops to be still considered perfectly
29432 nested. Hopefully the final version clarifies this.
29433 For now handle (multiple) {'s and empty statements. */
29434 cp_parser_parse_tentatively (parser);
29437 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29438 break;
29439 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29441 cp_lexer_consume_token (parser->lexer);
29442 bracecount++;
29444 else if (bracecount
29445 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29446 cp_lexer_consume_token (parser->lexer);
29447 else
29449 loc = cp_lexer_peek_token (parser->lexer)->location;
29450 error_at (loc, "not enough collapsed for loops");
29451 collapse_err = true;
29452 cp_parser_abort_tentative_parse (parser);
29453 declv = NULL_TREE;
29454 break;
29457 while (1);
29459 if (declv)
29461 cp_parser_parse_definitely (parser);
29462 nbraces += bracecount;
29466 /* Note that we saved the original contents of this flag when we entered
29467 the structured block, and so we don't need to re-save it here. */
29468 if (code == CILK_SIMD)
29469 parser->in_statement = IN_CILK_SIMD_FOR;
29470 else
29471 parser->in_statement = IN_OMP_FOR;
29473 /* Note that the grammar doesn't call for a structured block here,
29474 though the loop as a whole is a structured block. */
29475 body = push_stmt_list ();
29476 cp_parser_statement (parser, NULL_TREE, false, NULL);
29477 body = pop_stmt_list (body);
29479 if (declv == NULL_TREE)
29480 ret = NULL_TREE;
29481 else
29482 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29483 pre_body, clauses);
29485 while (nbraces)
29487 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29489 cp_lexer_consume_token (parser->lexer);
29490 nbraces--;
29492 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29493 cp_lexer_consume_token (parser->lexer);
29494 else
29496 if (!collapse_err)
29498 error_at (cp_lexer_peek_token (parser->lexer)->location,
29499 "collapsed loops not perfectly nested");
29501 collapse_err = true;
29502 cp_parser_statement_seq_opt (parser, NULL);
29503 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29504 break;
29508 while (!for_block->is_empty ())
29509 add_stmt (pop_stmt_list (for_block->pop ()));
29510 release_tree_vector (for_block);
29512 return ret;
29515 /* Helper function for OpenMP parsing, split clauses and call
29516 finish_omp_clauses on each of the set of clauses afterwards. */
29518 static void
29519 cp_omp_split_clauses (location_t loc, enum tree_code code,
29520 omp_clause_mask mask, tree clauses, tree *cclauses)
29522 int i;
29523 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29524 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29525 if (cclauses[i])
29526 cclauses[i] = finish_omp_clauses (cclauses[i]);
29529 /* OpenMP 4.0:
29530 #pragma omp simd simd-clause[optseq] new-line
29531 for-loop */
29533 #define OMP_SIMD_CLAUSE_MASK \
29534 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29542 static tree
29543 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29544 char *p_name, omp_clause_mask mask, tree *cclauses)
29546 tree clauses, sb, ret;
29547 unsigned int save;
29548 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29550 strcat (p_name, " simd");
29551 mask |= OMP_SIMD_CLAUSE_MASK;
29552 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29554 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29555 cclauses == NULL);
29556 if (cclauses)
29558 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29559 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29562 sb = begin_omp_structured_block ();
29563 save = cp_parser_begin_omp_structured_block (parser);
29565 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29567 cp_parser_end_omp_structured_block (parser, save);
29568 add_stmt (finish_omp_structured_block (sb));
29570 return ret;
29573 /* OpenMP 2.5:
29574 #pragma omp for for-clause[optseq] new-line
29575 for-loop
29577 OpenMP 4.0:
29578 #pragma omp for simd for-simd-clause[optseq] new-line
29579 for-loop */
29581 #define OMP_FOR_CLAUSE_MASK \
29582 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29591 static tree
29592 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29593 char *p_name, omp_clause_mask mask, tree *cclauses)
29595 tree clauses, sb, ret;
29596 unsigned int save;
29597 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29599 strcat (p_name, " for");
29600 mask |= OMP_FOR_CLAUSE_MASK;
29601 if (cclauses)
29602 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29604 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29606 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29607 const char *p = IDENTIFIER_POINTER (id);
29609 if (strcmp (p, "simd") == 0)
29611 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29612 if (cclauses == NULL)
29613 cclauses = cclauses_buf;
29615 cp_lexer_consume_token (parser->lexer);
29616 if (!flag_openmp) /* flag_openmp_simd */
29617 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29618 cclauses);
29619 sb = begin_omp_structured_block ();
29620 save = cp_parser_begin_omp_structured_block (parser);
29621 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29622 cclauses);
29623 cp_parser_end_omp_structured_block (parser, save);
29624 tree body = finish_omp_structured_block (sb);
29625 if (ret == NULL)
29626 return ret;
29627 ret = make_node (OMP_FOR);
29628 TREE_TYPE (ret) = void_type_node;
29629 OMP_FOR_BODY (ret) = body;
29630 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29631 SET_EXPR_LOCATION (ret, loc);
29632 add_stmt (ret);
29633 return ret;
29636 if (!flag_openmp) /* flag_openmp_simd */
29638 cp_parser_require_pragma_eol (parser, pragma_tok);
29639 return NULL_TREE;
29642 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29643 cclauses == NULL);
29644 if (cclauses)
29646 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
29647 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29650 sb = begin_omp_structured_block ();
29651 save = cp_parser_begin_omp_structured_block (parser);
29653 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
29655 cp_parser_end_omp_structured_block (parser, save);
29656 add_stmt (finish_omp_structured_block (sb));
29658 return ret;
29661 /* OpenMP 2.5:
29662 # pragma omp master new-line
29663 structured-block */
29665 static tree
29666 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
29668 cp_parser_require_pragma_eol (parser, pragma_tok);
29669 return c_finish_omp_master (input_location,
29670 cp_parser_omp_structured_block (parser));
29673 /* OpenMP 2.5:
29674 # pragma omp ordered new-line
29675 structured-block */
29677 static tree
29678 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
29680 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29681 cp_parser_require_pragma_eol (parser, pragma_tok);
29682 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
29685 /* OpenMP 2.5:
29687 section-scope:
29688 { section-sequence }
29690 section-sequence:
29691 section-directive[opt] structured-block
29692 section-sequence section-directive structured-block */
29694 static tree
29695 cp_parser_omp_sections_scope (cp_parser *parser)
29697 tree stmt, substmt;
29698 bool error_suppress = false;
29699 cp_token *tok;
29701 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
29702 return NULL_TREE;
29704 stmt = push_stmt_list ();
29706 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
29708 substmt = cp_parser_omp_structured_block (parser);
29709 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29710 add_stmt (substmt);
29713 while (1)
29715 tok = cp_lexer_peek_token (parser->lexer);
29716 if (tok->type == CPP_CLOSE_BRACE)
29717 break;
29718 if (tok->type == CPP_EOF)
29719 break;
29721 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
29723 cp_lexer_consume_token (parser->lexer);
29724 cp_parser_require_pragma_eol (parser, tok);
29725 error_suppress = false;
29727 else if (!error_suppress)
29729 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
29730 error_suppress = true;
29733 substmt = cp_parser_omp_structured_block (parser);
29734 substmt = build1 (OMP_SECTION, void_type_node, substmt);
29735 add_stmt (substmt);
29737 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29739 substmt = pop_stmt_list (stmt);
29741 stmt = make_node (OMP_SECTIONS);
29742 TREE_TYPE (stmt) = void_type_node;
29743 OMP_SECTIONS_BODY (stmt) = substmt;
29745 add_stmt (stmt);
29746 return stmt;
29749 /* OpenMP 2.5:
29750 # pragma omp sections sections-clause[optseq] newline
29751 sections-scope */
29753 #define OMP_SECTIONS_CLAUSE_MASK \
29754 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29760 static tree
29761 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
29762 char *p_name, omp_clause_mask mask, tree *cclauses)
29764 tree clauses, ret;
29765 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29767 strcat (p_name, " sections");
29768 mask |= OMP_SECTIONS_CLAUSE_MASK;
29769 if (cclauses)
29770 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29772 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29773 cclauses == NULL);
29774 if (cclauses)
29776 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
29777 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
29780 ret = cp_parser_omp_sections_scope (parser);
29781 if (ret)
29782 OMP_SECTIONS_CLAUSES (ret) = clauses;
29784 return ret;
29787 /* OpenMP 2.5:
29788 # pragma omp parallel parallel-clause[optseq] new-line
29789 structured-block
29790 # pragma omp parallel for parallel-for-clause[optseq] new-line
29791 structured-block
29792 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
29793 structured-block
29795 OpenMP 4.0:
29796 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
29797 structured-block */
29799 #define OMP_PARALLEL_CLAUSE_MASK \
29800 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
29806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
29808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
29810 static tree
29811 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
29812 char *p_name, omp_clause_mask mask, tree *cclauses)
29814 tree stmt, clauses, block;
29815 unsigned int save;
29816 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29818 strcat (p_name, " parallel");
29819 mask |= OMP_PARALLEL_CLAUSE_MASK;
29821 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29823 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29824 if (cclauses == NULL)
29825 cclauses = cclauses_buf;
29827 cp_lexer_consume_token (parser->lexer);
29828 if (!flag_openmp) /* flag_openmp_simd */
29829 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29830 block = begin_omp_parallel ();
29831 save = cp_parser_begin_omp_structured_block (parser);
29832 cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
29833 cp_parser_end_omp_structured_block (parser, save);
29834 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29835 block);
29836 OMP_PARALLEL_COMBINED (stmt) = 1;
29837 return stmt;
29839 else if (cclauses)
29841 error_at (loc, "expected %<for%> after %qs", p_name);
29842 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
29843 return NULL_TREE;
29845 else if (!flag_openmp) /* flag_openmp_simd */
29847 cp_parser_require_pragma_eol (parser, pragma_tok);
29848 return NULL_TREE;
29850 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29852 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29853 const char *p = IDENTIFIER_POINTER (id);
29854 if (strcmp (p, "sections") == 0)
29856 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29857 cclauses = cclauses_buf;
29859 cp_lexer_consume_token (parser->lexer);
29860 block = begin_omp_parallel ();
29861 save = cp_parser_begin_omp_structured_block (parser);
29862 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
29863 cp_parser_end_omp_structured_block (parser, save);
29864 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
29865 block);
29866 OMP_PARALLEL_COMBINED (stmt) = 1;
29867 return stmt;
29871 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
29873 block = begin_omp_parallel ();
29874 save = cp_parser_begin_omp_structured_block (parser);
29875 cp_parser_statement (parser, NULL_TREE, false, NULL);
29876 cp_parser_end_omp_structured_block (parser, save);
29877 stmt = finish_omp_parallel (clauses, block);
29878 return stmt;
29881 /* OpenMP 2.5:
29882 # pragma omp single single-clause[optseq] new-line
29883 structured-block */
29885 #define OMP_SINGLE_CLAUSE_MASK \
29886 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
29889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
29891 static tree
29892 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
29894 tree stmt = make_node (OMP_SINGLE);
29895 TREE_TYPE (stmt) = void_type_node;
29897 OMP_SINGLE_CLAUSES (stmt)
29898 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
29899 "#pragma omp single", pragma_tok);
29900 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
29902 return add_stmt (stmt);
29905 /* OpenMP 3.0:
29906 # pragma omp task task-clause[optseq] new-line
29907 structured-block */
29909 #define OMP_TASK_CLAUSE_MASK \
29910 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
29911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
29912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
29913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
29916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
29917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
29918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
29920 static tree
29921 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
29923 tree clauses, block;
29924 unsigned int save;
29926 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
29927 "#pragma omp task", pragma_tok);
29928 block = begin_omp_task ();
29929 save = cp_parser_begin_omp_structured_block (parser);
29930 cp_parser_statement (parser, NULL_TREE, false, NULL);
29931 cp_parser_end_omp_structured_block (parser, save);
29932 return finish_omp_task (clauses, block);
29935 /* OpenMP 3.0:
29936 # pragma omp taskwait new-line */
29938 static void
29939 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
29941 cp_parser_require_pragma_eol (parser, pragma_tok);
29942 finish_omp_taskwait ();
29945 /* OpenMP 3.1:
29946 # pragma omp taskyield new-line */
29948 static void
29949 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
29951 cp_parser_require_pragma_eol (parser, pragma_tok);
29952 finish_omp_taskyield ();
29955 /* OpenMP 4.0:
29956 # pragma omp taskgroup new-line
29957 structured-block */
29959 static tree
29960 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
29962 cp_parser_require_pragma_eol (parser, pragma_tok);
29963 return c_finish_omp_taskgroup (input_location,
29964 cp_parser_omp_structured_block (parser));
29968 /* OpenMP 2.5:
29969 # pragma omp threadprivate (variable-list) */
29971 static void
29972 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
29974 tree vars;
29976 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29977 cp_parser_require_pragma_eol (parser, pragma_tok);
29979 finish_omp_threadprivate (vars);
29982 /* OpenMP 4.0:
29983 # pragma omp cancel cancel-clause[optseq] new-line */
29985 #define OMP_CANCEL_CLAUSE_MASK \
29986 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
29987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
29988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
29989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
29990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
29992 static void
29993 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
29995 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
29996 "#pragma omp cancel", pragma_tok);
29997 finish_omp_cancel (clauses);
30000 /* OpenMP 4.0:
30001 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30003 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30004 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30009 static void
30010 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30012 tree clauses;
30013 bool point_seen = false;
30015 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30017 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30018 const char *p = IDENTIFIER_POINTER (id);
30020 if (strcmp (p, "point") == 0)
30022 cp_lexer_consume_token (parser->lexer);
30023 point_seen = true;
30026 if (!point_seen)
30028 cp_parser_error (parser, "expected %<point%>");
30029 cp_parser_require_pragma_eol (parser, pragma_tok);
30030 return;
30033 clauses = cp_parser_omp_all_clauses (parser,
30034 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30035 "#pragma omp cancellation point",
30036 pragma_tok);
30037 finish_omp_cancellation_point (clauses);
30040 /* OpenMP 4.0:
30041 #pragma omp distribute distribute-clause[optseq] new-line
30042 for-loop */
30044 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30045 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30050 static tree
30051 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30052 char *p_name, omp_clause_mask mask, tree *cclauses)
30054 tree clauses, sb, ret;
30055 unsigned int save;
30056 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30058 strcat (p_name, " distribute");
30059 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30061 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30063 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30064 const char *p = IDENTIFIER_POINTER (id);
30065 bool simd = false;
30066 bool parallel = false;
30068 if (strcmp (p, "simd") == 0)
30069 simd = true;
30070 else
30071 parallel = strcmp (p, "parallel") == 0;
30072 if (parallel || simd)
30074 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30075 if (cclauses == NULL)
30076 cclauses = cclauses_buf;
30077 cp_lexer_consume_token (parser->lexer);
30078 if (!flag_openmp) /* flag_openmp_simd */
30080 if (simd)
30081 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30082 cclauses);
30083 else
30084 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30085 cclauses);
30087 sb = begin_omp_structured_block ();
30088 save = cp_parser_begin_omp_structured_block (parser);
30089 if (simd)
30090 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30091 cclauses);
30092 else
30093 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30094 cclauses);
30095 cp_parser_end_omp_structured_block (parser, save);
30096 tree body = finish_omp_structured_block (sb);
30097 if (ret == NULL)
30098 return ret;
30099 ret = make_node (OMP_DISTRIBUTE);
30100 TREE_TYPE (ret) = void_type_node;
30101 OMP_FOR_BODY (ret) = body;
30102 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30103 SET_EXPR_LOCATION (ret, loc);
30104 add_stmt (ret);
30105 return ret;
30108 if (!flag_openmp) /* flag_openmp_simd */
30110 cp_parser_require_pragma_eol (parser, pragma_tok);
30111 return NULL_TREE;
30114 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30115 cclauses == NULL);
30116 if (cclauses)
30118 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30119 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30122 sb = begin_omp_structured_block ();
30123 save = cp_parser_begin_omp_structured_block (parser);
30125 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30127 cp_parser_end_omp_structured_block (parser, save);
30128 add_stmt (finish_omp_structured_block (sb));
30130 return ret;
30133 /* OpenMP 4.0:
30134 # pragma omp teams teams-clause[optseq] new-line
30135 structured-block */
30137 #define OMP_TEAMS_CLAUSE_MASK \
30138 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30146 static tree
30147 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30148 char *p_name, omp_clause_mask mask, tree *cclauses)
30150 tree clauses, sb, ret;
30151 unsigned int save;
30152 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30154 strcat (p_name, " teams");
30155 mask |= OMP_TEAMS_CLAUSE_MASK;
30157 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30159 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30160 const char *p = IDENTIFIER_POINTER (id);
30161 if (strcmp (p, "distribute") == 0)
30163 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30164 if (cclauses == NULL)
30165 cclauses = cclauses_buf;
30167 cp_lexer_consume_token (parser->lexer);
30168 if (!flag_openmp) /* flag_openmp_simd */
30169 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30170 cclauses);
30171 sb = begin_omp_structured_block ();
30172 save = cp_parser_begin_omp_structured_block (parser);
30173 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30174 cclauses);
30175 cp_parser_end_omp_structured_block (parser, save);
30176 tree body = finish_omp_structured_block (sb);
30177 if (ret == NULL)
30178 return ret;
30179 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30180 ret = make_node (OMP_TEAMS);
30181 TREE_TYPE (ret) = void_type_node;
30182 OMP_TEAMS_CLAUSES (ret) = clauses;
30183 OMP_TEAMS_BODY (ret) = body;
30184 return add_stmt (ret);
30187 if (!flag_openmp) /* flag_openmp_simd */
30189 cp_parser_require_pragma_eol (parser, pragma_tok);
30190 return NULL_TREE;
30193 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30194 cclauses == NULL);
30195 if (cclauses)
30197 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30198 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30201 tree stmt = make_node (OMP_TEAMS);
30202 TREE_TYPE (stmt) = void_type_node;
30203 OMP_TEAMS_CLAUSES (stmt) = clauses;
30204 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30206 return add_stmt (stmt);
30209 /* OpenMP 4.0:
30210 # pragma omp target data target-data-clause[optseq] new-line
30211 structured-block */
30213 #define OMP_TARGET_DATA_CLAUSE_MASK \
30214 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30218 static tree
30219 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30221 tree stmt = make_node (OMP_TARGET_DATA);
30222 TREE_TYPE (stmt) = void_type_node;
30224 OMP_TARGET_DATA_CLAUSES (stmt)
30225 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30226 "#pragma omp target data", pragma_tok);
30227 keep_next_level (true);
30228 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30230 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30231 return add_stmt (stmt);
30234 /* OpenMP 4.0:
30235 # pragma omp target update target-update-clause[optseq] new-line */
30237 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30238 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30243 static bool
30244 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30245 enum pragma_context context)
30247 if (context == pragma_stmt)
30249 error_at (pragma_tok->location,
30250 "%<#pragma omp target update%> may only be "
30251 "used in compound statements");
30252 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30253 return false;
30256 tree clauses
30257 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30258 "#pragma omp target update", pragma_tok);
30259 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30260 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30262 error_at (pragma_tok->location,
30263 "%<#pragma omp target update must contain at least one "
30264 "%<from%> or %<to%> clauses");
30265 return false;
30268 tree stmt = make_node (OMP_TARGET_UPDATE);
30269 TREE_TYPE (stmt) = void_type_node;
30270 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30271 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30272 add_stmt (stmt);
30273 return false;
30276 /* OpenMP 4.0:
30277 # pragma omp target target-clause[optseq] new-line
30278 structured-block */
30280 #define OMP_TARGET_CLAUSE_MASK \
30281 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30285 static bool
30286 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30287 enum pragma_context context)
30289 if (context != pragma_stmt && context != pragma_compound)
30291 cp_parser_error (parser, "expected declaration specifiers");
30292 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30293 return false;
30296 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30298 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30299 const char *p = IDENTIFIER_POINTER (id);
30301 if (strcmp (p, "teams") == 0)
30303 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30304 char p_name[sizeof ("#pragma omp target teams distribute "
30305 "parallel for simd")];
30307 cp_lexer_consume_token (parser->lexer);
30308 strcpy (p_name, "#pragma omp target");
30309 if (!flag_openmp) /* flag_openmp_simd */
30310 return cp_parser_omp_teams (parser, pragma_tok, p_name,
30311 OMP_TARGET_CLAUSE_MASK, cclauses);
30312 keep_next_level (true);
30313 tree sb = begin_omp_structured_block ();
30314 unsigned save = cp_parser_begin_omp_structured_block (parser);
30315 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30316 OMP_TARGET_CLAUSE_MASK, cclauses);
30317 cp_parser_end_omp_structured_block (parser, save);
30318 tree body = finish_omp_structured_block (sb);
30319 if (ret == NULL)
30320 return ret;
30321 tree stmt = make_node (OMP_TARGET);
30322 TREE_TYPE (stmt) = void_type_node;
30323 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30324 OMP_TARGET_BODY (stmt) = body;
30325 add_stmt (stmt);
30326 return true;
30328 else if (!flag_openmp) /* flag_openmp_simd */
30330 cp_parser_require_pragma_eol (parser, pragma_tok);
30331 return NULL_TREE;
30333 else if (strcmp (p, "data") == 0)
30335 cp_lexer_consume_token (parser->lexer);
30336 cp_parser_omp_target_data (parser, pragma_tok);
30337 return true;
30339 else if (strcmp (p, "update") == 0)
30341 cp_lexer_consume_token (parser->lexer);
30342 return cp_parser_omp_target_update (parser, pragma_tok, context);
30346 tree stmt = make_node (OMP_TARGET);
30347 TREE_TYPE (stmt) = void_type_node;
30349 OMP_TARGET_CLAUSES (stmt)
30350 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30351 "#pragma omp target", pragma_tok);
30352 keep_next_level (true);
30353 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30355 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30356 add_stmt (stmt);
30357 return true;
30360 /* OpenMP 4.0:
30361 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30363 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30364 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30371 static void
30372 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30373 enum pragma_context context)
30375 bool first_p = parser->omp_declare_simd == NULL;
30376 cp_omp_declare_simd_data data;
30377 if (first_p)
30379 data.error_seen = false;
30380 data.fndecl_seen = false;
30381 data.tokens = vNULL;
30382 parser->omp_declare_simd = &data;
30384 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30385 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30386 cp_lexer_consume_token (parser->lexer);
30387 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30388 parser->omp_declare_simd->error_seen = true;
30389 cp_parser_require_pragma_eol (parser, pragma_tok);
30390 struct cp_token_cache *cp
30391 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30392 parser->omp_declare_simd->tokens.safe_push (cp);
30393 if (first_p)
30395 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30396 cp_parser_pragma (parser, context);
30397 switch (context)
30399 case pragma_external:
30400 cp_parser_declaration (parser);
30401 break;
30402 case pragma_member:
30403 cp_parser_member_declaration (parser);
30404 break;
30405 case pragma_objc_icode:
30406 cp_parser_block_declaration (parser, /*statement_p=*/false);
30407 break;
30408 default:
30409 cp_parser_declaration_statement (parser);
30410 break;
30412 if (parser->omp_declare_simd
30413 && !parser->omp_declare_simd->error_seen
30414 && !parser->omp_declare_simd->fndecl_seen)
30415 error_at (pragma_tok->location,
30416 "%<#pragma omp declare simd%> not immediately followed by "
30417 "function declaration or definition");
30418 data.tokens.release ();
30419 parser->omp_declare_simd = NULL;
30423 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30424 This function is modelled similar to the late parsing of omp declare
30425 simd. */
30427 static tree
30428 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30430 struct cp_token_cache *ce;
30431 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30432 int ii = 0;
30434 if (parser->omp_declare_simd != NULL)
30436 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30437 " marked as a Cilk Plus SIMD-enabled function");
30438 XDELETE (parser->cilk_simd_fn_info);
30439 parser->cilk_simd_fn_info = NULL;
30440 return attrs;
30442 if (!info->error_seen && info->fndecl_seen)
30444 error ("vector attribute not immediately followed by a single function"
30445 " declaration or definition");
30446 info->error_seen = true;
30448 if (info->error_seen)
30449 return attrs;
30451 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
30453 tree c, cl;
30455 cp_parser_push_lexer_for_tokens (parser, ce);
30456 parser->lexer->in_pragma = true;
30457 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
30458 "SIMD-enabled functions attribute",
30459 NULL);
30460 cp_parser_pop_lexer (parser);
30461 if (cl)
30462 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30464 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
30465 TREE_CHAIN (c) = attrs;
30466 attrs = c;
30468 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30469 TREE_CHAIN (c) = attrs;
30470 if (processing_template_decl)
30471 ATTR_IS_DEPENDENT (c) = 1;
30472 attrs = c;
30474 info->fndecl_seen = true;
30475 XDELETE (parser->cilk_simd_fn_info);
30476 parser->cilk_simd_fn_info = NULL;
30477 return attrs;
30480 /* Finalize #pragma omp declare simd clauses after direct declarator has
30481 been parsed, and put that into "omp declare simd" attribute. */
30483 static tree
30484 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30486 struct cp_token_cache *ce;
30487 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30488 int i;
30490 if (!data->error_seen && data->fndecl_seen)
30492 error ("%<#pragma omp declare simd%> not immediately followed by "
30493 "a single function declaration or definition");
30494 data->error_seen = true;
30495 return attrs;
30497 if (data->error_seen)
30498 return attrs;
30500 FOR_EACH_VEC_ELT (data->tokens, i, ce)
30502 tree c, cl;
30504 cp_parser_push_lexer_for_tokens (parser, ce);
30505 parser->lexer->in_pragma = true;
30506 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30507 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30508 cp_lexer_consume_token (parser->lexer);
30509 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
30510 "#pragma omp declare simd", pragma_tok);
30511 cp_parser_pop_lexer (parser);
30512 if (cl)
30513 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30514 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30515 TREE_CHAIN (c) = attrs;
30516 if (processing_template_decl)
30517 ATTR_IS_DEPENDENT (c) = 1;
30518 attrs = c;
30521 data->fndecl_seen = true;
30522 return attrs;
30526 /* OpenMP 4.0:
30527 # pragma omp declare target new-line
30528 declarations and definitions
30529 # pragma omp end declare target new-line */
30531 static void
30532 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
30534 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30535 scope_chain->omp_declare_target_attribute++;
30538 static void
30539 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
30541 const char *p = "";
30542 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30544 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30545 p = IDENTIFIER_POINTER (id);
30547 if (strcmp (p, "declare") == 0)
30549 cp_lexer_consume_token (parser->lexer);
30550 p = "";
30551 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30553 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30554 p = IDENTIFIER_POINTER (id);
30556 if (strcmp (p, "target") == 0)
30557 cp_lexer_consume_token (parser->lexer);
30558 else
30560 cp_parser_error (parser, "expected %<target%>");
30561 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30562 return;
30565 else
30567 cp_parser_error (parser, "expected %<declare%>");
30568 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30569 return;
30571 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30572 if (!scope_chain->omp_declare_target_attribute)
30573 error_at (pragma_tok->location,
30574 "%<#pragma omp end declare target%> without corresponding "
30575 "%<#pragma omp declare target%>");
30576 else
30577 scope_chain->omp_declare_target_attribute--;
30580 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30581 expression and optional initializer clause of
30582 #pragma omp declare reduction. We store the expression(s) as
30583 either 3, 6 or 7 special statements inside of the artificial function's
30584 body. The first two statements are DECL_EXPRs for the artificial
30585 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30586 expression that uses those variables.
30587 If there was any INITIALIZER clause, this is followed by further statements,
30588 the fourth and fifth statements are DECL_EXPRs for the artificial
30589 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30590 constructor variant (first token after open paren is not omp_priv),
30591 then the sixth statement is a statement with the function call expression
30592 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30593 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30594 to initialize the OMP_PRIV artificial variable and there is seventh
30595 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30597 static bool
30598 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
30600 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
30601 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
30602 type = TREE_TYPE (type);
30603 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
30604 DECL_ARTIFICIAL (omp_out) = 1;
30605 pushdecl (omp_out);
30606 add_decl_expr (omp_out);
30607 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
30608 DECL_ARTIFICIAL (omp_in) = 1;
30609 pushdecl (omp_in);
30610 add_decl_expr (omp_in);
30611 tree combiner;
30612 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
30614 keep_next_level (true);
30615 tree block = begin_omp_structured_block ();
30616 combiner = cp_parser_expression (parser, false, NULL);
30617 finish_expr_stmt (combiner);
30618 block = finish_omp_structured_block (block);
30619 add_stmt (block);
30621 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30622 return false;
30624 const char *p = "";
30625 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30627 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30628 p = IDENTIFIER_POINTER (id);
30631 if (strcmp (p, "initializer") == 0)
30633 cp_lexer_consume_token (parser->lexer);
30634 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30635 return false;
30637 p = "";
30638 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30640 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30641 p = IDENTIFIER_POINTER (id);
30644 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
30645 DECL_ARTIFICIAL (omp_priv) = 1;
30646 pushdecl (omp_priv);
30647 add_decl_expr (omp_priv);
30648 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
30649 DECL_ARTIFICIAL (omp_orig) = 1;
30650 pushdecl (omp_orig);
30651 add_decl_expr (omp_orig);
30653 keep_next_level (true);
30654 block = begin_omp_structured_block ();
30656 bool ctor = false;
30657 if (strcmp (p, "omp_priv") == 0)
30659 bool is_direct_init, is_non_constant_init;
30660 ctor = true;
30661 cp_lexer_consume_token (parser->lexer);
30662 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30663 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
30664 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30665 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
30666 == CPP_CLOSE_PAREN
30667 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
30668 == CPP_CLOSE_PAREN))
30670 finish_omp_structured_block (block);
30671 error ("invalid initializer clause");
30672 return false;
30674 initializer = cp_parser_initializer (parser, &is_direct_init,
30675 &is_non_constant_init);
30676 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
30677 NULL_TREE, LOOKUP_ONLYCONVERTING);
30679 else
30681 cp_parser_parse_tentatively (parser);
30682 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
30683 /*check_dependency_p=*/true,
30684 /*template_p=*/NULL,
30685 /*declarator_p=*/false,
30686 /*optional_p=*/false);
30687 vec<tree, va_gc> *args;
30688 if (fn_name == error_mark_node
30689 || cp_parser_error_occurred (parser)
30690 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30691 || ((args = cp_parser_parenthesized_expression_list
30692 (parser, non_attr, /*cast_p=*/false,
30693 /*allow_expansion_p=*/true,
30694 /*non_constant_p=*/NULL)),
30695 cp_parser_error_occurred (parser)))
30697 finish_omp_structured_block (block);
30698 cp_parser_abort_tentative_parse (parser);
30699 cp_parser_error (parser, "expected id-expression (arguments)");
30700 return false;
30702 unsigned int i;
30703 tree arg;
30704 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
30705 if (arg == omp_priv
30706 || (TREE_CODE (arg) == ADDR_EXPR
30707 && TREE_OPERAND (arg, 0) == omp_priv))
30708 break;
30709 cp_parser_abort_tentative_parse (parser);
30710 if (arg == NULL_TREE)
30711 error ("one of the initializer call arguments should be %<omp_priv%>"
30712 " or %<&omp_priv%>");
30713 initializer = cp_parser_postfix_expression (parser, false, false, false,
30714 false, NULL);
30715 finish_expr_stmt (initializer);
30718 block = finish_omp_structured_block (block);
30719 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
30720 finish_expr_stmt (block);
30722 if (ctor)
30723 add_decl_expr (omp_orig);
30725 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30726 return false;
30729 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
30730 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
30732 return true;
30735 /* OpenMP 4.0
30736 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30737 initializer-clause[opt] new-line
30739 initializer-clause:
30740 initializer (omp_priv initializer)
30741 initializer (function-name (argument-list)) */
30743 static void
30744 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
30745 enum pragma_context)
30747 auto_vec<tree> types;
30748 enum tree_code reduc_code = ERROR_MARK;
30749 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
30750 unsigned int i;
30751 cp_token *first_token;
30752 cp_token_cache *cp;
30753 int errs;
30754 void *p;
30756 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
30757 p = obstack_alloc (&declarator_obstack, 0);
30759 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30760 goto fail;
30762 switch (cp_lexer_peek_token (parser->lexer)->type)
30764 case CPP_PLUS:
30765 reduc_code = PLUS_EXPR;
30766 break;
30767 case CPP_MULT:
30768 reduc_code = MULT_EXPR;
30769 break;
30770 case CPP_MINUS:
30771 reduc_code = MINUS_EXPR;
30772 break;
30773 case CPP_AND:
30774 reduc_code = BIT_AND_EXPR;
30775 break;
30776 case CPP_XOR:
30777 reduc_code = BIT_XOR_EXPR;
30778 break;
30779 case CPP_OR:
30780 reduc_code = BIT_IOR_EXPR;
30781 break;
30782 case CPP_AND_AND:
30783 reduc_code = TRUTH_ANDIF_EXPR;
30784 break;
30785 case CPP_OR_OR:
30786 reduc_code = TRUTH_ORIF_EXPR;
30787 break;
30788 case CPP_NAME:
30789 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
30790 break;
30791 default:
30792 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
30793 "%<|%>, %<&&%>, %<||%> or identifier");
30794 goto fail;
30797 if (reduc_code != ERROR_MARK)
30798 cp_lexer_consume_token (parser->lexer);
30800 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
30801 if (reduc_id == error_mark_node)
30802 goto fail;
30804 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30805 goto fail;
30807 /* Types may not be defined in declare reduction type list. */
30808 const char *saved_message;
30809 saved_message = parser->type_definition_forbidden_message;
30810 parser->type_definition_forbidden_message
30811 = G_("types may not be defined in declare reduction type list");
30812 bool saved_colon_corrects_to_scope_p;
30813 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30814 parser->colon_corrects_to_scope_p = false;
30815 bool saved_colon_doesnt_start_class_def_p;
30816 saved_colon_doesnt_start_class_def_p
30817 = parser->colon_doesnt_start_class_def_p;
30818 parser->colon_doesnt_start_class_def_p = true;
30820 while (true)
30822 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30823 type = cp_parser_type_id (parser);
30824 if (type == error_mark_node)
30826 else if (ARITHMETIC_TYPE_P (type)
30827 && (orig_reduc_id == NULL_TREE
30828 || (TREE_CODE (type) != COMPLEX_TYPE
30829 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30830 "min") == 0
30831 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
30832 "max") == 0))))
30833 error_at (loc, "predeclared arithmetic type %qT in "
30834 "%<#pragma omp declare reduction%>", type);
30835 else if (TREE_CODE (type) == FUNCTION_TYPE
30836 || TREE_CODE (type) == METHOD_TYPE
30837 || TREE_CODE (type) == ARRAY_TYPE)
30838 error_at (loc, "function or array type %qT in "
30839 "%<#pragma omp declare reduction%>", type);
30840 else if (TREE_CODE (type) == REFERENCE_TYPE)
30841 error_at (loc, "reference type %qT in "
30842 "%<#pragma omp declare reduction%>", type);
30843 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
30844 error_at (loc, "const, volatile or __restrict qualified type %qT in "
30845 "%<#pragma omp declare reduction%>", type);
30846 else
30847 types.safe_push (type);
30849 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30850 cp_lexer_consume_token (parser->lexer);
30851 else
30852 break;
30855 /* Restore the saved message. */
30856 parser->type_definition_forbidden_message = saved_message;
30857 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30858 parser->colon_doesnt_start_class_def_p
30859 = saved_colon_doesnt_start_class_def_p;
30861 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
30862 || types.is_empty ())
30864 fail:
30865 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30866 goto done;
30869 first_token = cp_lexer_peek_token (parser->lexer);
30870 cp = NULL;
30871 errs = errorcount;
30872 FOR_EACH_VEC_ELT (types, i, type)
30874 tree fntype
30875 = build_function_type_list (void_type_node,
30876 cp_build_reference_type (type, false),
30877 NULL_TREE);
30878 tree this_reduc_id = reduc_id;
30879 if (!dependent_type_p (type))
30880 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
30881 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
30882 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
30883 DECL_ARTIFICIAL (fndecl) = 1;
30884 DECL_EXTERNAL (fndecl) = 1;
30885 DECL_DECLARED_INLINE_P (fndecl) = 1;
30886 DECL_IGNORED_P (fndecl) = 1;
30887 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
30888 DECL_ATTRIBUTES (fndecl)
30889 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
30890 DECL_ATTRIBUTES (fndecl));
30891 if (processing_template_decl)
30892 fndecl = push_template_decl (fndecl);
30893 bool block_scope = false;
30894 tree block = NULL_TREE;
30895 if (current_function_decl)
30897 block_scope = true;
30898 DECL_CONTEXT (fndecl) = global_namespace;
30899 if (!processing_template_decl)
30900 pushdecl (fndecl);
30902 else if (current_class_type)
30904 if (cp == NULL)
30906 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30907 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30908 cp_lexer_consume_token (parser->lexer);
30909 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30910 goto fail;
30911 cp = cp_token_cache_new (first_token,
30912 cp_lexer_peek_nth_token (parser->lexer,
30913 2));
30915 DECL_STATIC_FUNCTION_P (fndecl) = 1;
30916 finish_member_declaration (fndecl);
30917 DECL_PENDING_INLINE_INFO (fndecl) = cp;
30918 DECL_PENDING_INLINE_P (fndecl) = 1;
30919 vec_safe_push (unparsed_funs_with_definitions, fndecl);
30920 continue;
30922 else
30924 DECL_CONTEXT (fndecl) = current_namespace;
30925 pushdecl (fndecl);
30927 if (!block_scope)
30928 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
30929 else
30930 block = begin_omp_structured_block ();
30931 if (cp)
30933 cp_parser_push_lexer_for_tokens (parser, cp);
30934 parser->lexer->in_pragma = true;
30936 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
30938 if (!block_scope)
30939 finish_function (0);
30940 else
30941 DECL_CONTEXT (fndecl) = current_function_decl;
30942 if (cp)
30943 cp_parser_pop_lexer (parser);
30944 goto fail;
30946 if (cp)
30947 cp_parser_pop_lexer (parser);
30948 if (!block_scope)
30949 finish_function (0);
30950 else
30952 DECL_CONTEXT (fndecl) = current_function_decl;
30953 block = finish_omp_structured_block (block);
30954 if (TREE_CODE (block) == BIND_EXPR)
30955 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
30956 else if (TREE_CODE (block) == STATEMENT_LIST)
30957 DECL_SAVED_TREE (fndecl) = block;
30958 if (processing_template_decl)
30959 add_decl_expr (fndecl);
30961 cp_check_omp_declare_reduction (fndecl);
30962 if (cp == NULL && types.length () > 1)
30963 cp = cp_token_cache_new (first_token,
30964 cp_lexer_peek_nth_token (parser->lexer, 2));
30965 if (errs != errorcount)
30966 break;
30969 cp_parser_require_pragma_eol (parser, pragma_tok);
30971 done:
30972 /* Free any declarators allocated. */
30973 obstack_free (&declarator_obstack, p);
30976 /* OpenMP 4.0
30977 #pragma omp declare simd declare-simd-clauses[optseq] new-line
30978 #pragma omp declare reduction (reduction-id : typename-list : expression) \
30979 initializer-clause[opt] new-line
30980 #pragma omp declare target new-line */
30982 static void
30983 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
30984 enum pragma_context context)
30986 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30988 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30989 const char *p = IDENTIFIER_POINTER (id);
30991 if (strcmp (p, "simd") == 0)
30993 cp_lexer_consume_token (parser->lexer);
30994 cp_parser_omp_declare_simd (parser, pragma_tok,
30995 context);
30996 return;
30998 cp_ensure_no_omp_declare_simd (parser);
30999 if (strcmp (p, "reduction") == 0)
31001 cp_lexer_consume_token (parser->lexer);
31002 cp_parser_omp_declare_reduction (parser, pragma_tok,
31003 context);
31004 return;
31006 if (!flag_openmp) /* flag_openmp_simd */
31008 cp_parser_require_pragma_eol (parser, pragma_tok);
31009 return;
31011 if (strcmp (p, "target") == 0)
31013 cp_lexer_consume_token (parser->lexer);
31014 cp_parser_omp_declare_target (parser, pragma_tok);
31015 return;
31018 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31019 "or %<target%>");
31020 cp_parser_require_pragma_eol (parser, pragma_tok);
31023 /* Main entry point to OpenMP statement pragmas. */
31025 static void
31026 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31028 tree stmt;
31029 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31030 omp_clause_mask mask (0);
31032 switch (pragma_tok->pragma_kind)
31034 case PRAGMA_OMP_ATOMIC:
31035 cp_parser_omp_atomic (parser, pragma_tok);
31036 return;
31037 case PRAGMA_OMP_CRITICAL:
31038 stmt = cp_parser_omp_critical (parser, pragma_tok);
31039 break;
31040 case PRAGMA_OMP_DISTRIBUTE:
31041 strcpy (p_name, "#pragma omp");
31042 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31043 break;
31044 case PRAGMA_OMP_FOR:
31045 strcpy (p_name, "#pragma omp");
31046 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31047 break;
31048 case PRAGMA_OMP_MASTER:
31049 stmt = cp_parser_omp_master (parser, pragma_tok);
31050 break;
31051 case PRAGMA_OMP_ORDERED:
31052 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31053 break;
31054 case PRAGMA_OMP_PARALLEL:
31055 strcpy (p_name, "#pragma omp");
31056 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31057 break;
31058 case PRAGMA_OMP_SECTIONS:
31059 strcpy (p_name, "#pragma omp");
31060 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31061 break;
31062 case PRAGMA_OMP_SIMD:
31063 strcpy (p_name, "#pragma omp");
31064 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31065 break;
31066 case PRAGMA_OMP_SINGLE:
31067 stmt = cp_parser_omp_single (parser, pragma_tok);
31068 break;
31069 case PRAGMA_OMP_TASK:
31070 stmt = cp_parser_omp_task (parser, pragma_tok);
31071 break;
31072 case PRAGMA_OMP_TASKGROUP:
31073 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31074 break;
31075 case PRAGMA_OMP_TEAMS:
31076 strcpy (p_name, "#pragma omp");
31077 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31078 break;
31079 default:
31080 gcc_unreachable ();
31083 if (stmt)
31084 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31087 /* Transactional Memory parsing routines. */
31089 /* Parse a transaction attribute.
31091 txn-attribute:
31092 attribute
31093 [ [ identifier ] ]
31095 ??? Simplify this when C++0x bracket attributes are
31096 implemented properly. */
31098 static tree
31099 cp_parser_txn_attribute_opt (cp_parser *parser)
31101 cp_token *token;
31102 tree attr_name, attr = NULL;
31104 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31105 return cp_parser_attributes_opt (parser);
31107 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31108 return NULL_TREE;
31109 cp_lexer_consume_token (parser->lexer);
31110 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31111 goto error1;
31113 token = cp_lexer_peek_token (parser->lexer);
31114 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31116 token = cp_lexer_consume_token (parser->lexer);
31118 attr_name = (token->type == CPP_KEYWORD
31119 /* For keywords, use the canonical spelling,
31120 not the parsed identifier. */
31121 ? ridpointers[(int) token->keyword]
31122 : token->u.value);
31123 attr = build_tree_list (attr_name, NULL_TREE);
31125 else
31126 cp_parser_error (parser, "expected identifier");
31128 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31129 error1:
31130 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31131 return attr;
31134 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31136 transaction-statement:
31137 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31138 compound-statement
31139 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31142 static tree
31143 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31145 unsigned char old_in = parser->in_transaction;
31146 unsigned char this_in = 1, new_in;
31147 cp_token *token;
31148 tree stmt, attrs, noex;
31150 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31151 || keyword == RID_TRANSACTION_RELAXED);
31152 token = cp_parser_require_keyword (parser, keyword,
31153 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31154 : RT_TRANSACTION_RELAXED));
31155 gcc_assert (token != NULL);
31157 if (keyword == RID_TRANSACTION_RELAXED)
31158 this_in |= TM_STMT_ATTR_RELAXED;
31159 else
31161 attrs = cp_parser_txn_attribute_opt (parser);
31162 if (attrs)
31163 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31166 /* Parse a noexcept specification. */
31167 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31169 /* Keep track if we're in the lexical scope of an outer transaction. */
31170 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31172 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31174 parser->in_transaction = new_in;
31175 cp_parser_compound_statement (parser, NULL, false, false);
31176 parser->in_transaction = old_in;
31178 finish_transaction_stmt (stmt, NULL, this_in, noex);
31180 return stmt;
31183 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31185 transaction-expression:
31186 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31187 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31190 static tree
31191 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31193 unsigned char old_in = parser->in_transaction;
31194 unsigned char this_in = 1;
31195 cp_token *token;
31196 tree expr, noex;
31197 bool noex_expr;
31199 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31200 || keyword == RID_TRANSACTION_RELAXED);
31202 if (!flag_tm)
31203 error (keyword == RID_TRANSACTION_RELAXED
31204 ? G_("%<__transaction_relaxed%> without transactional memory "
31205 "support enabled")
31206 : G_("%<__transaction_atomic%> without transactional memory "
31207 "support enabled"));
31209 token = cp_parser_require_keyword (parser, keyword,
31210 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31211 : RT_TRANSACTION_RELAXED));
31212 gcc_assert (token != NULL);
31214 if (keyword == RID_TRANSACTION_RELAXED)
31215 this_in |= TM_STMT_ATTR_RELAXED;
31217 /* Set this early. This might mean that we allow transaction_cancel in
31218 an expression that we find out later actually has to be a constexpr.
31219 However, we expect that cxx_constant_value will be able to deal with
31220 this; also, if the noexcept has no constexpr, then what we parse next
31221 really is a transaction's body. */
31222 parser->in_transaction = this_in;
31224 /* Parse a noexcept specification. */
31225 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31226 true);
31228 if (!noex || !noex_expr
31229 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31231 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31233 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
31234 expr = finish_parenthesized_expr (expr);
31236 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31238 else
31240 /* The only expression that is available got parsed for the noexcept
31241 already. noexcept is true then. */
31242 expr = noex;
31243 noex = boolean_true_node;
31246 expr = build_transaction_expr (token->location, expr, this_in, noex);
31247 parser->in_transaction = old_in;
31249 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31250 return error_mark_node;
31252 return (flag_tm ? expr : error_mark_node);
31255 /* Parse a function-transaction-block.
31257 function-transaction-block:
31258 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31259 function-body
31260 __transaction_atomic txn-attribute[opt] function-try-block
31261 __transaction_relaxed ctor-initializer[opt] function-body
31262 __transaction_relaxed function-try-block
31265 static bool
31266 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31268 unsigned char old_in = parser->in_transaction;
31269 unsigned char new_in = 1;
31270 tree compound_stmt, stmt, attrs;
31271 bool ctor_initializer_p;
31272 cp_token *token;
31274 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31275 || keyword == RID_TRANSACTION_RELAXED);
31276 token = cp_parser_require_keyword (parser, keyword,
31277 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31278 : RT_TRANSACTION_RELAXED));
31279 gcc_assert (token != NULL);
31281 if (keyword == RID_TRANSACTION_RELAXED)
31282 new_in |= TM_STMT_ATTR_RELAXED;
31283 else
31285 attrs = cp_parser_txn_attribute_opt (parser);
31286 if (attrs)
31287 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31290 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31292 parser->in_transaction = new_in;
31294 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31295 ctor_initializer_p = cp_parser_function_try_block (parser);
31296 else
31297 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31298 (parser, /*in_function_try_block=*/false);
31300 parser->in_transaction = old_in;
31302 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31304 return ctor_initializer_p;
31307 /* Parse a __transaction_cancel statement.
31309 cancel-statement:
31310 __transaction_cancel txn-attribute[opt] ;
31311 __transaction_cancel txn-attribute[opt] throw-expression ;
31313 ??? Cancel and throw is not yet implemented. */
31315 static tree
31316 cp_parser_transaction_cancel (cp_parser *parser)
31318 cp_token *token;
31319 bool is_outer = false;
31320 tree stmt, attrs;
31322 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31323 RT_TRANSACTION_CANCEL);
31324 gcc_assert (token != NULL);
31326 attrs = cp_parser_txn_attribute_opt (parser);
31327 if (attrs)
31328 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31330 /* ??? Parse cancel-and-throw here. */
31332 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31334 if (!flag_tm)
31336 error_at (token->location, "%<__transaction_cancel%> without "
31337 "transactional memory support enabled");
31338 return error_mark_node;
31340 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31342 error_at (token->location, "%<__transaction_cancel%> within a "
31343 "%<__transaction_relaxed%>");
31344 return error_mark_node;
31346 else if (is_outer)
31348 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31349 && !is_tm_may_cancel_outer (current_function_decl))
31351 error_at (token->location, "outer %<__transaction_cancel%> not "
31352 "within outer %<__transaction_atomic%>");
31353 error_at (token->location,
31354 " or a %<transaction_may_cancel_outer%> function");
31355 return error_mark_node;
31358 else if (parser->in_transaction == 0)
31360 error_at (token->location, "%<__transaction_cancel%> not within "
31361 "%<__transaction_atomic%>");
31362 return error_mark_node;
31365 stmt = build_tm_abort_call (token->location, is_outer);
31366 add_stmt (stmt);
31368 return stmt;
31371 /* The parser. */
31373 static GTY (()) cp_parser *the_parser;
31376 /* Special handling for the first token or line in the file. The first
31377 thing in the file might be #pragma GCC pch_preprocess, which loads a
31378 PCH file, which is a GC collection point. So we need to handle this
31379 first pragma without benefit of an existing lexer structure.
31381 Always returns one token to the caller in *FIRST_TOKEN. This is
31382 either the true first token of the file, or the first token after
31383 the initial pragma. */
31385 static void
31386 cp_parser_initial_pragma (cp_token *first_token)
31388 tree name = NULL;
31390 cp_lexer_get_preprocessor_token (NULL, first_token);
31391 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31392 return;
31394 cp_lexer_get_preprocessor_token (NULL, first_token);
31395 if (first_token->type == CPP_STRING)
31397 name = first_token->u.value;
31399 cp_lexer_get_preprocessor_token (NULL, first_token);
31400 if (first_token->type != CPP_PRAGMA_EOL)
31401 error_at (first_token->location,
31402 "junk at end of %<#pragma GCC pch_preprocess%>");
31404 else
31405 error_at (first_token->location, "expected string literal");
31407 /* Skip to the end of the pragma. */
31408 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31409 cp_lexer_get_preprocessor_token (NULL, first_token);
31411 /* Now actually load the PCH file. */
31412 if (name)
31413 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31415 /* Read one more token to return to our caller. We have to do this
31416 after reading the PCH file in, since its pointers have to be
31417 live. */
31418 cp_lexer_get_preprocessor_token (NULL, first_token);
31421 /* Normal parsing of a pragma token. Here we can (and must) use the
31422 regular lexer. */
31424 static bool
31425 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31427 cp_token *pragma_tok;
31428 unsigned int id;
31430 pragma_tok = cp_lexer_consume_token (parser->lexer);
31431 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31432 parser->lexer->in_pragma = true;
31434 id = pragma_tok->pragma_kind;
31435 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31436 cp_ensure_no_omp_declare_simd (parser);
31437 switch (id)
31439 case PRAGMA_GCC_PCH_PREPROCESS:
31440 error_at (pragma_tok->location,
31441 "%<#pragma GCC pch_preprocess%> must be first");
31442 break;
31444 case PRAGMA_OMP_BARRIER:
31445 switch (context)
31447 case pragma_compound:
31448 cp_parser_omp_barrier (parser, pragma_tok);
31449 return false;
31450 case pragma_stmt:
31451 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31452 "used in compound statements");
31453 break;
31454 default:
31455 goto bad_stmt;
31457 break;
31459 case PRAGMA_OMP_FLUSH:
31460 switch (context)
31462 case pragma_compound:
31463 cp_parser_omp_flush (parser, pragma_tok);
31464 return false;
31465 case pragma_stmt:
31466 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31467 "used in compound statements");
31468 break;
31469 default:
31470 goto bad_stmt;
31472 break;
31474 case PRAGMA_OMP_TASKWAIT:
31475 switch (context)
31477 case pragma_compound:
31478 cp_parser_omp_taskwait (parser, pragma_tok);
31479 return false;
31480 case pragma_stmt:
31481 error_at (pragma_tok->location,
31482 "%<#pragma omp taskwait%> may only be "
31483 "used in compound statements");
31484 break;
31485 default:
31486 goto bad_stmt;
31488 break;
31490 case PRAGMA_OMP_TASKYIELD:
31491 switch (context)
31493 case pragma_compound:
31494 cp_parser_omp_taskyield (parser, pragma_tok);
31495 return false;
31496 case pragma_stmt:
31497 error_at (pragma_tok->location,
31498 "%<#pragma omp taskyield%> may only be "
31499 "used in compound statements");
31500 break;
31501 default:
31502 goto bad_stmt;
31504 break;
31506 case PRAGMA_OMP_CANCEL:
31507 switch (context)
31509 case pragma_compound:
31510 cp_parser_omp_cancel (parser, pragma_tok);
31511 return false;
31512 case pragma_stmt:
31513 error_at (pragma_tok->location,
31514 "%<#pragma omp cancel%> may only be "
31515 "used in compound statements");
31516 break;
31517 default:
31518 goto bad_stmt;
31520 break;
31522 case PRAGMA_OMP_CANCELLATION_POINT:
31523 switch (context)
31525 case pragma_compound:
31526 cp_parser_omp_cancellation_point (parser, pragma_tok);
31527 return false;
31528 case pragma_stmt:
31529 error_at (pragma_tok->location,
31530 "%<#pragma omp cancellation point%> may only be "
31531 "used in compound statements");
31532 break;
31533 default:
31534 goto bad_stmt;
31536 break;
31538 case PRAGMA_OMP_THREADPRIVATE:
31539 cp_parser_omp_threadprivate (parser, pragma_tok);
31540 return false;
31542 case PRAGMA_OMP_DECLARE_REDUCTION:
31543 cp_parser_omp_declare (parser, pragma_tok, context);
31544 return false;
31546 case PRAGMA_OMP_ATOMIC:
31547 case PRAGMA_OMP_CRITICAL:
31548 case PRAGMA_OMP_DISTRIBUTE:
31549 case PRAGMA_OMP_FOR:
31550 case PRAGMA_OMP_MASTER:
31551 case PRAGMA_OMP_ORDERED:
31552 case PRAGMA_OMP_PARALLEL:
31553 case PRAGMA_OMP_SECTIONS:
31554 case PRAGMA_OMP_SIMD:
31555 case PRAGMA_OMP_SINGLE:
31556 case PRAGMA_OMP_TASK:
31557 case PRAGMA_OMP_TASKGROUP:
31558 case PRAGMA_OMP_TEAMS:
31559 if (context != pragma_stmt && context != pragma_compound)
31560 goto bad_stmt;
31561 cp_parser_omp_construct (parser, pragma_tok);
31562 return true;
31564 case PRAGMA_OMP_TARGET:
31565 return cp_parser_omp_target (parser, pragma_tok, context);
31567 case PRAGMA_OMP_END_DECLARE_TARGET:
31568 cp_parser_omp_end_declare_target (parser, pragma_tok);
31569 return false;
31571 case PRAGMA_OMP_SECTION:
31572 error_at (pragma_tok->location,
31573 "%<#pragma omp section%> may only be used in "
31574 "%<#pragma omp sections%> construct");
31575 break;
31577 case PRAGMA_IVDEP:
31579 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31580 cp_token *tok;
31581 tok = cp_lexer_peek_token (the_parser->lexer);
31582 if (tok->type != CPP_KEYWORD
31583 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
31584 && tok->keyword != RID_DO))
31586 cp_parser_error (parser, "for, while or do statement expected");
31587 return false;
31589 cp_parser_iteration_statement (parser, true);
31590 return true;
31593 case PRAGMA_CILK_SIMD:
31594 if (context == pragma_external)
31596 error_at (pragma_tok->location,
31597 "%<#pragma simd%> must be inside a function");
31598 break;
31600 cp_parser_cilk_simd (parser, pragma_tok);
31601 return true;
31603 default:
31604 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
31605 c_invoke_pragma_handler (id);
31606 break;
31608 bad_stmt:
31609 cp_parser_error (parser, "expected declaration specifiers");
31610 break;
31613 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31614 return false;
31617 /* The interface the pragma parsers have to the lexer. */
31619 enum cpp_ttype
31620 pragma_lex (tree *value)
31622 cp_token *tok;
31623 enum cpp_ttype ret;
31625 tok = cp_lexer_peek_token (the_parser->lexer);
31627 ret = tok->type;
31628 *value = tok->u.value;
31630 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
31631 ret = CPP_EOF;
31632 else if (ret == CPP_STRING)
31633 *value = cp_parser_string_literal (the_parser, false, false);
31634 else
31636 cp_lexer_consume_token (the_parser->lexer);
31637 if (ret == CPP_KEYWORD)
31638 ret = CPP_NAME;
31641 return ret;
31645 /* External interface. */
31647 /* Parse one entire translation unit. */
31649 void
31650 c_parse_file (void)
31652 static bool already_called = false;
31654 if (already_called)
31656 sorry ("inter-module optimizations not implemented for C++");
31657 return;
31659 already_called = true;
31661 the_parser = cp_parser_new ();
31662 push_deferring_access_checks (flag_access_control
31663 ? dk_no_deferred : dk_no_check);
31664 cp_parser_translation_unit (the_parser);
31665 the_parser = NULL;
31668 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
31669 vectorlength clause:
31670 Syntax:
31671 vectorlength ( constant-expression ) */
31673 static tree
31674 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
31675 bool is_simd_fn)
31677 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31678 tree expr;
31679 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
31680 safelen clause. Thus, vectorlength is represented as OMP 4.0
31681 safelen. For SIMD-enabled function it is represented by OMP 4.0
31682 simdlen. */
31683 if (!is_simd_fn)
31684 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
31685 loc);
31686 else
31687 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
31688 loc);
31690 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31691 return error_mark_node;
31693 expr = cp_parser_constant_expression (parser, false, NULL);
31694 expr = maybe_constant_value (expr);
31696 /* If expr == error_mark_node, then don't emit any errors nor
31697 create a clause. if any of the above functions returns
31698 error mark node then they would have emitted an error message. */
31699 if (expr == error_mark_node)
31701 else if (!TREE_TYPE (expr)
31702 || !TREE_CONSTANT (expr)
31703 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
31704 error_at (loc, "vectorlength must be an integer constant");
31705 else if (TREE_CONSTANT (expr)
31706 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
31707 error_at (loc, "vectorlength must be a power of 2");
31708 else
31710 tree c;
31711 if (!is_simd_fn)
31713 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
31714 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
31715 OMP_CLAUSE_CHAIN (c) = clauses;
31716 clauses = c;
31718 else
31720 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
31721 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
31722 OMP_CLAUSE_CHAIN (c) = clauses;
31723 clauses = c;
31727 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31728 return error_mark_node;
31729 return clauses;
31732 /* Handles the Cilk Plus #pragma simd linear clause.
31733 Syntax:
31734 linear ( simd-linear-variable-list )
31736 simd-linear-variable-list:
31737 simd-linear-variable
31738 simd-linear-variable-list , simd-linear-variable
31740 simd-linear-variable:
31741 id-expression
31742 id-expression : simd-linear-step
31744 simd-linear-step:
31745 conditional-expression */
31747 static tree
31748 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
31750 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31752 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31753 return clauses;
31754 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31756 cp_parser_error (parser, "expected identifier");
31757 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31758 return error_mark_node;
31761 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31762 parser->colon_corrects_to_scope_p = false;
31763 while (1)
31765 cp_token *token = cp_lexer_peek_token (parser->lexer);
31766 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31768 cp_parser_error (parser, "expected variable-name");
31769 clauses = error_mark_node;
31770 break;
31773 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
31774 false, false);
31775 tree decl = cp_parser_lookup_name_simple (parser, var_name,
31776 token->location);
31777 if (decl == error_mark_node)
31779 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
31780 token->location);
31781 clauses = error_mark_node;
31783 else
31785 tree e = NULL_TREE;
31786 tree step_size = integer_one_node;
31788 /* If present, parse the linear step. Otherwise, assume the default
31789 value of 1. */
31790 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
31792 cp_lexer_consume_token (parser->lexer);
31794 e = cp_parser_assignment_expression (parser, false, NULL);
31795 e = maybe_constant_value (e);
31797 if (e == error_mark_node)
31799 /* If an error has occurred, then the whole pragma is
31800 considered ill-formed. Thus, no reason to keep
31801 parsing. */
31802 clauses = error_mark_node;
31803 break;
31805 else if (type_dependent_expression_p (e)
31806 || value_dependent_expression_p (e)
31807 || (TREE_TYPE (e)
31808 && INTEGRAL_TYPE_P (TREE_TYPE (e))
31809 && (TREE_CONSTANT (e)
31810 || DECL_P (e))))
31811 step_size = e;
31812 else
31813 cp_parser_error (parser,
31814 "step size must be an integer constant "
31815 "expression or an integer variable");
31818 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
31819 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
31820 OMP_CLAUSE_DECL (l) = decl;
31821 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
31822 OMP_CLAUSE_CHAIN (l) = clauses;
31823 clauses = l;
31825 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31826 cp_lexer_consume_token (parser->lexer);
31827 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31828 break;
31829 else
31831 error_at (cp_lexer_peek_token (parser->lexer)->location,
31832 "expected %<,%> or %<)%> after %qE", decl);
31833 clauses = error_mark_node;
31834 break;
31837 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31838 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31839 return clauses;
31842 /* Returns the name of the next clause. If the clause is not
31843 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
31844 token is not consumed. Otherwise, the appropriate enum from the
31845 pragma_simd_clause is returned and the token is consumed. */
31847 static pragma_omp_clause
31848 cp_parser_cilk_simd_clause_name (cp_parser *parser)
31850 pragma_omp_clause clause_type;
31851 cp_token *token = cp_lexer_peek_token (parser->lexer);
31853 if (token->keyword == RID_PRIVATE)
31854 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
31855 else if (!token->u.value || token->type != CPP_NAME)
31856 return PRAGMA_CILK_CLAUSE_NONE;
31857 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
31858 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
31859 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
31860 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
31861 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
31862 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
31863 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
31864 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
31865 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
31866 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
31867 else
31868 return PRAGMA_CILK_CLAUSE_NONE;
31870 cp_lexer_consume_token (parser->lexer);
31871 return clause_type;
31874 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
31876 static tree
31877 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
31879 tree clauses = NULL_TREE;
31881 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31882 && clauses != error_mark_node)
31884 pragma_omp_clause c_kind;
31885 c_kind = cp_parser_cilk_simd_clause_name (parser);
31886 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
31887 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
31888 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
31889 clauses = cp_parser_cilk_simd_linear (parser, clauses);
31890 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
31891 /* Use the OpenMP 4.0 equivalent function. */
31892 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
31893 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
31894 /* Use the OpenMP 4.0 equivalent function. */
31895 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
31896 clauses);
31897 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
31898 /* Use the OMP 4.0 equivalent function. */
31899 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
31900 clauses);
31901 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
31902 /* Use the OMP 4.0 equivalent function. */
31903 clauses = cp_parser_omp_clause_reduction (parser, clauses);
31904 else
31906 clauses = error_mark_node;
31907 cp_parser_error (parser, "expected %<#pragma simd%> clause");
31908 break;
31912 cp_parser_skip_to_pragma_eol (parser, pragma_token);
31914 if (clauses == error_mark_node)
31915 return error_mark_node;
31916 else
31917 return c_finish_cilk_clauses (clauses);
31920 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
31922 static void
31923 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
31925 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
31927 if (clauses == error_mark_node)
31928 return;
31930 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
31932 error_at (cp_lexer_peek_token (parser->lexer)->location,
31933 "for statement expected");
31934 return;
31937 tree sb = begin_omp_structured_block ();
31938 int save = cp_parser_begin_omp_structured_block (parser);
31939 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
31940 if (ret)
31941 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
31942 cp_parser_end_omp_structured_block (parser, save);
31943 add_stmt (finish_omp_structured_block (sb));
31944 return;
31947 /* Create an identifier for a generic parameter type (a synthesized
31948 template parameter implied by `auto' or a concept identifier). */
31950 static GTY(()) int generic_parm_count;
31951 static tree
31952 make_generic_type_name ()
31954 char buf[32];
31955 sprintf (buf, "auto:%d", ++generic_parm_count);
31956 return get_identifier (buf);
31959 /* Predicate that behaves as is_auto_or_concept but matches the parent
31960 node of the generic type rather than the generic type itself. This
31961 allows for type transformation in add_implicit_template_parms. */
31963 static inline bool
31964 tree_type_is_auto_or_concept (const_tree t)
31966 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
31969 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
31970 (creating a new template parameter list if necessary). Returns the newly
31971 created template type parm. */
31973 tree
31974 synthesize_implicit_template_parm (cp_parser *parser)
31976 gcc_assert (current_binding_level->kind == sk_function_parms);
31978 /* We are either continuing a function template that already contains implicit
31979 template parameters, creating a new fully-implicit function template, or
31980 extending an existing explicit function template with implicit template
31981 parameters. */
31983 cp_binding_level *const entry_scope = current_binding_level;
31985 bool become_template = false;
31986 cp_binding_level *parent_scope = 0;
31988 if (parser->implicit_template_scope)
31990 gcc_assert (parser->implicit_template_parms);
31992 current_binding_level = parser->implicit_template_scope;
31994 else
31996 /* Roll back to the existing template parameter scope (in the case of
31997 extending an explicit function template) or introduce a new template
31998 parameter scope ahead of the function parameter scope (or class scope
31999 in the case of out-of-line member definitions). The function scope is
32000 added back after template parameter synthesis below. */
32002 cp_binding_level *scope = entry_scope;
32004 while (scope->kind == sk_function_parms)
32006 parent_scope = scope;
32007 scope = scope->level_chain;
32009 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32011 /* If not defining a class, then any class scope is a scope level in
32012 an out-of-line member definition. In this case simply wind back
32013 beyond the first such scope to inject the template parameter list.
32014 Otherwise wind back to the class being defined. The latter can
32015 occur in class member friend declarations such as:
32017 class A {
32018 void foo (auto);
32020 class B {
32021 friend void A::foo (auto);
32024 The template parameter list synthesized for the friend declaration
32025 must be injected in the scope of 'B'. This can also occur in
32026 erroneous cases such as:
32028 struct A {
32029 struct B {
32030 void foo (auto);
32032 void B::foo (auto) {}
32035 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32036 but, nevertheless, the template parameter list synthesized for the
32037 declarator should be injected into the scope of 'A' as if the
32038 ill-formed template was specified explicitly. */
32040 while (scope->kind == sk_class && !scope->defining_class_p)
32042 parent_scope = scope;
32043 scope = scope->level_chain;
32047 current_binding_level = scope;
32049 if (scope->kind != sk_template_parms
32050 || !function_being_declared_is_template_p (parser))
32052 /* Introduce a new template parameter list for implicit template
32053 parameters. */
32055 become_template = true;
32057 parser->implicit_template_scope
32058 = begin_scope (sk_template_parms, NULL);
32060 ++processing_template_decl;
32062 parser->fully_implicit_function_template_p = true;
32063 ++parser->num_template_parameter_lists;
32065 else
32067 /* Synthesize implicit template parameters at the end of the explicit
32068 template parameter list. */
32070 gcc_assert (current_template_parms);
32072 parser->implicit_template_scope = scope;
32074 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32075 parser->implicit_template_parms
32076 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32080 /* Synthesize a new template parameter and track the current template
32081 parameter chain with implicit_template_parms. */
32083 tree synth_id = make_generic_type_name ();
32084 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32085 synth_id);
32086 tree new_parm
32087 = process_template_parm (parser->implicit_template_parms,
32088 input_location,
32089 build_tree_list (NULL_TREE, synth_tmpl_parm),
32090 /*non_type=*/false,
32091 /*param_pack=*/false);
32094 if (parser->implicit_template_parms)
32095 parser->implicit_template_parms
32096 = TREE_CHAIN (parser->implicit_template_parms);
32097 else
32098 parser->implicit_template_parms = new_parm;
32100 tree new_type = TREE_TYPE (getdecls ());
32102 /* If creating a fully implicit function template, start the new implicit
32103 template parameter list with this synthesized type, otherwise grow the
32104 current template parameter list. */
32106 if (become_template)
32108 parent_scope->level_chain = current_binding_level;
32110 tree new_parms = make_tree_vec (1);
32111 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32112 current_template_parms = tree_cons (size_int (processing_template_decl),
32113 new_parms, current_template_parms);
32115 else
32117 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32118 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32119 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32120 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32123 current_binding_level = entry_scope;
32125 return new_type;
32128 /* Finish the declaration of a fully implicit function template. Such a
32129 template has no explicit template parameter list so has not been through the
32130 normal template head and tail processing. synthesize_implicit_template_parm
32131 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32132 provided if the declaration is a class member such that its template
32133 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32134 form is returned. Otherwise NULL_TREE is returned. */
32136 tree
32137 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32139 gcc_assert (parser->fully_implicit_function_template_p);
32141 if (member_decl_opt && member_decl_opt != error_mark_node
32142 && DECL_VIRTUAL_P (member_decl_opt))
32144 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32145 "implicit templates may not be %<virtual%>");
32146 DECL_VIRTUAL_P (member_decl_opt) = false;
32149 if (member_decl_opt)
32150 member_decl_opt = finish_member_template_decl (member_decl_opt);
32151 end_template_decl ();
32153 parser->fully_implicit_function_template_p = false;
32154 --parser->num_template_parameter_lists;
32156 return member_decl_opt;
32159 #include "gt-cp-parser.h"