Index declspecs.locations by ds_typedef rather than ds_thread.
[official-gcc.git] / gcc / cp / parser.c
blob7107134639e27796bd11304fd306afc798edb67d
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "timevar.h"
27 #include "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-family/c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic-core.h"
35 #include "target.h"
36 #include "cgraph.h"
37 #include "c-family/c-common.h"
38 #include "c-family/c-objc.h"
39 #include "plugin.h"
40 #include "tree-pretty-print.h"
41 #include "parser.h"
44 /* The lexer. */
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 static cp_token eof_token =
51 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
54 /* The various kinds of non integral constant we encounter. */
55 typedef enum non_integral_constant {
56 NIC_NONE,
57 /* floating-point literal */
58 NIC_FLOAT,
59 /* %<this%> */
60 NIC_THIS,
61 /* %<__FUNCTION__%> */
62 NIC_FUNC_NAME,
63 /* %<__PRETTY_FUNCTION__%> */
64 NIC_PRETTY_FUNC,
65 /* %<__func__%> */
66 NIC_C99_FUNC,
67 /* "%<va_arg%> */
68 NIC_VA_ARG,
69 /* a cast */
70 NIC_CAST,
71 /* %<typeid%> operator */
72 NIC_TYPEID,
73 /* non-constant compound literals */
74 NIC_NCC,
75 /* a function call */
76 NIC_FUNC_CALL,
77 /* an increment */
78 NIC_INC,
79 /* an decrement */
80 NIC_DEC,
81 /* an array reference */
82 NIC_ARRAY_REF,
83 /* %<->%> */
84 NIC_ARROW,
85 /* %<.%> */
86 NIC_POINT,
87 /* the address of a label */
88 NIC_ADDR_LABEL,
89 /* %<*%> */
90 NIC_STAR,
91 /* %<&%> */
92 NIC_ADDR,
93 /* %<++%> */
94 NIC_PREINCREMENT,
95 /* %<--%> */
96 NIC_PREDECREMENT,
97 /* %<new%> */
98 NIC_NEW,
99 /* %<delete%> */
100 NIC_DEL,
101 /* calls to overloaded operators */
102 NIC_OVERLOADED,
103 /* an assignment */
104 NIC_ASSIGNMENT,
105 /* a comma operator */
106 NIC_COMMA,
107 /* a call to a constructor */
108 NIC_CONSTRUCTOR,
109 /* a transaction expression */
110 NIC_TRANSACTION
111 } non_integral_constant;
113 /* The various kinds of errors about name-lookup failing. */
114 typedef enum name_lookup_error {
115 /* NULL */
116 NLE_NULL,
117 /* is not a type */
118 NLE_TYPE,
119 /* is not a class or namespace */
120 NLE_CXX98,
121 /* is not a class, namespace, or enumeration */
122 NLE_NOT_CXX98
123 } name_lookup_error;
125 /* The various kinds of required token */
126 typedef enum required_token {
127 RT_NONE,
128 RT_SEMICOLON, /* ';' */
129 RT_OPEN_PAREN, /* '(' */
130 RT_CLOSE_BRACE, /* '}' */
131 RT_OPEN_BRACE, /* '{' */
132 RT_CLOSE_SQUARE, /* ']' */
133 RT_OPEN_SQUARE, /* '[' */
134 RT_COMMA, /* ',' */
135 RT_SCOPE, /* '::' */
136 RT_LESS, /* '<' */
137 RT_GREATER, /* '>' */
138 RT_EQ, /* '=' */
139 RT_ELLIPSIS, /* '...' */
140 RT_MULT, /* '*' */
141 RT_COMPL, /* '~' */
142 RT_COLON, /* ':' */
143 RT_COLON_SCOPE, /* ':' or '::' */
144 RT_CLOSE_PAREN, /* ')' */
145 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
146 RT_PRAGMA_EOL, /* end of line */
147 RT_NAME, /* identifier */
149 /* The type is CPP_KEYWORD */
150 RT_NEW, /* new */
151 RT_DELETE, /* delete */
152 RT_RETURN, /* return */
153 RT_WHILE, /* while */
154 RT_EXTERN, /* extern */
155 RT_STATIC_ASSERT, /* static_assert */
156 RT_DECLTYPE, /* decltype */
157 RT_OPERATOR, /* operator */
158 RT_CLASS, /* class */
159 RT_TEMPLATE, /* template */
160 RT_NAMESPACE, /* namespace */
161 RT_USING, /* using */
162 RT_ASM, /* asm */
163 RT_TRY, /* try */
164 RT_CATCH, /* catch */
165 RT_THROW, /* throw */
166 RT_LABEL, /* __label__ */
167 RT_AT_TRY, /* @try */
168 RT_AT_SYNCHRONIZED, /* @synchronized */
169 RT_AT_THROW, /* @throw */
171 RT_SELECT, /* selection-statement */
172 RT_INTERATION, /* iteration-statement */
173 RT_JUMP, /* jump-statement */
174 RT_CLASS_KEY, /* class-key */
175 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
176 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
177 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
178 RT_TRANSACTION_CANCEL /* __transaction_cancel */
179 } required_token;
181 /* Prototypes. */
183 static cp_lexer *cp_lexer_new_main
184 (void);
185 static cp_lexer *cp_lexer_new_from_tokens
186 (cp_token_cache *tokens);
187 static void cp_lexer_destroy
188 (cp_lexer *);
189 static int cp_lexer_saving_tokens
190 (const cp_lexer *);
191 static cp_token *cp_lexer_token_at
192 (cp_lexer *, cp_token_position);
193 static void cp_lexer_get_preprocessor_token
194 (cp_lexer *, cp_token *);
195 static inline cp_token *cp_lexer_peek_token
196 (cp_lexer *);
197 static cp_token *cp_lexer_peek_nth_token
198 (cp_lexer *, size_t);
199 static inline bool cp_lexer_next_token_is
200 (cp_lexer *, enum cpp_ttype);
201 static bool cp_lexer_next_token_is_not
202 (cp_lexer *, enum cpp_ttype);
203 static bool cp_lexer_next_token_is_keyword
204 (cp_lexer *, enum rid);
205 static cp_token *cp_lexer_consume_token
206 (cp_lexer *);
207 static void cp_lexer_purge_token
208 (cp_lexer *);
209 static void cp_lexer_purge_tokens_after
210 (cp_lexer *, cp_token_position);
211 static void cp_lexer_save_tokens
212 (cp_lexer *);
213 static void cp_lexer_commit_tokens
214 (cp_lexer *);
215 static void cp_lexer_rollback_tokens
216 (cp_lexer *);
217 static void cp_lexer_print_token
218 (FILE *, cp_token *);
219 static inline bool cp_lexer_debugging_p
220 (cp_lexer *);
221 static void cp_lexer_start_debugging
222 (cp_lexer *) ATTRIBUTE_UNUSED;
223 static void cp_lexer_stop_debugging
224 (cp_lexer *) ATTRIBUTE_UNUSED;
226 static cp_token_cache *cp_token_cache_new
227 (cp_token *, cp_token *);
229 static void cp_parser_initial_pragma
230 (cp_token *);
232 static tree cp_literal_operator_id
233 (const char *);
235 /* Manifest constants. */
236 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
237 #define CP_SAVED_TOKEN_STACK 5
239 /* Variables. */
241 /* The stream to which debugging output should be written. */
242 static FILE *cp_lexer_debug_stream;
244 /* Nonzero if we are parsing an unevaluated operand: an operand to
245 sizeof, typeof, or alignof. */
246 int cp_unevaluated_operand;
248 /* Dump up to NUM tokens in BUFFER to FILE starting with token
249 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
250 first token in BUFFER. If NUM is 0, dump all the tokens. If
251 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
252 highlighted by surrounding it in [[ ]]. */
254 static void
255 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer,
256 cp_token *start_token, unsigned num,
257 cp_token *curr_token)
259 unsigned i, nprinted;
260 cp_token *token;
261 bool do_print;
263 fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
265 if (buffer == NULL)
266 return;
268 if (num == 0)
269 num = VEC_length (cp_token, buffer);
271 if (start_token == NULL)
272 start_token = VEC_address (cp_token, buffer);
274 if (start_token > VEC_address (cp_token, buffer))
276 cp_lexer_print_token (file, &VEC_index (cp_token, buffer, 0));
277 fprintf (file, " ... ");
280 do_print = false;
281 nprinted = 0;
282 for (i = 0; VEC_iterate (cp_token, buffer, i, token) && nprinted < num; i++)
284 if (token == start_token)
285 do_print = true;
287 if (!do_print)
288 continue;
290 nprinted++;
291 if (token == curr_token)
292 fprintf (file, "[[");
294 cp_lexer_print_token (file, token);
296 if (token == curr_token)
297 fprintf (file, "]]");
299 switch (token->type)
301 case CPP_SEMICOLON:
302 case CPP_OPEN_BRACE:
303 case CPP_CLOSE_BRACE:
304 case CPP_EOF:
305 fputc ('\n', file);
306 break;
308 default:
309 fputc (' ', file);
313 if (i == num && i < VEC_length (cp_token, buffer))
315 fprintf (file, " ... ");
316 cp_lexer_print_token (file, &VEC_last (cp_token, buffer));
319 fprintf (file, "\n");
323 /* Dump all tokens in BUFFER to stderr. */
325 void
326 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
328 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
332 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
333 description for T. */
335 static void
336 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
338 if (t)
340 fprintf (file, "%s: ", desc);
341 print_node_brief (file, "", t, 0);
346 /* Dump parser context C to FILE. */
348 static void
349 cp_debug_print_context (FILE *file, cp_parser_context *c)
351 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
352 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
353 print_node_brief (file, "", c->object_type, 0);
354 fprintf (file, "}\n");
358 /* Print the stack of parsing contexts to FILE starting with FIRST. */
360 static void
361 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
363 unsigned i;
364 cp_parser_context *c;
366 fprintf (file, "Parsing context stack:\n");
367 for (i = 0, c = first; c; c = c->next, i++)
369 fprintf (file, "\t#%u: ", i);
370 cp_debug_print_context (file, c);
375 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
377 static void
378 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
380 if (flag)
381 fprintf (file, "%s: true\n", desc);
385 /* Print an unparsed function entry UF to FILE. */
387 static void
388 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
390 unsigned i;
391 cp_default_arg_entry *default_arg_fn;
392 tree fn;
394 fprintf (file, "\tFunctions with default args:\n");
395 for (i = 0;
396 VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i,
397 default_arg_fn);
398 i++)
400 fprintf (file, "\t\tClass type: ");
401 print_node_brief (file, "", default_arg_fn->class_type, 0);
402 fprintf (file, "\t\tDeclaration: ");
403 print_node_brief (file, "", default_arg_fn->decl, 0);
404 fprintf (file, "\n");
407 fprintf (file, "\n\tFunctions with definitions that require "
408 "post-processing\n\t\t");
409 for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++)
411 print_node_brief (file, "", fn, 0);
412 fprintf (file, " ");
414 fprintf (file, "\n");
416 fprintf (file, "\n\tNon-static data members with initializers that require "
417 "post-processing\n\t\t");
418 for (i = 0; VEC_iterate (tree, uf->nsdmis, i, fn); i++)
420 print_node_brief (file, "", fn, 0);
421 fprintf (file, " ");
423 fprintf (file, "\n");
427 /* Print the stack of unparsed member functions S to FILE. */
429 static void
430 cp_debug_print_unparsed_queues (FILE *file,
431 VEC(cp_unparsed_functions_entry, gc) *s)
433 unsigned i;
434 cp_unparsed_functions_entry *uf;
436 fprintf (file, "Unparsed functions\n");
437 for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++)
439 fprintf (file, "#%u:\n", i);
440 cp_debug_print_unparsed_function (file, uf);
445 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
446 the given PARSER. If FILE is NULL, the output is printed on stderr. */
448 static void
449 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
451 cp_token *next_token, *first_token, *start_token;
453 if (file == NULL)
454 file = stderr;
456 next_token = parser->lexer->next_token;
457 first_token = VEC_address (cp_token, parser->lexer->buffer);
458 start_token = (next_token > first_token + window_size / 2)
459 ? next_token - window_size / 2
460 : first_token;
461 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
462 next_token);
466 /* Dump debugging information for the given PARSER. If FILE is NULL,
467 the output is printed on stderr. */
469 void
470 cp_debug_parser (FILE *file, cp_parser *parser)
472 const size_t window_size = 20;
473 cp_token *token;
474 expanded_location eloc;
476 if (file == NULL)
477 file = stderr;
479 fprintf (file, "Parser state\n\n");
480 fprintf (file, "Number of tokens: %u\n",
481 VEC_length (cp_token, parser->lexer->buffer));
482 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
483 cp_debug_print_tree_if_set (file, "Object scope",
484 parser->object_scope);
485 cp_debug_print_tree_if_set (file, "Qualifying scope",
486 parser->qualifying_scope);
487 cp_debug_print_context_stack (file, parser->context);
488 cp_debug_print_flag (file, "Allow GNU extensions",
489 parser->allow_gnu_extensions_p);
490 cp_debug_print_flag (file, "'>' token is greater-than",
491 parser->greater_than_is_operator_p);
492 cp_debug_print_flag (file, "Default args allowed in current "
493 "parameter list", parser->default_arg_ok_p);
494 cp_debug_print_flag (file, "Parsing integral constant-expression",
495 parser->integral_constant_expression_p);
496 cp_debug_print_flag (file, "Allow non-constant expression in current "
497 "constant-expression",
498 parser->allow_non_integral_constant_expression_p);
499 cp_debug_print_flag (file, "Seen non-constant expression",
500 parser->non_integral_constant_expression_p);
501 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
502 "current context",
503 parser->local_variables_forbidden_p);
504 cp_debug_print_flag (file, "In unbraced linkage specification",
505 parser->in_unbraced_linkage_specification_p);
506 cp_debug_print_flag (file, "Parsing a declarator",
507 parser->in_declarator_p);
508 cp_debug_print_flag (file, "In template argument list",
509 parser->in_template_argument_list_p);
510 cp_debug_print_flag (file, "Parsing an iteration statement",
511 parser->in_statement & IN_ITERATION_STMT);
512 cp_debug_print_flag (file, "Parsing a switch statement",
513 parser->in_statement & IN_SWITCH_STMT);
514 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
515 parser->in_statement & IN_OMP_BLOCK);
516 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
517 parser->in_statement & IN_OMP_FOR);
518 cp_debug_print_flag (file, "Parsing an if statement",
519 parser->in_statement & IN_IF_STMT);
520 cp_debug_print_flag (file, "Parsing a type-id in an expression "
521 "context", parser->in_type_id_in_expr_p);
522 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
523 parser->implicit_extern_c);
524 cp_debug_print_flag (file, "String expressions should be translated "
525 "to execution character set",
526 parser->translate_strings_p);
527 cp_debug_print_flag (file, "Parsing function body outside of a "
528 "local class", parser->in_function_body);
529 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
530 parser->colon_corrects_to_scope_p);
531 if (parser->type_definition_forbidden_message)
532 fprintf (file, "Error message for forbidden type definitions: %s\n",
533 parser->type_definition_forbidden_message);
534 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
535 fprintf (file, "Number of class definitions in progress: %u\n",
536 parser->num_classes_being_defined);
537 fprintf (file, "Number of template parameter lists for the current "
538 "declaration: %u\n", parser->num_template_parameter_lists);
539 cp_debug_parser_tokens (file, parser, window_size);
540 token = parser->lexer->next_token;
541 fprintf (file, "Next token to parse:\n");
542 fprintf (file, "\tToken: ");
543 cp_lexer_print_token (file, token);
544 eloc = expand_location (token->location);
545 fprintf (file, "\n\tFile: %s\n", eloc.file);
546 fprintf (file, "\tLine: %d\n", eloc.line);
547 fprintf (file, "\tColumn: %d\n", eloc.column);
551 /* Allocate memory for a new lexer object and return it. */
553 static cp_lexer *
554 cp_lexer_alloc (void)
556 cp_lexer *lexer;
558 c_common_no_more_pch ();
560 /* Allocate the memory. */
561 lexer = ggc_alloc_cleared_cp_lexer ();
563 /* Initially we are not debugging. */
564 lexer->debugging_p = false;
566 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
567 CP_SAVED_TOKEN_STACK);
569 /* Create the buffer. */
570 lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
572 return lexer;
576 /* Create a new main C++ lexer, the lexer that gets tokens from the
577 preprocessor. */
579 static cp_lexer *
580 cp_lexer_new_main (void)
582 cp_lexer *lexer;
583 cp_token token;
585 /* It's possible that parsing the first pragma will load a PCH file,
586 which is a GC collection point. So we have to do that before
587 allocating any memory. */
588 cp_parser_initial_pragma (&token);
590 lexer = cp_lexer_alloc ();
592 /* Put the first token in the buffer. */
593 VEC_quick_push (cp_token, lexer->buffer, token);
595 /* Get the remaining tokens from the preprocessor. */
596 while (token.type != CPP_EOF)
598 cp_lexer_get_preprocessor_token (lexer, &token);
599 VEC_safe_push (cp_token, gc, lexer->buffer, token);
602 lexer->last_token = VEC_address (cp_token, lexer->buffer)
603 + VEC_length (cp_token, lexer->buffer)
604 - 1;
605 lexer->next_token = VEC_length (cp_token, lexer->buffer)
606 ? VEC_address (cp_token, lexer->buffer)
607 : &eof_token;
609 /* Subsequent preprocessor diagnostics should use compiler
610 diagnostic functions to get the compiler source location. */
611 done_lexing = true;
613 gcc_assert (!lexer->next_token->purged_p);
614 return lexer;
617 /* Create a new lexer whose token stream is primed with the tokens in
618 CACHE. When these tokens are exhausted, no new tokens will be read. */
620 static cp_lexer *
621 cp_lexer_new_from_tokens (cp_token_cache *cache)
623 cp_token *first = cache->first;
624 cp_token *last = cache->last;
625 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
627 /* We do not own the buffer. */
628 lexer->buffer = NULL;
629 lexer->next_token = first == last ? &eof_token : first;
630 lexer->last_token = last;
632 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
633 CP_SAVED_TOKEN_STACK);
635 /* Initially we are not debugging. */
636 lexer->debugging_p = false;
638 gcc_assert (!lexer->next_token->purged_p);
639 return lexer;
642 /* Frees all resources associated with LEXER. */
644 static void
645 cp_lexer_destroy (cp_lexer *lexer)
647 VEC_free (cp_token, gc, lexer->buffer);
648 VEC_free (cp_token_position, heap, lexer->saved_tokens);
649 ggc_free (lexer);
652 /* Returns nonzero if debugging information should be output. */
654 static inline bool
655 cp_lexer_debugging_p (cp_lexer *lexer)
657 return lexer->debugging_p;
661 static inline cp_token_position
662 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
664 gcc_assert (!previous_p || lexer->next_token != &eof_token);
666 return lexer->next_token - previous_p;
669 static inline cp_token *
670 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
672 return pos;
675 static inline void
676 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
678 lexer->next_token = cp_lexer_token_at (lexer, pos);
681 static inline cp_token_position
682 cp_lexer_previous_token_position (cp_lexer *lexer)
684 if (lexer->next_token == &eof_token)
685 return lexer->last_token - 1;
686 else
687 return cp_lexer_token_position (lexer, true);
690 static inline cp_token *
691 cp_lexer_previous_token (cp_lexer *lexer)
693 cp_token_position tp = cp_lexer_previous_token_position (lexer);
695 return cp_lexer_token_at (lexer, tp);
698 /* nonzero if we are presently saving tokens. */
700 static inline int
701 cp_lexer_saving_tokens (const cp_lexer* lexer)
703 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
706 /* Store the next token from the preprocessor in *TOKEN. Return true
707 if we reach EOF. If LEXER is NULL, assume we are handling an
708 initial #pragma pch_preprocess, and thus want the lexer to return
709 processed strings. */
711 static void
712 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
714 static int is_extern_c = 0;
716 /* Get a new token from the preprocessor. */
717 token->type
718 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
719 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
720 token->keyword = RID_MAX;
721 token->pragma_kind = PRAGMA_NONE;
722 token->purged_p = false;
724 /* On some systems, some header files are surrounded by an
725 implicit extern "C" block. Set a flag in the token if it
726 comes from such a header. */
727 is_extern_c += pending_lang_change;
728 pending_lang_change = 0;
729 token->implicit_extern_c = is_extern_c > 0;
731 /* Check to see if this token is a keyword. */
732 if (token->type == CPP_NAME)
734 if (C_IS_RESERVED_WORD (token->u.value))
736 /* Mark this token as a keyword. */
737 token->type = CPP_KEYWORD;
738 /* Record which keyword. */
739 token->keyword = C_RID_CODE (token->u.value);
741 else
743 if (warn_cxx0x_compat
744 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
745 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
747 /* Warn about the C++0x keyword (but still treat it as
748 an identifier). */
749 warning (OPT_Wc__0x_compat,
750 "identifier %qE is a keyword in C++11",
751 token->u.value);
753 /* Clear out the C_RID_CODE so we don't warn about this
754 particular identifier-turned-keyword again. */
755 C_SET_RID_CODE (token->u.value, RID_MAX);
758 token->ambiguous_p = false;
759 token->keyword = RID_MAX;
762 else if (token->type == CPP_AT_NAME)
764 /* This only happens in Objective-C++; it must be a keyword. */
765 token->type = CPP_KEYWORD;
766 switch (C_RID_CODE (token->u.value))
768 /* Replace 'class' with '@class', 'private' with '@private',
769 etc. This prevents confusion with the C++ keyword
770 'class', and makes the tokens consistent with other
771 Objective-C 'AT' keywords. For example '@class' is
772 reported as RID_AT_CLASS which is consistent with
773 '@synchronized', which is reported as
774 RID_AT_SYNCHRONIZED.
776 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
777 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
778 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
779 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
780 case RID_THROW: token->keyword = RID_AT_THROW; break;
781 case RID_TRY: token->keyword = RID_AT_TRY; break;
782 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
783 default: token->keyword = C_RID_CODE (token->u.value);
786 else if (token->type == CPP_PRAGMA)
788 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
789 token->pragma_kind = ((enum pragma_kind)
790 TREE_INT_CST_LOW (token->u.value));
791 token->u.value = NULL_TREE;
795 /* Update the globals input_location and the input file stack from TOKEN. */
796 static inline void
797 cp_lexer_set_source_position_from_token (cp_token *token)
799 if (token->type != CPP_EOF)
801 input_location = token->location;
805 /* Return a pointer to the next token in the token stream, but do not
806 consume it. */
808 static inline cp_token *
809 cp_lexer_peek_token (cp_lexer *lexer)
811 if (cp_lexer_debugging_p (lexer))
813 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
814 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
815 putc ('\n', cp_lexer_debug_stream);
817 return lexer->next_token;
820 /* Return true if the next token has the indicated TYPE. */
822 static inline bool
823 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
825 return cp_lexer_peek_token (lexer)->type == type;
828 /* Return true if the next token does not have the indicated TYPE. */
830 static inline bool
831 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
833 return !cp_lexer_next_token_is (lexer, type);
836 /* Return true if the next token is the indicated KEYWORD. */
838 static inline bool
839 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
841 return cp_lexer_peek_token (lexer)->keyword == keyword;
844 /* Return true if the next token is not the indicated KEYWORD. */
846 static inline bool
847 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
849 return cp_lexer_peek_token (lexer)->keyword != keyword;
852 /* Return true if the next token is a keyword for a decl-specifier. */
854 static bool
855 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
857 cp_token *token;
859 token = cp_lexer_peek_token (lexer);
860 switch (token->keyword)
862 /* auto specifier: storage-class-specifier in C++,
863 simple-type-specifier in C++0x. */
864 case RID_AUTO:
865 /* Storage classes. */
866 case RID_REGISTER:
867 case RID_STATIC:
868 case RID_EXTERN:
869 case RID_MUTABLE:
870 case RID_THREAD:
871 /* Elaborated type specifiers. */
872 case RID_ENUM:
873 case RID_CLASS:
874 case RID_STRUCT:
875 case RID_UNION:
876 case RID_TYPENAME:
877 /* Simple type specifiers. */
878 case RID_CHAR:
879 case RID_CHAR16:
880 case RID_CHAR32:
881 case RID_WCHAR:
882 case RID_BOOL:
883 case RID_SHORT:
884 case RID_INT:
885 case RID_LONG:
886 case RID_INT128:
887 case RID_SIGNED:
888 case RID_UNSIGNED:
889 case RID_FLOAT:
890 case RID_DOUBLE:
891 case RID_VOID:
892 /* GNU extensions. */
893 case RID_ATTRIBUTE:
894 case RID_TYPEOF:
895 /* C++0x extensions. */
896 case RID_DECLTYPE:
897 case RID_UNDERLYING_TYPE:
898 return true;
900 default:
901 return false;
905 /* Returns TRUE iff the token T begins a decltype type. */
907 static bool
908 token_is_decltype (cp_token *t)
910 return (t->keyword == RID_DECLTYPE
911 || t->type == CPP_DECLTYPE);
914 /* Returns TRUE iff the next token begins a decltype type. */
916 static bool
917 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
919 cp_token *t = cp_lexer_peek_token (lexer);
920 return token_is_decltype (t);
923 /* Return a pointer to the Nth token in the token stream. If N is 1,
924 then this is precisely equivalent to cp_lexer_peek_token (except
925 that it is not inline). One would like to disallow that case, but
926 there is one case (cp_parser_nth_token_starts_template_id) where
927 the caller passes a variable for N and it might be 1. */
929 static cp_token *
930 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
932 cp_token *token;
934 /* N is 1-based, not zero-based. */
935 gcc_assert (n > 0);
937 if (cp_lexer_debugging_p (lexer))
938 fprintf (cp_lexer_debug_stream,
939 "cp_lexer: peeking ahead %ld at token: ", (long)n);
941 --n;
942 token = lexer->next_token;
943 gcc_assert (!n || token != &eof_token);
944 while (n != 0)
946 ++token;
947 if (token == lexer->last_token)
949 token = &eof_token;
950 break;
953 if (!token->purged_p)
954 --n;
957 if (cp_lexer_debugging_p (lexer))
959 cp_lexer_print_token (cp_lexer_debug_stream, token);
960 putc ('\n', cp_lexer_debug_stream);
963 return token;
966 /* Return the next token, and advance the lexer's next_token pointer
967 to point to the next non-purged token. */
969 static cp_token *
970 cp_lexer_consume_token (cp_lexer* lexer)
972 cp_token *token = lexer->next_token;
974 gcc_assert (token != &eof_token);
975 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
979 lexer->next_token++;
980 if (lexer->next_token == lexer->last_token)
982 lexer->next_token = &eof_token;
983 break;
987 while (lexer->next_token->purged_p);
989 cp_lexer_set_source_position_from_token (token);
991 /* Provide debugging output. */
992 if (cp_lexer_debugging_p (lexer))
994 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
995 cp_lexer_print_token (cp_lexer_debug_stream, token);
996 putc ('\n', cp_lexer_debug_stream);
999 return token;
1002 /* Permanently remove the next token from the token stream, and
1003 advance the next_token pointer to refer to the next non-purged
1004 token. */
1006 static void
1007 cp_lexer_purge_token (cp_lexer *lexer)
1009 cp_token *tok = lexer->next_token;
1011 gcc_assert (tok != &eof_token);
1012 tok->purged_p = true;
1013 tok->location = UNKNOWN_LOCATION;
1014 tok->u.value = NULL_TREE;
1015 tok->keyword = RID_MAX;
1019 tok++;
1020 if (tok == lexer->last_token)
1022 tok = &eof_token;
1023 break;
1026 while (tok->purged_p);
1027 lexer->next_token = tok;
1030 /* Permanently remove all tokens after TOK, up to, but not
1031 including, the token that will be returned next by
1032 cp_lexer_peek_token. */
1034 static void
1035 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1037 cp_token *peek = lexer->next_token;
1039 if (peek == &eof_token)
1040 peek = lexer->last_token;
1042 gcc_assert (tok < peek);
1044 for ( tok += 1; tok != peek; tok += 1)
1046 tok->purged_p = true;
1047 tok->location = UNKNOWN_LOCATION;
1048 tok->u.value = NULL_TREE;
1049 tok->keyword = RID_MAX;
1053 /* Begin saving tokens. All tokens consumed after this point will be
1054 preserved. */
1056 static void
1057 cp_lexer_save_tokens (cp_lexer* lexer)
1059 /* Provide debugging output. */
1060 if (cp_lexer_debugging_p (lexer))
1061 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1063 VEC_safe_push (cp_token_position, heap,
1064 lexer->saved_tokens, lexer->next_token);
1067 /* Commit to the portion of the token stream most recently saved. */
1069 static void
1070 cp_lexer_commit_tokens (cp_lexer* lexer)
1072 /* Provide debugging output. */
1073 if (cp_lexer_debugging_p (lexer))
1074 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1076 VEC_pop (cp_token_position, lexer->saved_tokens);
1079 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1080 to the token stream. Stop saving tokens. */
1082 static void
1083 cp_lexer_rollback_tokens (cp_lexer* lexer)
1085 /* Provide debugging output. */
1086 if (cp_lexer_debugging_p (lexer))
1087 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1089 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
1092 /* Print a representation of the TOKEN on the STREAM. */
1094 static void
1095 cp_lexer_print_token (FILE * stream, cp_token *token)
1097 /* We don't use cpp_type2name here because the parser defines
1098 a few tokens of its own. */
1099 static const char *const token_names[] = {
1100 /* cpplib-defined token types */
1101 #define OP(e, s) #e,
1102 #define TK(e, s) #e,
1103 TTYPE_TABLE
1104 #undef OP
1105 #undef TK
1106 /* C++ parser token types - see "Manifest constants", above. */
1107 "KEYWORD",
1108 "TEMPLATE_ID",
1109 "NESTED_NAME_SPECIFIER",
1112 /* For some tokens, print the associated data. */
1113 switch (token->type)
1115 case CPP_KEYWORD:
1116 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1117 For example, `struct' is mapped to an INTEGER_CST. */
1118 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1119 break;
1120 /* else fall through */
1121 case CPP_NAME:
1122 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1123 break;
1125 case CPP_STRING:
1126 case CPP_STRING16:
1127 case CPP_STRING32:
1128 case CPP_WSTRING:
1129 case CPP_UTF8STRING:
1130 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1131 break;
1133 case CPP_NUMBER:
1134 print_generic_expr (stream, token->u.value, 0);
1135 break;
1137 default:
1138 /* If we have a name for the token, print it out. Otherwise, we
1139 simply give the numeric code. */
1140 if (token->type < ARRAY_SIZE(token_names))
1141 fputs (token_names[token->type], stream);
1142 else
1143 fprintf (stream, "[%d]", token->type);
1144 break;
1148 /* Start emitting debugging information. */
1150 static void
1151 cp_lexer_start_debugging (cp_lexer* lexer)
1153 lexer->debugging_p = true;
1154 cp_lexer_debug_stream = stderr;
1157 /* Stop emitting debugging information. */
1159 static void
1160 cp_lexer_stop_debugging (cp_lexer* lexer)
1162 lexer->debugging_p = false;
1163 cp_lexer_debug_stream = NULL;
1166 /* Create a new cp_token_cache, representing a range of tokens. */
1168 static cp_token_cache *
1169 cp_token_cache_new (cp_token *first, cp_token *last)
1171 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1172 cache->first = first;
1173 cache->last = last;
1174 return cache;
1178 /* Decl-specifiers. */
1180 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1182 static void
1183 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1185 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1188 /* Declarators. */
1190 /* Nothing other than the parser should be creating declarators;
1191 declarators are a semi-syntactic representation of C++ entities.
1192 Other parts of the front end that need to create entities (like
1193 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1195 static cp_declarator *make_call_declarator
1196 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
1197 static cp_declarator *make_array_declarator
1198 (cp_declarator *, tree);
1199 static cp_declarator *make_pointer_declarator
1200 (cp_cv_quals, cp_declarator *, tree);
1201 static cp_declarator *make_reference_declarator
1202 (cp_cv_quals, cp_declarator *, bool, tree);
1203 static cp_parameter_declarator *make_parameter_declarator
1204 (cp_decl_specifier_seq *, cp_declarator *, tree);
1205 static cp_declarator *make_ptrmem_declarator
1206 (cp_cv_quals, tree, cp_declarator *, tree);
1208 /* An erroneous declarator. */
1209 static cp_declarator *cp_error_declarator;
1211 /* The obstack on which declarators and related data structures are
1212 allocated. */
1213 static struct obstack declarator_obstack;
1215 /* Alloc BYTES from the declarator memory pool. */
1217 static inline void *
1218 alloc_declarator (size_t bytes)
1220 return obstack_alloc (&declarator_obstack, bytes);
1223 /* Allocate a declarator of the indicated KIND. Clear fields that are
1224 common to all declarators. */
1226 static cp_declarator *
1227 make_declarator (cp_declarator_kind kind)
1229 cp_declarator *declarator;
1231 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1232 declarator->kind = kind;
1233 declarator->attributes = NULL_TREE;
1234 declarator->std_attributes = NULL_TREE;
1235 declarator->declarator = NULL;
1236 declarator->parameter_pack_p = false;
1237 declarator->id_loc = UNKNOWN_LOCATION;
1239 return declarator;
1242 /* Make a declarator for a generalized identifier. If
1243 QUALIFYING_SCOPE is non-NULL, the identifier is
1244 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1245 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1246 is, if any. */
1248 static cp_declarator *
1249 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1250 special_function_kind sfk)
1252 cp_declarator *declarator;
1254 /* It is valid to write:
1256 class C { void f(); };
1257 typedef C D;
1258 void D::f();
1260 The standard is not clear about whether `typedef const C D' is
1261 legal; as of 2002-09-15 the committee is considering that
1262 question. EDG 3.0 allows that syntax. Therefore, we do as
1263 well. */
1264 if (qualifying_scope && TYPE_P (qualifying_scope))
1265 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1267 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1268 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1269 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1271 declarator = make_declarator (cdk_id);
1272 declarator->u.id.qualifying_scope = qualifying_scope;
1273 declarator->u.id.unqualified_name = unqualified_name;
1274 declarator->u.id.sfk = sfk;
1276 return declarator;
1279 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1280 of modifiers such as const or volatile to apply to the pointer
1281 type, represented as identifiers. ATTRIBUTES represent the attributes that
1282 appertain to the pointer or reference. */
1284 cp_declarator *
1285 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1286 tree attributes)
1288 cp_declarator *declarator;
1290 declarator = make_declarator (cdk_pointer);
1291 declarator->declarator = target;
1292 declarator->u.pointer.qualifiers = cv_qualifiers;
1293 declarator->u.pointer.class_type = NULL_TREE;
1294 if (target)
1296 declarator->id_loc = target->id_loc;
1297 declarator->parameter_pack_p = target->parameter_pack_p;
1298 target->parameter_pack_p = false;
1300 else
1301 declarator->parameter_pack_p = false;
1303 declarator->std_attributes = attributes;
1305 return declarator;
1308 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1309 represent the attributes that appertain to the pointer or
1310 reference. */
1312 cp_declarator *
1313 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1314 bool rvalue_ref, tree attributes)
1316 cp_declarator *declarator;
1318 declarator = make_declarator (cdk_reference);
1319 declarator->declarator = target;
1320 declarator->u.reference.qualifiers = cv_qualifiers;
1321 declarator->u.reference.rvalue_ref = rvalue_ref;
1322 if (target)
1324 declarator->id_loc = target->id_loc;
1325 declarator->parameter_pack_p = target->parameter_pack_p;
1326 target->parameter_pack_p = false;
1328 else
1329 declarator->parameter_pack_p = false;
1331 declarator->std_attributes = attributes;
1333 return declarator;
1336 /* Like make_pointer_declarator -- but for a pointer to a non-static
1337 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1338 appertain to the pointer or reference. */
1340 cp_declarator *
1341 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1342 cp_declarator *pointee,
1343 tree attributes)
1345 cp_declarator *declarator;
1347 declarator = make_declarator (cdk_ptrmem);
1348 declarator->declarator = pointee;
1349 declarator->u.pointer.qualifiers = cv_qualifiers;
1350 declarator->u.pointer.class_type = class_type;
1352 if (pointee)
1354 declarator->parameter_pack_p = pointee->parameter_pack_p;
1355 pointee->parameter_pack_p = false;
1357 else
1358 declarator->parameter_pack_p = false;
1360 declarator->std_attributes = attributes;
1362 return declarator;
1365 /* Make a declarator for the function given by TARGET, with the
1366 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1367 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1368 indicates what exceptions can be thrown. */
1370 cp_declarator *
1371 make_call_declarator (cp_declarator *target,
1372 tree parms,
1373 cp_cv_quals cv_qualifiers,
1374 cp_virt_specifiers virt_specifiers,
1375 tree exception_specification,
1376 tree late_return_type)
1378 cp_declarator *declarator;
1380 declarator = make_declarator (cdk_function);
1381 declarator->declarator = target;
1382 declarator->u.function.parameters = parms;
1383 declarator->u.function.qualifiers = cv_qualifiers;
1384 declarator->u.function.virt_specifiers = virt_specifiers;
1385 declarator->u.function.exception_specification = exception_specification;
1386 declarator->u.function.late_return_type = late_return_type;
1387 if (target)
1389 declarator->id_loc = target->id_loc;
1390 declarator->parameter_pack_p = target->parameter_pack_p;
1391 target->parameter_pack_p = false;
1393 else
1394 declarator->parameter_pack_p = false;
1396 return declarator;
1399 /* Make a declarator for an array of BOUNDS elements, each of which is
1400 defined by ELEMENT. */
1402 cp_declarator *
1403 make_array_declarator (cp_declarator *element, tree bounds)
1405 cp_declarator *declarator;
1407 declarator = make_declarator (cdk_array);
1408 declarator->declarator = element;
1409 declarator->u.array.bounds = bounds;
1410 if (element)
1412 declarator->id_loc = element->id_loc;
1413 declarator->parameter_pack_p = element->parameter_pack_p;
1414 element->parameter_pack_p = false;
1416 else
1417 declarator->parameter_pack_p = false;
1419 return declarator;
1422 /* Determine whether the declarator we've seen so far can be a
1423 parameter pack, when followed by an ellipsis. */
1424 static bool
1425 declarator_can_be_parameter_pack (cp_declarator *declarator)
1427 /* Search for a declarator name, or any other declarator that goes
1428 after the point where the ellipsis could appear in a parameter
1429 pack. If we find any of these, then this declarator can not be
1430 made into a parameter pack. */
1431 bool found = false;
1432 while (declarator && !found)
1434 switch ((int)declarator->kind)
1436 case cdk_id:
1437 case cdk_array:
1438 found = true;
1439 break;
1441 case cdk_error:
1442 return true;
1444 default:
1445 declarator = declarator->declarator;
1446 break;
1450 return !found;
1453 cp_parameter_declarator *no_parameters;
1455 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1456 DECLARATOR and DEFAULT_ARGUMENT. */
1458 cp_parameter_declarator *
1459 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1460 cp_declarator *declarator,
1461 tree default_argument)
1463 cp_parameter_declarator *parameter;
1465 parameter = ((cp_parameter_declarator *)
1466 alloc_declarator (sizeof (cp_parameter_declarator)));
1467 parameter->next = NULL;
1468 if (decl_specifiers)
1469 parameter->decl_specifiers = *decl_specifiers;
1470 else
1471 clear_decl_specs (&parameter->decl_specifiers);
1472 parameter->declarator = declarator;
1473 parameter->default_argument = default_argument;
1474 parameter->ellipsis_p = false;
1476 return parameter;
1479 /* Returns true iff DECLARATOR is a declaration for a function. */
1481 static bool
1482 function_declarator_p (const cp_declarator *declarator)
1484 while (declarator)
1486 if (declarator->kind == cdk_function
1487 && declarator->declarator->kind == cdk_id)
1488 return true;
1489 if (declarator->kind == cdk_id
1490 || declarator->kind == cdk_error)
1491 return false;
1492 declarator = declarator->declarator;
1494 return false;
1497 /* The parser. */
1499 /* Overview
1500 --------
1502 A cp_parser parses the token stream as specified by the C++
1503 grammar. Its job is purely parsing, not semantic analysis. For
1504 example, the parser breaks the token stream into declarators,
1505 expressions, statements, and other similar syntactic constructs.
1506 It does not check that the types of the expressions on either side
1507 of an assignment-statement are compatible, or that a function is
1508 not declared with a parameter of type `void'.
1510 The parser invokes routines elsewhere in the compiler to perform
1511 semantic analysis and to build up the abstract syntax tree for the
1512 code processed.
1514 The parser (and the template instantiation code, which is, in a
1515 way, a close relative of parsing) are the only parts of the
1516 compiler that should be calling push_scope and pop_scope, or
1517 related functions. The parser (and template instantiation code)
1518 keeps track of what scope is presently active; everything else
1519 should simply honor that. (The code that generates static
1520 initializers may also need to set the scope, in order to check
1521 access control correctly when emitting the initializers.)
1523 Methodology
1524 -----------
1526 The parser is of the standard recursive-descent variety. Upcoming
1527 tokens in the token stream are examined in order to determine which
1528 production to use when parsing a non-terminal. Some C++ constructs
1529 require arbitrary look ahead to disambiguate. For example, it is
1530 impossible, in the general case, to tell whether a statement is an
1531 expression or declaration without scanning the entire statement.
1532 Therefore, the parser is capable of "parsing tentatively." When the
1533 parser is not sure what construct comes next, it enters this mode.
1534 Then, while we attempt to parse the construct, the parser queues up
1535 error messages, rather than issuing them immediately, and saves the
1536 tokens it consumes. If the construct is parsed successfully, the
1537 parser "commits", i.e., it issues any queued error messages and
1538 the tokens that were being preserved are permanently discarded.
1539 If, however, the construct is not parsed successfully, the parser
1540 rolls back its state completely so that it can resume parsing using
1541 a different alternative.
1543 Future Improvements
1544 -------------------
1546 The performance of the parser could probably be improved substantially.
1547 We could often eliminate the need to parse tentatively by looking ahead
1548 a little bit. In some places, this approach might not entirely eliminate
1549 the need to parse tentatively, but it might still speed up the average
1550 case. */
1552 /* Flags that are passed to some parsing functions. These values can
1553 be bitwise-ored together. */
1555 enum
1557 /* No flags. */
1558 CP_PARSER_FLAGS_NONE = 0x0,
1559 /* The construct is optional. If it is not present, then no error
1560 should be issued. */
1561 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1562 /* When parsing a type-specifier, treat user-defined type-names
1563 as non-type identifiers. */
1564 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1565 /* When parsing a type-specifier, do not try to parse a class-specifier
1566 or enum-specifier. */
1567 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1568 /* When parsing a decl-specifier-seq, only allow type-specifier or
1569 constexpr. */
1570 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1573 /* This type is used for parameters and variables which hold
1574 combinations of the above flags. */
1575 typedef int cp_parser_flags;
1577 /* The different kinds of declarators we want to parse. */
1579 typedef enum cp_parser_declarator_kind
1581 /* We want an abstract declarator. */
1582 CP_PARSER_DECLARATOR_ABSTRACT,
1583 /* We want a named declarator. */
1584 CP_PARSER_DECLARATOR_NAMED,
1585 /* We don't mind, but the name must be an unqualified-id. */
1586 CP_PARSER_DECLARATOR_EITHER
1587 } cp_parser_declarator_kind;
1589 /* The precedence values used to parse binary expressions. The minimum value
1590 of PREC must be 1, because zero is reserved to quickly discriminate
1591 binary operators from other tokens. */
1593 enum cp_parser_prec
1595 PREC_NOT_OPERATOR,
1596 PREC_LOGICAL_OR_EXPRESSION,
1597 PREC_LOGICAL_AND_EXPRESSION,
1598 PREC_INCLUSIVE_OR_EXPRESSION,
1599 PREC_EXCLUSIVE_OR_EXPRESSION,
1600 PREC_AND_EXPRESSION,
1601 PREC_EQUALITY_EXPRESSION,
1602 PREC_RELATIONAL_EXPRESSION,
1603 PREC_SHIFT_EXPRESSION,
1604 PREC_ADDITIVE_EXPRESSION,
1605 PREC_MULTIPLICATIVE_EXPRESSION,
1606 PREC_PM_EXPRESSION,
1607 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1610 /* A mapping from a token type to a corresponding tree node type, with a
1611 precedence value. */
1613 typedef struct cp_parser_binary_operations_map_node
1615 /* The token type. */
1616 enum cpp_ttype token_type;
1617 /* The corresponding tree code. */
1618 enum tree_code tree_type;
1619 /* The precedence of this operator. */
1620 enum cp_parser_prec prec;
1621 } cp_parser_binary_operations_map_node;
1623 typedef struct cp_parser_expression_stack_entry
1625 /* Left hand side of the binary operation we are currently
1626 parsing. */
1627 tree lhs;
1628 /* Original tree code for left hand side, if it was a binary
1629 expression itself (used for -Wparentheses). */
1630 enum tree_code lhs_type;
1631 /* Tree code for the binary operation we are parsing. */
1632 enum tree_code tree_type;
1633 /* Precedence of the binary operation we are parsing. */
1634 enum cp_parser_prec prec;
1635 /* Location of the binary operation we are parsing. */
1636 location_t loc;
1637 } cp_parser_expression_stack_entry;
1639 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1640 entries because precedence levels on the stack are monotonically
1641 increasing. */
1642 typedef struct cp_parser_expression_stack_entry
1643 cp_parser_expression_stack[NUM_PREC_VALUES];
1645 /* Prototypes. */
1647 /* Constructors and destructors. */
1649 static cp_parser_context *cp_parser_context_new
1650 (cp_parser_context *);
1652 /* Class variables. */
1654 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1656 /* The operator-precedence table used by cp_parser_binary_expression.
1657 Transformed into an associative array (binops_by_token) by
1658 cp_parser_new. */
1660 static const cp_parser_binary_operations_map_node binops[] = {
1661 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1662 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1664 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1665 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1666 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1668 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1669 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1671 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1672 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1674 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1675 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1676 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1677 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1679 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1680 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1682 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1684 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1686 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1688 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1690 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1693 /* The same as binops, but initialized by cp_parser_new so that
1694 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1695 for speed. */
1696 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1698 /* Constructors and destructors. */
1700 /* Construct a new context. The context below this one on the stack
1701 is given by NEXT. */
1703 static cp_parser_context *
1704 cp_parser_context_new (cp_parser_context* next)
1706 cp_parser_context *context;
1708 /* Allocate the storage. */
1709 if (cp_parser_context_free_list != NULL)
1711 /* Pull the first entry from the free list. */
1712 context = cp_parser_context_free_list;
1713 cp_parser_context_free_list = context->next;
1714 memset (context, 0, sizeof (*context));
1716 else
1717 context = ggc_alloc_cleared_cp_parser_context ();
1719 /* No errors have occurred yet in this context. */
1720 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1721 /* If this is not the bottommost context, copy information that we
1722 need from the previous context. */
1723 if (next)
1725 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1726 expression, then we are parsing one in this context, too. */
1727 context->object_type = next->object_type;
1728 /* Thread the stack. */
1729 context->next = next;
1732 return context;
1735 /* Managing the unparsed function queues. */
1737 #define unparsed_funs_with_default_args \
1738 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).funs_with_default_args
1739 #define unparsed_funs_with_definitions \
1740 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).funs_with_definitions
1741 #define unparsed_nsdmis \
1742 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).nsdmis
1744 static void
1745 push_unparsed_function_queues (cp_parser *parser)
1747 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1748 VEC_safe_push (cp_unparsed_functions_entry, gc, parser->unparsed_queues, e);
1751 static void
1752 pop_unparsed_function_queues (cp_parser *parser)
1754 release_tree_vector (unparsed_funs_with_definitions);
1755 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1758 /* Prototypes. */
1760 /* Constructors and destructors. */
1762 static cp_parser *cp_parser_new
1763 (void);
1765 /* Routines to parse various constructs.
1767 Those that return `tree' will return the error_mark_node (rather
1768 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1769 Sometimes, they will return an ordinary node if error-recovery was
1770 attempted, even though a parse error occurred. So, to check
1771 whether or not a parse error occurred, you should always use
1772 cp_parser_error_occurred. If the construct is optional (indicated
1773 either by an `_opt' in the name of the function that does the
1774 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1775 the construct is not present. */
1777 /* Lexical conventions [gram.lex] */
1779 static tree cp_parser_identifier
1780 (cp_parser *);
1781 static tree cp_parser_string_literal
1782 (cp_parser *, bool, bool);
1783 static tree cp_parser_userdef_char_literal
1784 (cp_parser *);
1785 static tree cp_parser_userdef_string_literal
1786 (cp_token *);
1787 static tree cp_parser_userdef_numeric_literal
1788 (cp_parser *);
1790 /* Basic concepts [gram.basic] */
1792 static bool cp_parser_translation_unit
1793 (cp_parser *);
1795 /* Expressions [gram.expr] */
1797 static tree cp_parser_primary_expression
1798 (cp_parser *, bool, bool, bool, cp_id_kind *);
1799 static tree cp_parser_id_expression
1800 (cp_parser *, bool, bool, bool *, bool, bool);
1801 static tree cp_parser_unqualified_id
1802 (cp_parser *, bool, bool, bool, bool);
1803 static tree cp_parser_nested_name_specifier_opt
1804 (cp_parser *, bool, bool, bool, bool);
1805 static tree cp_parser_nested_name_specifier
1806 (cp_parser *, bool, bool, bool, bool);
1807 static tree cp_parser_qualifying_entity
1808 (cp_parser *, bool, bool, bool, bool, bool);
1809 static tree cp_parser_postfix_expression
1810 (cp_parser *, bool, bool, bool, cp_id_kind *);
1811 static tree cp_parser_postfix_open_square_expression
1812 (cp_parser *, tree, bool);
1813 static tree cp_parser_postfix_dot_deref_expression
1814 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1815 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1816 (cp_parser *, int, bool, bool, bool *);
1817 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1818 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1819 static void cp_parser_pseudo_destructor_name
1820 (cp_parser *, tree *, tree *);
1821 static tree cp_parser_unary_expression
1822 (cp_parser *, bool, bool, cp_id_kind *);
1823 static enum tree_code cp_parser_unary_operator
1824 (cp_token *);
1825 static tree cp_parser_new_expression
1826 (cp_parser *);
1827 static VEC(tree,gc) *cp_parser_new_placement
1828 (cp_parser *);
1829 static tree cp_parser_new_type_id
1830 (cp_parser *, tree *);
1831 static cp_declarator *cp_parser_new_declarator_opt
1832 (cp_parser *);
1833 static cp_declarator *cp_parser_direct_new_declarator
1834 (cp_parser *);
1835 static VEC(tree,gc) *cp_parser_new_initializer
1836 (cp_parser *);
1837 static tree cp_parser_delete_expression
1838 (cp_parser *);
1839 static tree cp_parser_cast_expression
1840 (cp_parser *, bool, bool, cp_id_kind *);
1841 static tree cp_parser_binary_expression
1842 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1843 static tree cp_parser_question_colon_clause
1844 (cp_parser *, tree);
1845 static tree cp_parser_assignment_expression
1846 (cp_parser *, bool, cp_id_kind *);
1847 static enum tree_code cp_parser_assignment_operator_opt
1848 (cp_parser *);
1849 static tree cp_parser_expression
1850 (cp_parser *, bool, cp_id_kind *);
1851 static tree cp_parser_constant_expression
1852 (cp_parser *, bool, bool *);
1853 static tree cp_parser_builtin_offsetof
1854 (cp_parser *);
1855 static tree cp_parser_lambda_expression
1856 (cp_parser *);
1857 static void cp_parser_lambda_introducer
1858 (cp_parser *, tree);
1859 static bool cp_parser_lambda_declarator_opt
1860 (cp_parser *, tree);
1861 static void cp_parser_lambda_body
1862 (cp_parser *, tree);
1864 /* Statements [gram.stmt.stmt] */
1866 static void cp_parser_statement
1867 (cp_parser *, tree, bool, bool *);
1868 static void cp_parser_label_for_labeled_statement
1869 (cp_parser *, tree);
1870 static tree cp_parser_expression_statement
1871 (cp_parser *, tree);
1872 static tree cp_parser_compound_statement
1873 (cp_parser *, tree, bool, bool);
1874 static void cp_parser_statement_seq_opt
1875 (cp_parser *, tree);
1876 static tree cp_parser_selection_statement
1877 (cp_parser *, bool *);
1878 static tree cp_parser_condition
1879 (cp_parser *);
1880 static tree cp_parser_iteration_statement
1881 (cp_parser *);
1882 static bool cp_parser_for_init_statement
1883 (cp_parser *, tree *decl);
1884 static tree cp_parser_for
1885 (cp_parser *);
1886 static tree cp_parser_c_for
1887 (cp_parser *, tree, tree);
1888 static tree cp_parser_range_for
1889 (cp_parser *, tree, tree, tree);
1890 static void do_range_for_auto_deduction
1891 (tree, tree);
1892 static tree cp_parser_perform_range_for_lookup
1893 (tree, tree *, tree *);
1894 static tree cp_parser_range_for_member_function
1895 (tree, tree);
1896 static tree cp_parser_jump_statement
1897 (cp_parser *);
1898 static void cp_parser_declaration_statement
1899 (cp_parser *);
1901 static tree cp_parser_implicitly_scoped_statement
1902 (cp_parser *, bool *);
1903 static void cp_parser_already_scoped_statement
1904 (cp_parser *);
1906 /* Declarations [gram.dcl.dcl] */
1908 static void cp_parser_declaration_seq_opt
1909 (cp_parser *);
1910 static void cp_parser_declaration
1911 (cp_parser *);
1912 static void cp_parser_block_declaration
1913 (cp_parser *, bool);
1914 static void cp_parser_simple_declaration
1915 (cp_parser *, bool, tree *);
1916 static void cp_parser_decl_specifier_seq
1917 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1918 static tree cp_parser_storage_class_specifier_opt
1919 (cp_parser *);
1920 static tree cp_parser_function_specifier_opt
1921 (cp_parser *, cp_decl_specifier_seq *);
1922 static tree cp_parser_type_specifier
1923 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1924 int *, bool *);
1925 static tree cp_parser_simple_type_specifier
1926 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1927 static tree cp_parser_type_name
1928 (cp_parser *);
1929 static tree cp_parser_nonclass_name
1930 (cp_parser* parser);
1931 static tree cp_parser_elaborated_type_specifier
1932 (cp_parser *, bool, bool);
1933 static tree cp_parser_enum_specifier
1934 (cp_parser *);
1935 static void cp_parser_enumerator_list
1936 (cp_parser *, tree);
1937 static void cp_parser_enumerator_definition
1938 (cp_parser *, tree);
1939 static tree cp_parser_namespace_name
1940 (cp_parser *);
1941 static void cp_parser_namespace_definition
1942 (cp_parser *);
1943 static void cp_parser_namespace_body
1944 (cp_parser *);
1945 static tree cp_parser_qualified_namespace_specifier
1946 (cp_parser *);
1947 static void cp_parser_namespace_alias_definition
1948 (cp_parser *);
1949 static bool cp_parser_using_declaration
1950 (cp_parser *, bool);
1951 static void cp_parser_using_directive
1952 (cp_parser *);
1953 static tree cp_parser_alias_declaration
1954 (cp_parser *);
1955 static void cp_parser_asm_definition
1956 (cp_parser *);
1957 static void cp_parser_linkage_specification
1958 (cp_parser *);
1959 static void cp_parser_static_assert
1960 (cp_parser *, bool);
1961 static tree cp_parser_decltype
1962 (cp_parser *);
1964 /* Declarators [gram.dcl.decl] */
1966 static tree cp_parser_init_declarator
1967 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1968 static cp_declarator *cp_parser_declarator
1969 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1970 static cp_declarator *cp_parser_direct_declarator
1971 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1972 static enum tree_code cp_parser_ptr_operator
1973 (cp_parser *, tree *, cp_cv_quals *, tree *);
1974 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1975 (cp_parser *);
1976 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1977 (cp_parser *);
1978 static tree cp_parser_late_return_type_opt
1979 (cp_parser *, cp_cv_quals);
1980 static tree cp_parser_declarator_id
1981 (cp_parser *, bool);
1982 static tree cp_parser_type_id
1983 (cp_parser *);
1984 static tree cp_parser_template_type_arg
1985 (cp_parser *);
1986 static tree cp_parser_trailing_type_id (cp_parser *);
1987 static tree cp_parser_type_id_1
1988 (cp_parser *, bool, bool);
1989 static void cp_parser_type_specifier_seq
1990 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1991 static tree cp_parser_parameter_declaration_clause
1992 (cp_parser *);
1993 static tree cp_parser_parameter_declaration_list
1994 (cp_parser *, bool *);
1995 static cp_parameter_declarator *cp_parser_parameter_declaration
1996 (cp_parser *, bool, bool *);
1997 static tree cp_parser_default_argument
1998 (cp_parser *, bool);
1999 static void cp_parser_function_body
2000 (cp_parser *, bool);
2001 static tree cp_parser_initializer
2002 (cp_parser *, bool *, bool *);
2003 static tree cp_parser_initializer_clause
2004 (cp_parser *, bool *);
2005 static tree cp_parser_braced_list
2006 (cp_parser*, bool*);
2007 static VEC(constructor_elt,gc) *cp_parser_initializer_list
2008 (cp_parser *, bool *);
2010 static bool cp_parser_ctor_initializer_opt_and_function_body
2011 (cp_parser *, bool);
2013 /* Classes [gram.class] */
2015 static tree cp_parser_class_name
2016 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2017 static tree cp_parser_class_specifier
2018 (cp_parser *);
2019 static tree cp_parser_class_head
2020 (cp_parser *, bool *);
2021 static enum tag_types cp_parser_class_key
2022 (cp_parser *);
2023 static void cp_parser_member_specification_opt
2024 (cp_parser *);
2025 static void cp_parser_member_declaration
2026 (cp_parser *);
2027 static tree cp_parser_pure_specifier
2028 (cp_parser *);
2029 static tree cp_parser_constant_initializer
2030 (cp_parser *);
2032 /* Derived classes [gram.class.derived] */
2034 static tree cp_parser_base_clause
2035 (cp_parser *);
2036 static tree cp_parser_base_specifier
2037 (cp_parser *);
2039 /* Special member functions [gram.special] */
2041 static tree cp_parser_conversion_function_id
2042 (cp_parser *);
2043 static tree cp_parser_conversion_type_id
2044 (cp_parser *);
2045 static cp_declarator *cp_parser_conversion_declarator_opt
2046 (cp_parser *);
2047 static bool cp_parser_ctor_initializer_opt
2048 (cp_parser *);
2049 static void cp_parser_mem_initializer_list
2050 (cp_parser *);
2051 static tree cp_parser_mem_initializer
2052 (cp_parser *);
2053 static tree cp_parser_mem_initializer_id
2054 (cp_parser *);
2056 /* Overloading [gram.over] */
2058 static tree cp_parser_operator_function_id
2059 (cp_parser *);
2060 static tree cp_parser_operator
2061 (cp_parser *);
2063 /* Templates [gram.temp] */
2065 static void cp_parser_template_declaration
2066 (cp_parser *, bool);
2067 static tree cp_parser_template_parameter_list
2068 (cp_parser *);
2069 static tree cp_parser_template_parameter
2070 (cp_parser *, bool *, bool *);
2071 static tree cp_parser_type_parameter
2072 (cp_parser *, bool *);
2073 static tree cp_parser_template_id
2074 (cp_parser *, bool, bool, enum tag_types, bool);
2075 static tree cp_parser_template_name
2076 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2077 static tree cp_parser_template_argument_list
2078 (cp_parser *);
2079 static tree cp_parser_template_argument
2080 (cp_parser *);
2081 static void cp_parser_explicit_instantiation
2082 (cp_parser *);
2083 static void cp_parser_explicit_specialization
2084 (cp_parser *);
2086 /* Exception handling [gram.exception] */
2088 static tree cp_parser_try_block
2089 (cp_parser *);
2090 static bool cp_parser_function_try_block
2091 (cp_parser *);
2092 static void cp_parser_handler_seq
2093 (cp_parser *);
2094 static void cp_parser_handler
2095 (cp_parser *);
2096 static tree cp_parser_exception_declaration
2097 (cp_parser *);
2098 static tree cp_parser_throw_expression
2099 (cp_parser *);
2100 static tree cp_parser_exception_specification_opt
2101 (cp_parser *);
2102 static tree cp_parser_type_id_list
2103 (cp_parser *);
2105 /* GNU Extensions */
2107 static tree cp_parser_asm_specification_opt
2108 (cp_parser *);
2109 static tree cp_parser_asm_operand_list
2110 (cp_parser *);
2111 static tree cp_parser_asm_clobber_list
2112 (cp_parser *);
2113 static tree cp_parser_asm_label_list
2114 (cp_parser *);
2115 static bool cp_next_tokens_can_be_attribute_p
2116 (cp_parser *);
2117 static bool cp_next_tokens_can_be_gnu_attribute_p
2118 (cp_parser *);
2119 static bool cp_next_tokens_can_be_std_attribute_p
2120 (cp_parser *);
2121 static bool cp_nth_tokens_can_be_std_attribute_p
2122 (cp_parser *, size_t);
2123 static bool cp_nth_tokens_can_be_gnu_attribute_p
2124 (cp_parser *, size_t);
2125 static bool cp_nth_tokens_can_be_attribute_p
2126 (cp_parser *, size_t);
2127 static tree cp_parser_attributes_opt
2128 (cp_parser *);
2129 static tree cp_parser_gnu_attributes_opt
2130 (cp_parser *);
2131 static tree cp_parser_gnu_attribute_list
2132 (cp_parser *);
2133 static tree cp_parser_std_attribute
2134 (cp_parser *);
2135 static tree cp_parser_std_attribute_spec
2136 (cp_parser *);
2137 static tree cp_parser_std_attribute_spec_seq
2138 (cp_parser *);
2139 static bool cp_parser_extension_opt
2140 (cp_parser *, int *);
2141 static void cp_parser_label_declaration
2142 (cp_parser *);
2144 /* Transactional Memory Extensions */
2146 static tree cp_parser_transaction
2147 (cp_parser *, enum rid);
2148 static tree cp_parser_transaction_expression
2149 (cp_parser *, enum rid);
2150 static bool cp_parser_function_transaction
2151 (cp_parser *, enum rid);
2152 static tree cp_parser_transaction_cancel
2153 (cp_parser *);
2155 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2156 static bool cp_parser_pragma
2157 (cp_parser *, enum pragma_context);
2159 /* Objective-C++ Productions */
2161 static tree cp_parser_objc_message_receiver
2162 (cp_parser *);
2163 static tree cp_parser_objc_message_args
2164 (cp_parser *);
2165 static tree cp_parser_objc_message_expression
2166 (cp_parser *);
2167 static tree cp_parser_objc_encode_expression
2168 (cp_parser *);
2169 static tree cp_parser_objc_defs_expression
2170 (cp_parser *);
2171 static tree cp_parser_objc_protocol_expression
2172 (cp_parser *);
2173 static tree cp_parser_objc_selector_expression
2174 (cp_parser *);
2175 static tree cp_parser_objc_expression
2176 (cp_parser *);
2177 static bool cp_parser_objc_selector_p
2178 (enum cpp_ttype);
2179 static tree cp_parser_objc_selector
2180 (cp_parser *);
2181 static tree cp_parser_objc_protocol_refs_opt
2182 (cp_parser *);
2183 static void cp_parser_objc_declaration
2184 (cp_parser *, tree);
2185 static tree cp_parser_objc_statement
2186 (cp_parser *);
2187 static bool cp_parser_objc_valid_prefix_attributes
2188 (cp_parser *, tree *);
2189 static void cp_parser_objc_at_property_declaration
2190 (cp_parser *) ;
2191 static void cp_parser_objc_at_synthesize_declaration
2192 (cp_parser *) ;
2193 static void cp_parser_objc_at_dynamic_declaration
2194 (cp_parser *) ;
2195 static tree cp_parser_objc_struct_declaration
2196 (cp_parser *) ;
2198 /* Utility Routines */
2200 static tree cp_parser_lookup_name
2201 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2202 static tree cp_parser_lookup_name_simple
2203 (cp_parser *, tree, location_t);
2204 static tree cp_parser_maybe_treat_template_as_class
2205 (tree, bool);
2206 static bool cp_parser_check_declarator_template_parameters
2207 (cp_parser *, cp_declarator *, location_t);
2208 static bool cp_parser_check_template_parameters
2209 (cp_parser *, unsigned, location_t, cp_declarator *);
2210 static tree cp_parser_simple_cast_expression
2211 (cp_parser *);
2212 static tree cp_parser_global_scope_opt
2213 (cp_parser *, bool);
2214 static bool cp_parser_constructor_declarator_p
2215 (cp_parser *, bool);
2216 static tree cp_parser_function_definition_from_specifiers_and_declarator
2217 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2218 static tree cp_parser_function_definition_after_declarator
2219 (cp_parser *, bool);
2220 static void cp_parser_template_declaration_after_export
2221 (cp_parser *, bool);
2222 static void cp_parser_perform_template_parameter_access_checks
2223 (VEC (deferred_access_check,gc)*);
2224 static tree cp_parser_single_declaration
2225 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2226 static tree cp_parser_functional_cast
2227 (cp_parser *, tree);
2228 static tree cp_parser_save_member_function_body
2229 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2230 static tree cp_parser_save_nsdmi
2231 (cp_parser *);
2232 static tree cp_parser_enclosed_template_argument_list
2233 (cp_parser *);
2234 static void cp_parser_save_default_args
2235 (cp_parser *, tree);
2236 static void cp_parser_late_parsing_for_member
2237 (cp_parser *, tree);
2238 static tree cp_parser_late_parse_one_default_arg
2239 (cp_parser *, tree, tree, tree);
2240 static void cp_parser_late_parsing_nsdmi
2241 (cp_parser *, tree);
2242 static void cp_parser_late_parsing_default_args
2243 (cp_parser *, tree);
2244 static tree cp_parser_sizeof_operand
2245 (cp_parser *, enum rid);
2246 static tree cp_parser_trait_expr
2247 (cp_parser *, enum rid);
2248 static bool cp_parser_declares_only_class_p
2249 (cp_parser *);
2250 static void cp_parser_set_storage_class
2251 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2252 static void cp_parser_set_decl_spec_type
2253 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2254 static void set_and_check_decl_spec_loc
2255 (cp_decl_specifier_seq *decl_specs,
2256 cp_decl_spec ds, cp_token *);
2257 static bool cp_parser_friend_p
2258 (const cp_decl_specifier_seq *);
2259 static void cp_parser_required_error
2260 (cp_parser *, required_token, bool);
2261 static cp_token *cp_parser_require
2262 (cp_parser *, enum cpp_ttype, required_token);
2263 static cp_token *cp_parser_require_keyword
2264 (cp_parser *, enum rid, required_token);
2265 static bool cp_parser_token_starts_function_definition_p
2266 (cp_token *);
2267 static bool cp_parser_next_token_starts_class_definition_p
2268 (cp_parser *);
2269 static bool cp_parser_next_token_ends_template_argument_p
2270 (cp_parser *);
2271 static bool cp_parser_nth_token_starts_template_argument_list_p
2272 (cp_parser *, size_t);
2273 static enum tag_types cp_parser_token_is_class_key
2274 (cp_token *);
2275 static void cp_parser_check_class_key
2276 (enum tag_types, tree type);
2277 static void cp_parser_check_access_in_redeclaration
2278 (tree type, location_t location);
2279 static bool cp_parser_optional_template_keyword
2280 (cp_parser *);
2281 static void cp_parser_pre_parsed_nested_name_specifier
2282 (cp_parser *);
2283 static bool cp_parser_cache_group
2284 (cp_parser *, enum cpp_ttype, unsigned);
2285 static tree cp_parser_cache_defarg
2286 (cp_parser *parser, bool nsdmi);
2287 static void cp_parser_parse_tentatively
2288 (cp_parser *);
2289 static void cp_parser_commit_to_tentative_parse
2290 (cp_parser *);
2291 static void cp_parser_abort_tentative_parse
2292 (cp_parser *);
2293 static bool cp_parser_parse_definitely
2294 (cp_parser *);
2295 static inline bool cp_parser_parsing_tentatively
2296 (cp_parser *);
2297 static bool cp_parser_uncommitted_to_tentative_parse_p
2298 (cp_parser *);
2299 static void cp_parser_error
2300 (cp_parser *, const char *);
2301 static void cp_parser_name_lookup_error
2302 (cp_parser *, tree, tree, name_lookup_error, location_t);
2303 static bool cp_parser_simulate_error
2304 (cp_parser *);
2305 static bool cp_parser_check_type_definition
2306 (cp_parser *);
2307 static void cp_parser_check_for_definition_in_return_type
2308 (cp_declarator *, tree, location_t type_location);
2309 static void cp_parser_check_for_invalid_template_id
2310 (cp_parser *, tree, enum tag_types, location_t location);
2311 static bool cp_parser_non_integral_constant_expression
2312 (cp_parser *, non_integral_constant);
2313 static void cp_parser_diagnose_invalid_type_name
2314 (cp_parser *, tree, tree, location_t);
2315 static bool cp_parser_parse_and_diagnose_invalid_type_name
2316 (cp_parser *);
2317 static int cp_parser_skip_to_closing_parenthesis
2318 (cp_parser *, bool, bool, bool);
2319 static void cp_parser_skip_to_end_of_statement
2320 (cp_parser *);
2321 static void cp_parser_consume_semicolon_at_end_of_statement
2322 (cp_parser *);
2323 static void cp_parser_skip_to_end_of_block_or_statement
2324 (cp_parser *);
2325 static bool cp_parser_skip_to_closing_brace
2326 (cp_parser *);
2327 static void cp_parser_skip_to_end_of_template_parameter_list
2328 (cp_parser *);
2329 static void cp_parser_skip_to_pragma_eol
2330 (cp_parser*, cp_token *);
2331 static bool cp_parser_error_occurred
2332 (cp_parser *);
2333 static bool cp_parser_allow_gnu_extensions_p
2334 (cp_parser *);
2335 static bool cp_parser_is_pure_string_literal
2336 (cp_token *);
2337 static bool cp_parser_is_string_literal
2338 (cp_token *);
2339 static bool cp_parser_is_keyword
2340 (cp_token *, enum rid);
2341 static tree cp_parser_make_typename_type
2342 (cp_parser *, tree, tree, location_t location);
2343 static cp_declarator * cp_parser_make_indirect_declarator
2344 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2346 /* Returns nonzero if we are parsing tentatively. */
2348 static inline bool
2349 cp_parser_parsing_tentatively (cp_parser* parser)
2351 return parser->context->next != NULL;
2354 /* Returns nonzero if TOKEN is a string literal. */
2356 static bool
2357 cp_parser_is_pure_string_literal (cp_token* token)
2359 return (token->type == CPP_STRING ||
2360 token->type == CPP_STRING16 ||
2361 token->type == CPP_STRING32 ||
2362 token->type == CPP_WSTRING ||
2363 token->type == CPP_UTF8STRING);
2366 /* Returns nonzero if TOKEN is a string literal
2367 of a user-defined string literal. */
2369 static bool
2370 cp_parser_is_string_literal (cp_token* token)
2372 return (cp_parser_is_pure_string_literal (token) ||
2373 token->type == CPP_STRING_USERDEF ||
2374 token->type == CPP_STRING16_USERDEF ||
2375 token->type == CPP_STRING32_USERDEF ||
2376 token->type == CPP_WSTRING_USERDEF ||
2377 token->type == CPP_UTF8STRING_USERDEF);
2380 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2382 static bool
2383 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2385 return token->keyword == keyword;
2388 /* If not parsing tentatively, issue a diagnostic of the form
2389 FILE:LINE: MESSAGE before TOKEN
2390 where TOKEN is the next token in the input stream. MESSAGE
2391 (specified by the caller) is usually of the form "expected
2392 OTHER-TOKEN". */
2394 static void
2395 cp_parser_error (cp_parser* parser, const char* gmsgid)
2397 if (!cp_parser_simulate_error (parser))
2399 cp_token *token = cp_lexer_peek_token (parser->lexer);
2400 /* This diagnostic makes more sense if it is tagged to the line
2401 of the token we just peeked at. */
2402 cp_lexer_set_source_position_from_token (token);
2404 if (token->type == CPP_PRAGMA)
2406 error_at (token->location,
2407 "%<#pragma%> is not allowed here");
2408 cp_parser_skip_to_pragma_eol (parser, token);
2409 return;
2412 c_parse_error (gmsgid,
2413 /* Because c_parser_error does not understand
2414 CPP_KEYWORD, keywords are treated like
2415 identifiers. */
2416 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2417 token->u.value, token->flags);
2421 /* Issue an error about name-lookup failing. NAME is the
2422 IDENTIFIER_NODE DECL is the result of
2423 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2424 the thing that we hoped to find. */
2426 static void
2427 cp_parser_name_lookup_error (cp_parser* parser,
2428 tree name,
2429 tree decl,
2430 name_lookup_error desired,
2431 location_t location)
2433 /* If name lookup completely failed, tell the user that NAME was not
2434 declared. */
2435 if (decl == error_mark_node)
2437 if (parser->scope && parser->scope != global_namespace)
2438 error_at (location, "%<%E::%E%> has not been declared",
2439 parser->scope, name);
2440 else if (parser->scope == global_namespace)
2441 error_at (location, "%<::%E%> has not been declared", name);
2442 else if (parser->object_scope
2443 && !CLASS_TYPE_P (parser->object_scope))
2444 error_at (location, "request for member %qE in non-class type %qT",
2445 name, parser->object_scope);
2446 else if (parser->object_scope)
2447 error_at (location, "%<%T::%E%> has not been declared",
2448 parser->object_scope, name);
2449 else
2450 error_at (location, "%qE has not been declared", name);
2452 else if (parser->scope && parser->scope != global_namespace)
2454 switch (desired)
2456 case NLE_TYPE:
2457 error_at (location, "%<%E::%E%> is not a type",
2458 parser->scope, name);
2459 break;
2460 case NLE_CXX98:
2461 error_at (location, "%<%E::%E%> is not a class or namespace",
2462 parser->scope, name);
2463 break;
2464 case NLE_NOT_CXX98:
2465 error_at (location,
2466 "%<%E::%E%> is not a class, namespace, or enumeration",
2467 parser->scope, name);
2468 break;
2469 default:
2470 gcc_unreachable ();
2474 else if (parser->scope == global_namespace)
2476 switch (desired)
2478 case NLE_TYPE:
2479 error_at (location, "%<::%E%> is not a type", name);
2480 break;
2481 case NLE_CXX98:
2482 error_at (location, "%<::%E%> is not a class or namespace", name);
2483 break;
2484 case NLE_NOT_CXX98:
2485 error_at (location,
2486 "%<::%E%> is not a class, namespace, or enumeration",
2487 name);
2488 break;
2489 default:
2490 gcc_unreachable ();
2493 else
2495 switch (desired)
2497 case NLE_TYPE:
2498 error_at (location, "%qE is not a type", name);
2499 break;
2500 case NLE_CXX98:
2501 error_at (location, "%qE is not a class or namespace", name);
2502 break;
2503 case NLE_NOT_CXX98:
2504 error_at (location,
2505 "%qE is not a class, namespace, or enumeration", name);
2506 break;
2507 default:
2508 gcc_unreachable ();
2513 /* If we are parsing tentatively, remember that an error has occurred
2514 during this tentative parse. Returns true if the error was
2515 simulated; false if a message should be issued by the caller. */
2517 static bool
2518 cp_parser_simulate_error (cp_parser* parser)
2520 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2522 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2523 return true;
2525 return false;
2528 /* This function is called when a type is defined. If type
2529 definitions are forbidden at this point, an error message is
2530 issued. */
2532 static bool
2533 cp_parser_check_type_definition (cp_parser* parser)
2535 /* If types are forbidden here, issue a message. */
2536 if (parser->type_definition_forbidden_message)
2538 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2539 in the message need to be interpreted. */
2540 error (parser->type_definition_forbidden_message);
2541 return false;
2543 return true;
2546 /* This function is called when the DECLARATOR is processed. The TYPE
2547 was a type defined in the decl-specifiers. If it is invalid to
2548 define a type in the decl-specifiers for DECLARATOR, an error is
2549 issued. TYPE_LOCATION is the location of TYPE and is used
2550 for error reporting. */
2552 static void
2553 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2554 tree type, location_t type_location)
2556 /* [dcl.fct] forbids type definitions in return types.
2557 Unfortunately, it's not easy to know whether or not we are
2558 processing a return type until after the fact. */
2559 while (declarator
2560 && (declarator->kind == cdk_pointer
2561 || declarator->kind == cdk_reference
2562 || declarator->kind == cdk_ptrmem))
2563 declarator = declarator->declarator;
2564 if (declarator
2565 && declarator->kind == cdk_function)
2567 error_at (type_location,
2568 "new types may not be defined in a return type");
2569 inform (type_location,
2570 "(perhaps a semicolon is missing after the definition of %qT)",
2571 type);
2575 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2576 "<" in any valid C++ program. If the next token is indeed "<",
2577 issue a message warning the user about what appears to be an
2578 invalid attempt to form a template-id. LOCATION is the location
2579 of the type-specifier (TYPE) */
2581 static void
2582 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2583 tree type,
2584 enum tag_types tag_type,
2585 location_t location)
2587 cp_token_position start = 0;
2589 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2591 if (TYPE_P (type))
2592 error_at (location, "%qT is not a template", type);
2593 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2595 if (tag_type != none_type)
2596 error_at (location, "%qE is not a class template", type);
2597 else
2598 error_at (location, "%qE is not a template", type);
2600 else
2601 error_at (location, "invalid template-id");
2602 /* Remember the location of the invalid "<". */
2603 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2604 start = cp_lexer_token_position (parser->lexer, true);
2605 /* Consume the "<". */
2606 cp_lexer_consume_token (parser->lexer);
2607 /* Parse the template arguments. */
2608 cp_parser_enclosed_template_argument_list (parser);
2609 /* Permanently remove the invalid template arguments so that
2610 this error message is not issued again. */
2611 if (start)
2612 cp_lexer_purge_tokens_after (parser->lexer, start);
2616 /* If parsing an integral constant-expression, issue an error message
2617 about the fact that THING appeared and return true. Otherwise,
2618 return false. In either case, set
2619 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2621 static bool
2622 cp_parser_non_integral_constant_expression (cp_parser *parser,
2623 non_integral_constant thing)
2625 parser->non_integral_constant_expression_p = true;
2626 if (parser->integral_constant_expression_p)
2628 if (!parser->allow_non_integral_constant_expression_p)
2630 const char *msg = NULL;
2631 switch (thing)
2633 case NIC_FLOAT:
2634 error ("floating-point literal "
2635 "cannot appear in a constant-expression");
2636 return true;
2637 case NIC_CAST:
2638 error ("a cast to a type other than an integral or "
2639 "enumeration type cannot appear in a "
2640 "constant-expression");
2641 return true;
2642 case NIC_TYPEID:
2643 error ("%<typeid%> operator "
2644 "cannot appear in a constant-expression");
2645 return true;
2646 case NIC_NCC:
2647 error ("non-constant compound literals "
2648 "cannot appear in a constant-expression");
2649 return true;
2650 case NIC_FUNC_CALL:
2651 error ("a function call "
2652 "cannot appear in a constant-expression");
2653 return true;
2654 case NIC_INC:
2655 error ("an increment "
2656 "cannot appear in a constant-expression");
2657 return true;
2658 case NIC_DEC:
2659 error ("an decrement "
2660 "cannot appear in a constant-expression");
2661 return true;
2662 case NIC_ARRAY_REF:
2663 error ("an array reference "
2664 "cannot appear in a constant-expression");
2665 return true;
2666 case NIC_ADDR_LABEL:
2667 error ("the address of a label "
2668 "cannot appear in a constant-expression");
2669 return true;
2670 case NIC_OVERLOADED:
2671 error ("calls to overloaded operators "
2672 "cannot appear in a constant-expression");
2673 return true;
2674 case NIC_ASSIGNMENT:
2675 error ("an assignment cannot appear in a constant-expression");
2676 return true;
2677 case NIC_COMMA:
2678 error ("a comma operator "
2679 "cannot appear in a constant-expression");
2680 return true;
2681 case NIC_CONSTRUCTOR:
2682 error ("a call to a constructor "
2683 "cannot appear in a constant-expression");
2684 return true;
2685 case NIC_TRANSACTION:
2686 error ("a transaction expression "
2687 "cannot appear in a constant-expression");
2688 return true;
2689 case NIC_THIS:
2690 msg = "this";
2691 break;
2692 case NIC_FUNC_NAME:
2693 msg = "__FUNCTION__";
2694 break;
2695 case NIC_PRETTY_FUNC:
2696 msg = "__PRETTY_FUNCTION__";
2697 break;
2698 case NIC_C99_FUNC:
2699 msg = "__func__";
2700 break;
2701 case NIC_VA_ARG:
2702 msg = "va_arg";
2703 break;
2704 case NIC_ARROW:
2705 msg = "->";
2706 break;
2707 case NIC_POINT:
2708 msg = ".";
2709 break;
2710 case NIC_STAR:
2711 msg = "*";
2712 break;
2713 case NIC_ADDR:
2714 msg = "&";
2715 break;
2716 case NIC_PREINCREMENT:
2717 msg = "++";
2718 break;
2719 case NIC_PREDECREMENT:
2720 msg = "--";
2721 break;
2722 case NIC_NEW:
2723 msg = "new";
2724 break;
2725 case NIC_DEL:
2726 msg = "delete";
2727 break;
2728 default:
2729 gcc_unreachable ();
2731 if (msg)
2732 error ("%qs cannot appear in a constant-expression", msg);
2733 return true;
2736 return false;
2739 /* Emit a diagnostic for an invalid type name. SCOPE is the
2740 qualifying scope (or NULL, if none) for ID. This function commits
2741 to the current active tentative parse, if any. (Otherwise, the
2742 problematic construct might be encountered again later, resulting
2743 in duplicate error messages.) LOCATION is the location of ID. */
2745 static void
2746 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2747 tree scope, tree id,
2748 location_t location)
2750 tree decl, old_scope;
2751 cp_parser_commit_to_tentative_parse (parser);
2752 /* Try to lookup the identifier. */
2753 old_scope = parser->scope;
2754 parser->scope = scope;
2755 decl = cp_parser_lookup_name_simple (parser, id, location);
2756 parser->scope = old_scope;
2757 /* If the lookup found a template-name, it means that the user forgot
2758 to specify an argument list. Emit a useful error message. */
2759 if (TREE_CODE (decl) == TEMPLATE_DECL)
2760 error_at (location,
2761 "invalid use of template-name %qE without an argument list",
2762 decl);
2763 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2764 error_at (location, "invalid use of destructor %qD as a type", id);
2765 else if (TREE_CODE (decl) == TYPE_DECL)
2766 /* Something like 'unsigned A a;' */
2767 error_at (location, "invalid combination of multiple type-specifiers");
2768 else if (!parser->scope)
2770 /* Issue an error message. */
2771 error_at (location, "%qE does not name a type", id);
2772 /* If we're in a template class, it's possible that the user was
2773 referring to a type from a base class. For example:
2775 template <typename T> struct A { typedef T X; };
2776 template <typename T> struct B : public A<T> { X x; };
2778 The user should have said "typename A<T>::X". */
2779 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2780 inform (location, "C++11 %<constexpr%> only available with "
2781 "-std=c++11 or -std=gnu++11");
2782 else if (processing_template_decl && current_class_type
2783 && TYPE_BINFO (current_class_type))
2785 tree b;
2787 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2789 b = TREE_CHAIN (b))
2791 tree base_type = BINFO_TYPE (b);
2792 if (CLASS_TYPE_P (base_type)
2793 && dependent_type_p (base_type))
2795 tree field;
2796 /* Go from a particular instantiation of the
2797 template (which will have an empty TYPE_FIELDs),
2798 to the main version. */
2799 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2800 for (field = TYPE_FIELDS (base_type);
2801 field;
2802 field = DECL_CHAIN (field))
2803 if (TREE_CODE (field) == TYPE_DECL
2804 && DECL_NAME (field) == id)
2806 inform (location,
2807 "(perhaps %<typename %T::%E%> was intended)",
2808 BINFO_TYPE (b), id);
2809 break;
2811 if (field)
2812 break;
2817 /* Here we diagnose qualified-ids where the scope is actually correct,
2818 but the identifier does not resolve to a valid type name. */
2819 else if (parser->scope != error_mark_node)
2821 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2822 error_at (location, "%qE in namespace %qE does not name a type",
2823 id, parser->scope);
2824 else if (CLASS_TYPE_P (parser->scope)
2825 && constructor_name_p (id, parser->scope))
2827 /* A<T>::A<T>() */
2828 error_at (location, "%<%T::%E%> names the constructor, not"
2829 " the type", parser->scope, id);
2830 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2831 error_at (location, "and %qT has no template constructors",
2832 parser->scope);
2834 else if (TYPE_P (parser->scope)
2835 && dependent_scope_p (parser->scope))
2836 error_at (location, "need %<typename%> before %<%T::%E%> because "
2837 "%qT is a dependent scope",
2838 parser->scope, id, parser->scope);
2839 else if (TYPE_P (parser->scope))
2840 error_at (location, "%qE in %q#T does not name a type",
2841 id, parser->scope);
2842 else
2843 gcc_unreachable ();
2847 /* Check for a common situation where a type-name should be present,
2848 but is not, and issue a sensible error message. Returns true if an
2849 invalid type-name was detected.
2851 The situation handled by this function are variable declarations of the
2852 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2853 Usually, `ID' should name a type, but if we got here it means that it
2854 does not. We try to emit the best possible error message depending on
2855 how exactly the id-expression looks like. */
2857 static bool
2858 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2860 tree id;
2861 cp_token *token = cp_lexer_peek_token (parser->lexer);
2863 /* Avoid duplicate error about ambiguous lookup. */
2864 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2866 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2867 if (next->type == CPP_NAME && next->ambiguous_p)
2868 goto out;
2871 cp_parser_parse_tentatively (parser);
2872 id = cp_parser_id_expression (parser,
2873 /*template_keyword_p=*/false,
2874 /*check_dependency_p=*/true,
2875 /*template_p=*/NULL,
2876 /*declarator_p=*/true,
2877 /*optional_p=*/false);
2878 /* If the next token is a (, this is a function with no explicit return
2879 type, i.e. constructor, destructor or conversion op. */
2880 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2881 || TREE_CODE (id) == TYPE_DECL)
2883 cp_parser_abort_tentative_parse (parser);
2884 return false;
2886 if (!cp_parser_parse_definitely (parser))
2887 return false;
2889 /* Emit a diagnostic for the invalid type. */
2890 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2891 id, token->location);
2892 out:
2893 /* If we aren't in the middle of a declarator (i.e. in a
2894 parameter-declaration-clause), skip to the end of the declaration;
2895 there's no point in trying to process it. */
2896 if (!parser->in_declarator_p)
2897 cp_parser_skip_to_end_of_block_or_statement (parser);
2898 return true;
2901 /* Consume tokens up to, and including, the next non-nested closing `)'.
2902 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2903 are doing error recovery. Returns -1 if OR_COMMA is true and we
2904 found an unnested comma. */
2906 static int
2907 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2908 bool recovering,
2909 bool or_comma,
2910 bool consume_paren)
2912 unsigned paren_depth = 0;
2913 unsigned brace_depth = 0;
2914 unsigned square_depth = 0;
2916 if (recovering && !or_comma
2917 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2918 return 0;
2920 while (true)
2922 cp_token * token = cp_lexer_peek_token (parser->lexer);
2924 switch (token->type)
2926 case CPP_EOF:
2927 case CPP_PRAGMA_EOL:
2928 /* If we've run out of tokens, then there is no closing `)'. */
2929 return 0;
2931 /* This is good for lambda expression capture-lists. */
2932 case CPP_OPEN_SQUARE:
2933 ++square_depth;
2934 break;
2935 case CPP_CLOSE_SQUARE:
2936 if (!square_depth--)
2937 return 0;
2938 break;
2940 case CPP_SEMICOLON:
2941 /* This matches the processing in skip_to_end_of_statement. */
2942 if (!brace_depth)
2943 return 0;
2944 break;
2946 case CPP_OPEN_BRACE:
2947 ++brace_depth;
2948 break;
2949 case CPP_CLOSE_BRACE:
2950 if (!brace_depth--)
2951 return 0;
2952 break;
2954 case CPP_COMMA:
2955 if (recovering && or_comma && !brace_depth && !paren_depth
2956 && !square_depth)
2957 return -1;
2958 break;
2960 case CPP_OPEN_PAREN:
2961 if (!brace_depth)
2962 ++paren_depth;
2963 break;
2965 case CPP_CLOSE_PAREN:
2966 if (!brace_depth && !paren_depth--)
2968 if (consume_paren)
2969 cp_lexer_consume_token (parser->lexer);
2970 return 1;
2972 break;
2974 default:
2975 break;
2978 /* Consume the token. */
2979 cp_lexer_consume_token (parser->lexer);
2983 /* Consume tokens until we reach the end of the current statement.
2984 Normally, that will be just before consuming a `;'. However, if a
2985 non-nested `}' comes first, then we stop before consuming that. */
2987 static void
2988 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2990 unsigned nesting_depth = 0;
2992 while (true)
2994 cp_token *token = cp_lexer_peek_token (parser->lexer);
2996 switch (token->type)
2998 case CPP_EOF:
2999 case CPP_PRAGMA_EOL:
3000 /* If we've run out of tokens, stop. */
3001 return;
3003 case CPP_SEMICOLON:
3004 /* If the next token is a `;', we have reached the end of the
3005 statement. */
3006 if (!nesting_depth)
3007 return;
3008 break;
3010 case CPP_CLOSE_BRACE:
3011 /* If this is a non-nested '}', stop before consuming it.
3012 That way, when confronted with something like:
3014 { 3 + }
3016 we stop before consuming the closing '}', even though we
3017 have not yet reached a `;'. */
3018 if (nesting_depth == 0)
3019 return;
3021 /* If it is the closing '}' for a block that we have
3022 scanned, stop -- but only after consuming the token.
3023 That way given:
3025 void f g () { ... }
3026 typedef int I;
3028 we will stop after the body of the erroneously declared
3029 function, but before consuming the following `typedef'
3030 declaration. */
3031 if (--nesting_depth == 0)
3033 cp_lexer_consume_token (parser->lexer);
3034 return;
3037 case CPP_OPEN_BRACE:
3038 ++nesting_depth;
3039 break;
3041 default:
3042 break;
3045 /* Consume the token. */
3046 cp_lexer_consume_token (parser->lexer);
3050 /* This function is called at the end of a statement or declaration.
3051 If the next token is a semicolon, it is consumed; otherwise, error
3052 recovery is attempted. */
3054 static void
3055 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3057 /* Look for the trailing `;'. */
3058 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3060 /* If there is additional (erroneous) input, skip to the end of
3061 the statement. */
3062 cp_parser_skip_to_end_of_statement (parser);
3063 /* If the next token is now a `;', consume it. */
3064 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3065 cp_lexer_consume_token (parser->lexer);
3069 /* Skip tokens until we have consumed an entire block, or until we
3070 have consumed a non-nested `;'. */
3072 static void
3073 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3075 int nesting_depth = 0;
3077 while (nesting_depth >= 0)
3079 cp_token *token = cp_lexer_peek_token (parser->lexer);
3081 switch (token->type)
3083 case CPP_EOF:
3084 case CPP_PRAGMA_EOL:
3085 /* If we've run out of tokens, stop. */
3086 return;
3088 case CPP_SEMICOLON:
3089 /* Stop if this is an unnested ';'. */
3090 if (!nesting_depth)
3091 nesting_depth = -1;
3092 break;
3094 case CPP_CLOSE_BRACE:
3095 /* Stop if this is an unnested '}', or closes the outermost
3096 nesting level. */
3097 nesting_depth--;
3098 if (nesting_depth < 0)
3099 return;
3100 if (!nesting_depth)
3101 nesting_depth = -1;
3102 break;
3104 case CPP_OPEN_BRACE:
3105 /* Nest. */
3106 nesting_depth++;
3107 break;
3109 default:
3110 break;
3113 /* Consume the token. */
3114 cp_lexer_consume_token (parser->lexer);
3118 /* Skip tokens until a non-nested closing curly brace is the next
3119 token, or there are no more tokens. Return true in the first case,
3120 false otherwise. */
3122 static bool
3123 cp_parser_skip_to_closing_brace (cp_parser *parser)
3125 unsigned nesting_depth = 0;
3127 while (true)
3129 cp_token *token = cp_lexer_peek_token (parser->lexer);
3131 switch (token->type)
3133 case CPP_EOF:
3134 case CPP_PRAGMA_EOL:
3135 /* If we've run out of tokens, stop. */
3136 return false;
3138 case CPP_CLOSE_BRACE:
3139 /* If the next token is a non-nested `}', then we have reached
3140 the end of the current block. */
3141 if (nesting_depth-- == 0)
3142 return true;
3143 break;
3145 case CPP_OPEN_BRACE:
3146 /* If it the next token is a `{', then we are entering a new
3147 block. Consume the entire block. */
3148 ++nesting_depth;
3149 break;
3151 default:
3152 break;
3155 /* Consume the token. */
3156 cp_lexer_consume_token (parser->lexer);
3160 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3161 parameter is the PRAGMA token, allowing us to purge the entire pragma
3162 sequence. */
3164 static void
3165 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3167 cp_token *token;
3169 parser->lexer->in_pragma = false;
3172 token = cp_lexer_consume_token (parser->lexer);
3173 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3175 /* Ensure that the pragma is not parsed again. */
3176 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3179 /* Require pragma end of line, resyncing with it as necessary. The
3180 arguments are as for cp_parser_skip_to_pragma_eol. */
3182 static void
3183 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3185 parser->lexer->in_pragma = false;
3186 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3187 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3190 /* This is a simple wrapper around make_typename_type. When the id is
3191 an unresolved identifier node, we can provide a superior diagnostic
3192 using cp_parser_diagnose_invalid_type_name. */
3194 static tree
3195 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3196 tree id, location_t id_location)
3198 tree result;
3199 if (TREE_CODE (id) == IDENTIFIER_NODE)
3201 result = make_typename_type (scope, id, typename_type,
3202 /*complain=*/tf_none);
3203 if (result == error_mark_node)
3204 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3205 return result;
3207 return make_typename_type (scope, id, typename_type, tf_error);
3210 /* This is a wrapper around the
3211 make_{pointer,ptrmem,reference}_declarator functions that decides
3212 which one to call based on the CODE and CLASS_TYPE arguments. The
3213 CODE argument should be one of the values returned by
3214 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3215 appertain to the pointer or reference. */
3217 static cp_declarator *
3218 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3219 cp_cv_quals cv_qualifiers,
3220 cp_declarator *target,
3221 tree attributes)
3223 if (code == ERROR_MARK)
3224 return cp_error_declarator;
3226 if (code == INDIRECT_REF)
3227 if (class_type == NULL_TREE)
3228 return make_pointer_declarator (cv_qualifiers, target, attributes);
3229 else
3230 return make_ptrmem_declarator (cv_qualifiers, class_type,
3231 target, attributes);
3232 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3233 return make_reference_declarator (cv_qualifiers, target,
3234 false, attributes);
3235 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3236 return make_reference_declarator (cv_qualifiers, target,
3237 true, attributes);
3238 gcc_unreachable ();
3241 /* Create a new C++ parser. */
3243 static cp_parser *
3244 cp_parser_new (void)
3246 cp_parser *parser;
3247 cp_lexer *lexer;
3248 unsigned i;
3250 /* cp_lexer_new_main is called before doing GC allocation because
3251 cp_lexer_new_main might load a PCH file. */
3252 lexer = cp_lexer_new_main ();
3254 /* Initialize the binops_by_token so that we can get the tree
3255 directly from the token. */
3256 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3257 binops_by_token[binops[i].token_type] = binops[i];
3259 parser = ggc_alloc_cleared_cp_parser ();
3260 parser->lexer = lexer;
3261 parser->context = cp_parser_context_new (NULL);
3263 /* For now, we always accept GNU extensions. */
3264 parser->allow_gnu_extensions_p = 1;
3266 /* The `>' token is a greater-than operator, not the end of a
3267 template-id. */
3268 parser->greater_than_is_operator_p = true;
3270 parser->default_arg_ok_p = true;
3272 /* We are not parsing a constant-expression. */
3273 parser->integral_constant_expression_p = false;
3274 parser->allow_non_integral_constant_expression_p = false;
3275 parser->non_integral_constant_expression_p = false;
3277 /* Local variable names are not forbidden. */
3278 parser->local_variables_forbidden_p = false;
3280 /* We are not processing an `extern "C"' declaration. */
3281 parser->in_unbraced_linkage_specification_p = false;
3283 /* We are not processing a declarator. */
3284 parser->in_declarator_p = false;
3286 /* We are not processing a template-argument-list. */
3287 parser->in_template_argument_list_p = false;
3289 /* We are not in an iteration statement. */
3290 parser->in_statement = 0;
3292 /* We are not in a switch statement. */
3293 parser->in_switch_statement_p = false;
3295 /* We are not parsing a type-id inside an expression. */
3296 parser->in_type_id_in_expr_p = false;
3298 /* Declarations aren't implicitly extern "C". */
3299 parser->implicit_extern_c = false;
3301 /* String literals should be translated to the execution character set. */
3302 parser->translate_strings_p = true;
3304 /* We are not parsing a function body. */
3305 parser->in_function_body = false;
3307 /* We can correct until told otherwise. */
3308 parser->colon_corrects_to_scope_p = true;
3310 /* The unparsed function queue is empty. */
3311 push_unparsed_function_queues (parser);
3313 /* There are no classes being defined. */
3314 parser->num_classes_being_defined = 0;
3316 /* No template parameters apply. */
3317 parser->num_template_parameter_lists = 0;
3319 return parser;
3322 /* Create a cp_lexer structure which will emit the tokens in CACHE
3323 and push it onto the parser's lexer stack. This is used for delayed
3324 parsing of in-class method bodies and default arguments, and should
3325 not be confused with tentative parsing. */
3326 static void
3327 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3329 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3330 lexer->next = parser->lexer;
3331 parser->lexer = lexer;
3333 /* Move the current source position to that of the first token in the
3334 new lexer. */
3335 cp_lexer_set_source_position_from_token (lexer->next_token);
3338 /* Pop the top lexer off the parser stack. This is never used for the
3339 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3340 static void
3341 cp_parser_pop_lexer (cp_parser *parser)
3343 cp_lexer *lexer = parser->lexer;
3344 parser->lexer = lexer->next;
3345 cp_lexer_destroy (lexer);
3347 /* Put the current source position back where it was before this
3348 lexer was pushed. */
3349 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3352 /* Lexical conventions [gram.lex] */
3354 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3355 identifier. */
3357 static tree
3358 cp_parser_identifier (cp_parser* parser)
3360 cp_token *token;
3362 /* Look for the identifier. */
3363 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3364 /* Return the value. */
3365 return token ? token->u.value : error_mark_node;
3368 /* Parse a sequence of adjacent string constants. Returns a
3369 TREE_STRING representing the combined, nul-terminated string
3370 constant. If TRANSLATE is true, translate the string to the
3371 execution character set. If WIDE_OK is true, a wide string is
3372 invalid here.
3374 C++98 [lex.string] says that if a narrow string literal token is
3375 adjacent to a wide string literal token, the behavior is undefined.
3376 However, C99 6.4.5p4 says that this results in a wide string literal.
3377 We follow C99 here, for consistency with the C front end.
3379 This code is largely lifted from lex_string() in c-lex.c.
3381 FUTURE: ObjC++ will need to handle @-strings here. */
3382 static tree
3383 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3385 tree value;
3386 size_t count;
3387 struct obstack str_ob;
3388 cpp_string str, istr, *strs;
3389 cp_token *tok;
3390 enum cpp_ttype type, curr_type;
3391 int have_suffix_p = 0;
3392 tree string_tree;
3393 tree suffix_id = NULL_TREE;
3394 bool curr_tok_is_userdef_p = false;
3396 tok = cp_lexer_peek_token (parser->lexer);
3397 if (!cp_parser_is_string_literal (tok))
3399 cp_parser_error (parser, "expected string-literal");
3400 return error_mark_node;
3403 if (cpp_userdef_string_p (tok->type))
3405 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3406 curr_type = cpp_userdef_string_remove_type (tok->type);
3407 curr_tok_is_userdef_p = true;
3409 else
3411 string_tree = tok->u.value;
3412 curr_type = tok->type;
3414 type = curr_type;
3416 /* Try to avoid the overhead of creating and destroying an obstack
3417 for the common case of just one string. */
3418 if (!cp_parser_is_string_literal
3419 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3421 cp_lexer_consume_token (parser->lexer);
3423 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3424 str.len = TREE_STRING_LENGTH (string_tree);
3425 count = 1;
3427 if (curr_tok_is_userdef_p)
3429 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3430 have_suffix_p = 1;
3431 curr_type = cpp_userdef_string_remove_type (tok->type);
3433 else
3434 curr_type = tok->type;
3436 strs = &str;
3438 else
3440 gcc_obstack_init (&str_ob);
3441 count = 0;
3445 cp_lexer_consume_token (parser->lexer);
3446 count++;
3447 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3448 str.len = TREE_STRING_LENGTH (string_tree);
3450 if (curr_tok_is_userdef_p)
3452 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3453 if (have_suffix_p == 0)
3455 suffix_id = curr_suffix_id;
3456 have_suffix_p = 1;
3458 else if (have_suffix_p == 1
3459 && curr_suffix_id != suffix_id)
3461 error ("inconsistent user-defined literal suffixes"
3462 " %qD and %qD in string literal",
3463 suffix_id, curr_suffix_id);
3464 have_suffix_p = -1;
3466 curr_type = cpp_userdef_string_remove_type (tok->type);
3468 else
3469 curr_type = tok->type;
3471 if (type != curr_type)
3473 if (type == CPP_STRING)
3474 type = curr_type;
3475 else if (curr_type != CPP_STRING)
3476 error_at (tok->location,
3477 "unsupported non-standard concatenation "
3478 "of string literals");
3481 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3483 tok = cp_lexer_peek_token (parser->lexer);
3484 if (cpp_userdef_string_p (tok->type))
3486 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3487 curr_type = cpp_userdef_string_remove_type (tok->type);
3488 curr_tok_is_userdef_p = true;
3490 else
3492 string_tree = tok->u.value;
3493 curr_type = tok->type;
3494 curr_tok_is_userdef_p = false;
3497 while (cp_parser_is_string_literal (tok));
3499 strs = (cpp_string *) obstack_finish (&str_ob);
3502 if (type != CPP_STRING && !wide_ok)
3504 cp_parser_error (parser, "a wide string is invalid in this context");
3505 type = CPP_STRING;
3508 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3509 (parse_in, strs, count, &istr, type))
3511 value = build_string (istr.len, (const char *)istr.text);
3512 free (CONST_CAST (unsigned char *, istr.text));
3514 switch (type)
3516 default:
3517 case CPP_STRING:
3518 case CPP_UTF8STRING:
3519 TREE_TYPE (value) = char_array_type_node;
3520 break;
3521 case CPP_STRING16:
3522 TREE_TYPE (value) = char16_array_type_node;
3523 break;
3524 case CPP_STRING32:
3525 TREE_TYPE (value) = char32_array_type_node;
3526 break;
3527 case CPP_WSTRING:
3528 TREE_TYPE (value) = wchar_array_type_node;
3529 break;
3532 value = fix_string_type (value);
3534 if (have_suffix_p)
3536 tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3537 tok->u.value = literal;
3538 return cp_parser_userdef_string_literal (tok);
3541 else
3542 /* cpp_interpret_string has issued an error. */
3543 value = error_mark_node;
3545 if (count > 1)
3546 obstack_free (&str_ob, 0);
3548 return value;
3551 /* Look up a literal operator with the name and the exact arguments. */
3553 static tree
3554 lookup_literal_operator (tree name, VEC(tree,gc) *args)
3556 tree decl, fns;
3557 decl = lookup_name (name);
3558 if (!decl || !is_overloaded_fn (decl))
3559 return error_mark_node;
3561 for (fns = decl; fns; fns = OVL_NEXT (fns))
3563 unsigned int ix;
3564 bool found = true;
3565 tree fn = OVL_CURRENT (fns);
3566 tree argtypes = NULL_TREE;
3567 argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3568 if (argtypes != NULL_TREE)
3570 for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE;
3571 ++ix, argtypes = TREE_CHAIN (argtypes))
3573 tree targ = TREE_VALUE (argtypes);
3574 tree tparm = TREE_TYPE (VEC_index (tree, args, ix));
3575 bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3576 bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3577 if ((ptr || arr || !same_type_p (targ, tparm))
3578 && (!ptr || !arr
3579 || !same_type_p (TREE_TYPE (targ),
3580 TREE_TYPE (tparm))))
3581 found = false;
3583 if (found
3584 && ix == VEC_length (tree, args)
3585 /* May be this should be sufficient_parms_p instead,
3586 depending on how exactly should user-defined literals
3587 work in presence of default arguments on the literal
3588 operator parameters. */
3589 && argtypes == void_list_node)
3590 return fn;
3594 return error_mark_node;
3597 /* Parse a user-defined char constant. Returns a call to a user-defined
3598 literal operator taking the character as an argument. */
3600 static tree
3601 cp_parser_userdef_char_literal (cp_parser *parser)
3603 cp_token *token = cp_lexer_consume_token (parser->lexer);
3604 tree literal = token->u.value;
3605 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3606 tree value = USERDEF_LITERAL_VALUE (literal);
3607 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3608 tree decl, result;
3610 /* Build up a call to the user-defined operator */
3611 /* Lookup the name we got back from the id-expression. */
3612 VEC(tree,gc) *args = make_tree_vector ();
3613 VEC_safe_push (tree, gc, args, value);
3614 decl = lookup_literal_operator (name, args);
3615 if (!decl || decl == error_mark_node)
3617 error ("unable to find character literal operator %qD with %qT argument",
3618 name, TREE_TYPE (value));
3619 release_tree_vector (args);
3620 return error_mark_node;
3622 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3623 release_tree_vector (args);
3624 if (result != error_mark_node)
3625 return result;
3627 error ("unable to find character literal operator %qD with %qT argument",
3628 name, TREE_TYPE (value));
3629 return error_mark_node;
3632 /* A subroutine of cp_parser_userdef_numeric_literal to
3633 create a char... template parameter pack from a string node. */
3635 static tree
3636 make_char_string_pack (tree value)
3638 tree charvec;
3639 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3640 const char *str = TREE_STRING_POINTER (value);
3641 int i, len = TREE_STRING_LENGTH (value) - 1;
3642 tree argvec = make_tree_vec (1);
3644 /* Fill in CHARVEC with all of the parameters. */
3645 charvec = make_tree_vec (len);
3646 for (i = 0; i < len; ++i)
3647 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3649 /* Build the argument packs. */
3650 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3651 TREE_TYPE (argpack) = char_type_node;
3653 TREE_VEC_ELT (argvec, 0) = argpack;
3655 return argvec;
3658 /* Parse a user-defined numeric constant. returns a call to a user-defined
3659 literal operator. */
3661 static tree
3662 cp_parser_userdef_numeric_literal (cp_parser *parser)
3664 cp_token *token = cp_lexer_consume_token (parser->lexer);
3665 tree literal = token->u.value;
3666 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3667 tree value = USERDEF_LITERAL_VALUE (literal);
3668 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3669 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3670 tree decl, result;
3671 VEC(tree,gc) *args;
3673 /* Look for a literal operator taking the exact type of numeric argument
3674 as the literal value. */
3675 args = make_tree_vector ();
3676 VEC_safe_push (tree, gc, args, value);
3677 decl = lookup_literal_operator (name, args);
3678 if (decl && decl != error_mark_node)
3680 result = finish_call_expr (decl, &args, false, true, tf_none);
3681 if (result != error_mark_node)
3683 release_tree_vector (args);
3684 return result;
3687 release_tree_vector (args);
3689 /* If the numeric argument didn't work, look for a raw literal
3690 operator taking a const char* argument consisting of the number
3691 in string format. */
3692 args = make_tree_vector ();
3693 VEC_safe_push (tree, gc, args, num_string);
3694 decl = lookup_literal_operator (name, args);
3695 if (decl && decl != error_mark_node)
3697 result = finish_call_expr (decl, &args, false, true, tf_none);
3698 if (result != error_mark_node)
3700 release_tree_vector (args);
3701 return result;
3704 release_tree_vector (args);
3706 /* If the raw literal didn't work, look for a non-type template
3707 function with parameter pack char.... Call the function with
3708 template parameter characters representing the number. */
3709 args = make_tree_vector ();
3710 decl = lookup_literal_operator (name, args);
3711 if (decl && decl != error_mark_node)
3713 tree tmpl_args = make_char_string_pack (num_string);
3714 decl = lookup_template_function (decl, tmpl_args);
3715 result = finish_call_expr (decl, &args, false, true, tf_none);
3716 if (result != error_mark_node)
3718 release_tree_vector (args);
3719 return result;
3722 release_tree_vector (args);
3724 error ("unable to find numeric literal operator %qD", name);
3725 return error_mark_node;
3728 /* Parse a user-defined string constant. Returns a call to a user-defined
3729 literal operator taking a character pointer and the length of the string
3730 as arguments. */
3732 static tree
3733 cp_parser_userdef_string_literal (cp_token *token)
3735 tree literal = token->u.value;
3736 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3737 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3738 tree value = USERDEF_LITERAL_VALUE (literal);
3739 int len = TREE_STRING_LENGTH (value)
3740 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3741 tree decl, result;
3743 /* Build up a call to the user-defined operator */
3744 /* Lookup the name we got back from the id-expression. */
3745 VEC(tree,gc) *args = make_tree_vector ();
3746 VEC_safe_push (tree, gc, args, value);
3747 VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3748 decl = lookup_name (name);
3749 if (!decl || decl == error_mark_node)
3751 error ("unable to find string literal operator %qD", name);
3752 release_tree_vector (args);
3753 return error_mark_node;
3755 result = finish_call_expr (decl, &args, false, true, tf_none);
3756 release_tree_vector (args);
3757 if (result != error_mark_node)
3758 return result;
3760 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3761 name, TREE_TYPE (value), size_type_node);
3762 return error_mark_node;
3766 /* Basic concepts [gram.basic] */
3768 /* Parse a translation-unit.
3770 translation-unit:
3771 declaration-seq [opt]
3773 Returns TRUE if all went well. */
3775 static bool
3776 cp_parser_translation_unit (cp_parser* parser)
3778 /* The address of the first non-permanent object on the declarator
3779 obstack. */
3780 static void *declarator_obstack_base;
3782 bool success;
3784 /* Create the declarator obstack, if necessary. */
3785 if (!cp_error_declarator)
3787 gcc_obstack_init (&declarator_obstack);
3788 /* Create the error declarator. */
3789 cp_error_declarator = make_declarator (cdk_error);
3790 /* Create the empty parameter list. */
3791 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3792 /* Remember where the base of the declarator obstack lies. */
3793 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3796 cp_parser_declaration_seq_opt (parser);
3798 /* If there are no tokens left then all went well. */
3799 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3801 /* Get rid of the token array; we don't need it any more. */
3802 cp_lexer_destroy (parser->lexer);
3803 parser->lexer = NULL;
3805 /* This file might have been a context that's implicitly extern
3806 "C". If so, pop the lang context. (Only relevant for PCH.) */
3807 if (parser->implicit_extern_c)
3809 pop_lang_context ();
3810 parser->implicit_extern_c = false;
3813 /* Finish up. */
3814 finish_translation_unit ();
3816 success = true;
3818 else
3820 cp_parser_error (parser, "expected declaration");
3821 success = false;
3824 /* Make sure the declarator obstack was fully cleaned up. */
3825 gcc_assert (obstack_next_free (&declarator_obstack)
3826 == declarator_obstack_base);
3828 /* All went well. */
3829 return success;
3832 /* Expressions [gram.expr] */
3834 /* Parse a primary-expression.
3836 primary-expression:
3837 literal
3838 this
3839 ( expression )
3840 id-expression
3842 GNU Extensions:
3844 primary-expression:
3845 ( compound-statement )
3846 __builtin_va_arg ( assignment-expression , type-id )
3847 __builtin_offsetof ( type-id , offsetof-expression )
3849 C++ Extensions:
3850 __has_nothrow_assign ( type-id )
3851 __has_nothrow_constructor ( type-id )
3852 __has_nothrow_copy ( type-id )
3853 __has_trivial_assign ( type-id )
3854 __has_trivial_constructor ( type-id )
3855 __has_trivial_copy ( type-id )
3856 __has_trivial_destructor ( type-id )
3857 __has_virtual_destructor ( type-id )
3858 __is_abstract ( type-id )
3859 __is_base_of ( type-id , type-id )
3860 __is_class ( type-id )
3861 __is_convertible_to ( type-id , type-id )
3862 __is_empty ( type-id )
3863 __is_enum ( type-id )
3864 __is_final ( type-id )
3865 __is_literal_type ( type-id )
3866 __is_pod ( type-id )
3867 __is_polymorphic ( type-id )
3868 __is_std_layout ( type-id )
3869 __is_trivial ( type-id )
3870 __is_union ( type-id )
3872 Objective-C++ Extension:
3874 primary-expression:
3875 objc-expression
3877 literal:
3878 __null
3880 ADDRESS_P is true iff this expression was immediately preceded by
3881 "&" and therefore might denote a pointer-to-member. CAST_P is true
3882 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3883 true iff this expression is a template argument.
3885 Returns a representation of the expression. Upon return, *IDK
3886 indicates what kind of id-expression (if any) was present. */
3888 static tree
3889 cp_parser_primary_expression (cp_parser *parser,
3890 bool address_p,
3891 bool cast_p,
3892 bool template_arg_p,
3893 cp_id_kind *idk)
3895 cp_token *token = NULL;
3897 /* Assume the primary expression is not an id-expression. */
3898 *idk = CP_ID_KIND_NONE;
3900 /* Peek at the next token. */
3901 token = cp_lexer_peek_token (parser->lexer);
3902 switch (token->type)
3904 /* literal:
3905 integer-literal
3906 character-literal
3907 floating-literal
3908 string-literal
3909 boolean-literal
3910 pointer-literal
3911 user-defined-literal */
3912 case CPP_CHAR:
3913 case CPP_CHAR16:
3914 case CPP_CHAR32:
3915 case CPP_WCHAR:
3916 case CPP_NUMBER:
3917 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3918 return cp_parser_userdef_numeric_literal (parser);
3919 token = cp_lexer_consume_token (parser->lexer);
3920 if (TREE_CODE (token->u.value) == FIXED_CST)
3922 error_at (token->location,
3923 "fixed-point types not supported in C++");
3924 return error_mark_node;
3926 /* Floating-point literals are only allowed in an integral
3927 constant expression if they are cast to an integral or
3928 enumeration type. */
3929 if (TREE_CODE (token->u.value) == REAL_CST
3930 && parser->integral_constant_expression_p
3931 && pedantic)
3933 /* CAST_P will be set even in invalid code like "int(2.7 +
3934 ...)". Therefore, we have to check that the next token
3935 is sure to end the cast. */
3936 if (cast_p)
3938 cp_token *next_token;
3940 next_token = cp_lexer_peek_token (parser->lexer);
3941 if (/* The comma at the end of an
3942 enumerator-definition. */
3943 next_token->type != CPP_COMMA
3944 /* The curly brace at the end of an enum-specifier. */
3945 && next_token->type != CPP_CLOSE_BRACE
3946 /* The end of a statement. */
3947 && next_token->type != CPP_SEMICOLON
3948 /* The end of the cast-expression. */
3949 && next_token->type != CPP_CLOSE_PAREN
3950 /* The end of an array bound. */
3951 && next_token->type != CPP_CLOSE_SQUARE
3952 /* The closing ">" in a template-argument-list. */
3953 && (next_token->type != CPP_GREATER
3954 || parser->greater_than_is_operator_p)
3955 /* C++0x only: A ">>" treated like two ">" tokens,
3956 in a template-argument-list. */
3957 && (next_token->type != CPP_RSHIFT
3958 || (cxx_dialect == cxx98)
3959 || parser->greater_than_is_operator_p))
3960 cast_p = false;
3963 /* If we are within a cast, then the constraint that the
3964 cast is to an integral or enumeration type will be
3965 checked at that point. If we are not within a cast, then
3966 this code is invalid. */
3967 if (!cast_p)
3968 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3970 return token->u.value;
3972 case CPP_CHAR_USERDEF:
3973 case CPP_CHAR16_USERDEF:
3974 case CPP_CHAR32_USERDEF:
3975 case CPP_WCHAR_USERDEF:
3976 return cp_parser_userdef_char_literal (parser);
3978 case CPP_STRING:
3979 case CPP_STRING16:
3980 case CPP_STRING32:
3981 case CPP_WSTRING:
3982 case CPP_UTF8STRING:
3983 case CPP_STRING_USERDEF:
3984 case CPP_STRING16_USERDEF:
3985 case CPP_STRING32_USERDEF:
3986 case CPP_WSTRING_USERDEF:
3987 case CPP_UTF8STRING_USERDEF:
3988 /* ??? Should wide strings be allowed when parser->translate_strings_p
3989 is false (i.e. in attributes)? If not, we can kill the third
3990 argument to cp_parser_string_literal. */
3991 return cp_parser_string_literal (parser,
3992 parser->translate_strings_p,
3993 true);
3995 case CPP_OPEN_PAREN:
3997 tree expr;
3998 bool saved_greater_than_is_operator_p;
4000 /* Consume the `('. */
4001 cp_lexer_consume_token (parser->lexer);
4002 /* Within a parenthesized expression, a `>' token is always
4003 the greater-than operator. */
4004 saved_greater_than_is_operator_p
4005 = parser->greater_than_is_operator_p;
4006 parser->greater_than_is_operator_p = true;
4007 /* If we see `( { ' then we are looking at the beginning of
4008 a GNU statement-expression. */
4009 if (cp_parser_allow_gnu_extensions_p (parser)
4010 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4012 /* Statement-expressions are not allowed by the standard. */
4013 pedwarn (token->location, OPT_Wpedantic,
4014 "ISO C++ forbids braced-groups within expressions");
4016 /* And they're not allowed outside of a function-body; you
4017 cannot, for example, write:
4019 int i = ({ int j = 3; j + 1; });
4021 at class or namespace scope. */
4022 if (!parser->in_function_body
4023 || parser->in_template_argument_list_p)
4025 error_at (token->location,
4026 "statement-expressions are not allowed outside "
4027 "functions nor in template-argument lists");
4028 cp_parser_skip_to_end_of_block_or_statement (parser);
4029 expr = error_mark_node;
4031 else
4033 /* Start the statement-expression. */
4034 expr = begin_stmt_expr ();
4035 /* Parse the compound-statement. */
4036 cp_parser_compound_statement (parser, expr, false, false);
4037 /* Finish up. */
4038 expr = finish_stmt_expr (expr, false);
4041 else
4043 /* Parse the parenthesized expression. */
4044 expr = cp_parser_expression (parser, cast_p, idk);
4045 /* Let the front end know that this expression was
4046 enclosed in parentheses. This matters in case, for
4047 example, the expression is of the form `A::B', since
4048 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4049 not. */
4050 finish_parenthesized_expr (expr);
4051 /* DR 705: Wrapping an unqualified name in parentheses
4052 suppresses arg-dependent lookup. We want to pass back
4053 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4054 (c++/37862), but none of the others. */
4055 if (*idk != CP_ID_KIND_QUALIFIED)
4056 *idk = CP_ID_KIND_NONE;
4058 /* The `>' token might be the end of a template-id or
4059 template-parameter-list now. */
4060 parser->greater_than_is_operator_p
4061 = saved_greater_than_is_operator_p;
4062 /* Consume the `)'. */
4063 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4064 cp_parser_skip_to_end_of_statement (parser);
4066 return expr;
4069 case CPP_OPEN_SQUARE:
4070 if (c_dialect_objc ())
4071 /* We have an Objective-C++ message. */
4072 return cp_parser_objc_expression (parser);
4074 tree lam = cp_parser_lambda_expression (parser);
4075 /* Don't warn about a failed tentative parse. */
4076 if (cp_parser_error_occurred (parser))
4077 return error_mark_node;
4078 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4079 return lam;
4082 case CPP_OBJC_STRING:
4083 if (c_dialect_objc ())
4084 /* We have an Objective-C++ string literal. */
4085 return cp_parser_objc_expression (parser);
4086 cp_parser_error (parser, "expected primary-expression");
4087 return error_mark_node;
4089 case CPP_KEYWORD:
4090 switch (token->keyword)
4092 /* These two are the boolean literals. */
4093 case RID_TRUE:
4094 cp_lexer_consume_token (parser->lexer);
4095 return boolean_true_node;
4096 case RID_FALSE:
4097 cp_lexer_consume_token (parser->lexer);
4098 return boolean_false_node;
4100 /* The `__null' literal. */
4101 case RID_NULL:
4102 cp_lexer_consume_token (parser->lexer);
4103 return null_node;
4105 /* The `nullptr' literal. */
4106 case RID_NULLPTR:
4107 cp_lexer_consume_token (parser->lexer);
4108 return nullptr_node;
4110 /* Recognize the `this' keyword. */
4111 case RID_THIS:
4112 cp_lexer_consume_token (parser->lexer);
4113 if (parser->local_variables_forbidden_p)
4115 error_at (token->location,
4116 "%<this%> may not be used in this context");
4117 return error_mark_node;
4119 /* Pointers cannot appear in constant-expressions. */
4120 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4121 return error_mark_node;
4122 return finish_this_expr ();
4124 /* The `operator' keyword can be the beginning of an
4125 id-expression. */
4126 case RID_OPERATOR:
4127 goto id_expression;
4129 case RID_FUNCTION_NAME:
4130 case RID_PRETTY_FUNCTION_NAME:
4131 case RID_C99_FUNCTION_NAME:
4133 non_integral_constant name;
4135 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4136 __func__ are the names of variables -- but they are
4137 treated specially. Therefore, they are handled here,
4138 rather than relying on the generic id-expression logic
4139 below. Grammatically, these names are id-expressions.
4141 Consume the token. */
4142 token = cp_lexer_consume_token (parser->lexer);
4144 switch (token->keyword)
4146 case RID_FUNCTION_NAME:
4147 name = NIC_FUNC_NAME;
4148 break;
4149 case RID_PRETTY_FUNCTION_NAME:
4150 name = NIC_PRETTY_FUNC;
4151 break;
4152 case RID_C99_FUNCTION_NAME:
4153 name = NIC_C99_FUNC;
4154 break;
4155 default:
4156 gcc_unreachable ();
4159 if (cp_parser_non_integral_constant_expression (parser, name))
4160 return error_mark_node;
4162 /* Look up the name. */
4163 return finish_fname (token->u.value);
4166 case RID_VA_ARG:
4168 tree expression;
4169 tree type;
4170 source_location type_location;
4172 /* The `__builtin_va_arg' construct is used to handle
4173 `va_arg'. Consume the `__builtin_va_arg' token. */
4174 cp_lexer_consume_token (parser->lexer);
4175 /* Look for the opening `('. */
4176 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4177 /* Now, parse the assignment-expression. */
4178 expression = cp_parser_assignment_expression (parser,
4179 /*cast_p=*/false, NULL);
4180 /* Look for the `,'. */
4181 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4182 type_location = cp_lexer_peek_token (parser->lexer)->location;
4183 /* Parse the type-id. */
4184 type = cp_parser_type_id (parser);
4185 /* Look for the closing `)'. */
4186 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4187 /* Using `va_arg' in a constant-expression is not
4188 allowed. */
4189 if (cp_parser_non_integral_constant_expression (parser,
4190 NIC_VA_ARG))
4191 return error_mark_node;
4192 return build_x_va_arg (type_location, expression, type);
4195 case RID_OFFSETOF:
4196 return cp_parser_builtin_offsetof (parser);
4198 case RID_HAS_NOTHROW_ASSIGN:
4199 case RID_HAS_NOTHROW_CONSTRUCTOR:
4200 case RID_HAS_NOTHROW_COPY:
4201 case RID_HAS_TRIVIAL_ASSIGN:
4202 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4203 case RID_HAS_TRIVIAL_COPY:
4204 case RID_HAS_TRIVIAL_DESTRUCTOR:
4205 case RID_HAS_VIRTUAL_DESTRUCTOR:
4206 case RID_IS_ABSTRACT:
4207 case RID_IS_BASE_OF:
4208 case RID_IS_CLASS:
4209 case RID_IS_CONVERTIBLE_TO:
4210 case RID_IS_EMPTY:
4211 case RID_IS_ENUM:
4212 case RID_IS_FINAL:
4213 case RID_IS_LITERAL_TYPE:
4214 case RID_IS_POD:
4215 case RID_IS_POLYMORPHIC:
4216 case RID_IS_STD_LAYOUT:
4217 case RID_IS_TRIVIAL:
4218 case RID_IS_UNION:
4219 return cp_parser_trait_expr (parser, token->keyword);
4221 /* Objective-C++ expressions. */
4222 case RID_AT_ENCODE:
4223 case RID_AT_PROTOCOL:
4224 case RID_AT_SELECTOR:
4225 return cp_parser_objc_expression (parser);
4227 case RID_TEMPLATE:
4228 if (parser->in_function_body
4229 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4230 == CPP_LESS))
4232 error_at (token->location,
4233 "a template declaration cannot appear at block scope");
4234 cp_parser_skip_to_end_of_block_or_statement (parser);
4235 return error_mark_node;
4237 default:
4238 cp_parser_error (parser, "expected primary-expression");
4239 return error_mark_node;
4242 /* An id-expression can start with either an identifier, a
4243 `::' as the beginning of a qualified-id, or the "operator"
4244 keyword. */
4245 case CPP_NAME:
4246 case CPP_SCOPE:
4247 case CPP_TEMPLATE_ID:
4248 case CPP_NESTED_NAME_SPECIFIER:
4250 tree id_expression;
4251 tree decl;
4252 const char *error_msg;
4253 bool template_p;
4254 bool done;
4255 cp_token *id_expr_token;
4257 id_expression:
4258 /* Parse the id-expression. */
4259 id_expression
4260 = cp_parser_id_expression (parser,
4261 /*template_keyword_p=*/false,
4262 /*check_dependency_p=*/true,
4263 &template_p,
4264 /*declarator_p=*/false,
4265 /*optional_p=*/false);
4266 if (id_expression == error_mark_node)
4267 return error_mark_node;
4268 id_expr_token = token;
4269 token = cp_lexer_peek_token (parser->lexer);
4270 done = (token->type != CPP_OPEN_SQUARE
4271 && token->type != CPP_OPEN_PAREN
4272 && token->type != CPP_DOT
4273 && token->type != CPP_DEREF
4274 && token->type != CPP_PLUS_PLUS
4275 && token->type != CPP_MINUS_MINUS);
4276 /* If we have a template-id, then no further lookup is
4277 required. If the template-id was for a template-class, we
4278 will sometimes have a TYPE_DECL at this point. */
4279 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4280 || TREE_CODE (id_expression) == TYPE_DECL)
4281 decl = id_expression;
4282 /* Look up the name. */
4283 else
4285 tree ambiguous_decls;
4287 /* If we already know that this lookup is ambiguous, then
4288 we've already issued an error message; there's no reason
4289 to check again. */
4290 if (id_expr_token->type == CPP_NAME
4291 && id_expr_token->ambiguous_p)
4293 cp_parser_simulate_error (parser);
4294 return error_mark_node;
4297 decl = cp_parser_lookup_name (parser, id_expression,
4298 none_type,
4299 template_p,
4300 /*is_namespace=*/false,
4301 /*check_dependency=*/true,
4302 &ambiguous_decls,
4303 id_expr_token->location);
4304 /* If the lookup was ambiguous, an error will already have
4305 been issued. */
4306 if (ambiguous_decls)
4307 return error_mark_node;
4309 /* In Objective-C++, we may have an Objective-C 2.0
4310 dot-syntax for classes here. */
4311 if (c_dialect_objc ()
4312 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4313 && TREE_CODE (decl) == TYPE_DECL
4314 && objc_is_class_name (decl))
4316 tree component;
4317 cp_lexer_consume_token (parser->lexer);
4318 component = cp_parser_identifier (parser);
4319 if (component == error_mark_node)
4320 return error_mark_node;
4322 return objc_build_class_component_ref (id_expression, component);
4325 /* In Objective-C++, an instance variable (ivar) may be preferred
4326 to whatever cp_parser_lookup_name() found. */
4327 decl = objc_lookup_ivar (decl, id_expression);
4329 /* If name lookup gives us a SCOPE_REF, then the
4330 qualifying scope was dependent. */
4331 if (TREE_CODE (decl) == SCOPE_REF)
4333 /* At this point, we do not know if DECL is a valid
4334 integral constant expression. We assume that it is
4335 in fact such an expression, so that code like:
4337 template <int N> struct A {
4338 int a[B<N>::i];
4341 is accepted. At template-instantiation time, we
4342 will check that B<N>::i is actually a constant. */
4343 return decl;
4345 /* Check to see if DECL is a local variable in a context
4346 where that is forbidden. */
4347 if (parser->local_variables_forbidden_p
4348 && local_variable_p (decl))
4350 /* It might be that we only found DECL because we are
4351 trying to be generous with pre-ISO scoping rules.
4352 For example, consider:
4354 int i;
4355 void g() {
4356 for (int i = 0; i < 10; ++i) {}
4357 extern void f(int j = i);
4360 Here, name look up will originally find the out
4361 of scope `i'. We need to issue a warning message,
4362 but then use the global `i'. */
4363 decl = check_for_out_of_scope_variable (decl);
4364 if (local_variable_p (decl))
4366 error_at (id_expr_token->location,
4367 "local variable %qD may not appear in this context",
4368 decl);
4369 return error_mark_node;
4374 decl = (finish_id_expression
4375 (id_expression, decl, parser->scope,
4376 idk,
4377 parser->integral_constant_expression_p,
4378 parser->allow_non_integral_constant_expression_p,
4379 &parser->non_integral_constant_expression_p,
4380 template_p, done, address_p,
4381 template_arg_p,
4382 &error_msg,
4383 id_expr_token->location));
4384 if (error_msg)
4385 cp_parser_error (parser, error_msg);
4386 return decl;
4389 /* Anything else is an error. */
4390 default:
4391 cp_parser_error (parser, "expected primary-expression");
4392 return error_mark_node;
4396 /* Parse an id-expression.
4398 id-expression:
4399 unqualified-id
4400 qualified-id
4402 qualified-id:
4403 :: [opt] nested-name-specifier template [opt] unqualified-id
4404 :: identifier
4405 :: operator-function-id
4406 :: template-id
4408 Return a representation of the unqualified portion of the
4409 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4410 a `::' or nested-name-specifier.
4412 Often, if the id-expression was a qualified-id, the caller will
4413 want to make a SCOPE_REF to represent the qualified-id. This
4414 function does not do this in order to avoid wastefully creating
4415 SCOPE_REFs when they are not required.
4417 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4418 `template' keyword.
4420 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4421 uninstantiated templates.
4423 If *TEMPLATE_P is non-NULL, it is set to true iff the
4424 `template' keyword is used to explicitly indicate that the entity
4425 named is a template.
4427 If DECLARATOR_P is true, the id-expression is appearing as part of
4428 a declarator, rather than as part of an expression. */
4430 static tree
4431 cp_parser_id_expression (cp_parser *parser,
4432 bool template_keyword_p,
4433 bool check_dependency_p,
4434 bool *template_p,
4435 bool declarator_p,
4436 bool optional_p)
4438 bool global_scope_p;
4439 bool nested_name_specifier_p;
4441 /* Assume the `template' keyword was not used. */
4442 if (template_p)
4443 *template_p = template_keyword_p;
4445 /* Look for the optional `::' operator. */
4446 global_scope_p
4447 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4448 != NULL_TREE);
4449 /* Look for the optional nested-name-specifier. */
4450 nested_name_specifier_p
4451 = (cp_parser_nested_name_specifier_opt (parser,
4452 /*typename_keyword_p=*/false,
4453 check_dependency_p,
4454 /*type_p=*/false,
4455 declarator_p)
4456 != NULL_TREE);
4457 /* If there is a nested-name-specifier, then we are looking at
4458 the first qualified-id production. */
4459 if (nested_name_specifier_p)
4461 tree saved_scope;
4462 tree saved_object_scope;
4463 tree saved_qualifying_scope;
4464 tree unqualified_id;
4465 bool is_template;
4467 /* See if the next token is the `template' keyword. */
4468 if (!template_p)
4469 template_p = &is_template;
4470 *template_p = cp_parser_optional_template_keyword (parser);
4471 /* Name lookup we do during the processing of the
4472 unqualified-id might obliterate SCOPE. */
4473 saved_scope = parser->scope;
4474 saved_object_scope = parser->object_scope;
4475 saved_qualifying_scope = parser->qualifying_scope;
4476 /* Process the final unqualified-id. */
4477 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4478 check_dependency_p,
4479 declarator_p,
4480 /*optional_p=*/false);
4481 /* Restore the SAVED_SCOPE for our caller. */
4482 parser->scope = saved_scope;
4483 parser->object_scope = saved_object_scope;
4484 parser->qualifying_scope = saved_qualifying_scope;
4486 return unqualified_id;
4488 /* Otherwise, if we are in global scope, then we are looking at one
4489 of the other qualified-id productions. */
4490 else if (global_scope_p)
4492 cp_token *token;
4493 tree id;
4495 /* Peek at the next token. */
4496 token = cp_lexer_peek_token (parser->lexer);
4498 /* If it's an identifier, and the next token is not a "<", then
4499 we can avoid the template-id case. This is an optimization
4500 for this common case. */
4501 if (token->type == CPP_NAME
4502 && !cp_parser_nth_token_starts_template_argument_list_p
4503 (parser, 2))
4504 return cp_parser_identifier (parser);
4506 cp_parser_parse_tentatively (parser);
4507 /* Try a template-id. */
4508 id = cp_parser_template_id (parser,
4509 /*template_keyword_p=*/false,
4510 /*check_dependency_p=*/true,
4511 none_type,
4512 declarator_p);
4513 /* If that worked, we're done. */
4514 if (cp_parser_parse_definitely (parser))
4515 return id;
4517 /* Peek at the next token. (Changes in the token buffer may
4518 have invalidated the pointer obtained above.) */
4519 token = cp_lexer_peek_token (parser->lexer);
4521 switch (token->type)
4523 case CPP_NAME:
4524 return cp_parser_identifier (parser);
4526 case CPP_KEYWORD:
4527 if (token->keyword == RID_OPERATOR)
4528 return cp_parser_operator_function_id (parser);
4529 /* Fall through. */
4531 default:
4532 cp_parser_error (parser, "expected id-expression");
4533 return error_mark_node;
4536 else
4537 return cp_parser_unqualified_id (parser, template_keyword_p,
4538 /*check_dependency_p=*/true,
4539 declarator_p,
4540 optional_p);
4543 /* Parse an unqualified-id.
4545 unqualified-id:
4546 identifier
4547 operator-function-id
4548 conversion-function-id
4549 ~ class-name
4550 template-id
4552 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4553 keyword, in a construct like `A::template ...'.
4555 Returns a representation of unqualified-id. For the `identifier'
4556 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4557 production a BIT_NOT_EXPR is returned; the operand of the
4558 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4559 other productions, see the documentation accompanying the
4560 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4561 names are looked up in uninstantiated templates. If DECLARATOR_P
4562 is true, the unqualified-id is appearing as part of a declarator,
4563 rather than as part of an expression. */
4565 static tree
4566 cp_parser_unqualified_id (cp_parser* parser,
4567 bool template_keyword_p,
4568 bool check_dependency_p,
4569 bool declarator_p,
4570 bool optional_p)
4572 cp_token *token;
4574 /* Peek at the next token. */
4575 token = cp_lexer_peek_token (parser->lexer);
4577 switch (token->type)
4579 case CPP_NAME:
4581 tree id;
4583 /* We don't know yet whether or not this will be a
4584 template-id. */
4585 cp_parser_parse_tentatively (parser);
4586 /* Try a template-id. */
4587 id = cp_parser_template_id (parser, template_keyword_p,
4588 check_dependency_p,
4589 none_type,
4590 declarator_p);
4591 /* If it worked, we're done. */
4592 if (cp_parser_parse_definitely (parser))
4593 return id;
4594 /* Otherwise, it's an ordinary identifier. */
4595 return cp_parser_identifier (parser);
4598 case CPP_TEMPLATE_ID:
4599 return cp_parser_template_id (parser, template_keyword_p,
4600 check_dependency_p,
4601 none_type,
4602 declarator_p);
4604 case CPP_COMPL:
4606 tree type_decl;
4607 tree qualifying_scope;
4608 tree object_scope;
4609 tree scope;
4610 bool done;
4612 /* Consume the `~' token. */
4613 cp_lexer_consume_token (parser->lexer);
4614 /* Parse the class-name. The standard, as written, seems to
4615 say that:
4617 template <typename T> struct S { ~S (); };
4618 template <typename T> S<T>::~S() {}
4620 is invalid, since `~' must be followed by a class-name, but
4621 `S<T>' is dependent, and so not known to be a class.
4622 That's not right; we need to look in uninstantiated
4623 templates. A further complication arises from:
4625 template <typename T> void f(T t) {
4626 t.T::~T();
4629 Here, it is not possible to look up `T' in the scope of `T'
4630 itself. We must look in both the current scope, and the
4631 scope of the containing complete expression.
4633 Yet another issue is:
4635 struct S {
4636 int S;
4637 ~S();
4640 S::~S() {}
4642 The standard does not seem to say that the `S' in `~S'
4643 should refer to the type `S' and not the data member
4644 `S::S'. */
4646 /* DR 244 says that we look up the name after the "~" in the
4647 same scope as we looked up the qualifying name. That idea
4648 isn't fully worked out; it's more complicated than that. */
4649 scope = parser->scope;
4650 object_scope = parser->object_scope;
4651 qualifying_scope = parser->qualifying_scope;
4653 /* Check for invalid scopes. */
4654 if (scope == error_mark_node)
4656 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4657 cp_lexer_consume_token (parser->lexer);
4658 return error_mark_node;
4660 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4662 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4663 error_at (token->location,
4664 "scope %qT before %<~%> is not a class-name",
4665 scope);
4666 cp_parser_simulate_error (parser);
4667 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4668 cp_lexer_consume_token (parser->lexer);
4669 return error_mark_node;
4671 gcc_assert (!scope || TYPE_P (scope));
4673 /* If the name is of the form "X::~X" it's OK even if X is a
4674 typedef. */
4675 token = cp_lexer_peek_token (parser->lexer);
4676 if (scope
4677 && token->type == CPP_NAME
4678 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4679 != CPP_LESS)
4680 && (token->u.value == TYPE_IDENTIFIER (scope)
4681 || (CLASS_TYPE_P (scope)
4682 && constructor_name_p (token->u.value, scope))))
4684 cp_lexer_consume_token (parser->lexer);
4685 return build_nt (BIT_NOT_EXPR, scope);
4688 /* If there was an explicit qualification (S::~T), first look
4689 in the scope given by the qualification (i.e., S).
4691 Note: in the calls to cp_parser_class_name below we pass
4692 typename_type so that lookup finds the injected-class-name
4693 rather than the constructor. */
4694 done = false;
4695 type_decl = NULL_TREE;
4696 if (scope)
4698 cp_parser_parse_tentatively (parser);
4699 type_decl = cp_parser_class_name (parser,
4700 /*typename_keyword_p=*/false,
4701 /*template_keyword_p=*/false,
4702 typename_type,
4703 /*check_dependency=*/false,
4704 /*class_head_p=*/false,
4705 declarator_p);
4706 if (cp_parser_parse_definitely (parser))
4707 done = true;
4709 /* In "N::S::~S", look in "N" as well. */
4710 if (!done && scope && qualifying_scope)
4712 cp_parser_parse_tentatively (parser);
4713 parser->scope = qualifying_scope;
4714 parser->object_scope = NULL_TREE;
4715 parser->qualifying_scope = NULL_TREE;
4716 type_decl
4717 = cp_parser_class_name (parser,
4718 /*typename_keyword_p=*/false,
4719 /*template_keyword_p=*/false,
4720 typename_type,
4721 /*check_dependency=*/false,
4722 /*class_head_p=*/false,
4723 declarator_p);
4724 if (cp_parser_parse_definitely (parser))
4725 done = true;
4727 /* In "p->S::~T", look in the scope given by "*p" as well. */
4728 else if (!done && object_scope)
4730 cp_parser_parse_tentatively (parser);
4731 parser->scope = object_scope;
4732 parser->object_scope = NULL_TREE;
4733 parser->qualifying_scope = NULL_TREE;
4734 type_decl
4735 = cp_parser_class_name (parser,
4736 /*typename_keyword_p=*/false,
4737 /*template_keyword_p=*/false,
4738 typename_type,
4739 /*check_dependency=*/false,
4740 /*class_head_p=*/false,
4741 declarator_p);
4742 if (cp_parser_parse_definitely (parser))
4743 done = true;
4745 /* Look in the surrounding context. */
4746 if (!done)
4748 parser->scope = NULL_TREE;
4749 parser->object_scope = NULL_TREE;
4750 parser->qualifying_scope = NULL_TREE;
4751 if (processing_template_decl)
4752 cp_parser_parse_tentatively (parser);
4753 type_decl
4754 = cp_parser_class_name (parser,
4755 /*typename_keyword_p=*/false,
4756 /*template_keyword_p=*/false,
4757 typename_type,
4758 /*check_dependency=*/false,
4759 /*class_head_p=*/false,
4760 declarator_p);
4761 if (processing_template_decl
4762 && ! cp_parser_parse_definitely (parser))
4764 /* We couldn't find a type with this name, so just accept
4765 it and check for a match at instantiation time. */
4766 type_decl = cp_parser_identifier (parser);
4767 if (type_decl != error_mark_node)
4768 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4769 return type_decl;
4772 /* If an error occurred, assume that the name of the
4773 destructor is the same as the name of the qualifying
4774 class. That allows us to keep parsing after running
4775 into ill-formed destructor names. */
4776 if (type_decl == error_mark_node && scope)
4777 return build_nt (BIT_NOT_EXPR, scope);
4778 else if (type_decl == error_mark_node)
4779 return error_mark_node;
4781 /* Check that destructor name and scope match. */
4782 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4784 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4785 error_at (token->location,
4786 "declaration of %<~%T%> as member of %qT",
4787 type_decl, scope);
4788 cp_parser_simulate_error (parser);
4789 return error_mark_node;
4792 /* [class.dtor]
4794 A typedef-name that names a class shall not be used as the
4795 identifier in the declarator for a destructor declaration. */
4796 if (declarator_p
4797 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4798 && !DECL_SELF_REFERENCE_P (type_decl)
4799 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4800 error_at (token->location,
4801 "typedef-name %qD used as destructor declarator",
4802 type_decl);
4804 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4807 case CPP_KEYWORD:
4808 if (token->keyword == RID_OPERATOR)
4810 tree id;
4812 /* This could be a template-id, so we try that first. */
4813 cp_parser_parse_tentatively (parser);
4814 /* Try a template-id. */
4815 id = cp_parser_template_id (parser, template_keyword_p,
4816 /*check_dependency_p=*/true,
4817 none_type,
4818 declarator_p);
4819 /* If that worked, we're done. */
4820 if (cp_parser_parse_definitely (parser))
4821 return id;
4822 /* We still don't know whether we're looking at an
4823 operator-function-id or a conversion-function-id. */
4824 cp_parser_parse_tentatively (parser);
4825 /* Try an operator-function-id. */
4826 id = cp_parser_operator_function_id (parser);
4827 /* If that didn't work, try a conversion-function-id. */
4828 if (!cp_parser_parse_definitely (parser))
4829 id = cp_parser_conversion_function_id (parser);
4830 else if (UDLIT_OPER_P (id))
4832 /* 17.6.3.3.5 */
4833 const char *name = UDLIT_OP_SUFFIX (id);
4834 if (name[0] != '_' && !in_system_header)
4835 warning (0, "literal operator suffixes not preceded by %<_%>"
4836 " are reserved for future standardization");
4839 return id;
4841 /* Fall through. */
4843 default:
4844 if (optional_p)
4845 return NULL_TREE;
4846 cp_parser_error (parser, "expected unqualified-id");
4847 return error_mark_node;
4851 /* Parse an (optional) nested-name-specifier.
4853 nested-name-specifier: [C++98]
4854 class-or-namespace-name :: nested-name-specifier [opt]
4855 class-or-namespace-name :: template nested-name-specifier [opt]
4857 nested-name-specifier: [C++0x]
4858 type-name ::
4859 namespace-name ::
4860 nested-name-specifier identifier ::
4861 nested-name-specifier template [opt] simple-template-id ::
4863 PARSER->SCOPE should be set appropriately before this function is
4864 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4865 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4866 in name lookups.
4868 Sets PARSER->SCOPE to the class (TYPE) or namespace
4869 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4870 it unchanged if there is no nested-name-specifier. Returns the new
4871 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4873 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4874 part of a declaration and/or decl-specifier. */
4876 static tree
4877 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4878 bool typename_keyword_p,
4879 bool check_dependency_p,
4880 bool type_p,
4881 bool is_declaration)
4883 bool success = false;
4884 cp_token_position start = 0;
4885 cp_token *token;
4887 /* Remember where the nested-name-specifier starts. */
4888 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4890 start = cp_lexer_token_position (parser->lexer, false);
4891 push_deferring_access_checks (dk_deferred);
4894 while (true)
4896 tree new_scope;
4897 tree old_scope;
4898 tree saved_qualifying_scope;
4899 bool template_keyword_p;
4901 /* Spot cases that cannot be the beginning of a
4902 nested-name-specifier. */
4903 token = cp_lexer_peek_token (parser->lexer);
4905 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4906 the already parsed nested-name-specifier. */
4907 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4909 /* Grab the nested-name-specifier and continue the loop. */
4910 cp_parser_pre_parsed_nested_name_specifier (parser);
4911 /* If we originally encountered this nested-name-specifier
4912 with IS_DECLARATION set to false, we will not have
4913 resolved TYPENAME_TYPEs, so we must do so here. */
4914 if (is_declaration
4915 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4917 new_scope = resolve_typename_type (parser->scope,
4918 /*only_current_p=*/false);
4919 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4920 parser->scope = new_scope;
4922 success = true;
4923 continue;
4926 /* Spot cases that cannot be the beginning of a
4927 nested-name-specifier. On the second and subsequent times
4928 through the loop, we look for the `template' keyword. */
4929 if (success && token->keyword == RID_TEMPLATE)
4931 /* A template-id can start a nested-name-specifier. */
4932 else if (token->type == CPP_TEMPLATE_ID)
4934 /* DR 743: decltype can be used in a nested-name-specifier. */
4935 else if (token_is_decltype (token))
4937 else
4939 /* If the next token is not an identifier, then it is
4940 definitely not a type-name or namespace-name. */
4941 if (token->type != CPP_NAME)
4942 break;
4943 /* If the following token is neither a `<' (to begin a
4944 template-id), nor a `::', then we are not looking at a
4945 nested-name-specifier. */
4946 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4948 if (token->type == CPP_COLON
4949 && parser->colon_corrects_to_scope_p
4950 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4952 error_at (token->location,
4953 "found %<:%> in nested-name-specifier, expected %<::%>");
4954 token->type = CPP_SCOPE;
4957 if (token->type != CPP_SCOPE
4958 && !cp_parser_nth_token_starts_template_argument_list_p
4959 (parser, 2))
4960 break;
4963 /* The nested-name-specifier is optional, so we parse
4964 tentatively. */
4965 cp_parser_parse_tentatively (parser);
4967 /* Look for the optional `template' keyword, if this isn't the
4968 first time through the loop. */
4969 if (success)
4970 template_keyword_p = cp_parser_optional_template_keyword (parser);
4971 else
4972 template_keyword_p = false;
4974 /* Save the old scope since the name lookup we are about to do
4975 might destroy it. */
4976 old_scope = parser->scope;
4977 saved_qualifying_scope = parser->qualifying_scope;
4978 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4979 look up names in "X<T>::I" in order to determine that "Y" is
4980 a template. So, if we have a typename at this point, we make
4981 an effort to look through it. */
4982 if (is_declaration
4983 && !typename_keyword_p
4984 && parser->scope
4985 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4986 parser->scope = resolve_typename_type (parser->scope,
4987 /*only_current_p=*/false);
4988 /* Parse the qualifying entity. */
4989 new_scope
4990 = cp_parser_qualifying_entity (parser,
4991 typename_keyword_p,
4992 template_keyword_p,
4993 check_dependency_p,
4994 type_p,
4995 is_declaration);
4996 /* Look for the `::' token. */
4997 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4999 /* If we found what we wanted, we keep going; otherwise, we're
5000 done. */
5001 if (!cp_parser_parse_definitely (parser))
5003 bool error_p = false;
5005 /* Restore the OLD_SCOPE since it was valid before the
5006 failed attempt at finding the last
5007 class-or-namespace-name. */
5008 parser->scope = old_scope;
5009 parser->qualifying_scope = saved_qualifying_scope;
5011 /* If the next token is a decltype, and the one after that is a
5012 `::', then the decltype has failed to resolve to a class or
5013 enumeration type. Give this error even when parsing
5014 tentatively since it can't possibly be valid--and we're going
5015 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5016 won't get another chance.*/
5017 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5018 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5019 == CPP_SCOPE))
5021 token = cp_lexer_consume_token (parser->lexer);
5022 error_at (token->location, "decltype evaluates to %qT, "
5023 "which is not a class or enumeration type",
5024 token->u.value);
5025 parser->scope = error_mark_node;
5026 error_p = true;
5027 /* As below. */
5028 success = true;
5029 cp_lexer_consume_token (parser->lexer);
5032 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5033 break;
5034 /* If the next token is an identifier, and the one after
5035 that is a `::', then any valid interpretation would have
5036 found a class-or-namespace-name. */
5037 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5038 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5039 == CPP_SCOPE)
5040 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5041 != CPP_COMPL))
5043 token = cp_lexer_consume_token (parser->lexer);
5044 if (!error_p)
5046 if (!token->ambiguous_p)
5048 tree decl;
5049 tree ambiguous_decls;
5051 decl = cp_parser_lookup_name (parser, token->u.value,
5052 none_type,
5053 /*is_template=*/false,
5054 /*is_namespace=*/false,
5055 /*check_dependency=*/true,
5056 &ambiguous_decls,
5057 token->location);
5058 if (TREE_CODE (decl) == TEMPLATE_DECL)
5059 error_at (token->location,
5060 "%qD used without template parameters",
5061 decl);
5062 else if (ambiguous_decls)
5064 error_at (token->location,
5065 "reference to %qD is ambiguous",
5066 token->u.value);
5067 print_candidates (ambiguous_decls);
5068 decl = error_mark_node;
5070 else
5072 if (cxx_dialect != cxx98)
5073 cp_parser_name_lookup_error
5074 (parser, token->u.value, decl, NLE_NOT_CXX98,
5075 token->location);
5076 else
5077 cp_parser_name_lookup_error
5078 (parser, token->u.value, decl, NLE_CXX98,
5079 token->location);
5082 parser->scope = error_mark_node;
5083 error_p = true;
5084 /* Treat this as a successful nested-name-specifier
5085 due to:
5087 [basic.lookup.qual]
5089 If the name found is not a class-name (clause
5090 _class_) or namespace-name (_namespace.def_), the
5091 program is ill-formed. */
5092 success = true;
5094 cp_lexer_consume_token (parser->lexer);
5096 break;
5098 /* We've found one valid nested-name-specifier. */
5099 success = true;
5100 /* Name lookup always gives us a DECL. */
5101 if (TREE_CODE (new_scope) == TYPE_DECL)
5102 new_scope = TREE_TYPE (new_scope);
5103 /* Uses of "template" must be followed by actual templates. */
5104 if (template_keyword_p
5105 && !(CLASS_TYPE_P (new_scope)
5106 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5107 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5108 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5109 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5110 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5111 == TEMPLATE_ID_EXPR)))
5112 permerror (input_location, TYPE_P (new_scope)
5113 ? G_("%qT is not a template")
5114 : G_("%qD is not a template"),
5115 new_scope);
5116 /* If it is a class scope, try to complete it; we are about to
5117 be looking up names inside the class. */
5118 if (TYPE_P (new_scope)
5119 /* Since checking types for dependency can be expensive,
5120 avoid doing it if the type is already complete. */
5121 && !COMPLETE_TYPE_P (new_scope)
5122 /* Do not try to complete dependent types. */
5123 && !dependent_type_p (new_scope))
5125 new_scope = complete_type (new_scope);
5126 /* If it is a typedef to current class, use the current
5127 class instead, as the typedef won't have any names inside
5128 it yet. */
5129 if (!COMPLETE_TYPE_P (new_scope)
5130 && currently_open_class (new_scope))
5131 new_scope = TYPE_MAIN_VARIANT (new_scope);
5133 /* Make sure we look in the right scope the next time through
5134 the loop. */
5135 parser->scope = new_scope;
5138 /* If parsing tentatively, replace the sequence of tokens that makes
5139 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5140 token. That way, should we re-parse the token stream, we will
5141 not have to repeat the effort required to do the parse, nor will
5142 we issue duplicate error messages. */
5143 if (success && start)
5145 cp_token *token;
5147 token = cp_lexer_token_at (parser->lexer, start);
5148 /* Reset the contents of the START token. */
5149 token->type = CPP_NESTED_NAME_SPECIFIER;
5150 /* Retrieve any deferred checks. Do not pop this access checks yet
5151 so the memory will not be reclaimed during token replacing below. */
5152 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5153 token->u.tree_check_value->value = parser->scope;
5154 token->u.tree_check_value->checks = get_deferred_access_checks ();
5155 token->u.tree_check_value->qualifying_scope =
5156 parser->qualifying_scope;
5157 token->keyword = RID_MAX;
5159 /* Purge all subsequent tokens. */
5160 cp_lexer_purge_tokens_after (parser->lexer, start);
5163 if (start)
5164 pop_to_parent_deferring_access_checks ();
5166 return success ? parser->scope : NULL_TREE;
5169 /* Parse a nested-name-specifier. See
5170 cp_parser_nested_name_specifier_opt for details. This function
5171 behaves identically, except that it will an issue an error if no
5172 nested-name-specifier is present. */
5174 static tree
5175 cp_parser_nested_name_specifier (cp_parser *parser,
5176 bool typename_keyword_p,
5177 bool check_dependency_p,
5178 bool type_p,
5179 bool is_declaration)
5181 tree scope;
5183 /* Look for the nested-name-specifier. */
5184 scope = cp_parser_nested_name_specifier_opt (parser,
5185 typename_keyword_p,
5186 check_dependency_p,
5187 type_p,
5188 is_declaration);
5189 /* If it was not present, issue an error message. */
5190 if (!scope)
5192 cp_parser_error (parser, "expected nested-name-specifier");
5193 parser->scope = NULL_TREE;
5196 return scope;
5199 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5200 this is either a class-name or a namespace-name (which corresponds
5201 to the class-or-namespace-name production in the grammar). For
5202 C++0x, it can also be a type-name that refers to an enumeration
5203 type or a simple-template-id.
5205 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5206 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5207 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5208 TYPE_P is TRUE iff the next name should be taken as a class-name,
5209 even the same name is declared to be another entity in the same
5210 scope.
5212 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5213 specified by the class-or-namespace-name. If neither is found the
5214 ERROR_MARK_NODE is returned. */
5216 static tree
5217 cp_parser_qualifying_entity (cp_parser *parser,
5218 bool typename_keyword_p,
5219 bool template_keyword_p,
5220 bool check_dependency_p,
5221 bool type_p,
5222 bool is_declaration)
5224 tree saved_scope;
5225 tree saved_qualifying_scope;
5226 tree saved_object_scope;
5227 tree scope;
5228 bool only_class_p;
5229 bool successful_parse_p;
5231 /* DR 743: decltype can appear in a nested-name-specifier. */
5232 if (cp_lexer_next_token_is_decltype (parser->lexer))
5234 scope = cp_parser_decltype (parser);
5235 if (TREE_CODE (scope) != ENUMERAL_TYPE
5236 && !MAYBE_CLASS_TYPE_P (scope))
5238 cp_parser_simulate_error (parser);
5239 return error_mark_node;
5241 if (TYPE_NAME (scope))
5242 scope = TYPE_NAME (scope);
5243 return scope;
5246 /* Before we try to parse the class-name, we must save away the
5247 current PARSER->SCOPE since cp_parser_class_name will destroy
5248 it. */
5249 saved_scope = parser->scope;
5250 saved_qualifying_scope = parser->qualifying_scope;
5251 saved_object_scope = parser->object_scope;
5252 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5253 there is no need to look for a namespace-name. */
5254 only_class_p = template_keyword_p
5255 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5256 if (!only_class_p)
5257 cp_parser_parse_tentatively (parser);
5258 scope = cp_parser_class_name (parser,
5259 typename_keyword_p,
5260 template_keyword_p,
5261 type_p ? class_type : none_type,
5262 check_dependency_p,
5263 /*class_head_p=*/false,
5264 is_declaration);
5265 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5266 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5267 if (!only_class_p
5268 && cxx_dialect != cxx98
5269 && !successful_parse_p)
5271 /* Restore the saved scope. */
5272 parser->scope = saved_scope;
5273 parser->qualifying_scope = saved_qualifying_scope;
5274 parser->object_scope = saved_object_scope;
5276 /* Parse tentatively. */
5277 cp_parser_parse_tentatively (parser);
5279 /* Parse a type-name */
5280 scope = cp_parser_type_name (parser);
5282 /* "If the name found does not designate a namespace or a class,
5283 enumeration, or dependent type, the program is ill-formed."
5285 We cover classes and dependent types above and namespaces below,
5286 so this code is only looking for enums. */
5287 if (!scope || TREE_CODE (scope) != TYPE_DECL
5288 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5289 cp_parser_simulate_error (parser);
5291 successful_parse_p = cp_parser_parse_definitely (parser);
5293 /* If that didn't work, try for a namespace-name. */
5294 if (!only_class_p && !successful_parse_p)
5296 /* Restore the saved scope. */
5297 parser->scope = saved_scope;
5298 parser->qualifying_scope = saved_qualifying_scope;
5299 parser->object_scope = saved_object_scope;
5300 /* If we are not looking at an identifier followed by the scope
5301 resolution operator, then this is not part of a
5302 nested-name-specifier. (Note that this function is only used
5303 to parse the components of a nested-name-specifier.) */
5304 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5305 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5306 return error_mark_node;
5307 scope = cp_parser_namespace_name (parser);
5310 return scope;
5313 /* Parse a postfix-expression.
5315 postfix-expression:
5316 primary-expression
5317 postfix-expression [ expression ]
5318 postfix-expression ( expression-list [opt] )
5319 simple-type-specifier ( expression-list [opt] )
5320 typename :: [opt] nested-name-specifier identifier
5321 ( expression-list [opt] )
5322 typename :: [opt] nested-name-specifier template [opt] template-id
5323 ( expression-list [opt] )
5324 postfix-expression . template [opt] id-expression
5325 postfix-expression -> template [opt] id-expression
5326 postfix-expression . pseudo-destructor-name
5327 postfix-expression -> pseudo-destructor-name
5328 postfix-expression ++
5329 postfix-expression --
5330 dynamic_cast < type-id > ( expression )
5331 static_cast < type-id > ( expression )
5332 reinterpret_cast < type-id > ( expression )
5333 const_cast < type-id > ( expression )
5334 typeid ( expression )
5335 typeid ( type-id )
5337 GNU Extension:
5339 postfix-expression:
5340 ( type-id ) { initializer-list , [opt] }
5342 This extension is a GNU version of the C99 compound-literal
5343 construct. (The C99 grammar uses `type-name' instead of `type-id',
5344 but they are essentially the same concept.)
5346 If ADDRESS_P is true, the postfix expression is the operand of the
5347 `&' operator. CAST_P is true if this expression is the target of a
5348 cast.
5350 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5351 class member access expressions [expr.ref].
5353 Returns a representation of the expression. */
5355 static tree
5356 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5357 bool member_access_only_p,
5358 cp_id_kind * pidk_return)
5360 cp_token *token;
5361 enum rid keyword;
5362 cp_id_kind idk = CP_ID_KIND_NONE;
5363 tree postfix_expression = NULL_TREE;
5364 bool is_member_access = false;
5366 /* Peek at the next token. */
5367 token = cp_lexer_peek_token (parser->lexer);
5368 /* Some of the productions are determined by keywords. */
5369 keyword = token->keyword;
5370 switch (keyword)
5372 case RID_DYNCAST:
5373 case RID_STATCAST:
5374 case RID_REINTCAST:
5375 case RID_CONSTCAST:
5377 tree type;
5378 tree expression;
5379 const char *saved_message;
5381 /* All of these can be handled in the same way from the point
5382 of view of parsing. Begin by consuming the token
5383 identifying the cast. */
5384 cp_lexer_consume_token (parser->lexer);
5386 /* New types cannot be defined in the cast. */
5387 saved_message = parser->type_definition_forbidden_message;
5388 parser->type_definition_forbidden_message
5389 = G_("types may not be defined in casts");
5391 /* Look for the opening `<'. */
5392 cp_parser_require (parser, CPP_LESS, RT_LESS);
5393 /* Parse the type to which we are casting. */
5394 type = cp_parser_type_id (parser);
5395 /* Look for the closing `>'. */
5396 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5397 /* Restore the old message. */
5398 parser->type_definition_forbidden_message = saved_message;
5400 /* And the expression which is being cast. */
5401 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5402 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5403 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5405 /* Only type conversions to integral or enumeration types
5406 can be used in constant-expressions. */
5407 if (!cast_valid_in_integral_constant_expression_p (type)
5408 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5409 return error_mark_node;
5411 switch (keyword)
5413 case RID_DYNCAST:
5414 postfix_expression
5415 = build_dynamic_cast (type, expression, tf_warning_or_error);
5416 break;
5417 case RID_STATCAST:
5418 postfix_expression
5419 = build_static_cast (type, expression, tf_warning_or_error);
5420 break;
5421 case RID_REINTCAST:
5422 postfix_expression
5423 = build_reinterpret_cast (type, expression,
5424 tf_warning_or_error);
5425 break;
5426 case RID_CONSTCAST:
5427 postfix_expression
5428 = build_const_cast (type, expression, tf_warning_or_error);
5429 break;
5430 default:
5431 gcc_unreachable ();
5434 break;
5436 case RID_TYPEID:
5438 tree type;
5439 const char *saved_message;
5440 bool saved_in_type_id_in_expr_p;
5442 /* Consume the `typeid' token. */
5443 cp_lexer_consume_token (parser->lexer);
5444 /* Look for the `(' token. */
5445 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5446 /* Types cannot be defined in a `typeid' expression. */
5447 saved_message = parser->type_definition_forbidden_message;
5448 parser->type_definition_forbidden_message
5449 = G_("types may not be defined in a %<typeid%> expression");
5450 /* We can't be sure yet whether we're looking at a type-id or an
5451 expression. */
5452 cp_parser_parse_tentatively (parser);
5453 /* Try a type-id first. */
5454 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5455 parser->in_type_id_in_expr_p = true;
5456 type = cp_parser_type_id (parser);
5457 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5458 /* Look for the `)' token. Otherwise, we can't be sure that
5459 we're not looking at an expression: consider `typeid (int
5460 (3))', for example. */
5461 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5462 /* If all went well, simply lookup the type-id. */
5463 if (cp_parser_parse_definitely (parser))
5464 postfix_expression = get_typeid (type);
5465 /* Otherwise, fall back to the expression variant. */
5466 else
5468 tree expression;
5470 /* Look for an expression. */
5471 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5472 /* Compute its typeid. */
5473 postfix_expression = build_typeid (expression);
5474 /* Look for the `)' token. */
5475 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5477 /* Restore the saved message. */
5478 parser->type_definition_forbidden_message = saved_message;
5479 /* `typeid' may not appear in an integral constant expression. */
5480 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5481 return error_mark_node;
5483 break;
5485 case RID_TYPENAME:
5487 tree type;
5488 /* The syntax permitted here is the same permitted for an
5489 elaborated-type-specifier. */
5490 type = cp_parser_elaborated_type_specifier (parser,
5491 /*is_friend=*/false,
5492 /*is_declaration=*/false);
5493 postfix_expression = cp_parser_functional_cast (parser, type);
5495 break;
5497 case RID_BUILTIN_SHUFFLE:
5499 VEC(tree,gc)* vec;
5500 unsigned int i;
5501 tree p;
5502 location_t loc = token->location;
5504 cp_lexer_consume_token (parser->lexer);
5505 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5506 /*cast_p=*/false, /*allow_expansion_p=*/true,
5507 /*non_constant_p=*/NULL);
5508 if (vec == NULL)
5509 return error_mark_node;
5511 FOR_EACH_VEC_ELT (tree, vec, i, p)
5512 mark_exp_read (p);
5514 if (VEC_length (tree, vec) == 2)
5515 return
5516 c_build_vec_perm_expr
5517 (loc, VEC_index (tree, vec, 0),
5518 NULL_TREE, VEC_index (tree, vec, 1));
5520 else if (VEC_length (tree, vec) == 3)
5521 return
5522 c_build_vec_perm_expr
5523 (loc, VEC_index (tree, vec, 0),
5524 VEC_index (tree, vec, 1),
5525 VEC_index (tree, vec, 2));
5526 else
5528 error_at (loc, "wrong number of arguments to "
5529 "%<__builtin_shuffle%>");
5530 return error_mark_node;
5532 break;
5535 default:
5537 tree type;
5539 /* If the next thing is a simple-type-specifier, we may be
5540 looking at a functional cast. We could also be looking at
5541 an id-expression. So, we try the functional cast, and if
5542 that doesn't work we fall back to the primary-expression. */
5543 cp_parser_parse_tentatively (parser);
5544 /* Look for the simple-type-specifier. */
5545 type = cp_parser_simple_type_specifier (parser,
5546 /*decl_specs=*/NULL,
5547 CP_PARSER_FLAGS_NONE);
5548 /* Parse the cast itself. */
5549 if (!cp_parser_error_occurred (parser))
5550 postfix_expression
5551 = cp_parser_functional_cast (parser, type);
5552 /* If that worked, we're done. */
5553 if (cp_parser_parse_definitely (parser))
5554 break;
5556 /* If the functional-cast didn't work out, try a
5557 compound-literal. */
5558 if (cp_parser_allow_gnu_extensions_p (parser)
5559 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5561 VEC(constructor_elt,gc) *initializer_list = NULL;
5562 bool saved_in_type_id_in_expr_p;
5564 cp_parser_parse_tentatively (parser);
5565 /* Consume the `('. */
5566 cp_lexer_consume_token (parser->lexer);
5567 /* Parse the type. */
5568 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5569 parser->in_type_id_in_expr_p = true;
5570 type = cp_parser_type_id (parser);
5571 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5572 /* Look for the `)'. */
5573 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5574 /* Look for the `{'. */
5575 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5576 /* If things aren't going well, there's no need to
5577 keep going. */
5578 if (!cp_parser_error_occurred (parser))
5580 bool non_constant_p;
5581 /* Parse the initializer-list. */
5582 initializer_list
5583 = cp_parser_initializer_list (parser, &non_constant_p);
5584 /* Allow a trailing `,'. */
5585 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5586 cp_lexer_consume_token (parser->lexer);
5587 /* Look for the final `}'. */
5588 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5590 /* If that worked, we're definitely looking at a
5591 compound-literal expression. */
5592 if (cp_parser_parse_definitely (parser))
5594 /* Warn the user that a compound literal is not
5595 allowed in standard C++. */
5596 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5597 /* For simplicity, we disallow compound literals in
5598 constant-expressions. We could
5599 allow compound literals of integer type, whose
5600 initializer was a constant, in constant
5601 expressions. Permitting that usage, as a further
5602 extension, would not change the meaning of any
5603 currently accepted programs. (Of course, as
5604 compound literals are not part of ISO C++, the
5605 standard has nothing to say.) */
5606 if (cp_parser_non_integral_constant_expression (parser,
5607 NIC_NCC))
5609 postfix_expression = error_mark_node;
5610 break;
5612 /* Form the representation of the compound-literal. */
5613 postfix_expression
5614 = (finish_compound_literal
5615 (type, build_constructor (init_list_type_node,
5616 initializer_list),
5617 tf_warning_or_error));
5618 break;
5622 /* It must be a primary-expression. */
5623 postfix_expression
5624 = cp_parser_primary_expression (parser, address_p, cast_p,
5625 /*template_arg_p=*/false,
5626 &idk);
5628 break;
5631 /* Keep looping until the postfix-expression is complete. */
5632 while (true)
5634 if (idk == CP_ID_KIND_UNQUALIFIED
5635 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5636 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5637 /* It is not a Koenig lookup function call. */
5638 postfix_expression
5639 = unqualified_name_lookup_error (postfix_expression);
5641 /* Peek at the next token. */
5642 token = cp_lexer_peek_token (parser->lexer);
5644 switch (token->type)
5646 case CPP_OPEN_SQUARE:
5647 if (cp_next_tokens_can_be_std_attribute_p (parser))
5649 cp_parser_error (parser,
5650 "two consecutive %<[%> shall "
5651 "only introduce an attribute");
5652 return error_mark_node;
5654 postfix_expression
5655 = cp_parser_postfix_open_square_expression (parser,
5656 postfix_expression,
5657 false);
5658 idk = CP_ID_KIND_NONE;
5659 is_member_access = false;
5660 break;
5662 case CPP_OPEN_PAREN:
5663 /* postfix-expression ( expression-list [opt] ) */
5665 bool koenig_p;
5666 bool is_builtin_constant_p;
5667 bool saved_integral_constant_expression_p = false;
5668 bool saved_non_integral_constant_expression_p = false;
5669 VEC(tree,gc) *args;
5671 is_member_access = false;
5673 is_builtin_constant_p
5674 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5675 if (is_builtin_constant_p)
5677 /* The whole point of __builtin_constant_p is to allow
5678 non-constant expressions to appear as arguments. */
5679 saved_integral_constant_expression_p
5680 = parser->integral_constant_expression_p;
5681 saved_non_integral_constant_expression_p
5682 = parser->non_integral_constant_expression_p;
5683 parser->integral_constant_expression_p = false;
5685 args = (cp_parser_parenthesized_expression_list
5686 (parser, non_attr,
5687 /*cast_p=*/false, /*allow_expansion_p=*/true,
5688 /*non_constant_p=*/NULL));
5689 if (is_builtin_constant_p)
5691 parser->integral_constant_expression_p
5692 = saved_integral_constant_expression_p;
5693 parser->non_integral_constant_expression_p
5694 = saved_non_integral_constant_expression_p;
5697 if (args == NULL)
5699 postfix_expression = error_mark_node;
5700 break;
5703 /* Function calls are not permitted in
5704 constant-expressions. */
5705 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5706 && cp_parser_non_integral_constant_expression (parser,
5707 NIC_FUNC_CALL))
5709 postfix_expression = error_mark_node;
5710 release_tree_vector (args);
5711 break;
5714 koenig_p = false;
5715 if (idk == CP_ID_KIND_UNQUALIFIED
5716 || idk == CP_ID_KIND_TEMPLATE_ID)
5718 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5720 if (!VEC_empty (tree, args))
5722 koenig_p = true;
5723 if (!any_type_dependent_arguments_p (args))
5724 postfix_expression
5725 = perform_koenig_lookup (postfix_expression, args,
5726 /*include_std=*/false,
5727 tf_warning_or_error);
5729 else
5730 postfix_expression
5731 = unqualified_fn_lookup_error (postfix_expression);
5733 /* We do not perform argument-dependent lookup if
5734 normal lookup finds a non-function, in accordance
5735 with the expected resolution of DR 218. */
5736 else if (!VEC_empty (tree, args)
5737 && is_overloaded_fn (postfix_expression))
5739 tree fn = get_first_fn (postfix_expression);
5740 fn = STRIP_TEMPLATE (fn);
5742 /* Do not do argument dependent lookup if regular
5743 lookup finds a member function or a block-scope
5744 function declaration. [basic.lookup.argdep]/3 */
5745 if (!DECL_FUNCTION_MEMBER_P (fn)
5746 && !DECL_LOCAL_FUNCTION_P (fn))
5748 koenig_p = true;
5749 if (!any_type_dependent_arguments_p (args))
5750 postfix_expression
5751 = perform_koenig_lookup (postfix_expression, args,
5752 /*include_std=*/false,
5753 tf_warning_or_error);
5758 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5760 tree instance = TREE_OPERAND (postfix_expression, 0);
5761 tree fn = TREE_OPERAND (postfix_expression, 1);
5763 if (processing_template_decl
5764 && (type_dependent_expression_p (instance)
5765 || (!BASELINK_P (fn)
5766 && TREE_CODE (fn) != FIELD_DECL)
5767 || type_dependent_expression_p (fn)
5768 || any_type_dependent_arguments_p (args)))
5770 postfix_expression
5771 = build_nt_call_vec (postfix_expression, args);
5772 release_tree_vector (args);
5773 break;
5776 if (BASELINK_P (fn))
5778 postfix_expression
5779 = (build_new_method_call
5780 (instance, fn, &args, NULL_TREE,
5781 (idk == CP_ID_KIND_QUALIFIED
5782 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5783 : LOOKUP_NORMAL),
5784 /*fn_p=*/NULL,
5785 tf_warning_or_error));
5787 else
5788 postfix_expression
5789 = finish_call_expr (postfix_expression, &args,
5790 /*disallow_virtual=*/false,
5791 /*koenig_p=*/false,
5792 tf_warning_or_error);
5794 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5795 || TREE_CODE (postfix_expression) == MEMBER_REF
5796 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5797 postfix_expression = (build_offset_ref_call_from_tree
5798 (postfix_expression, &args,
5799 tf_warning_or_error));
5800 else if (idk == CP_ID_KIND_QUALIFIED)
5801 /* A call to a static class member, or a namespace-scope
5802 function. */
5803 postfix_expression
5804 = finish_call_expr (postfix_expression, &args,
5805 /*disallow_virtual=*/true,
5806 koenig_p,
5807 tf_warning_or_error);
5808 else
5809 /* All other function calls. */
5810 postfix_expression
5811 = finish_call_expr (postfix_expression, &args,
5812 /*disallow_virtual=*/false,
5813 koenig_p,
5814 tf_warning_or_error);
5816 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5817 idk = CP_ID_KIND_NONE;
5819 release_tree_vector (args);
5821 break;
5823 case CPP_DOT:
5824 case CPP_DEREF:
5825 /* postfix-expression . template [opt] id-expression
5826 postfix-expression . pseudo-destructor-name
5827 postfix-expression -> template [opt] id-expression
5828 postfix-expression -> pseudo-destructor-name */
5830 /* Consume the `.' or `->' operator. */
5831 cp_lexer_consume_token (parser->lexer);
5833 postfix_expression
5834 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5835 postfix_expression,
5836 false, &idk,
5837 token->location);
5839 is_member_access = true;
5840 break;
5842 case CPP_PLUS_PLUS:
5843 /* postfix-expression ++ */
5844 /* Consume the `++' token. */
5845 cp_lexer_consume_token (parser->lexer);
5846 /* Generate a representation for the complete expression. */
5847 postfix_expression
5848 = finish_increment_expr (postfix_expression,
5849 POSTINCREMENT_EXPR);
5850 /* Increments may not appear in constant-expressions. */
5851 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5852 postfix_expression = error_mark_node;
5853 idk = CP_ID_KIND_NONE;
5854 is_member_access = false;
5855 break;
5857 case CPP_MINUS_MINUS:
5858 /* postfix-expression -- */
5859 /* Consume the `--' token. */
5860 cp_lexer_consume_token (parser->lexer);
5861 /* Generate a representation for the complete expression. */
5862 postfix_expression
5863 = finish_increment_expr (postfix_expression,
5864 POSTDECREMENT_EXPR);
5865 /* Decrements may not appear in constant-expressions. */
5866 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5867 postfix_expression = error_mark_node;
5868 idk = CP_ID_KIND_NONE;
5869 is_member_access = false;
5870 break;
5872 default:
5873 if (pidk_return != NULL)
5874 * pidk_return = idk;
5875 if (member_access_only_p)
5876 return is_member_access? postfix_expression : error_mark_node;
5877 else
5878 return postfix_expression;
5882 /* We should never get here. */
5883 gcc_unreachable ();
5884 return error_mark_node;
5887 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5888 by cp_parser_builtin_offsetof. We're looking for
5890 postfix-expression [ expression ]
5891 postfix-expression [ braced-init-list ] (C++11)
5893 FOR_OFFSETOF is set if we're being called in that context, which
5894 changes how we deal with integer constant expressions. */
5896 static tree
5897 cp_parser_postfix_open_square_expression (cp_parser *parser,
5898 tree postfix_expression,
5899 bool for_offsetof)
5901 tree index;
5902 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5904 /* Consume the `[' token. */
5905 cp_lexer_consume_token (parser->lexer);
5907 /* Parse the index expression. */
5908 /* ??? For offsetof, there is a question of what to allow here. If
5909 offsetof is not being used in an integral constant expression context,
5910 then we *could* get the right answer by computing the value at runtime.
5911 If we are in an integral constant expression context, then we might
5912 could accept any constant expression; hard to say without analysis.
5913 Rather than open the barn door too wide right away, allow only integer
5914 constant expressions here. */
5915 if (for_offsetof)
5916 index = cp_parser_constant_expression (parser, false, NULL);
5917 else
5919 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5921 bool expr_nonconst_p;
5922 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5923 index = cp_parser_braced_list (parser, &expr_nonconst_p);
5925 else
5926 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5929 /* Look for the closing `]'. */
5930 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5932 /* Build the ARRAY_REF. */
5933 postfix_expression = grok_array_decl (loc, postfix_expression, index);
5935 /* When not doing offsetof, array references are not permitted in
5936 constant-expressions. */
5937 if (!for_offsetof
5938 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5939 postfix_expression = error_mark_node;
5941 return postfix_expression;
5944 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5945 by cp_parser_builtin_offsetof. We're looking for
5947 postfix-expression . template [opt] id-expression
5948 postfix-expression . pseudo-destructor-name
5949 postfix-expression -> template [opt] id-expression
5950 postfix-expression -> pseudo-destructor-name
5952 FOR_OFFSETOF is set if we're being called in that context. That sorta
5953 limits what of the above we'll actually accept, but nevermind.
5954 TOKEN_TYPE is the "." or "->" token, which will already have been
5955 removed from the stream. */
5957 static tree
5958 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5959 enum cpp_ttype token_type,
5960 tree postfix_expression,
5961 bool for_offsetof, cp_id_kind *idk,
5962 location_t location)
5964 tree name;
5965 bool dependent_p;
5966 bool pseudo_destructor_p;
5967 tree scope = NULL_TREE;
5969 /* If this is a `->' operator, dereference the pointer. */
5970 if (token_type == CPP_DEREF)
5971 postfix_expression = build_x_arrow (location, postfix_expression,
5972 tf_warning_or_error);
5973 /* Check to see whether or not the expression is type-dependent. */
5974 dependent_p = type_dependent_expression_p (postfix_expression);
5975 /* The identifier following the `->' or `.' is not qualified. */
5976 parser->scope = NULL_TREE;
5977 parser->qualifying_scope = NULL_TREE;
5978 parser->object_scope = NULL_TREE;
5979 *idk = CP_ID_KIND_NONE;
5981 /* Enter the scope corresponding to the type of the object
5982 given by the POSTFIX_EXPRESSION. */
5983 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5985 scope = TREE_TYPE (postfix_expression);
5986 /* According to the standard, no expression should ever have
5987 reference type. Unfortunately, we do not currently match
5988 the standard in this respect in that our internal representation
5989 of an expression may have reference type even when the standard
5990 says it does not. Therefore, we have to manually obtain the
5991 underlying type here. */
5992 scope = non_reference (scope);
5993 /* The type of the POSTFIX_EXPRESSION must be complete. */
5994 if (scope == unknown_type_node)
5996 error_at (location, "%qE does not have class type",
5997 postfix_expression);
5998 scope = NULL_TREE;
6000 /* Unlike the object expression in other contexts, *this is not
6001 required to be of complete type for purposes of class member
6002 access (5.2.5) outside the member function body. */
6003 else if (scope != current_class_ref
6004 && !(processing_template_decl && scope == current_class_type))
6005 scope = complete_type_or_else (scope, NULL_TREE);
6006 /* Let the name lookup machinery know that we are processing a
6007 class member access expression. */
6008 parser->context->object_type = scope;
6009 /* If something went wrong, we want to be able to discern that case,
6010 as opposed to the case where there was no SCOPE due to the type
6011 of expression being dependent. */
6012 if (!scope)
6013 scope = error_mark_node;
6014 /* If the SCOPE was erroneous, make the various semantic analysis
6015 functions exit quickly -- and without issuing additional error
6016 messages. */
6017 if (scope == error_mark_node)
6018 postfix_expression = error_mark_node;
6021 /* Assume this expression is not a pseudo-destructor access. */
6022 pseudo_destructor_p = false;
6024 /* If the SCOPE is a scalar type, then, if this is a valid program,
6025 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6026 is type dependent, it can be pseudo-destructor-name or something else.
6027 Try to parse it as pseudo-destructor-name first. */
6028 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6030 tree s;
6031 tree type;
6033 cp_parser_parse_tentatively (parser);
6034 /* Parse the pseudo-destructor-name. */
6035 s = NULL_TREE;
6036 cp_parser_pseudo_destructor_name (parser, &s, &type);
6037 if (dependent_p
6038 && (cp_parser_error_occurred (parser)
6039 || TREE_CODE (type) != TYPE_DECL
6040 || !SCALAR_TYPE_P (TREE_TYPE (type))))
6041 cp_parser_abort_tentative_parse (parser);
6042 else if (cp_parser_parse_definitely (parser))
6044 pseudo_destructor_p = true;
6045 postfix_expression
6046 = finish_pseudo_destructor_expr (postfix_expression,
6047 s, TREE_TYPE (type));
6051 if (!pseudo_destructor_p)
6053 /* If the SCOPE is not a scalar type, we are looking at an
6054 ordinary class member access expression, rather than a
6055 pseudo-destructor-name. */
6056 bool template_p;
6057 cp_token *token = cp_lexer_peek_token (parser->lexer);
6058 /* Parse the id-expression. */
6059 name = (cp_parser_id_expression
6060 (parser,
6061 cp_parser_optional_template_keyword (parser),
6062 /*check_dependency_p=*/true,
6063 &template_p,
6064 /*declarator_p=*/false,
6065 /*optional_p=*/false));
6066 /* In general, build a SCOPE_REF if the member name is qualified.
6067 However, if the name was not dependent and has already been
6068 resolved; there is no need to build the SCOPE_REF. For example;
6070 struct X { void f(); };
6071 template <typename T> void f(T* t) { t->X::f(); }
6073 Even though "t" is dependent, "X::f" is not and has been resolved
6074 to a BASELINK; there is no need to include scope information. */
6076 /* But we do need to remember that there was an explicit scope for
6077 virtual function calls. */
6078 if (parser->scope)
6079 *idk = CP_ID_KIND_QUALIFIED;
6081 /* If the name is a template-id that names a type, we will get a
6082 TYPE_DECL here. That is invalid code. */
6083 if (TREE_CODE (name) == TYPE_DECL)
6085 error_at (token->location, "invalid use of %qD", name);
6086 postfix_expression = error_mark_node;
6088 else
6090 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6092 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6094 error_at (token->location, "%<%D::%D%> is not a class member",
6095 parser->scope, name);
6096 postfix_expression = error_mark_node;
6098 else
6099 name = build_qualified_name (/*type=*/NULL_TREE,
6100 parser->scope,
6101 name,
6102 template_p);
6103 parser->scope = NULL_TREE;
6104 parser->qualifying_scope = NULL_TREE;
6105 parser->object_scope = NULL_TREE;
6107 if (parser->scope && name && BASELINK_P (name))
6108 adjust_result_of_qualified_name_lookup
6109 (name, parser->scope, scope);
6110 postfix_expression
6111 = finish_class_member_access_expr (postfix_expression, name,
6112 template_p,
6113 tf_warning_or_error);
6117 /* We no longer need to look up names in the scope of the object on
6118 the left-hand side of the `.' or `->' operator. */
6119 parser->context->object_type = NULL_TREE;
6121 /* Outside of offsetof, these operators may not appear in
6122 constant-expressions. */
6123 if (!for_offsetof
6124 && (cp_parser_non_integral_constant_expression
6125 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6126 postfix_expression = error_mark_node;
6128 return postfix_expression;
6131 /* Parse a parenthesized expression-list.
6133 expression-list:
6134 assignment-expression
6135 expression-list, assignment-expression
6137 attribute-list:
6138 expression-list
6139 identifier
6140 identifier, expression-list
6142 CAST_P is true if this expression is the target of a cast.
6144 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6145 argument pack.
6147 Returns a vector of trees. Each element is a representation of an
6148 assignment-expression. NULL is returned if the ( and or ) are
6149 missing. An empty, but allocated, vector is returned on no
6150 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6151 if we are parsing an attribute list for an attribute that wants a
6152 plain identifier argument, normal_attr for an attribute that wants
6153 an expression, or non_attr if we aren't parsing an attribute list. If
6154 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6155 not all of the expressions in the list were constant. */
6157 static VEC(tree,gc) *
6158 cp_parser_parenthesized_expression_list (cp_parser* parser,
6159 int is_attribute_list,
6160 bool cast_p,
6161 bool allow_expansion_p,
6162 bool *non_constant_p)
6164 VEC(tree,gc) *expression_list;
6165 bool fold_expr_p = is_attribute_list != non_attr;
6166 tree identifier = NULL_TREE;
6167 bool saved_greater_than_is_operator_p;
6169 /* Assume all the expressions will be constant. */
6170 if (non_constant_p)
6171 *non_constant_p = false;
6173 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6174 return NULL;
6176 expression_list = make_tree_vector ();
6178 /* Within a parenthesized expression, a `>' token is always
6179 the greater-than operator. */
6180 saved_greater_than_is_operator_p
6181 = parser->greater_than_is_operator_p;
6182 parser->greater_than_is_operator_p = true;
6184 /* Consume expressions until there are no more. */
6185 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6186 while (true)
6188 tree expr;
6190 /* At the beginning of attribute lists, check to see if the
6191 next token is an identifier. */
6192 if (is_attribute_list == id_attr
6193 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6195 cp_token *token;
6197 /* Consume the identifier. */
6198 token = cp_lexer_consume_token (parser->lexer);
6199 /* Save the identifier. */
6200 identifier = token->u.value;
6202 else
6204 bool expr_non_constant_p;
6206 /* Parse the next assignment-expression. */
6207 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6209 /* A braced-init-list. */
6210 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6211 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6212 if (non_constant_p && expr_non_constant_p)
6213 *non_constant_p = true;
6215 else if (non_constant_p)
6217 expr = (cp_parser_constant_expression
6218 (parser, /*allow_non_constant_p=*/true,
6219 &expr_non_constant_p));
6220 if (expr_non_constant_p)
6221 *non_constant_p = true;
6223 else
6224 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6226 if (fold_expr_p)
6227 expr = fold_non_dependent_expr (expr);
6229 /* If we have an ellipsis, then this is an expression
6230 expansion. */
6231 if (allow_expansion_p
6232 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6234 /* Consume the `...'. */
6235 cp_lexer_consume_token (parser->lexer);
6237 /* Build the argument pack. */
6238 expr = make_pack_expansion (expr);
6241 /* Add it to the list. We add error_mark_node
6242 expressions to the list, so that we can still tell if
6243 the correct form for a parenthesized expression-list
6244 is found. That gives better errors. */
6245 VEC_safe_push (tree, gc, expression_list, expr);
6247 if (expr == error_mark_node)
6248 goto skip_comma;
6251 /* After the first item, attribute lists look the same as
6252 expression lists. */
6253 is_attribute_list = non_attr;
6255 get_comma:;
6256 /* If the next token isn't a `,', then we are done. */
6257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6258 break;
6260 /* Otherwise, consume the `,' and keep going. */
6261 cp_lexer_consume_token (parser->lexer);
6264 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6266 int ending;
6268 skip_comma:;
6269 /* We try and resync to an unnested comma, as that will give the
6270 user better diagnostics. */
6271 ending = cp_parser_skip_to_closing_parenthesis (parser,
6272 /*recovering=*/true,
6273 /*or_comma=*/true,
6274 /*consume_paren=*/true);
6275 if (ending < 0)
6276 goto get_comma;
6277 if (!ending)
6279 parser->greater_than_is_operator_p
6280 = saved_greater_than_is_operator_p;
6281 return NULL;
6285 parser->greater_than_is_operator_p
6286 = saved_greater_than_is_operator_p;
6288 if (identifier)
6289 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6291 return expression_list;
6294 /* Parse a pseudo-destructor-name.
6296 pseudo-destructor-name:
6297 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6298 :: [opt] nested-name-specifier template template-id :: ~ type-name
6299 :: [opt] nested-name-specifier [opt] ~ type-name
6301 If either of the first two productions is used, sets *SCOPE to the
6302 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6303 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6304 or ERROR_MARK_NODE if the parse fails. */
6306 static void
6307 cp_parser_pseudo_destructor_name (cp_parser* parser,
6308 tree* scope,
6309 tree* type)
6311 bool nested_name_specifier_p;
6313 /* Assume that things will not work out. */
6314 *type = error_mark_node;
6316 /* Look for the optional `::' operator. */
6317 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6318 /* Look for the optional nested-name-specifier. */
6319 nested_name_specifier_p
6320 = (cp_parser_nested_name_specifier_opt (parser,
6321 /*typename_keyword_p=*/false,
6322 /*check_dependency_p=*/true,
6323 /*type_p=*/false,
6324 /*is_declaration=*/false)
6325 != NULL_TREE);
6326 /* Now, if we saw a nested-name-specifier, we might be doing the
6327 second production. */
6328 if (nested_name_specifier_p
6329 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6331 /* Consume the `template' keyword. */
6332 cp_lexer_consume_token (parser->lexer);
6333 /* Parse the template-id. */
6334 cp_parser_template_id (parser,
6335 /*template_keyword_p=*/true,
6336 /*check_dependency_p=*/false,
6337 class_type,
6338 /*is_declaration=*/true);
6339 /* Look for the `::' token. */
6340 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6342 /* If the next token is not a `~', then there might be some
6343 additional qualification. */
6344 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6346 /* At this point, we're looking for "type-name :: ~". The type-name
6347 must not be a class-name, since this is a pseudo-destructor. So,
6348 it must be either an enum-name, or a typedef-name -- both of which
6349 are just identifiers. So, we peek ahead to check that the "::"
6350 and "~" tokens are present; if they are not, then we can avoid
6351 calling type_name. */
6352 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6353 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6354 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6356 cp_parser_error (parser, "non-scalar type");
6357 return;
6360 /* Look for the type-name. */
6361 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6362 if (*scope == error_mark_node)
6363 return;
6365 /* Look for the `::' token. */
6366 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6368 else
6369 *scope = NULL_TREE;
6371 /* Look for the `~'. */
6372 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6374 /* Once we see the ~, this has to be a pseudo-destructor. */
6375 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6376 cp_parser_commit_to_tentative_parse (parser);
6378 /* Look for the type-name again. We are not responsible for
6379 checking that it matches the first type-name. */
6380 *type = cp_parser_nonclass_name (parser);
6383 /* Parse a unary-expression.
6385 unary-expression:
6386 postfix-expression
6387 ++ cast-expression
6388 -- cast-expression
6389 unary-operator cast-expression
6390 sizeof unary-expression
6391 sizeof ( type-id )
6392 alignof ( type-id ) [C++0x]
6393 new-expression
6394 delete-expression
6396 GNU Extensions:
6398 unary-expression:
6399 __extension__ cast-expression
6400 __alignof__ unary-expression
6401 __alignof__ ( type-id )
6402 alignof unary-expression [C++0x]
6403 __real__ cast-expression
6404 __imag__ cast-expression
6405 && identifier
6407 ADDRESS_P is true iff the unary-expression is appearing as the
6408 operand of the `&' operator. CAST_P is true if this expression is
6409 the target of a cast.
6411 Returns a representation of the expression. */
6413 static tree
6414 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6415 cp_id_kind * pidk)
6417 cp_token *token;
6418 enum tree_code unary_operator;
6420 /* Peek at the next token. */
6421 token = cp_lexer_peek_token (parser->lexer);
6422 /* Some keywords give away the kind of expression. */
6423 if (token->type == CPP_KEYWORD)
6425 enum rid keyword = token->keyword;
6427 switch (keyword)
6429 case RID_ALIGNOF:
6430 case RID_SIZEOF:
6432 tree operand, ret;
6433 enum tree_code op;
6434 location_t first_loc;
6436 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6437 /* Consume the token. */
6438 cp_lexer_consume_token (parser->lexer);
6439 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6440 /* Parse the operand. */
6441 operand = cp_parser_sizeof_operand (parser, keyword);
6443 if (TYPE_P (operand))
6444 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6445 else
6447 /* ISO C++ defines alignof only with types, not with
6448 expressions. So pedwarn if alignof is used with a non-
6449 type expression. However, __alignof__ is ok. */
6450 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6451 pedwarn (token->location, OPT_Wpedantic,
6452 "ISO C++ does not allow %<alignof%> "
6453 "with a non-type");
6455 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6457 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6458 SIZEOF_EXPR with the original operand. */
6459 if (op == SIZEOF_EXPR && ret != error_mark_node)
6461 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6463 if (!processing_template_decl && TYPE_P (operand))
6465 ret = build_min (SIZEOF_EXPR, size_type_node,
6466 build1 (NOP_EXPR, operand,
6467 error_mark_node));
6468 SIZEOF_EXPR_TYPE_P (ret) = 1;
6470 else
6471 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6472 TREE_SIDE_EFFECTS (ret) = 0;
6473 TREE_READONLY (ret) = 1;
6475 SET_EXPR_LOCATION (ret, first_loc);
6477 return ret;
6480 case RID_NEW:
6481 return cp_parser_new_expression (parser);
6483 case RID_DELETE:
6484 return cp_parser_delete_expression (parser);
6486 case RID_EXTENSION:
6488 /* The saved value of the PEDANTIC flag. */
6489 int saved_pedantic;
6490 tree expr;
6492 /* Save away the PEDANTIC flag. */
6493 cp_parser_extension_opt (parser, &saved_pedantic);
6494 /* Parse the cast-expression. */
6495 expr = cp_parser_simple_cast_expression (parser);
6496 /* Restore the PEDANTIC flag. */
6497 pedantic = saved_pedantic;
6499 return expr;
6502 case RID_REALPART:
6503 case RID_IMAGPART:
6505 tree expression;
6507 /* Consume the `__real__' or `__imag__' token. */
6508 cp_lexer_consume_token (parser->lexer);
6509 /* Parse the cast-expression. */
6510 expression = cp_parser_simple_cast_expression (parser);
6511 /* Create the complete representation. */
6512 return build_x_unary_op (token->location,
6513 (keyword == RID_REALPART
6514 ? REALPART_EXPR : IMAGPART_EXPR),
6515 expression,
6516 tf_warning_or_error);
6518 break;
6520 case RID_TRANSACTION_ATOMIC:
6521 case RID_TRANSACTION_RELAXED:
6522 return cp_parser_transaction_expression (parser, keyword);
6524 case RID_NOEXCEPT:
6526 tree expr;
6527 const char *saved_message;
6528 bool saved_integral_constant_expression_p;
6529 bool saved_non_integral_constant_expression_p;
6530 bool saved_greater_than_is_operator_p;
6532 cp_lexer_consume_token (parser->lexer);
6533 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6535 saved_message = parser->type_definition_forbidden_message;
6536 parser->type_definition_forbidden_message
6537 = G_("types may not be defined in %<noexcept%> expressions");
6539 saved_integral_constant_expression_p
6540 = parser->integral_constant_expression_p;
6541 saved_non_integral_constant_expression_p
6542 = parser->non_integral_constant_expression_p;
6543 parser->integral_constant_expression_p = false;
6545 saved_greater_than_is_operator_p
6546 = parser->greater_than_is_operator_p;
6547 parser->greater_than_is_operator_p = true;
6549 ++cp_unevaluated_operand;
6550 ++c_inhibit_evaluation_warnings;
6551 expr = cp_parser_expression (parser, false, NULL);
6552 --c_inhibit_evaluation_warnings;
6553 --cp_unevaluated_operand;
6555 parser->greater_than_is_operator_p
6556 = saved_greater_than_is_operator_p;
6558 parser->integral_constant_expression_p
6559 = saved_integral_constant_expression_p;
6560 parser->non_integral_constant_expression_p
6561 = saved_non_integral_constant_expression_p;
6563 parser->type_definition_forbidden_message = saved_message;
6565 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6566 return finish_noexcept_expr (expr, tf_warning_or_error);
6569 default:
6570 break;
6574 /* Look for the `:: new' and `:: delete', which also signal the
6575 beginning of a new-expression, or delete-expression,
6576 respectively. If the next token is `::', then it might be one of
6577 these. */
6578 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6580 enum rid keyword;
6582 /* See if the token after the `::' is one of the keywords in
6583 which we're interested. */
6584 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6585 /* If it's `new', we have a new-expression. */
6586 if (keyword == RID_NEW)
6587 return cp_parser_new_expression (parser);
6588 /* Similarly, for `delete'. */
6589 else if (keyword == RID_DELETE)
6590 return cp_parser_delete_expression (parser);
6593 /* Look for a unary operator. */
6594 unary_operator = cp_parser_unary_operator (token);
6595 /* The `++' and `--' operators can be handled similarly, even though
6596 they are not technically unary-operators in the grammar. */
6597 if (unary_operator == ERROR_MARK)
6599 if (token->type == CPP_PLUS_PLUS)
6600 unary_operator = PREINCREMENT_EXPR;
6601 else if (token->type == CPP_MINUS_MINUS)
6602 unary_operator = PREDECREMENT_EXPR;
6603 /* Handle the GNU address-of-label extension. */
6604 else if (cp_parser_allow_gnu_extensions_p (parser)
6605 && token->type == CPP_AND_AND)
6607 tree identifier;
6608 tree expression;
6609 location_t loc = token->location;
6611 /* Consume the '&&' token. */
6612 cp_lexer_consume_token (parser->lexer);
6613 /* Look for the identifier. */
6614 identifier = cp_parser_identifier (parser);
6615 /* Create an expression representing the address. */
6616 expression = finish_label_address_expr (identifier, loc);
6617 if (cp_parser_non_integral_constant_expression (parser,
6618 NIC_ADDR_LABEL))
6619 expression = error_mark_node;
6620 return expression;
6623 if (unary_operator != ERROR_MARK)
6625 tree cast_expression;
6626 tree expression = error_mark_node;
6627 non_integral_constant non_constant_p = NIC_NONE;
6628 location_t loc = token->location;
6630 /* Consume the operator token. */
6631 token = cp_lexer_consume_token (parser->lexer);
6632 /* Parse the cast-expression. */
6633 cast_expression
6634 = cp_parser_cast_expression (parser,
6635 unary_operator == ADDR_EXPR,
6636 /*cast_p=*/false, pidk);
6637 /* Now, build an appropriate representation. */
6638 switch (unary_operator)
6640 case INDIRECT_REF:
6641 non_constant_p = NIC_STAR;
6642 expression = build_x_indirect_ref (loc, cast_expression,
6643 RO_UNARY_STAR,
6644 tf_warning_or_error);
6645 break;
6647 case ADDR_EXPR:
6648 non_constant_p = NIC_ADDR;
6649 /* Fall through. */
6650 case BIT_NOT_EXPR:
6651 expression = build_x_unary_op (loc, unary_operator,
6652 cast_expression,
6653 tf_warning_or_error);
6654 break;
6656 case PREINCREMENT_EXPR:
6657 case PREDECREMENT_EXPR:
6658 non_constant_p = unary_operator == PREINCREMENT_EXPR
6659 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6660 /* Fall through. */
6661 case UNARY_PLUS_EXPR:
6662 case NEGATE_EXPR:
6663 case TRUTH_NOT_EXPR:
6664 expression = finish_unary_op_expr (loc, unary_operator,
6665 cast_expression);
6666 break;
6668 default:
6669 gcc_unreachable ();
6672 if (non_constant_p != NIC_NONE
6673 && cp_parser_non_integral_constant_expression (parser,
6674 non_constant_p))
6675 expression = error_mark_node;
6677 return expression;
6680 return cp_parser_postfix_expression (parser, address_p, cast_p,
6681 /*member_access_only_p=*/false,
6682 pidk);
6685 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6686 unary-operator, the corresponding tree code is returned. */
6688 static enum tree_code
6689 cp_parser_unary_operator (cp_token* token)
6691 switch (token->type)
6693 case CPP_MULT:
6694 return INDIRECT_REF;
6696 case CPP_AND:
6697 return ADDR_EXPR;
6699 case CPP_PLUS:
6700 return UNARY_PLUS_EXPR;
6702 case CPP_MINUS:
6703 return NEGATE_EXPR;
6705 case CPP_NOT:
6706 return TRUTH_NOT_EXPR;
6708 case CPP_COMPL:
6709 return BIT_NOT_EXPR;
6711 default:
6712 return ERROR_MARK;
6716 /* Parse a new-expression.
6718 new-expression:
6719 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6720 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6722 Returns a representation of the expression. */
6724 static tree
6725 cp_parser_new_expression (cp_parser* parser)
6727 bool global_scope_p;
6728 VEC(tree,gc) *placement;
6729 tree type;
6730 VEC(tree,gc) *initializer;
6731 tree nelts = NULL_TREE;
6732 tree ret;
6734 /* Look for the optional `::' operator. */
6735 global_scope_p
6736 = (cp_parser_global_scope_opt (parser,
6737 /*current_scope_valid_p=*/false)
6738 != NULL_TREE);
6739 /* Look for the `new' operator. */
6740 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6741 /* There's no easy way to tell a new-placement from the
6742 `( type-id )' construct. */
6743 cp_parser_parse_tentatively (parser);
6744 /* Look for a new-placement. */
6745 placement = cp_parser_new_placement (parser);
6746 /* If that didn't work out, there's no new-placement. */
6747 if (!cp_parser_parse_definitely (parser))
6749 if (placement != NULL)
6750 release_tree_vector (placement);
6751 placement = NULL;
6754 /* If the next token is a `(', then we have a parenthesized
6755 type-id. */
6756 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6758 cp_token *token;
6759 const char *saved_message = parser->type_definition_forbidden_message;
6761 /* Consume the `('. */
6762 cp_lexer_consume_token (parser->lexer);
6764 /* Parse the type-id. */
6765 parser->type_definition_forbidden_message
6766 = G_("types may not be defined in a new-expression");
6767 type = cp_parser_type_id (parser);
6768 parser->type_definition_forbidden_message = saved_message;
6770 /* Look for the closing `)'. */
6771 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6772 token = cp_lexer_peek_token (parser->lexer);
6773 /* There should not be a direct-new-declarator in this production,
6774 but GCC used to allowed this, so we check and emit a sensible error
6775 message for this case. */
6776 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6778 error_at (token->location,
6779 "array bound forbidden after parenthesized type-id");
6780 inform (token->location,
6781 "try removing the parentheses around the type-id");
6782 cp_parser_direct_new_declarator (parser);
6785 /* Otherwise, there must be a new-type-id. */
6786 else
6787 type = cp_parser_new_type_id (parser, &nelts);
6789 /* If the next token is a `(' or '{', then we have a new-initializer. */
6790 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6791 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6792 initializer = cp_parser_new_initializer (parser);
6793 else
6794 initializer = NULL;
6796 /* A new-expression may not appear in an integral constant
6797 expression. */
6798 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6799 ret = error_mark_node;
6800 else
6802 /* Create a representation of the new-expression. */
6803 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6804 tf_warning_or_error);
6807 if (placement != NULL)
6808 release_tree_vector (placement);
6809 if (initializer != NULL)
6810 release_tree_vector (initializer);
6812 return ret;
6815 /* Parse a new-placement.
6817 new-placement:
6818 ( expression-list )
6820 Returns the same representation as for an expression-list. */
6822 static VEC(tree,gc) *
6823 cp_parser_new_placement (cp_parser* parser)
6825 VEC(tree,gc) *expression_list;
6827 /* Parse the expression-list. */
6828 expression_list = (cp_parser_parenthesized_expression_list
6829 (parser, non_attr, /*cast_p=*/false,
6830 /*allow_expansion_p=*/true,
6831 /*non_constant_p=*/NULL));
6833 return expression_list;
6836 /* Parse a new-type-id.
6838 new-type-id:
6839 type-specifier-seq new-declarator [opt]
6841 Returns the TYPE allocated. If the new-type-id indicates an array
6842 type, *NELTS is set to the number of elements in the last array
6843 bound; the TYPE will not include the last array bound. */
6845 static tree
6846 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6848 cp_decl_specifier_seq type_specifier_seq;
6849 cp_declarator *new_declarator;
6850 cp_declarator *declarator;
6851 cp_declarator *outer_declarator;
6852 const char *saved_message;
6854 /* The type-specifier sequence must not contain type definitions.
6855 (It cannot contain declarations of new types either, but if they
6856 are not definitions we will catch that because they are not
6857 complete.) */
6858 saved_message = parser->type_definition_forbidden_message;
6859 parser->type_definition_forbidden_message
6860 = G_("types may not be defined in a new-type-id");
6861 /* Parse the type-specifier-seq. */
6862 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6863 /*is_trailing_return=*/false,
6864 &type_specifier_seq);
6865 /* Restore the old message. */
6866 parser->type_definition_forbidden_message = saved_message;
6868 if (type_specifier_seq.type == error_mark_node)
6869 return error_mark_node;
6871 /* Parse the new-declarator. */
6872 new_declarator = cp_parser_new_declarator_opt (parser);
6874 /* Determine the number of elements in the last array dimension, if
6875 any. */
6876 *nelts = NULL_TREE;
6877 /* Skip down to the last array dimension. */
6878 declarator = new_declarator;
6879 outer_declarator = NULL;
6880 while (declarator && (declarator->kind == cdk_pointer
6881 || declarator->kind == cdk_ptrmem))
6883 outer_declarator = declarator;
6884 declarator = declarator->declarator;
6886 while (declarator
6887 && declarator->kind == cdk_array
6888 && declarator->declarator
6889 && declarator->declarator->kind == cdk_array)
6891 outer_declarator = declarator;
6892 declarator = declarator->declarator;
6895 if (declarator && declarator->kind == cdk_array)
6897 *nelts = declarator->u.array.bounds;
6898 if (*nelts == error_mark_node)
6899 *nelts = integer_one_node;
6901 if (outer_declarator)
6902 outer_declarator->declarator = declarator->declarator;
6903 else
6904 new_declarator = NULL;
6907 return groktypename (&type_specifier_seq, new_declarator, false);
6910 /* Parse an (optional) new-declarator.
6912 new-declarator:
6913 ptr-operator new-declarator [opt]
6914 direct-new-declarator
6916 Returns the declarator. */
6918 static cp_declarator *
6919 cp_parser_new_declarator_opt (cp_parser* parser)
6921 enum tree_code code;
6922 tree type, std_attributes = NULL_TREE;
6923 cp_cv_quals cv_quals;
6925 /* We don't know if there's a ptr-operator next, or not. */
6926 cp_parser_parse_tentatively (parser);
6927 /* Look for a ptr-operator. */
6928 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
6929 /* If that worked, look for more new-declarators. */
6930 if (cp_parser_parse_definitely (parser))
6932 cp_declarator *declarator;
6934 /* Parse another optional declarator. */
6935 declarator = cp_parser_new_declarator_opt (parser);
6937 declarator = cp_parser_make_indirect_declarator
6938 (code, type, cv_quals, declarator, std_attributes);
6940 return declarator;
6943 /* If the next token is a `[', there is a direct-new-declarator. */
6944 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6945 return cp_parser_direct_new_declarator (parser);
6947 return NULL;
6950 /* Parse a direct-new-declarator.
6952 direct-new-declarator:
6953 [ expression ]
6954 direct-new-declarator [constant-expression]
6958 static cp_declarator *
6959 cp_parser_direct_new_declarator (cp_parser* parser)
6961 cp_declarator *declarator = NULL;
6963 while (true)
6965 tree expression;
6966 cp_token *token;
6968 /* Look for the opening `['. */
6969 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6971 token = cp_lexer_peek_token (parser->lexer);
6972 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6973 /* The standard requires that the expression have integral
6974 type. DR 74 adds enumeration types. We believe that the
6975 real intent is that these expressions be handled like the
6976 expression in a `switch' condition, which also allows
6977 classes with a single conversion to integral or
6978 enumeration type. */
6979 if (!processing_template_decl)
6981 expression
6982 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6983 expression,
6984 /*complain=*/true);
6985 if (!expression)
6987 error_at (token->location,
6988 "expression in new-declarator must have integral "
6989 "or enumeration type");
6990 expression = error_mark_node;
6994 /* Look for the closing `]'. */
6995 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6997 /* Add this bound to the declarator. */
6998 declarator = make_array_declarator (declarator, expression);
7000 /* If the next token is not a `[', then there are no more
7001 bounds. */
7002 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7003 break;
7006 return declarator;
7009 /* Parse a new-initializer.
7011 new-initializer:
7012 ( expression-list [opt] )
7013 braced-init-list
7015 Returns a representation of the expression-list. */
7017 static VEC(tree,gc) *
7018 cp_parser_new_initializer (cp_parser* parser)
7020 VEC(tree,gc) *expression_list;
7022 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7024 tree t;
7025 bool expr_non_constant_p;
7026 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7027 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7028 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7029 expression_list = make_tree_vector_single (t);
7031 else
7032 expression_list = (cp_parser_parenthesized_expression_list
7033 (parser, non_attr, /*cast_p=*/false,
7034 /*allow_expansion_p=*/true,
7035 /*non_constant_p=*/NULL));
7037 return expression_list;
7040 /* Parse a delete-expression.
7042 delete-expression:
7043 :: [opt] delete cast-expression
7044 :: [opt] delete [ ] cast-expression
7046 Returns a representation of the expression. */
7048 static tree
7049 cp_parser_delete_expression (cp_parser* parser)
7051 bool global_scope_p;
7052 bool array_p;
7053 tree expression;
7055 /* Look for the optional `::' operator. */
7056 global_scope_p
7057 = (cp_parser_global_scope_opt (parser,
7058 /*current_scope_valid_p=*/false)
7059 != NULL_TREE);
7060 /* Look for the `delete' keyword. */
7061 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7062 /* See if the array syntax is in use. */
7063 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7065 /* Consume the `[' token. */
7066 cp_lexer_consume_token (parser->lexer);
7067 /* Look for the `]' token. */
7068 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7069 /* Remember that this is the `[]' construct. */
7070 array_p = true;
7072 else
7073 array_p = false;
7075 /* Parse the cast-expression. */
7076 expression = cp_parser_simple_cast_expression (parser);
7078 /* A delete-expression may not appear in an integral constant
7079 expression. */
7080 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7081 return error_mark_node;
7083 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7084 tf_warning_or_error);
7087 /* Returns true if TOKEN may start a cast-expression and false
7088 otherwise. */
7090 static bool
7091 cp_parser_token_starts_cast_expression (cp_token *token)
7093 switch (token->type)
7095 case CPP_COMMA:
7096 case CPP_SEMICOLON:
7097 case CPP_QUERY:
7098 case CPP_COLON:
7099 case CPP_CLOSE_SQUARE:
7100 case CPP_CLOSE_PAREN:
7101 case CPP_CLOSE_BRACE:
7102 case CPP_DOT:
7103 case CPP_DOT_STAR:
7104 case CPP_DEREF:
7105 case CPP_DEREF_STAR:
7106 case CPP_DIV:
7107 case CPP_MOD:
7108 case CPP_LSHIFT:
7109 case CPP_RSHIFT:
7110 case CPP_LESS:
7111 case CPP_GREATER:
7112 case CPP_LESS_EQ:
7113 case CPP_GREATER_EQ:
7114 case CPP_EQ_EQ:
7115 case CPP_NOT_EQ:
7116 case CPP_EQ:
7117 case CPP_MULT_EQ:
7118 case CPP_DIV_EQ:
7119 case CPP_MOD_EQ:
7120 case CPP_PLUS_EQ:
7121 case CPP_MINUS_EQ:
7122 case CPP_RSHIFT_EQ:
7123 case CPP_LSHIFT_EQ:
7124 case CPP_AND_EQ:
7125 case CPP_XOR_EQ:
7126 case CPP_OR_EQ:
7127 case CPP_XOR:
7128 case CPP_OR:
7129 case CPP_OR_OR:
7130 case CPP_EOF:
7131 return false;
7133 /* '[' may start a primary-expression in obj-c++. */
7134 case CPP_OPEN_SQUARE:
7135 return c_dialect_objc ();
7137 default:
7138 return true;
7142 /* Parse a cast-expression.
7144 cast-expression:
7145 unary-expression
7146 ( type-id ) cast-expression
7148 ADDRESS_P is true iff the unary-expression is appearing as the
7149 operand of the `&' operator. CAST_P is true if this expression is
7150 the target of a cast.
7152 Returns a representation of the expression. */
7154 static tree
7155 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7156 cp_id_kind * pidk)
7158 /* If it's a `(', then we might be looking at a cast. */
7159 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7161 tree type = NULL_TREE;
7162 tree expr = NULL_TREE;
7163 bool compound_literal_p;
7164 const char *saved_message;
7166 /* There's no way to know yet whether or not this is a cast.
7167 For example, `(int (3))' is a unary-expression, while `(int)
7168 3' is a cast. So, we resort to parsing tentatively. */
7169 cp_parser_parse_tentatively (parser);
7170 /* Types may not be defined in a cast. */
7171 saved_message = parser->type_definition_forbidden_message;
7172 parser->type_definition_forbidden_message
7173 = G_("types may not be defined in casts");
7174 /* Consume the `('. */
7175 cp_lexer_consume_token (parser->lexer);
7176 /* A very tricky bit is that `(struct S) { 3 }' is a
7177 compound-literal (which we permit in C++ as an extension).
7178 But, that construct is not a cast-expression -- it is a
7179 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7180 is legal; if the compound-literal were a cast-expression,
7181 you'd need an extra set of parentheses.) But, if we parse
7182 the type-id, and it happens to be a class-specifier, then we
7183 will commit to the parse at that point, because we cannot
7184 undo the action that is done when creating a new class. So,
7185 then we cannot back up and do a postfix-expression.
7187 Therefore, we scan ahead to the closing `)', and check to see
7188 if the token after the `)' is a `{'. If so, we are not
7189 looking at a cast-expression.
7191 Save tokens so that we can put them back. */
7192 cp_lexer_save_tokens (parser->lexer);
7193 /* Skip tokens until the next token is a closing parenthesis.
7194 If we find the closing `)', and the next token is a `{', then
7195 we are looking at a compound-literal. */
7196 compound_literal_p
7197 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7198 /*consume_paren=*/true)
7199 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7200 /* Roll back the tokens we skipped. */
7201 cp_lexer_rollback_tokens (parser->lexer);
7202 /* If we were looking at a compound-literal, simulate an error
7203 so that the call to cp_parser_parse_definitely below will
7204 fail. */
7205 if (compound_literal_p)
7206 cp_parser_simulate_error (parser);
7207 else
7209 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7210 parser->in_type_id_in_expr_p = true;
7211 /* Look for the type-id. */
7212 type = cp_parser_type_id (parser);
7213 /* Look for the closing `)'. */
7214 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7215 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7218 /* Restore the saved message. */
7219 parser->type_definition_forbidden_message = saved_message;
7221 /* At this point this can only be either a cast or a
7222 parenthesized ctor such as `(T ())' that looks like a cast to
7223 function returning T. */
7224 if (!cp_parser_error_occurred (parser)
7225 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7226 (parser->lexer)))
7228 cp_parser_parse_definitely (parser);
7229 expr = cp_parser_cast_expression (parser,
7230 /*address_p=*/false,
7231 /*cast_p=*/true, pidk);
7233 /* Warn about old-style casts, if so requested. */
7234 if (warn_old_style_cast
7235 && !in_system_header
7236 && !VOID_TYPE_P (type)
7237 && current_lang_name != lang_name_c)
7238 warning (OPT_Wold_style_cast, "use of old-style cast");
7240 /* Only type conversions to integral or enumeration types
7241 can be used in constant-expressions. */
7242 if (!cast_valid_in_integral_constant_expression_p (type)
7243 && cp_parser_non_integral_constant_expression (parser,
7244 NIC_CAST))
7245 return error_mark_node;
7247 /* Perform the cast. */
7248 expr = build_c_cast (input_location, type, expr);
7249 return expr;
7251 else
7252 cp_parser_abort_tentative_parse (parser);
7255 /* If we get here, then it's not a cast, so it must be a
7256 unary-expression. */
7257 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7260 /* Parse a binary expression of the general form:
7262 pm-expression:
7263 cast-expression
7264 pm-expression .* cast-expression
7265 pm-expression ->* cast-expression
7267 multiplicative-expression:
7268 pm-expression
7269 multiplicative-expression * pm-expression
7270 multiplicative-expression / pm-expression
7271 multiplicative-expression % pm-expression
7273 additive-expression:
7274 multiplicative-expression
7275 additive-expression + multiplicative-expression
7276 additive-expression - multiplicative-expression
7278 shift-expression:
7279 additive-expression
7280 shift-expression << additive-expression
7281 shift-expression >> additive-expression
7283 relational-expression:
7284 shift-expression
7285 relational-expression < shift-expression
7286 relational-expression > shift-expression
7287 relational-expression <= shift-expression
7288 relational-expression >= shift-expression
7290 GNU Extension:
7292 relational-expression:
7293 relational-expression <? shift-expression
7294 relational-expression >? shift-expression
7296 equality-expression:
7297 relational-expression
7298 equality-expression == relational-expression
7299 equality-expression != relational-expression
7301 and-expression:
7302 equality-expression
7303 and-expression & equality-expression
7305 exclusive-or-expression:
7306 and-expression
7307 exclusive-or-expression ^ and-expression
7309 inclusive-or-expression:
7310 exclusive-or-expression
7311 inclusive-or-expression | exclusive-or-expression
7313 logical-and-expression:
7314 inclusive-or-expression
7315 logical-and-expression && inclusive-or-expression
7317 logical-or-expression:
7318 logical-and-expression
7319 logical-or-expression || logical-and-expression
7321 All these are implemented with a single function like:
7323 binary-expression:
7324 simple-cast-expression
7325 binary-expression <token> binary-expression
7327 CAST_P is true if this expression is the target of a cast.
7329 The binops_by_token map is used to get the tree codes for each <token> type.
7330 binary-expressions are associated according to a precedence table. */
7332 #define TOKEN_PRECEDENCE(token) \
7333 (((token->type == CPP_GREATER \
7334 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7335 && !parser->greater_than_is_operator_p) \
7336 ? PREC_NOT_OPERATOR \
7337 : binops_by_token[token->type].prec)
7339 static tree
7340 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7341 bool no_toplevel_fold_p,
7342 enum cp_parser_prec prec,
7343 cp_id_kind * pidk)
7345 cp_parser_expression_stack stack;
7346 cp_parser_expression_stack_entry *sp = &stack[0];
7347 cp_parser_expression_stack_entry current;
7348 tree rhs;
7349 cp_token *token;
7350 enum tree_code rhs_type;
7351 enum cp_parser_prec new_prec, lookahead_prec;
7352 tree overload;
7354 /* Parse the first expression. */
7355 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7356 cast_p, pidk);
7357 current.lhs_type = ERROR_MARK;
7358 current.prec = prec;
7360 if (cp_parser_error_occurred (parser))
7361 return error_mark_node;
7363 for (;;)
7365 /* Get an operator token. */
7366 token = cp_lexer_peek_token (parser->lexer);
7368 if (warn_cxx0x_compat
7369 && token->type == CPP_RSHIFT
7370 && !parser->greater_than_is_operator_p)
7372 if (warning_at (token->location, OPT_Wc__0x_compat,
7373 "%<>>%> operator is treated"
7374 " as two right angle brackets in C++11"))
7375 inform (token->location,
7376 "suggest parentheses around %<>>%> expression");
7379 new_prec = TOKEN_PRECEDENCE (token);
7381 /* Popping an entry off the stack means we completed a subexpression:
7382 - either we found a token which is not an operator (`>' where it is not
7383 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7384 will happen repeatedly;
7385 - or, we found an operator which has lower priority. This is the case
7386 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7387 parsing `3 * 4'. */
7388 if (new_prec <= current.prec)
7390 if (sp == stack)
7391 break;
7392 else
7393 goto pop;
7396 get_rhs:
7397 current.tree_type = binops_by_token[token->type].tree_type;
7398 current.loc = token->location;
7400 /* We used the operator token. */
7401 cp_lexer_consume_token (parser->lexer);
7403 /* For "false && x" or "true || x", x will never be executed;
7404 disable warnings while evaluating it. */
7405 if (current.tree_type == TRUTH_ANDIF_EXPR)
7406 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7407 else if (current.tree_type == TRUTH_ORIF_EXPR)
7408 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7410 /* Extract another operand. It may be the RHS of this expression
7411 or the LHS of a new, higher priority expression. */
7412 rhs = cp_parser_simple_cast_expression (parser);
7413 rhs_type = ERROR_MARK;
7415 /* Get another operator token. Look up its precedence to avoid
7416 building a useless (immediately popped) stack entry for common
7417 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7418 token = cp_lexer_peek_token (parser->lexer);
7419 lookahead_prec = TOKEN_PRECEDENCE (token);
7420 if (lookahead_prec > new_prec)
7422 /* ... and prepare to parse the RHS of the new, higher priority
7423 expression. Since precedence levels on the stack are
7424 monotonically increasing, we do not have to care about
7425 stack overflows. */
7426 *sp = current;
7427 ++sp;
7428 current.lhs = rhs;
7429 current.lhs_type = rhs_type;
7430 current.prec = new_prec;
7431 new_prec = lookahead_prec;
7432 goto get_rhs;
7434 pop:
7435 lookahead_prec = new_prec;
7436 /* If the stack is not empty, we have parsed into LHS the right side
7437 (`4' in the example above) of an expression we had suspended.
7438 We can use the information on the stack to recover the LHS (`3')
7439 from the stack together with the tree code (`MULT_EXPR'), and
7440 the precedence of the higher level subexpression
7441 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7442 which will be used to actually build the additive expression. */
7443 rhs = current.lhs;
7444 rhs_type = current.lhs_type;
7445 --sp;
7446 current = *sp;
7449 /* Undo the disabling of warnings done above. */
7450 if (current.tree_type == TRUTH_ANDIF_EXPR)
7451 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7452 else if (current.tree_type == TRUTH_ORIF_EXPR)
7453 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7455 overload = NULL;
7456 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7457 ERROR_MARK for everything that is not a binary expression.
7458 This makes warn_about_parentheses miss some warnings that
7459 involve unary operators. For unary expressions we should
7460 pass the correct tree_code unless the unary expression was
7461 surrounded by parentheses.
7463 if (no_toplevel_fold_p
7464 && lookahead_prec <= current.prec
7465 && sp == stack
7466 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7467 current.lhs = build2 (current.tree_type, boolean_type_node,
7468 current.lhs, rhs);
7469 else
7470 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7471 current.lhs, current.lhs_type,
7472 rhs, rhs_type, &overload,
7473 tf_warning_or_error);
7474 current.lhs_type = current.tree_type;
7475 if (EXPR_P (current.lhs))
7476 SET_EXPR_LOCATION (current.lhs, current.loc);
7478 /* If the binary operator required the use of an overloaded operator,
7479 then this expression cannot be an integral constant-expression.
7480 An overloaded operator can be used even if both operands are
7481 otherwise permissible in an integral constant-expression if at
7482 least one of the operands is of enumeration type. */
7484 if (overload
7485 && cp_parser_non_integral_constant_expression (parser,
7486 NIC_OVERLOADED))
7487 return error_mark_node;
7490 return current.lhs;
7494 /* Parse the `? expression : assignment-expression' part of a
7495 conditional-expression. The LOGICAL_OR_EXPR is the
7496 logical-or-expression that started the conditional-expression.
7497 Returns a representation of the entire conditional-expression.
7499 This routine is used by cp_parser_assignment_expression.
7501 ? expression : assignment-expression
7503 GNU Extensions:
7505 ? : assignment-expression */
7507 static tree
7508 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7510 tree expr;
7511 tree assignment_expr;
7512 struct cp_token *token;
7513 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7515 /* Consume the `?' token. */
7516 cp_lexer_consume_token (parser->lexer);
7517 token = cp_lexer_peek_token (parser->lexer);
7518 if (cp_parser_allow_gnu_extensions_p (parser)
7519 && token->type == CPP_COLON)
7521 pedwarn (token->location, OPT_Wpedantic,
7522 "ISO C++ does not allow ?: with omitted middle operand");
7523 /* Implicit true clause. */
7524 expr = NULL_TREE;
7525 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7526 warn_for_omitted_condop (token->location, logical_or_expr);
7528 else
7530 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7531 parser->colon_corrects_to_scope_p = false;
7532 /* Parse the expression. */
7533 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7534 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7535 c_inhibit_evaluation_warnings +=
7536 ((logical_or_expr == truthvalue_true_node)
7537 - (logical_or_expr == truthvalue_false_node));
7538 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7541 /* The next token should be a `:'. */
7542 cp_parser_require (parser, CPP_COLON, RT_COLON);
7543 /* Parse the assignment-expression. */
7544 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7545 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7547 /* Build the conditional-expression. */
7548 return build_x_conditional_expr (loc, logical_or_expr,
7549 expr,
7550 assignment_expr,
7551 tf_warning_or_error);
7554 /* Parse an assignment-expression.
7556 assignment-expression:
7557 conditional-expression
7558 logical-or-expression assignment-operator assignment_expression
7559 throw-expression
7561 CAST_P is true if this expression is the target of a cast.
7563 Returns a representation for the expression. */
7565 static tree
7566 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7567 cp_id_kind * pidk)
7569 tree expr;
7571 /* If the next token is the `throw' keyword, then we're looking at
7572 a throw-expression. */
7573 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7574 expr = cp_parser_throw_expression (parser);
7575 /* Otherwise, it must be that we are looking at a
7576 logical-or-expression. */
7577 else
7579 /* Parse the binary expressions (logical-or-expression). */
7580 expr = cp_parser_binary_expression (parser, cast_p, false,
7581 PREC_NOT_OPERATOR, pidk);
7582 /* If the next token is a `?' then we're actually looking at a
7583 conditional-expression. */
7584 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7585 return cp_parser_question_colon_clause (parser, expr);
7586 else
7588 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7590 /* If it's an assignment-operator, we're using the second
7591 production. */
7592 enum tree_code assignment_operator
7593 = cp_parser_assignment_operator_opt (parser);
7594 if (assignment_operator != ERROR_MARK)
7596 bool non_constant_p;
7597 location_t saved_input_location;
7599 /* Parse the right-hand side of the assignment. */
7600 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7602 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7603 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7605 /* An assignment may not appear in a
7606 constant-expression. */
7607 if (cp_parser_non_integral_constant_expression (parser,
7608 NIC_ASSIGNMENT))
7609 return error_mark_node;
7610 /* Build the assignment expression. Its default
7611 location is the location of the '=' token. */
7612 saved_input_location = input_location;
7613 input_location = loc;
7614 expr = build_x_modify_expr (loc, expr,
7615 assignment_operator,
7616 rhs,
7617 tf_warning_or_error);
7618 input_location = saved_input_location;
7623 return expr;
7626 /* Parse an (optional) assignment-operator.
7628 assignment-operator: one of
7629 = *= /= %= += -= >>= <<= &= ^= |=
7631 GNU Extension:
7633 assignment-operator: one of
7634 <?= >?=
7636 If the next token is an assignment operator, the corresponding tree
7637 code is returned, and the token is consumed. For example, for
7638 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7639 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7640 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7641 operator, ERROR_MARK is returned. */
7643 static enum tree_code
7644 cp_parser_assignment_operator_opt (cp_parser* parser)
7646 enum tree_code op;
7647 cp_token *token;
7649 /* Peek at the next token. */
7650 token = cp_lexer_peek_token (parser->lexer);
7652 switch (token->type)
7654 case CPP_EQ:
7655 op = NOP_EXPR;
7656 break;
7658 case CPP_MULT_EQ:
7659 op = MULT_EXPR;
7660 break;
7662 case CPP_DIV_EQ:
7663 op = TRUNC_DIV_EXPR;
7664 break;
7666 case CPP_MOD_EQ:
7667 op = TRUNC_MOD_EXPR;
7668 break;
7670 case CPP_PLUS_EQ:
7671 op = PLUS_EXPR;
7672 break;
7674 case CPP_MINUS_EQ:
7675 op = MINUS_EXPR;
7676 break;
7678 case CPP_RSHIFT_EQ:
7679 op = RSHIFT_EXPR;
7680 break;
7682 case CPP_LSHIFT_EQ:
7683 op = LSHIFT_EXPR;
7684 break;
7686 case CPP_AND_EQ:
7687 op = BIT_AND_EXPR;
7688 break;
7690 case CPP_XOR_EQ:
7691 op = BIT_XOR_EXPR;
7692 break;
7694 case CPP_OR_EQ:
7695 op = BIT_IOR_EXPR;
7696 break;
7698 default:
7699 /* Nothing else is an assignment operator. */
7700 op = ERROR_MARK;
7703 /* If it was an assignment operator, consume it. */
7704 if (op != ERROR_MARK)
7705 cp_lexer_consume_token (parser->lexer);
7707 return op;
7710 /* Parse an expression.
7712 expression:
7713 assignment-expression
7714 expression , assignment-expression
7716 CAST_P is true if this expression is the target of a cast.
7718 Returns a representation of the expression. */
7720 static tree
7721 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7723 tree expression = NULL_TREE;
7724 location_t loc = UNKNOWN_LOCATION;
7726 while (true)
7728 tree assignment_expression;
7730 /* Parse the next assignment-expression. */
7731 assignment_expression
7732 = cp_parser_assignment_expression (parser, cast_p, pidk);
7733 /* If this is the first assignment-expression, we can just
7734 save it away. */
7735 if (!expression)
7736 expression = assignment_expression;
7737 else
7738 expression = build_x_compound_expr (loc, expression,
7739 assignment_expression,
7740 tf_warning_or_error);
7741 /* If the next token is not a comma, then we are done with the
7742 expression. */
7743 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7744 break;
7745 /* Consume the `,'. */
7746 loc = cp_lexer_peek_token (parser->lexer)->location;
7747 cp_lexer_consume_token (parser->lexer);
7748 /* A comma operator cannot appear in a constant-expression. */
7749 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7750 expression = error_mark_node;
7753 return expression;
7756 /* Parse a constant-expression.
7758 constant-expression:
7759 conditional-expression
7761 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7762 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7763 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7764 is false, NON_CONSTANT_P should be NULL. */
7766 static tree
7767 cp_parser_constant_expression (cp_parser* parser,
7768 bool allow_non_constant_p,
7769 bool *non_constant_p)
7771 bool saved_integral_constant_expression_p;
7772 bool saved_allow_non_integral_constant_expression_p;
7773 bool saved_non_integral_constant_expression_p;
7774 tree expression;
7776 /* It might seem that we could simply parse the
7777 conditional-expression, and then check to see if it were
7778 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7779 one that the compiler can figure out is constant, possibly after
7780 doing some simplifications or optimizations. The standard has a
7781 precise definition of constant-expression, and we must honor
7782 that, even though it is somewhat more restrictive.
7784 For example:
7786 int i[(2, 3)];
7788 is not a legal declaration, because `(2, 3)' is not a
7789 constant-expression. The `,' operator is forbidden in a
7790 constant-expression. However, GCC's constant-folding machinery
7791 will fold this operation to an INTEGER_CST for `3'. */
7793 /* Save the old settings. */
7794 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7795 saved_allow_non_integral_constant_expression_p
7796 = parser->allow_non_integral_constant_expression_p;
7797 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7798 /* We are now parsing a constant-expression. */
7799 parser->integral_constant_expression_p = true;
7800 parser->allow_non_integral_constant_expression_p
7801 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7802 parser->non_integral_constant_expression_p = false;
7803 /* Although the grammar says "conditional-expression", we parse an
7804 "assignment-expression", which also permits "throw-expression"
7805 and the use of assignment operators. In the case that
7806 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7807 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7808 actually essential that we look for an assignment-expression.
7809 For example, cp_parser_initializer_clauses uses this function to
7810 determine whether a particular assignment-expression is in fact
7811 constant. */
7812 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7813 /* Restore the old settings. */
7814 parser->integral_constant_expression_p
7815 = saved_integral_constant_expression_p;
7816 parser->allow_non_integral_constant_expression_p
7817 = saved_allow_non_integral_constant_expression_p;
7818 if (cxx_dialect >= cxx0x)
7820 /* Require an rvalue constant expression here; that's what our
7821 callers expect. Reference constant expressions are handled
7822 separately in e.g. cp_parser_template_argument. */
7823 bool is_const = potential_rvalue_constant_expression (expression);
7824 parser->non_integral_constant_expression_p = !is_const;
7825 if (!is_const && !allow_non_constant_p)
7826 require_potential_rvalue_constant_expression (expression);
7828 if (allow_non_constant_p)
7829 *non_constant_p = parser->non_integral_constant_expression_p;
7830 parser->non_integral_constant_expression_p
7831 = saved_non_integral_constant_expression_p;
7833 return expression;
7836 /* Parse __builtin_offsetof.
7838 offsetof-expression:
7839 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7841 offsetof-member-designator:
7842 id-expression
7843 | offsetof-member-designator "." id-expression
7844 | offsetof-member-designator "[" expression "]"
7845 | offsetof-member-designator "->" id-expression */
7847 static tree
7848 cp_parser_builtin_offsetof (cp_parser *parser)
7850 int save_ice_p, save_non_ice_p;
7851 tree type, expr;
7852 cp_id_kind dummy;
7853 cp_token *token;
7855 /* We're about to accept non-integral-constant things, but will
7856 definitely yield an integral constant expression. Save and
7857 restore these values around our local parsing. */
7858 save_ice_p = parser->integral_constant_expression_p;
7859 save_non_ice_p = parser->non_integral_constant_expression_p;
7861 /* Consume the "__builtin_offsetof" token. */
7862 cp_lexer_consume_token (parser->lexer);
7863 /* Consume the opening `('. */
7864 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7865 /* Parse the type-id. */
7866 type = cp_parser_type_id (parser);
7867 /* Look for the `,'. */
7868 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7869 token = cp_lexer_peek_token (parser->lexer);
7871 /* Build the (type *)null that begins the traditional offsetof macro. */
7872 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7873 tf_warning_or_error);
7875 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7876 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7877 true, &dummy, token->location);
7878 while (true)
7880 token = cp_lexer_peek_token (parser->lexer);
7881 switch (token->type)
7883 case CPP_OPEN_SQUARE:
7884 /* offsetof-member-designator "[" expression "]" */
7885 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7886 break;
7888 case CPP_DEREF:
7889 /* offsetof-member-designator "->" identifier */
7890 expr = grok_array_decl (token->location, expr, integer_zero_node);
7891 /* FALLTHRU */
7893 case CPP_DOT:
7894 /* offsetof-member-designator "." identifier */
7895 cp_lexer_consume_token (parser->lexer);
7896 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7897 expr, true, &dummy,
7898 token->location);
7899 break;
7901 case CPP_CLOSE_PAREN:
7902 /* Consume the ")" token. */
7903 cp_lexer_consume_token (parser->lexer);
7904 goto success;
7906 default:
7907 /* Error. We know the following require will fail, but
7908 that gives the proper error message. */
7909 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7910 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7911 expr = error_mark_node;
7912 goto failure;
7916 success:
7917 /* If we're processing a template, we can't finish the semantics yet.
7918 Otherwise we can fold the entire expression now. */
7919 if (processing_template_decl)
7920 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7921 else
7922 expr = finish_offsetof (expr);
7924 failure:
7925 parser->integral_constant_expression_p = save_ice_p;
7926 parser->non_integral_constant_expression_p = save_non_ice_p;
7928 return expr;
7931 /* Parse a trait expression.
7933 Returns a representation of the expression, the underlying type
7934 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7936 static tree
7937 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7939 cp_trait_kind kind;
7940 tree type1, type2 = NULL_TREE;
7941 bool binary = false;
7942 cp_decl_specifier_seq decl_specs;
7944 switch (keyword)
7946 case RID_HAS_NOTHROW_ASSIGN:
7947 kind = CPTK_HAS_NOTHROW_ASSIGN;
7948 break;
7949 case RID_HAS_NOTHROW_CONSTRUCTOR:
7950 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7951 break;
7952 case RID_HAS_NOTHROW_COPY:
7953 kind = CPTK_HAS_NOTHROW_COPY;
7954 break;
7955 case RID_HAS_TRIVIAL_ASSIGN:
7956 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7957 break;
7958 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7959 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7960 break;
7961 case RID_HAS_TRIVIAL_COPY:
7962 kind = CPTK_HAS_TRIVIAL_COPY;
7963 break;
7964 case RID_HAS_TRIVIAL_DESTRUCTOR:
7965 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7966 break;
7967 case RID_HAS_VIRTUAL_DESTRUCTOR:
7968 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7969 break;
7970 case RID_IS_ABSTRACT:
7971 kind = CPTK_IS_ABSTRACT;
7972 break;
7973 case RID_IS_BASE_OF:
7974 kind = CPTK_IS_BASE_OF;
7975 binary = true;
7976 break;
7977 case RID_IS_CLASS:
7978 kind = CPTK_IS_CLASS;
7979 break;
7980 case RID_IS_CONVERTIBLE_TO:
7981 kind = CPTK_IS_CONVERTIBLE_TO;
7982 binary = true;
7983 break;
7984 case RID_IS_EMPTY:
7985 kind = CPTK_IS_EMPTY;
7986 break;
7987 case RID_IS_ENUM:
7988 kind = CPTK_IS_ENUM;
7989 break;
7990 case RID_IS_FINAL:
7991 kind = CPTK_IS_FINAL;
7992 break;
7993 case RID_IS_LITERAL_TYPE:
7994 kind = CPTK_IS_LITERAL_TYPE;
7995 break;
7996 case RID_IS_POD:
7997 kind = CPTK_IS_POD;
7998 break;
7999 case RID_IS_POLYMORPHIC:
8000 kind = CPTK_IS_POLYMORPHIC;
8001 break;
8002 case RID_IS_STD_LAYOUT:
8003 kind = CPTK_IS_STD_LAYOUT;
8004 break;
8005 case RID_IS_TRIVIAL:
8006 kind = CPTK_IS_TRIVIAL;
8007 break;
8008 case RID_IS_UNION:
8009 kind = CPTK_IS_UNION;
8010 break;
8011 case RID_UNDERLYING_TYPE:
8012 kind = CPTK_UNDERLYING_TYPE;
8013 break;
8014 case RID_BASES:
8015 kind = CPTK_BASES;
8016 break;
8017 case RID_DIRECT_BASES:
8018 kind = CPTK_DIRECT_BASES;
8019 break;
8020 default:
8021 gcc_unreachable ();
8024 /* Consume the token. */
8025 cp_lexer_consume_token (parser->lexer);
8027 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8029 type1 = cp_parser_type_id (parser);
8031 if (type1 == error_mark_node)
8032 return error_mark_node;
8034 /* Build a trivial decl-specifier-seq. */
8035 clear_decl_specs (&decl_specs);
8036 decl_specs.type = type1;
8038 /* Call grokdeclarator to figure out what type this is. */
8039 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8040 /*initialized=*/0, /*attrlist=*/NULL);
8042 if (binary)
8044 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8046 type2 = cp_parser_type_id (parser);
8048 if (type2 == error_mark_node)
8049 return error_mark_node;
8051 /* Build a trivial decl-specifier-seq. */
8052 clear_decl_specs (&decl_specs);
8053 decl_specs.type = type2;
8055 /* Call grokdeclarator to figure out what type this is. */
8056 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8057 /*initialized=*/0, /*attrlist=*/NULL);
8060 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8062 /* Complete the trait expression, which may mean either processing
8063 the trait expr now or saving it for template instantiation. */
8064 switch(kind)
8066 case CPTK_UNDERLYING_TYPE:
8067 return finish_underlying_type (type1);
8068 case CPTK_BASES:
8069 return finish_bases (type1, false);
8070 case CPTK_DIRECT_BASES:
8071 return finish_bases (type1, true);
8072 default:
8073 return finish_trait_expr (kind, type1, type2);
8077 /* Lambdas that appear in variable initializer or default argument scope
8078 get that in their mangling, so we need to record it. We might as well
8079 use the count for function and namespace scopes as well. */
8080 static GTY(()) tree lambda_scope;
8081 static GTY(()) int lambda_count;
8082 typedef struct GTY(()) tree_int
8084 tree t;
8085 int i;
8086 } tree_int;
8087 DEF_VEC_O(tree_int);
8088 DEF_VEC_ALLOC_O(tree_int,gc);
8089 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
8091 static void
8092 start_lambda_scope (tree decl)
8094 tree_int ti;
8095 gcc_assert (decl);
8096 /* Once we're inside a function, we ignore other scopes and just push
8097 the function again so that popping works properly. */
8098 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8099 decl = current_function_decl;
8100 ti.t = lambda_scope;
8101 ti.i = lambda_count;
8102 VEC_safe_push (tree_int, gc, lambda_scope_stack, ti);
8103 if (lambda_scope != decl)
8105 /* Don't reset the count if we're still in the same function. */
8106 lambda_scope = decl;
8107 lambda_count = 0;
8111 static void
8112 record_lambda_scope (tree lambda)
8114 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8115 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8118 static void
8119 finish_lambda_scope (void)
8121 tree_int *p = &VEC_last (tree_int, lambda_scope_stack);
8122 if (lambda_scope != p->t)
8124 lambda_scope = p->t;
8125 lambda_count = p->i;
8127 VEC_pop (tree_int, lambda_scope_stack);
8130 /* Parse a lambda expression.
8132 lambda-expression:
8133 lambda-introducer lambda-declarator [opt] compound-statement
8135 Returns a representation of the expression. */
8137 static tree
8138 cp_parser_lambda_expression (cp_parser* parser)
8140 tree lambda_expr = build_lambda_expr ();
8141 tree type;
8142 bool ok;
8144 LAMBDA_EXPR_LOCATION (lambda_expr)
8145 = cp_lexer_peek_token (parser->lexer)->location;
8147 if (cp_unevaluated_operand)
8148 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8149 "lambda-expression in unevaluated context");
8151 /* We may be in the middle of deferred access check. Disable
8152 it now. */
8153 push_deferring_access_checks (dk_no_deferred);
8155 cp_parser_lambda_introducer (parser, lambda_expr);
8157 type = begin_lambda_type (lambda_expr);
8158 if (type == error_mark_node)
8159 return error_mark_node;
8161 record_lambda_scope (lambda_expr);
8163 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8164 determine_visibility (TYPE_NAME (type));
8166 /* Now that we've started the type, add the capture fields for any
8167 explicit captures. */
8168 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8171 /* Inside the class, surrounding template-parameter-lists do not apply. */
8172 unsigned int saved_num_template_parameter_lists
8173 = parser->num_template_parameter_lists;
8174 unsigned char in_statement = parser->in_statement;
8175 bool in_switch_statement_p = parser->in_switch_statement_p;
8177 parser->num_template_parameter_lists = 0;
8178 parser->in_statement = 0;
8179 parser->in_switch_statement_p = false;
8181 /* By virtue of defining a local class, a lambda expression has access to
8182 the private variables of enclosing classes. */
8184 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8186 if (ok)
8187 cp_parser_lambda_body (parser, lambda_expr);
8188 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8189 cp_parser_skip_to_end_of_block_or_statement (parser);
8191 /* The capture list was built up in reverse order; fix that now. */
8193 tree newlist = NULL_TREE;
8194 tree elt, next;
8196 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8197 elt; elt = next)
8199 next = TREE_CHAIN (elt);
8200 TREE_CHAIN (elt) = newlist;
8201 newlist = elt;
8203 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8206 if (ok)
8207 maybe_add_lambda_conv_op (type);
8209 type = finish_struct (type, /*attributes=*/NULL_TREE);
8211 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8212 parser->in_statement = in_statement;
8213 parser->in_switch_statement_p = in_switch_statement_p;
8216 pop_deferring_access_checks ();
8218 /* This field is only used during parsing of the lambda. */
8219 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8221 /* This lambda shouldn't have any proxies left at this point. */
8222 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8223 /* And now that we're done, push proxies for an enclosing lambda. */
8224 insert_pending_capture_proxies ();
8226 if (ok)
8227 return build_lambda_object (lambda_expr);
8228 else
8229 return error_mark_node;
8232 /* Parse the beginning of a lambda expression.
8234 lambda-introducer:
8235 [ lambda-capture [opt] ]
8237 LAMBDA_EXPR is the current representation of the lambda expression. */
8239 static void
8240 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8242 /* Need commas after the first capture. */
8243 bool first = true;
8245 /* Eat the leading `['. */
8246 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8248 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8249 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8250 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8251 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8252 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8253 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8255 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8257 cp_lexer_consume_token (parser->lexer);
8258 first = false;
8261 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8263 cp_token* capture_token;
8264 tree capture_id;
8265 tree capture_init_expr;
8266 cp_id_kind idk = CP_ID_KIND_NONE;
8267 bool explicit_init_p = false;
8269 enum capture_kind_type
8271 BY_COPY,
8272 BY_REFERENCE
8274 enum capture_kind_type capture_kind = BY_COPY;
8276 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8278 error ("expected end of capture-list");
8279 return;
8282 if (first)
8283 first = false;
8284 else
8285 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8287 /* Possibly capture `this'. */
8288 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8290 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8291 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8292 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8293 "with by-copy capture default");
8294 cp_lexer_consume_token (parser->lexer);
8295 add_capture (lambda_expr,
8296 /*id=*/this_identifier,
8297 /*initializer=*/finish_this_expr(),
8298 /*by_reference_p=*/false,
8299 explicit_init_p);
8300 continue;
8303 /* Remember whether we want to capture as a reference or not. */
8304 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8306 capture_kind = BY_REFERENCE;
8307 cp_lexer_consume_token (parser->lexer);
8310 /* Get the identifier. */
8311 capture_token = cp_lexer_peek_token (parser->lexer);
8312 capture_id = cp_parser_identifier (parser);
8314 if (capture_id == error_mark_node)
8315 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8316 delimiters, but I modified this to stop on unnested ']' as well. It
8317 was already changed to stop on unnested '}', so the
8318 "closing_parenthesis" name is no more misleading with my change. */
8320 cp_parser_skip_to_closing_parenthesis (parser,
8321 /*recovering=*/true,
8322 /*or_comma=*/true,
8323 /*consume_paren=*/true);
8324 break;
8327 /* Find the initializer for this capture. */
8328 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8330 /* An explicit expression exists. */
8331 cp_lexer_consume_token (parser->lexer);
8332 pedwarn (input_location, OPT_Wpedantic,
8333 "ISO C++ does not allow initializers "
8334 "in lambda expression capture lists");
8335 capture_init_expr = cp_parser_assignment_expression (parser,
8336 /*cast_p=*/true,
8337 &idk);
8338 explicit_init_p = true;
8340 else
8342 const char* error_msg;
8344 /* Turn the identifier into an id-expression. */
8345 capture_init_expr
8346 = cp_parser_lookup_name
8347 (parser,
8348 capture_id,
8349 none_type,
8350 /*is_template=*/false,
8351 /*is_namespace=*/false,
8352 /*check_dependency=*/true,
8353 /*ambiguous_decls=*/NULL,
8354 capture_token->location);
8356 if (capture_init_expr == error_mark_node)
8358 unqualified_name_lookup_error (capture_id);
8359 continue;
8361 else if (DECL_P (capture_init_expr)
8362 && (TREE_CODE (capture_init_expr) != VAR_DECL
8363 && TREE_CODE (capture_init_expr) != PARM_DECL))
8365 error_at (capture_token->location,
8366 "capture of non-variable %qD ",
8367 capture_init_expr);
8368 inform (0, "%q+#D declared here", capture_init_expr);
8369 continue;
8371 if (TREE_CODE (capture_init_expr) == VAR_DECL
8372 && decl_storage_duration (capture_init_expr) != dk_auto)
8374 pedwarn (capture_token->location, 0, "capture of variable "
8375 "%qD with non-automatic storage duration",
8376 capture_init_expr);
8377 inform (0, "%q+#D declared here", capture_init_expr);
8378 continue;
8381 capture_init_expr
8382 = finish_id_expression
8383 (capture_id,
8384 capture_init_expr,
8385 parser->scope,
8386 &idk,
8387 /*integral_constant_expression_p=*/false,
8388 /*allow_non_integral_constant_expression_p=*/false,
8389 /*non_integral_constant_expression_p=*/NULL,
8390 /*template_p=*/false,
8391 /*done=*/true,
8392 /*address_p=*/false,
8393 /*template_arg_p=*/false,
8394 &error_msg,
8395 capture_token->location);
8398 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8399 && !explicit_init_p)
8401 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8402 && capture_kind == BY_COPY)
8403 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8404 "of %qD redundant with by-copy capture default",
8405 capture_id);
8406 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8407 && capture_kind == BY_REFERENCE)
8408 pedwarn (capture_token->location, 0, "explicit by-reference "
8409 "capture of %qD redundant with by-reference capture "
8410 "default", capture_id);
8413 add_capture (lambda_expr,
8414 capture_id,
8415 capture_init_expr,
8416 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8417 explicit_init_p);
8420 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8423 /* Parse the (optional) middle of a lambda expression.
8425 lambda-declarator:
8426 ( parameter-declaration-clause [opt] )
8427 attribute-specifier [opt]
8428 mutable [opt]
8429 exception-specification [opt]
8430 lambda-return-type-clause [opt]
8432 LAMBDA_EXPR is the current representation of the lambda expression. */
8434 static bool
8435 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8437 /* 5.1.1.4 of the standard says:
8438 If a lambda-expression does not include a lambda-declarator, it is as if
8439 the lambda-declarator were ().
8440 This means an empty parameter list, no attributes, and no exception
8441 specification. */
8442 tree param_list = void_list_node;
8443 tree attributes = NULL_TREE;
8444 tree exception_spec = NULL_TREE;
8445 tree t;
8447 /* The lambda-declarator is optional, but must begin with an opening
8448 parenthesis if present. */
8449 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8451 cp_lexer_consume_token (parser->lexer);
8453 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8455 /* Parse parameters. */
8456 param_list = cp_parser_parameter_declaration_clause (parser);
8458 /* Default arguments shall not be specified in the
8459 parameter-declaration-clause of a lambda-declarator. */
8460 for (t = param_list; t; t = TREE_CHAIN (t))
8461 if (TREE_PURPOSE (t))
8462 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8463 "default argument specified for lambda parameter");
8465 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8467 attributes = cp_parser_attributes_opt (parser);
8469 /* Parse optional `mutable' keyword. */
8470 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8472 cp_lexer_consume_token (parser->lexer);
8473 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8476 /* Parse optional exception specification. */
8477 exception_spec = cp_parser_exception_specification_opt (parser);
8479 /* Parse optional trailing return type. */
8480 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8482 cp_lexer_consume_token (parser->lexer);
8483 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8486 /* The function parameters must be in scope all the way until after the
8487 trailing-return-type in case of decltype. */
8488 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8489 pop_binding (DECL_NAME (t), t);
8491 leave_scope ();
8494 /* Create the function call operator.
8496 Messing with declarators like this is no uglier than building up the
8497 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8498 other code. */
8500 cp_decl_specifier_seq return_type_specs;
8501 cp_declarator* declarator;
8502 tree fco;
8503 int quals;
8504 void *p;
8506 clear_decl_specs (&return_type_specs);
8507 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8508 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8509 else
8510 /* Maybe we will deduce the return type later. */
8511 return_type_specs.type = make_auto ();
8513 p = obstack_alloc (&declarator_obstack, 0);
8515 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8516 sfk_none);
8518 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8519 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8520 declarator = make_call_declarator (declarator, param_list, quals,
8521 VIRT_SPEC_UNSPECIFIED,
8522 exception_spec,
8523 /*late_return_type=*/NULL_TREE);
8524 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8526 fco = grokmethod (&return_type_specs,
8527 declarator,
8528 attributes);
8529 if (fco != error_mark_node)
8531 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8532 DECL_ARTIFICIAL (fco) = 1;
8533 /* Give the object parameter a different name. */
8534 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8537 finish_member_declaration (fco);
8539 obstack_free (&declarator_obstack, p);
8541 return (fco != error_mark_node);
8545 /* Parse the body of a lambda expression, which is simply
8547 compound-statement
8549 but which requires special handling.
8550 LAMBDA_EXPR is the current representation of the lambda expression. */
8552 static void
8553 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8555 bool nested = (current_function_decl != NULL_TREE);
8556 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8557 if (nested)
8558 push_function_context ();
8559 else
8560 /* Still increment function_depth so that we don't GC in the
8561 middle of an expression. */
8562 ++function_depth;
8563 /* Clear this in case we're in the middle of a default argument. */
8564 parser->local_variables_forbidden_p = false;
8566 /* Finish the function call operator
8567 - class_specifier
8568 + late_parsing_for_member
8569 + function_definition_after_declarator
8570 + ctor_initializer_opt_and_function_body */
8572 tree fco = lambda_function (lambda_expr);
8573 tree body;
8574 bool done = false;
8575 tree compound_stmt;
8576 tree cap;
8578 /* Let the front end know that we are going to be defining this
8579 function. */
8580 start_preparsed_function (fco,
8581 NULL_TREE,
8582 SF_PRE_PARSED | SF_INCLASS_INLINE);
8584 start_lambda_scope (fco);
8585 body = begin_function_body ();
8587 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8588 goto out;
8590 /* Push the proxies for any explicit captures. */
8591 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8592 cap = TREE_CHAIN (cap))
8593 build_capture_proxy (TREE_PURPOSE (cap));
8595 compound_stmt = begin_compound_stmt (0);
8597 /* 5.1.1.4 of the standard says:
8598 If a lambda-expression does not include a trailing-return-type, it
8599 is as if the trailing-return-type denotes the following type:
8600 * if the compound-statement is of the form
8601 { return attribute-specifier [opt] expression ; }
8602 the type of the returned expression after lvalue-to-rvalue
8603 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8604 (_conv.array_ 4.2), and function-to-pointer conversion
8605 (_conv.func_ 4.3);
8606 * otherwise, void. */
8608 /* In a lambda that has neither a lambda-return-type-clause
8609 nor a deducible form, errors should be reported for return statements
8610 in the body. Since we used void as the placeholder return type, parsing
8611 the body as usual will give such desired behavior. */
8612 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8613 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8614 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8616 tree expr = NULL_TREE;
8617 cp_id_kind idk = CP_ID_KIND_NONE;
8619 /* Parse tentatively in case there's more after the initial return
8620 statement. */
8621 cp_parser_parse_tentatively (parser);
8623 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8625 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8627 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8628 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8630 if (cp_parser_parse_definitely (parser))
8632 if (!processing_template_decl)
8633 apply_deduced_return_type (fco, lambda_return_type (expr));
8635 /* Will get error here if type not deduced yet. */
8636 finish_return_stmt (expr);
8638 done = true;
8642 if (!done)
8644 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8645 cp_parser_label_declaration (parser);
8646 cp_parser_statement_seq_opt (parser, NULL_TREE);
8647 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8650 finish_compound_stmt (compound_stmt);
8652 out:
8653 finish_function_body (body);
8654 finish_lambda_scope ();
8656 /* Finish the function and generate code for it if necessary. */
8657 expand_or_defer_fn (finish_function (/*inline*/2));
8660 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8661 if (nested)
8662 pop_function_context();
8663 else
8664 --function_depth;
8667 /* Statements [gram.stmt.stmt] */
8669 /* Parse a statement.
8671 statement:
8672 labeled-statement
8673 expression-statement
8674 compound-statement
8675 selection-statement
8676 iteration-statement
8677 jump-statement
8678 declaration-statement
8679 try-block
8681 C++11:
8683 statement:
8684 labeled-statement
8685 attribute-specifier-seq (opt) expression-statement
8686 attribute-specifier-seq (opt) compound-statement
8687 attribute-specifier-seq (opt) selection-statement
8688 attribute-specifier-seq (opt) iteration-statement
8689 attribute-specifier-seq (opt) jump-statement
8690 declaration-statement
8691 attribute-specifier-seq (opt) try-block
8693 TM Extension:
8695 statement:
8696 atomic-statement
8698 IN_COMPOUND is true when the statement is nested inside a
8699 cp_parser_compound_statement; this matters for certain pragmas.
8701 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8702 is a (possibly labeled) if statement which is not enclosed in braces
8703 and has an else clause. This is used to implement -Wparentheses. */
8705 static void
8706 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8707 bool in_compound, bool *if_p)
8709 tree statement, std_attrs = NULL_TREE;
8710 cp_token *token;
8711 location_t statement_location, attrs_location;
8713 restart:
8714 if (if_p != NULL)
8715 *if_p = false;
8716 /* There is no statement yet. */
8717 statement = NULL_TREE;
8719 cp_lexer_save_tokens (parser->lexer);
8720 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
8721 if (c_dialect_objc ())
8722 /* In obj-c++, seing '[[' might be the either the beginning of
8723 c++11 attributes, or a nested objc-message-expression. So
8724 let's parse the c++11 attributes tentatively. */
8725 cp_parser_parse_tentatively (parser);
8726 std_attrs = cp_parser_std_attribute_spec_seq (parser);
8727 if (c_dialect_objc ())
8729 if (!cp_parser_parse_definitely (parser))
8730 std_attrs = NULL_TREE;
8733 /* Peek at the next token. */
8734 token = cp_lexer_peek_token (parser->lexer);
8735 /* Remember the location of the first token in the statement. */
8736 statement_location = token->location;
8737 /* If this is a keyword, then that will often determine what kind of
8738 statement we have. */
8739 if (token->type == CPP_KEYWORD)
8741 enum rid keyword = token->keyword;
8743 switch (keyword)
8745 case RID_CASE:
8746 case RID_DEFAULT:
8747 /* Looks like a labeled-statement with a case label.
8748 Parse the label, and then use tail recursion to parse
8749 the statement. */
8750 cp_parser_label_for_labeled_statement (parser, std_attrs);
8751 goto restart;
8753 case RID_IF:
8754 case RID_SWITCH:
8755 statement = cp_parser_selection_statement (parser, if_p);
8756 break;
8758 case RID_WHILE:
8759 case RID_DO:
8760 case RID_FOR:
8761 statement = cp_parser_iteration_statement (parser);
8762 break;
8764 case RID_BREAK:
8765 case RID_CONTINUE:
8766 case RID_RETURN:
8767 case RID_GOTO:
8768 statement = cp_parser_jump_statement (parser);
8769 break;
8771 /* Objective-C++ exception-handling constructs. */
8772 case RID_AT_TRY:
8773 case RID_AT_CATCH:
8774 case RID_AT_FINALLY:
8775 case RID_AT_SYNCHRONIZED:
8776 case RID_AT_THROW:
8777 statement = cp_parser_objc_statement (parser);
8778 break;
8780 case RID_TRY:
8781 statement = cp_parser_try_block (parser);
8782 break;
8784 case RID_NAMESPACE:
8785 /* This must be a namespace alias definition. */
8786 cp_parser_declaration_statement (parser);
8787 return;
8789 case RID_TRANSACTION_ATOMIC:
8790 case RID_TRANSACTION_RELAXED:
8791 statement = cp_parser_transaction (parser, keyword);
8792 break;
8793 case RID_TRANSACTION_CANCEL:
8794 statement = cp_parser_transaction_cancel (parser);
8795 break;
8797 default:
8798 /* It might be a keyword like `int' that can start a
8799 declaration-statement. */
8800 break;
8803 else if (token->type == CPP_NAME)
8805 /* If the next token is a `:', then we are looking at a
8806 labeled-statement. */
8807 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8808 if (token->type == CPP_COLON)
8810 /* Looks like a labeled-statement with an ordinary label.
8811 Parse the label, and then use tail recursion to parse
8812 the statement. */
8814 cp_parser_label_for_labeled_statement (parser, std_attrs);
8815 goto restart;
8818 /* Anything that starts with a `{' must be a compound-statement. */
8819 else if (token->type == CPP_OPEN_BRACE)
8820 statement = cp_parser_compound_statement (parser, NULL, false, false);
8821 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8822 a statement all its own. */
8823 else if (token->type == CPP_PRAGMA)
8825 /* Only certain OpenMP pragmas are attached to statements, and thus
8826 are considered statements themselves. All others are not. In
8827 the context of a compound, accept the pragma as a "statement" and
8828 return so that we can check for a close brace. Otherwise we
8829 require a real statement and must go back and read one. */
8830 if (in_compound)
8831 cp_parser_pragma (parser, pragma_compound);
8832 else if (!cp_parser_pragma (parser, pragma_stmt))
8833 goto restart;
8834 return;
8836 else if (token->type == CPP_EOF)
8838 cp_parser_error (parser, "expected statement");
8839 return;
8842 /* Everything else must be a declaration-statement or an
8843 expression-statement. Try for the declaration-statement
8844 first, unless we are looking at a `;', in which case we know that
8845 we have an expression-statement. */
8846 if (!statement)
8848 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8850 if (std_attrs != NULL_TREE)
8852 /* Attributes should be parsed as part of the the
8853 declaration, so let's un-parse them. */
8854 cp_lexer_rollback_tokens (parser->lexer);
8855 std_attrs = NULL_TREE;
8858 cp_parser_parse_tentatively (parser);
8859 /* Try to parse the declaration-statement. */
8860 cp_parser_declaration_statement (parser);
8861 /* If that worked, we're done. */
8862 if (cp_parser_parse_definitely (parser))
8863 return;
8865 /* Look for an expression-statement instead. */
8866 statement = cp_parser_expression_statement (parser, in_statement_expr);
8869 /* Set the line number for the statement. */
8870 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8871 SET_EXPR_LOCATION (statement, statement_location);
8873 /* Note that for now, we don't do anything with c++11 statements
8874 parsed at this level. */
8875 if (std_attrs != NULL_TREE)
8876 warning_at (attrs_location,
8877 OPT_Wattributes,
8878 "attributes at the beginning of statement are ignored");
8881 /* Parse the label for a labeled-statement, i.e.
8883 identifier :
8884 case constant-expression :
8885 default :
8887 GNU Extension:
8888 case constant-expression ... constant-expression : statement
8890 When a label is parsed without errors, the label is added to the
8891 parse tree by the finish_* functions, so this function doesn't
8892 have to return the label. */
8894 static void
8895 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
8897 cp_token *token;
8898 tree label = NULL_TREE;
8899 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8901 /* The next token should be an identifier. */
8902 token = cp_lexer_peek_token (parser->lexer);
8903 if (token->type != CPP_NAME
8904 && token->type != CPP_KEYWORD)
8906 cp_parser_error (parser, "expected labeled-statement");
8907 return;
8910 parser->colon_corrects_to_scope_p = false;
8911 switch (token->keyword)
8913 case RID_CASE:
8915 tree expr, expr_hi;
8916 cp_token *ellipsis;
8918 /* Consume the `case' token. */
8919 cp_lexer_consume_token (parser->lexer);
8920 /* Parse the constant-expression. */
8921 expr = cp_parser_constant_expression (parser,
8922 /*allow_non_constant_p=*/false,
8923 NULL);
8925 ellipsis = cp_lexer_peek_token (parser->lexer);
8926 if (ellipsis->type == CPP_ELLIPSIS)
8928 /* Consume the `...' token. */
8929 cp_lexer_consume_token (parser->lexer);
8930 expr_hi =
8931 cp_parser_constant_expression (parser,
8932 /*allow_non_constant_p=*/false,
8933 NULL);
8934 /* We don't need to emit warnings here, as the common code
8935 will do this for us. */
8937 else
8938 expr_hi = NULL_TREE;
8940 if (parser->in_switch_statement_p)
8941 finish_case_label (token->location, expr, expr_hi);
8942 else
8943 error_at (token->location,
8944 "case label %qE not within a switch statement",
8945 expr);
8947 break;
8949 case RID_DEFAULT:
8950 /* Consume the `default' token. */
8951 cp_lexer_consume_token (parser->lexer);
8953 if (parser->in_switch_statement_p)
8954 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8955 else
8956 error_at (token->location, "case label not within a switch statement");
8957 break;
8959 default:
8960 /* Anything else must be an ordinary label. */
8961 label = finish_label_stmt (cp_parser_identifier (parser));
8962 break;
8965 /* Require the `:' token. */
8966 cp_parser_require (parser, CPP_COLON, RT_COLON);
8968 /* An ordinary label may optionally be followed by attributes.
8969 However, this is only permitted if the attributes are then
8970 followed by a semicolon. This is because, for backward
8971 compatibility, when parsing
8972 lab: __attribute__ ((unused)) int i;
8973 we want the attribute to attach to "i", not "lab". */
8974 if (label != NULL_TREE
8975 && cp_next_tokens_can_be_gnu_attribute_p (parser))
8977 tree attrs;
8978 cp_parser_parse_tentatively (parser);
8979 attrs = cp_parser_gnu_attributes_opt (parser);
8980 if (attrs == NULL_TREE
8981 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8982 cp_parser_abort_tentative_parse (parser);
8983 else if (!cp_parser_parse_definitely (parser))
8985 else
8986 attributes = chainon (attributes, attrs);
8989 if (attributes != NULL_TREE)
8990 cplus_decl_attributes (&label, attributes, 0);
8992 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8995 /* Parse an expression-statement.
8997 expression-statement:
8998 expression [opt] ;
9000 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9001 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9002 indicates whether this expression-statement is part of an
9003 expression statement. */
9005 static tree
9006 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9008 tree statement = NULL_TREE;
9009 cp_token *token = cp_lexer_peek_token (parser->lexer);
9011 /* If the next token is a ';', then there is no expression
9012 statement. */
9013 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9014 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9016 /* Give a helpful message for "A<T>::type t;" and the like. */
9017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9018 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9020 if (TREE_CODE (statement) == SCOPE_REF)
9021 error_at (token->location, "need %<typename%> before %qE because "
9022 "%qT is a dependent scope",
9023 statement, TREE_OPERAND (statement, 0));
9024 else if (is_overloaded_fn (statement)
9025 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9027 /* A::A a; */
9028 tree fn = get_first_fn (statement);
9029 error_at (token->location,
9030 "%<%T::%D%> names the constructor, not the type",
9031 DECL_CONTEXT (fn), DECL_NAME (fn));
9035 /* Consume the final `;'. */
9036 cp_parser_consume_semicolon_at_end_of_statement (parser);
9038 if (in_statement_expr
9039 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9040 /* This is the final expression statement of a statement
9041 expression. */
9042 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9043 else if (statement)
9044 statement = finish_expr_stmt (statement);
9045 else
9046 finish_stmt ();
9048 return statement;
9051 /* Parse a compound-statement.
9053 compound-statement:
9054 { statement-seq [opt] }
9056 GNU extension:
9058 compound-statement:
9059 { label-declaration-seq [opt] statement-seq [opt] }
9061 label-declaration-seq:
9062 label-declaration
9063 label-declaration-seq label-declaration
9065 Returns a tree representing the statement. */
9067 static tree
9068 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9069 bool in_try, bool function_body)
9071 tree compound_stmt;
9073 /* Consume the `{'. */
9074 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9075 return error_mark_node;
9076 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9077 && !function_body)
9078 pedwarn (input_location, OPT_Wpedantic,
9079 "compound-statement in constexpr function");
9080 /* Begin the compound-statement. */
9081 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9082 /* If the next keyword is `__label__' we have a label declaration. */
9083 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9084 cp_parser_label_declaration (parser);
9085 /* Parse an (optional) statement-seq. */
9086 cp_parser_statement_seq_opt (parser, in_statement_expr);
9087 /* Finish the compound-statement. */
9088 finish_compound_stmt (compound_stmt);
9089 /* Consume the `}'. */
9090 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9092 return compound_stmt;
9095 /* Parse an (optional) statement-seq.
9097 statement-seq:
9098 statement
9099 statement-seq [opt] statement */
9101 static void
9102 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9104 /* Scan statements until there aren't any more. */
9105 while (true)
9107 cp_token *token = cp_lexer_peek_token (parser->lexer);
9109 /* If we are looking at a `}', then we have run out of
9110 statements; the same is true if we have reached the end
9111 of file, or have stumbled upon a stray '@end'. */
9112 if (token->type == CPP_CLOSE_BRACE
9113 || token->type == CPP_EOF
9114 || token->type == CPP_PRAGMA_EOL
9115 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9116 break;
9118 /* If we are in a compound statement and find 'else' then
9119 something went wrong. */
9120 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9122 if (parser->in_statement & IN_IF_STMT)
9123 break;
9124 else
9126 token = cp_lexer_consume_token (parser->lexer);
9127 error_at (token->location, "%<else%> without a previous %<if%>");
9131 /* Parse the statement. */
9132 cp_parser_statement (parser, in_statement_expr, true, NULL);
9136 /* Parse a selection-statement.
9138 selection-statement:
9139 if ( condition ) statement
9140 if ( condition ) statement else statement
9141 switch ( condition ) statement
9143 Returns the new IF_STMT or SWITCH_STMT.
9145 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9146 is a (possibly labeled) if statement which is not enclosed in
9147 braces and has an else clause. This is used to implement
9148 -Wparentheses. */
9150 static tree
9151 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9153 cp_token *token;
9154 enum rid keyword;
9156 if (if_p != NULL)
9157 *if_p = false;
9159 /* Peek at the next token. */
9160 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9162 /* See what kind of keyword it is. */
9163 keyword = token->keyword;
9164 switch (keyword)
9166 case RID_IF:
9167 case RID_SWITCH:
9169 tree statement;
9170 tree condition;
9172 /* Look for the `('. */
9173 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9175 cp_parser_skip_to_end_of_statement (parser);
9176 return error_mark_node;
9179 /* Begin the selection-statement. */
9180 if (keyword == RID_IF)
9181 statement = begin_if_stmt ();
9182 else
9183 statement = begin_switch_stmt ();
9185 /* Parse the condition. */
9186 condition = cp_parser_condition (parser);
9187 /* Look for the `)'. */
9188 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9189 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9190 /*consume_paren=*/true);
9192 if (keyword == RID_IF)
9194 bool nested_if;
9195 unsigned char in_statement;
9197 /* Add the condition. */
9198 finish_if_stmt_cond (condition, statement);
9200 /* Parse the then-clause. */
9201 in_statement = parser->in_statement;
9202 parser->in_statement |= IN_IF_STMT;
9203 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9205 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9206 add_stmt (build_empty_stmt (loc));
9207 cp_lexer_consume_token (parser->lexer);
9208 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9209 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9210 "empty body in an %<if%> statement");
9211 nested_if = false;
9213 else
9214 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9215 parser->in_statement = in_statement;
9217 finish_then_clause (statement);
9219 /* If the next token is `else', parse the else-clause. */
9220 if (cp_lexer_next_token_is_keyword (parser->lexer,
9221 RID_ELSE))
9223 /* Consume the `else' keyword. */
9224 cp_lexer_consume_token (parser->lexer);
9225 begin_else_clause (statement);
9226 /* Parse the else-clause. */
9227 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9229 location_t loc;
9230 loc = cp_lexer_peek_token (parser->lexer)->location;
9231 warning_at (loc,
9232 OPT_Wempty_body, "suggest braces around "
9233 "empty body in an %<else%> statement");
9234 add_stmt (build_empty_stmt (loc));
9235 cp_lexer_consume_token (parser->lexer);
9237 else
9238 cp_parser_implicitly_scoped_statement (parser, NULL);
9240 finish_else_clause (statement);
9242 /* If we are currently parsing a then-clause, then
9243 IF_P will not be NULL. We set it to true to
9244 indicate that this if statement has an else clause.
9245 This may trigger the Wparentheses warning below
9246 when we get back up to the parent if statement. */
9247 if (if_p != NULL)
9248 *if_p = true;
9250 else
9252 /* This if statement does not have an else clause. If
9253 NESTED_IF is true, then the then-clause is an if
9254 statement which does have an else clause. We warn
9255 about the potential ambiguity. */
9256 if (nested_if)
9257 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9258 "suggest explicit braces to avoid ambiguous"
9259 " %<else%>");
9262 /* Now we're all done with the if-statement. */
9263 finish_if_stmt (statement);
9265 else
9267 bool in_switch_statement_p;
9268 unsigned char in_statement;
9270 /* Add the condition. */
9271 finish_switch_cond (condition, statement);
9273 /* Parse the body of the switch-statement. */
9274 in_switch_statement_p = parser->in_switch_statement_p;
9275 in_statement = parser->in_statement;
9276 parser->in_switch_statement_p = true;
9277 parser->in_statement |= IN_SWITCH_STMT;
9278 cp_parser_implicitly_scoped_statement (parser, NULL);
9279 parser->in_switch_statement_p = in_switch_statement_p;
9280 parser->in_statement = in_statement;
9282 /* Now we're all done with the switch-statement. */
9283 finish_switch_stmt (statement);
9286 return statement;
9288 break;
9290 default:
9291 cp_parser_error (parser, "expected selection-statement");
9292 return error_mark_node;
9296 /* Parse a condition.
9298 condition:
9299 expression
9300 type-specifier-seq declarator = initializer-clause
9301 type-specifier-seq declarator braced-init-list
9303 GNU Extension:
9305 condition:
9306 type-specifier-seq declarator asm-specification [opt]
9307 attributes [opt] = assignment-expression
9309 Returns the expression that should be tested. */
9311 static tree
9312 cp_parser_condition (cp_parser* parser)
9314 cp_decl_specifier_seq type_specifiers;
9315 const char *saved_message;
9316 int declares_class_or_enum;
9318 /* Try the declaration first. */
9319 cp_parser_parse_tentatively (parser);
9320 /* New types are not allowed in the type-specifier-seq for a
9321 condition. */
9322 saved_message = parser->type_definition_forbidden_message;
9323 parser->type_definition_forbidden_message
9324 = G_("types may not be defined in conditions");
9325 /* Parse the type-specifier-seq. */
9326 cp_parser_decl_specifier_seq (parser,
9327 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9328 &type_specifiers,
9329 &declares_class_or_enum);
9330 /* Restore the saved message. */
9331 parser->type_definition_forbidden_message = saved_message;
9332 /* If all is well, we might be looking at a declaration. */
9333 if (!cp_parser_error_occurred (parser))
9335 tree decl;
9336 tree asm_specification;
9337 tree attributes;
9338 cp_declarator *declarator;
9339 tree initializer = NULL_TREE;
9341 /* Parse the declarator. */
9342 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9343 /*ctor_dtor_or_conv_p=*/NULL,
9344 /*parenthesized_p=*/NULL,
9345 /*member_p=*/false);
9346 /* Parse the attributes. */
9347 attributes = cp_parser_attributes_opt (parser);
9348 /* Parse the asm-specification. */
9349 asm_specification = cp_parser_asm_specification_opt (parser);
9350 /* If the next token is not an `=' or '{', then we might still be
9351 looking at an expression. For example:
9353 if (A(a).x)
9355 looks like a decl-specifier-seq and a declarator -- but then
9356 there is no `=', so this is an expression. */
9357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9358 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9359 cp_parser_simulate_error (parser);
9361 /* If we did see an `=' or '{', then we are looking at a declaration
9362 for sure. */
9363 if (cp_parser_parse_definitely (parser))
9365 tree pushed_scope;
9366 bool non_constant_p;
9367 bool flags = LOOKUP_ONLYCONVERTING;
9369 /* Create the declaration. */
9370 decl = start_decl (declarator, &type_specifiers,
9371 /*initialized_p=*/true,
9372 attributes, /*prefix_attributes=*/NULL_TREE,
9373 &pushed_scope);
9375 /* Parse the initializer. */
9376 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9378 initializer = cp_parser_braced_list (parser, &non_constant_p);
9379 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9380 flags = 0;
9382 else
9384 /* Consume the `='. */
9385 cp_parser_require (parser, CPP_EQ, RT_EQ);
9386 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9388 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9389 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9391 /* Process the initializer. */
9392 cp_finish_decl (decl,
9393 initializer, !non_constant_p,
9394 asm_specification,
9395 flags);
9397 if (pushed_scope)
9398 pop_scope (pushed_scope);
9400 return convert_from_reference (decl);
9403 /* If we didn't even get past the declarator successfully, we are
9404 definitely not looking at a declaration. */
9405 else
9406 cp_parser_abort_tentative_parse (parser);
9408 /* Otherwise, we are looking at an expression. */
9409 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9412 /* Parses a for-statement or range-for-statement until the closing ')',
9413 not included. */
9415 static tree
9416 cp_parser_for (cp_parser *parser)
9418 tree init, scope, decl;
9419 bool is_range_for;
9421 /* Begin the for-statement. */
9422 scope = begin_for_scope (&init);
9424 /* Parse the initialization. */
9425 is_range_for = cp_parser_for_init_statement (parser, &decl);
9427 if (is_range_for)
9428 return cp_parser_range_for (parser, scope, init, decl);
9429 else
9430 return cp_parser_c_for (parser, scope, init);
9433 static tree
9434 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9436 /* Normal for loop */
9437 tree condition = NULL_TREE;
9438 tree expression = NULL_TREE;
9439 tree stmt;
9441 stmt = begin_for_stmt (scope, init);
9442 /* The for-init-statement has already been parsed in
9443 cp_parser_for_init_statement, so no work is needed here. */
9444 finish_for_init_stmt (stmt);
9446 /* If there's a condition, process it. */
9447 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9448 condition = cp_parser_condition (parser);
9449 finish_for_cond (condition, stmt);
9450 /* Look for the `;'. */
9451 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9453 /* If there's an expression, process it. */
9454 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9455 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9456 finish_for_expr (expression, stmt);
9458 return stmt;
9461 /* Tries to parse a range-based for-statement:
9463 range-based-for:
9464 decl-specifier-seq declarator : expression
9466 The decl-specifier-seq declarator and the `:' are already parsed by
9467 cp_parser_for_init_statement. If processing_template_decl it returns a
9468 newly created RANGE_FOR_STMT; if not, it is converted to a
9469 regular FOR_STMT. */
9471 static tree
9472 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9474 tree stmt, range_expr;
9476 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9478 bool expr_non_constant_p;
9479 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9481 else
9482 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9484 /* If in template, STMT is converted to a normal for-statement
9485 at instantiation. If not, it is done just ahead. */
9486 if (processing_template_decl)
9488 if (check_for_bare_parameter_packs (range_expr))
9489 range_expr = error_mark_node;
9490 stmt = begin_range_for_stmt (scope, init);
9491 finish_range_for_decl (stmt, range_decl, range_expr);
9492 if (!type_dependent_expression_p (range_expr)
9493 /* do_auto_deduction doesn't mess with template init-lists. */
9494 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9495 do_range_for_auto_deduction (range_decl, range_expr);
9497 else
9499 stmt = begin_for_stmt (scope, init);
9500 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9502 return stmt;
9505 /* Subroutine of cp_convert_range_for: given the initializer expression,
9506 builds up the range temporary. */
9508 static tree
9509 build_range_temp (tree range_expr)
9511 tree range_type, range_temp;
9513 /* Find out the type deduced by the declaration
9514 `auto &&__range = range_expr'. */
9515 range_type = cp_build_reference_type (make_auto (), true);
9516 range_type = do_auto_deduction (range_type, range_expr,
9517 type_uses_auto (range_type));
9519 /* Create the __range variable. */
9520 range_temp = build_decl (input_location, VAR_DECL,
9521 get_identifier ("__for_range"), range_type);
9522 TREE_USED (range_temp) = 1;
9523 DECL_ARTIFICIAL (range_temp) = 1;
9525 return range_temp;
9528 /* Used by cp_parser_range_for in template context: we aren't going to
9529 do a full conversion yet, but we still need to resolve auto in the
9530 type of the for-range-declaration if present. This is basically
9531 a shortcut version of cp_convert_range_for. */
9533 static void
9534 do_range_for_auto_deduction (tree decl, tree range_expr)
9536 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9537 if (auto_node)
9539 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9540 range_temp = convert_from_reference (build_range_temp (range_expr));
9541 iter_type = (cp_parser_perform_range_for_lookup
9542 (range_temp, &begin_dummy, &end_dummy));
9543 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9544 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9545 tf_warning_or_error);
9546 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9547 iter_decl, auto_node);
9551 /* Converts a range-based for-statement into a normal
9552 for-statement, as per the definition.
9554 for (RANGE_DECL : RANGE_EXPR)
9555 BLOCK
9557 should be equivalent to:
9560 auto &&__range = RANGE_EXPR;
9561 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9562 __begin != __end;
9563 ++__begin)
9565 RANGE_DECL = *__begin;
9566 BLOCK
9570 If RANGE_EXPR is an array:
9571 BEGIN_EXPR = __range
9572 END_EXPR = __range + ARRAY_SIZE(__range)
9573 Else if RANGE_EXPR has a member 'begin' or 'end':
9574 BEGIN_EXPR = __range.begin()
9575 END_EXPR = __range.end()
9576 Else:
9577 BEGIN_EXPR = begin(__range)
9578 END_EXPR = end(__range);
9580 If __range has a member 'begin' but not 'end', or vice versa, we must
9581 still use the second alternative (it will surely fail, however).
9582 When calling begin()/end() in the third alternative we must use
9583 argument dependent lookup, but always considering 'std' as an associated
9584 namespace. */
9586 tree
9587 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9589 tree begin, end;
9590 tree iter_type, begin_expr, end_expr;
9591 tree condition, expression;
9593 if (range_decl == error_mark_node || range_expr == error_mark_node)
9594 /* If an error happened previously do nothing or else a lot of
9595 unhelpful errors would be issued. */
9596 begin_expr = end_expr = iter_type = error_mark_node;
9597 else
9599 tree range_temp = build_range_temp (range_expr);
9600 pushdecl (range_temp);
9601 cp_finish_decl (range_temp, range_expr,
9602 /*is_constant_init*/false, NULL_TREE,
9603 LOOKUP_ONLYCONVERTING);
9605 range_temp = convert_from_reference (range_temp);
9606 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9607 &begin_expr, &end_expr);
9610 /* The new for initialization statement. */
9611 begin = build_decl (input_location, VAR_DECL,
9612 get_identifier ("__for_begin"), iter_type);
9613 TREE_USED (begin) = 1;
9614 DECL_ARTIFICIAL (begin) = 1;
9615 pushdecl (begin);
9616 cp_finish_decl (begin, begin_expr,
9617 /*is_constant_init*/false, NULL_TREE,
9618 LOOKUP_ONLYCONVERTING);
9620 end = build_decl (input_location, VAR_DECL,
9621 get_identifier ("__for_end"), iter_type);
9622 TREE_USED (end) = 1;
9623 DECL_ARTIFICIAL (end) = 1;
9624 pushdecl (end);
9625 cp_finish_decl (end, end_expr,
9626 /*is_constant_init*/false, NULL_TREE,
9627 LOOKUP_ONLYCONVERTING);
9629 finish_for_init_stmt (statement);
9631 /* The new for condition. */
9632 condition = build_x_binary_op (input_location, NE_EXPR,
9633 begin, ERROR_MARK,
9634 end, ERROR_MARK,
9635 NULL, tf_warning_or_error);
9636 finish_for_cond (condition, statement);
9638 /* The new increment expression. */
9639 expression = finish_unary_op_expr (input_location,
9640 PREINCREMENT_EXPR, begin);
9641 finish_for_expr (expression, statement);
9643 /* The declaration is initialized with *__begin inside the loop body. */
9644 cp_finish_decl (range_decl,
9645 build_x_indirect_ref (input_location, begin, RO_NULL,
9646 tf_warning_or_error),
9647 /*is_constant_init*/false, NULL_TREE,
9648 LOOKUP_ONLYCONVERTING);
9650 return statement;
9653 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9654 We need to solve both at the same time because the method used
9655 depends on the existence of members begin or end.
9656 Returns the type deduced for the iterator expression. */
9658 static tree
9659 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9661 if (error_operand_p (range))
9663 *begin = *end = error_mark_node;
9664 return error_mark_node;
9667 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9669 error ("range-based %<for%> expression of type %qT "
9670 "has incomplete type", TREE_TYPE (range));
9671 *begin = *end = error_mark_node;
9672 return error_mark_node;
9674 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9676 /* If RANGE is an array, we will use pointer arithmetic. */
9677 *begin = range;
9678 *end = build_binary_op (input_location, PLUS_EXPR,
9679 range,
9680 array_type_nelts_top (TREE_TYPE (range)),
9682 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9684 else
9686 /* If it is not an array, we must do a bit of magic. */
9687 tree id_begin, id_end;
9688 tree member_begin, member_end;
9690 *begin = *end = error_mark_node;
9692 id_begin = get_identifier ("begin");
9693 id_end = get_identifier ("end");
9694 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9695 /*protect=*/2, /*want_type=*/false,
9696 tf_warning_or_error);
9697 member_end = lookup_member (TREE_TYPE (range), id_end,
9698 /*protect=*/2, /*want_type=*/false,
9699 tf_warning_or_error);
9701 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9703 /* Use the member functions. */
9704 if (member_begin != NULL_TREE)
9705 *begin = cp_parser_range_for_member_function (range, id_begin);
9706 else
9707 error ("range-based %<for%> expression of type %qT has an "
9708 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9710 if (member_end != NULL_TREE)
9711 *end = cp_parser_range_for_member_function (range, id_end);
9712 else
9713 error ("range-based %<for%> expression of type %qT has a "
9714 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9716 else
9718 /* Use global functions with ADL. */
9719 VEC(tree,gc) *vec;
9720 vec = make_tree_vector ();
9722 VEC_safe_push (tree, gc, vec, range);
9724 member_begin = perform_koenig_lookup (id_begin, vec,
9725 /*include_std=*/true,
9726 tf_warning_or_error);
9727 *begin = finish_call_expr (member_begin, &vec, false, true,
9728 tf_warning_or_error);
9729 member_end = perform_koenig_lookup (id_end, vec,
9730 /*include_std=*/true,
9731 tf_warning_or_error);
9732 *end = finish_call_expr (member_end, &vec, false, true,
9733 tf_warning_or_error);
9735 release_tree_vector (vec);
9738 /* Last common checks. */
9739 if (*begin == error_mark_node || *end == error_mark_node)
9741 /* If one of the expressions is an error do no more checks. */
9742 *begin = *end = error_mark_node;
9743 return error_mark_node;
9745 else
9747 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9748 /* The unqualified type of the __begin and __end temporaries should
9749 be the same, as required by the multiple auto declaration. */
9750 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9751 error ("inconsistent begin/end types in range-based %<for%> "
9752 "statement: %qT and %qT",
9753 TREE_TYPE (*begin), TREE_TYPE (*end));
9754 return iter_type;
9759 /* Helper function for cp_parser_perform_range_for_lookup.
9760 Builds a tree for RANGE.IDENTIFIER(). */
9762 static tree
9763 cp_parser_range_for_member_function (tree range, tree identifier)
9765 tree member, res;
9766 VEC(tree,gc) *vec;
9768 member = finish_class_member_access_expr (range, identifier,
9769 false, tf_warning_or_error);
9770 if (member == error_mark_node)
9771 return error_mark_node;
9773 vec = make_tree_vector ();
9774 res = finish_call_expr (member, &vec,
9775 /*disallow_virtual=*/false,
9776 /*koenig_p=*/false,
9777 tf_warning_or_error);
9778 release_tree_vector (vec);
9779 return res;
9782 /* Parse an iteration-statement.
9784 iteration-statement:
9785 while ( condition ) statement
9786 do statement while ( expression ) ;
9787 for ( for-init-statement condition [opt] ; expression [opt] )
9788 statement
9790 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9792 static tree
9793 cp_parser_iteration_statement (cp_parser* parser)
9795 cp_token *token;
9796 enum rid keyword;
9797 tree statement;
9798 unsigned char in_statement;
9800 /* Peek at the next token. */
9801 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9802 if (!token)
9803 return error_mark_node;
9805 /* Remember whether or not we are already within an iteration
9806 statement. */
9807 in_statement = parser->in_statement;
9809 /* See what kind of keyword it is. */
9810 keyword = token->keyword;
9811 switch (keyword)
9813 case RID_WHILE:
9815 tree condition;
9817 /* Begin the while-statement. */
9818 statement = begin_while_stmt ();
9819 /* Look for the `('. */
9820 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9821 /* Parse the condition. */
9822 condition = cp_parser_condition (parser);
9823 finish_while_stmt_cond (condition, statement);
9824 /* Look for the `)'. */
9825 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9826 /* Parse the dependent statement. */
9827 parser->in_statement = IN_ITERATION_STMT;
9828 cp_parser_already_scoped_statement (parser);
9829 parser->in_statement = in_statement;
9830 /* We're done with the while-statement. */
9831 finish_while_stmt (statement);
9833 break;
9835 case RID_DO:
9837 tree expression;
9839 /* Begin the do-statement. */
9840 statement = begin_do_stmt ();
9841 /* Parse the body of the do-statement. */
9842 parser->in_statement = IN_ITERATION_STMT;
9843 cp_parser_implicitly_scoped_statement (parser, NULL);
9844 parser->in_statement = in_statement;
9845 finish_do_body (statement);
9846 /* Look for the `while' keyword. */
9847 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9848 /* Look for the `('. */
9849 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9850 /* Parse the expression. */
9851 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9852 /* We're done with the do-statement. */
9853 finish_do_stmt (expression, statement);
9854 /* Look for the `)'. */
9855 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9856 /* Look for the `;'. */
9857 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9859 break;
9861 case RID_FOR:
9863 /* Look for the `('. */
9864 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9866 statement = cp_parser_for (parser);
9868 /* Look for the `)'. */
9869 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9871 /* Parse the body of the for-statement. */
9872 parser->in_statement = IN_ITERATION_STMT;
9873 cp_parser_already_scoped_statement (parser);
9874 parser->in_statement = in_statement;
9876 /* We're done with the for-statement. */
9877 finish_for_stmt (statement);
9879 break;
9881 default:
9882 cp_parser_error (parser, "expected iteration-statement");
9883 statement = error_mark_node;
9884 break;
9887 return statement;
9890 /* Parse a for-init-statement or the declarator of a range-based-for.
9891 Returns true if a range-based-for declaration is seen.
9893 for-init-statement:
9894 expression-statement
9895 simple-declaration */
9897 static bool
9898 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9900 /* If the next token is a `;', then we have an empty
9901 expression-statement. Grammatically, this is also a
9902 simple-declaration, but an invalid one, because it does not
9903 declare anything. Therefore, if we did not handle this case
9904 specially, we would issue an error message about an invalid
9905 declaration. */
9906 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9908 bool is_range_for = false;
9909 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9911 parser->colon_corrects_to_scope_p = false;
9913 /* We're going to speculatively look for a declaration, falling back
9914 to an expression, if necessary. */
9915 cp_parser_parse_tentatively (parser);
9916 /* Parse the declaration. */
9917 cp_parser_simple_declaration (parser,
9918 /*function_definition_allowed_p=*/false,
9919 decl);
9920 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9921 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9923 /* It is a range-for, consume the ':' */
9924 cp_lexer_consume_token (parser->lexer);
9925 is_range_for = true;
9926 if (cxx_dialect < cxx0x)
9928 error_at (cp_lexer_peek_token (parser->lexer)->location,
9929 "range-based %<for%> loops are not allowed "
9930 "in C++98 mode");
9931 *decl = error_mark_node;
9934 else
9935 /* The ';' is not consumed yet because we told
9936 cp_parser_simple_declaration not to. */
9937 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9939 if (cp_parser_parse_definitely (parser))
9940 return is_range_for;
9941 /* If the tentative parse failed, then we shall need to look for an
9942 expression-statement. */
9944 /* If we are here, it is an expression-statement. */
9945 cp_parser_expression_statement (parser, NULL_TREE);
9946 return false;
9949 /* Parse a jump-statement.
9951 jump-statement:
9952 break ;
9953 continue ;
9954 return expression [opt] ;
9955 return braced-init-list ;
9956 goto identifier ;
9958 GNU extension:
9960 jump-statement:
9961 goto * expression ;
9963 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9965 static tree
9966 cp_parser_jump_statement (cp_parser* parser)
9968 tree statement = error_mark_node;
9969 cp_token *token;
9970 enum rid keyword;
9971 unsigned char in_statement;
9973 /* Peek at the next token. */
9974 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9975 if (!token)
9976 return error_mark_node;
9978 /* See what kind of keyword it is. */
9979 keyword = token->keyword;
9980 switch (keyword)
9982 case RID_BREAK:
9983 in_statement = parser->in_statement & ~IN_IF_STMT;
9984 switch (in_statement)
9986 case 0:
9987 error_at (token->location, "break statement not within loop or switch");
9988 break;
9989 default:
9990 gcc_assert ((in_statement & IN_SWITCH_STMT)
9991 || in_statement == IN_ITERATION_STMT);
9992 statement = finish_break_stmt ();
9993 break;
9994 case IN_OMP_BLOCK:
9995 error_at (token->location, "invalid exit from OpenMP structured block");
9996 break;
9997 case IN_OMP_FOR:
9998 error_at (token->location, "break statement used with OpenMP for loop");
9999 break;
10001 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10002 break;
10004 case RID_CONTINUE:
10005 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10007 case 0:
10008 error_at (token->location, "continue statement not within a loop");
10009 break;
10010 case IN_ITERATION_STMT:
10011 case IN_OMP_FOR:
10012 statement = finish_continue_stmt ();
10013 break;
10014 case IN_OMP_BLOCK:
10015 error_at (token->location, "invalid exit from OpenMP structured block");
10016 break;
10017 default:
10018 gcc_unreachable ();
10020 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10021 break;
10023 case RID_RETURN:
10025 tree expr;
10026 bool expr_non_constant_p;
10028 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10030 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10031 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10033 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10034 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10035 else
10036 /* If the next token is a `;', then there is no
10037 expression. */
10038 expr = NULL_TREE;
10039 /* Build the return-statement. */
10040 statement = finish_return_stmt (expr);
10041 /* Look for the final `;'. */
10042 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10044 break;
10046 case RID_GOTO:
10047 /* Create the goto-statement. */
10048 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10050 /* Issue a warning about this use of a GNU extension. */
10051 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10052 /* Consume the '*' token. */
10053 cp_lexer_consume_token (parser->lexer);
10054 /* Parse the dependent expression. */
10055 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10057 else
10058 finish_goto_stmt (cp_parser_identifier (parser));
10059 /* Look for the final `;'. */
10060 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10061 break;
10063 default:
10064 cp_parser_error (parser, "expected jump-statement");
10065 break;
10068 return statement;
10071 /* Parse a declaration-statement.
10073 declaration-statement:
10074 block-declaration */
10076 static void
10077 cp_parser_declaration_statement (cp_parser* parser)
10079 void *p;
10081 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10082 p = obstack_alloc (&declarator_obstack, 0);
10084 /* Parse the block-declaration. */
10085 cp_parser_block_declaration (parser, /*statement_p=*/true);
10087 /* Free any declarators allocated. */
10088 obstack_free (&declarator_obstack, p);
10090 /* Finish off the statement. */
10091 finish_stmt ();
10094 /* Some dependent statements (like `if (cond) statement'), are
10095 implicitly in their own scope. In other words, if the statement is
10096 a single statement (as opposed to a compound-statement), it is
10097 none-the-less treated as if it were enclosed in braces. Any
10098 declarations appearing in the dependent statement are out of scope
10099 after control passes that point. This function parses a statement,
10100 but ensures that is in its own scope, even if it is not a
10101 compound-statement.
10103 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10104 is a (possibly labeled) if statement which is not enclosed in
10105 braces and has an else clause. This is used to implement
10106 -Wparentheses.
10108 Returns the new statement. */
10110 static tree
10111 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10113 tree statement;
10115 if (if_p != NULL)
10116 *if_p = false;
10118 /* Mark if () ; with a special NOP_EXPR. */
10119 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10121 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10122 cp_lexer_consume_token (parser->lexer);
10123 statement = add_stmt (build_empty_stmt (loc));
10125 /* if a compound is opened, we simply parse the statement directly. */
10126 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10127 statement = cp_parser_compound_statement (parser, NULL, false, false);
10128 /* If the token is not a `{', then we must take special action. */
10129 else
10131 /* Create a compound-statement. */
10132 statement = begin_compound_stmt (0);
10133 /* Parse the dependent-statement. */
10134 cp_parser_statement (parser, NULL_TREE, false, if_p);
10135 /* Finish the dummy compound-statement. */
10136 finish_compound_stmt (statement);
10139 /* Return the statement. */
10140 return statement;
10143 /* For some dependent statements (like `while (cond) statement'), we
10144 have already created a scope. Therefore, even if the dependent
10145 statement is a compound-statement, we do not want to create another
10146 scope. */
10148 static void
10149 cp_parser_already_scoped_statement (cp_parser* parser)
10151 /* If the token is a `{', then we must take special action. */
10152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10153 cp_parser_statement (parser, NULL_TREE, false, NULL);
10154 else
10156 /* Avoid calling cp_parser_compound_statement, so that we
10157 don't create a new scope. Do everything else by hand. */
10158 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10159 /* If the next keyword is `__label__' we have a label declaration. */
10160 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10161 cp_parser_label_declaration (parser);
10162 /* Parse an (optional) statement-seq. */
10163 cp_parser_statement_seq_opt (parser, NULL_TREE);
10164 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10168 /* Declarations [gram.dcl.dcl] */
10170 /* Parse an optional declaration-sequence.
10172 declaration-seq:
10173 declaration
10174 declaration-seq declaration */
10176 static void
10177 cp_parser_declaration_seq_opt (cp_parser* parser)
10179 while (true)
10181 cp_token *token;
10183 token = cp_lexer_peek_token (parser->lexer);
10185 if (token->type == CPP_CLOSE_BRACE
10186 || token->type == CPP_EOF
10187 || token->type == CPP_PRAGMA_EOL)
10188 break;
10190 if (token->type == CPP_SEMICOLON)
10192 /* A declaration consisting of a single semicolon is
10193 invalid. Allow it unless we're being pedantic. */
10194 cp_lexer_consume_token (parser->lexer);
10195 if (!in_system_header)
10196 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10197 continue;
10200 /* If we're entering or exiting a region that's implicitly
10201 extern "C", modify the lang context appropriately. */
10202 if (!parser->implicit_extern_c && token->implicit_extern_c)
10204 push_lang_context (lang_name_c);
10205 parser->implicit_extern_c = true;
10207 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10209 pop_lang_context ();
10210 parser->implicit_extern_c = false;
10213 if (token->type == CPP_PRAGMA)
10215 /* A top-level declaration can consist solely of a #pragma.
10216 A nested declaration cannot, so this is done here and not
10217 in cp_parser_declaration. (A #pragma at block scope is
10218 handled in cp_parser_statement.) */
10219 cp_parser_pragma (parser, pragma_external);
10220 continue;
10223 /* Parse the declaration itself. */
10224 cp_parser_declaration (parser);
10228 /* Parse a declaration.
10230 declaration:
10231 block-declaration
10232 function-definition
10233 template-declaration
10234 explicit-instantiation
10235 explicit-specialization
10236 linkage-specification
10237 namespace-definition
10239 GNU extension:
10241 declaration:
10242 __extension__ declaration */
10244 static void
10245 cp_parser_declaration (cp_parser* parser)
10247 cp_token token1;
10248 cp_token token2;
10249 int saved_pedantic;
10250 void *p;
10251 tree attributes = NULL_TREE;
10253 /* Check for the `__extension__' keyword. */
10254 if (cp_parser_extension_opt (parser, &saved_pedantic))
10256 /* Parse the qualified declaration. */
10257 cp_parser_declaration (parser);
10258 /* Restore the PEDANTIC flag. */
10259 pedantic = saved_pedantic;
10261 return;
10264 /* Try to figure out what kind of declaration is present. */
10265 token1 = *cp_lexer_peek_token (parser->lexer);
10267 if (token1.type != CPP_EOF)
10268 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10269 else
10271 token2.type = CPP_EOF;
10272 token2.keyword = RID_MAX;
10275 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10276 p = obstack_alloc (&declarator_obstack, 0);
10278 /* If the next token is `extern' and the following token is a string
10279 literal, then we have a linkage specification. */
10280 if (token1.keyword == RID_EXTERN
10281 && cp_parser_is_pure_string_literal (&token2))
10282 cp_parser_linkage_specification (parser);
10283 /* If the next token is `template', then we have either a template
10284 declaration, an explicit instantiation, or an explicit
10285 specialization. */
10286 else if (token1.keyword == RID_TEMPLATE)
10288 /* `template <>' indicates a template specialization. */
10289 if (token2.type == CPP_LESS
10290 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10291 cp_parser_explicit_specialization (parser);
10292 /* `template <' indicates a template declaration. */
10293 else if (token2.type == CPP_LESS)
10294 cp_parser_template_declaration (parser, /*member_p=*/false);
10295 /* Anything else must be an explicit instantiation. */
10296 else
10297 cp_parser_explicit_instantiation (parser);
10299 /* If the next token is `export', then we have a template
10300 declaration. */
10301 else if (token1.keyword == RID_EXPORT)
10302 cp_parser_template_declaration (parser, /*member_p=*/false);
10303 /* If the next token is `extern', 'static' or 'inline' and the one
10304 after that is `template', we have a GNU extended explicit
10305 instantiation directive. */
10306 else if (cp_parser_allow_gnu_extensions_p (parser)
10307 && (token1.keyword == RID_EXTERN
10308 || token1.keyword == RID_STATIC
10309 || token1.keyword == RID_INLINE)
10310 && token2.keyword == RID_TEMPLATE)
10311 cp_parser_explicit_instantiation (parser);
10312 /* If the next token is `namespace', check for a named or unnamed
10313 namespace definition. */
10314 else if (token1.keyword == RID_NAMESPACE
10315 && (/* A named namespace definition. */
10316 (token2.type == CPP_NAME
10317 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10318 != CPP_EQ))
10319 /* An unnamed namespace definition. */
10320 || token2.type == CPP_OPEN_BRACE
10321 || token2.keyword == RID_ATTRIBUTE))
10322 cp_parser_namespace_definition (parser);
10323 /* An inline (associated) namespace definition. */
10324 else if (token1.keyword == RID_INLINE
10325 && token2.keyword == RID_NAMESPACE)
10326 cp_parser_namespace_definition (parser);
10327 /* Objective-C++ declaration/definition. */
10328 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10329 cp_parser_objc_declaration (parser, NULL_TREE);
10330 else if (c_dialect_objc ()
10331 && token1.keyword == RID_ATTRIBUTE
10332 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10333 cp_parser_objc_declaration (parser, attributes);
10334 /* We must have either a block declaration or a function
10335 definition. */
10336 else
10337 /* Try to parse a block-declaration, or a function-definition. */
10338 cp_parser_block_declaration (parser, /*statement_p=*/false);
10340 /* Free any declarators allocated. */
10341 obstack_free (&declarator_obstack, p);
10344 /* Parse a block-declaration.
10346 block-declaration:
10347 simple-declaration
10348 asm-definition
10349 namespace-alias-definition
10350 using-declaration
10351 using-directive
10353 GNU Extension:
10355 block-declaration:
10356 __extension__ block-declaration
10358 C++0x Extension:
10360 block-declaration:
10361 static_assert-declaration
10363 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10364 part of a declaration-statement. */
10366 static void
10367 cp_parser_block_declaration (cp_parser *parser,
10368 bool statement_p)
10370 cp_token *token1;
10371 int saved_pedantic;
10373 /* Check for the `__extension__' keyword. */
10374 if (cp_parser_extension_opt (parser, &saved_pedantic))
10376 /* Parse the qualified declaration. */
10377 cp_parser_block_declaration (parser, statement_p);
10378 /* Restore the PEDANTIC flag. */
10379 pedantic = saved_pedantic;
10381 return;
10384 /* Peek at the next token to figure out which kind of declaration is
10385 present. */
10386 token1 = cp_lexer_peek_token (parser->lexer);
10388 /* If the next keyword is `asm', we have an asm-definition. */
10389 if (token1->keyword == RID_ASM)
10391 if (statement_p)
10392 cp_parser_commit_to_tentative_parse (parser);
10393 cp_parser_asm_definition (parser);
10395 /* If the next keyword is `namespace', we have a
10396 namespace-alias-definition. */
10397 else if (token1->keyword == RID_NAMESPACE)
10398 cp_parser_namespace_alias_definition (parser);
10399 /* If the next keyword is `using', we have a
10400 using-declaration, a using-directive, or an alias-declaration. */
10401 else if (token1->keyword == RID_USING)
10403 cp_token *token2;
10405 if (statement_p)
10406 cp_parser_commit_to_tentative_parse (parser);
10407 /* If the token after `using' is `namespace', then we have a
10408 using-directive. */
10409 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10410 if (token2->keyword == RID_NAMESPACE)
10411 cp_parser_using_directive (parser);
10412 /* If the second token after 'using' is '=', then we have an
10413 alias-declaration. */
10414 else if (cxx_dialect >= cxx0x
10415 && token2->type == CPP_NAME
10416 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10417 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
10418 cp_parser_alias_declaration (parser);
10419 /* Otherwise, it's a using-declaration. */
10420 else
10421 cp_parser_using_declaration (parser,
10422 /*access_declaration_p=*/false);
10424 /* If the next keyword is `__label__' we have a misplaced label
10425 declaration. */
10426 else if (token1->keyword == RID_LABEL)
10428 cp_lexer_consume_token (parser->lexer);
10429 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10430 cp_parser_skip_to_end_of_statement (parser);
10431 /* If the next token is now a `;', consume it. */
10432 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10433 cp_lexer_consume_token (parser->lexer);
10435 /* If the next token is `static_assert' we have a static assertion. */
10436 else if (token1->keyword == RID_STATIC_ASSERT)
10437 cp_parser_static_assert (parser, /*member_p=*/false);
10438 /* Anything else must be a simple-declaration. */
10439 else
10440 cp_parser_simple_declaration (parser, !statement_p,
10441 /*maybe_range_for_decl*/NULL);
10444 /* Parse a simple-declaration.
10446 simple-declaration:
10447 decl-specifier-seq [opt] init-declarator-list [opt] ;
10449 init-declarator-list:
10450 init-declarator
10451 init-declarator-list , init-declarator
10453 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10454 function-definition as a simple-declaration.
10456 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10457 parsed declaration if it is an uninitialized single declarator not followed
10458 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10459 if present, will not be consumed. */
10461 static void
10462 cp_parser_simple_declaration (cp_parser* parser,
10463 bool function_definition_allowed_p,
10464 tree *maybe_range_for_decl)
10466 cp_decl_specifier_seq decl_specifiers;
10467 int declares_class_or_enum;
10468 bool saw_declarator;
10470 if (maybe_range_for_decl)
10471 *maybe_range_for_decl = NULL_TREE;
10473 /* Defer access checks until we know what is being declared; the
10474 checks for names appearing in the decl-specifier-seq should be
10475 done as if we were in the scope of the thing being declared. */
10476 push_deferring_access_checks (dk_deferred);
10478 /* Parse the decl-specifier-seq. We have to keep track of whether
10479 or not the decl-specifier-seq declares a named class or
10480 enumeration type, since that is the only case in which the
10481 init-declarator-list is allowed to be empty.
10483 [dcl.dcl]
10485 In a simple-declaration, the optional init-declarator-list can be
10486 omitted only when declaring a class or enumeration, that is when
10487 the decl-specifier-seq contains either a class-specifier, an
10488 elaborated-type-specifier, or an enum-specifier. */
10489 cp_parser_decl_specifier_seq (parser,
10490 CP_PARSER_FLAGS_OPTIONAL,
10491 &decl_specifiers,
10492 &declares_class_or_enum);
10493 /* We no longer need to defer access checks. */
10494 stop_deferring_access_checks ();
10496 /* In a block scope, a valid declaration must always have a
10497 decl-specifier-seq. By not trying to parse declarators, we can
10498 resolve the declaration/expression ambiguity more quickly. */
10499 if (!function_definition_allowed_p
10500 && !decl_specifiers.any_specifiers_p)
10502 cp_parser_error (parser, "expected declaration");
10503 goto done;
10506 /* If the next two tokens are both identifiers, the code is
10507 erroneous. The usual cause of this situation is code like:
10509 T t;
10511 where "T" should name a type -- but does not. */
10512 if (!decl_specifiers.any_type_specifiers_p
10513 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10515 /* If parsing tentatively, we should commit; we really are
10516 looking at a declaration. */
10517 cp_parser_commit_to_tentative_parse (parser);
10518 /* Give up. */
10519 goto done;
10522 /* If we have seen at least one decl-specifier, and the next token
10523 is not a parenthesis, then we must be looking at a declaration.
10524 (After "int (" we might be looking at a functional cast.) */
10525 if (decl_specifiers.any_specifiers_p
10526 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10527 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10528 && !cp_parser_error_occurred (parser))
10529 cp_parser_commit_to_tentative_parse (parser);
10531 /* Keep going until we hit the `;' at the end of the simple
10532 declaration. */
10533 saw_declarator = false;
10534 while (cp_lexer_next_token_is_not (parser->lexer,
10535 CPP_SEMICOLON))
10537 cp_token *token;
10538 bool function_definition_p;
10539 tree decl;
10541 if (saw_declarator)
10543 /* If we are processing next declarator, coma is expected */
10544 token = cp_lexer_peek_token (parser->lexer);
10545 gcc_assert (token->type == CPP_COMMA);
10546 cp_lexer_consume_token (parser->lexer);
10547 if (maybe_range_for_decl)
10548 *maybe_range_for_decl = error_mark_node;
10550 else
10551 saw_declarator = true;
10553 /* Parse the init-declarator. */
10554 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10555 /*checks=*/NULL,
10556 function_definition_allowed_p,
10557 /*member_p=*/false,
10558 declares_class_or_enum,
10559 &function_definition_p,
10560 maybe_range_for_decl);
10561 /* If an error occurred while parsing tentatively, exit quickly.
10562 (That usually happens when in the body of a function; each
10563 statement is treated as a declaration-statement until proven
10564 otherwise.) */
10565 if (cp_parser_error_occurred (parser))
10566 goto done;
10567 /* Handle function definitions specially. */
10568 if (function_definition_p)
10570 /* If the next token is a `,', then we are probably
10571 processing something like:
10573 void f() {}, *p;
10575 which is erroneous. */
10576 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10578 cp_token *token = cp_lexer_peek_token (parser->lexer);
10579 error_at (token->location,
10580 "mixing"
10581 " declarations and function-definitions is forbidden");
10583 /* Otherwise, we're done with the list of declarators. */
10584 else
10586 pop_deferring_access_checks ();
10587 return;
10590 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10591 *maybe_range_for_decl = decl;
10592 /* The next token should be either a `,' or a `;'. */
10593 token = cp_lexer_peek_token (parser->lexer);
10594 /* If it's a `,', there are more declarators to come. */
10595 if (token->type == CPP_COMMA)
10596 /* will be consumed next time around */;
10597 /* If it's a `;', we are done. */
10598 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10599 break;
10600 /* Anything else is an error. */
10601 else
10603 /* If we have already issued an error message we don't need
10604 to issue another one. */
10605 if (decl != error_mark_node
10606 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10607 cp_parser_error (parser, "expected %<,%> or %<;%>");
10608 /* Skip tokens until we reach the end of the statement. */
10609 cp_parser_skip_to_end_of_statement (parser);
10610 /* If the next token is now a `;', consume it. */
10611 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10612 cp_lexer_consume_token (parser->lexer);
10613 goto done;
10615 /* After the first time around, a function-definition is not
10616 allowed -- even if it was OK at first. For example:
10618 int i, f() {}
10620 is not valid. */
10621 function_definition_allowed_p = false;
10624 /* Issue an error message if no declarators are present, and the
10625 decl-specifier-seq does not itself declare a class or
10626 enumeration. */
10627 if (!saw_declarator)
10629 if (cp_parser_declares_only_class_p (parser))
10630 shadow_tag (&decl_specifiers);
10631 /* Perform any deferred access checks. */
10632 perform_deferred_access_checks (tf_warning_or_error);
10635 /* Consume the `;'. */
10636 if (!maybe_range_for_decl)
10637 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10639 done:
10640 pop_deferring_access_checks ();
10643 /* Parse a decl-specifier-seq.
10645 decl-specifier-seq:
10646 decl-specifier-seq [opt] decl-specifier
10647 decl-specifier attribute-specifier-seq [opt] (C++11)
10649 decl-specifier:
10650 storage-class-specifier
10651 type-specifier
10652 function-specifier
10653 friend
10654 typedef
10656 GNU Extension:
10658 decl-specifier:
10659 attributes
10661 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10663 The parser flags FLAGS is used to control type-specifier parsing.
10665 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10666 flags:
10668 1: one of the decl-specifiers is an elaborated-type-specifier
10669 (i.e., a type declaration)
10670 2: one of the decl-specifiers is an enum-specifier or a
10671 class-specifier (i.e., a type definition)
10675 static void
10676 cp_parser_decl_specifier_seq (cp_parser* parser,
10677 cp_parser_flags flags,
10678 cp_decl_specifier_seq *decl_specs,
10679 int* declares_class_or_enum)
10681 bool constructor_possible_p = !parser->in_declarator_p;
10682 bool found_decl_spec = false;
10683 cp_token *start_token = NULL;
10684 cp_decl_spec ds;
10686 /* Clear DECL_SPECS. */
10687 clear_decl_specs (decl_specs);
10689 /* Assume no class or enumeration type is declared. */
10690 *declares_class_or_enum = 0;
10692 /* Keep reading specifiers until there are no more to read. */
10693 while (true)
10695 bool constructor_p;
10696 cp_token *token;
10697 ds = ds_last;
10699 /* Peek at the next token. */
10700 token = cp_lexer_peek_token (parser->lexer);
10702 /* Save the first token of the decl spec list for error
10703 reporting. */
10704 if (!start_token)
10705 start_token = token;
10706 /* Handle attributes. */
10707 if (cp_next_tokens_can_be_attribute_p (parser))
10709 /* Parse the attributes. */
10710 tree attrs = cp_parser_attributes_opt (parser);
10712 /* In a sequence of declaration specifiers, c++11 attributes
10713 appertain to the type that precede them. In that case
10714 [dcl.spec]/1 says:
10716 The attribute-specifier-seq affects the type only for
10717 the declaration it appears in, not other declarations
10718 involving the same type.
10720 But for now let's force the user to position the
10721 attribute either at the beginning of the declaration or
10722 after the declarator-id, which would clearly mean that it
10723 applies to the declarator. */
10724 if (cxx11_attribute_p (attrs))
10726 if (!found_decl_spec)
10727 /* The c++11 attribute is at the beginning of the
10728 declaration. It appertains to the entity being
10729 declared. */;
10730 else
10732 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
10734 /* This is an attribute following a
10735 class-specifier. */
10736 if (decl_specs->type_definition_p)
10737 warn_misplaced_attr_for_class_type (token->location,
10738 decl_specs->type);
10739 attrs = NULL_TREE;
10741 else
10743 decl_specs->std_attributes
10744 = chainon (decl_specs->std_attributes,
10745 attrs);
10746 if (decl_specs->locations[ds_std_attribute] == 0)
10747 decl_specs->locations[ds_std_attribute] = token->location;
10749 continue;
10753 decl_specs->attributes
10754 = chainon (decl_specs->attributes,
10755 attrs);
10756 if (decl_specs->locations[ds_attribute] == 0)
10757 decl_specs->locations[ds_attribute] = token->location;
10758 continue;
10760 /* Assume we will find a decl-specifier keyword. */
10761 found_decl_spec = true;
10762 /* If the next token is an appropriate keyword, we can simply
10763 add it to the list. */
10764 switch (token->keyword)
10766 /* decl-specifier:
10767 friend
10768 constexpr */
10769 case RID_FRIEND:
10770 if (!at_class_scope_p ())
10772 error_at (token->location, "%<friend%> used outside of class");
10773 cp_lexer_purge_token (parser->lexer);
10775 else
10777 ds = ds_friend;
10778 /* Consume the token. */
10779 cp_lexer_consume_token (parser->lexer);
10781 break;
10783 case RID_CONSTEXPR:
10784 ds = ds_constexpr;
10785 cp_lexer_consume_token (parser->lexer);
10786 break;
10788 /* function-specifier:
10789 inline
10790 virtual
10791 explicit */
10792 case RID_INLINE:
10793 case RID_VIRTUAL:
10794 case RID_EXPLICIT:
10795 cp_parser_function_specifier_opt (parser, decl_specs);
10796 break;
10798 /* decl-specifier:
10799 typedef */
10800 case RID_TYPEDEF:
10801 ds = ds_typedef;
10802 /* Consume the token. */
10803 cp_lexer_consume_token (parser->lexer);
10804 /* A constructor declarator cannot appear in a typedef. */
10805 constructor_possible_p = false;
10806 /* The "typedef" keyword can only occur in a declaration; we
10807 may as well commit at this point. */
10808 cp_parser_commit_to_tentative_parse (parser);
10810 if (decl_specs->storage_class != sc_none)
10811 decl_specs->conflicting_specifiers_p = true;
10812 break;
10814 /* storage-class-specifier:
10815 auto
10816 register
10817 static
10818 extern
10819 mutable
10821 GNU Extension:
10822 thread */
10823 case RID_AUTO:
10824 if (cxx_dialect == cxx98)
10826 /* Consume the token. */
10827 cp_lexer_consume_token (parser->lexer);
10829 /* Complain about `auto' as a storage specifier, if
10830 we're complaining about C++0x compatibility. */
10831 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10832 " changes meaning in C++11; please remove it");
10834 /* Set the storage class anyway. */
10835 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10836 token);
10838 else
10839 /* C++0x auto type-specifier. */
10840 found_decl_spec = false;
10841 break;
10843 case RID_REGISTER:
10844 case RID_STATIC:
10845 case RID_EXTERN:
10846 case RID_MUTABLE:
10847 /* Consume the token. */
10848 cp_lexer_consume_token (parser->lexer);
10849 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10850 token);
10851 break;
10852 case RID_THREAD:
10853 /* Consume the token. */
10854 ds = ds_thread;
10855 cp_lexer_consume_token (parser->lexer);
10856 break;
10858 default:
10859 /* We did not yet find a decl-specifier yet. */
10860 found_decl_spec = false;
10861 break;
10864 if (found_decl_spec
10865 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10866 && token->keyword != RID_CONSTEXPR)
10867 error ("decl-specifier invalid in condition");
10869 if (ds != ds_last)
10870 set_and_check_decl_spec_loc (decl_specs, ds, token);
10872 /* Constructors are a special case. The `S' in `S()' is not a
10873 decl-specifier; it is the beginning of the declarator. */
10874 constructor_p
10875 = (!found_decl_spec
10876 && constructor_possible_p
10877 && (cp_parser_constructor_declarator_p
10878 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
10880 /* If we don't have a DECL_SPEC yet, then we must be looking at
10881 a type-specifier. */
10882 if (!found_decl_spec && !constructor_p)
10884 int decl_spec_declares_class_or_enum;
10885 bool is_cv_qualifier;
10886 tree type_spec;
10888 type_spec
10889 = cp_parser_type_specifier (parser, flags,
10890 decl_specs,
10891 /*is_declaration=*/true,
10892 &decl_spec_declares_class_or_enum,
10893 &is_cv_qualifier);
10894 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10896 /* If this type-specifier referenced a user-defined type
10897 (a typedef, class-name, etc.), then we can't allow any
10898 more such type-specifiers henceforth.
10900 [dcl.spec]
10902 The longest sequence of decl-specifiers that could
10903 possibly be a type name is taken as the
10904 decl-specifier-seq of a declaration. The sequence shall
10905 be self-consistent as described below.
10907 [dcl.type]
10909 As a general rule, at most one type-specifier is allowed
10910 in the complete decl-specifier-seq of a declaration. The
10911 only exceptions are the following:
10913 -- const or volatile can be combined with any other
10914 type-specifier.
10916 -- signed or unsigned can be combined with char, long,
10917 short, or int.
10919 -- ..
10921 Example:
10923 typedef char* Pc;
10924 void g (const int Pc);
10926 Here, Pc is *not* part of the decl-specifier seq; it's
10927 the declarator. Therefore, once we see a type-specifier
10928 (other than a cv-qualifier), we forbid any additional
10929 user-defined types. We *do* still allow things like `int
10930 int' to be considered a decl-specifier-seq, and issue the
10931 error message later. */
10932 if (type_spec && !is_cv_qualifier)
10933 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10934 /* A constructor declarator cannot follow a type-specifier. */
10935 if (type_spec)
10937 constructor_possible_p = false;
10938 found_decl_spec = true;
10939 if (!is_cv_qualifier)
10940 decl_specs->any_type_specifiers_p = true;
10944 /* If we still do not have a DECL_SPEC, then there are no more
10945 decl-specifiers. */
10946 if (!found_decl_spec)
10947 break;
10949 decl_specs->any_specifiers_p = true;
10950 /* After we see one decl-specifier, further decl-specifiers are
10951 always optional. */
10952 flags |= CP_PARSER_FLAGS_OPTIONAL;
10955 /* Don't allow a friend specifier with a class definition. */
10956 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
10957 && (*declares_class_or_enum & 2))
10958 error_at (decl_specs->locations[ds_friend],
10959 "class definition may not be declared a friend");
10962 /* Parse an (optional) storage-class-specifier.
10964 storage-class-specifier:
10965 auto
10966 register
10967 static
10968 extern
10969 mutable
10971 GNU Extension:
10973 storage-class-specifier:
10974 thread
10976 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10978 static tree
10979 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10981 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10983 case RID_AUTO:
10984 if (cxx_dialect != cxx98)
10985 return NULL_TREE;
10986 /* Fall through for C++98. */
10988 case RID_REGISTER:
10989 case RID_STATIC:
10990 case RID_EXTERN:
10991 case RID_MUTABLE:
10992 case RID_THREAD:
10993 /* Consume the token. */
10994 return cp_lexer_consume_token (parser->lexer)->u.value;
10996 default:
10997 return NULL_TREE;
11001 /* Parse an (optional) function-specifier.
11003 function-specifier:
11004 inline
11005 virtual
11006 explicit
11008 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11009 Updates DECL_SPECS, if it is non-NULL. */
11011 static tree
11012 cp_parser_function_specifier_opt (cp_parser* parser,
11013 cp_decl_specifier_seq *decl_specs)
11015 cp_token *token = cp_lexer_peek_token (parser->lexer);
11016 switch (token->keyword)
11018 case RID_INLINE:
11019 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11020 break;
11022 case RID_VIRTUAL:
11023 /* 14.5.2.3 [temp.mem]
11025 A member function template shall not be virtual. */
11026 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11027 error_at (token->location, "templates may not be %<virtual%>");
11028 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11029 break;
11031 case RID_EXPLICIT:
11032 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11033 break;
11035 default:
11036 return NULL_TREE;
11039 /* Consume the token. */
11040 return cp_lexer_consume_token (parser->lexer)->u.value;
11043 /* Parse a linkage-specification.
11045 linkage-specification:
11046 extern string-literal { declaration-seq [opt] }
11047 extern string-literal declaration */
11049 static void
11050 cp_parser_linkage_specification (cp_parser* parser)
11052 tree linkage;
11054 /* Look for the `extern' keyword. */
11055 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11057 /* Look for the string-literal. */
11058 linkage = cp_parser_string_literal (parser, false, false);
11060 /* Transform the literal into an identifier. If the literal is a
11061 wide-character string, or contains embedded NULs, then we can't
11062 handle it as the user wants. */
11063 if (strlen (TREE_STRING_POINTER (linkage))
11064 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11066 cp_parser_error (parser, "invalid linkage-specification");
11067 /* Assume C++ linkage. */
11068 linkage = lang_name_cplusplus;
11070 else
11071 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11073 /* We're now using the new linkage. */
11074 push_lang_context (linkage);
11076 /* If the next token is a `{', then we're using the first
11077 production. */
11078 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11080 /* Consume the `{' token. */
11081 cp_lexer_consume_token (parser->lexer);
11082 /* Parse the declarations. */
11083 cp_parser_declaration_seq_opt (parser);
11084 /* Look for the closing `}'. */
11085 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11087 /* Otherwise, there's just one declaration. */
11088 else
11090 bool saved_in_unbraced_linkage_specification_p;
11092 saved_in_unbraced_linkage_specification_p
11093 = parser->in_unbraced_linkage_specification_p;
11094 parser->in_unbraced_linkage_specification_p = true;
11095 cp_parser_declaration (parser);
11096 parser->in_unbraced_linkage_specification_p
11097 = saved_in_unbraced_linkage_specification_p;
11100 /* We're done with the linkage-specification. */
11101 pop_lang_context ();
11104 /* Parse a static_assert-declaration.
11106 static_assert-declaration:
11107 static_assert ( constant-expression , string-literal ) ;
11109 If MEMBER_P, this static_assert is a class member. */
11111 static void
11112 cp_parser_static_assert(cp_parser *parser, bool member_p)
11114 tree condition;
11115 tree message;
11116 cp_token *token;
11117 location_t saved_loc;
11118 bool dummy;
11120 /* Peek at the `static_assert' token so we can keep track of exactly
11121 where the static assertion started. */
11122 token = cp_lexer_peek_token (parser->lexer);
11123 saved_loc = token->location;
11125 /* Look for the `static_assert' keyword. */
11126 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11127 RT_STATIC_ASSERT))
11128 return;
11130 /* We know we are in a static assertion; commit to any tentative
11131 parse. */
11132 if (cp_parser_parsing_tentatively (parser))
11133 cp_parser_commit_to_tentative_parse (parser);
11135 /* Parse the `(' starting the static assertion condition. */
11136 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11138 /* Parse the constant-expression. Allow a non-constant expression
11139 here in order to give better diagnostics in finish_static_assert. */
11140 condition =
11141 cp_parser_constant_expression (parser,
11142 /*allow_non_constant_p=*/true,
11143 /*non_constant_p=*/&dummy);
11145 /* Parse the separating `,'. */
11146 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11148 /* Parse the string-literal message. */
11149 message = cp_parser_string_literal (parser,
11150 /*translate=*/false,
11151 /*wide_ok=*/true);
11153 /* A `)' completes the static assertion. */
11154 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11155 cp_parser_skip_to_closing_parenthesis (parser,
11156 /*recovering=*/true,
11157 /*or_comma=*/false,
11158 /*consume_paren=*/true);
11160 /* A semicolon terminates the declaration. */
11161 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11163 /* Complete the static assertion, which may mean either processing
11164 the static assert now or saving it for template instantiation. */
11165 finish_static_assert (condition, message, saved_loc, member_p);
11168 /* Parse a `decltype' type. Returns the type.
11170 simple-type-specifier:
11171 decltype ( expression ) */
11173 static tree
11174 cp_parser_decltype (cp_parser *parser)
11176 tree expr;
11177 bool id_expression_or_member_access_p = false;
11178 const char *saved_message;
11179 bool saved_integral_constant_expression_p;
11180 bool saved_non_integral_constant_expression_p;
11181 cp_token *id_expr_start_token;
11182 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11184 if (start_token->type == CPP_DECLTYPE)
11186 /* Already parsed. */
11187 cp_lexer_consume_token (parser->lexer);
11188 return start_token->u.value;
11191 /* Look for the `decltype' token. */
11192 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11193 return error_mark_node;
11195 /* Types cannot be defined in a `decltype' expression. Save away the
11196 old message. */
11197 saved_message = parser->type_definition_forbidden_message;
11199 /* And create the new one. */
11200 parser->type_definition_forbidden_message
11201 = G_("types may not be defined in %<decltype%> expressions");
11203 /* The restrictions on constant-expressions do not apply inside
11204 decltype expressions. */
11205 saved_integral_constant_expression_p
11206 = parser->integral_constant_expression_p;
11207 saved_non_integral_constant_expression_p
11208 = parser->non_integral_constant_expression_p;
11209 parser->integral_constant_expression_p = false;
11211 /* Do not actually evaluate the expression. */
11212 ++cp_unevaluated_operand;
11214 /* Do not warn about problems with the expression. */
11215 ++c_inhibit_evaluation_warnings;
11217 /* Parse the opening `('. */
11218 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11219 return error_mark_node;
11221 /* First, try parsing an id-expression. */
11222 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11223 cp_parser_parse_tentatively (parser);
11224 expr = cp_parser_id_expression (parser,
11225 /*template_keyword_p=*/false,
11226 /*check_dependency_p=*/true,
11227 /*template_p=*/NULL,
11228 /*declarator_p=*/false,
11229 /*optional_p=*/false);
11231 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11233 bool non_integral_constant_expression_p = false;
11234 tree id_expression = expr;
11235 cp_id_kind idk;
11236 const char *error_msg;
11238 if (TREE_CODE (expr) == IDENTIFIER_NODE)
11239 /* Lookup the name we got back from the id-expression. */
11240 expr = cp_parser_lookup_name (parser, expr,
11241 none_type,
11242 /*is_template=*/false,
11243 /*is_namespace=*/false,
11244 /*check_dependency=*/true,
11245 /*ambiguous_decls=*/NULL,
11246 id_expr_start_token->location);
11248 if (expr
11249 && expr != error_mark_node
11250 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11251 && TREE_CODE (expr) != TYPE_DECL
11252 && (TREE_CODE (expr) != BIT_NOT_EXPR
11253 || !TYPE_P (TREE_OPERAND (expr, 0)))
11254 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11256 /* Complete lookup of the id-expression. */
11257 expr = (finish_id_expression
11258 (id_expression, expr, parser->scope, &idk,
11259 /*integral_constant_expression_p=*/false,
11260 /*allow_non_integral_constant_expression_p=*/true,
11261 &non_integral_constant_expression_p,
11262 /*template_p=*/false,
11263 /*done=*/true,
11264 /*address_p=*/false,
11265 /*template_arg_p=*/false,
11266 &error_msg,
11267 id_expr_start_token->location));
11269 if (expr == error_mark_node)
11270 /* We found an id-expression, but it was something that we
11271 should not have found. This is an error, not something
11272 we can recover from, so note that we found an
11273 id-expression and we'll recover as gracefully as
11274 possible. */
11275 id_expression_or_member_access_p = true;
11278 if (expr
11279 && expr != error_mark_node
11280 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11281 /* We have an id-expression. */
11282 id_expression_or_member_access_p = true;
11285 if (!id_expression_or_member_access_p)
11287 /* Abort the id-expression parse. */
11288 cp_parser_abort_tentative_parse (parser);
11290 /* Parsing tentatively, again. */
11291 cp_parser_parse_tentatively (parser);
11293 /* Parse a class member access. */
11294 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11295 /*cast_p=*/false,
11296 /*member_access_only_p=*/true, NULL);
11298 if (expr
11299 && expr != error_mark_node
11300 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11301 /* We have an id-expression. */
11302 id_expression_or_member_access_p = true;
11305 if (id_expression_or_member_access_p)
11306 /* We have parsed the complete id-expression or member access. */
11307 cp_parser_parse_definitely (parser);
11308 else
11310 bool saved_greater_than_is_operator_p;
11312 /* Abort our attempt to parse an id-expression or member access
11313 expression. */
11314 cp_parser_abort_tentative_parse (parser);
11316 /* Within a parenthesized expression, a `>' token is always
11317 the greater-than operator. */
11318 saved_greater_than_is_operator_p
11319 = parser->greater_than_is_operator_p;
11320 parser->greater_than_is_operator_p = true;
11322 /* Parse a full expression. */
11323 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11325 /* The `>' token might be the end of a template-id or
11326 template-parameter-list now. */
11327 parser->greater_than_is_operator_p
11328 = saved_greater_than_is_operator_p;
11331 /* Go back to evaluating expressions. */
11332 --cp_unevaluated_operand;
11333 --c_inhibit_evaluation_warnings;
11335 /* Restore the old message and the integral constant expression
11336 flags. */
11337 parser->type_definition_forbidden_message = saved_message;
11338 parser->integral_constant_expression_p
11339 = saved_integral_constant_expression_p;
11340 parser->non_integral_constant_expression_p
11341 = saved_non_integral_constant_expression_p;
11343 /* Parse to the closing `)'. */
11344 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11346 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11347 /*consume_paren=*/true);
11348 return error_mark_node;
11351 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11352 tf_warning_or_error);
11354 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11355 it again. */
11356 start_token->type = CPP_DECLTYPE;
11357 start_token->u.value = expr;
11358 start_token->keyword = RID_MAX;
11359 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11361 return expr;
11364 /* Special member functions [gram.special] */
11366 /* Parse a conversion-function-id.
11368 conversion-function-id:
11369 operator conversion-type-id
11371 Returns an IDENTIFIER_NODE representing the operator. */
11373 static tree
11374 cp_parser_conversion_function_id (cp_parser* parser)
11376 tree type;
11377 tree saved_scope;
11378 tree saved_qualifying_scope;
11379 tree saved_object_scope;
11380 tree pushed_scope = NULL_TREE;
11382 /* Look for the `operator' token. */
11383 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11384 return error_mark_node;
11385 /* When we parse the conversion-type-id, the current scope will be
11386 reset. However, we need that information in able to look up the
11387 conversion function later, so we save it here. */
11388 saved_scope = parser->scope;
11389 saved_qualifying_scope = parser->qualifying_scope;
11390 saved_object_scope = parser->object_scope;
11391 /* We must enter the scope of the class so that the names of
11392 entities declared within the class are available in the
11393 conversion-type-id. For example, consider:
11395 struct S {
11396 typedef int I;
11397 operator I();
11400 S::operator I() { ... }
11402 In order to see that `I' is a type-name in the definition, we
11403 must be in the scope of `S'. */
11404 if (saved_scope)
11405 pushed_scope = push_scope (saved_scope);
11406 /* Parse the conversion-type-id. */
11407 type = cp_parser_conversion_type_id (parser);
11408 /* Leave the scope of the class, if any. */
11409 if (pushed_scope)
11410 pop_scope (pushed_scope);
11411 /* Restore the saved scope. */
11412 parser->scope = saved_scope;
11413 parser->qualifying_scope = saved_qualifying_scope;
11414 parser->object_scope = saved_object_scope;
11415 /* If the TYPE is invalid, indicate failure. */
11416 if (type == error_mark_node)
11417 return error_mark_node;
11418 return mangle_conv_op_name_for_type (type);
11421 /* Parse a conversion-type-id:
11423 conversion-type-id:
11424 type-specifier-seq conversion-declarator [opt]
11426 Returns the TYPE specified. */
11428 static tree
11429 cp_parser_conversion_type_id (cp_parser* parser)
11431 tree attributes;
11432 cp_decl_specifier_seq type_specifiers;
11433 cp_declarator *declarator;
11434 tree type_specified;
11436 /* Parse the attributes. */
11437 attributes = cp_parser_attributes_opt (parser);
11438 /* Parse the type-specifiers. */
11439 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11440 /*is_trailing_return=*/false,
11441 &type_specifiers);
11442 /* If that didn't work, stop. */
11443 if (type_specifiers.type == error_mark_node)
11444 return error_mark_node;
11445 /* Parse the conversion-declarator. */
11446 declarator = cp_parser_conversion_declarator_opt (parser);
11448 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11449 /*initialized=*/0, &attributes);
11450 if (attributes)
11451 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11453 /* Don't give this error when parsing tentatively. This happens to
11454 work because we always parse this definitively once. */
11455 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11456 && type_uses_auto (type_specified))
11458 if (cxx_dialect < cxx1y)
11460 error ("invalid use of %<auto%> in conversion operator");
11461 return error_mark_node;
11463 else if (template_parm_scope_p ())
11464 warning (0, "use of %<auto%> in member template "
11465 "conversion operator can never be deduced");
11468 return type_specified;
11471 /* Parse an (optional) conversion-declarator.
11473 conversion-declarator:
11474 ptr-operator conversion-declarator [opt]
11478 static cp_declarator *
11479 cp_parser_conversion_declarator_opt (cp_parser* parser)
11481 enum tree_code code;
11482 tree class_type, std_attributes = NULL_TREE;
11483 cp_cv_quals cv_quals;
11485 /* We don't know if there's a ptr-operator next, or not. */
11486 cp_parser_parse_tentatively (parser);
11487 /* Try the ptr-operator. */
11488 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
11489 &std_attributes);
11490 /* If it worked, look for more conversion-declarators. */
11491 if (cp_parser_parse_definitely (parser))
11493 cp_declarator *declarator;
11495 /* Parse another optional declarator. */
11496 declarator = cp_parser_conversion_declarator_opt (parser);
11498 declarator = cp_parser_make_indirect_declarator
11499 (code, class_type, cv_quals, declarator, std_attributes);
11501 return declarator;
11504 return NULL;
11507 /* Parse an (optional) ctor-initializer.
11509 ctor-initializer:
11510 : mem-initializer-list
11512 Returns TRUE iff the ctor-initializer was actually present. */
11514 static bool
11515 cp_parser_ctor_initializer_opt (cp_parser* parser)
11517 /* If the next token is not a `:', then there is no
11518 ctor-initializer. */
11519 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11521 /* Do default initialization of any bases and members. */
11522 if (DECL_CONSTRUCTOR_P (current_function_decl))
11523 finish_mem_initializers (NULL_TREE);
11525 return false;
11528 /* Consume the `:' token. */
11529 cp_lexer_consume_token (parser->lexer);
11530 /* And the mem-initializer-list. */
11531 cp_parser_mem_initializer_list (parser);
11533 return true;
11536 /* Parse a mem-initializer-list.
11538 mem-initializer-list:
11539 mem-initializer ... [opt]
11540 mem-initializer ... [opt] , mem-initializer-list */
11542 static void
11543 cp_parser_mem_initializer_list (cp_parser* parser)
11545 tree mem_initializer_list = NULL_TREE;
11546 tree target_ctor = error_mark_node;
11547 cp_token *token = cp_lexer_peek_token (parser->lexer);
11549 /* Let the semantic analysis code know that we are starting the
11550 mem-initializer-list. */
11551 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11552 error_at (token->location,
11553 "only constructors take member initializers");
11555 /* Loop through the list. */
11556 while (true)
11558 tree mem_initializer;
11560 token = cp_lexer_peek_token (parser->lexer);
11561 /* Parse the mem-initializer. */
11562 mem_initializer = cp_parser_mem_initializer (parser);
11563 /* If the next token is a `...', we're expanding member initializers. */
11564 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11566 /* Consume the `...'. */
11567 cp_lexer_consume_token (parser->lexer);
11569 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11570 can be expanded but members cannot. */
11571 if (mem_initializer != error_mark_node
11572 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11574 error_at (token->location,
11575 "cannot expand initializer for member %<%D%>",
11576 TREE_PURPOSE (mem_initializer));
11577 mem_initializer = error_mark_node;
11580 /* Construct the pack expansion type. */
11581 if (mem_initializer != error_mark_node)
11582 mem_initializer = make_pack_expansion (mem_initializer);
11584 if (target_ctor != error_mark_node
11585 && mem_initializer != error_mark_node)
11587 error ("mem-initializer for %qD follows constructor delegation",
11588 TREE_PURPOSE (mem_initializer));
11589 mem_initializer = error_mark_node;
11591 /* Look for a target constructor. */
11592 if (mem_initializer != error_mark_node
11593 && TYPE_P (TREE_PURPOSE (mem_initializer))
11594 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11596 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11597 if (mem_initializer_list)
11599 error ("constructor delegation follows mem-initializer for %qD",
11600 TREE_PURPOSE (mem_initializer_list));
11601 mem_initializer = error_mark_node;
11603 target_ctor = mem_initializer;
11605 /* Add it to the list, unless it was erroneous. */
11606 if (mem_initializer != error_mark_node)
11608 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11609 mem_initializer_list = mem_initializer;
11611 /* If the next token is not a `,', we're done. */
11612 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11613 break;
11614 /* Consume the `,' token. */
11615 cp_lexer_consume_token (parser->lexer);
11618 /* Perform semantic analysis. */
11619 if (DECL_CONSTRUCTOR_P (current_function_decl))
11620 finish_mem_initializers (mem_initializer_list);
11623 /* Parse a mem-initializer.
11625 mem-initializer:
11626 mem-initializer-id ( expression-list [opt] )
11627 mem-initializer-id braced-init-list
11629 GNU extension:
11631 mem-initializer:
11632 ( expression-list [opt] )
11634 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11635 class) or FIELD_DECL (for a non-static data member) to initialize;
11636 the TREE_VALUE is the expression-list. An empty initialization
11637 list is represented by void_list_node. */
11639 static tree
11640 cp_parser_mem_initializer (cp_parser* parser)
11642 tree mem_initializer_id;
11643 tree expression_list;
11644 tree member;
11645 cp_token *token = cp_lexer_peek_token (parser->lexer);
11647 /* Find out what is being initialized. */
11648 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11650 permerror (token->location,
11651 "anachronistic old-style base class initializer");
11652 mem_initializer_id = NULL_TREE;
11654 else
11656 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11657 if (mem_initializer_id == error_mark_node)
11658 return mem_initializer_id;
11660 member = expand_member_init (mem_initializer_id);
11661 if (member && !DECL_P (member))
11662 in_base_initializer = 1;
11664 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11666 bool expr_non_constant_p;
11667 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11668 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11669 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11670 expression_list = build_tree_list (NULL_TREE, expression_list);
11672 else
11674 VEC(tree,gc)* vec;
11675 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11676 /*cast_p=*/false,
11677 /*allow_expansion_p=*/true,
11678 /*non_constant_p=*/NULL);
11679 if (vec == NULL)
11680 return error_mark_node;
11681 expression_list = build_tree_list_vec (vec);
11682 release_tree_vector (vec);
11685 if (expression_list == error_mark_node)
11686 return error_mark_node;
11687 if (!expression_list)
11688 expression_list = void_type_node;
11690 in_base_initializer = 0;
11692 return member ? build_tree_list (member, expression_list) : error_mark_node;
11695 /* Parse a mem-initializer-id.
11697 mem-initializer-id:
11698 :: [opt] nested-name-specifier [opt] class-name
11699 identifier
11701 Returns a TYPE indicating the class to be initializer for the first
11702 production. Returns an IDENTIFIER_NODE indicating the data member
11703 to be initialized for the second production. */
11705 static tree
11706 cp_parser_mem_initializer_id (cp_parser* parser)
11708 bool global_scope_p;
11709 bool nested_name_specifier_p;
11710 bool template_p = false;
11711 tree id;
11713 cp_token *token = cp_lexer_peek_token (parser->lexer);
11715 /* `typename' is not allowed in this context ([temp.res]). */
11716 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11718 error_at (token->location,
11719 "keyword %<typename%> not allowed in this context (a qualified "
11720 "member initializer is implicitly a type)");
11721 cp_lexer_consume_token (parser->lexer);
11723 /* Look for the optional `::' operator. */
11724 global_scope_p
11725 = (cp_parser_global_scope_opt (parser,
11726 /*current_scope_valid_p=*/false)
11727 != NULL_TREE);
11728 /* Look for the optional nested-name-specifier. The simplest way to
11729 implement:
11731 [temp.res]
11733 The keyword `typename' is not permitted in a base-specifier or
11734 mem-initializer; in these contexts a qualified name that
11735 depends on a template-parameter is implicitly assumed to be a
11736 type name.
11738 is to assume that we have seen the `typename' keyword at this
11739 point. */
11740 nested_name_specifier_p
11741 = (cp_parser_nested_name_specifier_opt (parser,
11742 /*typename_keyword_p=*/true,
11743 /*check_dependency_p=*/true,
11744 /*type_p=*/true,
11745 /*is_declaration=*/true)
11746 != NULL_TREE);
11747 if (nested_name_specifier_p)
11748 template_p = cp_parser_optional_template_keyword (parser);
11749 /* If there is a `::' operator or a nested-name-specifier, then we
11750 are definitely looking for a class-name. */
11751 if (global_scope_p || nested_name_specifier_p)
11752 return cp_parser_class_name (parser,
11753 /*typename_keyword_p=*/true,
11754 /*template_keyword_p=*/template_p,
11755 typename_type,
11756 /*check_dependency_p=*/true,
11757 /*class_head_p=*/false,
11758 /*is_declaration=*/true);
11759 /* Otherwise, we could also be looking for an ordinary identifier. */
11760 cp_parser_parse_tentatively (parser);
11761 /* Try a class-name. */
11762 id = cp_parser_class_name (parser,
11763 /*typename_keyword_p=*/true,
11764 /*template_keyword_p=*/false,
11765 none_type,
11766 /*check_dependency_p=*/true,
11767 /*class_head_p=*/false,
11768 /*is_declaration=*/true);
11769 /* If we found one, we're done. */
11770 if (cp_parser_parse_definitely (parser))
11771 return id;
11772 /* Otherwise, look for an ordinary identifier. */
11773 return cp_parser_identifier (parser);
11776 /* Overloading [gram.over] */
11778 /* Parse an operator-function-id.
11780 operator-function-id:
11781 operator operator
11783 Returns an IDENTIFIER_NODE for the operator which is a
11784 human-readable spelling of the identifier, e.g., `operator +'. */
11786 static tree
11787 cp_parser_operator_function_id (cp_parser* parser)
11789 /* Look for the `operator' keyword. */
11790 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11791 return error_mark_node;
11792 /* And then the name of the operator itself. */
11793 return cp_parser_operator (parser);
11796 /* Return an identifier node for a user-defined literal operator.
11797 The suffix identifier is chained to the operator name identifier. */
11799 static tree
11800 cp_literal_operator_id (const char* name)
11802 tree identifier;
11803 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11804 + strlen (name) + 10);
11805 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11806 identifier = get_identifier (buffer);
11807 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11809 return identifier;
11812 /* Parse an operator.
11814 operator:
11815 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11816 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11817 || ++ -- , ->* -> () []
11819 GNU Extensions:
11821 operator:
11822 <? >? <?= >?=
11824 Returns an IDENTIFIER_NODE for the operator which is a
11825 human-readable spelling of the identifier, e.g., `operator +'. */
11827 static tree
11828 cp_parser_operator (cp_parser* parser)
11830 tree id = NULL_TREE;
11831 cp_token *token;
11833 /* Peek at the next token. */
11834 token = cp_lexer_peek_token (parser->lexer);
11835 /* Figure out which operator we have. */
11836 switch (token->type)
11838 case CPP_KEYWORD:
11840 enum tree_code op;
11842 /* The keyword should be either `new' or `delete'. */
11843 if (token->keyword == RID_NEW)
11844 op = NEW_EXPR;
11845 else if (token->keyword == RID_DELETE)
11846 op = DELETE_EXPR;
11847 else
11848 break;
11850 /* Consume the `new' or `delete' token. */
11851 cp_lexer_consume_token (parser->lexer);
11853 /* Peek at the next token. */
11854 token = cp_lexer_peek_token (parser->lexer);
11855 /* If it's a `[' token then this is the array variant of the
11856 operator. */
11857 if (token->type == CPP_OPEN_SQUARE)
11859 /* Consume the `[' token. */
11860 cp_lexer_consume_token (parser->lexer);
11861 /* Look for the `]' token. */
11862 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11863 id = ansi_opname (op == NEW_EXPR
11864 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11866 /* Otherwise, we have the non-array variant. */
11867 else
11868 id = ansi_opname (op);
11870 return id;
11873 case CPP_PLUS:
11874 id = ansi_opname (PLUS_EXPR);
11875 break;
11877 case CPP_MINUS:
11878 id = ansi_opname (MINUS_EXPR);
11879 break;
11881 case CPP_MULT:
11882 id = ansi_opname (MULT_EXPR);
11883 break;
11885 case CPP_DIV:
11886 id = ansi_opname (TRUNC_DIV_EXPR);
11887 break;
11889 case CPP_MOD:
11890 id = ansi_opname (TRUNC_MOD_EXPR);
11891 break;
11893 case CPP_XOR:
11894 id = ansi_opname (BIT_XOR_EXPR);
11895 break;
11897 case CPP_AND:
11898 id = ansi_opname (BIT_AND_EXPR);
11899 break;
11901 case CPP_OR:
11902 id = ansi_opname (BIT_IOR_EXPR);
11903 break;
11905 case CPP_COMPL:
11906 id = ansi_opname (BIT_NOT_EXPR);
11907 break;
11909 case CPP_NOT:
11910 id = ansi_opname (TRUTH_NOT_EXPR);
11911 break;
11913 case CPP_EQ:
11914 id = ansi_assopname (NOP_EXPR);
11915 break;
11917 case CPP_LESS:
11918 id = ansi_opname (LT_EXPR);
11919 break;
11921 case CPP_GREATER:
11922 id = ansi_opname (GT_EXPR);
11923 break;
11925 case CPP_PLUS_EQ:
11926 id = ansi_assopname (PLUS_EXPR);
11927 break;
11929 case CPP_MINUS_EQ:
11930 id = ansi_assopname (MINUS_EXPR);
11931 break;
11933 case CPP_MULT_EQ:
11934 id = ansi_assopname (MULT_EXPR);
11935 break;
11937 case CPP_DIV_EQ:
11938 id = ansi_assopname (TRUNC_DIV_EXPR);
11939 break;
11941 case CPP_MOD_EQ:
11942 id = ansi_assopname (TRUNC_MOD_EXPR);
11943 break;
11945 case CPP_XOR_EQ:
11946 id = ansi_assopname (BIT_XOR_EXPR);
11947 break;
11949 case CPP_AND_EQ:
11950 id = ansi_assopname (BIT_AND_EXPR);
11951 break;
11953 case CPP_OR_EQ:
11954 id = ansi_assopname (BIT_IOR_EXPR);
11955 break;
11957 case CPP_LSHIFT:
11958 id = ansi_opname (LSHIFT_EXPR);
11959 break;
11961 case CPP_RSHIFT:
11962 id = ansi_opname (RSHIFT_EXPR);
11963 break;
11965 case CPP_LSHIFT_EQ:
11966 id = ansi_assopname (LSHIFT_EXPR);
11967 break;
11969 case CPP_RSHIFT_EQ:
11970 id = ansi_assopname (RSHIFT_EXPR);
11971 break;
11973 case CPP_EQ_EQ:
11974 id = ansi_opname (EQ_EXPR);
11975 break;
11977 case CPP_NOT_EQ:
11978 id = ansi_opname (NE_EXPR);
11979 break;
11981 case CPP_LESS_EQ:
11982 id = ansi_opname (LE_EXPR);
11983 break;
11985 case CPP_GREATER_EQ:
11986 id = ansi_opname (GE_EXPR);
11987 break;
11989 case CPP_AND_AND:
11990 id = ansi_opname (TRUTH_ANDIF_EXPR);
11991 break;
11993 case CPP_OR_OR:
11994 id = ansi_opname (TRUTH_ORIF_EXPR);
11995 break;
11997 case CPP_PLUS_PLUS:
11998 id = ansi_opname (POSTINCREMENT_EXPR);
11999 break;
12001 case CPP_MINUS_MINUS:
12002 id = ansi_opname (PREDECREMENT_EXPR);
12003 break;
12005 case CPP_COMMA:
12006 id = ansi_opname (COMPOUND_EXPR);
12007 break;
12009 case CPP_DEREF_STAR:
12010 id = ansi_opname (MEMBER_REF);
12011 break;
12013 case CPP_DEREF:
12014 id = ansi_opname (COMPONENT_REF);
12015 break;
12017 case CPP_OPEN_PAREN:
12018 /* Consume the `('. */
12019 cp_lexer_consume_token (parser->lexer);
12020 /* Look for the matching `)'. */
12021 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12022 return ansi_opname (CALL_EXPR);
12024 case CPP_OPEN_SQUARE:
12025 /* Consume the `['. */
12026 cp_lexer_consume_token (parser->lexer);
12027 /* Look for the matching `]'. */
12028 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12029 return ansi_opname (ARRAY_REF);
12031 case CPP_STRING:
12032 if (cxx_dialect == cxx98)
12033 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12034 if (TREE_STRING_LENGTH (token->u.value) > 2)
12036 error ("expected empty string after %<operator%> keyword");
12037 return error_mark_node;
12039 /* Consume the string. */
12040 cp_lexer_consume_token (parser->lexer);
12041 /* Look for the suffix identifier. */
12042 token = cp_lexer_peek_token (parser->lexer);
12043 if (token->type == CPP_NAME)
12045 id = cp_parser_identifier (parser);
12046 if (id != error_mark_node)
12048 const char *name = IDENTIFIER_POINTER (id);
12049 return cp_literal_operator_id (name);
12052 else
12054 error ("expected suffix identifier");
12055 return error_mark_node;
12058 case CPP_STRING_USERDEF:
12059 error ("missing space between %<\"\"%> and suffix identifier");
12060 return error_mark_node;
12062 default:
12063 /* Anything else is an error. */
12064 break;
12067 /* If we have selected an identifier, we need to consume the
12068 operator token. */
12069 if (id)
12070 cp_lexer_consume_token (parser->lexer);
12071 /* Otherwise, no valid operator name was present. */
12072 else
12074 cp_parser_error (parser, "expected operator");
12075 id = error_mark_node;
12078 return id;
12081 /* Parse a template-declaration.
12083 template-declaration:
12084 export [opt] template < template-parameter-list > declaration
12086 If MEMBER_P is TRUE, this template-declaration occurs within a
12087 class-specifier.
12089 The grammar rule given by the standard isn't correct. What
12090 is really meant is:
12092 template-declaration:
12093 export [opt] template-parameter-list-seq
12094 decl-specifier-seq [opt] init-declarator [opt] ;
12095 export [opt] template-parameter-list-seq
12096 function-definition
12098 template-parameter-list-seq:
12099 template-parameter-list-seq [opt]
12100 template < template-parameter-list > */
12102 static void
12103 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12105 /* Check for `export'. */
12106 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12108 /* Consume the `export' token. */
12109 cp_lexer_consume_token (parser->lexer);
12110 /* Warn that we do not support `export'. */
12111 warning (0, "keyword %<export%> not implemented, and will be ignored");
12114 cp_parser_template_declaration_after_export (parser, member_p);
12117 /* Parse a template-parameter-list.
12119 template-parameter-list:
12120 template-parameter
12121 template-parameter-list , template-parameter
12123 Returns a TREE_LIST. Each node represents a template parameter.
12124 The nodes are connected via their TREE_CHAINs. */
12126 static tree
12127 cp_parser_template_parameter_list (cp_parser* parser)
12129 tree parameter_list = NULL_TREE;
12131 begin_template_parm_list ();
12133 /* The loop below parses the template parms. We first need to know
12134 the total number of template parms to be able to compute proper
12135 canonical types of each dependent type. So after the loop, when
12136 we know the total number of template parms,
12137 end_template_parm_list computes the proper canonical types and
12138 fixes up the dependent types accordingly. */
12139 while (true)
12141 tree parameter;
12142 bool is_non_type;
12143 bool is_parameter_pack;
12144 location_t parm_loc;
12146 /* Parse the template-parameter. */
12147 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12148 parameter = cp_parser_template_parameter (parser,
12149 &is_non_type,
12150 &is_parameter_pack);
12151 /* Add it to the list. */
12152 if (parameter != error_mark_node)
12153 parameter_list = process_template_parm (parameter_list,
12154 parm_loc,
12155 parameter,
12156 is_non_type,
12157 is_parameter_pack);
12158 else
12160 tree err_parm = build_tree_list (parameter, parameter);
12161 parameter_list = chainon (parameter_list, err_parm);
12164 /* If the next token is not a `,', we're done. */
12165 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12166 break;
12167 /* Otherwise, consume the `,' token. */
12168 cp_lexer_consume_token (parser->lexer);
12171 return end_template_parm_list (parameter_list);
12174 /* Parse a template-parameter.
12176 template-parameter:
12177 type-parameter
12178 parameter-declaration
12180 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12181 the parameter. The TREE_PURPOSE is the default value, if any.
12182 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12183 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12184 set to true iff this parameter is a parameter pack. */
12186 static tree
12187 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12188 bool *is_parameter_pack)
12190 cp_token *token;
12191 cp_parameter_declarator *parameter_declarator;
12192 cp_declarator *id_declarator;
12193 tree parm;
12195 /* Assume it is a type parameter or a template parameter. */
12196 *is_non_type = false;
12197 /* Assume it not a parameter pack. */
12198 *is_parameter_pack = false;
12199 /* Peek at the next token. */
12200 token = cp_lexer_peek_token (parser->lexer);
12201 /* If it is `class' or `template', we have a type-parameter. */
12202 if (token->keyword == RID_TEMPLATE)
12203 return cp_parser_type_parameter (parser, is_parameter_pack);
12204 /* If it is `class' or `typename' we do not know yet whether it is a
12205 type parameter or a non-type parameter. Consider:
12207 template <typename T, typename T::X X> ...
12211 template <class C, class D*> ...
12213 Here, the first parameter is a type parameter, and the second is
12214 a non-type parameter. We can tell by looking at the token after
12215 the identifier -- if it is a `,', `=', or `>' then we have a type
12216 parameter. */
12217 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12219 /* Peek at the token after `class' or `typename'. */
12220 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12221 /* If it's an ellipsis, we have a template type parameter
12222 pack. */
12223 if (token->type == CPP_ELLIPSIS)
12224 return cp_parser_type_parameter (parser, is_parameter_pack);
12225 /* If it's an identifier, skip it. */
12226 if (token->type == CPP_NAME)
12227 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12228 /* Now, see if the token looks like the end of a template
12229 parameter. */
12230 if (token->type == CPP_COMMA
12231 || token->type == CPP_EQ
12232 || token->type == CPP_GREATER)
12233 return cp_parser_type_parameter (parser, is_parameter_pack);
12236 /* Otherwise, it is a non-type parameter.
12238 [temp.param]
12240 When parsing a default template-argument for a non-type
12241 template-parameter, the first non-nested `>' is taken as the end
12242 of the template parameter-list rather than a greater-than
12243 operator. */
12244 *is_non_type = true;
12245 parameter_declarator
12246 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12247 /*parenthesized_p=*/NULL);
12249 /* If the parameter declaration is marked as a parameter pack, set
12250 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12251 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12252 grokdeclarator. */
12253 if (parameter_declarator
12254 && parameter_declarator->declarator
12255 && parameter_declarator->declarator->parameter_pack_p)
12257 *is_parameter_pack = true;
12258 parameter_declarator->declarator->parameter_pack_p = false;
12261 if (parameter_declarator
12262 && parameter_declarator->default_argument)
12264 /* Can happen in some cases of erroneous input (c++/34892). */
12265 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12266 /* Consume the `...' for better error recovery. */
12267 cp_lexer_consume_token (parser->lexer);
12269 /* If the next token is an ellipsis, and we don't already have it
12270 marked as a parameter pack, then we have a parameter pack (that
12271 has no declarator). */
12272 else if (!*is_parameter_pack
12273 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12274 && (declarator_can_be_parameter_pack
12275 (parameter_declarator->declarator)))
12277 /* Consume the `...'. */
12278 cp_lexer_consume_token (parser->lexer);
12279 maybe_warn_variadic_templates ();
12281 *is_parameter_pack = true;
12283 /* We might end up with a pack expansion as the type of the non-type
12284 template parameter, in which case this is a non-type template
12285 parameter pack. */
12286 else if (parameter_declarator
12287 && parameter_declarator->decl_specifiers.type
12288 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12290 *is_parameter_pack = true;
12291 parameter_declarator->decl_specifiers.type =
12292 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12295 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12297 /* Parameter packs cannot have default arguments. However, a
12298 user may try to do so, so we'll parse them and give an
12299 appropriate diagnostic here. */
12301 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12303 /* Find the name of the parameter pack. */
12304 id_declarator = parameter_declarator->declarator;
12305 while (id_declarator && id_declarator->kind != cdk_id)
12306 id_declarator = id_declarator->declarator;
12308 if (id_declarator && id_declarator->kind == cdk_id)
12309 error_at (start_token->location,
12310 "template parameter pack %qD cannot have a default argument",
12311 id_declarator->u.id.unqualified_name);
12312 else
12313 error_at (start_token->location,
12314 "template parameter pack cannot have a default argument");
12316 /* Parse the default argument, but throw away the result. */
12317 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12320 parm = grokdeclarator (parameter_declarator->declarator,
12321 &parameter_declarator->decl_specifiers,
12322 TPARM, /*initialized=*/0,
12323 /*attrlist=*/NULL);
12324 if (parm == error_mark_node)
12325 return error_mark_node;
12327 return build_tree_list (parameter_declarator->default_argument, parm);
12330 /* Parse a type-parameter.
12332 type-parameter:
12333 class identifier [opt]
12334 class identifier [opt] = type-id
12335 typename identifier [opt]
12336 typename identifier [opt] = type-id
12337 template < template-parameter-list > class identifier [opt]
12338 template < template-parameter-list > class identifier [opt]
12339 = id-expression
12341 GNU Extension (variadic templates):
12343 type-parameter:
12344 class ... identifier [opt]
12345 typename ... identifier [opt]
12347 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12348 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12349 the declaration of the parameter.
12351 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12353 static tree
12354 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12356 cp_token *token;
12357 tree parameter;
12359 /* Look for a keyword to tell us what kind of parameter this is. */
12360 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12361 if (!token)
12362 return error_mark_node;
12364 switch (token->keyword)
12366 case RID_CLASS:
12367 case RID_TYPENAME:
12369 tree identifier;
12370 tree default_argument;
12372 /* If the next token is an ellipsis, we have a template
12373 argument pack. */
12374 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12376 /* Consume the `...' token. */
12377 cp_lexer_consume_token (parser->lexer);
12378 maybe_warn_variadic_templates ();
12380 *is_parameter_pack = true;
12383 /* If the next token is an identifier, then it names the
12384 parameter. */
12385 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12386 identifier = cp_parser_identifier (parser);
12387 else
12388 identifier = NULL_TREE;
12390 /* Create the parameter. */
12391 parameter = finish_template_type_parm (class_type_node, identifier);
12393 /* If the next token is an `=', we have a default argument. */
12394 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12396 /* Consume the `=' token. */
12397 cp_lexer_consume_token (parser->lexer);
12398 /* Parse the default-argument. */
12399 push_deferring_access_checks (dk_no_deferred);
12400 default_argument = cp_parser_type_id (parser);
12402 /* Template parameter packs cannot have default
12403 arguments. */
12404 if (*is_parameter_pack)
12406 if (identifier)
12407 error_at (token->location,
12408 "template parameter pack %qD cannot have a "
12409 "default argument", identifier);
12410 else
12411 error_at (token->location,
12412 "template parameter packs cannot have "
12413 "default arguments");
12414 default_argument = NULL_TREE;
12416 pop_deferring_access_checks ();
12418 else
12419 default_argument = NULL_TREE;
12421 /* Create the combined representation of the parameter and the
12422 default argument. */
12423 parameter = build_tree_list (default_argument, parameter);
12425 break;
12427 case RID_TEMPLATE:
12429 tree identifier;
12430 tree default_argument;
12432 /* Look for the `<'. */
12433 cp_parser_require (parser, CPP_LESS, RT_LESS);
12434 /* Parse the template-parameter-list. */
12435 cp_parser_template_parameter_list (parser);
12436 /* Look for the `>'. */
12437 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12438 /* Look for the `class' keyword. */
12439 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12440 /* If the next token is an ellipsis, we have a template
12441 argument pack. */
12442 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12444 /* Consume the `...' token. */
12445 cp_lexer_consume_token (parser->lexer);
12446 maybe_warn_variadic_templates ();
12448 *is_parameter_pack = true;
12450 /* If the next token is an `=', then there is a
12451 default-argument. If the next token is a `>', we are at
12452 the end of the parameter-list. If the next token is a `,',
12453 then we are at the end of this parameter. */
12454 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12455 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12456 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12458 identifier = cp_parser_identifier (parser);
12459 /* Treat invalid names as if the parameter were nameless. */
12460 if (identifier == error_mark_node)
12461 identifier = NULL_TREE;
12463 else
12464 identifier = NULL_TREE;
12466 /* Create the template parameter. */
12467 parameter = finish_template_template_parm (class_type_node,
12468 identifier);
12470 /* If the next token is an `=', then there is a
12471 default-argument. */
12472 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12474 bool is_template;
12476 /* Consume the `='. */
12477 cp_lexer_consume_token (parser->lexer);
12478 /* Parse the id-expression. */
12479 push_deferring_access_checks (dk_no_deferred);
12480 /* save token before parsing the id-expression, for error
12481 reporting */
12482 token = cp_lexer_peek_token (parser->lexer);
12483 default_argument
12484 = cp_parser_id_expression (parser,
12485 /*template_keyword_p=*/false,
12486 /*check_dependency_p=*/true,
12487 /*template_p=*/&is_template,
12488 /*declarator_p=*/false,
12489 /*optional_p=*/false);
12490 if (TREE_CODE (default_argument) == TYPE_DECL)
12491 /* If the id-expression was a template-id that refers to
12492 a template-class, we already have the declaration here,
12493 so no further lookup is needed. */
12495 else
12496 /* Look up the name. */
12497 default_argument
12498 = cp_parser_lookup_name (parser, default_argument,
12499 none_type,
12500 /*is_template=*/is_template,
12501 /*is_namespace=*/false,
12502 /*check_dependency=*/true,
12503 /*ambiguous_decls=*/NULL,
12504 token->location);
12505 /* See if the default argument is valid. */
12506 default_argument
12507 = check_template_template_default_arg (default_argument);
12509 /* Template parameter packs cannot have default
12510 arguments. */
12511 if (*is_parameter_pack)
12513 if (identifier)
12514 error_at (token->location,
12515 "template parameter pack %qD cannot "
12516 "have a default argument",
12517 identifier);
12518 else
12519 error_at (token->location, "template parameter packs cannot "
12520 "have default arguments");
12521 default_argument = NULL_TREE;
12523 pop_deferring_access_checks ();
12525 else
12526 default_argument = NULL_TREE;
12528 /* Create the combined representation of the parameter and the
12529 default argument. */
12530 parameter = build_tree_list (default_argument, parameter);
12532 break;
12534 default:
12535 gcc_unreachable ();
12536 break;
12539 return parameter;
12542 /* Parse a template-id.
12544 template-id:
12545 template-name < template-argument-list [opt] >
12547 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12548 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12549 returned. Otherwise, if the template-name names a function, or set
12550 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12551 names a class, returns a TYPE_DECL for the specialization.
12553 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12554 uninstantiated templates. */
12556 static tree
12557 cp_parser_template_id (cp_parser *parser,
12558 bool template_keyword_p,
12559 bool check_dependency_p,
12560 enum tag_types tag_type,
12561 bool is_declaration)
12563 int i;
12564 tree templ;
12565 tree arguments;
12566 tree template_id;
12567 cp_token_position start_of_id = 0;
12568 deferred_access_check *chk;
12569 VEC (deferred_access_check,gc) *access_check;
12570 cp_token *next_token = NULL, *next_token_2 = NULL;
12571 bool is_identifier;
12573 /* If the next token corresponds to a template-id, there is no need
12574 to reparse it. */
12575 next_token = cp_lexer_peek_token (parser->lexer);
12576 if (next_token->type == CPP_TEMPLATE_ID)
12578 struct tree_check *check_value;
12580 /* Get the stored value. */
12581 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12582 /* Perform any access checks that were deferred. */
12583 access_check = check_value->checks;
12584 if (access_check)
12586 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12587 perform_or_defer_access_check (chk->binfo,
12588 chk->decl,
12589 chk->diag_decl,
12590 tf_warning_or_error);
12592 /* Return the stored value. */
12593 return check_value->value;
12596 /* Avoid performing name lookup if there is no possibility of
12597 finding a template-id. */
12598 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12599 || (next_token->type == CPP_NAME
12600 && !cp_parser_nth_token_starts_template_argument_list_p
12601 (parser, 2)))
12603 cp_parser_error (parser, "expected template-id");
12604 return error_mark_node;
12607 /* Remember where the template-id starts. */
12608 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12609 start_of_id = cp_lexer_token_position (parser->lexer, false);
12611 push_deferring_access_checks (dk_deferred);
12613 /* Parse the template-name. */
12614 is_identifier = false;
12615 templ = cp_parser_template_name (parser, template_keyword_p,
12616 check_dependency_p,
12617 is_declaration,
12618 tag_type,
12619 &is_identifier);
12620 if (templ == error_mark_node || is_identifier)
12622 pop_deferring_access_checks ();
12623 return templ;
12626 /* If we find the sequence `[:' after a template-name, it's probably
12627 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12628 parse correctly the argument list. */
12629 next_token = cp_lexer_peek_token (parser->lexer);
12630 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12631 if (next_token->type == CPP_OPEN_SQUARE
12632 && next_token->flags & DIGRAPH
12633 && next_token_2->type == CPP_COLON
12634 && !(next_token_2->flags & PREV_WHITE))
12636 cp_parser_parse_tentatively (parser);
12637 /* Change `:' into `::'. */
12638 next_token_2->type = CPP_SCOPE;
12639 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12640 CPP_LESS. */
12641 cp_lexer_consume_token (parser->lexer);
12643 /* Parse the arguments. */
12644 arguments = cp_parser_enclosed_template_argument_list (parser);
12645 if (!cp_parser_parse_definitely (parser))
12647 /* If we couldn't parse an argument list, then we revert our changes
12648 and return simply an error. Maybe this is not a template-id
12649 after all. */
12650 next_token_2->type = CPP_COLON;
12651 cp_parser_error (parser, "expected %<<%>");
12652 pop_deferring_access_checks ();
12653 return error_mark_node;
12655 /* Otherwise, emit an error about the invalid digraph, but continue
12656 parsing because we got our argument list. In C++11 do not emit
12657 any error, per 2.5/3. */
12658 if (cxx_dialect < cxx0x
12659 && permerror (next_token->location,
12660 "%<<::%> cannot begin a template-argument list"))
12662 static bool hint = false;
12663 inform (next_token->location,
12664 "%<<:%> is an alternate spelling for %<[%>."
12665 " Insert whitespace between %<<%> and %<::%>");
12666 if (!hint && !flag_permissive)
12668 inform (next_token->location, "(if you use %<-fpermissive%> "
12669 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
12670 "accept your code)");
12671 hint = true;
12675 else
12677 /* Look for the `<' that starts the template-argument-list. */
12678 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12680 pop_deferring_access_checks ();
12681 return error_mark_node;
12683 /* Parse the arguments. */
12684 arguments = cp_parser_enclosed_template_argument_list (parser);
12687 /* Build a representation of the specialization. */
12688 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12689 template_id = build_min_nt_loc (next_token->location,
12690 TEMPLATE_ID_EXPR,
12691 templ, arguments);
12692 else if (DECL_TYPE_TEMPLATE_P (templ)
12693 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12695 bool entering_scope;
12696 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12697 template (rather than some instantiation thereof) only if
12698 is not nested within some other construct. For example, in
12699 "template <typename T> void f(T) { A<T>::", A<T> is just an
12700 instantiation of A. */
12701 entering_scope = (template_parm_scope_p ()
12702 && cp_lexer_next_token_is (parser->lexer,
12703 CPP_SCOPE));
12704 template_id
12705 = finish_template_type (templ, arguments, entering_scope);
12707 else
12709 /* If it's not a class-template or a template-template, it should be
12710 a function-template. */
12711 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12712 || TREE_CODE (templ) == OVERLOAD
12713 || BASELINK_P (templ)));
12715 template_id = lookup_template_function (templ, arguments);
12718 /* If parsing tentatively, replace the sequence of tokens that makes
12719 up the template-id with a CPP_TEMPLATE_ID token. That way,
12720 should we re-parse the token stream, we will not have to repeat
12721 the effort required to do the parse, nor will we issue duplicate
12722 error messages about problems during instantiation of the
12723 template. */
12724 if (start_of_id)
12726 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12728 /* Reset the contents of the START_OF_ID token. */
12729 token->type = CPP_TEMPLATE_ID;
12730 /* Retrieve any deferred checks. Do not pop this access checks yet
12731 so the memory will not be reclaimed during token replacing below. */
12732 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12733 token->u.tree_check_value->value = template_id;
12734 token->u.tree_check_value->checks = get_deferred_access_checks ();
12735 token->keyword = RID_MAX;
12737 /* Purge all subsequent tokens. */
12738 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12740 /* ??? Can we actually assume that, if template_id ==
12741 error_mark_node, we will have issued a diagnostic to the
12742 user, as opposed to simply marking the tentative parse as
12743 failed? */
12744 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12745 error_at (token->location, "parse error in template argument list");
12748 pop_deferring_access_checks ();
12749 return template_id;
12752 /* Parse a template-name.
12754 template-name:
12755 identifier
12757 The standard should actually say:
12759 template-name:
12760 identifier
12761 operator-function-id
12763 A defect report has been filed about this issue.
12765 A conversion-function-id cannot be a template name because they cannot
12766 be part of a template-id. In fact, looking at this code:
12768 a.operator K<int>()
12770 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12771 It is impossible to call a templated conversion-function-id with an
12772 explicit argument list, since the only allowed template parameter is
12773 the type to which it is converting.
12775 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12776 `template' keyword, in a construction like:
12778 T::template f<3>()
12780 In that case `f' is taken to be a template-name, even though there
12781 is no way of knowing for sure.
12783 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12784 name refers to a set of overloaded functions, at least one of which
12785 is a template, or an IDENTIFIER_NODE with the name of the template,
12786 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12787 names are looked up inside uninstantiated templates. */
12789 static tree
12790 cp_parser_template_name (cp_parser* parser,
12791 bool template_keyword_p,
12792 bool check_dependency_p,
12793 bool is_declaration,
12794 enum tag_types tag_type,
12795 bool *is_identifier)
12797 tree identifier;
12798 tree decl;
12799 tree fns;
12800 cp_token *token = cp_lexer_peek_token (parser->lexer);
12802 /* If the next token is `operator', then we have either an
12803 operator-function-id or a conversion-function-id. */
12804 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12806 /* We don't know whether we're looking at an
12807 operator-function-id or a conversion-function-id. */
12808 cp_parser_parse_tentatively (parser);
12809 /* Try an operator-function-id. */
12810 identifier = cp_parser_operator_function_id (parser);
12811 /* If that didn't work, try a conversion-function-id. */
12812 if (!cp_parser_parse_definitely (parser))
12814 cp_parser_error (parser, "expected template-name");
12815 return error_mark_node;
12818 /* Look for the identifier. */
12819 else
12820 identifier = cp_parser_identifier (parser);
12822 /* If we didn't find an identifier, we don't have a template-id. */
12823 if (identifier == error_mark_node)
12824 return error_mark_node;
12826 /* If the name immediately followed the `template' keyword, then it
12827 is a template-name. However, if the next token is not `<', then
12828 we do not treat it as a template-name, since it is not being used
12829 as part of a template-id. This enables us to handle constructs
12830 like:
12832 template <typename T> struct S { S(); };
12833 template <typename T> S<T>::S();
12835 correctly. We would treat `S' as a template -- if it were `S<T>'
12836 -- but we do not if there is no `<'. */
12838 if (processing_template_decl
12839 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12841 /* In a declaration, in a dependent context, we pretend that the
12842 "template" keyword was present in order to improve error
12843 recovery. For example, given:
12845 template <typename T> void f(T::X<int>);
12847 we want to treat "X<int>" as a template-id. */
12848 if (is_declaration
12849 && !template_keyword_p
12850 && parser->scope && TYPE_P (parser->scope)
12851 && check_dependency_p
12852 && dependent_scope_p (parser->scope)
12853 /* Do not do this for dtors (or ctors), since they never
12854 need the template keyword before their name. */
12855 && !constructor_name_p (identifier, parser->scope))
12857 cp_token_position start = 0;
12859 /* Explain what went wrong. */
12860 error_at (token->location, "non-template %qD used as template",
12861 identifier);
12862 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12863 parser->scope, identifier);
12864 /* If parsing tentatively, find the location of the "<" token. */
12865 if (cp_parser_simulate_error (parser))
12866 start = cp_lexer_token_position (parser->lexer, true);
12867 /* Parse the template arguments so that we can issue error
12868 messages about them. */
12869 cp_lexer_consume_token (parser->lexer);
12870 cp_parser_enclosed_template_argument_list (parser);
12871 /* Skip tokens until we find a good place from which to
12872 continue parsing. */
12873 cp_parser_skip_to_closing_parenthesis (parser,
12874 /*recovering=*/true,
12875 /*or_comma=*/true,
12876 /*consume_paren=*/false);
12877 /* If parsing tentatively, permanently remove the
12878 template argument list. That will prevent duplicate
12879 error messages from being issued about the missing
12880 "template" keyword. */
12881 if (start)
12882 cp_lexer_purge_tokens_after (parser->lexer, start);
12883 if (is_identifier)
12884 *is_identifier = true;
12885 return identifier;
12888 /* If the "template" keyword is present, then there is generally
12889 no point in doing name-lookup, so we just return IDENTIFIER.
12890 But, if the qualifying scope is non-dependent then we can
12891 (and must) do name-lookup normally. */
12892 if (template_keyword_p
12893 && (!parser->scope
12894 || (TYPE_P (parser->scope)
12895 && dependent_type_p (parser->scope))))
12896 return identifier;
12899 /* Look up the name. */
12900 decl = cp_parser_lookup_name (parser, identifier,
12901 tag_type,
12902 /*is_template=*/true,
12903 /*is_namespace=*/false,
12904 check_dependency_p,
12905 /*ambiguous_decls=*/NULL,
12906 token->location);
12908 /* If DECL is a template, then the name was a template-name. */
12909 if (TREE_CODE (decl) == TEMPLATE_DECL)
12911 else
12913 tree fn = NULL_TREE;
12915 /* The standard does not explicitly indicate whether a name that
12916 names a set of overloaded declarations, some of which are
12917 templates, is a template-name. However, such a name should
12918 be a template-name; otherwise, there is no way to form a
12919 template-id for the overloaded templates. */
12920 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12921 if (TREE_CODE (fns) == OVERLOAD)
12922 for (fn = fns; fn; fn = OVL_NEXT (fn))
12923 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12924 break;
12926 if (!fn)
12928 /* The name does not name a template. */
12929 cp_parser_error (parser, "expected template-name");
12930 return error_mark_node;
12934 /* If DECL is dependent, and refers to a function, then just return
12935 its name; we will look it up again during template instantiation. */
12936 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12938 tree scope = ovl_scope (decl);
12939 if (TYPE_P (scope) && dependent_type_p (scope))
12940 return identifier;
12943 return decl;
12946 /* Parse a template-argument-list.
12948 template-argument-list:
12949 template-argument ... [opt]
12950 template-argument-list , template-argument ... [opt]
12952 Returns a TREE_VEC containing the arguments. */
12954 static tree
12955 cp_parser_template_argument_list (cp_parser* parser)
12957 tree fixed_args[10];
12958 unsigned n_args = 0;
12959 unsigned alloced = 10;
12960 tree *arg_ary = fixed_args;
12961 tree vec;
12962 bool saved_in_template_argument_list_p;
12963 bool saved_ice_p;
12964 bool saved_non_ice_p;
12966 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12967 parser->in_template_argument_list_p = true;
12968 /* Even if the template-id appears in an integral
12969 constant-expression, the contents of the argument list do
12970 not. */
12971 saved_ice_p = parser->integral_constant_expression_p;
12972 parser->integral_constant_expression_p = false;
12973 saved_non_ice_p = parser->non_integral_constant_expression_p;
12974 parser->non_integral_constant_expression_p = false;
12976 /* Parse the arguments. */
12979 tree argument;
12981 if (n_args)
12982 /* Consume the comma. */
12983 cp_lexer_consume_token (parser->lexer);
12985 /* Parse the template-argument. */
12986 argument = cp_parser_template_argument (parser);
12988 /* If the next token is an ellipsis, we're expanding a template
12989 argument pack. */
12990 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12992 if (argument == error_mark_node)
12994 cp_token *token = cp_lexer_peek_token (parser->lexer);
12995 error_at (token->location,
12996 "expected parameter pack before %<...%>");
12998 /* Consume the `...' token. */
12999 cp_lexer_consume_token (parser->lexer);
13001 /* Make the argument into a TYPE_PACK_EXPANSION or
13002 EXPR_PACK_EXPANSION. */
13003 argument = make_pack_expansion (argument);
13006 if (n_args == alloced)
13008 alloced *= 2;
13010 if (arg_ary == fixed_args)
13012 arg_ary = XNEWVEC (tree, alloced);
13013 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13015 else
13016 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13018 arg_ary[n_args++] = argument;
13020 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13022 vec = make_tree_vec (n_args);
13024 while (n_args--)
13025 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13027 if (arg_ary != fixed_args)
13028 free (arg_ary);
13029 parser->non_integral_constant_expression_p = saved_non_ice_p;
13030 parser->integral_constant_expression_p = saved_ice_p;
13031 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13032 #ifdef ENABLE_CHECKING
13033 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13034 #endif
13035 return vec;
13038 /* Parse a template-argument.
13040 template-argument:
13041 assignment-expression
13042 type-id
13043 id-expression
13045 The representation is that of an assignment-expression, type-id, or
13046 id-expression -- except that the qualified id-expression is
13047 evaluated, so that the value returned is either a DECL or an
13048 OVERLOAD.
13050 Although the standard says "assignment-expression", it forbids
13051 throw-expressions or assignments in the template argument.
13052 Therefore, we use "conditional-expression" instead. */
13054 static tree
13055 cp_parser_template_argument (cp_parser* parser)
13057 tree argument;
13058 bool template_p;
13059 bool address_p;
13060 bool maybe_type_id = false;
13061 cp_token *token = NULL, *argument_start_token = NULL;
13062 location_t loc = 0;
13063 cp_id_kind idk;
13065 /* There's really no way to know what we're looking at, so we just
13066 try each alternative in order.
13068 [temp.arg]
13070 In a template-argument, an ambiguity between a type-id and an
13071 expression is resolved to a type-id, regardless of the form of
13072 the corresponding template-parameter.
13074 Therefore, we try a type-id first. */
13075 cp_parser_parse_tentatively (parser);
13076 argument = cp_parser_template_type_arg (parser);
13077 /* If there was no error parsing the type-id but the next token is a
13078 '>>', our behavior depends on which dialect of C++ we're
13079 parsing. In C++98, we probably found a typo for '> >'. But there
13080 are type-id which are also valid expressions. For instance:
13082 struct X { int operator >> (int); };
13083 template <int V> struct Foo {};
13084 Foo<X () >> 5> r;
13086 Here 'X()' is a valid type-id of a function type, but the user just
13087 wanted to write the expression "X() >> 5". Thus, we remember that we
13088 found a valid type-id, but we still try to parse the argument as an
13089 expression to see what happens.
13091 In C++0x, the '>>' will be considered two separate '>'
13092 tokens. */
13093 if (!cp_parser_error_occurred (parser)
13094 && cxx_dialect == cxx98
13095 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13097 maybe_type_id = true;
13098 cp_parser_abort_tentative_parse (parser);
13100 else
13102 /* If the next token isn't a `,' or a `>', then this argument wasn't
13103 really finished. This means that the argument is not a valid
13104 type-id. */
13105 if (!cp_parser_next_token_ends_template_argument_p (parser))
13106 cp_parser_error (parser, "expected template-argument");
13107 /* If that worked, we're done. */
13108 if (cp_parser_parse_definitely (parser))
13109 return argument;
13111 /* We're still not sure what the argument will be. */
13112 cp_parser_parse_tentatively (parser);
13113 /* Try a template. */
13114 argument_start_token = cp_lexer_peek_token (parser->lexer);
13115 argument = cp_parser_id_expression (parser,
13116 /*template_keyword_p=*/false,
13117 /*check_dependency_p=*/true,
13118 &template_p,
13119 /*declarator_p=*/false,
13120 /*optional_p=*/false);
13121 /* If the next token isn't a `,' or a `>', then this argument wasn't
13122 really finished. */
13123 if (!cp_parser_next_token_ends_template_argument_p (parser))
13124 cp_parser_error (parser, "expected template-argument");
13125 if (!cp_parser_error_occurred (parser))
13127 /* Figure out what is being referred to. If the id-expression
13128 was for a class template specialization, then we will have a
13129 TYPE_DECL at this point. There is no need to do name lookup
13130 at this point in that case. */
13131 if (TREE_CODE (argument) != TYPE_DECL)
13132 argument = cp_parser_lookup_name (parser, argument,
13133 none_type,
13134 /*is_template=*/template_p,
13135 /*is_namespace=*/false,
13136 /*check_dependency=*/true,
13137 /*ambiguous_decls=*/NULL,
13138 argument_start_token->location);
13139 if (TREE_CODE (argument) != TEMPLATE_DECL
13140 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13141 cp_parser_error (parser, "expected template-name");
13143 if (cp_parser_parse_definitely (parser))
13144 return argument;
13145 /* It must be a non-type argument. There permitted cases are given
13146 in [temp.arg.nontype]:
13148 -- an integral constant-expression of integral or enumeration
13149 type; or
13151 -- the name of a non-type template-parameter; or
13153 -- the name of an object or function with external linkage...
13155 -- the address of an object or function with external linkage...
13157 -- a pointer to member... */
13158 /* Look for a non-type template parameter. */
13159 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13161 cp_parser_parse_tentatively (parser);
13162 argument = cp_parser_primary_expression (parser,
13163 /*address_p=*/false,
13164 /*cast_p=*/false,
13165 /*template_arg_p=*/true,
13166 &idk);
13167 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13168 || !cp_parser_next_token_ends_template_argument_p (parser))
13169 cp_parser_simulate_error (parser);
13170 if (cp_parser_parse_definitely (parser))
13171 return argument;
13174 /* If the next token is "&", the argument must be the address of an
13175 object or function with external linkage. */
13176 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13177 if (address_p)
13179 loc = cp_lexer_peek_token (parser->lexer)->location;
13180 cp_lexer_consume_token (parser->lexer);
13182 /* See if we might have an id-expression. */
13183 token = cp_lexer_peek_token (parser->lexer);
13184 if (token->type == CPP_NAME
13185 || token->keyword == RID_OPERATOR
13186 || token->type == CPP_SCOPE
13187 || token->type == CPP_TEMPLATE_ID
13188 || token->type == CPP_NESTED_NAME_SPECIFIER)
13190 cp_parser_parse_tentatively (parser);
13191 argument = cp_parser_primary_expression (parser,
13192 address_p,
13193 /*cast_p=*/false,
13194 /*template_arg_p=*/true,
13195 &idk);
13196 if (cp_parser_error_occurred (parser)
13197 || !cp_parser_next_token_ends_template_argument_p (parser))
13198 cp_parser_abort_tentative_parse (parser);
13199 else
13201 tree probe;
13203 if (TREE_CODE (argument) == INDIRECT_REF)
13205 gcc_assert (REFERENCE_REF_P (argument));
13206 argument = TREE_OPERAND (argument, 0);
13209 /* If we're in a template, we represent a qualified-id referring
13210 to a static data member as a SCOPE_REF even if the scope isn't
13211 dependent so that we can check access control later. */
13212 probe = argument;
13213 if (TREE_CODE (probe) == SCOPE_REF)
13214 probe = TREE_OPERAND (probe, 1);
13215 if (TREE_CODE (probe) == VAR_DECL)
13217 /* A variable without external linkage might still be a
13218 valid constant-expression, so no error is issued here
13219 if the external-linkage check fails. */
13220 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13221 cp_parser_simulate_error (parser);
13223 else if (is_overloaded_fn (argument))
13224 /* All overloaded functions are allowed; if the external
13225 linkage test does not pass, an error will be issued
13226 later. */
13228 else if (address_p
13229 && (TREE_CODE (argument) == OFFSET_REF
13230 || TREE_CODE (argument) == SCOPE_REF))
13231 /* A pointer-to-member. */
13233 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13235 else
13236 cp_parser_simulate_error (parser);
13238 if (cp_parser_parse_definitely (parser))
13240 if (address_p)
13241 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13242 tf_warning_or_error);
13243 return argument;
13247 /* If the argument started with "&", there are no other valid
13248 alternatives at this point. */
13249 if (address_p)
13251 cp_parser_error (parser, "invalid non-type template argument");
13252 return error_mark_node;
13255 /* If the argument wasn't successfully parsed as a type-id followed
13256 by '>>', the argument can only be a constant expression now.
13257 Otherwise, we try parsing the constant-expression tentatively,
13258 because the argument could really be a type-id. */
13259 if (maybe_type_id)
13260 cp_parser_parse_tentatively (parser);
13261 argument = cp_parser_constant_expression (parser,
13262 /*allow_non_constant_p=*/false,
13263 /*non_constant_p=*/NULL);
13264 argument = fold_non_dependent_expr (argument);
13265 if (!maybe_type_id)
13266 return argument;
13267 if (!cp_parser_next_token_ends_template_argument_p (parser))
13268 cp_parser_error (parser, "expected template-argument");
13269 if (cp_parser_parse_definitely (parser))
13270 return argument;
13271 /* We did our best to parse the argument as a non type-id, but that
13272 was the only alternative that matched (albeit with a '>' after
13273 it). We can assume it's just a typo from the user, and a
13274 diagnostic will then be issued. */
13275 return cp_parser_template_type_arg (parser);
13278 /* Parse an explicit-instantiation.
13280 explicit-instantiation:
13281 template declaration
13283 Although the standard says `declaration', what it really means is:
13285 explicit-instantiation:
13286 template decl-specifier-seq [opt] declarator [opt] ;
13288 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13289 supposed to be allowed. A defect report has been filed about this
13290 issue.
13292 GNU Extension:
13294 explicit-instantiation:
13295 storage-class-specifier template
13296 decl-specifier-seq [opt] declarator [opt] ;
13297 function-specifier template
13298 decl-specifier-seq [opt] declarator [opt] ; */
13300 static void
13301 cp_parser_explicit_instantiation (cp_parser* parser)
13303 int declares_class_or_enum;
13304 cp_decl_specifier_seq decl_specifiers;
13305 tree extension_specifier = NULL_TREE;
13307 timevar_push (TV_TEMPLATE_INST);
13309 /* Look for an (optional) storage-class-specifier or
13310 function-specifier. */
13311 if (cp_parser_allow_gnu_extensions_p (parser))
13313 extension_specifier
13314 = cp_parser_storage_class_specifier_opt (parser);
13315 if (!extension_specifier)
13316 extension_specifier
13317 = cp_parser_function_specifier_opt (parser,
13318 /*decl_specs=*/NULL);
13321 /* Look for the `template' keyword. */
13322 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13323 /* Let the front end know that we are processing an explicit
13324 instantiation. */
13325 begin_explicit_instantiation ();
13326 /* [temp.explicit] says that we are supposed to ignore access
13327 control while processing explicit instantiation directives. */
13328 push_deferring_access_checks (dk_no_check);
13329 /* Parse a decl-specifier-seq. */
13330 cp_parser_decl_specifier_seq (parser,
13331 CP_PARSER_FLAGS_OPTIONAL,
13332 &decl_specifiers,
13333 &declares_class_or_enum);
13334 /* If there was exactly one decl-specifier, and it declared a class,
13335 and there's no declarator, then we have an explicit type
13336 instantiation. */
13337 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13339 tree type;
13341 type = check_tag_decl (&decl_specifiers,
13342 /*explicit_type_instantiation_p=*/true);
13343 /* Turn access control back on for names used during
13344 template instantiation. */
13345 pop_deferring_access_checks ();
13346 if (type)
13347 do_type_instantiation (type, extension_specifier,
13348 /*complain=*/tf_error);
13350 else
13352 cp_declarator *declarator;
13353 tree decl;
13355 /* Parse the declarator. */
13356 declarator
13357 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13358 /*ctor_dtor_or_conv_p=*/NULL,
13359 /*parenthesized_p=*/NULL,
13360 /*member_p=*/false);
13361 if (declares_class_or_enum & 2)
13362 cp_parser_check_for_definition_in_return_type (declarator,
13363 decl_specifiers.type,
13364 decl_specifiers.locations[ds_type_spec]);
13365 if (declarator != cp_error_declarator)
13367 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13368 permerror (decl_specifiers.locations[ds_inline],
13369 "explicit instantiation shall not use"
13370 " %<inline%> specifier");
13371 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13372 permerror (decl_specifiers.locations[ds_constexpr],
13373 "explicit instantiation shall not use"
13374 " %<constexpr%> specifier");
13376 decl = grokdeclarator (declarator, &decl_specifiers,
13377 NORMAL, 0, &decl_specifiers.attributes);
13378 /* Turn access control back on for names used during
13379 template instantiation. */
13380 pop_deferring_access_checks ();
13381 /* Do the explicit instantiation. */
13382 do_decl_instantiation (decl, extension_specifier);
13384 else
13386 pop_deferring_access_checks ();
13387 /* Skip the body of the explicit instantiation. */
13388 cp_parser_skip_to_end_of_statement (parser);
13391 /* We're done with the instantiation. */
13392 end_explicit_instantiation ();
13394 cp_parser_consume_semicolon_at_end_of_statement (parser);
13396 timevar_pop (TV_TEMPLATE_INST);
13399 /* Parse an explicit-specialization.
13401 explicit-specialization:
13402 template < > declaration
13404 Although the standard says `declaration', what it really means is:
13406 explicit-specialization:
13407 template <> decl-specifier [opt] init-declarator [opt] ;
13408 template <> function-definition
13409 template <> explicit-specialization
13410 template <> template-declaration */
13412 static void
13413 cp_parser_explicit_specialization (cp_parser* parser)
13415 bool need_lang_pop;
13416 cp_token *token = cp_lexer_peek_token (parser->lexer);
13418 /* Look for the `template' keyword. */
13419 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13420 /* Look for the `<'. */
13421 cp_parser_require (parser, CPP_LESS, RT_LESS);
13422 /* Look for the `>'. */
13423 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13424 /* We have processed another parameter list. */
13425 ++parser->num_template_parameter_lists;
13426 /* [temp]
13428 A template ... explicit specialization ... shall not have C
13429 linkage. */
13430 if (current_lang_name == lang_name_c)
13432 error_at (token->location, "template specialization with C linkage");
13433 /* Give it C++ linkage to avoid confusing other parts of the
13434 front end. */
13435 push_lang_context (lang_name_cplusplus);
13436 need_lang_pop = true;
13438 else
13439 need_lang_pop = false;
13440 /* Let the front end know that we are beginning a specialization. */
13441 if (!begin_specialization ())
13443 end_specialization ();
13444 return;
13447 /* If the next keyword is `template', we need to figure out whether
13448 or not we're looking a template-declaration. */
13449 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13451 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13452 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13453 cp_parser_template_declaration_after_export (parser,
13454 /*member_p=*/false);
13455 else
13456 cp_parser_explicit_specialization (parser);
13458 else
13459 /* Parse the dependent declaration. */
13460 cp_parser_single_declaration (parser,
13461 /*checks=*/NULL,
13462 /*member_p=*/false,
13463 /*explicit_specialization_p=*/true,
13464 /*friend_p=*/NULL);
13465 /* We're done with the specialization. */
13466 end_specialization ();
13467 /* For the erroneous case of a template with C linkage, we pushed an
13468 implicit C++ linkage scope; exit that scope now. */
13469 if (need_lang_pop)
13470 pop_lang_context ();
13471 /* We're done with this parameter list. */
13472 --parser->num_template_parameter_lists;
13475 /* Parse a type-specifier.
13477 type-specifier:
13478 simple-type-specifier
13479 class-specifier
13480 enum-specifier
13481 elaborated-type-specifier
13482 cv-qualifier
13484 GNU Extension:
13486 type-specifier:
13487 __complex__
13489 Returns a representation of the type-specifier. For a
13490 class-specifier, enum-specifier, or elaborated-type-specifier, a
13491 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13493 The parser flags FLAGS is used to control type-specifier parsing.
13495 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13496 in a decl-specifier-seq.
13498 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13499 class-specifier, enum-specifier, or elaborated-type-specifier, then
13500 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13501 if a type is declared; 2 if it is defined. Otherwise, it is set to
13502 zero.
13504 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13505 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13506 is set to FALSE. */
13508 static tree
13509 cp_parser_type_specifier (cp_parser* parser,
13510 cp_parser_flags flags,
13511 cp_decl_specifier_seq *decl_specs,
13512 bool is_declaration,
13513 int* declares_class_or_enum,
13514 bool* is_cv_qualifier)
13516 tree type_spec = NULL_TREE;
13517 cp_token *token;
13518 enum rid keyword;
13519 cp_decl_spec ds = ds_last;
13521 /* Assume this type-specifier does not declare a new type. */
13522 if (declares_class_or_enum)
13523 *declares_class_or_enum = 0;
13524 /* And that it does not specify a cv-qualifier. */
13525 if (is_cv_qualifier)
13526 *is_cv_qualifier = false;
13527 /* Peek at the next token. */
13528 token = cp_lexer_peek_token (parser->lexer);
13530 /* If we're looking at a keyword, we can use that to guide the
13531 production we choose. */
13532 keyword = token->keyword;
13533 switch (keyword)
13535 case RID_ENUM:
13536 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13537 goto elaborated_type_specifier;
13539 /* Look for the enum-specifier. */
13540 type_spec = cp_parser_enum_specifier (parser);
13541 /* If that worked, we're done. */
13542 if (type_spec)
13544 if (declares_class_or_enum)
13545 *declares_class_or_enum = 2;
13546 if (decl_specs)
13547 cp_parser_set_decl_spec_type (decl_specs,
13548 type_spec,
13549 token,
13550 /*type_definition_p=*/true);
13551 return type_spec;
13553 else
13554 goto elaborated_type_specifier;
13556 /* Any of these indicate either a class-specifier, or an
13557 elaborated-type-specifier. */
13558 case RID_CLASS:
13559 case RID_STRUCT:
13560 case RID_UNION:
13561 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13562 goto elaborated_type_specifier;
13564 /* Parse tentatively so that we can back up if we don't find a
13565 class-specifier. */
13566 cp_parser_parse_tentatively (parser);
13567 /* Look for the class-specifier. */
13568 type_spec = cp_parser_class_specifier (parser);
13569 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13570 /* If that worked, we're done. */
13571 if (cp_parser_parse_definitely (parser))
13573 if (declares_class_or_enum)
13574 *declares_class_or_enum = 2;
13575 if (decl_specs)
13576 cp_parser_set_decl_spec_type (decl_specs,
13577 type_spec,
13578 token,
13579 /*type_definition_p=*/true);
13580 return type_spec;
13583 /* Fall through. */
13584 elaborated_type_specifier:
13585 /* We're declaring (not defining) a class or enum. */
13586 if (declares_class_or_enum)
13587 *declares_class_or_enum = 1;
13589 /* Fall through. */
13590 case RID_TYPENAME:
13591 /* Look for an elaborated-type-specifier. */
13592 type_spec
13593 = (cp_parser_elaborated_type_specifier
13594 (parser,
13595 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
13596 is_declaration));
13597 if (decl_specs)
13598 cp_parser_set_decl_spec_type (decl_specs,
13599 type_spec,
13600 token,
13601 /*type_definition_p=*/false);
13602 return type_spec;
13604 case RID_CONST:
13605 ds = ds_const;
13606 if (is_cv_qualifier)
13607 *is_cv_qualifier = true;
13608 break;
13610 case RID_VOLATILE:
13611 ds = ds_volatile;
13612 if (is_cv_qualifier)
13613 *is_cv_qualifier = true;
13614 break;
13616 case RID_RESTRICT:
13617 ds = ds_restrict;
13618 if (is_cv_qualifier)
13619 *is_cv_qualifier = true;
13620 break;
13622 case RID_COMPLEX:
13623 /* The `__complex__' keyword is a GNU extension. */
13624 ds = ds_complex;
13625 break;
13627 default:
13628 break;
13631 /* Handle simple keywords. */
13632 if (ds != ds_last)
13634 if (decl_specs)
13636 set_and_check_decl_spec_loc (decl_specs, ds, token);
13637 decl_specs->any_specifiers_p = true;
13639 return cp_lexer_consume_token (parser->lexer)->u.value;
13642 /* If we do not already have a type-specifier, assume we are looking
13643 at a simple-type-specifier. */
13644 type_spec = cp_parser_simple_type_specifier (parser,
13645 decl_specs,
13646 flags);
13648 /* If we didn't find a type-specifier, and a type-specifier was not
13649 optional in this context, issue an error message. */
13650 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13652 cp_parser_error (parser, "expected type specifier");
13653 return error_mark_node;
13656 return type_spec;
13659 /* Parse a simple-type-specifier.
13661 simple-type-specifier:
13662 :: [opt] nested-name-specifier [opt] type-name
13663 :: [opt] nested-name-specifier template template-id
13664 char
13665 wchar_t
13666 bool
13667 short
13669 long
13670 signed
13671 unsigned
13672 float
13673 double
13674 void
13676 C++0x Extension:
13678 simple-type-specifier:
13679 auto
13680 decltype ( expression )
13681 char16_t
13682 char32_t
13683 __underlying_type ( type-id )
13685 GNU Extension:
13687 simple-type-specifier:
13688 __int128
13689 __typeof__ unary-expression
13690 __typeof__ ( type-id )
13692 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13693 appropriately updated. */
13695 static tree
13696 cp_parser_simple_type_specifier (cp_parser* parser,
13697 cp_decl_specifier_seq *decl_specs,
13698 cp_parser_flags flags)
13700 tree type = NULL_TREE;
13701 cp_token *token;
13703 /* Peek at the next token. */
13704 token = cp_lexer_peek_token (parser->lexer);
13706 /* If we're looking at a keyword, things are easy. */
13707 switch (token->keyword)
13709 case RID_CHAR:
13710 if (decl_specs)
13711 decl_specs->explicit_char_p = true;
13712 type = char_type_node;
13713 break;
13714 case RID_CHAR16:
13715 type = char16_type_node;
13716 break;
13717 case RID_CHAR32:
13718 type = char32_type_node;
13719 break;
13720 case RID_WCHAR:
13721 type = wchar_type_node;
13722 break;
13723 case RID_BOOL:
13724 type = boolean_type_node;
13725 break;
13726 case RID_SHORT:
13727 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
13728 type = short_integer_type_node;
13729 break;
13730 case RID_INT:
13731 if (decl_specs)
13732 decl_specs->explicit_int_p = true;
13733 type = integer_type_node;
13734 break;
13735 case RID_INT128:
13736 if (!int128_integer_type_node)
13737 break;
13738 if (decl_specs)
13739 decl_specs->explicit_int128_p = true;
13740 type = int128_integer_type_node;
13741 break;
13742 case RID_LONG:
13743 if (decl_specs)
13744 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
13745 type = long_integer_type_node;
13746 break;
13747 case RID_SIGNED:
13748 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
13749 type = integer_type_node;
13750 break;
13751 case RID_UNSIGNED:
13752 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
13753 type = unsigned_type_node;
13754 break;
13755 case RID_FLOAT:
13756 type = float_type_node;
13757 break;
13758 case RID_DOUBLE:
13759 type = double_type_node;
13760 break;
13761 case RID_VOID:
13762 type = void_type_node;
13763 break;
13765 case RID_AUTO:
13766 maybe_warn_cpp0x (CPP0X_AUTO);
13767 type = make_auto ();
13768 break;
13770 case RID_DECLTYPE:
13771 /* Since DR 743, decltype can either be a simple-type-specifier by
13772 itself or begin a nested-name-specifier. Parsing it will replace
13773 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13774 handling below decide what to do. */
13775 cp_parser_decltype (parser);
13776 cp_lexer_set_token_position (parser->lexer, token);
13777 break;
13779 case RID_TYPEOF:
13780 /* Consume the `typeof' token. */
13781 cp_lexer_consume_token (parser->lexer);
13782 /* Parse the operand to `typeof'. */
13783 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13784 /* If it is not already a TYPE, take its type. */
13785 if (!TYPE_P (type))
13786 type = finish_typeof (type);
13788 if (decl_specs)
13789 cp_parser_set_decl_spec_type (decl_specs, type,
13790 token,
13791 /*type_definition_p=*/false);
13793 return type;
13795 case RID_UNDERLYING_TYPE:
13796 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13797 if (decl_specs)
13798 cp_parser_set_decl_spec_type (decl_specs, type,
13799 token,
13800 /*type_definition_p=*/false);
13802 return type;
13804 case RID_BASES:
13805 case RID_DIRECT_BASES:
13806 type = cp_parser_trait_expr (parser, token->keyword);
13807 if (decl_specs)
13808 cp_parser_set_decl_spec_type (decl_specs, type,
13809 token,
13810 /*type_definition_p=*/false);
13811 return type;
13812 default:
13813 break;
13816 /* If token is an already-parsed decltype not followed by ::,
13817 it's a simple-type-specifier. */
13818 if (token->type == CPP_DECLTYPE
13819 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13821 type = token->u.value;
13822 if (decl_specs)
13823 cp_parser_set_decl_spec_type (decl_specs, type,
13824 token,
13825 /*type_definition_p=*/false);
13826 cp_lexer_consume_token (parser->lexer);
13827 return type;
13830 /* If the type-specifier was for a built-in type, we're done. */
13831 if (type)
13833 /* Record the type. */
13834 if (decl_specs
13835 && (token->keyword != RID_SIGNED
13836 && token->keyword != RID_UNSIGNED
13837 && token->keyword != RID_SHORT
13838 && token->keyword != RID_LONG))
13839 cp_parser_set_decl_spec_type (decl_specs,
13840 type,
13841 token,
13842 /*type_definition_p=*/false);
13843 if (decl_specs)
13844 decl_specs->any_specifiers_p = true;
13846 /* Consume the token. */
13847 cp_lexer_consume_token (parser->lexer);
13849 /* There is no valid C++ program where a non-template type is
13850 followed by a "<". That usually indicates that the user thought
13851 that the type was a template. */
13852 cp_parser_check_for_invalid_template_id (parser, type, none_type,
13853 token->location);
13855 return TYPE_NAME (type);
13858 /* The type-specifier must be a user-defined type. */
13859 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13861 bool qualified_p;
13862 bool global_p;
13864 /* Don't gobble tokens or issue error messages if this is an
13865 optional type-specifier. */
13866 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13867 cp_parser_parse_tentatively (parser);
13869 /* Look for the optional `::' operator. */
13870 global_p
13871 = (cp_parser_global_scope_opt (parser,
13872 /*current_scope_valid_p=*/false)
13873 != NULL_TREE);
13874 /* Look for the nested-name specifier. */
13875 qualified_p
13876 = (cp_parser_nested_name_specifier_opt (parser,
13877 /*typename_keyword_p=*/false,
13878 /*check_dependency_p=*/true,
13879 /*type_p=*/false,
13880 /*is_declaration=*/false)
13881 != NULL_TREE);
13882 token = cp_lexer_peek_token (parser->lexer);
13883 /* If we have seen a nested-name-specifier, and the next token
13884 is `template', then we are using the template-id production. */
13885 if (parser->scope
13886 && cp_parser_optional_template_keyword (parser))
13888 /* Look for the template-id. */
13889 type = cp_parser_template_id (parser,
13890 /*template_keyword_p=*/true,
13891 /*check_dependency_p=*/true,
13892 none_type,
13893 /*is_declaration=*/false);
13894 /* If the template-id did not name a type, we are out of
13895 luck. */
13896 if (TREE_CODE (type) != TYPE_DECL)
13898 cp_parser_error (parser, "expected template-id for type");
13899 type = NULL_TREE;
13902 /* Otherwise, look for a type-name. */
13903 else
13904 type = cp_parser_type_name (parser);
13905 /* Keep track of all name-lookups performed in class scopes. */
13906 if (type
13907 && !global_p
13908 && !qualified_p
13909 && TREE_CODE (type) == TYPE_DECL
13910 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13911 maybe_note_name_used_in_class (DECL_NAME (type), type);
13912 /* If it didn't work out, we don't have a TYPE. */
13913 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13914 && !cp_parser_parse_definitely (parser))
13915 type = NULL_TREE;
13916 if (type && decl_specs)
13917 cp_parser_set_decl_spec_type (decl_specs, type,
13918 token,
13919 /*type_definition_p=*/false);
13922 /* If we didn't get a type-name, issue an error message. */
13923 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13925 cp_parser_error (parser, "expected type-name");
13926 return error_mark_node;
13929 if (type && type != error_mark_node)
13931 /* See if TYPE is an Objective-C type, and if so, parse and
13932 accept any protocol references following it. Do this before
13933 the cp_parser_check_for_invalid_template_id() call, because
13934 Objective-C types can be followed by '<...>' which would
13935 enclose protocol names rather than template arguments, and so
13936 everything is fine. */
13937 if (c_dialect_objc () && !parser->scope
13938 && (objc_is_id (type) || objc_is_class_name (type)))
13940 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13941 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13943 /* Clobber the "unqualified" type previously entered into
13944 DECL_SPECS with the new, improved protocol-qualified version. */
13945 if (decl_specs)
13946 decl_specs->type = qual_type;
13948 return qual_type;
13951 /* There is no valid C++ program where a non-template type is
13952 followed by a "<". That usually indicates that the user
13953 thought that the type was a template. */
13954 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13955 none_type,
13956 token->location);
13959 return type;
13962 /* Parse a type-name.
13964 type-name:
13965 class-name
13966 enum-name
13967 typedef-name
13968 simple-template-id [in c++0x]
13970 enum-name:
13971 identifier
13973 typedef-name:
13974 identifier
13976 Returns a TYPE_DECL for the type. */
13978 static tree
13979 cp_parser_type_name (cp_parser* parser)
13981 tree type_decl;
13983 /* We can't know yet whether it is a class-name or not. */
13984 cp_parser_parse_tentatively (parser);
13985 /* Try a class-name. */
13986 type_decl = cp_parser_class_name (parser,
13987 /*typename_keyword_p=*/false,
13988 /*template_keyword_p=*/false,
13989 none_type,
13990 /*check_dependency_p=*/true,
13991 /*class_head_p=*/false,
13992 /*is_declaration=*/false);
13993 /* If it's not a class-name, keep looking. */
13994 if (!cp_parser_parse_definitely (parser))
13996 if (cxx_dialect < cxx0x)
13997 /* It must be a typedef-name or an enum-name. */
13998 return cp_parser_nonclass_name (parser);
14000 cp_parser_parse_tentatively (parser);
14001 /* It is either a simple-template-id representing an
14002 instantiation of an alias template... */
14003 type_decl = cp_parser_template_id (parser,
14004 /*template_keyword_p=*/false,
14005 /*check_dependency_p=*/false,
14006 none_type,
14007 /*is_declaration=*/false);
14008 /* Note that this must be an instantiation of an alias template
14009 because [temp.names]/6 says:
14011 A template-id that names an alias template specialization
14012 is a type-name.
14014 Whereas [temp.names]/7 says:
14016 A simple-template-id that names a class template
14017 specialization is a class-name. */
14018 if (type_decl != NULL_TREE
14019 && TREE_CODE (type_decl) == TYPE_DECL
14020 && TYPE_DECL_ALIAS_P (type_decl))
14021 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14022 else
14023 cp_parser_simulate_error (parser);
14025 if (!cp_parser_parse_definitely (parser))
14026 /* ... Or a typedef-name or an enum-name. */
14027 return cp_parser_nonclass_name (parser);
14030 return type_decl;
14033 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14035 enum-name:
14036 identifier
14038 typedef-name:
14039 identifier
14041 Returns a TYPE_DECL for the type. */
14043 static tree
14044 cp_parser_nonclass_name (cp_parser* parser)
14046 tree type_decl;
14047 tree identifier;
14049 cp_token *token = cp_lexer_peek_token (parser->lexer);
14050 identifier = cp_parser_identifier (parser);
14051 if (identifier == error_mark_node)
14052 return error_mark_node;
14054 /* Look up the type-name. */
14055 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14057 if (TREE_CODE (type_decl) == USING_DECL)
14059 if (!DECL_DEPENDENT_P (type_decl))
14060 type_decl = strip_using_decl (type_decl);
14061 else if (USING_DECL_TYPENAME_P (type_decl))
14063 /* We have found a type introduced by a using
14064 declaration at class scope that refers to a dependent
14065 type.
14067 using typename :: [opt] nested-name-specifier unqualified-id ;
14069 type_decl = make_typename_type (TREE_TYPE (type_decl),
14070 DECL_NAME (type_decl),
14071 typename_type, tf_error);
14072 if (type_decl != error_mark_node)
14073 type_decl = TYPE_NAME (type_decl);
14077 if (TREE_CODE (type_decl) != TYPE_DECL
14078 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14080 /* See if this is an Objective-C type. */
14081 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14082 tree type = objc_get_protocol_qualified_type (identifier, protos);
14083 if (type)
14084 type_decl = TYPE_NAME (type);
14087 /* Issue an error if we did not find a type-name. */
14088 if (TREE_CODE (type_decl) != TYPE_DECL
14089 /* In Objective-C, we have the complication that class names are
14090 normally type names and start declarations (eg, the
14091 "NSObject" in "NSObject *object;"), but can be used in an
14092 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14093 is an expression. So, a classname followed by a dot is not a
14094 valid type-name. */
14095 || (objc_is_class_name (TREE_TYPE (type_decl))
14096 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14098 if (!cp_parser_simulate_error (parser))
14099 cp_parser_name_lookup_error (parser, identifier, type_decl,
14100 NLE_TYPE, token->location);
14101 return error_mark_node;
14103 /* Remember that the name was used in the definition of the
14104 current class so that we can check later to see if the
14105 meaning would have been different after the class was
14106 entirely defined. */
14107 else if (type_decl != error_mark_node
14108 && !parser->scope)
14109 maybe_note_name_used_in_class (identifier, type_decl);
14111 return type_decl;
14114 /* Parse an elaborated-type-specifier. Note that the grammar given
14115 here incorporates the resolution to DR68.
14117 elaborated-type-specifier:
14118 class-key :: [opt] nested-name-specifier [opt] identifier
14119 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14120 enum-key :: [opt] nested-name-specifier [opt] identifier
14121 typename :: [opt] nested-name-specifier identifier
14122 typename :: [opt] nested-name-specifier template [opt]
14123 template-id
14125 GNU extension:
14127 elaborated-type-specifier:
14128 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14129 class-key attributes :: [opt] nested-name-specifier [opt]
14130 template [opt] template-id
14131 enum attributes :: [opt] nested-name-specifier [opt] identifier
14133 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14134 declared `friend'. If IS_DECLARATION is TRUE, then this
14135 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14136 something is being declared.
14138 Returns the TYPE specified. */
14140 static tree
14141 cp_parser_elaborated_type_specifier (cp_parser* parser,
14142 bool is_friend,
14143 bool is_declaration)
14145 enum tag_types tag_type;
14146 tree identifier;
14147 tree type = NULL_TREE;
14148 tree attributes = NULL_TREE;
14149 tree globalscope;
14150 cp_token *token = NULL;
14152 /* See if we're looking at the `enum' keyword. */
14153 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14155 /* Consume the `enum' token. */
14156 cp_lexer_consume_token (parser->lexer);
14157 /* Remember that it's an enumeration type. */
14158 tag_type = enum_type;
14159 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14160 enums) is used here. */
14161 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14162 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14164 pedwarn (input_location, 0, "elaborated-type-specifier "
14165 "for a scoped enum must not use the %<%D%> keyword",
14166 cp_lexer_peek_token (parser->lexer)->u.value);
14167 /* Consume the `struct' or `class' and parse it anyway. */
14168 cp_lexer_consume_token (parser->lexer);
14170 /* Parse the attributes. */
14171 attributes = cp_parser_attributes_opt (parser);
14173 /* Or, it might be `typename'. */
14174 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14175 RID_TYPENAME))
14177 /* Consume the `typename' token. */
14178 cp_lexer_consume_token (parser->lexer);
14179 /* Remember that it's a `typename' type. */
14180 tag_type = typename_type;
14182 /* Otherwise it must be a class-key. */
14183 else
14185 tag_type = cp_parser_class_key (parser);
14186 if (tag_type == none_type)
14187 return error_mark_node;
14188 /* Parse the attributes. */
14189 attributes = cp_parser_attributes_opt (parser);
14192 /* Look for the `::' operator. */
14193 globalscope = cp_parser_global_scope_opt (parser,
14194 /*current_scope_valid_p=*/false);
14195 /* Look for the nested-name-specifier. */
14196 if (tag_type == typename_type && !globalscope)
14198 if (!cp_parser_nested_name_specifier (parser,
14199 /*typename_keyword_p=*/true,
14200 /*check_dependency_p=*/true,
14201 /*type_p=*/true,
14202 is_declaration))
14203 return error_mark_node;
14205 else
14206 /* Even though `typename' is not present, the proposed resolution
14207 to Core Issue 180 says that in `class A<T>::B', `B' should be
14208 considered a type-name, even if `A<T>' is dependent. */
14209 cp_parser_nested_name_specifier_opt (parser,
14210 /*typename_keyword_p=*/true,
14211 /*check_dependency_p=*/true,
14212 /*type_p=*/true,
14213 is_declaration);
14214 /* For everything but enumeration types, consider a template-id.
14215 For an enumeration type, consider only a plain identifier. */
14216 if (tag_type != enum_type)
14218 bool template_p = false;
14219 tree decl;
14221 /* Allow the `template' keyword. */
14222 template_p = cp_parser_optional_template_keyword (parser);
14223 /* If we didn't see `template', we don't know if there's a
14224 template-id or not. */
14225 if (!template_p)
14226 cp_parser_parse_tentatively (parser);
14227 /* Parse the template-id. */
14228 token = cp_lexer_peek_token (parser->lexer);
14229 decl = cp_parser_template_id (parser, template_p,
14230 /*check_dependency_p=*/true,
14231 tag_type,
14232 is_declaration);
14233 /* If we didn't find a template-id, look for an ordinary
14234 identifier. */
14235 if (!template_p && !cp_parser_parse_definitely (parser))
14237 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14238 in effect, then we must assume that, upon instantiation, the
14239 template will correspond to a class. */
14240 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14241 && tag_type == typename_type)
14242 type = make_typename_type (parser->scope, decl,
14243 typename_type,
14244 /*complain=*/tf_error);
14245 /* If the `typename' keyword is in effect and DECL is not a type
14246 decl. Then type is non existant. */
14247 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14248 type = NULL_TREE;
14249 else
14250 type = check_elaborated_type_specifier (tag_type, decl,
14251 /*allow_template_p=*/true);
14254 if (!type)
14256 token = cp_lexer_peek_token (parser->lexer);
14257 identifier = cp_parser_identifier (parser);
14259 if (identifier == error_mark_node)
14261 parser->scope = NULL_TREE;
14262 return error_mark_node;
14265 /* For a `typename', we needn't call xref_tag. */
14266 if (tag_type == typename_type
14267 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14268 return cp_parser_make_typename_type (parser, parser->scope,
14269 identifier,
14270 token->location);
14271 /* Look up a qualified name in the usual way. */
14272 if (parser->scope)
14274 tree decl;
14275 tree ambiguous_decls;
14277 decl = cp_parser_lookup_name (parser, identifier,
14278 tag_type,
14279 /*is_template=*/false,
14280 /*is_namespace=*/false,
14281 /*check_dependency=*/true,
14282 &ambiguous_decls,
14283 token->location);
14285 /* If the lookup was ambiguous, an error will already have been
14286 issued. */
14287 if (ambiguous_decls)
14288 return error_mark_node;
14290 /* If we are parsing friend declaration, DECL may be a
14291 TEMPLATE_DECL tree node here. However, we need to check
14292 whether this TEMPLATE_DECL results in valid code. Consider
14293 the following example:
14295 namespace N {
14296 template <class T> class C {};
14298 class X {
14299 template <class T> friend class N::C; // #1, valid code
14301 template <class T> class Y {
14302 friend class N::C; // #2, invalid code
14305 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14306 name lookup of `N::C'. We see that friend declaration must
14307 be template for the code to be valid. Note that
14308 processing_template_decl does not work here since it is
14309 always 1 for the above two cases. */
14311 decl = (cp_parser_maybe_treat_template_as_class
14312 (decl, /*tag_name_p=*/is_friend
14313 && parser->num_template_parameter_lists));
14315 if (TREE_CODE (decl) != TYPE_DECL)
14317 cp_parser_diagnose_invalid_type_name (parser,
14318 parser->scope,
14319 identifier,
14320 token->location);
14321 return error_mark_node;
14324 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14326 bool allow_template = (parser->num_template_parameter_lists
14327 || DECL_SELF_REFERENCE_P (decl));
14328 type = check_elaborated_type_specifier (tag_type, decl,
14329 allow_template);
14331 if (type == error_mark_node)
14332 return error_mark_node;
14335 /* Forward declarations of nested types, such as
14337 class C1::C2;
14338 class C1::C2::C3;
14340 are invalid unless all components preceding the final '::'
14341 are complete. If all enclosing types are complete, these
14342 declarations become merely pointless.
14344 Invalid forward declarations of nested types are errors
14345 caught elsewhere in parsing. Those that are pointless arrive
14346 here. */
14348 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14349 && !is_friend && !processing_explicit_instantiation)
14350 warning (0, "declaration %qD does not declare anything", decl);
14352 type = TREE_TYPE (decl);
14354 else
14356 /* An elaborated-type-specifier sometimes introduces a new type and
14357 sometimes names an existing type. Normally, the rule is that it
14358 introduces a new type only if there is not an existing type of
14359 the same name already in scope. For example, given:
14361 struct S {};
14362 void f() { struct S s; }
14364 the `struct S' in the body of `f' is the same `struct S' as in
14365 the global scope; the existing definition is used. However, if
14366 there were no global declaration, this would introduce a new
14367 local class named `S'.
14369 An exception to this rule applies to the following code:
14371 namespace N { struct S; }
14373 Here, the elaborated-type-specifier names a new type
14374 unconditionally; even if there is already an `S' in the
14375 containing scope this declaration names a new type.
14376 This exception only applies if the elaborated-type-specifier
14377 forms the complete declaration:
14379 [class.name]
14381 A declaration consisting solely of `class-key identifier ;' is
14382 either a redeclaration of the name in the current scope or a
14383 forward declaration of the identifier as a class name. It
14384 introduces the name into the current scope.
14386 We are in this situation precisely when the next token is a `;'.
14388 An exception to the exception is that a `friend' declaration does
14389 *not* name a new type; i.e., given:
14391 struct S { friend struct T; };
14393 `T' is not a new type in the scope of `S'.
14395 Also, `new struct S' or `sizeof (struct S)' never results in the
14396 definition of a new type; a new type can only be declared in a
14397 declaration context. */
14399 tag_scope ts;
14400 bool template_p;
14402 if (is_friend)
14403 /* Friends have special name lookup rules. */
14404 ts = ts_within_enclosing_non_class;
14405 else if (is_declaration
14406 && cp_lexer_next_token_is (parser->lexer,
14407 CPP_SEMICOLON))
14408 /* This is a `class-key identifier ;' */
14409 ts = ts_current;
14410 else
14411 ts = ts_global;
14413 template_p =
14414 (parser->num_template_parameter_lists
14415 && (cp_parser_next_token_starts_class_definition_p (parser)
14416 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14417 /* An unqualified name was used to reference this type, so
14418 there were no qualifying templates. */
14419 if (!cp_parser_check_template_parameters (parser,
14420 /*num_templates=*/0,
14421 token->location,
14422 /*declarator=*/NULL))
14423 return error_mark_node;
14424 type = xref_tag (tag_type, identifier, ts, template_p);
14428 if (type == error_mark_node)
14429 return error_mark_node;
14431 /* Allow attributes on forward declarations of classes. */
14432 if (attributes)
14434 if (TREE_CODE (type) == TYPENAME_TYPE)
14435 warning (OPT_Wattributes,
14436 "attributes ignored on uninstantiated type");
14437 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14438 && ! processing_explicit_instantiation)
14439 warning (OPT_Wattributes,
14440 "attributes ignored on template instantiation");
14441 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14442 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14443 else
14444 warning (OPT_Wattributes,
14445 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14448 if (tag_type != enum_type)
14450 /* Indicate whether this class was declared as a `class' or as a
14451 `struct'. */
14452 if (TREE_CODE (type) == RECORD_TYPE)
14453 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14454 cp_parser_check_class_key (tag_type, type);
14457 /* A "<" cannot follow an elaborated type specifier. If that
14458 happens, the user was probably trying to form a template-id. */
14459 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
14460 token->location);
14462 return type;
14465 /* Parse an enum-specifier.
14467 enum-specifier:
14468 enum-head { enumerator-list [opt] }
14469 enum-head { enumerator-list , } [C++0x]
14471 enum-head:
14472 enum-key identifier [opt] enum-base [opt]
14473 enum-key nested-name-specifier identifier enum-base [opt]
14475 enum-key:
14476 enum
14477 enum class [C++0x]
14478 enum struct [C++0x]
14480 enum-base: [C++0x]
14481 : type-specifier-seq
14483 opaque-enum-specifier:
14484 enum-key identifier enum-base [opt] ;
14486 GNU Extensions:
14487 enum-key attributes[opt] identifier [opt] enum-base [opt]
14488 { enumerator-list [opt] }attributes[opt]
14489 enum-key attributes[opt] identifier [opt] enum-base [opt]
14490 { enumerator-list, }attributes[opt] [C++0x]
14492 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14493 if the token stream isn't an enum-specifier after all. */
14495 static tree
14496 cp_parser_enum_specifier (cp_parser* parser)
14498 tree identifier;
14499 tree type = NULL_TREE;
14500 tree prev_scope;
14501 tree nested_name_specifier = NULL_TREE;
14502 tree attributes;
14503 bool scoped_enum_p = false;
14504 bool has_underlying_type = false;
14505 bool nested_being_defined = false;
14506 bool new_value_list = false;
14507 bool is_new_type = false;
14508 bool is_anonymous = false;
14509 tree underlying_type = NULL_TREE;
14510 cp_token *type_start_token = NULL;
14511 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14513 parser->colon_corrects_to_scope_p = false;
14515 /* Parse tentatively so that we can back up if we don't find a
14516 enum-specifier. */
14517 cp_parser_parse_tentatively (parser);
14519 /* Caller guarantees that the current token is 'enum', an identifier
14520 possibly follows, and the token after that is an opening brace.
14521 If we don't have an identifier, fabricate an anonymous name for
14522 the enumeration being defined. */
14523 cp_lexer_consume_token (parser->lexer);
14525 /* Parse the "class" or "struct", which indicates a scoped
14526 enumeration type in C++0x. */
14527 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14528 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14530 if (cxx_dialect < cxx0x)
14531 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14533 /* Consume the `struct' or `class' token. */
14534 cp_lexer_consume_token (parser->lexer);
14536 scoped_enum_p = true;
14539 attributes = cp_parser_attributes_opt (parser);
14541 /* Clear the qualification. */
14542 parser->scope = NULL_TREE;
14543 parser->qualifying_scope = NULL_TREE;
14544 parser->object_scope = NULL_TREE;
14546 /* Figure out in what scope the declaration is being placed. */
14547 prev_scope = current_scope ();
14549 type_start_token = cp_lexer_peek_token (parser->lexer);
14551 push_deferring_access_checks (dk_no_check);
14552 nested_name_specifier
14553 = cp_parser_nested_name_specifier_opt (parser,
14554 /*typename_keyword_p=*/true,
14555 /*check_dependency_p=*/false,
14556 /*type_p=*/false,
14557 /*is_declaration=*/false);
14559 if (nested_name_specifier)
14561 tree name;
14563 identifier = cp_parser_identifier (parser);
14564 name = cp_parser_lookup_name (parser, identifier,
14565 enum_type,
14566 /*is_template=*/false,
14567 /*is_namespace=*/false,
14568 /*check_dependency=*/true,
14569 /*ambiguous_decls=*/NULL,
14570 input_location);
14571 if (name)
14573 type = TREE_TYPE (name);
14574 if (TREE_CODE (type) == TYPENAME_TYPE)
14576 /* Are template enums allowed in ISO? */
14577 if (template_parm_scope_p ())
14578 pedwarn (type_start_token->location, OPT_Wpedantic,
14579 "%qD is an enumeration template", name);
14580 /* ignore a typename reference, for it will be solved by name
14581 in start_enum. */
14582 type = NULL_TREE;
14585 else
14586 error_at (type_start_token->location,
14587 "%qD is not an enumerator-name", identifier);
14589 else
14591 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14592 identifier = cp_parser_identifier (parser);
14593 else
14595 identifier = make_anon_name ();
14596 is_anonymous = true;
14599 pop_deferring_access_checks ();
14601 /* Check for the `:' that denotes a specified underlying type in C++0x.
14602 Note that a ':' could also indicate a bitfield width, however. */
14603 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14605 cp_decl_specifier_seq type_specifiers;
14607 /* Consume the `:'. */
14608 cp_lexer_consume_token (parser->lexer);
14610 /* Parse the type-specifier-seq. */
14611 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14612 /*is_trailing_return=*/false,
14613 &type_specifiers);
14615 /* At this point this is surely not elaborated type specifier. */
14616 if (!cp_parser_parse_definitely (parser))
14617 return NULL_TREE;
14619 if (cxx_dialect < cxx0x)
14620 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14622 has_underlying_type = true;
14624 /* If that didn't work, stop. */
14625 if (type_specifiers.type != error_mark_node)
14627 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14628 /*initialized=*/0, NULL);
14629 if (underlying_type == error_mark_node)
14630 underlying_type = NULL_TREE;
14634 /* Look for the `{' but don't consume it yet. */
14635 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14637 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14639 cp_parser_error (parser, "expected %<{%>");
14640 if (has_underlying_type)
14642 type = NULL_TREE;
14643 goto out;
14646 /* An opaque-enum-specifier must have a ';' here. */
14647 if ((scoped_enum_p || underlying_type)
14648 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14650 cp_parser_error (parser, "expected %<;%> or %<{%>");
14651 if (has_underlying_type)
14653 type = NULL_TREE;
14654 goto out;
14659 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14660 return NULL_TREE;
14662 if (nested_name_specifier)
14664 if (CLASS_TYPE_P (nested_name_specifier))
14666 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14667 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14668 push_scope (nested_name_specifier);
14670 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14672 push_nested_namespace (nested_name_specifier);
14676 /* Issue an error message if type-definitions are forbidden here. */
14677 if (!cp_parser_check_type_definition (parser))
14678 type = error_mark_node;
14679 else
14680 /* Create the new type. We do this before consuming the opening
14681 brace so the enum will be recorded as being on the line of its
14682 tag (or the 'enum' keyword, if there is no tag). */
14683 type = start_enum (identifier, type, underlying_type,
14684 scoped_enum_p, &is_new_type);
14686 /* If the next token is not '{' it is an opaque-enum-specifier or an
14687 elaborated-type-specifier. */
14688 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14690 timevar_push (TV_PARSE_ENUM);
14691 if (nested_name_specifier)
14693 /* The following catches invalid code such as:
14694 enum class S<int>::E { A, B, C }; */
14695 if (!processing_specialization
14696 && CLASS_TYPE_P (nested_name_specifier)
14697 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14698 error_at (type_start_token->location, "cannot add an enumerator "
14699 "list to a template instantiation");
14701 /* If that scope does not contain the scope in which the
14702 class was originally declared, the program is invalid. */
14703 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14705 if (at_namespace_scope_p ())
14706 error_at (type_start_token->location,
14707 "declaration of %qD in namespace %qD which does not "
14708 "enclose %qD",
14709 type, prev_scope, nested_name_specifier);
14710 else
14711 error_at (type_start_token->location,
14712 "declaration of %qD in %qD which does not enclose %qD",
14713 type, prev_scope, nested_name_specifier);
14714 type = error_mark_node;
14718 if (scoped_enum_p)
14719 begin_scope (sk_scoped_enum, type);
14721 /* Consume the opening brace. */
14722 cp_lexer_consume_token (parser->lexer);
14724 if (type == error_mark_node)
14725 ; /* Nothing to add */
14726 else if (OPAQUE_ENUM_P (type)
14727 || (cxx_dialect > cxx98 && processing_specialization))
14729 new_value_list = true;
14730 SET_OPAQUE_ENUM_P (type, false);
14731 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14733 else
14735 error_at (type_start_token->location, "multiple definition of %q#T", type);
14736 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14737 "previous definition here");
14738 type = error_mark_node;
14741 if (type == error_mark_node)
14742 cp_parser_skip_to_end_of_block_or_statement (parser);
14743 /* If the next token is not '}', then there are some enumerators. */
14744 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14745 cp_parser_enumerator_list (parser, type);
14747 /* Consume the final '}'. */
14748 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14750 if (scoped_enum_p)
14751 finish_scope ();
14752 timevar_pop (TV_PARSE_ENUM);
14754 else
14756 /* If a ';' follows, then it is an opaque-enum-specifier
14757 and additional restrictions apply. */
14758 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14760 if (is_anonymous)
14761 error_at (type_start_token->location,
14762 "opaque-enum-specifier without name");
14763 else if (nested_name_specifier)
14764 error_at (type_start_token->location,
14765 "opaque-enum-specifier must use a simple identifier");
14769 /* Look for trailing attributes to apply to this enumeration, and
14770 apply them if appropriate. */
14771 if (cp_parser_allow_gnu_extensions_p (parser))
14773 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
14774 trailing_attr = chainon (trailing_attr, attributes);
14775 cplus_decl_attributes (&type,
14776 trailing_attr,
14777 (int) ATTR_FLAG_TYPE_IN_PLACE);
14780 /* Finish up the enumeration. */
14781 if (type != error_mark_node)
14783 if (new_value_list)
14784 finish_enum_value_list (type);
14785 if (is_new_type)
14786 finish_enum (type);
14789 if (nested_name_specifier)
14791 if (CLASS_TYPE_P (nested_name_specifier))
14793 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14794 pop_scope (nested_name_specifier);
14796 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14798 pop_nested_namespace (nested_name_specifier);
14801 out:
14802 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14803 return type;
14806 /* Parse an enumerator-list. The enumerators all have the indicated
14807 TYPE.
14809 enumerator-list:
14810 enumerator-definition
14811 enumerator-list , enumerator-definition */
14813 static void
14814 cp_parser_enumerator_list (cp_parser* parser, tree type)
14816 while (true)
14818 /* Parse an enumerator-definition. */
14819 cp_parser_enumerator_definition (parser, type);
14821 /* If the next token is not a ',', we've reached the end of
14822 the list. */
14823 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14824 break;
14825 /* Otherwise, consume the `,' and keep going. */
14826 cp_lexer_consume_token (parser->lexer);
14827 /* If the next token is a `}', there is a trailing comma. */
14828 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14830 if (cxx_dialect < cxx0x && !in_system_header)
14831 pedwarn (input_location, OPT_Wpedantic,
14832 "comma at end of enumerator list");
14833 break;
14838 /* Parse an enumerator-definition. The enumerator has the indicated
14839 TYPE.
14841 enumerator-definition:
14842 enumerator
14843 enumerator = constant-expression
14845 enumerator:
14846 identifier */
14848 static void
14849 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14851 tree identifier;
14852 tree value;
14853 location_t loc;
14855 /* Save the input location because we are interested in the location
14856 of the identifier and not the location of the explicit value. */
14857 loc = cp_lexer_peek_token (parser->lexer)->location;
14859 /* Look for the identifier. */
14860 identifier = cp_parser_identifier (parser);
14861 if (identifier == error_mark_node)
14862 return;
14864 /* If the next token is an '=', then there is an explicit value. */
14865 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14867 /* Consume the `=' token. */
14868 cp_lexer_consume_token (parser->lexer);
14869 /* Parse the value. */
14870 value = cp_parser_constant_expression (parser,
14871 /*allow_non_constant_p=*/false,
14872 NULL);
14874 else
14875 value = NULL_TREE;
14877 /* If we are processing a template, make sure the initializer of the
14878 enumerator doesn't contain any bare template parameter pack. */
14879 if (check_for_bare_parameter_packs (value))
14880 value = error_mark_node;
14882 /* integral_constant_value will pull out this expression, so make sure
14883 it's folded as appropriate. */
14884 value = fold_non_dependent_expr (value);
14886 /* Create the enumerator. */
14887 build_enumerator (identifier, value, type, loc);
14890 /* Parse a namespace-name.
14892 namespace-name:
14893 original-namespace-name
14894 namespace-alias
14896 Returns the NAMESPACE_DECL for the namespace. */
14898 static tree
14899 cp_parser_namespace_name (cp_parser* parser)
14901 tree identifier;
14902 tree namespace_decl;
14904 cp_token *token = cp_lexer_peek_token (parser->lexer);
14906 /* Get the name of the namespace. */
14907 identifier = cp_parser_identifier (parser);
14908 if (identifier == error_mark_node)
14909 return error_mark_node;
14911 /* Look up the identifier in the currently active scope. Look only
14912 for namespaces, due to:
14914 [basic.lookup.udir]
14916 When looking up a namespace-name in a using-directive or alias
14917 definition, only namespace names are considered.
14919 And:
14921 [basic.lookup.qual]
14923 During the lookup of a name preceding the :: scope resolution
14924 operator, object, function, and enumerator names are ignored.
14926 (Note that cp_parser_qualifying_entity only calls this
14927 function if the token after the name is the scope resolution
14928 operator.) */
14929 namespace_decl = cp_parser_lookup_name (parser, identifier,
14930 none_type,
14931 /*is_template=*/false,
14932 /*is_namespace=*/true,
14933 /*check_dependency=*/true,
14934 /*ambiguous_decls=*/NULL,
14935 token->location);
14936 /* If it's not a namespace, issue an error. */
14937 if (namespace_decl == error_mark_node
14938 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14940 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14941 error_at (token->location, "%qD is not a namespace-name", identifier);
14942 cp_parser_error (parser, "expected namespace-name");
14943 namespace_decl = error_mark_node;
14946 return namespace_decl;
14949 /* Parse a namespace-definition.
14951 namespace-definition:
14952 named-namespace-definition
14953 unnamed-namespace-definition
14955 named-namespace-definition:
14956 original-namespace-definition
14957 extension-namespace-definition
14959 original-namespace-definition:
14960 namespace identifier { namespace-body }
14962 extension-namespace-definition:
14963 namespace original-namespace-name { namespace-body }
14965 unnamed-namespace-definition:
14966 namespace { namespace-body } */
14968 static void
14969 cp_parser_namespace_definition (cp_parser* parser)
14971 tree identifier, attribs;
14972 bool has_visibility;
14973 bool is_inline;
14975 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14977 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14978 is_inline = true;
14979 cp_lexer_consume_token (parser->lexer);
14981 else
14982 is_inline = false;
14984 /* Look for the `namespace' keyword. */
14985 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14987 /* Get the name of the namespace. We do not attempt to distinguish
14988 between an original-namespace-definition and an
14989 extension-namespace-definition at this point. The semantic
14990 analysis routines are responsible for that. */
14991 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14992 identifier = cp_parser_identifier (parser);
14993 else
14994 identifier = NULL_TREE;
14996 /* Parse any specified attributes. */
14997 attribs = cp_parser_attributes_opt (parser);
14999 /* Look for the `{' to start the namespace. */
15000 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15001 /* Start the namespace. */
15002 push_namespace (identifier);
15004 /* "inline namespace" is equivalent to a stub namespace definition
15005 followed by a strong using directive. */
15006 if (is_inline)
15008 tree name_space = current_namespace;
15009 /* Set up namespace association. */
15010 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15011 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15012 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15013 /* Import the contents of the inline namespace. */
15014 pop_namespace ();
15015 do_using_directive (name_space);
15016 push_namespace (identifier);
15019 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15021 /* Parse the body of the namespace. */
15022 cp_parser_namespace_body (parser);
15024 if (has_visibility)
15025 pop_visibility (1);
15027 /* Finish the namespace. */
15028 pop_namespace ();
15029 /* Look for the final `}'. */
15030 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15033 /* Parse a namespace-body.
15035 namespace-body:
15036 declaration-seq [opt] */
15038 static void
15039 cp_parser_namespace_body (cp_parser* parser)
15041 cp_parser_declaration_seq_opt (parser);
15044 /* Parse a namespace-alias-definition.
15046 namespace-alias-definition:
15047 namespace identifier = qualified-namespace-specifier ; */
15049 static void
15050 cp_parser_namespace_alias_definition (cp_parser* parser)
15052 tree identifier;
15053 tree namespace_specifier;
15055 cp_token *token = cp_lexer_peek_token (parser->lexer);
15057 /* Look for the `namespace' keyword. */
15058 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15059 /* Look for the identifier. */
15060 identifier = cp_parser_identifier (parser);
15061 if (identifier == error_mark_node)
15062 return;
15063 /* Look for the `=' token. */
15064 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15065 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15067 error_at (token->location, "%<namespace%> definition is not allowed here");
15068 /* Skip the definition. */
15069 cp_lexer_consume_token (parser->lexer);
15070 if (cp_parser_skip_to_closing_brace (parser))
15071 cp_lexer_consume_token (parser->lexer);
15072 return;
15074 cp_parser_require (parser, CPP_EQ, RT_EQ);
15075 /* Look for the qualified-namespace-specifier. */
15076 namespace_specifier
15077 = cp_parser_qualified_namespace_specifier (parser);
15078 /* Look for the `;' token. */
15079 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15081 /* Register the alias in the symbol table. */
15082 do_namespace_alias (identifier, namespace_specifier);
15085 /* Parse a qualified-namespace-specifier.
15087 qualified-namespace-specifier:
15088 :: [opt] nested-name-specifier [opt] namespace-name
15090 Returns a NAMESPACE_DECL corresponding to the specified
15091 namespace. */
15093 static tree
15094 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15096 /* Look for the optional `::'. */
15097 cp_parser_global_scope_opt (parser,
15098 /*current_scope_valid_p=*/false);
15100 /* Look for the optional nested-name-specifier. */
15101 cp_parser_nested_name_specifier_opt (parser,
15102 /*typename_keyword_p=*/false,
15103 /*check_dependency_p=*/true,
15104 /*type_p=*/false,
15105 /*is_declaration=*/true);
15107 return cp_parser_namespace_name (parser);
15110 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15111 access declaration.
15113 using-declaration:
15114 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15115 using :: unqualified-id ;
15117 access-declaration:
15118 qualified-id ;
15122 static bool
15123 cp_parser_using_declaration (cp_parser* parser,
15124 bool access_declaration_p)
15126 cp_token *token;
15127 bool typename_p = false;
15128 bool global_scope_p;
15129 tree decl;
15130 tree identifier;
15131 tree qscope;
15132 int oldcount = errorcount;
15133 cp_token *diag_token = NULL;
15135 if (access_declaration_p)
15137 diag_token = cp_lexer_peek_token (parser->lexer);
15138 cp_parser_parse_tentatively (parser);
15140 else
15142 /* Look for the `using' keyword. */
15143 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15145 /* Peek at the next token. */
15146 token = cp_lexer_peek_token (parser->lexer);
15147 /* See if it's `typename'. */
15148 if (token->keyword == RID_TYPENAME)
15150 /* Remember that we've seen it. */
15151 typename_p = true;
15152 /* Consume the `typename' token. */
15153 cp_lexer_consume_token (parser->lexer);
15157 /* Look for the optional global scope qualification. */
15158 global_scope_p
15159 = (cp_parser_global_scope_opt (parser,
15160 /*current_scope_valid_p=*/false)
15161 != NULL_TREE);
15163 /* If we saw `typename', or didn't see `::', then there must be a
15164 nested-name-specifier present. */
15165 if (typename_p || !global_scope_p)
15166 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15167 /*check_dependency_p=*/true,
15168 /*type_p=*/false,
15169 /*is_declaration=*/true);
15170 /* Otherwise, we could be in either of the two productions. In that
15171 case, treat the nested-name-specifier as optional. */
15172 else
15173 qscope = cp_parser_nested_name_specifier_opt (parser,
15174 /*typename_keyword_p=*/false,
15175 /*check_dependency_p=*/true,
15176 /*type_p=*/false,
15177 /*is_declaration=*/true);
15178 if (!qscope)
15179 qscope = global_namespace;
15181 if (access_declaration_p && cp_parser_error_occurred (parser))
15182 /* Something has already gone wrong; there's no need to parse
15183 further. Since an error has occurred, the return value of
15184 cp_parser_parse_definitely will be false, as required. */
15185 return cp_parser_parse_definitely (parser);
15187 token = cp_lexer_peek_token (parser->lexer);
15188 /* Parse the unqualified-id. */
15189 identifier = cp_parser_unqualified_id (parser,
15190 /*template_keyword_p=*/false,
15191 /*check_dependency_p=*/true,
15192 /*declarator_p=*/true,
15193 /*optional_p=*/false);
15195 if (access_declaration_p)
15197 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15198 cp_parser_simulate_error (parser);
15199 if (!cp_parser_parse_definitely (parser))
15200 return false;
15203 /* The function we call to handle a using-declaration is different
15204 depending on what scope we are in. */
15205 if (qscope == error_mark_node || identifier == error_mark_node)
15207 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
15208 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15209 /* [namespace.udecl]
15211 A using declaration shall not name a template-id. */
15212 error_at (token->location,
15213 "a template-id may not appear in a using-declaration");
15214 else
15216 if (at_class_scope_p ())
15218 /* Create the USING_DECL. */
15219 decl = do_class_using_decl (parser->scope, identifier);
15221 if (decl && typename_p)
15222 USING_DECL_TYPENAME_P (decl) = 1;
15224 if (check_for_bare_parameter_packs (decl))
15225 return false;
15226 else
15227 /* Add it to the list of members in this class. */
15228 finish_member_declaration (decl);
15230 else
15232 decl = cp_parser_lookup_name_simple (parser,
15233 identifier,
15234 token->location);
15235 if (decl == error_mark_node)
15236 cp_parser_name_lookup_error (parser, identifier,
15237 decl, NLE_NULL,
15238 token->location);
15239 else if (check_for_bare_parameter_packs (decl))
15240 return false;
15241 else if (!at_namespace_scope_p ())
15242 do_local_using_decl (decl, qscope, identifier);
15243 else
15244 do_toplevel_using_decl (decl, qscope, identifier);
15248 /* Look for the final `;'. */
15249 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15251 if (access_declaration_p && errorcount == oldcount)
15252 warning_at (diag_token->location, OPT_Wdeprecated,
15253 "access declarations are deprecated "
15254 "in favour of using-declarations; "
15255 "suggestion: add the %<using%> keyword");
15257 return true;
15260 /* Parse an alias-declaration.
15262 alias-declaration:
15263 using identifier attribute-specifier-seq [opt] = type-id */
15265 static tree
15266 cp_parser_alias_declaration (cp_parser* parser)
15268 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15269 location_t id_location;
15270 cp_declarator *declarator;
15271 cp_decl_specifier_seq decl_specs;
15272 bool member_p;
15273 const char *saved_message = NULL;
15275 /* Look for the `using' keyword. */
15276 cp_token *using_token
15277 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
15278 if (using_token == NULL)
15279 return error_mark_node;
15281 id_location = cp_lexer_peek_token (parser->lexer)->location;
15282 id = cp_parser_identifier (parser);
15283 if (id == error_mark_node)
15284 return error_mark_node;
15286 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
15287 attributes = cp_parser_attributes_opt (parser);
15288 if (attributes == error_mark_node)
15289 return error_mark_node;
15291 cp_parser_require (parser, CPP_EQ, RT_EQ);
15293 if (cp_parser_error_occurred (parser))
15294 return error_mark_node;
15296 /* Now we are going to parse the type-id of the declaration. */
15299 [dcl.type]/3 says:
15301 "A type-specifier-seq shall not define a class or enumeration
15302 unless it appears in the type-id of an alias-declaration (7.1.3) that
15303 is not the declaration of a template-declaration."
15305 In other words, if we currently are in an alias template, the
15306 type-id should not define a type.
15308 So let's set parser->type_definition_forbidden_message in that
15309 case; cp_parser_check_type_definition (called by
15310 cp_parser_class_specifier) will then emit an error if a type is
15311 defined in the type-id. */
15312 if (parser->num_template_parameter_lists)
15314 saved_message = parser->type_definition_forbidden_message;
15315 parser->type_definition_forbidden_message =
15316 G_("types may not be defined in alias template declarations");
15319 type = cp_parser_type_id (parser);
15321 /* Restore the error message if need be. */
15322 if (parser->num_template_parameter_lists)
15323 parser->type_definition_forbidden_message = saved_message;
15325 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15327 if (cp_parser_error_occurred (parser))
15328 return error_mark_node;
15330 /* A typedef-name can also be introduced by an alias-declaration. The
15331 identifier following the using keyword becomes a typedef-name. It has
15332 the same semantics as if it were introduced by the typedef
15333 specifier. In particular, it does not define a new type and it shall
15334 not appear in the type-id. */
15336 clear_decl_specs (&decl_specs);
15337 decl_specs.type = type;
15338 if (attributes != NULL_TREE)
15340 decl_specs.attributes = attributes;
15341 set_and_check_decl_spec_loc (&decl_specs,
15342 ds_attribute,
15343 attrs_token);
15345 set_and_check_decl_spec_loc (&decl_specs,
15346 ds_typedef,
15347 using_token);
15348 set_and_check_decl_spec_loc (&decl_specs,
15349 ds_alias,
15350 using_token);
15352 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15353 declarator->id_loc = id_location;
15355 member_p = at_class_scope_p ();
15356 if (member_p)
15357 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15358 NULL_TREE, attributes);
15359 else
15360 decl = start_decl (declarator, &decl_specs, 0,
15361 attributes, NULL_TREE, &pushed_scope);
15362 if (decl == error_mark_node)
15363 return decl;
15365 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15367 if (pushed_scope)
15368 pop_scope (pushed_scope);
15370 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15371 added into the symbol table; otherwise, return the TYPE_DECL. */
15372 if (DECL_LANG_SPECIFIC (decl)
15373 && DECL_TEMPLATE_INFO (decl)
15374 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15376 decl = DECL_TI_TEMPLATE (decl);
15377 if (member_p)
15378 check_member_template (decl);
15381 return decl;
15384 /* Parse a using-directive.
15386 using-directive:
15387 using namespace :: [opt] nested-name-specifier [opt]
15388 namespace-name ; */
15390 static void
15391 cp_parser_using_directive (cp_parser* parser)
15393 tree namespace_decl;
15394 tree attribs;
15396 /* Look for the `using' keyword. */
15397 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15398 /* And the `namespace' keyword. */
15399 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15400 /* Look for the optional `::' operator. */
15401 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15402 /* And the optional nested-name-specifier. */
15403 cp_parser_nested_name_specifier_opt (parser,
15404 /*typename_keyword_p=*/false,
15405 /*check_dependency_p=*/true,
15406 /*type_p=*/false,
15407 /*is_declaration=*/true);
15408 /* Get the namespace being used. */
15409 namespace_decl = cp_parser_namespace_name (parser);
15410 /* And any specified attributes. */
15411 attribs = cp_parser_attributes_opt (parser);
15412 /* Update the symbol table. */
15413 parse_using_directive (namespace_decl, attribs);
15414 /* Look for the final `;'. */
15415 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15418 /* Parse an asm-definition.
15420 asm-definition:
15421 asm ( string-literal ) ;
15423 GNU Extension:
15425 asm-definition:
15426 asm volatile [opt] ( string-literal ) ;
15427 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15428 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15429 : asm-operand-list [opt] ) ;
15430 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15431 : asm-operand-list [opt]
15432 : asm-clobber-list [opt] ) ;
15433 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15434 : asm-clobber-list [opt]
15435 : asm-goto-list ) ; */
15437 static void
15438 cp_parser_asm_definition (cp_parser* parser)
15440 tree string;
15441 tree outputs = NULL_TREE;
15442 tree inputs = NULL_TREE;
15443 tree clobbers = NULL_TREE;
15444 tree labels = NULL_TREE;
15445 tree asm_stmt;
15446 bool volatile_p = false;
15447 bool extended_p = false;
15448 bool invalid_inputs_p = false;
15449 bool invalid_outputs_p = false;
15450 bool goto_p = false;
15451 required_token missing = RT_NONE;
15453 /* Look for the `asm' keyword. */
15454 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15455 /* See if the next token is `volatile'. */
15456 if (cp_parser_allow_gnu_extensions_p (parser)
15457 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15459 /* Remember that we saw the `volatile' keyword. */
15460 volatile_p = true;
15461 /* Consume the token. */
15462 cp_lexer_consume_token (parser->lexer);
15464 if (cp_parser_allow_gnu_extensions_p (parser)
15465 && parser->in_function_body
15466 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15468 /* Remember that we saw the `goto' keyword. */
15469 goto_p = true;
15470 /* Consume the token. */
15471 cp_lexer_consume_token (parser->lexer);
15473 /* Look for the opening `('. */
15474 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15475 return;
15476 /* Look for the string. */
15477 string = cp_parser_string_literal (parser, false, false);
15478 if (string == error_mark_node)
15480 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15481 /*consume_paren=*/true);
15482 return;
15485 /* If we're allowing GNU extensions, check for the extended assembly
15486 syntax. Unfortunately, the `:' tokens need not be separated by
15487 a space in C, and so, for compatibility, we tolerate that here
15488 too. Doing that means that we have to treat the `::' operator as
15489 two `:' tokens. */
15490 if (cp_parser_allow_gnu_extensions_p (parser)
15491 && parser->in_function_body
15492 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15493 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15495 bool inputs_p = false;
15496 bool clobbers_p = false;
15497 bool labels_p = false;
15499 /* The extended syntax was used. */
15500 extended_p = true;
15502 /* Look for outputs. */
15503 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15505 /* Consume the `:'. */
15506 cp_lexer_consume_token (parser->lexer);
15507 /* Parse the output-operands. */
15508 if (cp_lexer_next_token_is_not (parser->lexer,
15509 CPP_COLON)
15510 && cp_lexer_next_token_is_not (parser->lexer,
15511 CPP_SCOPE)
15512 && cp_lexer_next_token_is_not (parser->lexer,
15513 CPP_CLOSE_PAREN)
15514 && !goto_p)
15515 outputs = cp_parser_asm_operand_list (parser);
15517 if (outputs == error_mark_node)
15518 invalid_outputs_p = true;
15520 /* If the next token is `::', there are no outputs, and the
15521 next token is the beginning of the inputs. */
15522 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15523 /* The inputs are coming next. */
15524 inputs_p = true;
15526 /* Look for inputs. */
15527 if (inputs_p
15528 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15530 /* Consume the `:' or `::'. */
15531 cp_lexer_consume_token (parser->lexer);
15532 /* Parse the output-operands. */
15533 if (cp_lexer_next_token_is_not (parser->lexer,
15534 CPP_COLON)
15535 && cp_lexer_next_token_is_not (parser->lexer,
15536 CPP_SCOPE)
15537 && cp_lexer_next_token_is_not (parser->lexer,
15538 CPP_CLOSE_PAREN))
15539 inputs = cp_parser_asm_operand_list (parser);
15541 if (inputs == error_mark_node)
15542 invalid_inputs_p = true;
15544 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15545 /* The clobbers are coming next. */
15546 clobbers_p = true;
15548 /* Look for clobbers. */
15549 if (clobbers_p
15550 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15552 clobbers_p = true;
15553 /* Consume the `:' or `::'. */
15554 cp_lexer_consume_token (parser->lexer);
15555 /* Parse the clobbers. */
15556 if (cp_lexer_next_token_is_not (parser->lexer,
15557 CPP_COLON)
15558 && cp_lexer_next_token_is_not (parser->lexer,
15559 CPP_CLOSE_PAREN))
15560 clobbers = cp_parser_asm_clobber_list (parser);
15562 else if (goto_p
15563 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15564 /* The labels are coming next. */
15565 labels_p = true;
15567 /* Look for labels. */
15568 if (labels_p
15569 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15571 labels_p = true;
15572 /* Consume the `:' or `::'. */
15573 cp_lexer_consume_token (parser->lexer);
15574 /* Parse the labels. */
15575 labels = cp_parser_asm_label_list (parser);
15578 if (goto_p && !labels_p)
15579 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15581 else if (goto_p)
15582 missing = RT_COLON_SCOPE;
15584 /* Look for the closing `)'. */
15585 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15586 missing ? missing : RT_CLOSE_PAREN))
15587 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15588 /*consume_paren=*/true);
15589 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15591 if (!invalid_inputs_p && !invalid_outputs_p)
15593 /* Create the ASM_EXPR. */
15594 if (parser->in_function_body)
15596 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15597 inputs, clobbers, labels);
15598 /* If the extended syntax was not used, mark the ASM_EXPR. */
15599 if (!extended_p)
15601 tree temp = asm_stmt;
15602 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15603 temp = TREE_OPERAND (temp, 0);
15605 ASM_INPUT_P (temp) = 1;
15608 else
15609 add_asm_node (string);
15613 /* Declarators [gram.dcl.decl] */
15615 /* Parse an init-declarator.
15617 init-declarator:
15618 declarator initializer [opt]
15620 GNU Extension:
15622 init-declarator:
15623 declarator asm-specification [opt] attributes [opt] initializer [opt]
15625 function-definition:
15626 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15627 function-body
15628 decl-specifier-seq [opt] declarator function-try-block
15630 GNU Extension:
15632 function-definition:
15633 __extension__ function-definition
15635 TM Extension:
15637 function-definition:
15638 decl-specifier-seq [opt] declarator function-transaction-block
15640 The DECL_SPECIFIERS apply to this declarator. Returns a
15641 representation of the entity declared. If MEMBER_P is TRUE, then
15642 this declarator appears in a class scope. The new DECL created by
15643 this declarator is returned.
15645 The CHECKS are access checks that should be performed once we know
15646 what entity is being declared (and, therefore, what classes have
15647 befriended it).
15649 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15650 for a function-definition here as well. If the declarator is a
15651 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15652 be TRUE upon return. By that point, the function-definition will
15653 have been completely parsed.
15655 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15656 is FALSE.
15658 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15659 parsed declaration if it is an uninitialized single declarator not followed
15660 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15661 if present, will not be consumed. If returned, this declarator will be
15662 created with SD_INITIALIZED but will not call cp_finish_decl. */
15664 static tree
15665 cp_parser_init_declarator (cp_parser* parser,
15666 cp_decl_specifier_seq *decl_specifiers,
15667 VEC (deferred_access_check,gc)* checks,
15668 bool function_definition_allowed_p,
15669 bool member_p,
15670 int declares_class_or_enum,
15671 bool* function_definition_p,
15672 tree* maybe_range_for_decl)
15674 cp_token *token = NULL, *asm_spec_start_token = NULL,
15675 *attributes_start_token = NULL;
15676 cp_declarator *declarator;
15677 tree prefix_attributes;
15678 tree attributes = NULL;
15679 tree asm_specification;
15680 tree initializer;
15681 tree decl = NULL_TREE;
15682 tree scope;
15683 int is_initialized;
15684 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15685 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15686 "(...)". */
15687 enum cpp_ttype initialization_kind;
15688 bool is_direct_init = false;
15689 bool is_non_constant_init;
15690 int ctor_dtor_or_conv_p;
15691 bool friend_p;
15692 tree pushed_scope = NULL_TREE;
15693 bool range_for_decl_p = false;
15695 /* Gather the attributes that were provided with the
15696 decl-specifiers. */
15697 prefix_attributes = decl_specifiers->attributes;
15699 /* Assume that this is not the declarator for a function
15700 definition. */
15701 if (function_definition_p)
15702 *function_definition_p = false;
15704 /* Defer access checks while parsing the declarator; we cannot know
15705 what names are accessible until we know what is being
15706 declared. */
15707 resume_deferring_access_checks ();
15709 /* Parse the declarator. */
15710 token = cp_lexer_peek_token (parser->lexer);
15711 declarator
15712 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15713 &ctor_dtor_or_conv_p,
15714 /*parenthesized_p=*/NULL,
15715 member_p);
15716 /* Gather up the deferred checks. */
15717 stop_deferring_access_checks ();
15719 /* If the DECLARATOR was erroneous, there's no need to go
15720 further. */
15721 if (declarator == cp_error_declarator)
15722 return error_mark_node;
15724 /* Check that the number of template-parameter-lists is OK. */
15725 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15726 token->location))
15727 return error_mark_node;
15729 if (declares_class_or_enum & 2)
15730 cp_parser_check_for_definition_in_return_type (declarator,
15731 decl_specifiers->type,
15732 decl_specifiers->locations[ds_type_spec]);
15734 /* Figure out what scope the entity declared by the DECLARATOR is
15735 located in. `grokdeclarator' sometimes changes the scope, so
15736 we compute it now. */
15737 scope = get_scope_of_declarator (declarator);
15739 /* Perform any lookups in the declared type which were thought to be
15740 dependent, but are not in the scope of the declarator. */
15741 decl_specifiers->type
15742 = maybe_update_decl_type (decl_specifiers->type, scope);
15744 /* If we're allowing GNU extensions, look for an
15745 asm-specification. */
15746 if (cp_parser_allow_gnu_extensions_p (parser))
15748 /* Look for an asm-specification. */
15749 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15750 asm_specification = cp_parser_asm_specification_opt (parser);
15752 else
15753 asm_specification = NULL_TREE;
15755 /* Look for attributes. */
15756 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15757 attributes = cp_parser_attributes_opt (parser);
15759 /* Peek at the next token. */
15760 token = cp_lexer_peek_token (parser->lexer);
15761 /* Check to see if the token indicates the start of a
15762 function-definition. */
15763 if (function_declarator_p (declarator)
15764 && cp_parser_token_starts_function_definition_p (token))
15766 if (!function_definition_allowed_p)
15768 /* If a function-definition should not appear here, issue an
15769 error message. */
15770 cp_parser_error (parser,
15771 "a function-definition is not allowed here");
15772 return error_mark_node;
15774 else
15776 location_t func_brace_location
15777 = cp_lexer_peek_token (parser->lexer)->location;
15779 /* Neither attributes nor an asm-specification are allowed
15780 on a function-definition. */
15781 if (asm_specification)
15782 error_at (asm_spec_start_token->location,
15783 "an asm-specification is not allowed "
15784 "on a function-definition");
15785 if (attributes)
15786 error_at (attributes_start_token->location,
15787 "attributes are not allowed on a function-definition");
15788 /* This is a function-definition. */
15789 *function_definition_p = true;
15791 /* Parse the function definition. */
15792 if (member_p)
15793 decl = cp_parser_save_member_function_body (parser,
15794 decl_specifiers,
15795 declarator,
15796 prefix_attributes);
15797 else
15798 decl
15799 = (cp_parser_function_definition_from_specifiers_and_declarator
15800 (parser, decl_specifiers, prefix_attributes, declarator));
15802 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15804 /* This is where the prologue starts... */
15805 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15806 = func_brace_location;
15809 return decl;
15813 /* [dcl.dcl]
15815 Only in function declarations for constructors, destructors, and
15816 type conversions can the decl-specifier-seq be omitted.
15818 We explicitly postpone this check past the point where we handle
15819 function-definitions because we tolerate function-definitions
15820 that are missing their return types in some modes. */
15821 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15823 cp_parser_error (parser,
15824 "expected constructor, destructor, or type conversion");
15825 return error_mark_node;
15828 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15829 if (token->type == CPP_EQ
15830 || token->type == CPP_OPEN_PAREN
15831 || token->type == CPP_OPEN_BRACE)
15833 is_initialized = SD_INITIALIZED;
15834 initialization_kind = token->type;
15835 if (maybe_range_for_decl)
15836 *maybe_range_for_decl = error_mark_node;
15838 if (token->type == CPP_EQ
15839 && function_declarator_p (declarator))
15841 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15842 if (t2->keyword == RID_DEFAULT)
15843 is_initialized = SD_DEFAULTED;
15844 else if (t2->keyword == RID_DELETE)
15845 is_initialized = SD_DELETED;
15848 else
15850 /* If the init-declarator isn't initialized and isn't followed by a
15851 `,' or `;', it's not a valid init-declarator. */
15852 if (token->type != CPP_COMMA
15853 && token->type != CPP_SEMICOLON)
15855 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15856 range_for_decl_p = true;
15857 else
15859 cp_parser_error (parser, "expected initializer");
15860 return error_mark_node;
15863 is_initialized = SD_UNINITIALIZED;
15864 initialization_kind = CPP_EOF;
15867 /* Because start_decl has side-effects, we should only call it if we
15868 know we're going ahead. By this point, we know that we cannot
15869 possibly be looking at any other construct. */
15870 cp_parser_commit_to_tentative_parse (parser);
15872 /* If the decl specifiers were bad, issue an error now that we're
15873 sure this was intended to be a declarator. Then continue
15874 declaring the variable(s), as int, to try to cut down on further
15875 errors. */
15876 if (decl_specifiers->any_specifiers_p
15877 && decl_specifiers->type == error_mark_node)
15879 cp_parser_error (parser, "invalid type in declaration");
15880 decl_specifiers->type = integer_type_node;
15883 /* Check to see whether or not this declaration is a friend. */
15884 friend_p = cp_parser_friend_p (decl_specifiers);
15886 /* Enter the newly declared entry in the symbol table. If we're
15887 processing a declaration in a class-specifier, we wait until
15888 after processing the initializer. */
15889 if (!member_p)
15891 if (parser->in_unbraced_linkage_specification_p)
15892 decl_specifiers->storage_class = sc_extern;
15893 decl = start_decl (declarator, decl_specifiers,
15894 range_for_decl_p? SD_INITIALIZED : is_initialized,
15895 attributes, prefix_attributes,
15896 &pushed_scope);
15897 /* Adjust location of decl if declarator->id_loc is more appropriate:
15898 set, and decl wasn't merged with another decl, in which case its
15899 location would be different from input_location, and more accurate. */
15900 if (DECL_P (decl)
15901 && declarator->id_loc != UNKNOWN_LOCATION
15902 && DECL_SOURCE_LOCATION (decl) == input_location)
15903 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15905 else if (scope)
15906 /* Enter the SCOPE. That way unqualified names appearing in the
15907 initializer will be looked up in SCOPE. */
15908 pushed_scope = push_scope (scope);
15910 /* Perform deferred access control checks, now that we know in which
15911 SCOPE the declared entity resides. */
15912 if (!member_p && decl)
15914 tree saved_current_function_decl = NULL_TREE;
15916 /* If the entity being declared is a function, pretend that we
15917 are in its scope. If it is a `friend', it may have access to
15918 things that would not otherwise be accessible. */
15919 if (TREE_CODE (decl) == FUNCTION_DECL)
15921 saved_current_function_decl = current_function_decl;
15922 current_function_decl = decl;
15925 /* Perform access checks for template parameters. */
15926 cp_parser_perform_template_parameter_access_checks (checks);
15928 /* Perform the access control checks for the declarator and the
15929 decl-specifiers. */
15930 perform_deferred_access_checks (tf_warning_or_error);
15932 /* Restore the saved value. */
15933 if (TREE_CODE (decl) == FUNCTION_DECL)
15934 current_function_decl = saved_current_function_decl;
15937 /* Parse the initializer. */
15938 initializer = NULL_TREE;
15939 is_direct_init = false;
15940 is_non_constant_init = true;
15941 if (is_initialized)
15943 if (function_declarator_p (declarator))
15945 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15946 if (initialization_kind == CPP_EQ)
15947 initializer = cp_parser_pure_specifier (parser);
15948 else
15950 /* If the declaration was erroneous, we don't really
15951 know what the user intended, so just silently
15952 consume the initializer. */
15953 if (decl != error_mark_node)
15954 error_at (initializer_start_token->location,
15955 "initializer provided for function");
15956 cp_parser_skip_to_closing_parenthesis (parser,
15957 /*recovering=*/true,
15958 /*or_comma=*/false,
15959 /*consume_paren=*/true);
15962 else
15964 /* We want to record the extra mangling scope for in-class
15965 initializers of class members and initializers of static data
15966 member templates. The former involves deferring
15967 parsing of the initializer until end of class as with default
15968 arguments. So right here we only handle the latter. */
15969 if (!member_p && processing_template_decl)
15970 start_lambda_scope (decl);
15971 initializer = cp_parser_initializer (parser,
15972 &is_direct_init,
15973 &is_non_constant_init);
15974 if (!member_p && processing_template_decl)
15975 finish_lambda_scope ();
15976 if (initializer == error_mark_node)
15977 cp_parser_skip_to_end_of_statement (parser);
15981 /* The old parser allows attributes to appear after a parenthesized
15982 initializer. Mark Mitchell proposed removing this functionality
15983 on the GCC mailing lists on 2002-08-13. This parser accepts the
15984 attributes -- but ignores them. */
15985 if (cp_parser_allow_gnu_extensions_p (parser)
15986 && initialization_kind == CPP_OPEN_PAREN)
15987 if (cp_parser_attributes_opt (parser))
15988 warning (OPT_Wattributes,
15989 "attributes after parenthesized initializer ignored");
15991 /* For an in-class declaration, use `grokfield' to create the
15992 declaration. */
15993 if (member_p)
15995 if (pushed_scope)
15997 pop_scope (pushed_scope);
15998 pushed_scope = NULL_TREE;
16000 decl = grokfield (declarator, decl_specifiers,
16001 initializer, !is_non_constant_init,
16002 /*asmspec=*/NULL_TREE,
16003 prefix_attributes);
16004 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16005 cp_parser_save_default_args (parser, decl);
16008 /* Finish processing the declaration. But, skip member
16009 declarations. */
16010 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16012 cp_finish_decl (decl,
16013 initializer, !is_non_constant_init,
16014 asm_specification,
16015 /* If the initializer is in parentheses, then this is
16016 a direct-initialization, which means that an
16017 `explicit' constructor is OK. Otherwise, an
16018 `explicit' constructor cannot be used. */
16019 ((is_direct_init || !is_initialized)
16020 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16022 else if ((cxx_dialect != cxx98) && friend_p
16023 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16024 /* Core issue #226 (C++0x only): A default template-argument
16025 shall not be specified in a friend class template
16026 declaration. */
16027 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16028 /*is_partial=*/false, /*is_friend_decl=*/1);
16030 if (!friend_p && pushed_scope)
16031 pop_scope (pushed_scope);
16033 return decl;
16036 /* Parse a declarator.
16038 declarator:
16039 direct-declarator
16040 ptr-operator declarator
16042 abstract-declarator:
16043 ptr-operator abstract-declarator [opt]
16044 direct-abstract-declarator
16046 GNU Extensions:
16048 declarator:
16049 attributes [opt] direct-declarator
16050 attributes [opt] ptr-operator declarator
16052 abstract-declarator:
16053 attributes [opt] ptr-operator abstract-declarator [opt]
16054 attributes [opt] direct-abstract-declarator
16056 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16057 detect constructor, destructor or conversion operators. It is set
16058 to -1 if the declarator is a name, and +1 if it is a
16059 function. Otherwise it is set to zero. Usually you just want to
16060 test for >0, but internally the negative value is used.
16062 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16063 a decl-specifier-seq unless it declares a constructor, destructor,
16064 or conversion. It might seem that we could check this condition in
16065 semantic analysis, rather than parsing, but that makes it difficult
16066 to handle something like `f()'. We want to notice that there are
16067 no decl-specifiers, and therefore realize that this is an
16068 expression, not a declaration.)
16070 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16071 the declarator is a direct-declarator of the form "(...)".
16073 MEMBER_P is true iff this declarator is a member-declarator. */
16075 static cp_declarator *
16076 cp_parser_declarator (cp_parser* parser,
16077 cp_parser_declarator_kind dcl_kind,
16078 int* ctor_dtor_or_conv_p,
16079 bool* parenthesized_p,
16080 bool member_p)
16082 cp_declarator *declarator;
16083 enum tree_code code;
16084 cp_cv_quals cv_quals;
16085 tree class_type;
16086 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16088 /* Assume this is not a constructor, destructor, or type-conversion
16089 operator. */
16090 if (ctor_dtor_or_conv_p)
16091 *ctor_dtor_or_conv_p = 0;
16093 if (cp_parser_allow_gnu_extensions_p (parser))
16094 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16096 /* Check for the ptr-operator production. */
16097 cp_parser_parse_tentatively (parser);
16098 /* Parse the ptr-operator. */
16099 code = cp_parser_ptr_operator (parser,
16100 &class_type,
16101 &cv_quals,
16102 &std_attributes);
16104 /* If that worked, then we have a ptr-operator. */
16105 if (cp_parser_parse_definitely (parser))
16107 /* If a ptr-operator was found, then this declarator was not
16108 parenthesized. */
16109 if (parenthesized_p)
16110 *parenthesized_p = true;
16111 /* The dependent declarator is optional if we are parsing an
16112 abstract-declarator. */
16113 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16114 cp_parser_parse_tentatively (parser);
16116 /* Parse the dependent declarator. */
16117 declarator = cp_parser_declarator (parser, dcl_kind,
16118 /*ctor_dtor_or_conv_p=*/NULL,
16119 /*parenthesized_p=*/NULL,
16120 /*member_p=*/false);
16122 /* If we are parsing an abstract-declarator, we must handle the
16123 case where the dependent declarator is absent. */
16124 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16125 && !cp_parser_parse_definitely (parser))
16126 declarator = NULL;
16128 declarator = cp_parser_make_indirect_declarator
16129 (code, class_type, cv_quals, declarator, std_attributes);
16131 /* Everything else is a direct-declarator. */
16132 else
16134 if (parenthesized_p)
16135 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
16136 CPP_OPEN_PAREN);
16137 declarator = cp_parser_direct_declarator (parser, dcl_kind,
16138 ctor_dtor_or_conv_p,
16139 member_p);
16142 if (gnu_attributes && declarator && declarator != cp_error_declarator)
16143 declarator->attributes = gnu_attributes;
16144 return declarator;
16147 /* Parse a direct-declarator or direct-abstract-declarator.
16149 direct-declarator:
16150 declarator-id
16151 direct-declarator ( parameter-declaration-clause )
16152 cv-qualifier-seq [opt]
16153 exception-specification [opt]
16154 direct-declarator [ constant-expression [opt] ]
16155 ( declarator )
16157 direct-abstract-declarator:
16158 direct-abstract-declarator [opt]
16159 ( parameter-declaration-clause )
16160 cv-qualifier-seq [opt]
16161 exception-specification [opt]
16162 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16163 ( abstract-declarator )
16165 Returns a representation of the declarator. DCL_KIND is
16166 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16167 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16168 we are parsing a direct-declarator. It is
16169 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16170 of ambiguity we prefer an abstract declarator, as per
16171 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16172 cp_parser_declarator. */
16174 static cp_declarator *
16175 cp_parser_direct_declarator (cp_parser* parser,
16176 cp_parser_declarator_kind dcl_kind,
16177 int* ctor_dtor_or_conv_p,
16178 bool member_p)
16180 cp_token *token;
16181 cp_declarator *declarator = NULL;
16182 tree scope = NULL_TREE;
16183 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16184 bool saved_in_declarator_p = parser->in_declarator_p;
16185 bool first = true;
16186 tree pushed_scope = NULL_TREE;
16188 while (true)
16190 /* Peek at the next token. */
16191 token = cp_lexer_peek_token (parser->lexer);
16192 if (token->type == CPP_OPEN_PAREN)
16194 /* This is either a parameter-declaration-clause, or a
16195 parenthesized declarator. When we know we are parsing a
16196 named declarator, it must be a parenthesized declarator
16197 if FIRST is true. For instance, `(int)' is a
16198 parameter-declaration-clause, with an omitted
16199 direct-abstract-declarator. But `((*))', is a
16200 parenthesized abstract declarator. Finally, when T is a
16201 template parameter `(T)' is a
16202 parameter-declaration-clause, and not a parenthesized
16203 named declarator.
16205 We first try and parse a parameter-declaration-clause,
16206 and then try a nested declarator (if FIRST is true).
16208 It is not an error for it not to be a
16209 parameter-declaration-clause, even when FIRST is
16210 false. Consider,
16212 int i (int);
16213 int i (3);
16215 The first is the declaration of a function while the
16216 second is the definition of a variable, including its
16217 initializer.
16219 Having seen only the parenthesis, we cannot know which of
16220 these two alternatives should be selected. Even more
16221 complex are examples like:
16223 int i (int (a));
16224 int i (int (3));
16226 The former is a function-declaration; the latter is a
16227 variable initialization.
16229 Thus again, we try a parameter-declaration-clause, and if
16230 that fails, we back out and return. */
16232 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16234 tree params;
16235 unsigned saved_num_template_parameter_lists;
16236 bool is_declarator = false;
16237 tree t;
16239 /* In a member-declarator, the only valid interpretation
16240 of a parenthesis is the start of a
16241 parameter-declaration-clause. (It is invalid to
16242 initialize a static data member with a parenthesized
16243 initializer; only the "=" form of initialization is
16244 permitted.) */
16245 if (!member_p)
16246 cp_parser_parse_tentatively (parser);
16248 /* Consume the `('. */
16249 cp_lexer_consume_token (parser->lexer);
16250 if (first)
16252 /* If this is going to be an abstract declarator, we're
16253 in a declarator and we can't have default args. */
16254 parser->default_arg_ok_p = false;
16255 parser->in_declarator_p = true;
16258 /* Inside the function parameter list, surrounding
16259 template-parameter-lists do not apply. */
16260 saved_num_template_parameter_lists
16261 = parser->num_template_parameter_lists;
16262 parser->num_template_parameter_lists = 0;
16264 begin_scope (sk_function_parms, NULL_TREE);
16266 /* Parse the parameter-declaration-clause. */
16267 params = cp_parser_parameter_declaration_clause (parser);
16269 parser->num_template_parameter_lists
16270 = saved_num_template_parameter_lists;
16272 /* Consume the `)'. */
16273 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16275 /* If all went well, parse the cv-qualifier-seq and the
16276 exception-specification. */
16277 if (member_p || cp_parser_parse_definitely (parser))
16279 cp_cv_quals cv_quals;
16280 cp_virt_specifiers virt_specifiers;
16281 tree exception_specification;
16282 tree late_return;
16283 tree attrs;
16285 is_declarator = true;
16287 if (ctor_dtor_or_conv_p)
16288 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16289 first = false;
16291 /* Parse the cv-qualifier-seq. */
16292 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16293 /* And the exception-specification. */
16294 exception_specification
16295 = cp_parser_exception_specification_opt (parser);
16297 attrs = cp_parser_std_attribute_spec_seq (parser);
16299 late_return = (cp_parser_late_return_type_opt
16300 (parser, member_p ? cv_quals : -1));
16302 /* Parse the virt-specifier-seq. */
16303 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16305 /* Create the function-declarator. */
16306 declarator = make_call_declarator (declarator,
16307 params,
16308 cv_quals,
16309 virt_specifiers,
16310 exception_specification,
16311 late_return);
16312 declarator->std_attributes = attrs;
16313 /* Any subsequent parameter lists are to do with
16314 return type, so are not those of the declared
16315 function. */
16316 parser->default_arg_ok_p = false;
16319 /* Remove the function parms from scope. */
16320 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16321 pop_binding (DECL_NAME (t), t);
16322 leave_scope();
16324 if (is_declarator)
16325 /* Repeat the main loop. */
16326 continue;
16329 /* If this is the first, we can try a parenthesized
16330 declarator. */
16331 if (first)
16333 bool saved_in_type_id_in_expr_p;
16335 parser->default_arg_ok_p = saved_default_arg_ok_p;
16336 parser->in_declarator_p = saved_in_declarator_p;
16338 /* Consume the `('. */
16339 cp_lexer_consume_token (parser->lexer);
16340 /* Parse the nested declarator. */
16341 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16342 parser->in_type_id_in_expr_p = true;
16343 declarator
16344 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16345 /*parenthesized_p=*/NULL,
16346 member_p);
16347 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16348 first = false;
16349 /* Expect a `)'. */
16350 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16351 declarator = cp_error_declarator;
16352 if (declarator == cp_error_declarator)
16353 break;
16355 goto handle_declarator;
16357 /* Otherwise, we must be done. */
16358 else
16359 break;
16361 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16362 && token->type == CPP_OPEN_SQUARE
16363 && !cp_next_tokens_can_be_attribute_p (parser))
16365 /* Parse an array-declarator. */
16366 tree bounds, attrs;
16368 if (ctor_dtor_or_conv_p)
16369 *ctor_dtor_or_conv_p = 0;
16371 first = false;
16372 parser->default_arg_ok_p = false;
16373 parser->in_declarator_p = true;
16374 /* Consume the `['. */
16375 cp_lexer_consume_token (parser->lexer);
16376 /* Peek at the next token. */
16377 token = cp_lexer_peek_token (parser->lexer);
16378 /* If the next token is `]', then there is no
16379 constant-expression. */
16380 if (token->type != CPP_CLOSE_SQUARE)
16382 bool non_constant_p;
16384 bounds
16385 = cp_parser_constant_expression (parser,
16386 /*allow_non_constant=*/true,
16387 &non_constant_p);
16388 if (!non_constant_p)
16389 /* OK */;
16390 else if (error_operand_p (bounds))
16391 /* Already gave an error. */;
16392 else if (!parser->in_function_body
16393 || current_binding_level->kind == sk_function_parms)
16395 /* Normally, the array bound must be an integral constant
16396 expression. However, as an extension, we allow VLAs
16397 in function scopes as long as they aren't part of a
16398 parameter declaration. */
16399 cp_parser_error (parser,
16400 "array bound is not an integer constant");
16401 bounds = error_mark_node;
16403 else if (processing_template_decl)
16405 /* Remember this wasn't a constant-expression. */
16406 bounds = build_nop (TREE_TYPE (bounds), bounds);
16407 TREE_SIDE_EFFECTS (bounds) = 1;
16410 else
16411 bounds = NULL_TREE;
16412 /* Look for the closing `]'. */
16413 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16415 declarator = cp_error_declarator;
16416 break;
16419 attrs = cp_parser_std_attribute_spec_seq (parser);
16420 declarator = make_array_declarator (declarator, bounds);
16421 declarator->std_attributes = attrs;
16423 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16426 tree qualifying_scope;
16427 tree unqualified_name;
16428 tree attrs;
16429 special_function_kind sfk;
16430 bool abstract_ok;
16431 bool pack_expansion_p = false;
16432 cp_token *declarator_id_start_token;
16434 /* Parse a declarator-id */
16435 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16436 if (abstract_ok)
16438 cp_parser_parse_tentatively (parser);
16440 /* If we see an ellipsis, we should be looking at a
16441 parameter pack. */
16442 if (token->type == CPP_ELLIPSIS)
16444 /* Consume the `...' */
16445 cp_lexer_consume_token (parser->lexer);
16447 pack_expansion_p = true;
16451 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16452 unqualified_name
16453 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16454 qualifying_scope = parser->scope;
16455 if (abstract_ok)
16457 bool okay = false;
16459 if (!unqualified_name && pack_expansion_p)
16461 /* Check whether an error occurred. */
16462 okay = !cp_parser_error_occurred (parser);
16464 /* We already consumed the ellipsis to mark a
16465 parameter pack, but we have no way to report it,
16466 so abort the tentative parse. We will be exiting
16467 immediately anyway. */
16468 cp_parser_abort_tentative_parse (parser);
16470 else
16471 okay = cp_parser_parse_definitely (parser);
16473 if (!okay)
16474 unqualified_name = error_mark_node;
16475 else if (unqualified_name
16476 && (qualifying_scope
16477 || (TREE_CODE (unqualified_name)
16478 != IDENTIFIER_NODE)))
16480 cp_parser_error (parser, "expected unqualified-id");
16481 unqualified_name = error_mark_node;
16485 if (!unqualified_name)
16486 return NULL;
16487 if (unqualified_name == error_mark_node)
16489 declarator = cp_error_declarator;
16490 pack_expansion_p = false;
16491 declarator->parameter_pack_p = false;
16492 break;
16495 attrs = cp_parser_std_attribute_spec_seq (parser);
16497 if (qualifying_scope && at_namespace_scope_p ()
16498 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16500 /* In the declaration of a member of a template class
16501 outside of the class itself, the SCOPE will sometimes
16502 be a TYPENAME_TYPE. For example, given:
16504 template <typename T>
16505 int S<T>::R::i = 3;
16507 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16508 this context, we must resolve S<T>::R to an ordinary
16509 type, rather than a typename type.
16511 The reason we normally avoid resolving TYPENAME_TYPEs
16512 is that a specialization of `S' might render
16513 `S<T>::R' not a type. However, if `S' is
16514 specialized, then this `i' will not be used, so there
16515 is no harm in resolving the types here. */
16516 tree type;
16518 /* Resolve the TYPENAME_TYPE. */
16519 type = resolve_typename_type (qualifying_scope,
16520 /*only_current_p=*/false);
16521 /* If that failed, the declarator is invalid. */
16522 if (TREE_CODE (type) == TYPENAME_TYPE)
16524 if (typedef_variant_p (type))
16525 error_at (declarator_id_start_token->location,
16526 "cannot define member of dependent typedef "
16527 "%qT", type);
16528 else
16529 error_at (declarator_id_start_token->location,
16530 "%<%T::%E%> is not a type",
16531 TYPE_CONTEXT (qualifying_scope),
16532 TYPE_IDENTIFIER (qualifying_scope));
16534 qualifying_scope = type;
16537 sfk = sfk_none;
16539 if (unqualified_name)
16541 tree class_type;
16543 if (qualifying_scope
16544 && CLASS_TYPE_P (qualifying_scope))
16545 class_type = qualifying_scope;
16546 else
16547 class_type = current_class_type;
16549 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16551 tree name_type = TREE_TYPE (unqualified_name);
16552 if (class_type && same_type_p (name_type, class_type))
16554 if (qualifying_scope
16555 && CLASSTYPE_USE_TEMPLATE (name_type))
16557 error_at (declarator_id_start_token->location,
16558 "invalid use of constructor as a template");
16559 inform (declarator_id_start_token->location,
16560 "use %<%T::%D%> instead of %<%T::%D%> to "
16561 "name the constructor in a qualified name",
16562 class_type,
16563 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16564 class_type, name_type);
16565 declarator = cp_error_declarator;
16566 break;
16568 else
16569 unqualified_name = constructor_name (class_type);
16571 else
16573 /* We do not attempt to print the declarator
16574 here because we do not have enough
16575 information about its original syntactic
16576 form. */
16577 cp_parser_error (parser, "invalid declarator");
16578 declarator = cp_error_declarator;
16579 break;
16583 if (class_type)
16585 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16586 sfk = sfk_destructor;
16587 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16588 sfk = sfk_conversion;
16589 else if (/* There's no way to declare a constructor
16590 for an anonymous type, even if the type
16591 got a name for linkage purposes. */
16592 !TYPE_WAS_ANONYMOUS (class_type)
16593 && constructor_name_p (unqualified_name,
16594 class_type))
16596 unqualified_name = constructor_name (class_type);
16597 sfk = sfk_constructor;
16599 else if (is_overloaded_fn (unqualified_name)
16600 && DECL_CONSTRUCTOR_P (get_first_fn
16601 (unqualified_name)))
16602 sfk = sfk_constructor;
16604 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16605 *ctor_dtor_or_conv_p = -1;
16608 declarator = make_id_declarator (qualifying_scope,
16609 unqualified_name,
16610 sfk);
16611 declarator->std_attributes = attrs;
16612 declarator->id_loc = token->location;
16613 declarator->parameter_pack_p = pack_expansion_p;
16615 if (pack_expansion_p)
16616 maybe_warn_variadic_templates ();
16619 handle_declarator:;
16620 scope = get_scope_of_declarator (declarator);
16621 if (scope)
16622 /* Any names that appear after the declarator-id for a
16623 member are looked up in the containing scope. */
16624 pushed_scope = push_scope (scope);
16625 parser->in_declarator_p = true;
16626 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16627 || (declarator && declarator->kind == cdk_id))
16628 /* Default args are only allowed on function
16629 declarations. */
16630 parser->default_arg_ok_p = saved_default_arg_ok_p;
16631 else
16632 parser->default_arg_ok_p = false;
16634 first = false;
16636 /* We're done. */
16637 else
16638 break;
16641 /* For an abstract declarator, we might wind up with nothing at this
16642 point. That's an error; the declarator is not optional. */
16643 if (!declarator)
16644 cp_parser_error (parser, "expected declarator");
16646 /* If we entered a scope, we must exit it now. */
16647 if (pushed_scope)
16648 pop_scope (pushed_scope);
16650 parser->default_arg_ok_p = saved_default_arg_ok_p;
16651 parser->in_declarator_p = saved_in_declarator_p;
16653 return declarator;
16656 /* Parse a ptr-operator.
16658 ptr-operator:
16659 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16660 * cv-qualifier-seq [opt]
16662 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16663 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16665 GNU Extension:
16667 ptr-operator:
16668 & cv-qualifier-seq [opt]
16670 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16671 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16672 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16673 filled in with the TYPE containing the member. *CV_QUALS is
16674 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16675 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16676 Note that the tree codes returned by this function have nothing
16677 to do with the types of trees that will be eventually be created
16678 to represent the pointer or reference type being parsed. They are
16679 just constants with suggestive names. */
16680 static enum tree_code
16681 cp_parser_ptr_operator (cp_parser* parser,
16682 tree* type,
16683 cp_cv_quals *cv_quals,
16684 tree *attributes)
16686 enum tree_code code = ERROR_MARK;
16687 cp_token *token;
16688 tree attrs = NULL_TREE;
16690 /* Assume that it's not a pointer-to-member. */
16691 *type = NULL_TREE;
16692 /* And that there are no cv-qualifiers. */
16693 *cv_quals = TYPE_UNQUALIFIED;
16695 /* Peek at the next token. */
16696 token = cp_lexer_peek_token (parser->lexer);
16698 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16699 if (token->type == CPP_MULT)
16700 code = INDIRECT_REF;
16701 else if (token->type == CPP_AND)
16702 code = ADDR_EXPR;
16703 else if ((cxx_dialect != cxx98) &&
16704 token->type == CPP_AND_AND) /* C++0x only */
16705 code = NON_LVALUE_EXPR;
16707 if (code != ERROR_MARK)
16709 /* Consume the `*', `&' or `&&'. */
16710 cp_lexer_consume_token (parser->lexer);
16712 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16713 `&', if we are allowing GNU extensions. (The only qualifier
16714 that can legally appear after `&' is `restrict', but that is
16715 enforced during semantic analysis. */
16716 if (code == INDIRECT_REF
16717 || cp_parser_allow_gnu_extensions_p (parser))
16718 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16720 attrs = cp_parser_std_attribute_spec_seq (parser);
16721 if (attributes != NULL)
16722 *attributes = attrs;
16724 else
16726 /* Try the pointer-to-member case. */
16727 cp_parser_parse_tentatively (parser);
16728 /* Look for the optional `::' operator. */
16729 cp_parser_global_scope_opt (parser,
16730 /*current_scope_valid_p=*/false);
16731 /* Look for the nested-name specifier. */
16732 token = cp_lexer_peek_token (parser->lexer);
16733 cp_parser_nested_name_specifier (parser,
16734 /*typename_keyword_p=*/false,
16735 /*check_dependency_p=*/true,
16736 /*type_p=*/false,
16737 /*is_declaration=*/false);
16738 /* If we found it, and the next token is a `*', then we are
16739 indeed looking at a pointer-to-member operator. */
16740 if (!cp_parser_error_occurred (parser)
16741 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16743 /* Indicate that the `*' operator was used. */
16744 code = INDIRECT_REF;
16746 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16747 error_at (token->location, "%qD is a namespace", parser->scope);
16748 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16749 error_at (token->location, "cannot form pointer to member of "
16750 "non-class %q#T", parser->scope);
16751 else
16753 /* The type of which the member is a member is given by the
16754 current SCOPE. */
16755 *type = parser->scope;
16756 /* The next name will not be qualified. */
16757 parser->scope = NULL_TREE;
16758 parser->qualifying_scope = NULL_TREE;
16759 parser->object_scope = NULL_TREE;
16760 /* Look for optional c++11 attributes. */
16761 attrs = cp_parser_std_attribute_spec_seq (parser);
16762 if (attributes != NULL)
16763 *attributes = attrs;
16764 /* Look for the optional cv-qualifier-seq. */
16765 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16768 /* If that didn't work we don't have a ptr-operator. */
16769 if (!cp_parser_parse_definitely (parser))
16770 cp_parser_error (parser, "expected ptr-operator");
16773 return code;
16776 /* Parse an (optional) cv-qualifier-seq.
16778 cv-qualifier-seq:
16779 cv-qualifier cv-qualifier-seq [opt]
16781 cv-qualifier:
16782 const
16783 volatile
16785 GNU Extension:
16787 cv-qualifier:
16788 __restrict__
16790 Returns a bitmask representing the cv-qualifiers. */
16792 static cp_cv_quals
16793 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16795 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16797 while (true)
16799 cp_token *token;
16800 cp_cv_quals cv_qualifier;
16802 /* Peek at the next token. */
16803 token = cp_lexer_peek_token (parser->lexer);
16804 /* See if it's a cv-qualifier. */
16805 switch (token->keyword)
16807 case RID_CONST:
16808 cv_qualifier = TYPE_QUAL_CONST;
16809 break;
16811 case RID_VOLATILE:
16812 cv_qualifier = TYPE_QUAL_VOLATILE;
16813 break;
16815 case RID_RESTRICT:
16816 cv_qualifier = TYPE_QUAL_RESTRICT;
16817 break;
16819 default:
16820 cv_qualifier = TYPE_UNQUALIFIED;
16821 break;
16824 if (!cv_qualifier)
16825 break;
16827 if (cv_quals & cv_qualifier)
16829 error_at (token->location, "duplicate cv-qualifier");
16830 cp_lexer_purge_token (parser->lexer);
16832 else
16834 cp_lexer_consume_token (parser->lexer);
16835 cv_quals |= cv_qualifier;
16839 return cv_quals;
16842 /* Parse an (optional) virt-specifier-seq.
16844 virt-specifier-seq:
16845 virt-specifier virt-specifier-seq [opt]
16847 virt-specifier:
16848 override
16849 final
16851 Returns a bitmask representing the virt-specifiers. */
16853 static cp_virt_specifiers
16854 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16856 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16858 while (true)
16860 cp_token *token;
16861 cp_virt_specifiers virt_specifier;
16863 /* Peek at the next token. */
16864 token = cp_lexer_peek_token (parser->lexer);
16865 /* See if it's a virt-specifier-qualifier. */
16866 if (token->type != CPP_NAME)
16867 break;
16868 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16870 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16871 virt_specifier = VIRT_SPEC_OVERRIDE;
16873 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16875 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16876 virt_specifier = VIRT_SPEC_FINAL;
16878 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16880 virt_specifier = VIRT_SPEC_FINAL;
16882 else
16883 break;
16885 if (virt_specifiers & virt_specifier)
16887 error_at (token->location, "duplicate virt-specifier");
16888 cp_lexer_purge_token (parser->lexer);
16890 else
16892 cp_lexer_consume_token (parser->lexer);
16893 virt_specifiers |= virt_specifier;
16896 return virt_specifiers;
16899 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16900 is in scope even though it isn't real. */
16902 static void
16903 inject_this_parameter (tree ctype, cp_cv_quals quals)
16905 tree this_parm;
16907 if (current_class_ptr)
16909 /* We don't clear this between NSDMIs. Is it already what we want? */
16910 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16911 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16912 && cp_type_quals (type) == quals)
16913 return;
16916 this_parm = build_this_parm (ctype, quals);
16917 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16918 current_class_ptr = NULL_TREE;
16919 current_class_ref
16920 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16921 current_class_ptr = this_parm;
16924 /* Parse a late-specified return type, if any. This is not a separate
16925 non-terminal, but part of a function declarator, which looks like
16927 -> trailing-type-specifier-seq abstract-declarator(opt)
16929 Returns the type indicated by the type-id.
16931 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16932 function. */
16934 static tree
16935 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16937 cp_token *token;
16938 tree type;
16940 /* Peek at the next token. */
16941 token = cp_lexer_peek_token (parser->lexer);
16942 /* A late-specified return type is indicated by an initial '->'. */
16943 if (token->type != CPP_DEREF)
16944 return NULL_TREE;
16946 /* Consume the ->. */
16947 cp_lexer_consume_token (parser->lexer);
16949 if (quals >= 0)
16951 /* DR 1207: 'this' is in scope in the trailing return type. */
16952 gcc_assert (current_class_ptr == NULL_TREE);
16953 inject_this_parameter (current_class_type, quals);
16956 type = cp_parser_trailing_type_id (parser);
16958 if (quals >= 0)
16959 current_class_ptr = current_class_ref = NULL_TREE;
16961 return type;
16964 /* Parse a declarator-id.
16966 declarator-id:
16967 id-expression
16968 :: [opt] nested-name-specifier [opt] type-name
16970 In the `id-expression' case, the value returned is as for
16971 cp_parser_id_expression if the id-expression was an unqualified-id.
16972 If the id-expression was a qualified-id, then a SCOPE_REF is
16973 returned. The first operand is the scope (either a NAMESPACE_DECL
16974 or TREE_TYPE), but the second is still just a representation of an
16975 unqualified-id. */
16977 static tree
16978 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16980 tree id;
16981 /* The expression must be an id-expression. Assume that qualified
16982 names are the names of types so that:
16984 template <class T>
16985 int S<T>::R::i = 3;
16987 will work; we must treat `S<T>::R' as the name of a type.
16988 Similarly, assume that qualified names are templates, where
16989 required, so that:
16991 template <class T>
16992 int S<T>::R<T>::i = 3;
16994 will work, too. */
16995 id = cp_parser_id_expression (parser,
16996 /*template_keyword_p=*/false,
16997 /*check_dependency_p=*/false,
16998 /*template_p=*/NULL,
16999 /*declarator_p=*/true,
17000 optional_p);
17001 if (id && BASELINK_P (id))
17002 id = BASELINK_FUNCTIONS (id);
17003 return id;
17006 /* Parse a type-id.
17008 type-id:
17009 type-specifier-seq abstract-declarator [opt]
17011 Returns the TYPE specified. */
17013 static tree
17014 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17015 bool is_trailing_return)
17017 cp_decl_specifier_seq type_specifier_seq;
17018 cp_declarator *abstract_declarator;
17020 /* Parse the type-specifier-seq. */
17021 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17022 is_trailing_return,
17023 &type_specifier_seq);
17024 if (type_specifier_seq.type == error_mark_node)
17025 return error_mark_node;
17027 /* There might or might not be an abstract declarator. */
17028 cp_parser_parse_tentatively (parser);
17029 /* Look for the declarator. */
17030 abstract_declarator
17031 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
17032 /*parenthesized_p=*/NULL,
17033 /*member_p=*/false);
17034 /* Check to see if there really was a declarator. */
17035 if (!cp_parser_parse_definitely (parser))
17036 abstract_declarator = NULL;
17038 if (type_specifier_seq.type
17039 && type_uses_auto (type_specifier_seq.type))
17041 /* A type-id with type 'auto' is only ok if the abstract declarator
17042 is a function declarator with a late-specified return type. */
17043 if (abstract_declarator
17044 && abstract_declarator->kind == cdk_function
17045 && abstract_declarator->u.function.late_return_type)
17046 /* OK */;
17047 else
17049 error ("invalid use of %<auto%>");
17050 return error_mark_node;
17054 return groktypename (&type_specifier_seq, abstract_declarator,
17055 is_template_arg);
17058 static tree cp_parser_type_id (cp_parser *parser)
17060 return cp_parser_type_id_1 (parser, false, false);
17063 static tree cp_parser_template_type_arg (cp_parser *parser)
17065 tree r;
17066 const char *saved_message = parser->type_definition_forbidden_message;
17067 parser->type_definition_forbidden_message
17068 = G_("types may not be defined in template arguments");
17069 r = cp_parser_type_id_1 (parser, true, false);
17070 parser->type_definition_forbidden_message = saved_message;
17071 return r;
17074 static tree cp_parser_trailing_type_id (cp_parser *parser)
17076 return cp_parser_type_id_1 (parser, false, true);
17079 /* Parse a type-specifier-seq.
17081 type-specifier-seq:
17082 type-specifier type-specifier-seq [opt]
17084 GNU extension:
17086 type-specifier-seq:
17087 attributes type-specifier-seq [opt]
17089 If IS_DECLARATION is true, we are at the start of a "condition" or
17090 exception-declaration, so we might be followed by a declarator-id.
17092 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17093 i.e. we've just seen "->".
17095 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17097 static void
17098 cp_parser_type_specifier_seq (cp_parser* parser,
17099 bool is_declaration,
17100 bool is_trailing_return,
17101 cp_decl_specifier_seq *type_specifier_seq)
17103 bool seen_type_specifier = false;
17104 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
17105 cp_token *start_token = NULL;
17107 /* Clear the TYPE_SPECIFIER_SEQ. */
17108 clear_decl_specs (type_specifier_seq);
17110 /* In the context of a trailing return type, enum E { } is an
17111 elaborated-type-specifier followed by a function-body, not an
17112 enum-specifier. */
17113 if (is_trailing_return)
17114 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
17116 /* Parse the type-specifiers and attributes. */
17117 while (true)
17119 tree type_specifier;
17120 bool is_cv_qualifier;
17122 /* Check for attributes first. */
17123 if (cp_next_tokens_can_be_attribute_p (parser))
17125 type_specifier_seq->attributes =
17126 chainon (type_specifier_seq->attributes,
17127 cp_parser_attributes_opt (parser));
17128 continue;
17131 /* record the token of the beginning of the type specifier seq,
17132 for error reporting purposes*/
17133 if (!start_token)
17134 start_token = cp_lexer_peek_token (parser->lexer);
17136 /* Look for the type-specifier. */
17137 type_specifier = cp_parser_type_specifier (parser,
17138 flags,
17139 type_specifier_seq,
17140 /*is_declaration=*/false,
17141 NULL,
17142 &is_cv_qualifier);
17143 if (!type_specifier)
17145 /* If the first type-specifier could not be found, this is not a
17146 type-specifier-seq at all. */
17147 if (!seen_type_specifier)
17149 cp_parser_error (parser, "expected type-specifier");
17150 type_specifier_seq->type = error_mark_node;
17151 return;
17153 /* If subsequent type-specifiers could not be found, the
17154 type-specifier-seq is complete. */
17155 break;
17158 seen_type_specifier = true;
17159 /* The standard says that a condition can be:
17161 type-specifier-seq declarator = assignment-expression
17163 However, given:
17165 struct S {};
17166 if (int S = ...)
17168 we should treat the "S" as a declarator, not as a
17169 type-specifier. The standard doesn't say that explicitly for
17170 type-specifier-seq, but it does say that for
17171 decl-specifier-seq in an ordinary declaration. Perhaps it
17172 would be clearer just to allow a decl-specifier-seq here, and
17173 then add a semantic restriction that if any decl-specifiers
17174 that are not type-specifiers appear, the program is invalid. */
17175 if (is_declaration && !is_cv_qualifier)
17176 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
17180 /* Parse a parameter-declaration-clause.
17182 parameter-declaration-clause:
17183 parameter-declaration-list [opt] ... [opt]
17184 parameter-declaration-list , ...
17186 Returns a representation for the parameter declarations. A return
17187 value of NULL indicates a parameter-declaration-clause consisting
17188 only of an ellipsis. */
17190 static tree
17191 cp_parser_parameter_declaration_clause (cp_parser* parser)
17193 tree parameters;
17194 cp_token *token;
17195 bool ellipsis_p;
17196 bool is_error;
17198 /* Peek at the next token. */
17199 token = cp_lexer_peek_token (parser->lexer);
17200 /* Check for trivial parameter-declaration-clauses. */
17201 if (token->type == CPP_ELLIPSIS)
17203 /* Consume the `...' token. */
17204 cp_lexer_consume_token (parser->lexer);
17205 return NULL_TREE;
17207 else if (token->type == CPP_CLOSE_PAREN)
17208 /* There are no parameters. */
17210 #ifndef NO_IMPLICIT_EXTERN_C
17211 if (in_system_header && current_class_type == NULL
17212 && current_lang_name == lang_name_c)
17213 return NULL_TREE;
17214 else
17215 #endif
17216 return void_list_node;
17218 /* Check for `(void)', too, which is a special case. */
17219 else if (token->keyword == RID_VOID
17220 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17221 == CPP_CLOSE_PAREN))
17223 /* Consume the `void' token. */
17224 cp_lexer_consume_token (parser->lexer);
17225 /* There are no parameters. */
17226 return void_list_node;
17229 /* Parse the parameter-declaration-list. */
17230 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17231 /* If a parse error occurred while parsing the
17232 parameter-declaration-list, then the entire
17233 parameter-declaration-clause is erroneous. */
17234 if (is_error)
17235 return NULL;
17237 /* Peek at the next token. */
17238 token = cp_lexer_peek_token (parser->lexer);
17239 /* If it's a `,', the clause should terminate with an ellipsis. */
17240 if (token->type == CPP_COMMA)
17242 /* Consume the `,'. */
17243 cp_lexer_consume_token (parser->lexer);
17244 /* Expect an ellipsis. */
17245 ellipsis_p
17246 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17248 /* It might also be `...' if the optional trailing `,' was
17249 omitted. */
17250 else if (token->type == CPP_ELLIPSIS)
17252 /* Consume the `...' token. */
17253 cp_lexer_consume_token (parser->lexer);
17254 /* And remember that we saw it. */
17255 ellipsis_p = true;
17257 else
17258 ellipsis_p = false;
17260 /* Finish the parameter list. */
17261 if (!ellipsis_p)
17262 parameters = chainon (parameters, void_list_node);
17264 return parameters;
17267 /* Parse a parameter-declaration-list.
17269 parameter-declaration-list:
17270 parameter-declaration
17271 parameter-declaration-list , parameter-declaration
17273 Returns a representation of the parameter-declaration-list, as for
17274 cp_parser_parameter_declaration_clause. However, the
17275 `void_list_node' is never appended to the list. Upon return,
17276 *IS_ERROR will be true iff an error occurred. */
17278 static tree
17279 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17281 tree parameters = NULL_TREE;
17282 tree *tail = &parameters;
17283 bool saved_in_unbraced_linkage_specification_p;
17284 int index = 0;
17286 /* Assume all will go well. */
17287 *is_error = false;
17288 /* The special considerations that apply to a function within an
17289 unbraced linkage specifications do not apply to the parameters
17290 to the function. */
17291 saved_in_unbraced_linkage_specification_p
17292 = parser->in_unbraced_linkage_specification_p;
17293 parser->in_unbraced_linkage_specification_p = false;
17295 /* Look for more parameters. */
17296 while (true)
17298 cp_parameter_declarator *parameter;
17299 tree decl = error_mark_node;
17300 bool parenthesized_p = false;
17301 /* Parse the parameter. */
17302 parameter
17303 = cp_parser_parameter_declaration (parser,
17304 /*template_parm_p=*/false,
17305 &parenthesized_p);
17307 /* We don't know yet if the enclosing context is deprecated, so wait
17308 and warn in grokparms if appropriate. */
17309 deprecated_state = DEPRECATED_SUPPRESS;
17311 if (parameter)
17312 decl = grokdeclarator (parameter->declarator,
17313 &parameter->decl_specifiers,
17314 PARM,
17315 parameter->default_argument != NULL_TREE,
17316 &parameter->decl_specifiers.attributes);
17318 deprecated_state = DEPRECATED_NORMAL;
17320 /* If a parse error occurred parsing the parameter declaration,
17321 then the entire parameter-declaration-list is erroneous. */
17322 if (decl == error_mark_node)
17324 *is_error = true;
17325 parameters = error_mark_node;
17326 break;
17329 if (parameter->decl_specifiers.attributes)
17330 cplus_decl_attributes (&decl,
17331 parameter->decl_specifiers.attributes,
17333 if (DECL_NAME (decl))
17334 decl = pushdecl (decl);
17336 if (decl != error_mark_node)
17338 retrofit_lang_decl (decl);
17339 DECL_PARM_INDEX (decl) = ++index;
17340 DECL_PARM_LEVEL (decl) = function_parm_depth ();
17343 /* Add the new parameter to the list. */
17344 *tail = build_tree_list (parameter->default_argument, decl);
17345 tail = &TREE_CHAIN (*tail);
17347 /* Peek at the next token. */
17348 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17349 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17350 /* These are for Objective-C++ */
17351 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17352 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17353 /* The parameter-declaration-list is complete. */
17354 break;
17355 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17357 cp_token *token;
17359 /* Peek at the next token. */
17360 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17361 /* If it's an ellipsis, then the list is complete. */
17362 if (token->type == CPP_ELLIPSIS)
17363 break;
17364 /* Otherwise, there must be more parameters. Consume the
17365 `,'. */
17366 cp_lexer_consume_token (parser->lexer);
17367 /* When parsing something like:
17369 int i(float f, double d)
17371 we can tell after seeing the declaration for "f" that we
17372 are not looking at an initialization of a variable "i",
17373 but rather at the declaration of a function "i".
17375 Due to the fact that the parsing of template arguments
17376 (as specified to a template-id) requires backtracking we
17377 cannot use this technique when inside a template argument
17378 list. */
17379 if (!parser->in_template_argument_list_p
17380 && !parser->in_type_id_in_expr_p
17381 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17382 /* However, a parameter-declaration of the form
17383 "foat(f)" (which is a valid declaration of a
17384 parameter "f") can also be interpreted as an
17385 expression (the conversion of "f" to "float"). */
17386 && !parenthesized_p)
17387 cp_parser_commit_to_tentative_parse (parser);
17389 else
17391 cp_parser_error (parser, "expected %<,%> or %<...%>");
17392 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17393 cp_parser_skip_to_closing_parenthesis (parser,
17394 /*recovering=*/true,
17395 /*or_comma=*/false,
17396 /*consume_paren=*/false);
17397 break;
17401 parser->in_unbraced_linkage_specification_p
17402 = saved_in_unbraced_linkage_specification_p;
17404 return parameters;
17407 /* Parse a parameter declaration.
17409 parameter-declaration:
17410 decl-specifier-seq ... [opt] declarator
17411 decl-specifier-seq declarator = assignment-expression
17412 decl-specifier-seq ... [opt] abstract-declarator [opt]
17413 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17415 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17416 declares a template parameter. (In that case, a non-nested `>'
17417 token encountered during the parsing of the assignment-expression
17418 is not interpreted as a greater-than operator.)
17420 Returns a representation of the parameter, or NULL if an error
17421 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17422 true iff the declarator is of the form "(p)". */
17424 static cp_parameter_declarator *
17425 cp_parser_parameter_declaration (cp_parser *parser,
17426 bool template_parm_p,
17427 bool *parenthesized_p)
17429 int declares_class_or_enum;
17430 cp_decl_specifier_seq decl_specifiers;
17431 cp_declarator *declarator;
17432 tree default_argument;
17433 cp_token *token = NULL, *declarator_token_start = NULL;
17434 const char *saved_message;
17436 /* In a template parameter, `>' is not an operator.
17438 [temp.param]
17440 When parsing a default template-argument for a non-type
17441 template-parameter, the first non-nested `>' is taken as the end
17442 of the template parameter-list rather than a greater-than
17443 operator. */
17445 /* Type definitions may not appear in parameter types. */
17446 saved_message = parser->type_definition_forbidden_message;
17447 parser->type_definition_forbidden_message
17448 = G_("types may not be defined in parameter types");
17450 /* Parse the declaration-specifiers. */
17451 cp_parser_decl_specifier_seq (parser,
17452 CP_PARSER_FLAGS_NONE,
17453 &decl_specifiers,
17454 &declares_class_or_enum);
17456 /* Complain about missing 'typename' or other invalid type names. */
17457 if (!decl_specifiers.any_type_specifiers_p)
17458 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17460 /* If an error occurred, there's no reason to attempt to parse the
17461 rest of the declaration. */
17462 if (cp_parser_error_occurred (parser))
17464 parser->type_definition_forbidden_message = saved_message;
17465 return NULL;
17468 /* Peek at the next token. */
17469 token = cp_lexer_peek_token (parser->lexer);
17471 /* If the next token is a `)', `,', `=', `>', or `...', then there
17472 is no declarator. However, when variadic templates are enabled,
17473 there may be a declarator following `...'. */
17474 if (token->type == CPP_CLOSE_PAREN
17475 || token->type == CPP_COMMA
17476 || token->type == CPP_EQ
17477 || token->type == CPP_GREATER)
17479 declarator = NULL;
17480 if (parenthesized_p)
17481 *parenthesized_p = false;
17483 /* Otherwise, there should be a declarator. */
17484 else
17486 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17487 parser->default_arg_ok_p = false;
17489 /* After seeing a decl-specifier-seq, if the next token is not a
17490 "(", there is no possibility that the code is a valid
17491 expression. Therefore, if parsing tentatively, we commit at
17492 this point. */
17493 if (!parser->in_template_argument_list_p
17494 /* In an expression context, having seen:
17496 (int((char ...
17498 we cannot be sure whether we are looking at a
17499 function-type (taking a "char" as a parameter) or a cast
17500 of some object of type "char" to "int". */
17501 && !parser->in_type_id_in_expr_p
17502 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17503 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17504 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17505 cp_parser_commit_to_tentative_parse (parser);
17506 /* Parse the declarator. */
17507 declarator_token_start = token;
17508 declarator = cp_parser_declarator (parser,
17509 CP_PARSER_DECLARATOR_EITHER,
17510 /*ctor_dtor_or_conv_p=*/NULL,
17511 parenthesized_p,
17512 /*member_p=*/false);
17513 parser->default_arg_ok_p = saved_default_arg_ok_p;
17514 /* After the declarator, allow more attributes. */
17515 decl_specifiers.attributes
17516 = chainon (decl_specifiers.attributes,
17517 cp_parser_attributes_opt (parser));
17520 /* If the next token is an ellipsis, and we have not seen a
17521 declarator name, and the type of the declarator contains parameter
17522 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17523 a parameter pack expansion expression. Otherwise, leave the
17524 ellipsis for a C-style variadic function. */
17525 token = cp_lexer_peek_token (parser->lexer);
17526 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17528 tree type = decl_specifiers.type;
17530 if (type && DECL_P (type))
17531 type = TREE_TYPE (type);
17533 if (type
17534 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17535 && declarator_can_be_parameter_pack (declarator)
17536 && (!declarator || !declarator->parameter_pack_p)
17537 && uses_parameter_packs (type))
17539 /* Consume the `...'. */
17540 cp_lexer_consume_token (parser->lexer);
17541 maybe_warn_variadic_templates ();
17543 /* Build a pack expansion type */
17544 if (declarator)
17545 declarator->parameter_pack_p = true;
17546 else
17547 decl_specifiers.type = make_pack_expansion (type);
17551 /* The restriction on defining new types applies only to the type
17552 of the parameter, not to the default argument. */
17553 parser->type_definition_forbidden_message = saved_message;
17555 /* If the next token is `=', then process a default argument. */
17556 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17558 token = cp_lexer_peek_token (parser->lexer);
17559 /* If we are defining a class, then the tokens that make up the
17560 default argument must be saved and processed later. */
17561 if (!template_parm_p && at_class_scope_p ()
17562 && TYPE_BEING_DEFINED (current_class_type)
17563 && !LAMBDA_TYPE_P (current_class_type))
17564 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17565 /* Outside of a class definition, we can just parse the
17566 assignment-expression. */
17567 else
17568 default_argument
17569 = cp_parser_default_argument (parser, template_parm_p);
17571 if (!parser->default_arg_ok_p)
17573 if (flag_permissive)
17574 warning (0, "deprecated use of default argument for parameter of non-function");
17575 else
17577 error_at (token->location,
17578 "default arguments are only "
17579 "permitted for function parameters");
17580 default_argument = NULL_TREE;
17583 else if ((declarator && declarator->parameter_pack_p)
17584 || (decl_specifiers.type
17585 && PACK_EXPANSION_P (decl_specifiers.type)))
17587 /* Find the name of the parameter pack. */
17588 cp_declarator *id_declarator = declarator;
17589 while (id_declarator && id_declarator->kind != cdk_id)
17590 id_declarator = id_declarator->declarator;
17592 if (id_declarator && id_declarator->kind == cdk_id)
17593 error_at (declarator_token_start->location,
17594 template_parm_p
17595 ? G_("template parameter pack %qD "
17596 "cannot have a default argument")
17597 : G_("parameter pack %qD cannot have "
17598 "a default argument"),
17599 id_declarator->u.id.unqualified_name);
17600 else
17601 error_at (declarator_token_start->location,
17602 template_parm_p
17603 ? G_("template parameter pack cannot have "
17604 "a default argument")
17605 : G_("parameter pack cannot have a "
17606 "default argument"));
17608 default_argument = NULL_TREE;
17611 else
17612 default_argument = NULL_TREE;
17614 return make_parameter_declarator (&decl_specifiers,
17615 declarator,
17616 default_argument);
17619 /* Parse a default argument and return it.
17621 TEMPLATE_PARM_P is true if this is a default argument for a
17622 non-type template parameter. */
17623 static tree
17624 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17626 tree default_argument = NULL_TREE;
17627 bool saved_greater_than_is_operator_p;
17628 bool saved_local_variables_forbidden_p;
17629 bool non_constant_p, is_direct_init;
17631 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17632 set correctly. */
17633 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17634 parser->greater_than_is_operator_p = !template_parm_p;
17635 /* Local variable names (and the `this' keyword) may not
17636 appear in a default argument. */
17637 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17638 parser->local_variables_forbidden_p = true;
17639 /* Parse the assignment-expression. */
17640 if (template_parm_p)
17641 push_deferring_access_checks (dk_no_deferred);
17642 default_argument
17643 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17644 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17645 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17646 if (template_parm_p)
17647 pop_deferring_access_checks ();
17648 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17649 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17651 return default_argument;
17654 /* Parse a function-body.
17656 function-body:
17657 compound_statement */
17659 static void
17660 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
17662 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
17665 /* Parse a ctor-initializer-opt followed by a function-body. Return
17666 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
17667 is true we are parsing a function-try-block. */
17669 static bool
17670 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
17671 bool in_function_try_block)
17673 tree body, list;
17674 bool ctor_initializer_p;
17675 const bool check_body_p =
17676 DECL_CONSTRUCTOR_P (current_function_decl)
17677 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17678 tree last = NULL;
17680 /* Begin the function body. */
17681 body = begin_function_body ();
17682 /* Parse the optional ctor-initializer. */
17683 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17685 /* If we're parsing a constexpr constructor definition, we need
17686 to check that the constructor body is indeed empty. However,
17687 before we get to cp_parser_function_body lot of junk has been
17688 generated, so we can't just check that we have an empty block.
17689 Rather we take a snapshot of the outermost block, and check whether
17690 cp_parser_function_body changed its state. */
17691 if (check_body_p)
17693 list = cur_stmt_list;
17694 if (STATEMENT_LIST_TAIL (list))
17695 last = STATEMENT_LIST_TAIL (list)->stmt;
17697 /* Parse the function-body. */
17698 cp_parser_function_body (parser, in_function_try_block);
17699 if (check_body_p)
17700 check_constexpr_ctor_body (last, list);
17701 /* Finish the function body. */
17702 finish_function_body (body);
17704 return ctor_initializer_p;
17707 /* Parse an initializer.
17709 initializer:
17710 = initializer-clause
17711 ( expression-list )
17713 Returns an expression representing the initializer. If no
17714 initializer is present, NULL_TREE is returned.
17716 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17717 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17718 set to TRUE if there is no initializer present. If there is an
17719 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17720 is set to true; otherwise it is set to false. */
17722 static tree
17723 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17724 bool* non_constant_p)
17726 cp_token *token;
17727 tree init;
17729 /* Peek at the next token. */
17730 token = cp_lexer_peek_token (parser->lexer);
17732 /* Let our caller know whether or not this initializer was
17733 parenthesized. */
17734 *is_direct_init = (token->type != CPP_EQ);
17735 /* Assume that the initializer is constant. */
17736 *non_constant_p = false;
17738 if (token->type == CPP_EQ)
17740 /* Consume the `='. */
17741 cp_lexer_consume_token (parser->lexer);
17742 /* Parse the initializer-clause. */
17743 init = cp_parser_initializer_clause (parser, non_constant_p);
17745 else if (token->type == CPP_OPEN_PAREN)
17747 VEC(tree,gc) *vec;
17748 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17749 /*cast_p=*/false,
17750 /*allow_expansion_p=*/true,
17751 non_constant_p);
17752 if (vec == NULL)
17753 return error_mark_node;
17754 init = build_tree_list_vec (vec);
17755 release_tree_vector (vec);
17757 else if (token->type == CPP_OPEN_BRACE)
17759 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17760 init = cp_parser_braced_list (parser, non_constant_p);
17761 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17763 else
17765 /* Anything else is an error. */
17766 cp_parser_error (parser, "expected initializer");
17767 init = error_mark_node;
17770 return init;
17773 /* Parse an initializer-clause.
17775 initializer-clause:
17776 assignment-expression
17777 braced-init-list
17779 Returns an expression representing the initializer.
17781 If the `assignment-expression' production is used the value
17782 returned is simply a representation for the expression.
17784 Otherwise, calls cp_parser_braced_list. */
17786 static tree
17787 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17789 tree initializer;
17791 /* Assume the expression is constant. */
17792 *non_constant_p = false;
17794 /* If it is not a `{', then we are looking at an
17795 assignment-expression. */
17796 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17798 initializer
17799 = cp_parser_constant_expression (parser,
17800 /*allow_non_constant_p=*/true,
17801 non_constant_p);
17803 else
17804 initializer = cp_parser_braced_list (parser, non_constant_p);
17806 return initializer;
17809 /* Parse a brace-enclosed initializer list.
17811 braced-init-list:
17812 { initializer-list , [opt] }
17815 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17816 the elements of the initializer-list (or NULL, if the last
17817 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17818 NULL_TREE. There is no way to detect whether or not the optional
17819 trailing `,' was provided. NON_CONSTANT_P is as for
17820 cp_parser_initializer. */
17822 static tree
17823 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17825 tree initializer;
17827 /* Consume the `{' token. */
17828 cp_lexer_consume_token (parser->lexer);
17829 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17830 initializer = make_node (CONSTRUCTOR);
17831 /* If it's not a `}', then there is a non-trivial initializer. */
17832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17834 /* Parse the initializer list. */
17835 CONSTRUCTOR_ELTS (initializer)
17836 = cp_parser_initializer_list (parser, non_constant_p);
17837 /* A trailing `,' token is allowed. */
17838 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17839 cp_lexer_consume_token (parser->lexer);
17841 /* Now, there should be a trailing `}'. */
17842 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17843 TREE_TYPE (initializer) = init_list_type_node;
17844 return initializer;
17847 /* Parse an initializer-list.
17849 initializer-list:
17850 initializer-clause ... [opt]
17851 initializer-list , initializer-clause ... [opt]
17853 GNU Extension:
17855 initializer-list:
17856 designation initializer-clause ...[opt]
17857 initializer-list , designation initializer-clause ...[opt]
17859 designation:
17860 . identifier =
17861 identifier :
17862 [ constant-expression ] =
17864 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17865 for the initializer. If the INDEX of the elt is non-NULL, it is the
17866 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17867 as for cp_parser_initializer. */
17869 static VEC(constructor_elt,gc) *
17870 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17872 VEC(constructor_elt,gc) *v = NULL;
17874 /* Assume all of the expressions are constant. */
17875 *non_constant_p = false;
17877 /* Parse the rest of the list. */
17878 while (true)
17880 cp_token *token;
17881 tree designator;
17882 tree initializer;
17883 bool clause_non_constant_p;
17885 /* If the next token is an identifier and the following one is a
17886 colon, we are looking at the GNU designated-initializer
17887 syntax. */
17888 if (cp_parser_allow_gnu_extensions_p (parser)
17889 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17890 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17892 /* Warn the user that they are using an extension. */
17893 pedwarn (input_location, OPT_Wpedantic,
17894 "ISO C++ does not allow designated initializers");
17895 /* Consume the identifier. */
17896 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17897 /* Consume the `:'. */
17898 cp_lexer_consume_token (parser->lexer);
17900 /* Also handle the C99 syntax, '. id ='. */
17901 else if (cp_parser_allow_gnu_extensions_p (parser)
17902 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17903 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17904 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17906 /* Warn the user that they are using an extension. */
17907 pedwarn (input_location, OPT_Wpedantic,
17908 "ISO C++ does not allow C99 designated initializers");
17909 /* Consume the `.'. */
17910 cp_lexer_consume_token (parser->lexer);
17911 /* Consume the identifier. */
17912 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17913 /* Consume the `='. */
17914 cp_lexer_consume_token (parser->lexer);
17916 /* Also handle C99 array designators, '[ const ] ='. */
17917 else if (cp_parser_allow_gnu_extensions_p (parser)
17918 && !c_dialect_objc ()
17919 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17921 /* In C++11, [ could start a lambda-introducer. */
17922 cp_parser_parse_tentatively (parser);
17923 cp_lexer_consume_token (parser->lexer);
17924 designator = cp_parser_constant_expression (parser, false, NULL);
17925 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17926 cp_parser_require (parser, CPP_EQ, RT_EQ);
17927 if (!cp_parser_parse_definitely (parser))
17928 designator = NULL_TREE;
17930 else
17931 designator = NULL_TREE;
17933 /* Parse the initializer. */
17934 initializer = cp_parser_initializer_clause (parser,
17935 &clause_non_constant_p);
17936 /* If any clause is non-constant, so is the entire initializer. */
17937 if (clause_non_constant_p)
17938 *non_constant_p = true;
17940 /* If we have an ellipsis, this is an initializer pack
17941 expansion. */
17942 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17944 /* Consume the `...'. */
17945 cp_lexer_consume_token (parser->lexer);
17947 /* Turn the initializer into an initializer expansion. */
17948 initializer = make_pack_expansion (initializer);
17951 /* Add it to the vector. */
17952 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17954 /* If the next token is not a comma, we have reached the end of
17955 the list. */
17956 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17957 break;
17959 /* Peek at the next token. */
17960 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17961 /* If the next token is a `}', then we're still done. An
17962 initializer-clause can have a trailing `,' after the
17963 initializer-list and before the closing `}'. */
17964 if (token->type == CPP_CLOSE_BRACE)
17965 break;
17967 /* Consume the `,' token. */
17968 cp_lexer_consume_token (parser->lexer);
17971 return v;
17974 /* Classes [gram.class] */
17976 /* Parse a class-name.
17978 class-name:
17979 identifier
17980 template-id
17982 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17983 to indicate that names looked up in dependent types should be
17984 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17985 keyword has been used to indicate that the name that appears next
17986 is a template. TAG_TYPE indicates the explicit tag given before
17987 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17988 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17989 is the class being defined in a class-head.
17991 Returns the TYPE_DECL representing the class. */
17993 static tree
17994 cp_parser_class_name (cp_parser *parser,
17995 bool typename_keyword_p,
17996 bool template_keyword_p,
17997 enum tag_types tag_type,
17998 bool check_dependency_p,
17999 bool class_head_p,
18000 bool is_declaration)
18002 tree decl;
18003 tree scope;
18004 bool typename_p;
18005 cp_token *token;
18006 tree identifier = NULL_TREE;
18008 /* All class-names start with an identifier. */
18009 token = cp_lexer_peek_token (parser->lexer);
18010 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
18012 cp_parser_error (parser, "expected class-name");
18013 return error_mark_node;
18016 /* PARSER->SCOPE can be cleared when parsing the template-arguments
18017 to a template-id, so we save it here. */
18018 scope = parser->scope;
18019 if (scope == error_mark_node)
18020 return error_mark_node;
18022 /* Any name names a type if we're following the `typename' keyword
18023 in a qualified name where the enclosing scope is type-dependent. */
18024 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
18025 && dependent_type_p (scope));
18026 /* Handle the common case (an identifier, but not a template-id)
18027 efficiently. */
18028 if (token->type == CPP_NAME
18029 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
18031 cp_token *identifier_token;
18032 bool ambiguous_p;
18034 /* Look for the identifier. */
18035 identifier_token = cp_lexer_peek_token (parser->lexer);
18036 ambiguous_p = identifier_token->ambiguous_p;
18037 identifier = cp_parser_identifier (parser);
18038 /* If the next token isn't an identifier, we are certainly not
18039 looking at a class-name. */
18040 if (identifier == error_mark_node)
18041 decl = error_mark_node;
18042 /* If we know this is a type-name, there's no need to look it
18043 up. */
18044 else if (typename_p)
18045 decl = identifier;
18046 else
18048 tree ambiguous_decls;
18049 /* If we already know that this lookup is ambiguous, then
18050 we've already issued an error message; there's no reason
18051 to check again. */
18052 if (ambiguous_p)
18054 cp_parser_simulate_error (parser);
18055 return error_mark_node;
18057 /* If the next token is a `::', then the name must be a type
18058 name.
18060 [basic.lookup.qual]
18062 During the lookup for a name preceding the :: scope
18063 resolution operator, object, function, and enumerator
18064 names are ignored. */
18065 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18066 tag_type = typename_type;
18067 /* Look up the name. */
18068 decl = cp_parser_lookup_name (parser, identifier,
18069 tag_type,
18070 /*is_template=*/false,
18071 /*is_namespace=*/false,
18072 check_dependency_p,
18073 &ambiguous_decls,
18074 identifier_token->location);
18075 if (ambiguous_decls)
18077 if (cp_parser_parsing_tentatively (parser))
18078 cp_parser_simulate_error (parser);
18079 return error_mark_node;
18083 else
18085 /* Try a template-id. */
18086 decl = cp_parser_template_id (parser, template_keyword_p,
18087 check_dependency_p,
18088 tag_type,
18089 is_declaration);
18090 if (decl == error_mark_node)
18091 return error_mark_node;
18094 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
18096 /* If this is a typename, create a TYPENAME_TYPE. */
18097 if (typename_p && decl != error_mark_node)
18099 decl = make_typename_type (scope, decl, typename_type,
18100 /*complain=*/tf_error);
18101 if (decl != error_mark_node)
18102 decl = TYPE_NAME (decl);
18105 decl = strip_using_decl (decl);
18107 /* Check to see that it is really the name of a class. */
18108 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18109 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
18110 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18111 /* Situations like this:
18113 template <typename T> struct A {
18114 typename T::template X<int>::I i;
18117 are problematic. Is `T::template X<int>' a class-name? The
18118 standard does not seem to be definitive, but there is no other
18119 valid interpretation of the following `::'. Therefore, those
18120 names are considered class-names. */
18122 decl = make_typename_type (scope, decl, tag_type, tf_error);
18123 if (decl != error_mark_node)
18124 decl = TYPE_NAME (decl);
18126 else if (TREE_CODE (decl) != TYPE_DECL
18127 || TREE_TYPE (decl) == error_mark_node
18128 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
18129 /* In Objective-C 2.0, a classname followed by '.' starts a
18130 dot-syntax expression, and it's not a type-name. */
18131 || (c_dialect_objc ()
18132 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
18133 && objc_is_class_name (decl)))
18134 decl = error_mark_node;
18136 if (decl == error_mark_node)
18137 cp_parser_error (parser, "expected class-name");
18138 else if (identifier && !parser->scope)
18139 maybe_note_name_used_in_class (identifier, decl);
18141 return decl;
18144 /* Parse a class-specifier.
18146 class-specifier:
18147 class-head { member-specification [opt] }
18149 Returns the TREE_TYPE representing the class. */
18151 static tree
18152 cp_parser_class_specifier_1 (cp_parser* parser)
18154 tree type;
18155 tree attributes = NULL_TREE;
18156 bool nested_name_specifier_p;
18157 unsigned saved_num_template_parameter_lists;
18158 bool saved_in_function_body;
18159 unsigned char in_statement;
18160 bool in_switch_statement_p;
18161 bool saved_in_unbraced_linkage_specification_p;
18162 tree old_scope = NULL_TREE;
18163 tree scope = NULL_TREE;
18164 cp_token *closing_brace;
18166 push_deferring_access_checks (dk_no_deferred);
18168 /* Parse the class-head. */
18169 type = cp_parser_class_head (parser,
18170 &nested_name_specifier_p);
18171 /* If the class-head was a semantic disaster, skip the entire body
18172 of the class. */
18173 if (!type)
18175 cp_parser_skip_to_end_of_block_or_statement (parser);
18176 pop_deferring_access_checks ();
18177 return error_mark_node;
18180 /* Look for the `{'. */
18181 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
18183 pop_deferring_access_checks ();
18184 return error_mark_node;
18187 /* Issue an error message if type-definitions are forbidden here. */
18188 cp_parser_check_type_definition (parser);
18189 /* Remember that we are defining one more class. */
18190 ++parser->num_classes_being_defined;
18191 /* Inside the class, surrounding template-parameter-lists do not
18192 apply. */
18193 saved_num_template_parameter_lists
18194 = parser->num_template_parameter_lists;
18195 parser->num_template_parameter_lists = 0;
18196 /* We are not in a function body. */
18197 saved_in_function_body = parser->in_function_body;
18198 parser->in_function_body = false;
18199 /* Or in a loop. */
18200 in_statement = parser->in_statement;
18201 parser->in_statement = 0;
18202 /* Or in a switch. */
18203 in_switch_statement_p = parser->in_switch_statement_p;
18204 parser->in_switch_statement_p = false;
18205 /* We are not immediately inside an extern "lang" block. */
18206 saved_in_unbraced_linkage_specification_p
18207 = parser->in_unbraced_linkage_specification_p;
18208 parser->in_unbraced_linkage_specification_p = false;
18210 /* Start the class. */
18211 if (nested_name_specifier_p)
18213 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18214 old_scope = push_inner_scope (scope);
18216 type = begin_class_definition (type);
18218 if (type == error_mark_node)
18219 /* If the type is erroneous, skip the entire body of the class. */
18220 cp_parser_skip_to_closing_brace (parser);
18221 else
18222 /* Parse the member-specification. */
18223 cp_parser_member_specification_opt (parser);
18225 /* Look for the trailing `}'. */
18226 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18227 /* Look for trailing attributes to apply to this class. */
18228 if (cp_parser_allow_gnu_extensions_p (parser))
18229 attributes = cp_parser_gnu_attributes_opt (parser);
18230 if (type != error_mark_node)
18231 type = finish_struct (type, attributes);
18232 if (nested_name_specifier_p)
18233 pop_inner_scope (old_scope, scope);
18235 /* We've finished a type definition. Check for the common syntax
18236 error of forgetting a semicolon after the definition. We need to
18237 be careful, as we can't just check for not-a-semicolon and be done
18238 with it; the user might have typed:
18240 class X { } c = ...;
18241 class X { } *p = ...;
18243 and so forth. Instead, enumerate all the possible tokens that
18244 might follow this production; if we don't see one of them, then
18245 complain and silently insert the semicolon. */
18247 cp_token *token = cp_lexer_peek_token (parser->lexer);
18248 bool want_semicolon = true;
18250 if (cp_next_tokens_can_be_std_attribute_p (parser))
18251 /* Don't try to parse c++11 attributes here. As per the
18252 grammar, that should be a task for
18253 cp_parser_decl_specifier_seq. */
18254 want_semicolon = false;
18256 switch (token->type)
18258 case CPP_NAME:
18259 case CPP_SEMICOLON:
18260 case CPP_MULT:
18261 case CPP_AND:
18262 case CPP_OPEN_PAREN:
18263 case CPP_CLOSE_PAREN:
18264 case CPP_COMMA:
18265 want_semicolon = false;
18266 break;
18268 /* While it's legal for type qualifiers and storage class
18269 specifiers to follow type definitions in the grammar, only
18270 compiler testsuites contain code like that. Assume that if
18271 we see such code, then what we're really seeing is a case
18272 like:
18274 class X { }
18275 const <type> var = ...;
18279 class Y { }
18280 static <type> func (...) ...
18282 i.e. the qualifier or specifier applies to the next
18283 declaration. To do so, however, we need to look ahead one
18284 more token to see if *that* token is a type specifier.
18286 This code could be improved to handle:
18288 class Z { }
18289 static const <type> var = ...; */
18290 case CPP_KEYWORD:
18291 if (keyword_is_decl_specifier (token->keyword))
18293 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18295 /* Handling user-defined types here would be nice, but very
18296 tricky. */
18297 want_semicolon
18298 = (lookahead->type == CPP_KEYWORD
18299 && keyword_begins_type_specifier (lookahead->keyword));
18301 break;
18302 default:
18303 break;
18306 /* If we don't have a type, then something is very wrong and we
18307 shouldn't try to do anything clever. Likewise for not seeing the
18308 closing brace. */
18309 if (closing_brace && TYPE_P (type) && want_semicolon)
18311 cp_token_position prev
18312 = cp_lexer_previous_token_position (parser->lexer);
18313 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18314 location_t loc = prev_token->location;
18316 if (CLASSTYPE_DECLARED_CLASS (type))
18317 error_at (loc, "expected %<;%> after class definition");
18318 else if (TREE_CODE (type) == RECORD_TYPE)
18319 error_at (loc, "expected %<;%> after struct definition");
18320 else if (TREE_CODE (type) == UNION_TYPE)
18321 error_at (loc, "expected %<;%> after union definition");
18322 else
18323 gcc_unreachable ();
18325 /* Unget one token and smash it to look as though we encountered
18326 a semicolon in the input stream. */
18327 cp_lexer_set_token_position (parser->lexer, prev);
18328 token = cp_lexer_peek_token (parser->lexer);
18329 token->type = CPP_SEMICOLON;
18330 token->keyword = RID_MAX;
18334 /* If this class is not itself within the scope of another class,
18335 then we need to parse the bodies of all of the queued function
18336 definitions. Note that the queued functions defined in a class
18337 are not always processed immediately following the
18338 class-specifier for that class. Consider:
18340 struct A {
18341 struct B { void f() { sizeof (A); } };
18344 If `f' were processed before the processing of `A' were
18345 completed, there would be no way to compute the size of `A'.
18346 Note that the nesting we are interested in here is lexical --
18347 not the semantic nesting given by TYPE_CONTEXT. In particular,
18348 for:
18350 struct A { struct B; };
18351 struct A::B { void f() { } };
18353 there is no need to delay the parsing of `A::B::f'. */
18354 if (--parser->num_classes_being_defined == 0)
18356 tree decl;
18357 tree class_type = NULL_TREE;
18358 tree pushed_scope = NULL_TREE;
18359 unsigned ix;
18360 cp_default_arg_entry *e;
18361 tree save_ccp, save_ccr;
18363 /* In a first pass, parse default arguments to the functions.
18364 Then, in a second pass, parse the bodies of the functions.
18365 This two-phased approach handles cases like:
18367 struct S {
18368 void f() { g(); }
18369 void g(int i = 3);
18373 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18374 ix, e)
18376 decl = e->decl;
18377 /* If there are default arguments that have not yet been processed,
18378 take care of them now. */
18379 if (class_type != e->class_type)
18381 if (pushed_scope)
18382 pop_scope (pushed_scope);
18383 class_type = e->class_type;
18384 pushed_scope = push_scope (class_type);
18386 /* Make sure that any template parameters are in scope. */
18387 maybe_begin_member_template_processing (decl);
18388 /* Parse the default argument expressions. */
18389 cp_parser_late_parsing_default_args (parser, decl);
18390 /* Remove any template parameters from the symbol table. */
18391 maybe_end_member_template_processing ();
18393 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18394 /* Now parse any NSDMIs. */
18395 save_ccp = current_class_ptr;
18396 save_ccr = current_class_ref;
18397 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18399 if (class_type != DECL_CONTEXT (decl))
18401 if (pushed_scope)
18402 pop_scope (pushed_scope);
18403 class_type = DECL_CONTEXT (decl);
18404 pushed_scope = push_scope (class_type);
18406 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18407 cp_parser_late_parsing_nsdmi (parser, decl);
18409 VEC_truncate (tree, unparsed_nsdmis, 0);
18410 current_class_ptr = save_ccp;
18411 current_class_ref = save_ccr;
18412 if (pushed_scope)
18413 pop_scope (pushed_scope);
18414 /* Now parse the body of the functions. */
18415 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18416 cp_parser_late_parsing_for_member (parser, decl);
18417 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18420 /* Put back any saved access checks. */
18421 pop_deferring_access_checks ();
18423 /* Restore saved state. */
18424 parser->in_switch_statement_p = in_switch_statement_p;
18425 parser->in_statement = in_statement;
18426 parser->in_function_body = saved_in_function_body;
18427 parser->num_template_parameter_lists
18428 = saved_num_template_parameter_lists;
18429 parser->in_unbraced_linkage_specification_p
18430 = saved_in_unbraced_linkage_specification_p;
18432 return type;
18435 static tree
18436 cp_parser_class_specifier (cp_parser* parser)
18438 tree ret;
18439 timevar_push (TV_PARSE_STRUCT);
18440 ret = cp_parser_class_specifier_1 (parser);
18441 timevar_pop (TV_PARSE_STRUCT);
18442 return ret;
18445 /* Parse a class-head.
18447 class-head:
18448 class-key identifier [opt] base-clause [opt]
18449 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18450 class-key nested-name-specifier [opt] template-id
18451 base-clause [opt]
18453 class-virt-specifier:
18454 final
18456 GNU Extensions:
18457 class-key attributes identifier [opt] base-clause [opt]
18458 class-key attributes nested-name-specifier identifier base-clause [opt]
18459 class-key attributes nested-name-specifier [opt] template-id
18460 base-clause [opt]
18462 Upon return BASES is initialized to the list of base classes (or
18463 NULL, if there are none) in the same form returned by
18464 cp_parser_base_clause.
18466 Returns the TYPE of the indicated class. Sets
18467 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18468 involving a nested-name-specifier was used, and FALSE otherwise.
18470 Returns error_mark_node if this is not a class-head.
18472 Returns NULL_TREE if the class-head is syntactically valid, but
18473 semantically invalid in a way that means we should skip the entire
18474 body of the class. */
18476 static tree
18477 cp_parser_class_head (cp_parser* parser,
18478 bool* nested_name_specifier_p)
18480 tree nested_name_specifier;
18481 enum tag_types class_key;
18482 tree id = NULL_TREE;
18483 tree type = NULL_TREE;
18484 tree attributes;
18485 tree bases;
18486 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18487 bool template_id_p = false;
18488 bool qualified_p = false;
18489 bool invalid_nested_name_p = false;
18490 bool invalid_explicit_specialization_p = false;
18491 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18492 tree pushed_scope = NULL_TREE;
18493 unsigned num_templates;
18494 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18495 /* Assume no nested-name-specifier will be present. */
18496 *nested_name_specifier_p = false;
18497 /* Assume no template parameter lists will be used in defining the
18498 type. */
18499 num_templates = 0;
18500 parser->colon_corrects_to_scope_p = false;
18502 /* Look for the class-key. */
18503 class_key = cp_parser_class_key (parser);
18504 if (class_key == none_type)
18505 return error_mark_node;
18507 /* Parse the attributes. */
18508 attributes = cp_parser_attributes_opt (parser);
18510 /* If the next token is `::', that is invalid -- but sometimes
18511 people do try to write:
18513 struct ::S {};
18515 Handle this gracefully by accepting the extra qualifier, and then
18516 issuing an error about it later if this really is a
18517 class-head. If it turns out just to be an elaborated type
18518 specifier, remain silent. */
18519 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18520 qualified_p = true;
18522 push_deferring_access_checks (dk_no_check);
18524 /* Determine the name of the class. Begin by looking for an
18525 optional nested-name-specifier. */
18526 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18527 nested_name_specifier
18528 = cp_parser_nested_name_specifier_opt (parser,
18529 /*typename_keyword_p=*/false,
18530 /*check_dependency_p=*/false,
18531 /*type_p=*/true,
18532 /*is_declaration=*/false);
18533 /* If there was a nested-name-specifier, then there *must* be an
18534 identifier. */
18535 if (nested_name_specifier)
18537 type_start_token = cp_lexer_peek_token (parser->lexer);
18538 /* Although the grammar says `identifier', it really means
18539 `class-name' or `template-name'. You are only allowed to
18540 define a class that has already been declared with this
18541 syntax.
18543 The proposed resolution for Core Issue 180 says that wherever
18544 you see `class T::X' you should treat `X' as a type-name.
18546 It is OK to define an inaccessible class; for example:
18548 class A { class B; };
18549 class A::B {};
18551 We do not know if we will see a class-name, or a
18552 template-name. We look for a class-name first, in case the
18553 class-name is a template-id; if we looked for the
18554 template-name first we would stop after the template-name. */
18555 cp_parser_parse_tentatively (parser);
18556 type = cp_parser_class_name (parser,
18557 /*typename_keyword_p=*/false,
18558 /*template_keyword_p=*/false,
18559 class_type,
18560 /*check_dependency_p=*/false,
18561 /*class_head_p=*/true,
18562 /*is_declaration=*/false);
18563 /* If that didn't work, ignore the nested-name-specifier. */
18564 if (!cp_parser_parse_definitely (parser))
18566 invalid_nested_name_p = true;
18567 type_start_token = cp_lexer_peek_token (parser->lexer);
18568 id = cp_parser_identifier (parser);
18569 if (id == error_mark_node)
18570 id = NULL_TREE;
18572 /* If we could not find a corresponding TYPE, treat this
18573 declaration like an unqualified declaration. */
18574 if (type == error_mark_node)
18575 nested_name_specifier = NULL_TREE;
18576 /* Otherwise, count the number of templates used in TYPE and its
18577 containing scopes. */
18578 else
18580 tree scope;
18582 for (scope = TREE_TYPE (type);
18583 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18584 scope = (TYPE_P (scope)
18585 ? TYPE_CONTEXT (scope)
18586 : DECL_CONTEXT (scope)))
18587 if (TYPE_P (scope)
18588 && CLASS_TYPE_P (scope)
18589 && CLASSTYPE_TEMPLATE_INFO (scope)
18590 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18591 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18592 ++num_templates;
18595 /* Otherwise, the identifier is optional. */
18596 else
18598 /* We don't know whether what comes next is a template-id,
18599 an identifier, or nothing at all. */
18600 cp_parser_parse_tentatively (parser);
18601 /* Check for a template-id. */
18602 type_start_token = cp_lexer_peek_token (parser->lexer);
18603 id = cp_parser_template_id (parser,
18604 /*template_keyword_p=*/false,
18605 /*check_dependency_p=*/true,
18606 class_key,
18607 /*is_declaration=*/true);
18608 /* If that didn't work, it could still be an identifier. */
18609 if (!cp_parser_parse_definitely (parser))
18611 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18613 type_start_token = cp_lexer_peek_token (parser->lexer);
18614 id = cp_parser_identifier (parser);
18616 else
18617 id = NULL_TREE;
18619 else
18621 template_id_p = true;
18622 ++num_templates;
18626 pop_deferring_access_checks ();
18628 if (id)
18630 cp_parser_check_for_invalid_template_id (parser, id,
18631 class_key,
18632 type_start_token->location);
18634 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18636 /* If it's not a `:' or a `{' then we can't really be looking at a
18637 class-head, since a class-head only appears as part of a
18638 class-specifier. We have to detect this situation before calling
18639 xref_tag, since that has irreversible side-effects. */
18640 if (!cp_parser_next_token_starts_class_definition_p (parser))
18642 cp_parser_error (parser, "expected %<{%> or %<:%>");
18643 type = error_mark_node;
18644 goto out;
18647 /* At this point, we're going ahead with the class-specifier, even
18648 if some other problem occurs. */
18649 cp_parser_commit_to_tentative_parse (parser);
18650 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18652 cp_parser_error (parser,
18653 "cannot specify %<override%> for a class");
18654 type = error_mark_node;
18655 goto out;
18657 /* Issue the error about the overly-qualified name now. */
18658 if (qualified_p)
18660 cp_parser_error (parser,
18661 "global qualification of class name is invalid");
18662 type = error_mark_node;
18663 goto out;
18665 else if (invalid_nested_name_p)
18667 cp_parser_error (parser,
18668 "qualified name does not name a class");
18669 type = error_mark_node;
18670 goto out;
18672 else if (nested_name_specifier)
18674 tree scope;
18676 /* Reject typedef-names in class heads. */
18677 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18679 error_at (type_start_token->location,
18680 "invalid class name in declaration of %qD",
18681 type);
18682 type = NULL_TREE;
18683 goto done;
18686 /* Figure out in what scope the declaration is being placed. */
18687 scope = current_scope ();
18688 /* If that scope does not contain the scope in which the
18689 class was originally declared, the program is invalid. */
18690 if (scope && !is_ancestor (scope, nested_name_specifier))
18692 if (at_namespace_scope_p ())
18693 error_at (type_start_token->location,
18694 "declaration of %qD in namespace %qD which does not "
18695 "enclose %qD",
18696 type, scope, nested_name_specifier);
18697 else
18698 error_at (type_start_token->location,
18699 "declaration of %qD in %qD which does not enclose %qD",
18700 type, scope, nested_name_specifier);
18701 type = NULL_TREE;
18702 goto done;
18704 /* [dcl.meaning]
18706 A declarator-id shall not be qualified except for the
18707 definition of a ... nested class outside of its class
18708 ... [or] the definition or explicit instantiation of a
18709 class member of a namespace outside of its namespace. */
18710 if (scope == nested_name_specifier)
18712 permerror (nested_name_specifier_token_start->location,
18713 "extra qualification not allowed");
18714 nested_name_specifier = NULL_TREE;
18715 num_templates = 0;
18718 /* An explicit-specialization must be preceded by "template <>". If
18719 it is not, try to recover gracefully. */
18720 if (at_namespace_scope_p ()
18721 && parser->num_template_parameter_lists == 0
18722 && template_id_p)
18724 error_at (type_start_token->location,
18725 "an explicit specialization must be preceded by %<template <>%>");
18726 invalid_explicit_specialization_p = true;
18727 /* Take the same action that would have been taken by
18728 cp_parser_explicit_specialization. */
18729 ++parser->num_template_parameter_lists;
18730 begin_specialization ();
18732 /* There must be no "return" statements between this point and the
18733 end of this function; set "type "to the correct return value and
18734 use "goto done;" to return. */
18735 /* Make sure that the right number of template parameters were
18736 present. */
18737 if (!cp_parser_check_template_parameters (parser, num_templates,
18738 type_start_token->location,
18739 /*declarator=*/NULL))
18741 /* If something went wrong, there is no point in even trying to
18742 process the class-definition. */
18743 type = NULL_TREE;
18744 goto done;
18747 /* Look up the type. */
18748 if (template_id_p)
18750 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18751 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18752 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18754 error_at (type_start_token->location,
18755 "function template %qD redeclared as a class template", id);
18756 type = error_mark_node;
18758 else
18760 type = TREE_TYPE (id);
18761 type = maybe_process_partial_specialization (type);
18763 if (nested_name_specifier)
18764 pushed_scope = push_scope (nested_name_specifier);
18766 else if (nested_name_specifier)
18768 tree class_type;
18770 /* Given:
18772 template <typename T> struct S { struct T };
18773 template <typename T> struct S<T>::T { };
18775 we will get a TYPENAME_TYPE when processing the definition of
18776 `S::T'. We need to resolve it to the actual type before we
18777 try to define it. */
18778 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18780 class_type = resolve_typename_type (TREE_TYPE (type),
18781 /*only_current_p=*/false);
18782 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18783 type = TYPE_NAME (class_type);
18784 else
18786 cp_parser_error (parser, "could not resolve typename type");
18787 type = error_mark_node;
18791 if (maybe_process_partial_specialization (TREE_TYPE (type))
18792 == error_mark_node)
18794 type = NULL_TREE;
18795 goto done;
18798 class_type = current_class_type;
18799 /* Enter the scope indicated by the nested-name-specifier. */
18800 pushed_scope = push_scope (nested_name_specifier);
18801 /* Get the canonical version of this type. */
18802 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18803 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18804 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18806 type = push_template_decl (type);
18807 if (type == error_mark_node)
18809 type = NULL_TREE;
18810 goto done;
18814 type = TREE_TYPE (type);
18815 *nested_name_specifier_p = true;
18817 else /* The name is not a nested name. */
18819 /* If the class was unnamed, create a dummy name. */
18820 if (!id)
18821 id = make_anon_name ();
18822 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18823 parser->num_template_parameter_lists);
18826 /* Indicate whether this class was declared as a `class' or as a
18827 `struct'. */
18828 if (TREE_CODE (type) == RECORD_TYPE)
18829 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18830 cp_parser_check_class_key (class_key, type);
18832 /* If this type was already complete, and we see another definition,
18833 that's an error. */
18834 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18836 error_at (type_start_token->location, "redefinition of %q#T",
18837 type);
18838 error_at (type_start_token->location, "previous definition of %q+#T",
18839 type);
18840 type = NULL_TREE;
18841 goto done;
18843 else if (type == error_mark_node)
18844 type = NULL_TREE;
18846 if (type)
18848 /* Apply attributes now, before any use of the class as a template
18849 argument in its base list. */
18850 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18851 fixup_attribute_variants (type);
18854 /* We will have entered the scope containing the class; the names of
18855 base classes should be looked up in that context. For example:
18857 struct A { struct B {}; struct C; };
18858 struct A::C : B {};
18860 is valid. */
18862 /* Get the list of base-classes, if there is one. */
18863 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18864 bases = cp_parser_base_clause (parser);
18865 else
18866 bases = NULL_TREE;
18868 /* If we're really defining a class, process the base classes.
18869 If they're invalid, fail. */
18870 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
18871 && !xref_basetypes (type, bases))
18872 type = NULL_TREE;
18874 done:
18875 /* Leave the scope given by the nested-name-specifier. We will
18876 enter the class scope itself while processing the members. */
18877 if (pushed_scope)
18878 pop_scope (pushed_scope);
18880 if (invalid_explicit_specialization_p)
18882 end_specialization ();
18883 --parser->num_template_parameter_lists;
18886 if (type)
18887 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18888 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18889 CLASSTYPE_FINAL (type) = 1;
18890 out:
18891 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18892 return type;
18895 /* Parse a class-key.
18897 class-key:
18898 class
18899 struct
18900 union
18902 Returns the kind of class-key specified, or none_type to indicate
18903 error. */
18905 static enum tag_types
18906 cp_parser_class_key (cp_parser* parser)
18908 cp_token *token;
18909 enum tag_types tag_type;
18911 /* Look for the class-key. */
18912 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18913 if (!token)
18914 return none_type;
18916 /* Check to see if the TOKEN is a class-key. */
18917 tag_type = cp_parser_token_is_class_key (token);
18918 if (!tag_type)
18919 cp_parser_error (parser, "expected class-key");
18920 return tag_type;
18923 /* Parse an (optional) member-specification.
18925 member-specification:
18926 member-declaration member-specification [opt]
18927 access-specifier : member-specification [opt] */
18929 static void
18930 cp_parser_member_specification_opt (cp_parser* parser)
18932 while (true)
18934 cp_token *token;
18935 enum rid keyword;
18937 /* Peek at the next token. */
18938 token = cp_lexer_peek_token (parser->lexer);
18939 /* If it's a `}', or EOF then we've seen all the members. */
18940 if (token->type == CPP_CLOSE_BRACE
18941 || token->type == CPP_EOF
18942 || token->type == CPP_PRAGMA_EOL)
18943 break;
18945 /* See if this token is a keyword. */
18946 keyword = token->keyword;
18947 switch (keyword)
18949 case RID_PUBLIC:
18950 case RID_PROTECTED:
18951 case RID_PRIVATE:
18952 /* Consume the access-specifier. */
18953 cp_lexer_consume_token (parser->lexer);
18954 /* Remember which access-specifier is active. */
18955 current_access_specifier = token->u.value;
18956 /* Look for the `:'. */
18957 cp_parser_require (parser, CPP_COLON, RT_COLON);
18958 break;
18960 default:
18961 /* Accept #pragmas at class scope. */
18962 if (token->type == CPP_PRAGMA)
18964 cp_parser_pragma (parser, pragma_external);
18965 break;
18968 /* Otherwise, the next construction must be a
18969 member-declaration. */
18970 cp_parser_member_declaration (parser);
18975 /* Parse a member-declaration.
18977 member-declaration:
18978 decl-specifier-seq [opt] member-declarator-list [opt] ;
18979 function-definition ; [opt]
18980 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18981 using-declaration
18982 template-declaration
18983 alias-declaration
18985 member-declarator-list:
18986 member-declarator
18987 member-declarator-list , member-declarator
18989 member-declarator:
18990 declarator pure-specifier [opt]
18991 declarator constant-initializer [opt]
18992 identifier [opt] : constant-expression
18994 GNU Extensions:
18996 member-declaration:
18997 __extension__ member-declaration
18999 member-declarator:
19000 declarator attributes [opt] pure-specifier [opt]
19001 declarator attributes [opt] constant-initializer [opt]
19002 identifier [opt] attributes [opt] : constant-expression
19004 C++0x Extensions:
19006 member-declaration:
19007 static_assert-declaration */
19009 static void
19010 cp_parser_member_declaration (cp_parser* parser)
19012 cp_decl_specifier_seq decl_specifiers;
19013 tree prefix_attributes;
19014 tree decl;
19015 int declares_class_or_enum;
19016 bool friend_p;
19017 cp_token *token = NULL;
19018 cp_token *decl_spec_token_start = NULL;
19019 cp_token *initializer_token_start = NULL;
19020 int saved_pedantic;
19021 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19023 /* Check for the `__extension__' keyword. */
19024 if (cp_parser_extension_opt (parser, &saved_pedantic))
19026 /* Recurse. */
19027 cp_parser_member_declaration (parser);
19028 /* Restore the old value of the PEDANTIC flag. */
19029 pedantic = saved_pedantic;
19031 return;
19034 /* Check for a template-declaration. */
19035 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19037 /* An explicit specialization here is an error condition, and we
19038 expect the specialization handler to detect and report this. */
19039 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19040 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
19041 cp_parser_explicit_specialization (parser);
19042 else
19043 cp_parser_template_declaration (parser, /*member_p=*/true);
19045 return;
19048 /* Check for a using-declaration. */
19049 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
19051 if (cxx_dialect < cxx0x)
19053 /* Parse the using-declaration. */
19054 cp_parser_using_declaration (parser,
19055 /*access_declaration_p=*/false);
19056 return;
19058 else
19060 tree decl;
19061 cp_parser_parse_tentatively (parser);
19062 decl = cp_parser_alias_declaration (parser);
19063 if (cp_parser_parse_definitely (parser))
19064 finish_member_declaration (decl);
19065 else
19066 cp_parser_using_declaration (parser,
19067 /*access_declaration_p=*/false);
19068 return;
19072 /* Check for @defs. */
19073 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
19075 tree ivar, member;
19076 tree ivar_chains = cp_parser_objc_defs_expression (parser);
19077 ivar = ivar_chains;
19078 while (ivar)
19080 member = ivar;
19081 ivar = TREE_CHAIN (member);
19082 TREE_CHAIN (member) = NULL_TREE;
19083 finish_member_declaration (member);
19085 return;
19088 /* If the next token is `static_assert' we have a static assertion. */
19089 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
19091 cp_parser_static_assert (parser, /*member_p=*/true);
19092 return;
19095 parser->colon_corrects_to_scope_p = false;
19097 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
19098 goto out;
19100 /* Parse the decl-specifier-seq. */
19101 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19102 cp_parser_decl_specifier_seq (parser,
19103 CP_PARSER_FLAGS_OPTIONAL,
19104 &decl_specifiers,
19105 &declares_class_or_enum);
19106 /* Check for an invalid type-name. */
19107 if (!decl_specifiers.any_type_specifiers_p
19108 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19109 goto out;
19110 /* If there is no declarator, then the decl-specifier-seq should
19111 specify a type. */
19112 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19114 /* If there was no decl-specifier-seq, and the next token is a
19115 `;', then we have something like:
19117 struct S { ; };
19119 [class.mem]
19121 Each member-declaration shall declare at least one member
19122 name of the class. */
19123 if (!decl_specifiers.any_specifiers_p)
19125 cp_token *token = cp_lexer_peek_token (parser->lexer);
19126 if (!in_system_header_at (token->location))
19127 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
19129 else
19131 tree type;
19133 /* See if this declaration is a friend. */
19134 friend_p = cp_parser_friend_p (&decl_specifiers);
19135 /* If there were decl-specifiers, check to see if there was
19136 a class-declaration. */
19137 type = check_tag_decl (&decl_specifiers,
19138 /*explicit_type_instantiation_p=*/false);
19139 /* Nested classes have already been added to the class, but
19140 a `friend' needs to be explicitly registered. */
19141 if (friend_p)
19143 /* If the `friend' keyword was present, the friend must
19144 be introduced with a class-key. */
19145 if (!declares_class_or_enum && cxx_dialect < cxx0x)
19146 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
19147 "in C++03 a class-key must be used "
19148 "when declaring a friend");
19149 /* In this case:
19151 template <typename T> struct A {
19152 friend struct A<T>::B;
19155 A<T>::B will be represented by a TYPENAME_TYPE, and
19156 therefore not recognized by check_tag_decl. */
19157 if (!type)
19159 type = decl_specifiers.type;
19160 if (type && TREE_CODE (type) == TYPE_DECL)
19161 type = TREE_TYPE (type);
19163 if (!type || !TYPE_P (type))
19164 error_at (decl_spec_token_start->location,
19165 "friend declaration does not name a class or "
19166 "function");
19167 else
19168 make_friend_class (current_class_type, type,
19169 /*complain=*/true);
19171 /* If there is no TYPE, an error message will already have
19172 been issued. */
19173 else if (!type || type == error_mark_node)
19175 /* An anonymous aggregate has to be handled specially; such
19176 a declaration really declares a data member (with a
19177 particular type), as opposed to a nested class. */
19178 else if (ANON_AGGR_TYPE_P (type))
19180 /* C++11 9.5/6. */
19181 if (decl_specifiers.storage_class != sc_none)
19182 error_at (decl_spec_token_start->location,
19183 "a storage class on an anonymous aggregate "
19184 "in class scope is not allowed");
19186 /* Remove constructors and such from TYPE, now that we
19187 know it is an anonymous aggregate. */
19188 fixup_anonymous_aggr (type);
19189 /* And make the corresponding data member. */
19190 decl = build_decl (decl_spec_token_start->location,
19191 FIELD_DECL, NULL_TREE, type);
19192 /* Add it to the class. */
19193 finish_member_declaration (decl);
19195 else
19196 cp_parser_check_access_in_redeclaration
19197 (TYPE_NAME (type),
19198 decl_spec_token_start->location);
19201 else
19203 bool assume_semicolon = false;
19205 /* Clear attributes from the decl_specifiers but keep them
19206 around as prefix attributes that apply them to the entity
19207 being declared. */
19208 prefix_attributes = decl_specifiers.attributes;
19209 decl_specifiers.attributes = NULL_TREE;
19211 /* See if these declarations will be friends. */
19212 friend_p = cp_parser_friend_p (&decl_specifiers);
19214 /* Keep going until we hit the `;' at the end of the
19215 declaration. */
19216 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19218 tree attributes = NULL_TREE;
19219 tree first_attribute;
19221 /* Peek at the next token. */
19222 token = cp_lexer_peek_token (parser->lexer);
19224 /* Check for a bitfield declaration. */
19225 if (token->type == CPP_COLON
19226 || (token->type == CPP_NAME
19227 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19228 == CPP_COLON))
19230 tree identifier;
19231 tree width;
19233 /* Get the name of the bitfield. Note that we cannot just
19234 check TOKEN here because it may have been invalidated by
19235 the call to cp_lexer_peek_nth_token above. */
19236 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19237 identifier = cp_parser_identifier (parser);
19238 else
19239 identifier = NULL_TREE;
19241 /* Consume the `:' token. */
19242 cp_lexer_consume_token (parser->lexer);
19243 /* Get the width of the bitfield. */
19244 width
19245 = cp_parser_constant_expression (parser,
19246 /*allow_non_constant=*/false,
19247 NULL);
19249 /* Look for attributes that apply to the bitfield. */
19250 attributes = cp_parser_attributes_opt (parser);
19251 /* Remember which attributes are prefix attributes and
19252 which are not. */
19253 first_attribute = attributes;
19254 /* Combine the attributes. */
19255 attributes = chainon (prefix_attributes, attributes);
19257 /* Create the bitfield declaration. */
19258 decl = grokbitfield (identifier
19259 ? make_id_declarator (NULL_TREE,
19260 identifier,
19261 sfk_none)
19262 : NULL,
19263 &decl_specifiers,
19264 width,
19265 attributes);
19267 else
19269 cp_declarator *declarator;
19270 tree initializer;
19271 tree asm_specification;
19272 int ctor_dtor_or_conv_p;
19274 /* Parse the declarator. */
19275 declarator
19276 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19277 &ctor_dtor_or_conv_p,
19278 /*parenthesized_p=*/NULL,
19279 /*member_p=*/true);
19281 /* If something went wrong parsing the declarator, make sure
19282 that we at least consume some tokens. */
19283 if (declarator == cp_error_declarator)
19285 /* Skip to the end of the statement. */
19286 cp_parser_skip_to_end_of_statement (parser);
19287 /* If the next token is not a semicolon, that is
19288 probably because we just skipped over the body of
19289 a function. So, we consume a semicolon if
19290 present, but do not issue an error message if it
19291 is not present. */
19292 if (cp_lexer_next_token_is (parser->lexer,
19293 CPP_SEMICOLON))
19294 cp_lexer_consume_token (parser->lexer);
19295 goto out;
19298 if (declares_class_or_enum & 2)
19299 cp_parser_check_for_definition_in_return_type
19300 (declarator, decl_specifiers.type,
19301 decl_specifiers.locations[ds_type_spec]);
19303 /* Look for an asm-specification. */
19304 asm_specification = cp_parser_asm_specification_opt (parser);
19305 /* Look for attributes that apply to the declaration. */
19306 attributes = cp_parser_attributes_opt (parser);
19307 /* Remember which attributes are prefix attributes and
19308 which are not. */
19309 first_attribute = attributes;
19310 /* Combine the attributes. */
19311 attributes = chainon (prefix_attributes, attributes);
19313 /* If it's an `=', then we have a constant-initializer or a
19314 pure-specifier. It is not correct to parse the
19315 initializer before registering the member declaration
19316 since the member declaration should be in scope while
19317 its initializer is processed. However, the rest of the
19318 front end does not yet provide an interface that allows
19319 us to handle this correctly. */
19320 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19322 /* In [class.mem]:
19324 A pure-specifier shall be used only in the declaration of
19325 a virtual function.
19327 A member-declarator can contain a constant-initializer
19328 only if it declares a static member of integral or
19329 enumeration type.
19331 Therefore, if the DECLARATOR is for a function, we look
19332 for a pure-specifier; otherwise, we look for a
19333 constant-initializer. When we call `grokfield', it will
19334 perform more stringent semantics checks. */
19335 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19336 if (function_declarator_p (declarator)
19337 || (decl_specifiers.type
19338 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19339 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19340 == FUNCTION_TYPE)))
19341 initializer = cp_parser_pure_specifier (parser);
19342 else if (decl_specifiers.storage_class != sc_static)
19343 initializer = cp_parser_save_nsdmi (parser);
19344 else if (cxx_dialect >= cxx0x)
19346 bool nonconst;
19347 /* Don't require a constant rvalue in C++11, since we
19348 might want a reference constant. We'll enforce
19349 constancy later. */
19350 cp_lexer_consume_token (parser->lexer);
19351 /* Parse the initializer. */
19352 initializer = cp_parser_initializer_clause (parser,
19353 &nonconst);
19355 else
19356 /* Parse the initializer. */
19357 initializer = cp_parser_constant_initializer (parser);
19359 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19360 && !function_declarator_p (declarator))
19362 bool x;
19363 if (decl_specifiers.storage_class != sc_static)
19364 initializer = cp_parser_save_nsdmi (parser);
19365 else
19366 initializer = cp_parser_initializer (parser, &x, &x);
19368 /* Otherwise, there is no initializer. */
19369 else
19370 initializer = NULL_TREE;
19372 /* See if we are probably looking at a function
19373 definition. We are certainly not looking at a
19374 member-declarator. Calling `grokfield' has
19375 side-effects, so we must not do it unless we are sure
19376 that we are looking at a member-declarator. */
19377 if (cp_parser_token_starts_function_definition_p
19378 (cp_lexer_peek_token (parser->lexer)))
19380 /* The grammar does not allow a pure-specifier to be
19381 used when a member function is defined. (It is
19382 possible that this fact is an oversight in the
19383 standard, since a pure function may be defined
19384 outside of the class-specifier. */
19385 if (initializer && initializer_token_start)
19386 error_at (initializer_token_start->location,
19387 "pure-specifier on function-definition");
19388 decl = cp_parser_save_member_function_body (parser,
19389 &decl_specifiers,
19390 declarator,
19391 attributes);
19392 /* If the member was not a friend, declare it here. */
19393 if (!friend_p)
19394 finish_member_declaration (decl);
19395 /* Peek at the next token. */
19396 token = cp_lexer_peek_token (parser->lexer);
19397 /* If the next token is a semicolon, consume it. */
19398 if (token->type == CPP_SEMICOLON)
19399 cp_lexer_consume_token (parser->lexer);
19400 goto out;
19402 else
19403 if (declarator->kind == cdk_function)
19404 declarator->id_loc = token->location;
19405 /* Create the declaration. */
19406 decl = grokfield (declarator, &decl_specifiers,
19407 initializer, /*init_const_expr_p=*/true,
19408 asm_specification,
19409 attributes);
19412 /* Reset PREFIX_ATTRIBUTES. */
19413 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19414 attributes = TREE_CHAIN (attributes);
19415 if (attributes)
19416 TREE_CHAIN (attributes) = NULL_TREE;
19418 /* If there is any qualification still in effect, clear it
19419 now; we will be starting fresh with the next declarator. */
19420 parser->scope = NULL_TREE;
19421 parser->qualifying_scope = NULL_TREE;
19422 parser->object_scope = NULL_TREE;
19423 /* If it's a `,', then there are more declarators. */
19424 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19425 cp_lexer_consume_token (parser->lexer);
19426 /* If the next token isn't a `;', then we have a parse error. */
19427 else if (cp_lexer_next_token_is_not (parser->lexer,
19428 CPP_SEMICOLON))
19430 /* The next token might be a ways away from where the
19431 actual semicolon is missing. Find the previous token
19432 and use that for our error position. */
19433 cp_token *token = cp_lexer_previous_token (parser->lexer);
19434 error_at (token->location,
19435 "expected %<;%> at end of member declaration");
19437 /* Assume that the user meant to provide a semicolon. If
19438 we were to cp_parser_skip_to_end_of_statement, we might
19439 skip to a semicolon inside a member function definition
19440 and issue nonsensical error messages. */
19441 assume_semicolon = true;
19444 if (decl)
19446 /* Add DECL to the list of members. */
19447 if (!friend_p)
19448 finish_member_declaration (decl);
19450 if (TREE_CODE (decl) == FUNCTION_DECL)
19451 cp_parser_save_default_args (parser, decl);
19452 else if (TREE_CODE (decl) == FIELD_DECL
19453 && !DECL_C_BIT_FIELD (decl)
19454 && DECL_INITIAL (decl))
19455 /* Add DECL to the queue of NSDMI to be parsed later. */
19456 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19459 if (assume_semicolon)
19460 goto out;
19464 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19465 out:
19466 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19469 /* Parse a pure-specifier.
19471 pure-specifier:
19474 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19475 Otherwise, ERROR_MARK_NODE is returned. */
19477 static tree
19478 cp_parser_pure_specifier (cp_parser* parser)
19480 cp_token *token;
19482 /* Look for the `=' token. */
19483 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19484 return error_mark_node;
19485 /* Look for the `0' token. */
19486 token = cp_lexer_peek_token (parser->lexer);
19488 if (token->type == CPP_EOF
19489 || token->type == CPP_PRAGMA_EOL)
19490 return error_mark_node;
19492 cp_lexer_consume_token (parser->lexer);
19494 /* Accept = default or = delete in c++0x mode. */
19495 if (token->keyword == RID_DEFAULT
19496 || token->keyword == RID_DELETE)
19498 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19499 return token->u.value;
19502 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19503 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19505 cp_parser_error (parser,
19506 "invalid pure specifier (only %<= 0%> is allowed)");
19507 cp_parser_skip_to_end_of_statement (parser);
19508 return error_mark_node;
19510 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19512 error_at (token->location, "templates may not be %<virtual%>");
19513 return error_mark_node;
19516 return integer_zero_node;
19519 /* Parse a constant-initializer.
19521 constant-initializer:
19522 = constant-expression
19524 Returns a representation of the constant-expression. */
19526 static tree
19527 cp_parser_constant_initializer (cp_parser* parser)
19529 /* Look for the `=' token. */
19530 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19531 return error_mark_node;
19533 /* It is invalid to write:
19535 struct S { static const int i = { 7 }; };
19538 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19540 cp_parser_error (parser,
19541 "a brace-enclosed initializer is not allowed here");
19542 /* Consume the opening brace. */
19543 cp_lexer_consume_token (parser->lexer);
19544 /* Skip the initializer. */
19545 cp_parser_skip_to_closing_brace (parser);
19546 /* Look for the trailing `}'. */
19547 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19549 return error_mark_node;
19552 return cp_parser_constant_expression (parser,
19553 /*allow_non_constant=*/false,
19554 NULL);
19557 /* Derived classes [gram.class.derived] */
19559 /* Parse a base-clause.
19561 base-clause:
19562 : base-specifier-list
19564 base-specifier-list:
19565 base-specifier ... [opt]
19566 base-specifier-list , base-specifier ... [opt]
19568 Returns a TREE_LIST representing the base-classes, in the order in
19569 which they were declared. The representation of each node is as
19570 described by cp_parser_base_specifier.
19572 In the case that no bases are specified, this function will return
19573 NULL_TREE, not ERROR_MARK_NODE. */
19575 static tree
19576 cp_parser_base_clause (cp_parser* parser)
19578 tree bases = NULL_TREE;
19580 /* Look for the `:' that begins the list. */
19581 cp_parser_require (parser, CPP_COLON, RT_COLON);
19583 /* Scan the base-specifier-list. */
19584 while (true)
19586 cp_token *token;
19587 tree base;
19588 bool pack_expansion_p = false;
19590 /* Look for the base-specifier. */
19591 base = cp_parser_base_specifier (parser);
19592 /* Look for the (optional) ellipsis. */
19593 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19595 /* Consume the `...'. */
19596 cp_lexer_consume_token (parser->lexer);
19598 pack_expansion_p = true;
19601 /* Add BASE to the front of the list. */
19602 if (base && base != error_mark_node)
19604 if (pack_expansion_p)
19605 /* Make this a pack expansion type. */
19606 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19608 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19610 TREE_CHAIN (base) = bases;
19611 bases = base;
19614 /* Peek at the next token. */
19615 token = cp_lexer_peek_token (parser->lexer);
19616 /* If it's not a comma, then the list is complete. */
19617 if (token->type != CPP_COMMA)
19618 break;
19619 /* Consume the `,'. */
19620 cp_lexer_consume_token (parser->lexer);
19623 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19624 base class had a qualified name. However, the next name that
19625 appears is certainly not qualified. */
19626 parser->scope = NULL_TREE;
19627 parser->qualifying_scope = NULL_TREE;
19628 parser->object_scope = NULL_TREE;
19630 return nreverse (bases);
19633 /* Parse a base-specifier.
19635 base-specifier:
19636 :: [opt] nested-name-specifier [opt] class-name
19637 virtual access-specifier [opt] :: [opt] nested-name-specifier
19638 [opt] class-name
19639 access-specifier virtual [opt] :: [opt] nested-name-specifier
19640 [opt] class-name
19642 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19643 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19644 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19645 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19647 static tree
19648 cp_parser_base_specifier (cp_parser* parser)
19650 cp_token *token;
19651 bool done = false;
19652 bool virtual_p = false;
19653 bool duplicate_virtual_error_issued_p = false;
19654 bool duplicate_access_error_issued_p = false;
19655 bool class_scope_p, template_p;
19656 tree access = access_default_node;
19657 tree type;
19659 /* Process the optional `virtual' and `access-specifier'. */
19660 while (!done)
19662 /* Peek at the next token. */
19663 token = cp_lexer_peek_token (parser->lexer);
19664 /* Process `virtual'. */
19665 switch (token->keyword)
19667 case RID_VIRTUAL:
19668 /* If `virtual' appears more than once, issue an error. */
19669 if (virtual_p && !duplicate_virtual_error_issued_p)
19671 cp_parser_error (parser,
19672 "%<virtual%> specified more than once in base-specified");
19673 duplicate_virtual_error_issued_p = true;
19676 virtual_p = true;
19678 /* Consume the `virtual' token. */
19679 cp_lexer_consume_token (parser->lexer);
19681 break;
19683 case RID_PUBLIC:
19684 case RID_PROTECTED:
19685 case RID_PRIVATE:
19686 /* If more than one access specifier appears, issue an
19687 error. */
19688 if (access != access_default_node
19689 && !duplicate_access_error_issued_p)
19691 cp_parser_error (parser,
19692 "more than one access specifier in base-specified");
19693 duplicate_access_error_issued_p = true;
19696 access = ridpointers[(int) token->keyword];
19698 /* Consume the access-specifier. */
19699 cp_lexer_consume_token (parser->lexer);
19701 break;
19703 default:
19704 done = true;
19705 break;
19708 /* It is not uncommon to see programs mechanically, erroneously, use
19709 the 'typename' keyword to denote (dependent) qualified types
19710 as base classes. */
19711 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19713 token = cp_lexer_peek_token (parser->lexer);
19714 if (!processing_template_decl)
19715 error_at (token->location,
19716 "keyword %<typename%> not allowed outside of templates");
19717 else
19718 error_at (token->location,
19719 "keyword %<typename%> not allowed in this context "
19720 "(the base class is implicitly a type)");
19721 cp_lexer_consume_token (parser->lexer);
19724 /* Look for the optional `::' operator. */
19725 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19726 /* Look for the nested-name-specifier. The simplest way to
19727 implement:
19729 [temp.res]
19731 The keyword `typename' is not permitted in a base-specifier or
19732 mem-initializer; in these contexts a qualified name that
19733 depends on a template-parameter is implicitly assumed to be a
19734 type name.
19736 is to pretend that we have seen the `typename' keyword at this
19737 point. */
19738 cp_parser_nested_name_specifier_opt (parser,
19739 /*typename_keyword_p=*/true,
19740 /*check_dependency_p=*/true,
19741 typename_type,
19742 /*is_declaration=*/true);
19743 /* If the base class is given by a qualified name, assume that names
19744 we see are type names or templates, as appropriate. */
19745 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19746 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19748 if (!parser->scope
19749 && cp_lexer_next_token_is_decltype (parser->lexer))
19750 /* DR 950 allows decltype as a base-specifier. */
19751 type = cp_parser_decltype (parser);
19752 else
19754 /* Otherwise, look for the class-name. */
19755 type = cp_parser_class_name (parser,
19756 class_scope_p,
19757 template_p,
19758 typename_type,
19759 /*check_dependency_p=*/true,
19760 /*class_head_p=*/false,
19761 /*is_declaration=*/true);
19762 type = TREE_TYPE (type);
19765 if (type == error_mark_node)
19766 return error_mark_node;
19768 return finish_base_specifier (type, access, virtual_p);
19771 /* Exception handling [gram.exception] */
19773 /* Parse an (optional) noexcept-specification.
19775 noexcept-specification:
19776 noexcept ( constant-expression ) [opt]
19778 If no noexcept-specification is present, returns NULL_TREE.
19779 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19780 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19781 there are no parentheses. CONSUMED_EXPR will be set accordingly.
19782 Otherwise, returns a noexcept specification unless RETURN_COND is true,
19783 in which case a boolean condition is returned instead. */
19785 static tree
19786 cp_parser_noexcept_specification_opt (cp_parser* parser,
19787 bool require_constexpr,
19788 bool* consumed_expr,
19789 bool return_cond)
19791 cp_token *token;
19792 const char *saved_message;
19794 /* Peek at the next token. */
19795 token = cp_lexer_peek_token (parser->lexer);
19797 /* Is it a noexcept-specification? */
19798 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19800 tree expr;
19801 cp_lexer_consume_token (parser->lexer);
19803 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19805 cp_lexer_consume_token (parser->lexer);
19807 if (require_constexpr)
19809 /* Types may not be defined in an exception-specification. */
19810 saved_message = parser->type_definition_forbidden_message;
19811 parser->type_definition_forbidden_message
19812 = G_("types may not be defined in an exception-specification");
19814 expr = cp_parser_constant_expression (parser, false, NULL);
19816 /* Restore the saved message. */
19817 parser->type_definition_forbidden_message = saved_message;
19819 else
19821 expr = cp_parser_expression (parser, false, NULL);
19822 *consumed_expr = true;
19825 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19827 else
19829 expr = boolean_true_node;
19830 if (!require_constexpr)
19831 *consumed_expr = false;
19834 /* We cannot build a noexcept-spec right away because this will check
19835 that expr is a constexpr. */
19836 if (!return_cond)
19837 return build_noexcept_spec (expr, tf_warning_or_error);
19838 else
19839 return expr;
19841 else
19842 return NULL_TREE;
19845 /* Parse an (optional) exception-specification.
19847 exception-specification:
19848 throw ( type-id-list [opt] )
19850 Returns a TREE_LIST representing the exception-specification. The
19851 TREE_VALUE of each node is a type. */
19853 static tree
19854 cp_parser_exception_specification_opt (cp_parser* parser)
19856 cp_token *token;
19857 tree type_id_list;
19858 const char *saved_message;
19860 /* Peek at the next token. */
19861 token = cp_lexer_peek_token (parser->lexer);
19863 /* Is it a noexcept-specification? */
19864 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19865 false);
19866 if (type_id_list != NULL_TREE)
19867 return type_id_list;
19869 /* If it's not `throw', then there's no exception-specification. */
19870 if (!cp_parser_is_keyword (token, RID_THROW))
19871 return NULL_TREE;
19873 #if 0
19874 /* Enable this once a lot of code has transitioned to noexcept? */
19875 if (cxx_dialect >= cxx0x && !in_system_header)
19876 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19877 "deprecated in C++0x; use %<noexcept%> instead");
19878 #endif
19880 /* Consume the `throw'. */
19881 cp_lexer_consume_token (parser->lexer);
19883 /* Look for the `('. */
19884 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19886 /* Peek at the next token. */
19887 token = cp_lexer_peek_token (parser->lexer);
19888 /* If it's not a `)', then there is a type-id-list. */
19889 if (token->type != CPP_CLOSE_PAREN)
19891 /* Types may not be defined in an exception-specification. */
19892 saved_message = parser->type_definition_forbidden_message;
19893 parser->type_definition_forbidden_message
19894 = G_("types may not be defined in an exception-specification");
19895 /* Parse the type-id-list. */
19896 type_id_list = cp_parser_type_id_list (parser);
19897 /* Restore the saved message. */
19898 parser->type_definition_forbidden_message = saved_message;
19900 else
19901 type_id_list = empty_except_spec;
19903 /* Look for the `)'. */
19904 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19906 return type_id_list;
19909 /* Parse an (optional) type-id-list.
19911 type-id-list:
19912 type-id ... [opt]
19913 type-id-list , type-id ... [opt]
19915 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19916 in the order that the types were presented. */
19918 static tree
19919 cp_parser_type_id_list (cp_parser* parser)
19921 tree types = NULL_TREE;
19923 while (true)
19925 cp_token *token;
19926 tree type;
19928 /* Get the next type-id. */
19929 type = cp_parser_type_id (parser);
19930 /* Parse the optional ellipsis. */
19931 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19933 /* Consume the `...'. */
19934 cp_lexer_consume_token (parser->lexer);
19936 /* Turn the type into a pack expansion expression. */
19937 type = make_pack_expansion (type);
19939 /* Add it to the list. */
19940 types = add_exception_specifier (types, type, /*complain=*/1);
19941 /* Peek at the next token. */
19942 token = cp_lexer_peek_token (parser->lexer);
19943 /* If it is not a `,', we are done. */
19944 if (token->type != CPP_COMMA)
19945 break;
19946 /* Consume the `,'. */
19947 cp_lexer_consume_token (parser->lexer);
19950 return nreverse (types);
19953 /* Parse a try-block.
19955 try-block:
19956 try compound-statement handler-seq */
19958 static tree
19959 cp_parser_try_block (cp_parser* parser)
19961 tree try_block;
19963 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19964 try_block = begin_try_block ();
19965 cp_parser_compound_statement (parser, NULL, true, false);
19966 finish_try_block (try_block);
19967 cp_parser_handler_seq (parser);
19968 finish_handler_sequence (try_block);
19970 return try_block;
19973 /* Parse a function-try-block.
19975 function-try-block:
19976 try ctor-initializer [opt] function-body handler-seq */
19978 static bool
19979 cp_parser_function_try_block (cp_parser* parser)
19981 tree compound_stmt;
19982 tree try_block;
19983 bool ctor_initializer_p;
19985 /* Look for the `try' keyword. */
19986 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19987 return false;
19988 /* Let the rest of the front end know where we are. */
19989 try_block = begin_function_try_block (&compound_stmt);
19990 /* Parse the function-body. */
19991 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
19992 (parser, /*in_function_try_block=*/true);
19993 /* We're done with the `try' part. */
19994 finish_function_try_block (try_block);
19995 /* Parse the handlers. */
19996 cp_parser_handler_seq (parser);
19997 /* We're done with the handlers. */
19998 finish_function_handler_sequence (try_block, compound_stmt);
20000 return ctor_initializer_p;
20003 /* Parse a handler-seq.
20005 handler-seq:
20006 handler handler-seq [opt] */
20008 static void
20009 cp_parser_handler_seq (cp_parser* parser)
20011 while (true)
20013 cp_token *token;
20015 /* Parse the handler. */
20016 cp_parser_handler (parser);
20017 /* Peek at the next token. */
20018 token = cp_lexer_peek_token (parser->lexer);
20019 /* If it's not `catch' then there are no more handlers. */
20020 if (!cp_parser_is_keyword (token, RID_CATCH))
20021 break;
20025 /* Parse a handler.
20027 handler:
20028 catch ( exception-declaration ) compound-statement */
20030 static void
20031 cp_parser_handler (cp_parser* parser)
20033 tree handler;
20034 tree declaration;
20036 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
20037 handler = begin_handler ();
20038 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20039 declaration = cp_parser_exception_declaration (parser);
20040 finish_handler_parms (declaration, handler);
20041 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20042 cp_parser_compound_statement (parser, NULL, false, false);
20043 finish_handler (handler);
20046 /* Parse an exception-declaration.
20048 exception-declaration:
20049 type-specifier-seq declarator
20050 type-specifier-seq abstract-declarator
20051 type-specifier-seq
20054 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20055 ellipsis variant is used. */
20057 static tree
20058 cp_parser_exception_declaration (cp_parser* parser)
20060 cp_decl_specifier_seq type_specifiers;
20061 cp_declarator *declarator;
20062 const char *saved_message;
20064 /* If it's an ellipsis, it's easy to handle. */
20065 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20067 /* Consume the `...' token. */
20068 cp_lexer_consume_token (parser->lexer);
20069 return NULL_TREE;
20072 /* Types may not be defined in exception-declarations. */
20073 saved_message = parser->type_definition_forbidden_message;
20074 parser->type_definition_forbidden_message
20075 = G_("types may not be defined in exception-declarations");
20077 /* Parse the type-specifier-seq. */
20078 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
20079 /*is_trailing_return=*/false,
20080 &type_specifiers);
20081 /* If it's a `)', then there is no declarator. */
20082 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
20083 declarator = NULL;
20084 else
20085 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
20086 /*ctor_dtor_or_conv_p=*/NULL,
20087 /*parenthesized_p=*/NULL,
20088 /*member_p=*/false);
20090 /* Restore the saved message. */
20091 parser->type_definition_forbidden_message = saved_message;
20093 if (!type_specifiers.any_specifiers_p)
20094 return error_mark_node;
20096 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
20099 /* Parse a throw-expression.
20101 throw-expression:
20102 throw assignment-expression [opt]
20104 Returns a THROW_EXPR representing the throw-expression. */
20106 static tree
20107 cp_parser_throw_expression (cp_parser* parser)
20109 tree expression;
20110 cp_token* token;
20112 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
20113 token = cp_lexer_peek_token (parser->lexer);
20114 /* Figure out whether or not there is an assignment-expression
20115 following the "throw" keyword. */
20116 if (token->type == CPP_COMMA
20117 || token->type == CPP_SEMICOLON
20118 || token->type == CPP_CLOSE_PAREN
20119 || token->type == CPP_CLOSE_SQUARE
20120 || token->type == CPP_CLOSE_BRACE
20121 || token->type == CPP_COLON)
20122 expression = NULL_TREE;
20123 else
20124 expression = cp_parser_assignment_expression (parser,
20125 /*cast_p=*/false, NULL);
20127 return build_throw (expression);
20130 /* GNU Extensions */
20132 /* Parse an (optional) asm-specification.
20134 asm-specification:
20135 asm ( string-literal )
20137 If the asm-specification is present, returns a STRING_CST
20138 corresponding to the string-literal. Otherwise, returns
20139 NULL_TREE. */
20141 static tree
20142 cp_parser_asm_specification_opt (cp_parser* parser)
20144 cp_token *token;
20145 tree asm_specification;
20147 /* Peek at the next token. */
20148 token = cp_lexer_peek_token (parser->lexer);
20149 /* If the next token isn't the `asm' keyword, then there's no
20150 asm-specification. */
20151 if (!cp_parser_is_keyword (token, RID_ASM))
20152 return NULL_TREE;
20154 /* Consume the `asm' token. */
20155 cp_lexer_consume_token (parser->lexer);
20156 /* Look for the `('. */
20157 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20159 /* Look for the string-literal. */
20160 asm_specification = cp_parser_string_literal (parser, false, false);
20162 /* Look for the `)'. */
20163 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20165 return asm_specification;
20168 /* Parse an asm-operand-list.
20170 asm-operand-list:
20171 asm-operand
20172 asm-operand-list , asm-operand
20174 asm-operand:
20175 string-literal ( expression )
20176 [ string-literal ] string-literal ( expression )
20178 Returns a TREE_LIST representing the operands. The TREE_VALUE of
20179 each node is the expression. The TREE_PURPOSE is itself a
20180 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
20181 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
20182 is a STRING_CST for the string literal before the parenthesis. Returns
20183 ERROR_MARK_NODE if any of the operands are invalid. */
20185 static tree
20186 cp_parser_asm_operand_list (cp_parser* parser)
20188 tree asm_operands = NULL_TREE;
20189 bool invalid_operands = false;
20191 while (true)
20193 tree string_literal;
20194 tree expression;
20195 tree name;
20197 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20199 /* Consume the `[' token. */
20200 cp_lexer_consume_token (parser->lexer);
20201 /* Read the operand name. */
20202 name = cp_parser_identifier (parser);
20203 if (name != error_mark_node)
20204 name = build_string (IDENTIFIER_LENGTH (name),
20205 IDENTIFIER_POINTER (name));
20206 /* Look for the closing `]'. */
20207 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20209 else
20210 name = NULL_TREE;
20211 /* Look for the string-literal. */
20212 string_literal = cp_parser_string_literal (parser, false, false);
20214 /* Look for the `('. */
20215 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20216 /* Parse the expression. */
20217 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20218 /* Look for the `)'. */
20219 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20221 if (name == error_mark_node
20222 || string_literal == error_mark_node
20223 || expression == error_mark_node)
20224 invalid_operands = true;
20226 /* Add this operand to the list. */
20227 asm_operands = tree_cons (build_tree_list (name, string_literal),
20228 expression,
20229 asm_operands);
20230 /* If the next token is not a `,', there are no more
20231 operands. */
20232 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20233 break;
20234 /* Consume the `,'. */
20235 cp_lexer_consume_token (parser->lexer);
20238 return invalid_operands ? error_mark_node : nreverse (asm_operands);
20241 /* Parse an asm-clobber-list.
20243 asm-clobber-list:
20244 string-literal
20245 asm-clobber-list , string-literal
20247 Returns a TREE_LIST, indicating the clobbers in the order that they
20248 appeared. The TREE_VALUE of each node is a STRING_CST. */
20250 static tree
20251 cp_parser_asm_clobber_list (cp_parser* parser)
20253 tree clobbers = NULL_TREE;
20255 while (true)
20257 tree string_literal;
20259 /* Look for the string literal. */
20260 string_literal = cp_parser_string_literal (parser, false, false);
20261 /* Add it to the list. */
20262 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20263 /* If the next token is not a `,', then the list is
20264 complete. */
20265 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20266 break;
20267 /* Consume the `,' token. */
20268 cp_lexer_consume_token (parser->lexer);
20271 return clobbers;
20274 /* Parse an asm-label-list.
20276 asm-label-list:
20277 identifier
20278 asm-label-list , identifier
20280 Returns a TREE_LIST, indicating the labels in the order that they
20281 appeared. The TREE_VALUE of each node is a label. */
20283 static tree
20284 cp_parser_asm_label_list (cp_parser* parser)
20286 tree labels = NULL_TREE;
20288 while (true)
20290 tree identifier, label, name;
20292 /* Look for the identifier. */
20293 identifier = cp_parser_identifier (parser);
20294 if (!error_operand_p (identifier))
20296 label = lookup_label (identifier);
20297 if (TREE_CODE (label) == LABEL_DECL)
20299 TREE_USED (label) = 1;
20300 check_goto (label);
20301 name = build_string (IDENTIFIER_LENGTH (identifier),
20302 IDENTIFIER_POINTER (identifier));
20303 labels = tree_cons (name, label, labels);
20306 /* If the next token is not a `,', then the list is
20307 complete. */
20308 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20309 break;
20310 /* Consume the `,' token. */
20311 cp_lexer_consume_token (parser->lexer);
20314 return nreverse (labels);
20317 /* Return TRUE iff the next tokens in the stream are possibly the
20318 beginning of a GNU extension attribute. */
20320 static bool
20321 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
20323 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
20326 /* Return TRUE iff the next tokens in the stream are possibly the
20327 beginning of a standard C++-11 attribute specifier. */
20329 static bool
20330 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
20332 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
20335 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20336 beginning of a standard C++-11 attribute specifier. */
20338 static bool
20339 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
20341 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20343 return (cxx_dialect >= cxx0x
20344 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
20345 || (token->type == CPP_OPEN_SQUARE
20346 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
20347 && token->type == CPP_OPEN_SQUARE)));
20350 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20351 beginning of a GNU extension attribute. */
20353 static bool
20354 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
20356 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20358 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
20361 /* Return true iff the next tokens can be the beginning of either a
20362 GNU attribute list, or a standard C++11 attribute sequence. */
20364 static bool
20365 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
20367 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
20368 || cp_next_tokens_can_be_std_attribute_p (parser));
20371 /* Return true iff the next Nth tokens can be the beginning of either
20372 a GNU attribute list, or a standard C++11 attribute sequence. */
20374 static bool
20375 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
20377 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
20378 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
20381 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
20382 of GNU attributes, or return NULL. */
20384 static tree
20385 cp_parser_attributes_opt (cp_parser *parser)
20387 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
20388 return cp_parser_gnu_attributes_opt (parser);
20389 return cp_parser_std_attribute_spec_seq (parser);
20392 /* Parse an (optional) series of attributes.
20394 attributes:
20395 attributes attribute
20397 attribute:
20398 __attribute__ (( attribute-list [opt] ))
20400 The return value is as for cp_parser_gnu_attribute_list. */
20402 static tree
20403 cp_parser_gnu_attributes_opt (cp_parser* parser)
20405 tree attributes = NULL_TREE;
20407 while (true)
20409 cp_token *token;
20410 tree attribute_list;
20411 bool ok = true;
20413 /* Peek at the next token. */
20414 token = cp_lexer_peek_token (parser->lexer);
20415 /* If it's not `__attribute__', then we're done. */
20416 if (token->keyword != RID_ATTRIBUTE)
20417 break;
20419 /* Consume the `__attribute__' keyword. */
20420 cp_lexer_consume_token (parser->lexer);
20421 /* Look for the two `(' tokens. */
20422 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20423 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20425 /* Peek at the next token. */
20426 token = cp_lexer_peek_token (parser->lexer);
20427 if (token->type != CPP_CLOSE_PAREN)
20428 /* Parse the attribute-list. */
20429 attribute_list = cp_parser_gnu_attribute_list (parser);
20430 else
20431 /* If the next token is a `)', then there is no attribute
20432 list. */
20433 attribute_list = NULL;
20435 /* Look for the two `)' tokens. */
20436 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20437 ok = false;
20438 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20439 ok = false;
20440 if (!ok)
20441 cp_parser_skip_to_end_of_statement (parser);
20443 /* Add these new attributes to the list. */
20444 attributes = chainon (attributes, attribute_list);
20447 return attributes;
20450 /* Parse a GNU attribute-list.
20452 attribute-list:
20453 attribute
20454 attribute-list , attribute
20456 attribute:
20457 identifier
20458 identifier ( identifier )
20459 identifier ( identifier , expression-list )
20460 identifier ( expression-list )
20462 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20463 to an attribute. The TREE_PURPOSE of each node is the identifier
20464 indicating which attribute is in use. The TREE_VALUE represents
20465 the arguments, if any. */
20467 static tree
20468 cp_parser_gnu_attribute_list (cp_parser* parser)
20470 tree attribute_list = NULL_TREE;
20471 bool save_translate_strings_p = parser->translate_strings_p;
20473 parser->translate_strings_p = false;
20474 while (true)
20476 cp_token *token;
20477 tree identifier;
20478 tree attribute;
20480 /* Look for the identifier. We also allow keywords here; for
20481 example `__attribute__ ((const))' is legal. */
20482 token = cp_lexer_peek_token (parser->lexer);
20483 if (token->type == CPP_NAME
20484 || token->type == CPP_KEYWORD)
20486 tree arguments = NULL_TREE;
20488 /* Consume the token. */
20489 token = cp_lexer_consume_token (parser->lexer);
20491 /* Save away the identifier that indicates which attribute
20492 this is. */
20493 identifier = (token->type == CPP_KEYWORD)
20494 /* For keywords, use the canonical spelling, not the
20495 parsed identifier. */
20496 ? ridpointers[(int) token->keyword]
20497 : token->u.value;
20499 attribute = build_tree_list (identifier, NULL_TREE);
20501 /* Peek at the next token. */
20502 token = cp_lexer_peek_token (parser->lexer);
20503 /* If it's an `(', then parse the attribute arguments. */
20504 if (token->type == CPP_OPEN_PAREN)
20506 VEC(tree,gc) *vec;
20507 int attr_flag = (attribute_takes_identifier_p (identifier)
20508 ? id_attr : normal_attr);
20509 vec = cp_parser_parenthesized_expression_list
20510 (parser, attr_flag, /*cast_p=*/false,
20511 /*allow_expansion_p=*/false,
20512 /*non_constant_p=*/NULL);
20513 if (vec == NULL)
20514 arguments = error_mark_node;
20515 else
20517 arguments = build_tree_list_vec (vec);
20518 release_tree_vector (vec);
20520 /* Save the arguments away. */
20521 TREE_VALUE (attribute) = arguments;
20524 if (arguments != error_mark_node)
20526 /* Add this attribute to the list. */
20527 TREE_CHAIN (attribute) = attribute_list;
20528 attribute_list = attribute;
20531 token = cp_lexer_peek_token (parser->lexer);
20533 /* Now, look for more attributes. If the next token isn't a
20534 `,', we're done. */
20535 if (token->type != CPP_COMMA)
20536 break;
20538 /* Consume the comma and keep going. */
20539 cp_lexer_consume_token (parser->lexer);
20541 parser->translate_strings_p = save_translate_strings_p;
20543 /* We built up the list in reverse order. */
20544 return nreverse (attribute_list);
20547 /* Parse a standard C++11 attribute.
20549 The returned representation is a TREE_LIST which TREE_PURPOSE is
20550 the scoped name of the attribute, and the TREE_VALUE is its
20551 arguments list.
20553 Note that the scoped name of the attribute is itself a TREE_LIST
20554 which TREE_PURPOSE is the namespace of the attribute, and
20555 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
20556 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
20557 and which TREE_PURPOSE is directly the attribute name.
20559 Clients of the attribute code should use get_attribute_namespace
20560 and get_attribute_name to get the actual namespace and name of
20561 attributes, regardless of their being GNU or C++11 attributes.
20563 attribute:
20564 attribute-token attribute-argument-clause [opt]
20566 attribute-token:
20567 identifier
20568 attribute-scoped-token
20570 attribute-scoped-token:
20571 attribute-namespace :: identifier
20573 attribute-namespace:
20574 identifier
20576 attribute-argument-clause:
20577 ( balanced-token-seq )
20579 balanced-token-seq:
20580 balanced-token [opt]
20581 balanced-token-seq balanced-token
20583 balanced-token:
20584 ( balanced-token-seq )
20585 [ balanced-token-seq ]
20586 { balanced-token-seq }. */
20588 static tree
20589 cp_parser_std_attribute (cp_parser *parser)
20591 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
20592 cp_token *token;
20594 /* First, parse name of the the attribute, a.k.a
20595 attribute-token. */
20597 token = cp_lexer_peek_token (parser->lexer);
20598 if (token->type == CPP_NAME)
20599 attr_id = token->u.value;
20600 else if (token->type == CPP_KEYWORD)
20601 attr_id = ridpointers[(int) token->keyword];
20602 else if (token->flags & NAMED_OP)
20603 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
20605 if (attr_id == NULL_TREE)
20606 return NULL_TREE;
20608 cp_lexer_consume_token (parser->lexer);
20610 token = cp_lexer_peek_token (parser->lexer);
20611 if (token->type == CPP_SCOPE)
20613 /* We are seeing a scoped attribute token. */
20615 cp_lexer_consume_token (parser->lexer);
20616 attr_ns = attr_id;
20618 token = cp_lexer_consume_token (parser->lexer);
20619 if (token->type == CPP_NAME)
20620 attr_id = token->u.value;
20621 else if (token->type == CPP_KEYWORD)
20622 attr_id = ridpointers[(int) token->keyword];
20623 else
20625 error_at (token->location,
20626 "expected an identifier for the attribute name");
20627 return error_mark_node;
20629 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
20630 NULL_TREE);
20631 token = cp_lexer_peek_token (parser->lexer);
20633 else
20634 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
20635 NULL_TREE);
20637 /* Now parse the optional argument clause of the attribute. */
20639 if (token->type != CPP_OPEN_PAREN)
20640 return attribute;
20643 VEC(tree, gc) *vec;
20644 int attr_flag = normal_attr;
20646 if (attr_ns == get_identifier ("gnu")
20647 && attribute_takes_identifier_p (attr_id))
20648 /* A GNU attribute that takes an identifier in parameter. */
20649 attr_flag = id_attr;
20651 vec = cp_parser_parenthesized_expression_list
20652 (parser, attr_flag, /*cast_p=*/false,
20653 /*allow_expansion_p=*/true,
20654 /*non_constant_p=*/NULL);
20655 if (vec == NULL)
20656 arguments = error_mark_node;
20657 else
20659 arguments = build_tree_list_vec (vec);
20660 release_tree_vector (vec);
20663 if (arguments == error_mark_node)
20664 attribute = error_mark_node;
20665 else
20666 TREE_VALUE (attribute) = arguments;
20669 return attribute;
20672 /* Parse a list of standard C++-11 attributes.
20674 attribute-list:
20675 attribute [opt]
20676 attribute-list , attribute[opt]
20677 attribute ...
20678 attribute-list , attribute ...
20681 static tree
20682 cp_parser_std_attribute_list (cp_parser *parser)
20684 tree attributes = NULL_TREE, attribute = NULL_TREE;
20685 cp_token *token = NULL;
20687 while (true)
20689 attribute = cp_parser_std_attribute (parser);
20690 if (attribute == error_mark_node)
20691 break;
20692 if (attribute != NULL_TREE)
20694 TREE_CHAIN (attribute) = attributes;
20695 attributes = attribute;
20697 token = cp_lexer_peek_token (parser->lexer);
20698 if (token->type != CPP_COMMA)
20699 break;
20700 cp_lexer_consume_token (parser->lexer);
20702 attributes = nreverse (attributes);
20703 return attributes;
20706 /* Parse a standard C++-11 attribute specifier.
20708 attribute-specifier:
20709 [ [ attribute-list ] ]
20710 alignment-specifier
20712 alignment-specifier:
20713 alignas ( type-id ... [opt] )
20714 alignas ( alignment-expression ... [opt] ). */
20716 static tree
20717 cp_parser_std_attribute_spec (cp_parser *parser)
20719 tree attributes = NULL_TREE;
20720 cp_token *token = cp_lexer_peek_token (parser->lexer);
20722 if (token->type == CPP_OPEN_SQUARE
20723 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
20725 cp_lexer_consume_token (parser->lexer);
20726 cp_lexer_consume_token (parser->lexer);
20728 attributes = cp_parser_std_attribute_list (parser);
20730 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
20731 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20732 cp_parser_skip_to_end_of_statement (parser);
20733 else
20734 /* Warn about parsing c++11 attribute in non-c++1 mode, only
20735 when we are sure that we have actually parsed them. */
20736 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
20738 else
20740 tree alignas_expr;
20742 /* Look for an alignment-specifier. */
20744 token = cp_lexer_peek_token (parser->lexer);
20746 if (token->type != CPP_KEYWORD
20747 || token->keyword != RID_ALIGNAS)
20748 return NULL_TREE;
20750 cp_lexer_consume_token (parser->lexer);
20751 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
20753 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
20755 cp_parser_error (parser, "expected %<(%>");
20756 return error_mark_node;
20759 cp_parser_parse_tentatively (parser);
20760 alignas_expr = cp_parser_type_id (parser);
20762 if (!cp_parser_parse_definitely (parser))
20764 gcc_assert (alignas_expr == error_mark_node
20765 || alignas_expr == NULL_TREE);
20767 alignas_expr =
20768 cp_parser_assignment_expression (parser, /*cast_p=*/false,
20769 /**cp_id_kind=*/NULL);
20770 if (alignas_expr == NULL_TREE
20771 || alignas_expr == error_mark_node)
20772 return alignas_expr;
20775 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
20777 cp_parser_error (parser, "expected %<)%>");
20778 return error_mark_node;
20781 alignas_expr = cxx_alignas_expr (alignas_expr);
20783 /* Build the C++-11 representation of an 'aligned'
20784 attribute. */
20785 attributes =
20786 build_tree_list (build_tree_list (get_identifier ("gnu"),
20787 get_identifier ("aligned")),
20788 build_tree_list (NULL_TREE, alignas_expr));
20791 return attributes;
20794 /* Parse a standard C++-11 attribute-specifier-seq.
20796 attribute-specifier-seq:
20797 attribute-specifier-seq [opt] attribute-specifier
20800 static tree
20801 cp_parser_std_attribute_spec_seq (cp_parser *parser)
20803 tree attr_specs = NULL;
20805 while (true)
20807 tree attr_spec = cp_parser_std_attribute_spec (parser);
20808 if (attr_spec == NULL_TREE)
20809 break;
20810 if (attr_spec == error_mark_node)
20811 return error_mark_node;
20813 TREE_CHAIN (attr_spec) = attr_specs;
20814 attr_specs = attr_spec;
20817 attr_specs = nreverse (attr_specs);
20818 return attr_specs;
20821 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20822 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20823 current value of the PEDANTIC flag, regardless of whether or not
20824 the `__extension__' keyword is present. The caller is responsible
20825 for restoring the value of the PEDANTIC flag. */
20827 static bool
20828 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20830 /* Save the old value of the PEDANTIC flag. */
20831 *saved_pedantic = pedantic;
20833 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20835 /* Consume the `__extension__' token. */
20836 cp_lexer_consume_token (parser->lexer);
20837 /* We're not being pedantic while the `__extension__' keyword is
20838 in effect. */
20839 pedantic = 0;
20841 return true;
20844 return false;
20847 /* Parse a label declaration.
20849 label-declaration:
20850 __label__ label-declarator-seq ;
20852 label-declarator-seq:
20853 identifier , label-declarator-seq
20854 identifier */
20856 static void
20857 cp_parser_label_declaration (cp_parser* parser)
20859 /* Look for the `__label__' keyword. */
20860 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20862 while (true)
20864 tree identifier;
20866 /* Look for an identifier. */
20867 identifier = cp_parser_identifier (parser);
20868 /* If we failed, stop. */
20869 if (identifier == error_mark_node)
20870 break;
20871 /* Declare it as a label. */
20872 finish_label_decl (identifier);
20873 /* If the next token is a `;', stop. */
20874 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20875 break;
20876 /* Look for the `,' separating the label declarations. */
20877 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20880 /* Look for the final `;'. */
20881 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20884 /* Support Functions */
20886 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20887 NAME should have one of the representations used for an
20888 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20889 is returned. If PARSER->SCOPE is a dependent type, then a
20890 SCOPE_REF is returned.
20892 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20893 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20894 was formed. Abstractly, such entities should not be passed to this
20895 function, because they do not need to be looked up, but it is
20896 simpler to check for this special case here, rather than at the
20897 call-sites.
20899 In cases not explicitly covered above, this function returns a
20900 DECL, OVERLOAD, or baselink representing the result of the lookup.
20901 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20902 is returned.
20904 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20905 (e.g., "struct") that was used. In that case bindings that do not
20906 refer to types are ignored.
20908 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20909 ignored.
20911 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20912 are ignored.
20914 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20915 types.
20917 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20918 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20919 NULL_TREE otherwise. */
20921 static tree
20922 cp_parser_lookup_name (cp_parser *parser, tree name,
20923 enum tag_types tag_type,
20924 bool is_template,
20925 bool is_namespace,
20926 bool check_dependency,
20927 tree *ambiguous_decls,
20928 location_t name_location)
20930 tree decl;
20931 tree object_type = parser->context->object_type;
20933 /* Assume that the lookup will be unambiguous. */
20934 if (ambiguous_decls)
20935 *ambiguous_decls = NULL_TREE;
20937 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20938 no longer valid. Note that if we are parsing tentatively, and
20939 the parse fails, OBJECT_TYPE will be automatically restored. */
20940 parser->context->object_type = NULL_TREE;
20942 if (name == error_mark_node)
20943 return error_mark_node;
20945 /* A template-id has already been resolved; there is no lookup to
20946 do. */
20947 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20948 return name;
20949 if (BASELINK_P (name))
20951 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20952 == TEMPLATE_ID_EXPR);
20953 return name;
20956 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20957 it should already have been checked to make sure that the name
20958 used matches the type being destroyed. */
20959 if (TREE_CODE (name) == BIT_NOT_EXPR)
20961 tree type;
20963 /* Figure out to which type this destructor applies. */
20964 if (parser->scope)
20965 type = parser->scope;
20966 else if (object_type)
20967 type = object_type;
20968 else
20969 type = current_class_type;
20970 /* If that's not a class type, there is no destructor. */
20971 if (!type || !CLASS_TYPE_P (type))
20972 return error_mark_node;
20973 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20974 lazily_declare_fn (sfk_destructor, type);
20975 if (!CLASSTYPE_DESTRUCTORS (type))
20976 return error_mark_node;
20977 /* If it was a class type, return the destructor. */
20978 return CLASSTYPE_DESTRUCTORS (type);
20981 /* By this point, the NAME should be an ordinary identifier. If
20982 the id-expression was a qualified name, the qualifying scope is
20983 stored in PARSER->SCOPE at this point. */
20984 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20986 /* Perform the lookup. */
20987 if (parser->scope)
20989 bool dependent_p;
20991 if (parser->scope == error_mark_node)
20992 return error_mark_node;
20994 /* If the SCOPE is dependent, the lookup must be deferred until
20995 the template is instantiated -- unless we are explicitly
20996 looking up names in uninstantiated templates. Even then, we
20997 cannot look up the name if the scope is not a class type; it
20998 might, for example, be a template type parameter. */
20999 dependent_p = (TYPE_P (parser->scope)
21000 && dependent_scope_p (parser->scope));
21001 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
21002 && dependent_p)
21003 /* Defer lookup. */
21004 decl = error_mark_node;
21005 else
21007 tree pushed_scope = NULL_TREE;
21009 /* If PARSER->SCOPE is a dependent type, then it must be a
21010 class type, and we must not be checking dependencies;
21011 otherwise, we would have processed this lookup above. So
21012 that PARSER->SCOPE is not considered a dependent base by
21013 lookup_member, we must enter the scope here. */
21014 if (dependent_p)
21015 pushed_scope = push_scope (parser->scope);
21017 /* If the PARSER->SCOPE is a template specialization, it
21018 may be instantiated during name lookup. In that case,
21019 errors may be issued. Even if we rollback the current
21020 tentative parse, those errors are valid. */
21021 decl = lookup_qualified_name (parser->scope, name,
21022 tag_type != none_type,
21023 /*complain=*/true);
21025 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21026 lookup result and the nested-name-specifier nominates a class C:
21027 * if the name specified after the nested-name-specifier, when
21028 looked up in C, is the injected-class-name of C (Clause 9), or
21029 * if the name specified after the nested-name-specifier is the
21030 same as the identifier or the simple-template-id's template-
21031 name in the last component of the nested-name-specifier,
21032 the name is instead considered to name the constructor of
21033 class C. [ Note: for example, the constructor is not an
21034 acceptable lookup result in an elaborated-type-specifier so
21035 the constructor would not be used in place of the
21036 injected-class-name. --end note ] Such a constructor name
21037 shall be used only in the declarator-id of a declaration that
21038 names a constructor or in a using-declaration. */
21039 if (tag_type == none_type
21040 && DECL_SELF_REFERENCE_P (decl)
21041 && same_type_p (DECL_CONTEXT (decl), parser->scope))
21042 decl = lookup_qualified_name (parser->scope, ctor_identifier,
21043 tag_type != none_type,
21044 /*complain=*/true);
21046 /* If we have a single function from a using decl, pull it out. */
21047 if (TREE_CODE (decl) == OVERLOAD
21048 && !really_overloaded_fn (decl))
21049 decl = OVL_FUNCTION (decl);
21051 if (pushed_scope)
21052 pop_scope (pushed_scope);
21055 /* If the scope is a dependent type and either we deferred lookup or
21056 we did lookup but didn't find the name, rememeber the name. */
21057 if (decl == error_mark_node && TYPE_P (parser->scope)
21058 && dependent_type_p (parser->scope))
21060 if (tag_type)
21062 tree type;
21064 /* The resolution to Core Issue 180 says that `struct
21065 A::B' should be considered a type-name, even if `A'
21066 is dependent. */
21067 type = make_typename_type (parser->scope, name, tag_type,
21068 /*complain=*/tf_error);
21069 decl = TYPE_NAME (type);
21071 else if (is_template
21072 && (cp_parser_next_token_ends_template_argument_p (parser)
21073 || cp_lexer_next_token_is (parser->lexer,
21074 CPP_CLOSE_PAREN)))
21075 decl = make_unbound_class_template (parser->scope,
21076 name, NULL_TREE,
21077 /*complain=*/tf_error);
21078 else
21079 decl = build_qualified_name (/*type=*/NULL_TREE,
21080 parser->scope, name,
21081 is_template);
21083 parser->qualifying_scope = parser->scope;
21084 parser->object_scope = NULL_TREE;
21086 else if (object_type)
21088 tree object_decl = NULL_TREE;
21089 /* Look up the name in the scope of the OBJECT_TYPE, unless the
21090 OBJECT_TYPE is not a class. */
21091 if (CLASS_TYPE_P (object_type))
21092 /* If the OBJECT_TYPE is a template specialization, it may
21093 be instantiated during name lookup. In that case, errors
21094 may be issued. Even if we rollback the current tentative
21095 parse, those errors are valid. */
21096 object_decl = lookup_member (object_type,
21097 name,
21098 /*protect=*/0,
21099 tag_type != none_type,
21100 tf_warning_or_error);
21101 /* Look it up in the enclosing context, too. */
21102 decl = lookup_name_real (name, tag_type != none_type,
21103 /*nonclass=*/0,
21104 /*block_p=*/true, is_namespace, 0);
21105 parser->object_scope = object_type;
21106 parser->qualifying_scope = NULL_TREE;
21107 if (object_decl)
21108 decl = object_decl;
21110 else
21112 decl = lookup_name_real (name, tag_type != none_type,
21113 /*nonclass=*/0,
21114 /*block_p=*/true, is_namespace, 0);
21115 parser->qualifying_scope = NULL_TREE;
21116 parser->object_scope = NULL_TREE;
21119 /* If the lookup failed, let our caller know. */
21120 if (!decl || decl == error_mark_node)
21121 return error_mark_node;
21123 /* Pull out the template from an injected-class-name (or multiple). */
21124 if (is_template)
21125 decl = maybe_get_template_decl_from_type_decl (decl);
21127 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
21128 if (TREE_CODE (decl) == TREE_LIST)
21130 if (ambiguous_decls)
21131 *ambiguous_decls = decl;
21132 /* The error message we have to print is too complicated for
21133 cp_parser_error, so we incorporate its actions directly. */
21134 if (!cp_parser_simulate_error (parser))
21136 error_at (name_location, "reference to %qD is ambiguous",
21137 name);
21138 print_candidates (decl);
21140 return error_mark_node;
21143 gcc_assert (DECL_P (decl)
21144 || TREE_CODE (decl) == OVERLOAD
21145 || TREE_CODE (decl) == SCOPE_REF
21146 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
21147 || BASELINK_P (decl));
21149 /* If we have resolved the name of a member declaration, check to
21150 see if the declaration is accessible. When the name resolves to
21151 set of overloaded functions, accessibility is checked when
21152 overload resolution is done.
21154 During an explicit instantiation, access is not checked at all,
21155 as per [temp.explicit]. */
21156 if (DECL_P (decl))
21157 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
21159 maybe_record_typedef_use (decl);
21161 return decl;
21164 /* Like cp_parser_lookup_name, but for use in the typical case where
21165 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
21166 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
21168 static tree
21169 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
21171 return cp_parser_lookup_name (parser, name,
21172 none_type,
21173 /*is_template=*/false,
21174 /*is_namespace=*/false,
21175 /*check_dependency=*/true,
21176 /*ambiguous_decls=*/NULL,
21177 location);
21180 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
21181 the current context, return the TYPE_DECL. If TAG_NAME_P is
21182 true, the DECL indicates the class being defined in a class-head,
21183 or declared in an elaborated-type-specifier.
21185 Otherwise, return DECL. */
21187 static tree
21188 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
21190 /* If the TEMPLATE_DECL is being declared as part of a class-head,
21191 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
21193 struct A {
21194 template <typename T> struct B;
21197 template <typename T> struct A::B {};
21199 Similarly, in an elaborated-type-specifier:
21201 namespace N { struct X{}; }
21203 struct A {
21204 template <typename T> friend struct N::X;
21207 However, if the DECL refers to a class type, and we are in
21208 the scope of the class, then the name lookup automatically
21209 finds the TYPE_DECL created by build_self_reference rather
21210 than a TEMPLATE_DECL. For example, in:
21212 template <class T> struct S {
21213 S s;
21216 there is no need to handle such case. */
21218 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
21219 return DECL_TEMPLATE_RESULT (decl);
21221 return decl;
21224 /* If too many, or too few, template-parameter lists apply to the
21225 declarator, issue an error message. Returns TRUE if all went well,
21226 and FALSE otherwise. */
21228 static bool
21229 cp_parser_check_declarator_template_parameters (cp_parser* parser,
21230 cp_declarator *declarator,
21231 location_t declarator_location)
21233 switch (declarator->kind)
21235 case cdk_id:
21237 unsigned num_templates = 0;
21238 tree scope = declarator->u.id.qualifying_scope;
21240 if (scope)
21241 num_templates = num_template_headers_for_class (scope);
21242 else if (TREE_CODE (declarator->u.id.unqualified_name)
21243 == TEMPLATE_ID_EXPR)
21244 /* If the DECLARATOR has the form `X<y>' then it uses one
21245 additional level of template parameters. */
21246 ++num_templates;
21248 return cp_parser_check_template_parameters
21249 (parser, num_templates, declarator_location, declarator);
21252 case cdk_function:
21253 case cdk_array:
21254 case cdk_pointer:
21255 case cdk_reference:
21256 case cdk_ptrmem:
21257 return (cp_parser_check_declarator_template_parameters
21258 (parser, declarator->declarator, declarator_location));
21260 case cdk_error:
21261 return true;
21263 default:
21264 gcc_unreachable ();
21266 return false;
21269 /* NUM_TEMPLATES were used in the current declaration. If that is
21270 invalid, return FALSE and issue an error messages. Otherwise,
21271 return TRUE. If DECLARATOR is non-NULL, then we are checking a
21272 declarator and we can print more accurate diagnostics. */
21274 static bool
21275 cp_parser_check_template_parameters (cp_parser* parser,
21276 unsigned num_templates,
21277 location_t location,
21278 cp_declarator *declarator)
21280 /* If there are the same number of template classes and parameter
21281 lists, that's OK. */
21282 if (parser->num_template_parameter_lists == num_templates)
21283 return true;
21284 /* If there are more, but only one more, then we are referring to a
21285 member template. That's OK too. */
21286 if (parser->num_template_parameter_lists == num_templates + 1)
21287 return true;
21288 /* If there are more template classes than parameter lists, we have
21289 something like:
21291 template <class T> void S<T>::R<T>::f (); */
21292 if (parser->num_template_parameter_lists < num_templates)
21294 if (declarator && !current_function_decl)
21295 error_at (location, "specializing member %<%T::%E%> "
21296 "requires %<template<>%> syntax",
21297 declarator->u.id.qualifying_scope,
21298 declarator->u.id.unqualified_name);
21299 else if (declarator)
21300 error_at (location, "invalid declaration of %<%T::%E%>",
21301 declarator->u.id.qualifying_scope,
21302 declarator->u.id.unqualified_name);
21303 else
21304 error_at (location, "too few template-parameter-lists");
21305 return false;
21307 /* Otherwise, there are too many template parameter lists. We have
21308 something like:
21310 template <class T> template <class U> void S::f(); */
21311 error_at (location, "too many template-parameter-lists");
21312 return false;
21315 /* Parse an optional `::' token indicating that the following name is
21316 from the global namespace. If so, PARSER->SCOPE is set to the
21317 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
21318 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
21319 Returns the new value of PARSER->SCOPE, if the `::' token is
21320 present, and NULL_TREE otherwise. */
21322 static tree
21323 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
21325 cp_token *token;
21327 /* Peek at the next token. */
21328 token = cp_lexer_peek_token (parser->lexer);
21329 /* If we're looking at a `::' token then we're starting from the
21330 global namespace, not our current location. */
21331 if (token->type == CPP_SCOPE)
21333 /* Consume the `::' token. */
21334 cp_lexer_consume_token (parser->lexer);
21335 /* Set the SCOPE so that we know where to start the lookup. */
21336 parser->scope = global_namespace;
21337 parser->qualifying_scope = global_namespace;
21338 parser->object_scope = NULL_TREE;
21340 return parser->scope;
21342 else if (!current_scope_valid_p)
21344 parser->scope = NULL_TREE;
21345 parser->qualifying_scope = NULL_TREE;
21346 parser->object_scope = NULL_TREE;
21349 return NULL_TREE;
21352 /* Returns TRUE if the upcoming token sequence is the start of a
21353 constructor declarator. If FRIEND_P is true, the declarator is
21354 preceded by the `friend' specifier. */
21356 static bool
21357 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
21359 bool constructor_p;
21360 tree nested_name_specifier;
21361 cp_token *next_token;
21363 /* The common case is that this is not a constructor declarator, so
21364 try to avoid doing lots of work if at all possible. It's not
21365 valid declare a constructor at function scope. */
21366 if (parser->in_function_body)
21367 return false;
21368 /* And only certain tokens can begin a constructor declarator. */
21369 next_token = cp_lexer_peek_token (parser->lexer);
21370 if (next_token->type != CPP_NAME
21371 && next_token->type != CPP_SCOPE
21372 && next_token->type != CPP_NESTED_NAME_SPECIFIER
21373 && next_token->type != CPP_TEMPLATE_ID)
21374 return false;
21376 /* Parse tentatively; we are going to roll back all of the tokens
21377 consumed here. */
21378 cp_parser_parse_tentatively (parser);
21379 /* Assume that we are looking at a constructor declarator. */
21380 constructor_p = true;
21382 /* Look for the optional `::' operator. */
21383 cp_parser_global_scope_opt (parser,
21384 /*current_scope_valid_p=*/false);
21385 /* Look for the nested-name-specifier. */
21386 nested_name_specifier
21387 = (cp_parser_nested_name_specifier_opt (parser,
21388 /*typename_keyword_p=*/false,
21389 /*check_dependency_p=*/false,
21390 /*type_p=*/false,
21391 /*is_declaration=*/false));
21392 /* Outside of a class-specifier, there must be a
21393 nested-name-specifier. */
21394 if (!nested_name_specifier &&
21395 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
21396 || friend_p))
21397 constructor_p = false;
21398 else if (nested_name_specifier == error_mark_node)
21399 constructor_p = false;
21401 /* If we have a class scope, this is easy; DR 147 says that S::S always
21402 names the constructor, and no other qualified name could. */
21403 if (constructor_p && nested_name_specifier
21404 && CLASS_TYPE_P (nested_name_specifier))
21406 tree id = cp_parser_unqualified_id (parser,
21407 /*template_keyword_p=*/false,
21408 /*check_dependency_p=*/false,
21409 /*declarator_p=*/true,
21410 /*optional_p=*/false);
21411 if (is_overloaded_fn (id))
21412 id = DECL_NAME (get_first_fn (id));
21413 if (!constructor_name_p (id, nested_name_specifier))
21414 constructor_p = false;
21416 /* If we still think that this might be a constructor-declarator,
21417 look for a class-name. */
21418 else if (constructor_p)
21420 /* If we have:
21422 template <typename T> struct S {
21423 S();
21426 we must recognize that the nested `S' names a class. */
21427 tree type_decl;
21428 type_decl = cp_parser_class_name (parser,
21429 /*typename_keyword_p=*/false,
21430 /*template_keyword_p=*/false,
21431 none_type,
21432 /*check_dependency_p=*/false,
21433 /*class_head_p=*/false,
21434 /*is_declaration=*/false);
21435 /* If there was no class-name, then this is not a constructor. */
21436 constructor_p = !cp_parser_error_occurred (parser);
21438 /* If we're still considering a constructor, we have to see a `(',
21439 to begin the parameter-declaration-clause, followed by either a
21440 `)', an `...', or a decl-specifier. We need to check for a
21441 type-specifier to avoid being fooled into thinking that:
21443 S (f) (int);
21445 is a constructor. (It is actually a function named `f' that
21446 takes one parameter (of type `int') and returns a value of type
21447 `S'. */
21448 if (constructor_p
21449 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21450 constructor_p = false;
21452 if (constructor_p
21453 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
21454 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
21455 /* A parameter declaration begins with a decl-specifier,
21456 which is either the "attribute" keyword, a storage class
21457 specifier, or (usually) a type-specifier. */
21458 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
21460 tree type;
21461 tree pushed_scope = NULL_TREE;
21462 unsigned saved_num_template_parameter_lists;
21464 /* Names appearing in the type-specifier should be looked up
21465 in the scope of the class. */
21466 if (current_class_type)
21467 type = NULL_TREE;
21468 else
21470 type = TREE_TYPE (type_decl);
21471 if (TREE_CODE (type) == TYPENAME_TYPE)
21473 type = resolve_typename_type (type,
21474 /*only_current_p=*/false);
21475 if (TREE_CODE (type) == TYPENAME_TYPE)
21477 cp_parser_abort_tentative_parse (parser);
21478 return false;
21481 pushed_scope = push_scope (type);
21484 /* Inside the constructor parameter list, surrounding
21485 template-parameter-lists do not apply. */
21486 saved_num_template_parameter_lists
21487 = parser->num_template_parameter_lists;
21488 parser->num_template_parameter_lists = 0;
21490 /* Look for the type-specifier. */
21491 cp_parser_type_specifier (parser,
21492 CP_PARSER_FLAGS_NONE,
21493 /*decl_specs=*/NULL,
21494 /*is_declarator=*/true,
21495 /*declares_class_or_enum=*/NULL,
21496 /*is_cv_qualifier=*/NULL);
21498 parser->num_template_parameter_lists
21499 = saved_num_template_parameter_lists;
21501 /* Leave the scope of the class. */
21502 if (pushed_scope)
21503 pop_scope (pushed_scope);
21505 constructor_p = !cp_parser_error_occurred (parser);
21509 /* We did not really want to consume any tokens. */
21510 cp_parser_abort_tentative_parse (parser);
21512 return constructor_p;
21515 /* Parse the definition of the function given by the DECL_SPECIFIERS,
21516 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
21517 they must be performed once we are in the scope of the function.
21519 Returns the function defined. */
21521 static tree
21522 cp_parser_function_definition_from_specifiers_and_declarator
21523 (cp_parser* parser,
21524 cp_decl_specifier_seq *decl_specifiers,
21525 tree attributes,
21526 const cp_declarator *declarator)
21528 tree fn;
21529 bool success_p;
21531 /* Begin the function-definition. */
21532 success_p = start_function (decl_specifiers, declarator, attributes);
21534 /* The things we're about to see are not directly qualified by any
21535 template headers we've seen thus far. */
21536 reset_specialization ();
21538 /* If there were names looked up in the decl-specifier-seq that we
21539 did not check, check them now. We must wait until we are in the
21540 scope of the function to perform the checks, since the function
21541 might be a friend. */
21542 perform_deferred_access_checks (tf_warning_or_error);
21544 if (!success_p)
21546 /* Skip the entire function. */
21547 cp_parser_skip_to_end_of_block_or_statement (parser);
21548 fn = error_mark_node;
21550 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
21552 /* Seen already, skip it. An error message has already been output. */
21553 cp_parser_skip_to_end_of_block_or_statement (parser);
21554 fn = current_function_decl;
21555 current_function_decl = NULL_TREE;
21556 /* If this is a function from a class, pop the nested class. */
21557 if (current_class_name)
21558 pop_nested_class ();
21560 else
21562 timevar_id_t tv;
21563 if (DECL_DECLARED_INLINE_P (current_function_decl))
21564 tv = TV_PARSE_INLINE;
21565 else
21566 tv = TV_PARSE_FUNC;
21567 timevar_push (tv);
21568 fn = cp_parser_function_definition_after_declarator (parser,
21569 /*inline_p=*/false);
21570 timevar_pop (tv);
21573 return fn;
21576 /* Parse the part of a function-definition that follows the
21577 declarator. INLINE_P is TRUE iff this function is an inline
21578 function defined within a class-specifier.
21580 Returns the function defined. */
21582 static tree
21583 cp_parser_function_definition_after_declarator (cp_parser* parser,
21584 bool inline_p)
21586 tree fn;
21587 bool ctor_initializer_p = false;
21588 bool saved_in_unbraced_linkage_specification_p;
21589 bool saved_in_function_body;
21590 unsigned saved_num_template_parameter_lists;
21591 cp_token *token;
21593 saved_in_function_body = parser->in_function_body;
21594 parser->in_function_body = true;
21595 /* If the next token is `return', then the code may be trying to
21596 make use of the "named return value" extension that G++ used to
21597 support. */
21598 token = cp_lexer_peek_token (parser->lexer);
21599 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21601 /* Consume the `return' keyword. */
21602 cp_lexer_consume_token (parser->lexer);
21603 /* Look for the identifier that indicates what value is to be
21604 returned. */
21605 cp_parser_identifier (parser);
21606 /* Issue an error message. */
21607 error_at (token->location,
21608 "named return values are no longer supported");
21609 /* Skip tokens until we reach the start of the function body. */
21610 while (true)
21612 cp_token *token = cp_lexer_peek_token (parser->lexer);
21613 if (token->type == CPP_OPEN_BRACE
21614 || token->type == CPP_EOF
21615 || token->type == CPP_PRAGMA_EOL)
21616 break;
21617 cp_lexer_consume_token (parser->lexer);
21620 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21621 anything declared inside `f'. */
21622 saved_in_unbraced_linkage_specification_p
21623 = parser->in_unbraced_linkage_specification_p;
21624 parser->in_unbraced_linkage_specification_p = false;
21625 /* Inside the function, surrounding template-parameter-lists do not
21626 apply. */
21627 saved_num_template_parameter_lists
21628 = parser->num_template_parameter_lists;
21629 parser->num_template_parameter_lists = 0;
21631 start_lambda_scope (current_function_decl);
21633 /* If the next token is `try', `__transaction_atomic', or
21634 `__transaction_relaxed`, then we are looking at either function-try-block
21635 or function-transaction-block. Note that all of these include the
21636 function-body. */
21637 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21638 ctor_initializer_p = cp_parser_function_transaction (parser,
21639 RID_TRANSACTION_ATOMIC);
21640 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21641 RID_TRANSACTION_RELAXED))
21642 ctor_initializer_p = cp_parser_function_transaction (parser,
21643 RID_TRANSACTION_RELAXED);
21644 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21645 ctor_initializer_p = cp_parser_function_try_block (parser);
21646 else
21647 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21648 (parser, /*in_function_try_block=*/false);
21650 finish_lambda_scope ();
21652 /* Finish the function. */
21653 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21654 (inline_p ? 2 : 0));
21655 /* Generate code for it, if necessary. */
21656 expand_or_defer_fn (fn);
21657 /* Restore the saved values. */
21658 parser->in_unbraced_linkage_specification_p
21659 = saved_in_unbraced_linkage_specification_p;
21660 parser->num_template_parameter_lists
21661 = saved_num_template_parameter_lists;
21662 parser->in_function_body = saved_in_function_body;
21664 return fn;
21667 /* Parse a template-declaration, assuming that the `export' (and
21668 `extern') keywords, if present, has already been scanned. MEMBER_P
21669 is as for cp_parser_template_declaration. */
21671 static void
21672 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21674 tree decl = NULL_TREE;
21675 VEC (deferred_access_check,gc) *checks;
21676 tree parameter_list;
21677 bool friend_p = false;
21678 bool need_lang_pop;
21679 cp_token *token;
21681 /* Look for the `template' keyword. */
21682 token = cp_lexer_peek_token (parser->lexer);
21683 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21684 return;
21686 /* And the `<'. */
21687 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21688 return;
21689 if (at_class_scope_p () && current_function_decl)
21691 /* 14.5.2.2 [temp.mem]
21693 A local class shall not have member templates. */
21694 error_at (token->location,
21695 "invalid declaration of member template in local class");
21696 cp_parser_skip_to_end_of_block_or_statement (parser);
21697 return;
21699 /* [temp]
21701 A template ... shall not have C linkage. */
21702 if (current_lang_name == lang_name_c)
21704 error_at (token->location, "template with C linkage");
21705 /* Give it C++ linkage to avoid confusing other parts of the
21706 front end. */
21707 push_lang_context (lang_name_cplusplus);
21708 need_lang_pop = true;
21710 else
21711 need_lang_pop = false;
21713 /* We cannot perform access checks on the template parameter
21714 declarations until we know what is being declared, just as we
21715 cannot check the decl-specifier list. */
21716 push_deferring_access_checks (dk_deferred);
21718 /* If the next token is `>', then we have an invalid
21719 specialization. Rather than complain about an invalid template
21720 parameter, issue an error message here. */
21721 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21723 cp_parser_error (parser, "invalid explicit specialization");
21724 begin_specialization ();
21725 parameter_list = NULL_TREE;
21727 else
21729 /* Parse the template parameters. */
21730 parameter_list = cp_parser_template_parameter_list (parser);
21733 /* Get the deferred access checks from the parameter list. These
21734 will be checked once we know what is being declared, as for a
21735 member template the checks must be performed in the scope of the
21736 class containing the member. */
21737 checks = get_deferred_access_checks ();
21739 /* Look for the `>'. */
21740 cp_parser_skip_to_end_of_template_parameter_list (parser);
21741 /* We just processed one more parameter list. */
21742 ++parser->num_template_parameter_lists;
21743 /* If the next token is `template', there are more template
21744 parameters. */
21745 if (cp_lexer_next_token_is_keyword (parser->lexer,
21746 RID_TEMPLATE))
21747 cp_parser_template_declaration_after_export (parser, member_p);
21748 else if (cxx_dialect >= cxx0x
21749 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21750 decl = cp_parser_alias_declaration (parser);
21751 else
21753 /* There are no access checks when parsing a template, as we do not
21754 know if a specialization will be a friend. */
21755 push_deferring_access_checks (dk_no_check);
21756 token = cp_lexer_peek_token (parser->lexer);
21757 decl = cp_parser_single_declaration (parser,
21758 checks,
21759 member_p,
21760 /*explicit_specialization_p=*/false,
21761 &friend_p);
21762 pop_deferring_access_checks ();
21764 /* If this is a member template declaration, let the front
21765 end know. */
21766 if (member_p && !friend_p && decl)
21768 if (TREE_CODE (decl) == TYPE_DECL)
21769 cp_parser_check_access_in_redeclaration (decl, token->location);
21771 decl = finish_member_template_decl (decl);
21773 else if (friend_p && decl
21774 && (TREE_CODE (decl) == TYPE_DECL
21775 || DECL_TYPE_TEMPLATE_P (decl)))
21776 make_friend_class (current_class_type, TREE_TYPE (decl),
21777 /*complain=*/true);
21779 /* We are done with the current parameter list. */
21780 --parser->num_template_parameter_lists;
21782 pop_deferring_access_checks ();
21784 /* Finish up. */
21785 finish_template_decl (parameter_list);
21787 /* Check the template arguments for a literal operator template. */
21788 if (decl
21789 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21790 && UDLIT_OPER_P (DECL_NAME (decl)))
21792 bool ok = true;
21793 if (parameter_list == NULL_TREE)
21794 ok = false;
21795 else
21797 int num_parms = TREE_VEC_LENGTH (parameter_list);
21798 if (num_parms != 1)
21799 ok = false;
21800 else
21802 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21803 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21804 if (TREE_TYPE (parm) != char_type_node
21805 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21806 ok = false;
21809 if (!ok)
21810 error ("literal operator template %qD has invalid parameter list."
21811 " Expected non-type template argument pack <char...>",
21812 decl);
21814 /* Register member declarations. */
21815 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21816 finish_member_declaration (decl);
21817 /* For the erroneous case of a template with C linkage, we pushed an
21818 implicit C++ linkage scope; exit that scope now. */
21819 if (need_lang_pop)
21820 pop_lang_context ();
21821 /* If DECL is a function template, we must return to parse it later.
21822 (Even though there is no definition, there might be default
21823 arguments that need handling.) */
21824 if (member_p && decl
21825 && (TREE_CODE (decl) == FUNCTION_DECL
21826 || DECL_FUNCTION_TEMPLATE_P (decl)))
21827 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21830 /* Perform the deferred access checks from a template-parameter-list.
21831 CHECKS is a TREE_LIST of access checks, as returned by
21832 get_deferred_access_checks. */
21834 static void
21835 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21837 ++processing_template_parmlist;
21838 perform_access_checks (checks, tf_warning_or_error);
21839 --processing_template_parmlist;
21842 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21843 `function-definition' sequence that follows a template header.
21844 If MEMBER_P is true, this declaration appears in a class scope.
21846 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21847 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21849 static tree
21850 cp_parser_single_declaration (cp_parser* parser,
21851 VEC (deferred_access_check,gc)* checks,
21852 bool member_p,
21853 bool explicit_specialization_p,
21854 bool* friend_p)
21856 int declares_class_or_enum;
21857 tree decl = NULL_TREE;
21858 cp_decl_specifier_seq decl_specifiers;
21859 bool function_definition_p = false;
21860 cp_token *decl_spec_token_start;
21862 /* This function is only used when processing a template
21863 declaration. */
21864 gcc_assert (innermost_scope_kind () == sk_template_parms
21865 || innermost_scope_kind () == sk_template_spec);
21867 /* Defer access checks until we know what is being declared. */
21868 push_deferring_access_checks (dk_deferred);
21870 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21871 alternative. */
21872 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21873 cp_parser_decl_specifier_seq (parser,
21874 CP_PARSER_FLAGS_OPTIONAL,
21875 &decl_specifiers,
21876 &declares_class_or_enum);
21877 if (friend_p)
21878 *friend_p = cp_parser_friend_p (&decl_specifiers);
21880 /* There are no template typedefs. */
21881 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
21883 error_at (decl_spec_token_start->location,
21884 "template declaration of %<typedef%>");
21885 decl = error_mark_node;
21888 /* Gather up the access checks that occurred the
21889 decl-specifier-seq. */
21890 stop_deferring_access_checks ();
21892 /* Check for the declaration of a template class. */
21893 if (declares_class_or_enum)
21895 if (cp_parser_declares_only_class_p (parser))
21897 decl = shadow_tag (&decl_specifiers);
21899 /* In this case:
21901 struct C {
21902 friend template <typename T> struct A<T>::B;
21905 A<T>::B will be represented by a TYPENAME_TYPE, and
21906 therefore not recognized by shadow_tag. */
21907 if (friend_p && *friend_p
21908 && !decl
21909 && decl_specifiers.type
21910 && TYPE_P (decl_specifiers.type))
21911 decl = decl_specifiers.type;
21913 if (decl && decl != error_mark_node)
21914 decl = TYPE_NAME (decl);
21915 else
21916 decl = error_mark_node;
21918 /* Perform access checks for template parameters. */
21919 cp_parser_perform_template_parameter_access_checks (checks);
21923 /* Complain about missing 'typename' or other invalid type names. */
21924 if (!decl_specifiers.any_type_specifiers_p
21925 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21927 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21928 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21929 the rest of this declaration. */
21930 decl = error_mark_node;
21931 goto out;
21934 /* If it's not a template class, try for a template function. If
21935 the next token is a `;', then this declaration does not declare
21936 anything. But, if there were errors in the decl-specifiers, then
21937 the error might well have come from an attempted class-specifier.
21938 In that case, there's no need to warn about a missing declarator. */
21939 if (!decl
21940 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21941 || decl_specifiers.type != error_mark_node))
21943 decl = cp_parser_init_declarator (parser,
21944 &decl_specifiers,
21945 checks,
21946 /*function_definition_allowed_p=*/true,
21947 member_p,
21948 declares_class_or_enum,
21949 &function_definition_p,
21950 NULL);
21952 /* 7.1.1-1 [dcl.stc]
21954 A storage-class-specifier shall not be specified in an explicit
21955 specialization... */
21956 if (decl
21957 && explicit_specialization_p
21958 && decl_specifiers.storage_class != sc_none)
21960 error_at (decl_spec_token_start->location,
21961 "explicit template specialization cannot have a storage class");
21962 decl = error_mark_node;
21965 if (decl && TREE_CODE (decl) == VAR_DECL)
21966 check_template_variable (decl);
21969 /* Look for a trailing `;' after the declaration. */
21970 if (!function_definition_p
21971 && (decl == error_mark_node
21972 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21973 cp_parser_skip_to_end_of_block_or_statement (parser);
21975 out:
21976 pop_deferring_access_checks ();
21978 /* Clear any current qualification; whatever comes next is the start
21979 of something new. */
21980 parser->scope = NULL_TREE;
21981 parser->qualifying_scope = NULL_TREE;
21982 parser->object_scope = NULL_TREE;
21984 return decl;
21987 /* Parse a cast-expression that is not the operand of a unary "&". */
21989 static tree
21990 cp_parser_simple_cast_expression (cp_parser *parser)
21992 return cp_parser_cast_expression (parser, /*address_p=*/false,
21993 /*cast_p=*/false, NULL);
21996 /* Parse a functional cast to TYPE. Returns an expression
21997 representing the cast. */
21999 static tree
22000 cp_parser_functional_cast (cp_parser* parser, tree type)
22002 VEC(tree,gc) *vec;
22003 tree expression_list;
22004 tree cast;
22005 bool nonconst_p;
22007 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22009 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22010 expression_list = cp_parser_braced_list (parser, &nonconst_p);
22011 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
22012 if (TREE_CODE (type) == TYPE_DECL)
22013 type = TREE_TYPE (type);
22014 return finish_compound_literal (type, expression_list,
22015 tf_warning_or_error);
22019 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22020 /*cast_p=*/true,
22021 /*allow_expansion_p=*/true,
22022 /*non_constant_p=*/NULL);
22023 if (vec == NULL)
22024 expression_list = error_mark_node;
22025 else
22027 expression_list = build_tree_list_vec (vec);
22028 release_tree_vector (vec);
22031 cast = build_functional_cast (type, expression_list,
22032 tf_warning_or_error);
22033 /* [expr.const]/1: In an integral constant expression "only type
22034 conversions to integral or enumeration type can be used". */
22035 if (TREE_CODE (type) == TYPE_DECL)
22036 type = TREE_TYPE (type);
22037 if (cast != error_mark_node
22038 && !cast_valid_in_integral_constant_expression_p (type)
22039 && cp_parser_non_integral_constant_expression (parser,
22040 NIC_CONSTRUCTOR))
22041 return error_mark_node;
22042 return cast;
22045 /* Save the tokens that make up the body of a member function defined
22046 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
22047 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
22048 specifiers applied to the declaration. Returns the FUNCTION_DECL
22049 for the member function. */
22051 static tree
22052 cp_parser_save_member_function_body (cp_parser* parser,
22053 cp_decl_specifier_seq *decl_specifiers,
22054 cp_declarator *declarator,
22055 tree attributes)
22057 cp_token *first;
22058 cp_token *last;
22059 tree fn;
22061 /* Create the FUNCTION_DECL. */
22062 fn = grokmethod (decl_specifiers, declarator, attributes);
22063 /* If something went badly wrong, bail out now. */
22064 if (fn == error_mark_node)
22066 /* If there's a function-body, skip it. */
22067 if (cp_parser_token_starts_function_definition_p
22068 (cp_lexer_peek_token (parser->lexer)))
22069 cp_parser_skip_to_end_of_block_or_statement (parser);
22070 return error_mark_node;
22073 /* Remember it, if there default args to post process. */
22074 cp_parser_save_default_args (parser, fn);
22076 /* Save away the tokens that make up the body of the
22077 function. */
22078 first = parser->lexer->next_token;
22079 /* We can have braced-init-list mem-initializers before the fn body. */
22080 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22082 cp_lexer_consume_token (parser->lexer);
22083 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22084 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
22086 /* cache_group will stop after an un-nested { } pair, too. */
22087 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
22088 break;
22090 /* variadic mem-inits have ... after the ')'. */
22091 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22092 cp_lexer_consume_token (parser->lexer);
22095 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22096 /* Handle function try blocks. */
22097 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
22098 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22099 last = parser->lexer->next_token;
22101 /* Save away the inline definition; we will process it when the
22102 class is complete. */
22103 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
22104 DECL_PENDING_INLINE_P (fn) = 1;
22106 /* We need to know that this was defined in the class, so that
22107 friend templates are handled correctly. */
22108 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
22110 /* Add FN to the queue of functions to be parsed later. */
22111 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
22113 return fn;
22116 /* Save the tokens that make up the in-class initializer for a non-static
22117 data member. Returns a DEFAULT_ARG. */
22119 static tree
22120 cp_parser_save_nsdmi (cp_parser* parser)
22122 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
22125 /* Parse a template-argument-list, as well as the trailing ">" (but
22126 not the opening "<"). See cp_parser_template_argument_list for the
22127 return value. */
22129 static tree
22130 cp_parser_enclosed_template_argument_list (cp_parser* parser)
22132 tree arguments;
22133 tree saved_scope;
22134 tree saved_qualifying_scope;
22135 tree saved_object_scope;
22136 bool saved_greater_than_is_operator_p;
22137 int saved_unevaluated_operand;
22138 int saved_inhibit_evaluation_warnings;
22140 /* [temp.names]
22142 When parsing a template-id, the first non-nested `>' is taken as
22143 the end of the template-argument-list rather than a greater-than
22144 operator. */
22145 saved_greater_than_is_operator_p
22146 = parser->greater_than_is_operator_p;
22147 parser->greater_than_is_operator_p = false;
22148 /* Parsing the argument list may modify SCOPE, so we save it
22149 here. */
22150 saved_scope = parser->scope;
22151 saved_qualifying_scope = parser->qualifying_scope;
22152 saved_object_scope = parser->object_scope;
22153 /* We need to evaluate the template arguments, even though this
22154 template-id may be nested within a "sizeof". */
22155 saved_unevaluated_operand = cp_unevaluated_operand;
22156 cp_unevaluated_operand = 0;
22157 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22158 c_inhibit_evaluation_warnings = 0;
22159 /* Parse the template-argument-list itself. */
22160 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
22161 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22162 arguments = NULL_TREE;
22163 else
22164 arguments = cp_parser_template_argument_list (parser);
22165 /* Look for the `>' that ends the template-argument-list. If we find
22166 a '>>' instead, it's probably just a typo. */
22167 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22169 if (cxx_dialect != cxx98)
22171 /* In C++0x, a `>>' in a template argument list or cast
22172 expression is considered to be two separate `>'
22173 tokens. So, change the current token to a `>', but don't
22174 consume it: it will be consumed later when the outer
22175 template argument list (or cast expression) is parsed.
22176 Note that this replacement of `>' for `>>' is necessary
22177 even if we are parsing tentatively: in the tentative
22178 case, after calling
22179 cp_parser_enclosed_template_argument_list we will always
22180 throw away all of the template arguments and the first
22181 closing `>', either because the template argument list
22182 was erroneous or because we are replacing those tokens
22183 with a CPP_TEMPLATE_ID token. The second `>' (which will
22184 not have been thrown away) is needed either to close an
22185 outer template argument list or to complete a new-style
22186 cast. */
22187 cp_token *token = cp_lexer_peek_token (parser->lexer);
22188 token->type = CPP_GREATER;
22190 else if (!saved_greater_than_is_operator_p)
22192 /* If we're in a nested template argument list, the '>>' has
22193 to be a typo for '> >'. We emit the error message, but we
22194 continue parsing and we push a '>' as next token, so that
22195 the argument list will be parsed correctly. Note that the
22196 global source location is still on the token before the
22197 '>>', so we need to say explicitly where we want it. */
22198 cp_token *token = cp_lexer_peek_token (parser->lexer);
22199 error_at (token->location, "%<>>%> should be %<> >%> "
22200 "within a nested template argument list");
22202 token->type = CPP_GREATER;
22204 else
22206 /* If this is not a nested template argument list, the '>>'
22207 is a typo for '>'. Emit an error message and continue.
22208 Same deal about the token location, but here we can get it
22209 right by consuming the '>>' before issuing the diagnostic. */
22210 cp_token *token = cp_lexer_consume_token (parser->lexer);
22211 error_at (token->location,
22212 "spurious %<>>%>, use %<>%> to terminate "
22213 "a template argument list");
22216 else
22217 cp_parser_skip_to_end_of_template_parameter_list (parser);
22218 /* The `>' token might be a greater-than operator again now. */
22219 parser->greater_than_is_operator_p
22220 = saved_greater_than_is_operator_p;
22221 /* Restore the SAVED_SCOPE. */
22222 parser->scope = saved_scope;
22223 parser->qualifying_scope = saved_qualifying_scope;
22224 parser->object_scope = saved_object_scope;
22225 cp_unevaluated_operand = saved_unevaluated_operand;
22226 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22228 return arguments;
22231 /* MEMBER_FUNCTION is a member function, or a friend. If default
22232 arguments, or the body of the function have not yet been parsed,
22233 parse them now. */
22235 static void
22236 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
22238 timevar_push (TV_PARSE_INMETH);
22239 /* If this member is a template, get the underlying
22240 FUNCTION_DECL. */
22241 if (DECL_FUNCTION_TEMPLATE_P (member_function))
22242 member_function = DECL_TEMPLATE_RESULT (member_function);
22244 /* There should not be any class definitions in progress at this
22245 point; the bodies of members are only parsed outside of all class
22246 definitions. */
22247 gcc_assert (parser->num_classes_being_defined == 0);
22248 /* While we're parsing the member functions we might encounter more
22249 classes. We want to handle them right away, but we don't want
22250 them getting mixed up with functions that are currently in the
22251 queue. */
22252 push_unparsed_function_queues (parser);
22254 /* Make sure that any template parameters are in scope. */
22255 maybe_begin_member_template_processing (member_function);
22257 /* If the body of the function has not yet been parsed, parse it
22258 now. */
22259 if (DECL_PENDING_INLINE_P (member_function))
22261 tree function_scope;
22262 cp_token_cache *tokens;
22264 /* The function is no longer pending; we are processing it. */
22265 tokens = DECL_PENDING_INLINE_INFO (member_function);
22266 DECL_PENDING_INLINE_INFO (member_function) = NULL;
22267 DECL_PENDING_INLINE_P (member_function) = 0;
22269 /* If this is a local class, enter the scope of the containing
22270 function. */
22271 function_scope = current_function_decl;
22272 if (function_scope)
22273 push_function_context ();
22275 /* Push the body of the function onto the lexer stack. */
22276 cp_parser_push_lexer_for_tokens (parser, tokens);
22278 /* Let the front end know that we going to be defining this
22279 function. */
22280 start_preparsed_function (member_function, NULL_TREE,
22281 SF_PRE_PARSED | SF_INCLASS_INLINE);
22283 /* Don't do access checking if it is a templated function. */
22284 if (processing_template_decl)
22285 push_deferring_access_checks (dk_no_check);
22287 /* Now, parse the body of the function. */
22288 cp_parser_function_definition_after_declarator (parser,
22289 /*inline_p=*/true);
22291 if (processing_template_decl)
22292 pop_deferring_access_checks ();
22294 /* Leave the scope of the containing function. */
22295 if (function_scope)
22296 pop_function_context ();
22297 cp_parser_pop_lexer (parser);
22300 /* Remove any template parameters from the symbol table. */
22301 maybe_end_member_template_processing ();
22303 /* Restore the queue. */
22304 pop_unparsed_function_queues (parser);
22305 timevar_pop (TV_PARSE_INMETH);
22308 /* If DECL contains any default args, remember it on the unparsed
22309 functions queue. */
22311 static void
22312 cp_parser_save_default_args (cp_parser* parser, tree decl)
22314 tree probe;
22316 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
22317 probe;
22318 probe = TREE_CHAIN (probe))
22319 if (TREE_PURPOSE (probe))
22321 cp_default_arg_entry entry = {current_class_type, decl};
22322 VEC_safe_push (cp_default_arg_entry, gc,
22323 unparsed_funs_with_default_args, entry);
22324 break;
22328 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
22329 which is either a FIELD_DECL or PARM_DECL. Parse it and return
22330 the result. For a PARM_DECL, PARMTYPE is the corresponding type
22331 from the parameter-type-list. */
22333 static tree
22334 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
22335 tree default_arg, tree parmtype)
22337 cp_token_cache *tokens;
22338 tree parsed_arg;
22339 bool dummy;
22341 if (default_arg == error_mark_node)
22342 return error_mark_node;
22344 /* Push the saved tokens for the default argument onto the parser's
22345 lexer stack. */
22346 tokens = DEFARG_TOKENS (default_arg);
22347 cp_parser_push_lexer_for_tokens (parser, tokens);
22349 start_lambda_scope (decl);
22351 /* Parse the default argument. */
22352 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
22353 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
22354 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22356 finish_lambda_scope ();
22358 if (parsed_arg == error_mark_node)
22359 cp_parser_skip_to_end_of_statement (parser);
22361 if (!processing_template_decl)
22363 /* In a non-template class, check conversions now. In a template,
22364 we'll wait and instantiate these as needed. */
22365 if (TREE_CODE (decl) == PARM_DECL)
22366 parsed_arg = check_default_argument (parmtype, parsed_arg);
22367 else
22369 int flags = LOOKUP_IMPLICIT;
22370 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
22371 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
22372 flags = LOOKUP_NORMAL;
22373 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
22377 /* If the token stream has not been completely used up, then
22378 there was extra junk after the end of the default
22379 argument. */
22380 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22382 if (TREE_CODE (decl) == PARM_DECL)
22383 cp_parser_error (parser, "expected %<,%>");
22384 else
22385 cp_parser_error (parser, "expected %<;%>");
22388 /* Revert to the main lexer. */
22389 cp_parser_pop_lexer (parser);
22391 return parsed_arg;
22394 /* FIELD is a non-static data member with an initializer which we saved for
22395 later; parse it now. */
22397 static void
22398 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
22400 tree def;
22402 push_unparsed_function_queues (parser);
22403 def = cp_parser_late_parse_one_default_arg (parser, field,
22404 DECL_INITIAL (field),
22405 NULL_TREE);
22406 pop_unparsed_function_queues (parser);
22408 DECL_INITIAL (field) = def;
22411 /* FN is a FUNCTION_DECL which may contains a parameter with an
22412 unparsed DEFAULT_ARG. Parse the default args now. This function
22413 assumes that the current scope is the scope in which the default
22414 argument should be processed. */
22416 static void
22417 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
22419 bool saved_local_variables_forbidden_p;
22420 tree parm, parmdecl;
22422 /* While we're parsing the default args, we might (due to the
22423 statement expression extension) encounter more classes. We want
22424 to handle them right away, but we don't want them getting mixed
22425 up with default args that are currently in the queue. */
22426 push_unparsed_function_queues (parser);
22428 /* Local variable names (and the `this' keyword) may not appear
22429 in a default argument. */
22430 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22431 parser->local_variables_forbidden_p = true;
22433 push_defarg_context (fn);
22435 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
22436 parmdecl = DECL_ARGUMENTS (fn);
22437 parm && parm != void_list_node;
22438 parm = TREE_CHAIN (parm),
22439 parmdecl = DECL_CHAIN (parmdecl))
22441 tree default_arg = TREE_PURPOSE (parm);
22442 tree parsed_arg;
22443 VEC(tree,gc) *insts;
22444 tree copy;
22445 unsigned ix;
22447 if (!default_arg)
22448 continue;
22450 if (TREE_CODE (default_arg) != DEFAULT_ARG)
22451 /* This can happen for a friend declaration for a function
22452 already declared with default arguments. */
22453 continue;
22455 parsed_arg
22456 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
22457 default_arg,
22458 TREE_VALUE (parm));
22459 if (parsed_arg == error_mark_node)
22461 continue;
22464 TREE_PURPOSE (parm) = parsed_arg;
22466 /* Update any instantiations we've already created. */
22467 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
22468 VEC_iterate (tree, insts, ix, copy); ix++)
22469 TREE_PURPOSE (copy) = parsed_arg;
22472 pop_defarg_context ();
22474 /* Make sure no default arg is missing. */
22475 check_default_args (fn);
22477 /* Restore the state of local_variables_forbidden_p. */
22478 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22480 /* Restore the queue. */
22481 pop_unparsed_function_queues (parser);
22484 /* Parse the operand of `sizeof' (or a similar operator). Returns
22485 either a TYPE or an expression, depending on the form of the
22486 input. The KEYWORD indicates which kind of expression we have
22487 encountered. */
22489 static tree
22490 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
22492 tree expr = NULL_TREE;
22493 const char *saved_message;
22494 char *tmp;
22495 bool saved_integral_constant_expression_p;
22496 bool saved_non_integral_constant_expression_p;
22497 bool pack_expansion_p = false;
22499 /* Types cannot be defined in a `sizeof' expression. Save away the
22500 old message. */
22501 saved_message = parser->type_definition_forbidden_message;
22502 /* And create the new one. */
22503 tmp = concat ("types may not be defined in %<",
22504 IDENTIFIER_POINTER (ridpointers[keyword]),
22505 "%> expressions", NULL);
22506 parser->type_definition_forbidden_message = tmp;
22508 /* The restrictions on constant-expressions do not apply inside
22509 sizeof expressions. */
22510 saved_integral_constant_expression_p
22511 = parser->integral_constant_expression_p;
22512 saved_non_integral_constant_expression_p
22513 = parser->non_integral_constant_expression_p;
22514 parser->integral_constant_expression_p = false;
22516 /* If it's a `...', then we are computing the length of a parameter
22517 pack. */
22518 if (keyword == RID_SIZEOF
22519 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22521 /* Consume the `...'. */
22522 cp_lexer_consume_token (parser->lexer);
22523 maybe_warn_variadic_templates ();
22525 /* Note that this is an expansion. */
22526 pack_expansion_p = true;
22529 /* Do not actually evaluate the expression. */
22530 ++cp_unevaluated_operand;
22531 ++c_inhibit_evaluation_warnings;
22532 /* If it's a `(', then we might be looking at the type-id
22533 construction. */
22534 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22536 tree type;
22537 bool saved_in_type_id_in_expr_p;
22539 /* We can't be sure yet whether we're looking at a type-id or an
22540 expression. */
22541 cp_parser_parse_tentatively (parser);
22542 /* Consume the `('. */
22543 cp_lexer_consume_token (parser->lexer);
22544 /* Parse the type-id. */
22545 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
22546 parser->in_type_id_in_expr_p = true;
22547 type = cp_parser_type_id (parser);
22548 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
22549 /* Now, look for the trailing `)'. */
22550 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22551 /* If all went well, then we're done. */
22552 if (cp_parser_parse_definitely (parser))
22554 cp_decl_specifier_seq decl_specs;
22556 /* Build a trivial decl-specifier-seq. */
22557 clear_decl_specs (&decl_specs);
22558 decl_specs.type = type;
22560 /* Call grokdeclarator to figure out what type this is. */
22561 expr = grokdeclarator (NULL,
22562 &decl_specs,
22563 TYPENAME,
22564 /*initialized=*/0,
22565 /*attrlist=*/NULL);
22568 else if (pack_expansion_p)
22569 permerror (cp_lexer_peek_token (parser->lexer)->location,
22570 "%<sizeof...%> argument must be surrounded by parentheses");
22572 /* If the type-id production did not work out, then we must be
22573 looking at the unary-expression production. */
22574 if (!expr)
22575 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
22576 /*cast_p=*/false, NULL);
22578 if (pack_expansion_p)
22579 /* Build a pack expansion. */
22580 expr = make_pack_expansion (expr);
22582 /* Go back to evaluating expressions. */
22583 --cp_unevaluated_operand;
22584 --c_inhibit_evaluation_warnings;
22586 /* Free the message we created. */
22587 free (tmp);
22588 /* And restore the old one. */
22589 parser->type_definition_forbidden_message = saved_message;
22590 parser->integral_constant_expression_p
22591 = saved_integral_constant_expression_p;
22592 parser->non_integral_constant_expression_p
22593 = saved_non_integral_constant_expression_p;
22595 return expr;
22598 /* If the current declaration has no declarator, return true. */
22600 static bool
22601 cp_parser_declares_only_class_p (cp_parser *parser)
22603 /* If the next token is a `;' or a `,' then there is no
22604 declarator. */
22605 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22606 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22609 /* Update the DECL_SPECS to reflect the storage class indicated by
22610 KEYWORD. */
22612 static void
22613 cp_parser_set_storage_class (cp_parser *parser,
22614 cp_decl_specifier_seq *decl_specs,
22615 enum rid keyword,
22616 cp_token *token)
22618 cp_storage_class storage_class;
22620 if (parser->in_unbraced_linkage_specification_p)
22622 error_at (token->location, "invalid use of %qD in linkage specification",
22623 ridpointers[keyword]);
22624 return;
22626 else if (decl_specs->storage_class != sc_none)
22628 decl_specs->conflicting_specifiers_p = true;
22629 return;
22632 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22633 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
22634 && decl_specs->gnu_thread_keyword_p)
22636 pedwarn (decl_specs->locations[ds_thread], 0,
22637 "%<__thread%> before %qD", ridpointers[keyword]);
22640 switch (keyword)
22642 case RID_AUTO:
22643 storage_class = sc_auto;
22644 break;
22645 case RID_REGISTER:
22646 storage_class = sc_register;
22647 break;
22648 case RID_STATIC:
22649 storage_class = sc_static;
22650 break;
22651 case RID_EXTERN:
22652 storage_class = sc_extern;
22653 break;
22654 case RID_MUTABLE:
22655 storage_class = sc_mutable;
22656 break;
22657 default:
22658 gcc_unreachable ();
22660 decl_specs->storage_class = storage_class;
22661 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
22663 /* A storage class specifier cannot be applied alongside a typedef
22664 specifier. If there is a typedef specifier present then set
22665 conflicting_specifiers_p which will trigger an error later
22666 on in grokdeclarator. */
22667 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
22668 decl_specs->conflicting_specifiers_p = true;
22671 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22672 is true, the type is a class or enum definition. */
22674 static void
22675 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22676 tree type_spec,
22677 cp_token *token,
22678 bool type_definition_p)
22680 decl_specs->any_specifiers_p = true;
22682 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22683 (with, for example, in "typedef int wchar_t;") we remember that
22684 this is what happened. In system headers, we ignore these
22685 declarations so that G++ can work with system headers that are not
22686 C++-safe. */
22687 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
22688 && !type_definition_p
22689 && (type_spec == boolean_type_node
22690 || type_spec == char16_type_node
22691 || type_spec == char32_type_node
22692 || type_spec == wchar_type_node)
22693 && (decl_specs->type
22694 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
22695 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
22696 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
22697 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
22699 decl_specs->redefined_builtin_type = type_spec;
22700 set_and_check_decl_spec_loc (decl_specs,
22701 ds_redefined_builtin_type_spec,
22702 token);
22703 if (!decl_specs->type)
22705 decl_specs->type = type_spec;
22706 decl_specs->type_definition_p = false;
22707 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
22710 else if (decl_specs->type)
22711 decl_specs->multiple_types_p = true;
22712 else
22714 decl_specs->type = type_spec;
22715 decl_specs->type_definition_p = type_definition_p;
22716 decl_specs->redefined_builtin_type = NULL_TREE;
22717 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
22721 /* True iff TOKEN is the GNU keyword __thread. */
22723 static bool
22724 token_is__thread (cp_token *token)
22726 gcc_assert (token->keyword == RID_THREAD);
22727 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
22730 /* Set the location for a declarator specifier and check if it is
22731 duplicated.
22733 DECL_SPECS is the sequence of declarator specifiers onto which to
22734 set the location.
22736 DS is the single declarator specifier to set which location is to
22737 be set onto the existing sequence of declarators.
22739 LOCATION is the location for the declarator specifier to
22740 consider. */
22742 static void
22743 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
22744 cp_decl_spec ds, cp_token *token)
22746 gcc_assert (ds < ds_last);
22748 if (decl_specs == NULL)
22749 return;
22751 source_location location = token->location;
22753 if (decl_specs->locations[ds] == 0)
22755 decl_specs->locations[ds] = location;
22756 if (ds == ds_thread)
22757 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
22759 else
22761 if (ds == ds_long)
22763 if (decl_specs->locations[ds_long_long] != 0)
22764 error_at (location,
22765 "%<long long long%> is too long for GCC");
22766 else
22768 decl_specs->locations[ds_long_long] = location;
22769 pedwarn_cxx98 (location,
22770 OPT_Wlong_long,
22771 "ISO C++ 1998 does not support %<long long%>");
22774 else if (ds == ds_thread)
22776 bool gnu = token_is__thread (token);
22777 if (gnu != decl_specs->gnu_thread_keyword_p)
22778 error_at (location,
22779 "both %<__thread%> and %<thread_local%> specified");
22780 else
22781 error_at (location, "duplicate %qD", token->u.value);
22783 else
22785 static const char *const decl_spec_names[] = {
22786 "signed",
22787 "unsigned",
22788 "short",
22789 "long",
22790 "const",
22791 "volatile",
22792 "restrict",
22793 "inline",
22794 "virtual",
22795 "explicit",
22796 "friend",
22797 "typedef",
22798 "using",
22799 "constexpr",
22800 "__complex"
22802 error_at (location,
22803 "duplicate %qs", decl_spec_names[ds]);
22808 /* Return true iff the declarator specifier DS is present in the
22809 sequence of declarator specifiers DECL_SPECS. */
22811 bool
22812 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
22813 cp_decl_spec ds)
22815 gcc_assert (ds < ds_last);
22817 if (decl_specs == NULL)
22818 return false;
22820 return decl_specs->locations[ds] != 0;
22823 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22824 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22826 static bool
22827 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22829 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
22832 /* Issue an error message indicating that TOKEN_DESC was expected.
22833 If KEYWORD is true, it indicated this function is called by
22834 cp_parser_require_keword and the required token can only be
22835 a indicated keyword. */
22837 static void
22838 cp_parser_required_error (cp_parser *parser,
22839 required_token token_desc,
22840 bool keyword)
22842 switch (token_desc)
22844 case RT_NEW:
22845 cp_parser_error (parser, "expected %<new%>");
22846 return;
22847 case RT_DELETE:
22848 cp_parser_error (parser, "expected %<delete%>");
22849 return;
22850 case RT_RETURN:
22851 cp_parser_error (parser, "expected %<return%>");
22852 return;
22853 case RT_WHILE:
22854 cp_parser_error (parser, "expected %<while%>");
22855 return;
22856 case RT_EXTERN:
22857 cp_parser_error (parser, "expected %<extern%>");
22858 return;
22859 case RT_STATIC_ASSERT:
22860 cp_parser_error (parser, "expected %<static_assert%>");
22861 return;
22862 case RT_DECLTYPE:
22863 cp_parser_error (parser, "expected %<decltype%>");
22864 return;
22865 case RT_OPERATOR:
22866 cp_parser_error (parser, "expected %<operator%>");
22867 return;
22868 case RT_CLASS:
22869 cp_parser_error (parser, "expected %<class%>");
22870 return;
22871 case RT_TEMPLATE:
22872 cp_parser_error (parser, "expected %<template%>");
22873 return;
22874 case RT_NAMESPACE:
22875 cp_parser_error (parser, "expected %<namespace%>");
22876 return;
22877 case RT_USING:
22878 cp_parser_error (parser, "expected %<using%>");
22879 return;
22880 case RT_ASM:
22881 cp_parser_error (parser, "expected %<asm%>");
22882 return;
22883 case RT_TRY:
22884 cp_parser_error (parser, "expected %<try%>");
22885 return;
22886 case RT_CATCH:
22887 cp_parser_error (parser, "expected %<catch%>");
22888 return;
22889 case RT_THROW:
22890 cp_parser_error (parser, "expected %<throw%>");
22891 return;
22892 case RT_LABEL:
22893 cp_parser_error (parser, "expected %<__label__%>");
22894 return;
22895 case RT_AT_TRY:
22896 cp_parser_error (parser, "expected %<@try%>");
22897 return;
22898 case RT_AT_SYNCHRONIZED:
22899 cp_parser_error (parser, "expected %<@synchronized%>");
22900 return;
22901 case RT_AT_THROW:
22902 cp_parser_error (parser, "expected %<@throw%>");
22903 return;
22904 case RT_TRANSACTION_ATOMIC:
22905 cp_parser_error (parser, "expected %<__transaction_atomic%>");
22906 return;
22907 case RT_TRANSACTION_RELAXED:
22908 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22909 return;
22910 default:
22911 break;
22913 if (!keyword)
22915 switch (token_desc)
22917 case RT_SEMICOLON:
22918 cp_parser_error (parser, "expected %<;%>");
22919 return;
22920 case RT_OPEN_PAREN:
22921 cp_parser_error (parser, "expected %<(%>");
22922 return;
22923 case RT_CLOSE_BRACE:
22924 cp_parser_error (parser, "expected %<}%>");
22925 return;
22926 case RT_OPEN_BRACE:
22927 cp_parser_error (parser, "expected %<{%>");
22928 return;
22929 case RT_CLOSE_SQUARE:
22930 cp_parser_error (parser, "expected %<]%>");
22931 return;
22932 case RT_OPEN_SQUARE:
22933 cp_parser_error (parser, "expected %<[%>");
22934 return;
22935 case RT_COMMA:
22936 cp_parser_error (parser, "expected %<,%>");
22937 return;
22938 case RT_SCOPE:
22939 cp_parser_error (parser, "expected %<::%>");
22940 return;
22941 case RT_LESS:
22942 cp_parser_error (parser, "expected %<<%>");
22943 return;
22944 case RT_GREATER:
22945 cp_parser_error (parser, "expected %<>%>");
22946 return;
22947 case RT_EQ:
22948 cp_parser_error (parser, "expected %<=%>");
22949 return;
22950 case RT_ELLIPSIS:
22951 cp_parser_error (parser, "expected %<...%>");
22952 return;
22953 case RT_MULT:
22954 cp_parser_error (parser, "expected %<*%>");
22955 return;
22956 case RT_COMPL:
22957 cp_parser_error (parser, "expected %<~%>");
22958 return;
22959 case RT_COLON:
22960 cp_parser_error (parser, "expected %<:%>");
22961 return;
22962 case RT_COLON_SCOPE:
22963 cp_parser_error (parser, "expected %<:%> or %<::%>");
22964 return;
22965 case RT_CLOSE_PAREN:
22966 cp_parser_error (parser, "expected %<)%>");
22967 return;
22968 case RT_COMMA_CLOSE_PAREN:
22969 cp_parser_error (parser, "expected %<,%> or %<)%>");
22970 return;
22971 case RT_PRAGMA_EOL:
22972 cp_parser_error (parser, "expected end of line");
22973 return;
22974 case RT_NAME:
22975 cp_parser_error (parser, "expected identifier");
22976 return;
22977 case RT_SELECT:
22978 cp_parser_error (parser, "expected selection-statement");
22979 return;
22980 case RT_INTERATION:
22981 cp_parser_error (parser, "expected iteration-statement");
22982 return;
22983 case RT_JUMP:
22984 cp_parser_error (parser, "expected jump-statement");
22985 return;
22986 case RT_CLASS_KEY:
22987 cp_parser_error (parser, "expected class-key");
22988 return;
22989 case RT_CLASS_TYPENAME_TEMPLATE:
22990 cp_parser_error (parser,
22991 "expected %<class%>, %<typename%>, or %<template%>");
22992 return;
22993 default:
22994 gcc_unreachable ();
22997 else
22998 gcc_unreachable ();
23003 /* If the next token is of the indicated TYPE, consume it. Otherwise,
23004 issue an error message indicating that TOKEN_DESC was expected.
23006 Returns the token consumed, if the token had the appropriate type.
23007 Otherwise, returns NULL. */
23009 static cp_token *
23010 cp_parser_require (cp_parser* parser,
23011 enum cpp_ttype type,
23012 required_token token_desc)
23014 if (cp_lexer_next_token_is (parser->lexer, type))
23015 return cp_lexer_consume_token (parser->lexer);
23016 else
23018 /* Output the MESSAGE -- unless we're parsing tentatively. */
23019 if (!cp_parser_simulate_error (parser))
23020 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
23021 return NULL;
23025 /* An error message is produced if the next token is not '>'.
23026 All further tokens are skipped until the desired token is
23027 found or '{', '}', ';' or an unbalanced ')' or ']'. */
23029 static void
23030 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
23032 /* Current level of '< ... >'. */
23033 unsigned level = 0;
23034 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
23035 unsigned nesting_depth = 0;
23037 /* Are we ready, yet? If not, issue error message. */
23038 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
23039 return;
23041 /* Skip tokens until the desired token is found. */
23042 while (true)
23044 /* Peek at the next token. */
23045 switch (cp_lexer_peek_token (parser->lexer)->type)
23047 case CPP_LESS:
23048 if (!nesting_depth)
23049 ++level;
23050 break;
23052 case CPP_RSHIFT:
23053 if (cxx_dialect == cxx98)
23054 /* C++0x views the `>>' operator as two `>' tokens, but
23055 C++98 does not. */
23056 break;
23057 else if (!nesting_depth && level-- == 0)
23059 /* We've hit a `>>' where the first `>' closes the
23060 template argument list, and the second `>' is
23061 spurious. Just consume the `>>' and stop; we've
23062 already produced at least one error. */
23063 cp_lexer_consume_token (parser->lexer);
23064 return;
23066 /* Fall through for C++0x, so we handle the second `>' in
23067 the `>>'. */
23069 case CPP_GREATER:
23070 if (!nesting_depth && level-- == 0)
23072 /* We've reached the token we want, consume it and stop. */
23073 cp_lexer_consume_token (parser->lexer);
23074 return;
23076 break;
23078 case CPP_OPEN_PAREN:
23079 case CPP_OPEN_SQUARE:
23080 ++nesting_depth;
23081 break;
23083 case CPP_CLOSE_PAREN:
23084 case CPP_CLOSE_SQUARE:
23085 if (nesting_depth-- == 0)
23086 return;
23087 break;
23089 case CPP_EOF:
23090 case CPP_PRAGMA_EOL:
23091 case CPP_SEMICOLON:
23092 case CPP_OPEN_BRACE:
23093 case CPP_CLOSE_BRACE:
23094 /* The '>' was probably forgotten, don't look further. */
23095 return;
23097 default:
23098 break;
23101 /* Consume this token. */
23102 cp_lexer_consume_token (parser->lexer);
23106 /* If the next token is the indicated keyword, consume it. Otherwise,
23107 issue an error message indicating that TOKEN_DESC was expected.
23109 Returns the token consumed, if the token had the appropriate type.
23110 Otherwise, returns NULL. */
23112 static cp_token *
23113 cp_parser_require_keyword (cp_parser* parser,
23114 enum rid keyword,
23115 required_token token_desc)
23117 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
23119 if (token && token->keyword != keyword)
23121 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
23122 return NULL;
23125 return token;
23128 /* Returns TRUE iff TOKEN is a token that can begin the body of a
23129 function-definition. */
23131 static bool
23132 cp_parser_token_starts_function_definition_p (cp_token* token)
23134 return (/* An ordinary function-body begins with an `{'. */
23135 token->type == CPP_OPEN_BRACE
23136 /* A ctor-initializer begins with a `:'. */
23137 || token->type == CPP_COLON
23138 /* A function-try-block begins with `try'. */
23139 || token->keyword == RID_TRY
23140 /* A function-transaction-block begins with `__transaction_atomic'
23141 or `__transaction_relaxed'. */
23142 || token->keyword == RID_TRANSACTION_ATOMIC
23143 || token->keyword == RID_TRANSACTION_RELAXED
23144 /* The named return value extension begins with `return'. */
23145 || token->keyword == RID_RETURN);
23148 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
23149 definition. */
23151 static bool
23152 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
23154 cp_token *token;
23156 token = cp_lexer_peek_token (parser->lexer);
23157 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
23160 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
23161 C++0x) ending a template-argument. */
23163 static bool
23164 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
23166 cp_token *token;
23168 token = cp_lexer_peek_token (parser->lexer);
23169 return (token->type == CPP_COMMA
23170 || token->type == CPP_GREATER
23171 || token->type == CPP_ELLIPSIS
23172 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
23175 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
23176 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
23178 static bool
23179 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
23180 size_t n)
23182 cp_token *token;
23184 token = cp_lexer_peek_nth_token (parser->lexer, n);
23185 if (token->type == CPP_LESS)
23186 return true;
23187 /* Check for the sequence `<::' in the original code. It would be lexed as
23188 `[:', where `[' is a digraph, and there is no whitespace before
23189 `:'. */
23190 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
23192 cp_token *token2;
23193 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
23194 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
23195 return true;
23197 return false;
23200 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
23201 or none_type otherwise. */
23203 static enum tag_types
23204 cp_parser_token_is_class_key (cp_token* token)
23206 switch (token->keyword)
23208 case RID_CLASS:
23209 return class_type;
23210 case RID_STRUCT:
23211 return record_type;
23212 case RID_UNION:
23213 return union_type;
23215 default:
23216 return none_type;
23220 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
23222 static void
23223 cp_parser_check_class_key (enum tag_types class_key, tree type)
23225 if (type == error_mark_node)
23226 return;
23227 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
23229 permerror (input_location, "%qs tag used in naming %q#T",
23230 class_key == union_type ? "union"
23231 : class_key == record_type ? "struct" : "class",
23232 type);
23233 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
23234 "%q#T was previously declared here", type);
23238 /* Issue an error message if DECL is redeclared with different
23239 access than its original declaration [class.access.spec/3].
23240 This applies to nested classes and nested class templates.
23241 [class.mem/1]. */
23243 static void
23244 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
23246 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
23247 return;
23249 if ((TREE_PRIVATE (decl)
23250 != (current_access_specifier == access_private_node))
23251 || (TREE_PROTECTED (decl)
23252 != (current_access_specifier == access_protected_node)))
23253 error_at (location, "%qD redeclared with different access", decl);
23256 /* Look for the `template' keyword, as a syntactic disambiguator.
23257 Return TRUE iff it is present, in which case it will be
23258 consumed. */
23260 static bool
23261 cp_parser_optional_template_keyword (cp_parser *parser)
23263 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23265 /* In C++98 the `template' keyword can only be used within templates;
23266 outside templates the parser can always figure out what is a
23267 template and what is not. In C++11, per the resolution of DR 468,
23268 `template' is allowed in cases where it is not strictly necessary. */
23269 if (!processing_template_decl
23270 && pedantic && cxx_dialect == cxx98)
23272 cp_token *token = cp_lexer_peek_token (parser->lexer);
23273 pedwarn (token->location, OPT_Wpedantic,
23274 "in C++98 %<template%> (as a disambiguator) is only "
23275 "allowed within templates");
23276 /* If this part of the token stream is rescanned, the same
23277 error message would be generated. So, we purge the token
23278 from the stream. */
23279 cp_lexer_purge_token (parser->lexer);
23280 return false;
23282 else
23284 /* Consume the `template' keyword. */
23285 cp_lexer_consume_token (parser->lexer);
23286 return true;
23289 return false;
23292 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
23293 set PARSER->SCOPE, and perform other related actions. */
23295 static void
23296 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
23298 int i;
23299 struct tree_check *check_value;
23300 deferred_access_check *chk;
23301 VEC (deferred_access_check,gc) *checks;
23303 /* Get the stored value. */
23304 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
23305 /* Perform any access checks that were deferred. */
23306 checks = check_value->checks;
23307 if (checks)
23309 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
23310 perform_or_defer_access_check (chk->binfo,
23311 chk->decl,
23312 chk->diag_decl, tf_warning_or_error);
23314 /* Set the scope from the stored value. */
23315 parser->scope = check_value->value;
23316 parser->qualifying_scope = check_value->qualifying_scope;
23317 parser->object_scope = NULL_TREE;
23320 /* Consume tokens up through a non-nested END token. Returns TRUE if we
23321 encounter the end of a block before what we were looking for. */
23323 static bool
23324 cp_parser_cache_group (cp_parser *parser,
23325 enum cpp_ttype end,
23326 unsigned depth)
23328 while (true)
23330 cp_token *token = cp_lexer_peek_token (parser->lexer);
23332 /* Abort a parenthesized expression if we encounter a semicolon. */
23333 if ((end == CPP_CLOSE_PAREN || depth == 0)
23334 && token->type == CPP_SEMICOLON)
23335 return true;
23336 /* If we've reached the end of the file, stop. */
23337 if (token->type == CPP_EOF
23338 || (end != CPP_PRAGMA_EOL
23339 && token->type == CPP_PRAGMA_EOL))
23340 return true;
23341 if (token->type == CPP_CLOSE_BRACE && depth == 0)
23342 /* We've hit the end of an enclosing block, so there's been some
23343 kind of syntax error. */
23344 return true;
23346 /* Consume the token. */
23347 cp_lexer_consume_token (parser->lexer);
23348 /* See if it starts a new group. */
23349 if (token->type == CPP_OPEN_BRACE)
23351 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
23352 /* In theory this should probably check end == '}', but
23353 cp_parser_save_member_function_body needs it to exit
23354 after either '}' or ')' when called with ')'. */
23355 if (depth == 0)
23356 return false;
23358 else if (token->type == CPP_OPEN_PAREN)
23360 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
23361 if (depth == 0 && end == CPP_CLOSE_PAREN)
23362 return false;
23364 else if (token->type == CPP_PRAGMA)
23365 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
23366 else if (token->type == end)
23367 return false;
23371 /* Like above, for caching a default argument or NSDMI. Both of these are
23372 terminated by a non-nested comma, but it can be unclear whether or not a
23373 comma is nested in a template argument list unless we do more parsing.
23374 In order to handle this ambiguity, when we encounter a ',' after a '<'
23375 we try to parse what follows as a parameter-declaration-list (in the
23376 case of a default argument) or a member-declarator (in the case of an
23377 NSDMI). If that succeeds, then we stop caching. */
23379 static tree
23380 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
23382 unsigned depth = 0;
23383 int maybe_template_id = 0;
23384 cp_token *first_token;
23385 cp_token *token;
23386 tree default_argument;
23388 /* Add tokens until we have processed the entire default
23389 argument. We add the range [first_token, token). */
23390 first_token = cp_lexer_peek_token (parser->lexer);
23391 if (first_token->type == CPP_OPEN_BRACE)
23393 /* For list-initialization, this is straightforward. */
23394 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23395 token = cp_lexer_peek_token (parser->lexer);
23397 else while (true)
23399 bool done = false;
23401 /* Peek at the next token. */
23402 token = cp_lexer_peek_token (parser->lexer);
23403 /* What we do depends on what token we have. */
23404 switch (token->type)
23406 /* In valid code, a default argument must be
23407 immediately followed by a `,' `)', or `...'. */
23408 case CPP_COMMA:
23409 if (depth == 0 && maybe_template_id)
23411 /* If we've seen a '<', we might be in a
23412 template-argument-list. Until Core issue 325 is
23413 resolved, we don't know how this situation ought
23414 to be handled, so try to DTRT. We check whether
23415 what comes after the comma is a valid parameter
23416 declaration list. If it is, then the comma ends
23417 the default argument; otherwise the default
23418 argument continues. */
23419 bool error = false;
23420 tree t;
23422 /* Set ITALP so cp_parser_parameter_declaration_list
23423 doesn't decide to commit to this parse. */
23424 bool saved_italp = parser->in_template_argument_list_p;
23425 parser->in_template_argument_list_p = true;
23427 cp_parser_parse_tentatively (parser);
23428 cp_lexer_consume_token (parser->lexer);
23430 if (nsdmi)
23432 int ctor_dtor_or_conv_p;
23433 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23434 &ctor_dtor_or_conv_p,
23435 /*parenthesized_p=*/NULL,
23436 /*member_p=*/true);
23438 else
23440 begin_scope (sk_function_parms, NULL_TREE);
23441 cp_parser_parameter_declaration_list (parser, &error);
23442 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
23443 pop_binding (DECL_NAME (t), t);
23444 leave_scope ();
23446 if (!cp_parser_error_occurred (parser) && !error)
23447 done = true;
23448 cp_parser_abort_tentative_parse (parser);
23450 parser->in_template_argument_list_p = saved_italp;
23451 break;
23453 case CPP_CLOSE_PAREN:
23454 case CPP_ELLIPSIS:
23455 /* If we run into a non-nested `;', `}', or `]',
23456 then the code is invalid -- but the default
23457 argument is certainly over. */
23458 case CPP_SEMICOLON:
23459 case CPP_CLOSE_BRACE:
23460 case CPP_CLOSE_SQUARE:
23461 if (depth == 0)
23462 done = true;
23463 /* Update DEPTH, if necessary. */
23464 else if (token->type == CPP_CLOSE_PAREN
23465 || token->type == CPP_CLOSE_BRACE
23466 || token->type == CPP_CLOSE_SQUARE)
23467 --depth;
23468 break;
23470 case CPP_OPEN_PAREN:
23471 case CPP_OPEN_SQUARE:
23472 case CPP_OPEN_BRACE:
23473 ++depth;
23474 break;
23476 case CPP_LESS:
23477 if (depth == 0)
23478 /* This might be the comparison operator, or it might
23479 start a template argument list. */
23480 ++maybe_template_id;
23481 break;
23483 case CPP_RSHIFT:
23484 if (cxx_dialect == cxx98)
23485 break;
23486 /* Fall through for C++0x, which treats the `>>'
23487 operator like two `>' tokens in certain
23488 cases. */
23490 case CPP_GREATER:
23491 if (depth == 0)
23493 /* This might be an operator, or it might close a
23494 template argument list. But if a previous '<'
23495 started a template argument list, this will have
23496 closed it, so we can't be in one anymore. */
23497 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
23498 if (maybe_template_id < 0)
23499 maybe_template_id = 0;
23501 break;
23503 /* If we run out of tokens, issue an error message. */
23504 case CPP_EOF:
23505 case CPP_PRAGMA_EOL:
23506 error_at (token->location, "file ends in default argument");
23507 done = true;
23508 break;
23510 case CPP_NAME:
23511 case CPP_SCOPE:
23512 /* In these cases, we should look for template-ids.
23513 For example, if the default argument is
23514 `X<int, double>()', we need to do name lookup to
23515 figure out whether or not `X' is a template; if
23516 so, the `,' does not end the default argument.
23518 That is not yet done. */
23519 break;
23521 default:
23522 break;
23525 /* If we've reached the end, stop. */
23526 if (done)
23527 break;
23529 /* Add the token to the token block. */
23530 token = cp_lexer_consume_token (parser->lexer);
23533 /* Create a DEFAULT_ARG to represent the unparsed default
23534 argument. */
23535 default_argument = make_node (DEFAULT_ARG);
23536 DEFARG_TOKENS (default_argument)
23537 = cp_token_cache_new (first_token, token);
23538 DEFARG_INSTANTIATIONS (default_argument) = NULL;
23540 return default_argument;
23543 /* Begin parsing tentatively. We always save tokens while parsing
23544 tentatively so that if the tentative parsing fails we can restore the
23545 tokens. */
23547 static void
23548 cp_parser_parse_tentatively (cp_parser* parser)
23550 /* Enter a new parsing context. */
23551 parser->context = cp_parser_context_new (parser->context);
23552 /* Begin saving tokens. */
23553 cp_lexer_save_tokens (parser->lexer);
23554 /* In order to avoid repetitive access control error messages,
23555 access checks are queued up until we are no longer parsing
23556 tentatively. */
23557 push_deferring_access_checks (dk_deferred);
23560 /* Commit to the currently active tentative parse. */
23562 static void
23563 cp_parser_commit_to_tentative_parse (cp_parser* parser)
23565 cp_parser_context *context;
23566 cp_lexer *lexer;
23568 /* Mark all of the levels as committed. */
23569 lexer = parser->lexer;
23570 for (context = parser->context; context->next; context = context->next)
23572 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
23573 break;
23574 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
23575 while (!cp_lexer_saving_tokens (lexer))
23576 lexer = lexer->next;
23577 cp_lexer_commit_tokens (lexer);
23581 /* Abort the currently active tentative parse. All consumed tokens
23582 will be rolled back, and no diagnostics will be issued. */
23584 static void
23585 cp_parser_abort_tentative_parse (cp_parser* parser)
23587 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
23588 || errorcount > 0);
23589 cp_parser_simulate_error (parser);
23590 /* Now, pretend that we want to see if the construct was
23591 successfully parsed. */
23592 cp_parser_parse_definitely (parser);
23595 /* Stop parsing tentatively. If a parse error has occurred, restore the
23596 token stream. Otherwise, commit to the tokens we have consumed.
23597 Returns true if no error occurred; false otherwise. */
23599 static bool
23600 cp_parser_parse_definitely (cp_parser* parser)
23602 bool error_occurred;
23603 cp_parser_context *context;
23605 /* Remember whether or not an error occurred, since we are about to
23606 destroy that information. */
23607 error_occurred = cp_parser_error_occurred (parser);
23608 /* Remove the topmost context from the stack. */
23609 context = parser->context;
23610 parser->context = context->next;
23611 /* If no parse errors occurred, commit to the tentative parse. */
23612 if (!error_occurred)
23614 /* Commit to the tokens read tentatively, unless that was
23615 already done. */
23616 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
23617 cp_lexer_commit_tokens (parser->lexer);
23619 pop_to_parent_deferring_access_checks ();
23621 /* Otherwise, if errors occurred, roll back our state so that things
23622 are just as they were before we began the tentative parse. */
23623 else
23625 cp_lexer_rollback_tokens (parser->lexer);
23626 pop_deferring_access_checks ();
23628 /* Add the context to the front of the free list. */
23629 context->next = cp_parser_context_free_list;
23630 cp_parser_context_free_list = context;
23632 return !error_occurred;
23635 /* Returns true if we are parsing tentatively and are not committed to
23636 this tentative parse. */
23638 static bool
23639 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
23641 return (cp_parser_parsing_tentatively (parser)
23642 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
23645 /* Returns nonzero iff an error has occurred during the most recent
23646 tentative parse. */
23648 static bool
23649 cp_parser_error_occurred (cp_parser* parser)
23651 return (cp_parser_parsing_tentatively (parser)
23652 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
23655 /* Returns nonzero if GNU extensions are allowed. */
23657 static bool
23658 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
23660 return parser->allow_gnu_extensions_p;
23663 /* Objective-C++ Productions */
23666 /* Parse an Objective-C expression, which feeds into a primary-expression
23667 above.
23669 objc-expression:
23670 objc-message-expression
23671 objc-string-literal
23672 objc-encode-expression
23673 objc-protocol-expression
23674 objc-selector-expression
23676 Returns a tree representation of the expression. */
23678 static tree
23679 cp_parser_objc_expression (cp_parser* parser)
23681 /* Try to figure out what kind of declaration is present. */
23682 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23684 switch (kwd->type)
23686 case CPP_OPEN_SQUARE:
23687 return cp_parser_objc_message_expression (parser);
23689 case CPP_OBJC_STRING:
23690 kwd = cp_lexer_consume_token (parser->lexer);
23691 return objc_build_string_object (kwd->u.value);
23693 case CPP_KEYWORD:
23694 switch (kwd->keyword)
23696 case RID_AT_ENCODE:
23697 return cp_parser_objc_encode_expression (parser);
23699 case RID_AT_PROTOCOL:
23700 return cp_parser_objc_protocol_expression (parser);
23702 case RID_AT_SELECTOR:
23703 return cp_parser_objc_selector_expression (parser);
23705 default:
23706 break;
23708 default:
23709 error_at (kwd->location,
23710 "misplaced %<@%D%> Objective-C++ construct",
23711 kwd->u.value);
23712 cp_parser_skip_to_end_of_block_or_statement (parser);
23715 return error_mark_node;
23718 /* Parse an Objective-C message expression.
23720 objc-message-expression:
23721 [ objc-message-receiver objc-message-args ]
23723 Returns a representation of an Objective-C message. */
23725 static tree
23726 cp_parser_objc_message_expression (cp_parser* parser)
23728 tree receiver, messageargs;
23730 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
23731 receiver = cp_parser_objc_message_receiver (parser);
23732 messageargs = cp_parser_objc_message_args (parser);
23733 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23735 return objc_build_message_expr (receiver, messageargs);
23738 /* Parse an objc-message-receiver.
23740 objc-message-receiver:
23741 expression
23742 simple-type-specifier
23744 Returns a representation of the type or expression. */
23746 static tree
23747 cp_parser_objc_message_receiver (cp_parser* parser)
23749 tree rcv;
23751 /* An Objective-C message receiver may be either (1) a type
23752 or (2) an expression. */
23753 cp_parser_parse_tentatively (parser);
23754 rcv = cp_parser_expression (parser, false, NULL);
23756 if (cp_parser_parse_definitely (parser))
23757 return rcv;
23759 rcv = cp_parser_simple_type_specifier (parser,
23760 /*decl_specs=*/NULL,
23761 CP_PARSER_FLAGS_NONE);
23763 return objc_get_class_reference (rcv);
23766 /* Parse the arguments and selectors comprising an Objective-C message.
23768 objc-message-args:
23769 objc-selector
23770 objc-selector-args
23771 objc-selector-args , objc-comma-args
23773 objc-selector-args:
23774 objc-selector [opt] : assignment-expression
23775 objc-selector-args objc-selector [opt] : assignment-expression
23777 objc-comma-args:
23778 assignment-expression
23779 objc-comma-args , assignment-expression
23781 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23782 selector arguments and TREE_VALUE containing a list of comma
23783 arguments. */
23785 static tree
23786 cp_parser_objc_message_args (cp_parser* parser)
23788 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23789 bool maybe_unary_selector_p = true;
23790 cp_token *token = cp_lexer_peek_token (parser->lexer);
23792 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23794 tree selector = NULL_TREE, arg;
23796 if (token->type != CPP_COLON)
23797 selector = cp_parser_objc_selector (parser);
23799 /* Detect if we have a unary selector. */
23800 if (maybe_unary_selector_p
23801 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23802 return build_tree_list (selector, NULL_TREE);
23804 maybe_unary_selector_p = false;
23805 cp_parser_require (parser, CPP_COLON, RT_COLON);
23806 arg = cp_parser_assignment_expression (parser, false, NULL);
23808 sel_args
23809 = chainon (sel_args,
23810 build_tree_list (selector, arg));
23812 token = cp_lexer_peek_token (parser->lexer);
23815 /* Handle non-selector arguments, if any. */
23816 while (token->type == CPP_COMMA)
23818 tree arg;
23820 cp_lexer_consume_token (parser->lexer);
23821 arg = cp_parser_assignment_expression (parser, false, NULL);
23823 addl_args
23824 = chainon (addl_args,
23825 build_tree_list (NULL_TREE, arg));
23827 token = cp_lexer_peek_token (parser->lexer);
23830 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23832 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23833 return build_tree_list (error_mark_node, error_mark_node);
23836 return build_tree_list (sel_args, addl_args);
23839 /* Parse an Objective-C encode expression.
23841 objc-encode-expression:
23842 @encode objc-typename
23844 Returns an encoded representation of the type argument. */
23846 static tree
23847 cp_parser_objc_encode_expression (cp_parser* parser)
23849 tree type;
23850 cp_token *token;
23852 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
23853 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23854 token = cp_lexer_peek_token (parser->lexer);
23855 type = complete_type (cp_parser_type_id (parser));
23856 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23858 if (!type)
23860 error_at (token->location,
23861 "%<@encode%> must specify a type as an argument");
23862 return error_mark_node;
23865 /* This happens if we find @encode(T) (where T is a template
23866 typename or something dependent on a template typename) when
23867 parsing a template. In that case, we can't compile it
23868 immediately, but we rather create an AT_ENCODE_EXPR which will
23869 need to be instantiated when the template is used.
23871 if (dependent_type_p (type))
23873 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23874 TREE_READONLY (value) = 1;
23875 return value;
23878 return objc_build_encode_expr (type);
23881 /* Parse an Objective-C @defs expression. */
23883 static tree
23884 cp_parser_objc_defs_expression (cp_parser *parser)
23886 tree name;
23888 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
23889 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23890 name = cp_parser_identifier (parser);
23891 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23893 return objc_get_class_ivars (name);
23896 /* Parse an Objective-C protocol expression.
23898 objc-protocol-expression:
23899 @protocol ( identifier )
23901 Returns a representation of the protocol expression. */
23903 static tree
23904 cp_parser_objc_protocol_expression (cp_parser* parser)
23906 tree proto;
23908 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23909 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23910 proto = cp_parser_identifier (parser);
23911 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23913 return objc_build_protocol_expr (proto);
23916 /* Parse an Objective-C selector expression.
23918 objc-selector-expression:
23919 @selector ( objc-method-signature )
23921 objc-method-signature:
23922 objc-selector
23923 objc-selector-seq
23925 objc-selector-seq:
23926 objc-selector :
23927 objc-selector-seq objc-selector :
23929 Returns a representation of the method selector. */
23931 static tree
23932 cp_parser_objc_selector_expression (cp_parser* parser)
23934 tree sel_seq = NULL_TREE;
23935 bool maybe_unary_selector_p = true;
23936 cp_token *token;
23937 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23939 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
23940 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23941 token = cp_lexer_peek_token (parser->lexer);
23943 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23944 || token->type == CPP_SCOPE)
23946 tree selector = NULL_TREE;
23948 if (token->type != CPP_COLON
23949 || token->type == CPP_SCOPE)
23950 selector = cp_parser_objc_selector (parser);
23952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23953 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23955 /* Detect if we have a unary selector. */
23956 if (maybe_unary_selector_p)
23958 sel_seq = selector;
23959 goto finish_selector;
23961 else
23963 cp_parser_error (parser, "expected %<:%>");
23966 maybe_unary_selector_p = false;
23967 token = cp_lexer_consume_token (parser->lexer);
23969 if (token->type == CPP_SCOPE)
23971 sel_seq
23972 = chainon (sel_seq,
23973 build_tree_list (selector, NULL_TREE));
23974 sel_seq
23975 = chainon (sel_seq,
23976 build_tree_list (NULL_TREE, NULL_TREE));
23978 else
23979 sel_seq
23980 = chainon (sel_seq,
23981 build_tree_list (selector, NULL_TREE));
23983 token = cp_lexer_peek_token (parser->lexer);
23986 finish_selector:
23987 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23989 return objc_build_selector_expr (loc, sel_seq);
23992 /* Parse a list of identifiers.
23994 objc-identifier-list:
23995 identifier
23996 objc-identifier-list , identifier
23998 Returns a TREE_LIST of identifier nodes. */
24000 static tree
24001 cp_parser_objc_identifier_list (cp_parser* parser)
24003 tree identifier;
24004 tree list;
24005 cp_token *sep;
24007 identifier = cp_parser_identifier (parser);
24008 if (identifier == error_mark_node)
24009 return error_mark_node;
24011 list = build_tree_list (NULL_TREE, identifier);
24012 sep = cp_lexer_peek_token (parser->lexer);
24014 while (sep->type == CPP_COMMA)
24016 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24017 identifier = cp_parser_identifier (parser);
24018 if (identifier == error_mark_node)
24019 return list;
24021 list = chainon (list, build_tree_list (NULL_TREE,
24022 identifier));
24023 sep = cp_lexer_peek_token (parser->lexer);
24026 return list;
24029 /* Parse an Objective-C alias declaration.
24031 objc-alias-declaration:
24032 @compatibility_alias identifier identifier ;
24034 This function registers the alias mapping with the Objective-C front end.
24035 It returns nothing. */
24037 static void
24038 cp_parser_objc_alias_declaration (cp_parser* parser)
24040 tree alias, orig;
24042 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
24043 alias = cp_parser_identifier (parser);
24044 orig = cp_parser_identifier (parser);
24045 objc_declare_alias (alias, orig);
24046 cp_parser_consume_semicolon_at_end_of_statement (parser);
24049 /* Parse an Objective-C class forward-declaration.
24051 objc-class-declaration:
24052 @class objc-identifier-list ;
24054 The function registers the forward declarations with the Objective-C
24055 front end. It returns nothing. */
24057 static void
24058 cp_parser_objc_class_declaration (cp_parser* parser)
24060 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
24061 while (true)
24063 tree id;
24065 id = cp_parser_identifier (parser);
24066 if (id == error_mark_node)
24067 break;
24069 objc_declare_class (id);
24071 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24072 cp_lexer_consume_token (parser->lexer);
24073 else
24074 break;
24076 cp_parser_consume_semicolon_at_end_of_statement (parser);
24079 /* Parse a list of Objective-C protocol references.
24081 objc-protocol-refs-opt:
24082 objc-protocol-refs [opt]
24084 objc-protocol-refs:
24085 < objc-identifier-list >
24087 Returns a TREE_LIST of identifiers, if any. */
24089 static tree
24090 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
24092 tree protorefs = NULL_TREE;
24094 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
24096 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
24097 protorefs = cp_parser_objc_identifier_list (parser);
24098 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
24101 return protorefs;
24104 /* Parse a Objective-C visibility specification. */
24106 static void
24107 cp_parser_objc_visibility_spec (cp_parser* parser)
24109 cp_token *vis = cp_lexer_peek_token (parser->lexer);
24111 switch (vis->keyword)
24113 case RID_AT_PRIVATE:
24114 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
24115 break;
24116 case RID_AT_PROTECTED:
24117 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
24118 break;
24119 case RID_AT_PUBLIC:
24120 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
24121 break;
24122 case RID_AT_PACKAGE:
24123 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
24124 break;
24125 default:
24126 return;
24129 /* Eat '@private'/'@protected'/'@public'. */
24130 cp_lexer_consume_token (parser->lexer);
24133 /* Parse an Objective-C method type. Return 'true' if it is a class
24134 (+) method, and 'false' if it is an instance (-) method. */
24136 static inline bool
24137 cp_parser_objc_method_type (cp_parser* parser)
24139 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
24140 return true;
24141 else
24142 return false;
24145 /* Parse an Objective-C protocol qualifier. */
24147 static tree
24148 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
24150 tree quals = NULL_TREE, node;
24151 cp_token *token = cp_lexer_peek_token (parser->lexer);
24153 node = token->u.value;
24155 while (node && TREE_CODE (node) == IDENTIFIER_NODE
24156 && (node == ridpointers [(int) RID_IN]
24157 || node == ridpointers [(int) RID_OUT]
24158 || node == ridpointers [(int) RID_INOUT]
24159 || node == ridpointers [(int) RID_BYCOPY]
24160 || node == ridpointers [(int) RID_BYREF]
24161 || node == ridpointers [(int) RID_ONEWAY]))
24163 quals = tree_cons (NULL_TREE, node, quals);
24164 cp_lexer_consume_token (parser->lexer);
24165 token = cp_lexer_peek_token (parser->lexer);
24166 node = token->u.value;
24169 return quals;
24172 /* Parse an Objective-C typename. */
24174 static tree
24175 cp_parser_objc_typename (cp_parser* parser)
24177 tree type_name = NULL_TREE;
24179 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24181 tree proto_quals, cp_type = NULL_TREE;
24183 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24184 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
24186 /* An ObjC type name may consist of just protocol qualifiers, in which
24187 case the type shall default to 'id'. */
24188 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24190 cp_type = cp_parser_type_id (parser);
24192 /* If the type could not be parsed, an error has already
24193 been produced. For error recovery, behave as if it had
24194 not been specified, which will use the default type
24195 'id'. */
24196 if (cp_type == error_mark_node)
24198 cp_type = NULL_TREE;
24199 /* We need to skip to the closing parenthesis as
24200 cp_parser_type_id() does not seem to do it for
24201 us. */
24202 cp_parser_skip_to_closing_parenthesis (parser,
24203 /*recovering=*/true,
24204 /*or_comma=*/false,
24205 /*consume_paren=*/false);
24209 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24210 type_name = build_tree_list (proto_quals, cp_type);
24213 return type_name;
24216 /* Check to see if TYPE refers to an Objective-C selector name. */
24218 static bool
24219 cp_parser_objc_selector_p (enum cpp_ttype type)
24221 return (type == CPP_NAME || type == CPP_KEYWORD
24222 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
24223 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
24224 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
24225 || type == CPP_XOR || type == CPP_XOR_EQ);
24228 /* Parse an Objective-C selector. */
24230 static tree
24231 cp_parser_objc_selector (cp_parser* parser)
24233 cp_token *token = cp_lexer_consume_token (parser->lexer);
24235 if (!cp_parser_objc_selector_p (token->type))
24237 error_at (token->location, "invalid Objective-C++ selector name");
24238 return error_mark_node;
24241 /* C++ operator names are allowed to appear in ObjC selectors. */
24242 switch (token->type)
24244 case CPP_AND_AND: return get_identifier ("and");
24245 case CPP_AND_EQ: return get_identifier ("and_eq");
24246 case CPP_AND: return get_identifier ("bitand");
24247 case CPP_OR: return get_identifier ("bitor");
24248 case CPP_COMPL: return get_identifier ("compl");
24249 case CPP_NOT: return get_identifier ("not");
24250 case CPP_NOT_EQ: return get_identifier ("not_eq");
24251 case CPP_OR_OR: return get_identifier ("or");
24252 case CPP_OR_EQ: return get_identifier ("or_eq");
24253 case CPP_XOR: return get_identifier ("xor");
24254 case CPP_XOR_EQ: return get_identifier ("xor_eq");
24255 default: return token->u.value;
24259 /* Parse an Objective-C params list. */
24261 static tree
24262 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
24264 tree params = NULL_TREE;
24265 bool maybe_unary_selector_p = true;
24266 cp_token *token = cp_lexer_peek_token (parser->lexer);
24268 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24270 tree selector = NULL_TREE, type_name, identifier;
24271 tree parm_attr = NULL_TREE;
24273 if (token->keyword == RID_ATTRIBUTE)
24274 break;
24276 if (token->type != CPP_COLON)
24277 selector = cp_parser_objc_selector (parser);
24279 /* Detect if we have a unary selector. */
24280 if (maybe_unary_selector_p
24281 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24283 params = selector; /* Might be followed by attributes. */
24284 break;
24287 maybe_unary_selector_p = false;
24288 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24290 /* Something went quite wrong. There should be a colon
24291 here, but there is not. Stop parsing parameters. */
24292 break;
24294 type_name = cp_parser_objc_typename (parser);
24295 /* New ObjC allows attributes on parameters too. */
24296 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
24297 parm_attr = cp_parser_attributes_opt (parser);
24298 identifier = cp_parser_identifier (parser);
24300 params
24301 = chainon (params,
24302 objc_build_keyword_decl (selector,
24303 type_name,
24304 identifier,
24305 parm_attr));
24307 token = cp_lexer_peek_token (parser->lexer);
24310 if (params == NULL_TREE)
24312 cp_parser_error (parser, "objective-c++ method declaration is expected");
24313 return error_mark_node;
24316 /* We allow tail attributes for the method. */
24317 if (token->keyword == RID_ATTRIBUTE)
24319 *attributes = cp_parser_attributes_opt (parser);
24320 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24321 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24322 return params;
24323 cp_parser_error (parser,
24324 "method attributes must be specified at the end");
24325 return error_mark_node;
24328 if (params == NULL_TREE)
24330 cp_parser_error (parser, "objective-c++ method declaration is expected");
24331 return error_mark_node;
24333 return params;
24336 /* Parse the non-keyword Objective-C params. */
24338 static tree
24339 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
24340 tree* attributes)
24342 tree params = make_node (TREE_LIST);
24343 cp_token *token = cp_lexer_peek_token (parser->lexer);
24344 *ellipsisp = false; /* Initially, assume no ellipsis. */
24346 while (token->type == CPP_COMMA)
24348 cp_parameter_declarator *parmdecl;
24349 tree parm;
24351 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24352 token = cp_lexer_peek_token (parser->lexer);
24354 if (token->type == CPP_ELLIPSIS)
24356 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
24357 *ellipsisp = true;
24358 token = cp_lexer_peek_token (parser->lexer);
24359 break;
24362 /* TODO: parse attributes for tail parameters. */
24363 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
24364 parm = grokdeclarator (parmdecl->declarator,
24365 &parmdecl->decl_specifiers,
24366 PARM, /*initialized=*/0,
24367 /*attrlist=*/NULL);
24369 chainon (params, build_tree_list (NULL_TREE, parm));
24370 token = cp_lexer_peek_token (parser->lexer);
24373 /* We allow tail attributes for the method. */
24374 if (token->keyword == RID_ATTRIBUTE)
24376 if (*attributes == NULL_TREE)
24378 *attributes = cp_parser_attributes_opt (parser);
24379 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24380 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24381 return params;
24383 else
24384 /* We have an error, but parse the attributes, so that we can
24385 carry on. */
24386 *attributes = cp_parser_attributes_opt (parser);
24388 cp_parser_error (parser,
24389 "method attributes must be specified at the end");
24390 return error_mark_node;
24393 return params;
24396 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
24398 static void
24399 cp_parser_objc_interstitial_code (cp_parser* parser)
24401 cp_token *token = cp_lexer_peek_token (parser->lexer);
24403 /* If the next token is `extern' and the following token is a string
24404 literal, then we have a linkage specification. */
24405 if (token->keyword == RID_EXTERN
24406 && cp_parser_is_pure_string_literal
24407 (cp_lexer_peek_nth_token (parser->lexer, 2)))
24408 cp_parser_linkage_specification (parser);
24409 /* Handle #pragma, if any. */
24410 else if (token->type == CPP_PRAGMA)
24411 cp_parser_pragma (parser, pragma_external);
24412 /* Allow stray semicolons. */
24413 else if (token->type == CPP_SEMICOLON)
24414 cp_lexer_consume_token (parser->lexer);
24415 /* Mark methods as optional or required, when building protocols. */
24416 else if (token->keyword == RID_AT_OPTIONAL)
24418 cp_lexer_consume_token (parser->lexer);
24419 objc_set_method_opt (true);
24421 else if (token->keyword == RID_AT_REQUIRED)
24423 cp_lexer_consume_token (parser->lexer);
24424 objc_set_method_opt (false);
24426 else if (token->keyword == RID_NAMESPACE)
24427 cp_parser_namespace_definition (parser);
24428 /* Other stray characters must generate errors. */
24429 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
24431 cp_lexer_consume_token (parser->lexer);
24432 error ("stray %qs between Objective-C++ methods",
24433 token->type == CPP_OPEN_BRACE ? "{" : "}");
24435 /* Finally, try to parse a block-declaration, or a function-definition. */
24436 else
24437 cp_parser_block_declaration (parser, /*statement_p=*/false);
24440 /* Parse a method signature. */
24442 static tree
24443 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
24445 tree rettype, kwdparms, optparms;
24446 bool ellipsis = false;
24447 bool is_class_method;
24449 is_class_method = cp_parser_objc_method_type (parser);
24450 rettype = cp_parser_objc_typename (parser);
24451 *attributes = NULL_TREE;
24452 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
24453 if (kwdparms == error_mark_node)
24454 return error_mark_node;
24455 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
24456 if (optparms == error_mark_node)
24457 return error_mark_node;
24459 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
24462 static bool
24463 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
24465 tree tattr;
24466 cp_lexer_save_tokens (parser->lexer);
24467 tattr = cp_parser_attributes_opt (parser);
24468 gcc_assert (tattr) ;
24470 /* If the attributes are followed by a method introducer, this is not allowed.
24471 Dump the attributes and flag the situation. */
24472 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
24473 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
24474 return true;
24476 /* Otherwise, the attributes introduce some interstitial code, possibly so
24477 rewind to allow that check. */
24478 cp_lexer_rollback_tokens (parser->lexer);
24479 return false;
24482 /* Parse an Objective-C method prototype list. */
24484 static void
24485 cp_parser_objc_method_prototype_list (cp_parser* parser)
24487 cp_token *token = cp_lexer_peek_token (parser->lexer);
24489 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24491 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24493 tree attributes, sig;
24494 bool is_class_method;
24495 if (token->type == CPP_PLUS)
24496 is_class_method = true;
24497 else
24498 is_class_method = false;
24499 sig = cp_parser_objc_method_signature (parser, &attributes);
24500 if (sig == error_mark_node)
24502 cp_parser_skip_to_end_of_block_or_statement (parser);
24503 token = cp_lexer_peek_token (parser->lexer);
24504 continue;
24506 objc_add_method_declaration (is_class_method, sig, attributes);
24507 cp_parser_consume_semicolon_at_end_of_statement (parser);
24509 else if (token->keyword == RID_AT_PROPERTY)
24510 cp_parser_objc_at_property_declaration (parser);
24511 else if (token->keyword == RID_ATTRIBUTE
24512 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24513 warning_at (cp_lexer_peek_token (parser->lexer)->location,
24514 OPT_Wattributes,
24515 "prefix attributes are ignored for methods");
24516 else
24517 /* Allow for interspersed non-ObjC++ code. */
24518 cp_parser_objc_interstitial_code (parser);
24520 token = cp_lexer_peek_token (parser->lexer);
24523 if (token->type != CPP_EOF)
24524 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24525 else
24526 cp_parser_error (parser, "expected %<@end%>");
24528 objc_finish_interface ();
24531 /* Parse an Objective-C method definition list. */
24533 static void
24534 cp_parser_objc_method_definition_list (cp_parser* parser)
24536 cp_token *token = cp_lexer_peek_token (parser->lexer);
24538 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24540 tree meth;
24542 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24544 cp_token *ptk;
24545 tree sig, attribute;
24546 bool is_class_method;
24547 if (token->type == CPP_PLUS)
24548 is_class_method = true;
24549 else
24550 is_class_method = false;
24551 push_deferring_access_checks (dk_deferred);
24552 sig = cp_parser_objc_method_signature (parser, &attribute);
24553 if (sig == error_mark_node)
24555 cp_parser_skip_to_end_of_block_or_statement (parser);
24556 token = cp_lexer_peek_token (parser->lexer);
24557 continue;
24559 objc_start_method_definition (is_class_method, sig, attribute,
24560 NULL_TREE);
24562 /* For historical reasons, we accept an optional semicolon. */
24563 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24564 cp_lexer_consume_token (parser->lexer);
24566 ptk = cp_lexer_peek_token (parser->lexer);
24567 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
24568 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
24570 perform_deferred_access_checks (tf_warning_or_error);
24571 stop_deferring_access_checks ();
24572 meth = cp_parser_function_definition_after_declarator (parser,
24573 false);
24574 pop_deferring_access_checks ();
24575 objc_finish_method_definition (meth);
24578 /* The following case will be removed once @synthesize is
24579 completely implemented. */
24580 else if (token->keyword == RID_AT_PROPERTY)
24581 cp_parser_objc_at_property_declaration (parser);
24582 else if (token->keyword == RID_AT_SYNTHESIZE)
24583 cp_parser_objc_at_synthesize_declaration (parser);
24584 else if (token->keyword == RID_AT_DYNAMIC)
24585 cp_parser_objc_at_dynamic_declaration (parser);
24586 else if (token->keyword == RID_ATTRIBUTE
24587 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24588 warning_at (token->location, OPT_Wattributes,
24589 "prefix attributes are ignored for methods");
24590 else
24591 /* Allow for interspersed non-ObjC++ code. */
24592 cp_parser_objc_interstitial_code (parser);
24594 token = cp_lexer_peek_token (parser->lexer);
24597 if (token->type != CPP_EOF)
24598 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24599 else
24600 cp_parser_error (parser, "expected %<@end%>");
24602 objc_finish_implementation ();
24605 /* Parse Objective-C ivars. */
24607 static void
24608 cp_parser_objc_class_ivars (cp_parser* parser)
24610 cp_token *token = cp_lexer_peek_token (parser->lexer);
24612 if (token->type != CPP_OPEN_BRACE)
24613 return; /* No ivars specified. */
24615 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
24616 token = cp_lexer_peek_token (parser->lexer);
24618 while (token->type != CPP_CLOSE_BRACE
24619 && token->keyword != RID_AT_END && token->type != CPP_EOF)
24621 cp_decl_specifier_seq declspecs;
24622 int decl_class_or_enum_p;
24623 tree prefix_attributes;
24625 cp_parser_objc_visibility_spec (parser);
24627 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24628 break;
24630 cp_parser_decl_specifier_seq (parser,
24631 CP_PARSER_FLAGS_OPTIONAL,
24632 &declspecs,
24633 &decl_class_or_enum_p);
24635 /* auto, register, static, extern, mutable. */
24636 if (declspecs.storage_class != sc_none)
24638 cp_parser_error (parser, "invalid type for instance variable");
24639 declspecs.storage_class = sc_none;
24642 /* thread_local. */
24643 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24645 cp_parser_error (parser, "invalid type for instance variable");
24646 declspecs.locations[ds_thread] = 0;
24649 /* typedef. */
24650 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24652 cp_parser_error (parser, "invalid type for instance variable");
24653 declspecs.locations[ds_typedef] = 0;
24656 prefix_attributes = declspecs.attributes;
24657 declspecs.attributes = NULL_TREE;
24659 /* Keep going until we hit the `;' at the end of the
24660 declaration. */
24661 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24663 tree width = NULL_TREE, attributes, first_attribute, decl;
24664 cp_declarator *declarator = NULL;
24665 int ctor_dtor_or_conv_p;
24667 /* Check for a (possibly unnamed) bitfield declaration. */
24668 token = cp_lexer_peek_token (parser->lexer);
24669 if (token->type == CPP_COLON)
24670 goto eat_colon;
24672 if (token->type == CPP_NAME
24673 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24674 == CPP_COLON))
24676 /* Get the name of the bitfield. */
24677 declarator = make_id_declarator (NULL_TREE,
24678 cp_parser_identifier (parser),
24679 sfk_none);
24681 eat_colon:
24682 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24683 /* Get the width of the bitfield. */
24684 width
24685 = cp_parser_constant_expression (parser,
24686 /*allow_non_constant=*/false,
24687 NULL);
24689 else
24691 /* Parse the declarator. */
24692 declarator
24693 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24694 &ctor_dtor_or_conv_p,
24695 /*parenthesized_p=*/NULL,
24696 /*member_p=*/false);
24699 /* Look for attributes that apply to the ivar. */
24700 attributes = cp_parser_attributes_opt (parser);
24701 /* Remember which attributes are prefix attributes and
24702 which are not. */
24703 first_attribute = attributes;
24704 /* Combine the attributes. */
24705 attributes = chainon (prefix_attributes, attributes);
24707 if (width)
24708 /* Create the bitfield declaration. */
24709 decl = grokbitfield (declarator, &declspecs,
24710 width,
24711 attributes);
24712 else
24713 decl = grokfield (declarator, &declspecs,
24714 NULL_TREE, /*init_const_expr_p=*/false,
24715 NULL_TREE, attributes);
24717 /* Add the instance variable. */
24718 if (decl != error_mark_node && decl != NULL_TREE)
24719 objc_add_instance_variable (decl);
24721 /* Reset PREFIX_ATTRIBUTES. */
24722 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24723 attributes = TREE_CHAIN (attributes);
24724 if (attributes)
24725 TREE_CHAIN (attributes) = NULL_TREE;
24727 token = cp_lexer_peek_token (parser->lexer);
24729 if (token->type == CPP_COMMA)
24731 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24732 continue;
24734 break;
24737 cp_parser_consume_semicolon_at_end_of_statement (parser);
24738 token = cp_lexer_peek_token (parser->lexer);
24741 if (token->keyword == RID_AT_END)
24742 cp_parser_error (parser, "expected %<}%>");
24744 /* Do not consume the RID_AT_END, so it will be read again as terminating
24745 the @interface of @implementation. */
24746 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24747 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
24749 /* For historical reasons, we accept an optional semicolon. */
24750 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24751 cp_lexer_consume_token (parser->lexer);
24754 /* Parse an Objective-C protocol declaration. */
24756 static void
24757 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24759 tree proto, protorefs;
24760 cp_token *tok;
24762 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24763 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24765 tok = cp_lexer_peek_token (parser->lexer);
24766 error_at (tok->location, "identifier expected after %<@protocol%>");
24767 cp_parser_consume_semicolon_at_end_of_statement (parser);
24768 return;
24771 /* See if we have a forward declaration or a definition. */
24772 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24774 /* Try a forward declaration first. */
24775 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24777 while (true)
24779 tree id;
24781 id = cp_parser_identifier (parser);
24782 if (id == error_mark_node)
24783 break;
24785 objc_declare_protocol (id, attributes);
24787 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24788 cp_lexer_consume_token (parser->lexer);
24789 else
24790 break;
24792 cp_parser_consume_semicolon_at_end_of_statement (parser);
24795 /* Ok, we got a full-fledged definition (or at least should). */
24796 else
24798 proto = cp_parser_identifier (parser);
24799 protorefs = cp_parser_objc_protocol_refs_opt (parser);
24800 objc_start_protocol (proto, protorefs, attributes);
24801 cp_parser_objc_method_prototype_list (parser);
24805 /* Parse an Objective-C superclass or category. */
24807 static void
24808 cp_parser_objc_superclass_or_category (cp_parser *parser,
24809 bool iface_p,
24810 tree *super,
24811 tree *categ, bool *is_class_extension)
24813 cp_token *next = cp_lexer_peek_token (parser->lexer);
24815 *super = *categ = NULL_TREE;
24816 *is_class_extension = false;
24817 if (next->type == CPP_COLON)
24819 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24820 *super = cp_parser_identifier (parser);
24822 else if (next->type == CPP_OPEN_PAREN)
24824 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24826 /* If there is no category name, and this is an @interface, we
24827 have a class extension. */
24828 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24830 *categ = NULL_TREE;
24831 *is_class_extension = true;
24833 else
24834 *categ = cp_parser_identifier (parser);
24836 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24840 /* Parse an Objective-C class interface. */
24842 static void
24843 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24845 tree name, super, categ, protos;
24846 bool is_class_extension;
24848 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
24849 name = cp_parser_identifier (parser);
24850 if (name == error_mark_node)
24852 /* It's hard to recover because even if valid @interface stuff
24853 is to follow, we can't compile it (or validate it) if we
24854 don't even know which class it refers to. Let's assume this
24855 was a stray '@interface' token in the stream and skip it.
24857 return;
24859 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24860 &is_class_extension);
24861 protos = cp_parser_objc_protocol_refs_opt (parser);
24863 /* We have either a class or a category on our hands. */
24864 if (categ || is_class_extension)
24865 objc_start_category_interface (name, categ, protos, attributes);
24866 else
24868 objc_start_class_interface (name, super, protos, attributes);
24869 /* Handle instance variable declarations, if any. */
24870 cp_parser_objc_class_ivars (parser);
24871 objc_continue_interface ();
24874 cp_parser_objc_method_prototype_list (parser);
24877 /* Parse an Objective-C class implementation. */
24879 static void
24880 cp_parser_objc_class_implementation (cp_parser* parser)
24882 tree name, super, categ;
24883 bool is_class_extension;
24885 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
24886 name = cp_parser_identifier (parser);
24887 if (name == error_mark_node)
24889 /* It's hard to recover because even if valid @implementation
24890 stuff is to follow, we can't compile it (or validate it) if
24891 we don't even know which class it refers to. Let's assume
24892 this was a stray '@implementation' token in the stream and
24893 skip it.
24895 return;
24897 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24898 &is_class_extension);
24900 /* We have either a class or a category on our hands. */
24901 if (categ)
24902 objc_start_category_implementation (name, categ);
24903 else
24905 objc_start_class_implementation (name, super);
24906 /* Handle instance variable declarations, if any. */
24907 cp_parser_objc_class_ivars (parser);
24908 objc_continue_implementation ();
24911 cp_parser_objc_method_definition_list (parser);
24914 /* Consume the @end token and finish off the implementation. */
24916 static void
24917 cp_parser_objc_end_implementation (cp_parser* parser)
24919 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24920 objc_finish_implementation ();
24923 /* Parse an Objective-C declaration. */
24925 static void
24926 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24928 /* Try to figure out what kind of declaration is present. */
24929 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24931 if (attributes)
24932 switch (kwd->keyword)
24934 case RID_AT_ALIAS:
24935 case RID_AT_CLASS:
24936 case RID_AT_END:
24937 error_at (kwd->location, "attributes may not be specified before"
24938 " the %<@%D%> Objective-C++ keyword",
24939 kwd->u.value);
24940 attributes = NULL;
24941 break;
24942 case RID_AT_IMPLEMENTATION:
24943 warning_at (kwd->location, OPT_Wattributes,
24944 "prefix attributes are ignored before %<@%D%>",
24945 kwd->u.value);
24946 attributes = NULL;
24947 default:
24948 break;
24951 switch (kwd->keyword)
24953 case RID_AT_ALIAS:
24954 cp_parser_objc_alias_declaration (parser);
24955 break;
24956 case RID_AT_CLASS:
24957 cp_parser_objc_class_declaration (parser);
24958 break;
24959 case RID_AT_PROTOCOL:
24960 cp_parser_objc_protocol_declaration (parser, attributes);
24961 break;
24962 case RID_AT_INTERFACE:
24963 cp_parser_objc_class_interface (parser, attributes);
24964 break;
24965 case RID_AT_IMPLEMENTATION:
24966 cp_parser_objc_class_implementation (parser);
24967 break;
24968 case RID_AT_END:
24969 cp_parser_objc_end_implementation (parser);
24970 break;
24971 default:
24972 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24973 kwd->u.value);
24974 cp_parser_skip_to_end_of_block_or_statement (parser);
24978 /* Parse an Objective-C try-catch-finally statement.
24980 objc-try-catch-finally-stmt:
24981 @try compound-statement objc-catch-clause-seq [opt]
24982 objc-finally-clause [opt]
24984 objc-catch-clause-seq:
24985 objc-catch-clause objc-catch-clause-seq [opt]
24987 objc-catch-clause:
24988 @catch ( objc-exception-declaration ) compound-statement
24990 objc-finally-clause:
24991 @finally compound-statement
24993 objc-exception-declaration:
24994 parameter-declaration
24995 '...'
24997 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24999 Returns NULL_TREE.
25001 PS: This function is identical to c_parser_objc_try_catch_finally_statement
25002 for C. Keep them in sync. */
25004 static tree
25005 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
25007 location_t location;
25008 tree stmt;
25010 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
25011 location = cp_lexer_peek_token (parser->lexer)->location;
25012 objc_maybe_warn_exceptions (location);
25013 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
25014 node, lest it get absorbed into the surrounding block. */
25015 stmt = push_stmt_list ();
25016 cp_parser_compound_statement (parser, NULL, false, false);
25017 objc_begin_try_stmt (location, pop_stmt_list (stmt));
25019 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
25021 cp_parameter_declarator *parm;
25022 tree parameter_declaration = error_mark_node;
25023 bool seen_open_paren = false;
25025 cp_lexer_consume_token (parser->lexer);
25026 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25027 seen_open_paren = true;
25028 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25030 /* We have "@catch (...)" (where the '...' are literally
25031 what is in the code). Skip the '...'.
25032 parameter_declaration is set to NULL_TREE, and
25033 objc_being_catch_clauses() knows that that means
25034 '...'. */
25035 cp_lexer_consume_token (parser->lexer);
25036 parameter_declaration = NULL_TREE;
25038 else
25040 /* We have "@catch (NSException *exception)" or something
25041 like that. Parse the parameter declaration. */
25042 parm = cp_parser_parameter_declaration (parser, false, NULL);
25043 if (parm == NULL)
25044 parameter_declaration = error_mark_node;
25045 else
25046 parameter_declaration = grokdeclarator (parm->declarator,
25047 &parm->decl_specifiers,
25048 PARM, /*initialized=*/0,
25049 /*attrlist=*/NULL);
25051 if (seen_open_paren)
25052 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25053 else
25055 /* If there was no open parenthesis, we are recovering from
25056 an error, and we are trying to figure out what mistake
25057 the user has made. */
25059 /* If there is an immediate closing parenthesis, the user
25060 probably forgot the opening one (ie, they typed "@catch
25061 NSException *e)". Parse the closing parenthesis and keep
25062 going. */
25063 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25064 cp_lexer_consume_token (parser->lexer);
25066 /* If these is no immediate closing parenthesis, the user
25067 probably doesn't know that parenthesis are required at
25068 all (ie, they typed "@catch NSException *e"). So, just
25069 forget about the closing parenthesis and keep going. */
25071 objc_begin_catch_clause (parameter_declaration);
25072 cp_parser_compound_statement (parser, NULL, false, false);
25073 objc_finish_catch_clause ();
25075 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
25077 cp_lexer_consume_token (parser->lexer);
25078 location = cp_lexer_peek_token (parser->lexer)->location;
25079 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
25080 node, lest it get absorbed into the surrounding block. */
25081 stmt = push_stmt_list ();
25082 cp_parser_compound_statement (parser, NULL, false, false);
25083 objc_build_finally_clause (location, pop_stmt_list (stmt));
25086 return objc_finish_try_stmt ();
25089 /* Parse an Objective-C synchronized statement.
25091 objc-synchronized-stmt:
25092 @synchronized ( expression ) compound-statement
25094 Returns NULL_TREE. */
25096 static tree
25097 cp_parser_objc_synchronized_statement (cp_parser *parser)
25099 location_t location;
25100 tree lock, stmt;
25102 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
25104 location = cp_lexer_peek_token (parser->lexer)->location;
25105 objc_maybe_warn_exceptions (location);
25106 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25107 lock = cp_parser_expression (parser, false, NULL);
25108 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25110 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
25111 node, lest it get absorbed into the surrounding block. */
25112 stmt = push_stmt_list ();
25113 cp_parser_compound_statement (parser, NULL, false, false);
25115 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
25118 /* Parse an Objective-C throw statement.
25120 objc-throw-stmt:
25121 @throw assignment-expression [opt] ;
25123 Returns a constructed '@throw' statement. */
25125 static tree
25126 cp_parser_objc_throw_statement (cp_parser *parser)
25128 tree expr = NULL_TREE;
25129 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25131 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
25133 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25134 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25136 cp_parser_consume_semicolon_at_end_of_statement (parser);
25138 return objc_build_throw_stmt (loc, expr);
25141 /* Parse an Objective-C statement. */
25143 static tree
25144 cp_parser_objc_statement (cp_parser * parser)
25146 /* Try to figure out what kind of declaration is present. */
25147 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25149 switch (kwd->keyword)
25151 case RID_AT_TRY:
25152 return cp_parser_objc_try_catch_finally_statement (parser);
25153 case RID_AT_SYNCHRONIZED:
25154 return cp_parser_objc_synchronized_statement (parser);
25155 case RID_AT_THROW:
25156 return cp_parser_objc_throw_statement (parser);
25157 default:
25158 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25159 kwd->u.value);
25160 cp_parser_skip_to_end_of_block_or_statement (parser);
25163 return error_mark_node;
25166 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
25167 look ahead to see if an objc keyword follows the attributes. This
25168 is to detect the use of prefix attributes on ObjC @interface and
25169 @protocol. */
25171 static bool
25172 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
25174 cp_lexer_save_tokens (parser->lexer);
25175 *attrib = cp_parser_attributes_opt (parser);
25176 gcc_assert (*attrib);
25177 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
25179 cp_lexer_commit_tokens (parser->lexer);
25180 return true;
25182 cp_lexer_rollback_tokens (parser->lexer);
25183 return false;
25186 /* This routine is a minimal replacement for
25187 c_parser_struct_declaration () used when parsing the list of
25188 types/names or ObjC++ properties. For example, when parsing the
25189 code
25191 @property (readonly) int a, b, c;
25193 this function is responsible for parsing "int a, int b, int c" and
25194 returning the declarations as CHAIN of DECLs.
25196 TODO: Share this code with cp_parser_objc_class_ivars. It's very
25197 similar parsing. */
25198 static tree
25199 cp_parser_objc_struct_declaration (cp_parser *parser)
25201 tree decls = NULL_TREE;
25202 cp_decl_specifier_seq declspecs;
25203 int decl_class_or_enum_p;
25204 tree prefix_attributes;
25206 cp_parser_decl_specifier_seq (parser,
25207 CP_PARSER_FLAGS_NONE,
25208 &declspecs,
25209 &decl_class_or_enum_p);
25211 if (declspecs.type == error_mark_node)
25212 return error_mark_node;
25214 /* auto, register, static, extern, mutable. */
25215 if (declspecs.storage_class != sc_none)
25217 cp_parser_error (parser, "invalid type for property");
25218 declspecs.storage_class = sc_none;
25221 /* thread_local. */
25222 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
25224 cp_parser_error (parser, "invalid type for property");
25225 declspecs.locations[ds_thread] = 0;
25228 /* typedef. */
25229 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
25231 cp_parser_error (parser, "invalid type for property");
25232 declspecs.locations[ds_typedef] = 0;
25235 prefix_attributes = declspecs.attributes;
25236 declspecs.attributes = NULL_TREE;
25238 /* Keep going until we hit the `;' at the end of the declaration. */
25239 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25241 tree attributes, first_attribute, decl;
25242 cp_declarator *declarator;
25243 cp_token *token;
25245 /* Parse the declarator. */
25246 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25247 NULL, NULL, false);
25249 /* Look for attributes that apply to the ivar. */
25250 attributes = cp_parser_attributes_opt (parser);
25251 /* Remember which attributes are prefix attributes and
25252 which are not. */
25253 first_attribute = attributes;
25254 /* Combine the attributes. */
25255 attributes = chainon (prefix_attributes, attributes);
25257 decl = grokfield (declarator, &declspecs,
25258 NULL_TREE, /*init_const_expr_p=*/false,
25259 NULL_TREE, attributes);
25261 if (decl == error_mark_node || decl == NULL_TREE)
25262 return error_mark_node;
25264 /* Reset PREFIX_ATTRIBUTES. */
25265 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25266 attributes = TREE_CHAIN (attributes);
25267 if (attributes)
25268 TREE_CHAIN (attributes) = NULL_TREE;
25270 DECL_CHAIN (decl) = decls;
25271 decls = decl;
25273 token = cp_lexer_peek_token (parser->lexer);
25274 if (token->type == CPP_COMMA)
25276 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25277 continue;
25279 else
25280 break;
25282 return decls;
25285 /* Parse an Objective-C @property declaration. The syntax is:
25287 objc-property-declaration:
25288 '@property' objc-property-attributes[opt] struct-declaration ;
25290 objc-property-attributes:
25291 '(' objc-property-attribute-list ')'
25293 objc-property-attribute-list:
25294 objc-property-attribute
25295 objc-property-attribute-list, objc-property-attribute
25297 objc-property-attribute
25298 'getter' = identifier
25299 'setter' = identifier
25300 'readonly'
25301 'readwrite'
25302 'assign'
25303 'retain'
25304 'copy'
25305 'nonatomic'
25307 For example:
25308 @property NSString *name;
25309 @property (readonly) id object;
25310 @property (retain, nonatomic, getter=getTheName) id name;
25311 @property int a, b, c;
25313 PS: This function is identical to
25314 c_parser_objc_at_property_declaration for C. Keep them in sync. */
25315 static void
25316 cp_parser_objc_at_property_declaration (cp_parser *parser)
25318 /* The following variables hold the attributes of the properties as
25319 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
25320 seen. When we see an attribute, we set them to 'true' (if they
25321 are boolean properties) or to the identifier (if they have an
25322 argument, ie, for getter and setter). Note that here we only
25323 parse the list of attributes, check the syntax and accumulate the
25324 attributes that we find. objc_add_property_declaration() will
25325 then process the information. */
25326 bool property_assign = false;
25327 bool property_copy = false;
25328 tree property_getter_ident = NULL_TREE;
25329 bool property_nonatomic = false;
25330 bool property_readonly = false;
25331 bool property_readwrite = false;
25332 bool property_retain = false;
25333 tree property_setter_ident = NULL_TREE;
25335 /* 'properties' is the list of properties that we read. Usually a
25336 single one, but maybe more (eg, in "@property int a, b, c;" there
25337 are three). */
25338 tree properties;
25339 location_t loc;
25341 loc = cp_lexer_peek_token (parser->lexer)->location;
25343 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
25345 /* Parse the optional attribute list... */
25346 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25348 /* Eat the '('. */
25349 cp_lexer_consume_token (parser->lexer);
25351 while (true)
25353 bool syntax_error = false;
25354 cp_token *token = cp_lexer_peek_token (parser->lexer);
25355 enum rid keyword;
25357 if (token->type != CPP_NAME)
25359 cp_parser_error (parser, "expected identifier");
25360 break;
25362 keyword = C_RID_CODE (token->u.value);
25363 cp_lexer_consume_token (parser->lexer);
25364 switch (keyword)
25366 case RID_ASSIGN: property_assign = true; break;
25367 case RID_COPY: property_copy = true; break;
25368 case RID_NONATOMIC: property_nonatomic = true; break;
25369 case RID_READONLY: property_readonly = true; break;
25370 case RID_READWRITE: property_readwrite = true; break;
25371 case RID_RETAIN: property_retain = true; break;
25373 case RID_GETTER:
25374 case RID_SETTER:
25375 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
25377 if (keyword == RID_GETTER)
25378 cp_parser_error (parser,
25379 "missing %<=%> (after %<getter%> attribute)");
25380 else
25381 cp_parser_error (parser,
25382 "missing %<=%> (after %<setter%> attribute)");
25383 syntax_error = true;
25384 break;
25386 cp_lexer_consume_token (parser->lexer); /* eat the = */
25387 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
25389 cp_parser_error (parser, "expected identifier");
25390 syntax_error = true;
25391 break;
25393 if (keyword == RID_SETTER)
25395 if (property_setter_ident != NULL_TREE)
25397 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
25398 cp_lexer_consume_token (parser->lexer);
25400 else
25401 property_setter_ident = cp_parser_objc_selector (parser);
25402 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25403 cp_parser_error (parser, "setter name must terminate with %<:%>");
25404 else
25405 cp_lexer_consume_token (parser->lexer);
25407 else
25409 if (property_getter_ident != NULL_TREE)
25411 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
25412 cp_lexer_consume_token (parser->lexer);
25414 else
25415 property_getter_ident = cp_parser_objc_selector (parser);
25417 break;
25418 default:
25419 cp_parser_error (parser, "unknown property attribute");
25420 syntax_error = true;
25421 break;
25424 if (syntax_error)
25425 break;
25427 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25428 cp_lexer_consume_token (parser->lexer);
25429 else
25430 break;
25433 /* FIXME: "@property (setter, assign);" will generate a spurious
25434 "error: expected ‘)’ before ‘,’ token". This is because
25435 cp_parser_require, unlike the C counterpart, will produce an
25436 error even if we are in error recovery. */
25437 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25439 cp_parser_skip_to_closing_parenthesis (parser,
25440 /*recovering=*/true,
25441 /*or_comma=*/false,
25442 /*consume_paren=*/true);
25446 /* ... and the property declaration(s). */
25447 properties = cp_parser_objc_struct_declaration (parser);
25449 if (properties == error_mark_node)
25451 cp_parser_skip_to_end_of_statement (parser);
25452 /* If the next token is now a `;', consume it. */
25453 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25454 cp_lexer_consume_token (parser->lexer);
25455 return;
25458 if (properties == NULL_TREE)
25459 cp_parser_error (parser, "expected identifier");
25460 else
25462 /* Comma-separated properties are chained together in
25463 reverse order; add them one by one. */
25464 properties = nreverse (properties);
25466 for (; properties; properties = TREE_CHAIN (properties))
25467 objc_add_property_declaration (loc, copy_node (properties),
25468 property_readonly, property_readwrite,
25469 property_assign, property_retain,
25470 property_copy, property_nonatomic,
25471 property_getter_ident, property_setter_ident);
25474 cp_parser_consume_semicolon_at_end_of_statement (parser);
25477 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
25479 objc-synthesize-declaration:
25480 @synthesize objc-synthesize-identifier-list ;
25482 objc-synthesize-identifier-list:
25483 objc-synthesize-identifier
25484 objc-synthesize-identifier-list, objc-synthesize-identifier
25486 objc-synthesize-identifier
25487 identifier
25488 identifier = identifier
25490 For example:
25491 @synthesize MyProperty;
25492 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
25494 PS: This function is identical to c_parser_objc_at_synthesize_declaration
25495 for C. Keep them in sync.
25497 static void
25498 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
25500 tree list = NULL_TREE;
25501 location_t loc;
25502 loc = cp_lexer_peek_token (parser->lexer)->location;
25504 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
25505 while (true)
25507 tree property, ivar;
25508 property = cp_parser_identifier (parser);
25509 if (property == error_mark_node)
25511 cp_parser_consume_semicolon_at_end_of_statement (parser);
25512 return;
25514 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25516 cp_lexer_consume_token (parser->lexer);
25517 ivar = cp_parser_identifier (parser);
25518 if (ivar == error_mark_node)
25520 cp_parser_consume_semicolon_at_end_of_statement (parser);
25521 return;
25524 else
25525 ivar = NULL_TREE;
25526 list = chainon (list, build_tree_list (ivar, property));
25527 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25528 cp_lexer_consume_token (parser->lexer);
25529 else
25530 break;
25532 cp_parser_consume_semicolon_at_end_of_statement (parser);
25533 objc_add_synthesize_declaration (loc, list);
25536 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
25538 objc-dynamic-declaration:
25539 @dynamic identifier-list ;
25541 For example:
25542 @dynamic MyProperty;
25543 @dynamic MyProperty, AnotherProperty;
25545 PS: This function is identical to c_parser_objc_at_dynamic_declaration
25546 for C. Keep them in sync.
25548 static void
25549 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
25551 tree list = NULL_TREE;
25552 location_t loc;
25553 loc = cp_lexer_peek_token (parser->lexer)->location;
25555 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
25556 while (true)
25558 tree property;
25559 property = cp_parser_identifier (parser);
25560 if (property == error_mark_node)
25562 cp_parser_consume_semicolon_at_end_of_statement (parser);
25563 return;
25565 list = chainon (list, build_tree_list (NULL, property));
25566 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25567 cp_lexer_consume_token (parser->lexer);
25568 else
25569 break;
25571 cp_parser_consume_semicolon_at_end_of_statement (parser);
25572 objc_add_dynamic_declaration (loc, list);
25576 /* OpenMP 2.5 parsing routines. */
25578 /* Returns name of the next clause.
25579 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
25580 the token is not consumed. Otherwise appropriate pragma_omp_clause is
25581 returned and the token is consumed. */
25583 static pragma_omp_clause
25584 cp_parser_omp_clause_name (cp_parser *parser)
25586 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
25588 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
25589 result = PRAGMA_OMP_CLAUSE_IF;
25590 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
25591 result = PRAGMA_OMP_CLAUSE_DEFAULT;
25592 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
25593 result = PRAGMA_OMP_CLAUSE_PRIVATE;
25594 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25596 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25597 const char *p = IDENTIFIER_POINTER (id);
25599 switch (p[0])
25601 case 'c':
25602 if (!strcmp ("collapse", p))
25603 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
25604 else if (!strcmp ("copyin", p))
25605 result = PRAGMA_OMP_CLAUSE_COPYIN;
25606 else if (!strcmp ("copyprivate", p))
25607 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
25608 break;
25609 case 'f':
25610 if (!strcmp ("final", p))
25611 result = PRAGMA_OMP_CLAUSE_FINAL;
25612 else if (!strcmp ("firstprivate", p))
25613 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
25614 break;
25615 case 'l':
25616 if (!strcmp ("lastprivate", p))
25617 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
25618 break;
25619 case 'm':
25620 if (!strcmp ("mergeable", p))
25621 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
25622 break;
25623 case 'n':
25624 if (!strcmp ("nowait", p))
25625 result = PRAGMA_OMP_CLAUSE_NOWAIT;
25626 else if (!strcmp ("num_threads", p))
25627 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
25628 break;
25629 case 'o':
25630 if (!strcmp ("ordered", p))
25631 result = PRAGMA_OMP_CLAUSE_ORDERED;
25632 break;
25633 case 'r':
25634 if (!strcmp ("reduction", p))
25635 result = PRAGMA_OMP_CLAUSE_REDUCTION;
25636 break;
25637 case 's':
25638 if (!strcmp ("schedule", p))
25639 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
25640 else if (!strcmp ("shared", p))
25641 result = PRAGMA_OMP_CLAUSE_SHARED;
25642 break;
25643 case 'u':
25644 if (!strcmp ("untied", p))
25645 result = PRAGMA_OMP_CLAUSE_UNTIED;
25646 break;
25650 if (result != PRAGMA_OMP_CLAUSE_NONE)
25651 cp_lexer_consume_token (parser->lexer);
25653 return result;
25656 /* Validate that a clause of the given type does not already exist. */
25658 static void
25659 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
25660 const char *name, location_t location)
25662 tree c;
25664 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25665 if (OMP_CLAUSE_CODE (c) == code)
25667 error_at (location, "too many %qs clauses", name);
25668 break;
25672 /* OpenMP 2.5:
25673 variable-list:
25674 identifier
25675 variable-list , identifier
25677 In addition, we match a closing parenthesis. An opening parenthesis
25678 will have been consumed by the caller.
25680 If KIND is nonzero, create the appropriate node and install the decl
25681 in OMP_CLAUSE_DECL and add the node to the head of the list.
25683 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25684 return the list created. */
25686 static tree
25687 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
25688 tree list)
25690 cp_token *token;
25691 while (1)
25693 tree name, decl;
25695 token = cp_lexer_peek_token (parser->lexer);
25696 name = cp_parser_id_expression (parser, /*template_p=*/false,
25697 /*check_dependency_p=*/true,
25698 /*template_p=*/NULL,
25699 /*declarator_p=*/false,
25700 /*optional_p=*/false);
25701 if (name == error_mark_node)
25702 goto skip_comma;
25704 decl = cp_parser_lookup_name_simple (parser, name, token->location);
25705 if (decl == error_mark_node)
25706 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
25707 token->location);
25708 else if (kind != 0)
25710 tree u = build_omp_clause (token->location, kind);
25711 OMP_CLAUSE_DECL (u) = decl;
25712 OMP_CLAUSE_CHAIN (u) = list;
25713 list = u;
25715 else
25716 list = tree_cons (decl, NULL_TREE, list);
25718 get_comma:
25719 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25720 break;
25721 cp_lexer_consume_token (parser->lexer);
25724 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25726 int ending;
25728 /* Try to resync to an unnested comma. Copied from
25729 cp_parser_parenthesized_expression_list. */
25730 skip_comma:
25731 ending = cp_parser_skip_to_closing_parenthesis (parser,
25732 /*recovering=*/true,
25733 /*or_comma=*/true,
25734 /*consume_paren=*/true);
25735 if (ending < 0)
25736 goto get_comma;
25739 return list;
25742 /* Similarly, but expect leading and trailing parenthesis. This is a very
25743 common case for omp clauses. */
25745 static tree
25746 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25748 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25749 return cp_parser_omp_var_list_no_open (parser, kind, list);
25750 return list;
25753 /* OpenMP 3.0:
25754 collapse ( constant-expression ) */
25756 static tree
25757 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25759 tree c, num;
25760 location_t loc;
25761 HOST_WIDE_INT n;
25763 loc = cp_lexer_peek_token (parser->lexer)->location;
25764 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25765 return list;
25767 num = cp_parser_constant_expression (parser, false, NULL);
25769 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25770 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25771 /*or_comma=*/false,
25772 /*consume_paren=*/true);
25774 if (num == error_mark_node)
25775 return list;
25776 num = fold_non_dependent_expr (num);
25777 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25778 || !host_integerp (num, 0)
25779 || (n = tree_low_cst (num, 0)) <= 0
25780 || (int) n != n)
25782 error_at (loc, "collapse argument needs positive constant integer expression");
25783 return list;
25786 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25787 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25788 OMP_CLAUSE_CHAIN (c) = list;
25789 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25791 return c;
25794 /* OpenMP 2.5:
25795 default ( shared | none ) */
25797 static tree
25798 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25800 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25801 tree c;
25803 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25804 return list;
25805 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25807 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25808 const char *p = IDENTIFIER_POINTER (id);
25810 switch (p[0])
25812 case 'n':
25813 if (strcmp ("none", p) != 0)
25814 goto invalid_kind;
25815 kind = OMP_CLAUSE_DEFAULT_NONE;
25816 break;
25818 case 's':
25819 if (strcmp ("shared", p) != 0)
25820 goto invalid_kind;
25821 kind = OMP_CLAUSE_DEFAULT_SHARED;
25822 break;
25824 default:
25825 goto invalid_kind;
25828 cp_lexer_consume_token (parser->lexer);
25830 else
25832 invalid_kind:
25833 cp_parser_error (parser, "expected %<none%> or %<shared%>");
25836 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25837 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25838 /*or_comma=*/false,
25839 /*consume_paren=*/true);
25841 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25842 return list;
25844 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25845 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25846 OMP_CLAUSE_CHAIN (c) = list;
25847 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25849 return c;
25852 /* OpenMP 3.1:
25853 final ( expression ) */
25855 static tree
25856 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25858 tree t, c;
25860 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25861 return list;
25863 t = cp_parser_condition (parser);
25865 if (t == error_mark_node
25866 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25867 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25868 /*or_comma=*/false,
25869 /*consume_paren=*/true);
25871 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25873 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25874 OMP_CLAUSE_FINAL_EXPR (c) = t;
25875 OMP_CLAUSE_CHAIN (c) = list;
25877 return c;
25880 /* OpenMP 2.5:
25881 if ( expression ) */
25883 static tree
25884 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25886 tree t, c;
25888 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25889 return list;
25891 t = cp_parser_condition (parser);
25893 if (t == error_mark_node
25894 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25895 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25896 /*or_comma=*/false,
25897 /*consume_paren=*/true);
25899 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25901 c = build_omp_clause (location, OMP_CLAUSE_IF);
25902 OMP_CLAUSE_IF_EXPR (c) = t;
25903 OMP_CLAUSE_CHAIN (c) = list;
25905 return c;
25908 /* OpenMP 3.1:
25909 mergeable */
25911 static tree
25912 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
25913 tree list, location_t location)
25915 tree c;
25917 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25918 location);
25920 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25921 OMP_CLAUSE_CHAIN (c) = list;
25922 return c;
25925 /* OpenMP 2.5:
25926 nowait */
25928 static tree
25929 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
25930 tree list, location_t location)
25932 tree c;
25934 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25936 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25937 OMP_CLAUSE_CHAIN (c) = list;
25938 return c;
25941 /* OpenMP 2.5:
25942 num_threads ( expression ) */
25944 static tree
25945 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25946 location_t location)
25948 tree t, c;
25950 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25951 return list;
25953 t = cp_parser_expression (parser, false, NULL);
25955 if (t == error_mark_node
25956 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25957 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25958 /*or_comma=*/false,
25959 /*consume_paren=*/true);
25961 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25962 "num_threads", location);
25964 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25965 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25966 OMP_CLAUSE_CHAIN (c) = list;
25968 return c;
25971 /* OpenMP 2.5:
25972 ordered */
25974 static tree
25975 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
25976 tree list, location_t location)
25978 tree c;
25980 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25981 "ordered", location);
25983 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25984 OMP_CLAUSE_CHAIN (c) = list;
25985 return c;
25988 /* OpenMP 2.5:
25989 reduction ( reduction-operator : variable-list )
25991 reduction-operator:
25992 One of: + * - & ^ | && ||
25994 OpenMP 3.1:
25996 reduction-operator:
25997 One of: + * - & ^ | && || min max */
25999 static tree
26000 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
26002 enum tree_code code;
26003 tree nlist, c;
26005 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26006 return list;
26008 switch (cp_lexer_peek_token (parser->lexer)->type)
26010 case CPP_PLUS:
26011 code = PLUS_EXPR;
26012 break;
26013 case CPP_MULT:
26014 code = MULT_EXPR;
26015 break;
26016 case CPP_MINUS:
26017 code = MINUS_EXPR;
26018 break;
26019 case CPP_AND:
26020 code = BIT_AND_EXPR;
26021 break;
26022 case CPP_XOR:
26023 code = BIT_XOR_EXPR;
26024 break;
26025 case CPP_OR:
26026 code = BIT_IOR_EXPR;
26027 break;
26028 case CPP_AND_AND:
26029 code = TRUTH_ANDIF_EXPR;
26030 break;
26031 case CPP_OR_OR:
26032 code = TRUTH_ORIF_EXPR;
26033 break;
26034 case CPP_NAME:
26036 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26037 const char *p = IDENTIFIER_POINTER (id);
26039 if (strcmp (p, "min") == 0)
26041 code = MIN_EXPR;
26042 break;
26044 if (strcmp (p, "max") == 0)
26046 code = MAX_EXPR;
26047 break;
26050 /* FALLTHROUGH */
26051 default:
26052 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
26053 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
26054 resync_fail:
26055 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26056 /*or_comma=*/false,
26057 /*consume_paren=*/true);
26058 return list;
26060 cp_lexer_consume_token (parser->lexer);
26062 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26063 goto resync_fail;
26065 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
26066 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
26067 OMP_CLAUSE_REDUCTION_CODE (c) = code;
26069 return nlist;
26072 /* OpenMP 2.5:
26073 schedule ( schedule-kind )
26074 schedule ( schedule-kind , expression )
26076 schedule-kind:
26077 static | dynamic | guided | runtime | auto */
26079 static tree
26080 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
26082 tree c, t;
26084 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26085 return list;
26087 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
26089 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26091 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26092 const char *p = IDENTIFIER_POINTER (id);
26094 switch (p[0])
26096 case 'd':
26097 if (strcmp ("dynamic", p) != 0)
26098 goto invalid_kind;
26099 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
26100 break;
26102 case 'g':
26103 if (strcmp ("guided", p) != 0)
26104 goto invalid_kind;
26105 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
26106 break;
26108 case 'r':
26109 if (strcmp ("runtime", p) != 0)
26110 goto invalid_kind;
26111 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
26112 break;
26114 default:
26115 goto invalid_kind;
26118 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
26119 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
26120 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
26121 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
26122 else
26123 goto invalid_kind;
26124 cp_lexer_consume_token (parser->lexer);
26126 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26128 cp_token *token;
26129 cp_lexer_consume_token (parser->lexer);
26131 token = cp_lexer_peek_token (parser->lexer);
26132 t = cp_parser_assignment_expression (parser, false, NULL);
26134 if (t == error_mark_node)
26135 goto resync_fail;
26136 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
26137 error_at (token->location, "schedule %<runtime%> does not take "
26138 "a %<chunk_size%> parameter");
26139 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
26140 error_at (token->location, "schedule %<auto%> does not take "
26141 "a %<chunk_size%> parameter");
26142 else
26143 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
26145 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26146 goto resync_fail;
26148 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
26149 goto resync_fail;
26151 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
26152 OMP_CLAUSE_CHAIN (c) = list;
26153 return c;
26155 invalid_kind:
26156 cp_parser_error (parser, "invalid schedule kind");
26157 resync_fail:
26158 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26159 /*or_comma=*/false,
26160 /*consume_paren=*/true);
26161 return list;
26164 /* OpenMP 3.0:
26165 untied */
26167 static tree
26168 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
26169 tree list, location_t location)
26171 tree c;
26173 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
26175 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
26176 OMP_CLAUSE_CHAIN (c) = list;
26177 return c;
26180 /* Parse all OpenMP clauses. The set clauses allowed by the directive
26181 is a bitmask in MASK. Return the list of clauses found; the result
26182 of clause default goes in *pdefault. */
26184 static tree
26185 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
26186 const char *where, cp_token *pragma_tok)
26188 tree clauses = NULL;
26189 bool first = true;
26190 cp_token *token = NULL;
26192 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
26194 pragma_omp_clause c_kind;
26195 const char *c_name;
26196 tree prev = clauses;
26198 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26199 cp_lexer_consume_token (parser->lexer);
26201 token = cp_lexer_peek_token (parser->lexer);
26202 c_kind = cp_parser_omp_clause_name (parser);
26203 first = false;
26205 switch (c_kind)
26207 case PRAGMA_OMP_CLAUSE_COLLAPSE:
26208 clauses = cp_parser_omp_clause_collapse (parser, clauses,
26209 token->location);
26210 c_name = "collapse";
26211 break;
26212 case PRAGMA_OMP_CLAUSE_COPYIN:
26213 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
26214 c_name = "copyin";
26215 break;
26216 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
26217 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
26218 clauses);
26219 c_name = "copyprivate";
26220 break;
26221 case PRAGMA_OMP_CLAUSE_DEFAULT:
26222 clauses = cp_parser_omp_clause_default (parser, clauses,
26223 token->location);
26224 c_name = "default";
26225 break;
26226 case PRAGMA_OMP_CLAUSE_FINAL:
26227 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
26228 c_name = "final";
26229 break;
26230 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
26231 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
26232 clauses);
26233 c_name = "firstprivate";
26234 break;
26235 case PRAGMA_OMP_CLAUSE_IF:
26236 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
26237 c_name = "if";
26238 break;
26239 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
26240 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
26241 clauses);
26242 c_name = "lastprivate";
26243 break;
26244 case PRAGMA_OMP_CLAUSE_MERGEABLE:
26245 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
26246 token->location);
26247 c_name = "mergeable";
26248 break;
26249 case PRAGMA_OMP_CLAUSE_NOWAIT:
26250 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
26251 c_name = "nowait";
26252 break;
26253 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
26254 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
26255 token->location);
26256 c_name = "num_threads";
26257 break;
26258 case PRAGMA_OMP_CLAUSE_ORDERED:
26259 clauses = cp_parser_omp_clause_ordered (parser, clauses,
26260 token->location);
26261 c_name = "ordered";
26262 break;
26263 case PRAGMA_OMP_CLAUSE_PRIVATE:
26264 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
26265 clauses);
26266 c_name = "private";
26267 break;
26268 case PRAGMA_OMP_CLAUSE_REDUCTION:
26269 clauses = cp_parser_omp_clause_reduction (parser, clauses);
26270 c_name = "reduction";
26271 break;
26272 case PRAGMA_OMP_CLAUSE_SCHEDULE:
26273 clauses = cp_parser_omp_clause_schedule (parser, clauses,
26274 token->location);
26275 c_name = "schedule";
26276 break;
26277 case PRAGMA_OMP_CLAUSE_SHARED:
26278 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
26279 clauses);
26280 c_name = "shared";
26281 break;
26282 case PRAGMA_OMP_CLAUSE_UNTIED:
26283 clauses = cp_parser_omp_clause_untied (parser, clauses,
26284 token->location);
26285 c_name = "nowait";
26286 break;
26287 default:
26288 cp_parser_error (parser, "expected %<#pragma omp%> clause");
26289 goto saw_error;
26292 if (((mask >> c_kind) & 1) == 0)
26294 /* Remove the invalid clause(s) from the list to avoid
26295 confusing the rest of the compiler. */
26296 clauses = prev;
26297 error_at (token->location, "%qs is not valid for %qs", c_name, where);
26300 saw_error:
26301 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
26302 return finish_omp_clauses (clauses);
26305 /* OpenMP 2.5:
26306 structured-block:
26307 statement
26309 In practice, we're also interested in adding the statement to an
26310 outer node. So it is convenient if we work around the fact that
26311 cp_parser_statement calls add_stmt. */
26313 static unsigned
26314 cp_parser_begin_omp_structured_block (cp_parser *parser)
26316 unsigned save = parser->in_statement;
26318 /* Only move the values to IN_OMP_BLOCK if they weren't false.
26319 This preserves the "not within loop or switch" style error messages
26320 for nonsense cases like
26321 void foo() {
26322 #pragma omp single
26323 break;
26326 if (parser->in_statement)
26327 parser->in_statement = IN_OMP_BLOCK;
26329 return save;
26332 static void
26333 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
26335 parser->in_statement = save;
26338 static tree
26339 cp_parser_omp_structured_block (cp_parser *parser)
26341 tree stmt = begin_omp_structured_block ();
26342 unsigned int save = cp_parser_begin_omp_structured_block (parser);
26344 cp_parser_statement (parser, NULL_TREE, false, NULL);
26346 cp_parser_end_omp_structured_block (parser, save);
26347 return finish_omp_structured_block (stmt);
26350 /* OpenMP 2.5:
26351 # pragma omp atomic new-line
26352 expression-stmt
26354 expression-stmt:
26355 x binop= expr | x++ | ++x | x-- | --x
26356 binop:
26357 +, *, -, /, &, ^, |, <<, >>
26359 where x is an lvalue expression with scalar type.
26361 OpenMP 3.1:
26362 # pragma omp atomic new-line
26363 update-stmt
26365 # pragma omp atomic read new-line
26366 read-stmt
26368 # pragma omp atomic write new-line
26369 write-stmt
26371 # pragma omp atomic update new-line
26372 update-stmt
26374 # pragma omp atomic capture new-line
26375 capture-stmt
26377 # pragma omp atomic capture new-line
26378 capture-block
26380 read-stmt:
26381 v = x
26382 write-stmt:
26383 x = expr
26384 update-stmt:
26385 expression-stmt | x = x binop expr
26386 capture-stmt:
26387 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
26388 capture-block:
26389 { v = x; update-stmt; } | { update-stmt; v = x; }
26391 where x and v are lvalue expressions with scalar type. */
26393 static void
26394 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
26396 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
26397 tree rhs1 = NULL_TREE, orig_lhs;
26398 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
26399 bool structured_block = false;
26401 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26403 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26404 const char *p = IDENTIFIER_POINTER (id);
26406 if (!strcmp (p, "read"))
26407 code = OMP_ATOMIC_READ;
26408 else if (!strcmp (p, "write"))
26409 code = NOP_EXPR;
26410 else if (!strcmp (p, "update"))
26411 code = OMP_ATOMIC;
26412 else if (!strcmp (p, "capture"))
26413 code = OMP_ATOMIC_CAPTURE_NEW;
26414 else
26415 p = NULL;
26416 if (p)
26417 cp_lexer_consume_token (parser->lexer);
26419 cp_parser_require_pragma_eol (parser, pragma_tok);
26421 switch (code)
26423 case OMP_ATOMIC_READ:
26424 case NOP_EXPR: /* atomic write */
26425 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26426 /*cast_p=*/false, NULL);
26427 if (v == error_mark_node)
26428 goto saw_error;
26429 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26430 goto saw_error;
26431 if (code == NOP_EXPR)
26432 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26433 else
26434 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26435 /*cast_p=*/false, NULL);
26436 if (lhs == error_mark_node)
26437 goto saw_error;
26438 if (code == NOP_EXPR)
26440 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
26441 opcode. */
26442 code = OMP_ATOMIC;
26443 rhs = lhs;
26444 lhs = v;
26445 v = NULL_TREE;
26447 goto done;
26448 case OMP_ATOMIC_CAPTURE_NEW:
26449 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26451 cp_lexer_consume_token (parser->lexer);
26452 structured_block = true;
26454 else
26456 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26457 /*cast_p=*/false, NULL);
26458 if (v == error_mark_node)
26459 goto saw_error;
26460 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26461 goto saw_error;
26463 default:
26464 break;
26467 restart:
26468 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26469 /*cast_p=*/false, NULL);
26470 orig_lhs = lhs;
26471 switch (TREE_CODE (lhs))
26473 case ERROR_MARK:
26474 goto saw_error;
26476 case POSTINCREMENT_EXPR:
26477 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26478 code = OMP_ATOMIC_CAPTURE_OLD;
26479 /* FALLTHROUGH */
26480 case PREINCREMENT_EXPR:
26481 lhs = TREE_OPERAND (lhs, 0);
26482 opcode = PLUS_EXPR;
26483 rhs = integer_one_node;
26484 break;
26486 case POSTDECREMENT_EXPR:
26487 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26488 code = OMP_ATOMIC_CAPTURE_OLD;
26489 /* FALLTHROUGH */
26490 case PREDECREMENT_EXPR:
26491 lhs = TREE_OPERAND (lhs, 0);
26492 opcode = MINUS_EXPR;
26493 rhs = integer_one_node;
26494 break;
26496 case COMPOUND_EXPR:
26497 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
26498 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
26499 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
26500 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
26501 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
26502 (TREE_OPERAND (lhs, 1), 0), 0)))
26503 == BOOLEAN_TYPE)
26504 /* Undo effects of boolean_increment for post {in,de}crement. */
26505 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
26506 /* FALLTHRU */
26507 case MODIFY_EXPR:
26508 if (TREE_CODE (lhs) == MODIFY_EXPR
26509 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
26511 /* Undo effects of boolean_increment. */
26512 if (integer_onep (TREE_OPERAND (lhs, 1)))
26514 /* This is pre or post increment. */
26515 rhs = TREE_OPERAND (lhs, 1);
26516 lhs = TREE_OPERAND (lhs, 0);
26517 opcode = NOP_EXPR;
26518 if (code == OMP_ATOMIC_CAPTURE_NEW
26519 && !structured_block
26520 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
26521 code = OMP_ATOMIC_CAPTURE_OLD;
26522 break;
26525 /* FALLTHRU */
26526 default:
26527 switch (cp_lexer_peek_token (parser->lexer)->type)
26529 case CPP_MULT_EQ:
26530 opcode = MULT_EXPR;
26531 break;
26532 case CPP_DIV_EQ:
26533 opcode = TRUNC_DIV_EXPR;
26534 break;
26535 case CPP_PLUS_EQ:
26536 opcode = PLUS_EXPR;
26537 break;
26538 case CPP_MINUS_EQ:
26539 opcode = MINUS_EXPR;
26540 break;
26541 case CPP_LSHIFT_EQ:
26542 opcode = LSHIFT_EXPR;
26543 break;
26544 case CPP_RSHIFT_EQ:
26545 opcode = RSHIFT_EXPR;
26546 break;
26547 case CPP_AND_EQ:
26548 opcode = BIT_AND_EXPR;
26549 break;
26550 case CPP_OR_EQ:
26551 opcode = BIT_IOR_EXPR;
26552 break;
26553 case CPP_XOR_EQ:
26554 opcode = BIT_XOR_EXPR;
26555 break;
26556 case CPP_EQ:
26557 if (structured_block || code == OMP_ATOMIC)
26559 enum cp_parser_prec oprec;
26560 cp_token *token;
26561 cp_lexer_consume_token (parser->lexer);
26562 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26563 /*cast_p=*/false, NULL);
26564 if (rhs1 == error_mark_node)
26565 goto saw_error;
26566 token = cp_lexer_peek_token (parser->lexer);
26567 switch (token->type)
26569 case CPP_SEMICOLON:
26570 if (code == OMP_ATOMIC_CAPTURE_NEW)
26572 code = OMP_ATOMIC_CAPTURE_OLD;
26573 v = lhs;
26574 lhs = NULL_TREE;
26575 lhs1 = rhs1;
26576 rhs1 = NULL_TREE;
26577 cp_lexer_consume_token (parser->lexer);
26578 goto restart;
26580 cp_parser_error (parser,
26581 "invalid form of %<#pragma omp atomic%>");
26582 goto saw_error;
26583 case CPP_MULT:
26584 opcode = MULT_EXPR;
26585 break;
26586 case CPP_DIV:
26587 opcode = TRUNC_DIV_EXPR;
26588 break;
26589 case CPP_PLUS:
26590 opcode = PLUS_EXPR;
26591 break;
26592 case CPP_MINUS:
26593 opcode = MINUS_EXPR;
26594 break;
26595 case CPP_LSHIFT:
26596 opcode = LSHIFT_EXPR;
26597 break;
26598 case CPP_RSHIFT:
26599 opcode = RSHIFT_EXPR;
26600 break;
26601 case CPP_AND:
26602 opcode = BIT_AND_EXPR;
26603 break;
26604 case CPP_OR:
26605 opcode = BIT_IOR_EXPR;
26606 break;
26607 case CPP_XOR:
26608 opcode = BIT_XOR_EXPR;
26609 break;
26610 default:
26611 cp_parser_error (parser,
26612 "invalid operator for %<#pragma omp atomic%>");
26613 goto saw_error;
26615 oprec = TOKEN_PRECEDENCE (token);
26616 gcc_assert (oprec != PREC_NOT_OPERATOR);
26617 if (commutative_tree_code (opcode))
26618 oprec = (enum cp_parser_prec) (oprec - 1);
26619 cp_lexer_consume_token (parser->lexer);
26620 rhs = cp_parser_binary_expression (parser, false, false,
26621 oprec, NULL);
26622 if (rhs == error_mark_node)
26623 goto saw_error;
26624 goto stmt_done;
26626 /* FALLTHROUGH */
26627 default:
26628 cp_parser_error (parser,
26629 "invalid operator for %<#pragma omp atomic%>");
26630 goto saw_error;
26632 cp_lexer_consume_token (parser->lexer);
26634 rhs = cp_parser_expression (parser, false, NULL);
26635 if (rhs == error_mark_node)
26636 goto saw_error;
26637 break;
26639 stmt_done:
26640 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
26642 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26643 goto saw_error;
26644 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26645 /*cast_p=*/false, NULL);
26646 if (v == error_mark_node)
26647 goto saw_error;
26648 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26649 goto saw_error;
26650 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26651 /*cast_p=*/false, NULL);
26652 if (lhs1 == error_mark_node)
26653 goto saw_error;
26655 if (structured_block)
26657 cp_parser_consume_semicolon_at_end_of_statement (parser);
26658 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26660 done:
26661 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
26662 if (!structured_block)
26663 cp_parser_consume_semicolon_at_end_of_statement (parser);
26664 return;
26666 saw_error:
26667 cp_parser_skip_to_end_of_block_or_statement (parser);
26668 if (structured_block)
26670 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26671 cp_lexer_consume_token (parser->lexer);
26672 else if (code == OMP_ATOMIC_CAPTURE_NEW)
26674 cp_parser_skip_to_end_of_block_or_statement (parser);
26675 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26676 cp_lexer_consume_token (parser->lexer);
26682 /* OpenMP 2.5:
26683 # pragma omp barrier new-line */
26685 static void
26686 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
26688 cp_parser_require_pragma_eol (parser, pragma_tok);
26689 finish_omp_barrier ();
26692 /* OpenMP 2.5:
26693 # pragma omp critical [(name)] new-line
26694 structured-block */
26696 static tree
26697 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
26699 tree stmt, name = NULL;
26701 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26703 cp_lexer_consume_token (parser->lexer);
26705 name = cp_parser_identifier (parser);
26707 if (name == error_mark_node
26708 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26709 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26710 /*or_comma=*/false,
26711 /*consume_paren=*/true);
26712 if (name == error_mark_node)
26713 name = NULL;
26715 cp_parser_require_pragma_eol (parser, pragma_tok);
26717 stmt = cp_parser_omp_structured_block (parser);
26718 return c_finish_omp_critical (input_location, stmt, name);
26721 /* OpenMP 2.5:
26722 # pragma omp flush flush-vars[opt] new-line
26724 flush-vars:
26725 ( variable-list ) */
26727 static void
26728 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26730 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26731 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26732 cp_parser_require_pragma_eol (parser, pragma_tok);
26734 finish_omp_flush ();
26737 /* Helper function, to parse omp for increment expression. */
26739 static tree
26740 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26742 tree cond = cp_parser_binary_expression (parser, false, true,
26743 PREC_NOT_OPERATOR, NULL);
26744 if (cond == error_mark_node
26745 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26747 cp_parser_skip_to_end_of_statement (parser);
26748 return error_mark_node;
26751 switch (TREE_CODE (cond))
26753 case GT_EXPR:
26754 case GE_EXPR:
26755 case LT_EXPR:
26756 case LE_EXPR:
26757 break;
26758 default:
26759 return error_mark_node;
26762 /* If decl is an iterator, preserve LHS and RHS of the relational
26763 expr until finish_omp_for. */
26764 if (decl
26765 && (type_dependent_expression_p (decl)
26766 || CLASS_TYPE_P (TREE_TYPE (decl))))
26767 return cond;
26769 return build_x_binary_op (input_location, TREE_CODE (cond),
26770 TREE_OPERAND (cond, 0), ERROR_MARK,
26771 TREE_OPERAND (cond, 1), ERROR_MARK,
26772 /*overload=*/NULL, tf_warning_or_error);
26775 /* Helper function, to parse omp for increment expression. */
26777 static tree
26778 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26780 cp_token *token = cp_lexer_peek_token (parser->lexer);
26781 enum tree_code op;
26782 tree lhs, rhs;
26783 cp_id_kind idk;
26784 bool decl_first;
26786 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26788 op = (token->type == CPP_PLUS_PLUS
26789 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26790 cp_lexer_consume_token (parser->lexer);
26791 lhs = cp_parser_cast_expression (parser, false, false, NULL);
26792 if (lhs != decl)
26793 return error_mark_node;
26794 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26797 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26798 if (lhs != decl)
26799 return error_mark_node;
26801 token = cp_lexer_peek_token (parser->lexer);
26802 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26804 op = (token->type == CPP_PLUS_PLUS
26805 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26806 cp_lexer_consume_token (parser->lexer);
26807 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26810 op = cp_parser_assignment_operator_opt (parser);
26811 if (op == ERROR_MARK)
26812 return error_mark_node;
26814 if (op != NOP_EXPR)
26816 rhs = cp_parser_assignment_expression (parser, false, NULL);
26817 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26818 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26821 lhs = cp_parser_binary_expression (parser, false, false,
26822 PREC_ADDITIVE_EXPRESSION, NULL);
26823 token = cp_lexer_peek_token (parser->lexer);
26824 decl_first = lhs == decl;
26825 if (decl_first)
26826 lhs = NULL_TREE;
26827 if (token->type != CPP_PLUS
26828 && token->type != CPP_MINUS)
26829 return error_mark_node;
26833 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26834 cp_lexer_consume_token (parser->lexer);
26835 rhs = cp_parser_binary_expression (parser, false, false,
26836 PREC_ADDITIVE_EXPRESSION, NULL);
26837 token = cp_lexer_peek_token (parser->lexer);
26838 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26840 if (lhs == NULL_TREE)
26842 if (op == PLUS_EXPR)
26843 lhs = rhs;
26844 else
26845 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
26846 tf_warning_or_error);
26848 else
26849 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
26850 ERROR_MARK, NULL, tf_warning_or_error);
26853 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26855 if (!decl_first)
26857 if (rhs != decl || op == MINUS_EXPR)
26858 return error_mark_node;
26859 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26861 else
26862 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26864 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26867 /* Parse the restricted form of the for statement allowed by OpenMP. */
26869 static tree
26870 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26872 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26873 tree real_decl, initv, condv, incrv, declv;
26874 tree this_pre_body, cl;
26875 location_t loc_first;
26876 bool collapse_err = false;
26877 int i, collapse = 1, nbraces = 0;
26878 VEC(tree,gc) *for_block = make_tree_vector ();
26880 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26881 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26882 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26884 gcc_assert (collapse >= 1);
26886 declv = make_tree_vec (collapse);
26887 initv = make_tree_vec (collapse);
26888 condv = make_tree_vec (collapse);
26889 incrv = make_tree_vec (collapse);
26891 loc_first = cp_lexer_peek_token (parser->lexer)->location;
26893 for (i = 0; i < collapse; i++)
26895 int bracecount = 0;
26896 bool add_private_clause = false;
26897 location_t loc;
26899 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26901 cp_parser_error (parser, "for statement expected");
26902 return NULL;
26904 loc = cp_lexer_consume_token (parser->lexer)->location;
26906 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26907 return NULL;
26909 init = decl = real_decl = NULL;
26910 this_pre_body = push_stmt_list ();
26911 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26913 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26915 init-expr:
26916 var = lb
26917 integer-type var = lb
26918 random-access-iterator-type var = lb
26919 pointer-type var = lb
26921 cp_decl_specifier_seq type_specifiers;
26923 /* First, try to parse as an initialized declaration. See
26924 cp_parser_condition, from whence the bulk of this is copied. */
26926 cp_parser_parse_tentatively (parser);
26927 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26928 /*is_trailing_return=*/false,
26929 &type_specifiers);
26930 if (cp_parser_parse_definitely (parser))
26932 /* If parsing a type specifier seq succeeded, then this
26933 MUST be a initialized declaration. */
26934 tree asm_specification, attributes;
26935 cp_declarator *declarator;
26937 declarator = cp_parser_declarator (parser,
26938 CP_PARSER_DECLARATOR_NAMED,
26939 /*ctor_dtor_or_conv_p=*/NULL,
26940 /*parenthesized_p=*/NULL,
26941 /*member_p=*/false);
26942 attributes = cp_parser_attributes_opt (parser);
26943 asm_specification = cp_parser_asm_specification_opt (parser);
26945 if (declarator == cp_error_declarator)
26946 cp_parser_skip_to_end_of_statement (parser);
26948 else
26950 tree pushed_scope, auto_node;
26952 decl = start_decl (declarator, &type_specifiers,
26953 SD_INITIALIZED, attributes,
26954 /*prefix_attributes=*/NULL_TREE,
26955 &pushed_scope);
26957 auto_node = type_uses_auto (TREE_TYPE (decl));
26958 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26960 if (cp_lexer_next_token_is (parser->lexer,
26961 CPP_OPEN_PAREN))
26962 error ("parenthesized initialization is not allowed in "
26963 "OpenMP %<for%> loop");
26964 else
26965 /* Trigger an error. */
26966 cp_parser_require (parser, CPP_EQ, RT_EQ);
26968 init = error_mark_node;
26969 cp_parser_skip_to_end_of_statement (parser);
26971 else if (CLASS_TYPE_P (TREE_TYPE (decl))
26972 || type_dependent_expression_p (decl)
26973 || auto_node)
26975 bool is_direct_init, is_non_constant_init;
26977 init = cp_parser_initializer (parser,
26978 &is_direct_init,
26979 &is_non_constant_init);
26981 if (auto_node)
26983 TREE_TYPE (decl)
26984 = do_auto_deduction (TREE_TYPE (decl), init,
26985 auto_node);
26987 if (!CLASS_TYPE_P (TREE_TYPE (decl))
26988 && !type_dependent_expression_p (decl))
26989 goto non_class;
26992 cp_finish_decl (decl, init, !is_non_constant_init,
26993 asm_specification,
26994 LOOKUP_ONLYCONVERTING);
26995 if (CLASS_TYPE_P (TREE_TYPE (decl)))
26997 VEC_safe_push (tree, gc, for_block, this_pre_body);
26998 init = NULL_TREE;
27000 else
27001 init = pop_stmt_list (this_pre_body);
27002 this_pre_body = NULL_TREE;
27004 else
27006 /* Consume '='. */
27007 cp_lexer_consume_token (parser->lexer);
27008 init = cp_parser_assignment_expression (parser, false, NULL);
27010 non_class:
27011 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
27012 init = error_mark_node;
27013 else
27014 cp_finish_decl (decl, NULL_TREE,
27015 /*init_const_expr_p=*/false,
27016 asm_specification,
27017 LOOKUP_ONLYCONVERTING);
27020 if (pushed_scope)
27021 pop_scope (pushed_scope);
27024 else
27026 cp_id_kind idk;
27027 /* If parsing a type specifier sequence failed, then
27028 this MUST be a simple expression. */
27029 cp_parser_parse_tentatively (parser);
27030 decl = cp_parser_primary_expression (parser, false, false,
27031 false, &idk);
27032 if (!cp_parser_error_occurred (parser)
27033 && decl
27034 && DECL_P (decl)
27035 && CLASS_TYPE_P (TREE_TYPE (decl)))
27037 tree rhs;
27039 cp_parser_parse_definitely (parser);
27040 cp_parser_require (parser, CPP_EQ, RT_EQ);
27041 rhs = cp_parser_assignment_expression (parser, false, NULL);
27042 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
27043 decl, NOP_EXPR,
27044 rhs,
27045 tf_warning_or_error));
27046 add_private_clause = true;
27048 else
27050 decl = NULL;
27051 cp_parser_abort_tentative_parse (parser);
27052 init = cp_parser_expression (parser, false, NULL);
27053 if (init)
27055 if (TREE_CODE (init) == MODIFY_EXPR
27056 || TREE_CODE (init) == MODOP_EXPR)
27057 real_decl = TREE_OPERAND (init, 0);
27062 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27063 if (this_pre_body)
27065 this_pre_body = pop_stmt_list (this_pre_body);
27066 if (pre_body)
27068 tree t = pre_body;
27069 pre_body = push_stmt_list ();
27070 add_stmt (t);
27071 add_stmt (this_pre_body);
27072 pre_body = pop_stmt_list (pre_body);
27074 else
27075 pre_body = this_pre_body;
27078 if (decl)
27079 real_decl = decl;
27080 if (par_clauses != NULL && real_decl != NULL_TREE)
27082 tree *c;
27083 for (c = par_clauses; *c ; )
27084 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
27085 && OMP_CLAUSE_DECL (*c) == real_decl)
27087 error_at (loc, "iteration variable %qD"
27088 " should not be firstprivate", real_decl);
27089 *c = OMP_CLAUSE_CHAIN (*c);
27091 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
27092 && OMP_CLAUSE_DECL (*c) == real_decl)
27094 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
27095 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
27096 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
27097 OMP_CLAUSE_DECL (l) = real_decl;
27098 OMP_CLAUSE_CHAIN (l) = clauses;
27099 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
27100 clauses = l;
27101 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
27102 CP_OMP_CLAUSE_INFO (*c) = NULL;
27103 add_private_clause = false;
27105 else
27107 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
27108 && OMP_CLAUSE_DECL (*c) == real_decl)
27109 add_private_clause = false;
27110 c = &OMP_CLAUSE_CHAIN (*c);
27114 if (add_private_clause)
27116 tree c;
27117 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27119 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
27120 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
27121 && OMP_CLAUSE_DECL (c) == decl)
27122 break;
27123 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
27124 && OMP_CLAUSE_DECL (c) == decl)
27125 error_at (loc, "iteration variable %qD "
27126 "should not be firstprivate",
27127 decl);
27128 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
27129 && OMP_CLAUSE_DECL (c) == decl)
27130 error_at (loc, "iteration variable %qD should not be reduction",
27131 decl);
27133 if (c == NULL)
27135 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
27136 OMP_CLAUSE_DECL (c) = decl;
27137 c = finish_omp_clauses (c);
27138 if (c)
27140 OMP_CLAUSE_CHAIN (c) = clauses;
27141 clauses = c;
27146 cond = NULL;
27147 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27148 cond = cp_parser_omp_for_cond (parser, decl);
27149 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27151 incr = NULL;
27152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27154 /* If decl is an iterator, preserve the operator on decl
27155 until finish_omp_for. */
27156 if (real_decl
27157 && ((processing_template_decl
27158 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
27159 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
27160 incr = cp_parser_omp_for_incr (parser, real_decl);
27161 else
27162 incr = cp_parser_expression (parser, false, NULL);
27163 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
27164 SET_EXPR_LOCATION (incr, input_location);
27167 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27168 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27169 /*or_comma=*/false,
27170 /*consume_paren=*/true);
27172 TREE_VEC_ELT (declv, i) = decl;
27173 TREE_VEC_ELT (initv, i) = init;
27174 TREE_VEC_ELT (condv, i) = cond;
27175 TREE_VEC_ELT (incrv, i) = incr;
27177 if (i == collapse - 1)
27178 break;
27180 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
27181 in between the collapsed for loops to be still considered perfectly
27182 nested. Hopefully the final version clarifies this.
27183 For now handle (multiple) {'s and empty statements. */
27184 cp_parser_parse_tentatively (parser);
27187 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27188 break;
27189 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27191 cp_lexer_consume_token (parser->lexer);
27192 bracecount++;
27194 else if (bracecount
27195 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27196 cp_lexer_consume_token (parser->lexer);
27197 else
27199 loc = cp_lexer_peek_token (parser->lexer)->location;
27200 error_at (loc, "not enough collapsed for loops");
27201 collapse_err = true;
27202 cp_parser_abort_tentative_parse (parser);
27203 declv = NULL_TREE;
27204 break;
27207 while (1);
27209 if (declv)
27211 cp_parser_parse_definitely (parser);
27212 nbraces += bracecount;
27216 /* Note that we saved the original contents of this flag when we entered
27217 the structured block, and so we don't need to re-save it here. */
27218 parser->in_statement = IN_OMP_FOR;
27220 /* Note that the grammar doesn't call for a structured block here,
27221 though the loop as a whole is a structured block. */
27222 body = push_stmt_list ();
27223 cp_parser_statement (parser, NULL_TREE, false, NULL);
27224 body = pop_stmt_list (body);
27226 if (declv == NULL_TREE)
27227 ret = NULL_TREE;
27228 else
27229 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
27230 pre_body, clauses);
27232 while (nbraces)
27234 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27236 cp_lexer_consume_token (parser->lexer);
27237 nbraces--;
27239 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27240 cp_lexer_consume_token (parser->lexer);
27241 else
27243 if (!collapse_err)
27245 error_at (cp_lexer_peek_token (parser->lexer)->location,
27246 "collapsed loops not perfectly nested");
27248 collapse_err = true;
27249 cp_parser_statement_seq_opt (parser, NULL);
27250 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27251 break;
27255 while (!VEC_empty (tree, for_block))
27256 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
27257 release_tree_vector (for_block);
27259 return ret;
27262 /* OpenMP 2.5:
27263 #pragma omp for for-clause[optseq] new-line
27264 for-loop */
27266 #define OMP_FOR_CLAUSE_MASK \
27267 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27268 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27269 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27270 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27271 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
27272 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
27273 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
27274 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
27276 static tree
27277 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
27279 tree clauses, sb, ret;
27280 unsigned int save;
27282 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
27283 "#pragma omp for", pragma_tok);
27285 sb = begin_omp_structured_block ();
27286 save = cp_parser_begin_omp_structured_block (parser);
27288 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
27290 cp_parser_end_omp_structured_block (parser, save);
27291 add_stmt (finish_omp_structured_block (sb));
27293 return ret;
27296 /* OpenMP 2.5:
27297 # pragma omp master new-line
27298 structured-block */
27300 static tree
27301 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
27303 cp_parser_require_pragma_eol (parser, pragma_tok);
27304 return c_finish_omp_master (input_location,
27305 cp_parser_omp_structured_block (parser));
27308 /* OpenMP 2.5:
27309 # pragma omp ordered new-line
27310 structured-block */
27312 static tree
27313 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
27315 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27316 cp_parser_require_pragma_eol (parser, pragma_tok);
27317 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
27320 /* OpenMP 2.5:
27322 section-scope:
27323 { section-sequence }
27325 section-sequence:
27326 section-directive[opt] structured-block
27327 section-sequence section-directive structured-block */
27329 static tree
27330 cp_parser_omp_sections_scope (cp_parser *parser)
27332 tree stmt, substmt;
27333 bool error_suppress = false;
27334 cp_token *tok;
27336 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
27337 return NULL_TREE;
27339 stmt = push_stmt_list ();
27341 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
27343 unsigned save;
27345 substmt = begin_omp_structured_block ();
27346 save = cp_parser_begin_omp_structured_block (parser);
27348 while (1)
27350 cp_parser_statement (parser, NULL_TREE, false, NULL);
27352 tok = cp_lexer_peek_token (parser->lexer);
27353 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27354 break;
27355 if (tok->type == CPP_CLOSE_BRACE)
27356 break;
27357 if (tok->type == CPP_EOF)
27358 break;
27361 cp_parser_end_omp_structured_block (parser, save);
27362 substmt = finish_omp_structured_block (substmt);
27363 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27364 add_stmt (substmt);
27367 while (1)
27369 tok = cp_lexer_peek_token (parser->lexer);
27370 if (tok->type == CPP_CLOSE_BRACE)
27371 break;
27372 if (tok->type == CPP_EOF)
27373 break;
27375 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27377 cp_lexer_consume_token (parser->lexer);
27378 cp_parser_require_pragma_eol (parser, tok);
27379 error_suppress = false;
27381 else if (!error_suppress)
27383 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
27384 error_suppress = true;
27387 substmt = cp_parser_omp_structured_block (parser);
27388 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27389 add_stmt (substmt);
27391 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
27393 substmt = pop_stmt_list (stmt);
27395 stmt = make_node (OMP_SECTIONS);
27396 TREE_TYPE (stmt) = void_type_node;
27397 OMP_SECTIONS_BODY (stmt) = substmt;
27399 add_stmt (stmt);
27400 return stmt;
27403 /* OpenMP 2.5:
27404 # pragma omp sections sections-clause[optseq] newline
27405 sections-scope */
27407 #define OMP_SECTIONS_CLAUSE_MASK \
27408 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27409 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27410 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27411 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27412 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27414 static tree
27415 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
27417 tree clauses, ret;
27419 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
27420 "#pragma omp sections", pragma_tok);
27422 ret = cp_parser_omp_sections_scope (parser);
27423 if (ret)
27424 OMP_SECTIONS_CLAUSES (ret) = clauses;
27426 return ret;
27429 /* OpenMP 2.5:
27430 # pragma parallel parallel-clause new-line
27431 # pragma parallel for parallel-for-clause new-line
27432 # pragma parallel sections parallel-sections-clause new-line */
27434 #define OMP_PARALLEL_CLAUSE_MASK \
27435 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27436 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27437 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27438 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27439 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27440 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
27441 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27442 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
27444 static tree
27445 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
27447 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
27448 const char *p_name = "#pragma omp parallel";
27449 tree stmt, clauses, par_clause, ws_clause, block;
27450 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
27451 unsigned int save;
27452 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27454 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27456 cp_lexer_consume_token (parser->lexer);
27457 p_kind = PRAGMA_OMP_PARALLEL_FOR;
27458 p_name = "#pragma omp parallel for";
27459 mask |= OMP_FOR_CLAUSE_MASK;
27460 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27462 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27464 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27465 const char *p = IDENTIFIER_POINTER (id);
27466 if (strcmp (p, "sections") == 0)
27468 cp_lexer_consume_token (parser->lexer);
27469 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
27470 p_name = "#pragma omp parallel sections";
27471 mask |= OMP_SECTIONS_CLAUSE_MASK;
27472 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27476 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
27477 block = begin_omp_parallel ();
27478 save = cp_parser_begin_omp_structured_block (parser);
27480 switch (p_kind)
27482 case PRAGMA_OMP_PARALLEL:
27483 cp_parser_statement (parser, NULL_TREE, false, NULL);
27484 par_clause = clauses;
27485 break;
27487 case PRAGMA_OMP_PARALLEL_FOR:
27488 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27489 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
27490 break;
27492 case PRAGMA_OMP_PARALLEL_SECTIONS:
27493 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27494 stmt = cp_parser_omp_sections_scope (parser);
27495 if (stmt)
27496 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
27497 break;
27499 default:
27500 gcc_unreachable ();
27503 cp_parser_end_omp_structured_block (parser, save);
27504 stmt = finish_omp_parallel (par_clause, block);
27505 if (p_kind != PRAGMA_OMP_PARALLEL)
27506 OMP_PARALLEL_COMBINED (stmt) = 1;
27507 return stmt;
27510 /* OpenMP 2.5:
27511 # pragma omp single single-clause[optseq] new-line
27512 structured-block */
27514 #define OMP_SINGLE_CLAUSE_MASK \
27515 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27516 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27517 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
27518 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27520 static tree
27521 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
27523 tree stmt = make_node (OMP_SINGLE);
27524 TREE_TYPE (stmt) = void_type_node;
27526 OMP_SINGLE_CLAUSES (stmt)
27527 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
27528 "#pragma omp single", pragma_tok);
27529 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
27531 return add_stmt (stmt);
27534 /* OpenMP 3.0:
27535 # pragma omp task task-clause[optseq] new-line
27536 structured-block */
27538 #define OMP_TASK_CLAUSE_MASK \
27539 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27540 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
27541 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27542 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27543 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27544 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27545 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
27546 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
27548 static tree
27549 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
27551 tree clauses, block;
27552 unsigned int save;
27554 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
27555 "#pragma omp task", pragma_tok);
27556 block = begin_omp_task ();
27557 save = cp_parser_begin_omp_structured_block (parser);
27558 cp_parser_statement (parser, NULL_TREE, false, NULL);
27559 cp_parser_end_omp_structured_block (parser, save);
27560 return finish_omp_task (clauses, block);
27563 /* OpenMP 3.0:
27564 # pragma omp taskwait new-line */
27566 static void
27567 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
27569 cp_parser_require_pragma_eol (parser, pragma_tok);
27570 finish_omp_taskwait ();
27573 /* OpenMP 3.1:
27574 # pragma omp taskyield new-line */
27576 static void
27577 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
27579 cp_parser_require_pragma_eol (parser, pragma_tok);
27580 finish_omp_taskyield ();
27583 /* OpenMP 2.5:
27584 # pragma omp threadprivate (variable-list) */
27586 static void
27587 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
27589 tree vars;
27591 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27592 cp_parser_require_pragma_eol (parser, pragma_tok);
27594 finish_omp_threadprivate (vars);
27597 /* Main entry point to OpenMP statement pragmas. */
27599 static void
27600 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
27602 tree stmt;
27604 switch (pragma_tok->pragma_kind)
27606 case PRAGMA_OMP_ATOMIC:
27607 cp_parser_omp_atomic (parser, pragma_tok);
27608 return;
27609 case PRAGMA_OMP_CRITICAL:
27610 stmt = cp_parser_omp_critical (parser, pragma_tok);
27611 break;
27612 case PRAGMA_OMP_FOR:
27613 stmt = cp_parser_omp_for (parser, pragma_tok);
27614 break;
27615 case PRAGMA_OMP_MASTER:
27616 stmt = cp_parser_omp_master (parser, pragma_tok);
27617 break;
27618 case PRAGMA_OMP_ORDERED:
27619 stmt = cp_parser_omp_ordered (parser, pragma_tok);
27620 break;
27621 case PRAGMA_OMP_PARALLEL:
27622 stmt = cp_parser_omp_parallel (parser, pragma_tok);
27623 break;
27624 case PRAGMA_OMP_SECTIONS:
27625 stmt = cp_parser_omp_sections (parser, pragma_tok);
27626 break;
27627 case PRAGMA_OMP_SINGLE:
27628 stmt = cp_parser_omp_single (parser, pragma_tok);
27629 break;
27630 case PRAGMA_OMP_TASK:
27631 stmt = cp_parser_omp_task (parser, pragma_tok);
27632 break;
27633 default:
27634 gcc_unreachable ();
27637 if (stmt)
27638 SET_EXPR_LOCATION (stmt, pragma_tok->location);
27641 /* Transactional Memory parsing routines. */
27643 /* Parse a transaction attribute.
27645 txn-attribute:
27646 attribute
27647 [ [ identifier ] ]
27649 ??? Simplify this when C++0x bracket attributes are
27650 implemented properly. */
27652 static tree
27653 cp_parser_txn_attribute_opt (cp_parser *parser)
27655 cp_token *token;
27656 tree attr_name, attr = NULL;
27658 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27659 return cp_parser_attributes_opt (parser);
27661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
27662 return NULL_TREE;
27663 cp_lexer_consume_token (parser->lexer);
27664 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
27665 goto error1;
27667 token = cp_lexer_peek_token (parser->lexer);
27668 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
27670 token = cp_lexer_consume_token (parser->lexer);
27672 attr_name = (token->type == CPP_KEYWORD
27673 /* For keywords, use the canonical spelling,
27674 not the parsed identifier. */
27675 ? ridpointers[(int) token->keyword]
27676 : token->u.value);
27677 attr = build_tree_list (attr_name, NULL_TREE);
27679 else
27680 cp_parser_error (parser, "expected identifier");
27682 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27683 error1:
27684 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27685 return attr;
27688 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27690 transaction-statement:
27691 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27692 compound-statement
27693 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27696 static tree
27697 cp_parser_transaction (cp_parser *parser, enum rid keyword)
27699 unsigned char old_in = parser->in_transaction;
27700 unsigned char this_in = 1, new_in;
27701 cp_token *token;
27702 tree stmt, attrs, noex;
27704 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27705 || keyword == RID_TRANSACTION_RELAXED);
27706 token = cp_parser_require_keyword (parser, keyword,
27707 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27708 : RT_TRANSACTION_RELAXED));
27709 gcc_assert (token != NULL);
27711 if (keyword == RID_TRANSACTION_RELAXED)
27712 this_in |= TM_STMT_ATTR_RELAXED;
27713 else
27715 attrs = cp_parser_txn_attribute_opt (parser);
27716 if (attrs)
27717 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27720 /* Parse a noexcept specification. */
27721 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27723 /* Keep track if we're in the lexical scope of an outer transaction. */
27724 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27726 stmt = begin_transaction_stmt (token->location, NULL, this_in);
27728 parser->in_transaction = new_in;
27729 cp_parser_compound_statement (parser, NULL, false, false);
27730 parser->in_transaction = old_in;
27732 finish_transaction_stmt (stmt, NULL, this_in, noex);
27734 return stmt;
27737 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27739 transaction-expression:
27740 __transaction_atomic txn-noexcept-spec[opt] ( expression )
27741 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27744 static tree
27745 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27747 unsigned char old_in = parser->in_transaction;
27748 unsigned char this_in = 1;
27749 cp_token *token;
27750 tree expr, noex;
27751 bool noex_expr;
27753 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27754 || keyword == RID_TRANSACTION_RELAXED);
27756 if (!flag_tm)
27757 error (keyword == RID_TRANSACTION_RELAXED
27758 ? G_("%<__transaction_relaxed%> without transactional memory "
27759 "support enabled")
27760 : G_("%<__transaction_atomic%> without transactional memory "
27761 "support enabled"));
27763 token = cp_parser_require_keyword (parser, keyword,
27764 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27765 : RT_TRANSACTION_RELAXED));
27766 gcc_assert (token != NULL);
27768 if (keyword == RID_TRANSACTION_RELAXED)
27769 this_in |= TM_STMT_ATTR_RELAXED;
27771 /* Set this early. This might mean that we allow transaction_cancel in
27772 an expression that we find out later actually has to be a constexpr.
27773 However, we expect that cxx_constant_value will be able to deal with
27774 this; also, if the noexcept has no constexpr, then what we parse next
27775 really is a transaction's body. */
27776 parser->in_transaction = this_in;
27778 /* Parse a noexcept specification. */
27779 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27780 true);
27782 if (!noex || !noex_expr
27783 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27785 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27787 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27788 finish_parenthesized_expr (expr);
27790 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27792 else
27794 /* The only expression that is available got parsed for the noexcept
27795 already. noexcept is true then. */
27796 expr = noex;
27797 noex = boolean_true_node;
27800 expr = build_transaction_expr (token->location, expr, this_in, noex);
27801 parser->in_transaction = old_in;
27803 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27804 return error_mark_node;
27806 return (flag_tm ? expr : error_mark_node);
27809 /* Parse a function-transaction-block.
27811 function-transaction-block:
27812 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27813 function-body
27814 __transaction_atomic txn-attribute[opt] function-try-block
27815 __transaction_relaxed ctor-initializer[opt] function-body
27816 __transaction_relaxed function-try-block
27819 static bool
27820 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27822 unsigned char old_in = parser->in_transaction;
27823 unsigned char new_in = 1;
27824 tree compound_stmt, stmt, attrs;
27825 bool ctor_initializer_p;
27826 cp_token *token;
27828 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27829 || keyword == RID_TRANSACTION_RELAXED);
27830 token = cp_parser_require_keyword (parser, keyword,
27831 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27832 : RT_TRANSACTION_RELAXED));
27833 gcc_assert (token != NULL);
27835 if (keyword == RID_TRANSACTION_RELAXED)
27836 new_in |= TM_STMT_ATTR_RELAXED;
27837 else
27839 attrs = cp_parser_txn_attribute_opt (parser);
27840 if (attrs)
27841 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27844 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27846 parser->in_transaction = new_in;
27848 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27849 ctor_initializer_p = cp_parser_function_try_block (parser);
27850 else
27851 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
27852 (parser, /*in_function_try_block=*/false);
27854 parser->in_transaction = old_in;
27856 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27858 return ctor_initializer_p;
27861 /* Parse a __transaction_cancel statement.
27863 cancel-statement:
27864 __transaction_cancel txn-attribute[opt] ;
27865 __transaction_cancel txn-attribute[opt] throw-expression ;
27867 ??? Cancel and throw is not yet implemented. */
27869 static tree
27870 cp_parser_transaction_cancel (cp_parser *parser)
27872 cp_token *token;
27873 bool is_outer = false;
27874 tree stmt, attrs;
27876 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27877 RT_TRANSACTION_CANCEL);
27878 gcc_assert (token != NULL);
27880 attrs = cp_parser_txn_attribute_opt (parser);
27881 if (attrs)
27882 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27884 /* ??? Parse cancel-and-throw here. */
27886 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27888 if (!flag_tm)
27890 error_at (token->location, "%<__transaction_cancel%> without "
27891 "transactional memory support enabled");
27892 return error_mark_node;
27894 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27896 error_at (token->location, "%<__transaction_cancel%> within a "
27897 "%<__transaction_relaxed%>");
27898 return error_mark_node;
27900 else if (is_outer)
27902 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27903 && !is_tm_may_cancel_outer (current_function_decl))
27905 error_at (token->location, "outer %<__transaction_cancel%> not "
27906 "within outer %<__transaction_atomic%>");
27907 error_at (token->location,
27908 " or a %<transaction_may_cancel_outer%> function");
27909 return error_mark_node;
27912 else if (parser->in_transaction == 0)
27914 error_at (token->location, "%<__transaction_cancel%> not within "
27915 "%<__transaction_atomic%>");
27916 return error_mark_node;
27919 stmt = build_tm_abort_call (token->location, is_outer);
27920 add_stmt (stmt);
27921 finish_stmt ();
27923 return stmt;
27926 /* The parser. */
27928 static GTY (()) cp_parser *the_parser;
27931 /* Special handling for the first token or line in the file. The first
27932 thing in the file might be #pragma GCC pch_preprocess, which loads a
27933 PCH file, which is a GC collection point. So we need to handle this
27934 first pragma without benefit of an existing lexer structure.
27936 Always returns one token to the caller in *FIRST_TOKEN. This is
27937 either the true first token of the file, or the first token after
27938 the initial pragma. */
27940 static void
27941 cp_parser_initial_pragma (cp_token *first_token)
27943 tree name = NULL;
27945 cp_lexer_get_preprocessor_token (NULL, first_token);
27946 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27947 return;
27949 cp_lexer_get_preprocessor_token (NULL, first_token);
27950 if (first_token->type == CPP_STRING)
27952 name = first_token->u.value;
27954 cp_lexer_get_preprocessor_token (NULL, first_token);
27955 if (first_token->type != CPP_PRAGMA_EOL)
27956 error_at (first_token->location,
27957 "junk at end of %<#pragma GCC pch_preprocess%>");
27959 else
27960 error_at (first_token->location, "expected string literal");
27962 /* Skip to the end of the pragma. */
27963 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27964 cp_lexer_get_preprocessor_token (NULL, first_token);
27966 /* Now actually load the PCH file. */
27967 if (name)
27968 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27970 /* Read one more token to return to our caller. We have to do this
27971 after reading the PCH file in, since its pointers have to be
27972 live. */
27973 cp_lexer_get_preprocessor_token (NULL, first_token);
27976 /* Normal parsing of a pragma token. Here we can (and must) use the
27977 regular lexer. */
27979 static bool
27980 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27982 cp_token *pragma_tok;
27983 unsigned int id;
27985 pragma_tok = cp_lexer_consume_token (parser->lexer);
27986 gcc_assert (pragma_tok->type == CPP_PRAGMA);
27987 parser->lexer->in_pragma = true;
27989 id = pragma_tok->pragma_kind;
27990 switch (id)
27992 case PRAGMA_GCC_PCH_PREPROCESS:
27993 error_at (pragma_tok->location,
27994 "%<#pragma GCC pch_preprocess%> must be first");
27995 break;
27997 case PRAGMA_OMP_BARRIER:
27998 switch (context)
28000 case pragma_compound:
28001 cp_parser_omp_barrier (parser, pragma_tok);
28002 return false;
28003 case pragma_stmt:
28004 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
28005 "used in compound statements");
28006 break;
28007 default:
28008 goto bad_stmt;
28010 break;
28012 case PRAGMA_OMP_FLUSH:
28013 switch (context)
28015 case pragma_compound:
28016 cp_parser_omp_flush (parser, pragma_tok);
28017 return false;
28018 case pragma_stmt:
28019 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
28020 "used in compound statements");
28021 break;
28022 default:
28023 goto bad_stmt;
28025 break;
28027 case PRAGMA_OMP_TASKWAIT:
28028 switch (context)
28030 case pragma_compound:
28031 cp_parser_omp_taskwait (parser, pragma_tok);
28032 return false;
28033 case pragma_stmt:
28034 error_at (pragma_tok->location,
28035 "%<#pragma omp taskwait%> may only be "
28036 "used in compound statements");
28037 break;
28038 default:
28039 goto bad_stmt;
28041 break;
28043 case PRAGMA_OMP_TASKYIELD:
28044 switch (context)
28046 case pragma_compound:
28047 cp_parser_omp_taskyield (parser, pragma_tok);
28048 return false;
28049 case pragma_stmt:
28050 error_at (pragma_tok->location,
28051 "%<#pragma omp taskyield%> may only be "
28052 "used in compound statements");
28053 break;
28054 default:
28055 goto bad_stmt;
28057 break;
28059 case PRAGMA_OMP_THREADPRIVATE:
28060 cp_parser_omp_threadprivate (parser, pragma_tok);
28061 return false;
28063 case PRAGMA_OMP_ATOMIC:
28064 case PRAGMA_OMP_CRITICAL:
28065 case PRAGMA_OMP_FOR:
28066 case PRAGMA_OMP_MASTER:
28067 case PRAGMA_OMP_ORDERED:
28068 case PRAGMA_OMP_PARALLEL:
28069 case PRAGMA_OMP_SECTIONS:
28070 case PRAGMA_OMP_SINGLE:
28071 case PRAGMA_OMP_TASK:
28072 if (context == pragma_external)
28073 goto bad_stmt;
28074 cp_parser_omp_construct (parser, pragma_tok);
28075 return true;
28077 case PRAGMA_OMP_SECTION:
28078 error_at (pragma_tok->location,
28079 "%<#pragma omp section%> may only be used in "
28080 "%<#pragma omp sections%> construct");
28081 break;
28083 default:
28084 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
28085 c_invoke_pragma_handler (id);
28086 break;
28088 bad_stmt:
28089 cp_parser_error (parser, "expected declaration specifiers");
28090 break;
28093 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28094 return false;
28097 /* The interface the pragma parsers have to the lexer. */
28099 enum cpp_ttype
28100 pragma_lex (tree *value)
28102 cp_token *tok;
28103 enum cpp_ttype ret;
28105 tok = cp_lexer_peek_token (the_parser->lexer);
28107 ret = tok->type;
28108 *value = tok->u.value;
28110 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
28111 ret = CPP_EOF;
28112 else if (ret == CPP_STRING)
28113 *value = cp_parser_string_literal (the_parser, false, false);
28114 else
28116 cp_lexer_consume_token (the_parser->lexer);
28117 if (ret == CPP_KEYWORD)
28118 ret = CPP_NAME;
28121 return ret;
28125 /* External interface. */
28127 /* Parse one entire translation unit. */
28129 void
28130 c_parse_file (void)
28132 static bool already_called = false;
28134 if (already_called)
28136 sorry ("inter-module optimizations not implemented for C++");
28137 return;
28139 already_called = true;
28141 the_parser = cp_parser_new ();
28142 push_deferring_access_checks (flag_access_control
28143 ? dk_no_deferred : dk_no_check);
28144 cp_parser_translation_unit (the_parser);
28145 the_parser = NULL;
28148 #include "gt-cp-parser.h"