* pt.c (tsubst_default_argument): Indicate where the default
[official-gcc.git] / gcc / cp / parser.c
blob0f897c96f070ee5a6678ada7c9344258c716118a
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 ATTRIBUTE_UNUSED, 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 *);
1201 static cp_declarator *make_reference_declarator
1202 (cp_cv_quals, cp_declarator *, bool);
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 *);
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->declarator = NULL;
1235 declarator->parameter_pack_p = false;
1236 declarator->id_loc = UNKNOWN_LOCATION;
1238 return declarator;
1241 /* Make a declarator for a generalized identifier. If
1242 QUALIFYING_SCOPE is non-NULL, the identifier is
1243 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1244 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1245 is, if any. */
1247 static cp_declarator *
1248 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1249 special_function_kind sfk)
1251 cp_declarator *declarator;
1253 /* It is valid to write:
1255 class C { void f(); };
1256 typedef C D;
1257 void D::f();
1259 The standard is not clear about whether `typedef const C D' is
1260 legal; as of 2002-09-15 the committee is considering that
1261 question. EDG 3.0 allows that syntax. Therefore, we do as
1262 well. */
1263 if (qualifying_scope && TYPE_P (qualifying_scope))
1264 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1266 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1267 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1268 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1270 declarator = make_declarator (cdk_id);
1271 declarator->u.id.qualifying_scope = qualifying_scope;
1272 declarator->u.id.unqualified_name = unqualified_name;
1273 declarator->u.id.sfk = sfk;
1275 return declarator;
1278 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1279 of modifiers such as const or volatile to apply to the pointer
1280 type, represented as identifiers. */
1282 cp_declarator *
1283 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1285 cp_declarator *declarator;
1287 declarator = make_declarator (cdk_pointer);
1288 declarator->declarator = target;
1289 declarator->u.pointer.qualifiers = cv_qualifiers;
1290 declarator->u.pointer.class_type = NULL_TREE;
1291 if (target)
1293 declarator->id_loc = target->id_loc;
1294 declarator->parameter_pack_p = target->parameter_pack_p;
1295 target->parameter_pack_p = false;
1297 else
1298 declarator->parameter_pack_p = false;
1300 return declarator;
1303 /* Like make_pointer_declarator -- but for references. */
1305 cp_declarator *
1306 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1307 bool rvalue_ref)
1309 cp_declarator *declarator;
1311 declarator = make_declarator (cdk_reference);
1312 declarator->declarator = target;
1313 declarator->u.reference.qualifiers = cv_qualifiers;
1314 declarator->u.reference.rvalue_ref = rvalue_ref;
1315 if (target)
1317 declarator->id_loc = target->id_loc;
1318 declarator->parameter_pack_p = target->parameter_pack_p;
1319 target->parameter_pack_p = false;
1321 else
1322 declarator->parameter_pack_p = false;
1324 return declarator;
1327 /* Like make_pointer_declarator -- but for a pointer to a non-static
1328 member of CLASS_TYPE. */
1330 cp_declarator *
1331 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1332 cp_declarator *pointee)
1334 cp_declarator *declarator;
1336 declarator = make_declarator (cdk_ptrmem);
1337 declarator->declarator = pointee;
1338 declarator->u.pointer.qualifiers = cv_qualifiers;
1339 declarator->u.pointer.class_type = class_type;
1341 if (pointee)
1343 declarator->parameter_pack_p = pointee->parameter_pack_p;
1344 pointee->parameter_pack_p = false;
1346 else
1347 declarator->parameter_pack_p = false;
1349 return declarator;
1352 /* Make a declarator for the function given by TARGET, with the
1353 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1354 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1355 indicates what exceptions can be thrown. */
1357 cp_declarator *
1358 make_call_declarator (cp_declarator *target,
1359 tree parms,
1360 cp_cv_quals cv_qualifiers,
1361 cp_virt_specifiers virt_specifiers,
1362 tree exception_specification,
1363 tree late_return_type)
1365 cp_declarator *declarator;
1367 declarator = make_declarator (cdk_function);
1368 declarator->declarator = target;
1369 declarator->u.function.parameters = parms;
1370 declarator->u.function.qualifiers = cv_qualifiers;
1371 declarator->u.function.virt_specifiers = virt_specifiers;
1372 declarator->u.function.exception_specification = exception_specification;
1373 declarator->u.function.late_return_type = late_return_type;
1374 if (target)
1376 declarator->id_loc = target->id_loc;
1377 declarator->parameter_pack_p = target->parameter_pack_p;
1378 target->parameter_pack_p = false;
1380 else
1381 declarator->parameter_pack_p = false;
1383 return declarator;
1386 /* Make a declarator for an array of BOUNDS elements, each of which is
1387 defined by ELEMENT. */
1389 cp_declarator *
1390 make_array_declarator (cp_declarator *element, tree bounds)
1392 cp_declarator *declarator;
1394 declarator = make_declarator (cdk_array);
1395 declarator->declarator = element;
1396 declarator->u.array.bounds = bounds;
1397 if (element)
1399 declarator->id_loc = element->id_loc;
1400 declarator->parameter_pack_p = element->parameter_pack_p;
1401 element->parameter_pack_p = false;
1403 else
1404 declarator->parameter_pack_p = false;
1406 return declarator;
1409 /* Determine whether the declarator we've seen so far can be a
1410 parameter pack, when followed by an ellipsis. */
1411 static bool
1412 declarator_can_be_parameter_pack (cp_declarator *declarator)
1414 /* Search for a declarator name, or any other declarator that goes
1415 after the point where the ellipsis could appear in a parameter
1416 pack. If we find any of these, then this declarator can not be
1417 made into a parameter pack. */
1418 bool found = false;
1419 while (declarator && !found)
1421 switch ((int)declarator->kind)
1423 case cdk_id:
1424 case cdk_array:
1425 found = true;
1426 break;
1428 case cdk_error:
1429 return true;
1431 default:
1432 declarator = declarator->declarator;
1433 break;
1437 return !found;
1440 cp_parameter_declarator *no_parameters;
1442 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1443 DECLARATOR and DEFAULT_ARGUMENT. */
1445 cp_parameter_declarator *
1446 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1447 cp_declarator *declarator,
1448 tree default_argument)
1450 cp_parameter_declarator *parameter;
1452 parameter = ((cp_parameter_declarator *)
1453 alloc_declarator (sizeof (cp_parameter_declarator)));
1454 parameter->next = NULL;
1455 if (decl_specifiers)
1456 parameter->decl_specifiers = *decl_specifiers;
1457 else
1458 clear_decl_specs (&parameter->decl_specifiers);
1459 parameter->declarator = declarator;
1460 parameter->default_argument = default_argument;
1461 parameter->ellipsis_p = false;
1463 return parameter;
1466 /* Returns true iff DECLARATOR is a declaration for a function. */
1468 static bool
1469 function_declarator_p (const cp_declarator *declarator)
1471 while (declarator)
1473 if (declarator->kind == cdk_function
1474 && declarator->declarator->kind == cdk_id)
1475 return true;
1476 if (declarator->kind == cdk_id
1477 || declarator->kind == cdk_error)
1478 return false;
1479 declarator = declarator->declarator;
1481 return false;
1484 /* The parser. */
1486 /* Overview
1487 --------
1489 A cp_parser parses the token stream as specified by the C++
1490 grammar. Its job is purely parsing, not semantic analysis. For
1491 example, the parser breaks the token stream into declarators,
1492 expressions, statements, and other similar syntactic constructs.
1493 It does not check that the types of the expressions on either side
1494 of an assignment-statement are compatible, or that a function is
1495 not declared with a parameter of type `void'.
1497 The parser invokes routines elsewhere in the compiler to perform
1498 semantic analysis and to build up the abstract syntax tree for the
1499 code processed.
1501 The parser (and the template instantiation code, which is, in a
1502 way, a close relative of parsing) are the only parts of the
1503 compiler that should be calling push_scope and pop_scope, or
1504 related functions. The parser (and template instantiation code)
1505 keeps track of what scope is presently active; everything else
1506 should simply honor that. (The code that generates static
1507 initializers may also need to set the scope, in order to check
1508 access control correctly when emitting the initializers.)
1510 Methodology
1511 -----------
1513 The parser is of the standard recursive-descent variety. Upcoming
1514 tokens in the token stream are examined in order to determine which
1515 production to use when parsing a non-terminal. Some C++ constructs
1516 require arbitrary look ahead to disambiguate. For example, it is
1517 impossible, in the general case, to tell whether a statement is an
1518 expression or declaration without scanning the entire statement.
1519 Therefore, the parser is capable of "parsing tentatively." When the
1520 parser is not sure what construct comes next, it enters this mode.
1521 Then, while we attempt to parse the construct, the parser queues up
1522 error messages, rather than issuing them immediately, and saves the
1523 tokens it consumes. If the construct is parsed successfully, the
1524 parser "commits", i.e., it issues any queued error messages and
1525 the tokens that were being preserved are permanently discarded.
1526 If, however, the construct is not parsed successfully, the parser
1527 rolls back its state completely so that it can resume parsing using
1528 a different alternative.
1530 Future Improvements
1531 -------------------
1533 The performance of the parser could probably be improved substantially.
1534 We could often eliminate the need to parse tentatively by looking ahead
1535 a little bit. In some places, this approach might not entirely eliminate
1536 the need to parse tentatively, but it might still speed up the average
1537 case. */
1539 /* Flags that are passed to some parsing functions. These values can
1540 be bitwise-ored together. */
1542 enum
1544 /* No flags. */
1545 CP_PARSER_FLAGS_NONE = 0x0,
1546 /* The construct is optional. If it is not present, then no error
1547 should be issued. */
1548 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1549 /* When parsing a type-specifier, treat user-defined type-names
1550 as non-type identifiers. */
1551 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1552 /* When parsing a type-specifier, do not try to parse a class-specifier
1553 or enum-specifier. */
1554 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1555 /* When parsing a decl-specifier-seq, only allow type-specifier or
1556 constexpr. */
1557 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1560 /* This type is used for parameters and variables which hold
1561 combinations of the above flags. */
1562 typedef int cp_parser_flags;
1564 /* The different kinds of declarators we want to parse. */
1566 typedef enum cp_parser_declarator_kind
1568 /* We want an abstract declarator. */
1569 CP_PARSER_DECLARATOR_ABSTRACT,
1570 /* We want a named declarator. */
1571 CP_PARSER_DECLARATOR_NAMED,
1572 /* We don't mind, but the name must be an unqualified-id. */
1573 CP_PARSER_DECLARATOR_EITHER
1574 } cp_parser_declarator_kind;
1576 /* The precedence values used to parse binary expressions. The minimum value
1577 of PREC must be 1, because zero is reserved to quickly discriminate
1578 binary operators from other tokens. */
1580 enum cp_parser_prec
1582 PREC_NOT_OPERATOR,
1583 PREC_LOGICAL_OR_EXPRESSION,
1584 PREC_LOGICAL_AND_EXPRESSION,
1585 PREC_INCLUSIVE_OR_EXPRESSION,
1586 PREC_EXCLUSIVE_OR_EXPRESSION,
1587 PREC_AND_EXPRESSION,
1588 PREC_EQUALITY_EXPRESSION,
1589 PREC_RELATIONAL_EXPRESSION,
1590 PREC_SHIFT_EXPRESSION,
1591 PREC_ADDITIVE_EXPRESSION,
1592 PREC_MULTIPLICATIVE_EXPRESSION,
1593 PREC_PM_EXPRESSION,
1594 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1597 /* A mapping from a token type to a corresponding tree node type, with a
1598 precedence value. */
1600 typedef struct cp_parser_binary_operations_map_node
1602 /* The token type. */
1603 enum cpp_ttype token_type;
1604 /* The corresponding tree code. */
1605 enum tree_code tree_type;
1606 /* The precedence of this operator. */
1607 enum cp_parser_prec prec;
1608 } cp_parser_binary_operations_map_node;
1610 typedef struct cp_parser_expression_stack_entry
1612 /* Left hand side of the binary operation we are currently
1613 parsing. */
1614 tree lhs;
1615 /* Original tree code for left hand side, if it was a binary
1616 expression itself (used for -Wparentheses). */
1617 enum tree_code lhs_type;
1618 /* Tree code for the binary operation we are parsing. */
1619 enum tree_code tree_type;
1620 /* Precedence of the binary operation we are parsing. */
1621 enum cp_parser_prec prec;
1622 /* Location of the binary operation we are parsing. */
1623 location_t loc;
1624 } cp_parser_expression_stack_entry;
1626 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1627 entries because precedence levels on the stack are monotonically
1628 increasing. */
1629 typedef struct cp_parser_expression_stack_entry
1630 cp_parser_expression_stack[NUM_PREC_VALUES];
1632 /* Prototypes. */
1634 /* Constructors and destructors. */
1636 static cp_parser_context *cp_parser_context_new
1637 (cp_parser_context *);
1639 /* Class variables. */
1641 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1643 /* The operator-precedence table used by cp_parser_binary_expression.
1644 Transformed into an associative array (binops_by_token) by
1645 cp_parser_new. */
1647 static const cp_parser_binary_operations_map_node binops[] = {
1648 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1649 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1651 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1652 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1653 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1655 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1656 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1658 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1659 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1661 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1662 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1663 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1664 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1666 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1667 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1669 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1671 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1673 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1675 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1677 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1680 /* The same as binops, but initialized by cp_parser_new so that
1681 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1682 for speed. */
1683 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1685 /* Constructors and destructors. */
1687 /* Construct a new context. The context below this one on the stack
1688 is given by NEXT. */
1690 static cp_parser_context *
1691 cp_parser_context_new (cp_parser_context* next)
1693 cp_parser_context *context;
1695 /* Allocate the storage. */
1696 if (cp_parser_context_free_list != NULL)
1698 /* Pull the first entry from the free list. */
1699 context = cp_parser_context_free_list;
1700 cp_parser_context_free_list = context->next;
1701 memset (context, 0, sizeof (*context));
1703 else
1704 context = ggc_alloc_cleared_cp_parser_context ();
1706 /* No errors have occurred yet in this context. */
1707 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1708 /* If this is not the bottommost context, copy information that we
1709 need from the previous context. */
1710 if (next)
1712 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1713 expression, then we are parsing one in this context, too. */
1714 context->object_type = next->object_type;
1715 /* Thread the stack. */
1716 context->next = next;
1719 return context;
1722 /* Managing the unparsed function queues. */
1724 #define unparsed_funs_with_default_args \
1725 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).funs_with_default_args
1726 #define unparsed_funs_with_definitions \
1727 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).funs_with_definitions
1728 #define unparsed_nsdmis \
1729 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues).nsdmis
1731 static void
1732 push_unparsed_function_queues (cp_parser *parser)
1734 VEC_safe_push (cp_unparsed_functions_entry, gc,
1735 parser->unparsed_queues, NULL);
1736 unparsed_funs_with_default_args = NULL;
1737 unparsed_funs_with_definitions = make_tree_vector ();
1738 unparsed_nsdmis = NULL;
1741 static void
1742 pop_unparsed_function_queues (cp_parser *parser)
1744 release_tree_vector (unparsed_funs_with_definitions);
1745 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1748 /* Prototypes. */
1750 /* Constructors and destructors. */
1752 static cp_parser *cp_parser_new
1753 (void);
1755 /* Routines to parse various constructs.
1757 Those that return `tree' will return the error_mark_node (rather
1758 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1759 Sometimes, they will return an ordinary node if error-recovery was
1760 attempted, even though a parse error occurred. So, to check
1761 whether or not a parse error occurred, you should always use
1762 cp_parser_error_occurred. If the construct is optional (indicated
1763 either by an `_opt' in the name of the function that does the
1764 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1765 the construct is not present. */
1767 /* Lexical conventions [gram.lex] */
1769 static tree cp_parser_identifier
1770 (cp_parser *);
1771 static tree cp_parser_string_literal
1772 (cp_parser *, bool, bool);
1773 static tree cp_parser_userdef_char_literal
1774 (cp_parser *);
1775 static tree cp_parser_userdef_string_literal
1776 (cp_token *);
1777 static tree cp_parser_userdef_numeric_literal
1778 (cp_parser *);
1780 /* Basic concepts [gram.basic] */
1782 static bool cp_parser_translation_unit
1783 (cp_parser *);
1785 /* Expressions [gram.expr] */
1787 static tree cp_parser_primary_expression
1788 (cp_parser *, bool, bool, bool, cp_id_kind *);
1789 static tree cp_parser_id_expression
1790 (cp_parser *, bool, bool, bool *, bool, bool);
1791 static tree cp_parser_unqualified_id
1792 (cp_parser *, bool, bool, bool, bool);
1793 static tree cp_parser_nested_name_specifier_opt
1794 (cp_parser *, bool, bool, bool, bool);
1795 static tree cp_parser_nested_name_specifier
1796 (cp_parser *, bool, bool, bool, bool);
1797 static tree cp_parser_qualifying_entity
1798 (cp_parser *, bool, bool, bool, bool, bool);
1799 static tree cp_parser_postfix_expression
1800 (cp_parser *, bool, bool, bool, cp_id_kind *);
1801 static tree cp_parser_postfix_open_square_expression
1802 (cp_parser *, tree, bool);
1803 static tree cp_parser_postfix_dot_deref_expression
1804 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1805 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1806 (cp_parser *, int, bool, bool, bool *);
1807 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1808 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1809 static void cp_parser_pseudo_destructor_name
1810 (cp_parser *, tree *, tree *);
1811 static tree cp_parser_unary_expression
1812 (cp_parser *, bool, bool, cp_id_kind *);
1813 static enum tree_code cp_parser_unary_operator
1814 (cp_token *);
1815 static tree cp_parser_new_expression
1816 (cp_parser *);
1817 static VEC(tree,gc) *cp_parser_new_placement
1818 (cp_parser *);
1819 static tree cp_parser_new_type_id
1820 (cp_parser *, tree *);
1821 static cp_declarator *cp_parser_new_declarator_opt
1822 (cp_parser *);
1823 static cp_declarator *cp_parser_direct_new_declarator
1824 (cp_parser *);
1825 static VEC(tree,gc) *cp_parser_new_initializer
1826 (cp_parser *);
1827 static tree cp_parser_delete_expression
1828 (cp_parser *);
1829 static tree cp_parser_cast_expression
1830 (cp_parser *, bool, bool, cp_id_kind *);
1831 static tree cp_parser_binary_expression
1832 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1833 static tree cp_parser_question_colon_clause
1834 (cp_parser *, tree);
1835 static tree cp_parser_assignment_expression
1836 (cp_parser *, bool, cp_id_kind *);
1837 static enum tree_code cp_parser_assignment_operator_opt
1838 (cp_parser *);
1839 static tree cp_parser_expression
1840 (cp_parser *, bool, cp_id_kind *);
1841 static tree cp_parser_constant_expression
1842 (cp_parser *, bool, bool *);
1843 static tree cp_parser_builtin_offsetof
1844 (cp_parser *);
1845 static tree cp_parser_lambda_expression
1846 (cp_parser *);
1847 static void cp_parser_lambda_introducer
1848 (cp_parser *, tree);
1849 static bool cp_parser_lambda_declarator_opt
1850 (cp_parser *, tree);
1851 static void cp_parser_lambda_body
1852 (cp_parser *, tree);
1854 /* Statements [gram.stmt.stmt] */
1856 static void cp_parser_statement
1857 (cp_parser *, tree, bool, bool *);
1858 static void cp_parser_label_for_labeled_statement
1859 (cp_parser *);
1860 static tree cp_parser_expression_statement
1861 (cp_parser *, tree);
1862 static tree cp_parser_compound_statement
1863 (cp_parser *, tree, bool, bool);
1864 static void cp_parser_statement_seq_opt
1865 (cp_parser *, tree);
1866 static tree cp_parser_selection_statement
1867 (cp_parser *, bool *);
1868 static tree cp_parser_condition
1869 (cp_parser *);
1870 static tree cp_parser_iteration_statement
1871 (cp_parser *);
1872 static bool cp_parser_for_init_statement
1873 (cp_parser *, tree *decl);
1874 static tree cp_parser_for
1875 (cp_parser *);
1876 static tree cp_parser_c_for
1877 (cp_parser *, tree, tree);
1878 static tree cp_parser_range_for
1879 (cp_parser *, tree, tree, tree);
1880 static void do_range_for_auto_deduction
1881 (tree, tree);
1882 static tree cp_parser_perform_range_for_lookup
1883 (tree, tree *, tree *);
1884 static tree cp_parser_range_for_member_function
1885 (tree, tree);
1886 static tree cp_parser_jump_statement
1887 (cp_parser *);
1888 static void cp_parser_declaration_statement
1889 (cp_parser *);
1891 static tree cp_parser_implicitly_scoped_statement
1892 (cp_parser *, bool *);
1893 static void cp_parser_already_scoped_statement
1894 (cp_parser *);
1896 /* Declarations [gram.dcl.dcl] */
1898 static void cp_parser_declaration_seq_opt
1899 (cp_parser *);
1900 static void cp_parser_declaration
1901 (cp_parser *);
1902 static void cp_parser_block_declaration
1903 (cp_parser *, bool);
1904 static void cp_parser_simple_declaration
1905 (cp_parser *, bool, tree *);
1906 static void cp_parser_decl_specifier_seq
1907 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1908 static tree cp_parser_storage_class_specifier_opt
1909 (cp_parser *);
1910 static tree cp_parser_function_specifier_opt
1911 (cp_parser *, cp_decl_specifier_seq *);
1912 static tree cp_parser_type_specifier
1913 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1914 int *, bool *);
1915 static tree cp_parser_simple_type_specifier
1916 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1917 static tree cp_parser_type_name
1918 (cp_parser *);
1919 static tree cp_parser_nonclass_name
1920 (cp_parser* parser);
1921 static tree cp_parser_elaborated_type_specifier
1922 (cp_parser *, bool, bool);
1923 static tree cp_parser_enum_specifier
1924 (cp_parser *);
1925 static void cp_parser_enumerator_list
1926 (cp_parser *, tree);
1927 static void cp_parser_enumerator_definition
1928 (cp_parser *, tree);
1929 static tree cp_parser_namespace_name
1930 (cp_parser *);
1931 static void cp_parser_namespace_definition
1932 (cp_parser *);
1933 static void cp_parser_namespace_body
1934 (cp_parser *);
1935 static tree cp_parser_qualified_namespace_specifier
1936 (cp_parser *);
1937 static void cp_parser_namespace_alias_definition
1938 (cp_parser *);
1939 static bool cp_parser_using_declaration
1940 (cp_parser *, bool);
1941 static void cp_parser_using_directive
1942 (cp_parser *);
1943 static tree cp_parser_alias_declaration
1944 (cp_parser *);
1945 static void cp_parser_asm_definition
1946 (cp_parser *);
1947 static void cp_parser_linkage_specification
1948 (cp_parser *);
1949 static void cp_parser_static_assert
1950 (cp_parser *, bool);
1951 static tree cp_parser_decltype
1952 (cp_parser *);
1954 /* Declarators [gram.dcl.decl] */
1956 static tree cp_parser_init_declarator
1957 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1958 static cp_declarator *cp_parser_declarator
1959 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1960 static cp_declarator *cp_parser_direct_declarator
1961 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1962 static enum tree_code cp_parser_ptr_operator
1963 (cp_parser *, tree *, cp_cv_quals *);
1964 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1965 (cp_parser *);
1966 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1967 (cp_parser *);
1968 static tree cp_parser_late_return_type_opt
1969 (cp_parser *, cp_cv_quals);
1970 static tree cp_parser_declarator_id
1971 (cp_parser *, bool);
1972 static tree cp_parser_type_id
1973 (cp_parser *);
1974 static tree cp_parser_template_type_arg
1975 (cp_parser *);
1976 static tree cp_parser_trailing_type_id (cp_parser *);
1977 static tree cp_parser_type_id_1
1978 (cp_parser *, bool, bool);
1979 static void cp_parser_type_specifier_seq
1980 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1981 static tree cp_parser_parameter_declaration_clause
1982 (cp_parser *);
1983 static tree cp_parser_parameter_declaration_list
1984 (cp_parser *, bool *);
1985 static cp_parameter_declarator *cp_parser_parameter_declaration
1986 (cp_parser *, bool, bool *);
1987 static tree cp_parser_default_argument
1988 (cp_parser *, bool);
1989 static void cp_parser_function_body
1990 (cp_parser *, bool);
1991 static tree cp_parser_initializer
1992 (cp_parser *, bool *, bool *);
1993 static tree cp_parser_initializer_clause
1994 (cp_parser *, bool *);
1995 static tree cp_parser_braced_list
1996 (cp_parser*, bool*);
1997 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1998 (cp_parser *, bool *);
2000 static bool cp_parser_ctor_initializer_opt_and_function_body
2001 (cp_parser *, bool);
2003 /* Classes [gram.class] */
2005 static tree cp_parser_class_name
2006 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2007 static tree cp_parser_class_specifier
2008 (cp_parser *);
2009 static tree cp_parser_class_head
2010 (cp_parser *, bool *);
2011 static enum tag_types cp_parser_class_key
2012 (cp_parser *);
2013 static void cp_parser_member_specification_opt
2014 (cp_parser *);
2015 static void cp_parser_member_declaration
2016 (cp_parser *);
2017 static tree cp_parser_pure_specifier
2018 (cp_parser *);
2019 static tree cp_parser_constant_initializer
2020 (cp_parser *);
2022 /* Derived classes [gram.class.derived] */
2024 static tree cp_parser_base_clause
2025 (cp_parser *);
2026 static tree cp_parser_base_specifier
2027 (cp_parser *);
2029 /* Special member functions [gram.special] */
2031 static tree cp_parser_conversion_function_id
2032 (cp_parser *);
2033 static tree cp_parser_conversion_type_id
2034 (cp_parser *);
2035 static cp_declarator *cp_parser_conversion_declarator_opt
2036 (cp_parser *);
2037 static bool cp_parser_ctor_initializer_opt
2038 (cp_parser *);
2039 static void cp_parser_mem_initializer_list
2040 (cp_parser *);
2041 static tree cp_parser_mem_initializer
2042 (cp_parser *);
2043 static tree cp_parser_mem_initializer_id
2044 (cp_parser *);
2046 /* Overloading [gram.over] */
2048 static tree cp_parser_operator_function_id
2049 (cp_parser *);
2050 static tree cp_parser_operator
2051 (cp_parser *);
2053 /* Templates [gram.temp] */
2055 static void cp_parser_template_declaration
2056 (cp_parser *, bool);
2057 static tree cp_parser_template_parameter_list
2058 (cp_parser *);
2059 static tree cp_parser_template_parameter
2060 (cp_parser *, bool *, bool *);
2061 static tree cp_parser_type_parameter
2062 (cp_parser *, bool *);
2063 static tree cp_parser_template_id
2064 (cp_parser *, bool, bool, enum tag_types, bool);
2065 static tree cp_parser_template_name
2066 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2067 static tree cp_parser_template_argument_list
2068 (cp_parser *);
2069 static tree cp_parser_template_argument
2070 (cp_parser *);
2071 static void cp_parser_explicit_instantiation
2072 (cp_parser *);
2073 static void cp_parser_explicit_specialization
2074 (cp_parser *);
2076 /* Exception handling [gram.exception] */
2078 static tree cp_parser_try_block
2079 (cp_parser *);
2080 static bool cp_parser_function_try_block
2081 (cp_parser *);
2082 static void cp_parser_handler_seq
2083 (cp_parser *);
2084 static void cp_parser_handler
2085 (cp_parser *);
2086 static tree cp_parser_exception_declaration
2087 (cp_parser *);
2088 static tree cp_parser_throw_expression
2089 (cp_parser *);
2090 static tree cp_parser_exception_specification_opt
2091 (cp_parser *);
2092 static tree cp_parser_type_id_list
2093 (cp_parser *);
2095 /* GNU Extensions */
2097 static tree cp_parser_asm_specification_opt
2098 (cp_parser *);
2099 static tree cp_parser_asm_operand_list
2100 (cp_parser *);
2101 static tree cp_parser_asm_clobber_list
2102 (cp_parser *);
2103 static tree cp_parser_asm_label_list
2104 (cp_parser *);
2105 static tree cp_parser_attributes_opt
2106 (cp_parser *);
2107 static tree cp_parser_attribute_list
2108 (cp_parser *);
2109 static bool cp_parser_extension_opt
2110 (cp_parser *, int *);
2111 static void cp_parser_label_declaration
2112 (cp_parser *);
2114 /* Transactional Memory Extensions */
2116 static tree cp_parser_transaction
2117 (cp_parser *, enum rid);
2118 static tree cp_parser_transaction_expression
2119 (cp_parser *, enum rid);
2120 static bool cp_parser_function_transaction
2121 (cp_parser *, enum rid);
2122 static tree cp_parser_transaction_cancel
2123 (cp_parser *);
2125 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2126 static bool cp_parser_pragma
2127 (cp_parser *, enum pragma_context);
2129 /* Objective-C++ Productions */
2131 static tree cp_parser_objc_message_receiver
2132 (cp_parser *);
2133 static tree cp_parser_objc_message_args
2134 (cp_parser *);
2135 static tree cp_parser_objc_message_expression
2136 (cp_parser *);
2137 static tree cp_parser_objc_encode_expression
2138 (cp_parser *);
2139 static tree cp_parser_objc_defs_expression
2140 (cp_parser *);
2141 static tree cp_parser_objc_protocol_expression
2142 (cp_parser *);
2143 static tree cp_parser_objc_selector_expression
2144 (cp_parser *);
2145 static tree cp_parser_objc_expression
2146 (cp_parser *);
2147 static bool cp_parser_objc_selector_p
2148 (enum cpp_ttype);
2149 static tree cp_parser_objc_selector
2150 (cp_parser *);
2151 static tree cp_parser_objc_protocol_refs_opt
2152 (cp_parser *);
2153 static void cp_parser_objc_declaration
2154 (cp_parser *, tree);
2155 static tree cp_parser_objc_statement
2156 (cp_parser *);
2157 static bool cp_parser_objc_valid_prefix_attributes
2158 (cp_parser *, tree *);
2159 static void cp_parser_objc_at_property_declaration
2160 (cp_parser *) ;
2161 static void cp_parser_objc_at_synthesize_declaration
2162 (cp_parser *) ;
2163 static void cp_parser_objc_at_dynamic_declaration
2164 (cp_parser *) ;
2165 static tree cp_parser_objc_struct_declaration
2166 (cp_parser *) ;
2168 /* Utility Routines */
2170 static tree cp_parser_lookup_name
2171 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2172 static tree cp_parser_lookup_name_simple
2173 (cp_parser *, tree, location_t);
2174 static tree cp_parser_maybe_treat_template_as_class
2175 (tree, bool);
2176 static bool cp_parser_check_declarator_template_parameters
2177 (cp_parser *, cp_declarator *, location_t);
2178 static bool cp_parser_check_template_parameters
2179 (cp_parser *, unsigned, location_t, cp_declarator *);
2180 static tree cp_parser_simple_cast_expression
2181 (cp_parser *);
2182 static tree cp_parser_global_scope_opt
2183 (cp_parser *, bool);
2184 static bool cp_parser_constructor_declarator_p
2185 (cp_parser *, bool);
2186 static tree cp_parser_function_definition_from_specifiers_and_declarator
2187 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2188 static tree cp_parser_function_definition_after_declarator
2189 (cp_parser *, bool);
2190 static void cp_parser_template_declaration_after_export
2191 (cp_parser *, bool);
2192 static void cp_parser_perform_template_parameter_access_checks
2193 (VEC (deferred_access_check,gc)*);
2194 static tree cp_parser_single_declaration
2195 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2196 static tree cp_parser_functional_cast
2197 (cp_parser *, tree);
2198 static tree cp_parser_save_member_function_body
2199 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2200 static tree cp_parser_save_nsdmi
2201 (cp_parser *);
2202 static tree cp_parser_enclosed_template_argument_list
2203 (cp_parser *);
2204 static void cp_parser_save_default_args
2205 (cp_parser *, tree);
2206 static void cp_parser_late_parsing_for_member
2207 (cp_parser *, tree);
2208 static tree cp_parser_late_parse_one_default_arg
2209 (cp_parser *, tree, tree, tree);
2210 static void cp_parser_late_parsing_nsdmi
2211 (cp_parser *, tree);
2212 static void cp_parser_late_parsing_default_args
2213 (cp_parser *, tree);
2214 static tree cp_parser_sizeof_operand
2215 (cp_parser *, enum rid);
2216 static tree cp_parser_trait_expr
2217 (cp_parser *, enum rid);
2218 static bool cp_parser_declares_only_class_p
2219 (cp_parser *);
2220 static void cp_parser_set_storage_class
2221 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2222 static void cp_parser_set_decl_spec_type
2223 (cp_decl_specifier_seq *, tree, location_t, bool);
2224 static void set_and_check_decl_spec_loc
2225 (cp_decl_specifier_seq *decl_specs,
2226 cp_decl_spec ds, source_location location);
2227 static bool cp_parser_friend_p
2228 (const cp_decl_specifier_seq *);
2229 static void cp_parser_required_error
2230 (cp_parser *, required_token, bool);
2231 static cp_token *cp_parser_require
2232 (cp_parser *, enum cpp_ttype, required_token);
2233 static cp_token *cp_parser_require_keyword
2234 (cp_parser *, enum rid, required_token);
2235 static bool cp_parser_token_starts_function_definition_p
2236 (cp_token *);
2237 static bool cp_parser_next_token_starts_class_definition_p
2238 (cp_parser *);
2239 static bool cp_parser_next_token_ends_template_argument_p
2240 (cp_parser *);
2241 static bool cp_parser_nth_token_starts_template_argument_list_p
2242 (cp_parser *, size_t);
2243 static enum tag_types cp_parser_token_is_class_key
2244 (cp_token *);
2245 static void cp_parser_check_class_key
2246 (enum tag_types, tree type);
2247 static void cp_parser_check_access_in_redeclaration
2248 (tree type, location_t location);
2249 static bool cp_parser_optional_template_keyword
2250 (cp_parser *);
2251 static void cp_parser_pre_parsed_nested_name_specifier
2252 (cp_parser *);
2253 static bool cp_parser_cache_group
2254 (cp_parser *, enum cpp_ttype, unsigned);
2255 static tree cp_parser_cache_defarg
2256 (cp_parser *parser, bool nsdmi);
2257 static void cp_parser_parse_tentatively
2258 (cp_parser *);
2259 static void cp_parser_commit_to_tentative_parse
2260 (cp_parser *);
2261 static void cp_parser_abort_tentative_parse
2262 (cp_parser *);
2263 static bool cp_parser_parse_definitely
2264 (cp_parser *);
2265 static inline bool cp_parser_parsing_tentatively
2266 (cp_parser *);
2267 static bool cp_parser_uncommitted_to_tentative_parse_p
2268 (cp_parser *);
2269 static void cp_parser_error
2270 (cp_parser *, const char *);
2271 static void cp_parser_name_lookup_error
2272 (cp_parser *, tree, tree, name_lookup_error, location_t);
2273 static bool cp_parser_simulate_error
2274 (cp_parser *);
2275 static bool cp_parser_check_type_definition
2276 (cp_parser *);
2277 static void cp_parser_check_for_definition_in_return_type
2278 (cp_declarator *, tree, location_t type_location);
2279 static void cp_parser_check_for_invalid_template_id
2280 (cp_parser *, tree, enum tag_types, location_t location);
2281 static bool cp_parser_non_integral_constant_expression
2282 (cp_parser *, non_integral_constant);
2283 static void cp_parser_diagnose_invalid_type_name
2284 (cp_parser *, tree, tree, location_t);
2285 static bool cp_parser_parse_and_diagnose_invalid_type_name
2286 (cp_parser *);
2287 static int cp_parser_skip_to_closing_parenthesis
2288 (cp_parser *, bool, bool, bool);
2289 static void cp_parser_skip_to_end_of_statement
2290 (cp_parser *);
2291 static void cp_parser_consume_semicolon_at_end_of_statement
2292 (cp_parser *);
2293 static void cp_parser_skip_to_end_of_block_or_statement
2294 (cp_parser *);
2295 static bool cp_parser_skip_to_closing_brace
2296 (cp_parser *);
2297 static void cp_parser_skip_to_end_of_template_parameter_list
2298 (cp_parser *);
2299 static void cp_parser_skip_to_pragma_eol
2300 (cp_parser*, cp_token *);
2301 static bool cp_parser_error_occurred
2302 (cp_parser *);
2303 static bool cp_parser_allow_gnu_extensions_p
2304 (cp_parser *);
2305 static bool cp_parser_is_pure_string_literal
2306 (cp_token *);
2307 static bool cp_parser_is_string_literal
2308 (cp_token *);
2309 static bool cp_parser_is_keyword
2310 (cp_token *, enum rid);
2311 static tree cp_parser_make_typename_type
2312 (cp_parser *, tree, tree, location_t location);
2313 static cp_declarator * cp_parser_make_indirect_declarator
2314 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2316 /* Returns nonzero if we are parsing tentatively. */
2318 static inline bool
2319 cp_parser_parsing_tentatively (cp_parser* parser)
2321 return parser->context->next != NULL;
2324 /* Returns nonzero if TOKEN is a string literal. */
2326 static bool
2327 cp_parser_is_pure_string_literal (cp_token* token)
2329 return (token->type == CPP_STRING ||
2330 token->type == CPP_STRING16 ||
2331 token->type == CPP_STRING32 ||
2332 token->type == CPP_WSTRING ||
2333 token->type == CPP_UTF8STRING);
2336 /* Returns nonzero if TOKEN is a string literal
2337 of a user-defined string literal. */
2339 static bool
2340 cp_parser_is_string_literal (cp_token* token)
2342 return (cp_parser_is_pure_string_literal (token) ||
2343 token->type == CPP_STRING_USERDEF ||
2344 token->type == CPP_STRING16_USERDEF ||
2345 token->type == CPP_STRING32_USERDEF ||
2346 token->type == CPP_WSTRING_USERDEF ||
2347 token->type == CPP_UTF8STRING_USERDEF);
2350 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2352 static bool
2353 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2355 return token->keyword == keyword;
2358 /* If not parsing tentatively, issue a diagnostic of the form
2359 FILE:LINE: MESSAGE before TOKEN
2360 where TOKEN is the next token in the input stream. MESSAGE
2361 (specified by the caller) is usually of the form "expected
2362 OTHER-TOKEN". */
2364 static void
2365 cp_parser_error (cp_parser* parser, const char* gmsgid)
2367 if (!cp_parser_simulate_error (parser))
2369 cp_token *token = cp_lexer_peek_token (parser->lexer);
2370 /* This diagnostic makes more sense if it is tagged to the line
2371 of the token we just peeked at. */
2372 cp_lexer_set_source_position_from_token (token);
2374 if (token->type == CPP_PRAGMA)
2376 error_at (token->location,
2377 "%<#pragma%> is not allowed here");
2378 cp_parser_skip_to_pragma_eol (parser, token);
2379 return;
2382 c_parse_error (gmsgid,
2383 /* Because c_parser_error does not understand
2384 CPP_KEYWORD, keywords are treated like
2385 identifiers. */
2386 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2387 token->u.value, token->flags);
2391 /* Issue an error about name-lookup failing. NAME is the
2392 IDENTIFIER_NODE DECL is the result of
2393 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2394 the thing that we hoped to find. */
2396 static void
2397 cp_parser_name_lookup_error (cp_parser* parser,
2398 tree name,
2399 tree decl,
2400 name_lookup_error desired,
2401 location_t location)
2403 /* If name lookup completely failed, tell the user that NAME was not
2404 declared. */
2405 if (decl == error_mark_node)
2407 if (parser->scope && parser->scope != global_namespace)
2408 error_at (location, "%<%E::%E%> has not been declared",
2409 parser->scope, name);
2410 else if (parser->scope == global_namespace)
2411 error_at (location, "%<::%E%> has not been declared", name);
2412 else if (parser->object_scope
2413 && !CLASS_TYPE_P (parser->object_scope))
2414 error_at (location, "request for member %qE in non-class type %qT",
2415 name, parser->object_scope);
2416 else if (parser->object_scope)
2417 error_at (location, "%<%T::%E%> has not been declared",
2418 parser->object_scope, name);
2419 else
2420 error_at (location, "%qE has not been declared", name);
2422 else if (parser->scope && parser->scope != global_namespace)
2424 switch (desired)
2426 case NLE_TYPE:
2427 error_at (location, "%<%E::%E%> is not a type",
2428 parser->scope, name);
2429 break;
2430 case NLE_CXX98:
2431 error_at (location, "%<%E::%E%> is not a class or namespace",
2432 parser->scope, name);
2433 break;
2434 case NLE_NOT_CXX98:
2435 error_at (location,
2436 "%<%E::%E%> is not a class, namespace, or enumeration",
2437 parser->scope, name);
2438 break;
2439 default:
2440 gcc_unreachable ();
2444 else if (parser->scope == global_namespace)
2446 switch (desired)
2448 case NLE_TYPE:
2449 error_at (location, "%<::%E%> is not a type", name);
2450 break;
2451 case NLE_CXX98:
2452 error_at (location, "%<::%E%> is not a class or namespace", name);
2453 break;
2454 case NLE_NOT_CXX98:
2455 error_at (location,
2456 "%<::%E%> is not a class, namespace, or enumeration",
2457 name);
2458 break;
2459 default:
2460 gcc_unreachable ();
2463 else
2465 switch (desired)
2467 case NLE_TYPE:
2468 error_at (location, "%qE is not a type", name);
2469 break;
2470 case NLE_CXX98:
2471 error_at (location, "%qE is not a class or namespace", name);
2472 break;
2473 case NLE_NOT_CXX98:
2474 error_at (location,
2475 "%qE is not a class, namespace, or enumeration", name);
2476 break;
2477 default:
2478 gcc_unreachable ();
2483 /* If we are parsing tentatively, remember that an error has occurred
2484 during this tentative parse. Returns true if the error was
2485 simulated; false if a message should be issued by the caller. */
2487 static bool
2488 cp_parser_simulate_error (cp_parser* parser)
2490 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2492 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2493 return true;
2495 return false;
2498 /* This function is called when a type is defined. If type
2499 definitions are forbidden at this point, an error message is
2500 issued. */
2502 static bool
2503 cp_parser_check_type_definition (cp_parser* parser)
2505 /* If types are forbidden here, issue a message. */
2506 if (parser->type_definition_forbidden_message)
2508 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2509 in the message need to be interpreted. */
2510 error (parser->type_definition_forbidden_message);
2511 return false;
2513 return true;
2516 /* This function is called when the DECLARATOR is processed. The TYPE
2517 was a type defined in the decl-specifiers. If it is invalid to
2518 define a type in the decl-specifiers for DECLARATOR, an error is
2519 issued. TYPE_LOCATION is the location of TYPE and is used
2520 for error reporting. */
2522 static void
2523 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2524 tree type, location_t type_location)
2526 /* [dcl.fct] forbids type definitions in return types.
2527 Unfortunately, it's not easy to know whether or not we are
2528 processing a return type until after the fact. */
2529 while (declarator
2530 && (declarator->kind == cdk_pointer
2531 || declarator->kind == cdk_reference
2532 || declarator->kind == cdk_ptrmem))
2533 declarator = declarator->declarator;
2534 if (declarator
2535 && declarator->kind == cdk_function)
2537 error_at (type_location,
2538 "new types may not be defined in a return type");
2539 inform (type_location,
2540 "(perhaps a semicolon is missing after the definition of %qT)",
2541 type);
2545 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2546 "<" in any valid C++ program. If the next token is indeed "<",
2547 issue a message warning the user about what appears to be an
2548 invalid attempt to form a template-id. LOCATION is the location
2549 of the type-specifier (TYPE) */
2551 static void
2552 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2553 tree type,
2554 enum tag_types tag_type,
2555 location_t location)
2557 cp_token_position start = 0;
2559 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2561 if (TYPE_P (type))
2562 error_at (location, "%qT is not a template", type);
2563 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2565 if (tag_type != none_type)
2566 error_at (location, "%qE is not a class template", type);
2567 else
2568 error_at (location, "%qE is not a template", type);
2570 else
2571 error_at (location, "invalid template-id");
2572 /* Remember the location of the invalid "<". */
2573 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2574 start = cp_lexer_token_position (parser->lexer, true);
2575 /* Consume the "<". */
2576 cp_lexer_consume_token (parser->lexer);
2577 /* Parse the template arguments. */
2578 cp_parser_enclosed_template_argument_list (parser);
2579 /* Permanently remove the invalid template arguments so that
2580 this error message is not issued again. */
2581 if (start)
2582 cp_lexer_purge_tokens_after (parser->lexer, start);
2586 /* If parsing an integral constant-expression, issue an error message
2587 about the fact that THING appeared and return true. Otherwise,
2588 return false. In either case, set
2589 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2591 static bool
2592 cp_parser_non_integral_constant_expression (cp_parser *parser,
2593 non_integral_constant thing)
2595 parser->non_integral_constant_expression_p = true;
2596 if (parser->integral_constant_expression_p)
2598 if (!parser->allow_non_integral_constant_expression_p)
2600 const char *msg = NULL;
2601 switch (thing)
2603 case NIC_FLOAT:
2604 error ("floating-point literal "
2605 "cannot appear in a constant-expression");
2606 return true;
2607 case NIC_CAST:
2608 error ("a cast to a type other than an integral or "
2609 "enumeration type cannot appear in a "
2610 "constant-expression");
2611 return true;
2612 case NIC_TYPEID:
2613 error ("%<typeid%> operator "
2614 "cannot appear in a constant-expression");
2615 return true;
2616 case NIC_NCC:
2617 error ("non-constant compound literals "
2618 "cannot appear in a constant-expression");
2619 return true;
2620 case NIC_FUNC_CALL:
2621 error ("a function call "
2622 "cannot appear in a constant-expression");
2623 return true;
2624 case NIC_INC:
2625 error ("an increment "
2626 "cannot appear in a constant-expression");
2627 return true;
2628 case NIC_DEC:
2629 error ("an decrement "
2630 "cannot appear in a constant-expression");
2631 return true;
2632 case NIC_ARRAY_REF:
2633 error ("an array reference "
2634 "cannot appear in a constant-expression");
2635 return true;
2636 case NIC_ADDR_LABEL:
2637 error ("the address of a label "
2638 "cannot appear in a constant-expression");
2639 return true;
2640 case NIC_OVERLOADED:
2641 error ("calls to overloaded operators "
2642 "cannot appear in a constant-expression");
2643 return true;
2644 case NIC_ASSIGNMENT:
2645 error ("an assignment cannot appear in a constant-expression");
2646 return true;
2647 case NIC_COMMA:
2648 error ("a comma operator "
2649 "cannot appear in a constant-expression");
2650 return true;
2651 case NIC_CONSTRUCTOR:
2652 error ("a call to a constructor "
2653 "cannot appear in a constant-expression");
2654 return true;
2655 case NIC_TRANSACTION:
2656 error ("a transaction expression "
2657 "cannot appear in a constant-expression");
2658 return true;
2659 case NIC_THIS:
2660 msg = "this";
2661 break;
2662 case NIC_FUNC_NAME:
2663 msg = "__FUNCTION__";
2664 break;
2665 case NIC_PRETTY_FUNC:
2666 msg = "__PRETTY_FUNCTION__";
2667 break;
2668 case NIC_C99_FUNC:
2669 msg = "__func__";
2670 break;
2671 case NIC_VA_ARG:
2672 msg = "va_arg";
2673 break;
2674 case NIC_ARROW:
2675 msg = "->";
2676 break;
2677 case NIC_POINT:
2678 msg = ".";
2679 break;
2680 case NIC_STAR:
2681 msg = "*";
2682 break;
2683 case NIC_ADDR:
2684 msg = "&";
2685 break;
2686 case NIC_PREINCREMENT:
2687 msg = "++";
2688 break;
2689 case NIC_PREDECREMENT:
2690 msg = "--";
2691 break;
2692 case NIC_NEW:
2693 msg = "new";
2694 break;
2695 case NIC_DEL:
2696 msg = "delete";
2697 break;
2698 default:
2699 gcc_unreachable ();
2701 if (msg)
2702 error ("%qs cannot appear in a constant-expression", msg);
2703 return true;
2706 return false;
2709 /* Emit a diagnostic for an invalid type name. SCOPE is the
2710 qualifying scope (or NULL, if none) for ID. This function commits
2711 to the current active tentative parse, if any. (Otherwise, the
2712 problematic construct might be encountered again later, resulting
2713 in duplicate error messages.) LOCATION is the location of ID. */
2715 static void
2716 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2717 tree scope, tree id,
2718 location_t location)
2720 tree decl, old_scope;
2721 cp_parser_commit_to_tentative_parse (parser);
2722 /* Try to lookup the identifier. */
2723 old_scope = parser->scope;
2724 parser->scope = scope;
2725 decl = cp_parser_lookup_name_simple (parser, id, location);
2726 parser->scope = old_scope;
2727 /* If the lookup found a template-name, it means that the user forgot
2728 to specify an argument list. Emit a useful error message. */
2729 if (TREE_CODE (decl) == TEMPLATE_DECL)
2730 error_at (location,
2731 "invalid use of template-name %qE without an argument list",
2732 decl);
2733 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2734 error_at (location, "invalid use of destructor %qD as a type", id);
2735 else if (TREE_CODE (decl) == TYPE_DECL)
2736 /* Something like 'unsigned A a;' */
2737 error_at (location, "invalid combination of multiple type-specifiers");
2738 else if (!parser->scope)
2740 /* Issue an error message. */
2741 error_at (location, "%qE does not name a type", id);
2742 /* If we're in a template class, it's possible that the user was
2743 referring to a type from a base class. For example:
2745 template <typename T> struct A { typedef T X; };
2746 template <typename T> struct B : public A<T> { X x; };
2748 The user should have said "typename A<T>::X". */
2749 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2750 inform (location, "C++11 %<constexpr%> only available with "
2751 "-std=c++11 or -std=gnu++11");
2752 else if (processing_template_decl && current_class_type
2753 && TYPE_BINFO (current_class_type))
2755 tree b;
2757 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2759 b = TREE_CHAIN (b))
2761 tree base_type = BINFO_TYPE (b);
2762 if (CLASS_TYPE_P (base_type)
2763 && dependent_type_p (base_type))
2765 tree field;
2766 /* Go from a particular instantiation of the
2767 template (which will have an empty TYPE_FIELDs),
2768 to the main version. */
2769 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2770 for (field = TYPE_FIELDS (base_type);
2771 field;
2772 field = DECL_CHAIN (field))
2773 if (TREE_CODE (field) == TYPE_DECL
2774 && DECL_NAME (field) == id)
2776 inform (location,
2777 "(perhaps %<typename %T::%E%> was intended)",
2778 BINFO_TYPE (b), id);
2779 break;
2781 if (field)
2782 break;
2787 /* Here we diagnose qualified-ids where the scope is actually correct,
2788 but the identifier does not resolve to a valid type name. */
2789 else if (parser->scope != error_mark_node)
2791 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2792 error_at (location, "%qE in namespace %qE does not name a type",
2793 id, parser->scope);
2794 else if (CLASS_TYPE_P (parser->scope)
2795 && constructor_name_p (id, parser->scope))
2797 /* A<T>::A<T>() */
2798 error_at (location, "%<%T::%E%> names the constructor, not"
2799 " the type", parser->scope, id);
2800 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2801 error_at (location, "and %qT has no template constructors",
2802 parser->scope);
2804 else if (TYPE_P (parser->scope)
2805 && dependent_scope_p (parser->scope))
2806 error_at (location, "need %<typename%> before %<%T::%E%> because "
2807 "%qT is a dependent scope",
2808 parser->scope, id, parser->scope);
2809 else if (TYPE_P (parser->scope))
2810 error_at (location, "%qE in %q#T does not name a type",
2811 id, parser->scope);
2812 else
2813 gcc_unreachable ();
2817 /* Check for a common situation where a type-name should be present,
2818 but is not, and issue a sensible error message. Returns true if an
2819 invalid type-name was detected.
2821 The situation handled by this function are variable declarations of the
2822 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2823 Usually, `ID' should name a type, but if we got here it means that it
2824 does not. We try to emit the best possible error message depending on
2825 how exactly the id-expression looks like. */
2827 static bool
2828 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2830 tree id;
2831 cp_token *token = cp_lexer_peek_token (parser->lexer);
2833 /* Avoid duplicate error about ambiguous lookup. */
2834 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2836 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2837 if (next->type == CPP_NAME && next->ambiguous_p)
2838 goto out;
2841 cp_parser_parse_tentatively (parser);
2842 id = cp_parser_id_expression (parser,
2843 /*template_keyword_p=*/false,
2844 /*check_dependency_p=*/true,
2845 /*template_p=*/NULL,
2846 /*declarator_p=*/true,
2847 /*optional_p=*/false);
2848 /* If the next token is a (, this is a function with no explicit return
2849 type, i.e. constructor, destructor or conversion op. */
2850 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2851 || TREE_CODE (id) == TYPE_DECL)
2853 cp_parser_abort_tentative_parse (parser);
2854 return false;
2856 if (!cp_parser_parse_definitely (parser))
2857 return false;
2859 /* Emit a diagnostic for the invalid type. */
2860 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2861 id, token->location);
2862 out:
2863 /* If we aren't in the middle of a declarator (i.e. in a
2864 parameter-declaration-clause), skip to the end of the declaration;
2865 there's no point in trying to process it. */
2866 if (!parser->in_declarator_p)
2867 cp_parser_skip_to_end_of_block_or_statement (parser);
2868 return true;
2871 /* Consume tokens up to, and including, the next non-nested closing `)'.
2872 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2873 are doing error recovery. Returns -1 if OR_COMMA is true and we
2874 found an unnested comma. */
2876 static int
2877 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2878 bool recovering,
2879 bool or_comma,
2880 bool consume_paren)
2882 unsigned paren_depth = 0;
2883 unsigned brace_depth = 0;
2884 unsigned square_depth = 0;
2886 if (recovering && !or_comma
2887 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2888 return 0;
2890 while (true)
2892 cp_token * token = cp_lexer_peek_token (parser->lexer);
2894 switch (token->type)
2896 case CPP_EOF:
2897 case CPP_PRAGMA_EOL:
2898 /* If we've run out of tokens, then there is no closing `)'. */
2899 return 0;
2901 /* This is good for lambda expression capture-lists. */
2902 case CPP_OPEN_SQUARE:
2903 ++square_depth;
2904 break;
2905 case CPP_CLOSE_SQUARE:
2906 if (!square_depth--)
2907 return 0;
2908 break;
2910 case CPP_SEMICOLON:
2911 /* This matches the processing in skip_to_end_of_statement. */
2912 if (!brace_depth)
2913 return 0;
2914 break;
2916 case CPP_OPEN_BRACE:
2917 ++brace_depth;
2918 break;
2919 case CPP_CLOSE_BRACE:
2920 if (!brace_depth--)
2921 return 0;
2922 break;
2924 case CPP_COMMA:
2925 if (recovering && or_comma && !brace_depth && !paren_depth
2926 && !square_depth)
2927 return -1;
2928 break;
2930 case CPP_OPEN_PAREN:
2931 if (!brace_depth)
2932 ++paren_depth;
2933 break;
2935 case CPP_CLOSE_PAREN:
2936 if (!brace_depth && !paren_depth--)
2938 if (consume_paren)
2939 cp_lexer_consume_token (parser->lexer);
2940 return 1;
2942 break;
2944 default:
2945 break;
2948 /* Consume the token. */
2949 cp_lexer_consume_token (parser->lexer);
2953 /* Consume tokens until we reach the end of the current statement.
2954 Normally, that will be just before consuming a `;'. However, if a
2955 non-nested `}' comes first, then we stop before consuming that. */
2957 static void
2958 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2960 unsigned nesting_depth = 0;
2962 while (true)
2964 cp_token *token = cp_lexer_peek_token (parser->lexer);
2966 switch (token->type)
2968 case CPP_EOF:
2969 case CPP_PRAGMA_EOL:
2970 /* If we've run out of tokens, stop. */
2971 return;
2973 case CPP_SEMICOLON:
2974 /* If the next token is a `;', we have reached the end of the
2975 statement. */
2976 if (!nesting_depth)
2977 return;
2978 break;
2980 case CPP_CLOSE_BRACE:
2981 /* If this is a non-nested '}', stop before consuming it.
2982 That way, when confronted with something like:
2984 { 3 + }
2986 we stop before consuming the closing '}', even though we
2987 have not yet reached a `;'. */
2988 if (nesting_depth == 0)
2989 return;
2991 /* If it is the closing '}' for a block that we have
2992 scanned, stop -- but only after consuming the token.
2993 That way given:
2995 void f g () { ... }
2996 typedef int I;
2998 we will stop after the body of the erroneously declared
2999 function, but before consuming the following `typedef'
3000 declaration. */
3001 if (--nesting_depth == 0)
3003 cp_lexer_consume_token (parser->lexer);
3004 return;
3007 case CPP_OPEN_BRACE:
3008 ++nesting_depth;
3009 break;
3011 default:
3012 break;
3015 /* Consume the token. */
3016 cp_lexer_consume_token (parser->lexer);
3020 /* This function is called at the end of a statement or declaration.
3021 If the next token is a semicolon, it is consumed; otherwise, error
3022 recovery is attempted. */
3024 static void
3025 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3027 /* Look for the trailing `;'. */
3028 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3030 /* If there is additional (erroneous) input, skip to the end of
3031 the statement. */
3032 cp_parser_skip_to_end_of_statement (parser);
3033 /* If the next token is now a `;', consume it. */
3034 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3035 cp_lexer_consume_token (parser->lexer);
3039 /* Skip tokens until we have consumed an entire block, or until we
3040 have consumed a non-nested `;'. */
3042 static void
3043 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3045 int nesting_depth = 0;
3047 while (nesting_depth >= 0)
3049 cp_token *token = cp_lexer_peek_token (parser->lexer);
3051 switch (token->type)
3053 case CPP_EOF:
3054 case CPP_PRAGMA_EOL:
3055 /* If we've run out of tokens, stop. */
3056 return;
3058 case CPP_SEMICOLON:
3059 /* Stop if this is an unnested ';'. */
3060 if (!nesting_depth)
3061 nesting_depth = -1;
3062 break;
3064 case CPP_CLOSE_BRACE:
3065 /* Stop if this is an unnested '}', or closes the outermost
3066 nesting level. */
3067 nesting_depth--;
3068 if (nesting_depth < 0)
3069 return;
3070 if (!nesting_depth)
3071 nesting_depth = -1;
3072 break;
3074 case CPP_OPEN_BRACE:
3075 /* Nest. */
3076 nesting_depth++;
3077 break;
3079 default:
3080 break;
3083 /* Consume the token. */
3084 cp_lexer_consume_token (parser->lexer);
3088 /* Skip tokens until a non-nested closing curly brace is the next
3089 token, or there are no more tokens. Return true in the first case,
3090 false otherwise. */
3092 static bool
3093 cp_parser_skip_to_closing_brace (cp_parser *parser)
3095 unsigned nesting_depth = 0;
3097 while (true)
3099 cp_token *token = cp_lexer_peek_token (parser->lexer);
3101 switch (token->type)
3103 case CPP_EOF:
3104 case CPP_PRAGMA_EOL:
3105 /* If we've run out of tokens, stop. */
3106 return false;
3108 case CPP_CLOSE_BRACE:
3109 /* If the next token is a non-nested `}', then we have reached
3110 the end of the current block. */
3111 if (nesting_depth-- == 0)
3112 return true;
3113 break;
3115 case CPP_OPEN_BRACE:
3116 /* If it the next token is a `{', then we are entering a new
3117 block. Consume the entire block. */
3118 ++nesting_depth;
3119 break;
3121 default:
3122 break;
3125 /* Consume the token. */
3126 cp_lexer_consume_token (parser->lexer);
3130 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3131 parameter is the PRAGMA token, allowing us to purge the entire pragma
3132 sequence. */
3134 static void
3135 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3137 cp_token *token;
3139 parser->lexer->in_pragma = false;
3142 token = cp_lexer_consume_token (parser->lexer);
3143 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3145 /* Ensure that the pragma is not parsed again. */
3146 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3149 /* Require pragma end of line, resyncing with it as necessary. The
3150 arguments are as for cp_parser_skip_to_pragma_eol. */
3152 static void
3153 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3155 parser->lexer->in_pragma = false;
3156 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3157 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3160 /* This is a simple wrapper around make_typename_type. When the id is
3161 an unresolved identifier node, we can provide a superior diagnostic
3162 using cp_parser_diagnose_invalid_type_name. */
3164 static tree
3165 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3166 tree id, location_t id_location)
3168 tree result;
3169 if (TREE_CODE (id) == IDENTIFIER_NODE)
3171 result = make_typename_type (scope, id, typename_type,
3172 /*complain=*/tf_none);
3173 if (result == error_mark_node)
3174 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3175 return result;
3177 return make_typename_type (scope, id, typename_type, tf_error);
3180 /* This is a wrapper around the
3181 make_{pointer,ptrmem,reference}_declarator functions that decides
3182 which one to call based on the CODE and CLASS_TYPE arguments. The
3183 CODE argument should be one of the values returned by
3184 cp_parser_ptr_operator. */
3185 static cp_declarator *
3186 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3187 cp_cv_quals cv_qualifiers,
3188 cp_declarator *target)
3190 if (code == ERROR_MARK)
3191 return cp_error_declarator;
3193 if (code == INDIRECT_REF)
3194 if (class_type == NULL_TREE)
3195 return make_pointer_declarator (cv_qualifiers, target);
3196 else
3197 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3198 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3199 return make_reference_declarator (cv_qualifiers, target, false);
3200 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3201 return make_reference_declarator (cv_qualifiers, target, true);
3202 gcc_unreachable ();
3205 /* Create a new C++ parser. */
3207 static cp_parser *
3208 cp_parser_new (void)
3210 cp_parser *parser;
3211 cp_lexer *lexer;
3212 unsigned i;
3214 /* cp_lexer_new_main is called before doing GC allocation because
3215 cp_lexer_new_main might load a PCH file. */
3216 lexer = cp_lexer_new_main ();
3218 /* Initialize the binops_by_token so that we can get the tree
3219 directly from the token. */
3220 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3221 binops_by_token[binops[i].token_type] = binops[i];
3223 parser = ggc_alloc_cleared_cp_parser ();
3224 parser->lexer = lexer;
3225 parser->context = cp_parser_context_new (NULL);
3227 /* For now, we always accept GNU extensions. */
3228 parser->allow_gnu_extensions_p = 1;
3230 /* The `>' token is a greater-than operator, not the end of a
3231 template-id. */
3232 parser->greater_than_is_operator_p = true;
3234 parser->default_arg_ok_p = true;
3236 /* We are not parsing a constant-expression. */
3237 parser->integral_constant_expression_p = false;
3238 parser->allow_non_integral_constant_expression_p = false;
3239 parser->non_integral_constant_expression_p = false;
3241 /* Local variable names are not forbidden. */
3242 parser->local_variables_forbidden_p = false;
3244 /* We are not processing an `extern "C"' declaration. */
3245 parser->in_unbraced_linkage_specification_p = false;
3247 /* We are not processing a declarator. */
3248 parser->in_declarator_p = false;
3250 /* We are not processing a template-argument-list. */
3251 parser->in_template_argument_list_p = false;
3253 /* We are not in an iteration statement. */
3254 parser->in_statement = 0;
3256 /* We are not in a switch statement. */
3257 parser->in_switch_statement_p = false;
3259 /* We are not parsing a type-id inside an expression. */
3260 parser->in_type_id_in_expr_p = false;
3262 /* Declarations aren't implicitly extern "C". */
3263 parser->implicit_extern_c = false;
3265 /* String literals should be translated to the execution character set. */
3266 parser->translate_strings_p = true;
3268 /* We are not parsing a function body. */
3269 parser->in_function_body = false;
3271 /* We can correct until told otherwise. */
3272 parser->colon_corrects_to_scope_p = true;
3274 /* The unparsed function queue is empty. */
3275 push_unparsed_function_queues (parser);
3277 /* There are no classes being defined. */
3278 parser->num_classes_being_defined = 0;
3280 /* No template parameters apply. */
3281 parser->num_template_parameter_lists = 0;
3283 return parser;
3286 /* Create a cp_lexer structure which will emit the tokens in CACHE
3287 and push it onto the parser's lexer stack. This is used for delayed
3288 parsing of in-class method bodies and default arguments, and should
3289 not be confused with tentative parsing. */
3290 static void
3291 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3293 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3294 lexer->next = parser->lexer;
3295 parser->lexer = lexer;
3297 /* Move the current source position to that of the first token in the
3298 new lexer. */
3299 cp_lexer_set_source_position_from_token (lexer->next_token);
3302 /* Pop the top lexer off the parser stack. This is never used for the
3303 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3304 static void
3305 cp_parser_pop_lexer (cp_parser *parser)
3307 cp_lexer *lexer = parser->lexer;
3308 parser->lexer = lexer->next;
3309 cp_lexer_destroy (lexer);
3311 /* Put the current source position back where it was before this
3312 lexer was pushed. */
3313 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3316 /* Lexical conventions [gram.lex] */
3318 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3319 identifier. */
3321 static tree
3322 cp_parser_identifier (cp_parser* parser)
3324 cp_token *token;
3326 /* Look for the identifier. */
3327 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3328 /* Return the value. */
3329 return token ? token->u.value : error_mark_node;
3332 /* Parse a sequence of adjacent string constants. Returns a
3333 TREE_STRING representing the combined, nul-terminated string
3334 constant. If TRANSLATE is true, translate the string to the
3335 execution character set. If WIDE_OK is true, a wide string is
3336 invalid here.
3338 C++98 [lex.string] says that if a narrow string literal token is
3339 adjacent to a wide string literal token, the behavior is undefined.
3340 However, C99 6.4.5p4 says that this results in a wide string literal.
3341 We follow C99 here, for consistency with the C front end.
3343 This code is largely lifted from lex_string() in c-lex.c.
3345 FUTURE: ObjC++ will need to handle @-strings here. */
3346 static tree
3347 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3349 tree value;
3350 size_t count;
3351 struct obstack str_ob;
3352 cpp_string str, istr, *strs;
3353 cp_token *tok;
3354 enum cpp_ttype type, curr_type;
3355 int have_suffix_p = 0;
3356 tree string_tree;
3357 tree suffix_id = NULL_TREE;
3358 bool curr_tok_is_userdef_p = false;
3360 tok = cp_lexer_peek_token (parser->lexer);
3361 if (!cp_parser_is_string_literal (tok))
3363 cp_parser_error (parser, "expected string-literal");
3364 return error_mark_node;
3367 if (cpp_userdef_string_p (tok->type))
3369 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3370 curr_type = cpp_userdef_string_remove_type (tok->type);
3371 curr_tok_is_userdef_p = true;
3373 else
3375 string_tree = tok->u.value;
3376 curr_type = tok->type;
3378 type = curr_type;
3380 /* Try to avoid the overhead of creating and destroying an obstack
3381 for the common case of just one string. */
3382 if (!cp_parser_is_string_literal
3383 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3385 cp_lexer_consume_token (parser->lexer);
3387 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3388 str.len = TREE_STRING_LENGTH (string_tree);
3389 count = 1;
3391 if (curr_tok_is_userdef_p)
3393 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3394 have_suffix_p = 1;
3395 curr_type = cpp_userdef_string_remove_type (tok->type);
3397 else
3398 curr_type = tok->type;
3400 strs = &str;
3402 else
3404 gcc_obstack_init (&str_ob);
3405 count = 0;
3409 cp_lexer_consume_token (parser->lexer);
3410 count++;
3411 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3412 str.len = TREE_STRING_LENGTH (string_tree);
3414 if (curr_tok_is_userdef_p)
3416 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3417 if (have_suffix_p == 0)
3419 suffix_id = curr_suffix_id;
3420 have_suffix_p = 1;
3422 else if (have_suffix_p == 1
3423 && curr_suffix_id != suffix_id)
3425 error ("inconsistent user-defined literal suffixes"
3426 " %qD and %qD in string literal",
3427 suffix_id, curr_suffix_id);
3428 have_suffix_p = -1;
3430 curr_type = cpp_userdef_string_remove_type (tok->type);
3432 else
3433 curr_type = tok->type;
3435 if (type != curr_type)
3437 if (type == CPP_STRING)
3438 type = curr_type;
3439 else if (curr_type != CPP_STRING)
3440 error_at (tok->location,
3441 "unsupported non-standard concatenation "
3442 "of string literals");
3445 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3447 tok = cp_lexer_peek_token (parser->lexer);
3448 if (cpp_userdef_string_p (tok->type))
3450 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3451 curr_type = cpp_userdef_string_remove_type (tok->type);
3452 curr_tok_is_userdef_p = true;
3454 else
3456 string_tree = tok->u.value;
3457 curr_type = tok->type;
3458 curr_tok_is_userdef_p = false;
3461 while (cp_parser_is_string_literal (tok));
3463 strs = (cpp_string *) obstack_finish (&str_ob);
3466 if (type != CPP_STRING && !wide_ok)
3468 cp_parser_error (parser, "a wide string is invalid in this context");
3469 type = CPP_STRING;
3472 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3473 (parse_in, strs, count, &istr, type))
3475 value = build_string (istr.len, (const char *)istr.text);
3476 free (CONST_CAST (unsigned char *, istr.text));
3478 switch (type)
3480 default:
3481 case CPP_STRING:
3482 case CPP_UTF8STRING:
3483 TREE_TYPE (value) = char_array_type_node;
3484 break;
3485 case CPP_STRING16:
3486 TREE_TYPE (value) = char16_array_type_node;
3487 break;
3488 case CPP_STRING32:
3489 TREE_TYPE (value) = char32_array_type_node;
3490 break;
3491 case CPP_WSTRING:
3492 TREE_TYPE (value) = wchar_array_type_node;
3493 break;
3496 value = fix_string_type (value);
3498 if (have_suffix_p)
3500 tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3501 tok->u.value = literal;
3502 return cp_parser_userdef_string_literal (tok);
3505 else
3506 /* cpp_interpret_string has issued an error. */
3507 value = error_mark_node;
3509 if (count > 1)
3510 obstack_free (&str_ob, 0);
3512 return value;
3515 /* Look up a literal operator with the name and the exact arguments. */
3517 static tree
3518 lookup_literal_operator (tree name, VEC(tree,gc) *args)
3520 tree decl, fns;
3521 decl = lookup_name (name);
3522 if (!decl || !is_overloaded_fn (decl))
3523 return error_mark_node;
3525 for (fns = decl; fns; fns = OVL_NEXT (fns))
3527 unsigned int ix;
3528 bool found = true;
3529 tree fn = OVL_CURRENT (fns);
3530 tree argtypes = NULL_TREE;
3531 argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3532 if (argtypes != NULL_TREE)
3534 for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE;
3535 ++ix, argtypes = TREE_CHAIN (argtypes))
3537 tree targ = TREE_VALUE (argtypes);
3538 tree tparm = TREE_TYPE (VEC_index (tree, args, ix));
3539 bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3540 bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3541 if ((ptr || arr || !same_type_p (targ, tparm))
3542 && (!ptr || !arr
3543 || !same_type_p (TREE_TYPE (targ),
3544 TREE_TYPE (tparm))))
3545 found = false;
3547 if (found
3548 && ix == VEC_length (tree, args)
3549 /* May be this should be sufficient_parms_p instead,
3550 depending on how exactly should user-defined literals
3551 work in presence of default arguments on the literal
3552 operator parameters. */
3553 && argtypes == void_list_node)
3554 return fn;
3558 return error_mark_node;
3561 /* Parse a user-defined char constant. Returns a call to a user-defined
3562 literal operator taking the character as an argument. */
3564 static tree
3565 cp_parser_userdef_char_literal (cp_parser *parser)
3567 cp_token *token = cp_lexer_consume_token (parser->lexer);
3568 tree literal = token->u.value;
3569 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3570 tree value = USERDEF_LITERAL_VALUE (literal);
3571 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3572 tree decl, result;
3574 /* Build up a call to the user-defined operator */
3575 /* Lookup the name we got back from the id-expression. */
3576 VEC(tree,gc) *args = make_tree_vector ();
3577 VEC_safe_push (tree, gc, args, value);
3578 decl = lookup_literal_operator (name, args);
3579 if (!decl || decl == error_mark_node)
3581 error ("unable to find character literal operator %qD with %qT argument",
3582 name, TREE_TYPE (value));
3583 release_tree_vector (args);
3584 return error_mark_node;
3586 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3587 release_tree_vector (args);
3588 if (result != error_mark_node)
3589 return result;
3591 error ("unable to find character literal operator %qD with %qT argument",
3592 name, TREE_TYPE (value));
3593 return error_mark_node;
3596 /* A subroutine of cp_parser_userdef_numeric_literal to
3597 create a char... template parameter pack from a string node. */
3599 static tree
3600 make_char_string_pack (tree value)
3602 tree charvec;
3603 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3604 const char *str = TREE_STRING_POINTER (value);
3605 int i, len = TREE_STRING_LENGTH (value) - 1;
3606 tree argvec = make_tree_vec (1);
3608 /* Fill in CHARVEC with all of the parameters. */
3609 charvec = make_tree_vec (len);
3610 for (i = 0; i < len; ++i)
3611 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3613 /* Build the argument packs. */
3614 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3615 TREE_TYPE (argpack) = char_type_node;
3617 TREE_VEC_ELT (argvec, 0) = argpack;
3619 return argvec;
3622 /* Parse a user-defined numeric constant. returns a call to a user-defined
3623 literal operator. */
3625 static tree
3626 cp_parser_userdef_numeric_literal (cp_parser *parser)
3628 cp_token *token = cp_lexer_consume_token (parser->lexer);
3629 tree literal = token->u.value;
3630 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3631 tree value = USERDEF_LITERAL_VALUE (literal);
3632 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3633 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3634 tree decl, result;
3635 VEC(tree,gc) *args;
3637 /* Look for a literal operator taking the exact type of numeric argument
3638 as the literal value. */
3639 args = make_tree_vector ();
3640 VEC_safe_push (tree, gc, args, value);
3641 decl = lookup_literal_operator (name, args);
3642 if (decl && decl != error_mark_node)
3644 result = finish_call_expr (decl, &args, false, true, tf_none);
3645 if (result != error_mark_node)
3647 release_tree_vector (args);
3648 return result;
3651 release_tree_vector (args);
3653 /* If the numeric argument didn't work, look for a raw literal
3654 operator taking a const char* argument consisting of the number
3655 in string format. */
3656 args = make_tree_vector ();
3657 VEC_safe_push (tree, gc, args, num_string);
3658 decl = lookup_literal_operator (name, args);
3659 if (decl && decl != error_mark_node)
3661 result = finish_call_expr (decl, &args, false, true, tf_none);
3662 if (result != error_mark_node)
3664 release_tree_vector (args);
3665 return result;
3668 release_tree_vector (args);
3670 /* If the raw literal didn't work, look for a non-type template
3671 function with parameter pack char.... Call the function with
3672 template parameter characters representing the number. */
3673 args = make_tree_vector ();
3674 decl = lookup_literal_operator (name, args);
3675 if (decl && decl != error_mark_node)
3677 tree tmpl_args = make_char_string_pack (num_string);
3678 decl = lookup_template_function (decl, tmpl_args);
3679 result = finish_call_expr (decl, &args, false, true, tf_none);
3680 if (result != error_mark_node)
3682 release_tree_vector (args);
3683 return result;
3686 release_tree_vector (args);
3688 error ("unable to find numeric literal operator %qD", name);
3689 return error_mark_node;
3692 /* Parse a user-defined string constant. Returns a call to a user-defined
3693 literal operator taking a character pointer and the length of the string
3694 as arguments. */
3696 static tree
3697 cp_parser_userdef_string_literal (cp_token *token)
3699 tree literal = token->u.value;
3700 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3701 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3702 tree value = USERDEF_LITERAL_VALUE (literal);
3703 int len = TREE_STRING_LENGTH (value)
3704 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3705 tree decl, result;
3707 /* Build up a call to the user-defined operator */
3708 /* Lookup the name we got back from the id-expression. */
3709 VEC(tree,gc) *args = make_tree_vector ();
3710 VEC_safe_push (tree, gc, args, value);
3711 VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3712 decl = lookup_name (name);
3713 if (!decl || decl == error_mark_node)
3715 error ("unable to find string literal operator %qD", name);
3716 release_tree_vector (args);
3717 return error_mark_node;
3719 result = finish_call_expr (decl, &args, false, true, tf_none);
3720 release_tree_vector (args);
3721 if (result != error_mark_node)
3722 return result;
3724 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3725 name, TREE_TYPE (value), size_type_node);
3726 return error_mark_node;
3730 /* Basic concepts [gram.basic] */
3732 /* Parse a translation-unit.
3734 translation-unit:
3735 declaration-seq [opt]
3737 Returns TRUE if all went well. */
3739 static bool
3740 cp_parser_translation_unit (cp_parser* parser)
3742 /* The address of the first non-permanent object on the declarator
3743 obstack. */
3744 static void *declarator_obstack_base;
3746 bool success;
3748 /* Create the declarator obstack, if necessary. */
3749 if (!cp_error_declarator)
3751 gcc_obstack_init (&declarator_obstack);
3752 /* Create the error declarator. */
3753 cp_error_declarator = make_declarator (cdk_error);
3754 /* Create the empty parameter list. */
3755 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3756 /* Remember where the base of the declarator obstack lies. */
3757 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3760 cp_parser_declaration_seq_opt (parser);
3762 /* If there are no tokens left then all went well. */
3763 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3765 /* Get rid of the token array; we don't need it any more. */
3766 cp_lexer_destroy (parser->lexer);
3767 parser->lexer = NULL;
3769 /* This file might have been a context that's implicitly extern
3770 "C". If so, pop the lang context. (Only relevant for PCH.) */
3771 if (parser->implicit_extern_c)
3773 pop_lang_context ();
3774 parser->implicit_extern_c = false;
3777 /* Finish up. */
3778 finish_translation_unit ();
3780 success = true;
3782 else
3784 cp_parser_error (parser, "expected declaration");
3785 success = false;
3788 /* Make sure the declarator obstack was fully cleaned up. */
3789 gcc_assert (obstack_next_free (&declarator_obstack)
3790 == declarator_obstack_base);
3792 /* All went well. */
3793 return success;
3796 /* Expressions [gram.expr] */
3798 /* Parse a primary-expression.
3800 primary-expression:
3801 literal
3802 this
3803 ( expression )
3804 id-expression
3806 GNU Extensions:
3808 primary-expression:
3809 ( compound-statement )
3810 __builtin_va_arg ( assignment-expression , type-id )
3811 __builtin_offsetof ( type-id , offsetof-expression )
3813 C++ Extensions:
3814 __has_nothrow_assign ( type-id )
3815 __has_nothrow_constructor ( type-id )
3816 __has_nothrow_copy ( type-id )
3817 __has_trivial_assign ( type-id )
3818 __has_trivial_constructor ( type-id )
3819 __has_trivial_copy ( type-id )
3820 __has_trivial_destructor ( type-id )
3821 __has_virtual_destructor ( type-id )
3822 __is_abstract ( type-id )
3823 __is_base_of ( type-id , type-id )
3824 __is_class ( type-id )
3825 __is_convertible_to ( type-id , type-id )
3826 __is_empty ( type-id )
3827 __is_enum ( type-id )
3828 __is_final ( type-id )
3829 __is_literal_type ( type-id )
3830 __is_pod ( type-id )
3831 __is_polymorphic ( type-id )
3832 __is_std_layout ( type-id )
3833 __is_trivial ( type-id )
3834 __is_union ( type-id )
3836 Objective-C++ Extension:
3838 primary-expression:
3839 objc-expression
3841 literal:
3842 __null
3844 ADDRESS_P is true iff this expression was immediately preceded by
3845 "&" and therefore might denote a pointer-to-member. CAST_P is true
3846 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3847 true iff this expression is a template argument.
3849 Returns a representation of the expression. Upon return, *IDK
3850 indicates what kind of id-expression (if any) was present. */
3852 static tree
3853 cp_parser_primary_expression (cp_parser *parser,
3854 bool address_p,
3855 bool cast_p,
3856 bool template_arg_p,
3857 cp_id_kind *idk)
3859 cp_token *token = NULL;
3861 /* Assume the primary expression is not an id-expression. */
3862 *idk = CP_ID_KIND_NONE;
3864 /* Peek at the next token. */
3865 token = cp_lexer_peek_token (parser->lexer);
3866 switch (token->type)
3868 /* literal:
3869 integer-literal
3870 character-literal
3871 floating-literal
3872 string-literal
3873 boolean-literal
3874 pointer-literal
3875 user-defined-literal */
3876 case CPP_CHAR:
3877 case CPP_CHAR16:
3878 case CPP_CHAR32:
3879 case CPP_WCHAR:
3880 case CPP_NUMBER:
3881 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3882 return cp_parser_userdef_numeric_literal (parser);
3883 token = cp_lexer_consume_token (parser->lexer);
3884 if (TREE_CODE (token->u.value) == FIXED_CST)
3886 error_at (token->location,
3887 "fixed-point types not supported in C++");
3888 return error_mark_node;
3890 /* Floating-point literals are only allowed in an integral
3891 constant expression if they are cast to an integral or
3892 enumeration type. */
3893 if (TREE_CODE (token->u.value) == REAL_CST
3894 && parser->integral_constant_expression_p
3895 && pedantic)
3897 /* CAST_P will be set even in invalid code like "int(2.7 +
3898 ...)". Therefore, we have to check that the next token
3899 is sure to end the cast. */
3900 if (cast_p)
3902 cp_token *next_token;
3904 next_token = cp_lexer_peek_token (parser->lexer);
3905 if (/* The comma at the end of an
3906 enumerator-definition. */
3907 next_token->type != CPP_COMMA
3908 /* The curly brace at the end of an enum-specifier. */
3909 && next_token->type != CPP_CLOSE_BRACE
3910 /* The end of a statement. */
3911 && next_token->type != CPP_SEMICOLON
3912 /* The end of the cast-expression. */
3913 && next_token->type != CPP_CLOSE_PAREN
3914 /* The end of an array bound. */
3915 && next_token->type != CPP_CLOSE_SQUARE
3916 /* The closing ">" in a template-argument-list. */
3917 && (next_token->type != CPP_GREATER
3918 || parser->greater_than_is_operator_p)
3919 /* C++0x only: A ">>" treated like two ">" tokens,
3920 in a template-argument-list. */
3921 && (next_token->type != CPP_RSHIFT
3922 || (cxx_dialect == cxx98)
3923 || parser->greater_than_is_operator_p))
3924 cast_p = false;
3927 /* If we are within a cast, then the constraint that the
3928 cast is to an integral or enumeration type will be
3929 checked at that point. If we are not within a cast, then
3930 this code is invalid. */
3931 if (!cast_p)
3932 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3934 return token->u.value;
3936 case CPP_CHAR_USERDEF:
3937 case CPP_CHAR16_USERDEF:
3938 case CPP_CHAR32_USERDEF:
3939 case CPP_WCHAR_USERDEF:
3940 return cp_parser_userdef_char_literal (parser);
3942 case CPP_STRING:
3943 case CPP_STRING16:
3944 case CPP_STRING32:
3945 case CPP_WSTRING:
3946 case CPP_UTF8STRING:
3947 case CPP_STRING_USERDEF:
3948 case CPP_STRING16_USERDEF:
3949 case CPP_STRING32_USERDEF:
3950 case CPP_WSTRING_USERDEF:
3951 case CPP_UTF8STRING_USERDEF:
3952 /* ??? Should wide strings be allowed when parser->translate_strings_p
3953 is false (i.e. in attributes)? If not, we can kill the third
3954 argument to cp_parser_string_literal. */
3955 return cp_parser_string_literal (parser,
3956 parser->translate_strings_p,
3957 true);
3959 case CPP_OPEN_PAREN:
3961 tree expr;
3962 bool saved_greater_than_is_operator_p;
3964 /* Consume the `('. */
3965 cp_lexer_consume_token (parser->lexer);
3966 /* Within a parenthesized expression, a `>' token is always
3967 the greater-than operator. */
3968 saved_greater_than_is_operator_p
3969 = parser->greater_than_is_operator_p;
3970 parser->greater_than_is_operator_p = true;
3971 /* If we see `( { ' then we are looking at the beginning of
3972 a GNU statement-expression. */
3973 if (cp_parser_allow_gnu_extensions_p (parser)
3974 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3976 /* Statement-expressions are not allowed by the standard. */
3977 pedwarn (token->location, OPT_Wpedantic,
3978 "ISO C++ forbids braced-groups within expressions");
3980 /* And they're not allowed outside of a function-body; you
3981 cannot, for example, write:
3983 int i = ({ int j = 3; j + 1; });
3985 at class or namespace scope. */
3986 if (!parser->in_function_body
3987 || parser->in_template_argument_list_p)
3989 error_at (token->location,
3990 "statement-expressions are not allowed outside "
3991 "functions nor in template-argument lists");
3992 cp_parser_skip_to_end_of_block_or_statement (parser);
3993 expr = error_mark_node;
3995 else
3997 /* Start the statement-expression. */
3998 expr = begin_stmt_expr ();
3999 /* Parse the compound-statement. */
4000 cp_parser_compound_statement (parser, expr, false, false);
4001 /* Finish up. */
4002 expr = finish_stmt_expr (expr, false);
4005 else
4007 /* Parse the parenthesized expression. */
4008 expr = cp_parser_expression (parser, cast_p, idk);
4009 /* Let the front end know that this expression was
4010 enclosed in parentheses. This matters in case, for
4011 example, the expression is of the form `A::B', since
4012 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4013 not. */
4014 finish_parenthesized_expr (expr);
4015 /* DR 705: Wrapping an unqualified name in parentheses
4016 suppresses arg-dependent lookup. We want to pass back
4017 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4018 (c++/37862), but none of the others. */
4019 if (*idk != CP_ID_KIND_QUALIFIED)
4020 *idk = CP_ID_KIND_NONE;
4022 /* The `>' token might be the end of a template-id or
4023 template-parameter-list now. */
4024 parser->greater_than_is_operator_p
4025 = saved_greater_than_is_operator_p;
4026 /* Consume the `)'. */
4027 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4028 cp_parser_skip_to_end_of_statement (parser);
4030 return expr;
4033 case CPP_OPEN_SQUARE:
4034 if (c_dialect_objc ())
4035 /* We have an Objective-C++ message. */
4036 return cp_parser_objc_expression (parser);
4038 tree lam = cp_parser_lambda_expression (parser);
4039 /* Don't warn about a failed tentative parse. */
4040 if (cp_parser_error_occurred (parser))
4041 return error_mark_node;
4042 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4043 return lam;
4046 case CPP_OBJC_STRING:
4047 if (c_dialect_objc ())
4048 /* We have an Objective-C++ string literal. */
4049 return cp_parser_objc_expression (parser);
4050 cp_parser_error (parser, "expected primary-expression");
4051 return error_mark_node;
4053 case CPP_KEYWORD:
4054 switch (token->keyword)
4056 /* These two are the boolean literals. */
4057 case RID_TRUE:
4058 cp_lexer_consume_token (parser->lexer);
4059 return boolean_true_node;
4060 case RID_FALSE:
4061 cp_lexer_consume_token (parser->lexer);
4062 return boolean_false_node;
4064 /* The `__null' literal. */
4065 case RID_NULL:
4066 cp_lexer_consume_token (parser->lexer);
4067 return null_node;
4069 /* The `nullptr' literal. */
4070 case RID_NULLPTR:
4071 cp_lexer_consume_token (parser->lexer);
4072 return nullptr_node;
4074 /* Recognize the `this' keyword. */
4075 case RID_THIS:
4076 cp_lexer_consume_token (parser->lexer);
4077 if (parser->local_variables_forbidden_p)
4079 error_at (token->location,
4080 "%<this%> may not be used in this context");
4081 return error_mark_node;
4083 /* Pointers cannot appear in constant-expressions. */
4084 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4085 return error_mark_node;
4086 return finish_this_expr ();
4088 /* The `operator' keyword can be the beginning of an
4089 id-expression. */
4090 case RID_OPERATOR:
4091 goto id_expression;
4093 case RID_FUNCTION_NAME:
4094 case RID_PRETTY_FUNCTION_NAME:
4095 case RID_C99_FUNCTION_NAME:
4097 non_integral_constant name;
4099 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4100 __func__ are the names of variables -- but they are
4101 treated specially. Therefore, they are handled here,
4102 rather than relying on the generic id-expression logic
4103 below. Grammatically, these names are id-expressions.
4105 Consume the token. */
4106 token = cp_lexer_consume_token (parser->lexer);
4108 switch (token->keyword)
4110 case RID_FUNCTION_NAME:
4111 name = NIC_FUNC_NAME;
4112 break;
4113 case RID_PRETTY_FUNCTION_NAME:
4114 name = NIC_PRETTY_FUNC;
4115 break;
4116 case RID_C99_FUNCTION_NAME:
4117 name = NIC_C99_FUNC;
4118 break;
4119 default:
4120 gcc_unreachable ();
4123 if (cp_parser_non_integral_constant_expression (parser, name))
4124 return error_mark_node;
4126 /* Look up the name. */
4127 return finish_fname (token->u.value);
4130 case RID_VA_ARG:
4132 tree expression;
4133 tree type;
4134 source_location type_location;
4136 /* The `__builtin_va_arg' construct is used to handle
4137 `va_arg'. Consume the `__builtin_va_arg' token. */
4138 cp_lexer_consume_token (parser->lexer);
4139 /* Look for the opening `('. */
4140 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4141 /* Now, parse the assignment-expression. */
4142 expression = cp_parser_assignment_expression (parser,
4143 /*cast_p=*/false, NULL);
4144 /* Look for the `,'. */
4145 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4146 type_location = cp_lexer_peek_token (parser->lexer)->location;
4147 /* Parse the type-id. */
4148 type = cp_parser_type_id (parser);
4149 /* Look for the closing `)'. */
4150 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4151 /* Using `va_arg' in a constant-expression is not
4152 allowed. */
4153 if (cp_parser_non_integral_constant_expression (parser,
4154 NIC_VA_ARG))
4155 return error_mark_node;
4156 return build_x_va_arg (type_location, expression, type);
4159 case RID_OFFSETOF:
4160 return cp_parser_builtin_offsetof (parser);
4162 case RID_HAS_NOTHROW_ASSIGN:
4163 case RID_HAS_NOTHROW_CONSTRUCTOR:
4164 case RID_HAS_NOTHROW_COPY:
4165 case RID_HAS_TRIVIAL_ASSIGN:
4166 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4167 case RID_HAS_TRIVIAL_COPY:
4168 case RID_HAS_TRIVIAL_DESTRUCTOR:
4169 case RID_HAS_VIRTUAL_DESTRUCTOR:
4170 case RID_IS_ABSTRACT:
4171 case RID_IS_BASE_OF:
4172 case RID_IS_CLASS:
4173 case RID_IS_CONVERTIBLE_TO:
4174 case RID_IS_EMPTY:
4175 case RID_IS_ENUM:
4176 case RID_IS_FINAL:
4177 case RID_IS_LITERAL_TYPE:
4178 case RID_IS_POD:
4179 case RID_IS_POLYMORPHIC:
4180 case RID_IS_STD_LAYOUT:
4181 case RID_IS_TRIVIAL:
4182 case RID_IS_UNION:
4183 return cp_parser_trait_expr (parser, token->keyword);
4185 /* Objective-C++ expressions. */
4186 case RID_AT_ENCODE:
4187 case RID_AT_PROTOCOL:
4188 case RID_AT_SELECTOR:
4189 return cp_parser_objc_expression (parser);
4191 case RID_TEMPLATE:
4192 if (parser->in_function_body
4193 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4194 == CPP_LESS))
4196 error_at (token->location,
4197 "a template declaration cannot appear at block scope");
4198 cp_parser_skip_to_end_of_block_or_statement (parser);
4199 return error_mark_node;
4201 default:
4202 cp_parser_error (parser, "expected primary-expression");
4203 return error_mark_node;
4206 /* An id-expression can start with either an identifier, a
4207 `::' as the beginning of a qualified-id, or the "operator"
4208 keyword. */
4209 case CPP_NAME:
4210 case CPP_SCOPE:
4211 case CPP_TEMPLATE_ID:
4212 case CPP_NESTED_NAME_SPECIFIER:
4214 tree id_expression;
4215 tree decl;
4216 const char *error_msg;
4217 bool template_p;
4218 bool done;
4219 cp_token *id_expr_token;
4221 id_expression:
4222 /* Parse the id-expression. */
4223 id_expression
4224 = cp_parser_id_expression (parser,
4225 /*template_keyword_p=*/false,
4226 /*check_dependency_p=*/true,
4227 &template_p,
4228 /*declarator_p=*/false,
4229 /*optional_p=*/false);
4230 if (id_expression == error_mark_node)
4231 return error_mark_node;
4232 id_expr_token = token;
4233 token = cp_lexer_peek_token (parser->lexer);
4234 done = (token->type != CPP_OPEN_SQUARE
4235 && token->type != CPP_OPEN_PAREN
4236 && token->type != CPP_DOT
4237 && token->type != CPP_DEREF
4238 && token->type != CPP_PLUS_PLUS
4239 && token->type != CPP_MINUS_MINUS);
4240 /* If we have a template-id, then no further lookup is
4241 required. If the template-id was for a template-class, we
4242 will sometimes have a TYPE_DECL at this point. */
4243 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4244 || TREE_CODE (id_expression) == TYPE_DECL)
4245 decl = id_expression;
4246 /* Look up the name. */
4247 else
4249 tree ambiguous_decls;
4251 /* If we already know that this lookup is ambiguous, then
4252 we've already issued an error message; there's no reason
4253 to check again. */
4254 if (id_expr_token->type == CPP_NAME
4255 && id_expr_token->ambiguous_p)
4257 cp_parser_simulate_error (parser);
4258 return error_mark_node;
4261 decl = cp_parser_lookup_name (parser, id_expression,
4262 none_type,
4263 template_p,
4264 /*is_namespace=*/false,
4265 /*check_dependency=*/true,
4266 &ambiguous_decls,
4267 id_expr_token->location);
4268 /* If the lookup was ambiguous, an error will already have
4269 been issued. */
4270 if (ambiguous_decls)
4271 return error_mark_node;
4273 /* In Objective-C++, we may have an Objective-C 2.0
4274 dot-syntax for classes here. */
4275 if (c_dialect_objc ()
4276 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4277 && TREE_CODE (decl) == TYPE_DECL
4278 && objc_is_class_name (decl))
4280 tree component;
4281 cp_lexer_consume_token (parser->lexer);
4282 component = cp_parser_identifier (parser);
4283 if (component == error_mark_node)
4284 return error_mark_node;
4286 return objc_build_class_component_ref (id_expression, component);
4289 /* In Objective-C++, an instance variable (ivar) may be preferred
4290 to whatever cp_parser_lookup_name() found. */
4291 decl = objc_lookup_ivar (decl, id_expression);
4293 /* If name lookup gives us a SCOPE_REF, then the
4294 qualifying scope was dependent. */
4295 if (TREE_CODE (decl) == SCOPE_REF)
4297 /* At this point, we do not know if DECL is a valid
4298 integral constant expression. We assume that it is
4299 in fact such an expression, so that code like:
4301 template <int N> struct A {
4302 int a[B<N>::i];
4305 is accepted. At template-instantiation time, we
4306 will check that B<N>::i is actually a constant. */
4307 return decl;
4309 /* Check to see if DECL is a local variable in a context
4310 where that is forbidden. */
4311 if (parser->local_variables_forbidden_p
4312 && local_variable_p (decl))
4314 /* It might be that we only found DECL because we are
4315 trying to be generous with pre-ISO scoping rules.
4316 For example, consider:
4318 int i;
4319 void g() {
4320 for (int i = 0; i < 10; ++i) {}
4321 extern void f(int j = i);
4324 Here, name look up will originally find the out
4325 of scope `i'. We need to issue a warning message,
4326 but then use the global `i'. */
4327 decl = check_for_out_of_scope_variable (decl);
4328 if (local_variable_p (decl))
4330 error_at (id_expr_token->location,
4331 "local variable %qD may not appear in this context",
4332 decl);
4333 return error_mark_node;
4338 decl = (finish_id_expression
4339 (id_expression, decl, parser->scope,
4340 idk,
4341 parser->integral_constant_expression_p,
4342 parser->allow_non_integral_constant_expression_p,
4343 &parser->non_integral_constant_expression_p,
4344 template_p, done, address_p,
4345 template_arg_p,
4346 &error_msg,
4347 id_expr_token->location));
4348 if (error_msg)
4349 cp_parser_error (parser, error_msg);
4350 return decl;
4353 /* Anything else is an error. */
4354 default:
4355 cp_parser_error (parser, "expected primary-expression");
4356 return error_mark_node;
4360 /* Parse an id-expression.
4362 id-expression:
4363 unqualified-id
4364 qualified-id
4366 qualified-id:
4367 :: [opt] nested-name-specifier template [opt] unqualified-id
4368 :: identifier
4369 :: operator-function-id
4370 :: template-id
4372 Return a representation of the unqualified portion of the
4373 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4374 a `::' or nested-name-specifier.
4376 Often, if the id-expression was a qualified-id, the caller will
4377 want to make a SCOPE_REF to represent the qualified-id. This
4378 function does not do this in order to avoid wastefully creating
4379 SCOPE_REFs when they are not required.
4381 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4382 `template' keyword.
4384 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4385 uninstantiated templates.
4387 If *TEMPLATE_P is non-NULL, it is set to true iff the
4388 `template' keyword is used to explicitly indicate that the entity
4389 named is a template.
4391 If DECLARATOR_P is true, the id-expression is appearing as part of
4392 a declarator, rather than as part of an expression. */
4394 static tree
4395 cp_parser_id_expression (cp_parser *parser,
4396 bool template_keyword_p,
4397 bool check_dependency_p,
4398 bool *template_p,
4399 bool declarator_p,
4400 bool optional_p)
4402 bool global_scope_p;
4403 bool nested_name_specifier_p;
4405 /* Assume the `template' keyword was not used. */
4406 if (template_p)
4407 *template_p = template_keyword_p;
4409 /* Look for the optional `::' operator. */
4410 global_scope_p
4411 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4412 != NULL_TREE);
4413 /* Look for the optional nested-name-specifier. */
4414 nested_name_specifier_p
4415 = (cp_parser_nested_name_specifier_opt (parser,
4416 /*typename_keyword_p=*/false,
4417 check_dependency_p,
4418 /*type_p=*/false,
4419 declarator_p)
4420 != NULL_TREE);
4421 /* If there is a nested-name-specifier, then we are looking at
4422 the first qualified-id production. */
4423 if (nested_name_specifier_p)
4425 tree saved_scope;
4426 tree saved_object_scope;
4427 tree saved_qualifying_scope;
4428 tree unqualified_id;
4429 bool is_template;
4431 /* See if the next token is the `template' keyword. */
4432 if (!template_p)
4433 template_p = &is_template;
4434 *template_p = cp_parser_optional_template_keyword (parser);
4435 /* Name lookup we do during the processing of the
4436 unqualified-id might obliterate SCOPE. */
4437 saved_scope = parser->scope;
4438 saved_object_scope = parser->object_scope;
4439 saved_qualifying_scope = parser->qualifying_scope;
4440 /* Process the final unqualified-id. */
4441 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4442 check_dependency_p,
4443 declarator_p,
4444 /*optional_p=*/false);
4445 /* Restore the SAVED_SCOPE for our caller. */
4446 parser->scope = saved_scope;
4447 parser->object_scope = saved_object_scope;
4448 parser->qualifying_scope = saved_qualifying_scope;
4450 return unqualified_id;
4452 /* Otherwise, if we are in global scope, then we are looking at one
4453 of the other qualified-id productions. */
4454 else if (global_scope_p)
4456 cp_token *token;
4457 tree id;
4459 /* Peek at the next token. */
4460 token = cp_lexer_peek_token (parser->lexer);
4462 /* If it's an identifier, and the next token is not a "<", then
4463 we can avoid the template-id case. This is an optimization
4464 for this common case. */
4465 if (token->type == CPP_NAME
4466 && !cp_parser_nth_token_starts_template_argument_list_p
4467 (parser, 2))
4468 return cp_parser_identifier (parser);
4470 cp_parser_parse_tentatively (parser);
4471 /* Try a template-id. */
4472 id = cp_parser_template_id (parser,
4473 /*template_keyword_p=*/false,
4474 /*check_dependency_p=*/true,
4475 none_type,
4476 declarator_p);
4477 /* If that worked, we're done. */
4478 if (cp_parser_parse_definitely (parser))
4479 return id;
4481 /* Peek at the next token. (Changes in the token buffer may
4482 have invalidated the pointer obtained above.) */
4483 token = cp_lexer_peek_token (parser->lexer);
4485 switch (token->type)
4487 case CPP_NAME:
4488 return cp_parser_identifier (parser);
4490 case CPP_KEYWORD:
4491 if (token->keyword == RID_OPERATOR)
4492 return cp_parser_operator_function_id (parser);
4493 /* Fall through. */
4495 default:
4496 cp_parser_error (parser, "expected id-expression");
4497 return error_mark_node;
4500 else
4501 return cp_parser_unqualified_id (parser, template_keyword_p,
4502 /*check_dependency_p=*/true,
4503 declarator_p,
4504 optional_p);
4507 /* Parse an unqualified-id.
4509 unqualified-id:
4510 identifier
4511 operator-function-id
4512 conversion-function-id
4513 ~ class-name
4514 template-id
4516 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4517 keyword, in a construct like `A::template ...'.
4519 Returns a representation of unqualified-id. For the `identifier'
4520 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4521 production a BIT_NOT_EXPR is returned; the operand of the
4522 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4523 other productions, see the documentation accompanying the
4524 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4525 names are looked up in uninstantiated templates. If DECLARATOR_P
4526 is true, the unqualified-id is appearing as part of a declarator,
4527 rather than as part of an expression. */
4529 static tree
4530 cp_parser_unqualified_id (cp_parser* parser,
4531 bool template_keyword_p,
4532 bool check_dependency_p,
4533 bool declarator_p,
4534 bool optional_p)
4536 cp_token *token;
4538 /* Peek at the next token. */
4539 token = cp_lexer_peek_token (parser->lexer);
4541 switch (token->type)
4543 case CPP_NAME:
4545 tree id;
4547 /* We don't know yet whether or not this will be a
4548 template-id. */
4549 cp_parser_parse_tentatively (parser);
4550 /* Try a template-id. */
4551 id = cp_parser_template_id (parser, template_keyword_p,
4552 check_dependency_p,
4553 none_type,
4554 declarator_p);
4555 /* If it worked, we're done. */
4556 if (cp_parser_parse_definitely (parser))
4557 return id;
4558 /* Otherwise, it's an ordinary identifier. */
4559 return cp_parser_identifier (parser);
4562 case CPP_TEMPLATE_ID:
4563 return cp_parser_template_id (parser, template_keyword_p,
4564 check_dependency_p,
4565 none_type,
4566 declarator_p);
4568 case CPP_COMPL:
4570 tree type_decl;
4571 tree qualifying_scope;
4572 tree object_scope;
4573 tree scope;
4574 bool done;
4576 /* Consume the `~' token. */
4577 cp_lexer_consume_token (parser->lexer);
4578 /* Parse the class-name. The standard, as written, seems to
4579 say that:
4581 template <typename T> struct S { ~S (); };
4582 template <typename T> S<T>::~S() {}
4584 is invalid, since `~' must be followed by a class-name, but
4585 `S<T>' is dependent, and so not known to be a class.
4586 That's not right; we need to look in uninstantiated
4587 templates. A further complication arises from:
4589 template <typename T> void f(T t) {
4590 t.T::~T();
4593 Here, it is not possible to look up `T' in the scope of `T'
4594 itself. We must look in both the current scope, and the
4595 scope of the containing complete expression.
4597 Yet another issue is:
4599 struct S {
4600 int S;
4601 ~S();
4604 S::~S() {}
4606 The standard does not seem to say that the `S' in `~S'
4607 should refer to the type `S' and not the data member
4608 `S::S'. */
4610 /* DR 244 says that we look up the name after the "~" in the
4611 same scope as we looked up the qualifying name. That idea
4612 isn't fully worked out; it's more complicated than that. */
4613 scope = parser->scope;
4614 object_scope = parser->object_scope;
4615 qualifying_scope = parser->qualifying_scope;
4617 /* Check for invalid scopes. */
4618 if (scope == error_mark_node)
4620 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4621 cp_lexer_consume_token (parser->lexer);
4622 return error_mark_node;
4624 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4626 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4627 error_at (token->location,
4628 "scope %qT before %<~%> is not a class-name",
4629 scope);
4630 cp_parser_simulate_error (parser);
4631 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4632 cp_lexer_consume_token (parser->lexer);
4633 return error_mark_node;
4635 gcc_assert (!scope || TYPE_P (scope));
4637 /* If the name is of the form "X::~X" it's OK even if X is a
4638 typedef. */
4639 token = cp_lexer_peek_token (parser->lexer);
4640 if (scope
4641 && token->type == CPP_NAME
4642 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4643 != CPP_LESS)
4644 && (token->u.value == TYPE_IDENTIFIER (scope)
4645 || (CLASS_TYPE_P (scope)
4646 && constructor_name_p (token->u.value, scope))))
4648 cp_lexer_consume_token (parser->lexer);
4649 return build_nt (BIT_NOT_EXPR, scope);
4652 /* If there was an explicit qualification (S::~T), first look
4653 in the scope given by the qualification (i.e., S).
4655 Note: in the calls to cp_parser_class_name below we pass
4656 typename_type so that lookup finds the injected-class-name
4657 rather than the constructor. */
4658 done = false;
4659 type_decl = NULL_TREE;
4660 if (scope)
4662 cp_parser_parse_tentatively (parser);
4663 type_decl = cp_parser_class_name (parser,
4664 /*typename_keyword_p=*/false,
4665 /*template_keyword_p=*/false,
4666 typename_type,
4667 /*check_dependency=*/false,
4668 /*class_head_p=*/false,
4669 declarator_p);
4670 if (cp_parser_parse_definitely (parser))
4671 done = true;
4673 /* In "N::S::~S", look in "N" as well. */
4674 if (!done && scope && qualifying_scope)
4676 cp_parser_parse_tentatively (parser);
4677 parser->scope = qualifying_scope;
4678 parser->object_scope = NULL_TREE;
4679 parser->qualifying_scope = NULL_TREE;
4680 type_decl
4681 = cp_parser_class_name (parser,
4682 /*typename_keyword_p=*/false,
4683 /*template_keyword_p=*/false,
4684 typename_type,
4685 /*check_dependency=*/false,
4686 /*class_head_p=*/false,
4687 declarator_p);
4688 if (cp_parser_parse_definitely (parser))
4689 done = true;
4691 /* In "p->S::~T", look in the scope given by "*p" as well. */
4692 else if (!done && object_scope)
4694 cp_parser_parse_tentatively (parser);
4695 parser->scope = object_scope;
4696 parser->object_scope = NULL_TREE;
4697 parser->qualifying_scope = NULL_TREE;
4698 type_decl
4699 = 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 /* Look in the surrounding context. */
4710 if (!done)
4712 parser->scope = NULL_TREE;
4713 parser->object_scope = NULL_TREE;
4714 parser->qualifying_scope = NULL_TREE;
4715 if (processing_template_decl)
4716 cp_parser_parse_tentatively (parser);
4717 type_decl
4718 = cp_parser_class_name (parser,
4719 /*typename_keyword_p=*/false,
4720 /*template_keyword_p=*/false,
4721 typename_type,
4722 /*check_dependency=*/false,
4723 /*class_head_p=*/false,
4724 declarator_p);
4725 if (processing_template_decl
4726 && ! cp_parser_parse_definitely (parser))
4728 /* We couldn't find a type with this name, so just accept
4729 it and check for a match at instantiation time. */
4730 type_decl = cp_parser_identifier (parser);
4731 if (type_decl != error_mark_node)
4732 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4733 return type_decl;
4736 /* If an error occurred, assume that the name of the
4737 destructor is the same as the name of the qualifying
4738 class. That allows us to keep parsing after running
4739 into ill-formed destructor names. */
4740 if (type_decl == error_mark_node && scope)
4741 return build_nt (BIT_NOT_EXPR, scope);
4742 else if (type_decl == error_mark_node)
4743 return error_mark_node;
4745 /* Check that destructor name and scope match. */
4746 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4748 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4749 error_at (token->location,
4750 "declaration of %<~%T%> as member of %qT",
4751 type_decl, scope);
4752 cp_parser_simulate_error (parser);
4753 return error_mark_node;
4756 /* [class.dtor]
4758 A typedef-name that names a class shall not be used as the
4759 identifier in the declarator for a destructor declaration. */
4760 if (declarator_p
4761 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4762 && !DECL_SELF_REFERENCE_P (type_decl)
4763 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4764 error_at (token->location,
4765 "typedef-name %qD used as destructor declarator",
4766 type_decl);
4768 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4771 case CPP_KEYWORD:
4772 if (token->keyword == RID_OPERATOR)
4774 tree id;
4776 /* This could be a template-id, so we try that first. */
4777 cp_parser_parse_tentatively (parser);
4778 /* Try a template-id. */
4779 id = cp_parser_template_id (parser, template_keyword_p,
4780 /*check_dependency_p=*/true,
4781 none_type,
4782 declarator_p);
4783 /* If that worked, we're done. */
4784 if (cp_parser_parse_definitely (parser))
4785 return id;
4786 /* We still don't know whether we're looking at an
4787 operator-function-id or a conversion-function-id. */
4788 cp_parser_parse_tentatively (parser);
4789 /* Try an operator-function-id. */
4790 id = cp_parser_operator_function_id (parser);
4791 /* If that didn't work, try a conversion-function-id. */
4792 if (!cp_parser_parse_definitely (parser))
4793 id = cp_parser_conversion_function_id (parser);
4794 else if (UDLIT_OPER_P (id))
4796 /* 17.6.3.3.5 */
4797 const char *name = UDLIT_OP_SUFFIX (id);
4798 if (name[0] != '_' && !in_system_header)
4799 warning (0, "literal operator suffixes not preceded by %<_%>"
4800 " are reserved for future standardization");
4803 return id;
4805 /* Fall through. */
4807 default:
4808 if (optional_p)
4809 return NULL_TREE;
4810 cp_parser_error (parser, "expected unqualified-id");
4811 return error_mark_node;
4815 /* Parse an (optional) nested-name-specifier.
4817 nested-name-specifier: [C++98]
4818 class-or-namespace-name :: nested-name-specifier [opt]
4819 class-or-namespace-name :: template nested-name-specifier [opt]
4821 nested-name-specifier: [C++0x]
4822 type-name ::
4823 namespace-name ::
4824 nested-name-specifier identifier ::
4825 nested-name-specifier template [opt] simple-template-id ::
4827 PARSER->SCOPE should be set appropriately before this function is
4828 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4829 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4830 in name lookups.
4832 Sets PARSER->SCOPE to the class (TYPE) or namespace
4833 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4834 it unchanged if there is no nested-name-specifier. Returns the new
4835 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4837 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4838 part of a declaration and/or decl-specifier. */
4840 static tree
4841 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4842 bool typename_keyword_p,
4843 bool check_dependency_p,
4844 bool type_p,
4845 bool is_declaration)
4847 bool success = false;
4848 cp_token_position start = 0;
4849 cp_token *token;
4851 /* Remember where the nested-name-specifier starts. */
4852 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4854 start = cp_lexer_token_position (parser->lexer, false);
4855 push_deferring_access_checks (dk_deferred);
4858 while (true)
4860 tree new_scope;
4861 tree old_scope;
4862 tree saved_qualifying_scope;
4863 bool template_keyword_p;
4865 /* Spot cases that cannot be the beginning of a
4866 nested-name-specifier. */
4867 token = cp_lexer_peek_token (parser->lexer);
4869 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4870 the already parsed nested-name-specifier. */
4871 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4873 /* Grab the nested-name-specifier and continue the loop. */
4874 cp_parser_pre_parsed_nested_name_specifier (parser);
4875 /* If we originally encountered this nested-name-specifier
4876 with IS_DECLARATION set to false, we will not have
4877 resolved TYPENAME_TYPEs, so we must do so here. */
4878 if (is_declaration
4879 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4881 new_scope = resolve_typename_type (parser->scope,
4882 /*only_current_p=*/false);
4883 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4884 parser->scope = new_scope;
4886 success = true;
4887 continue;
4890 /* Spot cases that cannot be the beginning of a
4891 nested-name-specifier. On the second and subsequent times
4892 through the loop, we look for the `template' keyword. */
4893 if (success && token->keyword == RID_TEMPLATE)
4895 /* A template-id can start a nested-name-specifier. */
4896 else if (token->type == CPP_TEMPLATE_ID)
4898 /* DR 743: decltype can be used in a nested-name-specifier. */
4899 else if (token_is_decltype (token))
4901 else
4903 /* If the next token is not an identifier, then it is
4904 definitely not a type-name or namespace-name. */
4905 if (token->type != CPP_NAME)
4906 break;
4907 /* If the following token is neither a `<' (to begin a
4908 template-id), nor a `::', then we are not looking at a
4909 nested-name-specifier. */
4910 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4912 if (token->type == CPP_COLON
4913 && parser->colon_corrects_to_scope_p
4914 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4916 error_at (token->location,
4917 "found %<:%> in nested-name-specifier, expected %<::%>");
4918 token->type = CPP_SCOPE;
4921 if (token->type != CPP_SCOPE
4922 && !cp_parser_nth_token_starts_template_argument_list_p
4923 (parser, 2))
4924 break;
4927 /* The nested-name-specifier is optional, so we parse
4928 tentatively. */
4929 cp_parser_parse_tentatively (parser);
4931 /* Look for the optional `template' keyword, if this isn't the
4932 first time through the loop. */
4933 if (success)
4934 template_keyword_p = cp_parser_optional_template_keyword (parser);
4935 else
4936 template_keyword_p = false;
4938 /* Save the old scope since the name lookup we are about to do
4939 might destroy it. */
4940 old_scope = parser->scope;
4941 saved_qualifying_scope = parser->qualifying_scope;
4942 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4943 look up names in "X<T>::I" in order to determine that "Y" is
4944 a template. So, if we have a typename at this point, we make
4945 an effort to look through it. */
4946 if (is_declaration
4947 && !typename_keyword_p
4948 && parser->scope
4949 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4950 parser->scope = resolve_typename_type (parser->scope,
4951 /*only_current_p=*/false);
4952 /* Parse the qualifying entity. */
4953 new_scope
4954 = cp_parser_qualifying_entity (parser,
4955 typename_keyword_p,
4956 template_keyword_p,
4957 check_dependency_p,
4958 type_p,
4959 is_declaration);
4960 /* Look for the `::' token. */
4961 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4963 /* If we found what we wanted, we keep going; otherwise, we're
4964 done. */
4965 if (!cp_parser_parse_definitely (parser))
4967 bool error_p = false;
4969 /* Restore the OLD_SCOPE since it was valid before the
4970 failed attempt at finding the last
4971 class-or-namespace-name. */
4972 parser->scope = old_scope;
4973 parser->qualifying_scope = saved_qualifying_scope;
4975 /* If the next token is a decltype, and the one after that is a
4976 `::', then the decltype has failed to resolve to a class or
4977 enumeration type. Give this error even when parsing
4978 tentatively since it can't possibly be valid--and we're going
4979 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4980 won't get another chance.*/
4981 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4982 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4983 == CPP_SCOPE))
4985 token = cp_lexer_consume_token (parser->lexer);
4986 error_at (token->location, "decltype evaluates to %qT, "
4987 "which is not a class or enumeration type",
4988 token->u.value);
4989 parser->scope = error_mark_node;
4990 error_p = true;
4991 /* As below. */
4992 success = true;
4993 cp_lexer_consume_token (parser->lexer);
4996 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4997 break;
4998 /* If the next token is an identifier, and the one after
4999 that is a `::', then any valid interpretation would have
5000 found a class-or-namespace-name. */
5001 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5002 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5003 == CPP_SCOPE)
5004 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5005 != CPP_COMPL))
5007 token = cp_lexer_consume_token (parser->lexer);
5008 if (!error_p)
5010 if (!token->ambiguous_p)
5012 tree decl;
5013 tree ambiguous_decls;
5015 decl = cp_parser_lookup_name (parser, token->u.value,
5016 none_type,
5017 /*is_template=*/false,
5018 /*is_namespace=*/false,
5019 /*check_dependency=*/true,
5020 &ambiguous_decls,
5021 token->location);
5022 if (TREE_CODE (decl) == TEMPLATE_DECL)
5023 error_at (token->location,
5024 "%qD used without template parameters",
5025 decl);
5026 else if (ambiguous_decls)
5028 error_at (token->location,
5029 "reference to %qD is ambiguous",
5030 token->u.value);
5031 print_candidates (ambiguous_decls);
5032 decl = error_mark_node;
5034 else
5036 if (cxx_dialect != cxx98)
5037 cp_parser_name_lookup_error
5038 (parser, token->u.value, decl, NLE_NOT_CXX98,
5039 token->location);
5040 else
5041 cp_parser_name_lookup_error
5042 (parser, token->u.value, decl, NLE_CXX98,
5043 token->location);
5046 parser->scope = error_mark_node;
5047 error_p = true;
5048 /* Treat this as a successful nested-name-specifier
5049 due to:
5051 [basic.lookup.qual]
5053 If the name found is not a class-name (clause
5054 _class_) or namespace-name (_namespace.def_), the
5055 program is ill-formed. */
5056 success = true;
5058 cp_lexer_consume_token (parser->lexer);
5060 break;
5062 /* We've found one valid nested-name-specifier. */
5063 success = true;
5064 /* Name lookup always gives us a DECL. */
5065 if (TREE_CODE (new_scope) == TYPE_DECL)
5066 new_scope = TREE_TYPE (new_scope);
5067 /* Uses of "template" must be followed by actual templates. */
5068 if (template_keyword_p
5069 && !(CLASS_TYPE_P (new_scope)
5070 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5071 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5072 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5073 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5074 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5075 == TEMPLATE_ID_EXPR)))
5076 permerror (input_location, TYPE_P (new_scope)
5077 ? G_("%qT is not a template")
5078 : G_("%qD is not a template"),
5079 new_scope);
5080 /* If it is a class scope, try to complete it; we are about to
5081 be looking up names inside the class. */
5082 if (TYPE_P (new_scope)
5083 /* Since checking types for dependency can be expensive,
5084 avoid doing it if the type is already complete. */
5085 && !COMPLETE_TYPE_P (new_scope)
5086 /* Do not try to complete dependent types. */
5087 && !dependent_type_p (new_scope))
5089 new_scope = complete_type (new_scope);
5090 /* If it is a typedef to current class, use the current
5091 class instead, as the typedef won't have any names inside
5092 it yet. */
5093 if (!COMPLETE_TYPE_P (new_scope)
5094 && currently_open_class (new_scope))
5095 new_scope = TYPE_MAIN_VARIANT (new_scope);
5097 /* Make sure we look in the right scope the next time through
5098 the loop. */
5099 parser->scope = new_scope;
5102 /* If parsing tentatively, replace the sequence of tokens that makes
5103 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5104 token. That way, should we re-parse the token stream, we will
5105 not have to repeat the effort required to do the parse, nor will
5106 we issue duplicate error messages. */
5107 if (success && start)
5109 cp_token *token;
5111 token = cp_lexer_token_at (parser->lexer, start);
5112 /* Reset the contents of the START token. */
5113 token->type = CPP_NESTED_NAME_SPECIFIER;
5114 /* Retrieve any deferred checks. Do not pop this access checks yet
5115 so the memory will not be reclaimed during token replacing below. */
5116 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5117 token->u.tree_check_value->value = parser->scope;
5118 token->u.tree_check_value->checks = get_deferred_access_checks ();
5119 token->u.tree_check_value->qualifying_scope =
5120 parser->qualifying_scope;
5121 token->keyword = RID_MAX;
5123 /* Purge all subsequent tokens. */
5124 cp_lexer_purge_tokens_after (parser->lexer, start);
5127 if (start)
5128 pop_to_parent_deferring_access_checks ();
5130 return success ? parser->scope : NULL_TREE;
5133 /* Parse a nested-name-specifier. See
5134 cp_parser_nested_name_specifier_opt for details. This function
5135 behaves identically, except that it will an issue an error if no
5136 nested-name-specifier is present. */
5138 static tree
5139 cp_parser_nested_name_specifier (cp_parser *parser,
5140 bool typename_keyword_p,
5141 bool check_dependency_p,
5142 bool type_p,
5143 bool is_declaration)
5145 tree scope;
5147 /* Look for the nested-name-specifier. */
5148 scope = cp_parser_nested_name_specifier_opt (parser,
5149 typename_keyword_p,
5150 check_dependency_p,
5151 type_p,
5152 is_declaration);
5153 /* If it was not present, issue an error message. */
5154 if (!scope)
5156 cp_parser_error (parser, "expected nested-name-specifier");
5157 parser->scope = NULL_TREE;
5160 return scope;
5163 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5164 this is either a class-name or a namespace-name (which corresponds
5165 to the class-or-namespace-name production in the grammar). For
5166 C++0x, it can also be a type-name that refers to an enumeration
5167 type or a simple-template-id.
5169 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5170 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5171 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5172 TYPE_P is TRUE iff the next name should be taken as a class-name,
5173 even the same name is declared to be another entity in the same
5174 scope.
5176 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5177 specified by the class-or-namespace-name. If neither is found the
5178 ERROR_MARK_NODE is returned. */
5180 static tree
5181 cp_parser_qualifying_entity (cp_parser *parser,
5182 bool typename_keyword_p,
5183 bool template_keyword_p,
5184 bool check_dependency_p,
5185 bool type_p,
5186 bool is_declaration)
5188 tree saved_scope;
5189 tree saved_qualifying_scope;
5190 tree saved_object_scope;
5191 tree scope;
5192 bool only_class_p;
5193 bool successful_parse_p;
5195 /* DR 743: decltype can appear in a nested-name-specifier. */
5196 if (cp_lexer_next_token_is_decltype (parser->lexer))
5198 scope = cp_parser_decltype (parser);
5199 if (TREE_CODE (scope) != ENUMERAL_TYPE
5200 && !MAYBE_CLASS_TYPE_P (scope))
5202 cp_parser_simulate_error (parser);
5203 return error_mark_node;
5205 if (TYPE_NAME (scope))
5206 scope = TYPE_NAME (scope);
5207 return scope;
5210 /* Before we try to parse the class-name, we must save away the
5211 current PARSER->SCOPE since cp_parser_class_name will destroy
5212 it. */
5213 saved_scope = parser->scope;
5214 saved_qualifying_scope = parser->qualifying_scope;
5215 saved_object_scope = parser->object_scope;
5216 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5217 there is no need to look for a namespace-name. */
5218 only_class_p = template_keyword_p
5219 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5220 if (!only_class_p)
5221 cp_parser_parse_tentatively (parser);
5222 scope = cp_parser_class_name (parser,
5223 typename_keyword_p,
5224 template_keyword_p,
5225 type_p ? class_type : none_type,
5226 check_dependency_p,
5227 /*class_head_p=*/false,
5228 is_declaration);
5229 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5230 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5231 if (!only_class_p
5232 && cxx_dialect != cxx98
5233 && !successful_parse_p)
5235 /* Restore the saved scope. */
5236 parser->scope = saved_scope;
5237 parser->qualifying_scope = saved_qualifying_scope;
5238 parser->object_scope = saved_object_scope;
5240 /* Parse tentatively. */
5241 cp_parser_parse_tentatively (parser);
5243 /* Parse a type-name */
5244 scope = cp_parser_type_name (parser);
5246 /* "If the name found does not designate a namespace or a class,
5247 enumeration, or dependent type, the program is ill-formed."
5249 We cover classes and dependent types above and namespaces below,
5250 so this code is only looking for enums. */
5251 if (!scope || TREE_CODE (scope) != TYPE_DECL
5252 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5253 cp_parser_simulate_error (parser);
5255 successful_parse_p = cp_parser_parse_definitely (parser);
5257 /* If that didn't work, try for a namespace-name. */
5258 if (!only_class_p && !successful_parse_p)
5260 /* Restore the saved scope. */
5261 parser->scope = saved_scope;
5262 parser->qualifying_scope = saved_qualifying_scope;
5263 parser->object_scope = saved_object_scope;
5264 /* If we are not looking at an identifier followed by the scope
5265 resolution operator, then this is not part of a
5266 nested-name-specifier. (Note that this function is only used
5267 to parse the components of a nested-name-specifier.) */
5268 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5269 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5270 return error_mark_node;
5271 scope = cp_parser_namespace_name (parser);
5274 return scope;
5277 /* Parse a postfix-expression.
5279 postfix-expression:
5280 primary-expression
5281 postfix-expression [ expression ]
5282 postfix-expression ( expression-list [opt] )
5283 simple-type-specifier ( expression-list [opt] )
5284 typename :: [opt] nested-name-specifier identifier
5285 ( expression-list [opt] )
5286 typename :: [opt] nested-name-specifier template [opt] template-id
5287 ( expression-list [opt] )
5288 postfix-expression . template [opt] id-expression
5289 postfix-expression -> template [opt] id-expression
5290 postfix-expression . pseudo-destructor-name
5291 postfix-expression -> pseudo-destructor-name
5292 postfix-expression ++
5293 postfix-expression --
5294 dynamic_cast < type-id > ( expression )
5295 static_cast < type-id > ( expression )
5296 reinterpret_cast < type-id > ( expression )
5297 const_cast < type-id > ( expression )
5298 typeid ( expression )
5299 typeid ( type-id )
5301 GNU Extension:
5303 postfix-expression:
5304 ( type-id ) { initializer-list , [opt] }
5306 This extension is a GNU version of the C99 compound-literal
5307 construct. (The C99 grammar uses `type-name' instead of `type-id',
5308 but they are essentially the same concept.)
5310 If ADDRESS_P is true, the postfix expression is the operand of the
5311 `&' operator. CAST_P is true if this expression is the target of a
5312 cast.
5314 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5315 class member access expressions [expr.ref].
5317 Returns a representation of the expression. */
5319 static tree
5320 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5321 bool member_access_only_p,
5322 cp_id_kind * pidk_return)
5324 cp_token *token;
5325 enum rid keyword;
5326 cp_id_kind idk = CP_ID_KIND_NONE;
5327 tree postfix_expression = NULL_TREE;
5328 bool is_member_access = false;
5330 /* Peek at the next token. */
5331 token = cp_lexer_peek_token (parser->lexer);
5332 /* Some of the productions are determined by keywords. */
5333 keyword = token->keyword;
5334 switch (keyword)
5336 case RID_DYNCAST:
5337 case RID_STATCAST:
5338 case RID_REINTCAST:
5339 case RID_CONSTCAST:
5341 tree type;
5342 tree expression;
5343 const char *saved_message;
5345 /* All of these can be handled in the same way from the point
5346 of view of parsing. Begin by consuming the token
5347 identifying the cast. */
5348 cp_lexer_consume_token (parser->lexer);
5350 /* New types cannot be defined in the cast. */
5351 saved_message = parser->type_definition_forbidden_message;
5352 parser->type_definition_forbidden_message
5353 = G_("types may not be defined in casts");
5355 /* Look for the opening `<'. */
5356 cp_parser_require (parser, CPP_LESS, RT_LESS);
5357 /* Parse the type to which we are casting. */
5358 type = cp_parser_type_id (parser);
5359 /* Look for the closing `>'. */
5360 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5361 /* Restore the old message. */
5362 parser->type_definition_forbidden_message = saved_message;
5364 /* And the expression which is being cast. */
5365 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5366 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5367 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5369 /* Only type conversions to integral or enumeration types
5370 can be used in constant-expressions. */
5371 if (!cast_valid_in_integral_constant_expression_p (type)
5372 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5373 return error_mark_node;
5375 switch (keyword)
5377 case RID_DYNCAST:
5378 postfix_expression
5379 = build_dynamic_cast (type, expression, tf_warning_or_error);
5380 break;
5381 case RID_STATCAST:
5382 postfix_expression
5383 = build_static_cast (type, expression, tf_warning_or_error);
5384 break;
5385 case RID_REINTCAST:
5386 postfix_expression
5387 = build_reinterpret_cast (type, expression,
5388 tf_warning_or_error);
5389 break;
5390 case RID_CONSTCAST:
5391 postfix_expression
5392 = build_const_cast (type, expression, tf_warning_or_error);
5393 break;
5394 default:
5395 gcc_unreachable ();
5398 break;
5400 case RID_TYPEID:
5402 tree type;
5403 const char *saved_message;
5404 bool saved_in_type_id_in_expr_p;
5406 /* Consume the `typeid' token. */
5407 cp_lexer_consume_token (parser->lexer);
5408 /* Look for the `(' token. */
5409 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5410 /* Types cannot be defined in a `typeid' expression. */
5411 saved_message = parser->type_definition_forbidden_message;
5412 parser->type_definition_forbidden_message
5413 = G_("types may not be defined in a %<typeid%> expression");
5414 /* We can't be sure yet whether we're looking at a type-id or an
5415 expression. */
5416 cp_parser_parse_tentatively (parser);
5417 /* Try a type-id first. */
5418 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5419 parser->in_type_id_in_expr_p = true;
5420 type = cp_parser_type_id (parser);
5421 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5422 /* Look for the `)' token. Otherwise, we can't be sure that
5423 we're not looking at an expression: consider `typeid (int
5424 (3))', for example. */
5425 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5426 /* If all went well, simply lookup the type-id. */
5427 if (cp_parser_parse_definitely (parser))
5428 postfix_expression = get_typeid (type);
5429 /* Otherwise, fall back to the expression variant. */
5430 else
5432 tree expression;
5434 /* Look for an expression. */
5435 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5436 /* Compute its typeid. */
5437 postfix_expression = build_typeid (expression);
5438 /* Look for the `)' token. */
5439 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5441 /* Restore the saved message. */
5442 parser->type_definition_forbidden_message = saved_message;
5443 /* `typeid' may not appear in an integral constant expression. */
5444 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5445 return error_mark_node;
5447 break;
5449 case RID_TYPENAME:
5451 tree type;
5452 /* The syntax permitted here is the same permitted for an
5453 elaborated-type-specifier. */
5454 type = cp_parser_elaborated_type_specifier (parser,
5455 /*is_friend=*/false,
5456 /*is_declaration=*/false);
5457 postfix_expression = cp_parser_functional_cast (parser, type);
5459 break;
5461 case RID_BUILTIN_SHUFFLE:
5463 VEC(tree,gc)* vec;
5464 unsigned int i;
5465 tree p;
5466 location_t loc = token->location;
5468 cp_lexer_consume_token (parser->lexer);
5469 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5470 /*cast_p=*/false, /*allow_expansion_p=*/true,
5471 /*non_constant_p=*/NULL);
5472 if (vec == NULL)
5473 return error_mark_node;
5475 FOR_EACH_VEC_ELT (tree, vec, i, p)
5476 mark_exp_read (p);
5478 if (VEC_length (tree, vec) == 2)
5479 return
5480 c_build_vec_perm_expr
5481 (loc, VEC_index (tree, vec, 0),
5482 NULL_TREE, VEC_index (tree, vec, 1));
5484 else if (VEC_length (tree, vec) == 3)
5485 return
5486 c_build_vec_perm_expr
5487 (loc, VEC_index (tree, vec, 0),
5488 VEC_index (tree, vec, 1),
5489 VEC_index (tree, vec, 2));
5490 else
5492 error_at (loc, "wrong number of arguments to "
5493 "%<__builtin_shuffle%>");
5494 return error_mark_node;
5496 break;
5499 default:
5501 tree type;
5503 /* If the next thing is a simple-type-specifier, we may be
5504 looking at a functional cast. We could also be looking at
5505 an id-expression. So, we try the functional cast, and if
5506 that doesn't work we fall back to the primary-expression. */
5507 cp_parser_parse_tentatively (parser);
5508 /* Look for the simple-type-specifier. */
5509 type = cp_parser_simple_type_specifier (parser,
5510 /*decl_specs=*/NULL,
5511 CP_PARSER_FLAGS_NONE);
5512 /* Parse the cast itself. */
5513 if (!cp_parser_error_occurred (parser))
5514 postfix_expression
5515 = cp_parser_functional_cast (parser, type);
5516 /* If that worked, we're done. */
5517 if (cp_parser_parse_definitely (parser))
5518 break;
5520 /* If the functional-cast didn't work out, try a
5521 compound-literal. */
5522 if (cp_parser_allow_gnu_extensions_p (parser)
5523 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5525 VEC(constructor_elt,gc) *initializer_list = NULL;
5526 bool saved_in_type_id_in_expr_p;
5528 cp_parser_parse_tentatively (parser);
5529 /* Consume the `('. */
5530 cp_lexer_consume_token (parser->lexer);
5531 /* Parse the type. */
5532 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5533 parser->in_type_id_in_expr_p = true;
5534 type = cp_parser_type_id (parser);
5535 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5536 /* Look for the `)'. */
5537 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5538 /* Look for the `{'. */
5539 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5540 /* If things aren't going well, there's no need to
5541 keep going. */
5542 if (!cp_parser_error_occurred (parser))
5544 bool non_constant_p;
5545 /* Parse the initializer-list. */
5546 initializer_list
5547 = cp_parser_initializer_list (parser, &non_constant_p);
5548 /* Allow a trailing `,'. */
5549 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5550 cp_lexer_consume_token (parser->lexer);
5551 /* Look for the final `}'. */
5552 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5554 /* If that worked, we're definitely looking at a
5555 compound-literal expression. */
5556 if (cp_parser_parse_definitely (parser))
5558 /* Warn the user that a compound literal is not
5559 allowed in standard C++. */
5560 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5561 /* For simplicity, we disallow compound literals in
5562 constant-expressions. We could
5563 allow compound literals of integer type, whose
5564 initializer was a constant, in constant
5565 expressions. Permitting that usage, as a further
5566 extension, would not change the meaning of any
5567 currently accepted programs. (Of course, as
5568 compound literals are not part of ISO C++, the
5569 standard has nothing to say.) */
5570 if (cp_parser_non_integral_constant_expression (parser,
5571 NIC_NCC))
5573 postfix_expression = error_mark_node;
5574 break;
5576 /* Form the representation of the compound-literal. */
5577 postfix_expression
5578 = (finish_compound_literal
5579 (type, build_constructor (init_list_type_node,
5580 initializer_list),
5581 tf_warning_or_error));
5582 break;
5586 /* It must be a primary-expression. */
5587 postfix_expression
5588 = cp_parser_primary_expression (parser, address_p, cast_p,
5589 /*template_arg_p=*/false,
5590 &idk);
5592 break;
5595 /* Keep looping until the postfix-expression is complete. */
5596 while (true)
5598 if (idk == CP_ID_KIND_UNQUALIFIED
5599 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5600 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5601 /* It is not a Koenig lookup function call. */
5602 postfix_expression
5603 = unqualified_name_lookup_error (postfix_expression);
5605 /* Peek at the next token. */
5606 token = cp_lexer_peek_token (parser->lexer);
5608 switch (token->type)
5610 case CPP_OPEN_SQUARE:
5611 postfix_expression
5612 = cp_parser_postfix_open_square_expression (parser,
5613 postfix_expression,
5614 false);
5615 idk = CP_ID_KIND_NONE;
5616 is_member_access = false;
5617 break;
5619 case CPP_OPEN_PAREN:
5620 /* postfix-expression ( expression-list [opt] ) */
5622 bool koenig_p;
5623 bool is_builtin_constant_p;
5624 bool saved_integral_constant_expression_p = false;
5625 bool saved_non_integral_constant_expression_p = false;
5626 VEC(tree,gc) *args;
5628 is_member_access = false;
5630 is_builtin_constant_p
5631 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5632 if (is_builtin_constant_p)
5634 /* The whole point of __builtin_constant_p is to allow
5635 non-constant expressions to appear as arguments. */
5636 saved_integral_constant_expression_p
5637 = parser->integral_constant_expression_p;
5638 saved_non_integral_constant_expression_p
5639 = parser->non_integral_constant_expression_p;
5640 parser->integral_constant_expression_p = false;
5642 args = (cp_parser_parenthesized_expression_list
5643 (parser, non_attr,
5644 /*cast_p=*/false, /*allow_expansion_p=*/true,
5645 /*non_constant_p=*/NULL));
5646 if (is_builtin_constant_p)
5648 parser->integral_constant_expression_p
5649 = saved_integral_constant_expression_p;
5650 parser->non_integral_constant_expression_p
5651 = saved_non_integral_constant_expression_p;
5654 if (args == NULL)
5656 postfix_expression = error_mark_node;
5657 break;
5660 /* Function calls are not permitted in
5661 constant-expressions. */
5662 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5663 && cp_parser_non_integral_constant_expression (parser,
5664 NIC_FUNC_CALL))
5666 postfix_expression = error_mark_node;
5667 release_tree_vector (args);
5668 break;
5671 koenig_p = false;
5672 if (idk == CP_ID_KIND_UNQUALIFIED
5673 || idk == CP_ID_KIND_TEMPLATE_ID)
5675 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5677 if (!VEC_empty (tree, args))
5679 koenig_p = true;
5680 if (!any_type_dependent_arguments_p (args))
5681 postfix_expression
5682 = perform_koenig_lookup (postfix_expression, args,
5683 /*include_std=*/false,
5684 tf_warning_or_error);
5686 else
5687 postfix_expression
5688 = unqualified_fn_lookup_error (postfix_expression);
5690 /* We do not perform argument-dependent lookup if
5691 normal lookup finds a non-function, in accordance
5692 with the expected resolution of DR 218. */
5693 else if (!VEC_empty (tree, args)
5694 && is_overloaded_fn (postfix_expression))
5696 tree fn = get_first_fn (postfix_expression);
5697 fn = STRIP_TEMPLATE (fn);
5699 /* Do not do argument dependent lookup if regular
5700 lookup finds a member function or a block-scope
5701 function declaration. [basic.lookup.argdep]/3 */
5702 if (!DECL_FUNCTION_MEMBER_P (fn)
5703 && !DECL_LOCAL_FUNCTION_P (fn))
5705 koenig_p = true;
5706 if (!any_type_dependent_arguments_p (args))
5707 postfix_expression
5708 = perform_koenig_lookup (postfix_expression, args,
5709 /*include_std=*/false,
5710 tf_warning_or_error);
5715 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5717 tree instance = TREE_OPERAND (postfix_expression, 0);
5718 tree fn = TREE_OPERAND (postfix_expression, 1);
5720 if (processing_template_decl
5721 && (type_dependent_expression_p (instance)
5722 || (!BASELINK_P (fn)
5723 && TREE_CODE (fn) != FIELD_DECL)
5724 || type_dependent_expression_p (fn)
5725 || any_type_dependent_arguments_p (args)))
5727 postfix_expression
5728 = build_nt_call_vec (postfix_expression, args);
5729 release_tree_vector (args);
5730 break;
5733 if (BASELINK_P (fn))
5735 postfix_expression
5736 = (build_new_method_call
5737 (instance, fn, &args, NULL_TREE,
5738 (idk == CP_ID_KIND_QUALIFIED
5739 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5740 : LOOKUP_NORMAL),
5741 /*fn_p=*/NULL,
5742 tf_warning_or_error));
5744 else
5745 postfix_expression
5746 = finish_call_expr (postfix_expression, &args,
5747 /*disallow_virtual=*/false,
5748 /*koenig_p=*/false,
5749 tf_warning_or_error);
5751 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5752 || TREE_CODE (postfix_expression) == MEMBER_REF
5753 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5754 postfix_expression = (build_offset_ref_call_from_tree
5755 (postfix_expression, &args));
5756 else if (idk == CP_ID_KIND_QUALIFIED)
5757 /* A call to a static class member, or a namespace-scope
5758 function. */
5759 postfix_expression
5760 = finish_call_expr (postfix_expression, &args,
5761 /*disallow_virtual=*/true,
5762 koenig_p,
5763 tf_warning_or_error);
5764 else
5765 /* All other function calls. */
5766 postfix_expression
5767 = finish_call_expr (postfix_expression, &args,
5768 /*disallow_virtual=*/false,
5769 koenig_p,
5770 tf_warning_or_error);
5772 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5773 idk = CP_ID_KIND_NONE;
5775 release_tree_vector (args);
5777 break;
5779 case CPP_DOT:
5780 case CPP_DEREF:
5781 /* postfix-expression . template [opt] id-expression
5782 postfix-expression . pseudo-destructor-name
5783 postfix-expression -> template [opt] id-expression
5784 postfix-expression -> pseudo-destructor-name */
5786 /* Consume the `.' or `->' operator. */
5787 cp_lexer_consume_token (parser->lexer);
5789 postfix_expression
5790 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5791 postfix_expression,
5792 false, &idk,
5793 token->location);
5795 is_member_access = true;
5796 break;
5798 case CPP_PLUS_PLUS:
5799 /* postfix-expression ++ */
5800 /* Consume the `++' token. */
5801 cp_lexer_consume_token (parser->lexer);
5802 /* Generate a representation for the complete expression. */
5803 postfix_expression
5804 = finish_increment_expr (postfix_expression,
5805 POSTINCREMENT_EXPR);
5806 /* Increments may not appear in constant-expressions. */
5807 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5808 postfix_expression = error_mark_node;
5809 idk = CP_ID_KIND_NONE;
5810 is_member_access = false;
5811 break;
5813 case CPP_MINUS_MINUS:
5814 /* postfix-expression -- */
5815 /* Consume the `--' token. */
5816 cp_lexer_consume_token (parser->lexer);
5817 /* Generate a representation for the complete expression. */
5818 postfix_expression
5819 = finish_increment_expr (postfix_expression,
5820 POSTDECREMENT_EXPR);
5821 /* Decrements may not appear in constant-expressions. */
5822 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5823 postfix_expression = error_mark_node;
5824 idk = CP_ID_KIND_NONE;
5825 is_member_access = false;
5826 break;
5828 default:
5829 if (pidk_return != NULL)
5830 * pidk_return = idk;
5831 if (member_access_only_p)
5832 return is_member_access? postfix_expression : error_mark_node;
5833 else
5834 return postfix_expression;
5838 /* We should never get here. */
5839 gcc_unreachable ();
5840 return error_mark_node;
5843 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5844 by cp_parser_builtin_offsetof. We're looking for
5846 postfix-expression [ expression ]
5847 postfix-expression [ braced-init-list ] (C++11)
5849 FOR_OFFSETOF is set if we're being called in that context, which
5850 changes how we deal with integer constant expressions. */
5852 static tree
5853 cp_parser_postfix_open_square_expression (cp_parser *parser,
5854 tree postfix_expression,
5855 bool for_offsetof)
5857 tree index;
5858 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5860 /* Consume the `[' token. */
5861 cp_lexer_consume_token (parser->lexer);
5863 /* Parse the index expression. */
5864 /* ??? For offsetof, there is a question of what to allow here. If
5865 offsetof is not being used in an integral constant expression context,
5866 then we *could* get the right answer by computing the value at runtime.
5867 If we are in an integral constant expression context, then we might
5868 could accept any constant expression; hard to say without analysis.
5869 Rather than open the barn door too wide right away, allow only integer
5870 constant expressions here. */
5871 if (for_offsetof)
5872 index = cp_parser_constant_expression (parser, false, NULL);
5873 else
5875 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5877 bool expr_nonconst_p;
5878 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5879 index = cp_parser_braced_list (parser, &expr_nonconst_p);
5881 else
5882 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5885 /* Look for the closing `]'. */
5886 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5888 /* Build the ARRAY_REF. */
5889 postfix_expression = grok_array_decl (loc, postfix_expression, index);
5891 /* When not doing offsetof, array references are not permitted in
5892 constant-expressions. */
5893 if (!for_offsetof
5894 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5895 postfix_expression = error_mark_node;
5897 return postfix_expression;
5900 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5901 by cp_parser_builtin_offsetof. We're looking for
5903 postfix-expression . template [opt] id-expression
5904 postfix-expression . pseudo-destructor-name
5905 postfix-expression -> template [opt] id-expression
5906 postfix-expression -> pseudo-destructor-name
5908 FOR_OFFSETOF is set if we're being called in that context. That sorta
5909 limits what of the above we'll actually accept, but nevermind.
5910 TOKEN_TYPE is the "." or "->" token, which will already have been
5911 removed from the stream. */
5913 static tree
5914 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5915 enum cpp_ttype token_type,
5916 tree postfix_expression,
5917 bool for_offsetof, cp_id_kind *idk,
5918 location_t location)
5920 tree name;
5921 bool dependent_p;
5922 bool pseudo_destructor_p;
5923 tree scope = NULL_TREE;
5925 /* If this is a `->' operator, dereference the pointer. */
5926 if (token_type == CPP_DEREF)
5927 postfix_expression = build_x_arrow (location, postfix_expression,
5928 tf_warning_or_error);
5929 /* Check to see whether or not the expression is type-dependent. */
5930 dependent_p = type_dependent_expression_p (postfix_expression);
5931 /* The identifier following the `->' or `.' is not qualified. */
5932 parser->scope = NULL_TREE;
5933 parser->qualifying_scope = NULL_TREE;
5934 parser->object_scope = NULL_TREE;
5935 *idk = CP_ID_KIND_NONE;
5937 /* Enter the scope corresponding to the type of the object
5938 given by the POSTFIX_EXPRESSION. */
5939 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5941 scope = TREE_TYPE (postfix_expression);
5942 /* According to the standard, no expression should ever have
5943 reference type. Unfortunately, we do not currently match
5944 the standard in this respect in that our internal representation
5945 of an expression may have reference type even when the standard
5946 says it does not. Therefore, we have to manually obtain the
5947 underlying type here. */
5948 scope = non_reference (scope);
5949 /* The type of the POSTFIX_EXPRESSION must be complete. */
5950 if (scope == unknown_type_node)
5952 error_at (location, "%qE does not have class type",
5953 postfix_expression);
5954 scope = NULL_TREE;
5956 /* Unlike the object expression in other contexts, *this is not
5957 required to be of complete type for purposes of class member
5958 access (5.2.5) outside the member function body. */
5959 else if (scope != current_class_ref
5960 && !(processing_template_decl && scope == current_class_type))
5961 scope = complete_type_or_else (scope, NULL_TREE);
5962 /* Let the name lookup machinery know that we are processing a
5963 class member access expression. */
5964 parser->context->object_type = scope;
5965 /* If something went wrong, we want to be able to discern that case,
5966 as opposed to the case where there was no SCOPE due to the type
5967 of expression being dependent. */
5968 if (!scope)
5969 scope = error_mark_node;
5970 /* If the SCOPE was erroneous, make the various semantic analysis
5971 functions exit quickly -- and without issuing additional error
5972 messages. */
5973 if (scope == error_mark_node)
5974 postfix_expression = error_mark_node;
5977 /* Assume this expression is not a pseudo-destructor access. */
5978 pseudo_destructor_p = false;
5980 /* If the SCOPE is a scalar type, then, if this is a valid program,
5981 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5982 is type dependent, it can be pseudo-destructor-name or something else.
5983 Try to parse it as pseudo-destructor-name first. */
5984 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5986 tree s;
5987 tree type;
5989 cp_parser_parse_tentatively (parser);
5990 /* Parse the pseudo-destructor-name. */
5991 s = NULL_TREE;
5992 cp_parser_pseudo_destructor_name (parser, &s, &type);
5993 if (dependent_p
5994 && (cp_parser_error_occurred (parser)
5995 || TREE_CODE (type) != TYPE_DECL
5996 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5997 cp_parser_abort_tentative_parse (parser);
5998 else if (cp_parser_parse_definitely (parser))
6000 pseudo_destructor_p = true;
6001 postfix_expression
6002 = finish_pseudo_destructor_expr (postfix_expression,
6003 s, TREE_TYPE (type));
6007 if (!pseudo_destructor_p)
6009 /* If the SCOPE is not a scalar type, we are looking at an
6010 ordinary class member access expression, rather than a
6011 pseudo-destructor-name. */
6012 bool template_p;
6013 cp_token *token = cp_lexer_peek_token (parser->lexer);
6014 /* Parse the id-expression. */
6015 name = (cp_parser_id_expression
6016 (parser,
6017 cp_parser_optional_template_keyword (parser),
6018 /*check_dependency_p=*/true,
6019 &template_p,
6020 /*declarator_p=*/false,
6021 /*optional_p=*/false));
6022 /* In general, build a SCOPE_REF if the member name is qualified.
6023 However, if the name was not dependent and has already been
6024 resolved; there is no need to build the SCOPE_REF. For example;
6026 struct X { void f(); };
6027 template <typename T> void f(T* t) { t->X::f(); }
6029 Even though "t" is dependent, "X::f" is not and has been resolved
6030 to a BASELINK; there is no need to include scope information. */
6032 /* But we do need to remember that there was an explicit scope for
6033 virtual function calls. */
6034 if (parser->scope)
6035 *idk = CP_ID_KIND_QUALIFIED;
6037 /* If the name is a template-id that names a type, we will get a
6038 TYPE_DECL here. That is invalid code. */
6039 if (TREE_CODE (name) == TYPE_DECL)
6041 error_at (token->location, "invalid use of %qD", name);
6042 postfix_expression = error_mark_node;
6044 else
6046 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6048 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6050 error_at (token->location, "%<%D::%D%> is not a class member",
6051 parser->scope, name);
6052 postfix_expression = error_mark_node;
6054 else
6055 name = build_qualified_name (/*type=*/NULL_TREE,
6056 parser->scope,
6057 name,
6058 template_p);
6059 parser->scope = NULL_TREE;
6060 parser->qualifying_scope = NULL_TREE;
6061 parser->object_scope = NULL_TREE;
6063 if (parser->scope && name && BASELINK_P (name))
6064 adjust_result_of_qualified_name_lookup
6065 (name, parser->scope, scope);
6066 postfix_expression
6067 = finish_class_member_access_expr (postfix_expression, name,
6068 template_p,
6069 tf_warning_or_error);
6073 /* We no longer need to look up names in the scope of the object on
6074 the left-hand side of the `.' or `->' operator. */
6075 parser->context->object_type = NULL_TREE;
6077 /* Outside of offsetof, these operators may not appear in
6078 constant-expressions. */
6079 if (!for_offsetof
6080 && (cp_parser_non_integral_constant_expression
6081 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6082 postfix_expression = error_mark_node;
6084 return postfix_expression;
6087 /* Parse a parenthesized expression-list.
6089 expression-list:
6090 assignment-expression
6091 expression-list, assignment-expression
6093 attribute-list:
6094 expression-list
6095 identifier
6096 identifier, expression-list
6098 CAST_P is true if this expression is the target of a cast.
6100 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6101 argument pack.
6103 Returns a vector of trees. Each element is a representation of an
6104 assignment-expression. NULL is returned if the ( and or ) are
6105 missing. An empty, but allocated, vector is returned on no
6106 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6107 if we are parsing an attribute list for an attribute that wants a
6108 plain identifier argument, normal_attr for an attribute that wants
6109 an expression, or non_attr if we aren't parsing an attribute list. If
6110 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6111 not all of the expressions in the list were constant. */
6113 static VEC(tree,gc) *
6114 cp_parser_parenthesized_expression_list (cp_parser* parser,
6115 int is_attribute_list,
6116 bool cast_p,
6117 bool allow_expansion_p,
6118 bool *non_constant_p)
6120 VEC(tree,gc) *expression_list;
6121 bool fold_expr_p = is_attribute_list != non_attr;
6122 tree identifier = NULL_TREE;
6123 bool saved_greater_than_is_operator_p;
6125 /* Assume all the expressions will be constant. */
6126 if (non_constant_p)
6127 *non_constant_p = false;
6129 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6130 return NULL;
6132 expression_list = make_tree_vector ();
6134 /* Within a parenthesized expression, a `>' token is always
6135 the greater-than operator. */
6136 saved_greater_than_is_operator_p
6137 = parser->greater_than_is_operator_p;
6138 parser->greater_than_is_operator_p = true;
6140 /* Consume expressions until there are no more. */
6141 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6142 while (true)
6144 tree expr;
6146 /* At the beginning of attribute lists, check to see if the
6147 next token is an identifier. */
6148 if (is_attribute_list == id_attr
6149 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6151 cp_token *token;
6153 /* Consume the identifier. */
6154 token = cp_lexer_consume_token (parser->lexer);
6155 /* Save the identifier. */
6156 identifier = token->u.value;
6158 else
6160 bool expr_non_constant_p;
6162 /* Parse the next assignment-expression. */
6163 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6165 /* A braced-init-list. */
6166 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6167 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6168 if (non_constant_p && expr_non_constant_p)
6169 *non_constant_p = true;
6171 else if (non_constant_p)
6173 expr = (cp_parser_constant_expression
6174 (parser, /*allow_non_constant_p=*/true,
6175 &expr_non_constant_p));
6176 if (expr_non_constant_p)
6177 *non_constant_p = true;
6179 else
6180 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6182 if (fold_expr_p)
6183 expr = fold_non_dependent_expr (expr);
6185 /* If we have an ellipsis, then this is an expression
6186 expansion. */
6187 if (allow_expansion_p
6188 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6190 /* Consume the `...'. */
6191 cp_lexer_consume_token (parser->lexer);
6193 /* Build the argument pack. */
6194 expr = make_pack_expansion (expr);
6197 /* Add it to the list. We add error_mark_node
6198 expressions to the list, so that we can still tell if
6199 the correct form for a parenthesized expression-list
6200 is found. That gives better errors. */
6201 VEC_safe_push (tree, gc, expression_list, expr);
6203 if (expr == error_mark_node)
6204 goto skip_comma;
6207 /* After the first item, attribute lists look the same as
6208 expression lists. */
6209 is_attribute_list = non_attr;
6211 get_comma:;
6212 /* If the next token isn't a `,', then we are done. */
6213 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6214 break;
6216 /* Otherwise, consume the `,' and keep going. */
6217 cp_lexer_consume_token (parser->lexer);
6220 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6222 int ending;
6224 skip_comma:;
6225 /* We try and resync to an unnested comma, as that will give the
6226 user better diagnostics. */
6227 ending = cp_parser_skip_to_closing_parenthesis (parser,
6228 /*recovering=*/true,
6229 /*or_comma=*/true,
6230 /*consume_paren=*/true);
6231 if (ending < 0)
6232 goto get_comma;
6233 if (!ending)
6235 parser->greater_than_is_operator_p
6236 = saved_greater_than_is_operator_p;
6237 return NULL;
6241 parser->greater_than_is_operator_p
6242 = saved_greater_than_is_operator_p;
6244 if (identifier)
6245 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6247 return expression_list;
6250 /* Parse a pseudo-destructor-name.
6252 pseudo-destructor-name:
6253 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6254 :: [opt] nested-name-specifier template template-id :: ~ type-name
6255 :: [opt] nested-name-specifier [opt] ~ type-name
6257 If either of the first two productions is used, sets *SCOPE to the
6258 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6259 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6260 or ERROR_MARK_NODE if the parse fails. */
6262 static void
6263 cp_parser_pseudo_destructor_name (cp_parser* parser,
6264 tree* scope,
6265 tree* type)
6267 bool nested_name_specifier_p;
6269 /* Assume that things will not work out. */
6270 *type = error_mark_node;
6272 /* Look for the optional `::' operator. */
6273 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6274 /* Look for the optional nested-name-specifier. */
6275 nested_name_specifier_p
6276 = (cp_parser_nested_name_specifier_opt (parser,
6277 /*typename_keyword_p=*/false,
6278 /*check_dependency_p=*/true,
6279 /*type_p=*/false,
6280 /*is_declaration=*/false)
6281 != NULL_TREE);
6282 /* Now, if we saw a nested-name-specifier, we might be doing the
6283 second production. */
6284 if (nested_name_specifier_p
6285 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6287 /* Consume the `template' keyword. */
6288 cp_lexer_consume_token (parser->lexer);
6289 /* Parse the template-id. */
6290 cp_parser_template_id (parser,
6291 /*template_keyword_p=*/true,
6292 /*check_dependency_p=*/false,
6293 class_type,
6294 /*is_declaration=*/true);
6295 /* Look for the `::' token. */
6296 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6298 /* If the next token is not a `~', then there might be some
6299 additional qualification. */
6300 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6302 /* At this point, we're looking for "type-name :: ~". The type-name
6303 must not be a class-name, since this is a pseudo-destructor. So,
6304 it must be either an enum-name, or a typedef-name -- both of which
6305 are just identifiers. So, we peek ahead to check that the "::"
6306 and "~" tokens are present; if they are not, then we can avoid
6307 calling type_name. */
6308 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6309 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6310 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6312 cp_parser_error (parser, "non-scalar type");
6313 return;
6316 /* Look for the type-name. */
6317 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6318 if (*scope == error_mark_node)
6319 return;
6321 /* Look for the `::' token. */
6322 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6324 else
6325 *scope = NULL_TREE;
6327 /* Look for the `~'. */
6328 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6330 /* Once we see the ~, this has to be a pseudo-destructor. */
6331 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6332 cp_parser_commit_to_tentative_parse (parser);
6334 /* Look for the type-name again. We are not responsible for
6335 checking that it matches the first type-name. */
6336 *type = cp_parser_nonclass_name (parser);
6339 /* Parse a unary-expression.
6341 unary-expression:
6342 postfix-expression
6343 ++ cast-expression
6344 -- cast-expression
6345 unary-operator cast-expression
6346 sizeof unary-expression
6347 sizeof ( type-id )
6348 alignof ( type-id ) [C++0x]
6349 new-expression
6350 delete-expression
6352 GNU Extensions:
6354 unary-expression:
6355 __extension__ cast-expression
6356 __alignof__ unary-expression
6357 __alignof__ ( type-id )
6358 alignof unary-expression [C++0x]
6359 __real__ cast-expression
6360 __imag__ cast-expression
6361 && identifier
6363 ADDRESS_P is true iff the unary-expression is appearing as the
6364 operand of the `&' operator. CAST_P is true if this expression is
6365 the target of a cast.
6367 Returns a representation of the expression. */
6369 static tree
6370 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6371 cp_id_kind * pidk)
6373 cp_token *token;
6374 enum tree_code unary_operator;
6376 /* Peek at the next token. */
6377 token = cp_lexer_peek_token (parser->lexer);
6378 /* Some keywords give away the kind of expression. */
6379 if (token->type == CPP_KEYWORD)
6381 enum rid keyword = token->keyword;
6383 switch (keyword)
6385 case RID_ALIGNOF:
6386 case RID_SIZEOF:
6388 tree operand;
6389 enum tree_code op;
6391 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6392 /* Consume the token. */
6393 cp_lexer_consume_token (parser->lexer);
6394 /* Parse the operand. */
6395 operand = cp_parser_sizeof_operand (parser, keyword);
6397 if (TYPE_P (operand))
6398 return cxx_sizeof_or_alignof_type (operand, op, true);
6399 else
6401 /* ISO C++ defines alignof only with types, not with
6402 expressions. So pedwarn if alignof is used with a non-
6403 type expression. However, __alignof__ is ok. */
6404 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6405 pedwarn (token->location, OPT_Wpedantic,
6406 "ISO C++ does not allow %<alignof%> "
6407 "with a non-type");
6409 return cxx_sizeof_or_alignof_expr (operand, op, true);
6413 case RID_NEW:
6414 return cp_parser_new_expression (parser);
6416 case RID_DELETE:
6417 return cp_parser_delete_expression (parser);
6419 case RID_EXTENSION:
6421 /* The saved value of the PEDANTIC flag. */
6422 int saved_pedantic;
6423 tree expr;
6425 /* Save away the PEDANTIC flag. */
6426 cp_parser_extension_opt (parser, &saved_pedantic);
6427 /* Parse the cast-expression. */
6428 expr = cp_parser_simple_cast_expression (parser);
6429 /* Restore the PEDANTIC flag. */
6430 pedantic = saved_pedantic;
6432 return expr;
6435 case RID_REALPART:
6436 case RID_IMAGPART:
6438 tree expression;
6440 /* Consume the `__real__' or `__imag__' token. */
6441 cp_lexer_consume_token (parser->lexer);
6442 /* Parse the cast-expression. */
6443 expression = cp_parser_simple_cast_expression (parser);
6444 /* Create the complete representation. */
6445 return build_x_unary_op (token->location,
6446 (keyword == RID_REALPART
6447 ? REALPART_EXPR : IMAGPART_EXPR),
6448 expression,
6449 tf_warning_or_error);
6451 break;
6453 case RID_TRANSACTION_ATOMIC:
6454 case RID_TRANSACTION_RELAXED:
6455 return cp_parser_transaction_expression (parser, keyword);
6457 case RID_NOEXCEPT:
6459 tree expr;
6460 const char *saved_message;
6461 bool saved_integral_constant_expression_p;
6462 bool saved_non_integral_constant_expression_p;
6463 bool saved_greater_than_is_operator_p;
6465 cp_lexer_consume_token (parser->lexer);
6466 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6468 saved_message = parser->type_definition_forbidden_message;
6469 parser->type_definition_forbidden_message
6470 = G_("types may not be defined in %<noexcept%> expressions");
6472 saved_integral_constant_expression_p
6473 = parser->integral_constant_expression_p;
6474 saved_non_integral_constant_expression_p
6475 = parser->non_integral_constant_expression_p;
6476 parser->integral_constant_expression_p = false;
6478 saved_greater_than_is_operator_p
6479 = parser->greater_than_is_operator_p;
6480 parser->greater_than_is_operator_p = true;
6482 ++cp_unevaluated_operand;
6483 ++c_inhibit_evaluation_warnings;
6484 expr = cp_parser_expression (parser, false, NULL);
6485 --c_inhibit_evaluation_warnings;
6486 --cp_unevaluated_operand;
6488 parser->greater_than_is_operator_p
6489 = saved_greater_than_is_operator_p;
6491 parser->integral_constant_expression_p
6492 = saved_integral_constant_expression_p;
6493 parser->non_integral_constant_expression_p
6494 = saved_non_integral_constant_expression_p;
6496 parser->type_definition_forbidden_message = saved_message;
6498 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6499 return finish_noexcept_expr (expr, tf_warning_or_error);
6502 default:
6503 break;
6507 /* Look for the `:: new' and `:: delete', which also signal the
6508 beginning of a new-expression, or delete-expression,
6509 respectively. If the next token is `::', then it might be one of
6510 these. */
6511 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6513 enum rid keyword;
6515 /* See if the token after the `::' is one of the keywords in
6516 which we're interested. */
6517 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6518 /* If it's `new', we have a new-expression. */
6519 if (keyword == RID_NEW)
6520 return cp_parser_new_expression (parser);
6521 /* Similarly, for `delete'. */
6522 else if (keyword == RID_DELETE)
6523 return cp_parser_delete_expression (parser);
6526 /* Look for a unary operator. */
6527 unary_operator = cp_parser_unary_operator (token);
6528 /* The `++' and `--' operators can be handled similarly, even though
6529 they are not technically unary-operators in the grammar. */
6530 if (unary_operator == ERROR_MARK)
6532 if (token->type == CPP_PLUS_PLUS)
6533 unary_operator = PREINCREMENT_EXPR;
6534 else if (token->type == CPP_MINUS_MINUS)
6535 unary_operator = PREDECREMENT_EXPR;
6536 /* Handle the GNU address-of-label extension. */
6537 else if (cp_parser_allow_gnu_extensions_p (parser)
6538 && token->type == CPP_AND_AND)
6540 tree identifier;
6541 tree expression;
6542 location_t loc = token->location;
6544 /* Consume the '&&' token. */
6545 cp_lexer_consume_token (parser->lexer);
6546 /* Look for the identifier. */
6547 identifier = cp_parser_identifier (parser);
6548 /* Create an expression representing the address. */
6549 expression = finish_label_address_expr (identifier, loc);
6550 if (cp_parser_non_integral_constant_expression (parser,
6551 NIC_ADDR_LABEL))
6552 expression = error_mark_node;
6553 return expression;
6556 if (unary_operator != ERROR_MARK)
6558 tree cast_expression;
6559 tree expression = error_mark_node;
6560 non_integral_constant non_constant_p = NIC_NONE;
6561 location_t loc = token->location;
6563 /* Consume the operator token. */
6564 token = cp_lexer_consume_token (parser->lexer);
6565 /* Parse the cast-expression. */
6566 cast_expression
6567 = cp_parser_cast_expression (parser,
6568 unary_operator == ADDR_EXPR,
6569 /*cast_p=*/false, pidk);
6570 /* Now, build an appropriate representation. */
6571 switch (unary_operator)
6573 case INDIRECT_REF:
6574 non_constant_p = NIC_STAR;
6575 expression = build_x_indirect_ref (loc, cast_expression,
6576 RO_UNARY_STAR,
6577 tf_warning_or_error);
6578 break;
6580 case ADDR_EXPR:
6581 non_constant_p = NIC_ADDR;
6582 /* Fall through. */
6583 case BIT_NOT_EXPR:
6584 expression = build_x_unary_op (loc, unary_operator,
6585 cast_expression,
6586 tf_warning_or_error);
6587 break;
6589 case PREINCREMENT_EXPR:
6590 case PREDECREMENT_EXPR:
6591 non_constant_p = unary_operator == PREINCREMENT_EXPR
6592 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6593 /* Fall through. */
6594 case UNARY_PLUS_EXPR:
6595 case NEGATE_EXPR:
6596 case TRUTH_NOT_EXPR:
6597 expression = finish_unary_op_expr (loc, unary_operator,
6598 cast_expression);
6599 break;
6601 default:
6602 gcc_unreachable ();
6605 if (non_constant_p != NIC_NONE
6606 && cp_parser_non_integral_constant_expression (parser,
6607 non_constant_p))
6608 expression = error_mark_node;
6610 return expression;
6613 return cp_parser_postfix_expression (parser, address_p, cast_p,
6614 /*member_access_only_p=*/false,
6615 pidk);
6618 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6619 unary-operator, the corresponding tree code is returned. */
6621 static enum tree_code
6622 cp_parser_unary_operator (cp_token* token)
6624 switch (token->type)
6626 case CPP_MULT:
6627 return INDIRECT_REF;
6629 case CPP_AND:
6630 return ADDR_EXPR;
6632 case CPP_PLUS:
6633 return UNARY_PLUS_EXPR;
6635 case CPP_MINUS:
6636 return NEGATE_EXPR;
6638 case CPP_NOT:
6639 return TRUTH_NOT_EXPR;
6641 case CPP_COMPL:
6642 return BIT_NOT_EXPR;
6644 default:
6645 return ERROR_MARK;
6649 /* Parse a new-expression.
6651 new-expression:
6652 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6653 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6655 Returns a representation of the expression. */
6657 static tree
6658 cp_parser_new_expression (cp_parser* parser)
6660 bool global_scope_p;
6661 VEC(tree,gc) *placement;
6662 tree type;
6663 VEC(tree,gc) *initializer;
6664 tree nelts = NULL_TREE;
6665 tree ret;
6667 /* Look for the optional `::' operator. */
6668 global_scope_p
6669 = (cp_parser_global_scope_opt (parser,
6670 /*current_scope_valid_p=*/false)
6671 != NULL_TREE);
6672 /* Look for the `new' operator. */
6673 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6674 /* There's no easy way to tell a new-placement from the
6675 `( type-id )' construct. */
6676 cp_parser_parse_tentatively (parser);
6677 /* Look for a new-placement. */
6678 placement = cp_parser_new_placement (parser);
6679 /* If that didn't work out, there's no new-placement. */
6680 if (!cp_parser_parse_definitely (parser))
6682 if (placement != NULL)
6683 release_tree_vector (placement);
6684 placement = NULL;
6687 /* If the next token is a `(', then we have a parenthesized
6688 type-id. */
6689 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6691 cp_token *token;
6692 const char *saved_message = parser->type_definition_forbidden_message;
6694 /* Consume the `('. */
6695 cp_lexer_consume_token (parser->lexer);
6697 /* Parse the type-id. */
6698 parser->type_definition_forbidden_message
6699 = G_("types may not be defined in a new-expression");
6700 type = cp_parser_type_id (parser);
6701 parser->type_definition_forbidden_message = saved_message;
6703 /* Look for the closing `)'. */
6704 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6705 token = cp_lexer_peek_token (parser->lexer);
6706 /* There should not be a direct-new-declarator in this production,
6707 but GCC used to allowed this, so we check and emit a sensible error
6708 message for this case. */
6709 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6711 error_at (token->location,
6712 "array bound forbidden after parenthesized type-id");
6713 inform (token->location,
6714 "try removing the parentheses around the type-id");
6715 cp_parser_direct_new_declarator (parser);
6718 /* Otherwise, there must be a new-type-id. */
6719 else
6720 type = cp_parser_new_type_id (parser, &nelts);
6722 /* If the next token is a `(' or '{', then we have a new-initializer. */
6723 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6724 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6725 initializer = cp_parser_new_initializer (parser);
6726 else
6727 initializer = NULL;
6729 /* A new-expression may not appear in an integral constant
6730 expression. */
6731 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6732 ret = error_mark_node;
6733 else
6735 /* Create a representation of the new-expression. */
6736 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6737 tf_warning_or_error);
6740 if (placement != NULL)
6741 release_tree_vector (placement);
6742 if (initializer != NULL)
6743 release_tree_vector (initializer);
6745 return ret;
6748 /* Parse a new-placement.
6750 new-placement:
6751 ( expression-list )
6753 Returns the same representation as for an expression-list. */
6755 static VEC(tree,gc) *
6756 cp_parser_new_placement (cp_parser* parser)
6758 VEC(tree,gc) *expression_list;
6760 /* Parse the expression-list. */
6761 expression_list = (cp_parser_parenthesized_expression_list
6762 (parser, non_attr, /*cast_p=*/false,
6763 /*allow_expansion_p=*/true,
6764 /*non_constant_p=*/NULL));
6766 return expression_list;
6769 /* Parse a new-type-id.
6771 new-type-id:
6772 type-specifier-seq new-declarator [opt]
6774 Returns the TYPE allocated. If the new-type-id indicates an array
6775 type, *NELTS is set to the number of elements in the last array
6776 bound; the TYPE will not include the last array bound. */
6778 static tree
6779 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6781 cp_decl_specifier_seq type_specifier_seq;
6782 cp_declarator *new_declarator;
6783 cp_declarator *declarator;
6784 cp_declarator *outer_declarator;
6785 const char *saved_message;
6787 /* The type-specifier sequence must not contain type definitions.
6788 (It cannot contain declarations of new types either, but if they
6789 are not definitions we will catch that because they are not
6790 complete.) */
6791 saved_message = parser->type_definition_forbidden_message;
6792 parser->type_definition_forbidden_message
6793 = G_("types may not be defined in a new-type-id");
6794 /* Parse the type-specifier-seq. */
6795 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6796 /*is_trailing_return=*/false,
6797 &type_specifier_seq);
6798 /* Restore the old message. */
6799 parser->type_definition_forbidden_message = saved_message;
6801 if (type_specifier_seq.type == error_mark_node)
6802 return error_mark_node;
6804 /* Parse the new-declarator. */
6805 new_declarator = cp_parser_new_declarator_opt (parser);
6807 /* Determine the number of elements in the last array dimension, if
6808 any. */
6809 *nelts = NULL_TREE;
6810 /* Skip down to the last array dimension. */
6811 declarator = new_declarator;
6812 outer_declarator = NULL;
6813 while (declarator && (declarator->kind == cdk_pointer
6814 || declarator->kind == cdk_ptrmem))
6816 outer_declarator = declarator;
6817 declarator = declarator->declarator;
6819 while (declarator
6820 && declarator->kind == cdk_array
6821 && declarator->declarator
6822 && declarator->declarator->kind == cdk_array)
6824 outer_declarator = declarator;
6825 declarator = declarator->declarator;
6828 if (declarator && declarator->kind == cdk_array)
6830 *nelts = declarator->u.array.bounds;
6831 if (*nelts == error_mark_node)
6832 *nelts = integer_one_node;
6834 if (outer_declarator)
6835 outer_declarator->declarator = declarator->declarator;
6836 else
6837 new_declarator = NULL;
6840 return groktypename (&type_specifier_seq, new_declarator, false);
6843 /* Parse an (optional) new-declarator.
6845 new-declarator:
6846 ptr-operator new-declarator [opt]
6847 direct-new-declarator
6849 Returns the declarator. */
6851 static cp_declarator *
6852 cp_parser_new_declarator_opt (cp_parser* parser)
6854 enum tree_code code;
6855 tree type;
6856 cp_cv_quals cv_quals;
6858 /* We don't know if there's a ptr-operator next, or not. */
6859 cp_parser_parse_tentatively (parser);
6860 /* Look for a ptr-operator. */
6861 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6862 /* If that worked, look for more new-declarators. */
6863 if (cp_parser_parse_definitely (parser))
6865 cp_declarator *declarator;
6867 /* Parse another optional declarator. */
6868 declarator = cp_parser_new_declarator_opt (parser);
6870 return cp_parser_make_indirect_declarator
6871 (code, type, cv_quals, declarator);
6874 /* If the next token is a `[', there is a direct-new-declarator. */
6875 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6876 return cp_parser_direct_new_declarator (parser);
6878 return NULL;
6881 /* Parse a direct-new-declarator.
6883 direct-new-declarator:
6884 [ expression ]
6885 direct-new-declarator [constant-expression]
6889 static cp_declarator *
6890 cp_parser_direct_new_declarator (cp_parser* parser)
6892 cp_declarator *declarator = NULL;
6894 while (true)
6896 tree expression;
6897 cp_token *token;
6899 /* Look for the opening `['. */
6900 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6902 token = cp_lexer_peek_token (parser->lexer);
6903 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6904 /* The standard requires that the expression have integral
6905 type. DR 74 adds enumeration types. We believe that the
6906 real intent is that these expressions be handled like the
6907 expression in a `switch' condition, which also allows
6908 classes with a single conversion to integral or
6909 enumeration type. */
6910 if (!processing_template_decl)
6912 expression
6913 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6914 expression,
6915 /*complain=*/true);
6916 if (!expression)
6918 error_at (token->location,
6919 "expression in new-declarator must have integral "
6920 "or enumeration type");
6921 expression = error_mark_node;
6925 /* Look for the closing `]'. */
6926 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6928 /* Add this bound to the declarator. */
6929 declarator = make_array_declarator (declarator, expression);
6931 /* If the next token is not a `[', then there are no more
6932 bounds. */
6933 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6934 break;
6937 return declarator;
6940 /* Parse a new-initializer.
6942 new-initializer:
6943 ( expression-list [opt] )
6944 braced-init-list
6946 Returns a representation of the expression-list. */
6948 static VEC(tree,gc) *
6949 cp_parser_new_initializer (cp_parser* parser)
6951 VEC(tree,gc) *expression_list;
6953 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6955 tree t;
6956 bool expr_non_constant_p;
6957 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6958 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6959 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6960 expression_list = make_tree_vector_single (t);
6962 else
6963 expression_list = (cp_parser_parenthesized_expression_list
6964 (parser, non_attr, /*cast_p=*/false,
6965 /*allow_expansion_p=*/true,
6966 /*non_constant_p=*/NULL));
6968 return expression_list;
6971 /* Parse a delete-expression.
6973 delete-expression:
6974 :: [opt] delete cast-expression
6975 :: [opt] delete [ ] cast-expression
6977 Returns a representation of the expression. */
6979 static tree
6980 cp_parser_delete_expression (cp_parser* parser)
6982 bool global_scope_p;
6983 bool array_p;
6984 tree expression;
6986 /* Look for the optional `::' operator. */
6987 global_scope_p
6988 = (cp_parser_global_scope_opt (parser,
6989 /*current_scope_valid_p=*/false)
6990 != NULL_TREE);
6991 /* Look for the `delete' keyword. */
6992 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6993 /* See if the array syntax is in use. */
6994 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6996 /* Consume the `[' token. */
6997 cp_lexer_consume_token (parser->lexer);
6998 /* Look for the `]' token. */
6999 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7000 /* Remember that this is the `[]' construct. */
7001 array_p = true;
7003 else
7004 array_p = false;
7006 /* Parse the cast-expression. */
7007 expression = cp_parser_simple_cast_expression (parser);
7009 /* A delete-expression may not appear in an integral constant
7010 expression. */
7011 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7012 return error_mark_node;
7014 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7015 tf_warning_or_error);
7018 /* Returns true if TOKEN may start a cast-expression and false
7019 otherwise. */
7021 static bool
7022 cp_parser_token_starts_cast_expression (cp_token *token)
7024 switch (token->type)
7026 case CPP_COMMA:
7027 case CPP_SEMICOLON:
7028 case CPP_QUERY:
7029 case CPP_COLON:
7030 case CPP_CLOSE_SQUARE:
7031 case CPP_CLOSE_PAREN:
7032 case CPP_CLOSE_BRACE:
7033 case CPP_DOT:
7034 case CPP_DOT_STAR:
7035 case CPP_DEREF:
7036 case CPP_DEREF_STAR:
7037 case CPP_DIV:
7038 case CPP_MOD:
7039 case CPP_LSHIFT:
7040 case CPP_RSHIFT:
7041 case CPP_LESS:
7042 case CPP_GREATER:
7043 case CPP_LESS_EQ:
7044 case CPP_GREATER_EQ:
7045 case CPP_EQ_EQ:
7046 case CPP_NOT_EQ:
7047 case CPP_EQ:
7048 case CPP_MULT_EQ:
7049 case CPP_DIV_EQ:
7050 case CPP_MOD_EQ:
7051 case CPP_PLUS_EQ:
7052 case CPP_MINUS_EQ:
7053 case CPP_RSHIFT_EQ:
7054 case CPP_LSHIFT_EQ:
7055 case CPP_AND_EQ:
7056 case CPP_XOR_EQ:
7057 case CPP_OR_EQ:
7058 case CPP_XOR:
7059 case CPP_OR:
7060 case CPP_OR_OR:
7061 case CPP_EOF:
7062 return false;
7064 /* '[' may start a primary-expression in obj-c++. */
7065 case CPP_OPEN_SQUARE:
7066 return c_dialect_objc ();
7068 default:
7069 return true;
7073 /* Parse a cast-expression.
7075 cast-expression:
7076 unary-expression
7077 ( type-id ) cast-expression
7079 ADDRESS_P is true iff the unary-expression is appearing as the
7080 operand of the `&' operator. CAST_P is true if this expression is
7081 the target of a cast.
7083 Returns a representation of the expression. */
7085 static tree
7086 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7087 cp_id_kind * pidk)
7089 /* If it's a `(', then we might be looking at a cast. */
7090 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7092 tree type = NULL_TREE;
7093 tree expr = NULL_TREE;
7094 bool compound_literal_p;
7095 const char *saved_message;
7097 /* There's no way to know yet whether or not this is a cast.
7098 For example, `(int (3))' is a unary-expression, while `(int)
7099 3' is a cast. So, we resort to parsing tentatively. */
7100 cp_parser_parse_tentatively (parser);
7101 /* Types may not be defined in a cast. */
7102 saved_message = parser->type_definition_forbidden_message;
7103 parser->type_definition_forbidden_message
7104 = G_("types may not be defined in casts");
7105 /* Consume the `('. */
7106 cp_lexer_consume_token (parser->lexer);
7107 /* A very tricky bit is that `(struct S) { 3 }' is a
7108 compound-literal (which we permit in C++ as an extension).
7109 But, that construct is not a cast-expression -- it is a
7110 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7111 is legal; if the compound-literal were a cast-expression,
7112 you'd need an extra set of parentheses.) But, if we parse
7113 the type-id, and it happens to be a class-specifier, then we
7114 will commit to the parse at that point, because we cannot
7115 undo the action that is done when creating a new class. So,
7116 then we cannot back up and do a postfix-expression.
7118 Therefore, we scan ahead to the closing `)', and check to see
7119 if the token after the `)' is a `{'. If so, we are not
7120 looking at a cast-expression.
7122 Save tokens so that we can put them back. */
7123 cp_lexer_save_tokens (parser->lexer);
7124 /* Skip tokens until the next token is a closing parenthesis.
7125 If we find the closing `)', and the next token is a `{', then
7126 we are looking at a compound-literal. */
7127 compound_literal_p
7128 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7129 /*consume_paren=*/true)
7130 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7131 /* Roll back the tokens we skipped. */
7132 cp_lexer_rollback_tokens (parser->lexer);
7133 /* If we were looking at a compound-literal, simulate an error
7134 so that the call to cp_parser_parse_definitely below will
7135 fail. */
7136 if (compound_literal_p)
7137 cp_parser_simulate_error (parser);
7138 else
7140 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7141 parser->in_type_id_in_expr_p = true;
7142 /* Look for the type-id. */
7143 type = cp_parser_type_id (parser);
7144 /* Look for the closing `)'. */
7145 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7146 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7149 /* Restore the saved message. */
7150 parser->type_definition_forbidden_message = saved_message;
7152 /* At this point this can only be either a cast or a
7153 parenthesized ctor such as `(T ())' that looks like a cast to
7154 function returning T. */
7155 if (!cp_parser_error_occurred (parser)
7156 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7157 (parser->lexer)))
7159 cp_parser_parse_definitely (parser);
7160 expr = cp_parser_cast_expression (parser,
7161 /*address_p=*/false,
7162 /*cast_p=*/true, pidk);
7164 /* Warn about old-style casts, if so requested. */
7165 if (warn_old_style_cast
7166 && !in_system_header
7167 && !VOID_TYPE_P (type)
7168 && current_lang_name != lang_name_c)
7169 warning (OPT_Wold_style_cast, "use of old-style cast");
7171 /* Only type conversions to integral or enumeration types
7172 can be used in constant-expressions. */
7173 if (!cast_valid_in_integral_constant_expression_p (type)
7174 && cp_parser_non_integral_constant_expression (parser,
7175 NIC_CAST))
7176 return error_mark_node;
7178 /* Perform the cast. */
7179 expr = build_c_cast (input_location, type, expr);
7180 return expr;
7182 else
7183 cp_parser_abort_tentative_parse (parser);
7186 /* If we get here, then it's not a cast, so it must be a
7187 unary-expression. */
7188 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7191 /* Parse a binary expression of the general form:
7193 pm-expression:
7194 cast-expression
7195 pm-expression .* cast-expression
7196 pm-expression ->* cast-expression
7198 multiplicative-expression:
7199 pm-expression
7200 multiplicative-expression * pm-expression
7201 multiplicative-expression / pm-expression
7202 multiplicative-expression % pm-expression
7204 additive-expression:
7205 multiplicative-expression
7206 additive-expression + multiplicative-expression
7207 additive-expression - multiplicative-expression
7209 shift-expression:
7210 additive-expression
7211 shift-expression << additive-expression
7212 shift-expression >> additive-expression
7214 relational-expression:
7215 shift-expression
7216 relational-expression < shift-expression
7217 relational-expression > shift-expression
7218 relational-expression <= shift-expression
7219 relational-expression >= shift-expression
7221 GNU Extension:
7223 relational-expression:
7224 relational-expression <? shift-expression
7225 relational-expression >? shift-expression
7227 equality-expression:
7228 relational-expression
7229 equality-expression == relational-expression
7230 equality-expression != relational-expression
7232 and-expression:
7233 equality-expression
7234 and-expression & equality-expression
7236 exclusive-or-expression:
7237 and-expression
7238 exclusive-or-expression ^ and-expression
7240 inclusive-or-expression:
7241 exclusive-or-expression
7242 inclusive-or-expression | exclusive-or-expression
7244 logical-and-expression:
7245 inclusive-or-expression
7246 logical-and-expression && inclusive-or-expression
7248 logical-or-expression:
7249 logical-and-expression
7250 logical-or-expression || logical-and-expression
7252 All these are implemented with a single function like:
7254 binary-expression:
7255 simple-cast-expression
7256 binary-expression <token> binary-expression
7258 CAST_P is true if this expression is the target of a cast.
7260 The binops_by_token map is used to get the tree codes for each <token> type.
7261 binary-expressions are associated according to a precedence table. */
7263 #define TOKEN_PRECEDENCE(token) \
7264 (((token->type == CPP_GREATER \
7265 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7266 && !parser->greater_than_is_operator_p) \
7267 ? PREC_NOT_OPERATOR \
7268 : binops_by_token[token->type].prec)
7270 static tree
7271 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7272 bool no_toplevel_fold_p,
7273 enum cp_parser_prec prec,
7274 cp_id_kind * pidk)
7276 cp_parser_expression_stack stack;
7277 cp_parser_expression_stack_entry *sp = &stack[0];
7278 cp_parser_expression_stack_entry current;
7279 tree rhs;
7280 cp_token *token;
7281 enum tree_code rhs_type;
7282 enum cp_parser_prec new_prec, lookahead_prec;
7283 tree overload;
7285 /* Parse the first expression. */
7286 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7287 cast_p, pidk);
7288 current.lhs_type = ERROR_MARK;
7289 current.prec = prec;
7291 if (cp_parser_error_occurred (parser))
7292 return error_mark_node;
7294 for (;;)
7296 /* Get an operator token. */
7297 token = cp_lexer_peek_token (parser->lexer);
7299 if (warn_cxx0x_compat
7300 && token->type == CPP_RSHIFT
7301 && !parser->greater_than_is_operator_p)
7303 if (warning_at (token->location, OPT_Wc__0x_compat,
7304 "%<>>%> operator is treated"
7305 " as two right angle brackets in C++11"))
7306 inform (token->location,
7307 "suggest parentheses around %<>>%> expression");
7310 new_prec = TOKEN_PRECEDENCE (token);
7312 /* Popping an entry off the stack means we completed a subexpression:
7313 - either we found a token which is not an operator (`>' where it is not
7314 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7315 will happen repeatedly;
7316 - or, we found an operator which has lower priority. This is the case
7317 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7318 parsing `3 * 4'. */
7319 if (new_prec <= current.prec)
7321 if (sp == stack)
7322 break;
7323 else
7324 goto pop;
7327 get_rhs:
7328 current.tree_type = binops_by_token[token->type].tree_type;
7329 current.loc = token->location;
7331 /* We used the operator token. */
7332 cp_lexer_consume_token (parser->lexer);
7334 /* For "false && x" or "true || x", x will never be executed;
7335 disable warnings while evaluating it. */
7336 if (current.tree_type == TRUTH_ANDIF_EXPR)
7337 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7338 else if (current.tree_type == TRUTH_ORIF_EXPR)
7339 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7341 /* Extract another operand. It may be the RHS of this expression
7342 or the LHS of a new, higher priority expression. */
7343 rhs = cp_parser_simple_cast_expression (parser);
7344 rhs_type = ERROR_MARK;
7346 /* Get another operator token. Look up its precedence to avoid
7347 building a useless (immediately popped) stack entry for common
7348 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7349 token = cp_lexer_peek_token (parser->lexer);
7350 lookahead_prec = TOKEN_PRECEDENCE (token);
7351 if (lookahead_prec > new_prec)
7353 /* ... and prepare to parse the RHS of the new, higher priority
7354 expression. Since precedence levels on the stack are
7355 monotonically increasing, we do not have to care about
7356 stack overflows. */
7357 *sp = current;
7358 ++sp;
7359 current.lhs = rhs;
7360 current.lhs_type = rhs_type;
7361 current.prec = new_prec;
7362 new_prec = lookahead_prec;
7363 goto get_rhs;
7365 pop:
7366 lookahead_prec = new_prec;
7367 /* If the stack is not empty, we have parsed into LHS the right side
7368 (`4' in the example above) of an expression we had suspended.
7369 We can use the information on the stack to recover the LHS (`3')
7370 from the stack together with the tree code (`MULT_EXPR'), and
7371 the precedence of the higher level subexpression
7372 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7373 which will be used to actually build the additive expression. */
7374 rhs = current.lhs;
7375 rhs_type = current.lhs_type;
7376 --sp;
7377 current = *sp;
7380 /* Undo the disabling of warnings done above. */
7381 if (current.tree_type == TRUTH_ANDIF_EXPR)
7382 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7383 else if (current.tree_type == TRUTH_ORIF_EXPR)
7384 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7386 overload = NULL;
7387 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7388 ERROR_MARK for everything that is not a binary expression.
7389 This makes warn_about_parentheses miss some warnings that
7390 involve unary operators. For unary expressions we should
7391 pass the correct tree_code unless the unary expression was
7392 surrounded by parentheses.
7394 if (no_toplevel_fold_p
7395 && lookahead_prec <= current.prec
7396 && sp == stack
7397 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7398 current.lhs = build2 (current.tree_type, boolean_type_node,
7399 current.lhs, rhs);
7400 else
7401 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7402 current.lhs, current.lhs_type,
7403 rhs, rhs_type, &overload,
7404 tf_warning_or_error);
7405 current.lhs_type = current.tree_type;
7407 /* If the binary operator required the use of an overloaded operator,
7408 then this expression cannot be an integral constant-expression.
7409 An overloaded operator can be used even if both operands are
7410 otherwise permissible in an integral constant-expression if at
7411 least one of the operands is of enumeration type. */
7413 if (overload
7414 && cp_parser_non_integral_constant_expression (parser,
7415 NIC_OVERLOADED))
7416 return error_mark_node;
7419 return current.lhs;
7423 /* Parse the `? expression : assignment-expression' part of a
7424 conditional-expression. The LOGICAL_OR_EXPR is the
7425 logical-or-expression that started the conditional-expression.
7426 Returns a representation of the entire conditional-expression.
7428 This routine is used by cp_parser_assignment_expression.
7430 ? expression : assignment-expression
7432 GNU Extensions:
7434 ? : assignment-expression */
7436 static tree
7437 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7439 tree expr;
7440 tree assignment_expr;
7441 struct cp_token *token;
7442 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7444 /* Consume the `?' token. */
7445 cp_lexer_consume_token (parser->lexer);
7446 token = cp_lexer_peek_token (parser->lexer);
7447 if (cp_parser_allow_gnu_extensions_p (parser)
7448 && token->type == CPP_COLON)
7450 pedwarn (token->location, OPT_Wpedantic,
7451 "ISO C++ does not allow ?: with omitted middle operand");
7452 /* Implicit true clause. */
7453 expr = NULL_TREE;
7454 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7455 warn_for_omitted_condop (token->location, logical_or_expr);
7457 else
7459 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7460 parser->colon_corrects_to_scope_p = false;
7461 /* Parse the expression. */
7462 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7463 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7464 c_inhibit_evaluation_warnings +=
7465 ((logical_or_expr == truthvalue_true_node)
7466 - (logical_or_expr == truthvalue_false_node));
7467 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7470 /* The next token should be a `:'. */
7471 cp_parser_require (parser, CPP_COLON, RT_COLON);
7472 /* Parse the assignment-expression. */
7473 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7474 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7476 /* Build the conditional-expression. */
7477 return build_x_conditional_expr (loc, logical_or_expr,
7478 expr,
7479 assignment_expr,
7480 tf_warning_or_error);
7483 /* Parse an assignment-expression.
7485 assignment-expression:
7486 conditional-expression
7487 logical-or-expression assignment-operator assignment_expression
7488 throw-expression
7490 CAST_P is true if this expression is the target of a cast.
7492 Returns a representation for the expression. */
7494 static tree
7495 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7496 cp_id_kind * pidk)
7498 tree expr;
7500 /* If the next token is the `throw' keyword, then we're looking at
7501 a throw-expression. */
7502 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7503 expr = cp_parser_throw_expression (parser);
7504 /* Otherwise, it must be that we are looking at a
7505 logical-or-expression. */
7506 else
7508 /* Parse the binary expressions (logical-or-expression). */
7509 expr = cp_parser_binary_expression (parser, cast_p, false,
7510 PREC_NOT_OPERATOR, pidk);
7511 /* If the next token is a `?' then we're actually looking at a
7512 conditional-expression. */
7513 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7514 return cp_parser_question_colon_clause (parser, expr);
7515 else
7517 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7519 /* If it's an assignment-operator, we're using the second
7520 production. */
7521 enum tree_code assignment_operator
7522 = cp_parser_assignment_operator_opt (parser);
7523 if (assignment_operator != ERROR_MARK)
7525 bool non_constant_p;
7526 location_t saved_input_location;
7528 /* Parse the right-hand side of the assignment. */
7529 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7531 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7532 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7534 /* An assignment may not appear in a
7535 constant-expression. */
7536 if (cp_parser_non_integral_constant_expression (parser,
7537 NIC_ASSIGNMENT))
7538 return error_mark_node;
7539 /* Build the assignment expression. Its default
7540 location is the location of the '=' token. */
7541 saved_input_location = input_location;
7542 input_location = loc;
7543 expr = build_x_modify_expr (loc, expr,
7544 assignment_operator,
7545 rhs,
7546 tf_warning_or_error);
7547 input_location = saved_input_location;
7552 return expr;
7555 /* Parse an (optional) assignment-operator.
7557 assignment-operator: one of
7558 = *= /= %= += -= >>= <<= &= ^= |=
7560 GNU Extension:
7562 assignment-operator: one of
7563 <?= >?=
7565 If the next token is an assignment operator, the corresponding tree
7566 code is returned, and the token is consumed. For example, for
7567 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7568 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7569 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7570 operator, ERROR_MARK is returned. */
7572 static enum tree_code
7573 cp_parser_assignment_operator_opt (cp_parser* parser)
7575 enum tree_code op;
7576 cp_token *token;
7578 /* Peek at the next token. */
7579 token = cp_lexer_peek_token (parser->lexer);
7581 switch (token->type)
7583 case CPP_EQ:
7584 op = NOP_EXPR;
7585 break;
7587 case CPP_MULT_EQ:
7588 op = MULT_EXPR;
7589 break;
7591 case CPP_DIV_EQ:
7592 op = TRUNC_DIV_EXPR;
7593 break;
7595 case CPP_MOD_EQ:
7596 op = TRUNC_MOD_EXPR;
7597 break;
7599 case CPP_PLUS_EQ:
7600 op = PLUS_EXPR;
7601 break;
7603 case CPP_MINUS_EQ:
7604 op = MINUS_EXPR;
7605 break;
7607 case CPP_RSHIFT_EQ:
7608 op = RSHIFT_EXPR;
7609 break;
7611 case CPP_LSHIFT_EQ:
7612 op = LSHIFT_EXPR;
7613 break;
7615 case CPP_AND_EQ:
7616 op = BIT_AND_EXPR;
7617 break;
7619 case CPP_XOR_EQ:
7620 op = BIT_XOR_EXPR;
7621 break;
7623 case CPP_OR_EQ:
7624 op = BIT_IOR_EXPR;
7625 break;
7627 default:
7628 /* Nothing else is an assignment operator. */
7629 op = ERROR_MARK;
7632 /* If it was an assignment operator, consume it. */
7633 if (op != ERROR_MARK)
7634 cp_lexer_consume_token (parser->lexer);
7636 return op;
7639 /* Parse an expression.
7641 expression:
7642 assignment-expression
7643 expression , assignment-expression
7645 CAST_P is true if this expression is the target of a cast.
7647 Returns a representation of the expression. */
7649 static tree
7650 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7652 tree expression = NULL_TREE;
7653 location_t loc = UNKNOWN_LOCATION;
7655 while (true)
7657 tree assignment_expression;
7659 /* Parse the next assignment-expression. */
7660 assignment_expression
7661 = cp_parser_assignment_expression (parser, cast_p, pidk);
7662 /* If this is the first assignment-expression, we can just
7663 save it away. */
7664 if (!expression)
7665 expression = assignment_expression;
7666 else
7667 expression = build_x_compound_expr (loc, expression,
7668 assignment_expression,
7669 tf_warning_or_error);
7670 /* If the next token is not a comma, then we are done with the
7671 expression. */
7672 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7673 break;
7674 /* Consume the `,'. */
7675 loc = cp_lexer_peek_token (parser->lexer)->location;
7676 cp_lexer_consume_token (parser->lexer);
7677 /* A comma operator cannot appear in a constant-expression. */
7678 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7679 expression = error_mark_node;
7682 return expression;
7685 /* Parse a constant-expression.
7687 constant-expression:
7688 conditional-expression
7690 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7691 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7692 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7693 is false, NON_CONSTANT_P should be NULL. */
7695 static tree
7696 cp_parser_constant_expression (cp_parser* parser,
7697 bool allow_non_constant_p,
7698 bool *non_constant_p)
7700 bool saved_integral_constant_expression_p;
7701 bool saved_allow_non_integral_constant_expression_p;
7702 bool saved_non_integral_constant_expression_p;
7703 tree expression;
7705 /* It might seem that we could simply parse the
7706 conditional-expression, and then check to see if it were
7707 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7708 one that the compiler can figure out is constant, possibly after
7709 doing some simplifications or optimizations. The standard has a
7710 precise definition of constant-expression, and we must honor
7711 that, even though it is somewhat more restrictive.
7713 For example:
7715 int i[(2, 3)];
7717 is not a legal declaration, because `(2, 3)' is not a
7718 constant-expression. The `,' operator is forbidden in a
7719 constant-expression. However, GCC's constant-folding machinery
7720 will fold this operation to an INTEGER_CST for `3'. */
7722 /* Save the old settings. */
7723 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7724 saved_allow_non_integral_constant_expression_p
7725 = parser->allow_non_integral_constant_expression_p;
7726 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7727 /* We are now parsing a constant-expression. */
7728 parser->integral_constant_expression_p = true;
7729 parser->allow_non_integral_constant_expression_p
7730 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7731 parser->non_integral_constant_expression_p = false;
7732 /* Although the grammar says "conditional-expression", we parse an
7733 "assignment-expression", which also permits "throw-expression"
7734 and the use of assignment operators. In the case that
7735 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7736 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7737 actually essential that we look for an assignment-expression.
7738 For example, cp_parser_initializer_clauses uses this function to
7739 determine whether a particular assignment-expression is in fact
7740 constant. */
7741 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7742 /* Restore the old settings. */
7743 parser->integral_constant_expression_p
7744 = saved_integral_constant_expression_p;
7745 parser->allow_non_integral_constant_expression_p
7746 = saved_allow_non_integral_constant_expression_p;
7747 if (cxx_dialect >= cxx0x)
7749 /* Require an rvalue constant expression here; that's what our
7750 callers expect. Reference constant expressions are handled
7751 separately in e.g. cp_parser_template_argument. */
7752 bool is_const = potential_rvalue_constant_expression (expression);
7753 parser->non_integral_constant_expression_p = !is_const;
7754 if (!is_const && !allow_non_constant_p)
7755 require_potential_rvalue_constant_expression (expression);
7757 if (allow_non_constant_p)
7758 *non_constant_p = parser->non_integral_constant_expression_p;
7759 parser->non_integral_constant_expression_p
7760 = saved_non_integral_constant_expression_p;
7762 return expression;
7765 /* Parse __builtin_offsetof.
7767 offsetof-expression:
7768 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7770 offsetof-member-designator:
7771 id-expression
7772 | offsetof-member-designator "." id-expression
7773 | offsetof-member-designator "[" expression "]"
7774 | offsetof-member-designator "->" id-expression */
7776 static tree
7777 cp_parser_builtin_offsetof (cp_parser *parser)
7779 int save_ice_p, save_non_ice_p;
7780 tree type, expr;
7781 cp_id_kind dummy;
7782 cp_token *token;
7784 /* We're about to accept non-integral-constant things, but will
7785 definitely yield an integral constant expression. Save and
7786 restore these values around our local parsing. */
7787 save_ice_p = parser->integral_constant_expression_p;
7788 save_non_ice_p = parser->non_integral_constant_expression_p;
7790 /* Consume the "__builtin_offsetof" token. */
7791 cp_lexer_consume_token (parser->lexer);
7792 /* Consume the opening `('. */
7793 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7794 /* Parse the type-id. */
7795 type = cp_parser_type_id (parser);
7796 /* Look for the `,'. */
7797 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7798 token = cp_lexer_peek_token (parser->lexer);
7800 /* Build the (type *)null that begins the traditional offsetof macro. */
7801 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7802 tf_warning_or_error);
7804 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7805 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7806 true, &dummy, token->location);
7807 while (true)
7809 token = cp_lexer_peek_token (parser->lexer);
7810 switch (token->type)
7812 case CPP_OPEN_SQUARE:
7813 /* offsetof-member-designator "[" expression "]" */
7814 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7815 break;
7817 case CPP_DEREF:
7818 /* offsetof-member-designator "->" identifier */
7819 expr = grok_array_decl (token->location, expr, integer_zero_node);
7820 /* FALLTHRU */
7822 case CPP_DOT:
7823 /* offsetof-member-designator "." identifier */
7824 cp_lexer_consume_token (parser->lexer);
7825 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7826 expr, true, &dummy,
7827 token->location);
7828 break;
7830 case CPP_CLOSE_PAREN:
7831 /* Consume the ")" token. */
7832 cp_lexer_consume_token (parser->lexer);
7833 goto success;
7835 default:
7836 /* Error. We know the following require will fail, but
7837 that gives the proper error message. */
7838 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7839 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7840 expr = error_mark_node;
7841 goto failure;
7845 success:
7846 /* If we're processing a template, we can't finish the semantics yet.
7847 Otherwise we can fold the entire expression now. */
7848 if (processing_template_decl)
7849 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7850 else
7851 expr = finish_offsetof (expr);
7853 failure:
7854 parser->integral_constant_expression_p = save_ice_p;
7855 parser->non_integral_constant_expression_p = save_non_ice_p;
7857 return expr;
7860 /* Parse a trait expression.
7862 Returns a representation of the expression, the underlying type
7863 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7865 static tree
7866 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7868 cp_trait_kind kind;
7869 tree type1, type2 = NULL_TREE;
7870 bool binary = false;
7871 cp_decl_specifier_seq decl_specs;
7873 switch (keyword)
7875 case RID_HAS_NOTHROW_ASSIGN:
7876 kind = CPTK_HAS_NOTHROW_ASSIGN;
7877 break;
7878 case RID_HAS_NOTHROW_CONSTRUCTOR:
7879 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7880 break;
7881 case RID_HAS_NOTHROW_COPY:
7882 kind = CPTK_HAS_NOTHROW_COPY;
7883 break;
7884 case RID_HAS_TRIVIAL_ASSIGN:
7885 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7886 break;
7887 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7888 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7889 break;
7890 case RID_HAS_TRIVIAL_COPY:
7891 kind = CPTK_HAS_TRIVIAL_COPY;
7892 break;
7893 case RID_HAS_TRIVIAL_DESTRUCTOR:
7894 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7895 break;
7896 case RID_HAS_VIRTUAL_DESTRUCTOR:
7897 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7898 break;
7899 case RID_IS_ABSTRACT:
7900 kind = CPTK_IS_ABSTRACT;
7901 break;
7902 case RID_IS_BASE_OF:
7903 kind = CPTK_IS_BASE_OF;
7904 binary = true;
7905 break;
7906 case RID_IS_CLASS:
7907 kind = CPTK_IS_CLASS;
7908 break;
7909 case RID_IS_CONVERTIBLE_TO:
7910 kind = CPTK_IS_CONVERTIBLE_TO;
7911 binary = true;
7912 break;
7913 case RID_IS_EMPTY:
7914 kind = CPTK_IS_EMPTY;
7915 break;
7916 case RID_IS_ENUM:
7917 kind = CPTK_IS_ENUM;
7918 break;
7919 case RID_IS_FINAL:
7920 kind = CPTK_IS_FINAL;
7921 break;
7922 case RID_IS_LITERAL_TYPE:
7923 kind = CPTK_IS_LITERAL_TYPE;
7924 break;
7925 case RID_IS_POD:
7926 kind = CPTK_IS_POD;
7927 break;
7928 case RID_IS_POLYMORPHIC:
7929 kind = CPTK_IS_POLYMORPHIC;
7930 break;
7931 case RID_IS_STD_LAYOUT:
7932 kind = CPTK_IS_STD_LAYOUT;
7933 break;
7934 case RID_IS_TRIVIAL:
7935 kind = CPTK_IS_TRIVIAL;
7936 break;
7937 case RID_IS_UNION:
7938 kind = CPTK_IS_UNION;
7939 break;
7940 case RID_UNDERLYING_TYPE:
7941 kind = CPTK_UNDERLYING_TYPE;
7942 break;
7943 case RID_BASES:
7944 kind = CPTK_BASES;
7945 break;
7946 case RID_DIRECT_BASES:
7947 kind = CPTK_DIRECT_BASES;
7948 break;
7949 default:
7950 gcc_unreachable ();
7953 /* Consume the token. */
7954 cp_lexer_consume_token (parser->lexer);
7956 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7958 type1 = cp_parser_type_id (parser);
7960 if (type1 == error_mark_node)
7961 return error_mark_node;
7963 /* Build a trivial decl-specifier-seq. */
7964 clear_decl_specs (&decl_specs);
7965 decl_specs.type = type1;
7967 /* Call grokdeclarator to figure out what type this is. */
7968 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7969 /*initialized=*/0, /*attrlist=*/NULL);
7971 if (binary)
7973 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7975 type2 = cp_parser_type_id (parser);
7977 if (type2 == error_mark_node)
7978 return error_mark_node;
7980 /* Build a trivial decl-specifier-seq. */
7981 clear_decl_specs (&decl_specs);
7982 decl_specs.type = type2;
7984 /* Call grokdeclarator to figure out what type this is. */
7985 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7986 /*initialized=*/0, /*attrlist=*/NULL);
7989 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7991 /* Complete the trait expression, which may mean either processing
7992 the trait expr now or saving it for template instantiation. */
7993 switch(kind)
7995 case CPTK_UNDERLYING_TYPE:
7996 return finish_underlying_type (type1);
7997 case CPTK_BASES:
7998 return finish_bases (type1, false);
7999 case CPTK_DIRECT_BASES:
8000 return finish_bases (type1, true);
8001 default:
8002 return finish_trait_expr (kind, type1, type2);
8006 /* Lambdas that appear in variable initializer or default argument scope
8007 get that in their mangling, so we need to record it. We might as well
8008 use the count for function and namespace scopes as well. */
8009 static GTY(()) tree lambda_scope;
8010 static GTY(()) int lambda_count;
8011 typedef struct GTY(()) tree_int
8013 tree t;
8014 int i;
8015 } tree_int;
8016 DEF_VEC_O(tree_int);
8017 DEF_VEC_ALLOC_O(tree_int,gc);
8018 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
8020 static void
8021 start_lambda_scope (tree decl)
8023 tree_int ti;
8024 gcc_assert (decl);
8025 /* Once we're inside a function, we ignore other scopes and just push
8026 the function again so that popping works properly. */
8027 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8028 decl = current_function_decl;
8029 ti.t = lambda_scope;
8030 ti.i = lambda_count;
8031 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
8032 if (lambda_scope != decl)
8034 /* Don't reset the count if we're still in the same function. */
8035 lambda_scope = decl;
8036 lambda_count = 0;
8040 static void
8041 record_lambda_scope (tree lambda)
8043 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8044 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8047 static void
8048 finish_lambda_scope (void)
8050 tree_int *p = &VEC_last (tree_int, lambda_scope_stack);
8051 if (lambda_scope != p->t)
8053 lambda_scope = p->t;
8054 lambda_count = p->i;
8056 VEC_pop (tree_int, lambda_scope_stack);
8059 /* Parse a lambda expression.
8061 lambda-expression:
8062 lambda-introducer lambda-declarator [opt] compound-statement
8064 Returns a representation of the expression. */
8066 static tree
8067 cp_parser_lambda_expression (cp_parser* parser)
8069 tree lambda_expr = build_lambda_expr ();
8070 tree type;
8071 bool ok;
8073 LAMBDA_EXPR_LOCATION (lambda_expr)
8074 = cp_lexer_peek_token (parser->lexer)->location;
8076 if (cp_unevaluated_operand)
8077 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8078 "lambda-expression in unevaluated context");
8080 /* We may be in the middle of deferred access check. Disable
8081 it now. */
8082 push_deferring_access_checks (dk_no_deferred);
8084 cp_parser_lambda_introducer (parser, lambda_expr);
8086 type = begin_lambda_type (lambda_expr);
8087 if (type == error_mark_node)
8088 return error_mark_node;
8090 record_lambda_scope (lambda_expr);
8092 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8093 determine_visibility (TYPE_NAME (type));
8095 /* Now that we've started the type, add the capture fields for any
8096 explicit captures. */
8097 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8100 /* Inside the class, surrounding template-parameter-lists do not apply. */
8101 unsigned int saved_num_template_parameter_lists
8102 = parser->num_template_parameter_lists;
8103 unsigned char in_statement = parser->in_statement;
8104 bool in_switch_statement_p = parser->in_switch_statement_p;
8106 parser->num_template_parameter_lists = 0;
8107 parser->in_statement = 0;
8108 parser->in_switch_statement_p = false;
8110 /* By virtue of defining a local class, a lambda expression has access to
8111 the private variables of enclosing classes. */
8113 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8115 if (ok)
8116 cp_parser_lambda_body (parser, lambda_expr);
8117 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8118 cp_parser_skip_to_end_of_block_or_statement (parser);
8120 /* The capture list was built up in reverse order; fix that now. */
8122 tree newlist = NULL_TREE;
8123 tree elt, next;
8125 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8126 elt; elt = next)
8128 next = TREE_CHAIN (elt);
8129 TREE_CHAIN (elt) = newlist;
8130 newlist = elt;
8132 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8135 if (ok)
8136 maybe_add_lambda_conv_op (type);
8138 type = finish_struct (type, /*attributes=*/NULL_TREE);
8140 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8141 parser->in_statement = in_statement;
8142 parser->in_switch_statement_p = in_switch_statement_p;
8145 pop_deferring_access_checks ();
8147 /* This field is only used during parsing of the lambda. */
8148 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8150 /* This lambda shouldn't have any proxies left at this point. */
8151 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8152 /* And now that we're done, push proxies for an enclosing lambda. */
8153 insert_pending_capture_proxies ();
8155 if (ok)
8156 return build_lambda_object (lambda_expr);
8157 else
8158 return error_mark_node;
8161 /* Parse the beginning of a lambda expression.
8163 lambda-introducer:
8164 [ lambda-capture [opt] ]
8166 LAMBDA_EXPR is the current representation of the lambda expression. */
8168 static void
8169 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8171 /* Need commas after the first capture. */
8172 bool first = true;
8174 /* Eat the leading `['. */
8175 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8177 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8178 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8179 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8180 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8181 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8182 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8184 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8186 cp_lexer_consume_token (parser->lexer);
8187 first = false;
8190 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8192 cp_token* capture_token;
8193 tree capture_id;
8194 tree capture_init_expr;
8195 cp_id_kind idk = CP_ID_KIND_NONE;
8196 bool explicit_init_p = false;
8198 enum capture_kind_type
8200 BY_COPY,
8201 BY_REFERENCE
8203 enum capture_kind_type capture_kind = BY_COPY;
8205 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8207 error ("expected end of capture-list");
8208 return;
8211 if (first)
8212 first = false;
8213 else
8214 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8216 /* Possibly capture `this'. */
8217 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8219 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8220 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8221 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8222 "with by-copy capture default");
8223 cp_lexer_consume_token (parser->lexer);
8224 add_capture (lambda_expr,
8225 /*id=*/this_identifier,
8226 /*initializer=*/finish_this_expr(),
8227 /*by_reference_p=*/false,
8228 explicit_init_p);
8229 continue;
8232 /* Remember whether we want to capture as a reference or not. */
8233 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8235 capture_kind = BY_REFERENCE;
8236 cp_lexer_consume_token (parser->lexer);
8239 /* Get the identifier. */
8240 capture_token = cp_lexer_peek_token (parser->lexer);
8241 capture_id = cp_parser_identifier (parser);
8243 if (capture_id == error_mark_node)
8244 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8245 delimiters, but I modified this to stop on unnested ']' as well. It
8246 was already changed to stop on unnested '}', so the
8247 "closing_parenthesis" name is no more misleading with my change. */
8249 cp_parser_skip_to_closing_parenthesis (parser,
8250 /*recovering=*/true,
8251 /*or_comma=*/true,
8252 /*consume_paren=*/true);
8253 break;
8256 /* Find the initializer for this capture. */
8257 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8259 /* An explicit expression exists. */
8260 cp_lexer_consume_token (parser->lexer);
8261 pedwarn (input_location, OPT_Wpedantic,
8262 "ISO C++ does not allow initializers "
8263 "in lambda expression capture lists");
8264 capture_init_expr = cp_parser_assignment_expression (parser,
8265 /*cast_p=*/true,
8266 &idk);
8267 explicit_init_p = true;
8269 else
8271 const char* error_msg;
8273 /* Turn the identifier into an id-expression. */
8274 capture_init_expr
8275 = cp_parser_lookup_name
8276 (parser,
8277 capture_id,
8278 none_type,
8279 /*is_template=*/false,
8280 /*is_namespace=*/false,
8281 /*check_dependency=*/true,
8282 /*ambiguous_decls=*/NULL,
8283 capture_token->location);
8285 if (capture_init_expr == error_mark_node)
8287 unqualified_name_lookup_error (capture_id);
8288 continue;
8290 else if (DECL_P (capture_init_expr)
8291 && (TREE_CODE (capture_init_expr) != VAR_DECL
8292 && TREE_CODE (capture_init_expr) != PARM_DECL))
8294 error_at (capture_token->location,
8295 "capture of non-variable %qD ",
8296 capture_init_expr);
8297 inform (0, "%q+#D declared here", capture_init_expr);
8298 continue;
8300 if (TREE_CODE (capture_init_expr) == VAR_DECL
8301 && decl_storage_duration (capture_init_expr) != dk_auto)
8303 pedwarn (capture_token->location, 0, "capture of variable "
8304 "%qD with non-automatic storage duration",
8305 capture_init_expr);
8306 inform (0, "%q+#D declared here", capture_init_expr);
8307 continue;
8310 capture_init_expr
8311 = finish_id_expression
8312 (capture_id,
8313 capture_init_expr,
8314 parser->scope,
8315 &idk,
8316 /*integral_constant_expression_p=*/false,
8317 /*allow_non_integral_constant_expression_p=*/false,
8318 /*non_integral_constant_expression_p=*/NULL,
8319 /*template_p=*/false,
8320 /*done=*/true,
8321 /*address_p=*/false,
8322 /*template_arg_p=*/false,
8323 &error_msg,
8324 capture_token->location);
8327 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8328 && !explicit_init_p)
8330 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8331 && capture_kind == BY_COPY)
8332 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8333 "of %qD redundant with by-copy capture default",
8334 capture_id);
8335 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8336 && capture_kind == BY_REFERENCE)
8337 pedwarn (capture_token->location, 0, "explicit by-reference "
8338 "capture of %qD redundant with by-reference capture "
8339 "default", capture_id);
8342 add_capture (lambda_expr,
8343 capture_id,
8344 capture_init_expr,
8345 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8346 explicit_init_p);
8349 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8352 /* Parse the (optional) middle of a lambda expression.
8354 lambda-declarator:
8355 ( parameter-declaration-clause [opt] )
8356 attribute-specifier [opt]
8357 mutable [opt]
8358 exception-specification [opt]
8359 lambda-return-type-clause [opt]
8361 LAMBDA_EXPR is the current representation of the lambda expression. */
8363 static bool
8364 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8366 /* 5.1.1.4 of the standard says:
8367 If a lambda-expression does not include a lambda-declarator, it is as if
8368 the lambda-declarator were ().
8369 This means an empty parameter list, no attributes, and no exception
8370 specification. */
8371 tree param_list = void_list_node;
8372 tree attributes = NULL_TREE;
8373 tree exception_spec = NULL_TREE;
8374 tree t;
8376 /* The lambda-declarator is optional, but must begin with an opening
8377 parenthesis if present. */
8378 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8380 cp_lexer_consume_token (parser->lexer);
8382 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8384 /* Parse parameters. */
8385 param_list = cp_parser_parameter_declaration_clause (parser);
8387 /* Default arguments shall not be specified in the
8388 parameter-declaration-clause of a lambda-declarator. */
8389 for (t = param_list; t; t = TREE_CHAIN (t))
8390 if (TREE_PURPOSE (t))
8391 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8392 "default argument specified for lambda parameter");
8394 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8396 attributes = cp_parser_attributes_opt (parser);
8398 /* Parse optional `mutable' keyword. */
8399 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8401 cp_lexer_consume_token (parser->lexer);
8402 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8405 /* Parse optional exception specification. */
8406 exception_spec = cp_parser_exception_specification_opt (parser);
8408 /* Parse optional trailing return type. */
8409 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8411 cp_lexer_consume_token (parser->lexer);
8412 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8415 /* The function parameters must be in scope all the way until after the
8416 trailing-return-type in case of decltype. */
8417 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8418 pop_binding (DECL_NAME (t), t);
8420 leave_scope ();
8423 /* Create the function call operator.
8425 Messing with declarators like this is no uglier than building up the
8426 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8427 other code. */
8429 cp_decl_specifier_seq return_type_specs;
8430 cp_declarator* declarator;
8431 tree fco;
8432 int quals;
8433 void *p;
8435 clear_decl_specs (&return_type_specs);
8436 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8437 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8438 else
8439 /* Maybe we will deduce the return type later. */
8440 return_type_specs.type = make_auto ();
8442 p = obstack_alloc (&declarator_obstack, 0);
8444 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8445 sfk_none);
8447 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8448 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8449 declarator = make_call_declarator (declarator, param_list, quals,
8450 VIRT_SPEC_UNSPECIFIED,
8451 exception_spec,
8452 /*late_return_type=*/NULL_TREE);
8453 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8455 fco = grokmethod (&return_type_specs,
8456 declarator,
8457 attributes);
8458 if (fco != error_mark_node)
8460 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8461 DECL_ARTIFICIAL (fco) = 1;
8462 /* Give the object parameter a different name. */
8463 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8466 finish_member_declaration (fco);
8468 obstack_free (&declarator_obstack, p);
8470 return (fco != error_mark_node);
8474 /* Parse the body of a lambda expression, which is simply
8476 compound-statement
8478 but which requires special handling.
8479 LAMBDA_EXPR is the current representation of the lambda expression. */
8481 static void
8482 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8484 bool nested = (current_function_decl != NULL_TREE);
8485 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8486 if (nested)
8487 push_function_context ();
8488 else
8489 /* Still increment function_depth so that we don't GC in the
8490 middle of an expression. */
8491 ++function_depth;
8492 /* Clear this in case we're in the middle of a default argument. */
8493 parser->local_variables_forbidden_p = false;
8495 /* Finish the function call operator
8496 - class_specifier
8497 + late_parsing_for_member
8498 + function_definition_after_declarator
8499 + ctor_initializer_opt_and_function_body */
8501 tree fco = lambda_function (lambda_expr);
8502 tree body;
8503 bool done = false;
8504 tree compound_stmt;
8505 tree cap;
8507 /* Let the front end know that we are going to be defining this
8508 function. */
8509 start_preparsed_function (fco,
8510 NULL_TREE,
8511 SF_PRE_PARSED | SF_INCLASS_INLINE);
8513 start_lambda_scope (fco);
8514 body = begin_function_body ();
8516 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8517 goto out;
8519 /* Push the proxies for any explicit captures. */
8520 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8521 cap = TREE_CHAIN (cap))
8522 build_capture_proxy (TREE_PURPOSE (cap));
8524 compound_stmt = begin_compound_stmt (0);
8526 /* 5.1.1.4 of the standard says:
8527 If a lambda-expression does not include a trailing-return-type, it
8528 is as if the trailing-return-type denotes the following type:
8529 * if the compound-statement is of the form
8530 { return attribute-specifier [opt] expression ; }
8531 the type of the returned expression after lvalue-to-rvalue
8532 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8533 (_conv.array_ 4.2), and function-to-pointer conversion
8534 (_conv.func_ 4.3);
8535 * otherwise, void. */
8537 /* In a lambda that has neither a lambda-return-type-clause
8538 nor a deducible form, errors should be reported for return statements
8539 in the body. Since we used void as the placeholder return type, parsing
8540 the body as usual will give such desired behavior. */
8541 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8542 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8543 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8545 tree expr = NULL_TREE;
8546 cp_id_kind idk = CP_ID_KIND_NONE;
8548 /* Parse tentatively in case there's more after the initial return
8549 statement. */
8550 cp_parser_parse_tentatively (parser);
8552 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8554 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8556 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8557 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8559 if (cp_parser_parse_definitely (parser))
8561 if (!processing_template_decl)
8562 apply_deduced_return_type (fco, lambda_return_type (expr));
8564 /* Will get error here if type not deduced yet. */
8565 finish_return_stmt (expr);
8567 done = true;
8571 if (!done)
8573 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8574 cp_parser_label_declaration (parser);
8575 cp_parser_statement_seq_opt (parser, NULL_TREE);
8576 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8579 finish_compound_stmt (compound_stmt);
8581 out:
8582 finish_function_body (body);
8583 finish_lambda_scope ();
8585 /* Finish the function and generate code for it if necessary. */
8586 expand_or_defer_fn (finish_function (/*inline*/2));
8589 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8590 if (nested)
8591 pop_function_context();
8592 else
8593 --function_depth;
8596 /* Statements [gram.stmt.stmt] */
8598 /* Parse a statement.
8600 statement:
8601 labeled-statement
8602 expression-statement
8603 compound-statement
8604 selection-statement
8605 iteration-statement
8606 jump-statement
8607 declaration-statement
8608 try-block
8610 TM Extension:
8612 statement:
8613 atomic-statement
8615 IN_COMPOUND is true when the statement is nested inside a
8616 cp_parser_compound_statement; this matters for certain pragmas.
8618 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8619 is a (possibly labeled) if statement which is not enclosed in braces
8620 and has an else clause. This is used to implement -Wparentheses. */
8622 static void
8623 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8624 bool in_compound, bool *if_p)
8626 tree statement;
8627 cp_token *token;
8628 location_t statement_location;
8630 restart:
8631 if (if_p != NULL)
8632 *if_p = false;
8633 /* There is no statement yet. */
8634 statement = NULL_TREE;
8635 /* Peek at the next token. */
8636 token = cp_lexer_peek_token (parser->lexer);
8637 /* Remember the location of the first token in the statement. */
8638 statement_location = token->location;
8639 /* If this is a keyword, then that will often determine what kind of
8640 statement we have. */
8641 if (token->type == CPP_KEYWORD)
8643 enum rid keyword = token->keyword;
8645 switch (keyword)
8647 case RID_CASE:
8648 case RID_DEFAULT:
8649 /* Looks like a labeled-statement with a case label.
8650 Parse the label, and then use tail recursion to parse
8651 the statement. */
8652 cp_parser_label_for_labeled_statement (parser);
8653 goto restart;
8655 case RID_IF:
8656 case RID_SWITCH:
8657 statement = cp_parser_selection_statement (parser, if_p);
8658 break;
8660 case RID_WHILE:
8661 case RID_DO:
8662 case RID_FOR:
8663 statement = cp_parser_iteration_statement (parser);
8664 break;
8666 case RID_BREAK:
8667 case RID_CONTINUE:
8668 case RID_RETURN:
8669 case RID_GOTO:
8670 statement = cp_parser_jump_statement (parser);
8671 break;
8673 /* Objective-C++ exception-handling constructs. */
8674 case RID_AT_TRY:
8675 case RID_AT_CATCH:
8676 case RID_AT_FINALLY:
8677 case RID_AT_SYNCHRONIZED:
8678 case RID_AT_THROW:
8679 statement = cp_parser_objc_statement (parser);
8680 break;
8682 case RID_TRY:
8683 statement = cp_parser_try_block (parser);
8684 break;
8686 case RID_NAMESPACE:
8687 /* This must be a namespace alias definition. */
8688 cp_parser_declaration_statement (parser);
8689 return;
8691 case RID_TRANSACTION_ATOMIC:
8692 case RID_TRANSACTION_RELAXED:
8693 statement = cp_parser_transaction (parser, keyword);
8694 break;
8695 case RID_TRANSACTION_CANCEL:
8696 statement = cp_parser_transaction_cancel (parser);
8697 break;
8699 default:
8700 /* It might be a keyword like `int' that can start a
8701 declaration-statement. */
8702 break;
8705 else if (token->type == CPP_NAME)
8707 /* If the next token is a `:', then we are looking at a
8708 labeled-statement. */
8709 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8710 if (token->type == CPP_COLON)
8712 /* Looks like a labeled-statement with an ordinary label.
8713 Parse the label, and then use tail recursion to parse
8714 the statement. */
8715 cp_parser_label_for_labeled_statement (parser);
8716 goto restart;
8719 /* Anything that starts with a `{' must be a compound-statement. */
8720 else if (token->type == CPP_OPEN_BRACE)
8721 statement = cp_parser_compound_statement (parser, NULL, false, false);
8722 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8723 a statement all its own. */
8724 else if (token->type == CPP_PRAGMA)
8726 /* Only certain OpenMP pragmas are attached to statements, and thus
8727 are considered statements themselves. All others are not. In
8728 the context of a compound, accept the pragma as a "statement" and
8729 return so that we can check for a close brace. Otherwise we
8730 require a real statement and must go back and read one. */
8731 if (in_compound)
8732 cp_parser_pragma (parser, pragma_compound);
8733 else if (!cp_parser_pragma (parser, pragma_stmt))
8734 goto restart;
8735 return;
8737 else if (token->type == CPP_EOF)
8739 cp_parser_error (parser, "expected statement");
8740 return;
8743 /* Everything else must be a declaration-statement or an
8744 expression-statement. Try for the declaration-statement
8745 first, unless we are looking at a `;', in which case we know that
8746 we have an expression-statement. */
8747 if (!statement)
8749 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8751 cp_parser_parse_tentatively (parser);
8752 /* Try to parse the declaration-statement. */
8753 cp_parser_declaration_statement (parser);
8754 /* If that worked, we're done. */
8755 if (cp_parser_parse_definitely (parser))
8756 return;
8758 /* Look for an expression-statement instead. */
8759 statement = cp_parser_expression_statement (parser, in_statement_expr);
8762 /* Set the line number for the statement. */
8763 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8764 SET_EXPR_LOCATION (statement, statement_location);
8767 /* Parse the label for a labeled-statement, i.e.
8769 identifier :
8770 case constant-expression :
8771 default :
8773 GNU Extension:
8774 case constant-expression ... constant-expression : statement
8776 When a label is parsed without errors, the label is added to the
8777 parse tree by the finish_* functions, so this function doesn't
8778 have to return the label. */
8780 static void
8781 cp_parser_label_for_labeled_statement (cp_parser* parser)
8783 cp_token *token;
8784 tree label = NULL_TREE;
8785 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8787 /* The next token should be an identifier. */
8788 token = cp_lexer_peek_token (parser->lexer);
8789 if (token->type != CPP_NAME
8790 && token->type != CPP_KEYWORD)
8792 cp_parser_error (parser, "expected labeled-statement");
8793 return;
8796 parser->colon_corrects_to_scope_p = false;
8797 switch (token->keyword)
8799 case RID_CASE:
8801 tree expr, expr_hi;
8802 cp_token *ellipsis;
8804 /* Consume the `case' token. */
8805 cp_lexer_consume_token (parser->lexer);
8806 /* Parse the constant-expression. */
8807 expr = cp_parser_constant_expression (parser,
8808 /*allow_non_constant_p=*/false,
8809 NULL);
8811 ellipsis = cp_lexer_peek_token (parser->lexer);
8812 if (ellipsis->type == CPP_ELLIPSIS)
8814 /* Consume the `...' token. */
8815 cp_lexer_consume_token (parser->lexer);
8816 expr_hi =
8817 cp_parser_constant_expression (parser,
8818 /*allow_non_constant_p=*/false,
8819 NULL);
8820 /* We don't need to emit warnings here, as the common code
8821 will do this for us. */
8823 else
8824 expr_hi = NULL_TREE;
8826 if (parser->in_switch_statement_p)
8827 finish_case_label (token->location, expr, expr_hi);
8828 else
8829 error_at (token->location,
8830 "case label %qE not within a switch statement",
8831 expr);
8833 break;
8835 case RID_DEFAULT:
8836 /* Consume the `default' token. */
8837 cp_lexer_consume_token (parser->lexer);
8839 if (parser->in_switch_statement_p)
8840 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8841 else
8842 error_at (token->location, "case label not within a switch statement");
8843 break;
8845 default:
8846 /* Anything else must be an ordinary label. */
8847 label = finish_label_stmt (cp_parser_identifier (parser));
8848 break;
8851 /* Require the `:' token. */
8852 cp_parser_require (parser, CPP_COLON, RT_COLON);
8854 /* An ordinary label may optionally be followed by attributes.
8855 However, this is only permitted if the attributes are then
8856 followed by a semicolon. This is because, for backward
8857 compatibility, when parsing
8858 lab: __attribute__ ((unused)) int i;
8859 we want the attribute to attach to "i", not "lab". */
8860 if (label != NULL_TREE
8861 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8863 tree attrs;
8865 cp_parser_parse_tentatively (parser);
8866 attrs = cp_parser_attributes_opt (parser);
8867 if (attrs == NULL_TREE
8868 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8869 cp_parser_abort_tentative_parse (parser);
8870 else if (!cp_parser_parse_definitely (parser))
8872 else
8873 cplus_decl_attributes (&label, attrs, 0);
8876 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8879 /* Parse an expression-statement.
8881 expression-statement:
8882 expression [opt] ;
8884 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8885 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8886 indicates whether this expression-statement is part of an
8887 expression statement. */
8889 static tree
8890 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8892 tree statement = NULL_TREE;
8893 cp_token *token = cp_lexer_peek_token (parser->lexer);
8895 /* If the next token is a ';', then there is no expression
8896 statement. */
8897 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8898 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8900 /* Give a helpful message for "A<T>::type t;" and the like. */
8901 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8902 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8904 if (TREE_CODE (statement) == SCOPE_REF)
8905 error_at (token->location, "need %<typename%> before %qE because "
8906 "%qT is a dependent scope",
8907 statement, TREE_OPERAND (statement, 0));
8908 else if (is_overloaded_fn (statement)
8909 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8911 /* A::A a; */
8912 tree fn = get_first_fn (statement);
8913 error_at (token->location,
8914 "%<%T::%D%> names the constructor, not the type",
8915 DECL_CONTEXT (fn), DECL_NAME (fn));
8919 /* Consume the final `;'. */
8920 cp_parser_consume_semicolon_at_end_of_statement (parser);
8922 if (in_statement_expr
8923 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8924 /* This is the final expression statement of a statement
8925 expression. */
8926 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8927 else if (statement)
8928 statement = finish_expr_stmt (statement);
8929 else
8930 finish_stmt ();
8932 return statement;
8935 /* Parse a compound-statement.
8937 compound-statement:
8938 { statement-seq [opt] }
8940 GNU extension:
8942 compound-statement:
8943 { label-declaration-seq [opt] statement-seq [opt] }
8945 label-declaration-seq:
8946 label-declaration
8947 label-declaration-seq label-declaration
8949 Returns a tree representing the statement. */
8951 static tree
8952 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8953 bool in_try, bool function_body)
8955 tree compound_stmt;
8957 /* Consume the `{'. */
8958 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8959 return error_mark_node;
8960 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8961 && !function_body)
8962 pedwarn (input_location, OPT_Wpedantic,
8963 "compound-statement in constexpr function");
8964 /* Begin the compound-statement. */
8965 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8966 /* If the next keyword is `__label__' we have a label declaration. */
8967 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8968 cp_parser_label_declaration (parser);
8969 /* Parse an (optional) statement-seq. */
8970 cp_parser_statement_seq_opt (parser, in_statement_expr);
8971 /* Finish the compound-statement. */
8972 finish_compound_stmt (compound_stmt);
8973 /* Consume the `}'. */
8974 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8976 return compound_stmt;
8979 /* Parse an (optional) statement-seq.
8981 statement-seq:
8982 statement
8983 statement-seq [opt] statement */
8985 static void
8986 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8988 /* Scan statements until there aren't any more. */
8989 while (true)
8991 cp_token *token = cp_lexer_peek_token (parser->lexer);
8993 /* If we are looking at a `}', then we have run out of
8994 statements; the same is true if we have reached the end
8995 of file, or have stumbled upon a stray '@end'. */
8996 if (token->type == CPP_CLOSE_BRACE
8997 || token->type == CPP_EOF
8998 || token->type == CPP_PRAGMA_EOL
8999 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9000 break;
9002 /* If we are in a compound statement and find 'else' then
9003 something went wrong. */
9004 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9006 if (parser->in_statement & IN_IF_STMT)
9007 break;
9008 else
9010 token = cp_lexer_consume_token (parser->lexer);
9011 error_at (token->location, "%<else%> without a previous %<if%>");
9015 /* Parse the statement. */
9016 cp_parser_statement (parser, in_statement_expr, true, NULL);
9020 /* Parse a selection-statement.
9022 selection-statement:
9023 if ( condition ) statement
9024 if ( condition ) statement else statement
9025 switch ( condition ) statement
9027 Returns the new IF_STMT or SWITCH_STMT.
9029 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9030 is a (possibly labeled) if statement which is not enclosed in
9031 braces and has an else clause. This is used to implement
9032 -Wparentheses. */
9034 static tree
9035 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9037 cp_token *token;
9038 enum rid keyword;
9040 if (if_p != NULL)
9041 *if_p = false;
9043 /* Peek at the next token. */
9044 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9046 /* See what kind of keyword it is. */
9047 keyword = token->keyword;
9048 switch (keyword)
9050 case RID_IF:
9051 case RID_SWITCH:
9053 tree statement;
9054 tree condition;
9056 /* Look for the `('. */
9057 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9059 cp_parser_skip_to_end_of_statement (parser);
9060 return error_mark_node;
9063 /* Begin the selection-statement. */
9064 if (keyword == RID_IF)
9065 statement = begin_if_stmt ();
9066 else
9067 statement = begin_switch_stmt ();
9069 /* Parse the condition. */
9070 condition = cp_parser_condition (parser);
9071 /* Look for the `)'. */
9072 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9073 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9074 /*consume_paren=*/true);
9076 if (keyword == RID_IF)
9078 bool nested_if;
9079 unsigned char in_statement;
9081 /* Add the condition. */
9082 finish_if_stmt_cond (condition, statement);
9084 /* Parse the then-clause. */
9085 in_statement = parser->in_statement;
9086 parser->in_statement |= IN_IF_STMT;
9087 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9089 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9090 add_stmt (build_empty_stmt (loc));
9091 cp_lexer_consume_token (parser->lexer);
9092 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9093 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9094 "empty body in an %<if%> statement");
9095 nested_if = false;
9097 else
9098 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9099 parser->in_statement = in_statement;
9101 finish_then_clause (statement);
9103 /* If the next token is `else', parse the else-clause. */
9104 if (cp_lexer_next_token_is_keyword (parser->lexer,
9105 RID_ELSE))
9107 /* Consume the `else' keyword. */
9108 cp_lexer_consume_token (parser->lexer);
9109 begin_else_clause (statement);
9110 /* Parse the else-clause. */
9111 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9113 location_t loc;
9114 loc = cp_lexer_peek_token (parser->lexer)->location;
9115 warning_at (loc,
9116 OPT_Wempty_body, "suggest braces around "
9117 "empty body in an %<else%> statement");
9118 add_stmt (build_empty_stmt (loc));
9119 cp_lexer_consume_token (parser->lexer);
9121 else
9122 cp_parser_implicitly_scoped_statement (parser, NULL);
9124 finish_else_clause (statement);
9126 /* If we are currently parsing a then-clause, then
9127 IF_P will not be NULL. We set it to true to
9128 indicate that this if statement has an else clause.
9129 This may trigger the Wparentheses warning below
9130 when we get back up to the parent if statement. */
9131 if (if_p != NULL)
9132 *if_p = true;
9134 else
9136 /* This if statement does not have an else clause. If
9137 NESTED_IF is true, then the then-clause is an if
9138 statement which does have an else clause. We warn
9139 about the potential ambiguity. */
9140 if (nested_if)
9141 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9142 "suggest explicit braces to avoid ambiguous"
9143 " %<else%>");
9146 /* Now we're all done with the if-statement. */
9147 finish_if_stmt (statement);
9149 else
9151 bool in_switch_statement_p;
9152 unsigned char in_statement;
9154 /* Add the condition. */
9155 finish_switch_cond (condition, statement);
9157 /* Parse the body of the switch-statement. */
9158 in_switch_statement_p = parser->in_switch_statement_p;
9159 in_statement = parser->in_statement;
9160 parser->in_switch_statement_p = true;
9161 parser->in_statement |= IN_SWITCH_STMT;
9162 cp_parser_implicitly_scoped_statement (parser, NULL);
9163 parser->in_switch_statement_p = in_switch_statement_p;
9164 parser->in_statement = in_statement;
9166 /* Now we're all done with the switch-statement. */
9167 finish_switch_stmt (statement);
9170 return statement;
9172 break;
9174 default:
9175 cp_parser_error (parser, "expected selection-statement");
9176 return error_mark_node;
9180 /* Parse a condition.
9182 condition:
9183 expression
9184 type-specifier-seq declarator = initializer-clause
9185 type-specifier-seq declarator braced-init-list
9187 GNU Extension:
9189 condition:
9190 type-specifier-seq declarator asm-specification [opt]
9191 attributes [opt] = assignment-expression
9193 Returns the expression that should be tested. */
9195 static tree
9196 cp_parser_condition (cp_parser* parser)
9198 cp_decl_specifier_seq type_specifiers;
9199 const char *saved_message;
9200 int declares_class_or_enum;
9202 /* Try the declaration first. */
9203 cp_parser_parse_tentatively (parser);
9204 /* New types are not allowed in the type-specifier-seq for a
9205 condition. */
9206 saved_message = parser->type_definition_forbidden_message;
9207 parser->type_definition_forbidden_message
9208 = G_("types may not be defined in conditions");
9209 /* Parse the type-specifier-seq. */
9210 cp_parser_decl_specifier_seq (parser,
9211 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9212 &type_specifiers,
9213 &declares_class_or_enum);
9214 /* Restore the saved message. */
9215 parser->type_definition_forbidden_message = saved_message;
9216 /* If all is well, we might be looking at a declaration. */
9217 if (!cp_parser_error_occurred (parser))
9219 tree decl;
9220 tree asm_specification;
9221 tree attributes;
9222 cp_declarator *declarator;
9223 tree initializer = NULL_TREE;
9225 /* Parse the declarator. */
9226 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9227 /*ctor_dtor_or_conv_p=*/NULL,
9228 /*parenthesized_p=*/NULL,
9229 /*member_p=*/false);
9230 /* Parse the attributes. */
9231 attributes = cp_parser_attributes_opt (parser);
9232 /* Parse the asm-specification. */
9233 asm_specification = cp_parser_asm_specification_opt (parser);
9234 /* If the next token is not an `=' or '{', then we might still be
9235 looking at an expression. For example:
9237 if (A(a).x)
9239 looks like a decl-specifier-seq and a declarator -- but then
9240 there is no `=', so this is an expression. */
9241 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9242 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9243 cp_parser_simulate_error (parser);
9245 /* If we did see an `=' or '{', then we are looking at a declaration
9246 for sure. */
9247 if (cp_parser_parse_definitely (parser))
9249 tree pushed_scope;
9250 bool non_constant_p;
9251 bool flags = LOOKUP_ONLYCONVERTING;
9253 /* Create the declaration. */
9254 decl = start_decl (declarator, &type_specifiers,
9255 /*initialized_p=*/true,
9256 attributes, /*prefix_attributes=*/NULL_TREE,
9257 &pushed_scope);
9259 /* Parse the initializer. */
9260 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9262 initializer = cp_parser_braced_list (parser, &non_constant_p);
9263 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9264 flags = 0;
9266 else
9268 /* Consume the `='. */
9269 cp_parser_require (parser, CPP_EQ, RT_EQ);
9270 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9272 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9273 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9275 /* Process the initializer. */
9276 cp_finish_decl (decl,
9277 initializer, !non_constant_p,
9278 asm_specification,
9279 flags);
9281 if (pushed_scope)
9282 pop_scope (pushed_scope);
9284 return convert_from_reference (decl);
9287 /* If we didn't even get past the declarator successfully, we are
9288 definitely not looking at a declaration. */
9289 else
9290 cp_parser_abort_tentative_parse (parser);
9292 /* Otherwise, we are looking at an expression. */
9293 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9296 /* Parses a for-statement or range-for-statement until the closing ')',
9297 not included. */
9299 static tree
9300 cp_parser_for (cp_parser *parser)
9302 tree init, scope, decl;
9303 bool is_range_for;
9305 /* Begin the for-statement. */
9306 scope = begin_for_scope (&init);
9308 /* Parse the initialization. */
9309 is_range_for = cp_parser_for_init_statement (parser, &decl);
9311 if (is_range_for)
9312 return cp_parser_range_for (parser, scope, init, decl);
9313 else
9314 return cp_parser_c_for (parser, scope, init);
9317 static tree
9318 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9320 /* Normal for loop */
9321 tree condition = NULL_TREE;
9322 tree expression = NULL_TREE;
9323 tree stmt;
9325 stmt = begin_for_stmt (scope, init);
9326 /* The for-init-statement has already been parsed in
9327 cp_parser_for_init_statement, so no work is needed here. */
9328 finish_for_init_stmt (stmt);
9330 /* If there's a condition, process it. */
9331 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9332 condition = cp_parser_condition (parser);
9333 finish_for_cond (condition, stmt);
9334 /* Look for the `;'. */
9335 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9337 /* If there's an expression, process it. */
9338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9339 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9340 finish_for_expr (expression, stmt);
9342 return stmt;
9345 /* Tries to parse a range-based for-statement:
9347 range-based-for:
9348 decl-specifier-seq declarator : expression
9350 The decl-specifier-seq declarator and the `:' are already parsed by
9351 cp_parser_for_init_statement. If processing_template_decl it returns a
9352 newly created RANGE_FOR_STMT; if not, it is converted to a
9353 regular FOR_STMT. */
9355 static tree
9356 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9358 tree stmt, range_expr;
9360 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9362 bool expr_non_constant_p;
9363 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9365 else
9366 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9368 /* If in template, STMT is converted to a normal for-statement
9369 at instantiation. If not, it is done just ahead. */
9370 if (processing_template_decl)
9372 if (check_for_bare_parameter_packs (range_expr))
9373 range_expr = error_mark_node;
9374 stmt = begin_range_for_stmt (scope, init);
9375 finish_range_for_decl (stmt, range_decl, range_expr);
9376 if (!type_dependent_expression_p (range_expr)
9377 /* do_auto_deduction doesn't mess with template init-lists. */
9378 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9379 do_range_for_auto_deduction (range_decl, range_expr);
9381 else
9383 stmt = begin_for_stmt (scope, init);
9384 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9386 return stmt;
9389 /* Subroutine of cp_convert_range_for: given the initializer expression,
9390 builds up the range temporary. */
9392 static tree
9393 build_range_temp (tree range_expr)
9395 tree range_type, range_temp;
9397 /* Find out the type deduced by the declaration
9398 `auto &&__range = range_expr'. */
9399 range_type = cp_build_reference_type (make_auto (), true);
9400 range_type = do_auto_deduction (range_type, range_expr,
9401 type_uses_auto (range_type));
9403 /* Create the __range variable. */
9404 range_temp = build_decl (input_location, VAR_DECL,
9405 get_identifier ("__for_range"), range_type);
9406 TREE_USED (range_temp) = 1;
9407 DECL_ARTIFICIAL (range_temp) = 1;
9409 return range_temp;
9412 /* Used by cp_parser_range_for in template context: we aren't going to
9413 do a full conversion yet, but we still need to resolve auto in the
9414 type of the for-range-declaration if present. This is basically
9415 a shortcut version of cp_convert_range_for. */
9417 static void
9418 do_range_for_auto_deduction (tree decl, tree range_expr)
9420 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9421 if (auto_node)
9423 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9424 range_temp = convert_from_reference (build_range_temp (range_expr));
9425 iter_type = (cp_parser_perform_range_for_lookup
9426 (range_temp, &begin_dummy, &end_dummy));
9427 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9428 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9429 tf_warning_or_error);
9430 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9431 iter_decl, auto_node);
9435 /* Converts a range-based for-statement into a normal
9436 for-statement, as per the definition.
9438 for (RANGE_DECL : RANGE_EXPR)
9439 BLOCK
9441 should be equivalent to:
9444 auto &&__range = RANGE_EXPR;
9445 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9446 __begin != __end;
9447 ++__begin)
9449 RANGE_DECL = *__begin;
9450 BLOCK
9454 If RANGE_EXPR is an array:
9455 BEGIN_EXPR = __range
9456 END_EXPR = __range + ARRAY_SIZE(__range)
9457 Else if RANGE_EXPR has a member 'begin' or 'end':
9458 BEGIN_EXPR = __range.begin()
9459 END_EXPR = __range.end()
9460 Else:
9461 BEGIN_EXPR = begin(__range)
9462 END_EXPR = end(__range);
9464 If __range has a member 'begin' but not 'end', or vice versa, we must
9465 still use the second alternative (it will surely fail, however).
9466 When calling begin()/end() in the third alternative we must use
9467 argument dependent lookup, but always considering 'std' as an associated
9468 namespace. */
9470 tree
9471 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9473 tree begin, end;
9474 tree iter_type, begin_expr, end_expr;
9475 tree condition, expression;
9477 if (range_decl == error_mark_node || range_expr == error_mark_node)
9478 /* If an error happened previously do nothing or else a lot of
9479 unhelpful errors would be issued. */
9480 begin_expr = end_expr = iter_type = error_mark_node;
9481 else
9483 tree range_temp = build_range_temp (range_expr);
9484 pushdecl (range_temp);
9485 cp_finish_decl (range_temp, range_expr,
9486 /*is_constant_init*/false, NULL_TREE,
9487 LOOKUP_ONLYCONVERTING);
9489 range_temp = convert_from_reference (range_temp);
9490 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9491 &begin_expr, &end_expr);
9494 /* The new for initialization statement. */
9495 begin = build_decl (input_location, VAR_DECL,
9496 get_identifier ("__for_begin"), iter_type);
9497 TREE_USED (begin) = 1;
9498 DECL_ARTIFICIAL (begin) = 1;
9499 pushdecl (begin);
9500 cp_finish_decl (begin, begin_expr,
9501 /*is_constant_init*/false, NULL_TREE,
9502 LOOKUP_ONLYCONVERTING);
9504 end = build_decl (input_location, VAR_DECL,
9505 get_identifier ("__for_end"), iter_type);
9506 TREE_USED (end) = 1;
9507 DECL_ARTIFICIAL (end) = 1;
9508 pushdecl (end);
9509 cp_finish_decl (end, end_expr,
9510 /*is_constant_init*/false, NULL_TREE,
9511 LOOKUP_ONLYCONVERTING);
9513 finish_for_init_stmt (statement);
9515 /* The new for condition. */
9516 condition = build_x_binary_op (input_location, NE_EXPR,
9517 begin, ERROR_MARK,
9518 end, ERROR_MARK,
9519 NULL, tf_warning_or_error);
9520 finish_for_cond (condition, statement);
9522 /* The new increment expression. */
9523 expression = finish_unary_op_expr (input_location,
9524 PREINCREMENT_EXPR, begin);
9525 finish_for_expr (expression, statement);
9527 /* The declaration is initialized with *__begin inside the loop body. */
9528 cp_finish_decl (range_decl,
9529 build_x_indirect_ref (input_location, begin, RO_NULL,
9530 tf_warning_or_error),
9531 /*is_constant_init*/false, NULL_TREE,
9532 LOOKUP_ONLYCONVERTING);
9534 return statement;
9537 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9538 We need to solve both at the same time because the method used
9539 depends on the existence of members begin or end.
9540 Returns the type deduced for the iterator expression. */
9542 static tree
9543 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9545 if (error_operand_p (range))
9547 *begin = *end = error_mark_node;
9548 return error_mark_node;
9551 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9553 error ("range-based %<for%> expression of type %qT "
9554 "has incomplete type", TREE_TYPE (range));
9555 *begin = *end = error_mark_node;
9556 return error_mark_node;
9558 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9560 /* If RANGE is an array, we will use pointer arithmetic. */
9561 *begin = range;
9562 *end = build_binary_op (input_location, PLUS_EXPR,
9563 range,
9564 array_type_nelts_top (TREE_TYPE (range)),
9566 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9568 else
9570 /* If it is not an array, we must do a bit of magic. */
9571 tree id_begin, id_end;
9572 tree member_begin, member_end;
9574 *begin = *end = error_mark_node;
9576 id_begin = get_identifier ("begin");
9577 id_end = get_identifier ("end");
9578 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9579 /*protect=*/2, /*want_type=*/false,
9580 tf_warning_or_error);
9581 member_end = lookup_member (TREE_TYPE (range), id_end,
9582 /*protect=*/2, /*want_type=*/false,
9583 tf_warning_or_error);
9585 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9587 /* Use the member functions. */
9588 if (member_begin != NULL_TREE)
9589 *begin = cp_parser_range_for_member_function (range, id_begin);
9590 else
9591 error ("range-based %<for%> expression of type %qT has an "
9592 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9594 if (member_end != NULL_TREE)
9595 *end = cp_parser_range_for_member_function (range, id_end);
9596 else
9597 error ("range-based %<for%> expression of type %qT has a "
9598 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9600 else
9602 /* Use global functions with ADL. */
9603 VEC(tree,gc) *vec;
9604 vec = make_tree_vector ();
9606 VEC_safe_push (tree, gc, vec, range);
9608 member_begin = perform_koenig_lookup (id_begin, vec,
9609 /*include_std=*/true,
9610 tf_warning_or_error);
9611 *begin = finish_call_expr (member_begin, &vec, false, true,
9612 tf_warning_or_error);
9613 member_end = perform_koenig_lookup (id_end, vec,
9614 /*include_std=*/true,
9615 tf_warning_or_error);
9616 *end = finish_call_expr (member_end, &vec, false, true,
9617 tf_warning_or_error);
9619 release_tree_vector (vec);
9622 /* Last common checks. */
9623 if (*begin == error_mark_node || *end == error_mark_node)
9625 /* If one of the expressions is an error do no more checks. */
9626 *begin = *end = error_mark_node;
9627 return error_mark_node;
9629 else
9631 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9632 /* The unqualified type of the __begin and __end temporaries should
9633 be the same, as required by the multiple auto declaration. */
9634 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9635 error ("inconsistent begin/end types in range-based %<for%> "
9636 "statement: %qT and %qT",
9637 TREE_TYPE (*begin), TREE_TYPE (*end));
9638 return iter_type;
9643 /* Helper function for cp_parser_perform_range_for_lookup.
9644 Builds a tree for RANGE.IDENTIFIER(). */
9646 static tree
9647 cp_parser_range_for_member_function (tree range, tree identifier)
9649 tree member, res;
9650 VEC(tree,gc) *vec;
9652 member = finish_class_member_access_expr (range, identifier,
9653 false, tf_warning_or_error);
9654 if (member == error_mark_node)
9655 return error_mark_node;
9657 vec = make_tree_vector ();
9658 res = finish_call_expr (member, &vec,
9659 /*disallow_virtual=*/false,
9660 /*koenig_p=*/false,
9661 tf_warning_or_error);
9662 release_tree_vector (vec);
9663 return res;
9666 /* Parse an iteration-statement.
9668 iteration-statement:
9669 while ( condition ) statement
9670 do statement while ( expression ) ;
9671 for ( for-init-statement condition [opt] ; expression [opt] )
9672 statement
9674 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9676 static tree
9677 cp_parser_iteration_statement (cp_parser* parser)
9679 cp_token *token;
9680 enum rid keyword;
9681 tree statement;
9682 unsigned char in_statement;
9684 /* Peek at the next token. */
9685 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9686 if (!token)
9687 return error_mark_node;
9689 /* Remember whether or not we are already within an iteration
9690 statement. */
9691 in_statement = parser->in_statement;
9693 /* See what kind of keyword it is. */
9694 keyword = token->keyword;
9695 switch (keyword)
9697 case RID_WHILE:
9699 tree condition;
9701 /* Begin the while-statement. */
9702 statement = begin_while_stmt ();
9703 /* Look for the `('. */
9704 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9705 /* Parse the condition. */
9706 condition = cp_parser_condition (parser);
9707 finish_while_stmt_cond (condition, statement);
9708 /* Look for the `)'. */
9709 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9710 /* Parse the dependent statement. */
9711 parser->in_statement = IN_ITERATION_STMT;
9712 cp_parser_already_scoped_statement (parser);
9713 parser->in_statement = in_statement;
9714 /* We're done with the while-statement. */
9715 finish_while_stmt (statement);
9717 break;
9719 case RID_DO:
9721 tree expression;
9723 /* Begin the do-statement. */
9724 statement = begin_do_stmt ();
9725 /* Parse the body of the do-statement. */
9726 parser->in_statement = IN_ITERATION_STMT;
9727 cp_parser_implicitly_scoped_statement (parser, NULL);
9728 parser->in_statement = in_statement;
9729 finish_do_body (statement);
9730 /* Look for the `while' keyword. */
9731 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9732 /* Look for the `('. */
9733 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9734 /* Parse the expression. */
9735 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9736 /* We're done with the do-statement. */
9737 finish_do_stmt (expression, statement);
9738 /* Look for the `)'. */
9739 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9740 /* Look for the `;'. */
9741 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9743 break;
9745 case RID_FOR:
9747 /* Look for the `('. */
9748 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9750 statement = cp_parser_for (parser);
9752 /* Look for the `)'. */
9753 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9755 /* Parse the body of the for-statement. */
9756 parser->in_statement = IN_ITERATION_STMT;
9757 cp_parser_already_scoped_statement (parser);
9758 parser->in_statement = in_statement;
9760 /* We're done with the for-statement. */
9761 finish_for_stmt (statement);
9763 break;
9765 default:
9766 cp_parser_error (parser, "expected iteration-statement");
9767 statement = error_mark_node;
9768 break;
9771 return statement;
9774 /* Parse a for-init-statement or the declarator of a range-based-for.
9775 Returns true if a range-based-for declaration is seen.
9777 for-init-statement:
9778 expression-statement
9779 simple-declaration */
9781 static bool
9782 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9784 /* If the next token is a `;', then we have an empty
9785 expression-statement. Grammatically, this is also a
9786 simple-declaration, but an invalid one, because it does not
9787 declare anything. Therefore, if we did not handle this case
9788 specially, we would issue an error message about an invalid
9789 declaration. */
9790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9792 bool is_range_for = false;
9793 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9795 parser->colon_corrects_to_scope_p = false;
9797 /* We're going to speculatively look for a declaration, falling back
9798 to an expression, if necessary. */
9799 cp_parser_parse_tentatively (parser);
9800 /* Parse the declaration. */
9801 cp_parser_simple_declaration (parser,
9802 /*function_definition_allowed_p=*/false,
9803 decl);
9804 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9805 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9807 /* It is a range-for, consume the ':' */
9808 cp_lexer_consume_token (parser->lexer);
9809 is_range_for = true;
9810 if (cxx_dialect < cxx0x)
9812 error_at (cp_lexer_peek_token (parser->lexer)->location,
9813 "range-based %<for%> loops are not allowed "
9814 "in C++98 mode");
9815 *decl = error_mark_node;
9818 else
9819 /* The ';' is not consumed yet because we told
9820 cp_parser_simple_declaration not to. */
9821 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9823 if (cp_parser_parse_definitely (parser))
9824 return is_range_for;
9825 /* If the tentative parse failed, then we shall need to look for an
9826 expression-statement. */
9828 /* If we are here, it is an expression-statement. */
9829 cp_parser_expression_statement (parser, NULL_TREE);
9830 return false;
9833 /* Parse a jump-statement.
9835 jump-statement:
9836 break ;
9837 continue ;
9838 return expression [opt] ;
9839 return braced-init-list ;
9840 goto identifier ;
9842 GNU extension:
9844 jump-statement:
9845 goto * expression ;
9847 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9849 static tree
9850 cp_parser_jump_statement (cp_parser* parser)
9852 tree statement = error_mark_node;
9853 cp_token *token;
9854 enum rid keyword;
9855 unsigned char in_statement;
9857 /* Peek at the next token. */
9858 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9859 if (!token)
9860 return error_mark_node;
9862 /* See what kind of keyword it is. */
9863 keyword = token->keyword;
9864 switch (keyword)
9866 case RID_BREAK:
9867 in_statement = parser->in_statement & ~IN_IF_STMT;
9868 switch (in_statement)
9870 case 0:
9871 error_at (token->location, "break statement not within loop or switch");
9872 break;
9873 default:
9874 gcc_assert ((in_statement & IN_SWITCH_STMT)
9875 || in_statement == IN_ITERATION_STMT);
9876 statement = finish_break_stmt ();
9877 break;
9878 case IN_OMP_BLOCK:
9879 error_at (token->location, "invalid exit from OpenMP structured block");
9880 break;
9881 case IN_OMP_FOR:
9882 error_at (token->location, "break statement used with OpenMP for loop");
9883 break;
9885 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9886 break;
9888 case RID_CONTINUE:
9889 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9891 case 0:
9892 error_at (token->location, "continue statement not within a loop");
9893 break;
9894 case IN_ITERATION_STMT:
9895 case IN_OMP_FOR:
9896 statement = finish_continue_stmt ();
9897 break;
9898 case IN_OMP_BLOCK:
9899 error_at (token->location, "invalid exit from OpenMP structured block");
9900 break;
9901 default:
9902 gcc_unreachable ();
9904 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9905 break;
9907 case RID_RETURN:
9909 tree expr;
9910 bool expr_non_constant_p;
9912 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9914 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9915 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9917 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9918 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9919 else
9920 /* If the next token is a `;', then there is no
9921 expression. */
9922 expr = NULL_TREE;
9923 /* Build the return-statement. */
9924 statement = finish_return_stmt (expr);
9925 /* Look for the final `;'. */
9926 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9928 break;
9930 case RID_GOTO:
9931 /* Create the goto-statement. */
9932 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9934 /* Issue a warning about this use of a GNU extension. */
9935 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
9936 /* Consume the '*' token. */
9937 cp_lexer_consume_token (parser->lexer);
9938 /* Parse the dependent expression. */
9939 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9941 else
9942 finish_goto_stmt (cp_parser_identifier (parser));
9943 /* Look for the final `;'. */
9944 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9945 break;
9947 default:
9948 cp_parser_error (parser, "expected jump-statement");
9949 break;
9952 return statement;
9955 /* Parse a declaration-statement.
9957 declaration-statement:
9958 block-declaration */
9960 static void
9961 cp_parser_declaration_statement (cp_parser* parser)
9963 void *p;
9965 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9966 p = obstack_alloc (&declarator_obstack, 0);
9968 /* Parse the block-declaration. */
9969 cp_parser_block_declaration (parser, /*statement_p=*/true);
9971 /* Free any declarators allocated. */
9972 obstack_free (&declarator_obstack, p);
9974 /* Finish off the statement. */
9975 finish_stmt ();
9978 /* Some dependent statements (like `if (cond) statement'), are
9979 implicitly in their own scope. In other words, if the statement is
9980 a single statement (as opposed to a compound-statement), it is
9981 none-the-less treated as if it were enclosed in braces. Any
9982 declarations appearing in the dependent statement are out of scope
9983 after control passes that point. This function parses a statement,
9984 but ensures that is in its own scope, even if it is not a
9985 compound-statement.
9987 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9988 is a (possibly labeled) if statement which is not enclosed in
9989 braces and has an else clause. This is used to implement
9990 -Wparentheses.
9992 Returns the new statement. */
9994 static tree
9995 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9997 tree statement;
9999 if (if_p != NULL)
10000 *if_p = false;
10002 /* Mark if () ; with a special NOP_EXPR. */
10003 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10005 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10006 cp_lexer_consume_token (parser->lexer);
10007 statement = add_stmt (build_empty_stmt (loc));
10009 /* if a compound is opened, we simply parse the statement directly. */
10010 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10011 statement = cp_parser_compound_statement (parser, NULL, false, false);
10012 /* If the token is not a `{', then we must take special action. */
10013 else
10015 /* Create a compound-statement. */
10016 statement = begin_compound_stmt (0);
10017 /* Parse the dependent-statement. */
10018 cp_parser_statement (parser, NULL_TREE, false, if_p);
10019 /* Finish the dummy compound-statement. */
10020 finish_compound_stmt (statement);
10023 /* Return the statement. */
10024 return statement;
10027 /* For some dependent statements (like `while (cond) statement'), we
10028 have already created a scope. Therefore, even if the dependent
10029 statement is a compound-statement, we do not want to create another
10030 scope. */
10032 static void
10033 cp_parser_already_scoped_statement (cp_parser* parser)
10035 /* If the token is a `{', then we must take special action. */
10036 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10037 cp_parser_statement (parser, NULL_TREE, false, NULL);
10038 else
10040 /* Avoid calling cp_parser_compound_statement, so that we
10041 don't create a new scope. Do everything else by hand. */
10042 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10043 /* If the next keyword is `__label__' we have a label declaration. */
10044 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10045 cp_parser_label_declaration (parser);
10046 /* Parse an (optional) statement-seq. */
10047 cp_parser_statement_seq_opt (parser, NULL_TREE);
10048 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10052 /* Declarations [gram.dcl.dcl] */
10054 /* Parse an optional declaration-sequence.
10056 declaration-seq:
10057 declaration
10058 declaration-seq declaration */
10060 static void
10061 cp_parser_declaration_seq_opt (cp_parser* parser)
10063 while (true)
10065 cp_token *token;
10067 token = cp_lexer_peek_token (parser->lexer);
10069 if (token->type == CPP_CLOSE_BRACE
10070 || token->type == CPP_EOF
10071 || token->type == CPP_PRAGMA_EOL)
10072 break;
10074 if (token->type == CPP_SEMICOLON)
10076 /* A declaration consisting of a single semicolon is
10077 invalid. Allow it unless we're being pedantic. */
10078 cp_lexer_consume_token (parser->lexer);
10079 if (!in_system_header)
10080 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10081 continue;
10084 /* If we're entering or exiting a region that's implicitly
10085 extern "C", modify the lang context appropriately. */
10086 if (!parser->implicit_extern_c && token->implicit_extern_c)
10088 push_lang_context (lang_name_c);
10089 parser->implicit_extern_c = true;
10091 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10093 pop_lang_context ();
10094 parser->implicit_extern_c = false;
10097 if (token->type == CPP_PRAGMA)
10099 /* A top-level declaration can consist solely of a #pragma.
10100 A nested declaration cannot, so this is done here and not
10101 in cp_parser_declaration. (A #pragma at block scope is
10102 handled in cp_parser_statement.) */
10103 cp_parser_pragma (parser, pragma_external);
10104 continue;
10107 /* Parse the declaration itself. */
10108 cp_parser_declaration (parser);
10112 /* Parse a declaration.
10114 declaration:
10115 block-declaration
10116 function-definition
10117 template-declaration
10118 explicit-instantiation
10119 explicit-specialization
10120 linkage-specification
10121 namespace-definition
10123 GNU extension:
10125 declaration:
10126 __extension__ declaration */
10128 static void
10129 cp_parser_declaration (cp_parser* parser)
10131 cp_token token1;
10132 cp_token token2;
10133 int saved_pedantic;
10134 void *p;
10135 tree attributes = NULL_TREE;
10137 /* Check for the `__extension__' keyword. */
10138 if (cp_parser_extension_opt (parser, &saved_pedantic))
10140 /* Parse the qualified declaration. */
10141 cp_parser_declaration (parser);
10142 /* Restore the PEDANTIC flag. */
10143 pedantic = saved_pedantic;
10145 return;
10148 /* Try to figure out what kind of declaration is present. */
10149 token1 = *cp_lexer_peek_token (parser->lexer);
10151 if (token1.type != CPP_EOF)
10152 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10153 else
10155 token2.type = CPP_EOF;
10156 token2.keyword = RID_MAX;
10159 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10160 p = obstack_alloc (&declarator_obstack, 0);
10162 /* If the next token is `extern' and the following token is a string
10163 literal, then we have a linkage specification. */
10164 if (token1.keyword == RID_EXTERN
10165 && cp_parser_is_pure_string_literal (&token2))
10166 cp_parser_linkage_specification (parser);
10167 /* If the next token is `template', then we have either a template
10168 declaration, an explicit instantiation, or an explicit
10169 specialization. */
10170 else if (token1.keyword == RID_TEMPLATE)
10172 /* `template <>' indicates a template specialization. */
10173 if (token2.type == CPP_LESS
10174 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10175 cp_parser_explicit_specialization (parser);
10176 /* `template <' indicates a template declaration. */
10177 else if (token2.type == CPP_LESS)
10178 cp_parser_template_declaration (parser, /*member_p=*/false);
10179 /* Anything else must be an explicit instantiation. */
10180 else
10181 cp_parser_explicit_instantiation (parser);
10183 /* If the next token is `export', then we have a template
10184 declaration. */
10185 else if (token1.keyword == RID_EXPORT)
10186 cp_parser_template_declaration (parser, /*member_p=*/false);
10187 /* If the next token is `extern', 'static' or 'inline' and the one
10188 after that is `template', we have a GNU extended explicit
10189 instantiation directive. */
10190 else if (cp_parser_allow_gnu_extensions_p (parser)
10191 && (token1.keyword == RID_EXTERN
10192 || token1.keyword == RID_STATIC
10193 || token1.keyword == RID_INLINE)
10194 && token2.keyword == RID_TEMPLATE)
10195 cp_parser_explicit_instantiation (parser);
10196 /* If the next token is `namespace', check for a named or unnamed
10197 namespace definition. */
10198 else if (token1.keyword == RID_NAMESPACE
10199 && (/* A named namespace definition. */
10200 (token2.type == CPP_NAME
10201 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10202 != CPP_EQ))
10203 /* An unnamed namespace definition. */
10204 || token2.type == CPP_OPEN_BRACE
10205 || token2.keyword == RID_ATTRIBUTE))
10206 cp_parser_namespace_definition (parser);
10207 /* An inline (associated) namespace definition. */
10208 else if (token1.keyword == RID_INLINE
10209 && token2.keyword == RID_NAMESPACE)
10210 cp_parser_namespace_definition (parser);
10211 /* Objective-C++ declaration/definition. */
10212 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10213 cp_parser_objc_declaration (parser, NULL_TREE);
10214 else if (c_dialect_objc ()
10215 && token1.keyword == RID_ATTRIBUTE
10216 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10217 cp_parser_objc_declaration (parser, attributes);
10218 /* We must have either a block declaration or a function
10219 definition. */
10220 else
10221 /* Try to parse a block-declaration, or a function-definition. */
10222 cp_parser_block_declaration (parser, /*statement_p=*/false);
10224 /* Free any declarators allocated. */
10225 obstack_free (&declarator_obstack, p);
10228 /* Parse a block-declaration.
10230 block-declaration:
10231 simple-declaration
10232 asm-definition
10233 namespace-alias-definition
10234 using-declaration
10235 using-directive
10237 GNU Extension:
10239 block-declaration:
10240 __extension__ block-declaration
10242 C++0x Extension:
10244 block-declaration:
10245 static_assert-declaration
10247 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10248 part of a declaration-statement. */
10250 static void
10251 cp_parser_block_declaration (cp_parser *parser,
10252 bool statement_p)
10254 cp_token *token1;
10255 int saved_pedantic;
10257 /* Check for the `__extension__' keyword. */
10258 if (cp_parser_extension_opt (parser, &saved_pedantic))
10260 /* Parse the qualified declaration. */
10261 cp_parser_block_declaration (parser, statement_p);
10262 /* Restore the PEDANTIC flag. */
10263 pedantic = saved_pedantic;
10265 return;
10268 /* Peek at the next token to figure out which kind of declaration is
10269 present. */
10270 token1 = cp_lexer_peek_token (parser->lexer);
10272 /* If the next keyword is `asm', we have an asm-definition. */
10273 if (token1->keyword == RID_ASM)
10275 if (statement_p)
10276 cp_parser_commit_to_tentative_parse (parser);
10277 cp_parser_asm_definition (parser);
10279 /* If the next keyword is `namespace', we have a
10280 namespace-alias-definition. */
10281 else if (token1->keyword == RID_NAMESPACE)
10282 cp_parser_namespace_alias_definition (parser);
10283 /* If the next keyword is `using', we have a
10284 using-declaration, a using-directive, or an alias-declaration. */
10285 else if (token1->keyword == RID_USING)
10287 cp_token *token2;
10289 if (statement_p)
10290 cp_parser_commit_to_tentative_parse (parser);
10291 /* If the token after `using' is `namespace', then we have a
10292 using-directive. */
10293 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10294 if (token2->keyword == RID_NAMESPACE)
10295 cp_parser_using_directive (parser);
10296 /* If the second token after 'using' is '=', then we have an
10297 alias-declaration. */
10298 else if (cxx_dialect >= cxx0x
10299 && token2->type == CPP_NAME
10300 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10301 || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10302 == RID_ATTRIBUTE)))
10303 cp_parser_alias_declaration (parser);
10304 /* Otherwise, it's a using-declaration. */
10305 else
10306 cp_parser_using_declaration (parser,
10307 /*access_declaration_p=*/false);
10309 /* If the next keyword is `__label__' we have a misplaced label
10310 declaration. */
10311 else if (token1->keyword == RID_LABEL)
10313 cp_lexer_consume_token (parser->lexer);
10314 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10315 cp_parser_skip_to_end_of_statement (parser);
10316 /* If the next token is now a `;', consume it. */
10317 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10318 cp_lexer_consume_token (parser->lexer);
10320 /* If the next token is `static_assert' we have a static assertion. */
10321 else if (token1->keyword == RID_STATIC_ASSERT)
10322 cp_parser_static_assert (parser, /*member_p=*/false);
10323 /* Anything else must be a simple-declaration. */
10324 else
10325 cp_parser_simple_declaration (parser, !statement_p,
10326 /*maybe_range_for_decl*/NULL);
10329 /* Parse a simple-declaration.
10331 simple-declaration:
10332 decl-specifier-seq [opt] init-declarator-list [opt] ;
10334 init-declarator-list:
10335 init-declarator
10336 init-declarator-list , init-declarator
10338 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10339 function-definition as a simple-declaration.
10341 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10342 parsed declaration if it is an uninitialized single declarator not followed
10343 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10344 if present, will not be consumed. */
10346 static void
10347 cp_parser_simple_declaration (cp_parser* parser,
10348 bool function_definition_allowed_p,
10349 tree *maybe_range_for_decl)
10351 cp_decl_specifier_seq decl_specifiers;
10352 int declares_class_or_enum;
10353 bool saw_declarator;
10355 if (maybe_range_for_decl)
10356 *maybe_range_for_decl = NULL_TREE;
10358 /* Defer access checks until we know what is being declared; the
10359 checks for names appearing in the decl-specifier-seq should be
10360 done as if we were in the scope of the thing being declared. */
10361 push_deferring_access_checks (dk_deferred);
10363 /* Parse the decl-specifier-seq. We have to keep track of whether
10364 or not the decl-specifier-seq declares a named class or
10365 enumeration type, since that is the only case in which the
10366 init-declarator-list is allowed to be empty.
10368 [dcl.dcl]
10370 In a simple-declaration, the optional init-declarator-list can be
10371 omitted only when declaring a class or enumeration, that is when
10372 the decl-specifier-seq contains either a class-specifier, an
10373 elaborated-type-specifier, or an enum-specifier. */
10374 cp_parser_decl_specifier_seq (parser,
10375 CP_PARSER_FLAGS_OPTIONAL,
10376 &decl_specifiers,
10377 &declares_class_or_enum);
10378 /* We no longer need to defer access checks. */
10379 stop_deferring_access_checks ();
10381 /* In a block scope, a valid declaration must always have a
10382 decl-specifier-seq. By not trying to parse declarators, we can
10383 resolve the declaration/expression ambiguity more quickly. */
10384 if (!function_definition_allowed_p
10385 && !decl_specifiers.any_specifiers_p)
10387 cp_parser_error (parser, "expected declaration");
10388 goto done;
10391 /* If the next two tokens are both identifiers, the code is
10392 erroneous. The usual cause of this situation is code like:
10394 T t;
10396 where "T" should name a type -- but does not. */
10397 if (!decl_specifiers.any_type_specifiers_p
10398 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10400 /* If parsing tentatively, we should commit; we really are
10401 looking at a declaration. */
10402 cp_parser_commit_to_tentative_parse (parser);
10403 /* Give up. */
10404 goto done;
10407 /* If we have seen at least one decl-specifier, and the next token
10408 is not a parenthesis, then we must be looking at a declaration.
10409 (After "int (" we might be looking at a functional cast.) */
10410 if (decl_specifiers.any_specifiers_p
10411 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10412 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10413 && !cp_parser_error_occurred (parser))
10414 cp_parser_commit_to_tentative_parse (parser);
10416 /* Keep going until we hit the `;' at the end of the simple
10417 declaration. */
10418 saw_declarator = false;
10419 while (cp_lexer_next_token_is_not (parser->lexer,
10420 CPP_SEMICOLON))
10422 cp_token *token;
10423 bool function_definition_p;
10424 tree decl;
10426 if (saw_declarator)
10428 /* If we are processing next declarator, coma is expected */
10429 token = cp_lexer_peek_token (parser->lexer);
10430 gcc_assert (token->type == CPP_COMMA);
10431 cp_lexer_consume_token (parser->lexer);
10432 if (maybe_range_for_decl)
10433 *maybe_range_for_decl = error_mark_node;
10435 else
10436 saw_declarator = true;
10438 /* Parse the init-declarator. */
10439 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10440 /*checks=*/NULL,
10441 function_definition_allowed_p,
10442 /*member_p=*/false,
10443 declares_class_or_enum,
10444 &function_definition_p,
10445 maybe_range_for_decl);
10446 /* If an error occurred while parsing tentatively, exit quickly.
10447 (That usually happens when in the body of a function; each
10448 statement is treated as a declaration-statement until proven
10449 otherwise.) */
10450 if (cp_parser_error_occurred (parser))
10451 goto done;
10452 /* Handle function definitions specially. */
10453 if (function_definition_p)
10455 /* If the next token is a `,', then we are probably
10456 processing something like:
10458 void f() {}, *p;
10460 which is erroneous. */
10461 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10463 cp_token *token = cp_lexer_peek_token (parser->lexer);
10464 error_at (token->location,
10465 "mixing"
10466 " declarations and function-definitions is forbidden");
10468 /* Otherwise, we're done with the list of declarators. */
10469 else
10471 pop_deferring_access_checks ();
10472 return;
10475 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10476 *maybe_range_for_decl = decl;
10477 /* The next token should be either a `,' or a `;'. */
10478 token = cp_lexer_peek_token (parser->lexer);
10479 /* If it's a `,', there are more declarators to come. */
10480 if (token->type == CPP_COMMA)
10481 /* will be consumed next time around */;
10482 /* If it's a `;', we are done. */
10483 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10484 break;
10485 /* Anything else is an error. */
10486 else
10488 /* If we have already issued an error message we don't need
10489 to issue another one. */
10490 if (decl != error_mark_node
10491 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10492 cp_parser_error (parser, "expected %<,%> or %<;%>");
10493 /* Skip tokens until we reach the end of the statement. */
10494 cp_parser_skip_to_end_of_statement (parser);
10495 /* If the next token is now a `;', consume it. */
10496 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10497 cp_lexer_consume_token (parser->lexer);
10498 goto done;
10500 /* After the first time around, a function-definition is not
10501 allowed -- even if it was OK at first. For example:
10503 int i, f() {}
10505 is not valid. */
10506 function_definition_allowed_p = false;
10509 /* Issue an error message if no declarators are present, and the
10510 decl-specifier-seq does not itself declare a class or
10511 enumeration. */
10512 if (!saw_declarator)
10514 if (cp_parser_declares_only_class_p (parser))
10515 shadow_tag (&decl_specifiers);
10516 /* Perform any deferred access checks. */
10517 perform_deferred_access_checks (tf_warning_or_error);
10520 /* Consume the `;'. */
10521 if (!maybe_range_for_decl)
10522 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10524 done:
10525 pop_deferring_access_checks ();
10528 /* Parse a decl-specifier-seq.
10530 decl-specifier-seq:
10531 decl-specifier-seq [opt] decl-specifier
10533 decl-specifier:
10534 storage-class-specifier
10535 type-specifier
10536 function-specifier
10537 friend
10538 typedef
10540 GNU Extension:
10542 decl-specifier:
10543 attributes
10545 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10547 The parser flags FLAGS is used to control type-specifier parsing.
10549 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10550 flags:
10552 1: one of the decl-specifiers is an elaborated-type-specifier
10553 (i.e., a type declaration)
10554 2: one of the decl-specifiers is an enum-specifier or a
10555 class-specifier (i.e., a type definition)
10559 static void
10560 cp_parser_decl_specifier_seq (cp_parser* parser,
10561 cp_parser_flags flags,
10562 cp_decl_specifier_seq *decl_specs,
10563 int* declares_class_or_enum)
10565 bool constructor_possible_p = !parser->in_declarator_p;
10566 cp_token *start_token = NULL;
10567 cp_decl_spec ds;
10569 /* Clear DECL_SPECS. */
10570 clear_decl_specs (decl_specs);
10572 /* Assume no class or enumeration type is declared. */
10573 *declares_class_or_enum = 0;
10575 /* Keep reading specifiers until there are no more to read. */
10576 while (true)
10578 bool constructor_p;
10579 bool found_decl_spec;
10580 cp_token *token;
10581 ds = ds_last;
10583 /* Peek at the next token. */
10584 token = cp_lexer_peek_token (parser->lexer);
10586 /* Save the first token of the decl spec list for error
10587 reporting. */
10588 if (!start_token)
10589 start_token = token;
10590 /* Handle attributes. */
10591 if (token->keyword == RID_ATTRIBUTE)
10593 /* Parse the attributes. */
10594 decl_specs->attributes
10595 = chainon (decl_specs->attributes,
10596 cp_parser_attributes_opt (parser));
10597 if (decl_specs->locations[ds_attribute] == 0)
10598 decl_specs->locations[ds_attribute] = token->location;
10599 continue;
10601 /* Assume we will find a decl-specifier keyword. */
10602 found_decl_spec = true;
10603 /* If the next token is an appropriate keyword, we can simply
10604 add it to the list. */
10605 switch (token->keyword)
10607 /* decl-specifier:
10608 friend
10609 constexpr */
10610 case RID_FRIEND:
10611 if (!at_class_scope_p ())
10613 error_at (token->location, "%<friend%> used outside of class");
10614 cp_lexer_purge_token (parser->lexer);
10616 else
10618 ds = ds_friend;
10619 /* Consume the token. */
10620 cp_lexer_consume_token (parser->lexer);
10622 break;
10624 case RID_CONSTEXPR:
10625 ds = ds_constexpr;
10626 cp_lexer_consume_token (parser->lexer);
10627 break;
10629 /* function-specifier:
10630 inline
10631 virtual
10632 explicit */
10633 case RID_INLINE:
10634 case RID_VIRTUAL:
10635 case RID_EXPLICIT:
10636 cp_parser_function_specifier_opt (parser, decl_specs);
10637 break;
10639 /* decl-specifier:
10640 typedef */
10641 case RID_TYPEDEF:
10642 ds = ds_typedef;
10643 /* Consume the token. */
10644 cp_lexer_consume_token (parser->lexer);
10645 /* A constructor declarator cannot appear in a typedef. */
10646 constructor_possible_p = false;
10647 /* The "typedef" keyword can only occur in a declaration; we
10648 may as well commit at this point. */
10649 cp_parser_commit_to_tentative_parse (parser);
10651 if (decl_specs->storage_class != sc_none)
10652 decl_specs->conflicting_specifiers_p = true;
10653 break;
10655 /* storage-class-specifier:
10656 auto
10657 register
10658 static
10659 extern
10660 mutable
10662 GNU Extension:
10663 thread */
10664 case RID_AUTO:
10665 if (cxx_dialect == cxx98)
10667 /* Consume the token. */
10668 cp_lexer_consume_token (parser->lexer);
10670 /* Complain about `auto' as a storage specifier, if
10671 we're complaining about C++0x compatibility. */
10672 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10673 " changes meaning in C++11; please remove it");
10675 /* Set the storage class anyway. */
10676 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10677 token->location);
10679 else
10680 /* C++0x auto type-specifier. */
10681 found_decl_spec = false;
10682 break;
10684 case RID_REGISTER:
10685 case RID_STATIC:
10686 case RID_EXTERN:
10687 case RID_MUTABLE:
10688 /* Consume the token. */
10689 cp_lexer_consume_token (parser->lexer);
10690 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10691 token->location);
10692 break;
10693 case RID_THREAD:
10694 /* Consume the token. */
10695 ds = ds_thread;
10696 cp_lexer_consume_token (parser->lexer);
10697 break;
10699 default:
10700 /* We did not yet find a decl-specifier yet. */
10701 found_decl_spec = false;
10702 break;
10705 if (found_decl_spec
10706 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10707 && token->keyword != RID_CONSTEXPR)
10708 error ("decl-specifier invalid in condition");
10710 if (ds != ds_last)
10711 set_and_check_decl_spec_loc (decl_specs, ds, token->location);
10713 /* Constructors are a special case. The `S' in `S()' is not a
10714 decl-specifier; it is the beginning of the declarator. */
10715 constructor_p
10716 = (!found_decl_spec
10717 && constructor_possible_p
10718 && (cp_parser_constructor_declarator_p
10719 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
10721 /* If we don't have a DECL_SPEC yet, then we must be looking at
10722 a type-specifier. */
10723 if (!found_decl_spec && !constructor_p)
10725 int decl_spec_declares_class_or_enum;
10726 bool is_cv_qualifier;
10727 tree type_spec;
10729 type_spec
10730 = cp_parser_type_specifier (parser, flags,
10731 decl_specs,
10732 /*is_declaration=*/true,
10733 &decl_spec_declares_class_or_enum,
10734 &is_cv_qualifier);
10735 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10737 /* If this type-specifier referenced a user-defined type
10738 (a typedef, class-name, etc.), then we can't allow any
10739 more such type-specifiers henceforth.
10741 [dcl.spec]
10743 The longest sequence of decl-specifiers that could
10744 possibly be a type name is taken as the
10745 decl-specifier-seq of a declaration. The sequence shall
10746 be self-consistent as described below.
10748 [dcl.type]
10750 As a general rule, at most one type-specifier is allowed
10751 in the complete decl-specifier-seq of a declaration. The
10752 only exceptions are the following:
10754 -- const or volatile can be combined with any other
10755 type-specifier.
10757 -- signed or unsigned can be combined with char, long,
10758 short, or int.
10760 -- ..
10762 Example:
10764 typedef char* Pc;
10765 void g (const int Pc);
10767 Here, Pc is *not* part of the decl-specifier seq; it's
10768 the declarator. Therefore, once we see a type-specifier
10769 (other than a cv-qualifier), we forbid any additional
10770 user-defined types. We *do* still allow things like `int
10771 int' to be considered a decl-specifier-seq, and issue the
10772 error message later. */
10773 if (type_spec && !is_cv_qualifier)
10774 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10775 /* A constructor declarator cannot follow a type-specifier. */
10776 if (type_spec)
10778 constructor_possible_p = false;
10779 found_decl_spec = true;
10780 if (!is_cv_qualifier)
10781 decl_specs->any_type_specifiers_p = true;
10785 /* If we still do not have a DECL_SPEC, then there are no more
10786 decl-specifiers. */
10787 if (!found_decl_spec)
10788 break;
10790 decl_specs->any_specifiers_p = true;
10791 /* After we see one decl-specifier, further decl-specifiers are
10792 always optional. */
10793 flags |= CP_PARSER_FLAGS_OPTIONAL;
10796 /* Don't allow a friend specifier with a class definition. */
10797 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
10798 && (*declares_class_or_enum & 2))
10799 error_at (decl_specs->locations[ds_friend],
10800 "class definition may not be declared a friend");
10803 /* Parse an (optional) storage-class-specifier.
10805 storage-class-specifier:
10806 auto
10807 register
10808 static
10809 extern
10810 mutable
10812 GNU Extension:
10814 storage-class-specifier:
10815 thread
10817 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10819 static tree
10820 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10822 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10824 case RID_AUTO:
10825 if (cxx_dialect != cxx98)
10826 return NULL_TREE;
10827 /* Fall through for C++98. */
10829 case RID_REGISTER:
10830 case RID_STATIC:
10831 case RID_EXTERN:
10832 case RID_MUTABLE:
10833 case RID_THREAD:
10834 /* Consume the token. */
10835 return cp_lexer_consume_token (parser->lexer)->u.value;
10837 default:
10838 return NULL_TREE;
10842 /* Parse an (optional) function-specifier.
10844 function-specifier:
10845 inline
10846 virtual
10847 explicit
10849 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10850 Updates DECL_SPECS, if it is non-NULL. */
10852 static tree
10853 cp_parser_function_specifier_opt (cp_parser* parser,
10854 cp_decl_specifier_seq *decl_specs)
10856 cp_token *token = cp_lexer_peek_token (parser->lexer);
10857 switch (token->keyword)
10859 case RID_INLINE:
10860 set_and_check_decl_spec_loc (decl_specs, ds_inline, token->location);
10861 break;
10863 case RID_VIRTUAL:
10864 /* 14.5.2.3 [temp.mem]
10866 A member function template shall not be virtual. */
10867 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10868 error_at (token->location, "templates may not be %<virtual%>");
10869 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token->location);
10870 break;
10872 case RID_EXPLICIT:
10873 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token->location);
10874 break;
10876 default:
10877 return NULL_TREE;
10880 /* Consume the token. */
10881 return cp_lexer_consume_token (parser->lexer)->u.value;
10884 /* Parse a linkage-specification.
10886 linkage-specification:
10887 extern string-literal { declaration-seq [opt] }
10888 extern string-literal declaration */
10890 static void
10891 cp_parser_linkage_specification (cp_parser* parser)
10893 tree linkage;
10895 /* Look for the `extern' keyword. */
10896 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10898 /* Look for the string-literal. */
10899 linkage = cp_parser_string_literal (parser, false, false);
10901 /* Transform the literal into an identifier. If the literal is a
10902 wide-character string, or contains embedded NULs, then we can't
10903 handle it as the user wants. */
10904 if (strlen (TREE_STRING_POINTER (linkage))
10905 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10907 cp_parser_error (parser, "invalid linkage-specification");
10908 /* Assume C++ linkage. */
10909 linkage = lang_name_cplusplus;
10911 else
10912 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10914 /* We're now using the new linkage. */
10915 push_lang_context (linkage);
10917 /* If the next token is a `{', then we're using the first
10918 production. */
10919 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10921 /* Consume the `{' token. */
10922 cp_lexer_consume_token (parser->lexer);
10923 /* Parse the declarations. */
10924 cp_parser_declaration_seq_opt (parser);
10925 /* Look for the closing `}'. */
10926 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10928 /* Otherwise, there's just one declaration. */
10929 else
10931 bool saved_in_unbraced_linkage_specification_p;
10933 saved_in_unbraced_linkage_specification_p
10934 = parser->in_unbraced_linkage_specification_p;
10935 parser->in_unbraced_linkage_specification_p = true;
10936 cp_parser_declaration (parser);
10937 parser->in_unbraced_linkage_specification_p
10938 = saved_in_unbraced_linkage_specification_p;
10941 /* We're done with the linkage-specification. */
10942 pop_lang_context ();
10945 /* Parse a static_assert-declaration.
10947 static_assert-declaration:
10948 static_assert ( constant-expression , string-literal ) ;
10950 If MEMBER_P, this static_assert is a class member. */
10952 static void
10953 cp_parser_static_assert(cp_parser *parser, bool member_p)
10955 tree condition;
10956 tree message;
10957 cp_token *token;
10958 location_t saved_loc;
10959 bool dummy;
10961 /* Peek at the `static_assert' token so we can keep track of exactly
10962 where the static assertion started. */
10963 token = cp_lexer_peek_token (parser->lexer);
10964 saved_loc = token->location;
10966 /* Look for the `static_assert' keyword. */
10967 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10968 RT_STATIC_ASSERT))
10969 return;
10971 /* We know we are in a static assertion; commit to any tentative
10972 parse. */
10973 if (cp_parser_parsing_tentatively (parser))
10974 cp_parser_commit_to_tentative_parse (parser);
10976 /* Parse the `(' starting the static assertion condition. */
10977 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10979 /* Parse the constant-expression. Allow a non-constant expression
10980 here in order to give better diagnostics in finish_static_assert. */
10981 condition =
10982 cp_parser_constant_expression (parser,
10983 /*allow_non_constant_p=*/true,
10984 /*non_constant_p=*/&dummy);
10986 /* Parse the separating `,'. */
10987 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10989 /* Parse the string-literal message. */
10990 message = cp_parser_string_literal (parser,
10991 /*translate=*/false,
10992 /*wide_ok=*/true);
10994 /* A `)' completes the static assertion. */
10995 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10996 cp_parser_skip_to_closing_parenthesis (parser,
10997 /*recovering=*/true,
10998 /*or_comma=*/false,
10999 /*consume_paren=*/true);
11001 /* A semicolon terminates the declaration. */
11002 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11004 /* Complete the static assertion, which may mean either processing
11005 the static assert now or saving it for template instantiation. */
11006 finish_static_assert (condition, message, saved_loc, member_p);
11009 /* Parse a `decltype' type. Returns the type.
11011 simple-type-specifier:
11012 decltype ( expression ) */
11014 static tree
11015 cp_parser_decltype (cp_parser *parser)
11017 tree expr;
11018 bool id_expression_or_member_access_p = false;
11019 const char *saved_message;
11020 bool saved_integral_constant_expression_p;
11021 bool saved_non_integral_constant_expression_p;
11022 cp_token *id_expr_start_token;
11023 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11025 if (start_token->type == CPP_DECLTYPE)
11027 /* Already parsed. */
11028 cp_lexer_consume_token (parser->lexer);
11029 return start_token->u.value;
11032 /* Look for the `decltype' token. */
11033 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11034 return error_mark_node;
11036 /* Types cannot be defined in a `decltype' expression. Save away the
11037 old message. */
11038 saved_message = parser->type_definition_forbidden_message;
11040 /* And create the new one. */
11041 parser->type_definition_forbidden_message
11042 = G_("types may not be defined in %<decltype%> expressions");
11044 /* The restrictions on constant-expressions do not apply inside
11045 decltype expressions. */
11046 saved_integral_constant_expression_p
11047 = parser->integral_constant_expression_p;
11048 saved_non_integral_constant_expression_p
11049 = parser->non_integral_constant_expression_p;
11050 parser->integral_constant_expression_p = false;
11052 /* Do not actually evaluate the expression. */
11053 ++cp_unevaluated_operand;
11055 /* Do not warn about problems with the expression. */
11056 ++c_inhibit_evaluation_warnings;
11058 /* Parse the opening `('. */
11059 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11060 return error_mark_node;
11062 /* First, try parsing an id-expression. */
11063 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11064 cp_parser_parse_tentatively (parser);
11065 expr = cp_parser_id_expression (parser,
11066 /*template_keyword_p=*/false,
11067 /*check_dependency_p=*/true,
11068 /*template_p=*/NULL,
11069 /*declarator_p=*/false,
11070 /*optional_p=*/false);
11072 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11074 bool non_integral_constant_expression_p = false;
11075 tree id_expression = expr;
11076 cp_id_kind idk;
11077 const char *error_msg;
11079 if (TREE_CODE (expr) == IDENTIFIER_NODE)
11080 /* Lookup the name we got back from the id-expression. */
11081 expr = cp_parser_lookup_name (parser, expr,
11082 none_type,
11083 /*is_template=*/false,
11084 /*is_namespace=*/false,
11085 /*check_dependency=*/true,
11086 /*ambiguous_decls=*/NULL,
11087 id_expr_start_token->location);
11089 if (expr
11090 && expr != error_mark_node
11091 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11092 && TREE_CODE (expr) != TYPE_DECL
11093 && (TREE_CODE (expr) != BIT_NOT_EXPR
11094 || !TYPE_P (TREE_OPERAND (expr, 0)))
11095 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11097 /* Complete lookup of the id-expression. */
11098 expr = (finish_id_expression
11099 (id_expression, expr, parser->scope, &idk,
11100 /*integral_constant_expression_p=*/false,
11101 /*allow_non_integral_constant_expression_p=*/true,
11102 &non_integral_constant_expression_p,
11103 /*template_p=*/false,
11104 /*done=*/true,
11105 /*address_p=*/false,
11106 /*template_arg_p=*/false,
11107 &error_msg,
11108 id_expr_start_token->location));
11110 if (expr == error_mark_node)
11111 /* We found an id-expression, but it was something that we
11112 should not have found. This is an error, not something
11113 we can recover from, so note that we found an
11114 id-expression and we'll recover as gracefully as
11115 possible. */
11116 id_expression_or_member_access_p = true;
11119 if (expr
11120 && expr != error_mark_node
11121 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11122 /* We have an id-expression. */
11123 id_expression_or_member_access_p = true;
11126 if (!id_expression_or_member_access_p)
11128 /* Abort the id-expression parse. */
11129 cp_parser_abort_tentative_parse (parser);
11131 /* Parsing tentatively, again. */
11132 cp_parser_parse_tentatively (parser);
11134 /* Parse a class member access. */
11135 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11136 /*cast_p=*/false,
11137 /*member_access_only_p=*/true, NULL);
11139 if (expr
11140 && expr != error_mark_node
11141 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11142 /* We have an id-expression. */
11143 id_expression_or_member_access_p = true;
11146 if (id_expression_or_member_access_p)
11147 /* We have parsed the complete id-expression or member access. */
11148 cp_parser_parse_definitely (parser);
11149 else
11151 bool saved_greater_than_is_operator_p;
11153 /* Abort our attempt to parse an id-expression or member access
11154 expression. */
11155 cp_parser_abort_tentative_parse (parser);
11157 /* Within a parenthesized expression, a `>' token is always
11158 the greater-than operator. */
11159 saved_greater_than_is_operator_p
11160 = parser->greater_than_is_operator_p;
11161 parser->greater_than_is_operator_p = true;
11163 /* Parse a full expression. */
11164 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11166 /* The `>' token might be the end of a template-id or
11167 template-parameter-list now. */
11168 parser->greater_than_is_operator_p
11169 = saved_greater_than_is_operator_p;
11172 /* Go back to evaluating expressions. */
11173 --cp_unevaluated_operand;
11174 --c_inhibit_evaluation_warnings;
11176 /* Restore the old message and the integral constant expression
11177 flags. */
11178 parser->type_definition_forbidden_message = saved_message;
11179 parser->integral_constant_expression_p
11180 = saved_integral_constant_expression_p;
11181 parser->non_integral_constant_expression_p
11182 = saved_non_integral_constant_expression_p;
11184 /* Parse to the closing `)'. */
11185 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11187 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11188 /*consume_paren=*/true);
11189 return error_mark_node;
11192 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11193 tf_warning_or_error);
11195 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11196 it again. */
11197 start_token->type = CPP_DECLTYPE;
11198 start_token->u.value = expr;
11199 start_token->keyword = RID_MAX;
11200 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11202 return expr;
11205 /* Special member functions [gram.special] */
11207 /* Parse a conversion-function-id.
11209 conversion-function-id:
11210 operator conversion-type-id
11212 Returns an IDENTIFIER_NODE representing the operator. */
11214 static tree
11215 cp_parser_conversion_function_id (cp_parser* parser)
11217 tree type;
11218 tree saved_scope;
11219 tree saved_qualifying_scope;
11220 tree saved_object_scope;
11221 tree pushed_scope = NULL_TREE;
11223 /* Look for the `operator' token. */
11224 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11225 return error_mark_node;
11226 /* When we parse the conversion-type-id, the current scope will be
11227 reset. However, we need that information in able to look up the
11228 conversion function later, so we save it here. */
11229 saved_scope = parser->scope;
11230 saved_qualifying_scope = parser->qualifying_scope;
11231 saved_object_scope = parser->object_scope;
11232 /* We must enter the scope of the class so that the names of
11233 entities declared within the class are available in the
11234 conversion-type-id. For example, consider:
11236 struct S {
11237 typedef int I;
11238 operator I();
11241 S::operator I() { ... }
11243 In order to see that `I' is a type-name in the definition, we
11244 must be in the scope of `S'. */
11245 if (saved_scope)
11246 pushed_scope = push_scope (saved_scope);
11247 /* Parse the conversion-type-id. */
11248 type = cp_parser_conversion_type_id (parser);
11249 /* Leave the scope of the class, if any. */
11250 if (pushed_scope)
11251 pop_scope (pushed_scope);
11252 /* Restore the saved scope. */
11253 parser->scope = saved_scope;
11254 parser->qualifying_scope = saved_qualifying_scope;
11255 parser->object_scope = saved_object_scope;
11256 /* If the TYPE is invalid, indicate failure. */
11257 if (type == error_mark_node)
11258 return error_mark_node;
11259 return mangle_conv_op_name_for_type (type);
11262 /* Parse a conversion-type-id:
11264 conversion-type-id:
11265 type-specifier-seq conversion-declarator [opt]
11267 Returns the TYPE specified. */
11269 static tree
11270 cp_parser_conversion_type_id (cp_parser* parser)
11272 tree attributes;
11273 cp_decl_specifier_seq type_specifiers;
11274 cp_declarator *declarator;
11275 tree type_specified;
11277 /* Parse the attributes. */
11278 attributes = cp_parser_attributes_opt (parser);
11279 /* Parse the type-specifiers. */
11280 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11281 /*is_trailing_return=*/false,
11282 &type_specifiers);
11283 /* If that didn't work, stop. */
11284 if (type_specifiers.type == error_mark_node)
11285 return error_mark_node;
11286 /* Parse the conversion-declarator. */
11287 declarator = cp_parser_conversion_declarator_opt (parser);
11289 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11290 /*initialized=*/0, &attributes);
11291 if (attributes)
11292 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11294 /* Don't give this error when parsing tentatively. This happens to
11295 work because we always parse this definitively once. */
11296 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11297 && type_uses_auto (type_specified))
11299 if (cxx_dialect < cxx1y)
11301 error ("invalid use of %<auto%> in conversion operator");
11302 return error_mark_node;
11304 else if (template_parm_scope_p ())
11305 warning (0, "use of %<auto%> in member template "
11306 "conversion operator can never be deduced");
11309 return type_specified;
11312 /* Parse an (optional) conversion-declarator.
11314 conversion-declarator:
11315 ptr-operator conversion-declarator [opt]
11319 static cp_declarator *
11320 cp_parser_conversion_declarator_opt (cp_parser* parser)
11322 enum tree_code code;
11323 tree class_type;
11324 cp_cv_quals cv_quals;
11326 /* We don't know if there's a ptr-operator next, or not. */
11327 cp_parser_parse_tentatively (parser);
11328 /* Try the ptr-operator. */
11329 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11330 /* If it worked, look for more conversion-declarators. */
11331 if (cp_parser_parse_definitely (parser))
11333 cp_declarator *declarator;
11335 /* Parse another optional declarator. */
11336 declarator = cp_parser_conversion_declarator_opt (parser);
11338 return cp_parser_make_indirect_declarator
11339 (code, class_type, cv_quals, declarator);
11342 return NULL;
11345 /* Parse an (optional) ctor-initializer.
11347 ctor-initializer:
11348 : mem-initializer-list
11350 Returns TRUE iff the ctor-initializer was actually present. */
11352 static bool
11353 cp_parser_ctor_initializer_opt (cp_parser* parser)
11355 /* If the next token is not a `:', then there is no
11356 ctor-initializer. */
11357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11359 /* Do default initialization of any bases and members. */
11360 if (DECL_CONSTRUCTOR_P (current_function_decl))
11361 finish_mem_initializers (NULL_TREE);
11363 return false;
11366 /* Consume the `:' token. */
11367 cp_lexer_consume_token (parser->lexer);
11368 /* And the mem-initializer-list. */
11369 cp_parser_mem_initializer_list (parser);
11371 return true;
11374 /* Parse a mem-initializer-list.
11376 mem-initializer-list:
11377 mem-initializer ... [opt]
11378 mem-initializer ... [opt] , mem-initializer-list */
11380 static void
11381 cp_parser_mem_initializer_list (cp_parser* parser)
11383 tree mem_initializer_list = NULL_TREE;
11384 tree target_ctor = error_mark_node;
11385 cp_token *token = cp_lexer_peek_token (parser->lexer);
11387 /* Let the semantic analysis code know that we are starting the
11388 mem-initializer-list. */
11389 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11390 error_at (token->location,
11391 "only constructors take member initializers");
11393 /* Loop through the list. */
11394 while (true)
11396 tree mem_initializer;
11398 token = cp_lexer_peek_token (parser->lexer);
11399 /* Parse the mem-initializer. */
11400 mem_initializer = cp_parser_mem_initializer (parser);
11401 /* If the next token is a `...', we're expanding member initializers. */
11402 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11404 /* Consume the `...'. */
11405 cp_lexer_consume_token (parser->lexer);
11407 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11408 can be expanded but members cannot. */
11409 if (mem_initializer != error_mark_node
11410 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11412 error_at (token->location,
11413 "cannot expand initializer for member %<%D%>",
11414 TREE_PURPOSE (mem_initializer));
11415 mem_initializer = error_mark_node;
11418 /* Construct the pack expansion type. */
11419 if (mem_initializer != error_mark_node)
11420 mem_initializer = make_pack_expansion (mem_initializer);
11422 if (target_ctor != error_mark_node
11423 && mem_initializer != error_mark_node)
11425 error ("mem-initializer for %qD follows constructor delegation",
11426 TREE_PURPOSE (mem_initializer));
11427 mem_initializer = error_mark_node;
11429 /* Look for a target constructor. */
11430 if (mem_initializer != error_mark_node
11431 && TYPE_P (TREE_PURPOSE (mem_initializer))
11432 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11434 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11435 if (mem_initializer_list)
11437 error ("constructor delegation follows mem-initializer for %qD",
11438 TREE_PURPOSE (mem_initializer_list));
11439 mem_initializer = error_mark_node;
11441 target_ctor = mem_initializer;
11443 /* Add it to the list, unless it was erroneous. */
11444 if (mem_initializer != error_mark_node)
11446 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11447 mem_initializer_list = mem_initializer;
11449 /* If the next token is not a `,', we're done. */
11450 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11451 break;
11452 /* Consume the `,' token. */
11453 cp_lexer_consume_token (parser->lexer);
11456 /* Perform semantic analysis. */
11457 if (DECL_CONSTRUCTOR_P (current_function_decl))
11458 finish_mem_initializers (mem_initializer_list);
11461 /* Parse a mem-initializer.
11463 mem-initializer:
11464 mem-initializer-id ( expression-list [opt] )
11465 mem-initializer-id braced-init-list
11467 GNU extension:
11469 mem-initializer:
11470 ( expression-list [opt] )
11472 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11473 class) or FIELD_DECL (for a non-static data member) to initialize;
11474 the TREE_VALUE is the expression-list. An empty initialization
11475 list is represented by void_list_node. */
11477 static tree
11478 cp_parser_mem_initializer (cp_parser* parser)
11480 tree mem_initializer_id;
11481 tree expression_list;
11482 tree member;
11483 cp_token *token = cp_lexer_peek_token (parser->lexer);
11485 /* Find out what is being initialized. */
11486 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11488 permerror (token->location,
11489 "anachronistic old-style base class initializer");
11490 mem_initializer_id = NULL_TREE;
11492 else
11494 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11495 if (mem_initializer_id == error_mark_node)
11496 return mem_initializer_id;
11498 member = expand_member_init (mem_initializer_id);
11499 if (member && !DECL_P (member))
11500 in_base_initializer = 1;
11502 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11504 bool expr_non_constant_p;
11505 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11506 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11507 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11508 expression_list = build_tree_list (NULL_TREE, expression_list);
11510 else
11512 VEC(tree,gc)* vec;
11513 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11514 /*cast_p=*/false,
11515 /*allow_expansion_p=*/true,
11516 /*non_constant_p=*/NULL);
11517 if (vec == NULL)
11518 return error_mark_node;
11519 expression_list = build_tree_list_vec (vec);
11520 release_tree_vector (vec);
11523 if (expression_list == error_mark_node)
11524 return error_mark_node;
11525 if (!expression_list)
11526 expression_list = void_type_node;
11528 in_base_initializer = 0;
11530 return member ? build_tree_list (member, expression_list) : error_mark_node;
11533 /* Parse a mem-initializer-id.
11535 mem-initializer-id:
11536 :: [opt] nested-name-specifier [opt] class-name
11537 identifier
11539 Returns a TYPE indicating the class to be initializer for the first
11540 production. Returns an IDENTIFIER_NODE indicating the data member
11541 to be initialized for the second production. */
11543 static tree
11544 cp_parser_mem_initializer_id (cp_parser* parser)
11546 bool global_scope_p;
11547 bool nested_name_specifier_p;
11548 bool template_p = false;
11549 tree id;
11551 cp_token *token = cp_lexer_peek_token (parser->lexer);
11553 /* `typename' is not allowed in this context ([temp.res]). */
11554 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11556 error_at (token->location,
11557 "keyword %<typename%> not allowed in this context (a qualified "
11558 "member initializer is implicitly a type)");
11559 cp_lexer_consume_token (parser->lexer);
11561 /* Look for the optional `::' operator. */
11562 global_scope_p
11563 = (cp_parser_global_scope_opt (parser,
11564 /*current_scope_valid_p=*/false)
11565 != NULL_TREE);
11566 /* Look for the optional nested-name-specifier. The simplest way to
11567 implement:
11569 [temp.res]
11571 The keyword `typename' is not permitted in a base-specifier or
11572 mem-initializer; in these contexts a qualified name that
11573 depends on a template-parameter is implicitly assumed to be a
11574 type name.
11576 is to assume that we have seen the `typename' keyword at this
11577 point. */
11578 nested_name_specifier_p
11579 = (cp_parser_nested_name_specifier_opt (parser,
11580 /*typename_keyword_p=*/true,
11581 /*check_dependency_p=*/true,
11582 /*type_p=*/true,
11583 /*is_declaration=*/true)
11584 != NULL_TREE);
11585 if (nested_name_specifier_p)
11586 template_p = cp_parser_optional_template_keyword (parser);
11587 /* If there is a `::' operator or a nested-name-specifier, then we
11588 are definitely looking for a class-name. */
11589 if (global_scope_p || nested_name_specifier_p)
11590 return cp_parser_class_name (parser,
11591 /*typename_keyword_p=*/true,
11592 /*template_keyword_p=*/template_p,
11593 typename_type,
11594 /*check_dependency_p=*/true,
11595 /*class_head_p=*/false,
11596 /*is_declaration=*/true);
11597 /* Otherwise, we could also be looking for an ordinary identifier. */
11598 cp_parser_parse_tentatively (parser);
11599 /* Try a class-name. */
11600 id = cp_parser_class_name (parser,
11601 /*typename_keyword_p=*/true,
11602 /*template_keyword_p=*/false,
11603 none_type,
11604 /*check_dependency_p=*/true,
11605 /*class_head_p=*/false,
11606 /*is_declaration=*/true);
11607 /* If we found one, we're done. */
11608 if (cp_parser_parse_definitely (parser))
11609 return id;
11610 /* Otherwise, look for an ordinary identifier. */
11611 return cp_parser_identifier (parser);
11614 /* Overloading [gram.over] */
11616 /* Parse an operator-function-id.
11618 operator-function-id:
11619 operator operator
11621 Returns an IDENTIFIER_NODE for the operator which is a
11622 human-readable spelling of the identifier, e.g., `operator +'. */
11624 static tree
11625 cp_parser_operator_function_id (cp_parser* parser)
11627 /* Look for the `operator' keyword. */
11628 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11629 return error_mark_node;
11630 /* And then the name of the operator itself. */
11631 return cp_parser_operator (parser);
11634 /* Return an identifier node for a user-defined literal operator.
11635 The suffix identifier is chained to the operator name identifier. */
11637 static tree
11638 cp_literal_operator_id (const char* name)
11640 tree identifier;
11641 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11642 + strlen (name) + 10);
11643 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11644 identifier = get_identifier (buffer);
11645 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11647 return identifier;
11650 /* Parse an operator.
11652 operator:
11653 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11654 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11655 || ++ -- , ->* -> () []
11657 GNU Extensions:
11659 operator:
11660 <? >? <?= >?=
11662 Returns an IDENTIFIER_NODE for the operator which is a
11663 human-readable spelling of the identifier, e.g., `operator +'. */
11665 static tree
11666 cp_parser_operator (cp_parser* parser)
11668 tree id = NULL_TREE;
11669 cp_token *token;
11671 /* Peek at the next token. */
11672 token = cp_lexer_peek_token (parser->lexer);
11673 /* Figure out which operator we have. */
11674 switch (token->type)
11676 case CPP_KEYWORD:
11678 enum tree_code op;
11680 /* The keyword should be either `new' or `delete'. */
11681 if (token->keyword == RID_NEW)
11682 op = NEW_EXPR;
11683 else if (token->keyword == RID_DELETE)
11684 op = DELETE_EXPR;
11685 else
11686 break;
11688 /* Consume the `new' or `delete' token. */
11689 cp_lexer_consume_token (parser->lexer);
11691 /* Peek at the next token. */
11692 token = cp_lexer_peek_token (parser->lexer);
11693 /* If it's a `[' token then this is the array variant of the
11694 operator. */
11695 if (token->type == CPP_OPEN_SQUARE)
11697 /* Consume the `[' token. */
11698 cp_lexer_consume_token (parser->lexer);
11699 /* Look for the `]' token. */
11700 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11701 id = ansi_opname (op == NEW_EXPR
11702 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11704 /* Otherwise, we have the non-array variant. */
11705 else
11706 id = ansi_opname (op);
11708 return id;
11711 case CPP_PLUS:
11712 id = ansi_opname (PLUS_EXPR);
11713 break;
11715 case CPP_MINUS:
11716 id = ansi_opname (MINUS_EXPR);
11717 break;
11719 case CPP_MULT:
11720 id = ansi_opname (MULT_EXPR);
11721 break;
11723 case CPP_DIV:
11724 id = ansi_opname (TRUNC_DIV_EXPR);
11725 break;
11727 case CPP_MOD:
11728 id = ansi_opname (TRUNC_MOD_EXPR);
11729 break;
11731 case CPP_XOR:
11732 id = ansi_opname (BIT_XOR_EXPR);
11733 break;
11735 case CPP_AND:
11736 id = ansi_opname (BIT_AND_EXPR);
11737 break;
11739 case CPP_OR:
11740 id = ansi_opname (BIT_IOR_EXPR);
11741 break;
11743 case CPP_COMPL:
11744 id = ansi_opname (BIT_NOT_EXPR);
11745 break;
11747 case CPP_NOT:
11748 id = ansi_opname (TRUTH_NOT_EXPR);
11749 break;
11751 case CPP_EQ:
11752 id = ansi_assopname (NOP_EXPR);
11753 break;
11755 case CPP_LESS:
11756 id = ansi_opname (LT_EXPR);
11757 break;
11759 case CPP_GREATER:
11760 id = ansi_opname (GT_EXPR);
11761 break;
11763 case CPP_PLUS_EQ:
11764 id = ansi_assopname (PLUS_EXPR);
11765 break;
11767 case CPP_MINUS_EQ:
11768 id = ansi_assopname (MINUS_EXPR);
11769 break;
11771 case CPP_MULT_EQ:
11772 id = ansi_assopname (MULT_EXPR);
11773 break;
11775 case CPP_DIV_EQ:
11776 id = ansi_assopname (TRUNC_DIV_EXPR);
11777 break;
11779 case CPP_MOD_EQ:
11780 id = ansi_assopname (TRUNC_MOD_EXPR);
11781 break;
11783 case CPP_XOR_EQ:
11784 id = ansi_assopname (BIT_XOR_EXPR);
11785 break;
11787 case CPP_AND_EQ:
11788 id = ansi_assopname (BIT_AND_EXPR);
11789 break;
11791 case CPP_OR_EQ:
11792 id = ansi_assopname (BIT_IOR_EXPR);
11793 break;
11795 case CPP_LSHIFT:
11796 id = ansi_opname (LSHIFT_EXPR);
11797 break;
11799 case CPP_RSHIFT:
11800 id = ansi_opname (RSHIFT_EXPR);
11801 break;
11803 case CPP_LSHIFT_EQ:
11804 id = ansi_assopname (LSHIFT_EXPR);
11805 break;
11807 case CPP_RSHIFT_EQ:
11808 id = ansi_assopname (RSHIFT_EXPR);
11809 break;
11811 case CPP_EQ_EQ:
11812 id = ansi_opname (EQ_EXPR);
11813 break;
11815 case CPP_NOT_EQ:
11816 id = ansi_opname (NE_EXPR);
11817 break;
11819 case CPP_LESS_EQ:
11820 id = ansi_opname (LE_EXPR);
11821 break;
11823 case CPP_GREATER_EQ:
11824 id = ansi_opname (GE_EXPR);
11825 break;
11827 case CPP_AND_AND:
11828 id = ansi_opname (TRUTH_ANDIF_EXPR);
11829 break;
11831 case CPP_OR_OR:
11832 id = ansi_opname (TRUTH_ORIF_EXPR);
11833 break;
11835 case CPP_PLUS_PLUS:
11836 id = ansi_opname (POSTINCREMENT_EXPR);
11837 break;
11839 case CPP_MINUS_MINUS:
11840 id = ansi_opname (PREDECREMENT_EXPR);
11841 break;
11843 case CPP_COMMA:
11844 id = ansi_opname (COMPOUND_EXPR);
11845 break;
11847 case CPP_DEREF_STAR:
11848 id = ansi_opname (MEMBER_REF);
11849 break;
11851 case CPP_DEREF:
11852 id = ansi_opname (COMPONENT_REF);
11853 break;
11855 case CPP_OPEN_PAREN:
11856 /* Consume the `('. */
11857 cp_lexer_consume_token (parser->lexer);
11858 /* Look for the matching `)'. */
11859 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11860 return ansi_opname (CALL_EXPR);
11862 case CPP_OPEN_SQUARE:
11863 /* Consume the `['. */
11864 cp_lexer_consume_token (parser->lexer);
11865 /* Look for the matching `]'. */
11866 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11867 return ansi_opname (ARRAY_REF);
11869 case CPP_STRING:
11870 if (cxx_dialect == cxx98)
11871 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11872 if (TREE_STRING_LENGTH (token->u.value) > 2)
11874 error ("expected empty string after %<operator%> keyword");
11875 return error_mark_node;
11877 /* Consume the string. */
11878 cp_lexer_consume_token (parser->lexer);
11879 /* Look for the suffix identifier. */
11880 token = cp_lexer_peek_token (parser->lexer);
11881 if (token->type == CPP_NAME)
11883 id = cp_parser_identifier (parser);
11884 if (id != error_mark_node)
11886 const char *name = IDENTIFIER_POINTER (id);
11887 return cp_literal_operator_id (name);
11890 else
11892 error ("expected suffix identifier");
11893 return error_mark_node;
11896 case CPP_STRING_USERDEF:
11897 error ("missing space between %<\"\"%> and suffix identifier");
11898 return error_mark_node;
11900 default:
11901 /* Anything else is an error. */
11902 break;
11905 /* If we have selected an identifier, we need to consume the
11906 operator token. */
11907 if (id)
11908 cp_lexer_consume_token (parser->lexer);
11909 /* Otherwise, no valid operator name was present. */
11910 else
11912 cp_parser_error (parser, "expected operator");
11913 id = error_mark_node;
11916 return id;
11919 /* Parse a template-declaration.
11921 template-declaration:
11922 export [opt] template < template-parameter-list > declaration
11924 If MEMBER_P is TRUE, this template-declaration occurs within a
11925 class-specifier.
11927 The grammar rule given by the standard isn't correct. What
11928 is really meant is:
11930 template-declaration:
11931 export [opt] template-parameter-list-seq
11932 decl-specifier-seq [opt] init-declarator [opt] ;
11933 export [opt] template-parameter-list-seq
11934 function-definition
11936 template-parameter-list-seq:
11937 template-parameter-list-seq [opt]
11938 template < template-parameter-list > */
11940 static void
11941 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11943 /* Check for `export'. */
11944 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11946 /* Consume the `export' token. */
11947 cp_lexer_consume_token (parser->lexer);
11948 /* Warn that we do not support `export'. */
11949 warning (0, "keyword %<export%> not implemented, and will be ignored");
11952 cp_parser_template_declaration_after_export (parser, member_p);
11955 /* Parse a template-parameter-list.
11957 template-parameter-list:
11958 template-parameter
11959 template-parameter-list , template-parameter
11961 Returns a TREE_LIST. Each node represents a template parameter.
11962 The nodes are connected via their TREE_CHAINs. */
11964 static tree
11965 cp_parser_template_parameter_list (cp_parser* parser)
11967 tree parameter_list = NULL_TREE;
11969 begin_template_parm_list ();
11971 /* The loop below parses the template parms. We first need to know
11972 the total number of template parms to be able to compute proper
11973 canonical types of each dependent type. So after the loop, when
11974 we know the total number of template parms,
11975 end_template_parm_list computes the proper canonical types and
11976 fixes up the dependent types accordingly. */
11977 while (true)
11979 tree parameter;
11980 bool is_non_type;
11981 bool is_parameter_pack;
11982 location_t parm_loc;
11984 /* Parse the template-parameter. */
11985 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11986 parameter = cp_parser_template_parameter (parser,
11987 &is_non_type,
11988 &is_parameter_pack);
11989 /* Add it to the list. */
11990 if (parameter != error_mark_node)
11991 parameter_list = process_template_parm (parameter_list,
11992 parm_loc,
11993 parameter,
11994 is_non_type,
11995 is_parameter_pack);
11996 else
11998 tree err_parm = build_tree_list (parameter, parameter);
11999 parameter_list = chainon (parameter_list, err_parm);
12002 /* If the next token is not a `,', we're done. */
12003 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12004 break;
12005 /* Otherwise, consume the `,' token. */
12006 cp_lexer_consume_token (parser->lexer);
12009 return end_template_parm_list (parameter_list);
12012 /* Parse a template-parameter.
12014 template-parameter:
12015 type-parameter
12016 parameter-declaration
12018 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12019 the parameter. The TREE_PURPOSE is the default value, if any.
12020 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12021 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12022 set to true iff this parameter is a parameter pack. */
12024 static tree
12025 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12026 bool *is_parameter_pack)
12028 cp_token *token;
12029 cp_parameter_declarator *parameter_declarator;
12030 cp_declarator *id_declarator;
12031 tree parm;
12033 /* Assume it is a type parameter or a template parameter. */
12034 *is_non_type = false;
12035 /* Assume it not a parameter pack. */
12036 *is_parameter_pack = false;
12037 /* Peek at the next token. */
12038 token = cp_lexer_peek_token (parser->lexer);
12039 /* If it is `class' or `template', we have a type-parameter. */
12040 if (token->keyword == RID_TEMPLATE)
12041 return cp_parser_type_parameter (parser, is_parameter_pack);
12042 /* If it is `class' or `typename' we do not know yet whether it is a
12043 type parameter or a non-type parameter. Consider:
12045 template <typename T, typename T::X X> ...
12049 template <class C, class D*> ...
12051 Here, the first parameter is a type parameter, and the second is
12052 a non-type parameter. We can tell by looking at the token after
12053 the identifier -- if it is a `,', `=', or `>' then we have a type
12054 parameter. */
12055 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12057 /* Peek at the token after `class' or `typename'. */
12058 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12059 /* If it's an ellipsis, we have a template type parameter
12060 pack. */
12061 if (token->type == CPP_ELLIPSIS)
12062 return cp_parser_type_parameter (parser, is_parameter_pack);
12063 /* If it's an identifier, skip it. */
12064 if (token->type == CPP_NAME)
12065 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12066 /* Now, see if the token looks like the end of a template
12067 parameter. */
12068 if (token->type == CPP_COMMA
12069 || token->type == CPP_EQ
12070 || token->type == CPP_GREATER)
12071 return cp_parser_type_parameter (parser, is_parameter_pack);
12074 /* Otherwise, it is a non-type parameter.
12076 [temp.param]
12078 When parsing a default template-argument for a non-type
12079 template-parameter, the first non-nested `>' is taken as the end
12080 of the template parameter-list rather than a greater-than
12081 operator. */
12082 *is_non_type = true;
12083 parameter_declarator
12084 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12085 /*parenthesized_p=*/NULL);
12087 /* If the parameter declaration is marked as a parameter pack, set
12088 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12089 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12090 grokdeclarator. */
12091 if (parameter_declarator
12092 && parameter_declarator->declarator
12093 && parameter_declarator->declarator->parameter_pack_p)
12095 *is_parameter_pack = true;
12096 parameter_declarator->declarator->parameter_pack_p = false;
12099 /* If the next token is an ellipsis, and we don't already have it
12100 marked as a parameter pack, then we have a parameter pack (that
12101 has no declarator). */
12102 if (!*is_parameter_pack
12103 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12104 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
12106 /* Consume the `...'. */
12107 cp_lexer_consume_token (parser->lexer);
12108 maybe_warn_variadic_templates ();
12110 *is_parameter_pack = true;
12112 /* We might end up with a pack expansion as the type of the non-type
12113 template parameter, in which case this is a non-type template
12114 parameter pack. */
12115 else if (parameter_declarator
12116 && parameter_declarator->decl_specifiers.type
12117 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12119 *is_parameter_pack = true;
12120 parameter_declarator->decl_specifiers.type =
12121 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12124 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12126 /* Parameter packs cannot have default arguments. However, a
12127 user may try to do so, so we'll parse them and give an
12128 appropriate diagnostic here. */
12130 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12132 /* Find the name of the parameter pack. */
12133 id_declarator = parameter_declarator->declarator;
12134 while (id_declarator && id_declarator->kind != cdk_id)
12135 id_declarator = id_declarator->declarator;
12137 if (id_declarator && id_declarator->kind == cdk_id)
12138 error_at (start_token->location,
12139 "template parameter pack %qD cannot have a default argument",
12140 id_declarator->u.id.unqualified_name);
12141 else
12142 error_at (start_token->location,
12143 "template parameter pack cannot have a default argument");
12145 /* Parse the default argument, but throw away the result. */
12146 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12149 parm = grokdeclarator (parameter_declarator->declarator,
12150 &parameter_declarator->decl_specifiers,
12151 TPARM, /*initialized=*/0,
12152 /*attrlist=*/NULL);
12153 if (parm == error_mark_node)
12154 return error_mark_node;
12156 return build_tree_list (parameter_declarator->default_argument, parm);
12159 /* Parse a type-parameter.
12161 type-parameter:
12162 class identifier [opt]
12163 class identifier [opt] = type-id
12164 typename identifier [opt]
12165 typename identifier [opt] = type-id
12166 template < template-parameter-list > class identifier [opt]
12167 template < template-parameter-list > class identifier [opt]
12168 = id-expression
12170 GNU Extension (variadic templates):
12172 type-parameter:
12173 class ... identifier [opt]
12174 typename ... identifier [opt]
12176 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12177 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12178 the declaration of the parameter.
12180 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12182 static tree
12183 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12185 cp_token *token;
12186 tree parameter;
12188 /* Look for a keyword to tell us what kind of parameter this is. */
12189 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12190 if (!token)
12191 return error_mark_node;
12193 switch (token->keyword)
12195 case RID_CLASS:
12196 case RID_TYPENAME:
12198 tree identifier;
12199 tree default_argument;
12201 /* If the next token is an ellipsis, we have a template
12202 argument pack. */
12203 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12205 /* Consume the `...' token. */
12206 cp_lexer_consume_token (parser->lexer);
12207 maybe_warn_variadic_templates ();
12209 *is_parameter_pack = true;
12212 /* If the next token is an identifier, then it names the
12213 parameter. */
12214 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12215 identifier = cp_parser_identifier (parser);
12216 else
12217 identifier = NULL_TREE;
12219 /* Create the parameter. */
12220 parameter = finish_template_type_parm (class_type_node, identifier);
12222 /* If the next token is an `=', we have a default argument. */
12223 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12225 /* Consume the `=' token. */
12226 cp_lexer_consume_token (parser->lexer);
12227 /* Parse the default-argument. */
12228 push_deferring_access_checks (dk_no_deferred);
12229 default_argument = cp_parser_type_id (parser);
12231 /* Template parameter packs cannot have default
12232 arguments. */
12233 if (*is_parameter_pack)
12235 if (identifier)
12236 error_at (token->location,
12237 "template parameter pack %qD cannot have a "
12238 "default argument", identifier);
12239 else
12240 error_at (token->location,
12241 "template parameter packs cannot have "
12242 "default arguments");
12243 default_argument = NULL_TREE;
12245 pop_deferring_access_checks ();
12247 else
12248 default_argument = NULL_TREE;
12250 /* Create the combined representation of the parameter and the
12251 default argument. */
12252 parameter = build_tree_list (default_argument, parameter);
12254 break;
12256 case RID_TEMPLATE:
12258 tree identifier;
12259 tree default_argument;
12261 /* Look for the `<'. */
12262 cp_parser_require (parser, CPP_LESS, RT_LESS);
12263 /* Parse the template-parameter-list. */
12264 cp_parser_template_parameter_list (parser);
12265 /* Look for the `>'. */
12266 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12267 /* Look for the `class' keyword. */
12268 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12269 /* If the next token is an ellipsis, we have a template
12270 argument pack. */
12271 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12273 /* Consume the `...' token. */
12274 cp_lexer_consume_token (parser->lexer);
12275 maybe_warn_variadic_templates ();
12277 *is_parameter_pack = true;
12279 /* If the next token is an `=', then there is a
12280 default-argument. If the next token is a `>', we are at
12281 the end of the parameter-list. If the next token is a `,',
12282 then we are at the end of this parameter. */
12283 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12284 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12285 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12287 identifier = cp_parser_identifier (parser);
12288 /* Treat invalid names as if the parameter were nameless. */
12289 if (identifier == error_mark_node)
12290 identifier = NULL_TREE;
12292 else
12293 identifier = NULL_TREE;
12295 /* Create the template parameter. */
12296 parameter = finish_template_template_parm (class_type_node,
12297 identifier);
12299 /* If the next token is an `=', then there is a
12300 default-argument. */
12301 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12303 bool is_template;
12305 /* Consume the `='. */
12306 cp_lexer_consume_token (parser->lexer);
12307 /* Parse the id-expression. */
12308 push_deferring_access_checks (dk_no_deferred);
12309 /* save token before parsing the id-expression, for error
12310 reporting */
12311 token = cp_lexer_peek_token (parser->lexer);
12312 default_argument
12313 = cp_parser_id_expression (parser,
12314 /*template_keyword_p=*/false,
12315 /*check_dependency_p=*/true,
12316 /*template_p=*/&is_template,
12317 /*declarator_p=*/false,
12318 /*optional_p=*/false);
12319 if (TREE_CODE (default_argument) == TYPE_DECL)
12320 /* If the id-expression was a template-id that refers to
12321 a template-class, we already have the declaration here,
12322 so no further lookup is needed. */
12324 else
12325 /* Look up the name. */
12326 default_argument
12327 = cp_parser_lookup_name (parser, default_argument,
12328 none_type,
12329 /*is_template=*/is_template,
12330 /*is_namespace=*/false,
12331 /*check_dependency=*/true,
12332 /*ambiguous_decls=*/NULL,
12333 token->location);
12334 /* See if the default argument is valid. */
12335 default_argument
12336 = check_template_template_default_arg (default_argument);
12338 /* Template parameter packs cannot have default
12339 arguments. */
12340 if (*is_parameter_pack)
12342 if (identifier)
12343 error_at (token->location,
12344 "template parameter pack %qD cannot "
12345 "have a default argument",
12346 identifier);
12347 else
12348 error_at (token->location, "template parameter packs cannot "
12349 "have default arguments");
12350 default_argument = NULL_TREE;
12352 pop_deferring_access_checks ();
12354 else
12355 default_argument = NULL_TREE;
12357 /* Create the combined representation of the parameter and the
12358 default argument. */
12359 parameter = build_tree_list (default_argument, parameter);
12361 break;
12363 default:
12364 gcc_unreachable ();
12365 break;
12368 return parameter;
12371 /* Parse a template-id.
12373 template-id:
12374 template-name < template-argument-list [opt] >
12376 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12377 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12378 returned. Otherwise, if the template-name names a function, or set
12379 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12380 names a class, returns a TYPE_DECL for the specialization.
12382 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12383 uninstantiated templates. */
12385 static tree
12386 cp_parser_template_id (cp_parser *parser,
12387 bool template_keyword_p,
12388 bool check_dependency_p,
12389 enum tag_types tag_type,
12390 bool is_declaration)
12392 int i;
12393 tree templ;
12394 tree arguments;
12395 tree template_id;
12396 cp_token_position start_of_id = 0;
12397 deferred_access_check *chk;
12398 VEC (deferred_access_check,gc) *access_check;
12399 cp_token *next_token = NULL, *next_token_2 = NULL;
12400 bool is_identifier;
12402 /* If the next token corresponds to a template-id, there is no need
12403 to reparse it. */
12404 next_token = cp_lexer_peek_token (parser->lexer);
12405 if (next_token->type == CPP_TEMPLATE_ID)
12407 struct tree_check *check_value;
12409 /* Get the stored value. */
12410 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12411 /* Perform any access checks that were deferred. */
12412 access_check = check_value->checks;
12413 if (access_check)
12415 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12416 perform_or_defer_access_check (chk->binfo,
12417 chk->decl,
12418 chk->diag_decl,
12419 tf_warning_or_error);
12421 /* Return the stored value. */
12422 return check_value->value;
12425 /* Avoid performing name lookup if there is no possibility of
12426 finding a template-id. */
12427 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12428 || (next_token->type == CPP_NAME
12429 && !cp_parser_nth_token_starts_template_argument_list_p
12430 (parser, 2)))
12432 cp_parser_error (parser, "expected template-id");
12433 return error_mark_node;
12436 /* Remember where the template-id starts. */
12437 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12438 start_of_id = cp_lexer_token_position (parser->lexer, false);
12440 push_deferring_access_checks (dk_deferred);
12442 /* Parse the template-name. */
12443 is_identifier = false;
12444 templ = cp_parser_template_name (parser, template_keyword_p,
12445 check_dependency_p,
12446 is_declaration,
12447 tag_type,
12448 &is_identifier);
12449 if (templ == error_mark_node || is_identifier)
12451 pop_deferring_access_checks ();
12452 return templ;
12455 /* If we find the sequence `[:' after a template-name, it's probably
12456 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12457 parse correctly the argument list. */
12458 next_token = cp_lexer_peek_token (parser->lexer);
12459 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12460 if (next_token->type == CPP_OPEN_SQUARE
12461 && next_token->flags & DIGRAPH
12462 && next_token_2->type == CPP_COLON
12463 && !(next_token_2->flags & PREV_WHITE))
12465 cp_parser_parse_tentatively (parser);
12466 /* Change `:' into `::'. */
12467 next_token_2->type = CPP_SCOPE;
12468 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12469 CPP_LESS. */
12470 cp_lexer_consume_token (parser->lexer);
12472 /* Parse the arguments. */
12473 arguments = cp_parser_enclosed_template_argument_list (parser);
12474 if (!cp_parser_parse_definitely (parser))
12476 /* If we couldn't parse an argument list, then we revert our changes
12477 and return simply an error. Maybe this is not a template-id
12478 after all. */
12479 next_token_2->type = CPP_COLON;
12480 cp_parser_error (parser, "expected %<<%>");
12481 pop_deferring_access_checks ();
12482 return error_mark_node;
12484 /* Otherwise, emit an error about the invalid digraph, but continue
12485 parsing because we got our argument list. */
12486 if (permerror (next_token->location,
12487 "%<<::%> cannot begin a template-argument list"))
12489 static bool hint = false;
12490 inform (next_token->location,
12491 "%<<:%> is an alternate spelling for %<[%>."
12492 " Insert whitespace between %<<%> and %<::%>");
12493 if (!hint && !flag_permissive)
12495 inform (next_token->location, "(if you use %<-fpermissive%>"
12496 " G++ will accept your code)");
12497 hint = true;
12501 else
12503 /* Look for the `<' that starts the template-argument-list. */
12504 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12506 pop_deferring_access_checks ();
12507 return error_mark_node;
12509 /* Parse the arguments. */
12510 arguments = cp_parser_enclosed_template_argument_list (parser);
12513 /* Build a representation of the specialization. */
12514 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12515 template_id = build_min_nt_loc (next_token->location,
12516 TEMPLATE_ID_EXPR,
12517 templ, arguments);
12518 else if (DECL_TYPE_TEMPLATE_P (templ)
12519 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12521 bool entering_scope;
12522 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12523 template (rather than some instantiation thereof) only if
12524 is not nested within some other construct. For example, in
12525 "template <typename T> void f(T) { A<T>::", A<T> is just an
12526 instantiation of A. */
12527 entering_scope = (template_parm_scope_p ()
12528 && cp_lexer_next_token_is (parser->lexer,
12529 CPP_SCOPE));
12530 template_id
12531 = finish_template_type (templ, arguments, entering_scope);
12533 else
12535 /* If it's not a class-template or a template-template, it should be
12536 a function-template. */
12537 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12538 || TREE_CODE (templ) == OVERLOAD
12539 || BASELINK_P (templ)));
12541 template_id = lookup_template_function (templ, arguments);
12544 /* If parsing tentatively, replace the sequence of tokens that makes
12545 up the template-id with a CPP_TEMPLATE_ID token. That way,
12546 should we re-parse the token stream, we will not have to repeat
12547 the effort required to do the parse, nor will we issue duplicate
12548 error messages about problems during instantiation of the
12549 template. */
12550 if (start_of_id)
12552 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12554 /* Reset the contents of the START_OF_ID token. */
12555 token->type = CPP_TEMPLATE_ID;
12556 /* Retrieve any deferred checks. Do not pop this access checks yet
12557 so the memory will not be reclaimed during token replacing below. */
12558 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12559 token->u.tree_check_value->value = template_id;
12560 token->u.tree_check_value->checks = get_deferred_access_checks ();
12561 token->keyword = RID_MAX;
12563 /* Purge all subsequent tokens. */
12564 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12566 /* ??? Can we actually assume that, if template_id ==
12567 error_mark_node, we will have issued a diagnostic to the
12568 user, as opposed to simply marking the tentative parse as
12569 failed? */
12570 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12571 error_at (token->location, "parse error in template argument list");
12574 pop_deferring_access_checks ();
12575 return template_id;
12578 /* Parse a template-name.
12580 template-name:
12581 identifier
12583 The standard should actually say:
12585 template-name:
12586 identifier
12587 operator-function-id
12589 A defect report has been filed about this issue.
12591 A conversion-function-id cannot be a template name because they cannot
12592 be part of a template-id. In fact, looking at this code:
12594 a.operator K<int>()
12596 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12597 It is impossible to call a templated conversion-function-id with an
12598 explicit argument list, since the only allowed template parameter is
12599 the type to which it is converting.
12601 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12602 `template' keyword, in a construction like:
12604 T::template f<3>()
12606 In that case `f' is taken to be a template-name, even though there
12607 is no way of knowing for sure.
12609 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12610 name refers to a set of overloaded functions, at least one of which
12611 is a template, or an IDENTIFIER_NODE with the name of the template,
12612 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12613 names are looked up inside uninstantiated templates. */
12615 static tree
12616 cp_parser_template_name (cp_parser* parser,
12617 bool template_keyword_p,
12618 bool check_dependency_p,
12619 bool is_declaration,
12620 enum tag_types tag_type,
12621 bool *is_identifier)
12623 tree identifier;
12624 tree decl;
12625 tree fns;
12626 cp_token *token = cp_lexer_peek_token (parser->lexer);
12628 /* If the next token is `operator', then we have either an
12629 operator-function-id or a conversion-function-id. */
12630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12632 /* We don't know whether we're looking at an
12633 operator-function-id or a conversion-function-id. */
12634 cp_parser_parse_tentatively (parser);
12635 /* Try an operator-function-id. */
12636 identifier = cp_parser_operator_function_id (parser);
12637 /* If that didn't work, try a conversion-function-id. */
12638 if (!cp_parser_parse_definitely (parser))
12640 cp_parser_error (parser, "expected template-name");
12641 return error_mark_node;
12644 /* Look for the identifier. */
12645 else
12646 identifier = cp_parser_identifier (parser);
12648 /* If we didn't find an identifier, we don't have a template-id. */
12649 if (identifier == error_mark_node)
12650 return error_mark_node;
12652 /* If the name immediately followed the `template' keyword, then it
12653 is a template-name. However, if the next token is not `<', then
12654 we do not treat it as a template-name, since it is not being used
12655 as part of a template-id. This enables us to handle constructs
12656 like:
12658 template <typename T> struct S { S(); };
12659 template <typename T> S<T>::S();
12661 correctly. We would treat `S' as a template -- if it were `S<T>'
12662 -- but we do not if there is no `<'. */
12664 if (processing_template_decl
12665 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12667 /* In a declaration, in a dependent context, we pretend that the
12668 "template" keyword was present in order to improve error
12669 recovery. For example, given:
12671 template <typename T> void f(T::X<int>);
12673 we want to treat "X<int>" as a template-id. */
12674 if (is_declaration
12675 && !template_keyword_p
12676 && parser->scope && TYPE_P (parser->scope)
12677 && check_dependency_p
12678 && dependent_scope_p (parser->scope)
12679 /* Do not do this for dtors (or ctors), since they never
12680 need the template keyword before their name. */
12681 && !constructor_name_p (identifier, parser->scope))
12683 cp_token_position start = 0;
12685 /* Explain what went wrong. */
12686 error_at (token->location, "non-template %qD used as template",
12687 identifier);
12688 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12689 parser->scope, identifier);
12690 /* If parsing tentatively, find the location of the "<" token. */
12691 if (cp_parser_simulate_error (parser))
12692 start = cp_lexer_token_position (parser->lexer, true);
12693 /* Parse the template arguments so that we can issue error
12694 messages about them. */
12695 cp_lexer_consume_token (parser->lexer);
12696 cp_parser_enclosed_template_argument_list (parser);
12697 /* Skip tokens until we find a good place from which to
12698 continue parsing. */
12699 cp_parser_skip_to_closing_parenthesis (parser,
12700 /*recovering=*/true,
12701 /*or_comma=*/true,
12702 /*consume_paren=*/false);
12703 /* If parsing tentatively, permanently remove the
12704 template argument list. That will prevent duplicate
12705 error messages from being issued about the missing
12706 "template" keyword. */
12707 if (start)
12708 cp_lexer_purge_tokens_after (parser->lexer, start);
12709 if (is_identifier)
12710 *is_identifier = true;
12711 return identifier;
12714 /* If the "template" keyword is present, then there is generally
12715 no point in doing name-lookup, so we just return IDENTIFIER.
12716 But, if the qualifying scope is non-dependent then we can
12717 (and must) do name-lookup normally. */
12718 if (template_keyword_p
12719 && (!parser->scope
12720 || (TYPE_P (parser->scope)
12721 && dependent_type_p (parser->scope))))
12722 return identifier;
12725 /* Look up the name. */
12726 decl = cp_parser_lookup_name (parser, identifier,
12727 tag_type,
12728 /*is_template=*/true,
12729 /*is_namespace=*/false,
12730 check_dependency_p,
12731 /*ambiguous_decls=*/NULL,
12732 token->location);
12734 /* If DECL is a template, then the name was a template-name. */
12735 if (TREE_CODE (decl) == TEMPLATE_DECL)
12737 else
12739 tree fn = NULL_TREE;
12741 /* The standard does not explicitly indicate whether a name that
12742 names a set of overloaded declarations, some of which are
12743 templates, is a template-name. However, such a name should
12744 be a template-name; otherwise, there is no way to form a
12745 template-id for the overloaded templates. */
12746 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12747 if (TREE_CODE (fns) == OVERLOAD)
12748 for (fn = fns; fn; fn = OVL_NEXT (fn))
12749 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12750 break;
12752 if (!fn)
12754 /* The name does not name a template. */
12755 cp_parser_error (parser, "expected template-name");
12756 return error_mark_node;
12760 /* If DECL is dependent, and refers to a function, then just return
12761 its name; we will look it up again during template instantiation. */
12762 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12764 tree scope = ovl_scope (decl);
12765 if (TYPE_P (scope) && dependent_type_p (scope))
12766 return identifier;
12769 return decl;
12772 /* Parse a template-argument-list.
12774 template-argument-list:
12775 template-argument ... [opt]
12776 template-argument-list , template-argument ... [opt]
12778 Returns a TREE_VEC containing the arguments. */
12780 static tree
12781 cp_parser_template_argument_list (cp_parser* parser)
12783 tree fixed_args[10];
12784 unsigned n_args = 0;
12785 unsigned alloced = 10;
12786 tree *arg_ary = fixed_args;
12787 tree vec;
12788 bool saved_in_template_argument_list_p;
12789 bool saved_ice_p;
12790 bool saved_non_ice_p;
12792 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12793 parser->in_template_argument_list_p = true;
12794 /* Even if the template-id appears in an integral
12795 constant-expression, the contents of the argument list do
12796 not. */
12797 saved_ice_p = parser->integral_constant_expression_p;
12798 parser->integral_constant_expression_p = false;
12799 saved_non_ice_p = parser->non_integral_constant_expression_p;
12800 parser->non_integral_constant_expression_p = false;
12802 /* Parse the arguments. */
12805 tree argument;
12807 if (n_args)
12808 /* Consume the comma. */
12809 cp_lexer_consume_token (parser->lexer);
12811 /* Parse the template-argument. */
12812 argument = cp_parser_template_argument (parser);
12814 /* If the next token is an ellipsis, we're expanding a template
12815 argument pack. */
12816 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12818 if (argument == error_mark_node)
12820 cp_token *token = cp_lexer_peek_token (parser->lexer);
12821 error_at (token->location,
12822 "expected parameter pack before %<...%>");
12824 /* Consume the `...' token. */
12825 cp_lexer_consume_token (parser->lexer);
12827 /* Make the argument into a TYPE_PACK_EXPANSION or
12828 EXPR_PACK_EXPANSION. */
12829 argument = make_pack_expansion (argument);
12832 if (n_args == alloced)
12834 alloced *= 2;
12836 if (arg_ary == fixed_args)
12838 arg_ary = XNEWVEC (tree, alloced);
12839 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12841 else
12842 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12844 arg_ary[n_args++] = argument;
12846 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12848 vec = make_tree_vec (n_args);
12850 while (n_args--)
12851 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12853 if (arg_ary != fixed_args)
12854 free (arg_ary);
12855 parser->non_integral_constant_expression_p = saved_non_ice_p;
12856 parser->integral_constant_expression_p = saved_ice_p;
12857 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12858 #ifdef ENABLE_CHECKING
12859 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12860 #endif
12861 return vec;
12864 /* Parse a template-argument.
12866 template-argument:
12867 assignment-expression
12868 type-id
12869 id-expression
12871 The representation is that of an assignment-expression, type-id, or
12872 id-expression -- except that the qualified id-expression is
12873 evaluated, so that the value returned is either a DECL or an
12874 OVERLOAD.
12876 Although the standard says "assignment-expression", it forbids
12877 throw-expressions or assignments in the template argument.
12878 Therefore, we use "conditional-expression" instead. */
12880 static tree
12881 cp_parser_template_argument (cp_parser* parser)
12883 tree argument;
12884 bool template_p;
12885 bool address_p;
12886 bool maybe_type_id = false;
12887 cp_token *token = NULL, *argument_start_token = NULL;
12888 location_t loc = 0;
12889 cp_id_kind idk;
12891 /* There's really no way to know what we're looking at, so we just
12892 try each alternative in order.
12894 [temp.arg]
12896 In a template-argument, an ambiguity between a type-id and an
12897 expression is resolved to a type-id, regardless of the form of
12898 the corresponding template-parameter.
12900 Therefore, we try a type-id first. */
12901 cp_parser_parse_tentatively (parser);
12902 argument = cp_parser_template_type_arg (parser);
12903 /* If there was no error parsing the type-id but the next token is a
12904 '>>', our behavior depends on which dialect of C++ we're
12905 parsing. In C++98, we probably found a typo for '> >'. But there
12906 are type-id which are also valid expressions. For instance:
12908 struct X { int operator >> (int); };
12909 template <int V> struct Foo {};
12910 Foo<X () >> 5> r;
12912 Here 'X()' is a valid type-id of a function type, but the user just
12913 wanted to write the expression "X() >> 5". Thus, we remember that we
12914 found a valid type-id, but we still try to parse the argument as an
12915 expression to see what happens.
12917 In C++0x, the '>>' will be considered two separate '>'
12918 tokens. */
12919 if (!cp_parser_error_occurred (parser)
12920 && cxx_dialect == cxx98
12921 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12923 maybe_type_id = true;
12924 cp_parser_abort_tentative_parse (parser);
12926 else
12928 /* If the next token isn't a `,' or a `>', then this argument wasn't
12929 really finished. This means that the argument is not a valid
12930 type-id. */
12931 if (!cp_parser_next_token_ends_template_argument_p (parser))
12932 cp_parser_error (parser, "expected template-argument");
12933 /* If that worked, we're done. */
12934 if (cp_parser_parse_definitely (parser))
12935 return argument;
12937 /* We're still not sure what the argument will be. */
12938 cp_parser_parse_tentatively (parser);
12939 /* Try a template. */
12940 argument_start_token = cp_lexer_peek_token (parser->lexer);
12941 argument = cp_parser_id_expression (parser,
12942 /*template_keyword_p=*/false,
12943 /*check_dependency_p=*/true,
12944 &template_p,
12945 /*declarator_p=*/false,
12946 /*optional_p=*/false);
12947 /* If the next token isn't a `,' or a `>', then this argument wasn't
12948 really finished. */
12949 if (!cp_parser_next_token_ends_template_argument_p (parser))
12950 cp_parser_error (parser, "expected template-argument");
12951 if (!cp_parser_error_occurred (parser))
12953 /* Figure out what is being referred to. If the id-expression
12954 was for a class template specialization, then we will have a
12955 TYPE_DECL at this point. There is no need to do name lookup
12956 at this point in that case. */
12957 if (TREE_CODE (argument) != TYPE_DECL)
12958 argument = cp_parser_lookup_name (parser, argument,
12959 none_type,
12960 /*is_template=*/template_p,
12961 /*is_namespace=*/false,
12962 /*check_dependency=*/true,
12963 /*ambiguous_decls=*/NULL,
12964 argument_start_token->location);
12965 if (TREE_CODE (argument) != TEMPLATE_DECL
12966 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12967 cp_parser_error (parser, "expected template-name");
12969 if (cp_parser_parse_definitely (parser))
12970 return argument;
12971 /* It must be a non-type argument. There permitted cases are given
12972 in [temp.arg.nontype]:
12974 -- an integral constant-expression of integral or enumeration
12975 type; or
12977 -- the name of a non-type template-parameter; or
12979 -- the name of an object or function with external linkage...
12981 -- the address of an object or function with external linkage...
12983 -- a pointer to member... */
12984 /* Look for a non-type template parameter. */
12985 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12987 cp_parser_parse_tentatively (parser);
12988 argument = cp_parser_primary_expression (parser,
12989 /*address_p=*/false,
12990 /*cast_p=*/false,
12991 /*template_arg_p=*/true,
12992 &idk);
12993 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12994 || !cp_parser_next_token_ends_template_argument_p (parser))
12995 cp_parser_simulate_error (parser);
12996 if (cp_parser_parse_definitely (parser))
12997 return argument;
13000 /* If the next token is "&", the argument must be the address of an
13001 object or function with external linkage. */
13002 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13003 if (address_p)
13005 loc = cp_lexer_peek_token (parser->lexer)->location;
13006 cp_lexer_consume_token (parser->lexer);
13008 /* See if we might have an id-expression. */
13009 token = cp_lexer_peek_token (parser->lexer);
13010 if (token->type == CPP_NAME
13011 || token->keyword == RID_OPERATOR
13012 || token->type == CPP_SCOPE
13013 || token->type == CPP_TEMPLATE_ID
13014 || token->type == CPP_NESTED_NAME_SPECIFIER)
13016 cp_parser_parse_tentatively (parser);
13017 argument = cp_parser_primary_expression (parser,
13018 address_p,
13019 /*cast_p=*/false,
13020 /*template_arg_p=*/true,
13021 &idk);
13022 if (cp_parser_error_occurred (parser)
13023 || !cp_parser_next_token_ends_template_argument_p (parser))
13024 cp_parser_abort_tentative_parse (parser);
13025 else
13027 tree probe;
13029 if (TREE_CODE (argument) == INDIRECT_REF)
13031 gcc_assert (REFERENCE_REF_P (argument));
13032 argument = TREE_OPERAND (argument, 0);
13035 /* If we're in a template, we represent a qualified-id referring
13036 to a static data member as a SCOPE_REF even if the scope isn't
13037 dependent so that we can check access control later. */
13038 probe = argument;
13039 if (TREE_CODE (probe) == SCOPE_REF)
13040 probe = TREE_OPERAND (probe, 1);
13041 if (TREE_CODE (probe) == VAR_DECL)
13043 /* A variable without external linkage might still be a
13044 valid constant-expression, so no error is issued here
13045 if the external-linkage check fails. */
13046 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13047 cp_parser_simulate_error (parser);
13049 else if (is_overloaded_fn (argument))
13050 /* All overloaded functions are allowed; if the external
13051 linkage test does not pass, an error will be issued
13052 later. */
13054 else if (address_p
13055 && (TREE_CODE (argument) == OFFSET_REF
13056 || TREE_CODE (argument) == SCOPE_REF))
13057 /* A pointer-to-member. */
13059 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13061 else
13062 cp_parser_simulate_error (parser);
13064 if (cp_parser_parse_definitely (parser))
13066 if (address_p)
13067 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13068 tf_warning_or_error);
13069 return argument;
13073 /* If the argument started with "&", there are no other valid
13074 alternatives at this point. */
13075 if (address_p)
13077 cp_parser_error (parser, "invalid non-type template argument");
13078 return error_mark_node;
13081 /* If the argument wasn't successfully parsed as a type-id followed
13082 by '>>', the argument can only be a constant expression now.
13083 Otherwise, we try parsing the constant-expression tentatively,
13084 because the argument could really be a type-id. */
13085 if (maybe_type_id)
13086 cp_parser_parse_tentatively (parser);
13087 argument = cp_parser_constant_expression (parser,
13088 /*allow_non_constant_p=*/false,
13089 /*non_constant_p=*/NULL);
13090 argument = fold_non_dependent_expr (argument);
13091 if (!maybe_type_id)
13092 return argument;
13093 if (!cp_parser_next_token_ends_template_argument_p (parser))
13094 cp_parser_error (parser, "expected template-argument");
13095 if (cp_parser_parse_definitely (parser))
13096 return argument;
13097 /* We did our best to parse the argument as a non type-id, but that
13098 was the only alternative that matched (albeit with a '>' after
13099 it). We can assume it's just a typo from the user, and a
13100 diagnostic will then be issued. */
13101 return cp_parser_template_type_arg (parser);
13104 /* Parse an explicit-instantiation.
13106 explicit-instantiation:
13107 template declaration
13109 Although the standard says `declaration', what it really means is:
13111 explicit-instantiation:
13112 template decl-specifier-seq [opt] declarator [opt] ;
13114 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13115 supposed to be allowed. A defect report has been filed about this
13116 issue.
13118 GNU Extension:
13120 explicit-instantiation:
13121 storage-class-specifier template
13122 decl-specifier-seq [opt] declarator [opt] ;
13123 function-specifier template
13124 decl-specifier-seq [opt] declarator [opt] ; */
13126 static void
13127 cp_parser_explicit_instantiation (cp_parser* parser)
13129 int declares_class_or_enum;
13130 cp_decl_specifier_seq decl_specifiers;
13131 tree extension_specifier = NULL_TREE;
13133 timevar_push (TV_TEMPLATE_INST);
13135 /* Look for an (optional) storage-class-specifier or
13136 function-specifier. */
13137 if (cp_parser_allow_gnu_extensions_p (parser))
13139 extension_specifier
13140 = cp_parser_storage_class_specifier_opt (parser);
13141 if (!extension_specifier)
13142 extension_specifier
13143 = cp_parser_function_specifier_opt (parser,
13144 /*decl_specs=*/NULL);
13147 /* Look for the `template' keyword. */
13148 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13149 /* Let the front end know that we are processing an explicit
13150 instantiation. */
13151 begin_explicit_instantiation ();
13152 /* [temp.explicit] says that we are supposed to ignore access
13153 control while processing explicit instantiation directives. */
13154 push_deferring_access_checks (dk_no_check);
13155 /* Parse a decl-specifier-seq. */
13156 cp_parser_decl_specifier_seq (parser,
13157 CP_PARSER_FLAGS_OPTIONAL,
13158 &decl_specifiers,
13159 &declares_class_or_enum);
13160 /* If there was exactly one decl-specifier, and it declared a class,
13161 and there's no declarator, then we have an explicit type
13162 instantiation. */
13163 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13165 tree type;
13167 type = check_tag_decl (&decl_specifiers);
13168 /* Turn access control back on for names used during
13169 template instantiation. */
13170 pop_deferring_access_checks ();
13171 if (type)
13172 do_type_instantiation (type, extension_specifier,
13173 /*complain=*/tf_error);
13175 else
13177 cp_declarator *declarator;
13178 tree decl;
13180 /* Parse the declarator. */
13181 declarator
13182 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13183 /*ctor_dtor_or_conv_p=*/NULL,
13184 /*parenthesized_p=*/NULL,
13185 /*member_p=*/false);
13186 if (declares_class_or_enum & 2)
13187 cp_parser_check_for_definition_in_return_type (declarator,
13188 decl_specifiers.type,
13189 decl_specifiers.locations[ds_type_spec]);
13190 if (declarator != cp_error_declarator)
13192 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13193 permerror (decl_specifiers.locations[ds_inline],
13194 "explicit instantiation shall not use"
13195 " %<inline%> specifier");
13196 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13197 permerror (decl_specifiers.locations[ds_constexpr],
13198 "explicit instantiation shall not use"
13199 " %<constexpr%> specifier");
13201 decl = grokdeclarator (declarator, &decl_specifiers,
13202 NORMAL, 0, &decl_specifiers.attributes);
13203 /* Turn access control back on for names used during
13204 template instantiation. */
13205 pop_deferring_access_checks ();
13206 /* Do the explicit instantiation. */
13207 do_decl_instantiation (decl, extension_specifier);
13209 else
13211 pop_deferring_access_checks ();
13212 /* Skip the body of the explicit instantiation. */
13213 cp_parser_skip_to_end_of_statement (parser);
13216 /* We're done with the instantiation. */
13217 end_explicit_instantiation ();
13219 cp_parser_consume_semicolon_at_end_of_statement (parser);
13221 timevar_pop (TV_TEMPLATE_INST);
13224 /* Parse an explicit-specialization.
13226 explicit-specialization:
13227 template < > declaration
13229 Although the standard says `declaration', what it really means is:
13231 explicit-specialization:
13232 template <> decl-specifier [opt] init-declarator [opt] ;
13233 template <> function-definition
13234 template <> explicit-specialization
13235 template <> template-declaration */
13237 static void
13238 cp_parser_explicit_specialization (cp_parser* parser)
13240 bool need_lang_pop;
13241 cp_token *token = cp_lexer_peek_token (parser->lexer);
13243 /* Look for the `template' keyword. */
13244 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13245 /* Look for the `<'. */
13246 cp_parser_require (parser, CPP_LESS, RT_LESS);
13247 /* Look for the `>'. */
13248 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13249 /* We have processed another parameter list. */
13250 ++parser->num_template_parameter_lists;
13251 /* [temp]
13253 A template ... explicit specialization ... shall not have C
13254 linkage. */
13255 if (current_lang_name == lang_name_c)
13257 error_at (token->location, "template specialization with C linkage");
13258 /* Give it C++ linkage to avoid confusing other parts of the
13259 front end. */
13260 push_lang_context (lang_name_cplusplus);
13261 need_lang_pop = true;
13263 else
13264 need_lang_pop = false;
13265 /* Let the front end know that we are beginning a specialization. */
13266 if (!begin_specialization ())
13268 end_specialization ();
13269 return;
13272 /* If the next keyword is `template', we need to figure out whether
13273 or not we're looking a template-declaration. */
13274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13276 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13277 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13278 cp_parser_template_declaration_after_export (parser,
13279 /*member_p=*/false);
13280 else
13281 cp_parser_explicit_specialization (parser);
13283 else
13284 /* Parse the dependent declaration. */
13285 cp_parser_single_declaration (parser,
13286 /*checks=*/NULL,
13287 /*member_p=*/false,
13288 /*explicit_specialization_p=*/true,
13289 /*friend_p=*/NULL);
13290 /* We're done with the specialization. */
13291 end_specialization ();
13292 /* For the erroneous case of a template with C linkage, we pushed an
13293 implicit C++ linkage scope; exit that scope now. */
13294 if (need_lang_pop)
13295 pop_lang_context ();
13296 /* We're done with this parameter list. */
13297 --parser->num_template_parameter_lists;
13300 /* Parse a type-specifier.
13302 type-specifier:
13303 simple-type-specifier
13304 class-specifier
13305 enum-specifier
13306 elaborated-type-specifier
13307 cv-qualifier
13309 GNU Extension:
13311 type-specifier:
13312 __complex__
13314 Returns a representation of the type-specifier. For a
13315 class-specifier, enum-specifier, or elaborated-type-specifier, a
13316 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13318 The parser flags FLAGS is used to control type-specifier parsing.
13320 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13321 in a decl-specifier-seq.
13323 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13324 class-specifier, enum-specifier, or elaborated-type-specifier, then
13325 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13326 if a type is declared; 2 if it is defined. Otherwise, it is set to
13327 zero.
13329 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13330 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13331 is set to FALSE. */
13333 static tree
13334 cp_parser_type_specifier (cp_parser* parser,
13335 cp_parser_flags flags,
13336 cp_decl_specifier_seq *decl_specs,
13337 bool is_declaration,
13338 int* declares_class_or_enum,
13339 bool* is_cv_qualifier)
13341 tree type_spec = NULL_TREE;
13342 cp_token *token;
13343 enum rid keyword;
13344 cp_decl_spec ds = ds_last;
13346 /* Assume this type-specifier does not declare a new type. */
13347 if (declares_class_or_enum)
13348 *declares_class_or_enum = 0;
13349 /* And that it does not specify a cv-qualifier. */
13350 if (is_cv_qualifier)
13351 *is_cv_qualifier = false;
13352 /* Peek at the next token. */
13353 token = cp_lexer_peek_token (parser->lexer);
13355 /* If we're looking at a keyword, we can use that to guide the
13356 production we choose. */
13357 keyword = token->keyword;
13358 switch (keyword)
13360 case RID_ENUM:
13361 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13362 goto elaborated_type_specifier;
13364 /* Look for the enum-specifier. */
13365 type_spec = cp_parser_enum_specifier (parser);
13366 /* If that worked, we're done. */
13367 if (type_spec)
13369 if (declares_class_or_enum)
13370 *declares_class_or_enum = 2;
13371 if (decl_specs)
13372 cp_parser_set_decl_spec_type (decl_specs,
13373 type_spec,
13374 token->location,
13375 /*type_definition_p=*/true);
13376 return type_spec;
13378 else
13379 goto elaborated_type_specifier;
13381 /* Any of these indicate either a class-specifier, or an
13382 elaborated-type-specifier. */
13383 case RID_CLASS:
13384 case RID_STRUCT:
13385 case RID_UNION:
13386 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13387 goto elaborated_type_specifier;
13389 /* Parse tentatively so that we can back up if we don't find a
13390 class-specifier. */
13391 cp_parser_parse_tentatively (parser);
13392 /* Look for the class-specifier. */
13393 type_spec = cp_parser_class_specifier (parser);
13394 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13395 /* If that worked, we're done. */
13396 if (cp_parser_parse_definitely (parser))
13398 if (declares_class_or_enum)
13399 *declares_class_or_enum = 2;
13400 if (decl_specs)
13401 cp_parser_set_decl_spec_type (decl_specs,
13402 type_spec,
13403 token->location,
13404 /*type_definition_p=*/true);
13405 return type_spec;
13408 /* Fall through. */
13409 elaborated_type_specifier:
13410 /* We're declaring (not defining) a class or enum. */
13411 if (declares_class_or_enum)
13412 *declares_class_or_enum = 1;
13414 /* Fall through. */
13415 case RID_TYPENAME:
13416 /* Look for an elaborated-type-specifier. */
13417 type_spec
13418 = (cp_parser_elaborated_type_specifier
13419 (parser,
13420 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
13421 is_declaration));
13422 if (decl_specs)
13423 cp_parser_set_decl_spec_type (decl_specs,
13424 type_spec,
13425 token->location,
13426 /*type_definition_p=*/false);
13427 return type_spec;
13429 case RID_CONST:
13430 ds = ds_const;
13431 if (is_cv_qualifier)
13432 *is_cv_qualifier = true;
13433 break;
13435 case RID_VOLATILE:
13436 ds = ds_volatile;
13437 if (is_cv_qualifier)
13438 *is_cv_qualifier = true;
13439 break;
13441 case RID_RESTRICT:
13442 ds = ds_restrict;
13443 if (is_cv_qualifier)
13444 *is_cv_qualifier = true;
13445 break;
13447 case RID_COMPLEX:
13448 /* The `__complex__' keyword is a GNU extension. */
13449 ds = ds_complex;
13450 break;
13452 default:
13453 break;
13456 /* Handle simple keywords. */
13457 if (ds != ds_last)
13459 if (decl_specs)
13461 set_and_check_decl_spec_loc (decl_specs, ds, token->location);
13462 decl_specs->any_specifiers_p = true;
13464 return cp_lexer_consume_token (parser->lexer)->u.value;
13467 /* If we do not already have a type-specifier, assume we are looking
13468 at a simple-type-specifier. */
13469 type_spec = cp_parser_simple_type_specifier (parser,
13470 decl_specs,
13471 flags);
13473 /* If we didn't find a type-specifier, and a type-specifier was not
13474 optional in this context, issue an error message. */
13475 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13477 cp_parser_error (parser, "expected type specifier");
13478 return error_mark_node;
13481 return type_spec;
13484 /* Parse a simple-type-specifier.
13486 simple-type-specifier:
13487 :: [opt] nested-name-specifier [opt] type-name
13488 :: [opt] nested-name-specifier template template-id
13489 char
13490 wchar_t
13491 bool
13492 short
13494 long
13495 signed
13496 unsigned
13497 float
13498 double
13499 void
13501 C++0x Extension:
13503 simple-type-specifier:
13504 auto
13505 decltype ( expression )
13506 char16_t
13507 char32_t
13508 __underlying_type ( type-id )
13510 GNU Extension:
13512 simple-type-specifier:
13513 __int128
13514 __typeof__ unary-expression
13515 __typeof__ ( type-id )
13517 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13518 appropriately updated. */
13520 static tree
13521 cp_parser_simple_type_specifier (cp_parser* parser,
13522 cp_decl_specifier_seq *decl_specs,
13523 cp_parser_flags flags)
13525 tree type = NULL_TREE;
13526 cp_token *token;
13528 /* Peek at the next token. */
13529 token = cp_lexer_peek_token (parser->lexer);
13531 /* If we're looking at a keyword, things are easy. */
13532 switch (token->keyword)
13534 case RID_CHAR:
13535 if (decl_specs)
13536 decl_specs->explicit_char_p = true;
13537 type = char_type_node;
13538 break;
13539 case RID_CHAR16:
13540 type = char16_type_node;
13541 break;
13542 case RID_CHAR32:
13543 type = char32_type_node;
13544 break;
13545 case RID_WCHAR:
13546 type = wchar_type_node;
13547 break;
13548 case RID_BOOL:
13549 type = boolean_type_node;
13550 break;
13551 case RID_SHORT:
13552 set_and_check_decl_spec_loc (decl_specs, ds_short, token->location);
13553 type = short_integer_type_node;
13554 break;
13555 case RID_INT:
13556 if (decl_specs)
13557 decl_specs->explicit_int_p = true;
13558 type = integer_type_node;
13559 break;
13560 case RID_INT128:
13561 if (!int128_integer_type_node)
13562 break;
13563 if (decl_specs)
13564 decl_specs->explicit_int128_p = true;
13565 type = int128_integer_type_node;
13566 break;
13567 case RID_LONG:
13568 if (decl_specs)
13569 set_and_check_decl_spec_loc (decl_specs, ds_long, token->location);
13570 type = long_integer_type_node;
13571 break;
13572 case RID_SIGNED:
13573 set_and_check_decl_spec_loc (decl_specs, ds_signed, token->location);
13574 type = integer_type_node;
13575 break;
13576 case RID_UNSIGNED:
13577 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token->location);
13578 type = unsigned_type_node;
13579 break;
13580 case RID_FLOAT:
13581 type = float_type_node;
13582 break;
13583 case RID_DOUBLE:
13584 type = double_type_node;
13585 break;
13586 case RID_VOID:
13587 type = void_type_node;
13588 break;
13590 case RID_AUTO:
13591 maybe_warn_cpp0x (CPP0X_AUTO);
13592 type = make_auto ();
13593 break;
13595 case RID_DECLTYPE:
13596 /* Since DR 743, decltype can either be a simple-type-specifier by
13597 itself or begin a nested-name-specifier. Parsing it will replace
13598 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13599 handling below decide what to do. */
13600 cp_parser_decltype (parser);
13601 cp_lexer_set_token_position (parser->lexer, token);
13602 break;
13604 case RID_TYPEOF:
13605 /* Consume the `typeof' token. */
13606 cp_lexer_consume_token (parser->lexer);
13607 /* Parse the operand to `typeof'. */
13608 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13609 /* If it is not already a TYPE, take its type. */
13610 if (!TYPE_P (type))
13611 type = finish_typeof (type);
13613 if (decl_specs)
13614 cp_parser_set_decl_spec_type (decl_specs, type,
13615 token->location,
13616 /*type_definition_p=*/false);
13618 return type;
13620 case RID_UNDERLYING_TYPE:
13621 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13622 if (decl_specs)
13623 cp_parser_set_decl_spec_type (decl_specs, type,
13624 token->location,
13625 /*type_definition_p=*/false);
13627 return type;
13629 case RID_BASES:
13630 case RID_DIRECT_BASES:
13631 type = cp_parser_trait_expr (parser, token->keyword);
13632 if (decl_specs)
13633 cp_parser_set_decl_spec_type (decl_specs, type,
13634 token->location,
13635 /*type_definition_p=*/false);
13636 return type;
13637 default:
13638 break;
13641 /* If token is an already-parsed decltype not followed by ::,
13642 it's a simple-type-specifier. */
13643 if (token->type == CPP_DECLTYPE
13644 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13646 type = token->u.value;
13647 if (decl_specs)
13648 cp_parser_set_decl_spec_type (decl_specs, type,
13649 token->location,
13650 /*type_definition_p=*/false);
13651 cp_lexer_consume_token (parser->lexer);
13652 return type;
13655 /* If the type-specifier was for a built-in type, we're done. */
13656 if (type)
13658 /* Record the type. */
13659 if (decl_specs
13660 && (token->keyword != RID_SIGNED
13661 && token->keyword != RID_UNSIGNED
13662 && token->keyword != RID_SHORT
13663 && token->keyword != RID_LONG))
13664 cp_parser_set_decl_spec_type (decl_specs,
13665 type,
13666 token->location,
13667 /*type_definition_p=*/false);
13668 if (decl_specs)
13669 decl_specs->any_specifiers_p = true;
13671 /* Consume the token. */
13672 cp_lexer_consume_token (parser->lexer);
13674 /* There is no valid C++ program where a non-template type is
13675 followed by a "<". That usually indicates that the user thought
13676 that the type was a template. */
13677 cp_parser_check_for_invalid_template_id (parser, type, none_type,
13678 token->location);
13680 return TYPE_NAME (type);
13683 /* The type-specifier must be a user-defined type. */
13684 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13686 bool qualified_p;
13687 bool global_p;
13689 /* Don't gobble tokens or issue error messages if this is an
13690 optional type-specifier. */
13691 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13692 cp_parser_parse_tentatively (parser);
13694 /* Look for the optional `::' operator. */
13695 global_p
13696 = (cp_parser_global_scope_opt (parser,
13697 /*current_scope_valid_p=*/false)
13698 != NULL_TREE);
13699 /* Look for the nested-name specifier. */
13700 qualified_p
13701 = (cp_parser_nested_name_specifier_opt (parser,
13702 /*typename_keyword_p=*/false,
13703 /*check_dependency_p=*/true,
13704 /*type_p=*/false,
13705 /*is_declaration=*/false)
13706 != NULL_TREE);
13707 token = cp_lexer_peek_token (parser->lexer);
13708 /* If we have seen a nested-name-specifier, and the next token
13709 is `template', then we are using the template-id production. */
13710 if (parser->scope
13711 && cp_parser_optional_template_keyword (parser))
13713 /* Look for the template-id. */
13714 type = cp_parser_template_id (parser,
13715 /*template_keyword_p=*/true,
13716 /*check_dependency_p=*/true,
13717 none_type,
13718 /*is_declaration=*/false);
13719 /* If the template-id did not name a type, we are out of
13720 luck. */
13721 if (TREE_CODE (type) != TYPE_DECL)
13723 cp_parser_error (parser, "expected template-id for type");
13724 type = NULL_TREE;
13727 /* Otherwise, look for a type-name. */
13728 else
13729 type = cp_parser_type_name (parser);
13730 /* Keep track of all name-lookups performed in class scopes. */
13731 if (type
13732 && !global_p
13733 && !qualified_p
13734 && TREE_CODE (type) == TYPE_DECL
13735 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13736 maybe_note_name_used_in_class (DECL_NAME (type), type);
13737 /* If it didn't work out, we don't have a TYPE. */
13738 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13739 && !cp_parser_parse_definitely (parser))
13740 type = NULL_TREE;
13741 if (type && decl_specs)
13742 cp_parser_set_decl_spec_type (decl_specs, type,
13743 token->location,
13744 /*type_definition_p=*/false);
13747 /* If we didn't get a type-name, issue an error message. */
13748 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13750 cp_parser_error (parser, "expected type-name");
13751 return error_mark_node;
13754 if (type && type != error_mark_node)
13756 /* See if TYPE is an Objective-C type, and if so, parse and
13757 accept any protocol references following it. Do this before
13758 the cp_parser_check_for_invalid_template_id() call, because
13759 Objective-C types can be followed by '<...>' which would
13760 enclose protocol names rather than template arguments, and so
13761 everything is fine. */
13762 if (c_dialect_objc () && !parser->scope
13763 && (objc_is_id (type) || objc_is_class_name (type)))
13765 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13766 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13768 /* Clobber the "unqualified" type previously entered into
13769 DECL_SPECS with the new, improved protocol-qualified version. */
13770 if (decl_specs)
13771 decl_specs->type = qual_type;
13773 return qual_type;
13776 /* There is no valid C++ program where a non-template type is
13777 followed by a "<". That usually indicates that the user
13778 thought that the type was a template. */
13779 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13780 none_type,
13781 token->location);
13784 return type;
13787 /* Parse a type-name.
13789 type-name:
13790 class-name
13791 enum-name
13792 typedef-name
13793 simple-template-id [in c++0x]
13795 enum-name:
13796 identifier
13798 typedef-name:
13799 identifier
13801 Returns a TYPE_DECL for the type. */
13803 static tree
13804 cp_parser_type_name (cp_parser* parser)
13806 tree type_decl;
13808 /* We can't know yet whether it is a class-name or not. */
13809 cp_parser_parse_tentatively (parser);
13810 /* Try a class-name. */
13811 type_decl = cp_parser_class_name (parser,
13812 /*typename_keyword_p=*/false,
13813 /*template_keyword_p=*/false,
13814 none_type,
13815 /*check_dependency_p=*/true,
13816 /*class_head_p=*/false,
13817 /*is_declaration=*/false);
13818 /* If it's not a class-name, keep looking. */
13819 if (!cp_parser_parse_definitely (parser))
13821 if (cxx_dialect < cxx0x)
13822 /* It must be a typedef-name or an enum-name. */
13823 return cp_parser_nonclass_name (parser);
13825 cp_parser_parse_tentatively (parser);
13826 /* It is either a simple-template-id representing an
13827 instantiation of an alias template... */
13828 type_decl = cp_parser_template_id (parser,
13829 /*template_keyword_p=*/false,
13830 /*check_dependency_p=*/false,
13831 none_type,
13832 /*is_declaration=*/false);
13833 /* Note that this must be an instantiation of an alias template
13834 because [temp.names]/6 says:
13836 A template-id that names an alias template specialization
13837 is a type-name.
13839 Whereas [temp.names]/7 says:
13841 A simple-template-id that names a class template
13842 specialization is a class-name. */
13843 if (type_decl != NULL_TREE
13844 && TREE_CODE (type_decl) == TYPE_DECL
13845 && TYPE_DECL_ALIAS_P (type_decl))
13846 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13847 else
13848 cp_parser_simulate_error (parser);
13850 if (!cp_parser_parse_definitely (parser))
13851 /* ... Or a typedef-name or an enum-name. */
13852 return cp_parser_nonclass_name (parser);
13855 return type_decl;
13858 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13860 enum-name:
13861 identifier
13863 typedef-name:
13864 identifier
13866 Returns a TYPE_DECL for the type. */
13868 static tree
13869 cp_parser_nonclass_name (cp_parser* parser)
13871 tree type_decl;
13872 tree identifier;
13874 cp_token *token = cp_lexer_peek_token (parser->lexer);
13875 identifier = cp_parser_identifier (parser);
13876 if (identifier == error_mark_node)
13877 return error_mark_node;
13879 /* Look up the type-name. */
13880 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13882 if (TREE_CODE (type_decl) == USING_DECL)
13884 if (!DECL_DEPENDENT_P (type_decl))
13885 type_decl = strip_using_decl (type_decl);
13886 else if (USING_DECL_TYPENAME_P (type_decl))
13888 /* We have found a type introduced by a using
13889 declaration at class scope that refers to a dependent
13890 type.
13892 using typename :: [opt] nested-name-specifier unqualified-id ;
13894 type_decl = make_typename_type (TREE_TYPE (type_decl),
13895 DECL_NAME (type_decl),
13896 typename_type, tf_error);
13897 if (type_decl != error_mark_node)
13898 type_decl = TYPE_NAME (type_decl);
13902 if (TREE_CODE (type_decl) != TYPE_DECL
13903 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13905 /* See if this is an Objective-C type. */
13906 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13907 tree type = objc_get_protocol_qualified_type (identifier, protos);
13908 if (type)
13909 type_decl = TYPE_NAME (type);
13912 /* Issue an error if we did not find a type-name. */
13913 if (TREE_CODE (type_decl) != TYPE_DECL
13914 /* In Objective-C, we have the complication that class names are
13915 normally type names and start declarations (eg, the
13916 "NSObject" in "NSObject *object;"), but can be used in an
13917 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13918 is an expression. So, a classname followed by a dot is not a
13919 valid type-name. */
13920 || (objc_is_class_name (TREE_TYPE (type_decl))
13921 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13923 if (!cp_parser_simulate_error (parser))
13924 cp_parser_name_lookup_error (parser, identifier, type_decl,
13925 NLE_TYPE, token->location);
13926 return error_mark_node;
13928 /* Remember that the name was used in the definition of the
13929 current class so that we can check later to see if the
13930 meaning would have been different after the class was
13931 entirely defined. */
13932 else if (type_decl != error_mark_node
13933 && !parser->scope)
13934 maybe_note_name_used_in_class (identifier, type_decl);
13936 return type_decl;
13939 /* Parse an elaborated-type-specifier. Note that the grammar given
13940 here incorporates the resolution to DR68.
13942 elaborated-type-specifier:
13943 class-key :: [opt] nested-name-specifier [opt] identifier
13944 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13945 enum-key :: [opt] nested-name-specifier [opt] identifier
13946 typename :: [opt] nested-name-specifier identifier
13947 typename :: [opt] nested-name-specifier template [opt]
13948 template-id
13950 GNU extension:
13952 elaborated-type-specifier:
13953 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13954 class-key attributes :: [opt] nested-name-specifier [opt]
13955 template [opt] template-id
13956 enum attributes :: [opt] nested-name-specifier [opt] identifier
13958 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13959 declared `friend'. If IS_DECLARATION is TRUE, then this
13960 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13961 something is being declared.
13963 Returns the TYPE specified. */
13965 static tree
13966 cp_parser_elaborated_type_specifier (cp_parser* parser,
13967 bool is_friend,
13968 bool is_declaration)
13970 enum tag_types tag_type;
13971 tree identifier;
13972 tree type = NULL_TREE;
13973 tree attributes = NULL_TREE;
13974 tree globalscope;
13975 cp_token *token = NULL;
13977 /* See if we're looking at the `enum' keyword. */
13978 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13980 /* Consume the `enum' token. */
13981 cp_lexer_consume_token (parser->lexer);
13982 /* Remember that it's an enumeration type. */
13983 tag_type = enum_type;
13984 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13985 enums) is used here. */
13986 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13987 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13989 pedwarn (input_location, 0, "elaborated-type-specifier "
13990 "for a scoped enum must not use the %<%D%> keyword",
13991 cp_lexer_peek_token (parser->lexer)->u.value);
13992 /* Consume the `struct' or `class' and parse it anyway. */
13993 cp_lexer_consume_token (parser->lexer);
13995 /* Parse the attributes. */
13996 attributes = cp_parser_attributes_opt (parser);
13998 /* Or, it might be `typename'. */
13999 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14000 RID_TYPENAME))
14002 /* Consume the `typename' token. */
14003 cp_lexer_consume_token (parser->lexer);
14004 /* Remember that it's a `typename' type. */
14005 tag_type = typename_type;
14007 /* Otherwise it must be a class-key. */
14008 else
14010 tag_type = cp_parser_class_key (parser);
14011 if (tag_type == none_type)
14012 return error_mark_node;
14013 /* Parse the attributes. */
14014 attributes = cp_parser_attributes_opt (parser);
14017 /* Look for the `::' operator. */
14018 globalscope = cp_parser_global_scope_opt (parser,
14019 /*current_scope_valid_p=*/false);
14020 /* Look for the nested-name-specifier. */
14021 if (tag_type == typename_type && !globalscope)
14023 if (!cp_parser_nested_name_specifier (parser,
14024 /*typename_keyword_p=*/true,
14025 /*check_dependency_p=*/true,
14026 /*type_p=*/true,
14027 is_declaration))
14028 return error_mark_node;
14030 else
14031 /* Even though `typename' is not present, the proposed resolution
14032 to Core Issue 180 says that in `class A<T>::B', `B' should be
14033 considered a type-name, even if `A<T>' is dependent. */
14034 cp_parser_nested_name_specifier_opt (parser,
14035 /*typename_keyword_p=*/true,
14036 /*check_dependency_p=*/true,
14037 /*type_p=*/true,
14038 is_declaration);
14039 /* For everything but enumeration types, consider a template-id.
14040 For an enumeration type, consider only a plain identifier. */
14041 if (tag_type != enum_type)
14043 bool template_p = false;
14044 tree decl;
14046 /* Allow the `template' keyword. */
14047 template_p = cp_parser_optional_template_keyword (parser);
14048 /* If we didn't see `template', we don't know if there's a
14049 template-id or not. */
14050 if (!template_p)
14051 cp_parser_parse_tentatively (parser);
14052 /* Parse the template-id. */
14053 token = cp_lexer_peek_token (parser->lexer);
14054 decl = cp_parser_template_id (parser, template_p,
14055 /*check_dependency_p=*/true,
14056 tag_type,
14057 is_declaration);
14058 /* If we didn't find a template-id, look for an ordinary
14059 identifier. */
14060 if (!template_p && !cp_parser_parse_definitely (parser))
14062 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14063 in effect, then we must assume that, upon instantiation, the
14064 template will correspond to a class. */
14065 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14066 && tag_type == typename_type)
14067 type = make_typename_type (parser->scope, decl,
14068 typename_type,
14069 /*complain=*/tf_error);
14070 /* If the `typename' keyword is in effect and DECL is not a type
14071 decl. Then type is non existant. */
14072 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14073 type = NULL_TREE;
14074 else
14075 type = check_elaborated_type_specifier (tag_type, decl,
14076 /*allow_template_p=*/true);
14079 if (!type)
14081 token = cp_lexer_peek_token (parser->lexer);
14082 identifier = cp_parser_identifier (parser);
14084 if (identifier == error_mark_node)
14086 parser->scope = NULL_TREE;
14087 return error_mark_node;
14090 /* For a `typename', we needn't call xref_tag. */
14091 if (tag_type == typename_type
14092 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14093 return cp_parser_make_typename_type (parser, parser->scope,
14094 identifier,
14095 token->location);
14096 /* Look up a qualified name in the usual way. */
14097 if (parser->scope)
14099 tree decl;
14100 tree ambiguous_decls;
14102 decl = cp_parser_lookup_name (parser, identifier,
14103 tag_type,
14104 /*is_template=*/false,
14105 /*is_namespace=*/false,
14106 /*check_dependency=*/true,
14107 &ambiguous_decls,
14108 token->location);
14110 /* If the lookup was ambiguous, an error will already have been
14111 issued. */
14112 if (ambiguous_decls)
14113 return error_mark_node;
14115 /* If we are parsing friend declaration, DECL may be a
14116 TEMPLATE_DECL tree node here. However, we need to check
14117 whether this TEMPLATE_DECL results in valid code. Consider
14118 the following example:
14120 namespace N {
14121 template <class T> class C {};
14123 class X {
14124 template <class T> friend class N::C; // #1, valid code
14126 template <class T> class Y {
14127 friend class N::C; // #2, invalid code
14130 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14131 name lookup of `N::C'. We see that friend declaration must
14132 be template for the code to be valid. Note that
14133 processing_template_decl does not work here since it is
14134 always 1 for the above two cases. */
14136 decl = (cp_parser_maybe_treat_template_as_class
14137 (decl, /*tag_name_p=*/is_friend
14138 && parser->num_template_parameter_lists));
14140 if (TREE_CODE (decl) != TYPE_DECL)
14142 cp_parser_diagnose_invalid_type_name (parser,
14143 parser->scope,
14144 identifier,
14145 token->location);
14146 return error_mark_node;
14149 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14151 bool allow_template = (parser->num_template_parameter_lists
14152 || DECL_SELF_REFERENCE_P (decl));
14153 type = check_elaborated_type_specifier (tag_type, decl,
14154 allow_template);
14156 if (type == error_mark_node)
14157 return error_mark_node;
14160 /* Forward declarations of nested types, such as
14162 class C1::C2;
14163 class C1::C2::C3;
14165 are invalid unless all components preceding the final '::'
14166 are complete. If all enclosing types are complete, these
14167 declarations become merely pointless.
14169 Invalid forward declarations of nested types are errors
14170 caught elsewhere in parsing. Those that are pointless arrive
14171 here. */
14173 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14174 && !is_friend && !processing_explicit_instantiation)
14175 warning (0, "declaration %qD does not declare anything", decl);
14177 type = TREE_TYPE (decl);
14179 else
14181 /* An elaborated-type-specifier sometimes introduces a new type and
14182 sometimes names an existing type. Normally, the rule is that it
14183 introduces a new type only if there is not an existing type of
14184 the same name already in scope. For example, given:
14186 struct S {};
14187 void f() { struct S s; }
14189 the `struct S' in the body of `f' is the same `struct S' as in
14190 the global scope; the existing definition is used. However, if
14191 there were no global declaration, this would introduce a new
14192 local class named `S'.
14194 An exception to this rule applies to the following code:
14196 namespace N { struct S; }
14198 Here, the elaborated-type-specifier names a new type
14199 unconditionally; even if there is already an `S' in the
14200 containing scope this declaration names a new type.
14201 This exception only applies if the elaborated-type-specifier
14202 forms the complete declaration:
14204 [class.name]
14206 A declaration consisting solely of `class-key identifier ;' is
14207 either a redeclaration of the name in the current scope or a
14208 forward declaration of the identifier as a class name. It
14209 introduces the name into the current scope.
14211 We are in this situation precisely when the next token is a `;'.
14213 An exception to the exception is that a `friend' declaration does
14214 *not* name a new type; i.e., given:
14216 struct S { friend struct T; };
14218 `T' is not a new type in the scope of `S'.
14220 Also, `new struct S' or `sizeof (struct S)' never results in the
14221 definition of a new type; a new type can only be declared in a
14222 declaration context. */
14224 tag_scope ts;
14225 bool template_p;
14227 if (is_friend)
14228 /* Friends have special name lookup rules. */
14229 ts = ts_within_enclosing_non_class;
14230 else if (is_declaration
14231 && cp_lexer_next_token_is (parser->lexer,
14232 CPP_SEMICOLON))
14233 /* This is a `class-key identifier ;' */
14234 ts = ts_current;
14235 else
14236 ts = ts_global;
14238 template_p =
14239 (parser->num_template_parameter_lists
14240 && (cp_parser_next_token_starts_class_definition_p (parser)
14241 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14242 /* An unqualified name was used to reference this type, so
14243 there were no qualifying templates. */
14244 if (!cp_parser_check_template_parameters (parser,
14245 /*num_templates=*/0,
14246 token->location,
14247 /*declarator=*/NULL))
14248 return error_mark_node;
14249 type = xref_tag (tag_type, identifier, ts, template_p);
14253 if (type == error_mark_node)
14254 return error_mark_node;
14256 /* Allow attributes on forward declarations of classes. */
14257 if (attributes)
14259 if (TREE_CODE (type) == TYPENAME_TYPE)
14260 warning (OPT_Wattributes,
14261 "attributes ignored on uninstantiated type");
14262 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14263 && ! processing_explicit_instantiation)
14264 warning (OPT_Wattributes,
14265 "attributes ignored on template instantiation");
14266 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14267 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14268 else
14269 warning (OPT_Wattributes,
14270 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14273 if (tag_type != enum_type)
14275 /* Indicate whether this class was declared as a `class' or as a
14276 `struct'. */
14277 if (TREE_CODE (type) == RECORD_TYPE)
14278 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14279 cp_parser_check_class_key (tag_type, type);
14282 /* A "<" cannot follow an elaborated type specifier. If that
14283 happens, the user was probably trying to form a template-id. */
14284 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
14285 token->location);
14287 return type;
14290 /* Parse an enum-specifier.
14292 enum-specifier:
14293 enum-head { enumerator-list [opt] }
14294 enum-head { enumerator-list , } [C++0x]
14296 enum-head:
14297 enum-key identifier [opt] enum-base [opt]
14298 enum-key nested-name-specifier identifier enum-base [opt]
14300 enum-key:
14301 enum
14302 enum class [C++0x]
14303 enum struct [C++0x]
14305 enum-base: [C++0x]
14306 : type-specifier-seq
14308 opaque-enum-specifier:
14309 enum-key identifier enum-base [opt] ;
14311 GNU Extensions:
14312 enum-key attributes[opt] identifier [opt] enum-base [opt]
14313 { enumerator-list [opt] }attributes[opt]
14314 enum-key attributes[opt] identifier [opt] enum-base [opt]
14315 { enumerator-list, }attributes[opt] [C++0x]
14317 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14318 if the token stream isn't an enum-specifier after all. */
14320 static tree
14321 cp_parser_enum_specifier (cp_parser* parser)
14323 tree identifier;
14324 tree type = NULL_TREE;
14325 tree prev_scope;
14326 tree nested_name_specifier = NULL_TREE;
14327 tree attributes;
14328 bool scoped_enum_p = false;
14329 bool has_underlying_type = false;
14330 bool nested_being_defined = false;
14331 bool new_value_list = false;
14332 bool is_new_type = false;
14333 bool is_anonymous = false;
14334 tree underlying_type = NULL_TREE;
14335 cp_token *type_start_token = NULL;
14336 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14338 parser->colon_corrects_to_scope_p = false;
14340 /* Parse tentatively so that we can back up if we don't find a
14341 enum-specifier. */
14342 cp_parser_parse_tentatively (parser);
14344 /* Caller guarantees that the current token is 'enum', an identifier
14345 possibly follows, and the token after that is an opening brace.
14346 If we don't have an identifier, fabricate an anonymous name for
14347 the enumeration being defined. */
14348 cp_lexer_consume_token (parser->lexer);
14350 /* Parse the "class" or "struct", which indicates a scoped
14351 enumeration type in C++0x. */
14352 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14353 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14355 if (cxx_dialect < cxx0x)
14356 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14358 /* Consume the `struct' or `class' token. */
14359 cp_lexer_consume_token (parser->lexer);
14361 scoped_enum_p = true;
14364 attributes = cp_parser_attributes_opt (parser);
14366 /* Clear the qualification. */
14367 parser->scope = NULL_TREE;
14368 parser->qualifying_scope = NULL_TREE;
14369 parser->object_scope = NULL_TREE;
14371 /* Figure out in what scope the declaration is being placed. */
14372 prev_scope = current_scope ();
14374 type_start_token = cp_lexer_peek_token (parser->lexer);
14376 push_deferring_access_checks (dk_no_check);
14377 nested_name_specifier
14378 = cp_parser_nested_name_specifier_opt (parser,
14379 /*typename_keyword_p=*/true,
14380 /*check_dependency_p=*/false,
14381 /*type_p=*/false,
14382 /*is_declaration=*/false);
14384 if (nested_name_specifier)
14386 tree name;
14388 identifier = cp_parser_identifier (parser);
14389 name = cp_parser_lookup_name (parser, identifier,
14390 enum_type,
14391 /*is_template=*/false,
14392 /*is_namespace=*/false,
14393 /*check_dependency=*/true,
14394 /*ambiguous_decls=*/NULL,
14395 input_location);
14396 if (name)
14398 type = TREE_TYPE (name);
14399 if (TREE_CODE (type) == TYPENAME_TYPE)
14401 /* Are template enums allowed in ISO? */
14402 if (template_parm_scope_p ())
14403 pedwarn (type_start_token->location, OPT_Wpedantic,
14404 "%qD is an enumeration template", name);
14405 /* ignore a typename reference, for it will be solved by name
14406 in start_enum. */
14407 type = NULL_TREE;
14410 else
14411 error_at (type_start_token->location,
14412 "%qD is not an enumerator-name", identifier);
14414 else
14416 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14417 identifier = cp_parser_identifier (parser);
14418 else
14420 identifier = make_anon_name ();
14421 is_anonymous = true;
14424 pop_deferring_access_checks ();
14426 /* Check for the `:' that denotes a specified underlying type in C++0x.
14427 Note that a ':' could also indicate a bitfield width, however. */
14428 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14430 cp_decl_specifier_seq type_specifiers;
14432 /* Consume the `:'. */
14433 cp_lexer_consume_token (parser->lexer);
14435 /* Parse the type-specifier-seq. */
14436 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14437 /*is_trailing_return=*/false,
14438 &type_specifiers);
14440 /* At this point this is surely not elaborated type specifier. */
14441 if (!cp_parser_parse_definitely (parser))
14442 return NULL_TREE;
14444 if (cxx_dialect < cxx0x)
14445 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14447 has_underlying_type = true;
14449 /* If that didn't work, stop. */
14450 if (type_specifiers.type != error_mark_node)
14452 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14453 /*initialized=*/0, NULL);
14454 if (underlying_type == error_mark_node)
14455 underlying_type = NULL_TREE;
14459 /* Look for the `{' but don't consume it yet. */
14460 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14462 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14464 cp_parser_error (parser, "expected %<{%>");
14465 if (has_underlying_type)
14467 type = NULL_TREE;
14468 goto out;
14471 /* An opaque-enum-specifier must have a ';' here. */
14472 if ((scoped_enum_p || underlying_type)
14473 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14475 cp_parser_error (parser, "expected %<;%> or %<{%>");
14476 if (has_underlying_type)
14478 type = NULL_TREE;
14479 goto out;
14484 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14485 return NULL_TREE;
14487 if (nested_name_specifier)
14489 if (CLASS_TYPE_P (nested_name_specifier))
14491 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14492 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14493 push_scope (nested_name_specifier);
14495 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14497 push_nested_namespace (nested_name_specifier);
14501 /* Issue an error message if type-definitions are forbidden here. */
14502 if (!cp_parser_check_type_definition (parser))
14503 type = error_mark_node;
14504 else
14505 /* Create the new type. We do this before consuming the opening
14506 brace so the enum will be recorded as being on the line of its
14507 tag (or the 'enum' keyword, if there is no tag). */
14508 type = start_enum (identifier, type, underlying_type,
14509 scoped_enum_p, &is_new_type);
14511 /* If the next token is not '{' it is an opaque-enum-specifier or an
14512 elaborated-type-specifier. */
14513 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14515 timevar_push (TV_PARSE_ENUM);
14516 if (nested_name_specifier)
14518 /* The following catches invalid code such as:
14519 enum class S<int>::E { A, B, C }; */
14520 if (!processing_specialization
14521 && CLASS_TYPE_P (nested_name_specifier)
14522 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14523 error_at (type_start_token->location, "cannot add an enumerator "
14524 "list to a template instantiation");
14526 /* If that scope does not contain the scope in which the
14527 class was originally declared, the program is invalid. */
14528 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14530 if (at_namespace_scope_p ())
14531 error_at (type_start_token->location,
14532 "declaration of %qD in namespace %qD which does not "
14533 "enclose %qD",
14534 type, prev_scope, nested_name_specifier);
14535 else
14536 error_at (type_start_token->location,
14537 "declaration of %qD in %qD which does not enclose %qD",
14538 type, prev_scope, nested_name_specifier);
14539 type = error_mark_node;
14543 if (scoped_enum_p)
14544 begin_scope (sk_scoped_enum, type);
14546 /* Consume the opening brace. */
14547 cp_lexer_consume_token (parser->lexer);
14549 if (type == error_mark_node)
14550 ; /* Nothing to add */
14551 else if (OPAQUE_ENUM_P (type)
14552 || (cxx_dialect > cxx98 && processing_specialization))
14554 new_value_list = true;
14555 SET_OPAQUE_ENUM_P (type, false);
14556 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14558 else
14560 error_at (type_start_token->location, "multiple definition of %q#T", type);
14561 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14562 "previous definition here");
14563 type = error_mark_node;
14566 if (type == error_mark_node)
14567 cp_parser_skip_to_end_of_block_or_statement (parser);
14568 /* If the next token is not '}', then there are some enumerators. */
14569 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14570 cp_parser_enumerator_list (parser, type);
14572 /* Consume the final '}'. */
14573 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14575 if (scoped_enum_p)
14576 finish_scope ();
14577 timevar_pop (TV_PARSE_ENUM);
14579 else
14581 /* If a ';' follows, then it is an opaque-enum-specifier
14582 and additional restrictions apply. */
14583 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14585 if (is_anonymous)
14586 error_at (type_start_token->location,
14587 "opaque-enum-specifier without name");
14588 else if (nested_name_specifier)
14589 error_at (type_start_token->location,
14590 "opaque-enum-specifier must use a simple identifier");
14594 /* Look for trailing attributes to apply to this enumeration, and
14595 apply them if appropriate. */
14596 if (cp_parser_allow_gnu_extensions_p (parser))
14598 tree trailing_attr = cp_parser_attributes_opt (parser);
14599 trailing_attr = chainon (trailing_attr, attributes);
14600 cplus_decl_attributes (&type,
14601 trailing_attr,
14602 (int) ATTR_FLAG_TYPE_IN_PLACE);
14605 /* Finish up the enumeration. */
14606 if (type != error_mark_node)
14608 if (new_value_list)
14609 finish_enum_value_list (type);
14610 if (is_new_type)
14611 finish_enum (type);
14614 if (nested_name_specifier)
14616 if (CLASS_TYPE_P (nested_name_specifier))
14618 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14619 pop_scope (nested_name_specifier);
14621 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14623 pop_nested_namespace (nested_name_specifier);
14626 out:
14627 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14628 return type;
14631 /* Parse an enumerator-list. The enumerators all have the indicated
14632 TYPE.
14634 enumerator-list:
14635 enumerator-definition
14636 enumerator-list , enumerator-definition */
14638 static void
14639 cp_parser_enumerator_list (cp_parser* parser, tree type)
14641 while (true)
14643 /* Parse an enumerator-definition. */
14644 cp_parser_enumerator_definition (parser, type);
14646 /* If the next token is not a ',', we've reached the end of
14647 the list. */
14648 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14649 break;
14650 /* Otherwise, consume the `,' and keep going. */
14651 cp_lexer_consume_token (parser->lexer);
14652 /* If the next token is a `}', there is a trailing comma. */
14653 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14655 if (cxx_dialect < cxx0x && !in_system_header)
14656 pedwarn (input_location, OPT_Wpedantic,
14657 "comma at end of enumerator list");
14658 break;
14663 /* Parse an enumerator-definition. The enumerator has the indicated
14664 TYPE.
14666 enumerator-definition:
14667 enumerator
14668 enumerator = constant-expression
14670 enumerator:
14671 identifier */
14673 static void
14674 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14676 tree identifier;
14677 tree value;
14678 location_t loc;
14680 /* Save the input location because we are interested in the location
14681 of the identifier and not the location of the explicit value. */
14682 loc = cp_lexer_peek_token (parser->lexer)->location;
14684 /* Look for the identifier. */
14685 identifier = cp_parser_identifier (parser);
14686 if (identifier == error_mark_node)
14687 return;
14689 /* If the next token is an '=', then there is an explicit value. */
14690 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14692 /* Consume the `=' token. */
14693 cp_lexer_consume_token (parser->lexer);
14694 /* Parse the value. */
14695 value = cp_parser_constant_expression (parser,
14696 /*allow_non_constant_p=*/false,
14697 NULL);
14699 else
14700 value = NULL_TREE;
14702 /* If we are processing a template, make sure the initializer of the
14703 enumerator doesn't contain any bare template parameter pack. */
14704 if (check_for_bare_parameter_packs (value))
14705 value = error_mark_node;
14707 /* integral_constant_value will pull out this expression, so make sure
14708 it's folded as appropriate. */
14709 value = fold_non_dependent_expr (value);
14711 /* Create the enumerator. */
14712 build_enumerator (identifier, value, type, loc);
14715 /* Parse a namespace-name.
14717 namespace-name:
14718 original-namespace-name
14719 namespace-alias
14721 Returns the NAMESPACE_DECL for the namespace. */
14723 static tree
14724 cp_parser_namespace_name (cp_parser* parser)
14726 tree identifier;
14727 tree namespace_decl;
14729 cp_token *token = cp_lexer_peek_token (parser->lexer);
14731 /* Get the name of the namespace. */
14732 identifier = cp_parser_identifier (parser);
14733 if (identifier == error_mark_node)
14734 return error_mark_node;
14736 /* Look up the identifier in the currently active scope. Look only
14737 for namespaces, due to:
14739 [basic.lookup.udir]
14741 When looking up a namespace-name in a using-directive or alias
14742 definition, only namespace names are considered.
14744 And:
14746 [basic.lookup.qual]
14748 During the lookup of a name preceding the :: scope resolution
14749 operator, object, function, and enumerator names are ignored.
14751 (Note that cp_parser_qualifying_entity only calls this
14752 function if the token after the name is the scope resolution
14753 operator.) */
14754 namespace_decl = cp_parser_lookup_name (parser, identifier,
14755 none_type,
14756 /*is_template=*/false,
14757 /*is_namespace=*/true,
14758 /*check_dependency=*/true,
14759 /*ambiguous_decls=*/NULL,
14760 token->location);
14761 /* If it's not a namespace, issue an error. */
14762 if (namespace_decl == error_mark_node
14763 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14765 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14766 error_at (token->location, "%qD is not a namespace-name", identifier);
14767 cp_parser_error (parser, "expected namespace-name");
14768 namespace_decl = error_mark_node;
14771 return namespace_decl;
14774 /* Parse a namespace-definition.
14776 namespace-definition:
14777 named-namespace-definition
14778 unnamed-namespace-definition
14780 named-namespace-definition:
14781 original-namespace-definition
14782 extension-namespace-definition
14784 original-namespace-definition:
14785 namespace identifier { namespace-body }
14787 extension-namespace-definition:
14788 namespace original-namespace-name { namespace-body }
14790 unnamed-namespace-definition:
14791 namespace { namespace-body } */
14793 static void
14794 cp_parser_namespace_definition (cp_parser* parser)
14796 tree identifier, attribs;
14797 bool has_visibility;
14798 bool is_inline;
14800 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14802 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14803 is_inline = true;
14804 cp_lexer_consume_token (parser->lexer);
14806 else
14807 is_inline = false;
14809 /* Look for the `namespace' keyword. */
14810 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14812 /* Get the name of the namespace. We do not attempt to distinguish
14813 between an original-namespace-definition and an
14814 extension-namespace-definition at this point. The semantic
14815 analysis routines are responsible for that. */
14816 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14817 identifier = cp_parser_identifier (parser);
14818 else
14819 identifier = NULL_TREE;
14821 /* Parse any specified attributes. */
14822 attribs = cp_parser_attributes_opt (parser);
14824 /* Look for the `{' to start the namespace. */
14825 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14826 /* Start the namespace. */
14827 push_namespace (identifier);
14829 /* "inline namespace" is equivalent to a stub namespace definition
14830 followed by a strong using directive. */
14831 if (is_inline)
14833 tree name_space = current_namespace;
14834 /* Set up namespace association. */
14835 DECL_NAMESPACE_ASSOCIATIONS (name_space)
14836 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14837 DECL_NAMESPACE_ASSOCIATIONS (name_space));
14838 /* Import the contents of the inline namespace. */
14839 pop_namespace ();
14840 do_using_directive (name_space);
14841 push_namespace (identifier);
14844 has_visibility = handle_namespace_attrs (current_namespace, attribs);
14846 /* Parse the body of the namespace. */
14847 cp_parser_namespace_body (parser);
14849 if (has_visibility)
14850 pop_visibility (1);
14852 /* Finish the namespace. */
14853 pop_namespace ();
14854 /* Look for the final `}'. */
14855 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14858 /* Parse a namespace-body.
14860 namespace-body:
14861 declaration-seq [opt] */
14863 static void
14864 cp_parser_namespace_body (cp_parser* parser)
14866 cp_parser_declaration_seq_opt (parser);
14869 /* Parse a namespace-alias-definition.
14871 namespace-alias-definition:
14872 namespace identifier = qualified-namespace-specifier ; */
14874 static void
14875 cp_parser_namespace_alias_definition (cp_parser* parser)
14877 tree identifier;
14878 tree namespace_specifier;
14880 cp_token *token = cp_lexer_peek_token (parser->lexer);
14882 /* Look for the `namespace' keyword. */
14883 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14884 /* Look for the identifier. */
14885 identifier = cp_parser_identifier (parser);
14886 if (identifier == error_mark_node)
14887 return;
14888 /* Look for the `=' token. */
14889 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14890 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14892 error_at (token->location, "%<namespace%> definition is not allowed here");
14893 /* Skip the definition. */
14894 cp_lexer_consume_token (parser->lexer);
14895 if (cp_parser_skip_to_closing_brace (parser))
14896 cp_lexer_consume_token (parser->lexer);
14897 return;
14899 cp_parser_require (parser, CPP_EQ, RT_EQ);
14900 /* Look for the qualified-namespace-specifier. */
14901 namespace_specifier
14902 = cp_parser_qualified_namespace_specifier (parser);
14903 /* Look for the `;' token. */
14904 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14906 /* Register the alias in the symbol table. */
14907 do_namespace_alias (identifier, namespace_specifier);
14910 /* Parse a qualified-namespace-specifier.
14912 qualified-namespace-specifier:
14913 :: [opt] nested-name-specifier [opt] namespace-name
14915 Returns a NAMESPACE_DECL corresponding to the specified
14916 namespace. */
14918 static tree
14919 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14921 /* Look for the optional `::'. */
14922 cp_parser_global_scope_opt (parser,
14923 /*current_scope_valid_p=*/false);
14925 /* Look for the optional nested-name-specifier. */
14926 cp_parser_nested_name_specifier_opt (parser,
14927 /*typename_keyword_p=*/false,
14928 /*check_dependency_p=*/true,
14929 /*type_p=*/false,
14930 /*is_declaration=*/true);
14932 return cp_parser_namespace_name (parser);
14935 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14936 access declaration.
14938 using-declaration:
14939 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14940 using :: unqualified-id ;
14942 access-declaration:
14943 qualified-id ;
14947 static bool
14948 cp_parser_using_declaration (cp_parser* parser,
14949 bool access_declaration_p)
14951 cp_token *token;
14952 bool typename_p = false;
14953 bool global_scope_p;
14954 tree decl;
14955 tree identifier;
14956 tree qscope;
14957 int oldcount = errorcount;
14958 cp_token *diag_token = NULL;
14960 if (access_declaration_p)
14962 diag_token = cp_lexer_peek_token (parser->lexer);
14963 cp_parser_parse_tentatively (parser);
14965 else
14967 /* Look for the `using' keyword. */
14968 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14970 /* Peek at the next token. */
14971 token = cp_lexer_peek_token (parser->lexer);
14972 /* See if it's `typename'. */
14973 if (token->keyword == RID_TYPENAME)
14975 /* Remember that we've seen it. */
14976 typename_p = true;
14977 /* Consume the `typename' token. */
14978 cp_lexer_consume_token (parser->lexer);
14982 /* Look for the optional global scope qualification. */
14983 global_scope_p
14984 = (cp_parser_global_scope_opt (parser,
14985 /*current_scope_valid_p=*/false)
14986 != NULL_TREE);
14988 /* If we saw `typename', or didn't see `::', then there must be a
14989 nested-name-specifier present. */
14990 if (typename_p || !global_scope_p)
14991 qscope = cp_parser_nested_name_specifier (parser, typename_p,
14992 /*check_dependency_p=*/true,
14993 /*type_p=*/false,
14994 /*is_declaration=*/true);
14995 /* Otherwise, we could be in either of the two productions. In that
14996 case, treat the nested-name-specifier as optional. */
14997 else
14998 qscope = cp_parser_nested_name_specifier_opt (parser,
14999 /*typename_keyword_p=*/false,
15000 /*check_dependency_p=*/true,
15001 /*type_p=*/false,
15002 /*is_declaration=*/true);
15003 if (!qscope)
15004 qscope = global_namespace;
15006 if (access_declaration_p && cp_parser_error_occurred (parser))
15007 /* Something has already gone wrong; there's no need to parse
15008 further. Since an error has occurred, the return value of
15009 cp_parser_parse_definitely will be false, as required. */
15010 return cp_parser_parse_definitely (parser);
15012 token = cp_lexer_peek_token (parser->lexer);
15013 /* Parse the unqualified-id. */
15014 identifier = cp_parser_unqualified_id (parser,
15015 /*template_keyword_p=*/false,
15016 /*check_dependency_p=*/true,
15017 /*declarator_p=*/true,
15018 /*optional_p=*/false);
15020 if (access_declaration_p)
15022 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15023 cp_parser_simulate_error (parser);
15024 if (!cp_parser_parse_definitely (parser))
15025 return false;
15028 /* The function we call to handle a using-declaration is different
15029 depending on what scope we are in. */
15030 if (qscope == error_mark_node || identifier == error_mark_node)
15032 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
15033 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15034 /* [namespace.udecl]
15036 A using declaration shall not name a template-id. */
15037 error_at (token->location,
15038 "a template-id may not appear in a using-declaration");
15039 else
15041 if (at_class_scope_p ())
15043 /* Create the USING_DECL. */
15044 decl = do_class_using_decl (parser->scope, identifier);
15046 if (decl && typename_p)
15047 USING_DECL_TYPENAME_P (decl) = 1;
15049 if (check_for_bare_parameter_packs (decl))
15050 return false;
15051 else
15052 /* Add it to the list of members in this class. */
15053 finish_member_declaration (decl);
15055 else
15057 decl = cp_parser_lookup_name_simple (parser,
15058 identifier,
15059 token->location);
15060 if (decl == error_mark_node)
15061 cp_parser_name_lookup_error (parser, identifier,
15062 decl, NLE_NULL,
15063 token->location);
15064 else if (check_for_bare_parameter_packs (decl))
15065 return false;
15066 else if (!at_namespace_scope_p ())
15067 do_local_using_decl (decl, qscope, identifier);
15068 else
15069 do_toplevel_using_decl (decl, qscope, identifier);
15073 /* Look for the final `;'. */
15074 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15076 if (access_declaration_p && errorcount == oldcount)
15077 warning_at (diag_token->location, OPT_Wdeprecated,
15078 "access declarations are deprecated "
15079 "in favour of using-declarations; "
15080 "suggestion: add the %<using%> keyword");
15082 return true;
15085 /* Parse an alias-declaration.
15087 alias-declaration:
15088 using identifier attribute-specifier-seq [opt] = type-id */
15090 static tree
15091 cp_parser_alias_declaration (cp_parser* parser)
15093 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15094 location_t id_location, using_location, attrs_location = 0;
15095 cp_declarator *declarator;
15096 cp_decl_specifier_seq decl_specs;
15097 bool member_p;
15098 const char *saved_message = NULL;
15100 /* Look for the `using' keyword. */
15101 using_location = cp_lexer_peek_token (parser->lexer)->location;
15102 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15103 id_location = cp_lexer_peek_token (parser->lexer)->location;
15104 id = cp_parser_identifier (parser);
15105 if (id == error_mark_node)
15106 return error_mark_node;
15108 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
15109 attributes = cp_parser_attributes_opt (parser);
15110 if (attributes == error_mark_node)
15111 return error_mark_node;
15113 cp_parser_require (parser, CPP_EQ, RT_EQ);
15115 if (cp_parser_error_occurred (parser))
15116 return error_mark_node;
15118 /* Now we are going to parse the type-id of the declaration. */
15121 [dcl.type]/3 says:
15123 "A type-specifier-seq shall not define a class or enumeration
15124 unless it appears in the type-id of an alias-declaration (7.1.3) that
15125 is not the declaration of a template-declaration."
15127 In other words, if we currently are in an alias template, the
15128 type-id should not define a type.
15130 So let's set parser->type_definition_forbidden_message in that
15131 case; cp_parser_check_type_definition (called by
15132 cp_parser_class_specifier) will then emit an error if a type is
15133 defined in the type-id. */
15134 if (parser->num_template_parameter_lists)
15136 saved_message = parser->type_definition_forbidden_message;
15137 parser->type_definition_forbidden_message =
15138 G_("types may not be defined in alias template declarations");
15141 type = cp_parser_type_id (parser);
15143 /* Restore the error message if need be. */
15144 if (parser->num_template_parameter_lists)
15145 parser->type_definition_forbidden_message = saved_message;
15147 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15149 if (cp_parser_error_occurred (parser))
15150 return error_mark_node;
15152 /* A typedef-name can also be introduced by an alias-declaration. The
15153 identifier following the using keyword becomes a typedef-name. It has
15154 the same semantics as if it were introduced by the typedef
15155 specifier. In particular, it does not define a new type and it shall
15156 not appear in the type-id. */
15158 clear_decl_specs (&decl_specs);
15159 decl_specs.type = type;
15160 if (attributes != NULL_TREE)
15162 decl_specs.attributes = attributes;
15163 set_and_check_decl_spec_loc (&decl_specs,
15164 ds_attribute,
15165 attrs_location);
15167 set_and_check_decl_spec_loc (&decl_specs,
15168 ds_typedef,
15169 using_location);
15170 set_and_check_decl_spec_loc (&decl_specs,
15171 ds_alias,
15172 using_location);
15174 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15175 declarator->id_loc = id_location;
15177 member_p = at_class_scope_p ();
15178 if (member_p)
15179 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15180 NULL_TREE, attributes);
15181 else
15182 decl = start_decl (declarator, &decl_specs, 0,
15183 attributes, NULL_TREE, &pushed_scope);
15184 if (decl == error_mark_node)
15185 return decl;
15187 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15189 if (pushed_scope)
15190 pop_scope (pushed_scope);
15192 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15193 added into the symbol table; otherwise, return the TYPE_DECL. */
15194 if (DECL_LANG_SPECIFIC (decl)
15195 && DECL_TEMPLATE_INFO (decl)
15196 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15198 decl = DECL_TI_TEMPLATE (decl);
15199 if (member_p)
15200 check_member_template (decl);
15203 return decl;
15206 /* Parse a using-directive.
15208 using-directive:
15209 using namespace :: [opt] nested-name-specifier [opt]
15210 namespace-name ; */
15212 static void
15213 cp_parser_using_directive (cp_parser* parser)
15215 tree namespace_decl;
15216 tree attribs;
15218 /* Look for the `using' keyword. */
15219 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15220 /* And the `namespace' keyword. */
15221 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15222 /* Look for the optional `::' operator. */
15223 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15224 /* And the optional nested-name-specifier. */
15225 cp_parser_nested_name_specifier_opt (parser,
15226 /*typename_keyword_p=*/false,
15227 /*check_dependency_p=*/true,
15228 /*type_p=*/false,
15229 /*is_declaration=*/true);
15230 /* Get the namespace being used. */
15231 namespace_decl = cp_parser_namespace_name (parser);
15232 /* And any specified attributes. */
15233 attribs = cp_parser_attributes_opt (parser);
15234 /* Update the symbol table. */
15235 parse_using_directive (namespace_decl, attribs);
15236 /* Look for the final `;'. */
15237 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15240 /* Parse an asm-definition.
15242 asm-definition:
15243 asm ( string-literal ) ;
15245 GNU Extension:
15247 asm-definition:
15248 asm volatile [opt] ( string-literal ) ;
15249 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15250 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15251 : asm-operand-list [opt] ) ;
15252 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15253 : asm-operand-list [opt]
15254 : asm-clobber-list [opt] ) ;
15255 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15256 : asm-clobber-list [opt]
15257 : asm-goto-list ) ; */
15259 static void
15260 cp_parser_asm_definition (cp_parser* parser)
15262 tree string;
15263 tree outputs = NULL_TREE;
15264 tree inputs = NULL_TREE;
15265 tree clobbers = NULL_TREE;
15266 tree labels = NULL_TREE;
15267 tree asm_stmt;
15268 bool volatile_p = false;
15269 bool extended_p = false;
15270 bool invalid_inputs_p = false;
15271 bool invalid_outputs_p = false;
15272 bool goto_p = false;
15273 required_token missing = RT_NONE;
15275 /* Look for the `asm' keyword. */
15276 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15277 /* See if the next token is `volatile'. */
15278 if (cp_parser_allow_gnu_extensions_p (parser)
15279 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15281 /* Remember that we saw the `volatile' keyword. */
15282 volatile_p = true;
15283 /* Consume the token. */
15284 cp_lexer_consume_token (parser->lexer);
15286 if (cp_parser_allow_gnu_extensions_p (parser)
15287 && parser->in_function_body
15288 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15290 /* Remember that we saw the `goto' keyword. */
15291 goto_p = true;
15292 /* Consume the token. */
15293 cp_lexer_consume_token (parser->lexer);
15295 /* Look for the opening `('. */
15296 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15297 return;
15298 /* Look for the string. */
15299 string = cp_parser_string_literal (parser, false, false);
15300 if (string == error_mark_node)
15302 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15303 /*consume_paren=*/true);
15304 return;
15307 /* If we're allowing GNU extensions, check for the extended assembly
15308 syntax. Unfortunately, the `:' tokens need not be separated by
15309 a space in C, and so, for compatibility, we tolerate that here
15310 too. Doing that means that we have to treat the `::' operator as
15311 two `:' tokens. */
15312 if (cp_parser_allow_gnu_extensions_p (parser)
15313 && parser->in_function_body
15314 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15315 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15317 bool inputs_p = false;
15318 bool clobbers_p = false;
15319 bool labels_p = false;
15321 /* The extended syntax was used. */
15322 extended_p = true;
15324 /* Look for outputs. */
15325 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15327 /* Consume the `:'. */
15328 cp_lexer_consume_token (parser->lexer);
15329 /* Parse the output-operands. */
15330 if (cp_lexer_next_token_is_not (parser->lexer,
15331 CPP_COLON)
15332 && cp_lexer_next_token_is_not (parser->lexer,
15333 CPP_SCOPE)
15334 && cp_lexer_next_token_is_not (parser->lexer,
15335 CPP_CLOSE_PAREN)
15336 && !goto_p)
15337 outputs = cp_parser_asm_operand_list (parser);
15339 if (outputs == error_mark_node)
15340 invalid_outputs_p = true;
15342 /* If the next token is `::', there are no outputs, and the
15343 next token is the beginning of the inputs. */
15344 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15345 /* The inputs are coming next. */
15346 inputs_p = true;
15348 /* Look for inputs. */
15349 if (inputs_p
15350 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15352 /* Consume the `:' or `::'. */
15353 cp_lexer_consume_token (parser->lexer);
15354 /* Parse the output-operands. */
15355 if (cp_lexer_next_token_is_not (parser->lexer,
15356 CPP_COLON)
15357 && cp_lexer_next_token_is_not (parser->lexer,
15358 CPP_SCOPE)
15359 && cp_lexer_next_token_is_not (parser->lexer,
15360 CPP_CLOSE_PAREN))
15361 inputs = cp_parser_asm_operand_list (parser);
15363 if (inputs == error_mark_node)
15364 invalid_inputs_p = true;
15366 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15367 /* The clobbers are coming next. */
15368 clobbers_p = true;
15370 /* Look for clobbers. */
15371 if (clobbers_p
15372 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15374 clobbers_p = true;
15375 /* Consume the `:' or `::'. */
15376 cp_lexer_consume_token (parser->lexer);
15377 /* Parse the clobbers. */
15378 if (cp_lexer_next_token_is_not (parser->lexer,
15379 CPP_COLON)
15380 && cp_lexer_next_token_is_not (parser->lexer,
15381 CPP_CLOSE_PAREN))
15382 clobbers = cp_parser_asm_clobber_list (parser);
15384 else if (goto_p
15385 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15386 /* The labels are coming next. */
15387 labels_p = true;
15389 /* Look for labels. */
15390 if (labels_p
15391 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15393 labels_p = true;
15394 /* Consume the `:' or `::'. */
15395 cp_lexer_consume_token (parser->lexer);
15396 /* Parse the labels. */
15397 labels = cp_parser_asm_label_list (parser);
15400 if (goto_p && !labels_p)
15401 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15403 else if (goto_p)
15404 missing = RT_COLON_SCOPE;
15406 /* Look for the closing `)'. */
15407 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15408 missing ? missing : RT_CLOSE_PAREN))
15409 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15410 /*consume_paren=*/true);
15411 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15413 if (!invalid_inputs_p && !invalid_outputs_p)
15415 /* Create the ASM_EXPR. */
15416 if (parser->in_function_body)
15418 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15419 inputs, clobbers, labels);
15420 /* If the extended syntax was not used, mark the ASM_EXPR. */
15421 if (!extended_p)
15423 tree temp = asm_stmt;
15424 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15425 temp = TREE_OPERAND (temp, 0);
15427 ASM_INPUT_P (temp) = 1;
15430 else
15431 add_asm_node (string);
15435 /* Declarators [gram.dcl.decl] */
15437 /* Parse an init-declarator.
15439 init-declarator:
15440 declarator initializer [opt]
15442 GNU Extension:
15444 init-declarator:
15445 declarator asm-specification [opt] attributes [opt] initializer [opt]
15447 function-definition:
15448 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15449 function-body
15450 decl-specifier-seq [opt] declarator function-try-block
15452 GNU Extension:
15454 function-definition:
15455 __extension__ function-definition
15457 TM Extension:
15459 function-definition:
15460 decl-specifier-seq [opt] declarator function-transaction-block
15462 The DECL_SPECIFIERS apply to this declarator. Returns a
15463 representation of the entity declared. If MEMBER_P is TRUE, then
15464 this declarator appears in a class scope. The new DECL created by
15465 this declarator is returned.
15467 The CHECKS are access checks that should be performed once we know
15468 what entity is being declared (and, therefore, what classes have
15469 befriended it).
15471 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15472 for a function-definition here as well. If the declarator is a
15473 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15474 be TRUE upon return. By that point, the function-definition will
15475 have been completely parsed.
15477 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15478 is FALSE.
15480 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15481 parsed declaration if it is an uninitialized single declarator not followed
15482 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15483 if present, will not be consumed. If returned, this declarator will be
15484 created with SD_INITIALIZED but will not call cp_finish_decl. */
15486 static tree
15487 cp_parser_init_declarator (cp_parser* parser,
15488 cp_decl_specifier_seq *decl_specifiers,
15489 VEC (deferred_access_check,gc)* checks,
15490 bool function_definition_allowed_p,
15491 bool member_p,
15492 int declares_class_or_enum,
15493 bool* function_definition_p,
15494 tree* maybe_range_for_decl)
15496 cp_token *token = NULL, *asm_spec_start_token = NULL,
15497 *attributes_start_token = NULL;
15498 cp_declarator *declarator;
15499 tree prefix_attributes;
15500 tree attributes;
15501 tree asm_specification;
15502 tree initializer;
15503 tree decl = NULL_TREE;
15504 tree scope;
15505 int is_initialized;
15506 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15507 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15508 "(...)". */
15509 enum cpp_ttype initialization_kind;
15510 bool is_direct_init = false;
15511 bool is_non_constant_init;
15512 int ctor_dtor_or_conv_p;
15513 bool friend_p;
15514 tree pushed_scope = NULL_TREE;
15515 bool range_for_decl_p = false;
15517 /* Gather the attributes that were provided with the
15518 decl-specifiers. */
15519 prefix_attributes = decl_specifiers->attributes;
15521 /* Assume that this is not the declarator for a function
15522 definition. */
15523 if (function_definition_p)
15524 *function_definition_p = false;
15526 /* Defer access checks while parsing the declarator; we cannot know
15527 what names are accessible until we know what is being
15528 declared. */
15529 resume_deferring_access_checks ();
15531 /* Parse the declarator. */
15532 token = cp_lexer_peek_token (parser->lexer);
15533 declarator
15534 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15535 &ctor_dtor_or_conv_p,
15536 /*parenthesized_p=*/NULL,
15537 member_p);
15538 /* Gather up the deferred checks. */
15539 stop_deferring_access_checks ();
15541 /* If the DECLARATOR was erroneous, there's no need to go
15542 further. */
15543 if (declarator == cp_error_declarator)
15544 return error_mark_node;
15546 /* Check that the number of template-parameter-lists is OK. */
15547 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15548 token->location))
15549 return error_mark_node;
15551 if (declares_class_or_enum & 2)
15552 cp_parser_check_for_definition_in_return_type (declarator,
15553 decl_specifiers->type,
15554 decl_specifiers->locations[ds_type_spec]);
15556 /* Figure out what scope the entity declared by the DECLARATOR is
15557 located in. `grokdeclarator' sometimes changes the scope, so
15558 we compute it now. */
15559 scope = get_scope_of_declarator (declarator);
15561 /* Perform any lookups in the declared type which were thought to be
15562 dependent, but are not in the scope of the declarator. */
15563 decl_specifiers->type
15564 = maybe_update_decl_type (decl_specifiers->type, scope);
15566 /* If we're allowing GNU extensions, look for an asm-specification
15567 and attributes. */
15568 if (cp_parser_allow_gnu_extensions_p (parser))
15570 /* Look for an asm-specification. */
15571 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15572 asm_specification = cp_parser_asm_specification_opt (parser);
15573 /* And attributes. */
15574 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15575 attributes = cp_parser_attributes_opt (parser);
15577 else
15579 asm_specification = NULL_TREE;
15580 attributes = NULL_TREE;
15583 /* Peek at the next token. */
15584 token = cp_lexer_peek_token (parser->lexer);
15585 /* Check to see if the token indicates the start of a
15586 function-definition. */
15587 if (function_declarator_p (declarator)
15588 && cp_parser_token_starts_function_definition_p (token))
15590 if (!function_definition_allowed_p)
15592 /* If a function-definition should not appear here, issue an
15593 error message. */
15594 cp_parser_error (parser,
15595 "a function-definition is not allowed here");
15596 return error_mark_node;
15598 else
15600 location_t func_brace_location
15601 = cp_lexer_peek_token (parser->lexer)->location;
15603 /* Neither attributes nor an asm-specification are allowed
15604 on a function-definition. */
15605 if (asm_specification)
15606 error_at (asm_spec_start_token->location,
15607 "an asm-specification is not allowed "
15608 "on a function-definition");
15609 if (attributes)
15610 error_at (attributes_start_token->location,
15611 "attributes are not allowed on a function-definition");
15612 /* This is a function-definition. */
15613 *function_definition_p = true;
15615 /* Parse the function definition. */
15616 if (member_p)
15617 decl = cp_parser_save_member_function_body (parser,
15618 decl_specifiers,
15619 declarator,
15620 prefix_attributes);
15621 else
15622 decl
15623 = (cp_parser_function_definition_from_specifiers_and_declarator
15624 (parser, decl_specifiers, prefix_attributes, declarator));
15626 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15628 /* This is where the prologue starts... */
15629 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15630 = func_brace_location;
15633 return decl;
15637 /* [dcl.dcl]
15639 Only in function declarations for constructors, destructors, and
15640 type conversions can the decl-specifier-seq be omitted.
15642 We explicitly postpone this check past the point where we handle
15643 function-definitions because we tolerate function-definitions
15644 that are missing their return types in some modes. */
15645 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15647 cp_parser_error (parser,
15648 "expected constructor, destructor, or type conversion");
15649 return error_mark_node;
15652 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15653 if (token->type == CPP_EQ
15654 || token->type == CPP_OPEN_PAREN
15655 || token->type == CPP_OPEN_BRACE)
15657 is_initialized = SD_INITIALIZED;
15658 initialization_kind = token->type;
15659 if (maybe_range_for_decl)
15660 *maybe_range_for_decl = error_mark_node;
15662 if (token->type == CPP_EQ
15663 && function_declarator_p (declarator))
15665 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15666 if (t2->keyword == RID_DEFAULT)
15667 is_initialized = SD_DEFAULTED;
15668 else if (t2->keyword == RID_DELETE)
15669 is_initialized = SD_DELETED;
15672 else
15674 /* If the init-declarator isn't initialized and isn't followed by a
15675 `,' or `;', it's not a valid init-declarator. */
15676 if (token->type != CPP_COMMA
15677 && token->type != CPP_SEMICOLON)
15679 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15680 range_for_decl_p = true;
15681 else
15683 cp_parser_error (parser, "expected initializer");
15684 return error_mark_node;
15687 is_initialized = SD_UNINITIALIZED;
15688 initialization_kind = CPP_EOF;
15691 /* Because start_decl has side-effects, we should only call it if we
15692 know we're going ahead. By this point, we know that we cannot
15693 possibly be looking at any other construct. */
15694 cp_parser_commit_to_tentative_parse (parser);
15696 /* If the decl specifiers were bad, issue an error now that we're
15697 sure this was intended to be a declarator. Then continue
15698 declaring the variable(s), as int, to try to cut down on further
15699 errors. */
15700 if (decl_specifiers->any_specifiers_p
15701 && decl_specifiers->type == error_mark_node)
15703 cp_parser_error (parser, "invalid type in declaration");
15704 decl_specifiers->type = integer_type_node;
15707 /* Check to see whether or not this declaration is a friend. */
15708 friend_p = cp_parser_friend_p (decl_specifiers);
15710 /* Enter the newly declared entry in the symbol table. If we're
15711 processing a declaration in a class-specifier, we wait until
15712 after processing the initializer. */
15713 if (!member_p)
15715 if (parser->in_unbraced_linkage_specification_p)
15716 decl_specifiers->storage_class = sc_extern;
15717 decl = start_decl (declarator, decl_specifiers,
15718 range_for_decl_p? SD_INITIALIZED : is_initialized,
15719 attributes, prefix_attributes,
15720 &pushed_scope);
15721 /* Adjust location of decl if declarator->id_loc is more appropriate:
15722 set, and decl wasn't merged with another decl, in which case its
15723 location would be different from input_location, and more accurate. */
15724 if (DECL_P (decl)
15725 && declarator->id_loc != UNKNOWN_LOCATION
15726 && DECL_SOURCE_LOCATION (decl) == input_location)
15727 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15729 else if (scope)
15730 /* Enter the SCOPE. That way unqualified names appearing in the
15731 initializer will be looked up in SCOPE. */
15732 pushed_scope = push_scope (scope);
15734 /* Perform deferred access control checks, now that we know in which
15735 SCOPE the declared entity resides. */
15736 if (!member_p && decl)
15738 tree saved_current_function_decl = NULL_TREE;
15740 /* If the entity being declared is a function, pretend that we
15741 are in its scope. If it is a `friend', it may have access to
15742 things that would not otherwise be accessible. */
15743 if (TREE_CODE (decl) == FUNCTION_DECL)
15745 saved_current_function_decl = current_function_decl;
15746 current_function_decl = decl;
15749 /* Perform access checks for template parameters. */
15750 cp_parser_perform_template_parameter_access_checks (checks);
15752 /* Perform the access control checks for the declarator and the
15753 decl-specifiers. */
15754 perform_deferred_access_checks (tf_warning_or_error);
15756 /* Restore the saved value. */
15757 if (TREE_CODE (decl) == FUNCTION_DECL)
15758 current_function_decl = saved_current_function_decl;
15761 /* Parse the initializer. */
15762 initializer = NULL_TREE;
15763 is_direct_init = false;
15764 is_non_constant_init = true;
15765 if (is_initialized)
15767 if (function_declarator_p (declarator))
15769 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15770 if (initialization_kind == CPP_EQ)
15771 initializer = cp_parser_pure_specifier (parser);
15772 else
15774 /* If the declaration was erroneous, we don't really
15775 know what the user intended, so just silently
15776 consume the initializer. */
15777 if (decl != error_mark_node)
15778 error_at (initializer_start_token->location,
15779 "initializer provided for function");
15780 cp_parser_skip_to_closing_parenthesis (parser,
15781 /*recovering=*/true,
15782 /*or_comma=*/false,
15783 /*consume_paren=*/true);
15786 else
15788 /* We want to record the extra mangling scope for in-class
15789 initializers of class members and initializers of static data
15790 member templates. The former involves deferring
15791 parsing of the initializer until end of class as with default
15792 arguments. So right here we only handle the latter. */
15793 if (!member_p && processing_template_decl)
15794 start_lambda_scope (decl);
15795 initializer = cp_parser_initializer (parser,
15796 &is_direct_init,
15797 &is_non_constant_init);
15798 if (!member_p && processing_template_decl)
15799 finish_lambda_scope ();
15800 if (initializer == error_mark_node)
15801 cp_parser_skip_to_end_of_statement (parser);
15805 /* The old parser allows attributes to appear after a parenthesized
15806 initializer. Mark Mitchell proposed removing this functionality
15807 on the GCC mailing lists on 2002-08-13. This parser accepts the
15808 attributes -- but ignores them. */
15809 if (cp_parser_allow_gnu_extensions_p (parser)
15810 && initialization_kind == CPP_OPEN_PAREN)
15811 if (cp_parser_attributes_opt (parser))
15812 warning (OPT_Wattributes,
15813 "attributes after parenthesized initializer ignored");
15815 /* For an in-class declaration, use `grokfield' to create the
15816 declaration. */
15817 if (member_p)
15819 if (pushed_scope)
15821 pop_scope (pushed_scope);
15822 pushed_scope = NULL_TREE;
15824 decl = grokfield (declarator, decl_specifiers,
15825 initializer, !is_non_constant_init,
15826 /*asmspec=*/NULL_TREE,
15827 prefix_attributes);
15828 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15829 cp_parser_save_default_args (parser, decl);
15832 /* Finish processing the declaration. But, skip member
15833 declarations. */
15834 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15836 cp_finish_decl (decl,
15837 initializer, !is_non_constant_init,
15838 asm_specification,
15839 /* If the initializer is in parentheses, then this is
15840 a direct-initialization, which means that an
15841 `explicit' constructor is OK. Otherwise, an
15842 `explicit' constructor cannot be used. */
15843 ((is_direct_init || !is_initialized)
15844 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15846 else if ((cxx_dialect != cxx98) && friend_p
15847 && decl && TREE_CODE (decl) == FUNCTION_DECL)
15848 /* Core issue #226 (C++0x only): A default template-argument
15849 shall not be specified in a friend class template
15850 declaration. */
15851 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
15852 /*is_partial=*/false, /*is_friend_decl=*/1);
15854 if (!friend_p && pushed_scope)
15855 pop_scope (pushed_scope);
15857 return decl;
15860 /* Parse a declarator.
15862 declarator:
15863 direct-declarator
15864 ptr-operator declarator
15866 abstract-declarator:
15867 ptr-operator abstract-declarator [opt]
15868 direct-abstract-declarator
15870 GNU Extensions:
15872 declarator:
15873 attributes [opt] direct-declarator
15874 attributes [opt] ptr-operator declarator
15876 abstract-declarator:
15877 attributes [opt] ptr-operator abstract-declarator [opt]
15878 attributes [opt] direct-abstract-declarator
15880 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15881 detect constructor, destructor or conversion operators. It is set
15882 to -1 if the declarator is a name, and +1 if it is a
15883 function. Otherwise it is set to zero. Usually you just want to
15884 test for >0, but internally the negative value is used.
15886 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15887 a decl-specifier-seq unless it declares a constructor, destructor,
15888 or conversion. It might seem that we could check this condition in
15889 semantic analysis, rather than parsing, but that makes it difficult
15890 to handle something like `f()'. We want to notice that there are
15891 no decl-specifiers, and therefore realize that this is an
15892 expression, not a declaration.)
15894 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15895 the declarator is a direct-declarator of the form "(...)".
15897 MEMBER_P is true iff this declarator is a member-declarator. */
15899 static cp_declarator *
15900 cp_parser_declarator (cp_parser* parser,
15901 cp_parser_declarator_kind dcl_kind,
15902 int* ctor_dtor_or_conv_p,
15903 bool* parenthesized_p,
15904 bool member_p)
15906 cp_declarator *declarator;
15907 enum tree_code code;
15908 cp_cv_quals cv_quals;
15909 tree class_type;
15910 tree attributes = NULL_TREE;
15912 /* Assume this is not a constructor, destructor, or type-conversion
15913 operator. */
15914 if (ctor_dtor_or_conv_p)
15915 *ctor_dtor_or_conv_p = 0;
15917 if (cp_parser_allow_gnu_extensions_p (parser))
15918 attributes = cp_parser_attributes_opt (parser);
15920 /* Check for the ptr-operator production. */
15921 cp_parser_parse_tentatively (parser);
15922 /* Parse the ptr-operator. */
15923 code = cp_parser_ptr_operator (parser,
15924 &class_type,
15925 &cv_quals);
15926 /* If that worked, then we have a ptr-operator. */
15927 if (cp_parser_parse_definitely (parser))
15929 /* If a ptr-operator was found, then this declarator was not
15930 parenthesized. */
15931 if (parenthesized_p)
15932 *parenthesized_p = true;
15933 /* The dependent declarator is optional if we are parsing an
15934 abstract-declarator. */
15935 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15936 cp_parser_parse_tentatively (parser);
15938 /* Parse the dependent declarator. */
15939 declarator = cp_parser_declarator (parser, dcl_kind,
15940 /*ctor_dtor_or_conv_p=*/NULL,
15941 /*parenthesized_p=*/NULL,
15942 /*member_p=*/false);
15944 /* If we are parsing an abstract-declarator, we must handle the
15945 case where the dependent declarator is absent. */
15946 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15947 && !cp_parser_parse_definitely (parser))
15948 declarator = NULL;
15950 declarator = cp_parser_make_indirect_declarator
15951 (code, class_type, cv_quals, declarator);
15953 /* Everything else is a direct-declarator. */
15954 else
15956 if (parenthesized_p)
15957 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15958 CPP_OPEN_PAREN);
15959 declarator = cp_parser_direct_declarator (parser, dcl_kind,
15960 ctor_dtor_or_conv_p,
15961 member_p);
15964 if (attributes && declarator && declarator != cp_error_declarator)
15965 declarator->attributes = attributes;
15967 return declarator;
15970 /* Parse a direct-declarator or direct-abstract-declarator.
15972 direct-declarator:
15973 declarator-id
15974 direct-declarator ( parameter-declaration-clause )
15975 cv-qualifier-seq [opt]
15976 exception-specification [opt]
15977 direct-declarator [ constant-expression [opt] ]
15978 ( declarator )
15980 direct-abstract-declarator:
15981 direct-abstract-declarator [opt]
15982 ( parameter-declaration-clause )
15983 cv-qualifier-seq [opt]
15984 exception-specification [opt]
15985 direct-abstract-declarator [opt] [ constant-expression [opt] ]
15986 ( abstract-declarator )
15988 Returns a representation of the declarator. DCL_KIND is
15989 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15990 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
15991 we are parsing a direct-declarator. It is
15992 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15993 of ambiguity we prefer an abstract declarator, as per
15994 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15995 cp_parser_declarator. */
15997 static cp_declarator *
15998 cp_parser_direct_declarator (cp_parser* parser,
15999 cp_parser_declarator_kind dcl_kind,
16000 int* ctor_dtor_or_conv_p,
16001 bool member_p)
16003 cp_token *token;
16004 cp_declarator *declarator = NULL;
16005 tree scope = NULL_TREE;
16006 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16007 bool saved_in_declarator_p = parser->in_declarator_p;
16008 bool first = true;
16009 tree pushed_scope = NULL_TREE;
16011 while (true)
16013 /* Peek at the next token. */
16014 token = cp_lexer_peek_token (parser->lexer);
16015 if (token->type == CPP_OPEN_PAREN)
16017 /* This is either a parameter-declaration-clause, or a
16018 parenthesized declarator. When we know we are parsing a
16019 named declarator, it must be a parenthesized declarator
16020 if FIRST is true. For instance, `(int)' is a
16021 parameter-declaration-clause, with an omitted
16022 direct-abstract-declarator. But `((*))', is a
16023 parenthesized abstract declarator. Finally, when T is a
16024 template parameter `(T)' is a
16025 parameter-declaration-clause, and not a parenthesized
16026 named declarator.
16028 We first try and parse a parameter-declaration-clause,
16029 and then try a nested declarator (if FIRST is true).
16031 It is not an error for it not to be a
16032 parameter-declaration-clause, even when FIRST is
16033 false. Consider,
16035 int i (int);
16036 int i (3);
16038 The first is the declaration of a function while the
16039 second is the definition of a variable, including its
16040 initializer.
16042 Having seen only the parenthesis, we cannot know which of
16043 these two alternatives should be selected. Even more
16044 complex are examples like:
16046 int i (int (a));
16047 int i (int (3));
16049 The former is a function-declaration; the latter is a
16050 variable initialization.
16052 Thus again, we try a parameter-declaration-clause, and if
16053 that fails, we back out and return. */
16055 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16057 tree params;
16058 unsigned saved_num_template_parameter_lists;
16059 bool is_declarator = false;
16060 tree t;
16062 /* In a member-declarator, the only valid interpretation
16063 of a parenthesis is the start of a
16064 parameter-declaration-clause. (It is invalid to
16065 initialize a static data member with a parenthesized
16066 initializer; only the "=" form of initialization is
16067 permitted.) */
16068 if (!member_p)
16069 cp_parser_parse_tentatively (parser);
16071 /* Consume the `('. */
16072 cp_lexer_consume_token (parser->lexer);
16073 if (first)
16075 /* If this is going to be an abstract declarator, we're
16076 in a declarator and we can't have default args. */
16077 parser->default_arg_ok_p = false;
16078 parser->in_declarator_p = true;
16081 /* Inside the function parameter list, surrounding
16082 template-parameter-lists do not apply. */
16083 saved_num_template_parameter_lists
16084 = parser->num_template_parameter_lists;
16085 parser->num_template_parameter_lists = 0;
16087 begin_scope (sk_function_parms, NULL_TREE);
16089 /* Parse the parameter-declaration-clause. */
16090 params = cp_parser_parameter_declaration_clause (parser);
16092 parser->num_template_parameter_lists
16093 = saved_num_template_parameter_lists;
16095 /* Consume the `)'. */
16096 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16098 /* If all went well, parse the cv-qualifier-seq and the
16099 exception-specification. */
16100 if (member_p || cp_parser_parse_definitely (parser))
16102 cp_cv_quals cv_quals;
16103 cp_virt_specifiers virt_specifiers;
16104 tree exception_specification;
16105 tree late_return;
16107 is_declarator = true;
16109 if (ctor_dtor_or_conv_p)
16110 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16111 first = false;
16113 /* Parse the cv-qualifier-seq. */
16114 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16115 /* And the exception-specification. */
16116 exception_specification
16117 = cp_parser_exception_specification_opt (parser);
16119 late_return = (cp_parser_late_return_type_opt
16120 (parser, member_p ? cv_quals : -1));
16122 /* Parse the virt-specifier-seq. */
16123 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16125 /* Create the function-declarator. */
16126 declarator = make_call_declarator (declarator,
16127 params,
16128 cv_quals,
16129 virt_specifiers,
16130 exception_specification,
16131 late_return);
16132 /* Any subsequent parameter lists are to do with
16133 return type, so are not those of the declared
16134 function. */
16135 parser->default_arg_ok_p = false;
16138 /* Remove the function parms from scope. */
16139 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16140 pop_binding (DECL_NAME (t), t);
16141 leave_scope();
16143 if (is_declarator)
16144 /* Repeat the main loop. */
16145 continue;
16148 /* If this is the first, we can try a parenthesized
16149 declarator. */
16150 if (first)
16152 bool saved_in_type_id_in_expr_p;
16154 parser->default_arg_ok_p = saved_default_arg_ok_p;
16155 parser->in_declarator_p = saved_in_declarator_p;
16157 /* Consume the `('. */
16158 cp_lexer_consume_token (parser->lexer);
16159 /* Parse the nested declarator. */
16160 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16161 parser->in_type_id_in_expr_p = true;
16162 declarator
16163 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16164 /*parenthesized_p=*/NULL,
16165 member_p);
16166 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16167 first = false;
16168 /* Expect a `)'. */
16169 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16170 declarator = cp_error_declarator;
16171 if (declarator == cp_error_declarator)
16172 break;
16174 goto handle_declarator;
16176 /* Otherwise, we must be done. */
16177 else
16178 break;
16180 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16181 && token->type == CPP_OPEN_SQUARE)
16183 /* Parse an array-declarator. */
16184 tree bounds;
16186 if (ctor_dtor_or_conv_p)
16187 *ctor_dtor_or_conv_p = 0;
16189 first = false;
16190 parser->default_arg_ok_p = false;
16191 parser->in_declarator_p = true;
16192 /* Consume the `['. */
16193 cp_lexer_consume_token (parser->lexer);
16194 /* Peek at the next token. */
16195 token = cp_lexer_peek_token (parser->lexer);
16196 /* If the next token is `]', then there is no
16197 constant-expression. */
16198 if (token->type != CPP_CLOSE_SQUARE)
16200 bool non_constant_p;
16202 bounds
16203 = cp_parser_constant_expression (parser,
16204 /*allow_non_constant=*/true,
16205 &non_constant_p);
16206 if (!non_constant_p)
16207 /* OK */;
16208 else if (error_operand_p (bounds))
16209 /* Already gave an error. */;
16210 else if (!parser->in_function_body
16211 || current_binding_level->kind == sk_function_parms)
16213 /* Normally, the array bound must be an integral constant
16214 expression. However, as an extension, we allow VLAs
16215 in function scopes as long as they aren't part of a
16216 parameter declaration. */
16217 cp_parser_error (parser,
16218 "array bound is not an integer constant");
16219 bounds = error_mark_node;
16221 else if (processing_template_decl)
16223 /* Remember this wasn't a constant-expression. */
16224 bounds = build_nop (TREE_TYPE (bounds), bounds);
16225 TREE_SIDE_EFFECTS (bounds) = 1;
16228 else
16229 bounds = NULL_TREE;
16230 /* Look for the closing `]'. */
16231 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16233 declarator = cp_error_declarator;
16234 break;
16237 declarator = make_array_declarator (declarator, bounds);
16239 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16242 tree qualifying_scope;
16243 tree unqualified_name;
16244 special_function_kind sfk;
16245 bool abstract_ok;
16246 bool pack_expansion_p = false;
16247 cp_token *declarator_id_start_token;
16249 /* Parse a declarator-id */
16250 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16251 if (abstract_ok)
16253 cp_parser_parse_tentatively (parser);
16255 /* If we see an ellipsis, we should be looking at a
16256 parameter pack. */
16257 if (token->type == CPP_ELLIPSIS)
16259 /* Consume the `...' */
16260 cp_lexer_consume_token (parser->lexer);
16262 pack_expansion_p = true;
16266 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16267 unqualified_name
16268 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16269 qualifying_scope = parser->scope;
16270 if (abstract_ok)
16272 bool okay = false;
16274 if (!unqualified_name && pack_expansion_p)
16276 /* Check whether an error occurred. */
16277 okay = !cp_parser_error_occurred (parser);
16279 /* We already consumed the ellipsis to mark a
16280 parameter pack, but we have no way to report it,
16281 so abort the tentative parse. We will be exiting
16282 immediately anyway. */
16283 cp_parser_abort_tentative_parse (parser);
16285 else
16286 okay = cp_parser_parse_definitely (parser);
16288 if (!okay)
16289 unqualified_name = error_mark_node;
16290 else if (unqualified_name
16291 && (qualifying_scope
16292 || (TREE_CODE (unqualified_name)
16293 != IDENTIFIER_NODE)))
16295 cp_parser_error (parser, "expected unqualified-id");
16296 unqualified_name = error_mark_node;
16300 if (!unqualified_name)
16301 return NULL;
16302 if (unqualified_name == error_mark_node)
16304 declarator = cp_error_declarator;
16305 pack_expansion_p = false;
16306 declarator->parameter_pack_p = false;
16307 break;
16310 if (qualifying_scope && at_namespace_scope_p ()
16311 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16313 /* In the declaration of a member of a template class
16314 outside of the class itself, the SCOPE will sometimes
16315 be a TYPENAME_TYPE. For example, given:
16317 template <typename T>
16318 int S<T>::R::i = 3;
16320 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16321 this context, we must resolve S<T>::R to an ordinary
16322 type, rather than a typename type.
16324 The reason we normally avoid resolving TYPENAME_TYPEs
16325 is that a specialization of `S' might render
16326 `S<T>::R' not a type. However, if `S' is
16327 specialized, then this `i' will not be used, so there
16328 is no harm in resolving the types here. */
16329 tree type;
16331 /* Resolve the TYPENAME_TYPE. */
16332 type = resolve_typename_type (qualifying_scope,
16333 /*only_current_p=*/false);
16334 /* If that failed, the declarator is invalid. */
16335 if (TREE_CODE (type) == TYPENAME_TYPE)
16337 if (typedef_variant_p (type))
16338 error_at (declarator_id_start_token->location,
16339 "cannot define member of dependent typedef "
16340 "%qT", type);
16341 else
16342 error_at (declarator_id_start_token->location,
16343 "%<%T::%E%> is not a type",
16344 TYPE_CONTEXT (qualifying_scope),
16345 TYPE_IDENTIFIER (qualifying_scope));
16347 qualifying_scope = type;
16350 sfk = sfk_none;
16352 if (unqualified_name)
16354 tree class_type;
16356 if (qualifying_scope
16357 && CLASS_TYPE_P (qualifying_scope))
16358 class_type = qualifying_scope;
16359 else
16360 class_type = current_class_type;
16362 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16364 tree name_type = TREE_TYPE (unqualified_name);
16365 if (class_type && same_type_p (name_type, class_type))
16367 if (qualifying_scope
16368 && CLASSTYPE_USE_TEMPLATE (name_type))
16370 error_at (declarator_id_start_token->location,
16371 "invalid use of constructor as a template");
16372 inform (declarator_id_start_token->location,
16373 "use %<%T::%D%> instead of %<%T::%D%> to "
16374 "name the constructor in a qualified name",
16375 class_type,
16376 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16377 class_type, name_type);
16378 declarator = cp_error_declarator;
16379 break;
16381 else
16382 unqualified_name = constructor_name (class_type);
16384 else
16386 /* We do not attempt to print the declarator
16387 here because we do not have enough
16388 information about its original syntactic
16389 form. */
16390 cp_parser_error (parser, "invalid declarator");
16391 declarator = cp_error_declarator;
16392 break;
16396 if (class_type)
16398 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16399 sfk = sfk_destructor;
16400 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16401 sfk = sfk_conversion;
16402 else if (/* There's no way to declare a constructor
16403 for an anonymous type, even if the type
16404 got a name for linkage purposes. */
16405 !TYPE_WAS_ANONYMOUS (class_type)
16406 && constructor_name_p (unqualified_name,
16407 class_type))
16409 unqualified_name = constructor_name (class_type);
16410 sfk = sfk_constructor;
16412 else if (is_overloaded_fn (unqualified_name)
16413 && DECL_CONSTRUCTOR_P (get_first_fn
16414 (unqualified_name)))
16415 sfk = sfk_constructor;
16417 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16418 *ctor_dtor_or_conv_p = -1;
16421 declarator = make_id_declarator (qualifying_scope,
16422 unqualified_name,
16423 sfk);
16424 declarator->id_loc = token->location;
16425 declarator->parameter_pack_p = pack_expansion_p;
16427 if (pack_expansion_p)
16428 maybe_warn_variadic_templates ();
16431 handle_declarator:;
16432 scope = get_scope_of_declarator (declarator);
16433 if (scope)
16434 /* Any names that appear after the declarator-id for a
16435 member are looked up in the containing scope. */
16436 pushed_scope = push_scope (scope);
16437 parser->in_declarator_p = true;
16438 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16439 || (declarator && declarator->kind == cdk_id))
16440 /* Default args are only allowed on function
16441 declarations. */
16442 parser->default_arg_ok_p = saved_default_arg_ok_p;
16443 else
16444 parser->default_arg_ok_p = false;
16446 first = false;
16448 /* We're done. */
16449 else
16450 break;
16453 /* For an abstract declarator, we might wind up with nothing at this
16454 point. That's an error; the declarator is not optional. */
16455 if (!declarator)
16456 cp_parser_error (parser, "expected declarator");
16458 /* If we entered a scope, we must exit it now. */
16459 if (pushed_scope)
16460 pop_scope (pushed_scope);
16462 parser->default_arg_ok_p = saved_default_arg_ok_p;
16463 parser->in_declarator_p = saved_in_declarator_p;
16465 return declarator;
16468 /* Parse a ptr-operator.
16470 ptr-operator:
16471 * cv-qualifier-seq [opt]
16473 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16475 GNU Extension:
16477 ptr-operator:
16478 & cv-qualifier-seq [opt]
16480 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16481 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16482 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16483 filled in with the TYPE containing the member. *CV_QUALS is
16484 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16485 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16486 Note that the tree codes returned by this function have nothing
16487 to do with the types of trees that will be eventually be created
16488 to represent the pointer or reference type being parsed. They are
16489 just constants with suggestive names. */
16490 static enum tree_code
16491 cp_parser_ptr_operator (cp_parser* parser,
16492 tree* type,
16493 cp_cv_quals *cv_quals)
16495 enum tree_code code = ERROR_MARK;
16496 cp_token *token;
16498 /* Assume that it's not a pointer-to-member. */
16499 *type = NULL_TREE;
16500 /* And that there are no cv-qualifiers. */
16501 *cv_quals = TYPE_UNQUALIFIED;
16503 /* Peek at the next token. */
16504 token = cp_lexer_peek_token (parser->lexer);
16506 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16507 if (token->type == CPP_MULT)
16508 code = INDIRECT_REF;
16509 else if (token->type == CPP_AND)
16510 code = ADDR_EXPR;
16511 else if ((cxx_dialect != cxx98) &&
16512 token->type == CPP_AND_AND) /* C++0x only */
16513 code = NON_LVALUE_EXPR;
16515 if (code != ERROR_MARK)
16517 /* Consume the `*', `&' or `&&'. */
16518 cp_lexer_consume_token (parser->lexer);
16520 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16521 `&', if we are allowing GNU extensions. (The only qualifier
16522 that can legally appear after `&' is `restrict', but that is
16523 enforced during semantic analysis. */
16524 if (code == INDIRECT_REF
16525 || cp_parser_allow_gnu_extensions_p (parser))
16526 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16528 else
16530 /* Try the pointer-to-member case. */
16531 cp_parser_parse_tentatively (parser);
16532 /* Look for the optional `::' operator. */
16533 cp_parser_global_scope_opt (parser,
16534 /*current_scope_valid_p=*/false);
16535 /* Look for the nested-name specifier. */
16536 token = cp_lexer_peek_token (parser->lexer);
16537 cp_parser_nested_name_specifier (parser,
16538 /*typename_keyword_p=*/false,
16539 /*check_dependency_p=*/true,
16540 /*type_p=*/false,
16541 /*is_declaration=*/false);
16542 /* If we found it, and the next token is a `*', then we are
16543 indeed looking at a pointer-to-member operator. */
16544 if (!cp_parser_error_occurred (parser)
16545 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16547 /* Indicate that the `*' operator was used. */
16548 code = INDIRECT_REF;
16550 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16551 error_at (token->location, "%qD is a namespace", parser->scope);
16552 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16553 error_at (token->location, "cannot form pointer to member of "
16554 "non-class %q#T", parser->scope);
16555 else
16557 /* The type of which the member is a member is given by the
16558 current SCOPE. */
16559 *type = parser->scope;
16560 /* The next name will not be qualified. */
16561 parser->scope = NULL_TREE;
16562 parser->qualifying_scope = NULL_TREE;
16563 parser->object_scope = NULL_TREE;
16564 /* Look for the optional cv-qualifier-seq. */
16565 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16568 /* If that didn't work we don't have a ptr-operator. */
16569 if (!cp_parser_parse_definitely (parser))
16570 cp_parser_error (parser, "expected ptr-operator");
16573 return code;
16576 /* Parse an (optional) cv-qualifier-seq.
16578 cv-qualifier-seq:
16579 cv-qualifier cv-qualifier-seq [opt]
16581 cv-qualifier:
16582 const
16583 volatile
16585 GNU Extension:
16587 cv-qualifier:
16588 __restrict__
16590 Returns a bitmask representing the cv-qualifiers. */
16592 static cp_cv_quals
16593 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16595 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16597 while (true)
16599 cp_token *token;
16600 cp_cv_quals cv_qualifier;
16602 /* Peek at the next token. */
16603 token = cp_lexer_peek_token (parser->lexer);
16604 /* See if it's a cv-qualifier. */
16605 switch (token->keyword)
16607 case RID_CONST:
16608 cv_qualifier = TYPE_QUAL_CONST;
16609 break;
16611 case RID_VOLATILE:
16612 cv_qualifier = TYPE_QUAL_VOLATILE;
16613 break;
16615 case RID_RESTRICT:
16616 cv_qualifier = TYPE_QUAL_RESTRICT;
16617 break;
16619 default:
16620 cv_qualifier = TYPE_UNQUALIFIED;
16621 break;
16624 if (!cv_qualifier)
16625 break;
16627 if (cv_quals & cv_qualifier)
16629 error_at (token->location, "duplicate cv-qualifier");
16630 cp_lexer_purge_token (parser->lexer);
16632 else
16634 cp_lexer_consume_token (parser->lexer);
16635 cv_quals |= cv_qualifier;
16639 return cv_quals;
16642 /* Parse an (optional) virt-specifier-seq.
16644 virt-specifier-seq:
16645 virt-specifier virt-specifier-seq [opt]
16647 virt-specifier:
16648 override
16649 final
16651 Returns a bitmask representing the virt-specifiers. */
16653 static cp_virt_specifiers
16654 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16656 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16658 while (true)
16660 cp_token *token;
16661 cp_virt_specifiers virt_specifier;
16663 /* Peek at the next token. */
16664 token = cp_lexer_peek_token (parser->lexer);
16665 /* See if it's a virt-specifier-qualifier. */
16666 if (token->type != CPP_NAME)
16667 break;
16668 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16670 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16671 virt_specifier = VIRT_SPEC_OVERRIDE;
16673 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16675 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16676 virt_specifier = VIRT_SPEC_FINAL;
16678 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16680 virt_specifier = VIRT_SPEC_FINAL;
16682 else
16683 break;
16685 if (virt_specifiers & virt_specifier)
16687 error_at (token->location, "duplicate virt-specifier");
16688 cp_lexer_purge_token (parser->lexer);
16690 else
16692 cp_lexer_consume_token (parser->lexer);
16693 virt_specifiers |= virt_specifier;
16696 return virt_specifiers;
16699 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16700 is in scope even though it isn't real. */
16702 static void
16703 inject_this_parameter (tree ctype, cp_cv_quals quals)
16705 tree this_parm;
16707 if (current_class_ptr)
16709 /* We don't clear this between NSDMIs. Is it already what we want? */
16710 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16711 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16712 && cp_type_quals (type) == quals)
16713 return;
16716 this_parm = build_this_parm (ctype, quals);
16717 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16718 current_class_ptr = NULL_TREE;
16719 current_class_ref
16720 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16721 current_class_ptr = this_parm;
16724 /* Parse a late-specified return type, if any. This is not a separate
16725 non-terminal, but part of a function declarator, which looks like
16727 -> trailing-type-specifier-seq abstract-declarator(opt)
16729 Returns the type indicated by the type-id.
16731 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16732 function. */
16734 static tree
16735 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16737 cp_token *token;
16738 tree type;
16740 /* Peek at the next token. */
16741 token = cp_lexer_peek_token (parser->lexer);
16742 /* A late-specified return type is indicated by an initial '->'. */
16743 if (token->type != CPP_DEREF)
16744 return NULL_TREE;
16746 /* Consume the ->. */
16747 cp_lexer_consume_token (parser->lexer);
16749 if (quals >= 0)
16751 /* DR 1207: 'this' is in scope in the trailing return type. */
16752 gcc_assert (current_class_ptr == NULL_TREE);
16753 inject_this_parameter (current_class_type, quals);
16756 type = cp_parser_trailing_type_id (parser);
16758 if (quals >= 0)
16759 current_class_ptr = current_class_ref = NULL_TREE;
16761 return type;
16764 /* Parse a declarator-id.
16766 declarator-id:
16767 id-expression
16768 :: [opt] nested-name-specifier [opt] type-name
16770 In the `id-expression' case, the value returned is as for
16771 cp_parser_id_expression if the id-expression was an unqualified-id.
16772 If the id-expression was a qualified-id, then a SCOPE_REF is
16773 returned. The first operand is the scope (either a NAMESPACE_DECL
16774 or TREE_TYPE), but the second is still just a representation of an
16775 unqualified-id. */
16777 static tree
16778 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16780 tree id;
16781 /* The expression must be an id-expression. Assume that qualified
16782 names are the names of types so that:
16784 template <class T>
16785 int S<T>::R::i = 3;
16787 will work; we must treat `S<T>::R' as the name of a type.
16788 Similarly, assume that qualified names are templates, where
16789 required, so that:
16791 template <class T>
16792 int S<T>::R<T>::i = 3;
16794 will work, too. */
16795 id = cp_parser_id_expression (parser,
16796 /*template_keyword_p=*/false,
16797 /*check_dependency_p=*/false,
16798 /*template_p=*/NULL,
16799 /*declarator_p=*/true,
16800 optional_p);
16801 if (id && BASELINK_P (id))
16802 id = BASELINK_FUNCTIONS (id);
16803 return id;
16806 /* Parse a type-id.
16808 type-id:
16809 type-specifier-seq abstract-declarator [opt]
16811 Returns the TYPE specified. */
16813 static tree
16814 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16815 bool is_trailing_return)
16817 cp_decl_specifier_seq type_specifier_seq;
16818 cp_declarator *abstract_declarator;
16820 /* Parse the type-specifier-seq. */
16821 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16822 is_trailing_return,
16823 &type_specifier_seq);
16824 if (type_specifier_seq.type == error_mark_node)
16825 return error_mark_node;
16827 /* There might or might not be an abstract declarator. */
16828 cp_parser_parse_tentatively (parser);
16829 /* Look for the declarator. */
16830 abstract_declarator
16831 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16832 /*parenthesized_p=*/NULL,
16833 /*member_p=*/false);
16834 /* Check to see if there really was a declarator. */
16835 if (!cp_parser_parse_definitely (parser))
16836 abstract_declarator = NULL;
16838 if (type_specifier_seq.type
16839 && type_uses_auto (type_specifier_seq.type))
16841 /* A type-id with type 'auto' is only ok if the abstract declarator
16842 is a function declarator with a late-specified return type. */
16843 if (abstract_declarator
16844 && abstract_declarator->kind == cdk_function
16845 && abstract_declarator->u.function.late_return_type)
16846 /* OK */;
16847 else
16849 error ("invalid use of %<auto%>");
16850 return error_mark_node;
16854 return groktypename (&type_specifier_seq, abstract_declarator,
16855 is_template_arg);
16858 static tree cp_parser_type_id (cp_parser *parser)
16860 return cp_parser_type_id_1 (parser, false, false);
16863 static tree cp_parser_template_type_arg (cp_parser *parser)
16865 tree r;
16866 const char *saved_message = parser->type_definition_forbidden_message;
16867 parser->type_definition_forbidden_message
16868 = G_("types may not be defined in template arguments");
16869 r = cp_parser_type_id_1 (parser, true, false);
16870 parser->type_definition_forbidden_message = saved_message;
16871 return r;
16874 static tree cp_parser_trailing_type_id (cp_parser *parser)
16876 return cp_parser_type_id_1 (parser, false, true);
16879 /* Parse a type-specifier-seq.
16881 type-specifier-seq:
16882 type-specifier type-specifier-seq [opt]
16884 GNU extension:
16886 type-specifier-seq:
16887 attributes type-specifier-seq [opt]
16889 If IS_DECLARATION is true, we are at the start of a "condition" or
16890 exception-declaration, so we might be followed by a declarator-id.
16892 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16893 i.e. we've just seen "->".
16895 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
16897 static void
16898 cp_parser_type_specifier_seq (cp_parser* parser,
16899 bool is_declaration,
16900 bool is_trailing_return,
16901 cp_decl_specifier_seq *type_specifier_seq)
16903 bool seen_type_specifier = false;
16904 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16905 cp_token *start_token = NULL;
16907 /* Clear the TYPE_SPECIFIER_SEQ. */
16908 clear_decl_specs (type_specifier_seq);
16910 /* In the context of a trailing return type, enum E { } is an
16911 elaborated-type-specifier followed by a function-body, not an
16912 enum-specifier. */
16913 if (is_trailing_return)
16914 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16916 /* Parse the type-specifiers and attributes. */
16917 while (true)
16919 tree type_specifier;
16920 bool is_cv_qualifier;
16922 /* Check for attributes first. */
16923 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16925 type_specifier_seq->attributes =
16926 chainon (type_specifier_seq->attributes,
16927 cp_parser_attributes_opt (parser));
16928 continue;
16931 /* record the token of the beginning of the type specifier seq,
16932 for error reporting purposes*/
16933 if (!start_token)
16934 start_token = cp_lexer_peek_token (parser->lexer);
16936 /* Look for the type-specifier. */
16937 type_specifier = cp_parser_type_specifier (parser,
16938 flags,
16939 type_specifier_seq,
16940 /*is_declaration=*/false,
16941 NULL,
16942 &is_cv_qualifier);
16943 if (!type_specifier)
16945 /* If the first type-specifier could not be found, this is not a
16946 type-specifier-seq at all. */
16947 if (!seen_type_specifier)
16949 cp_parser_error (parser, "expected type-specifier");
16950 type_specifier_seq->type = error_mark_node;
16951 return;
16953 /* If subsequent type-specifiers could not be found, the
16954 type-specifier-seq is complete. */
16955 break;
16958 seen_type_specifier = true;
16959 /* The standard says that a condition can be:
16961 type-specifier-seq declarator = assignment-expression
16963 However, given:
16965 struct S {};
16966 if (int S = ...)
16968 we should treat the "S" as a declarator, not as a
16969 type-specifier. The standard doesn't say that explicitly for
16970 type-specifier-seq, but it does say that for
16971 decl-specifier-seq in an ordinary declaration. Perhaps it
16972 would be clearer just to allow a decl-specifier-seq here, and
16973 then add a semantic restriction that if any decl-specifiers
16974 that are not type-specifiers appear, the program is invalid. */
16975 if (is_declaration && !is_cv_qualifier)
16976 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16980 /* Parse a parameter-declaration-clause.
16982 parameter-declaration-clause:
16983 parameter-declaration-list [opt] ... [opt]
16984 parameter-declaration-list , ...
16986 Returns a representation for the parameter declarations. A return
16987 value of NULL indicates a parameter-declaration-clause consisting
16988 only of an ellipsis. */
16990 static tree
16991 cp_parser_parameter_declaration_clause (cp_parser* parser)
16993 tree parameters;
16994 cp_token *token;
16995 bool ellipsis_p;
16996 bool is_error;
16998 /* Peek at the next token. */
16999 token = cp_lexer_peek_token (parser->lexer);
17000 /* Check for trivial parameter-declaration-clauses. */
17001 if (token->type == CPP_ELLIPSIS)
17003 /* Consume the `...' token. */
17004 cp_lexer_consume_token (parser->lexer);
17005 return NULL_TREE;
17007 else if (token->type == CPP_CLOSE_PAREN)
17008 /* There are no parameters. */
17010 #ifndef NO_IMPLICIT_EXTERN_C
17011 if (in_system_header && current_class_type == NULL
17012 && current_lang_name == lang_name_c)
17013 return NULL_TREE;
17014 else
17015 #endif
17016 return void_list_node;
17018 /* Check for `(void)', too, which is a special case. */
17019 else if (token->keyword == RID_VOID
17020 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17021 == CPP_CLOSE_PAREN))
17023 /* Consume the `void' token. */
17024 cp_lexer_consume_token (parser->lexer);
17025 /* There are no parameters. */
17026 return void_list_node;
17029 /* Parse the parameter-declaration-list. */
17030 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17031 /* If a parse error occurred while parsing the
17032 parameter-declaration-list, then the entire
17033 parameter-declaration-clause is erroneous. */
17034 if (is_error)
17035 return NULL;
17037 /* Peek at the next token. */
17038 token = cp_lexer_peek_token (parser->lexer);
17039 /* If it's a `,', the clause should terminate with an ellipsis. */
17040 if (token->type == CPP_COMMA)
17042 /* Consume the `,'. */
17043 cp_lexer_consume_token (parser->lexer);
17044 /* Expect an ellipsis. */
17045 ellipsis_p
17046 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17048 /* It might also be `...' if the optional trailing `,' was
17049 omitted. */
17050 else if (token->type == CPP_ELLIPSIS)
17052 /* Consume the `...' token. */
17053 cp_lexer_consume_token (parser->lexer);
17054 /* And remember that we saw it. */
17055 ellipsis_p = true;
17057 else
17058 ellipsis_p = false;
17060 /* Finish the parameter list. */
17061 if (!ellipsis_p)
17062 parameters = chainon (parameters, void_list_node);
17064 return parameters;
17067 /* Parse a parameter-declaration-list.
17069 parameter-declaration-list:
17070 parameter-declaration
17071 parameter-declaration-list , parameter-declaration
17073 Returns a representation of the parameter-declaration-list, as for
17074 cp_parser_parameter_declaration_clause. However, the
17075 `void_list_node' is never appended to the list. Upon return,
17076 *IS_ERROR will be true iff an error occurred. */
17078 static tree
17079 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17081 tree parameters = NULL_TREE;
17082 tree *tail = &parameters;
17083 bool saved_in_unbraced_linkage_specification_p;
17084 int index = 0;
17086 /* Assume all will go well. */
17087 *is_error = false;
17088 /* The special considerations that apply to a function within an
17089 unbraced linkage specifications do not apply to the parameters
17090 to the function. */
17091 saved_in_unbraced_linkage_specification_p
17092 = parser->in_unbraced_linkage_specification_p;
17093 parser->in_unbraced_linkage_specification_p = false;
17095 /* Look for more parameters. */
17096 while (true)
17098 cp_parameter_declarator *parameter;
17099 tree decl = error_mark_node;
17100 bool parenthesized_p = false;
17101 /* Parse the parameter. */
17102 parameter
17103 = cp_parser_parameter_declaration (parser,
17104 /*template_parm_p=*/false,
17105 &parenthesized_p);
17107 /* We don't know yet if the enclosing context is deprecated, so wait
17108 and warn in grokparms if appropriate. */
17109 deprecated_state = DEPRECATED_SUPPRESS;
17111 if (parameter)
17112 decl = grokdeclarator (parameter->declarator,
17113 &parameter->decl_specifiers,
17114 PARM,
17115 parameter->default_argument != NULL_TREE,
17116 &parameter->decl_specifiers.attributes);
17118 deprecated_state = DEPRECATED_NORMAL;
17120 /* If a parse error occurred parsing the parameter declaration,
17121 then the entire parameter-declaration-list is erroneous. */
17122 if (decl == error_mark_node)
17124 *is_error = true;
17125 parameters = error_mark_node;
17126 break;
17129 if (parameter->decl_specifiers.attributes)
17130 cplus_decl_attributes (&decl,
17131 parameter->decl_specifiers.attributes,
17133 if (DECL_NAME (decl))
17134 decl = pushdecl (decl);
17136 if (decl != error_mark_node)
17138 retrofit_lang_decl (decl);
17139 DECL_PARM_INDEX (decl) = ++index;
17140 DECL_PARM_LEVEL (decl) = function_parm_depth ();
17143 /* Add the new parameter to the list. */
17144 *tail = build_tree_list (parameter->default_argument, decl);
17145 tail = &TREE_CHAIN (*tail);
17147 /* Peek at the next token. */
17148 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17149 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17150 /* These are for Objective-C++ */
17151 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17152 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17153 /* The parameter-declaration-list is complete. */
17154 break;
17155 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17157 cp_token *token;
17159 /* Peek at the next token. */
17160 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17161 /* If it's an ellipsis, then the list is complete. */
17162 if (token->type == CPP_ELLIPSIS)
17163 break;
17164 /* Otherwise, there must be more parameters. Consume the
17165 `,'. */
17166 cp_lexer_consume_token (parser->lexer);
17167 /* When parsing something like:
17169 int i(float f, double d)
17171 we can tell after seeing the declaration for "f" that we
17172 are not looking at an initialization of a variable "i",
17173 but rather at the declaration of a function "i".
17175 Due to the fact that the parsing of template arguments
17176 (as specified to a template-id) requires backtracking we
17177 cannot use this technique when inside a template argument
17178 list. */
17179 if (!parser->in_template_argument_list_p
17180 && !parser->in_type_id_in_expr_p
17181 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17182 /* However, a parameter-declaration of the form
17183 "foat(f)" (which is a valid declaration of a
17184 parameter "f") can also be interpreted as an
17185 expression (the conversion of "f" to "float"). */
17186 && !parenthesized_p)
17187 cp_parser_commit_to_tentative_parse (parser);
17189 else
17191 cp_parser_error (parser, "expected %<,%> or %<...%>");
17192 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17193 cp_parser_skip_to_closing_parenthesis (parser,
17194 /*recovering=*/true,
17195 /*or_comma=*/false,
17196 /*consume_paren=*/false);
17197 break;
17201 parser->in_unbraced_linkage_specification_p
17202 = saved_in_unbraced_linkage_specification_p;
17204 return parameters;
17207 /* Parse a parameter declaration.
17209 parameter-declaration:
17210 decl-specifier-seq ... [opt] declarator
17211 decl-specifier-seq declarator = assignment-expression
17212 decl-specifier-seq ... [opt] abstract-declarator [opt]
17213 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17215 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17216 declares a template parameter. (In that case, a non-nested `>'
17217 token encountered during the parsing of the assignment-expression
17218 is not interpreted as a greater-than operator.)
17220 Returns a representation of the parameter, or NULL if an error
17221 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17222 true iff the declarator is of the form "(p)". */
17224 static cp_parameter_declarator *
17225 cp_parser_parameter_declaration (cp_parser *parser,
17226 bool template_parm_p,
17227 bool *parenthesized_p)
17229 int declares_class_or_enum;
17230 cp_decl_specifier_seq decl_specifiers;
17231 cp_declarator *declarator;
17232 tree default_argument;
17233 cp_token *token = NULL, *declarator_token_start = NULL;
17234 const char *saved_message;
17236 /* In a template parameter, `>' is not an operator.
17238 [temp.param]
17240 When parsing a default template-argument for a non-type
17241 template-parameter, the first non-nested `>' is taken as the end
17242 of the template parameter-list rather than a greater-than
17243 operator. */
17245 /* Type definitions may not appear in parameter types. */
17246 saved_message = parser->type_definition_forbidden_message;
17247 parser->type_definition_forbidden_message
17248 = G_("types may not be defined in parameter types");
17250 /* Parse the declaration-specifiers. */
17251 cp_parser_decl_specifier_seq (parser,
17252 CP_PARSER_FLAGS_NONE,
17253 &decl_specifiers,
17254 &declares_class_or_enum);
17256 /* Complain about missing 'typename' or other invalid type names. */
17257 if (!decl_specifiers.any_type_specifiers_p)
17258 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17260 /* If an error occurred, there's no reason to attempt to parse the
17261 rest of the declaration. */
17262 if (cp_parser_error_occurred (parser))
17264 parser->type_definition_forbidden_message = saved_message;
17265 return NULL;
17268 /* Peek at the next token. */
17269 token = cp_lexer_peek_token (parser->lexer);
17271 /* If the next token is a `)', `,', `=', `>', or `...', then there
17272 is no declarator. However, when variadic templates are enabled,
17273 there may be a declarator following `...'. */
17274 if (token->type == CPP_CLOSE_PAREN
17275 || token->type == CPP_COMMA
17276 || token->type == CPP_EQ
17277 || token->type == CPP_GREATER)
17279 declarator = NULL;
17280 if (parenthesized_p)
17281 *parenthesized_p = false;
17283 /* Otherwise, there should be a declarator. */
17284 else
17286 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17287 parser->default_arg_ok_p = false;
17289 /* After seeing a decl-specifier-seq, if the next token is not a
17290 "(", there is no possibility that the code is a valid
17291 expression. Therefore, if parsing tentatively, we commit at
17292 this point. */
17293 if (!parser->in_template_argument_list_p
17294 /* In an expression context, having seen:
17296 (int((char ...
17298 we cannot be sure whether we are looking at a
17299 function-type (taking a "char" as a parameter) or a cast
17300 of some object of type "char" to "int". */
17301 && !parser->in_type_id_in_expr_p
17302 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17303 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17304 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17305 cp_parser_commit_to_tentative_parse (parser);
17306 /* Parse the declarator. */
17307 declarator_token_start = token;
17308 declarator = cp_parser_declarator (parser,
17309 CP_PARSER_DECLARATOR_EITHER,
17310 /*ctor_dtor_or_conv_p=*/NULL,
17311 parenthesized_p,
17312 /*member_p=*/false);
17313 parser->default_arg_ok_p = saved_default_arg_ok_p;
17314 /* After the declarator, allow more attributes. */
17315 decl_specifiers.attributes
17316 = chainon (decl_specifiers.attributes,
17317 cp_parser_attributes_opt (parser));
17320 /* If the next token is an ellipsis, and we have not seen a
17321 declarator name, and the type of the declarator contains parameter
17322 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17323 a parameter pack expansion expression. Otherwise, leave the
17324 ellipsis for a C-style variadic function. */
17325 token = cp_lexer_peek_token (parser->lexer);
17326 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17328 tree type = decl_specifiers.type;
17330 if (type && DECL_P (type))
17331 type = TREE_TYPE (type);
17333 if (type
17334 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17335 && declarator_can_be_parameter_pack (declarator)
17336 && (!declarator || !declarator->parameter_pack_p)
17337 && uses_parameter_packs (type))
17339 /* Consume the `...'. */
17340 cp_lexer_consume_token (parser->lexer);
17341 maybe_warn_variadic_templates ();
17343 /* Build a pack expansion type */
17344 if (declarator)
17345 declarator->parameter_pack_p = true;
17346 else
17347 decl_specifiers.type = make_pack_expansion (type);
17351 /* The restriction on defining new types applies only to the type
17352 of the parameter, not to the default argument. */
17353 parser->type_definition_forbidden_message = saved_message;
17355 /* If the next token is `=', then process a default argument. */
17356 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17358 token = cp_lexer_peek_token (parser->lexer);
17359 /* If we are defining a class, then the tokens that make up the
17360 default argument must be saved and processed later. */
17361 if (!template_parm_p && at_class_scope_p ()
17362 && TYPE_BEING_DEFINED (current_class_type)
17363 && !LAMBDA_TYPE_P (current_class_type))
17364 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17365 /* Outside of a class definition, we can just parse the
17366 assignment-expression. */
17367 else
17368 default_argument
17369 = cp_parser_default_argument (parser, template_parm_p);
17371 if (!parser->default_arg_ok_p)
17373 if (flag_permissive)
17374 warning (0, "deprecated use of default argument for parameter of non-function");
17375 else
17377 error_at (token->location,
17378 "default arguments are only "
17379 "permitted for function parameters");
17380 default_argument = NULL_TREE;
17383 else if ((declarator && declarator->parameter_pack_p)
17384 || (decl_specifiers.type
17385 && PACK_EXPANSION_P (decl_specifiers.type)))
17387 /* Find the name of the parameter pack. */
17388 cp_declarator *id_declarator = declarator;
17389 while (id_declarator && id_declarator->kind != cdk_id)
17390 id_declarator = id_declarator->declarator;
17392 if (id_declarator && id_declarator->kind == cdk_id)
17393 error_at (declarator_token_start->location,
17394 template_parm_p
17395 ? G_("template parameter pack %qD "
17396 "cannot have a default argument")
17397 : G_("parameter pack %qD cannot have "
17398 "a default argument"),
17399 id_declarator->u.id.unqualified_name);
17400 else
17401 error_at (declarator_token_start->location,
17402 template_parm_p
17403 ? G_("template parameter pack cannot have "
17404 "a default argument")
17405 : G_("parameter pack cannot have a "
17406 "default argument"));
17408 default_argument = NULL_TREE;
17411 else
17412 default_argument = NULL_TREE;
17414 return make_parameter_declarator (&decl_specifiers,
17415 declarator,
17416 default_argument);
17419 /* Parse a default argument and return it.
17421 TEMPLATE_PARM_P is true if this is a default argument for a
17422 non-type template parameter. */
17423 static tree
17424 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17426 tree default_argument = NULL_TREE;
17427 bool saved_greater_than_is_operator_p;
17428 bool saved_local_variables_forbidden_p;
17429 bool non_constant_p, is_direct_init;
17431 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17432 set correctly. */
17433 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17434 parser->greater_than_is_operator_p = !template_parm_p;
17435 /* Local variable names (and the `this' keyword) may not
17436 appear in a default argument. */
17437 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17438 parser->local_variables_forbidden_p = true;
17439 /* Parse the assignment-expression. */
17440 if (template_parm_p)
17441 push_deferring_access_checks (dk_no_deferred);
17442 default_argument
17443 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17444 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17445 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17446 if (template_parm_p)
17447 pop_deferring_access_checks ();
17448 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17449 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17451 return default_argument;
17454 /* Parse a function-body.
17456 function-body:
17457 compound_statement */
17459 static void
17460 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
17462 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
17465 /* Parse a ctor-initializer-opt followed by a function-body. Return
17466 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
17467 is true we are parsing a function-try-block. */
17469 static bool
17470 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
17471 bool in_function_try_block)
17473 tree body, list;
17474 bool ctor_initializer_p;
17475 const bool check_body_p =
17476 DECL_CONSTRUCTOR_P (current_function_decl)
17477 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17478 tree last = NULL;
17480 /* Begin the function body. */
17481 body = begin_function_body ();
17482 /* Parse the optional ctor-initializer. */
17483 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17485 /* If we're parsing a constexpr constructor definition, we need
17486 to check that the constructor body is indeed empty. However,
17487 before we get to cp_parser_function_body lot of junk has been
17488 generated, so we can't just check that we have an empty block.
17489 Rather we take a snapshot of the outermost block, and check whether
17490 cp_parser_function_body changed its state. */
17491 if (check_body_p)
17493 list = cur_stmt_list;
17494 if (STATEMENT_LIST_TAIL (list))
17495 last = STATEMENT_LIST_TAIL (list)->stmt;
17497 /* Parse the function-body. */
17498 cp_parser_function_body (parser, in_function_try_block);
17499 if (check_body_p)
17500 check_constexpr_ctor_body (last, list);
17501 /* Finish the function body. */
17502 finish_function_body (body);
17504 return ctor_initializer_p;
17507 /* Parse an initializer.
17509 initializer:
17510 = initializer-clause
17511 ( expression-list )
17513 Returns an expression representing the initializer. If no
17514 initializer is present, NULL_TREE is returned.
17516 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17517 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17518 set to TRUE if there is no initializer present. If there is an
17519 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17520 is set to true; otherwise it is set to false. */
17522 static tree
17523 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17524 bool* non_constant_p)
17526 cp_token *token;
17527 tree init;
17529 /* Peek at the next token. */
17530 token = cp_lexer_peek_token (parser->lexer);
17532 /* Let our caller know whether or not this initializer was
17533 parenthesized. */
17534 *is_direct_init = (token->type != CPP_EQ);
17535 /* Assume that the initializer is constant. */
17536 *non_constant_p = false;
17538 if (token->type == CPP_EQ)
17540 /* Consume the `='. */
17541 cp_lexer_consume_token (parser->lexer);
17542 /* Parse the initializer-clause. */
17543 init = cp_parser_initializer_clause (parser, non_constant_p);
17545 else if (token->type == CPP_OPEN_PAREN)
17547 VEC(tree,gc) *vec;
17548 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17549 /*cast_p=*/false,
17550 /*allow_expansion_p=*/true,
17551 non_constant_p);
17552 if (vec == NULL)
17553 return error_mark_node;
17554 init = build_tree_list_vec (vec);
17555 release_tree_vector (vec);
17557 else if (token->type == CPP_OPEN_BRACE)
17559 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17560 init = cp_parser_braced_list (parser, non_constant_p);
17561 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17563 else
17565 /* Anything else is an error. */
17566 cp_parser_error (parser, "expected initializer");
17567 init = error_mark_node;
17570 return init;
17573 /* Parse an initializer-clause.
17575 initializer-clause:
17576 assignment-expression
17577 braced-init-list
17579 Returns an expression representing the initializer.
17581 If the `assignment-expression' production is used the value
17582 returned is simply a representation for the expression.
17584 Otherwise, calls cp_parser_braced_list. */
17586 static tree
17587 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17589 tree initializer;
17591 /* Assume the expression is constant. */
17592 *non_constant_p = false;
17594 /* If it is not a `{', then we are looking at an
17595 assignment-expression. */
17596 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17598 initializer
17599 = cp_parser_constant_expression (parser,
17600 /*allow_non_constant_p=*/true,
17601 non_constant_p);
17603 else
17604 initializer = cp_parser_braced_list (parser, non_constant_p);
17606 return initializer;
17609 /* Parse a brace-enclosed initializer list.
17611 braced-init-list:
17612 { initializer-list , [opt] }
17615 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17616 the elements of the initializer-list (or NULL, if the last
17617 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17618 NULL_TREE. There is no way to detect whether or not the optional
17619 trailing `,' was provided. NON_CONSTANT_P is as for
17620 cp_parser_initializer. */
17622 static tree
17623 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17625 tree initializer;
17627 /* Consume the `{' token. */
17628 cp_lexer_consume_token (parser->lexer);
17629 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17630 initializer = make_node (CONSTRUCTOR);
17631 /* If it's not a `}', then there is a non-trivial initializer. */
17632 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17634 /* Parse the initializer list. */
17635 CONSTRUCTOR_ELTS (initializer)
17636 = cp_parser_initializer_list (parser, non_constant_p);
17637 /* A trailing `,' token is allowed. */
17638 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17639 cp_lexer_consume_token (parser->lexer);
17641 /* Now, there should be a trailing `}'. */
17642 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17643 TREE_TYPE (initializer) = init_list_type_node;
17644 return initializer;
17647 /* Parse an initializer-list.
17649 initializer-list:
17650 initializer-clause ... [opt]
17651 initializer-list , initializer-clause ... [opt]
17653 GNU Extension:
17655 initializer-list:
17656 designation initializer-clause ...[opt]
17657 initializer-list , designation initializer-clause ...[opt]
17659 designation:
17660 . identifier =
17661 identifier :
17662 [ constant-expression ] =
17664 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17665 for the initializer. If the INDEX of the elt is non-NULL, it is the
17666 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17667 as for cp_parser_initializer. */
17669 static VEC(constructor_elt,gc) *
17670 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17672 VEC(constructor_elt,gc) *v = NULL;
17674 /* Assume all of the expressions are constant. */
17675 *non_constant_p = false;
17677 /* Parse the rest of the list. */
17678 while (true)
17680 cp_token *token;
17681 tree designator;
17682 tree initializer;
17683 bool clause_non_constant_p;
17685 /* If the next token is an identifier and the following one is a
17686 colon, we are looking at the GNU designated-initializer
17687 syntax. */
17688 if (cp_parser_allow_gnu_extensions_p (parser)
17689 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17690 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17692 /* Warn the user that they are using an extension. */
17693 pedwarn (input_location, OPT_Wpedantic,
17694 "ISO C++ does not allow designated initializers");
17695 /* Consume the identifier. */
17696 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17697 /* Consume the `:'. */
17698 cp_lexer_consume_token (parser->lexer);
17700 /* Also handle the C99 syntax, '. id ='. */
17701 else if (cp_parser_allow_gnu_extensions_p (parser)
17702 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17703 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17704 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17706 /* Warn the user that they are using an extension. */
17707 pedwarn (input_location, OPT_Wpedantic,
17708 "ISO C++ does not allow C99 designated initializers");
17709 /* Consume the `.'. */
17710 cp_lexer_consume_token (parser->lexer);
17711 /* Consume the identifier. */
17712 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17713 /* Consume the `='. */
17714 cp_lexer_consume_token (parser->lexer);
17716 /* Also handle C99 array designators, '[ const ] ='. */
17717 else if (cp_parser_allow_gnu_extensions_p (parser)
17718 && !c_dialect_objc ()
17719 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17721 /* In C++11, [ could start a lambda-introducer. */
17722 cp_parser_parse_tentatively (parser);
17723 cp_lexer_consume_token (parser->lexer);
17724 designator = cp_parser_constant_expression (parser, false, NULL);
17725 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17726 cp_parser_require (parser, CPP_EQ, RT_EQ);
17727 if (!cp_parser_parse_definitely (parser))
17728 designator = NULL_TREE;
17730 else
17731 designator = NULL_TREE;
17733 /* Parse the initializer. */
17734 initializer = cp_parser_initializer_clause (parser,
17735 &clause_non_constant_p);
17736 /* If any clause is non-constant, so is the entire initializer. */
17737 if (clause_non_constant_p)
17738 *non_constant_p = true;
17740 /* If we have an ellipsis, this is an initializer pack
17741 expansion. */
17742 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17744 /* Consume the `...'. */
17745 cp_lexer_consume_token (parser->lexer);
17747 /* Turn the initializer into an initializer expansion. */
17748 initializer = make_pack_expansion (initializer);
17751 /* Add it to the vector. */
17752 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17754 /* If the next token is not a comma, we have reached the end of
17755 the list. */
17756 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17757 break;
17759 /* Peek at the next token. */
17760 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17761 /* If the next token is a `}', then we're still done. An
17762 initializer-clause can have a trailing `,' after the
17763 initializer-list and before the closing `}'. */
17764 if (token->type == CPP_CLOSE_BRACE)
17765 break;
17767 /* Consume the `,' token. */
17768 cp_lexer_consume_token (parser->lexer);
17771 return v;
17774 /* Classes [gram.class] */
17776 /* Parse a class-name.
17778 class-name:
17779 identifier
17780 template-id
17782 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17783 to indicate that names looked up in dependent types should be
17784 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17785 keyword has been used to indicate that the name that appears next
17786 is a template. TAG_TYPE indicates the explicit tag given before
17787 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17788 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17789 is the class being defined in a class-head.
17791 Returns the TYPE_DECL representing the class. */
17793 static tree
17794 cp_parser_class_name (cp_parser *parser,
17795 bool typename_keyword_p,
17796 bool template_keyword_p,
17797 enum tag_types tag_type,
17798 bool check_dependency_p,
17799 bool class_head_p,
17800 bool is_declaration)
17802 tree decl;
17803 tree scope;
17804 bool typename_p;
17805 cp_token *token;
17806 tree identifier = NULL_TREE;
17808 /* All class-names start with an identifier. */
17809 token = cp_lexer_peek_token (parser->lexer);
17810 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17812 cp_parser_error (parser, "expected class-name");
17813 return error_mark_node;
17816 /* PARSER->SCOPE can be cleared when parsing the template-arguments
17817 to a template-id, so we save it here. */
17818 scope = parser->scope;
17819 if (scope == error_mark_node)
17820 return error_mark_node;
17822 /* Any name names a type if we're following the `typename' keyword
17823 in a qualified name where the enclosing scope is type-dependent. */
17824 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17825 && dependent_type_p (scope));
17826 /* Handle the common case (an identifier, but not a template-id)
17827 efficiently. */
17828 if (token->type == CPP_NAME
17829 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17831 cp_token *identifier_token;
17832 bool ambiguous_p;
17834 /* Look for the identifier. */
17835 identifier_token = cp_lexer_peek_token (parser->lexer);
17836 ambiguous_p = identifier_token->ambiguous_p;
17837 identifier = cp_parser_identifier (parser);
17838 /* If the next token isn't an identifier, we are certainly not
17839 looking at a class-name. */
17840 if (identifier == error_mark_node)
17841 decl = error_mark_node;
17842 /* If we know this is a type-name, there's no need to look it
17843 up. */
17844 else if (typename_p)
17845 decl = identifier;
17846 else
17848 tree ambiguous_decls;
17849 /* If we already know that this lookup is ambiguous, then
17850 we've already issued an error message; there's no reason
17851 to check again. */
17852 if (ambiguous_p)
17854 cp_parser_simulate_error (parser);
17855 return error_mark_node;
17857 /* If the next token is a `::', then the name must be a type
17858 name.
17860 [basic.lookup.qual]
17862 During the lookup for a name preceding the :: scope
17863 resolution operator, object, function, and enumerator
17864 names are ignored. */
17865 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17866 tag_type = typename_type;
17867 /* Look up the name. */
17868 decl = cp_parser_lookup_name (parser, identifier,
17869 tag_type,
17870 /*is_template=*/false,
17871 /*is_namespace=*/false,
17872 check_dependency_p,
17873 &ambiguous_decls,
17874 identifier_token->location);
17875 if (ambiguous_decls)
17877 if (cp_parser_parsing_tentatively (parser))
17878 cp_parser_simulate_error (parser);
17879 return error_mark_node;
17883 else
17885 /* Try a template-id. */
17886 decl = cp_parser_template_id (parser, template_keyword_p,
17887 check_dependency_p,
17888 tag_type,
17889 is_declaration);
17890 if (decl == error_mark_node)
17891 return error_mark_node;
17894 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17896 /* If this is a typename, create a TYPENAME_TYPE. */
17897 if (typename_p && decl != error_mark_node)
17899 decl = make_typename_type (scope, decl, typename_type,
17900 /*complain=*/tf_error);
17901 if (decl != error_mark_node)
17902 decl = TYPE_NAME (decl);
17905 decl = strip_using_decl (decl);
17907 /* Check to see that it is really the name of a class. */
17908 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17909 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17910 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17911 /* Situations like this:
17913 template <typename T> struct A {
17914 typename T::template X<int>::I i;
17917 are problematic. Is `T::template X<int>' a class-name? The
17918 standard does not seem to be definitive, but there is no other
17919 valid interpretation of the following `::'. Therefore, those
17920 names are considered class-names. */
17922 decl = make_typename_type (scope, decl, tag_type, tf_error);
17923 if (decl != error_mark_node)
17924 decl = TYPE_NAME (decl);
17926 else if (TREE_CODE (decl) != TYPE_DECL
17927 || TREE_TYPE (decl) == error_mark_node
17928 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17929 /* In Objective-C 2.0, a classname followed by '.' starts a
17930 dot-syntax expression, and it's not a type-name. */
17931 || (c_dialect_objc ()
17932 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
17933 && objc_is_class_name (decl)))
17934 decl = error_mark_node;
17936 if (decl == error_mark_node)
17937 cp_parser_error (parser, "expected class-name");
17938 else if (identifier && !parser->scope)
17939 maybe_note_name_used_in_class (identifier, decl);
17941 return decl;
17944 /* Parse a class-specifier.
17946 class-specifier:
17947 class-head { member-specification [opt] }
17949 Returns the TREE_TYPE representing the class. */
17951 static tree
17952 cp_parser_class_specifier_1 (cp_parser* parser)
17954 tree type;
17955 tree attributes = NULL_TREE;
17956 bool nested_name_specifier_p;
17957 unsigned saved_num_template_parameter_lists;
17958 bool saved_in_function_body;
17959 unsigned char in_statement;
17960 bool in_switch_statement_p;
17961 bool saved_in_unbraced_linkage_specification_p;
17962 tree old_scope = NULL_TREE;
17963 tree scope = NULL_TREE;
17964 cp_token *closing_brace;
17966 push_deferring_access_checks (dk_no_deferred);
17968 /* Parse the class-head. */
17969 type = cp_parser_class_head (parser,
17970 &nested_name_specifier_p);
17971 /* If the class-head was a semantic disaster, skip the entire body
17972 of the class. */
17973 if (!type)
17975 cp_parser_skip_to_end_of_block_or_statement (parser);
17976 pop_deferring_access_checks ();
17977 return error_mark_node;
17980 /* Look for the `{'. */
17981 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17983 pop_deferring_access_checks ();
17984 return error_mark_node;
17987 /* Issue an error message if type-definitions are forbidden here. */
17988 cp_parser_check_type_definition (parser);
17989 /* Remember that we are defining one more class. */
17990 ++parser->num_classes_being_defined;
17991 /* Inside the class, surrounding template-parameter-lists do not
17992 apply. */
17993 saved_num_template_parameter_lists
17994 = parser->num_template_parameter_lists;
17995 parser->num_template_parameter_lists = 0;
17996 /* We are not in a function body. */
17997 saved_in_function_body = parser->in_function_body;
17998 parser->in_function_body = false;
17999 /* Or in a loop. */
18000 in_statement = parser->in_statement;
18001 parser->in_statement = 0;
18002 /* Or in a switch. */
18003 in_switch_statement_p = parser->in_switch_statement_p;
18004 parser->in_switch_statement_p = false;
18005 /* We are not immediately inside an extern "lang" block. */
18006 saved_in_unbraced_linkage_specification_p
18007 = parser->in_unbraced_linkage_specification_p;
18008 parser->in_unbraced_linkage_specification_p = false;
18010 /* Start the class. */
18011 if (nested_name_specifier_p)
18013 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18014 old_scope = push_inner_scope (scope);
18016 type = begin_class_definition (type);
18018 if (type == error_mark_node)
18019 /* If the type is erroneous, skip the entire body of the class. */
18020 cp_parser_skip_to_closing_brace (parser);
18021 else
18022 /* Parse the member-specification. */
18023 cp_parser_member_specification_opt (parser);
18025 /* Look for the trailing `}'. */
18026 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18027 /* Look for trailing attributes to apply to this class. */
18028 if (cp_parser_allow_gnu_extensions_p (parser))
18029 attributes = cp_parser_attributes_opt (parser);
18030 if (type != error_mark_node)
18031 type = finish_struct (type, attributes);
18032 if (nested_name_specifier_p)
18033 pop_inner_scope (old_scope, scope);
18035 /* We've finished a type definition. Check for the common syntax
18036 error of forgetting a semicolon after the definition. We need to
18037 be careful, as we can't just check for not-a-semicolon and be done
18038 with it; the user might have typed:
18040 class X { } c = ...;
18041 class X { } *p = ...;
18043 and so forth. Instead, enumerate all the possible tokens that
18044 might follow this production; if we don't see one of them, then
18045 complain and silently insert the semicolon. */
18047 cp_token *token = cp_lexer_peek_token (parser->lexer);
18048 bool want_semicolon = true;
18050 switch (token->type)
18052 case CPP_NAME:
18053 case CPP_SEMICOLON:
18054 case CPP_MULT:
18055 case CPP_AND:
18056 case CPP_OPEN_PAREN:
18057 case CPP_CLOSE_PAREN:
18058 case CPP_COMMA:
18059 want_semicolon = false;
18060 break;
18062 /* While it's legal for type qualifiers and storage class
18063 specifiers to follow type definitions in the grammar, only
18064 compiler testsuites contain code like that. Assume that if
18065 we see such code, then what we're really seeing is a case
18066 like:
18068 class X { }
18069 const <type> var = ...;
18073 class Y { }
18074 static <type> func (...) ...
18076 i.e. the qualifier or specifier applies to the next
18077 declaration. To do so, however, we need to look ahead one
18078 more token to see if *that* token is a type specifier.
18080 This code could be improved to handle:
18082 class Z { }
18083 static const <type> var = ...; */
18084 case CPP_KEYWORD:
18085 if (keyword_is_decl_specifier (token->keyword))
18087 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18089 /* Handling user-defined types here would be nice, but very
18090 tricky. */
18091 want_semicolon
18092 = (lookahead->type == CPP_KEYWORD
18093 && keyword_begins_type_specifier (lookahead->keyword));
18095 break;
18096 default:
18097 break;
18100 /* If we don't have a type, then something is very wrong and we
18101 shouldn't try to do anything clever. Likewise for not seeing the
18102 closing brace. */
18103 if (closing_brace && TYPE_P (type) && want_semicolon)
18105 cp_token_position prev
18106 = cp_lexer_previous_token_position (parser->lexer);
18107 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18108 location_t loc = prev_token->location;
18110 if (CLASSTYPE_DECLARED_CLASS (type))
18111 error_at (loc, "expected %<;%> after class definition");
18112 else if (TREE_CODE (type) == RECORD_TYPE)
18113 error_at (loc, "expected %<;%> after struct definition");
18114 else if (TREE_CODE (type) == UNION_TYPE)
18115 error_at (loc, "expected %<;%> after union definition");
18116 else
18117 gcc_unreachable ();
18119 /* Unget one token and smash it to look as though we encountered
18120 a semicolon in the input stream. */
18121 cp_lexer_set_token_position (parser->lexer, prev);
18122 token = cp_lexer_peek_token (parser->lexer);
18123 token->type = CPP_SEMICOLON;
18124 token->keyword = RID_MAX;
18128 /* If this class is not itself within the scope of another class,
18129 then we need to parse the bodies of all of the queued function
18130 definitions. Note that the queued functions defined in a class
18131 are not always processed immediately following the
18132 class-specifier for that class. Consider:
18134 struct A {
18135 struct B { void f() { sizeof (A); } };
18138 If `f' were processed before the processing of `A' were
18139 completed, there would be no way to compute the size of `A'.
18140 Note that the nesting we are interested in here is lexical --
18141 not the semantic nesting given by TYPE_CONTEXT. In particular,
18142 for:
18144 struct A { struct B; };
18145 struct A::B { void f() { } };
18147 there is no need to delay the parsing of `A::B::f'. */
18148 if (--parser->num_classes_being_defined == 0)
18150 tree decl;
18151 tree class_type = NULL_TREE;
18152 tree pushed_scope = NULL_TREE;
18153 unsigned ix;
18154 cp_default_arg_entry *e;
18155 tree save_ccp, save_ccr;
18157 /* In a first pass, parse default arguments to the functions.
18158 Then, in a second pass, parse the bodies of the functions.
18159 This two-phased approach handles cases like:
18161 struct S {
18162 void f() { g(); }
18163 void g(int i = 3);
18167 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18168 ix, e)
18170 decl = e->decl;
18171 /* If there are default arguments that have not yet been processed,
18172 take care of them now. */
18173 if (class_type != e->class_type)
18175 if (pushed_scope)
18176 pop_scope (pushed_scope);
18177 class_type = e->class_type;
18178 pushed_scope = push_scope (class_type);
18180 /* Make sure that any template parameters are in scope. */
18181 maybe_begin_member_template_processing (decl);
18182 /* Parse the default argument expressions. */
18183 cp_parser_late_parsing_default_args (parser, decl);
18184 /* Remove any template parameters from the symbol table. */
18185 maybe_end_member_template_processing ();
18187 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18188 /* Now parse any NSDMIs. */
18189 save_ccp = current_class_ptr;
18190 save_ccr = current_class_ref;
18191 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18193 if (class_type != DECL_CONTEXT (decl))
18195 if (pushed_scope)
18196 pop_scope (pushed_scope);
18197 class_type = DECL_CONTEXT (decl);
18198 pushed_scope = push_scope (class_type);
18200 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18201 cp_parser_late_parsing_nsdmi (parser, decl);
18203 VEC_truncate (tree, unparsed_nsdmis, 0);
18204 current_class_ptr = save_ccp;
18205 current_class_ref = save_ccr;
18206 if (pushed_scope)
18207 pop_scope (pushed_scope);
18208 /* Now parse the body of the functions. */
18209 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18210 cp_parser_late_parsing_for_member (parser, decl);
18211 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18214 /* Put back any saved access checks. */
18215 pop_deferring_access_checks ();
18217 /* Restore saved state. */
18218 parser->in_switch_statement_p = in_switch_statement_p;
18219 parser->in_statement = in_statement;
18220 parser->in_function_body = saved_in_function_body;
18221 parser->num_template_parameter_lists
18222 = saved_num_template_parameter_lists;
18223 parser->in_unbraced_linkage_specification_p
18224 = saved_in_unbraced_linkage_specification_p;
18226 return type;
18229 static tree
18230 cp_parser_class_specifier (cp_parser* parser)
18232 tree ret;
18233 timevar_push (TV_PARSE_STRUCT);
18234 ret = cp_parser_class_specifier_1 (parser);
18235 timevar_pop (TV_PARSE_STRUCT);
18236 return ret;
18239 /* Parse a class-head.
18241 class-head:
18242 class-key identifier [opt] base-clause [opt]
18243 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18244 class-key nested-name-specifier [opt] template-id
18245 base-clause [opt]
18247 class-virt-specifier:
18248 final
18250 GNU Extensions:
18251 class-key attributes identifier [opt] base-clause [opt]
18252 class-key attributes nested-name-specifier identifier base-clause [opt]
18253 class-key attributes nested-name-specifier [opt] template-id
18254 base-clause [opt]
18256 Upon return BASES is initialized to the list of base classes (or
18257 NULL, if there are none) in the same form returned by
18258 cp_parser_base_clause.
18260 Returns the TYPE of the indicated class. Sets
18261 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18262 involving a nested-name-specifier was used, and FALSE otherwise.
18264 Returns error_mark_node if this is not a class-head.
18266 Returns NULL_TREE if the class-head is syntactically valid, but
18267 semantically invalid in a way that means we should skip the entire
18268 body of the class. */
18270 static tree
18271 cp_parser_class_head (cp_parser* parser,
18272 bool* nested_name_specifier_p)
18274 tree nested_name_specifier;
18275 enum tag_types class_key;
18276 tree id = NULL_TREE;
18277 tree type = NULL_TREE;
18278 tree attributes;
18279 tree bases;
18280 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18281 bool template_id_p = false;
18282 bool qualified_p = false;
18283 bool invalid_nested_name_p = false;
18284 bool invalid_explicit_specialization_p = false;
18285 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18286 tree pushed_scope = NULL_TREE;
18287 unsigned num_templates;
18288 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18289 /* Assume no nested-name-specifier will be present. */
18290 *nested_name_specifier_p = false;
18291 /* Assume no template parameter lists will be used in defining the
18292 type. */
18293 num_templates = 0;
18294 parser->colon_corrects_to_scope_p = false;
18296 /* Look for the class-key. */
18297 class_key = cp_parser_class_key (parser);
18298 if (class_key == none_type)
18299 return error_mark_node;
18301 /* Parse the attributes. */
18302 attributes = cp_parser_attributes_opt (parser);
18304 /* If the next token is `::', that is invalid -- but sometimes
18305 people do try to write:
18307 struct ::S {};
18309 Handle this gracefully by accepting the extra qualifier, and then
18310 issuing an error about it later if this really is a
18311 class-head. If it turns out just to be an elaborated type
18312 specifier, remain silent. */
18313 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18314 qualified_p = true;
18316 push_deferring_access_checks (dk_no_check);
18318 /* Determine the name of the class. Begin by looking for an
18319 optional nested-name-specifier. */
18320 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18321 nested_name_specifier
18322 = cp_parser_nested_name_specifier_opt (parser,
18323 /*typename_keyword_p=*/false,
18324 /*check_dependency_p=*/false,
18325 /*type_p=*/true,
18326 /*is_declaration=*/false);
18327 /* If there was a nested-name-specifier, then there *must* be an
18328 identifier. */
18329 if (nested_name_specifier)
18331 type_start_token = cp_lexer_peek_token (parser->lexer);
18332 /* Although the grammar says `identifier', it really means
18333 `class-name' or `template-name'. You are only allowed to
18334 define a class that has already been declared with this
18335 syntax.
18337 The proposed resolution for Core Issue 180 says that wherever
18338 you see `class T::X' you should treat `X' as a type-name.
18340 It is OK to define an inaccessible class; for example:
18342 class A { class B; };
18343 class A::B {};
18345 We do not know if we will see a class-name, or a
18346 template-name. We look for a class-name first, in case the
18347 class-name is a template-id; if we looked for the
18348 template-name first we would stop after the template-name. */
18349 cp_parser_parse_tentatively (parser);
18350 type = cp_parser_class_name (parser,
18351 /*typename_keyword_p=*/false,
18352 /*template_keyword_p=*/false,
18353 class_type,
18354 /*check_dependency_p=*/false,
18355 /*class_head_p=*/true,
18356 /*is_declaration=*/false);
18357 /* If that didn't work, ignore the nested-name-specifier. */
18358 if (!cp_parser_parse_definitely (parser))
18360 invalid_nested_name_p = true;
18361 type_start_token = cp_lexer_peek_token (parser->lexer);
18362 id = cp_parser_identifier (parser);
18363 if (id == error_mark_node)
18364 id = NULL_TREE;
18366 /* If we could not find a corresponding TYPE, treat this
18367 declaration like an unqualified declaration. */
18368 if (type == error_mark_node)
18369 nested_name_specifier = NULL_TREE;
18370 /* Otherwise, count the number of templates used in TYPE and its
18371 containing scopes. */
18372 else
18374 tree scope;
18376 for (scope = TREE_TYPE (type);
18377 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18378 scope = (TYPE_P (scope)
18379 ? TYPE_CONTEXT (scope)
18380 : DECL_CONTEXT (scope)))
18381 if (TYPE_P (scope)
18382 && CLASS_TYPE_P (scope)
18383 && CLASSTYPE_TEMPLATE_INFO (scope)
18384 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18385 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18386 ++num_templates;
18389 /* Otherwise, the identifier is optional. */
18390 else
18392 /* We don't know whether what comes next is a template-id,
18393 an identifier, or nothing at all. */
18394 cp_parser_parse_tentatively (parser);
18395 /* Check for a template-id. */
18396 type_start_token = cp_lexer_peek_token (parser->lexer);
18397 id = cp_parser_template_id (parser,
18398 /*template_keyword_p=*/false,
18399 /*check_dependency_p=*/true,
18400 class_key,
18401 /*is_declaration=*/true);
18402 /* If that didn't work, it could still be an identifier. */
18403 if (!cp_parser_parse_definitely (parser))
18405 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18407 type_start_token = cp_lexer_peek_token (parser->lexer);
18408 id = cp_parser_identifier (parser);
18410 else
18411 id = NULL_TREE;
18413 else
18415 template_id_p = true;
18416 ++num_templates;
18420 pop_deferring_access_checks ();
18422 if (id)
18424 cp_parser_check_for_invalid_template_id (parser, id,
18425 class_key,
18426 type_start_token->location);
18428 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18430 /* If it's not a `:' or a `{' then we can't really be looking at a
18431 class-head, since a class-head only appears as part of a
18432 class-specifier. We have to detect this situation before calling
18433 xref_tag, since that has irreversible side-effects. */
18434 if (!cp_parser_next_token_starts_class_definition_p (parser))
18436 cp_parser_error (parser, "expected %<{%> or %<:%>");
18437 type = error_mark_node;
18438 goto out;
18441 /* At this point, we're going ahead with the class-specifier, even
18442 if some other problem occurs. */
18443 cp_parser_commit_to_tentative_parse (parser);
18444 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18446 cp_parser_error (parser,
18447 "cannot specify %<override%> for a class");
18448 type = error_mark_node;
18449 goto out;
18451 /* Issue the error about the overly-qualified name now. */
18452 if (qualified_p)
18454 cp_parser_error (parser,
18455 "global qualification of class name is invalid");
18456 type = error_mark_node;
18457 goto out;
18459 else if (invalid_nested_name_p)
18461 cp_parser_error (parser,
18462 "qualified name does not name a class");
18463 type = error_mark_node;
18464 goto out;
18466 else if (nested_name_specifier)
18468 tree scope;
18470 /* Reject typedef-names in class heads. */
18471 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18473 error_at (type_start_token->location,
18474 "invalid class name in declaration of %qD",
18475 type);
18476 type = NULL_TREE;
18477 goto done;
18480 /* Figure out in what scope the declaration is being placed. */
18481 scope = current_scope ();
18482 /* If that scope does not contain the scope in which the
18483 class was originally declared, the program is invalid. */
18484 if (scope && !is_ancestor (scope, nested_name_specifier))
18486 if (at_namespace_scope_p ())
18487 error_at (type_start_token->location,
18488 "declaration of %qD in namespace %qD which does not "
18489 "enclose %qD",
18490 type, scope, nested_name_specifier);
18491 else
18492 error_at (type_start_token->location,
18493 "declaration of %qD in %qD which does not enclose %qD",
18494 type, scope, nested_name_specifier);
18495 type = NULL_TREE;
18496 goto done;
18498 /* [dcl.meaning]
18500 A declarator-id shall not be qualified except for the
18501 definition of a ... nested class outside of its class
18502 ... [or] the definition or explicit instantiation of a
18503 class member of a namespace outside of its namespace. */
18504 if (scope == nested_name_specifier)
18506 permerror (nested_name_specifier_token_start->location,
18507 "extra qualification not allowed");
18508 nested_name_specifier = NULL_TREE;
18509 num_templates = 0;
18512 /* An explicit-specialization must be preceded by "template <>". If
18513 it is not, try to recover gracefully. */
18514 if (at_namespace_scope_p ()
18515 && parser->num_template_parameter_lists == 0
18516 && template_id_p)
18518 error_at (type_start_token->location,
18519 "an explicit specialization must be preceded by %<template <>%>");
18520 invalid_explicit_specialization_p = true;
18521 /* Take the same action that would have been taken by
18522 cp_parser_explicit_specialization. */
18523 ++parser->num_template_parameter_lists;
18524 begin_specialization ();
18526 /* There must be no "return" statements between this point and the
18527 end of this function; set "type "to the correct return value and
18528 use "goto done;" to return. */
18529 /* Make sure that the right number of template parameters were
18530 present. */
18531 if (!cp_parser_check_template_parameters (parser, num_templates,
18532 type_start_token->location,
18533 /*declarator=*/NULL))
18535 /* If something went wrong, there is no point in even trying to
18536 process the class-definition. */
18537 type = NULL_TREE;
18538 goto done;
18541 /* Look up the type. */
18542 if (template_id_p)
18544 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18545 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18546 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18548 error_at (type_start_token->location,
18549 "function template %qD redeclared as a class template", id);
18550 type = error_mark_node;
18552 else
18554 type = TREE_TYPE (id);
18555 type = maybe_process_partial_specialization (type);
18557 if (nested_name_specifier)
18558 pushed_scope = push_scope (nested_name_specifier);
18560 else if (nested_name_specifier)
18562 tree class_type;
18564 /* Given:
18566 template <typename T> struct S { struct T };
18567 template <typename T> struct S<T>::T { };
18569 we will get a TYPENAME_TYPE when processing the definition of
18570 `S::T'. We need to resolve it to the actual type before we
18571 try to define it. */
18572 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18574 class_type = resolve_typename_type (TREE_TYPE (type),
18575 /*only_current_p=*/false);
18576 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18577 type = TYPE_NAME (class_type);
18578 else
18580 cp_parser_error (parser, "could not resolve typename type");
18581 type = error_mark_node;
18585 if (maybe_process_partial_specialization (TREE_TYPE (type))
18586 == error_mark_node)
18588 type = NULL_TREE;
18589 goto done;
18592 class_type = current_class_type;
18593 /* Enter the scope indicated by the nested-name-specifier. */
18594 pushed_scope = push_scope (nested_name_specifier);
18595 /* Get the canonical version of this type. */
18596 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18597 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18598 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18600 type = push_template_decl (type);
18601 if (type == error_mark_node)
18603 type = NULL_TREE;
18604 goto done;
18608 type = TREE_TYPE (type);
18609 *nested_name_specifier_p = true;
18611 else /* The name is not a nested name. */
18613 /* If the class was unnamed, create a dummy name. */
18614 if (!id)
18615 id = make_anon_name ();
18616 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18617 parser->num_template_parameter_lists);
18620 /* Indicate whether this class was declared as a `class' or as a
18621 `struct'. */
18622 if (TREE_CODE (type) == RECORD_TYPE)
18623 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18624 cp_parser_check_class_key (class_key, type);
18626 /* If this type was already complete, and we see another definition,
18627 that's an error. */
18628 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18630 error_at (type_start_token->location, "redefinition of %q#T",
18631 type);
18632 error_at (type_start_token->location, "previous definition of %q+#T",
18633 type);
18634 type = NULL_TREE;
18635 goto done;
18637 else if (type == error_mark_node)
18638 type = NULL_TREE;
18640 if (type)
18642 /* Apply attributes now, before any use of the class as a template
18643 argument in its base list. */
18644 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18645 fixup_attribute_variants (type);
18648 /* We will have entered the scope containing the class; the names of
18649 base classes should be looked up in that context. For example:
18651 struct A { struct B {}; struct C; };
18652 struct A::C : B {};
18654 is valid. */
18656 /* Get the list of base-classes, if there is one. */
18657 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18658 bases = cp_parser_base_clause (parser);
18659 else
18660 bases = NULL_TREE;
18662 /* If we're really defining a class, process the base classes.
18663 If they're invalid, fail. */
18664 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
18665 && !xref_basetypes (type, bases))
18666 type = NULL_TREE;
18668 done:
18669 /* Leave the scope given by the nested-name-specifier. We will
18670 enter the class scope itself while processing the members. */
18671 if (pushed_scope)
18672 pop_scope (pushed_scope);
18674 if (invalid_explicit_specialization_p)
18676 end_specialization ();
18677 --parser->num_template_parameter_lists;
18680 if (type)
18681 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18682 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18683 CLASSTYPE_FINAL (type) = 1;
18684 out:
18685 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18686 return type;
18689 /* Parse a class-key.
18691 class-key:
18692 class
18693 struct
18694 union
18696 Returns the kind of class-key specified, or none_type to indicate
18697 error. */
18699 static enum tag_types
18700 cp_parser_class_key (cp_parser* parser)
18702 cp_token *token;
18703 enum tag_types tag_type;
18705 /* Look for the class-key. */
18706 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18707 if (!token)
18708 return none_type;
18710 /* Check to see if the TOKEN is a class-key. */
18711 tag_type = cp_parser_token_is_class_key (token);
18712 if (!tag_type)
18713 cp_parser_error (parser, "expected class-key");
18714 return tag_type;
18717 /* Parse an (optional) member-specification.
18719 member-specification:
18720 member-declaration member-specification [opt]
18721 access-specifier : member-specification [opt] */
18723 static void
18724 cp_parser_member_specification_opt (cp_parser* parser)
18726 while (true)
18728 cp_token *token;
18729 enum rid keyword;
18731 /* Peek at the next token. */
18732 token = cp_lexer_peek_token (parser->lexer);
18733 /* If it's a `}', or EOF then we've seen all the members. */
18734 if (token->type == CPP_CLOSE_BRACE
18735 || token->type == CPP_EOF
18736 || token->type == CPP_PRAGMA_EOL)
18737 break;
18739 /* See if this token is a keyword. */
18740 keyword = token->keyword;
18741 switch (keyword)
18743 case RID_PUBLIC:
18744 case RID_PROTECTED:
18745 case RID_PRIVATE:
18746 /* Consume the access-specifier. */
18747 cp_lexer_consume_token (parser->lexer);
18748 /* Remember which access-specifier is active. */
18749 current_access_specifier = token->u.value;
18750 /* Look for the `:'. */
18751 cp_parser_require (parser, CPP_COLON, RT_COLON);
18752 break;
18754 default:
18755 /* Accept #pragmas at class scope. */
18756 if (token->type == CPP_PRAGMA)
18758 cp_parser_pragma (parser, pragma_external);
18759 break;
18762 /* Otherwise, the next construction must be a
18763 member-declaration. */
18764 cp_parser_member_declaration (parser);
18769 /* Parse a member-declaration.
18771 member-declaration:
18772 decl-specifier-seq [opt] member-declarator-list [opt] ;
18773 function-definition ; [opt]
18774 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18775 using-declaration
18776 template-declaration
18777 alias-declaration
18779 member-declarator-list:
18780 member-declarator
18781 member-declarator-list , member-declarator
18783 member-declarator:
18784 declarator pure-specifier [opt]
18785 declarator constant-initializer [opt]
18786 identifier [opt] : constant-expression
18788 GNU Extensions:
18790 member-declaration:
18791 __extension__ member-declaration
18793 member-declarator:
18794 declarator attributes [opt] pure-specifier [opt]
18795 declarator attributes [opt] constant-initializer [opt]
18796 identifier [opt] attributes [opt] : constant-expression
18798 C++0x Extensions:
18800 member-declaration:
18801 static_assert-declaration */
18803 static void
18804 cp_parser_member_declaration (cp_parser* parser)
18806 cp_decl_specifier_seq decl_specifiers;
18807 tree prefix_attributes;
18808 tree decl;
18809 int declares_class_or_enum;
18810 bool friend_p;
18811 cp_token *token = NULL;
18812 cp_token *decl_spec_token_start = NULL;
18813 cp_token *initializer_token_start = NULL;
18814 int saved_pedantic;
18815 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18817 /* Check for the `__extension__' keyword. */
18818 if (cp_parser_extension_opt (parser, &saved_pedantic))
18820 /* Recurse. */
18821 cp_parser_member_declaration (parser);
18822 /* Restore the old value of the PEDANTIC flag. */
18823 pedantic = saved_pedantic;
18825 return;
18828 /* Check for a template-declaration. */
18829 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18831 /* An explicit specialization here is an error condition, and we
18832 expect the specialization handler to detect and report this. */
18833 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18834 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18835 cp_parser_explicit_specialization (parser);
18836 else
18837 cp_parser_template_declaration (parser, /*member_p=*/true);
18839 return;
18842 /* Check for a using-declaration. */
18843 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18845 if (cxx_dialect < cxx0x)
18847 /* Parse the using-declaration. */
18848 cp_parser_using_declaration (parser,
18849 /*access_declaration_p=*/false);
18850 return;
18852 else
18854 tree decl;
18855 cp_parser_parse_tentatively (parser);
18856 decl = cp_parser_alias_declaration (parser);
18857 if (cp_parser_parse_definitely (parser))
18858 finish_member_declaration (decl);
18859 else
18860 cp_parser_using_declaration (parser,
18861 /*access_declaration_p=*/false);
18862 return;
18866 /* Check for @defs. */
18867 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18869 tree ivar, member;
18870 tree ivar_chains = cp_parser_objc_defs_expression (parser);
18871 ivar = ivar_chains;
18872 while (ivar)
18874 member = ivar;
18875 ivar = TREE_CHAIN (member);
18876 TREE_CHAIN (member) = NULL_TREE;
18877 finish_member_declaration (member);
18879 return;
18882 /* If the next token is `static_assert' we have a static assertion. */
18883 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18885 cp_parser_static_assert (parser, /*member_p=*/true);
18886 return;
18889 parser->colon_corrects_to_scope_p = false;
18891 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18892 goto out;
18894 /* Parse the decl-specifier-seq. */
18895 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18896 cp_parser_decl_specifier_seq (parser,
18897 CP_PARSER_FLAGS_OPTIONAL,
18898 &decl_specifiers,
18899 &declares_class_or_enum);
18900 prefix_attributes = decl_specifiers.attributes;
18901 decl_specifiers.attributes = NULL_TREE;
18902 /* Check for an invalid type-name. */
18903 if (!decl_specifiers.any_type_specifiers_p
18904 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18905 goto out;
18906 /* If there is no declarator, then the decl-specifier-seq should
18907 specify a type. */
18908 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18910 /* If there was no decl-specifier-seq, and the next token is a
18911 `;', then we have something like:
18913 struct S { ; };
18915 [class.mem]
18917 Each member-declaration shall declare at least one member
18918 name of the class. */
18919 if (!decl_specifiers.any_specifiers_p)
18921 cp_token *token = cp_lexer_peek_token (parser->lexer);
18922 if (!in_system_header_at (token->location))
18923 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
18925 else
18927 tree type;
18929 /* See if this declaration is a friend. */
18930 friend_p = cp_parser_friend_p (&decl_specifiers);
18931 /* If there were decl-specifiers, check to see if there was
18932 a class-declaration. */
18933 type = check_tag_decl (&decl_specifiers);
18934 /* Nested classes have already been added to the class, but
18935 a `friend' needs to be explicitly registered. */
18936 if (friend_p)
18938 /* If the `friend' keyword was present, the friend must
18939 be introduced with a class-key. */
18940 if (!declares_class_or_enum && cxx_dialect < cxx0x)
18941 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
18942 "in C++03 a class-key must be used "
18943 "when declaring a friend");
18944 /* In this case:
18946 template <typename T> struct A {
18947 friend struct A<T>::B;
18950 A<T>::B will be represented by a TYPENAME_TYPE, and
18951 therefore not recognized by check_tag_decl. */
18952 if (!type)
18954 type = decl_specifiers.type;
18955 if (type && TREE_CODE (type) == TYPE_DECL)
18956 type = TREE_TYPE (type);
18958 if (!type || !TYPE_P (type))
18959 error_at (decl_spec_token_start->location,
18960 "friend declaration does not name a class or "
18961 "function");
18962 else
18963 make_friend_class (current_class_type, type,
18964 /*complain=*/true);
18966 /* If there is no TYPE, an error message will already have
18967 been issued. */
18968 else if (!type || type == error_mark_node)
18970 /* An anonymous aggregate has to be handled specially; such
18971 a declaration really declares a data member (with a
18972 particular type), as opposed to a nested class. */
18973 else if (ANON_AGGR_TYPE_P (type))
18975 /* C++11 9.5/6. */
18976 if (decl_specifiers.storage_class != sc_none)
18977 error_at (decl_spec_token_start->location,
18978 "a storage class on an anonymous aggregate "
18979 "in class scope is not allowed");
18981 /* Remove constructors and such from TYPE, now that we
18982 know it is an anonymous aggregate. */
18983 fixup_anonymous_aggr (type);
18984 /* And make the corresponding data member. */
18985 decl = build_decl (decl_spec_token_start->location,
18986 FIELD_DECL, NULL_TREE, type);
18987 /* Add it to the class. */
18988 finish_member_declaration (decl);
18990 else
18991 cp_parser_check_access_in_redeclaration
18992 (TYPE_NAME (type),
18993 decl_spec_token_start->location);
18996 else
18998 bool assume_semicolon = false;
19000 /* See if these declarations will be friends. */
19001 friend_p = cp_parser_friend_p (&decl_specifiers);
19003 /* Keep going until we hit the `;' at the end of the
19004 declaration. */
19005 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19007 tree attributes = NULL_TREE;
19008 tree first_attribute;
19010 /* Peek at the next token. */
19011 token = cp_lexer_peek_token (parser->lexer);
19013 /* Check for a bitfield declaration. */
19014 if (token->type == CPP_COLON
19015 || (token->type == CPP_NAME
19016 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19017 == CPP_COLON))
19019 tree identifier;
19020 tree width;
19022 /* Get the name of the bitfield. Note that we cannot just
19023 check TOKEN here because it may have been invalidated by
19024 the call to cp_lexer_peek_nth_token above. */
19025 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19026 identifier = cp_parser_identifier (parser);
19027 else
19028 identifier = NULL_TREE;
19030 /* Consume the `:' token. */
19031 cp_lexer_consume_token (parser->lexer);
19032 /* Get the width of the bitfield. */
19033 width
19034 = cp_parser_constant_expression (parser,
19035 /*allow_non_constant=*/false,
19036 NULL);
19038 /* Look for attributes that apply to the bitfield. */
19039 attributes = cp_parser_attributes_opt (parser);
19040 /* Remember which attributes are prefix attributes and
19041 which are not. */
19042 first_attribute = attributes;
19043 /* Combine the attributes. */
19044 attributes = chainon (prefix_attributes, attributes);
19046 /* Create the bitfield declaration. */
19047 decl = grokbitfield (identifier
19048 ? make_id_declarator (NULL_TREE,
19049 identifier,
19050 sfk_none)
19051 : NULL,
19052 &decl_specifiers,
19053 width,
19054 attributes);
19056 else
19058 cp_declarator *declarator;
19059 tree initializer;
19060 tree asm_specification;
19061 int ctor_dtor_or_conv_p;
19063 /* Parse the declarator. */
19064 declarator
19065 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19066 &ctor_dtor_or_conv_p,
19067 /*parenthesized_p=*/NULL,
19068 /*member_p=*/true);
19070 /* If something went wrong parsing the declarator, make sure
19071 that we at least consume some tokens. */
19072 if (declarator == cp_error_declarator)
19074 /* Skip to the end of the statement. */
19075 cp_parser_skip_to_end_of_statement (parser);
19076 /* If the next token is not a semicolon, that is
19077 probably because we just skipped over the body of
19078 a function. So, we consume a semicolon if
19079 present, but do not issue an error message if it
19080 is not present. */
19081 if (cp_lexer_next_token_is (parser->lexer,
19082 CPP_SEMICOLON))
19083 cp_lexer_consume_token (parser->lexer);
19084 goto out;
19087 if (declares_class_or_enum & 2)
19088 cp_parser_check_for_definition_in_return_type
19089 (declarator, decl_specifiers.type,
19090 decl_specifiers.locations[ds_type_spec]);
19092 /* Look for an asm-specification. */
19093 asm_specification = cp_parser_asm_specification_opt (parser);
19094 /* Look for attributes that apply to the declaration. */
19095 attributes = cp_parser_attributes_opt (parser);
19096 /* Remember which attributes are prefix attributes and
19097 which are not. */
19098 first_attribute = attributes;
19099 /* Combine the attributes. */
19100 attributes = chainon (prefix_attributes, attributes);
19102 /* If it's an `=', then we have a constant-initializer or a
19103 pure-specifier. It is not correct to parse the
19104 initializer before registering the member declaration
19105 since the member declaration should be in scope while
19106 its initializer is processed. However, the rest of the
19107 front end does not yet provide an interface that allows
19108 us to handle this correctly. */
19109 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19111 /* In [class.mem]:
19113 A pure-specifier shall be used only in the declaration of
19114 a virtual function.
19116 A member-declarator can contain a constant-initializer
19117 only if it declares a static member of integral or
19118 enumeration type.
19120 Therefore, if the DECLARATOR is for a function, we look
19121 for a pure-specifier; otherwise, we look for a
19122 constant-initializer. When we call `grokfield', it will
19123 perform more stringent semantics checks. */
19124 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19125 if (function_declarator_p (declarator)
19126 || (decl_specifiers.type
19127 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19128 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19129 == FUNCTION_TYPE)))
19130 initializer = cp_parser_pure_specifier (parser);
19131 else if (decl_specifiers.storage_class != sc_static)
19132 initializer = cp_parser_save_nsdmi (parser);
19133 else if (cxx_dialect >= cxx0x)
19135 bool nonconst;
19136 /* Don't require a constant rvalue in C++11, since we
19137 might want a reference constant. We'll enforce
19138 constancy later. */
19139 cp_lexer_consume_token (parser->lexer);
19140 /* Parse the initializer. */
19141 initializer = cp_parser_initializer_clause (parser,
19142 &nonconst);
19144 else
19145 /* Parse the initializer. */
19146 initializer = cp_parser_constant_initializer (parser);
19148 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19149 && !function_declarator_p (declarator))
19151 bool x;
19152 if (decl_specifiers.storage_class != sc_static)
19153 initializer = cp_parser_save_nsdmi (parser);
19154 else
19155 initializer = cp_parser_initializer (parser, &x, &x);
19157 /* Otherwise, there is no initializer. */
19158 else
19159 initializer = NULL_TREE;
19161 /* See if we are probably looking at a function
19162 definition. We are certainly not looking at a
19163 member-declarator. Calling `grokfield' has
19164 side-effects, so we must not do it unless we are sure
19165 that we are looking at a member-declarator. */
19166 if (cp_parser_token_starts_function_definition_p
19167 (cp_lexer_peek_token (parser->lexer)))
19169 /* The grammar does not allow a pure-specifier to be
19170 used when a member function is defined. (It is
19171 possible that this fact is an oversight in the
19172 standard, since a pure function may be defined
19173 outside of the class-specifier. */
19174 if (initializer && initializer_token_start)
19175 error_at (initializer_token_start->location,
19176 "pure-specifier on function-definition");
19177 decl = cp_parser_save_member_function_body (parser,
19178 &decl_specifiers,
19179 declarator,
19180 attributes);
19181 /* If the member was not a friend, declare it here. */
19182 if (!friend_p)
19183 finish_member_declaration (decl);
19184 /* Peek at the next token. */
19185 token = cp_lexer_peek_token (parser->lexer);
19186 /* If the next token is a semicolon, consume it. */
19187 if (token->type == CPP_SEMICOLON)
19188 cp_lexer_consume_token (parser->lexer);
19189 goto out;
19191 else
19192 if (declarator->kind == cdk_function)
19193 declarator->id_loc = token->location;
19194 /* Create the declaration. */
19195 decl = grokfield (declarator, &decl_specifiers,
19196 initializer, /*init_const_expr_p=*/true,
19197 asm_specification,
19198 attributes);
19201 /* Reset PREFIX_ATTRIBUTES. */
19202 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19203 attributes = TREE_CHAIN (attributes);
19204 if (attributes)
19205 TREE_CHAIN (attributes) = NULL_TREE;
19207 /* If there is any qualification still in effect, clear it
19208 now; we will be starting fresh with the next declarator. */
19209 parser->scope = NULL_TREE;
19210 parser->qualifying_scope = NULL_TREE;
19211 parser->object_scope = NULL_TREE;
19212 /* If it's a `,', then there are more declarators. */
19213 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19214 cp_lexer_consume_token (parser->lexer);
19215 /* If the next token isn't a `;', then we have a parse error. */
19216 else if (cp_lexer_next_token_is_not (parser->lexer,
19217 CPP_SEMICOLON))
19219 /* The next token might be a ways away from where the
19220 actual semicolon is missing. Find the previous token
19221 and use that for our error position. */
19222 cp_token *token = cp_lexer_previous_token (parser->lexer);
19223 error_at (token->location,
19224 "expected %<;%> at end of member declaration");
19226 /* Assume that the user meant to provide a semicolon. If
19227 we were to cp_parser_skip_to_end_of_statement, we might
19228 skip to a semicolon inside a member function definition
19229 and issue nonsensical error messages. */
19230 assume_semicolon = true;
19233 if (decl)
19235 /* Add DECL to the list of members. */
19236 if (!friend_p)
19237 finish_member_declaration (decl);
19239 if (TREE_CODE (decl) == FUNCTION_DECL)
19240 cp_parser_save_default_args (parser, decl);
19241 else if (TREE_CODE (decl) == FIELD_DECL
19242 && !DECL_C_BIT_FIELD (decl)
19243 && DECL_INITIAL (decl))
19244 /* Add DECL to the queue of NSDMI to be parsed later. */
19245 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19248 if (assume_semicolon)
19249 goto out;
19253 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19254 out:
19255 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19258 /* Parse a pure-specifier.
19260 pure-specifier:
19263 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19264 Otherwise, ERROR_MARK_NODE is returned. */
19266 static tree
19267 cp_parser_pure_specifier (cp_parser* parser)
19269 cp_token *token;
19271 /* Look for the `=' token. */
19272 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19273 return error_mark_node;
19274 /* Look for the `0' token. */
19275 token = cp_lexer_peek_token (parser->lexer);
19277 if (token->type == CPP_EOF
19278 || token->type == CPP_PRAGMA_EOL)
19279 return error_mark_node;
19281 cp_lexer_consume_token (parser->lexer);
19283 /* Accept = default or = delete in c++0x mode. */
19284 if (token->keyword == RID_DEFAULT
19285 || token->keyword == RID_DELETE)
19287 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19288 return token->u.value;
19291 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19292 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19294 cp_parser_error (parser,
19295 "invalid pure specifier (only %<= 0%> is allowed)");
19296 cp_parser_skip_to_end_of_statement (parser);
19297 return error_mark_node;
19299 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19301 error_at (token->location, "templates may not be %<virtual%>");
19302 return error_mark_node;
19305 return integer_zero_node;
19308 /* Parse a constant-initializer.
19310 constant-initializer:
19311 = constant-expression
19313 Returns a representation of the constant-expression. */
19315 static tree
19316 cp_parser_constant_initializer (cp_parser* parser)
19318 /* Look for the `=' token. */
19319 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19320 return error_mark_node;
19322 /* It is invalid to write:
19324 struct S { static const int i = { 7 }; };
19327 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19329 cp_parser_error (parser,
19330 "a brace-enclosed initializer is not allowed here");
19331 /* Consume the opening brace. */
19332 cp_lexer_consume_token (parser->lexer);
19333 /* Skip the initializer. */
19334 cp_parser_skip_to_closing_brace (parser);
19335 /* Look for the trailing `}'. */
19336 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19338 return error_mark_node;
19341 return cp_parser_constant_expression (parser,
19342 /*allow_non_constant=*/false,
19343 NULL);
19346 /* Derived classes [gram.class.derived] */
19348 /* Parse a base-clause.
19350 base-clause:
19351 : base-specifier-list
19353 base-specifier-list:
19354 base-specifier ... [opt]
19355 base-specifier-list , base-specifier ... [opt]
19357 Returns a TREE_LIST representing the base-classes, in the order in
19358 which they were declared. The representation of each node is as
19359 described by cp_parser_base_specifier.
19361 In the case that no bases are specified, this function will return
19362 NULL_TREE, not ERROR_MARK_NODE. */
19364 static tree
19365 cp_parser_base_clause (cp_parser* parser)
19367 tree bases = NULL_TREE;
19369 /* Look for the `:' that begins the list. */
19370 cp_parser_require (parser, CPP_COLON, RT_COLON);
19372 /* Scan the base-specifier-list. */
19373 while (true)
19375 cp_token *token;
19376 tree base;
19377 bool pack_expansion_p = false;
19379 /* Look for the base-specifier. */
19380 base = cp_parser_base_specifier (parser);
19381 /* Look for the (optional) ellipsis. */
19382 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19384 /* Consume the `...'. */
19385 cp_lexer_consume_token (parser->lexer);
19387 pack_expansion_p = true;
19390 /* Add BASE to the front of the list. */
19391 if (base && base != error_mark_node)
19393 if (pack_expansion_p)
19394 /* Make this a pack expansion type. */
19395 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19397 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19399 TREE_CHAIN (base) = bases;
19400 bases = base;
19403 /* Peek at the next token. */
19404 token = cp_lexer_peek_token (parser->lexer);
19405 /* If it's not a comma, then the list is complete. */
19406 if (token->type != CPP_COMMA)
19407 break;
19408 /* Consume the `,'. */
19409 cp_lexer_consume_token (parser->lexer);
19412 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19413 base class had a qualified name. However, the next name that
19414 appears is certainly not qualified. */
19415 parser->scope = NULL_TREE;
19416 parser->qualifying_scope = NULL_TREE;
19417 parser->object_scope = NULL_TREE;
19419 return nreverse (bases);
19422 /* Parse a base-specifier.
19424 base-specifier:
19425 :: [opt] nested-name-specifier [opt] class-name
19426 virtual access-specifier [opt] :: [opt] nested-name-specifier
19427 [opt] class-name
19428 access-specifier virtual [opt] :: [opt] nested-name-specifier
19429 [opt] class-name
19431 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19432 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19433 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19434 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19436 static tree
19437 cp_parser_base_specifier (cp_parser* parser)
19439 cp_token *token;
19440 bool done = false;
19441 bool virtual_p = false;
19442 bool duplicate_virtual_error_issued_p = false;
19443 bool duplicate_access_error_issued_p = false;
19444 bool class_scope_p, template_p;
19445 tree access = access_default_node;
19446 tree type;
19448 /* Process the optional `virtual' and `access-specifier'. */
19449 while (!done)
19451 /* Peek at the next token. */
19452 token = cp_lexer_peek_token (parser->lexer);
19453 /* Process `virtual'. */
19454 switch (token->keyword)
19456 case RID_VIRTUAL:
19457 /* If `virtual' appears more than once, issue an error. */
19458 if (virtual_p && !duplicate_virtual_error_issued_p)
19460 cp_parser_error (parser,
19461 "%<virtual%> specified more than once in base-specified");
19462 duplicate_virtual_error_issued_p = true;
19465 virtual_p = true;
19467 /* Consume the `virtual' token. */
19468 cp_lexer_consume_token (parser->lexer);
19470 break;
19472 case RID_PUBLIC:
19473 case RID_PROTECTED:
19474 case RID_PRIVATE:
19475 /* If more than one access specifier appears, issue an
19476 error. */
19477 if (access != access_default_node
19478 && !duplicate_access_error_issued_p)
19480 cp_parser_error (parser,
19481 "more than one access specifier in base-specified");
19482 duplicate_access_error_issued_p = true;
19485 access = ridpointers[(int) token->keyword];
19487 /* Consume the access-specifier. */
19488 cp_lexer_consume_token (parser->lexer);
19490 break;
19492 default:
19493 done = true;
19494 break;
19497 /* It is not uncommon to see programs mechanically, erroneously, use
19498 the 'typename' keyword to denote (dependent) qualified types
19499 as base classes. */
19500 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19502 token = cp_lexer_peek_token (parser->lexer);
19503 if (!processing_template_decl)
19504 error_at (token->location,
19505 "keyword %<typename%> not allowed outside of templates");
19506 else
19507 error_at (token->location,
19508 "keyword %<typename%> not allowed in this context "
19509 "(the base class is implicitly a type)");
19510 cp_lexer_consume_token (parser->lexer);
19513 /* Look for the optional `::' operator. */
19514 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19515 /* Look for the nested-name-specifier. The simplest way to
19516 implement:
19518 [temp.res]
19520 The keyword `typename' is not permitted in a base-specifier or
19521 mem-initializer; in these contexts a qualified name that
19522 depends on a template-parameter is implicitly assumed to be a
19523 type name.
19525 is to pretend that we have seen the `typename' keyword at this
19526 point. */
19527 cp_parser_nested_name_specifier_opt (parser,
19528 /*typename_keyword_p=*/true,
19529 /*check_dependency_p=*/true,
19530 typename_type,
19531 /*is_declaration=*/true);
19532 /* If the base class is given by a qualified name, assume that names
19533 we see are type names or templates, as appropriate. */
19534 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19535 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19537 if (!parser->scope
19538 && cp_lexer_next_token_is_decltype (parser->lexer))
19539 /* DR 950 allows decltype as a base-specifier. */
19540 type = cp_parser_decltype (parser);
19541 else
19543 /* Otherwise, look for the class-name. */
19544 type = cp_parser_class_name (parser,
19545 class_scope_p,
19546 template_p,
19547 typename_type,
19548 /*check_dependency_p=*/true,
19549 /*class_head_p=*/false,
19550 /*is_declaration=*/true);
19551 type = TREE_TYPE (type);
19554 if (type == error_mark_node)
19555 return error_mark_node;
19557 return finish_base_specifier (type, access, virtual_p);
19560 /* Exception handling [gram.exception] */
19562 /* Parse an (optional) noexcept-specification.
19564 noexcept-specification:
19565 noexcept ( constant-expression ) [opt]
19567 If no noexcept-specification is present, returns NULL_TREE.
19568 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19569 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19570 there are no parentheses. CONSUMED_EXPR will be set accordingly.
19571 Otherwise, returns a noexcept specification unless RETURN_COND is true,
19572 in which case a boolean condition is returned instead. */
19574 static tree
19575 cp_parser_noexcept_specification_opt (cp_parser* parser,
19576 bool require_constexpr,
19577 bool* consumed_expr,
19578 bool return_cond)
19580 cp_token *token;
19581 const char *saved_message;
19583 /* Peek at the next token. */
19584 token = cp_lexer_peek_token (parser->lexer);
19586 /* Is it a noexcept-specification? */
19587 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19589 tree expr;
19590 cp_lexer_consume_token (parser->lexer);
19592 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19594 cp_lexer_consume_token (parser->lexer);
19596 if (require_constexpr)
19598 /* Types may not be defined in an exception-specification. */
19599 saved_message = parser->type_definition_forbidden_message;
19600 parser->type_definition_forbidden_message
19601 = G_("types may not be defined in an exception-specification");
19603 expr = cp_parser_constant_expression (parser, false, NULL);
19605 /* Restore the saved message. */
19606 parser->type_definition_forbidden_message = saved_message;
19608 else
19610 expr = cp_parser_expression (parser, false, NULL);
19611 *consumed_expr = true;
19614 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19616 else
19618 expr = boolean_true_node;
19619 if (!require_constexpr)
19620 *consumed_expr = false;
19623 /* We cannot build a noexcept-spec right away because this will check
19624 that expr is a constexpr. */
19625 if (!return_cond)
19626 return build_noexcept_spec (expr, tf_warning_or_error);
19627 else
19628 return expr;
19630 else
19631 return NULL_TREE;
19634 /* Parse an (optional) exception-specification.
19636 exception-specification:
19637 throw ( type-id-list [opt] )
19639 Returns a TREE_LIST representing the exception-specification. The
19640 TREE_VALUE of each node is a type. */
19642 static tree
19643 cp_parser_exception_specification_opt (cp_parser* parser)
19645 cp_token *token;
19646 tree type_id_list;
19647 const char *saved_message;
19649 /* Peek at the next token. */
19650 token = cp_lexer_peek_token (parser->lexer);
19652 /* Is it a noexcept-specification? */
19653 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19654 false);
19655 if (type_id_list != NULL_TREE)
19656 return type_id_list;
19658 /* If it's not `throw', then there's no exception-specification. */
19659 if (!cp_parser_is_keyword (token, RID_THROW))
19660 return NULL_TREE;
19662 #if 0
19663 /* Enable this once a lot of code has transitioned to noexcept? */
19664 if (cxx_dialect >= cxx0x && !in_system_header)
19665 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19666 "deprecated in C++0x; use %<noexcept%> instead");
19667 #endif
19669 /* Consume the `throw'. */
19670 cp_lexer_consume_token (parser->lexer);
19672 /* Look for the `('. */
19673 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19675 /* Peek at the next token. */
19676 token = cp_lexer_peek_token (parser->lexer);
19677 /* If it's not a `)', then there is a type-id-list. */
19678 if (token->type != CPP_CLOSE_PAREN)
19680 /* Types may not be defined in an exception-specification. */
19681 saved_message = parser->type_definition_forbidden_message;
19682 parser->type_definition_forbidden_message
19683 = G_("types may not be defined in an exception-specification");
19684 /* Parse the type-id-list. */
19685 type_id_list = cp_parser_type_id_list (parser);
19686 /* Restore the saved message. */
19687 parser->type_definition_forbidden_message = saved_message;
19689 else
19690 type_id_list = empty_except_spec;
19692 /* Look for the `)'. */
19693 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19695 return type_id_list;
19698 /* Parse an (optional) type-id-list.
19700 type-id-list:
19701 type-id ... [opt]
19702 type-id-list , type-id ... [opt]
19704 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19705 in the order that the types were presented. */
19707 static tree
19708 cp_parser_type_id_list (cp_parser* parser)
19710 tree types = NULL_TREE;
19712 while (true)
19714 cp_token *token;
19715 tree type;
19717 /* Get the next type-id. */
19718 type = cp_parser_type_id (parser);
19719 /* Parse the optional ellipsis. */
19720 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19722 /* Consume the `...'. */
19723 cp_lexer_consume_token (parser->lexer);
19725 /* Turn the type into a pack expansion expression. */
19726 type = make_pack_expansion (type);
19728 /* Add it to the list. */
19729 types = add_exception_specifier (types, type, /*complain=*/1);
19730 /* Peek at the next token. */
19731 token = cp_lexer_peek_token (parser->lexer);
19732 /* If it is not a `,', we are done. */
19733 if (token->type != CPP_COMMA)
19734 break;
19735 /* Consume the `,'. */
19736 cp_lexer_consume_token (parser->lexer);
19739 return nreverse (types);
19742 /* Parse a try-block.
19744 try-block:
19745 try compound-statement handler-seq */
19747 static tree
19748 cp_parser_try_block (cp_parser* parser)
19750 tree try_block;
19752 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19753 try_block = begin_try_block ();
19754 cp_parser_compound_statement (parser, NULL, true, false);
19755 finish_try_block (try_block);
19756 cp_parser_handler_seq (parser);
19757 finish_handler_sequence (try_block);
19759 return try_block;
19762 /* Parse a function-try-block.
19764 function-try-block:
19765 try ctor-initializer [opt] function-body handler-seq */
19767 static bool
19768 cp_parser_function_try_block (cp_parser* parser)
19770 tree compound_stmt;
19771 tree try_block;
19772 bool ctor_initializer_p;
19774 /* Look for the `try' keyword. */
19775 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19776 return false;
19777 /* Let the rest of the front end know where we are. */
19778 try_block = begin_function_try_block (&compound_stmt);
19779 /* Parse the function-body. */
19780 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
19781 (parser, /*in_function_try_block=*/true);
19782 /* We're done with the `try' part. */
19783 finish_function_try_block (try_block);
19784 /* Parse the handlers. */
19785 cp_parser_handler_seq (parser);
19786 /* We're done with the handlers. */
19787 finish_function_handler_sequence (try_block, compound_stmt);
19789 return ctor_initializer_p;
19792 /* Parse a handler-seq.
19794 handler-seq:
19795 handler handler-seq [opt] */
19797 static void
19798 cp_parser_handler_seq (cp_parser* parser)
19800 while (true)
19802 cp_token *token;
19804 /* Parse the handler. */
19805 cp_parser_handler (parser);
19806 /* Peek at the next token. */
19807 token = cp_lexer_peek_token (parser->lexer);
19808 /* If it's not `catch' then there are no more handlers. */
19809 if (!cp_parser_is_keyword (token, RID_CATCH))
19810 break;
19814 /* Parse a handler.
19816 handler:
19817 catch ( exception-declaration ) compound-statement */
19819 static void
19820 cp_parser_handler (cp_parser* parser)
19822 tree handler;
19823 tree declaration;
19825 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19826 handler = begin_handler ();
19827 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19828 declaration = cp_parser_exception_declaration (parser);
19829 finish_handler_parms (declaration, handler);
19830 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19831 cp_parser_compound_statement (parser, NULL, false, false);
19832 finish_handler (handler);
19835 /* Parse an exception-declaration.
19837 exception-declaration:
19838 type-specifier-seq declarator
19839 type-specifier-seq abstract-declarator
19840 type-specifier-seq
19843 Returns a VAR_DECL for the declaration, or NULL_TREE if the
19844 ellipsis variant is used. */
19846 static tree
19847 cp_parser_exception_declaration (cp_parser* parser)
19849 cp_decl_specifier_seq type_specifiers;
19850 cp_declarator *declarator;
19851 const char *saved_message;
19853 /* If it's an ellipsis, it's easy to handle. */
19854 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19856 /* Consume the `...' token. */
19857 cp_lexer_consume_token (parser->lexer);
19858 return NULL_TREE;
19861 /* Types may not be defined in exception-declarations. */
19862 saved_message = parser->type_definition_forbidden_message;
19863 parser->type_definition_forbidden_message
19864 = G_("types may not be defined in exception-declarations");
19866 /* Parse the type-specifier-seq. */
19867 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19868 /*is_trailing_return=*/false,
19869 &type_specifiers);
19870 /* If it's a `)', then there is no declarator. */
19871 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19872 declarator = NULL;
19873 else
19874 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19875 /*ctor_dtor_or_conv_p=*/NULL,
19876 /*parenthesized_p=*/NULL,
19877 /*member_p=*/false);
19879 /* Restore the saved message. */
19880 parser->type_definition_forbidden_message = saved_message;
19882 if (!type_specifiers.any_specifiers_p)
19883 return error_mark_node;
19885 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19888 /* Parse a throw-expression.
19890 throw-expression:
19891 throw assignment-expression [opt]
19893 Returns a THROW_EXPR representing the throw-expression. */
19895 static tree
19896 cp_parser_throw_expression (cp_parser* parser)
19898 tree expression;
19899 cp_token* token;
19901 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19902 token = cp_lexer_peek_token (parser->lexer);
19903 /* Figure out whether or not there is an assignment-expression
19904 following the "throw" keyword. */
19905 if (token->type == CPP_COMMA
19906 || token->type == CPP_SEMICOLON
19907 || token->type == CPP_CLOSE_PAREN
19908 || token->type == CPP_CLOSE_SQUARE
19909 || token->type == CPP_CLOSE_BRACE
19910 || token->type == CPP_COLON)
19911 expression = NULL_TREE;
19912 else
19913 expression = cp_parser_assignment_expression (parser,
19914 /*cast_p=*/false, NULL);
19916 return build_throw (expression);
19919 /* GNU Extensions */
19921 /* Parse an (optional) asm-specification.
19923 asm-specification:
19924 asm ( string-literal )
19926 If the asm-specification is present, returns a STRING_CST
19927 corresponding to the string-literal. Otherwise, returns
19928 NULL_TREE. */
19930 static tree
19931 cp_parser_asm_specification_opt (cp_parser* parser)
19933 cp_token *token;
19934 tree asm_specification;
19936 /* Peek at the next token. */
19937 token = cp_lexer_peek_token (parser->lexer);
19938 /* If the next token isn't the `asm' keyword, then there's no
19939 asm-specification. */
19940 if (!cp_parser_is_keyword (token, RID_ASM))
19941 return NULL_TREE;
19943 /* Consume the `asm' token. */
19944 cp_lexer_consume_token (parser->lexer);
19945 /* Look for the `('. */
19946 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19948 /* Look for the string-literal. */
19949 asm_specification = cp_parser_string_literal (parser, false, false);
19951 /* Look for the `)'. */
19952 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19954 return asm_specification;
19957 /* Parse an asm-operand-list.
19959 asm-operand-list:
19960 asm-operand
19961 asm-operand-list , asm-operand
19963 asm-operand:
19964 string-literal ( expression )
19965 [ string-literal ] string-literal ( expression )
19967 Returns a TREE_LIST representing the operands. The TREE_VALUE of
19968 each node is the expression. The TREE_PURPOSE is itself a
19969 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19970 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19971 is a STRING_CST for the string literal before the parenthesis. Returns
19972 ERROR_MARK_NODE if any of the operands are invalid. */
19974 static tree
19975 cp_parser_asm_operand_list (cp_parser* parser)
19977 tree asm_operands = NULL_TREE;
19978 bool invalid_operands = false;
19980 while (true)
19982 tree string_literal;
19983 tree expression;
19984 tree name;
19986 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19988 /* Consume the `[' token. */
19989 cp_lexer_consume_token (parser->lexer);
19990 /* Read the operand name. */
19991 name = cp_parser_identifier (parser);
19992 if (name != error_mark_node)
19993 name = build_string (IDENTIFIER_LENGTH (name),
19994 IDENTIFIER_POINTER (name));
19995 /* Look for the closing `]'. */
19996 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19998 else
19999 name = NULL_TREE;
20000 /* Look for the string-literal. */
20001 string_literal = cp_parser_string_literal (parser, false, false);
20003 /* Look for the `('. */
20004 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20005 /* Parse the expression. */
20006 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20007 /* Look for the `)'. */
20008 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20010 if (name == error_mark_node
20011 || string_literal == error_mark_node
20012 || expression == error_mark_node)
20013 invalid_operands = true;
20015 /* Add this operand to the list. */
20016 asm_operands = tree_cons (build_tree_list (name, string_literal),
20017 expression,
20018 asm_operands);
20019 /* If the next token is not a `,', there are no more
20020 operands. */
20021 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20022 break;
20023 /* Consume the `,'. */
20024 cp_lexer_consume_token (parser->lexer);
20027 return invalid_operands ? error_mark_node : nreverse (asm_operands);
20030 /* Parse an asm-clobber-list.
20032 asm-clobber-list:
20033 string-literal
20034 asm-clobber-list , string-literal
20036 Returns a TREE_LIST, indicating the clobbers in the order that they
20037 appeared. The TREE_VALUE of each node is a STRING_CST. */
20039 static tree
20040 cp_parser_asm_clobber_list (cp_parser* parser)
20042 tree clobbers = NULL_TREE;
20044 while (true)
20046 tree string_literal;
20048 /* Look for the string literal. */
20049 string_literal = cp_parser_string_literal (parser, false, false);
20050 /* Add it to the list. */
20051 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20052 /* If the next token is not a `,', then the list is
20053 complete. */
20054 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20055 break;
20056 /* Consume the `,' token. */
20057 cp_lexer_consume_token (parser->lexer);
20060 return clobbers;
20063 /* Parse an asm-label-list.
20065 asm-label-list:
20066 identifier
20067 asm-label-list , identifier
20069 Returns a TREE_LIST, indicating the labels in the order that they
20070 appeared. The TREE_VALUE of each node is a label. */
20072 static tree
20073 cp_parser_asm_label_list (cp_parser* parser)
20075 tree labels = NULL_TREE;
20077 while (true)
20079 tree identifier, label, name;
20081 /* Look for the identifier. */
20082 identifier = cp_parser_identifier (parser);
20083 if (!error_operand_p (identifier))
20085 label = lookup_label (identifier);
20086 if (TREE_CODE (label) == LABEL_DECL)
20088 TREE_USED (label) = 1;
20089 check_goto (label);
20090 name = build_string (IDENTIFIER_LENGTH (identifier),
20091 IDENTIFIER_POINTER (identifier));
20092 labels = tree_cons (name, label, labels);
20095 /* If the next token is not a `,', then the list is
20096 complete. */
20097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20098 break;
20099 /* Consume the `,' token. */
20100 cp_lexer_consume_token (parser->lexer);
20103 return nreverse (labels);
20106 /* Parse an (optional) series of attributes.
20108 attributes:
20109 attributes attribute
20111 attribute:
20112 __attribute__ (( attribute-list [opt] ))
20114 The return value is as for cp_parser_attribute_list. */
20116 static tree
20117 cp_parser_attributes_opt (cp_parser* parser)
20119 tree attributes = NULL_TREE;
20121 while (true)
20123 cp_token *token;
20124 tree attribute_list;
20125 bool ok = true;
20127 /* Peek at the next token. */
20128 token = cp_lexer_peek_token (parser->lexer);
20129 /* If it's not `__attribute__', then we're done. */
20130 if (token->keyword != RID_ATTRIBUTE)
20131 break;
20133 /* Consume the `__attribute__' keyword. */
20134 cp_lexer_consume_token (parser->lexer);
20135 /* Look for the two `(' tokens. */
20136 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20137 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20139 /* Peek at the next token. */
20140 token = cp_lexer_peek_token (parser->lexer);
20141 if (token->type != CPP_CLOSE_PAREN)
20142 /* Parse the attribute-list. */
20143 attribute_list = cp_parser_attribute_list (parser);
20144 else
20145 /* If the next token is a `)', then there is no attribute
20146 list. */
20147 attribute_list = NULL;
20149 /* Look for the two `)' tokens. */
20150 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20151 ok = false;
20152 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20153 ok = false;
20154 if (!ok)
20155 cp_parser_skip_to_end_of_statement (parser);
20157 /* Add these new attributes to the list. */
20158 attributes = chainon (attributes, attribute_list);
20161 return attributes;
20164 /* Parse an attribute-list.
20166 attribute-list:
20167 attribute
20168 attribute-list , attribute
20170 attribute:
20171 identifier
20172 identifier ( identifier )
20173 identifier ( identifier , expression-list )
20174 identifier ( expression-list )
20176 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20177 to an attribute. The TREE_PURPOSE of each node is the identifier
20178 indicating which attribute is in use. The TREE_VALUE represents
20179 the arguments, if any. */
20181 static tree
20182 cp_parser_attribute_list (cp_parser* parser)
20184 tree attribute_list = NULL_TREE;
20185 bool save_translate_strings_p = parser->translate_strings_p;
20187 parser->translate_strings_p = false;
20188 while (true)
20190 cp_token *token;
20191 tree identifier;
20192 tree attribute;
20194 /* Look for the identifier. We also allow keywords here; for
20195 example `__attribute__ ((const))' is legal. */
20196 token = cp_lexer_peek_token (parser->lexer);
20197 if (token->type == CPP_NAME
20198 || token->type == CPP_KEYWORD)
20200 tree arguments = NULL_TREE;
20202 /* Consume the token. */
20203 token = cp_lexer_consume_token (parser->lexer);
20205 /* Save away the identifier that indicates which attribute
20206 this is. */
20207 identifier = (token->type == CPP_KEYWORD)
20208 /* For keywords, use the canonical spelling, not the
20209 parsed identifier. */
20210 ? ridpointers[(int) token->keyword]
20211 : token->u.value;
20213 attribute = build_tree_list (identifier, NULL_TREE);
20215 /* Peek at the next token. */
20216 token = cp_lexer_peek_token (parser->lexer);
20217 /* If it's an `(', then parse the attribute arguments. */
20218 if (token->type == CPP_OPEN_PAREN)
20220 VEC(tree,gc) *vec;
20221 int attr_flag = (attribute_takes_identifier_p (identifier)
20222 ? id_attr : normal_attr);
20223 vec = cp_parser_parenthesized_expression_list
20224 (parser, attr_flag, /*cast_p=*/false,
20225 /*allow_expansion_p=*/false,
20226 /*non_constant_p=*/NULL);
20227 if (vec == NULL)
20228 arguments = error_mark_node;
20229 else
20231 arguments = build_tree_list_vec (vec);
20232 release_tree_vector (vec);
20234 /* Save the arguments away. */
20235 TREE_VALUE (attribute) = arguments;
20238 if (arguments != error_mark_node)
20240 /* Add this attribute to the list. */
20241 TREE_CHAIN (attribute) = attribute_list;
20242 attribute_list = attribute;
20245 token = cp_lexer_peek_token (parser->lexer);
20247 /* Now, look for more attributes. If the next token isn't a
20248 `,', we're done. */
20249 if (token->type != CPP_COMMA)
20250 break;
20252 /* Consume the comma and keep going. */
20253 cp_lexer_consume_token (parser->lexer);
20255 parser->translate_strings_p = save_translate_strings_p;
20257 /* We built up the list in reverse order. */
20258 return nreverse (attribute_list);
20261 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20262 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20263 current value of the PEDANTIC flag, regardless of whether or not
20264 the `__extension__' keyword is present. The caller is responsible
20265 for restoring the value of the PEDANTIC flag. */
20267 static bool
20268 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20270 /* Save the old value of the PEDANTIC flag. */
20271 *saved_pedantic = pedantic;
20273 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20275 /* Consume the `__extension__' token. */
20276 cp_lexer_consume_token (parser->lexer);
20277 /* We're not being pedantic while the `__extension__' keyword is
20278 in effect. */
20279 pedantic = 0;
20281 return true;
20284 return false;
20287 /* Parse a label declaration.
20289 label-declaration:
20290 __label__ label-declarator-seq ;
20292 label-declarator-seq:
20293 identifier , label-declarator-seq
20294 identifier */
20296 static void
20297 cp_parser_label_declaration (cp_parser* parser)
20299 /* Look for the `__label__' keyword. */
20300 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20302 while (true)
20304 tree identifier;
20306 /* Look for an identifier. */
20307 identifier = cp_parser_identifier (parser);
20308 /* If we failed, stop. */
20309 if (identifier == error_mark_node)
20310 break;
20311 /* Declare it as a label. */
20312 finish_label_decl (identifier);
20313 /* If the next token is a `;', stop. */
20314 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20315 break;
20316 /* Look for the `,' separating the label declarations. */
20317 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20320 /* Look for the final `;'. */
20321 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20324 /* Support Functions */
20326 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20327 NAME should have one of the representations used for an
20328 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20329 is returned. If PARSER->SCOPE is a dependent type, then a
20330 SCOPE_REF is returned.
20332 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20333 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20334 was formed. Abstractly, such entities should not be passed to this
20335 function, because they do not need to be looked up, but it is
20336 simpler to check for this special case here, rather than at the
20337 call-sites.
20339 In cases not explicitly covered above, this function returns a
20340 DECL, OVERLOAD, or baselink representing the result of the lookup.
20341 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20342 is returned.
20344 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20345 (e.g., "struct") that was used. In that case bindings that do not
20346 refer to types are ignored.
20348 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20349 ignored.
20351 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20352 are ignored.
20354 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20355 types.
20357 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20358 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20359 NULL_TREE otherwise. */
20361 static tree
20362 cp_parser_lookup_name (cp_parser *parser, tree name,
20363 enum tag_types tag_type,
20364 bool is_template,
20365 bool is_namespace,
20366 bool check_dependency,
20367 tree *ambiguous_decls,
20368 location_t name_location)
20370 tree decl;
20371 tree object_type = parser->context->object_type;
20373 /* Assume that the lookup will be unambiguous. */
20374 if (ambiguous_decls)
20375 *ambiguous_decls = NULL_TREE;
20377 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20378 no longer valid. Note that if we are parsing tentatively, and
20379 the parse fails, OBJECT_TYPE will be automatically restored. */
20380 parser->context->object_type = NULL_TREE;
20382 if (name == error_mark_node)
20383 return error_mark_node;
20385 /* A template-id has already been resolved; there is no lookup to
20386 do. */
20387 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20388 return name;
20389 if (BASELINK_P (name))
20391 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20392 == TEMPLATE_ID_EXPR);
20393 return name;
20396 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20397 it should already have been checked to make sure that the name
20398 used matches the type being destroyed. */
20399 if (TREE_CODE (name) == BIT_NOT_EXPR)
20401 tree type;
20403 /* Figure out to which type this destructor applies. */
20404 if (parser->scope)
20405 type = parser->scope;
20406 else if (object_type)
20407 type = object_type;
20408 else
20409 type = current_class_type;
20410 /* If that's not a class type, there is no destructor. */
20411 if (!type || !CLASS_TYPE_P (type))
20412 return error_mark_node;
20413 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20414 lazily_declare_fn (sfk_destructor, type);
20415 if (!CLASSTYPE_DESTRUCTORS (type))
20416 return error_mark_node;
20417 /* If it was a class type, return the destructor. */
20418 return CLASSTYPE_DESTRUCTORS (type);
20421 /* By this point, the NAME should be an ordinary identifier. If
20422 the id-expression was a qualified name, the qualifying scope is
20423 stored in PARSER->SCOPE at this point. */
20424 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20426 /* Perform the lookup. */
20427 if (parser->scope)
20429 bool dependent_p;
20431 if (parser->scope == error_mark_node)
20432 return error_mark_node;
20434 /* If the SCOPE is dependent, the lookup must be deferred until
20435 the template is instantiated -- unless we are explicitly
20436 looking up names in uninstantiated templates. Even then, we
20437 cannot look up the name if the scope is not a class type; it
20438 might, for example, be a template type parameter. */
20439 dependent_p = (TYPE_P (parser->scope)
20440 && dependent_scope_p (parser->scope));
20441 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20442 && dependent_p)
20443 /* Defer lookup. */
20444 decl = error_mark_node;
20445 else
20447 tree pushed_scope = NULL_TREE;
20449 /* If PARSER->SCOPE is a dependent type, then it must be a
20450 class type, and we must not be checking dependencies;
20451 otherwise, we would have processed this lookup above. So
20452 that PARSER->SCOPE is not considered a dependent base by
20453 lookup_member, we must enter the scope here. */
20454 if (dependent_p)
20455 pushed_scope = push_scope (parser->scope);
20457 /* If the PARSER->SCOPE is a template specialization, it
20458 may be instantiated during name lookup. In that case,
20459 errors may be issued. Even if we rollback the current
20460 tentative parse, those errors are valid. */
20461 decl = lookup_qualified_name (parser->scope, name,
20462 tag_type != none_type,
20463 /*complain=*/true);
20465 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20466 lookup result and the nested-name-specifier nominates a class C:
20467 * if the name specified after the nested-name-specifier, when
20468 looked up in C, is the injected-class-name of C (Clause 9), or
20469 * if the name specified after the nested-name-specifier is the
20470 same as the identifier or the simple-template-id's template-
20471 name in the last component of the nested-name-specifier,
20472 the name is instead considered to name the constructor of
20473 class C. [ Note: for example, the constructor is not an
20474 acceptable lookup result in an elaborated-type-specifier so
20475 the constructor would not be used in place of the
20476 injected-class-name. --end note ] Such a constructor name
20477 shall be used only in the declarator-id of a declaration that
20478 names a constructor or in a using-declaration. */
20479 if (tag_type == none_type
20480 && DECL_SELF_REFERENCE_P (decl)
20481 && same_type_p (DECL_CONTEXT (decl), parser->scope))
20482 decl = lookup_qualified_name (parser->scope, ctor_identifier,
20483 tag_type != none_type,
20484 /*complain=*/true);
20486 /* If we have a single function from a using decl, pull it out. */
20487 if (TREE_CODE (decl) == OVERLOAD
20488 && !really_overloaded_fn (decl))
20489 decl = OVL_FUNCTION (decl);
20491 if (pushed_scope)
20492 pop_scope (pushed_scope);
20495 /* If the scope is a dependent type and either we deferred lookup or
20496 we did lookup but didn't find the name, rememeber the name. */
20497 if (decl == error_mark_node && TYPE_P (parser->scope)
20498 && dependent_type_p (parser->scope))
20500 if (tag_type)
20502 tree type;
20504 /* The resolution to Core Issue 180 says that `struct
20505 A::B' should be considered a type-name, even if `A'
20506 is dependent. */
20507 type = make_typename_type (parser->scope, name, tag_type,
20508 /*complain=*/tf_error);
20509 decl = TYPE_NAME (type);
20511 else if (is_template
20512 && (cp_parser_next_token_ends_template_argument_p (parser)
20513 || cp_lexer_next_token_is (parser->lexer,
20514 CPP_CLOSE_PAREN)))
20515 decl = make_unbound_class_template (parser->scope,
20516 name, NULL_TREE,
20517 /*complain=*/tf_error);
20518 else
20519 decl = build_qualified_name (/*type=*/NULL_TREE,
20520 parser->scope, name,
20521 is_template);
20523 parser->qualifying_scope = parser->scope;
20524 parser->object_scope = NULL_TREE;
20526 else if (object_type)
20528 tree object_decl = NULL_TREE;
20529 /* Look up the name in the scope of the OBJECT_TYPE, unless the
20530 OBJECT_TYPE is not a class. */
20531 if (CLASS_TYPE_P (object_type))
20532 /* If the OBJECT_TYPE is a template specialization, it may
20533 be instantiated during name lookup. In that case, errors
20534 may be issued. Even if we rollback the current tentative
20535 parse, those errors are valid. */
20536 object_decl = lookup_member (object_type,
20537 name,
20538 /*protect=*/0,
20539 tag_type != none_type,
20540 tf_warning_or_error);
20541 /* Look it up in the enclosing context, too. */
20542 decl = lookup_name_real (name, tag_type != none_type,
20543 /*nonclass=*/0,
20544 /*block_p=*/true, is_namespace, 0);
20545 parser->object_scope = object_type;
20546 parser->qualifying_scope = NULL_TREE;
20547 if (object_decl)
20548 decl = object_decl;
20550 else
20552 decl = lookup_name_real (name, tag_type != none_type,
20553 /*nonclass=*/0,
20554 /*block_p=*/true, is_namespace, 0);
20555 parser->qualifying_scope = NULL_TREE;
20556 parser->object_scope = NULL_TREE;
20559 /* If the lookup failed, let our caller know. */
20560 if (!decl || decl == error_mark_node)
20561 return error_mark_node;
20563 /* Pull out the template from an injected-class-name (or multiple). */
20564 if (is_template)
20565 decl = maybe_get_template_decl_from_type_decl (decl);
20567 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
20568 if (TREE_CODE (decl) == TREE_LIST)
20570 if (ambiguous_decls)
20571 *ambiguous_decls = decl;
20572 /* The error message we have to print is too complicated for
20573 cp_parser_error, so we incorporate its actions directly. */
20574 if (!cp_parser_simulate_error (parser))
20576 error_at (name_location, "reference to %qD is ambiguous",
20577 name);
20578 print_candidates (decl);
20580 return error_mark_node;
20583 gcc_assert (DECL_P (decl)
20584 || TREE_CODE (decl) == OVERLOAD
20585 || TREE_CODE (decl) == SCOPE_REF
20586 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20587 || BASELINK_P (decl));
20589 /* If we have resolved the name of a member declaration, check to
20590 see if the declaration is accessible. When the name resolves to
20591 set of overloaded functions, accessibility is checked when
20592 overload resolution is done.
20594 During an explicit instantiation, access is not checked at all,
20595 as per [temp.explicit]. */
20596 if (DECL_P (decl))
20597 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20599 maybe_record_typedef_use (decl);
20601 return decl;
20604 /* Like cp_parser_lookup_name, but for use in the typical case where
20605 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20606 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
20608 static tree
20609 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20611 return cp_parser_lookup_name (parser, name,
20612 none_type,
20613 /*is_template=*/false,
20614 /*is_namespace=*/false,
20615 /*check_dependency=*/true,
20616 /*ambiguous_decls=*/NULL,
20617 location);
20620 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20621 the current context, return the TYPE_DECL. If TAG_NAME_P is
20622 true, the DECL indicates the class being defined in a class-head,
20623 or declared in an elaborated-type-specifier.
20625 Otherwise, return DECL. */
20627 static tree
20628 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20630 /* If the TEMPLATE_DECL is being declared as part of a class-head,
20631 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20633 struct A {
20634 template <typename T> struct B;
20637 template <typename T> struct A::B {};
20639 Similarly, in an elaborated-type-specifier:
20641 namespace N { struct X{}; }
20643 struct A {
20644 template <typename T> friend struct N::X;
20647 However, if the DECL refers to a class type, and we are in
20648 the scope of the class, then the name lookup automatically
20649 finds the TYPE_DECL created by build_self_reference rather
20650 than a TEMPLATE_DECL. For example, in:
20652 template <class T> struct S {
20653 S s;
20656 there is no need to handle such case. */
20658 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20659 return DECL_TEMPLATE_RESULT (decl);
20661 return decl;
20664 /* If too many, or too few, template-parameter lists apply to the
20665 declarator, issue an error message. Returns TRUE if all went well,
20666 and FALSE otherwise. */
20668 static bool
20669 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20670 cp_declarator *declarator,
20671 location_t declarator_location)
20673 unsigned num_templates;
20675 /* We haven't seen any classes that involve template parameters yet. */
20676 num_templates = 0;
20678 switch (declarator->kind)
20680 case cdk_id:
20681 if (declarator->u.id.qualifying_scope)
20683 tree scope;
20685 scope = declarator->u.id.qualifying_scope;
20687 while (scope && CLASS_TYPE_P (scope))
20689 /* You're supposed to have one `template <...>'
20690 for every template class, but you don't need one
20691 for a full specialization. For example:
20693 template <class T> struct S{};
20694 template <> struct S<int> { void f(); };
20695 void S<int>::f () {}
20697 is correct; there shouldn't be a `template <>' for
20698 the definition of `S<int>::f'. */
20699 if (!CLASSTYPE_TEMPLATE_INFO (scope))
20700 /* If SCOPE does not have template information of any
20701 kind, then it is not a template, nor is it nested
20702 within a template. */
20703 break;
20704 if (explicit_class_specialization_p (scope))
20705 break;
20706 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20707 ++num_templates;
20709 scope = TYPE_CONTEXT (scope);
20712 else if (TREE_CODE (declarator->u.id.unqualified_name)
20713 == TEMPLATE_ID_EXPR)
20714 /* If the DECLARATOR has the form `X<y>' then it uses one
20715 additional level of template parameters. */
20716 ++num_templates;
20718 return cp_parser_check_template_parameters
20719 (parser, num_templates, declarator_location, declarator);
20722 case cdk_function:
20723 case cdk_array:
20724 case cdk_pointer:
20725 case cdk_reference:
20726 case cdk_ptrmem:
20727 return (cp_parser_check_declarator_template_parameters
20728 (parser, declarator->declarator, declarator_location));
20730 case cdk_error:
20731 return true;
20733 default:
20734 gcc_unreachable ();
20736 return false;
20739 /* NUM_TEMPLATES were used in the current declaration. If that is
20740 invalid, return FALSE and issue an error messages. Otherwise,
20741 return TRUE. If DECLARATOR is non-NULL, then we are checking a
20742 declarator and we can print more accurate diagnostics. */
20744 static bool
20745 cp_parser_check_template_parameters (cp_parser* parser,
20746 unsigned num_templates,
20747 location_t location,
20748 cp_declarator *declarator)
20750 /* If there are the same number of template classes and parameter
20751 lists, that's OK. */
20752 if (parser->num_template_parameter_lists == num_templates)
20753 return true;
20754 /* If there are more, but only one more, then we are referring to a
20755 member template. That's OK too. */
20756 if (parser->num_template_parameter_lists == num_templates + 1)
20757 return true;
20758 /* If there are more template classes than parameter lists, we have
20759 something like:
20761 template <class T> void S<T>::R<T>::f (); */
20762 if (parser->num_template_parameter_lists < num_templates)
20764 if (declarator && !current_function_decl)
20765 error_at (location, "specializing member %<%T::%E%> "
20766 "requires %<template<>%> syntax",
20767 declarator->u.id.qualifying_scope,
20768 declarator->u.id.unqualified_name);
20769 else if (declarator)
20770 error_at (location, "invalid declaration of %<%T::%E%>",
20771 declarator->u.id.qualifying_scope,
20772 declarator->u.id.unqualified_name);
20773 else
20774 error_at (location, "too few template-parameter-lists");
20775 return false;
20777 /* Otherwise, there are too many template parameter lists. We have
20778 something like:
20780 template <class T> template <class U> void S::f(); */
20781 error_at (location, "too many template-parameter-lists");
20782 return false;
20785 /* Parse an optional `::' token indicating that the following name is
20786 from the global namespace. If so, PARSER->SCOPE is set to the
20787 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20788 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20789 Returns the new value of PARSER->SCOPE, if the `::' token is
20790 present, and NULL_TREE otherwise. */
20792 static tree
20793 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20795 cp_token *token;
20797 /* Peek at the next token. */
20798 token = cp_lexer_peek_token (parser->lexer);
20799 /* If we're looking at a `::' token then we're starting from the
20800 global namespace, not our current location. */
20801 if (token->type == CPP_SCOPE)
20803 /* Consume the `::' token. */
20804 cp_lexer_consume_token (parser->lexer);
20805 /* Set the SCOPE so that we know where to start the lookup. */
20806 parser->scope = global_namespace;
20807 parser->qualifying_scope = global_namespace;
20808 parser->object_scope = NULL_TREE;
20810 return parser->scope;
20812 else if (!current_scope_valid_p)
20814 parser->scope = NULL_TREE;
20815 parser->qualifying_scope = NULL_TREE;
20816 parser->object_scope = NULL_TREE;
20819 return NULL_TREE;
20822 /* Returns TRUE if the upcoming token sequence is the start of a
20823 constructor declarator. If FRIEND_P is true, the declarator is
20824 preceded by the `friend' specifier. */
20826 static bool
20827 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20829 bool constructor_p;
20830 tree nested_name_specifier;
20831 cp_token *next_token;
20833 /* The common case is that this is not a constructor declarator, so
20834 try to avoid doing lots of work if at all possible. It's not
20835 valid declare a constructor at function scope. */
20836 if (parser->in_function_body)
20837 return false;
20838 /* And only certain tokens can begin a constructor declarator. */
20839 next_token = cp_lexer_peek_token (parser->lexer);
20840 if (next_token->type != CPP_NAME
20841 && next_token->type != CPP_SCOPE
20842 && next_token->type != CPP_NESTED_NAME_SPECIFIER
20843 && next_token->type != CPP_TEMPLATE_ID)
20844 return false;
20846 /* Parse tentatively; we are going to roll back all of the tokens
20847 consumed here. */
20848 cp_parser_parse_tentatively (parser);
20849 /* Assume that we are looking at a constructor declarator. */
20850 constructor_p = true;
20852 /* Look for the optional `::' operator. */
20853 cp_parser_global_scope_opt (parser,
20854 /*current_scope_valid_p=*/false);
20855 /* Look for the nested-name-specifier. */
20856 nested_name_specifier
20857 = (cp_parser_nested_name_specifier_opt (parser,
20858 /*typename_keyword_p=*/false,
20859 /*check_dependency_p=*/false,
20860 /*type_p=*/false,
20861 /*is_declaration=*/false));
20862 /* Outside of a class-specifier, there must be a
20863 nested-name-specifier. */
20864 if (!nested_name_specifier &&
20865 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20866 || friend_p))
20867 constructor_p = false;
20868 else if (nested_name_specifier == error_mark_node)
20869 constructor_p = false;
20871 /* If we have a class scope, this is easy; DR 147 says that S::S always
20872 names the constructor, and no other qualified name could. */
20873 if (constructor_p && nested_name_specifier
20874 && CLASS_TYPE_P (nested_name_specifier))
20876 tree id = cp_parser_unqualified_id (parser,
20877 /*template_keyword_p=*/false,
20878 /*check_dependency_p=*/false,
20879 /*declarator_p=*/true,
20880 /*optional_p=*/false);
20881 if (is_overloaded_fn (id))
20882 id = DECL_NAME (get_first_fn (id));
20883 if (!constructor_name_p (id, nested_name_specifier))
20884 constructor_p = false;
20886 /* If we still think that this might be a constructor-declarator,
20887 look for a class-name. */
20888 else if (constructor_p)
20890 /* If we have:
20892 template <typename T> struct S {
20893 S();
20896 we must recognize that the nested `S' names a class. */
20897 tree type_decl;
20898 type_decl = cp_parser_class_name (parser,
20899 /*typename_keyword_p=*/false,
20900 /*template_keyword_p=*/false,
20901 none_type,
20902 /*check_dependency_p=*/false,
20903 /*class_head_p=*/false,
20904 /*is_declaration=*/false);
20905 /* If there was no class-name, then this is not a constructor. */
20906 constructor_p = !cp_parser_error_occurred (parser);
20908 /* If we're still considering a constructor, we have to see a `(',
20909 to begin the parameter-declaration-clause, followed by either a
20910 `)', an `...', or a decl-specifier. We need to check for a
20911 type-specifier to avoid being fooled into thinking that:
20913 S (f) (int);
20915 is a constructor. (It is actually a function named `f' that
20916 takes one parameter (of type `int') and returns a value of type
20917 `S'. */
20918 if (constructor_p
20919 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20920 constructor_p = false;
20922 if (constructor_p
20923 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20924 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20925 /* A parameter declaration begins with a decl-specifier,
20926 which is either the "attribute" keyword, a storage class
20927 specifier, or (usually) a type-specifier. */
20928 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20930 tree type;
20931 tree pushed_scope = NULL_TREE;
20932 unsigned saved_num_template_parameter_lists;
20934 /* Names appearing in the type-specifier should be looked up
20935 in the scope of the class. */
20936 if (current_class_type)
20937 type = NULL_TREE;
20938 else
20940 type = TREE_TYPE (type_decl);
20941 if (TREE_CODE (type) == TYPENAME_TYPE)
20943 type = resolve_typename_type (type,
20944 /*only_current_p=*/false);
20945 if (TREE_CODE (type) == TYPENAME_TYPE)
20947 cp_parser_abort_tentative_parse (parser);
20948 return false;
20951 pushed_scope = push_scope (type);
20954 /* Inside the constructor parameter list, surrounding
20955 template-parameter-lists do not apply. */
20956 saved_num_template_parameter_lists
20957 = parser->num_template_parameter_lists;
20958 parser->num_template_parameter_lists = 0;
20960 /* Look for the type-specifier. */
20961 cp_parser_type_specifier (parser,
20962 CP_PARSER_FLAGS_NONE,
20963 /*decl_specs=*/NULL,
20964 /*is_declarator=*/true,
20965 /*declares_class_or_enum=*/NULL,
20966 /*is_cv_qualifier=*/NULL);
20968 parser->num_template_parameter_lists
20969 = saved_num_template_parameter_lists;
20971 /* Leave the scope of the class. */
20972 if (pushed_scope)
20973 pop_scope (pushed_scope);
20975 constructor_p = !cp_parser_error_occurred (parser);
20979 /* We did not really want to consume any tokens. */
20980 cp_parser_abort_tentative_parse (parser);
20982 return constructor_p;
20985 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20986 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
20987 they must be performed once we are in the scope of the function.
20989 Returns the function defined. */
20991 static tree
20992 cp_parser_function_definition_from_specifiers_and_declarator
20993 (cp_parser* parser,
20994 cp_decl_specifier_seq *decl_specifiers,
20995 tree attributes,
20996 const cp_declarator *declarator)
20998 tree fn;
20999 bool success_p;
21001 /* Begin the function-definition. */
21002 success_p = start_function (decl_specifiers, declarator, attributes);
21004 /* The things we're about to see are not directly qualified by any
21005 template headers we've seen thus far. */
21006 reset_specialization ();
21008 /* If there were names looked up in the decl-specifier-seq that we
21009 did not check, check them now. We must wait until we are in the
21010 scope of the function to perform the checks, since the function
21011 might be a friend. */
21012 perform_deferred_access_checks (tf_warning_or_error);
21014 if (!success_p)
21016 /* Skip the entire function. */
21017 cp_parser_skip_to_end_of_block_or_statement (parser);
21018 fn = error_mark_node;
21020 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
21022 /* Seen already, skip it. An error message has already been output. */
21023 cp_parser_skip_to_end_of_block_or_statement (parser);
21024 fn = current_function_decl;
21025 current_function_decl = NULL_TREE;
21026 /* If this is a function from a class, pop the nested class. */
21027 if (current_class_name)
21028 pop_nested_class ();
21030 else
21032 timevar_id_t tv;
21033 if (DECL_DECLARED_INLINE_P (current_function_decl))
21034 tv = TV_PARSE_INLINE;
21035 else
21036 tv = TV_PARSE_FUNC;
21037 timevar_push (tv);
21038 fn = cp_parser_function_definition_after_declarator (parser,
21039 /*inline_p=*/false);
21040 timevar_pop (tv);
21043 return fn;
21046 /* Parse the part of a function-definition that follows the
21047 declarator. INLINE_P is TRUE iff this function is an inline
21048 function defined within a class-specifier.
21050 Returns the function defined. */
21052 static tree
21053 cp_parser_function_definition_after_declarator (cp_parser* parser,
21054 bool inline_p)
21056 tree fn;
21057 bool ctor_initializer_p = false;
21058 bool saved_in_unbraced_linkage_specification_p;
21059 bool saved_in_function_body;
21060 unsigned saved_num_template_parameter_lists;
21061 cp_token *token;
21063 saved_in_function_body = parser->in_function_body;
21064 parser->in_function_body = true;
21065 /* If the next token is `return', then the code may be trying to
21066 make use of the "named return value" extension that G++ used to
21067 support. */
21068 token = cp_lexer_peek_token (parser->lexer);
21069 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21071 /* Consume the `return' keyword. */
21072 cp_lexer_consume_token (parser->lexer);
21073 /* Look for the identifier that indicates what value is to be
21074 returned. */
21075 cp_parser_identifier (parser);
21076 /* Issue an error message. */
21077 error_at (token->location,
21078 "named return values are no longer supported");
21079 /* Skip tokens until we reach the start of the function body. */
21080 while (true)
21082 cp_token *token = cp_lexer_peek_token (parser->lexer);
21083 if (token->type == CPP_OPEN_BRACE
21084 || token->type == CPP_EOF
21085 || token->type == CPP_PRAGMA_EOL)
21086 break;
21087 cp_lexer_consume_token (parser->lexer);
21090 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21091 anything declared inside `f'. */
21092 saved_in_unbraced_linkage_specification_p
21093 = parser->in_unbraced_linkage_specification_p;
21094 parser->in_unbraced_linkage_specification_p = false;
21095 /* Inside the function, surrounding template-parameter-lists do not
21096 apply. */
21097 saved_num_template_parameter_lists
21098 = parser->num_template_parameter_lists;
21099 parser->num_template_parameter_lists = 0;
21101 start_lambda_scope (current_function_decl);
21103 /* If the next token is `try', `__transaction_atomic', or
21104 `__transaction_relaxed`, then we are looking at either function-try-block
21105 or function-transaction-block. Note that all of these include the
21106 function-body. */
21107 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21108 ctor_initializer_p = cp_parser_function_transaction (parser,
21109 RID_TRANSACTION_ATOMIC);
21110 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21111 RID_TRANSACTION_RELAXED))
21112 ctor_initializer_p = cp_parser_function_transaction (parser,
21113 RID_TRANSACTION_RELAXED);
21114 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21115 ctor_initializer_p = cp_parser_function_try_block (parser);
21116 else
21117 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21118 (parser, /*in_function_try_block=*/false);
21120 finish_lambda_scope ();
21122 /* Finish the function. */
21123 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21124 (inline_p ? 2 : 0));
21125 /* Generate code for it, if necessary. */
21126 expand_or_defer_fn (fn);
21127 /* Restore the saved values. */
21128 parser->in_unbraced_linkage_specification_p
21129 = saved_in_unbraced_linkage_specification_p;
21130 parser->num_template_parameter_lists
21131 = saved_num_template_parameter_lists;
21132 parser->in_function_body = saved_in_function_body;
21134 return fn;
21137 /* Parse a template-declaration, assuming that the `export' (and
21138 `extern') keywords, if present, has already been scanned. MEMBER_P
21139 is as for cp_parser_template_declaration. */
21141 static void
21142 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21144 tree decl = NULL_TREE;
21145 VEC (deferred_access_check,gc) *checks;
21146 tree parameter_list;
21147 bool friend_p = false;
21148 bool need_lang_pop;
21149 cp_token *token;
21151 /* Look for the `template' keyword. */
21152 token = cp_lexer_peek_token (parser->lexer);
21153 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21154 return;
21156 /* And the `<'. */
21157 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21158 return;
21159 if (at_class_scope_p () && current_function_decl)
21161 /* 14.5.2.2 [temp.mem]
21163 A local class shall not have member templates. */
21164 error_at (token->location,
21165 "invalid declaration of member template in local class");
21166 cp_parser_skip_to_end_of_block_or_statement (parser);
21167 return;
21169 /* [temp]
21171 A template ... shall not have C linkage. */
21172 if (current_lang_name == lang_name_c)
21174 error_at (token->location, "template with C linkage");
21175 /* Give it C++ linkage to avoid confusing other parts of the
21176 front end. */
21177 push_lang_context (lang_name_cplusplus);
21178 need_lang_pop = true;
21180 else
21181 need_lang_pop = false;
21183 /* We cannot perform access checks on the template parameter
21184 declarations until we know what is being declared, just as we
21185 cannot check the decl-specifier list. */
21186 push_deferring_access_checks (dk_deferred);
21188 /* If the next token is `>', then we have an invalid
21189 specialization. Rather than complain about an invalid template
21190 parameter, issue an error message here. */
21191 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21193 cp_parser_error (parser, "invalid explicit specialization");
21194 begin_specialization ();
21195 parameter_list = NULL_TREE;
21197 else
21199 /* Parse the template parameters. */
21200 parameter_list = cp_parser_template_parameter_list (parser);
21203 /* Get the deferred access checks from the parameter list. These
21204 will be checked once we know what is being declared, as for a
21205 member template the checks must be performed in the scope of the
21206 class containing the member. */
21207 checks = get_deferred_access_checks ();
21209 /* Look for the `>'. */
21210 cp_parser_skip_to_end_of_template_parameter_list (parser);
21211 /* We just processed one more parameter list. */
21212 ++parser->num_template_parameter_lists;
21213 /* If the next token is `template', there are more template
21214 parameters. */
21215 if (cp_lexer_next_token_is_keyword (parser->lexer,
21216 RID_TEMPLATE))
21217 cp_parser_template_declaration_after_export (parser, member_p);
21218 else if (cxx_dialect >= cxx0x
21219 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21220 decl = cp_parser_alias_declaration (parser);
21221 else
21223 /* There are no access checks when parsing a template, as we do not
21224 know if a specialization will be a friend. */
21225 push_deferring_access_checks (dk_no_check);
21226 token = cp_lexer_peek_token (parser->lexer);
21227 decl = cp_parser_single_declaration (parser,
21228 checks,
21229 member_p,
21230 /*explicit_specialization_p=*/false,
21231 &friend_p);
21232 pop_deferring_access_checks ();
21234 /* If this is a member template declaration, let the front
21235 end know. */
21236 if (member_p && !friend_p && decl)
21238 if (TREE_CODE (decl) == TYPE_DECL)
21239 cp_parser_check_access_in_redeclaration (decl, token->location);
21241 decl = finish_member_template_decl (decl);
21243 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21244 make_friend_class (current_class_type, TREE_TYPE (decl),
21245 /*complain=*/true);
21247 /* We are done with the current parameter list. */
21248 --parser->num_template_parameter_lists;
21250 pop_deferring_access_checks ();
21252 /* Finish up. */
21253 finish_template_decl (parameter_list);
21255 /* Check the template arguments for a literal operator template. */
21256 if (decl
21257 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21258 && UDLIT_OPER_P (DECL_NAME (decl)))
21260 bool ok = true;
21261 if (parameter_list == NULL_TREE)
21262 ok = false;
21263 else
21265 int num_parms = TREE_VEC_LENGTH (parameter_list);
21266 if (num_parms != 1)
21267 ok = false;
21268 else
21270 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21271 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21272 if (TREE_TYPE (parm) != char_type_node
21273 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21274 ok = false;
21277 if (!ok)
21278 error ("literal operator template %qD has invalid parameter list."
21279 " Expected non-type template argument pack <char...>",
21280 decl);
21282 /* Register member declarations. */
21283 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21284 finish_member_declaration (decl);
21285 /* For the erroneous case of a template with C linkage, we pushed an
21286 implicit C++ linkage scope; exit that scope now. */
21287 if (need_lang_pop)
21288 pop_lang_context ();
21289 /* If DECL is a function template, we must return to parse it later.
21290 (Even though there is no definition, there might be default
21291 arguments that need handling.) */
21292 if (member_p && decl
21293 && (TREE_CODE (decl) == FUNCTION_DECL
21294 || DECL_FUNCTION_TEMPLATE_P (decl)))
21295 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21298 /* Perform the deferred access checks from a template-parameter-list.
21299 CHECKS is a TREE_LIST of access checks, as returned by
21300 get_deferred_access_checks. */
21302 static void
21303 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21305 ++processing_template_parmlist;
21306 perform_access_checks (checks, tf_warning_or_error);
21307 --processing_template_parmlist;
21310 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21311 `function-definition' sequence. MEMBER_P is true, this declaration
21312 appears in a class scope.
21314 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21315 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21317 static tree
21318 cp_parser_single_declaration (cp_parser* parser,
21319 VEC (deferred_access_check,gc)* checks,
21320 bool member_p,
21321 bool explicit_specialization_p,
21322 bool* friend_p)
21324 int declares_class_or_enum;
21325 tree decl = NULL_TREE;
21326 cp_decl_specifier_seq decl_specifiers;
21327 bool function_definition_p = false;
21328 cp_token *decl_spec_token_start;
21330 /* This function is only used when processing a template
21331 declaration. */
21332 gcc_assert (innermost_scope_kind () == sk_template_parms
21333 || innermost_scope_kind () == sk_template_spec);
21335 /* Defer access checks until we know what is being declared. */
21336 push_deferring_access_checks (dk_deferred);
21338 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21339 alternative. */
21340 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21341 cp_parser_decl_specifier_seq (parser,
21342 CP_PARSER_FLAGS_OPTIONAL,
21343 &decl_specifiers,
21344 &declares_class_or_enum);
21345 if (friend_p)
21346 *friend_p = cp_parser_friend_p (&decl_specifiers);
21348 /* There are no template typedefs. */
21349 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
21351 error_at (decl_spec_token_start->location,
21352 "template declaration of %<typedef%>");
21353 decl = error_mark_node;
21356 /* Gather up the access checks that occurred the
21357 decl-specifier-seq. */
21358 stop_deferring_access_checks ();
21360 /* Check for the declaration of a template class. */
21361 if (declares_class_or_enum)
21363 if (cp_parser_declares_only_class_p (parser))
21365 decl = shadow_tag (&decl_specifiers);
21367 /* In this case:
21369 struct C {
21370 friend template <typename T> struct A<T>::B;
21373 A<T>::B will be represented by a TYPENAME_TYPE, and
21374 therefore not recognized by shadow_tag. */
21375 if (friend_p && *friend_p
21376 && !decl
21377 && decl_specifiers.type
21378 && TYPE_P (decl_specifiers.type))
21379 decl = decl_specifiers.type;
21381 if (decl && decl != error_mark_node)
21382 decl = TYPE_NAME (decl);
21383 else
21384 decl = error_mark_node;
21386 /* Perform access checks for template parameters. */
21387 cp_parser_perform_template_parameter_access_checks (checks);
21391 /* Complain about missing 'typename' or other invalid type names. */
21392 if (!decl_specifiers.any_type_specifiers_p
21393 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21395 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21396 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21397 the rest of this declaration. */
21398 decl = error_mark_node;
21399 goto out;
21402 /* If it's not a template class, try for a template function. If
21403 the next token is a `;', then this declaration does not declare
21404 anything. But, if there were errors in the decl-specifiers, then
21405 the error might well have come from an attempted class-specifier.
21406 In that case, there's no need to warn about a missing declarator. */
21407 if (!decl
21408 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21409 || decl_specifiers.type != error_mark_node))
21411 decl = cp_parser_init_declarator (parser,
21412 &decl_specifiers,
21413 checks,
21414 /*function_definition_allowed_p=*/true,
21415 member_p,
21416 declares_class_or_enum,
21417 &function_definition_p,
21418 NULL);
21420 /* 7.1.1-1 [dcl.stc]
21422 A storage-class-specifier shall not be specified in an explicit
21423 specialization... */
21424 if (decl
21425 && explicit_specialization_p
21426 && decl_specifiers.storage_class != sc_none)
21428 error_at (decl_spec_token_start->location,
21429 "explicit template specialization cannot have a storage class");
21430 decl = error_mark_node;
21434 /* Look for a trailing `;' after the declaration. */
21435 if (!function_definition_p
21436 && (decl == error_mark_node
21437 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21438 cp_parser_skip_to_end_of_block_or_statement (parser);
21440 out:
21441 pop_deferring_access_checks ();
21443 /* Clear any current qualification; whatever comes next is the start
21444 of something new. */
21445 parser->scope = NULL_TREE;
21446 parser->qualifying_scope = NULL_TREE;
21447 parser->object_scope = NULL_TREE;
21449 return decl;
21452 /* Parse a cast-expression that is not the operand of a unary "&". */
21454 static tree
21455 cp_parser_simple_cast_expression (cp_parser *parser)
21457 return cp_parser_cast_expression (parser, /*address_p=*/false,
21458 /*cast_p=*/false, NULL);
21461 /* Parse a functional cast to TYPE. Returns an expression
21462 representing the cast. */
21464 static tree
21465 cp_parser_functional_cast (cp_parser* parser, tree type)
21467 VEC(tree,gc) *vec;
21468 tree expression_list;
21469 tree cast;
21470 bool nonconst_p;
21472 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21474 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21475 expression_list = cp_parser_braced_list (parser, &nonconst_p);
21476 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21477 if (TREE_CODE (type) == TYPE_DECL)
21478 type = TREE_TYPE (type);
21479 return finish_compound_literal (type, expression_list,
21480 tf_warning_or_error);
21484 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21485 /*cast_p=*/true,
21486 /*allow_expansion_p=*/true,
21487 /*non_constant_p=*/NULL);
21488 if (vec == NULL)
21489 expression_list = error_mark_node;
21490 else
21492 expression_list = build_tree_list_vec (vec);
21493 release_tree_vector (vec);
21496 cast = build_functional_cast (type, expression_list,
21497 tf_warning_or_error);
21498 /* [expr.const]/1: In an integral constant expression "only type
21499 conversions to integral or enumeration type can be used". */
21500 if (TREE_CODE (type) == TYPE_DECL)
21501 type = TREE_TYPE (type);
21502 if (cast != error_mark_node
21503 && !cast_valid_in_integral_constant_expression_p (type)
21504 && cp_parser_non_integral_constant_expression (parser,
21505 NIC_CONSTRUCTOR))
21506 return error_mark_node;
21507 return cast;
21510 /* Save the tokens that make up the body of a member function defined
21511 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
21512 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
21513 specifiers applied to the declaration. Returns the FUNCTION_DECL
21514 for the member function. */
21516 static tree
21517 cp_parser_save_member_function_body (cp_parser* parser,
21518 cp_decl_specifier_seq *decl_specifiers,
21519 cp_declarator *declarator,
21520 tree attributes)
21522 cp_token *first;
21523 cp_token *last;
21524 tree fn;
21526 /* Create the FUNCTION_DECL. */
21527 fn = grokmethod (decl_specifiers, declarator, attributes);
21528 /* If something went badly wrong, bail out now. */
21529 if (fn == error_mark_node)
21531 /* If there's a function-body, skip it. */
21532 if (cp_parser_token_starts_function_definition_p
21533 (cp_lexer_peek_token (parser->lexer)))
21534 cp_parser_skip_to_end_of_block_or_statement (parser);
21535 return error_mark_node;
21538 /* Remember it, if there default args to post process. */
21539 cp_parser_save_default_args (parser, fn);
21541 /* Save away the tokens that make up the body of the
21542 function. */
21543 first = parser->lexer->next_token;
21544 /* We can have braced-init-list mem-initializers before the fn body. */
21545 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21547 cp_lexer_consume_token (parser->lexer);
21548 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21549 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21551 /* cache_group will stop after an un-nested { } pair, too. */
21552 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21553 break;
21555 /* variadic mem-inits have ... after the ')'. */
21556 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21557 cp_lexer_consume_token (parser->lexer);
21560 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21561 /* Handle function try blocks. */
21562 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21563 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21564 last = parser->lexer->next_token;
21566 /* Save away the inline definition; we will process it when the
21567 class is complete. */
21568 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21569 DECL_PENDING_INLINE_P (fn) = 1;
21571 /* We need to know that this was defined in the class, so that
21572 friend templates are handled correctly. */
21573 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21575 /* Add FN to the queue of functions to be parsed later. */
21576 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21578 return fn;
21581 /* Save the tokens that make up the in-class initializer for a non-static
21582 data member. Returns a DEFAULT_ARG. */
21584 static tree
21585 cp_parser_save_nsdmi (cp_parser* parser)
21587 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
21590 /* Parse a template-argument-list, as well as the trailing ">" (but
21591 not the opening "<"). See cp_parser_template_argument_list for the
21592 return value. */
21594 static tree
21595 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21597 tree arguments;
21598 tree saved_scope;
21599 tree saved_qualifying_scope;
21600 tree saved_object_scope;
21601 bool saved_greater_than_is_operator_p;
21602 int saved_unevaluated_operand;
21603 int saved_inhibit_evaluation_warnings;
21605 /* [temp.names]
21607 When parsing a template-id, the first non-nested `>' is taken as
21608 the end of the template-argument-list rather than a greater-than
21609 operator. */
21610 saved_greater_than_is_operator_p
21611 = parser->greater_than_is_operator_p;
21612 parser->greater_than_is_operator_p = false;
21613 /* Parsing the argument list may modify SCOPE, so we save it
21614 here. */
21615 saved_scope = parser->scope;
21616 saved_qualifying_scope = parser->qualifying_scope;
21617 saved_object_scope = parser->object_scope;
21618 /* We need to evaluate the template arguments, even though this
21619 template-id may be nested within a "sizeof". */
21620 saved_unevaluated_operand = cp_unevaluated_operand;
21621 cp_unevaluated_operand = 0;
21622 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21623 c_inhibit_evaluation_warnings = 0;
21624 /* Parse the template-argument-list itself. */
21625 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21626 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21627 arguments = NULL_TREE;
21628 else
21629 arguments = cp_parser_template_argument_list (parser);
21630 /* Look for the `>' that ends the template-argument-list. If we find
21631 a '>>' instead, it's probably just a typo. */
21632 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21634 if (cxx_dialect != cxx98)
21636 /* In C++0x, a `>>' in a template argument list or cast
21637 expression is considered to be two separate `>'
21638 tokens. So, change the current token to a `>', but don't
21639 consume it: it will be consumed later when the outer
21640 template argument list (or cast expression) is parsed.
21641 Note that this replacement of `>' for `>>' is necessary
21642 even if we are parsing tentatively: in the tentative
21643 case, after calling
21644 cp_parser_enclosed_template_argument_list we will always
21645 throw away all of the template arguments and the first
21646 closing `>', either because the template argument list
21647 was erroneous or because we are replacing those tokens
21648 with a CPP_TEMPLATE_ID token. The second `>' (which will
21649 not have been thrown away) is needed either to close an
21650 outer template argument list or to complete a new-style
21651 cast. */
21652 cp_token *token = cp_lexer_peek_token (parser->lexer);
21653 token->type = CPP_GREATER;
21655 else if (!saved_greater_than_is_operator_p)
21657 /* If we're in a nested template argument list, the '>>' has
21658 to be a typo for '> >'. We emit the error message, but we
21659 continue parsing and we push a '>' as next token, so that
21660 the argument list will be parsed correctly. Note that the
21661 global source location is still on the token before the
21662 '>>', so we need to say explicitly where we want it. */
21663 cp_token *token = cp_lexer_peek_token (parser->lexer);
21664 error_at (token->location, "%<>>%> should be %<> >%> "
21665 "within a nested template argument list");
21667 token->type = CPP_GREATER;
21669 else
21671 /* If this is not a nested template argument list, the '>>'
21672 is a typo for '>'. Emit an error message and continue.
21673 Same deal about the token location, but here we can get it
21674 right by consuming the '>>' before issuing the diagnostic. */
21675 cp_token *token = cp_lexer_consume_token (parser->lexer);
21676 error_at (token->location,
21677 "spurious %<>>%>, use %<>%> to terminate "
21678 "a template argument list");
21681 else
21682 cp_parser_skip_to_end_of_template_parameter_list (parser);
21683 /* The `>' token might be a greater-than operator again now. */
21684 parser->greater_than_is_operator_p
21685 = saved_greater_than_is_operator_p;
21686 /* Restore the SAVED_SCOPE. */
21687 parser->scope = saved_scope;
21688 parser->qualifying_scope = saved_qualifying_scope;
21689 parser->object_scope = saved_object_scope;
21690 cp_unevaluated_operand = saved_unevaluated_operand;
21691 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21693 return arguments;
21696 /* MEMBER_FUNCTION is a member function, or a friend. If default
21697 arguments, or the body of the function have not yet been parsed,
21698 parse them now. */
21700 static void
21701 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21703 timevar_push (TV_PARSE_INMETH);
21704 /* If this member is a template, get the underlying
21705 FUNCTION_DECL. */
21706 if (DECL_FUNCTION_TEMPLATE_P (member_function))
21707 member_function = DECL_TEMPLATE_RESULT (member_function);
21709 /* There should not be any class definitions in progress at this
21710 point; the bodies of members are only parsed outside of all class
21711 definitions. */
21712 gcc_assert (parser->num_classes_being_defined == 0);
21713 /* While we're parsing the member functions we might encounter more
21714 classes. We want to handle them right away, but we don't want
21715 them getting mixed up with functions that are currently in the
21716 queue. */
21717 push_unparsed_function_queues (parser);
21719 /* Make sure that any template parameters are in scope. */
21720 maybe_begin_member_template_processing (member_function);
21722 /* If the body of the function has not yet been parsed, parse it
21723 now. */
21724 if (DECL_PENDING_INLINE_P (member_function))
21726 tree function_scope;
21727 cp_token_cache *tokens;
21729 /* The function is no longer pending; we are processing it. */
21730 tokens = DECL_PENDING_INLINE_INFO (member_function);
21731 DECL_PENDING_INLINE_INFO (member_function) = NULL;
21732 DECL_PENDING_INLINE_P (member_function) = 0;
21734 /* If this is a local class, enter the scope of the containing
21735 function. */
21736 function_scope = current_function_decl;
21737 if (function_scope)
21738 push_function_context ();
21740 /* Push the body of the function onto the lexer stack. */
21741 cp_parser_push_lexer_for_tokens (parser, tokens);
21743 /* Let the front end know that we going to be defining this
21744 function. */
21745 start_preparsed_function (member_function, NULL_TREE,
21746 SF_PRE_PARSED | SF_INCLASS_INLINE);
21748 /* Don't do access checking if it is a templated function. */
21749 if (processing_template_decl)
21750 push_deferring_access_checks (dk_no_check);
21752 /* Now, parse the body of the function. */
21753 cp_parser_function_definition_after_declarator (parser,
21754 /*inline_p=*/true);
21756 if (processing_template_decl)
21757 pop_deferring_access_checks ();
21759 /* Leave the scope of the containing function. */
21760 if (function_scope)
21761 pop_function_context ();
21762 cp_parser_pop_lexer (parser);
21765 /* Remove any template parameters from the symbol table. */
21766 maybe_end_member_template_processing ();
21768 /* Restore the queue. */
21769 pop_unparsed_function_queues (parser);
21770 timevar_pop (TV_PARSE_INMETH);
21773 /* If DECL contains any default args, remember it on the unparsed
21774 functions queue. */
21776 static void
21777 cp_parser_save_default_args (cp_parser* parser, tree decl)
21779 tree probe;
21781 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21782 probe;
21783 probe = TREE_CHAIN (probe))
21784 if (TREE_PURPOSE (probe))
21786 cp_default_arg_entry *entry
21787 = VEC_safe_push (cp_default_arg_entry, gc,
21788 unparsed_funs_with_default_args, NULL);
21789 entry->class_type = current_class_type;
21790 entry->decl = decl;
21791 break;
21795 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21796 which is either a FIELD_DECL or PARM_DECL. Parse it and return
21797 the result. For a PARM_DECL, PARMTYPE is the corresponding type
21798 from the parameter-type-list. */
21800 static tree
21801 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21802 tree default_arg, tree parmtype)
21804 cp_token_cache *tokens;
21805 tree parsed_arg;
21806 bool dummy;
21808 if (default_arg == error_mark_node)
21809 return error_mark_node;
21811 /* Push the saved tokens for the default argument onto the parser's
21812 lexer stack. */
21813 tokens = DEFARG_TOKENS (default_arg);
21814 cp_parser_push_lexer_for_tokens (parser, tokens);
21816 start_lambda_scope (decl);
21818 /* Parse the default argument. */
21819 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21820 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21821 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21823 finish_lambda_scope ();
21825 if (parsed_arg == error_mark_node)
21826 cp_parser_skip_to_end_of_statement (parser);
21828 if (!processing_template_decl)
21830 /* In a non-template class, check conversions now. In a template,
21831 we'll wait and instantiate these as needed. */
21832 if (TREE_CODE (decl) == PARM_DECL)
21833 parsed_arg = check_default_argument (parmtype, parsed_arg);
21834 else
21836 int flags = LOOKUP_IMPLICIT;
21837 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21838 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21839 flags = LOOKUP_NORMAL;
21840 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21844 /* If the token stream has not been completely used up, then
21845 there was extra junk after the end of the default
21846 argument. */
21847 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21849 if (TREE_CODE (decl) == PARM_DECL)
21850 cp_parser_error (parser, "expected %<,%>");
21851 else
21852 cp_parser_error (parser, "expected %<;%>");
21855 /* Revert to the main lexer. */
21856 cp_parser_pop_lexer (parser);
21858 return parsed_arg;
21861 /* FIELD is a non-static data member with an initializer which we saved for
21862 later; parse it now. */
21864 static void
21865 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21867 tree def;
21869 push_unparsed_function_queues (parser);
21870 def = cp_parser_late_parse_one_default_arg (parser, field,
21871 DECL_INITIAL (field),
21872 NULL_TREE);
21873 pop_unparsed_function_queues (parser);
21875 DECL_INITIAL (field) = def;
21878 /* FN is a FUNCTION_DECL which may contains a parameter with an
21879 unparsed DEFAULT_ARG. Parse the default args now. This function
21880 assumes that the current scope is the scope in which the default
21881 argument should be processed. */
21883 static void
21884 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21886 bool saved_local_variables_forbidden_p;
21887 tree parm, parmdecl;
21889 /* While we're parsing the default args, we might (due to the
21890 statement expression extension) encounter more classes. We want
21891 to handle them right away, but we don't want them getting mixed
21892 up with default args that are currently in the queue. */
21893 push_unparsed_function_queues (parser);
21895 /* Local variable names (and the `this' keyword) may not appear
21896 in a default argument. */
21897 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21898 parser->local_variables_forbidden_p = true;
21900 push_defarg_context (fn);
21902 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21903 parmdecl = DECL_ARGUMENTS (fn);
21904 parm && parm != void_list_node;
21905 parm = TREE_CHAIN (parm),
21906 parmdecl = DECL_CHAIN (parmdecl))
21908 tree default_arg = TREE_PURPOSE (parm);
21909 tree parsed_arg;
21910 VEC(tree,gc) *insts;
21911 tree copy;
21912 unsigned ix;
21914 if (!default_arg)
21915 continue;
21917 if (TREE_CODE (default_arg) != DEFAULT_ARG)
21918 /* This can happen for a friend declaration for a function
21919 already declared with default arguments. */
21920 continue;
21922 parsed_arg
21923 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21924 default_arg,
21925 TREE_VALUE (parm));
21926 if (parsed_arg == error_mark_node)
21928 continue;
21931 TREE_PURPOSE (parm) = parsed_arg;
21933 /* Update any instantiations we've already created. */
21934 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21935 VEC_iterate (tree, insts, ix, copy); ix++)
21936 TREE_PURPOSE (copy) = parsed_arg;
21939 pop_defarg_context ();
21941 /* Make sure no default arg is missing. */
21942 check_default_args (fn);
21944 /* Restore the state of local_variables_forbidden_p. */
21945 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21947 /* Restore the queue. */
21948 pop_unparsed_function_queues (parser);
21951 /* Parse the operand of `sizeof' (or a similar operator). Returns
21952 either a TYPE or an expression, depending on the form of the
21953 input. The KEYWORD indicates which kind of expression we have
21954 encountered. */
21956 static tree
21957 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21959 tree expr = NULL_TREE;
21960 const char *saved_message;
21961 char *tmp;
21962 bool saved_integral_constant_expression_p;
21963 bool saved_non_integral_constant_expression_p;
21964 bool pack_expansion_p = false;
21966 /* Types cannot be defined in a `sizeof' expression. Save away the
21967 old message. */
21968 saved_message = parser->type_definition_forbidden_message;
21969 /* And create the new one. */
21970 tmp = concat ("types may not be defined in %<",
21971 IDENTIFIER_POINTER (ridpointers[keyword]),
21972 "%> expressions", NULL);
21973 parser->type_definition_forbidden_message = tmp;
21975 /* The restrictions on constant-expressions do not apply inside
21976 sizeof expressions. */
21977 saved_integral_constant_expression_p
21978 = parser->integral_constant_expression_p;
21979 saved_non_integral_constant_expression_p
21980 = parser->non_integral_constant_expression_p;
21981 parser->integral_constant_expression_p = false;
21983 /* If it's a `...', then we are computing the length of a parameter
21984 pack. */
21985 if (keyword == RID_SIZEOF
21986 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21988 /* Consume the `...'. */
21989 cp_lexer_consume_token (parser->lexer);
21990 maybe_warn_variadic_templates ();
21992 /* Note that this is an expansion. */
21993 pack_expansion_p = true;
21996 /* Do not actually evaluate the expression. */
21997 ++cp_unevaluated_operand;
21998 ++c_inhibit_evaluation_warnings;
21999 /* If it's a `(', then we might be looking at the type-id
22000 construction. */
22001 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22003 tree type;
22004 bool saved_in_type_id_in_expr_p;
22006 /* We can't be sure yet whether we're looking at a type-id or an
22007 expression. */
22008 cp_parser_parse_tentatively (parser);
22009 /* Consume the `('. */
22010 cp_lexer_consume_token (parser->lexer);
22011 /* Parse the type-id. */
22012 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
22013 parser->in_type_id_in_expr_p = true;
22014 type = cp_parser_type_id (parser);
22015 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
22016 /* Now, look for the trailing `)'. */
22017 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22018 /* If all went well, then we're done. */
22019 if (cp_parser_parse_definitely (parser))
22021 cp_decl_specifier_seq decl_specs;
22023 /* Build a trivial decl-specifier-seq. */
22024 clear_decl_specs (&decl_specs);
22025 decl_specs.type = type;
22027 /* Call grokdeclarator to figure out what type this is. */
22028 expr = grokdeclarator (NULL,
22029 &decl_specs,
22030 TYPENAME,
22031 /*initialized=*/0,
22032 /*attrlist=*/NULL);
22035 else if (pack_expansion_p)
22036 permerror (cp_lexer_peek_token (parser->lexer)->location,
22037 "%<sizeof...%> argument must be surrounded by parentheses");
22039 /* If the type-id production did not work out, then we must be
22040 looking at the unary-expression production. */
22041 if (!expr)
22042 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
22043 /*cast_p=*/false, NULL);
22045 if (pack_expansion_p)
22046 /* Build a pack expansion. */
22047 expr = make_pack_expansion (expr);
22049 /* Go back to evaluating expressions. */
22050 --cp_unevaluated_operand;
22051 --c_inhibit_evaluation_warnings;
22053 /* Free the message we created. */
22054 free (tmp);
22055 /* And restore the old one. */
22056 parser->type_definition_forbidden_message = saved_message;
22057 parser->integral_constant_expression_p
22058 = saved_integral_constant_expression_p;
22059 parser->non_integral_constant_expression_p
22060 = saved_non_integral_constant_expression_p;
22062 return expr;
22065 /* If the current declaration has no declarator, return true. */
22067 static bool
22068 cp_parser_declares_only_class_p (cp_parser *parser)
22070 /* If the next token is a `;' or a `,' then there is no
22071 declarator. */
22072 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22073 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22076 /* Update the DECL_SPECS to reflect the storage class indicated by
22077 KEYWORD. */
22079 static void
22080 cp_parser_set_storage_class (cp_parser *parser,
22081 cp_decl_specifier_seq *decl_specs,
22082 enum rid keyword,
22083 location_t location)
22085 cp_storage_class storage_class;
22087 if (parser->in_unbraced_linkage_specification_p)
22089 error_at (location, "invalid use of %qD in linkage specification",
22090 ridpointers[keyword]);
22091 return;
22093 else if (decl_specs->storage_class != sc_none)
22095 decl_specs->conflicting_specifiers_p = true;
22096 return;
22099 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22100 && decl_spec_seq_has_spec_p (decl_specs, ds_thread))
22102 error_at (decl_specs->locations[ds_thread],
22103 "%<__thread%> before %qD", ridpointers[keyword]);
22104 decl_specs->locations[ds_thread] = 0;
22107 switch (keyword)
22109 case RID_AUTO:
22110 storage_class = sc_auto;
22111 break;
22112 case RID_REGISTER:
22113 storage_class = sc_register;
22114 break;
22115 case RID_STATIC:
22116 storage_class = sc_static;
22117 break;
22118 case RID_EXTERN:
22119 storage_class = sc_extern;
22120 break;
22121 case RID_MUTABLE:
22122 storage_class = sc_mutable;
22123 break;
22124 default:
22125 gcc_unreachable ();
22127 decl_specs->storage_class = storage_class;
22128 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, location);
22130 /* A storage class specifier cannot be applied alongside a typedef
22131 specifier. If there is a typedef specifier present then set
22132 conflicting_specifiers_p which will trigger an error later
22133 on in grokdeclarator. */
22134 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
22135 decl_specs->conflicting_specifiers_p = true;
22138 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22139 is true, the type is a class or enum definition. */
22141 static void
22142 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22143 tree type_spec,
22144 location_t location,
22145 bool type_definition_p)
22147 decl_specs->any_specifiers_p = true;
22149 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22150 (with, for example, in "typedef int wchar_t;") we remember that
22151 this is what happened. In system headers, we ignore these
22152 declarations so that G++ can work with system headers that are not
22153 C++-safe. */
22154 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
22155 && !type_definition_p
22156 && (type_spec == boolean_type_node
22157 || type_spec == char16_type_node
22158 || type_spec == char32_type_node
22159 || type_spec == wchar_type_node)
22160 && (decl_specs->type
22161 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
22162 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
22163 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
22164 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
22166 decl_specs->redefined_builtin_type = type_spec;
22167 set_and_check_decl_spec_loc (decl_specs,
22168 ds_redefined_builtin_type_spec,
22169 location);
22170 if (!decl_specs->type)
22172 decl_specs->type = type_spec;
22173 decl_specs->type_definition_p = false;
22174 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, location);
22177 else if (decl_specs->type)
22178 decl_specs->multiple_types_p = true;
22179 else
22181 decl_specs->type = type_spec;
22182 decl_specs->type_definition_p = type_definition_p;
22183 decl_specs->redefined_builtin_type = NULL_TREE;
22184 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, location);
22188 /* Set the location for a declarator specifier and check if it is
22189 duplicated.
22191 DECL_SPECS is the sequence of declarator specifiers onto which to
22192 set the location.
22194 DS is the single declarator specifier to set which location is to
22195 be set onto the existing sequence of declarators.
22197 LOCATION is the location for the declarator specifier to
22198 consider. */
22200 static void
22201 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
22202 cp_decl_spec ds, source_location location)
22204 gcc_assert (ds < ds_last);
22206 if (decl_specs == NULL)
22207 return;
22209 if (decl_specs->locations[ds] == 0)
22210 decl_specs->locations[ds] = location;
22211 else
22213 if (ds == ds_long)
22215 if (decl_specs->locations[ds_long_long] != 0)
22216 error_at (location,
22217 "%<long long long%> is too long for GCC");
22218 else
22220 decl_specs->locations[ds_long_long] = location;
22221 pedwarn_cxx98 (location,
22222 OPT_Wlong_long,
22223 "ISO C++ 1998 does not support %<long long%>");
22226 else
22228 static const char *const decl_spec_names[] = {
22229 "signed",
22230 "unsigned",
22231 "short",
22232 "long",
22233 "const",
22234 "volatile",
22235 "restrict",
22236 "inline",
22237 "virtual",
22238 "explicit",
22239 "friend",
22240 "typedef",
22241 "using",
22242 "constexpr",
22243 "__complex",
22244 "__thread"
22246 error_at (location,
22247 "duplicate %qs", decl_spec_names[ds]);
22252 /* Return true iff the declarator specifier DS is present in the
22253 sequence of declarator specifiers DECL_SPECS. */
22255 bool
22256 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
22257 cp_decl_spec ds)
22259 gcc_assert (ds < ds_last);
22261 if (decl_specs == NULL)
22262 return false;
22264 return decl_specs->locations[ds] != 0;
22267 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22268 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22270 static bool
22271 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22273 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
22276 /* Issue an error message indicating that TOKEN_DESC was expected.
22277 If KEYWORD is true, it indicated this function is called by
22278 cp_parser_require_keword and the required token can only be
22279 a indicated keyword. */
22281 static void
22282 cp_parser_required_error (cp_parser *parser,
22283 required_token token_desc,
22284 bool keyword)
22286 switch (token_desc)
22288 case RT_NEW:
22289 cp_parser_error (parser, "expected %<new%>");
22290 return;
22291 case RT_DELETE:
22292 cp_parser_error (parser, "expected %<delete%>");
22293 return;
22294 case RT_RETURN:
22295 cp_parser_error (parser, "expected %<return%>");
22296 return;
22297 case RT_WHILE:
22298 cp_parser_error (parser, "expected %<while%>");
22299 return;
22300 case RT_EXTERN:
22301 cp_parser_error (parser, "expected %<extern%>");
22302 return;
22303 case RT_STATIC_ASSERT:
22304 cp_parser_error (parser, "expected %<static_assert%>");
22305 return;
22306 case RT_DECLTYPE:
22307 cp_parser_error (parser, "expected %<decltype%>");
22308 return;
22309 case RT_OPERATOR:
22310 cp_parser_error (parser, "expected %<operator%>");
22311 return;
22312 case RT_CLASS:
22313 cp_parser_error (parser, "expected %<class%>");
22314 return;
22315 case RT_TEMPLATE:
22316 cp_parser_error (parser, "expected %<template%>");
22317 return;
22318 case RT_NAMESPACE:
22319 cp_parser_error (parser, "expected %<namespace%>");
22320 return;
22321 case RT_USING:
22322 cp_parser_error (parser, "expected %<using%>");
22323 return;
22324 case RT_ASM:
22325 cp_parser_error (parser, "expected %<asm%>");
22326 return;
22327 case RT_TRY:
22328 cp_parser_error (parser, "expected %<try%>");
22329 return;
22330 case RT_CATCH:
22331 cp_parser_error (parser, "expected %<catch%>");
22332 return;
22333 case RT_THROW:
22334 cp_parser_error (parser, "expected %<throw%>");
22335 return;
22336 case RT_LABEL:
22337 cp_parser_error (parser, "expected %<__label__%>");
22338 return;
22339 case RT_AT_TRY:
22340 cp_parser_error (parser, "expected %<@try%>");
22341 return;
22342 case RT_AT_SYNCHRONIZED:
22343 cp_parser_error (parser, "expected %<@synchronized%>");
22344 return;
22345 case RT_AT_THROW:
22346 cp_parser_error (parser, "expected %<@throw%>");
22347 return;
22348 case RT_TRANSACTION_ATOMIC:
22349 cp_parser_error (parser, "expected %<__transaction_atomic%>");
22350 return;
22351 case RT_TRANSACTION_RELAXED:
22352 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22353 return;
22354 default:
22355 break;
22357 if (!keyword)
22359 switch (token_desc)
22361 case RT_SEMICOLON:
22362 cp_parser_error (parser, "expected %<;%>");
22363 return;
22364 case RT_OPEN_PAREN:
22365 cp_parser_error (parser, "expected %<(%>");
22366 return;
22367 case RT_CLOSE_BRACE:
22368 cp_parser_error (parser, "expected %<}%>");
22369 return;
22370 case RT_OPEN_BRACE:
22371 cp_parser_error (parser, "expected %<{%>");
22372 return;
22373 case RT_CLOSE_SQUARE:
22374 cp_parser_error (parser, "expected %<]%>");
22375 return;
22376 case RT_OPEN_SQUARE:
22377 cp_parser_error (parser, "expected %<[%>");
22378 return;
22379 case RT_COMMA:
22380 cp_parser_error (parser, "expected %<,%>");
22381 return;
22382 case RT_SCOPE:
22383 cp_parser_error (parser, "expected %<::%>");
22384 return;
22385 case RT_LESS:
22386 cp_parser_error (parser, "expected %<<%>");
22387 return;
22388 case RT_GREATER:
22389 cp_parser_error (parser, "expected %<>%>");
22390 return;
22391 case RT_EQ:
22392 cp_parser_error (parser, "expected %<=%>");
22393 return;
22394 case RT_ELLIPSIS:
22395 cp_parser_error (parser, "expected %<...%>");
22396 return;
22397 case RT_MULT:
22398 cp_parser_error (parser, "expected %<*%>");
22399 return;
22400 case RT_COMPL:
22401 cp_parser_error (parser, "expected %<~%>");
22402 return;
22403 case RT_COLON:
22404 cp_parser_error (parser, "expected %<:%>");
22405 return;
22406 case RT_COLON_SCOPE:
22407 cp_parser_error (parser, "expected %<:%> or %<::%>");
22408 return;
22409 case RT_CLOSE_PAREN:
22410 cp_parser_error (parser, "expected %<)%>");
22411 return;
22412 case RT_COMMA_CLOSE_PAREN:
22413 cp_parser_error (parser, "expected %<,%> or %<)%>");
22414 return;
22415 case RT_PRAGMA_EOL:
22416 cp_parser_error (parser, "expected end of line");
22417 return;
22418 case RT_NAME:
22419 cp_parser_error (parser, "expected identifier");
22420 return;
22421 case RT_SELECT:
22422 cp_parser_error (parser, "expected selection-statement");
22423 return;
22424 case RT_INTERATION:
22425 cp_parser_error (parser, "expected iteration-statement");
22426 return;
22427 case RT_JUMP:
22428 cp_parser_error (parser, "expected jump-statement");
22429 return;
22430 case RT_CLASS_KEY:
22431 cp_parser_error (parser, "expected class-key");
22432 return;
22433 case RT_CLASS_TYPENAME_TEMPLATE:
22434 cp_parser_error (parser,
22435 "expected %<class%>, %<typename%>, or %<template%>");
22436 return;
22437 default:
22438 gcc_unreachable ();
22441 else
22442 gcc_unreachable ();
22447 /* If the next token is of the indicated TYPE, consume it. Otherwise,
22448 issue an error message indicating that TOKEN_DESC was expected.
22450 Returns the token consumed, if the token had the appropriate type.
22451 Otherwise, returns NULL. */
22453 static cp_token *
22454 cp_parser_require (cp_parser* parser,
22455 enum cpp_ttype type,
22456 required_token token_desc)
22458 if (cp_lexer_next_token_is (parser->lexer, type))
22459 return cp_lexer_consume_token (parser->lexer);
22460 else
22462 /* Output the MESSAGE -- unless we're parsing tentatively. */
22463 if (!cp_parser_simulate_error (parser))
22464 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22465 return NULL;
22469 /* An error message is produced if the next token is not '>'.
22470 All further tokens are skipped until the desired token is
22471 found or '{', '}', ';' or an unbalanced ')' or ']'. */
22473 static void
22474 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22476 /* Current level of '< ... >'. */
22477 unsigned level = 0;
22478 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
22479 unsigned nesting_depth = 0;
22481 /* Are we ready, yet? If not, issue error message. */
22482 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22483 return;
22485 /* Skip tokens until the desired token is found. */
22486 while (true)
22488 /* Peek at the next token. */
22489 switch (cp_lexer_peek_token (parser->lexer)->type)
22491 case CPP_LESS:
22492 if (!nesting_depth)
22493 ++level;
22494 break;
22496 case CPP_RSHIFT:
22497 if (cxx_dialect == cxx98)
22498 /* C++0x views the `>>' operator as two `>' tokens, but
22499 C++98 does not. */
22500 break;
22501 else if (!nesting_depth && level-- == 0)
22503 /* We've hit a `>>' where the first `>' closes the
22504 template argument list, and the second `>' is
22505 spurious. Just consume the `>>' and stop; we've
22506 already produced at least one error. */
22507 cp_lexer_consume_token (parser->lexer);
22508 return;
22510 /* Fall through for C++0x, so we handle the second `>' in
22511 the `>>'. */
22513 case CPP_GREATER:
22514 if (!nesting_depth && level-- == 0)
22516 /* We've reached the token we want, consume it and stop. */
22517 cp_lexer_consume_token (parser->lexer);
22518 return;
22520 break;
22522 case CPP_OPEN_PAREN:
22523 case CPP_OPEN_SQUARE:
22524 ++nesting_depth;
22525 break;
22527 case CPP_CLOSE_PAREN:
22528 case CPP_CLOSE_SQUARE:
22529 if (nesting_depth-- == 0)
22530 return;
22531 break;
22533 case CPP_EOF:
22534 case CPP_PRAGMA_EOL:
22535 case CPP_SEMICOLON:
22536 case CPP_OPEN_BRACE:
22537 case CPP_CLOSE_BRACE:
22538 /* The '>' was probably forgotten, don't look further. */
22539 return;
22541 default:
22542 break;
22545 /* Consume this token. */
22546 cp_lexer_consume_token (parser->lexer);
22550 /* If the next token is the indicated keyword, consume it. Otherwise,
22551 issue an error message indicating that TOKEN_DESC was expected.
22553 Returns the token consumed, if the token had the appropriate type.
22554 Otherwise, returns NULL. */
22556 static cp_token *
22557 cp_parser_require_keyword (cp_parser* parser,
22558 enum rid keyword,
22559 required_token token_desc)
22561 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22563 if (token && token->keyword != keyword)
22565 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
22566 return NULL;
22569 return token;
22572 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22573 function-definition. */
22575 static bool
22576 cp_parser_token_starts_function_definition_p (cp_token* token)
22578 return (/* An ordinary function-body begins with an `{'. */
22579 token->type == CPP_OPEN_BRACE
22580 /* A ctor-initializer begins with a `:'. */
22581 || token->type == CPP_COLON
22582 /* A function-try-block begins with `try'. */
22583 || token->keyword == RID_TRY
22584 /* A function-transaction-block begins with `__transaction_atomic'
22585 or `__transaction_relaxed'. */
22586 || token->keyword == RID_TRANSACTION_ATOMIC
22587 || token->keyword == RID_TRANSACTION_RELAXED
22588 /* The named return value extension begins with `return'. */
22589 || token->keyword == RID_RETURN);
22592 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22593 definition. */
22595 static bool
22596 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22598 cp_token *token;
22600 token = cp_lexer_peek_token (parser->lexer);
22601 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22604 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22605 C++0x) ending a template-argument. */
22607 static bool
22608 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22610 cp_token *token;
22612 token = cp_lexer_peek_token (parser->lexer);
22613 return (token->type == CPP_COMMA
22614 || token->type == CPP_GREATER
22615 || token->type == CPP_ELLIPSIS
22616 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22619 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22620 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
22622 static bool
22623 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22624 size_t n)
22626 cp_token *token;
22628 token = cp_lexer_peek_nth_token (parser->lexer, n);
22629 if (token->type == CPP_LESS)
22630 return true;
22631 /* Check for the sequence `<::' in the original code. It would be lexed as
22632 `[:', where `[' is a digraph, and there is no whitespace before
22633 `:'. */
22634 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22636 cp_token *token2;
22637 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22638 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22639 return true;
22641 return false;
22644 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22645 or none_type otherwise. */
22647 static enum tag_types
22648 cp_parser_token_is_class_key (cp_token* token)
22650 switch (token->keyword)
22652 case RID_CLASS:
22653 return class_type;
22654 case RID_STRUCT:
22655 return record_type;
22656 case RID_UNION:
22657 return union_type;
22659 default:
22660 return none_type;
22664 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
22666 static void
22667 cp_parser_check_class_key (enum tag_types class_key, tree type)
22669 if (type == error_mark_node)
22670 return;
22671 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22673 permerror (input_location, "%qs tag used in naming %q#T",
22674 class_key == union_type ? "union"
22675 : class_key == record_type ? "struct" : "class",
22676 type);
22677 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
22678 "%q#T was previously declared here", type);
22682 /* Issue an error message if DECL is redeclared with different
22683 access than its original declaration [class.access.spec/3].
22684 This applies to nested classes and nested class templates.
22685 [class.mem/1]. */
22687 static void
22688 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22690 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22691 return;
22693 if ((TREE_PRIVATE (decl)
22694 != (current_access_specifier == access_private_node))
22695 || (TREE_PROTECTED (decl)
22696 != (current_access_specifier == access_protected_node)))
22697 error_at (location, "%qD redeclared with different access", decl);
22700 /* Look for the `template' keyword, as a syntactic disambiguator.
22701 Return TRUE iff it is present, in which case it will be
22702 consumed. */
22704 static bool
22705 cp_parser_optional_template_keyword (cp_parser *parser)
22707 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22709 /* The `template' keyword can only be used within templates;
22710 outside templates the parser can always figure out what is a
22711 template and what is not. */
22712 if (!processing_template_decl)
22714 cp_token *token = cp_lexer_peek_token (parser->lexer);
22715 error_at (token->location,
22716 "%<template%> (as a disambiguator) is only allowed "
22717 "within templates");
22718 /* If this part of the token stream is rescanned, the same
22719 error message would be generated. So, we purge the token
22720 from the stream. */
22721 cp_lexer_purge_token (parser->lexer);
22722 return false;
22724 else
22726 /* Consume the `template' keyword. */
22727 cp_lexer_consume_token (parser->lexer);
22728 return true;
22732 return false;
22735 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
22736 set PARSER->SCOPE, and perform other related actions. */
22738 static void
22739 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22741 int i;
22742 struct tree_check *check_value;
22743 deferred_access_check *chk;
22744 VEC (deferred_access_check,gc) *checks;
22746 /* Get the stored value. */
22747 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22748 /* Perform any access checks that were deferred. */
22749 checks = check_value->checks;
22750 if (checks)
22752 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22753 perform_or_defer_access_check (chk->binfo,
22754 chk->decl,
22755 chk->diag_decl, tf_warning_or_error);
22757 /* Set the scope from the stored value. */
22758 parser->scope = check_value->value;
22759 parser->qualifying_scope = check_value->qualifying_scope;
22760 parser->object_scope = NULL_TREE;
22763 /* Consume tokens up through a non-nested END token. Returns TRUE if we
22764 encounter the end of a block before what we were looking for. */
22766 static bool
22767 cp_parser_cache_group (cp_parser *parser,
22768 enum cpp_ttype end,
22769 unsigned depth)
22771 while (true)
22773 cp_token *token = cp_lexer_peek_token (parser->lexer);
22775 /* Abort a parenthesized expression if we encounter a semicolon. */
22776 if ((end == CPP_CLOSE_PAREN || depth == 0)
22777 && token->type == CPP_SEMICOLON)
22778 return true;
22779 /* If we've reached the end of the file, stop. */
22780 if (token->type == CPP_EOF
22781 || (end != CPP_PRAGMA_EOL
22782 && token->type == CPP_PRAGMA_EOL))
22783 return true;
22784 if (token->type == CPP_CLOSE_BRACE && depth == 0)
22785 /* We've hit the end of an enclosing block, so there's been some
22786 kind of syntax error. */
22787 return true;
22789 /* Consume the token. */
22790 cp_lexer_consume_token (parser->lexer);
22791 /* See if it starts a new group. */
22792 if (token->type == CPP_OPEN_BRACE)
22794 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22795 /* In theory this should probably check end == '}', but
22796 cp_parser_save_member_function_body needs it to exit
22797 after either '}' or ')' when called with ')'. */
22798 if (depth == 0)
22799 return false;
22801 else if (token->type == CPP_OPEN_PAREN)
22803 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22804 if (depth == 0 && end == CPP_CLOSE_PAREN)
22805 return false;
22807 else if (token->type == CPP_PRAGMA)
22808 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22809 else if (token->type == end)
22810 return false;
22814 /* Like above, for caching a default argument or NSDMI. Both of these are
22815 terminated by a non-nested comma, but it can be unclear whether or not a
22816 comma is nested in a template argument list unless we do more parsing.
22817 In order to handle this ambiguity, when we encounter a ',' after a '<'
22818 we try to parse what follows as a parameter-declaration-list (in the
22819 case of a default argument) or a member-declarator (in the case of an
22820 NSDMI). If that succeeds, then we stop caching. */
22822 static tree
22823 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
22825 unsigned depth = 0;
22826 int maybe_template_id = 0;
22827 cp_token *first_token;
22828 cp_token *token;
22829 tree default_argument;
22831 /* Add tokens until we have processed the entire default
22832 argument. We add the range [first_token, token). */
22833 first_token = cp_lexer_peek_token (parser->lexer);
22834 if (first_token->type == CPP_OPEN_BRACE)
22836 /* For list-initialization, this is straightforward. */
22837 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22838 token = cp_lexer_peek_token (parser->lexer);
22840 else while (true)
22842 bool done = false;
22844 /* Peek at the next token. */
22845 token = cp_lexer_peek_token (parser->lexer);
22846 /* What we do depends on what token we have. */
22847 switch (token->type)
22849 /* In valid code, a default argument must be
22850 immediately followed by a `,' `)', or `...'. */
22851 case CPP_COMMA:
22852 if (depth == 0 && maybe_template_id)
22854 /* If we've seen a '<', we might be in a
22855 template-argument-list. Until Core issue 325 is
22856 resolved, we don't know how this situation ought
22857 to be handled, so try to DTRT. We check whether
22858 what comes after the comma is a valid parameter
22859 declaration list. If it is, then the comma ends
22860 the default argument; otherwise the default
22861 argument continues. */
22862 bool error = false;
22863 tree t;
22865 /* Set ITALP so cp_parser_parameter_declaration_list
22866 doesn't decide to commit to this parse. */
22867 bool saved_italp = parser->in_template_argument_list_p;
22868 parser->in_template_argument_list_p = true;
22870 cp_parser_parse_tentatively (parser);
22871 cp_lexer_consume_token (parser->lexer);
22873 if (nsdmi)
22875 int ctor_dtor_or_conv_p;
22876 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22877 &ctor_dtor_or_conv_p,
22878 /*parenthesized_p=*/NULL,
22879 /*member_p=*/true);
22881 else
22883 begin_scope (sk_function_parms, NULL_TREE);
22884 cp_parser_parameter_declaration_list (parser, &error);
22885 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
22886 pop_binding (DECL_NAME (t), t);
22887 leave_scope ();
22889 if (!cp_parser_error_occurred (parser) && !error)
22890 done = true;
22891 cp_parser_abort_tentative_parse (parser);
22893 parser->in_template_argument_list_p = saved_italp;
22894 break;
22896 case CPP_CLOSE_PAREN:
22897 case CPP_ELLIPSIS:
22898 /* If we run into a non-nested `;', `}', or `]',
22899 then the code is invalid -- but the default
22900 argument is certainly over. */
22901 case CPP_SEMICOLON:
22902 case CPP_CLOSE_BRACE:
22903 case CPP_CLOSE_SQUARE:
22904 if (depth == 0)
22905 done = true;
22906 /* Update DEPTH, if necessary. */
22907 else if (token->type == CPP_CLOSE_PAREN
22908 || token->type == CPP_CLOSE_BRACE
22909 || token->type == CPP_CLOSE_SQUARE)
22910 --depth;
22911 break;
22913 case CPP_OPEN_PAREN:
22914 case CPP_OPEN_SQUARE:
22915 case CPP_OPEN_BRACE:
22916 ++depth;
22917 break;
22919 case CPP_LESS:
22920 if (depth == 0)
22921 /* This might be the comparison operator, or it might
22922 start a template argument list. */
22923 ++maybe_template_id;
22924 break;
22926 case CPP_RSHIFT:
22927 if (cxx_dialect == cxx98)
22928 break;
22929 /* Fall through for C++0x, which treats the `>>'
22930 operator like two `>' tokens in certain
22931 cases. */
22933 case CPP_GREATER:
22934 if (depth == 0)
22936 /* This might be an operator, or it might close a
22937 template argument list. But if a previous '<'
22938 started a template argument list, this will have
22939 closed it, so we can't be in one anymore. */
22940 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
22941 if (maybe_template_id < 0)
22942 maybe_template_id = 0;
22944 break;
22946 /* If we run out of tokens, issue an error message. */
22947 case CPP_EOF:
22948 case CPP_PRAGMA_EOL:
22949 error_at (token->location, "file ends in default argument");
22950 done = true;
22951 break;
22953 case CPP_NAME:
22954 case CPP_SCOPE:
22955 /* In these cases, we should look for template-ids.
22956 For example, if the default argument is
22957 `X<int, double>()', we need to do name lookup to
22958 figure out whether or not `X' is a template; if
22959 so, the `,' does not end the default argument.
22961 That is not yet done. */
22962 break;
22964 default:
22965 break;
22968 /* If we've reached the end, stop. */
22969 if (done)
22970 break;
22972 /* Add the token to the token block. */
22973 token = cp_lexer_consume_token (parser->lexer);
22976 /* Create a DEFAULT_ARG to represent the unparsed default
22977 argument. */
22978 default_argument = make_node (DEFAULT_ARG);
22979 DEFARG_TOKENS (default_argument)
22980 = cp_token_cache_new (first_token, token);
22981 DEFARG_INSTANTIATIONS (default_argument) = NULL;
22983 return default_argument;
22986 /* Begin parsing tentatively. We always save tokens while parsing
22987 tentatively so that if the tentative parsing fails we can restore the
22988 tokens. */
22990 static void
22991 cp_parser_parse_tentatively (cp_parser* parser)
22993 /* Enter a new parsing context. */
22994 parser->context = cp_parser_context_new (parser->context);
22995 /* Begin saving tokens. */
22996 cp_lexer_save_tokens (parser->lexer);
22997 /* In order to avoid repetitive access control error messages,
22998 access checks are queued up until we are no longer parsing
22999 tentatively. */
23000 push_deferring_access_checks (dk_deferred);
23003 /* Commit to the currently active tentative parse. */
23005 static void
23006 cp_parser_commit_to_tentative_parse (cp_parser* parser)
23008 cp_parser_context *context;
23009 cp_lexer *lexer;
23011 /* Mark all of the levels as committed. */
23012 lexer = parser->lexer;
23013 for (context = parser->context; context->next; context = context->next)
23015 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
23016 break;
23017 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
23018 while (!cp_lexer_saving_tokens (lexer))
23019 lexer = lexer->next;
23020 cp_lexer_commit_tokens (lexer);
23024 /* Abort the currently active tentative parse. All consumed tokens
23025 will be rolled back, and no diagnostics will be issued. */
23027 static void
23028 cp_parser_abort_tentative_parse (cp_parser* parser)
23030 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
23031 || errorcount > 0);
23032 cp_parser_simulate_error (parser);
23033 /* Now, pretend that we want to see if the construct was
23034 successfully parsed. */
23035 cp_parser_parse_definitely (parser);
23038 /* Stop parsing tentatively. If a parse error has occurred, restore the
23039 token stream. Otherwise, commit to the tokens we have consumed.
23040 Returns true if no error occurred; false otherwise. */
23042 static bool
23043 cp_parser_parse_definitely (cp_parser* parser)
23045 bool error_occurred;
23046 cp_parser_context *context;
23048 /* Remember whether or not an error occurred, since we are about to
23049 destroy that information. */
23050 error_occurred = cp_parser_error_occurred (parser);
23051 /* Remove the topmost context from the stack. */
23052 context = parser->context;
23053 parser->context = context->next;
23054 /* If no parse errors occurred, commit to the tentative parse. */
23055 if (!error_occurred)
23057 /* Commit to the tokens read tentatively, unless that was
23058 already done. */
23059 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
23060 cp_lexer_commit_tokens (parser->lexer);
23062 pop_to_parent_deferring_access_checks ();
23064 /* Otherwise, if errors occurred, roll back our state so that things
23065 are just as they were before we began the tentative parse. */
23066 else
23068 cp_lexer_rollback_tokens (parser->lexer);
23069 pop_deferring_access_checks ();
23071 /* Add the context to the front of the free list. */
23072 context->next = cp_parser_context_free_list;
23073 cp_parser_context_free_list = context;
23075 return !error_occurred;
23078 /* Returns true if we are parsing tentatively and are not committed to
23079 this tentative parse. */
23081 static bool
23082 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
23084 return (cp_parser_parsing_tentatively (parser)
23085 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
23088 /* Returns nonzero iff an error has occurred during the most recent
23089 tentative parse. */
23091 static bool
23092 cp_parser_error_occurred (cp_parser* parser)
23094 return (cp_parser_parsing_tentatively (parser)
23095 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
23098 /* Returns nonzero if GNU extensions are allowed. */
23100 static bool
23101 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
23103 return parser->allow_gnu_extensions_p;
23106 /* Objective-C++ Productions */
23109 /* Parse an Objective-C expression, which feeds into a primary-expression
23110 above.
23112 objc-expression:
23113 objc-message-expression
23114 objc-string-literal
23115 objc-encode-expression
23116 objc-protocol-expression
23117 objc-selector-expression
23119 Returns a tree representation of the expression. */
23121 static tree
23122 cp_parser_objc_expression (cp_parser* parser)
23124 /* Try to figure out what kind of declaration is present. */
23125 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23127 switch (kwd->type)
23129 case CPP_OPEN_SQUARE:
23130 return cp_parser_objc_message_expression (parser);
23132 case CPP_OBJC_STRING:
23133 kwd = cp_lexer_consume_token (parser->lexer);
23134 return objc_build_string_object (kwd->u.value);
23136 case CPP_KEYWORD:
23137 switch (kwd->keyword)
23139 case RID_AT_ENCODE:
23140 return cp_parser_objc_encode_expression (parser);
23142 case RID_AT_PROTOCOL:
23143 return cp_parser_objc_protocol_expression (parser);
23145 case RID_AT_SELECTOR:
23146 return cp_parser_objc_selector_expression (parser);
23148 default:
23149 break;
23151 default:
23152 error_at (kwd->location,
23153 "misplaced %<@%D%> Objective-C++ construct",
23154 kwd->u.value);
23155 cp_parser_skip_to_end_of_block_or_statement (parser);
23158 return error_mark_node;
23161 /* Parse an Objective-C message expression.
23163 objc-message-expression:
23164 [ objc-message-receiver objc-message-args ]
23166 Returns a representation of an Objective-C message. */
23168 static tree
23169 cp_parser_objc_message_expression (cp_parser* parser)
23171 tree receiver, messageargs;
23173 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
23174 receiver = cp_parser_objc_message_receiver (parser);
23175 messageargs = cp_parser_objc_message_args (parser);
23176 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23178 return objc_build_message_expr (receiver, messageargs);
23181 /* Parse an objc-message-receiver.
23183 objc-message-receiver:
23184 expression
23185 simple-type-specifier
23187 Returns a representation of the type or expression. */
23189 static tree
23190 cp_parser_objc_message_receiver (cp_parser* parser)
23192 tree rcv;
23194 /* An Objective-C message receiver may be either (1) a type
23195 or (2) an expression. */
23196 cp_parser_parse_tentatively (parser);
23197 rcv = cp_parser_expression (parser, false, NULL);
23199 if (cp_parser_parse_definitely (parser))
23200 return rcv;
23202 rcv = cp_parser_simple_type_specifier (parser,
23203 /*decl_specs=*/NULL,
23204 CP_PARSER_FLAGS_NONE);
23206 return objc_get_class_reference (rcv);
23209 /* Parse the arguments and selectors comprising an Objective-C message.
23211 objc-message-args:
23212 objc-selector
23213 objc-selector-args
23214 objc-selector-args , objc-comma-args
23216 objc-selector-args:
23217 objc-selector [opt] : assignment-expression
23218 objc-selector-args objc-selector [opt] : assignment-expression
23220 objc-comma-args:
23221 assignment-expression
23222 objc-comma-args , assignment-expression
23224 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23225 selector arguments and TREE_VALUE containing a list of comma
23226 arguments. */
23228 static tree
23229 cp_parser_objc_message_args (cp_parser* parser)
23231 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23232 bool maybe_unary_selector_p = true;
23233 cp_token *token = cp_lexer_peek_token (parser->lexer);
23235 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23237 tree selector = NULL_TREE, arg;
23239 if (token->type != CPP_COLON)
23240 selector = cp_parser_objc_selector (parser);
23242 /* Detect if we have a unary selector. */
23243 if (maybe_unary_selector_p
23244 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23245 return build_tree_list (selector, NULL_TREE);
23247 maybe_unary_selector_p = false;
23248 cp_parser_require (parser, CPP_COLON, RT_COLON);
23249 arg = cp_parser_assignment_expression (parser, false, NULL);
23251 sel_args
23252 = chainon (sel_args,
23253 build_tree_list (selector, arg));
23255 token = cp_lexer_peek_token (parser->lexer);
23258 /* Handle non-selector arguments, if any. */
23259 while (token->type == CPP_COMMA)
23261 tree arg;
23263 cp_lexer_consume_token (parser->lexer);
23264 arg = cp_parser_assignment_expression (parser, false, NULL);
23266 addl_args
23267 = chainon (addl_args,
23268 build_tree_list (NULL_TREE, arg));
23270 token = cp_lexer_peek_token (parser->lexer);
23273 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23275 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23276 return build_tree_list (error_mark_node, error_mark_node);
23279 return build_tree_list (sel_args, addl_args);
23282 /* Parse an Objective-C encode expression.
23284 objc-encode-expression:
23285 @encode objc-typename
23287 Returns an encoded representation of the type argument. */
23289 static tree
23290 cp_parser_objc_encode_expression (cp_parser* parser)
23292 tree type;
23293 cp_token *token;
23295 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
23296 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23297 token = cp_lexer_peek_token (parser->lexer);
23298 type = complete_type (cp_parser_type_id (parser));
23299 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23301 if (!type)
23303 error_at (token->location,
23304 "%<@encode%> must specify a type as an argument");
23305 return error_mark_node;
23308 /* This happens if we find @encode(T) (where T is a template
23309 typename or something dependent on a template typename) when
23310 parsing a template. In that case, we can't compile it
23311 immediately, but we rather create an AT_ENCODE_EXPR which will
23312 need to be instantiated when the template is used.
23314 if (dependent_type_p (type))
23316 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23317 TREE_READONLY (value) = 1;
23318 return value;
23321 return objc_build_encode_expr (type);
23324 /* Parse an Objective-C @defs expression. */
23326 static tree
23327 cp_parser_objc_defs_expression (cp_parser *parser)
23329 tree name;
23331 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
23332 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23333 name = cp_parser_identifier (parser);
23334 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23336 return objc_get_class_ivars (name);
23339 /* Parse an Objective-C protocol expression.
23341 objc-protocol-expression:
23342 @protocol ( identifier )
23344 Returns a representation of the protocol expression. */
23346 static tree
23347 cp_parser_objc_protocol_expression (cp_parser* parser)
23349 tree proto;
23351 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23352 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23353 proto = cp_parser_identifier (parser);
23354 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23356 return objc_build_protocol_expr (proto);
23359 /* Parse an Objective-C selector expression.
23361 objc-selector-expression:
23362 @selector ( objc-method-signature )
23364 objc-method-signature:
23365 objc-selector
23366 objc-selector-seq
23368 objc-selector-seq:
23369 objc-selector :
23370 objc-selector-seq objc-selector :
23372 Returns a representation of the method selector. */
23374 static tree
23375 cp_parser_objc_selector_expression (cp_parser* parser)
23377 tree sel_seq = NULL_TREE;
23378 bool maybe_unary_selector_p = true;
23379 cp_token *token;
23380 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23382 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
23383 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23384 token = cp_lexer_peek_token (parser->lexer);
23386 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23387 || token->type == CPP_SCOPE)
23389 tree selector = NULL_TREE;
23391 if (token->type != CPP_COLON
23392 || token->type == CPP_SCOPE)
23393 selector = cp_parser_objc_selector (parser);
23395 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23396 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23398 /* Detect if we have a unary selector. */
23399 if (maybe_unary_selector_p)
23401 sel_seq = selector;
23402 goto finish_selector;
23404 else
23406 cp_parser_error (parser, "expected %<:%>");
23409 maybe_unary_selector_p = false;
23410 token = cp_lexer_consume_token (parser->lexer);
23412 if (token->type == CPP_SCOPE)
23414 sel_seq
23415 = chainon (sel_seq,
23416 build_tree_list (selector, NULL_TREE));
23417 sel_seq
23418 = chainon (sel_seq,
23419 build_tree_list (NULL_TREE, NULL_TREE));
23421 else
23422 sel_seq
23423 = chainon (sel_seq,
23424 build_tree_list (selector, NULL_TREE));
23426 token = cp_lexer_peek_token (parser->lexer);
23429 finish_selector:
23430 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23432 return objc_build_selector_expr (loc, sel_seq);
23435 /* Parse a list of identifiers.
23437 objc-identifier-list:
23438 identifier
23439 objc-identifier-list , identifier
23441 Returns a TREE_LIST of identifier nodes. */
23443 static tree
23444 cp_parser_objc_identifier_list (cp_parser* parser)
23446 tree identifier;
23447 tree list;
23448 cp_token *sep;
23450 identifier = cp_parser_identifier (parser);
23451 if (identifier == error_mark_node)
23452 return error_mark_node;
23454 list = build_tree_list (NULL_TREE, identifier);
23455 sep = cp_lexer_peek_token (parser->lexer);
23457 while (sep->type == CPP_COMMA)
23459 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23460 identifier = cp_parser_identifier (parser);
23461 if (identifier == error_mark_node)
23462 return list;
23464 list = chainon (list, build_tree_list (NULL_TREE,
23465 identifier));
23466 sep = cp_lexer_peek_token (parser->lexer);
23469 return list;
23472 /* Parse an Objective-C alias declaration.
23474 objc-alias-declaration:
23475 @compatibility_alias identifier identifier ;
23477 This function registers the alias mapping with the Objective-C front end.
23478 It returns nothing. */
23480 static void
23481 cp_parser_objc_alias_declaration (cp_parser* parser)
23483 tree alias, orig;
23485 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
23486 alias = cp_parser_identifier (parser);
23487 orig = cp_parser_identifier (parser);
23488 objc_declare_alias (alias, orig);
23489 cp_parser_consume_semicolon_at_end_of_statement (parser);
23492 /* Parse an Objective-C class forward-declaration.
23494 objc-class-declaration:
23495 @class objc-identifier-list ;
23497 The function registers the forward declarations with the Objective-C
23498 front end. It returns nothing. */
23500 static void
23501 cp_parser_objc_class_declaration (cp_parser* parser)
23503 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
23504 while (true)
23506 tree id;
23508 id = cp_parser_identifier (parser);
23509 if (id == error_mark_node)
23510 break;
23512 objc_declare_class (id);
23514 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23515 cp_lexer_consume_token (parser->lexer);
23516 else
23517 break;
23519 cp_parser_consume_semicolon_at_end_of_statement (parser);
23522 /* Parse a list of Objective-C protocol references.
23524 objc-protocol-refs-opt:
23525 objc-protocol-refs [opt]
23527 objc-protocol-refs:
23528 < objc-identifier-list >
23530 Returns a TREE_LIST of identifiers, if any. */
23532 static tree
23533 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23535 tree protorefs = NULL_TREE;
23537 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23539 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
23540 protorefs = cp_parser_objc_identifier_list (parser);
23541 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23544 return protorefs;
23547 /* Parse a Objective-C visibility specification. */
23549 static void
23550 cp_parser_objc_visibility_spec (cp_parser* parser)
23552 cp_token *vis = cp_lexer_peek_token (parser->lexer);
23554 switch (vis->keyword)
23556 case RID_AT_PRIVATE:
23557 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23558 break;
23559 case RID_AT_PROTECTED:
23560 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23561 break;
23562 case RID_AT_PUBLIC:
23563 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23564 break;
23565 case RID_AT_PACKAGE:
23566 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23567 break;
23568 default:
23569 return;
23572 /* Eat '@private'/'@protected'/'@public'. */
23573 cp_lexer_consume_token (parser->lexer);
23576 /* Parse an Objective-C method type. Return 'true' if it is a class
23577 (+) method, and 'false' if it is an instance (-) method. */
23579 static inline bool
23580 cp_parser_objc_method_type (cp_parser* parser)
23582 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23583 return true;
23584 else
23585 return false;
23588 /* Parse an Objective-C protocol qualifier. */
23590 static tree
23591 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23593 tree quals = NULL_TREE, node;
23594 cp_token *token = cp_lexer_peek_token (parser->lexer);
23596 node = token->u.value;
23598 while (node && TREE_CODE (node) == IDENTIFIER_NODE
23599 && (node == ridpointers [(int) RID_IN]
23600 || node == ridpointers [(int) RID_OUT]
23601 || node == ridpointers [(int) RID_INOUT]
23602 || node == ridpointers [(int) RID_BYCOPY]
23603 || node == ridpointers [(int) RID_BYREF]
23604 || node == ridpointers [(int) RID_ONEWAY]))
23606 quals = tree_cons (NULL_TREE, node, quals);
23607 cp_lexer_consume_token (parser->lexer);
23608 token = cp_lexer_peek_token (parser->lexer);
23609 node = token->u.value;
23612 return quals;
23615 /* Parse an Objective-C typename. */
23617 static tree
23618 cp_parser_objc_typename (cp_parser* parser)
23620 tree type_name = NULL_TREE;
23622 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23624 tree proto_quals, cp_type = NULL_TREE;
23626 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23627 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23629 /* An ObjC type name may consist of just protocol qualifiers, in which
23630 case the type shall default to 'id'. */
23631 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23633 cp_type = cp_parser_type_id (parser);
23635 /* If the type could not be parsed, an error has already
23636 been produced. For error recovery, behave as if it had
23637 not been specified, which will use the default type
23638 'id'. */
23639 if (cp_type == error_mark_node)
23641 cp_type = NULL_TREE;
23642 /* We need to skip to the closing parenthesis as
23643 cp_parser_type_id() does not seem to do it for
23644 us. */
23645 cp_parser_skip_to_closing_parenthesis (parser,
23646 /*recovering=*/true,
23647 /*or_comma=*/false,
23648 /*consume_paren=*/false);
23652 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23653 type_name = build_tree_list (proto_quals, cp_type);
23656 return type_name;
23659 /* Check to see if TYPE refers to an Objective-C selector name. */
23661 static bool
23662 cp_parser_objc_selector_p (enum cpp_ttype type)
23664 return (type == CPP_NAME || type == CPP_KEYWORD
23665 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23666 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23667 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23668 || type == CPP_XOR || type == CPP_XOR_EQ);
23671 /* Parse an Objective-C selector. */
23673 static tree
23674 cp_parser_objc_selector (cp_parser* parser)
23676 cp_token *token = cp_lexer_consume_token (parser->lexer);
23678 if (!cp_parser_objc_selector_p (token->type))
23680 error_at (token->location, "invalid Objective-C++ selector name");
23681 return error_mark_node;
23684 /* C++ operator names are allowed to appear in ObjC selectors. */
23685 switch (token->type)
23687 case CPP_AND_AND: return get_identifier ("and");
23688 case CPP_AND_EQ: return get_identifier ("and_eq");
23689 case CPP_AND: return get_identifier ("bitand");
23690 case CPP_OR: return get_identifier ("bitor");
23691 case CPP_COMPL: return get_identifier ("compl");
23692 case CPP_NOT: return get_identifier ("not");
23693 case CPP_NOT_EQ: return get_identifier ("not_eq");
23694 case CPP_OR_OR: return get_identifier ("or");
23695 case CPP_OR_EQ: return get_identifier ("or_eq");
23696 case CPP_XOR: return get_identifier ("xor");
23697 case CPP_XOR_EQ: return get_identifier ("xor_eq");
23698 default: return token->u.value;
23702 /* Parse an Objective-C params list. */
23704 static tree
23705 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23707 tree params = NULL_TREE;
23708 bool maybe_unary_selector_p = true;
23709 cp_token *token = cp_lexer_peek_token (parser->lexer);
23711 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23713 tree selector = NULL_TREE, type_name, identifier;
23714 tree parm_attr = NULL_TREE;
23716 if (token->keyword == RID_ATTRIBUTE)
23717 break;
23719 if (token->type != CPP_COLON)
23720 selector = cp_parser_objc_selector (parser);
23722 /* Detect if we have a unary selector. */
23723 if (maybe_unary_selector_p
23724 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23726 params = selector; /* Might be followed by attributes. */
23727 break;
23730 maybe_unary_selector_p = false;
23731 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23733 /* Something went quite wrong. There should be a colon
23734 here, but there is not. Stop parsing parameters. */
23735 break;
23737 type_name = cp_parser_objc_typename (parser);
23738 /* New ObjC allows attributes on parameters too. */
23739 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23740 parm_attr = cp_parser_attributes_opt (parser);
23741 identifier = cp_parser_identifier (parser);
23743 params
23744 = chainon (params,
23745 objc_build_keyword_decl (selector,
23746 type_name,
23747 identifier,
23748 parm_attr));
23750 token = cp_lexer_peek_token (parser->lexer);
23753 if (params == NULL_TREE)
23755 cp_parser_error (parser, "objective-c++ method declaration is expected");
23756 return error_mark_node;
23759 /* We allow tail attributes for the method. */
23760 if (token->keyword == RID_ATTRIBUTE)
23762 *attributes = cp_parser_attributes_opt (parser);
23763 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23764 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23765 return params;
23766 cp_parser_error (parser,
23767 "method attributes must be specified at the end");
23768 return error_mark_node;
23771 if (params == NULL_TREE)
23773 cp_parser_error (parser, "objective-c++ method declaration is expected");
23774 return error_mark_node;
23776 return params;
23779 /* Parse the non-keyword Objective-C params. */
23781 static tree
23782 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
23783 tree* attributes)
23785 tree params = make_node (TREE_LIST);
23786 cp_token *token = cp_lexer_peek_token (parser->lexer);
23787 *ellipsisp = false; /* Initially, assume no ellipsis. */
23789 while (token->type == CPP_COMMA)
23791 cp_parameter_declarator *parmdecl;
23792 tree parm;
23794 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23795 token = cp_lexer_peek_token (parser->lexer);
23797 if (token->type == CPP_ELLIPSIS)
23799 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
23800 *ellipsisp = true;
23801 token = cp_lexer_peek_token (parser->lexer);
23802 break;
23805 /* TODO: parse attributes for tail parameters. */
23806 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23807 parm = grokdeclarator (parmdecl->declarator,
23808 &parmdecl->decl_specifiers,
23809 PARM, /*initialized=*/0,
23810 /*attrlist=*/NULL);
23812 chainon (params, build_tree_list (NULL_TREE, parm));
23813 token = cp_lexer_peek_token (parser->lexer);
23816 /* We allow tail attributes for the method. */
23817 if (token->keyword == RID_ATTRIBUTE)
23819 if (*attributes == NULL_TREE)
23821 *attributes = cp_parser_attributes_opt (parser);
23822 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23823 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23824 return params;
23826 else
23827 /* We have an error, but parse the attributes, so that we can
23828 carry on. */
23829 *attributes = cp_parser_attributes_opt (parser);
23831 cp_parser_error (parser,
23832 "method attributes must be specified at the end");
23833 return error_mark_node;
23836 return params;
23839 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
23841 static void
23842 cp_parser_objc_interstitial_code (cp_parser* parser)
23844 cp_token *token = cp_lexer_peek_token (parser->lexer);
23846 /* If the next token is `extern' and the following token is a string
23847 literal, then we have a linkage specification. */
23848 if (token->keyword == RID_EXTERN
23849 && cp_parser_is_pure_string_literal
23850 (cp_lexer_peek_nth_token (parser->lexer, 2)))
23851 cp_parser_linkage_specification (parser);
23852 /* Handle #pragma, if any. */
23853 else if (token->type == CPP_PRAGMA)
23854 cp_parser_pragma (parser, pragma_external);
23855 /* Allow stray semicolons. */
23856 else if (token->type == CPP_SEMICOLON)
23857 cp_lexer_consume_token (parser->lexer);
23858 /* Mark methods as optional or required, when building protocols. */
23859 else if (token->keyword == RID_AT_OPTIONAL)
23861 cp_lexer_consume_token (parser->lexer);
23862 objc_set_method_opt (true);
23864 else if (token->keyword == RID_AT_REQUIRED)
23866 cp_lexer_consume_token (parser->lexer);
23867 objc_set_method_opt (false);
23869 else if (token->keyword == RID_NAMESPACE)
23870 cp_parser_namespace_definition (parser);
23871 /* Other stray characters must generate errors. */
23872 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23874 cp_lexer_consume_token (parser->lexer);
23875 error ("stray %qs between Objective-C++ methods",
23876 token->type == CPP_OPEN_BRACE ? "{" : "}");
23878 /* Finally, try to parse a block-declaration, or a function-definition. */
23879 else
23880 cp_parser_block_declaration (parser, /*statement_p=*/false);
23883 /* Parse a method signature. */
23885 static tree
23886 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23888 tree rettype, kwdparms, optparms;
23889 bool ellipsis = false;
23890 bool is_class_method;
23892 is_class_method = cp_parser_objc_method_type (parser);
23893 rettype = cp_parser_objc_typename (parser);
23894 *attributes = NULL_TREE;
23895 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23896 if (kwdparms == error_mark_node)
23897 return error_mark_node;
23898 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23899 if (optparms == error_mark_node)
23900 return error_mark_node;
23902 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23905 static bool
23906 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23908 tree tattr;
23909 cp_lexer_save_tokens (parser->lexer);
23910 tattr = cp_parser_attributes_opt (parser);
23911 gcc_assert (tattr) ;
23913 /* If the attributes are followed by a method introducer, this is not allowed.
23914 Dump the attributes and flag the situation. */
23915 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23916 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23917 return true;
23919 /* Otherwise, the attributes introduce some interstitial code, possibly so
23920 rewind to allow that check. */
23921 cp_lexer_rollback_tokens (parser->lexer);
23922 return false;
23925 /* Parse an Objective-C method prototype list. */
23927 static void
23928 cp_parser_objc_method_prototype_list (cp_parser* parser)
23930 cp_token *token = cp_lexer_peek_token (parser->lexer);
23932 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23934 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23936 tree attributes, sig;
23937 bool is_class_method;
23938 if (token->type == CPP_PLUS)
23939 is_class_method = true;
23940 else
23941 is_class_method = false;
23942 sig = cp_parser_objc_method_signature (parser, &attributes);
23943 if (sig == error_mark_node)
23945 cp_parser_skip_to_end_of_block_or_statement (parser);
23946 token = cp_lexer_peek_token (parser->lexer);
23947 continue;
23949 objc_add_method_declaration (is_class_method, sig, attributes);
23950 cp_parser_consume_semicolon_at_end_of_statement (parser);
23952 else if (token->keyword == RID_AT_PROPERTY)
23953 cp_parser_objc_at_property_declaration (parser);
23954 else if (token->keyword == RID_ATTRIBUTE
23955 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23956 warning_at (cp_lexer_peek_token (parser->lexer)->location,
23957 OPT_Wattributes,
23958 "prefix attributes are ignored for methods");
23959 else
23960 /* Allow for interspersed non-ObjC++ code. */
23961 cp_parser_objc_interstitial_code (parser);
23963 token = cp_lexer_peek_token (parser->lexer);
23966 if (token->type != CPP_EOF)
23967 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23968 else
23969 cp_parser_error (parser, "expected %<@end%>");
23971 objc_finish_interface ();
23974 /* Parse an Objective-C method definition list. */
23976 static void
23977 cp_parser_objc_method_definition_list (cp_parser* parser)
23979 cp_token *token = cp_lexer_peek_token (parser->lexer);
23981 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23983 tree meth;
23985 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23987 cp_token *ptk;
23988 tree sig, attribute;
23989 bool is_class_method;
23990 if (token->type == CPP_PLUS)
23991 is_class_method = true;
23992 else
23993 is_class_method = false;
23994 push_deferring_access_checks (dk_deferred);
23995 sig = cp_parser_objc_method_signature (parser, &attribute);
23996 if (sig == error_mark_node)
23998 cp_parser_skip_to_end_of_block_or_statement (parser);
23999 token = cp_lexer_peek_token (parser->lexer);
24000 continue;
24002 objc_start_method_definition (is_class_method, sig, attribute,
24003 NULL_TREE);
24005 /* For historical reasons, we accept an optional semicolon. */
24006 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24007 cp_lexer_consume_token (parser->lexer);
24009 ptk = cp_lexer_peek_token (parser->lexer);
24010 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
24011 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
24013 perform_deferred_access_checks (tf_warning_or_error);
24014 stop_deferring_access_checks ();
24015 meth = cp_parser_function_definition_after_declarator (parser,
24016 false);
24017 pop_deferring_access_checks ();
24018 objc_finish_method_definition (meth);
24021 /* The following case will be removed once @synthesize is
24022 completely implemented. */
24023 else if (token->keyword == RID_AT_PROPERTY)
24024 cp_parser_objc_at_property_declaration (parser);
24025 else if (token->keyword == RID_AT_SYNTHESIZE)
24026 cp_parser_objc_at_synthesize_declaration (parser);
24027 else if (token->keyword == RID_AT_DYNAMIC)
24028 cp_parser_objc_at_dynamic_declaration (parser);
24029 else if (token->keyword == RID_ATTRIBUTE
24030 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24031 warning_at (token->location, OPT_Wattributes,
24032 "prefix attributes are ignored for methods");
24033 else
24034 /* Allow for interspersed non-ObjC++ code. */
24035 cp_parser_objc_interstitial_code (parser);
24037 token = cp_lexer_peek_token (parser->lexer);
24040 if (token->type != CPP_EOF)
24041 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24042 else
24043 cp_parser_error (parser, "expected %<@end%>");
24045 objc_finish_implementation ();
24048 /* Parse Objective-C ivars. */
24050 static void
24051 cp_parser_objc_class_ivars (cp_parser* parser)
24053 cp_token *token = cp_lexer_peek_token (parser->lexer);
24055 if (token->type != CPP_OPEN_BRACE)
24056 return; /* No ivars specified. */
24058 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
24059 token = cp_lexer_peek_token (parser->lexer);
24061 while (token->type != CPP_CLOSE_BRACE
24062 && token->keyword != RID_AT_END && token->type != CPP_EOF)
24064 cp_decl_specifier_seq declspecs;
24065 int decl_class_or_enum_p;
24066 tree prefix_attributes;
24068 cp_parser_objc_visibility_spec (parser);
24070 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24071 break;
24073 cp_parser_decl_specifier_seq (parser,
24074 CP_PARSER_FLAGS_OPTIONAL,
24075 &declspecs,
24076 &decl_class_or_enum_p);
24078 /* auto, register, static, extern, mutable. */
24079 if (declspecs.storage_class != sc_none)
24081 cp_parser_error (parser, "invalid type for instance variable");
24082 declspecs.storage_class = sc_none;
24085 /* __thread. */
24086 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24088 cp_parser_error (parser, "invalid type for instance variable");
24089 declspecs.locations[ds_thread] = 0;
24092 /* typedef. */
24093 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24095 cp_parser_error (parser, "invalid type for instance variable");
24096 declspecs.locations[ds_thread] = 0;
24099 prefix_attributes = declspecs.attributes;
24100 declspecs.attributes = NULL_TREE;
24102 /* Keep going until we hit the `;' at the end of the
24103 declaration. */
24104 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24106 tree width = NULL_TREE, attributes, first_attribute, decl;
24107 cp_declarator *declarator = NULL;
24108 int ctor_dtor_or_conv_p;
24110 /* Check for a (possibly unnamed) bitfield declaration. */
24111 token = cp_lexer_peek_token (parser->lexer);
24112 if (token->type == CPP_COLON)
24113 goto eat_colon;
24115 if (token->type == CPP_NAME
24116 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24117 == CPP_COLON))
24119 /* Get the name of the bitfield. */
24120 declarator = make_id_declarator (NULL_TREE,
24121 cp_parser_identifier (parser),
24122 sfk_none);
24124 eat_colon:
24125 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24126 /* Get the width of the bitfield. */
24127 width
24128 = cp_parser_constant_expression (parser,
24129 /*allow_non_constant=*/false,
24130 NULL);
24132 else
24134 /* Parse the declarator. */
24135 declarator
24136 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24137 &ctor_dtor_or_conv_p,
24138 /*parenthesized_p=*/NULL,
24139 /*member_p=*/false);
24142 /* Look for attributes that apply to the ivar. */
24143 attributes = cp_parser_attributes_opt (parser);
24144 /* Remember which attributes are prefix attributes and
24145 which are not. */
24146 first_attribute = attributes;
24147 /* Combine the attributes. */
24148 attributes = chainon (prefix_attributes, attributes);
24150 if (width)
24151 /* Create the bitfield declaration. */
24152 decl = grokbitfield (declarator, &declspecs,
24153 width,
24154 attributes);
24155 else
24156 decl = grokfield (declarator, &declspecs,
24157 NULL_TREE, /*init_const_expr_p=*/false,
24158 NULL_TREE, attributes);
24160 /* Add the instance variable. */
24161 if (decl != error_mark_node && decl != NULL_TREE)
24162 objc_add_instance_variable (decl);
24164 /* Reset PREFIX_ATTRIBUTES. */
24165 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24166 attributes = TREE_CHAIN (attributes);
24167 if (attributes)
24168 TREE_CHAIN (attributes) = NULL_TREE;
24170 token = cp_lexer_peek_token (parser->lexer);
24172 if (token->type == CPP_COMMA)
24174 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24175 continue;
24177 break;
24180 cp_parser_consume_semicolon_at_end_of_statement (parser);
24181 token = cp_lexer_peek_token (parser->lexer);
24184 if (token->keyword == RID_AT_END)
24185 cp_parser_error (parser, "expected %<}%>");
24187 /* Do not consume the RID_AT_END, so it will be read again as terminating
24188 the @interface of @implementation. */
24189 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24190 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
24192 /* For historical reasons, we accept an optional semicolon. */
24193 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24194 cp_lexer_consume_token (parser->lexer);
24197 /* Parse an Objective-C protocol declaration. */
24199 static void
24200 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24202 tree proto, protorefs;
24203 cp_token *tok;
24205 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24206 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24208 tok = cp_lexer_peek_token (parser->lexer);
24209 error_at (tok->location, "identifier expected after %<@protocol%>");
24210 cp_parser_consume_semicolon_at_end_of_statement (parser);
24211 return;
24214 /* See if we have a forward declaration or a definition. */
24215 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24217 /* Try a forward declaration first. */
24218 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24220 while (true)
24222 tree id;
24224 id = cp_parser_identifier (parser);
24225 if (id == error_mark_node)
24226 break;
24228 objc_declare_protocol (id, attributes);
24230 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24231 cp_lexer_consume_token (parser->lexer);
24232 else
24233 break;
24235 cp_parser_consume_semicolon_at_end_of_statement (parser);
24238 /* Ok, we got a full-fledged definition (or at least should). */
24239 else
24241 proto = cp_parser_identifier (parser);
24242 protorefs = cp_parser_objc_protocol_refs_opt (parser);
24243 objc_start_protocol (proto, protorefs, attributes);
24244 cp_parser_objc_method_prototype_list (parser);
24248 /* Parse an Objective-C superclass or category. */
24250 static void
24251 cp_parser_objc_superclass_or_category (cp_parser *parser,
24252 bool iface_p,
24253 tree *super,
24254 tree *categ, bool *is_class_extension)
24256 cp_token *next = cp_lexer_peek_token (parser->lexer);
24258 *super = *categ = NULL_TREE;
24259 *is_class_extension = false;
24260 if (next->type == CPP_COLON)
24262 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24263 *super = cp_parser_identifier (parser);
24265 else if (next->type == CPP_OPEN_PAREN)
24267 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24269 /* If there is no category name, and this is an @interface, we
24270 have a class extension. */
24271 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24273 *categ = NULL_TREE;
24274 *is_class_extension = true;
24276 else
24277 *categ = cp_parser_identifier (parser);
24279 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24283 /* Parse an Objective-C class interface. */
24285 static void
24286 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24288 tree name, super, categ, protos;
24289 bool is_class_extension;
24291 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
24292 name = cp_parser_identifier (parser);
24293 if (name == error_mark_node)
24295 /* It's hard to recover because even if valid @interface stuff
24296 is to follow, we can't compile it (or validate it) if we
24297 don't even know which class it refers to. Let's assume this
24298 was a stray '@interface' token in the stream and skip it.
24300 return;
24302 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24303 &is_class_extension);
24304 protos = cp_parser_objc_protocol_refs_opt (parser);
24306 /* We have either a class or a category on our hands. */
24307 if (categ || is_class_extension)
24308 objc_start_category_interface (name, categ, protos, attributes);
24309 else
24311 objc_start_class_interface (name, super, protos, attributes);
24312 /* Handle instance variable declarations, if any. */
24313 cp_parser_objc_class_ivars (parser);
24314 objc_continue_interface ();
24317 cp_parser_objc_method_prototype_list (parser);
24320 /* Parse an Objective-C class implementation. */
24322 static void
24323 cp_parser_objc_class_implementation (cp_parser* parser)
24325 tree name, super, categ;
24326 bool is_class_extension;
24328 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
24329 name = cp_parser_identifier (parser);
24330 if (name == error_mark_node)
24332 /* It's hard to recover because even if valid @implementation
24333 stuff is to follow, we can't compile it (or validate it) if
24334 we don't even know which class it refers to. Let's assume
24335 this was a stray '@implementation' token in the stream and
24336 skip it.
24338 return;
24340 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24341 &is_class_extension);
24343 /* We have either a class or a category on our hands. */
24344 if (categ)
24345 objc_start_category_implementation (name, categ);
24346 else
24348 objc_start_class_implementation (name, super);
24349 /* Handle instance variable declarations, if any. */
24350 cp_parser_objc_class_ivars (parser);
24351 objc_continue_implementation ();
24354 cp_parser_objc_method_definition_list (parser);
24357 /* Consume the @end token and finish off the implementation. */
24359 static void
24360 cp_parser_objc_end_implementation (cp_parser* parser)
24362 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24363 objc_finish_implementation ();
24366 /* Parse an Objective-C declaration. */
24368 static void
24369 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24371 /* Try to figure out what kind of declaration is present. */
24372 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24374 if (attributes)
24375 switch (kwd->keyword)
24377 case RID_AT_ALIAS:
24378 case RID_AT_CLASS:
24379 case RID_AT_END:
24380 error_at (kwd->location, "attributes may not be specified before"
24381 " the %<@%D%> Objective-C++ keyword",
24382 kwd->u.value);
24383 attributes = NULL;
24384 break;
24385 case RID_AT_IMPLEMENTATION:
24386 warning_at (kwd->location, OPT_Wattributes,
24387 "prefix attributes are ignored before %<@%D%>",
24388 kwd->u.value);
24389 attributes = NULL;
24390 default:
24391 break;
24394 switch (kwd->keyword)
24396 case RID_AT_ALIAS:
24397 cp_parser_objc_alias_declaration (parser);
24398 break;
24399 case RID_AT_CLASS:
24400 cp_parser_objc_class_declaration (parser);
24401 break;
24402 case RID_AT_PROTOCOL:
24403 cp_parser_objc_protocol_declaration (parser, attributes);
24404 break;
24405 case RID_AT_INTERFACE:
24406 cp_parser_objc_class_interface (parser, attributes);
24407 break;
24408 case RID_AT_IMPLEMENTATION:
24409 cp_parser_objc_class_implementation (parser);
24410 break;
24411 case RID_AT_END:
24412 cp_parser_objc_end_implementation (parser);
24413 break;
24414 default:
24415 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24416 kwd->u.value);
24417 cp_parser_skip_to_end_of_block_or_statement (parser);
24421 /* Parse an Objective-C try-catch-finally statement.
24423 objc-try-catch-finally-stmt:
24424 @try compound-statement objc-catch-clause-seq [opt]
24425 objc-finally-clause [opt]
24427 objc-catch-clause-seq:
24428 objc-catch-clause objc-catch-clause-seq [opt]
24430 objc-catch-clause:
24431 @catch ( objc-exception-declaration ) compound-statement
24433 objc-finally-clause:
24434 @finally compound-statement
24436 objc-exception-declaration:
24437 parameter-declaration
24438 '...'
24440 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24442 Returns NULL_TREE.
24444 PS: This function is identical to c_parser_objc_try_catch_finally_statement
24445 for C. Keep them in sync. */
24447 static tree
24448 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24450 location_t location;
24451 tree stmt;
24453 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24454 location = cp_lexer_peek_token (parser->lexer)->location;
24455 objc_maybe_warn_exceptions (location);
24456 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24457 node, lest it get absorbed into the surrounding block. */
24458 stmt = push_stmt_list ();
24459 cp_parser_compound_statement (parser, NULL, false, false);
24460 objc_begin_try_stmt (location, pop_stmt_list (stmt));
24462 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24464 cp_parameter_declarator *parm;
24465 tree parameter_declaration = error_mark_node;
24466 bool seen_open_paren = false;
24468 cp_lexer_consume_token (parser->lexer);
24469 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24470 seen_open_paren = true;
24471 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24473 /* We have "@catch (...)" (where the '...' are literally
24474 what is in the code). Skip the '...'.
24475 parameter_declaration is set to NULL_TREE, and
24476 objc_being_catch_clauses() knows that that means
24477 '...'. */
24478 cp_lexer_consume_token (parser->lexer);
24479 parameter_declaration = NULL_TREE;
24481 else
24483 /* We have "@catch (NSException *exception)" or something
24484 like that. Parse the parameter declaration. */
24485 parm = cp_parser_parameter_declaration (parser, false, NULL);
24486 if (parm == NULL)
24487 parameter_declaration = error_mark_node;
24488 else
24489 parameter_declaration = grokdeclarator (parm->declarator,
24490 &parm->decl_specifiers,
24491 PARM, /*initialized=*/0,
24492 /*attrlist=*/NULL);
24494 if (seen_open_paren)
24495 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24496 else
24498 /* If there was no open parenthesis, we are recovering from
24499 an error, and we are trying to figure out what mistake
24500 the user has made. */
24502 /* If there is an immediate closing parenthesis, the user
24503 probably forgot the opening one (ie, they typed "@catch
24504 NSException *e)". Parse the closing parenthesis and keep
24505 going. */
24506 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24507 cp_lexer_consume_token (parser->lexer);
24509 /* If these is no immediate closing parenthesis, the user
24510 probably doesn't know that parenthesis are required at
24511 all (ie, they typed "@catch NSException *e"). So, just
24512 forget about the closing parenthesis and keep going. */
24514 objc_begin_catch_clause (parameter_declaration);
24515 cp_parser_compound_statement (parser, NULL, false, false);
24516 objc_finish_catch_clause ();
24518 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24520 cp_lexer_consume_token (parser->lexer);
24521 location = cp_lexer_peek_token (parser->lexer)->location;
24522 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24523 node, lest it get absorbed into the surrounding block. */
24524 stmt = push_stmt_list ();
24525 cp_parser_compound_statement (parser, NULL, false, false);
24526 objc_build_finally_clause (location, pop_stmt_list (stmt));
24529 return objc_finish_try_stmt ();
24532 /* Parse an Objective-C synchronized statement.
24534 objc-synchronized-stmt:
24535 @synchronized ( expression ) compound-statement
24537 Returns NULL_TREE. */
24539 static tree
24540 cp_parser_objc_synchronized_statement (cp_parser *parser)
24542 location_t location;
24543 tree lock, stmt;
24545 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24547 location = cp_lexer_peek_token (parser->lexer)->location;
24548 objc_maybe_warn_exceptions (location);
24549 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24550 lock = cp_parser_expression (parser, false, NULL);
24551 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24553 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24554 node, lest it get absorbed into the surrounding block. */
24555 stmt = push_stmt_list ();
24556 cp_parser_compound_statement (parser, NULL, false, false);
24558 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24561 /* Parse an Objective-C throw statement.
24563 objc-throw-stmt:
24564 @throw assignment-expression [opt] ;
24566 Returns a constructed '@throw' statement. */
24568 static tree
24569 cp_parser_objc_throw_statement (cp_parser *parser)
24571 tree expr = NULL_TREE;
24572 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24574 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24576 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24577 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24579 cp_parser_consume_semicolon_at_end_of_statement (parser);
24581 return objc_build_throw_stmt (loc, expr);
24584 /* Parse an Objective-C statement. */
24586 static tree
24587 cp_parser_objc_statement (cp_parser * parser)
24589 /* Try to figure out what kind of declaration is present. */
24590 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24592 switch (kwd->keyword)
24594 case RID_AT_TRY:
24595 return cp_parser_objc_try_catch_finally_statement (parser);
24596 case RID_AT_SYNCHRONIZED:
24597 return cp_parser_objc_synchronized_statement (parser);
24598 case RID_AT_THROW:
24599 return cp_parser_objc_throw_statement (parser);
24600 default:
24601 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24602 kwd->u.value);
24603 cp_parser_skip_to_end_of_block_or_statement (parser);
24606 return error_mark_node;
24609 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
24610 look ahead to see if an objc keyword follows the attributes. This
24611 is to detect the use of prefix attributes on ObjC @interface and
24612 @protocol. */
24614 static bool
24615 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24617 cp_lexer_save_tokens (parser->lexer);
24618 *attrib = cp_parser_attributes_opt (parser);
24619 gcc_assert (*attrib);
24620 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24622 cp_lexer_commit_tokens (parser->lexer);
24623 return true;
24625 cp_lexer_rollback_tokens (parser->lexer);
24626 return false;
24629 /* This routine is a minimal replacement for
24630 c_parser_struct_declaration () used when parsing the list of
24631 types/names or ObjC++ properties. For example, when parsing the
24632 code
24634 @property (readonly) int a, b, c;
24636 this function is responsible for parsing "int a, int b, int c" and
24637 returning the declarations as CHAIN of DECLs.
24639 TODO: Share this code with cp_parser_objc_class_ivars. It's very
24640 similar parsing. */
24641 static tree
24642 cp_parser_objc_struct_declaration (cp_parser *parser)
24644 tree decls = NULL_TREE;
24645 cp_decl_specifier_seq declspecs;
24646 int decl_class_or_enum_p;
24647 tree prefix_attributes;
24649 cp_parser_decl_specifier_seq (parser,
24650 CP_PARSER_FLAGS_NONE,
24651 &declspecs,
24652 &decl_class_or_enum_p);
24654 if (declspecs.type == error_mark_node)
24655 return error_mark_node;
24657 /* auto, register, static, extern, mutable. */
24658 if (declspecs.storage_class != sc_none)
24660 cp_parser_error (parser, "invalid type for property");
24661 declspecs.storage_class = sc_none;
24664 /* __thread. */
24665 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24667 cp_parser_error (parser, "invalid type for property");
24668 declspecs.locations[ds_thread] = 0;
24671 /* typedef. */
24672 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24674 cp_parser_error (parser, "invalid type for property");
24675 declspecs.locations[ds_typedef] = 0;
24678 prefix_attributes = declspecs.attributes;
24679 declspecs.attributes = NULL_TREE;
24681 /* Keep going until we hit the `;' at the end of the declaration. */
24682 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24684 tree attributes, first_attribute, decl;
24685 cp_declarator *declarator;
24686 cp_token *token;
24688 /* Parse the declarator. */
24689 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24690 NULL, NULL, false);
24692 /* Look for attributes that apply to the ivar. */
24693 attributes = cp_parser_attributes_opt (parser);
24694 /* Remember which attributes are prefix attributes and
24695 which are not. */
24696 first_attribute = attributes;
24697 /* Combine the attributes. */
24698 attributes = chainon (prefix_attributes, attributes);
24700 decl = grokfield (declarator, &declspecs,
24701 NULL_TREE, /*init_const_expr_p=*/false,
24702 NULL_TREE, attributes);
24704 if (decl == error_mark_node || decl == NULL_TREE)
24705 return error_mark_node;
24707 /* Reset PREFIX_ATTRIBUTES. */
24708 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24709 attributes = TREE_CHAIN (attributes);
24710 if (attributes)
24711 TREE_CHAIN (attributes) = NULL_TREE;
24713 DECL_CHAIN (decl) = decls;
24714 decls = decl;
24716 token = cp_lexer_peek_token (parser->lexer);
24717 if (token->type == CPP_COMMA)
24719 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24720 continue;
24722 else
24723 break;
24725 return decls;
24728 /* Parse an Objective-C @property declaration. The syntax is:
24730 objc-property-declaration:
24731 '@property' objc-property-attributes[opt] struct-declaration ;
24733 objc-property-attributes:
24734 '(' objc-property-attribute-list ')'
24736 objc-property-attribute-list:
24737 objc-property-attribute
24738 objc-property-attribute-list, objc-property-attribute
24740 objc-property-attribute
24741 'getter' = identifier
24742 'setter' = identifier
24743 'readonly'
24744 'readwrite'
24745 'assign'
24746 'retain'
24747 'copy'
24748 'nonatomic'
24750 For example:
24751 @property NSString *name;
24752 @property (readonly) id object;
24753 @property (retain, nonatomic, getter=getTheName) id name;
24754 @property int a, b, c;
24756 PS: This function is identical to
24757 c_parser_objc_at_property_declaration for C. Keep them in sync. */
24758 static void
24759 cp_parser_objc_at_property_declaration (cp_parser *parser)
24761 /* The following variables hold the attributes of the properties as
24762 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
24763 seen. When we see an attribute, we set them to 'true' (if they
24764 are boolean properties) or to the identifier (if they have an
24765 argument, ie, for getter and setter). Note that here we only
24766 parse the list of attributes, check the syntax and accumulate the
24767 attributes that we find. objc_add_property_declaration() will
24768 then process the information. */
24769 bool property_assign = false;
24770 bool property_copy = false;
24771 tree property_getter_ident = NULL_TREE;
24772 bool property_nonatomic = false;
24773 bool property_readonly = false;
24774 bool property_readwrite = false;
24775 bool property_retain = false;
24776 tree property_setter_ident = NULL_TREE;
24778 /* 'properties' is the list of properties that we read. Usually a
24779 single one, but maybe more (eg, in "@property int a, b, c;" there
24780 are three). */
24781 tree properties;
24782 location_t loc;
24784 loc = cp_lexer_peek_token (parser->lexer)->location;
24786 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
24788 /* Parse the optional attribute list... */
24789 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24791 /* Eat the '('. */
24792 cp_lexer_consume_token (parser->lexer);
24794 while (true)
24796 bool syntax_error = false;
24797 cp_token *token = cp_lexer_peek_token (parser->lexer);
24798 enum rid keyword;
24800 if (token->type != CPP_NAME)
24802 cp_parser_error (parser, "expected identifier");
24803 break;
24805 keyword = C_RID_CODE (token->u.value);
24806 cp_lexer_consume_token (parser->lexer);
24807 switch (keyword)
24809 case RID_ASSIGN: property_assign = true; break;
24810 case RID_COPY: property_copy = true; break;
24811 case RID_NONATOMIC: property_nonatomic = true; break;
24812 case RID_READONLY: property_readonly = true; break;
24813 case RID_READWRITE: property_readwrite = true; break;
24814 case RID_RETAIN: property_retain = true; break;
24816 case RID_GETTER:
24817 case RID_SETTER:
24818 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24820 if (keyword == RID_GETTER)
24821 cp_parser_error (parser,
24822 "missing %<=%> (after %<getter%> attribute)");
24823 else
24824 cp_parser_error (parser,
24825 "missing %<=%> (after %<setter%> attribute)");
24826 syntax_error = true;
24827 break;
24829 cp_lexer_consume_token (parser->lexer); /* eat the = */
24830 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24832 cp_parser_error (parser, "expected identifier");
24833 syntax_error = true;
24834 break;
24836 if (keyword == RID_SETTER)
24838 if (property_setter_ident != NULL_TREE)
24840 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24841 cp_lexer_consume_token (parser->lexer);
24843 else
24844 property_setter_ident = cp_parser_objc_selector (parser);
24845 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24846 cp_parser_error (parser, "setter name must terminate with %<:%>");
24847 else
24848 cp_lexer_consume_token (parser->lexer);
24850 else
24852 if (property_getter_ident != NULL_TREE)
24854 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24855 cp_lexer_consume_token (parser->lexer);
24857 else
24858 property_getter_ident = cp_parser_objc_selector (parser);
24860 break;
24861 default:
24862 cp_parser_error (parser, "unknown property attribute");
24863 syntax_error = true;
24864 break;
24867 if (syntax_error)
24868 break;
24870 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24871 cp_lexer_consume_token (parser->lexer);
24872 else
24873 break;
24876 /* FIXME: "@property (setter, assign);" will generate a spurious
24877 "error: expected ‘)’ before ‘,’ token". This is because
24878 cp_parser_require, unlike the C counterpart, will produce an
24879 error even if we are in error recovery. */
24880 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24882 cp_parser_skip_to_closing_parenthesis (parser,
24883 /*recovering=*/true,
24884 /*or_comma=*/false,
24885 /*consume_paren=*/true);
24889 /* ... and the property declaration(s). */
24890 properties = cp_parser_objc_struct_declaration (parser);
24892 if (properties == error_mark_node)
24894 cp_parser_skip_to_end_of_statement (parser);
24895 /* If the next token is now a `;', consume it. */
24896 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24897 cp_lexer_consume_token (parser->lexer);
24898 return;
24901 if (properties == NULL_TREE)
24902 cp_parser_error (parser, "expected identifier");
24903 else
24905 /* Comma-separated properties are chained together in
24906 reverse order; add them one by one. */
24907 properties = nreverse (properties);
24909 for (; properties; properties = TREE_CHAIN (properties))
24910 objc_add_property_declaration (loc, copy_node (properties),
24911 property_readonly, property_readwrite,
24912 property_assign, property_retain,
24913 property_copy, property_nonatomic,
24914 property_getter_ident, property_setter_ident);
24917 cp_parser_consume_semicolon_at_end_of_statement (parser);
24920 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
24922 objc-synthesize-declaration:
24923 @synthesize objc-synthesize-identifier-list ;
24925 objc-synthesize-identifier-list:
24926 objc-synthesize-identifier
24927 objc-synthesize-identifier-list, objc-synthesize-identifier
24929 objc-synthesize-identifier
24930 identifier
24931 identifier = identifier
24933 For example:
24934 @synthesize MyProperty;
24935 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24937 PS: This function is identical to c_parser_objc_at_synthesize_declaration
24938 for C. Keep them in sync.
24940 static void
24941 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24943 tree list = NULL_TREE;
24944 location_t loc;
24945 loc = cp_lexer_peek_token (parser->lexer)->location;
24947 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
24948 while (true)
24950 tree property, ivar;
24951 property = cp_parser_identifier (parser);
24952 if (property == error_mark_node)
24954 cp_parser_consume_semicolon_at_end_of_statement (parser);
24955 return;
24957 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24959 cp_lexer_consume_token (parser->lexer);
24960 ivar = cp_parser_identifier (parser);
24961 if (ivar == error_mark_node)
24963 cp_parser_consume_semicolon_at_end_of_statement (parser);
24964 return;
24967 else
24968 ivar = NULL_TREE;
24969 list = chainon (list, build_tree_list (ivar, property));
24970 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24971 cp_lexer_consume_token (parser->lexer);
24972 else
24973 break;
24975 cp_parser_consume_semicolon_at_end_of_statement (parser);
24976 objc_add_synthesize_declaration (loc, list);
24979 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
24981 objc-dynamic-declaration:
24982 @dynamic identifier-list ;
24984 For example:
24985 @dynamic MyProperty;
24986 @dynamic MyProperty, AnotherProperty;
24988 PS: This function is identical to c_parser_objc_at_dynamic_declaration
24989 for C. Keep them in sync.
24991 static void
24992 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24994 tree list = NULL_TREE;
24995 location_t loc;
24996 loc = cp_lexer_peek_token (parser->lexer)->location;
24998 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
24999 while (true)
25001 tree property;
25002 property = cp_parser_identifier (parser);
25003 if (property == error_mark_node)
25005 cp_parser_consume_semicolon_at_end_of_statement (parser);
25006 return;
25008 list = chainon (list, build_tree_list (NULL, property));
25009 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25010 cp_lexer_consume_token (parser->lexer);
25011 else
25012 break;
25014 cp_parser_consume_semicolon_at_end_of_statement (parser);
25015 objc_add_dynamic_declaration (loc, list);
25019 /* OpenMP 2.5 parsing routines. */
25021 /* Returns name of the next clause.
25022 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
25023 the token is not consumed. Otherwise appropriate pragma_omp_clause is
25024 returned and the token is consumed. */
25026 static pragma_omp_clause
25027 cp_parser_omp_clause_name (cp_parser *parser)
25029 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
25031 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
25032 result = PRAGMA_OMP_CLAUSE_IF;
25033 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
25034 result = PRAGMA_OMP_CLAUSE_DEFAULT;
25035 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
25036 result = PRAGMA_OMP_CLAUSE_PRIVATE;
25037 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25039 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25040 const char *p = IDENTIFIER_POINTER (id);
25042 switch (p[0])
25044 case 'c':
25045 if (!strcmp ("collapse", p))
25046 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
25047 else if (!strcmp ("copyin", p))
25048 result = PRAGMA_OMP_CLAUSE_COPYIN;
25049 else if (!strcmp ("copyprivate", p))
25050 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
25051 break;
25052 case 'f':
25053 if (!strcmp ("final", p))
25054 result = PRAGMA_OMP_CLAUSE_FINAL;
25055 else if (!strcmp ("firstprivate", p))
25056 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
25057 break;
25058 case 'l':
25059 if (!strcmp ("lastprivate", p))
25060 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
25061 break;
25062 case 'm':
25063 if (!strcmp ("mergeable", p))
25064 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
25065 break;
25066 case 'n':
25067 if (!strcmp ("nowait", p))
25068 result = PRAGMA_OMP_CLAUSE_NOWAIT;
25069 else if (!strcmp ("num_threads", p))
25070 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
25071 break;
25072 case 'o':
25073 if (!strcmp ("ordered", p))
25074 result = PRAGMA_OMP_CLAUSE_ORDERED;
25075 break;
25076 case 'r':
25077 if (!strcmp ("reduction", p))
25078 result = PRAGMA_OMP_CLAUSE_REDUCTION;
25079 break;
25080 case 's':
25081 if (!strcmp ("schedule", p))
25082 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
25083 else if (!strcmp ("shared", p))
25084 result = PRAGMA_OMP_CLAUSE_SHARED;
25085 break;
25086 case 'u':
25087 if (!strcmp ("untied", p))
25088 result = PRAGMA_OMP_CLAUSE_UNTIED;
25089 break;
25093 if (result != PRAGMA_OMP_CLAUSE_NONE)
25094 cp_lexer_consume_token (parser->lexer);
25096 return result;
25099 /* Validate that a clause of the given type does not already exist. */
25101 static void
25102 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
25103 const char *name, location_t location)
25105 tree c;
25107 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25108 if (OMP_CLAUSE_CODE (c) == code)
25110 error_at (location, "too many %qs clauses", name);
25111 break;
25115 /* OpenMP 2.5:
25116 variable-list:
25117 identifier
25118 variable-list , identifier
25120 In addition, we match a closing parenthesis. An opening parenthesis
25121 will have been consumed by the caller.
25123 If KIND is nonzero, create the appropriate node and install the decl
25124 in OMP_CLAUSE_DECL and add the node to the head of the list.
25126 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25127 return the list created. */
25129 static tree
25130 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
25131 tree list)
25133 cp_token *token;
25134 while (1)
25136 tree name, decl;
25138 token = cp_lexer_peek_token (parser->lexer);
25139 name = cp_parser_id_expression (parser, /*template_p=*/false,
25140 /*check_dependency_p=*/true,
25141 /*template_p=*/NULL,
25142 /*declarator_p=*/false,
25143 /*optional_p=*/false);
25144 if (name == error_mark_node)
25145 goto skip_comma;
25147 decl = cp_parser_lookup_name_simple (parser, name, token->location);
25148 if (decl == error_mark_node)
25149 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
25150 token->location);
25151 else if (kind != 0)
25153 tree u = build_omp_clause (token->location, kind);
25154 OMP_CLAUSE_DECL (u) = decl;
25155 OMP_CLAUSE_CHAIN (u) = list;
25156 list = u;
25158 else
25159 list = tree_cons (decl, NULL_TREE, list);
25161 get_comma:
25162 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25163 break;
25164 cp_lexer_consume_token (parser->lexer);
25167 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25169 int ending;
25171 /* Try to resync to an unnested comma. Copied from
25172 cp_parser_parenthesized_expression_list. */
25173 skip_comma:
25174 ending = cp_parser_skip_to_closing_parenthesis (parser,
25175 /*recovering=*/true,
25176 /*or_comma=*/true,
25177 /*consume_paren=*/true);
25178 if (ending < 0)
25179 goto get_comma;
25182 return list;
25185 /* Similarly, but expect leading and trailing parenthesis. This is a very
25186 common case for omp clauses. */
25188 static tree
25189 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25191 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25192 return cp_parser_omp_var_list_no_open (parser, kind, list);
25193 return list;
25196 /* OpenMP 3.0:
25197 collapse ( constant-expression ) */
25199 static tree
25200 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25202 tree c, num;
25203 location_t loc;
25204 HOST_WIDE_INT n;
25206 loc = cp_lexer_peek_token (parser->lexer)->location;
25207 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25208 return list;
25210 num = cp_parser_constant_expression (parser, false, NULL);
25212 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25213 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25214 /*or_comma=*/false,
25215 /*consume_paren=*/true);
25217 if (num == error_mark_node)
25218 return list;
25219 num = fold_non_dependent_expr (num);
25220 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25221 || !host_integerp (num, 0)
25222 || (n = tree_low_cst (num, 0)) <= 0
25223 || (int) n != n)
25225 error_at (loc, "collapse argument needs positive constant integer expression");
25226 return list;
25229 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25230 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25231 OMP_CLAUSE_CHAIN (c) = list;
25232 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25234 return c;
25237 /* OpenMP 2.5:
25238 default ( shared | none ) */
25240 static tree
25241 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25243 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25244 tree c;
25246 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25247 return list;
25248 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25250 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25251 const char *p = IDENTIFIER_POINTER (id);
25253 switch (p[0])
25255 case 'n':
25256 if (strcmp ("none", p) != 0)
25257 goto invalid_kind;
25258 kind = OMP_CLAUSE_DEFAULT_NONE;
25259 break;
25261 case 's':
25262 if (strcmp ("shared", p) != 0)
25263 goto invalid_kind;
25264 kind = OMP_CLAUSE_DEFAULT_SHARED;
25265 break;
25267 default:
25268 goto invalid_kind;
25271 cp_lexer_consume_token (parser->lexer);
25273 else
25275 invalid_kind:
25276 cp_parser_error (parser, "expected %<none%> or %<shared%>");
25279 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25280 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25281 /*or_comma=*/false,
25282 /*consume_paren=*/true);
25284 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25285 return list;
25287 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25288 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25289 OMP_CLAUSE_CHAIN (c) = list;
25290 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25292 return c;
25295 /* OpenMP 3.1:
25296 final ( expression ) */
25298 static tree
25299 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25301 tree t, c;
25303 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25304 return list;
25306 t = cp_parser_condition (parser);
25308 if (t == error_mark_node
25309 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25310 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25311 /*or_comma=*/false,
25312 /*consume_paren=*/true);
25314 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25316 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25317 OMP_CLAUSE_FINAL_EXPR (c) = t;
25318 OMP_CLAUSE_CHAIN (c) = list;
25320 return c;
25323 /* OpenMP 2.5:
25324 if ( expression ) */
25326 static tree
25327 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25329 tree t, c;
25331 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25332 return list;
25334 t = cp_parser_condition (parser);
25336 if (t == error_mark_node
25337 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25338 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25339 /*or_comma=*/false,
25340 /*consume_paren=*/true);
25342 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25344 c = build_omp_clause (location, OMP_CLAUSE_IF);
25345 OMP_CLAUSE_IF_EXPR (c) = t;
25346 OMP_CLAUSE_CHAIN (c) = list;
25348 return c;
25351 /* OpenMP 3.1:
25352 mergeable */
25354 static tree
25355 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
25356 tree list, location_t location)
25358 tree c;
25360 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25361 location);
25363 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25364 OMP_CLAUSE_CHAIN (c) = list;
25365 return c;
25368 /* OpenMP 2.5:
25369 nowait */
25371 static tree
25372 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
25373 tree list, location_t location)
25375 tree c;
25377 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25379 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25380 OMP_CLAUSE_CHAIN (c) = list;
25381 return c;
25384 /* OpenMP 2.5:
25385 num_threads ( expression ) */
25387 static tree
25388 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25389 location_t location)
25391 tree t, c;
25393 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25394 return list;
25396 t = cp_parser_expression (parser, false, NULL);
25398 if (t == error_mark_node
25399 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25400 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25401 /*or_comma=*/false,
25402 /*consume_paren=*/true);
25404 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25405 "num_threads", location);
25407 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25408 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25409 OMP_CLAUSE_CHAIN (c) = list;
25411 return c;
25414 /* OpenMP 2.5:
25415 ordered */
25417 static tree
25418 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25419 tree list, location_t location)
25421 tree c;
25423 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25424 "ordered", location);
25426 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25427 OMP_CLAUSE_CHAIN (c) = list;
25428 return c;
25431 /* OpenMP 2.5:
25432 reduction ( reduction-operator : variable-list )
25434 reduction-operator:
25435 One of: + * - & ^ | && ||
25437 OpenMP 3.1:
25439 reduction-operator:
25440 One of: + * - & ^ | && || min max */
25442 static tree
25443 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25445 enum tree_code code;
25446 tree nlist, c;
25448 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25449 return list;
25451 switch (cp_lexer_peek_token (parser->lexer)->type)
25453 case CPP_PLUS:
25454 code = PLUS_EXPR;
25455 break;
25456 case CPP_MULT:
25457 code = MULT_EXPR;
25458 break;
25459 case CPP_MINUS:
25460 code = MINUS_EXPR;
25461 break;
25462 case CPP_AND:
25463 code = BIT_AND_EXPR;
25464 break;
25465 case CPP_XOR:
25466 code = BIT_XOR_EXPR;
25467 break;
25468 case CPP_OR:
25469 code = BIT_IOR_EXPR;
25470 break;
25471 case CPP_AND_AND:
25472 code = TRUTH_ANDIF_EXPR;
25473 break;
25474 case CPP_OR_OR:
25475 code = TRUTH_ORIF_EXPR;
25476 break;
25477 case CPP_NAME:
25479 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25480 const char *p = IDENTIFIER_POINTER (id);
25482 if (strcmp (p, "min") == 0)
25484 code = MIN_EXPR;
25485 break;
25487 if (strcmp (p, "max") == 0)
25489 code = MAX_EXPR;
25490 break;
25493 /* FALLTHROUGH */
25494 default:
25495 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25496 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25497 resync_fail:
25498 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25499 /*or_comma=*/false,
25500 /*consume_paren=*/true);
25501 return list;
25503 cp_lexer_consume_token (parser->lexer);
25505 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25506 goto resync_fail;
25508 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25509 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25510 OMP_CLAUSE_REDUCTION_CODE (c) = code;
25512 return nlist;
25515 /* OpenMP 2.5:
25516 schedule ( schedule-kind )
25517 schedule ( schedule-kind , expression )
25519 schedule-kind:
25520 static | dynamic | guided | runtime | auto */
25522 static tree
25523 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25525 tree c, t;
25527 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25528 return list;
25530 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25532 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25534 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25535 const char *p = IDENTIFIER_POINTER (id);
25537 switch (p[0])
25539 case 'd':
25540 if (strcmp ("dynamic", p) != 0)
25541 goto invalid_kind;
25542 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25543 break;
25545 case 'g':
25546 if (strcmp ("guided", p) != 0)
25547 goto invalid_kind;
25548 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25549 break;
25551 case 'r':
25552 if (strcmp ("runtime", p) != 0)
25553 goto invalid_kind;
25554 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25555 break;
25557 default:
25558 goto invalid_kind;
25561 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25562 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25563 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25564 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25565 else
25566 goto invalid_kind;
25567 cp_lexer_consume_token (parser->lexer);
25569 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25571 cp_token *token;
25572 cp_lexer_consume_token (parser->lexer);
25574 token = cp_lexer_peek_token (parser->lexer);
25575 t = cp_parser_assignment_expression (parser, false, NULL);
25577 if (t == error_mark_node)
25578 goto resync_fail;
25579 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25580 error_at (token->location, "schedule %<runtime%> does not take "
25581 "a %<chunk_size%> parameter");
25582 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25583 error_at (token->location, "schedule %<auto%> does not take "
25584 "a %<chunk_size%> parameter");
25585 else
25586 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25588 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25589 goto resync_fail;
25591 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25592 goto resync_fail;
25594 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25595 OMP_CLAUSE_CHAIN (c) = list;
25596 return c;
25598 invalid_kind:
25599 cp_parser_error (parser, "invalid schedule kind");
25600 resync_fail:
25601 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25602 /*or_comma=*/false,
25603 /*consume_paren=*/true);
25604 return list;
25607 /* OpenMP 3.0:
25608 untied */
25610 static tree
25611 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25612 tree list, location_t location)
25614 tree c;
25616 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25618 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25619 OMP_CLAUSE_CHAIN (c) = list;
25620 return c;
25623 /* Parse all OpenMP clauses. The set clauses allowed by the directive
25624 is a bitmask in MASK. Return the list of clauses found; the result
25625 of clause default goes in *pdefault. */
25627 static tree
25628 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25629 const char *where, cp_token *pragma_tok)
25631 tree clauses = NULL;
25632 bool first = true;
25633 cp_token *token = NULL;
25635 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25637 pragma_omp_clause c_kind;
25638 const char *c_name;
25639 tree prev = clauses;
25641 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25642 cp_lexer_consume_token (parser->lexer);
25644 token = cp_lexer_peek_token (parser->lexer);
25645 c_kind = cp_parser_omp_clause_name (parser);
25646 first = false;
25648 switch (c_kind)
25650 case PRAGMA_OMP_CLAUSE_COLLAPSE:
25651 clauses = cp_parser_omp_clause_collapse (parser, clauses,
25652 token->location);
25653 c_name = "collapse";
25654 break;
25655 case PRAGMA_OMP_CLAUSE_COPYIN:
25656 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25657 c_name = "copyin";
25658 break;
25659 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25660 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25661 clauses);
25662 c_name = "copyprivate";
25663 break;
25664 case PRAGMA_OMP_CLAUSE_DEFAULT:
25665 clauses = cp_parser_omp_clause_default (parser, clauses,
25666 token->location);
25667 c_name = "default";
25668 break;
25669 case PRAGMA_OMP_CLAUSE_FINAL:
25670 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25671 c_name = "final";
25672 break;
25673 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25674 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25675 clauses);
25676 c_name = "firstprivate";
25677 break;
25678 case PRAGMA_OMP_CLAUSE_IF:
25679 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25680 c_name = "if";
25681 break;
25682 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25683 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25684 clauses);
25685 c_name = "lastprivate";
25686 break;
25687 case PRAGMA_OMP_CLAUSE_MERGEABLE:
25688 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25689 token->location);
25690 c_name = "mergeable";
25691 break;
25692 case PRAGMA_OMP_CLAUSE_NOWAIT:
25693 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25694 c_name = "nowait";
25695 break;
25696 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25697 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25698 token->location);
25699 c_name = "num_threads";
25700 break;
25701 case PRAGMA_OMP_CLAUSE_ORDERED:
25702 clauses = cp_parser_omp_clause_ordered (parser, clauses,
25703 token->location);
25704 c_name = "ordered";
25705 break;
25706 case PRAGMA_OMP_CLAUSE_PRIVATE:
25707 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25708 clauses);
25709 c_name = "private";
25710 break;
25711 case PRAGMA_OMP_CLAUSE_REDUCTION:
25712 clauses = cp_parser_omp_clause_reduction (parser, clauses);
25713 c_name = "reduction";
25714 break;
25715 case PRAGMA_OMP_CLAUSE_SCHEDULE:
25716 clauses = cp_parser_omp_clause_schedule (parser, clauses,
25717 token->location);
25718 c_name = "schedule";
25719 break;
25720 case PRAGMA_OMP_CLAUSE_SHARED:
25721 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25722 clauses);
25723 c_name = "shared";
25724 break;
25725 case PRAGMA_OMP_CLAUSE_UNTIED:
25726 clauses = cp_parser_omp_clause_untied (parser, clauses,
25727 token->location);
25728 c_name = "nowait";
25729 break;
25730 default:
25731 cp_parser_error (parser, "expected %<#pragma omp%> clause");
25732 goto saw_error;
25735 if (((mask >> c_kind) & 1) == 0)
25737 /* Remove the invalid clause(s) from the list to avoid
25738 confusing the rest of the compiler. */
25739 clauses = prev;
25740 error_at (token->location, "%qs is not valid for %qs", c_name, where);
25743 saw_error:
25744 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25745 return finish_omp_clauses (clauses);
25748 /* OpenMP 2.5:
25749 structured-block:
25750 statement
25752 In practice, we're also interested in adding the statement to an
25753 outer node. So it is convenient if we work around the fact that
25754 cp_parser_statement calls add_stmt. */
25756 static unsigned
25757 cp_parser_begin_omp_structured_block (cp_parser *parser)
25759 unsigned save = parser->in_statement;
25761 /* Only move the values to IN_OMP_BLOCK if they weren't false.
25762 This preserves the "not within loop or switch" style error messages
25763 for nonsense cases like
25764 void foo() {
25765 #pragma omp single
25766 break;
25769 if (parser->in_statement)
25770 parser->in_statement = IN_OMP_BLOCK;
25772 return save;
25775 static void
25776 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25778 parser->in_statement = save;
25781 static tree
25782 cp_parser_omp_structured_block (cp_parser *parser)
25784 tree stmt = begin_omp_structured_block ();
25785 unsigned int save = cp_parser_begin_omp_structured_block (parser);
25787 cp_parser_statement (parser, NULL_TREE, false, NULL);
25789 cp_parser_end_omp_structured_block (parser, save);
25790 return finish_omp_structured_block (stmt);
25793 /* OpenMP 2.5:
25794 # pragma omp atomic new-line
25795 expression-stmt
25797 expression-stmt:
25798 x binop= expr | x++ | ++x | x-- | --x
25799 binop:
25800 +, *, -, /, &, ^, |, <<, >>
25802 where x is an lvalue expression with scalar type.
25804 OpenMP 3.1:
25805 # pragma omp atomic new-line
25806 update-stmt
25808 # pragma omp atomic read new-line
25809 read-stmt
25811 # pragma omp atomic write new-line
25812 write-stmt
25814 # pragma omp atomic update new-line
25815 update-stmt
25817 # pragma omp atomic capture new-line
25818 capture-stmt
25820 # pragma omp atomic capture new-line
25821 capture-block
25823 read-stmt:
25824 v = x
25825 write-stmt:
25826 x = expr
25827 update-stmt:
25828 expression-stmt | x = x binop expr
25829 capture-stmt:
25830 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25831 capture-block:
25832 { v = x; update-stmt; } | { update-stmt; v = x; }
25834 where x and v are lvalue expressions with scalar type. */
25836 static void
25837 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25839 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25840 tree rhs1 = NULL_TREE, orig_lhs;
25841 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25842 bool structured_block = false;
25844 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25846 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25847 const char *p = IDENTIFIER_POINTER (id);
25849 if (!strcmp (p, "read"))
25850 code = OMP_ATOMIC_READ;
25851 else if (!strcmp (p, "write"))
25852 code = NOP_EXPR;
25853 else if (!strcmp (p, "update"))
25854 code = OMP_ATOMIC;
25855 else if (!strcmp (p, "capture"))
25856 code = OMP_ATOMIC_CAPTURE_NEW;
25857 else
25858 p = NULL;
25859 if (p)
25860 cp_lexer_consume_token (parser->lexer);
25862 cp_parser_require_pragma_eol (parser, pragma_tok);
25864 switch (code)
25866 case OMP_ATOMIC_READ:
25867 case NOP_EXPR: /* atomic write */
25868 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25869 /*cast_p=*/false, NULL);
25870 if (v == error_mark_node)
25871 goto saw_error;
25872 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25873 goto saw_error;
25874 if (code == NOP_EXPR)
25875 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25876 else
25877 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25878 /*cast_p=*/false, NULL);
25879 if (lhs == error_mark_node)
25880 goto saw_error;
25881 if (code == NOP_EXPR)
25883 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25884 opcode. */
25885 code = OMP_ATOMIC;
25886 rhs = lhs;
25887 lhs = v;
25888 v = NULL_TREE;
25890 goto done;
25891 case OMP_ATOMIC_CAPTURE_NEW:
25892 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25894 cp_lexer_consume_token (parser->lexer);
25895 structured_block = true;
25897 else
25899 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25900 /*cast_p=*/false, NULL);
25901 if (v == error_mark_node)
25902 goto saw_error;
25903 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25904 goto saw_error;
25906 default:
25907 break;
25910 restart:
25911 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25912 /*cast_p=*/false, NULL);
25913 orig_lhs = lhs;
25914 switch (TREE_CODE (lhs))
25916 case ERROR_MARK:
25917 goto saw_error;
25919 case POSTINCREMENT_EXPR:
25920 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25921 code = OMP_ATOMIC_CAPTURE_OLD;
25922 /* FALLTHROUGH */
25923 case PREINCREMENT_EXPR:
25924 lhs = TREE_OPERAND (lhs, 0);
25925 opcode = PLUS_EXPR;
25926 rhs = integer_one_node;
25927 break;
25929 case POSTDECREMENT_EXPR:
25930 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25931 code = OMP_ATOMIC_CAPTURE_OLD;
25932 /* FALLTHROUGH */
25933 case PREDECREMENT_EXPR:
25934 lhs = TREE_OPERAND (lhs, 0);
25935 opcode = MINUS_EXPR;
25936 rhs = integer_one_node;
25937 break;
25939 case COMPOUND_EXPR:
25940 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25941 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25942 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25943 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25944 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25945 (TREE_OPERAND (lhs, 1), 0), 0)))
25946 == BOOLEAN_TYPE)
25947 /* Undo effects of boolean_increment for post {in,de}crement. */
25948 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25949 /* FALLTHRU */
25950 case MODIFY_EXPR:
25951 if (TREE_CODE (lhs) == MODIFY_EXPR
25952 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25954 /* Undo effects of boolean_increment. */
25955 if (integer_onep (TREE_OPERAND (lhs, 1)))
25957 /* This is pre or post increment. */
25958 rhs = TREE_OPERAND (lhs, 1);
25959 lhs = TREE_OPERAND (lhs, 0);
25960 opcode = NOP_EXPR;
25961 if (code == OMP_ATOMIC_CAPTURE_NEW
25962 && !structured_block
25963 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25964 code = OMP_ATOMIC_CAPTURE_OLD;
25965 break;
25968 /* FALLTHRU */
25969 default:
25970 switch (cp_lexer_peek_token (parser->lexer)->type)
25972 case CPP_MULT_EQ:
25973 opcode = MULT_EXPR;
25974 break;
25975 case CPP_DIV_EQ:
25976 opcode = TRUNC_DIV_EXPR;
25977 break;
25978 case CPP_PLUS_EQ:
25979 opcode = PLUS_EXPR;
25980 break;
25981 case CPP_MINUS_EQ:
25982 opcode = MINUS_EXPR;
25983 break;
25984 case CPP_LSHIFT_EQ:
25985 opcode = LSHIFT_EXPR;
25986 break;
25987 case CPP_RSHIFT_EQ:
25988 opcode = RSHIFT_EXPR;
25989 break;
25990 case CPP_AND_EQ:
25991 opcode = BIT_AND_EXPR;
25992 break;
25993 case CPP_OR_EQ:
25994 opcode = BIT_IOR_EXPR;
25995 break;
25996 case CPP_XOR_EQ:
25997 opcode = BIT_XOR_EXPR;
25998 break;
25999 case CPP_EQ:
26000 if (structured_block || code == OMP_ATOMIC)
26002 enum cp_parser_prec oprec;
26003 cp_token *token;
26004 cp_lexer_consume_token (parser->lexer);
26005 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26006 /*cast_p=*/false, NULL);
26007 if (rhs1 == error_mark_node)
26008 goto saw_error;
26009 token = cp_lexer_peek_token (parser->lexer);
26010 switch (token->type)
26012 case CPP_SEMICOLON:
26013 if (code == OMP_ATOMIC_CAPTURE_NEW)
26015 code = OMP_ATOMIC_CAPTURE_OLD;
26016 v = lhs;
26017 lhs = NULL_TREE;
26018 lhs1 = rhs1;
26019 rhs1 = NULL_TREE;
26020 cp_lexer_consume_token (parser->lexer);
26021 goto restart;
26023 cp_parser_error (parser,
26024 "invalid form of %<#pragma omp atomic%>");
26025 goto saw_error;
26026 case CPP_MULT:
26027 opcode = MULT_EXPR;
26028 break;
26029 case CPP_DIV:
26030 opcode = TRUNC_DIV_EXPR;
26031 break;
26032 case CPP_PLUS:
26033 opcode = PLUS_EXPR;
26034 break;
26035 case CPP_MINUS:
26036 opcode = MINUS_EXPR;
26037 break;
26038 case CPP_LSHIFT:
26039 opcode = LSHIFT_EXPR;
26040 break;
26041 case CPP_RSHIFT:
26042 opcode = RSHIFT_EXPR;
26043 break;
26044 case CPP_AND:
26045 opcode = BIT_AND_EXPR;
26046 break;
26047 case CPP_OR:
26048 opcode = BIT_IOR_EXPR;
26049 break;
26050 case CPP_XOR:
26051 opcode = BIT_XOR_EXPR;
26052 break;
26053 default:
26054 cp_parser_error (parser,
26055 "invalid operator for %<#pragma omp atomic%>");
26056 goto saw_error;
26058 oprec = TOKEN_PRECEDENCE (token);
26059 gcc_assert (oprec != PREC_NOT_OPERATOR);
26060 if (commutative_tree_code (opcode))
26061 oprec = (enum cp_parser_prec) (oprec - 1);
26062 cp_lexer_consume_token (parser->lexer);
26063 rhs = cp_parser_binary_expression (parser, false, false,
26064 oprec, NULL);
26065 if (rhs == error_mark_node)
26066 goto saw_error;
26067 goto stmt_done;
26069 /* FALLTHROUGH */
26070 default:
26071 cp_parser_error (parser,
26072 "invalid operator for %<#pragma omp atomic%>");
26073 goto saw_error;
26075 cp_lexer_consume_token (parser->lexer);
26077 rhs = cp_parser_expression (parser, false, NULL);
26078 if (rhs == error_mark_node)
26079 goto saw_error;
26080 break;
26082 stmt_done:
26083 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
26085 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26086 goto saw_error;
26087 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26088 /*cast_p=*/false, NULL);
26089 if (v == error_mark_node)
26090 goto saw_error;
26091 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26092 goto saw_error;
26093 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26094 /*cast_p=*/false, NULL);
26095 if (lhs1 == error_mark_node)
26096 goto saw_error;
26098 if (structured_block)
26100 cp_parser_consume_semicolon_at_end_of_statement (parser);
26101 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26103 done:
26104 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
26105 if (!structured_block)
26106 cp_parser_consume_semicolon_at_end_of_statement (parser);
26107 return;
26109 saw_error:
26110 cp_parser_skip_to_end_of_block_or_statement (parser);
26111 if (structured_block)
26113 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26114 cp_lexer_consume_token (parser->lexer);
26115 else if (code == OMP_ATOMIC_CAPTURE_NEW)
26117 cp_parser_skip_to_end_of_block_or_statement (parser);
26118 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26119 cp_lexer_consume_token (parser->lexer);
26125 /* OpenMP 2.5:
26126 # pragma omp barrier new-line */
26128 static void
26129 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
26131 cp_parser_require_pragma_eol (parser, pragma_tok);
26132 finish_omp_barrier ();
26135 /* OpenMP 2.5:
26136 # pragma omp critical [(name)] new-line
26137 structured-block */
26139 static tree
26140 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
26142 tree stmt, name = NULL;
26144 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26146 cp_lexer_consume_token (parser->lexer);
26148 name = cp_parser_identifier (parser);
26150 if (name == error_mark_node
26151 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26152 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26153 /*or_comma=*/false,
26154 /*consume_paren=*/true);
26155 if (name == error_mark_node)
26156 name = NULL;
26158 cp_parser_require_pragma_eol (parser, pragma_tok);
26160 stmt = cp_parser_omp_structured_block (parser);
26161 return c_finish_omp_critical (input_location, stmt, name);
26164 /* OpenMP 2.5:
26165 # pragma omp flush flush-vars[opt] new-line
26167 flush-vars:
26168 ( variable-list ) */
26170 static void
26171 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26173 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26174 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26175 cp_parser_require_pragma_eol (parser, pragma_tok);
26177 finish_omp_flush ();
26180 /* Helper function, to parse omp for increment expression. */
26182 static tree
26183 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26185 tree cond = cp_parser_binary_expression (parser, false, true,
26186 PREC_NOT_OPERATOR, NULL);
26187 if (cond == error_mark_node
26188 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26190 cp_parser_skip_to_end_of_statement (parser);
26191 return error_mark_node;
26194 switch (TREE_CODE (cond))
26196 case GT_EXPR:
26197 case GE_EXPR:
26198 case LT_EXPR:
26199 case LE_EXPR:
26200 break;
26201 default:
26202 return error_mark_node;
26205 /* If decl is an iterator, preserve LHS and RHS of the relational
26206 expr until finish_omp_for. */
26207 if (decl
26208 && (type_dependent_expression_p (decl)
26209 || CLASS_TYPE_P (TREE_TYPE (decl))))
26210 return cond;
26212 return build_x_binary_op (input_location, TREE_CODE (cond),
26213 TREE_OPERAND (cond, 0), ERROR_MARK,
26214 TREE_OPERAND (cond, 1), ERROR_MARK,
26215 /*overload=*/NULL, tf_warning_or_error);
26218 /* Helper function, to parse omp for increment expression. */
26220 static tree
26221 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26223 cp_token *token = cp_lexer_peek_token (parser->lexer);
26224 enum tree_code op;
26225 tree lhs, rhs;
26226 cp_id_kind idk;
26227 bool decl_first;
26229 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26231 op = (token->type == CPP_PLUS_PLUS
26232 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26233 cp_lexer_consume_token (parser->lexer);
26234 lhs = cp_parser_cast_expression (parser, false, false, NULL);
26235 if (lhs != decl)
26236 return error_mark_node;
26237 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26240 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26241 if (lhs != decl)
26242 return error_mark_node;
26244 token = cp_lexer_peek_token (parser->lexer);
26245 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26247 op = (token->type == CPP_PLUS_PLUS
26248 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26249 cp_lexer_consume_token (parser->lexer);
26250 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26253 op = cp_parser_assignment_operator_opt (parser);
26254 if (op == ERROR_MARK)
26255 return error_mark_node;
26257 if (op != NOP_EXPR)
26259 rhs = cp_parser_assignment_expression (parser, false, NULL);
26260 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26261 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26264 lhs = cp_parser_binary_expression (parser, false, false,
26265 PREC_ADDITIVE_EXPRESSION, NULL);
26266 token = cp_lexer_peek_token (parser->lexer);
26267 decl_first = lhs == decl;
26268 if (decl_first)
26269 lhs = NULL_TREE;
26270 if (token->type != CPP_PLUS
26271 && token->type != CPP_MINUS)
26272 return error_mark_node;
26276 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26277 cp_lexer_consume_token (parser->lexer);
26278 rhs = cp_parser_binary_expression (parser, false, false,
26279 PREC_ADDITIVE_EXPRESSION, NULL);
26280 token = cp_lexer_peek_token (parser->lexer);
26281 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26283 if (lhs == NULL_TREE)
26285 if (op == PLUS_EXPR)
26286 lhs = rhs;
26287 else
26288 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
26289 tf_warning_or_error);
26291 else
26292 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
26293 ERROR_MARK, NULL, tf_warning_or_error);
26296 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26298 if (!decl_first)
26300 if (rhs != decl || op == MINUS_EXPR)
26301 return error_mark_node;
26302 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26304 else
26305 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26307 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26310 /* Parse the restricted form of the for statement allowed by OpenMP. */
26312 static tree
26313 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26315 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26316 tree real_decl, initv, condv, incrv, declv;
26317 tree this_pre_body, cl;
26318 location_t loc_first;
26319 bool collapse_err = false;
26320 int i, collapse = 1, nbraces = 0;
26321 VEC(tree,gc) *for_block = make_tree_vector ();
26323 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26324 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26325 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26327 gcc_assert (collapse >= 1);
26329 declv = make_tree_vec (collapse);
26330 initv = make_tree_vec (collapse);
26331 condv = make_tree_vec (collapse);
26332 incrv = make_tree_vec (collapse);
26334 loc_first = cp_lexer_peek_token (parser->lexer)->location;
26336 for (i = 0; i < collapse; i++)
26338 int bracecount = 0;
26339 bool add_private_clause = false;
26340 location_t loc;
26342 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26344 cp_parser_error (parser, "for statement expected");
26345 return NULL;
26347 loc = cp_lexer_consume_token (parser->lexer)->location;
26349 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26350 return NULL;
26352 init = decl = real_decl = NULL;
26353 this_pre_body = push_stmt_list ();
26354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26356 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26358 init-expr:
26359 var = lb
26360 integer-type var = lb
26361 random-access-iterator-type var = lb
26362 pointer-type var = lb
26364 cp_decl_specifier_seq type_specifiers;
26366 /* First, try to parse as an initialized declaration. See
26367 cp_parser_condition, from whence the bulk of this is copied. */
26369 cp_parser_parse_tentatively (parser);
26370 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26371 /*is_trailing_return=*/false,
26372 &type_specifiers);
26373 if (cp_parser_parse_definitely (parser))
26375 /* If parsing a type specifier seq succeeded, then this
26376 MUST be a initialized declaration. */
26377 tree asm_specification, attributes;
26378 cp_declarator *declarator;
26380 declarator = cp_parser_declarator (parser,
26381 CP_PARSER_DECLARATOR_NAMED,
26382 /*ctor_dtor_or_conv_p=*/NULL,
26383 /*parenthesized_p=*/NULL,
26384 /*member_p=*/false);
26385 attributes = cp_parser_attributes_opt (parser);
26386 asm_specification = cp_parser_asm_specification_opt (parser);
26388 if (declarator == cp_error_declarator)
26389 cp_parser_skip_to_end_of_statement (parser);
26391 else
26393 tree pushed_scope, auto_node;
26395 decl = start_decl (declarator, &type_specifiers,
26396 SD_INITIALIZED, attributes,
26397 /*prefix_attributes=*/NULL_TREE,
26398 &pushed_scope);
26400 auto_node = type_uses_auto (TREE_TYPE (decl));
26401 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26403 if (cp_lexer_next_token_is (parser->lexer,
26404 CPP_OPEN_PAREN))
26405 error ("parenthesized initialization is not allowed in "
26406 "OpenMP %<for%> loop");
26407 else
26408 /* Trigger an error. */
26409 cp_parser_require (parser, CPP_EQ, RT_EQ);
26411 init = error_mark_node;
26412 cp_parser_skip_to_end_of_statement (parser);
26414 else if (CLASS_TYPE_P (TREE_TYPE (decl))
26415 || type_dependent_expression_p (decl)
26416 || auto_node)
26418 bool is_direct_init, is_non_constant_init;
26420 init = cp_parser_initializer (parser,
26421 &is_direct_init,
26422 &is_non_constant_init);
26424 if (auto_node)
26426 TREE_TYPE (decl)
26427 = do_auto_deduction (TREE_TYPE (decl), init,
26428 auto_node);
26430 if (!CLASS_TYPE_P (TREE_TYPE (decl))
26431 && !type_dependent_expression_p (decl))
26432 goto non_class;
26435 cp_finish_decl (decl, init, !is_non_constant_init,
26436 asm_specification,
26437 LOOKUP_ONLYCONVERTING);
26438 if (CLASS_TYPE_P (TREE_TYPE (decl)))
26440 VEC_safe_push (tree, gc, for_block, this_pre_body);
26441 init = NULL_TREE;
26443 else
26444 init = pop_stmt_list (this_pre_body);
26445 this_pre_body = NULL_TREE;
26447 else
26449 /* Consume '='. */
26450 cp_lexer_consume_token (parser->lexer);
26451 init = cp_parser_assignment_expression (parser, false, NULL);
26453 non_class:
26454 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26455 init = error_mark_node;
26456 else
26457 cp_finish_decl (decl, NULL_TREE,
26458 /*init_const_expr_p=*/false,
26459 asm_specification,
26460 LOOKUP_ONLYCONVERTING);
26463 if (pushed_scope)
26464 pop_scope (pushed_scope);
26467 else
26469 cp_id_kind idk;
26470 /* If parsing a type specifier sequence failed, then
26471 this MUST be a simple expression. */
26472 cp_parser_parse_tentatively (parser);
26473 decl = cp_parser_primary_expression (parser, false, false,
26474 false, &idk);
26475 if (!cp_parser_error_occurred (parser)
26476 && decl
26477 && DECL_P (decl)
26478 && CLASS_TYPE_P (TREE_TYPE (decl)))
26480 tree rhs;
26482 cp_parser_parse_definitely (parser);
26483 cp_parser_require (parser, CPP_EQ, RT_EQ);
26484 rhs = cp_parser_assignment_expression (parser, false, NULL);
26485 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
26486 decl, NOP_EXPR,
26487 rhs,
26488 tf_warning_or_error));
26489 add_private_clause = true;
26491 else
26493 decl = NULL;
26494 cp_parser_abort_tentative_parse (parser);
26495 init = cp_parser_expression (parser, false, NULL);
26496 if (init)
26498 if (TREE_CODE (init) == MODIFY_EXPR
26499 || TREE_CODE (init) == MODOP_EXPR)
26500 real_decl = TREE_OPERAND (init, 0);
26505 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26506 if (this_pre_body)
26508 this_pre_body = pop_stmt_list (this_pre_body);
26509 if (pre_body)
26511 tree t = pre_body;
26512 pre_body = push_stmt_list ();
26513 add_stmt (t);
26514 add_stmt (this_pre_body);
26515 pre_body = pop_stmt_list (pre_body);
26517 else
26518 pre_body = this_pre_body;
26521 if (decl)
26522 real_decl = decl;
26523 if (par_clauses != NULL && real_decl != NULL_TREE)
26525 tree *c;
26526 for (c = par_clauses; *c ; )
26527 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26528 && OMP_CLAUSE_DECL (*c) == real_decl)
26530 error_at (loc, "iteration variable %qD"
26531 " should not be firstprivate", real_decl);
26532 *c = OMP_CLAUSE_CHAIN (*c);
26534 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26535 && OMP_CLAUSE_DECL (*c) == real_decl)
26537 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26538 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
26539 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26540 OMP_CLAUSE_DECL (l) = real_decl;
26541 OMP_CLAUSE_CHAIN (l) = clauses;
26542 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26543 clauses = l;
26544 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26545 CP_OMP_CLAUSE_INFO (*c) = NULL;
26546 add_private_clause = false;
26548 else
26550 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26551 && OMP_CLAUSE_DECL (*c) == real_decl)
26552 add_private_clause = false;
26553 c = &OMP_CLAUSE_CHAIN (*c);
26557 if (add_private_clause)
26559 tree c;
26560 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26562 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26563 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26564 && OMP_CLAUSE_DECL (c) == decl)
26565 break;
26566 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26567 && OMP_CLAUSE_DECL (c) == decl)
26568 error_at (loc, "iteration variable %qD "
26569 "should not be firstprivate",
26570 decl);
26571 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26572 && OMP_CLAUSE_DECL (c) == decl)
26573 error_at (loc, "iteration variable %qD should not be reduction",
26574 decl);
26576 if (c == NULL)
26578 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26579 OMP_CLAUSE_DECL (c) = decl;
26580 c = finish_omp_clauses (c);
26581 if (c)
26583 OMP_CLAUSE_CHAIN (c) = clauses;
26584 clauses = c;
26589 cond = NULL;
26590 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26591 cond = cp_parser_omp_for_cond (parser, decl);
26592 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26594 incr = NULL;
26595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26597 /* If decl is an iterator, preserve the operator on decl
26598 until finish_omp_for. */
26599 if (real_decl
26600 && ((processing_template_decl
26601 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
26602 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
26603 incr = cp_parser_omp_for_incr (parser, real_decl);
26604 else
26605 incr = cp_parser_expression (parser, false, NULL);
26606 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
26607 SET_EXPR_LOCATION (incr, input_location);
26610 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26611 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26612 /*or_comma=*/false,
26613 /*consume_paren=*/true);
26615 TREE_VEC_ELT (declv, i) = decl;
26616 TREE_VEC_ELT (initv, i) = init;
26617 TREE_VEC_ELT (condv, i) = cond;
26618 TREE_VEC_ELT (incrv, i) = incr;
26620 if (i == collapse - 1)
26621 break;
26623 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26624 in between the collapsed for loops to be still considered perfectly
26625 nested. Hopefully the final version clarifies this.
26626 For now handle (multiple) {'s and empty statements. */
26627 cp_parser_parse_tentatively (parser);
26630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26631 break;
26632 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26634 cp_lexer_consume_token (parser->lexer);
26635 bracecount++;
26637 else if (bracecount
26638 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26639 cp_lexer_consume_token (parser->lexer);
26640 else
26642 loc = cp_lexer_peek_token (parser->lexer)->location;
26643 error_at (loc, "not enough collapsed for loops");
26644 collapse_err = true;
26645 cp_parser_abort_tentative_parse (parser);
26646 declv = NULL_TREE;
26647 break;
26650 while (1);
26652 if (declv)
26654 cp_parser_parse_definitely (parser);
26655 nbraces += bracecount;
26659 /* Note that we saved the original contents of this flag when we entered
26660 the structured block, and so we don't need to re-save it here. */
26661 parser->in_statement = IN_OMP_FOR;
26663 /* Note that the grammar doesn't call for a structured block here,
26664 though the loop as a whole is a structured block. */
26665 body = push_stmt_list ();
26666 cp_parser_statement (parser, NULL_TREE, false, NULL);
26667 body = pop_stmt_list (body);
26669 if (declv == NULL_TREE)
26670 ret = NULL_TREE;
26671 else
26672 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26673 pre_body, clauses);
26675 while (nbraces)
26677 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26679 cp_lexer_consume_token (parser->lexer);
26680 nbraces--;
26682 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26683 cp_lexer_consume_token (parser->lexer);
26684 else
26686 if (!collapse_err)
26688 error_at (cp_lexer_peek_token (parser->lexer)->location,
26689 "collapsed loops not perfectly nested");
26691 collapse_err = true;
26692 cp_parser_statement_seq_opt (parser, NULL);
26693 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26694 break;
26698 while (!VEC_empty (tree, for_block))
26699 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26700 release_tree_vector (for_block);
26702 return ret;
26705 /* OpenMP 2.5:
26706 #pragma omp for for-clause[optseq] new-line
26707 for-loop */
26709 #define OMP_FOR_CLAUSE_MASK \
26710 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26711 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26712 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26713 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26714 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
26715 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
26716 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
26717 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26719 static tree
26720 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26722 tree clauses, sb, ret;
26723 unsigned int save;
26725 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26726 "#pragma omp for", pragma_tok);
26728 sb = begin_omp_structured_block ();
26729 save = cp_parser_begin_omp_structured_block (parser);
26731 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26733 cp_parser_end_omp_structured_block (parser, save);
26734 add_stmt (finish_omp_structured_block (sb));
26736 return ret;
26739 /* OpenMP 2.5:
26740 # pragma omp master new-line
26741 structured-block */
26743 static tree
26744 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26746 cp_parser_require_pragma_eol (parser, pragma_tok);
26747 return c_finish_omp_master (input_location,
26748 cp_parser_omp_structured_block (parser));
26751 /* OpenMP 2.5:
26752 # pragma omp ordered new-line
26753 structured-block */
26755 static tree
26756 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26758 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26759 cp_parser_require_pragma_eol (parser, pragma_tok);
26760 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26763 /* OpenMP 2.5:
26765 section-scope:
26766 { section-sequence }
26768 section-sequence:
26769 section-directive[opt] structured-block
26770 section-sequence section-directive structured-block */
26772 static tree
26773 cp_parser_omp_sections_scope (cp_parser *parser)
26775 tree stmt, substmt;
26776 bool error_suppress = false;
26777 cp_token *tok;
26779 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26780 return NULL_TREE;
26782 stmt = push_stmt_list ();
26784 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26786 unsigned save;
26788 substmt = begin_omp_structured_block ();
26789 save = cp_parser_begin_omp_structured_block (parser);
26791 while (1)
26793 cp_parser_statement (parser, NULL_TREE, false, NULL);
26795 tok = cp_lexer_peek_token (parser->lexer);
26796 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26797 break;
26798 if (tok->type == CPP_CLOSE_BRACE)
26799 break;
26800 if (tok->type == CPP_EOF)
26801 break;
26804 cp_parser_end_omp_structured_block (parser, save);
26805 substmt = finish_omp_structured_block (substmt);
26806 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26807 add_stmt (substmt);
26810 while (1)
26812 tok = cp_lexer_peek_token (parser->lexer);
26813 if (tok->type == CPP_CLOSE_BRACE)
26814 break;
26815 if (tok->type == CPP_EOF)
26816 break;
26818 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26820 cp_lexer_consume_token (parser->lexer);
26821 cp_parser_require_pragma_eol (parser, tok);
26822 error_suppress = false;
26824 else if (!error_suppress)
26826 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26827 error_suppress = true;
26830 substmt = cp_parser_omp_structured_block (parser);
26831 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26832 add_stmt (substmt);
26834 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26836 substmt = pop_stmt_list (stmt);
26838 stmt = make_node (OMP_SECTIONS);
26839 TREE_TYPE (stmt) = void_type_node;
26840 OMP_SECTIONS_BODY (stmt) = substmt;
26842 add_stmt (stmt);
26843 return stmt;
26846 /* OpenMP 2.5:
26847 # pragma omp sections sections-clause[optseq] newline
26848 sections-scope */
26850 #define OMP_SECTIONS_CLAUSE_MASK \
26851 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26852 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26853 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26854 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26855 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26857 static tree
26858 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26860 tree clauses, ret;
26862 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26863 "#pragma omp sections", pragma_tok);
26865 ret = cp_parser_omp_sections_scope (parser);
26866 if (ret)
26867 OMP_SECTIONS_CLAUSES (ret) = clauses;
26869 return ret;
26872 /* OpenMP 2.5:
26873 # pragma parallel parallel-clause new-line
26874 # pragma parallel for parallel-for-clause new-line
26875 # pragma parallel sections parallel-sections-clause new-line */
26877 #define OMP_PARALLEL_CLAUSE_MASK \
26878 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26879 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26880 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26881 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26882 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26883 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
26884 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26885 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26887 static tree
26888 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26890 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26891 const char *p_name = "#pragma omp parallel";
26892 tree stmt, clauses, par_clause, ws_clause, block;
26893 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26894 unsigned int save;
26895 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26897 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26899 cp_lexer_consume_token (parser->lexer);
26900 p_kind = PRAGMA_OMP_PARALLEL_FOR;
26901 p_name = "#pragma omp parallel for";
26902 mask |= OMP_FOR_CLAUSE_MASK;
26903 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26905 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26907 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26908 const char *p = IDENTIFIER_POINTER (id);
26909 if (strcmp (p, "sections") == 0)
26911 cp_lexer_consume_token (parser->lexer);
26912 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26913 p_name = "#pragma omp parallel sections";
26914 mask |= OMP_SECTIONS_CLAUSE_MASK;
26915 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26919 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26920 block = begin_omp_parallel ();
26921 save = cp_parser_begin_omp_structured_block (parser);
26923 switch (p_kind)
26925 case PRAGMA_OMP_PARALLEL:
26926 cp_parser_statement (parser, NULL_TREE, false, NULL);
26927 par_clause = clauses;
26928 break;
26930 case PRAGMA_OMP_PARALLEL_FOR:
26931 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26932 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26933 break;
26935 case PRAGMA_OMP_PARALLEL_SECTIONS:
26936 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26937 stmt = cp_parser_omp_sections_scope (parser);
26938 if (stmt)
26939 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26940 break;
26942 default:
26943 gcc_unreachable ();
26946 cp_parser_end_omp_structured_block (parser, save);
26947 stmt = finish_omp_parallel (par_clause, block);
26948 if (p_kind != PRAGMA_OMP_PARALLEL)
26949 OMP_PARALLEL_COMBINED (stmt) = 1;
26950 return stmt;
26953 /* OpenMP 2.5:
26954 # pragma omp single single-clause[optseq] new-line
26955 structured-block */
26957 #define OMP_SINGLE_CLAUSE_MASK \
26958 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26959 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26960 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
26961 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26963 static tree
26964 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26966 tree stmt = make_node (OMP_SINGLE);
26967 TREE_TYPE (stmt) = void_type_node;
26969 OMP_SINGLE_CLAUSES (stmt)
26970 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26971 "#pragma omp single", pragma_tok);
26972 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26974 return add_stmt (stmt);
26977 /* OpenMP 3.0:
26978 # pragma omp task task-clause[optseq] new-line
26979 structured-block */
26981 #define OMP_TASK_CLAUSE_MASK \
26982 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26983 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
26984 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26985 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26986 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26987 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26988 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
26989 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26991 static tree
26992 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26994 tree clauses, block;
26995 unsigned int save;
26997 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26998 "#pragma omp task", pragma_tok);
26999 block = begin_omp_task ();
27000 save = cp_parser_begin_omp_structured_block (parser);
27001 cp_parser_statement (parser, NULL_TREE, false, NULL);
27002 cp_parser_end_omp_structured_block (parser, save);
27003 return finish_omp_task (clauses, block);
27006 /* OpenMP 3.0:
27007 # pragma omp taskwait new-line */
27009 static void
27010 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
27012 cp_parser_require_pragma_eol (parser, pragma_tok);
27013 finish_omp_taskwait ();
27016 /* OpenMP 3.1:
27017 # pragma omp taskyield new-line */
27019 static void
27020 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
27022 cp_parser_require_pragma_eol (parser, pragma_tok);
27023 finish_omp_taskyield ();
27026 /* OpenMP 2.5:
27027 # pragma omp threadprivate (variable-list) */
27029 static void
27030 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
27032 tree vars;
27034 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27035 cp_parser_require_pragma_eol (parser, pragma_tok);
27037 finish_omp_threadprivate (vars);
27040 /* Main entry point to OpenMP statement pragmas. */
27042 static void
27043 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
27045 tree stmt;
27047 switch (pragma_tok->pragma_kind)
27049 case PRAGMA_OMP_ATOMIC:
27050 cp_parser_omp_atomic (parser, pragma_tok);
27051 return;
27052 case PRAGMA_OMP_CRITICAL:
27053 stmt = cp_parser_omp_critical (parser, pragma_tok);
27054 break;
27055 case PRAGMA_OMP_FOR:
27056 stmt = cp_parser_omp_for (parser, pragma_tok);
27057 break;
27058 case PRAGMA_OMP_MASTER:
27059 stmt = cp_parser_omp_master (parser, pragma_tok);
27060 break;
27061 case PRAGMA_OMP_ORDERED:
27062 stmt = cp_parser_omp_ordered (parser, pragma_tok);
27063 break;
27064 case PRAGMA_OMP_PARALLEL:
27065 stmt = cp_parser_omp_parallel (parser, pragma_tok);
27066 break;
27067 case PRAGMA_OMP_SECTIONS:
27068 stmt = cp_parser_omp_sections (parser, pragma_tok);
27069 break;
27070 case PRAGMA_OMP_SINGLE:
27071 stmt = cp_parser_omp_single (parser, pragma_tok);
27072 break;
27073 case PRAGMA_OMP_TASK:
27074 stmt = cp_parser_omp_task (parser, pragma_tok);
27075 break;
27076 default:
27077 gcc_unreachable ();
27080 if (stmt)
27081 SET_EXPR_LOCATION (stmt, pragma_tok->location);
27084 /* Transactional Memory parsing routines. */
27086 /* Parse a transaction attribute.
27088 txn-attribute:
27089 attribute
27090 [ [ identifier ] ]
27092 ??? Simplify this when C++0x bracket attributes are
27093 implemented properly. */
27095 static tree
27096 cp_parser_txn_attribute_opt (cp_parser *parser)
27098 cp_token *token;
27099 tree attr_name, attr = NULL;
27101 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27102 return cp_parser_attributes_opt (parser);
27104 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
27105 return NULL_TREE;
27106 cp_lexer_consume_token (parser->lexer);
27107 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
27108 goto error1;
27110 token = cp_lexer_peek_token (parser->lexer);
27111 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
27113 token = cp_lexer_consume_token (parser->lexer);
27115 attr_name = (token->type == CPP_KEYWORD
27116 /* For keywords, use the canonical spelling,
27117 not the parsed identifier. */
27118 ? ridpointers[(int) token->keyword]
27119 : token->u.value);
27120 attr = build_tree_list (attr_name, NULL_TREE);
27122 else
27123 cp_parser_error (parser, "expected identifier");
27125 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27126 error1:
27127 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27128 return attr;
27131 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27133 transaction-statement:
27134 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27135 compound-statement
27136 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27139 static tree
27140 cp_parser_transaction (cp_parser *parser, enum rid keyword)
27142 unsigned char old_in = parser->in_transaction;
27143 unsigned char this_in = 1, new_in;
27144 cp_token *token;
27145 tree stmt, attrs, noex;
27147 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27148 || keyword == RID_TRANSACTION_RELAXED);
27149 token = cp_parser_require_keyword (parser, keyword,
27150 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27151 : RT_TRANSACTION_RELAXED));
27152 gcc_assert (token != NULL);
27154 if (keyword == RID_TRANSACTION_RELAXED)
27155 this_in |= TM_STMT_ATTR_RELAXED;
27156 else
27158 attrs = cp_parser_txn_attribute_opt (parser);
27159 if (attrs)
27160 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27163 /* Parse a noexcept specification. */
27164 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27166 /* Keep track if we're in the lexical scope of an outer transaction. */
27167 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27169 stmt = begin_transaction_stmt (token->location, NULL, this_in);
27171 parser->in_transaction = new_in;
27172 cp_parser_compound_statement (parser, NULL, false, false);
27173 parser->in_transaction = old_in;
27175 finish_transaction_stmt (stmt, NULL, this_in, noex);
27177 return stmt;
27180 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27182 transaction-expression:
27183 __transaction_atomic txn-noexcept-spec[opt] ( expression )
27184 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27187 static tree
27188 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27190 unsigned char old_in = parser->in_transaction;
27191 unsigned char this_in = 1;
27192 cp_token *token;
27193 tree expr, noex;
27194 bool noex_expr;
27196 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27197 || keyword == RID_TRANSACTION_RELAXED);
27199 if (!flag_tm)
27200 error (keyword == RID_TRANSACTION_RELAXED
27201 ? G_("%<__transaction_relaxed%> without transactional memory "
27202 "support enabled")
27203 : G_("%<__transaction_atomic%> without transactional memory "
27204 "support enabled"));
27206 token = cp_parser_require_keyword (parser, keyword,
27207 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27208 : RT_TRANSACTION_RELAXED));
27209 gcc_assert (token != NULL);
27211 if (keyword == RID_TRANSACTION_RELAXED)
27212 this_in |= TM_STMT_ATTR_RELAXED;
27214 /* Set this early. This might mean that we allow transaction_cancel in
27215 an expression that we find out later actually has to be a constexpr.
27216 However, we expect that cxx_constant_value will be able to deal with
27217 this; also, if the noexcept has no constexpr, then what we parse next
27218 really is a transaction's body. */
27219 parser->in_transaction = this_in;
27221 /* Parse a noexcept specification. */
27222 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27223 true);
27225 if (!noex || !noex_expr
27226 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27228 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27230 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27231 finish_parenthesized_expr (expr);
27233 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27235 else
27237 /* The only expression that is available got parsed for the noexcept
27238 already. noexcept is true then. */
27239 expr = noex;
27240 noex = boolean_true_node;
27243 expr = build_transaction_expr (token->location, expr, this_in, noex);
27244 parser->in_transaction = old_in;
27246 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27247 return error_mark_node;
27249 return (flag_tm ? expr : error_mark_node);
27252 /* Parse a function-transaction-block.
27254 function-transaction-block:
27255 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27256 function-body
27257 __transaction_atomic txn-attribute[opt] function-try-block
27258 __transaction_relaxed ctor-initializer[opt] function-body
27259 __transaction_relaxed function-try-block
27262 static bool
27263 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27265 unsigned char old_in = parser->in_transaction;
27266 unsigned char new_in = 1;
27267 tree compound_stmt, stmt, attrs;
27268 bool ctor_initializer_p;
27269 cp_token *token;
27271 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27272 || keyword == RID_TRANSACTION_RELAXED);
27273 token = cp_parser_require_keyword (parser, keyword,
27274 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27275 : RT_TRANSACTION_RELAXED));
27276 gcc_assert (token != NULL);
27278 if (keyword == RID_TRANSACTION_RELAXED)
27279 new_in |= TM_STMT_ATTR_RELAXED;
27280 else
27282 attrs = cp_parser_txn_attribute_opt (parser);
27283 if (attrs)
27284 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27287 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27289 parser->in_transaction = new_in;
27291 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27292 ctor_initializer_p = cp_parser_function_try_block (parser);
27293 else
27294 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
27295 (parser, /*in_function_try_block=*/false);
27297 parser->in_transaction = old_in;
27299 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27301 return ctor_initializer_p;
27304 /* Parse a __transaction_cancel statement.
27306 cancel-statement:
27307 __transaction_cancel txn-attribute[opt] ;
27308 __transaction_cancel txn-attribute[opt] throw-expression ;
27310 ??? Cancel and throw is not yet implemented. */
27312 static tree
27313 cp_parser_transaction_cancel (cp_parser *parser)
27315 cp_token *token;
27316 bool is_outer = false;
27317 tree stmt, attrs;
27319 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27320 RT_TRANSACTION_CANCEL);
27321 gcc_assert (token != NULL);
27323 attrs = cp_parser_txn_attribute_opt (parser);
27324 if (attrs)
27325 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27327 /* ??? Parse cancel-and-throw here. */
27329 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27331 if (!flag_tm)
27333 error_at (token->location, "%<__transaction_cancel%> without "
27334 "transactional memory support enabled");
27335 return error_mark_node;
27337 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27339 error_at (token->location, "%<__transaction_cancel%> within a "
27340 "%<__transaction_relaxed%>");
27341 return error_mark_node;
27343 else if (is_outer)
27345 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27346 && !is_tm_may_cancel_outer (current_function_decl))
27348 error_at (token->location, "outer %<__transaction_cancel%> not "
27349 "within outer %<__transaction_atomic%>");
27350 error_at (token->location,
27351 " or a %<transaction_may_cancel_outer%> function");
27352 return error_mark_node;
27355 else if (parser->in_transaction == 0)
27357 error_at (token->location, "%<__transaction_cancel%> not within "
27358 "%<__transaction_atomic%>");
27359 return error_mark_node;
27362 stmt = build_tm_abort_call (token->location, is_outer);
27363 add_stmt (stmt);
27364 finish_stmt ();
27366 return stmt;
27369 /* The parser. */
27371 static GTY (()) cp_parser *the_parser;
27374 /* Special handling for the first token or line in the file. The first
27375 thing in the file might be #pragma GCC pch_preprocess, which loads a
27376 PCH file, which is a GC collection point. So we need to handle this
27377 first pragma without benefit of an existing lexer structure.
27379 Always returns one token to the caller in *FIRST_TOKEN. This is
27380 either the true first token of the file, or the first token after
27381 the initial pragma. */
27383 static void
27384 cp_parser_initial_pragma (cp_token *first_token)
27386 tree name = NULL;
27388 cp_lexer_get_preprocessor_token (NULL, first_token);
27389 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27390 return;
27392 cp_lexer_get_preprocessor_token (NULL, first_token);
27393 if (first_token->type == CPP_STRING)
27395 name = first_token->u.value;
27397 cp_lexer_get_preprocessor_token (NULL, first_token);
27398 if (first_token->type != CPP_PRAGMA_EOL)
27399 error_at (first_token->location,
27400 "junk at end of %<#pragma GCC pch_preprocess%>");
27402 else
27403 error_at (first_token->location, "expected string literal");
27405 /* Skip to the end of the pragma. */
27406 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27407 cp_lexer_get_preprocessor_token (NULL, first_token);
27409 /* Now actually load the PCH file. */
27410 if (name)
27411 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27413 /* Read one more token to return to our caller. We have to do this
27414 after reading the PCH file in, since its pointers have to be
27415 live. */
27416 cp_lexer_get_preprocessor_token (NULL, first_token);
27419 /* Normal parsing of a pragma token. Here we can (and must) use the
27420 regular lexer. */
27422 static bool
27423 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27425 cp_token *pragma_tok;
27426 unsigned int id;
27428 pragma_tok = cp_lexer_consume_token (parser->lexer);
27429 gcc_assert (pragma_tok->type == CPP_PRAGMA);
27430 parser->lexer->in_pragma = true;
27432 id = pragma_tok->pragma_kind;
27433 switch (id)
27435 case PRAGMA_GCC_PCH_PREPROCESS:
27436 error_at (pragma_tok->location,
27437 "%<#pragma GCC pch_preprocess%> must be first");
27438 break;
27440 case PRAGMA_OMP_BARRIER:
27441 switch (context)
27443 case pragma_compound:
27444 cp_parser_omp_barrier (parser, pragma_tok);
27445 return false;
27446 case pragma_stmt:
27447 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27448 "used in compound statements");
27449 break;
27450 default:
27451 goto bad_stmt;
27453 break;
27455 case PRAGMA_OMP_FLUSH:
27456 switch (context)
27458 case pragma_compound:
27459 cp_parser_omp_flush (parser, pragma_tok);
27460 return false;
27461 case pragma_stmt:
27462 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27463 "used in compound statements");
27464 break;
27465 default:
27466 goto bad_stmt;
27468 break;
27470 case PRAGMA_OMP_TASKWAIT:
27471 switch (context)
27473 case pragma_compound:
27474 cp_parser_omp_taskwait (parser, pragma_tok);
27475 return false;
27476 case pragma_stmt:
27477 error_at (pragma_tok->location,
27478 "%<#pragma omp taskwait%> may only be "
27479 "used in compound statements");
27480 break;
27481 default:
27482 goto bad_stmt;
27484 break;
27486 case PRAGMA_OMP_TASKYIELD:
27487 switch (context)
27489 case pragma_compound:
27490 cp_parser_omp_taskyield (parser, pragma_tok);
27491 return false;
27492 case pragma_stmt:
27493 error_at (pragma_tok->location,
27494 "%<#pragma omp taskyield%> may only be "
27495 "used in compound statements");
27496 break;
27497 default:
27498 goto bad_stmt;
27500 break;
27502 case PRAGMA_OMP_THREADPRIVATE:
27503 cp_parser_omp_threadprivate (parser, pragma_tok);
27504 return false;
27506 case PRAGMA_OMP_ATOMIC:
27507 case PRAGMA_OMP_CRITICAL:
27508 case PRAGMA_OMP_FOR:
27509 case PRAGMA_OMP_MASTER:
27510 case PRAGMA_OMP_ORDERED:
27511 case PRAGMA_OMP_PARALLEL:
27512 case PRAGMA_OMP_SECTIONS:
27513 case PRAGMA_OMP_SINGLE:
27514 case PRAGMA_OMP_TASK:
27515 if (context == pragma_external)
27516 goto bad_stmt;
27517 cp_parser_omp_construct (parser, pragma_tok);
27518 return true;
27520 case PRAGMA_OMP_SECTION:
27521 error_at (pragma_tok->location,
27522 "%<#pragma omp section%> may only be used in "
27523 "%<#pragma omp sections%> construct");
27524 break;
27526 default:
27527 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27528 c_invoke_pragma_handler (id);
27529 break;
27531 bad_stmt:
27532 cp_parser_error (parser, "expected declaration specifiers");
27533 break;
27536 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27537 return false;
27540 /* The interface the pragma parsers have to the lexer. */
27542 enum cpp_ttype
27543 pragma_lex (tree *value)
27545 cp_token *tok;
27546 enum cpp_ttype ret;
27548 tok = cp_lexer_peek_token (the_parser->lexer);
27550 ret = tok->type;
27551 *value = tok->u.value;
27553 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27554 ret = CPP_EOF;
27555 else if (ret == CPP_STRING)
27556 *value = cp_parser_string_literal (the_parser, false, false);
27557 else
27559 cp_lexer_consume_token (the_parser->lexer);
27560 if (ret == CPP_KEYWORD)
27561 ret = CPP_NAME;
27564 return ret;
27568 /* External interface. */
27570 /* Parse one entire translation unit. */
27572 void
27573 c_parse_file (void)
27575 static bool already_called = false;
27577 if (already_called)
27579 sorry ("inter-module optimizations not implemented for C++");
27580 return;
27582 already_called = true;
27584 the_parser = cp_parser_new ();
27585 push_deferring_access_checks (flag_access_control
27586 ? dk_no_deferred : dk_no_check);
27587 cp_parser_translation_unit (the_parser);
27588 the_parser = NULL;
27591 #include "gt-cp-parser.h"