* g++.dg/cpp0x/constexpr-53094-2.C: Ignore non-standard ABI
[official-gcc.git] / gcc / cp / parser.c
blob74db9aef3e0c19cbbb61d1219602cd615198c678
1 /* C++ Parser.
2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "intl.h"
30 #include "c-family/c-pragma.h"
31 #include "decl.h"
32 #include "flags.h"
33 #include "diagnostic-core.h"
34 #include "target.h"
35 #include "cgraph.h"
36 #include "c-family/c-common.h"
37 #include "c-family/c-objc.h"
38 #include "plugin.h"
39 #include "tree-pretty-print.h"
40 #include "parser.h"
43 /* The lexer. */
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 static cp_token eof_token =
50 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
53 /* The various kinds of non integral constant we encounter. */
54 typedef enum non_integral_constant {
55 NIC_NONE,
56 /* floating-point literal */
57 NIC_FLOAT,
58 /* %<this%> */
59 NIC_THIS,
60 /* %<__FUNCTION__%> */
61 NIC_FUNC_NAME,
62 /* %<__PRETTY_FUNCTION__%> */
63 NIC_PRETTY_FUNC,
64 /* %<__func__%> */
65 NIC_C99_FUNC,
66 /* "%<va_arg%> */
67 NIC_VA_ARG,
68 /* a cast */
69 NIC_CAST,
70 /* %<typeid%> operator */
71 NIC_TYPEID,
72 /* non-constant compound literals */
73 NIC_NCC,
74 /* a function call */
75 NIC_FUNC_CALL,
76 /* an increment */
77 NIC_INC,
78 /* an decrement */
79 NIC_DEC,
80 /* an array reference */
81 NIC_ARRAY_REF,
82 /* %<->%> */
83 NIC_ARROW,
84 /* %<.%> */
85 NIC_POINT,
86 /* the address of a label */
87 NIC_ADDR_LABEL,
88 /* %<*%> */
89 NIC_STAR,
90 /* %<&%> */
91 NIC_ADDR,
92 /* %<++%> */
93 NIC_PREINCREMENT,
94 /* %<--%> */
95 NIC_PREDECREMENT,
96 /* %<new%> */
97 NIC_NEW,
98 /* %<delete%> */
99 NIC_DEL,
100 /* calls to overloaded operators */
101 NIC_OVERLOADED,
102 /* an assignment */
103 NIC_ASSIGNMENT,
104 /* a comma operator */
105 NIC_COMMA,
106 /* a call to a constructor */
107 NIC_CONSTRUCTOR,
108 /* a transaction expression */
109 NIC_TRANSACTION
110 } non_integral_constant;
112 /* The various kinds of errors about name-lookup failing. */
113 typedef enum name_lookup_error {
114 /* NULL */
115 NLE_NULL,
116 /* is not a type */
117 NLE_TYPE,
118 /* is not a class or namespace */
119 NLE_CXX98,
120 /* is not a class, namespace, or enumeration */
121 NLE_NOT_CXX98
122 } name_lookup_error;
124 /* The various kinds of required token */
125 typedef enum required_token {
126 RT_NONE,
127 RT_SEMICOLON, /* ';' */
128 RT_OPEN_PAREN, /* '(' */
129 RT_CLOSE_BRACE, /* '}' */
130 RT_OPEN_BRACE, /* '{' */
131 RT_CLOSE_SQUARE, /* ']' */
132 RT_OPEN_SQUARE, /* '[' */
133 RT_COMMA, /* ',' */
134 RT_SCOPE, /* '::' */
135 RT_LESS, /* '<' */
136 RT_GREATER, /* '>' */
137 RT_EQ, /* '=' */
138 RT_ELLIPSIS, /* '...' */
139 RT_MULT, /* '*' */
140 RT_COMPL, /* '~' */
141 RT_COLON, /* ':' */
142 RT_COLON_SCOPE, /* ':' or '::' */
143 RT_CLOSE_PAREN, /* ')' */
144 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
145 RT_PRAGMA_EOL, /* end of line */
146 RT_NAME, /* identifier */
148 /* The type is CPP_KEYWORD */
149 RT_NEW, /* new */
150 RT_DELETE, /* delete */
151 RT_RETURN, /* return */
152 RT_WHILE, /* while */
153 RT_EXTERN, /* extern */
154 RT_STATIC_ASSERT, /* static_assert */
155 RT_DECLTYPE, /* decltype */
156 RT_OPERATOR, /* operator */
157 RT_CLASS, /* class */
158 RT_TEMPLATE, /* template */
159 RT_NAMESPACE, /* namespace */
160 RT_USING, /* using */
161 RT_ASM, /* asm */
162 RT_TRY, /* try */
163 RT_CATCH, /* catch */
164 RT_THROW, /* throw */
165 RT_LABEL, /* __label__ */
166 RT_AT_TRY, /* @try */
167 RT_AT_SYNCHRONIZED, /* @synchronized */
168 RT_AT_THROW, /* @throw */
170 RT_SELECT, /* selection-statement */
171 RT_INTERATION, /* iteration-statement */
172 RT_JUMP, /* jump-statement */
173 RT_CLASS_KEY, /* class-key */
174 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
175 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
176 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
177 RT_TRANSACTION_CANCEL /* __transaction_cancel */
178 } required_token;
180 /* Prototypes. */
182 static cp_lexer *cp_lexer_new_main
183 (void);
184 static cp_lexer *cp_lexer_new_from_tokens
185 (cp_token_cache *tokens);
186 static void cp_lexer_destroy
187 (cp_lexer *);
188 static int cp_lexer_saving_tokens
189 (const cp_lexer *);
190 static cp_token *cp_lexer_token_at
191 (cp_lexer *, cp_token_position);
192 static void cp_lexer_get_preprocessor_token
193 (cp_lexer *, cp_token *);
194 static inline cp_token *cp_lexer_peek_token
195 (cp_lexer *);
196 static cp_token *cp_lexer_peek_nth_token
197 (cp_lexer *, size_t);
198 static inline bool cp_lexer_next_token_is
199 (cp_lexer *, enum cpp_ttype);
200 static bool cp_lexer_next_token_is_not
201 (cp_lexer *, enum cpp_ttype);
202 static bool cp_lexer_next_token_is_keyword
203 (cp_lexer *, enum rid);
204 static cp_token *cp_lexer_consume_token
205 (cp_lexer *);
206 static void cp_lexer_purge_token
207 (cp_lexer *);
208 static void cp_lexer_purge_tokens_after
209 (cp_lexer *, cp_token_position);
210 static void cp_lexer_save_tokens
211 (cp_lexer *);
212 static void cp_lexer_commit_tokens
213 (cp_lexer *);
214 static void cp_lexer_rollback_tokens
215 (cp_lexer *);
216 static void cp_lexer_print_token
217 (FILE *, cp_token *);
218 static inline bool cp_lexer_debugging_p
219 (cp_lexer *);
220 static void cp_lexer_start_debugging
221 (cp_lexer *) ATTRIBUTE_UNUSED;
222 static void cp_lexer_stop_debugging
223 (cp_lexer *) ATTRIBUTE_UNUSED;
225 static cp_token_cache *cp_token_cache_new
226 (cp_token *, cp_token *);
228 static void cp_parser_initial_pragma
229 (cp_token *);
231 static tree cp_literal_operator_id
232 (const char *);
234 /* Manifest constants. */
235 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
236 #define CP_SAVED_TOKEN_STACK 5
238 /* Variables. */
240 /* The stream to which debugging output should be written. */
241 static FILE *cp_lexer_debug_stream;
243 /* Nonzero if we are parsing an unevaluated operand: an operand to
244 sizeof, typeof, or alignof. */
245 int cp_unevaluated_operand;
247 /* Dump up to NUM tokens in BUFFER to FILE starting with token
248 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
249 first token in BUFFER. If NUM is 0, dump all the tokens. If
250 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
251 highlighted by surrounding it in [[ ]]. */
253 static void
254 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
255 cp_token *start_token, unsigned num,
256 cp_token *curr_token)
258 unsigned i, nprinted;
259 cp_token *token;
260 bool do_print;
262 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
264 if (buffer == NULL)
265 return;
267 if (num == 0)
268 num = buffer->length ();
270 if (start_token == NULL)
271 start_token = buffer->address ();
273 if (start_token > buffer->address ())
275 cp_lexer_print_token (file, &(*buffer)[0]);
276 fprintf (file, " ... ");
279 do_print = false;
280 nprinted = 0;
281 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
283 if (token == start_token)
284 do_print = true;
286 if (!do_print)
287 continue;
289 nprinted++;
290 if (token == curr_token)
291 fprintf (file, "[[");
293 cp_lexer_print_token (file, token);
295 if (token == curr_token)
296 fprintf (file, "]]");
298 switch (token->type)
300 case CPP_SEMICOLON:
301 case CPP_OPEN_BRACE:
302 case CPP_CLOSE_BRACE:
303 case CPP_EOF:
304 fputc ('\n', file);
305 break;
307 default:
308 fputc (' ', file);
312 if (i == num && i < buffer->length ())
314 fprintf (file, " ... ");
315 cp_lexer_print_token (file, &buffer->last ());
318 fprintf (file, "\n");
322 /* Dump all tokens in BUFFER to stderr. */
324 void
325 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
327 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
331 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
332 description for T. */
334 static void
335 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
337 if (t)
339 fprintf (file, "%s: ", desc);
340 print_node_brief (file, "", t, 0);
345 /* Dump parser context C to FILE. */
347 static void
348 cp_debug_print_context (FILE *file, cp_parser_context *c)
350 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
351 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
352 print_node_brief (file, "", c->object_type, 0);
353 fprintf (file, "}\n");
357 /* Print the stack of parsing contexts to FILE starting with FIRST. */
359 static void
360 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
362 unsigned i;
363 cp_parser_context *c;
365 fprintf (file, "Parsing context stack:\n");
366 for (i = 0, c = first; c; c = c->next, i++)
368 fprintf (file, "\t#%u: ", i);
369 cp_debug_print_context (file, c);
374 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
376 static void
377 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
379 if (flag)
380 fprintf (file, "%s: true\n", desc);
384 /* Print an unparsed function entry UF to FILE. */
386 static void
387 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
389 unsigned i;
390 cp_default_arg_entry *default_arg_fn;
391 tree fn;
393 fprintf (file, "\tFunctions with default args:\n");
394 for (i = 0;
395 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
396 i++)
398 fprintf (file, "\t\tClass type: ");
399 print_node_brief (file, "", default_arg_fn->class_type, 0);
400 fprintf (file, "\t\tDeclaration: ");
401 print_node_brief (file, "", default_arg_fn->decl, 0);
402 fprintf (file, "\n");
405 fprintf (file, "\n\tFunctions with definitions that require "
406 "post-processing\n\t\t");
407 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
409 print_node_brief (file, "", fn, 0);
410 fprintf (file, " ");
412 fprintf (file, "\n");
414 fprintf (file, "\n\tNon-static data members with initializers that require "
415 "post-processing\n\t\t");
416 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
418 print_node_brief (file, "", fn, 0);
419 fprintf (file, " ");
421 fprintf (file, "\n");
425 /* Print the stack of unparsed member functions S to FILE. */
427 static void
428 cp_debug_print_unparsed_queues (FILE *file,
429 vec<cp_unparsed_functions_entry, va_gc> *s)
431 unsigned i;
432 cp_unparsed_functions_entry *uf;
434 fprintf (file, "Unparsed functions\n");
435 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
437 fprintf (file, "#%u:\n", i);
438 cp_debug_print_unparsed_function (file, uf);
443 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
444 the given PARSER. If FILE is NULL, the output is printed on stderr. */
446 static void
447 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
449 cp_token *next_token, *first_token, *start_token;
451 if (file == NULL)
452 file = stderr;
454 next_token = parser->lexer->next_token;
455 first_token = parser->lexer->buffer->address ();
456 start_token = (next_token > first_token + window_size / 2)
457 ? next_token - window_size / 2
458 : first_token;
459 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
460 next_token);
464 /* Dump debugging information for the given PARSER. If FILE is NULL,
465 the output is printed on stderr. */
467 void
468 cp_debug_parser (FILE *file, cp_parser *parser)
470 const size_t window_size = 20;
471 cp_token *token;
472 expanded_location eloc;
474 if (file == NULL)
475 file = stderr;
477 fprintf (file, "Parser state\n\n");
478 fprintf (file, "Number of tokens: %u\n",
479 vec_safe_length (parser->lexer->buffer));
480 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
481 cp_debug_print_tree_if_set (file, "Object scope",
482 parser->object_scope);
483 cp_debug_print_tree_if_set (file, "Qualifying scope",
484 parser->qualifying_scope);
485 cp_debug_print_context_stack (file, parser->context);
486 cp_debug_print_flag (file, "Allow GNU extensions",
487 parser->allow_gnu_extensions_p);
488 cp_debug_print_flag (file, "'>' token is greater-than",
489 parser->greater_than_is_operator_p);
490 cp_debug_print_flag (file, "Default args allowed in current "
491 "parameter list", parser->default_arg_ok_p);
492 cp_debug_print_flag (file, "Parsing integral constant-expression",
493 parser->integral_constant_expression_p);
494 cp_debug_print_flag (file, "Allow non-constant expression in current "
495 "constant-expression",
496 parser->allow_non_integral_constant_expression_p);
497 cp_debug_print_flag (file, "Seen non-constant expression",
498 parser->non_integral_constant_expression_p);
499 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
500 "current context",
501 parser->local_variables_forbidden_p);
502 cp_debug_print_flag (file, "In unbraced linkage specification",
503 parser->in_unbraced_linkage_specification_p);
504 cp_debug_print_flag (file, "Parsing a declarator",
505 parser->in_declarator_p);
506 cp_debug_print_flag (file, "In template argument list",
507 parser->in_template_argument_list_p);
508 cp_debug_print_flag (file, "Parsing an iteration statement",
509 parser->in_statement & IN_ITERATION_STMT);
510 cp_debug_print_flag (file, "Parsing a switch statement",
511 parser->in_statement & IN_SWITCH_STMT);
512 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
513 parser->in_statement & IN_OMP_BLOCK);
514 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
515 parser->in_statement & IN_OMP_FOR);
516 cp_debug_print_flag (file, "Parsing an if statement",
517 parser->in_statement & IN_IF_STMT);
518 cp_debug_print_flag (file, "Parsing a type-id in an expression "
519 "context", parser->in_type_id_in_expr_p);
520 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
521 parser->implicit_extern_c);
522 cp_debug_print_flag (file, "String expressions should be translated "
523 "to execution character set",
524 parser->translate_strings_p);
525 cp_debug_print_flag (file, "Parsing function body outside of a "
526 "local class", parser->in_function_body);
527 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
528 parser->colon_corrects_to_scope_p);
529 if (parser->type_definition_forbidden_message)
530 fprintf (file, "Error message for forbidden type definitions: %s\n",
531 parser->type_definition_forbidden_message);
532 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
533 fprintf (file, "Number of class definitions in progress: %u\n",
534 parser->num_classes_being_defined);
535 fprintf (file, "Number of template parameter lists for the current "
536 "declaration: %u\n", parser->num_template_parameter_lists);
537 cp_debug_parser_tokens (file, parser, window_size);
538 token = parser->lexer->next_token;
539 fprintf (file, "Next token to parse:\n");
540 fprintf (file, "\tToken: ");
541 cp_lexer_print_token (file, token);
542 eloc = expand_location (token->location);
543 fprintf (file, "\n\tFile: %s\n", eloc.file);
544 fprintf (file, "\tLine: %d\n", eloc.line);
545 fprintf (file, "\tColumn: %d\n", eloc.column);
549 /* Allocate memory for a new lexer object and return it. */
551 static cp_lexer *
552 cp_lexer_alloc (void)
554 cp_lexer *lexer;
556 c_common_no_more_pch ();
558 /* Allocate the memory. */
559 lexer = ggc_alloc_cleared_cp_lexer ();
561 /* Initially we are not debugging. */
562 lexer->debugging_p = false;
564 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
566 /* Create the buffer. */
567 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
569 return lexer;
573 /* Create a new main C++ lexer, the lexer that gets tokens from the
574 preprocessor. */
576 static cp_lexer *
577 cp_lexer_new_main (void)
579 cp_lexer *lexer;
580 cp_token token;
582 /* It's possible that parsing the first pragma will load a PCH file,
583 which is a GC collection point. So we have to do that before
584 allocating any memory. */
585 cp_parser_initial_pragma (&token);
587 lexer = cp_lexer_alloc ();
589 /* Put the first token in the buffer. */
590 lexer->buffer->quick_push (token);
592 /* Get the remaining tokens from the preprocessor. */
593 while (token.type != CPP_EOF)
595 cp_lexer_get_preprocessor_token (lexer, &token);
596 vec_safe_push (lexer->buffer, token);
599 lexer->last_token = lexer->buffer->address ()
600 + lexer->buffer->length ()
601 - 1;
602 lexer->next_token = lexer->buffer->length ()
603 ? lexer->buffer->address ()
604 : &eof_token;
606 /* Subsequent preprocessor diagnostics should use compiler
607 diagnostic functions to get the compiler source location. */
608 done_lexing = true;
610 gcc_assert (!lexer->next_token->purged_p);
611 return lexer;
614 /* Create a new lexer whose token stream is primed with the tokens in
615 CACHE. When these tokens are exhausted, no new tokens will be read. */
617 static cp_lexer *
618 cp_lexer_new_from_tokens (cp_token_cache *cache)
620 cp_token *first = cache->first;
621 cp_token *last = cache->last;
622 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
624 /* We do not own the buffer. */
625 lexer->buffer = NULL;
626 lexer->next_token = first == last ? &eof_token : first;
627 lexer->last_token = last;
629 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
631 /* Initially we are not debugging. */
632 lexer->debugging_p = false;
634 gcc_assert (!lexer->next_token->purged_p);
635 return lexer;
638 /* Frees all resources associated with LEXER. */
640 static void
641 cp_lexer_destroy (cp_lexer *lexer)
643 vec_free (lexer->buffer);
644 lexer->saved_tokens.release ();
645 ggc_free (lexer);
648 /* Returns nonzero if debugging information should be output. */
650 static inline bool
651 cp_lexer_debugging_p (cp_lexer *lexer)
653 return lexer->debugging_p;
657 static inline cp_token_position
658 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
660 gcc_assert (!previous_p || lexer->next_token != &eof_token);
662 return lexer->next_token - previous_p;
665 static inline cp_token *
666 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
668 return pos;
671 static inline void
672 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
674 lexer->next_token = cp_lexer_token_at (lexer, pos);
677 static inline cp_token_position
678 cp_lexer_previous_token_position (cp_lexer *lexer)
680 if (lexer->next_token == &eof_token)
681 return lexer->last_token - 1;
682 else
683 return cp_lexer_token_position (lexer, true);
686 static inline cp_token *
687 cp_lexer_previous_token (cp_lexer *lexer)
689 cp_token_position tp = cp_lexer_previous_token_position (lexer);
691 return cp_lexer_token_at (lexer, tp);
694 /* nonzero if we are presently saving tokens. */
696 static inline int
697 cp_lexer_saving_tokens (const cp_lexer* lexer)
699 return lexer->saved_tokens.length () != 0;
702 /* Store the next token from the preprocessor in *TOKEN. Return true
703 if we reach EOF. If LEXER is NULL, assume we are handling an
704 initial #pragma pch_preprocess, and thus want the lexer to return
705 processed strings. */
707 static void
708 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
710 static int is_extern_c = 0;
712 /* Get a new token from the preprocessor. */
713 token->type
714 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
715 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
716 token->keyword = RID_MAX;
717 token->pragma_kind = PRAGMA_NONE;
718 token->purged_p = false;
720 /* On some systems, some header files are surrounded by an
721 implicit extern "C" block. Set a flag in the token if it
722 comes from such a header. */
723 is_extern_c += pending_lang_change;
724 pending_lang_change = 0;
725 token->implicit_extern_c = is_extern_c > 0;
727 /* Check to see if this token is a keyword. */
728 if (token->type == CPP_NAME)
730 if (C_IS_RESERVED_WORD (token->u.value))
732 /* Mark this token as a keyword. */
733 token->type = CPP_KEYWORD;
734 /* Record which keyword. */
735 token->keyword = C_RID_CODE (token->u.value);
737 else
739 if (warn_cxx0x_compat
740 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
741 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
743 /* Warn about the C++0x keyword (but still treat it as
744 an identifier). */
745 warning (OPT_Wc__0x_compat,
746 "identifier %qE is a keyword in C++11",
747 token->u.value);
749 /* Clear out the C_RID_CODE so we don't warn about this
750 particular identifier-turned-keyword again. */
751 C_SET_RID_CODE (token->u.value, RID_MAX);
754 token->ambiguous_p = false;
755 token->keyword = RID_MAX;
758 else if (token->type == CPP_AT_NAME)
760 /* This only happens in Objective-C++; it must be a keyword. */
761 token->type = CPP_KEYWORD;
762 switch (C_RID_CODE (token->u.value))
764 /* Replace 'class' with '@class', 'private' with '@private',
765 etc. This prevents confusion with the C++ keyword
766 'class', and makes the tokens consistent with other
767 Objective-C 'AT' keywords. For example '@class' is
768 reported as RID_AT_CLASS which is consistent with
769 '@synchronized', which is reported as
770 RID_AT_SYNCHRONIZED.
772 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
773 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
774 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
775 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
776 case RID_THROW: token->keyword = RID_AT_THROW; break;
777 case RID_TRY: token->keyword = RID_AT_TRY; break;
778 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
779 default: token->keyword = C_RID_CODE (token->u.value);
782 else if (token->type == CPP_PRAGMA)
784 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
785 token->pragma_kind = ((enum pragma_kind)
786 TREE_INT_CST_LOW (token->u.value));
787 token->u.value = NULL_TREE;
791 /* Update the globals input_location and the input file stack from TOKEN. */
792 static inline void
793 cp_lexer_set_source_position_from_token (cp_token *token)
795 if (token->type != CPP_EOF)
797 input_location = token->location;
801 /* Return a pointer to the next token in the token stream, but do not
802 consume it. */
804 static inline cp_token *
805 cp_lexer_peek_token (cp_lexer *lexer)
807 if (cp_lexer_debugging_p (lexer))
809 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
810 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
811 putc ('\n', cp_lexer_debug_stream);
813 return lexer->next_token;
816 /* Return true if the next token has the indicated TYPE. */
818 static inline bool
819 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
821 return cp_lexer_peek_token (lexer)->type == type;
824 /* Return true if the next token does not have the indicated TYPE. */
826 static inline bool
827 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
829 return !cp_lexer_next_token_is (lexer, type);
832 /* Return true if the next token is the indicated KEYWORD. */
834 static inline bool
835 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
837 return cp_lexer_peek_token (lexer)->keyword == keyword;
840 /* Return true if the next token is not the indicated KEYWORD. */
842 static inline bool
843 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
845 return cp_lexer_peek_token (lexer)->keyword != keyword;
848 /* Return true if the next token is a keyword for a decl-specifier. */
850 static bool
851 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
853 cp_token *token;
855 token = cp_lexer_peek_token (lexer);
856 switch (token->keyword)
858 /* auto specifier: storage-class-specifier in C++,
859 simple-type-specifier in C++0x. */
860 case RID_AUTO:
861 /* Storage classes. */
862 case RID_REGISTER:
863 case RID_STATIC:
864 case RID_EXTERN:
865 case RID_MUTABLE:
866 case RID_THREAD:
867 /* Elaborated type specifiers. */
868 case RID_ENUM:
869 case RID_CLASS:
870 case RID_STRUCT:
871 case RID_UNION:
872 case RID_TYPENAME:
873 /* Simple type specifiers. */
874 case RID_CHAR:
875 case RID_CHAR16:
876 case RID_CHAR32:
877 case RID_WCHAR:
878 case RID_BOOL:
879 case RID_SHORT:
880 case RID_INT:
881 case RID_LONG:
882 case RID_INT128:
883 case RID_SIGNED:
884 case RID_UNSIGNED:
885 case RID_FLOAT:
886 case RID_DOUBLE:
887 case RID_VOID:
888 /* GNU extensions. */
889 case RID_ATTRIBUTE:
890 case RID_TYPEOF:
891 /* C++0x extensions. */
892 case RID_DECLTYPE:
893 case RID_UNDERLYING_TYPE:
894 return true;
896 default:
897 return false;
901 /* Returns TRUE iff the token T begins a decltype type. */
903 static bool
904 token_is_decltype (cp_token *t)
906 return (t->keyword == RID_DECLTYPE
907 || t->type == CPP_DECLTYPE);
910 /* Returns TRUE iff the next token begins a decltype type. */
912 static bool
913 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
915 cp_token *t = cp_lexer_peek_token (lexer);
916 return token_is_decltype (t);
919 /* Return a pointer to the Nth token in the token stream. If N is 1,
920 then this is precisely equivalent to cp_lexer_peek_token (except
921 that it is not inline). One would like to disallow that case, but
922 there is one case (cp_parser_nth_token_starts_template_id) where
923 the caller passes a variable for N and it might be 1. */
925 static cp_token *
926 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
928 cp_token *token;
930 /* N is 1-based, not zero-based. */
931 gcc_assert (n > 0);
933 if (cp_lexer_debugging_p (lexer))
934 fprintf (cp_lexer_debug_stream,
935 "cp_lexer: peeking ahead %ld at token: ", (long)n);
937 --n;
938 token = lexer->next_token;
939 gcc_assert (!n || token != &eof_token);
940 while (n != 0)
942 ++token;
943 if (token == lexer->last_token)
945 token = &eof_token;
946 break;
949 if (!token->purged_p)
950 --n;
953 if (cp_lexer_debugging_p (lexer))
955 cp_lexer_print_token (cp_lexer_debug_stream, token);
956 putc ('\n', cp_lexer_debug_stream);
959 return token;
962 /* Return the next token, and advance the lexer's next_token pointer
963 to point to the next non-purged token. */
965 static cp_token *
966 cp_lexer_consume_token (cp_lexer* lexer)
968 cp_token *token = lexer->next_token;
970 gcc_assert (token != &eof_token);
971 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
975 lexer->next_token++;
976 if (lexer->next_token == lexer->last_token)
978 lexer->next_token = &eof_token;
979 break;
983 while (lexer->next_token->purged_p);
985 cp_lexer_set_source_position_from_token (token);
987 /* Provide debugging output. */
988 if (cp_lexer_debugging_p (lexer))
990 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
991 cp_lexer_print_token (cp_lexer_debug_stream, token);
992 putc ('\n', cp_lexer_debug_stream);
995 return token;
998 /* Permanently remove the next token from the token stream, and
999 advance the next_token pointer to refer to the next non-purged
1000 token. */
1002 static void
1003 cp_lexer_purge_token (cp_lexer *lexer)
1005 cp_token *tok = lexer->next_token;
1007 gcc_assert (tok != &eof_token);
1008 tok->purged_p = true;
1009 tok->location = UNKNOWN_LOCATION;
1010 tok->u.value = NULL_TREE;
1011 tok->keyword = RID_MAX;
1015 tok++;
1016 if (tok == lexer->last_token)
1018 tok = &eof_token;
1019 break;
1022 while (tok->purged_p);
1023 lexer->next_token = tok;
1026 /* Permanently remove all tokens after TOK, up to, but not
1027 including, the token that will be returned next by
1028 cp_lexer_peek_token. */
1030 static void
1031 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1033 cp_token *peek = lexer->next_token;
1035 if (peek == &eof_token)
1036 peek = lexer->last_token;
1038 gcc_assert (tok < peek);
1040 for ( tok += 1; tok != peek; tok += 1)
1042 tok->purged_p = true;
1043 tok->location = UNKNOWN_LOCATION;
1044 tok->u.value = NULL_TREE;
1045 tok->keyword = RID_MAX;
1049 /* Begin saving tokens. All tokens consumed after this point will be
1050 preserved. */
1052 static void
1053 cp_lexer_save_tokens (cp_lexer* lexer)
1055 /* Provide debugging output. */
1056 if (cp_lexer_debugging_p (lexer))
1057 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1059 lexer->saved_tokens.safe_push (lexer->next_token);
1062 /* Commit to the portion of the token stream most recently saved. */
1064 static void
1065 cp_lexer_commit_tokens (cp_lexer* lexer)
1067 /* Provide debugging output. */
1068 if (cp_lexer_debugging_p (lexer))
1069 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1071 lexer->saved_tokens.pop ();
1074 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1075 to the token stream. Stop saving tokens. */
1077 static void
1078 cp_lexer_rollback_tokens (cp_lexer* lexer)
1080 /* Provide debugging output. */
1081 if (cp_lexer_debugging_p (lexer))
1082 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1084 lexer->next_token = lexer->saved_tokens.pop ();
1087 /* Print a representation of the TOKEN on the STREAM. */
1089 static void
1090 cp_lexer_print_token (FILE * stream, cp_token *token)
1092 /* We don't use cpp_type2name here because the parser defines
1093 a few tokens of its own. */
1094 static const char *const token_names[] = {
1095 /* cpplib-defined token types */
1096 #define OP(e, s) #e,
1097 #define TK(e, s) #e,
1098 TTYPE_TABLE
1099 #undef OP
1100 #undef TK
1101 /* C++ parser token types - see "Manifest constants", above. */
1102 "KEYWORD",
1103 "TEMPLATE_ID",
1104 "NESTED_NAME_SPECIFIER",
1107 /* For some tokens, print the associated data. */
1108 switch (token->type)
1110 case CPP_KEYWORD:
1111 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1112 For example, `struct' is mapped to an INTEGER_CST. */
1113 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1114 break;
1115 /* else fall through */
1116 case CPP_NAME:
1117 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1118 break;
1120 case CPP_STRING:
1121 case CPP_STRING16:
1122 case CPP_STRING32:
1123 case CPP_WSTRING:
1124 case CPP_UTF8STRING:
1125 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1126 break;
1128 case CPP_NUMBER:
1129 print_generic_expr (stream, token->u.value, 0);
1130 break;
1132 default:
1133 /* If we have a name for the token, print it out. Otherwise, we
1134 simply give the numeric code. */
1135 if (token->type < ARRAY_SIZE(token_names))
1136 fputs (token_names[token->type], stream);
1137 else
1138 fprintf (stream, "[%d]", token->type);
1139 break;
1143 /* Start emitting debugging information. */
1145 static void
1146 cp_lexer_start_debugging (cp_lexer* lexer)
1148 lexer->debugging_p = true;
1149 cp_lexer_debug_stream = stderr;
1152 /* Stop emitting debugging information. */
1154 static void
1155 cp_lexer_stop_debugging (cp_lexer* lexer)
1157 lexer->debugging_p = false;
1158 cp_lexer_debug_stream = NULL;
1161 /* Create a new cp_token_cache, representing a range of tokens. */
1163 static cp_token_cache *
1164 cp_token_cache_new (cp_token *first, cp_token *last)
1166 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1167 cache->first = first;
1168 cache->last = last;
1169 return cache;
1173 /* Decl-specifiers. */
1175 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1177 static void
1178 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1180 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1183 /* Declarators. */
1185 /* Nothing other than the parser should be creating declarators;
1186 declarators are a semi-syntactic representation of C++ entities.
1187 Other parts of the front end that need to create entities (like
1188 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1190 static cp_declarator *make_call_declarator
1191 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
1192 static cp_declarator *make_array_declarator
1193 (cp_declarator *, tree);
1194 static cp_declarator *make_pointer_declarator
1195 (cp_cv_quals, cp_declarator *, tree);
1196 static cp_declarator *make_reference_declarator
1197 (cp_cv_quals, cp_declarator *, bool, tree);
1198 static cp_parameter_declarator *make_parameter_declarator
1199 (cp_decl_specifier_seq *, cp_declarator *, tree);
1200 static cp_declarator *make_ptrmem_declarator
1201 (cp_cv_quals, tree, cp_declarator *, tree);
1203 /* An erroneous declarator. */
1204 static cp_declarator *cp_error_declarator;
1206 /* The obstack on which declarators and related data structures are
1207 allocated. */
1208 static struct obstack declarator_obstack;
1210 /* Alloc BYTES from the declarator memory pool. */
1212 static inline void *
1213 alloc_declarator (size_t bytes)
1215 return obstack_alloc (&declarator_obstack, bytes);
1218 /* Allocate a declarator of the indicated KIND. Clear fields that are
1219 common to all declarators. */
1221 static cp_declarator *
1222 make_declarator (cp_declarator_kind kind)
1224 cp_declarator *declarator;
1226 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1227 declarator->kind = kind;
1228 declarator->attributes = NULL_TREE;
1229 declarator->std_attributes = NULL_TREE;
1230 declarator->declarator = NULL;
1231 declarator->parameter_pack_p = false;
1232 declarator->id_loc = UNKNOWN_LOCATION;
1234 return declarator;
1237 /* Make a declarator for a generalized identifier. If
1238 QUALIFYING_SCOPE is non-NULL, the identifier is
1239 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1240 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1241 is, if any. */
1243 static cp_declarator *
1244 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1245 special_function_kind sfk)
1247 cp_declarator *declarator;
1249 /* It is valid to write:
1251 class C { void f(); };
1252 typedef C D;
1253 void D::f();
1255 The standard is not clear about whether `typedef const C D' is
1256 legal; as of 2002-09-15 the committee is considering that
1257 question. EDG 3.0 allows that syntax. Therefore, we do as
1258 well. */
1259 if (qualifying_scope && TYPE_P (qualifying_scope))
1260 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1262 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1263 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1264 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1266 declarator = make_declarator (cdk_id);
1267 declarator->u.id.qualifying_scope = qualifying_scope;
1268 declarator->u.id.unqualified_name = unqualified_name;
1269 declarator->u.id.sfk = sfk;
1271 return declarator;
1274 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1275 of modifiers such as const or volatile to apply to the pointer
1276 type, represented as identifiers. ATTRIBUTES represent the attributes that
1277 appertain to the pointer or reference. */
1279 cp_declarator *
1280 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1281 tree attributes)
1283 cp_declarator *declarator;
1285 declarator = make_declarator (cdk_pointer);
1286 declarator->declarator = target;
1287 declarator->u.pointer.qualifiers = cv_qualifiers;
1288 declarator->u.pointer.class_type = NULL_TREE;
1289 if (target)
1291 declarator->id_loc = target->id_loc;
1292 declarator->parameter_pack_p = target->parameter_pack_p;
1293 target->parameter_pack_p = false;
1295 else
1296 declarator->parameter_pack_p = false;
1298 declarator->std_attributes = attributes;
1300 return declarator;
1303 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1304 represent the attributes that appertain to the pointer or
1305 reference. */
1307 cp_declarator *
1308 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1309 bool rvalue_ref, tree attributes)
1311 cp_declarator *declarator;
1313 declarator = make_declarator (cdk_reference);
1314 declarator->declarator = target;
1315 declarator->u.reference.qualifiers = cv_qualifiers;
1316 declarator->u.reference.rvalue_ref = rvalue_ref;
1317 if (target)
1319 declarator->id_loc = target->id_loc;
1320 declarator->parameter_pack_p = target->parameter_pack_p;
1321 target->parameter_pack_p = false;
1323 else
1324 declarator->parameter_pack_p = false;
1326 declarator->std_attributes = attributes;
1328 return declarator;
1331 /* Like make_pointer_declarator -- but for a pointer to a non-static
1332 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1333 appertain to the pointer or reference. */
1335 cp_declarator *
1336 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1337 cp_declarator *pointee,
1338 tree attributes)
1340 cp_declarator *declarator;
1342 declarator = make_declarator (cdk_ptrmem);
1343 declarator->declarator = pointee;
1344 declarator->u.pointer.qualifiers = cv_qualifiers;
1345 declarator->u.pointer.class_type = class_type;
1347 if (pointee)
1349 declarator->parameter_pack_p = pointee->parameter_pack_p;
1350 pointee->parameter_pack_p = false;
1352 else
1353 declarator->parameter_pack_p = false;
1355 declarator->std_attributes = attributes;
1357 return declarator;
1360 /* Make a declarator for the function given by TARGET, with the
1361 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1362 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1363 indicates what exceptions can be thrown. */
1365 cp_declarator *
1366 make_call_declarator (cp_declarator *target,
1367 tree parms,
1368 cp_cv_quals cv_qualifiers,
1369 cp_virt_specifiers virt_specifiers,
1370 tree exception_specification,
1371 tree late_return_type)
1373 cp_declarator *declarator;
1375 declarator = make_declarator (cdk_function);
1376 declarator->declarator = target;
1377 declarator->u.function.parameters = parms;
1378 declarator->u.function.qualifiers = cv_qualifiers;
1379 declarator->u.function.virt_specifiers = virt_specifiers;
1380 declarator->u.function.exception_specification = exception_specification;
1381 declarator->u.function.late_return_type = late_return_type;
1382 if (target)
1384 declarator->id_loc = target->id_loc;
1385 declarator->parameter_pack_p = target->parameter_pack_p;
1386 target->parameter_pack_p = false;
1388 else
1389 declarator->parameter_pack_p = false;
1391 return declarator;
1394 /* Make a declarator for an array of BOUNDS elements, each of which is
1395 defined by ELEMENT. */
1397 cp_declarator *
1398 make_array_declarator (cp_declarator *element, tree bounds)
1400 cp_declarator *declarator;
1402 declarator = make_declarator (cdk_array);
1403 declarator->declarator = element;
1404 declarator->u.array.bounds = bounds;
1405 if (element)
1407 declarator->id_loc = element->id_loc;
1408 declarator->parameter_pack_p = element->parameter_pack_p;
1409 element->parameter_pack_p = false;
1411 else
1412 declarator->parameter_pack_p = false;
1414 return declarator;
1417 /* Determine whether the declarator we've seen so far can be a
1418 parameter pack, when followed by an ellipsis. */
1419 static bool
1420 declarator_can_be_parameter_pack (cp_declarator *declarator)
1422 /* Search for a declarator name, or any other declarator that goes
1423 after the point where the ellipsis could appear in a parameter
1424 pack. If we find any of these, then this declarator can not be
1425 made into a parameter pack. */
1426 bool found = false;
1427 while (declarator && !found)
1429 switch ((int)declarator->kind)
1431 case cdk_id:
1432 case cdk_array:
1433 found = true;
1434 break;
1436 case cdk_error:
1437 return true;
1439 default:
1440 declarator = declarator->declarator;
1441 break;
1445 return !found;
1448 cp_parameter_declarator *no_parameters;
1450 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1451 DECLARATOR and DEFAULT_ARGUMENT. */
1453 cp_parameter_declarator *
1454 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1455 cp_declarator *declarator,
1456 tree default_argument)
1458 cp_parameter_declarator *parameter;
1460 parameter = ((cp_parameter_declarator *)
1461 alloc_declarator (sizeof (cp_parameter_declarator)));
1462 parameter->next = NULL;
1463 if (decl_specifiers)
1464 parameter->decl_specifiers = *decl_specifiers;
1465 else
1466 clear_decl_specs (&parameter->decl_specifiers);
1467 parameter->declarator = declarator;
1468 parameter->default_argument = default_argument;
1469 parameter->ellipsis_p = false;
1471 return parameter;
1474 /* Returns true iff DECLARATOR is a declaration for a function. */
1476 static bool
1477 function_declarator_p (const cp_declarator *declarator)
1479 while (declarator)
1481 if (declarator->kind == cdk_function
1482 && declarator->declarator->kind == cdk_id)
1483 return true;
1484 if (declarator->kind == cdk_id
1485 || declarator->kind == cdk_error)
1486 return false;
1487 declarator = declarator->declarator;
1489 return false;
1492 /* The parser. */
1494 /* Overview
1495 --------
1497 A cp_parser parses the token stream as specified by the C++
1498 grammar. Its job is purely parsing, not semantic analysis. For
1499 example, the parser breaks the token stream into declarators,
1500 expressions, statements, and other similar syntactic constructs.
1501 It does not check that the types of the expressions on either side
1502 of an assignment-statement are compatible, or that a function is
1503 not declared with a parameter of type `void'.
1505 The parser invokes routines elsewhere in the compiler to perform
1506 semantic analysis and to build up the abstract syntax tree for the
1507 code processed.
1509 The parser (and the template instantiation code, which is, in a
1510 way, a close relative of parsing) are the only parts of the
1511 compiler that should be calling push_scope and pop_scope, or
1512 related functions. The parser (and template instantiation code)
1513 keeps track of what scope is presently active; everything else
1514 should simply honor that. (The code that generates static
1515 initializers may also need to set the scope, in order to check
1516 access control correctly when emitting the initializers.)
1518 Methodology
1519 -----------
1521 The parser is of the standard recursive-descent variety. Upcoming
1522 tokens in the token stream are examined in order to determine which
1523 production to use when parsing a non-terminal. Some C++ constructs
1524 require arbitrary look ahead to disambiguate. For example, it is
1525 impossible, in the general case, to tell whether a statement is an
1526 expression or declaration without scanning the entire statement.
1527 Therefore, the parser is capable of "parsing tentatively." When the
1528 parser is not sure what construct comes next, it enters this mode.
1529 Then, while we attempt to parse the construct, the parser queues up
1530 error messages, rather than issuing them immediately, and saves the
1531 tokens it consumes. If the construct is parsed successfully, the
1532 parser "commits", i.e., it issues any queued error messages and
1533 the tokens that were being preserved are permanently discarded.
1534 If, however, the construct is not parsed successfully, the parser
1535 rolls back its state completely so that it can resume parsing using
1536 a different alternative.
1538 Future Improvements
1539 -------------------
1541 The performance of the parser could probably be improved substantially.
1542 We could often eliminate the need to parse tentatively by looking ahead
1543 a little bit. In some places, this approach might not entirely eliminate
1544 the need to parse tentatively, but it might still speed up the average
1545 case. */
1547 /* Flags that are passed to some parsing functions. These values can
1548 be bitwise-ored together. */
1550 enum
1552 /* No flags. */
1553 CP_PARSER_FLAGS_NONE = 0x0,
1554 /* The construct is optional. If it is not present, then no error
1555 should be issued. */
1556 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1557 /* When parsing a type-specifier, treat user-defined type-names
1558 as non-type identifiers. */
1559 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1560 /* When parsing a type-specifier, do not try to parse a class-specifier
1561 or enum-specifier. */
1562 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1563 /* When parsing a decl-specifier-seq, only allow type-specifier or
1564 constexpr. */
1565 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1568 /* This type is used for parameters and variables which hold
1569 combinations of the above flags. */
1570 typedef int cp_parser_flags;
1572 /* The different kinds of declarators we want to parse. */
1574 typedef enum cp_parser_declarator_kind
1576 /* We want an abstract declarator. */
1577 CP_PARSER_DECLARATOR_ABSTRACT,
1578 /* We want a named declarator. */
1579 CP_PARSER_DECLARATOR_NAMED,
1580 /* We don't mind, but the name must be an unqualified-id. */
1581 CP_PARSER_DECLARATOR_EITHER
1582 } cp_parser_declarator_kind;
1584 /* The precedence values used to parse binary expressions. The minimum value
1585 of PREC must be 1, because zero is reserved to quickly discriminate
1586 binary operators from other tokens. */
1588 enum cp_parser_prec
1590 PREC_NOT_OPERATOR,
1591 PREC_LOGICAL_OR_EXPRESSION,
1592 PREC_LOGICAL_AND_EXPRESSION,
1593 PREC_INCLUSIVE_OR_EXPRESSION,
1594 PREC_EXCLUSIVE_OR_EXPRESSION,
1595 PREC_AND_EXPRESSION,
1596 PREC_EQUALITY_EXPRESSION,
1597 PREC_RELATIONAL_EXPRESSION,
1598 PREC_SHIFT_EXPRESSION,
1599 PREC_ADDITIVE_EXPRESSION,
1600 PREC_MULTIPLICATIVE_EXPRESSION,
1601 PREC_PM_EXPRESSION,
1602 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1605 /* A mapping from a token type to a corresponding tree node type, with a
1606 precedence value. */
1608 typedef struct cp_parser_binary_operations_map_node
1610 /* The token type. */
1611 enum cpp_ttype token_type;
1612 /* The corresponding tree code. */
1613 enum tree_code tree_type;
1614 /* The precedence of this operator. */
1615 enum cp_parser_prec prec;
1616 } cp_parser_binary_operations_map_node;
1618 typedef struct cp_parser_expression_stack_entry
1620 /* Left hand side of the binary operation we are currently
1621 parsing. */
1622 tree lhs;
1623 /* Original tree code for left hand side, if it was a binary
1624 expression itself (used for -Wparentheses). */
1625 enum tree_code lhs_type;
1626 /* Tree code for the binary operation we are parsing. */
1627 enum tree_code tree_type;
1628 /* Precedence of the binary operation we are parsing. */
1629 enum cp_parser_prec prec;
1630 /* Location of the binary operation we are parsing. */
1631 location_t loc;
1632 } cp_parser_expression_stack_entry;
1634 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1635 entries because precedence levels on the stack are monotonically
1636 increasing. */
1637 typedef struct cp_parser_expression_stack_entry
1638 cp_parser_expression_stack[NUM_PREC_VALUES];
1640 /* Prototypes. */
1642 /* Constructors and destructors. */
1644 static cp_parser_context *cp_parser_context_new
1645 (cp_parser_context *);
1647 /* Class variables. */
1649 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1651 /* The operator-precedence table used by cp_parser_binary_expression.
1652 Transformed into an associative array (binops_by_token) by
1653 cp_parser_new. */
1655 static const cp_parser_binary_operations_map_node binops[] = {
1656 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1657 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1659 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1660 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1661 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1663 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1664 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1666 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1667 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1669 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1670 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1671 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1672 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1674 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1675 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1677 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1679 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1681 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1683 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1685 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1688 /* The same as binops, but initialized by cp_parser_new so that
1689 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1690 for speed. */
1691 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1693 /* Constructors and destructors. */
1695 /* Construct a new context. The context below this one on the stack
1696 is given by NEXT. */
1698 static cp_parser_context *
1699 cp_parser_context_new (cp_parser_context* next)
1701 cp_parser_context *context;
1703 /* Allocate the storage. */
1704 if (cp_parser_context_free_list != NULL)
1706 /* Pull the first entry from the free list. */
1707 context = cp_parser_context_free_list;
1708 cp_parser_context_free_list = context->next;
1709 memset (context, 0, sizeof (*context));
1711 else
1712 context = ggc_alloc_cleared_cp_parser_context ();
1714 /* No errors have occurred yet in this context. */
1715 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1716 /* If this is not the bottommost context, copy information that we
1717 need from the previous context. */
1718 if (next)
1720 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1721 expression, then we are parsing one in this context, too. */
1722 context->object_type = next->object_type;
1723 /* Thread the stack. */
1724 context->next = next;
1727 return context;
1730 /* Managing the unparsed function queues. */
1732 #define unparsed_funs_with_default_args \
1733 parser->unparsed_queues->last ().funs_with_default_args
1734 #define unparsed_funs_with_definitions \
1735 parser->unparsed_queues->last ().funs_with_definitions
1736 #define unparsed_nsdmis \
1737 parser->unparsed_queues->last ().nsdmis
1739 static void
1740 push_unparsed_function_queues (cp_parser *parser)
1742 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1743 vec_safe_push (parser->unparsed_queues, e);
1746 static void
1747 pop_unparsed_function_queues (cp_parser *parser)
1749 release_tree_vector (unparsed_funs_with_definitions);
1750 parser->unparsed_queues->pop ();
1753 /* Prototypes. */
1755 /* Constructors and destructors. */
1757 static cp_parser *cp_parser_new
1758 (void);
1760 /* Routines to parse various constructs.
1762 Those that return `tree' will return the error_mark_node (rather
1763 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1764 Sometimes, they will return an ordinary node if error-recovery was
1765 attempted, even though a parse error occurred. So, to check
1766 whether or not a parse error occurred, you should always use
1767 cp_parser_error_occurred. If the construct is optional (indicated
1768 either by an `_opt' in the name of the function that does the
1769 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1770 the construct is not present. */
1772 /* Lexical conventions [gram.lex] */
1774 static tree cp_parser_identifier
1775 (cp_parser *);
1776 static tree cp_parser_string_literal
1777 (cp_parser *, bool, bool);
1778 static tree cp_parser_userdef_char_literal
1779 (cp_parser *);
1780 static tree cp_parser_userdef_string_literal
1781 (cp_token *);
1782 static tree cp_parser_userdef_numeric_literal
1783 (cp_parser *);
1785 /* Basic concepts [gram.basic] */
1787 static bool cp_parser_translation_unit
1788 (cp_parser *);
1790 /* Expressions [gram.expr] */
1792 static tree cp_parser_primary_expression
1793 (cp_parser *, bool, bool, bool, cp_id_kind *);
1794 static tree cp_parser_id_expression
1795 (cp_parser *, bool, bool, bool *, bool, bool);
1796 static tree cp_parser_unqualified_id
1797 (cp_parser *, bool, bool, bool, bool);
1798 static tree cp_parser_nested_name_specifier_opt
1799 (cp_parser *, bool, bool, bool, bool);
1800 static tree cp_parser_nested_name_specifier
1801 (cp_parser *, bool, bool, bool, bool);
1802 static tree cp_parser_qualifying_entity
1803 (cp_parser *, bool, bool, bool, bool, bool);
1804 static tree cp_parser_postfix_expression
1805 (cp_parser *, bool, bool, bool, cp_id_kind *);
1806 static tree cp_parser_postfix_open_square_expression
1807 (cp_parser *, tree, bool);
1808 static tree cp_parser_postfix_dot_deref_expression
1809 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1810 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1811 (cp_parser *, int, bool, bool, bool *);
1812 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1813 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1814 static void cp_parser_pseudo_destructor_name
1815 (cp_parser *, tree *, tree *);
1816 static tree cp_parser_unary_expression
1817 (cp_parser *, bool, bool, cp_id_kind *);
1818 static enum tree_code cp_parser_unary_operator
1819 (cp_token *);
1820 static tree cp_parser_new_expression
1821 (cp_parser *);
1822 static vec<tree, va_gc> *cp_parser_new_placement
1823 (cp_parser *);
1824 static tree cp_parser_new_type_id
1825 (cp_parser *, tree *);
1826 static cp_declarator *cp_parser_new_declarator_opt
1827 (cp_parser *);
1828 static cp_declarator *cp_parser_direct_new_declarator
1829 (cp_parser *);
1830 static vec<tree, va_gc> *cp_parser_new_initializer
1831 (cp_parser *);
1832 static tree cp_parser_delete_expression
1833 (cp_parser *);
1834 static tree cp_parser_cast_expression
1835 (cp_parser *, bool, bool, cp_id_kind *);
1836 static tree cp_parser_binary_expression
1837 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1838 static tree cp_parser_question_colon_clause
1839 (cp_parser *, tree);
1840 static tree cp_parser_assignment_expression
1841 (cp_parser *, bool, cp_id_kind *);
1842 static enum tree_code cp_parser_assignment_operator_opt
1843 (cp_parser *);
1844 static tree cp_parser_expression
1845 (cp_parser *, bool, cp_id_kind *);
1846 static tree cp_parser_constant_expression
1847 (cp_parser *, bool, bool *);
1848 static tree cp_parser_builtin_offsetof
1849 (cp_parser *);
1850 static tree cp_parser_lambda_expression
1851 (cp_parser *);
1852 static void cp_parser_lambda_introducer
1853 (cp_parser *, tree);
1854 static bool cp_parser_lambda_declarator_opt
1855 (cp_parser *, tree);
1856 static void cp_parser_lambda_body
1857 (cp_parser *, tree);
1859 /* Statements [gram.stmt.stmt] */
1861 static void cp_parser_statement
1862 (cp_parser *, tree, bool, bool *);
1863 static void cp_parser_label_for_labeled_statement
1864 (cp_parser *, tree);
1865 static tree cp_parser_expression_statement
1866 (cp_parser *, tree);
1867 static tree cp_parser_compound_statement
1868 (cp_parser *, tree, bool, bool);
1869 static void cp_parser_statement_seq_opt
1870 (cp_parser *, tree);
1871 static tree cp_parser_selection_statement
1872 (cp_parser *, bool *);
1873 static tree cp_parser_condition
1874 (cp_parser *);
1875 static tree cp_parser_iteration_statement
1876 (cp_parser *);
1877 static bool cp_parser_for_init_statement
1878 (cp_parser *, tree *decl);
1879 static tree cp_parser_for
1880 (cp_parser *);
1881 static tree cp_parser_c_for
1882 (cp_parser *, tree, tree);
1883 static tree cp_parser_range_for
1884 (cp_parser *, tree, tree, tree);
1885 static void do_range_for_auto_deduction
1886 (tree, tree);
1887 static tree cp_parser_perform_range_for_lookup
1888 (tree, tree *, tree *);
1889 static tree cp_parser_range_for_member_function
1890 (tree, tree);
1891 static tree cp_parser_jump_statement
1892 (cp_parser *);
1893 static void cp_parser_declaration_statement
1894 (cp_parser *);
1896 static tree cp_parser_implicitly_scoped_statement
1897 (cp_parser *, bool *);
1898 static void cp_parser_already_scoped_statement
1899 (cp_parser *);
1901 /* Declarations [gram.dcl.dcl] */
1903 static void cp_parser_declaration_seq_opt
1904 (cp_parser *);
1905 static void cp_parser_declaration
1906 (cp_parser *);
1907 static void cp_parser_block_declaration
1908 (cp_parser *, bool);
1909 static void cp_parser_simple_declaration
1910 (cp_parser *, bool, tree *);
1911 static void cp_parser_decl_specifier_seq
1912 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1913 static tree cp_parser_storage_class_specifier_opt
1914 (cp_parser *);
1915 static tree cp_parser_function_specifier_opt
1916 (cp_parser *, cp_decl_specifier_seq *);
1917 static tree cp_parser_type_specifier
1918 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1919 int *, bool *);
1920 static tree cp_parser_simple_type_specifier
1921 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1922 static tree cp_parser_type_name
1923 (cp_parser *);
1924 static tree cp_parser_nonclass_name
1925 (cp_parser* parser);
1926 static tree cp_parser_elaborated_type_specifier
1927 (cp_parser *, bool, bool);
1928 static tree cp_parser_enum_specifier
1929 (cp_parser *);
1930 static void cp_parser_enumerator_list
1931 (cp_parser *, tree);
1932 static void cp_parser_enumerator_definition
1933 (cp_parser *, tree);
1934 static tree cp_parser_namespace_name
1935 (cp_parser *);
1936 static void cp_parser_namespace_definition
1937 (cp_parser *);
1938 static void cp_parser_namespace_body
1939 (cp_parser *);
1940 static tree cp_parser_qualified_namespace_specifier
1941 (cp_parser *);
1942 static void cp_parser_namespace_alias_definition
1943 (cp_parser *);
1944 static bool cp_parser_using_declaration
1945 (cp_parser *, bool);
1946 static void cp_parser_using_directive
1947 (cp_parser *);
1948 static tree cp_parser_alias_declaration
1949 (cp_parser *);
1950 static void cp_parser_asm_definition
1951 (cp_parser *);
1952 static void cp_parser_linkage_specification
1953 (cp_parser *);
1954 static void cp_parser_static_assert
1955 (cp_parser *, bool);
1956 static tree cp_parser_decltype
1957 (cp_parser *);
1959 /* Declarators [gram.dcl.decl] */
1961 static tree cp_parser_init_declarator
1962 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
1963 static cp_declarator *cp_parser_declarator
1964 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1965 static cp_declarator *cp_parser_direct_declarator
1966 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1967 static enum tree_code cp_parser_ptr_operator
1968 (cp_parser *, tree *, cp_cv_quals *, tree *);
1969 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1970 (cp_parser *);
1971 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1972 (cp_parser *);
1973 static tree cp_parser_late_return_type_opt
1974 (cp_parser *, cp_cv_quals);
1975 static tree cp_parser_declarator_id
1976 (cp_parser *, bool);
1977 static tree cp_parser_type_id
1978 (cp_parser *);
1979 static tree cp_parser_template_type_arg
1980 (cp_parser *);
1981 static tree cp_parser_trailing_type_id (cp_parser *);
1982 static tree cp_parser_type_id_1
1983 (cp_parser *, bool, bool);
1984 static void cp_parser_type_specifier_seq
1985 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1986 static tree cp_parser_parameter_declaration_clause
1987 (cp_parser *);
1988 static tree cp_parser_parameter_declaration_list
1989 (cp_parser *, bool *);
1990 static cp_parameter_declarator *cp_parser_parameter_declaration
1991 (cp_parser *, bool, bool *);
1992 static tree cp_parser_default_argument
1993 (cp_parser *, bool);
1994 static void cp_parser_function_body
1995 (cp_parser *, bool);
1996 static tree cp_parser_initializer
1997 (cp_parser *, bool *, bool *);
1998 static tree cp_parser_initializer_clause
1999 (cp_parser *, bool *);
2000 static tree cp_parser_braced_list
2001 (cp_parser*, bool*);
2002 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2003 (cp_parser *, bool *);
2005 static bool cp_parser_ctor_initializer_opt_and_function_body
2006 (cp_parser *, bool);
2008 /* Classes [gram.class] */
2010 static tree cp_parser_class_name
2011 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2012 static tree cp_parser_class_specifier
2013 (cp_parser *);
2014 static tree cp_parser_class_head
2015 (cp_parser *, bool *);
2016 static enum tag_types cp_parser_class_key
2017 (cp_parser *);
2018 static void cp_parser_member_specification_opt
2019 (cp_parser *);
2020 static void cp_parser_member_declaration
2021 (cp_parser *);
2022 static tree cp_parser_pure_specifier
2023 (cp_parser *);
2024 static tree cp_parser_constant_initializer
2025 (cp_parser *);
2027 /* Derived classes [gram.class.derived] */
2029 static tree cp_parser_base_clause
2030 (cp_parser *);
2031 static tree cp_parser_base_specifier
2032 (cp_parser *);
2034 /* Special member functions [gram.special] */
2036 static tree cp_parser_conversion_function_id
2037 (cp_parser *);
2038 static tree cp_parser_conversion_type_id
2039 (cp_parser *);
2040 static cp_declarator *cp_parser_conversion_declarator_opt
2041 (cp_parser *);
2042 static bool cp_parser_ctor_initializer_opt
2043 (cp_parser *);
2044 static void cp_parser_mem_initializer_list
2045 (cp_parser *);
2046 static tree cp_parser_mem_initializer
2047 (cp_parser *);
2048 static tree cp_parser_mem_initializer_id
2049 (cp_parser *);
2051 /* Overloading [gram.over] */
2053 static tree cp_parser_operator_function_id
2054 (cp_parser *);
2055 static tree cp_parser_operator
2056 (cp_parser *);
2058 /* Templates [gram.temp] */
2060 static void cp_parser_template_declaration
2061 (cp_parser *, bool);
2062 static tree cp_parser_template_parameter_list
2063 (cp_parser *);
2064 static tree cp_parser_template_parameter
2065 (cp_parser *, bool *, bool *);
2066 static tree cp_parser_type_parameter
2067 (cp_parser *, bool *);
2068 static tree cp_parser_template_id
2069 (cp_parser *, bool, bool, enum tag_types, bool);
2070 static tree cp_parser_template_name
2071 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2072 static tree cp_parser_template_argument_list
2073 (cp_parser *);
2074 static tree cp_parser_template_argument
2075 (cp_parser *);
2076 static void cp_parser_explicit_instantiation
2077 (cp_parser *);
2078 static void cp_parser_explicit_specialization
2079 (cp_parser *);
2081 /* Exception handling [gram.exception] */
2083 static tree cp_parser_try_block
2084 (cp_parser *);
2085 static bool cp_parser_function_try_block
2086 (cp_parser *);
2087 static void cp_parser_handler_seq
2088 (cp_parser *);
2089 static void cp_parser_handler
2090 (cp_parser *);
2091 static tree cp_parser_exception_declaration
2092 (cp_parser *);
2093 static tree cp_parser_throw_expression
2094 (cp_parser *);
2095 static tree cp_parser_exception_specification_opt
2096 (cp_parser *);
2097 static tree cp_parser_type_id_list
2098 (cp_parser *);
2100 /* GNU Extensions */
2102 static tree cp_parser_asm_specification_opt
2103 (cp_parser *);
2104 static tree cp_parser_asm_operand_list
2105 (cp_parser *);
2106 static tree cp_parser_asm_clobber_list
2107 (cp_parser *);
2108 static tree cp_parser_asm_label_list
2109 (cp_parser *);
2110 static bool cp_next_tokens_can_be_attribute_p
2111 (cp_parser *);
2112 static bool cp_next_tokens_can_be_gnu_attribute_p
2113 (cp_parser *);
2114 static bool cp_next_tokens_can_be_std_attribute_p
2115 (cp_parser *);
2116 static bool cp_nth_tokens_can_be_std_attribute_p
2117 (cp_parser *, size_t);
2118 static bool cp_nth_tokens_can_be_gnu_attribute_p
2119 (cp_parser *, size_t);
2120 static bool cp_nth_tokens_can_be_attribute_p
2121 (cp_parser *, size_t);
2122 static tree cp_parser_attributes_opt
2123 (cp_parser *);
2124 static tree cp_parser_gnu_attributes_opt
2125 (cp_parser *);
2126 static tree cp_parser_gnu_attribute_list
2127 (cp_parser *);
2128 static tree cp_parser_std_attribute
2129 (cp_parser *);
2130 static tree cp_parser_std_attribute_spec
2131 (cp_parser *);
2132 static tree cp_parser_std_attribute_spec_seq
2133 (cp_parser *);
2134 static bool cp_parser_extension_opt
2135 (cp_parser *, int *);
2136 static void cp_parser_label_declaration
2137 (cp_parser *);
2139 /* Transactional Memory Extensions */
2141 static tree cp_parser_transaction
2142 (cp_parser *, enum rid);
2143 static tree cp_parser_transaction_expression
2144 (cp_parser *, enum rid);
2145 static bool cp_parser_function_transaction
2146 (cp_parser *, enum rid);
2147 static tree cp_parser_transaction_cancel
2148 (cp_parser *);
2150 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2151 static bool cp_parser_pragma
2152 (cp_parser *, enum pragma_context);
2154 /* Objective-C++ Productions */
2156 static tree cp_parser_objc_message_receiver
2157 (cp_parser *);
2158 static tree cp_parser_objc_message_args
2159 (cp_parser *);
2160 static tree cp_parser_objc_message_expression
2161 (cp_parser *);
2162 static tree cp_parser_objc_encode_expression
2163 (cp_parser *);
2164 static tree cp_parser_objc_defs_expression
2165 (cp_parser *);
2166 static tree cp_parser_objc_protocol_expression
2167 (cp_parser *);
2168 static tree cp_parser_objc_selector_expression
2169 (cp_parser *);
2170 static tree cp_parser_objc_expression
2171 (cp_parser *);
2172 static bool cp_parser_objc_selector_p
2173 (enum cpp_ttype);
2174 static tree cp_parser_objc_selector
2175 (cp_parser *);
2176 static tree cp_parser_objc_protocol_refs_opt
2177 (cp_parser *);
2178 static void cp_parser_objc_declaration
2179 (cp_parser *, tree);
2180 static tree cp_parser_objc_statement
2181 (cp_parser *);
2182 static bool cp_parser_objc_valid_prefix_attributes
2183 (cp_parser *, tree *);
2184 static void cp_parser_objc_at_property_declaration
2185 (cp_parser *) ;
2186 static void cp_parser_objc_at_synthesize_declaration
2187 (cp_parser *) ;
2188 static void cp_parser_objc_at_dynamic_declaration
2189 (cp_parser *) ;
2190 static tree cp_parser_objc_struct_declaration
2191 (cp_parser *) ;
2193 /* Utility Routines */
2195 static tree cp_parser_lookup_name
2196 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2197 static tree cp_parser_lookup_name_simple
2198 (cp_parser *, tree, location_t);
2199 static tree cp_parser_maybe_treat_template_as_class
2200 (tree, bool);
2201 static bool cp_parser_check_declarator_template_parameters
2202 (cp_parser *, cp_declarator *, location_t);
2203 static bool cp_parser_check_template_parameters
2204 (cp_parser *, unsigned, location_t, cp_declarator *);
2205 static tree cp_parser_simple_cast_expression
2206 (cp_parser *);
2207 static tree cp_parser_global_scope_opt
2208 (cp_parser *, bool);
2209 static bool cp_parser_constructor_declarator_p
2210 (cp_parser *, bool);
2211 static tree cp_parser_function_definition_from_specifiers_and_declarator
2212 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2213 static tree cp_parser_function_definition_after_declarator
2214 (cp_parser *, bool);
2215 static void cp_parser_template_declaration_after_export
2216 (cp_parser *, bool);
2217 static void cp_parser_perform_template_parameter_access_checks
2218 (vec<deferred_access_check, va_gc> *);
2219 static tree cp_parser_single_declaration
2220 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2221 static tree cp_parser_functional_cast
2222 (cp_parser *, tree);
2223 static tree cp_parser_save_member_function_body
2224 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2225 static tree cp_parser_save_nsdmi
2226 (cp_parser *);
2227 static tree cp_parser_enclosed_template_argument_list
2228 (cp_parser *);
2229 static void cp_parser_save_default_args
2230 (cp_parser *, tree);
2231 static void cp_parser_late_parsing_for_member
2232 (cp_parser *, tree);
2233 static tree cp_parser_late_parse_one_default_arg
2234 (cp_parser *, tree, tree, tree);
2235 static void cp_parser_late_parsing_nsdmi
2236 (cp_parser *, tree);
2237 static void cp_parser_late_parsing_default_args
2238 (cp_parser *, tree);
2239 static tree cp_parser_sizeof_operand
2240 (cp_parser *, enum rid);
2241 static tree cp_parser_trait_expr
2242 (cp_parser *, enum rid);
2243 static bool cp_parser_declares_only_class_p
2244 (cp_parser *);
2245 static void cp_parser_set_storage_class
2246 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2247 static void cp_parser_set_decl_spec_type
2248 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2249 static void set_and_check_decl_spec_loc
2250 (cp_decl_specifier_seq *decl_specs,
2251 cp_decl_spec ds, cp_token *);
2252 static bool cp_parser_friend_p
2253 (const cp_decl_specifier_seq *);
2254 static void cp_parser_required_error
2255 (cp_parser *, required_token, bool);
2256 static cp_token *cp_parser_require
2257 (cp_parser *, enum cpp_ttype, required_token);
2258 static cp_token *cp_parser_require_keyword
2259 (cp_parser *, enum rid, required_token);
2260 static bool cp_parser_token_starts_function_definition_p
2261 (cp_token *);
2262 static bool cp_parser_next_token_starts_class_definition_p
2263 (cp_parser *);
2264 static bool cp_parser_next_token_ends_template_argument_p
2265 (cp_parser *);
2266 static bool cp_parser_nth_token_starts_template_argument_list_p
2267 (cp_parser *, size_t);
2268 static enum tag_types cp_parser_token_is_class_key
2269 (cp_token *);
2270 static void cp_parser_check_class_key
2271 (enum tag_types, tree type);
2272 static void cp_parser_check_access_in_redeclaration
2273 (tree type, location_t location);
2274 static bool cp_parser_optional_template_keyword
2275 (cp_parser *);
2276 static void cp_parser_pre_parsed_nested_name_specifier
2277 (cp_parser *);
2278 static bool cp_parser_cache_group
2279 (cp_parser *, enum cpp_ttype, unsigned);
2280 static tree cp_parser_cache_defarg
2281 (cp_parser *parser, bool nsdmi);
2282 static void cp_parser_parse_tentatively
2283 (cp_parser *);
2284 static void cp_parser_commit_to_tentative_parse
2285 (cp_parser *);
2286 static void cp_parser_abort_tentative_parse
2287 (cp_parser *);
2288 static bool cp_parser_parse_definitely
2289 (cp_parser *);
2290 static inline bool cp_parser_parsing_tentatively
2291 (cp_parser *);
2292 static bool cp_parser_uncommitted_to_tentative_parse_p
2293 (cp_parser *);
2294 static void cp_parser_error
2295 (cp_parser *, const char *);
2296 static void cp_parser_name_lookup_error
2297 (cp_parser *, tree, tree, name_lookup_error, location_t);
2298 static bool cp_parser_simulate_error
2299 (cp_parser *);
2300 static bool cp_parser_check_type_definition
2301 (cp_parser *);
2302 static void cp_parser_check_for_definition_in_return_type
2303 (cp_declarator *, tree, location_t type_location);
2304 static void cp_parser_check_for_invalid_template_id
2305 (cp_parser *, tree, enum tag_types, location_t location);
2306 static bool cp_parser_non_integral_constant_expression
2307 (cp_parser *, non_integral_constant);
2308 static void cp_parser_diagnose_invalid_type_name
2309 (cp_parser *, tree, tree, location_t);
2310 static bool cp_parser_parse_and_diagnose_invalid_type_name
2311 (cp_parser *);
2312 static int cp_parser_skip_to_closing_parenthesis
2313 (cp_parser *, bool, bool, bool);
2314 static void cp_parser_skip_to_end_of_statement
2315 (cp_parser *);
2316 static void cp_parser_consume_semicolon_at_end_of_statement
2317 (cp_parser *);
2318 static void cp_parser_skip_to_end_of_block_or_statement
2319 (cp_parser *);
2320 static bool cp_parser_skip_to_closing_brace
2321 (cp_parser *);
2322 static void cp_parser_skip_to_end_of_template_parameter_list
2323 (cp_parser *);
2324 static void cp_parser_skip_to_pragma_eol
2325 (cp_parser*, cp_token *);
2326 static bool cp_parser_error_occurred
2327 (cp_parser *);
2328 static bool cp_parser_allow_gnu_extensions_p
2329 (cp_parser *);
2330 static bool cp_parser_is_pure_string_literal
2331 (cp_token *);
2332 static bool cp_parser_is_string_literal
2333 (cp_token *);
2334 static bool cp_parser_is_keyword
2335 (cp_token *, enum rid);
2336 static tree cp_parser_make_typename_type
2337 (cp_parser *, tree, tree, location_t location);
2338 static cp_declarator * cp_parser_make_indirect_declarator
2339 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2341 /* Returns nonzero if we are parsing tentatively. */
2343 static inline bool
2344 cp_parser_parsing_tentatively (cp_parser* parser)
2346 return parser->context->next != NULL;
2349 /* Returns nonzero if TOKEN is a string literal. */
2351 static bool
2352 cp_parser_is_pure_string_literal (cp_token* token)
2354 return (token->type == CPP_STRING ||
2355 token->type == CPP_STRING16 ||
2356 token->type == CPP_STRING32 ||
2357 token->type == CPP_WSTRING ||
2358 token->type == CPP_UTF8STRING);
2361 /* Returns nonzero if TOKEN is a string literal
2362 of a user-defined string literal. */
2364 static bool
2365 cp_parser_is_string_literal (cp_token* token)
2367 return (cp_parser_is_pure_string_literal (token) ||
2368 token->type == CPP_STRING_USERDEF ||
2369 token->type == CPP_STRING16_USERDEF ||
2370 token->type == CPP_STRING32_USERDEF ||
2371 token->type == CPP_WSTRING_USERDEF ||
2372 token->type == CPP_UTF8STRING_USERDEF);
2375 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2377 static bool
2378 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2380 return token->keyword == keyword;
2383 /* If not parsing tentatively, issue a diagnostic of the form
2384 FILE:LINE: MESSAGE before TOKEN
2385 where TOKEN is the next token in the input stream. MESSAGE
2386 (specified by the caller) is usually of the form "expected
2387 OTHER-TOKEN". */
2389 static void
2390 cp_parser_error (cp_parser* parser, const char* gmsgid)
2392 if (!cp_parser_simulate_error (parser))
2394 cp_token *token = cp_lexer_peek_token (parser->lexer);
2395 /* This diagnostic makes more sense if it is tagged to the line
2396 of the token we just peeked at. */
2397 cp_lexer_set_source_position_from_token (token);
2399 if (token->type == CPP_PRAGMA)
2401 error_at (token->location,
2402 "%<#pragma%> is not allowed here");
2403 cp_parser_skip_to_pragma_eol (parser, token);
2404 return;
2407 c_parse_error (gmsgid,
2408 /* Because c_parser_error does not understand
2409 CPP_KEYWORD, keywords are treated like
2410 identifiers. */
2411 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2412 token->u.value, token->flags);
2416 /* Issue an error about name-lookup failing. NAME is the
2417 IDENTIFIER_NODE DECL is the result of
2418 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2419 the thing that we hoped to find. */
2421 static void
2422 cp_parser_name_lookup_error (cp_parser* parser,
2423 tree name,
2424 tree decl,
2425 name_lookup_error desired,
2426 location_t location)
2428 /* If name lookup completely failed, tell the user that NAME was not
2429 declared. */
2430 if (decl == error_mark_node)
2432 if (parser->scope && parser->scope != global_namespace)
2433 error_at (location, "%<%E::%E%> has not been declared",
2434 parser->scope, name);
2435 else if (parser->scope == global_namespace)
2436 error_at (location, "%<::%E%> has not been declared", name);
2437 else if (parser->object_scope
2438 && !CLASS_TYPE_P (parser->object_scope))
2439 error_at (location, "request for member %qE in non-class type %qT",
2440 name, parser->object_scope);
2441 else if (parser->object_scope)
2442 error_at (location, "%<%T::%E%> has not been declared",
2443 parser->object_scope, name);
2444 else
2445 error_at (location, "%qE has not been declared", name);
2447 else if (parser->scope && parser->scope != global_namespace)
2449 switch (desired)
2451 case NLE_TYPE:
2452 error_at (location, "%<%E::%E%> is not a type",
2453 parser->scope, name);
2454 break;
2455 case NLE_CXX98:
2456 error_at (location, "%<%E::%E%> is not a class or namespace",
2457 parser->scope, name);
2458 break;
2459 case NLE_NOT_CXX98:
2460 error_at (location,
2461 "%<%E::%E%> is not a class, namespace, or enumeration",
2462 parser->scope, name);
2463 break;
2464 default:
2465 gcc_unreachable ();
2469 else if (parser->scope == global_namespace)
2471 switch (desired)
2473 case NLE_TYPE:
2474 error_at (location, "%<::%E%> is not a type", name);
2475 break;
2476 case NLE_CXX98:
2477 error_at (location, "%<::%E%> is not a class or namespace", name);
2478 break;
2479 case NLE_NOT_CXX98:
2480 error_at (location,
2481 "%<::%E%> is not a class, namespace, or enumeration",
2482 name);
2483 break;
2484 default:
2485 gcc_unreachable ();
2488 else
2490 switch (desired)
2492 case NLE_TYPE:
2493 error_at (location, "%qE is not a type", name);
2494 break;
2495 case NLE_CXX98:
2496 error_at (location, "%qE is not a class or namespace", name);
2497 break;
2498 case NLE_NOT_CXX98:
2499 error_at (location,
2500 "%qE is not a class, namespace, or enumeration", name);
2501 break;
2502 default:
2503 gcc_unreachable ();
2508 /* If we are parsing tentatively, remember that an error has occurred
2509 during this tentative parse. Returns true if the error was
2510 simulated; false if a message should be issued by the caller. */
2512 static bool
2513 cp_parser_simulate_error (cp_parser* parser)
2515 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2517 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2518 return true;
2520 return false;
2523 /* This function is called when a type is defined. If type
2524 definitions are forbidden at this point, an error message is
2525 issued. */
2527 static bool
2528 cp_parser_check_type_definition (cp_parser* parser)
2530 /* If types are forbidden here, issue a message. */
2531 if (parser->type_definition_forbidden_message)
2533 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2534 in the message need to be interpreted. */
2535 error (parser->type_definition_forbidden_message);
2536 return false;
2538 return true;
2541 /* This function is called when the DECLARATOR is processed. The TYPE
2542 was a type defined in the decl-specifiers. If it is invalid to
2543 define a type in the decl-specifiers for DECLARATOR, an error is
2544 issued. TYPE_LOCATION is the location of TYPE and is used
2545 for error reporting. */
2547 static void
2548 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2549 tree type, location_t type_location)
2551 /* [dcl.fct] forbids type definitions in return types.
2552 Unfortunately, it's not easy to know whether or not we are
2553 processing a return type until after the fact. */
2554 while (declarator
2555 && (declarator->kind == cdk_pointer
2556 || declarator->kind == cdk_reference
2557 || declarator->kind == cdk_ptrmem))
2558 declarator = declarator->declarator;
2559 if (declarator
2560 && declarator->kind == cdk_function)
2562 error_at (type_location,
2563 "new types may not be defined in a return type");
2564 inform (type_location,
2565 "(perhaps a semicolon is missing after the definition of %qT)",
2566 type);
2570 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2571 "<" in any valid C++ program. If the next token is indeed "<",
2572 issue a message warning the user about what appears to be an
2573 invalid attempt to form a template-id. LOCATION is the location
2574 of the type-specifier (TYPE) */
2576 static void
2577 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2578 tree type,
2579 enum tag_types tag_type,
2580 location_t location)
2582 cp_token_position start = 0;
2584 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2586 if (TYPE_P (type))
2587 error_at (location, "%qT is not a template", type);
2588 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2590 if (tag_type != none_type)
2591 error_at (location, "%qE is not a class template", type);
2592 else
2593 error_at (location, "%qE is not a template", type);
2595 else
2596 error_at (location, "invalid template-id");
2597 /* Remember the location of the invalid "<". */
2598 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2599 start = cp_lexer_token_position (parser->lexer, true);
2600 /* Consume the "<". */
2601 cp_lexer_consume_token (parser->lexer);
2602 /* Parse the template arguments. */
2603 cp_parser_enclosed_template_argument_list (parser);
2604 /* Permanently remove the invalid template arguments so that
2605 this error message is not issued again. */
2606 if (start)
2607 cp_lexer_purge_tokens_after (parser->lexer, start);
2611 /* If parsing an integral constant-expression, issue an error message
2612 about the fact that THING appeared and return true. Otherwise,
2613 return false. In either case, set
2614 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2616 static bool
2617 cp_parser_non_integral_constant_expression (cp_parser *parser,
2618 non_integral_constant thing)
2620 parser->non_integral_constant_expression_p = true;
2621 if (parser->integral_constant_expression_p)
2623 if (!parser->allow_non_integral_constant_expression_p)
2625 const char *msg = NULL;
2626 switch (thing)
2628 case NIC_FLOAT:
2629 error ("floating-point literal "
2630 "cannot appear in a constant-expression");
2631 return true;
2632 case NIC_CAST:
2633 error ("a cast to a type other than an integral or "
2634 "enumeration type cannot appear in a "
2635 "constant-expression");
2636 return true;
2637 case NIC_TYPEID:
2638 error ("%<typeid%> operator "
2639 "cannot appear in a constant-expression");
2640 return true;
2641 case NIC_NCC:
2642 error ("non-constant compound literals "
2643 "cannot appear in a constant-expression");
2644 return true;
2645 case NIC_FUNC_CALL:
2646 error ("a function call "
2647 "cannot appear in a constant-expression");
2648 return true;
2649 case NIC_INC:
2650 error ("an increment "
2651 "cannot appear in a constant-expression");
2652 return true;
2653 case NIC_DEC:
2654 error ("an decrement "
2655 "cannot appear in a constant-expression");
2656 return true;
2657 case NIC_ARRAY_REF:
2658 error ("an array reference "
2659 "cannot appear in a constant-expression");
2660 return true;
2661 case NIC_ADDR_LABEL:
2662 error ("the address of a label "
2663 "cannot appear in a constant-expression");
2664 return true;
2665 case NIC_OVERLOADED:
2666 error ("calls to overloaded operators "
2667 "cannot appear in a constant-expression");
2668 return true;
2669 case NIC_ASSIGNMENT:
2670 error ("an assignment cannot appear in a constant-expression");
2671 return true;
2672 case NIC_COMMA:
2673 error ("a comma operator "
2674 "cannot appear in a constant-expression");
2675 return true;
2676 case NIC_CONSTRUCTOR:
2677 error ("a call to a constructor "
2678 "cannot appear in a constant-expression");
2679 return true;
2680 case NIC_TRANSACTION:
2681 error ("a transaction expression "
2682 "cannot appear in a constant-expression");
2683 return true;
2684 case NIC_THIS:
2685 msg = "this";
2686 break;
2687 case NIC_FUNC_NAME:
2688 msg = "__FUNCTION__";
2689 break;
2690 case NIC_PRETTY_FUNC:
2691 msg = "__PRETTY_FUNCTION__";
2692 break;
2693 case NIC_C99_FUNC:
2694 msg = "__func__";
2695 break;
2696 case NIC_VA_ARG:
2697 msg = "va_arg";
2698 break;
2699 case NIC_ARROW:
2700 msg = "->";
2701 break;
2702 case NIC_POINT:
2703 msg = ".";
2704 break;
2705 case NIC_STAR:
2706 msg = "*";
2707 break;
2708 case NIC_ADDR:
2709 msg = "&";
2710 break;
2711 case NIC_PREINCREMENT:
2712 msg = "++";
2713 break;
2714 case NIC_PREDECREMENT:
2715 msg = "--";
2716 break;
2717 case NIC_NEW:
2718 msg = "new";
2719 break;
2720 case NIC_DEL:
2721 msg = "delete";
2722 break;
2723 default:
2724 gcc_unreachable ();
2726 if (msg)
2727 error ("%qs cannot appear in a constant-expression", msg);
2728 return true;
2731 return false;
2734 /* Emit a diagnostic for an invalid type name. SCOPE is the
2735 qualifying scope (or NULL, if none) for ID. This function commits
2736 to the current active tentative parse, if any. (Otherwise, the
2737 problematic construct might be encountered again later, resulting
2738 in duplicate error messages.) LOCATION is the location of ID. */
2740 static void
2741 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2742 tree scope, tree id,
2743 location_t location)
2745 tree decl, old_scope;
2746 cp_parser_commit_to_tentative_parse (parser);
2747 /* Try to lookup the identifier. */
2748 old_scope = parser->scope;
2749 parser->scope = scope;
2750 decl = cp_parser_lookup_name_simple (parser, id, location);
2751 parser->scope = old_scope;
2752 /* If the lookup found a template-name, it means that the user forgot
2753 to specify an argument list. Emit a useful error message. */
2754 if (TREE_CODE (decl) == TEMPLATE_DECL)
2755 error_at (location,
2756 "invalid use of template-name %qE without an argument list",
2757 decl);
2758 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2759 error_at (location, "invalid use of destructor %qD as a type", id);
2760 else if (TREE_CODE (decl) == TYPE_DECL)
2761 /* Something like 'unsigned A a;' */
2762 error_at (location, "invalid combination of multiple type-specifiers");
2763 else if (!parser->scope)
2765 /* Issue an error message. */
2766 error_at (location, "%qE does not name a type", id);
2767 /* If we're in a template class, it's possible that the user was
2768 referring to a type from a base class. For example:
2770 template <typename T> struct A { typedef T X; };
2771 template <typename T> struct B : public A<T> { X x; };
2773 The user should have said "typename A<T>::X". */
2774 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2775 inform (location, "C++11 %<constexpr%> only available with "
2776 "-std=c++11 or -std=gnu++11");
2777 else if (processing_template_decl && current_class_type
2778 && TYPE_BINFO (current_class_type))
2780 tree b;
2782 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2784 b = TREE_CHAIN (b))
2786 tree base_type = BINFO_TYPE (b);
2787 if (CLASS_TYPE_P (base_type)
2788 && dependent_type_p (base_type))
2790 tree field;
2791 /* Go from a particular instantiation of the
2792 template (which will have an empty TYPE_FIELDs),
2793 to the main version. */
2794 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2795 for (field = TYPE_FIELDS (base_type);
2796 field;
2797 field = DECL_CHAIN (field))
2798 if (TREE_CODE (field) == TYPE_DECL
2799 && DECL_NAME (field) == id)
2801 inform (location,
2802 "(perhaps %<typename %T::%E%> was intended)",
2803 BINFO_TYPE (b), id);
2804 break;
2806 if (field)
2807 break;
2812 /* Here we diagnose qualified-ids where the scope is actually correct,
2813 but the identifier does not resolve to a valid type name. */
2814 else if (parser->scope != error_mark_node)
2816 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2817 error_at (location, "%qE in namespace %qE does not name a type",
2818 id, parser->scope);
2819 else if (CLASS_TYPE_P (parser->scope)
2820 && constructor_name_p (id, parser->scope))
2822 /* A<T>::A<T>() */
2823 error_at (location, "%<%T::%E%> names the constructor, not"
2824 " the type", parser->scope, id);
2825 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2826 error_at (location, "and %qT has no template constructors",
2827 parser->scope);
2829 else if (TYPE_P (parser->scope)
2830 && dependent_scope_p (parser->scope))
2831 error_at (location, "need %<typename%> before %<%T::%E%> because "
2832 "%qT is a dependent scope",
2833 parser->scope, id, parser->scope);
2834 else if (TYPE_P (parser->scope))
2835 error_at (location, "%qE in %q#T does not name a type",
2836 id, parser->scope);
2837 else
2838 gcc_unreachable ();
2842 /* Check for a common situation where a type-name should be present,
2843 but is not, and issue a sensible error message. Returns true if an
2844 invalid type-name was detected.
2846 The situation handled by this function are variable declarations of the
2847 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2848 Usually, `ID' should name a type, but if we got here it means that it
2849 does not. We try to emit the best possible error message depending on
2850 how exactly the id-expression looks like. */
2852 static bool
2853 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2855 tree id;
2856 cp_token *token = cp_lexer_peek_token (parser->lexer);
2858 /* Avoid duplicate error about ambiguous lookup. */
2859 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2861 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2862 if (next->type == CPP_NAME && next->ambiguous_p)
2863 goto out;
2866 cp_parser_parse_tentatively (parser);
2867 id = cp_parser_id_expression (parser,
2868 /*template_keyword_p=*/false,
2869 /*check_dependency_p=*/true,
2870 /*template_p=*/NULL,
2871 /*declarator_p=*/true,
2872 /*optional_p=*/false);
2873 /* If the next token is a (, this is a function with no explicit return
2874 type, i.e. constructor, destructor or conversion op. */
2875 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2876 || TREE_CODE (id) == TYPE_DECL)
2878 cp_parser_abort_tentative_parse (parser);
2879 return false;
2881 if (!cp_parser_parse_definitely (parser))
2882 return false;
2884 /* Emit a diagnostic for the invalid type. */
2885 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2886 id, token->location);
2887 out:
2888 /* If we aren't in the middle of a declarator (i.e. in a
2889 parameter-declaration-clause), skip to the end of the declaration;
2890 there's no point in trying to process it. */
2891 if (!parser->in_declarator_p)
2892 cp_parser_skip_to_end_of_block_or_statement (parser);
2893 return true;
2896 /* Consume tokens up to, and including, the next non-nested closing `)'.
2897 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2898 are doing error recovery. Returns -1 if OR_COMMA is true and we
2899 found an unnested comma. */
2901 static int
2902 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2903 bool recovering,
2904 bool or_comma,
2905 bool consume_paren)
2907 unsigned paren_depth = 0;
2908 unsigned brace_depth = 0;
2909 unsigned square_depth = 0;
2911 if (recovering && !or_comma
2912 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2913 return 0;
2915 while (true)
2917 cp_token * token = cp_lexer_peek_token (parser->lexer);
2919 switch (token->type)
2921 case CPP_EOF:
2922 case CPP_PRAGMA_EOL:
2923 /* If we've run out of tokens, then there is no closing `)'. */
2924 return 0;
2926 /* This is good for lambda expression capture-lists. */
2927 case CPP_OPEN_SQUARE:
2928 ++square_depth;
2929 break;
2930 case CPP_CLOSE_SQUARE:
2931 if (!square_depth--)
2932 return 0;
2933 break;
2935 case CPP_SEMICOLON:
2936 /* This matches the processing in skip_to_end_of_statement. */
2937 if (!brace_depth)
2938 return 0;
2939 break;
2941 case CPP_OPEN_BRACE:
2942 ++brace_depth;
2943 break;
2944 case CPP_CLOSE_BRACE:
2945 if (!brace_depth--)
2946 return 0;
2947 break;
2949 case CPP_COMMA:
2950 if (recovering && or_comma && !brace_depth && !paren_depth
2951 && !square_depth)
2952 return -1;
2953 break;
2955 case CPP_OPEN_PAREN:
2956 if (!brace_depth)
2957 ++paren_depth;
2958 break;
2960 case CPP_CLOSE_PAREN:
2961 if (!brace_depth && !paren_depth--)
2963 if (consume_paren)
2964 cp_lexer_consume_token (parser->lexer);
2965 return 1;
2967 break;
2969 default:
2970 break;
2973 /* Consume the token. */
2974 cp_lexer_consume_token (parser->lexer);
2978 /* Consume tokens until we reach the end of the current statement.
2979 Normally, that will be just before consuming a `;'. However, if a
2980 non-nested `}' comes first, then we stop before consuming that. */
2982 static void
2983 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2985 unsigned nesting_depth = 0;
2987 while (true)
2989 cp_token *token = cp_lexer_peek_token (parser->lexer);
2991 switch (token->type)
2993 case CPP_EOF:
2994 case CPP_PRAGMA_EOL:
2995 /* If we've run out of tokens, stop. */
2996 return;
2998 case CPP_SEMICOLON:
2999 /* If the next token is a `;', we have reached the end of the
3000 statement. */
3001 if (!nesting_depth)
3002 return;
3003 break;
3005 case CPP_CLOSE_BRACE:
3006 /* If this is a non-nested '}', stop before consuming it.
3007 That way, when confronted with something like:
3009 { 3 + }
3011 we stop before consuming the closing '}', even though we
3012 have not yet reached a `;'. */
3013 if (nesting_depth == 0)
3014 return;
3016 /* If it is the closing '}' for a block that we have
3017 scanned, stop -- but only after consuming the token.
3018 That way given:
3020 void f g () { ... }
3021 typedef int I;
3023 we will stop after the body of the erroneously declared
3024 function, but before consuming the following `typedef'
3025 declaration. */
3026 if (--nesting_depth == 0)
3028 cp_lexer_consume_token (parser->lexer);
3029 return;
3032 case CPP_OPEN_BRACE:
3033 ++nesting_depth;
3034 break;
3036 default:
3037 break;
3040 /* Consume the token. */
3041 cp_lexer_consume_token (parser->lexer);
3045 /* This function is called at the end of a statement or declaration.
3046 If the next token is a semicolon, it is consumed; otherwise, error
3047 recovery is attempted. */
3049 static void
3050 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3052 /* Look for the trailing `;'. */
3053 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3055 /* If there is additional (erroneous) input, skip to the end of
3056 the statement. */
3057 cp_parser_skip_to_end_of_statement (parser);
3058 /* If the next token is now a `;', consume it. */
3059 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3060 cp_lexer_consume_token (parser->lexer);
3064 /* Skip tokens until we have consumed an entire block, or until we
3065 have consumed a non-nested `;'. */
3067 static void
3068 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3070 int nesting_depth = 0;
3072 while (nesting_depth >= 0)
3074 cp_token *token = cp_lexer_peek_token (parser->lexer);
3076 switch (token->type)
3078 case CPP_EOF:
3079 case CPP_PRAGMA_EOL:
3080 /* If we've run out of tokens, stop. */
3081 return;
3083 case CPP_SEMICOLON:
3084 /* Stop if this is an unnested ';'. */
3085 if (!nesting_depth)
3086 nesting_depth = -1;
3087 break;
3089 case CPP_CLOSE_BRACE:
3090 /* Stop if this is an unnested '}', or closes the outermost
3091 nesting level. */
3092 nesting_depth--;
3093 if (nesting_depth < 0)
3094 return;
3095 if (!nesting_depth)
3096 nesting_depth = -1;
3097 break;
3099 case CPP_OPEN_BRACE:
3100 /* Nest. */
3101 nesting_depth++;
3102 break;
3104 default:
3105 break;
3108 /* Consume the token. */
3109 cp_lexer_consume_token (parser->lexer);
3113 /* Skip tokens until a non-nested closing curly brace is the next
3114 token, or there are no more tokens. Return true in the first case,
3115 false otherwise. */
3117 static bool
3118 cp_parser_skip_to_closing_brace (cp_parser *parser)
3120 unsigned nesting_depth = 0;
3122 while (true)
3124 cp_token *token = cp_lexer_peek_token (parser->lexer);
3126 switch (token->type)
3128 case CPP_EOF:
3129 case CPP_PRAGMA_EOL:
3130 /* If we've run out of tokens, stop. */
3131 return false;
3133 case CPP_CLOSE_BRACE:
3134 /* If the next token is a non-nested `}', then we have reached
3135 the end of the current block. */
3136 if (nesting_depth-- == 0)
3137 return true;
3138 break;
3140 case CPP_OPEN_BRACE:
3141 /* If it the next token is a `{', then we are entering a new
3142 block. Consume the entire block. */
3143 ++nesting_depth;
3144 break;
3146 default:
3147 break;
3150 /* Consume the token. */
3151 cp_lexer_consume_token (parser->lexer);
3155 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3156 parameter is the PRAGMA token, allowing us to purge the entire pragma
3157 sequence. */
3159 static void
3160 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3162 cp_token *token;
3164 parser->lexer->in_pragma = false;
3167 token = cp_lexer_consume_token (parser->lexer);
3168 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3170 /* Ensure that the pragma is not parsed again. */
3171 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3174 /* Require pragma end of line, resyncing with it as necessary. The
3175 arguments are as for cp_parser_skip_to_pragma_eol. */
3177 static void
3178 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3180 parser->lexer->in_pragma = false;
3181 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3182 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3185 /* This is a simple wrapper around make_typename_type. When the id is
3186 an unresolved identifier node, we can provide a superior diagnostic
3187 using cp_parser_diagnose_invalid_type_name. */
3189 static tree
3190 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3191 tree id, location_t id_location)
3193 tree result;
3194 if (TREE_CODE (id) == IDENTIFIER_NODE)
3196 result = make_typename_type (scope, id, typename_type,
3197 /*complain=*/tf_none);
3198 if (result == error_mark_node)
3199 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3200 return result;
3202 return make_typename_type (scope, id, typename_type, tf_error);
3205 /* This is a wrapper around the
3206 make_{pointer,ptrmem,reference}_declarator functions that decides
3207 which one to call based on the CODE and CLASS_TYPE arguments. The
3208 CODE argument should be one of the values returned by
3209 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3210 appertain to the pointer or reference. */
3212 static cp_declarator *
3213 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3214 cp_cv_quals cv_qualifiers,
3215 cp_declarator *target,
3216 tree attributes)
3218 if (code == ERROR_MARK)
3219 return cp_error_declarator;
3221 if (code == INDIRECT_REF)
3222 if (class_type == NULL_TREE)
3223 return make_pointer_declarator (cv_qualifiers, target, attributes);
3224 else
3225 return make_ptrmem_declarator (cv_qualifiers, class_type,
3226 target, attributes);
3227 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3228 return make_reference_declarator (cv_qualifiers, target,
3229 false, attributes);
3230 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3231 return make_reference_declarator (cv_qualifiers, target,
3232 true, attributes);
3233 gcc_unreachable ();
3236 /* Create a new C++ parser. */
3238 static cp_parser *
3239 cp_parser_new (void)
3241 cp_parser *parser;
3242 cp_lexer *lexer;
3243 unsigned i;
3245 /* cp_lexer_new_main is called before doing GC allocation because
3246 cp_lexer_new_main might load a PCH file. */
3247 lexer = cp_lexer_new_main ();
3249 /* Initialize the binops_by_token so that we can get the tree
3250 directly from the token. */
3251 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3252 binops_by_token[binops[i].token_type] = binops[i];
3254 parser = ggc_alloc_cleared_cp_parser ();
3255 parser->lexer = lexer;
3256 parser->context = cp_parser_context_new (NULL);
3258 /* For now, we always accept GNU extensions. */
3259 parser->allow_gnu_extensions_p = 1;
3261 /* The `>' token is a greater-than operator, not the end of a
3262 template-id. */
3263 parser->greater_than_is_operator_p = true;
3265 parser->default_arg_ok_p = true;
3267 /* We are not parsing a constant-expression. */
3268 parser->integral_constant_expression_p = false;
3269 parser->allow_non_integral_constant_expression_p = false;
3270 parser->non_integral_constant_expression_p = false;
3272 /* Local variable names are not forbidden. */
3273 parser->local_variables_forbidden_p = false;
3275 /* We are not processing an `extern "C"' declaration. */
3276 parser->in_unbraced_linkage_specification_p = false;
3278 /* We are not processing a declarator. */
3279 parser->in_declarator_p = false;
3281 /* We are not processing a template-argument-list. */
3282 parser->in_template_argument_list_p = false;
3284 /* We are not in an iteration statement. */
3285 parser->in_statement = 0;
3287 /* We are not in a switch statement. */
3288 parser->in_switch_statement_p = false;
3290 /* We are not parsing a type-id inside an expression. */
3291 parser->in_type_id_in_expr_p = false;
3293 /* Declarations aren't implicitly extern "C". */
3294 parser->implicit_extern_c = false;
3296 /* String literals should be translated to the execution character set. */
3297 parser->translate_strings_p = true;
3299 /* We are not parsing a function body. */
3300 parser->in_function_body = false;
3302 /* We can correct until told otherwise. */
3303 parser->colon_corrects_to_scope_p = true;
3305 /* The unparsed function queue is empty. */
3306 push_unparsed_function_queues (parser);
3308 /* There are no classes being defined. */
3309 parser->num_classes_being_defined = 0;
3311 /* No template parameters apply. */
3312 parser->num_template_parameter_lists = 0;
3314 return parser;
3317 /* Create a cp_lexer structure which will emit the tokens in CACHE
3318 and push it onto the parser's lexer stack. This is used for delayed
3319 parsing of in-class method bodies and default arguments, and should
3320 not be confused with tentative parsing. */
3321 static void
3322 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3324 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3325 lexer->next = parser->lexer;
3326 parser->lexer = lexer;
3328 /* Move the current source position to that of the first token in the
3329 new lexer. */
3330 cp_lexer_set_source_position_from_token (lexer->next_token);
3333 /* Pop the top lexer off the parser stack. This is never used for the
3334 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3335 static void
3336 cp_parser_pop_lexer (cp_parser *parser)
3338 cp_lexer *lexer = parser->lexer;
3339 parser->lexer = lexer->next;
3340 cp_lexer_destroy (lexer);
3342 /* Put the current source position back where it was before this
3343 lexer was pushed. */
3344 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3347 /* Lexical conventions [gram.lex] */
3349 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3350 identifier. */
3352 static tree
3353 cp_parser_identifier (cp_parser* parser)
3355 cp_token *token;
3357 /* Look for the identifier. */
3358 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3359 /* Return the value. */
3360 return token ? token->u.value : error_mark_node;
3363 /* Parse a sequence of adjacent string constants. Returns a
3364 TREE_STRING representing the combined, nul-terminated string
3365 constant. If TRANSLATE is true, translate the string to the
3366 execution character set. If WIDE_OK is true, a wide string is
3367 invalid here.
3369 C++98 [lex.string] says that if a narrow string literal token is
3370 adjacent to a wide string literal token, the behavior is undefined.
3371 However, C99 6.4.5p4 says that this results in a wide string literal.
3372 We follow C99 here, for consistency with the C front end.
3374 This code is largely lifted from lex_string() in c-lex.c.
3376 FUTURE: ObjC++ will need to handle @-strings here. */
3377 static tree
3378 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3380 tree value;
3381 size_t count;
3382 struct obstack str_ob;
3383 cpp_string str, istr, *strs;
3384 cp_token *tok;
3385 enum cpp_ttype type, curr_type;
3386 int have_suffix_p = 0;
3387 tree string_tree;
3388 tree suffix_id = NULL_TREE;
3389 bool curr_tok_is_userdef_p = false;
3391 tok = cp_lexer_peek_token (parser->lexer);
3392 if (!cp_parser_is_string_literal (tok))
3394 cp_parser_error (parser, "expected string-literal");
3395 return error_mark_node;
3398 if (cpp_userdef_string_p (tok->type))
3400 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3401 curr_type = cpp_userdef_string_remove_type (tok->type);
3402 curr_tok_is_userdef_p = true;
3404 else
3406 string_tree = tok->u.value;
3407 curr_type = tok->type;
3409 type = curr_type;
3411 /* Try to avoid the overhead of creating and destroying an obstack
3412 for the common case of just one string. */
3413 if (!cp_parser_is_string_literal
3414 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3416 cp_lexer_consume_token (parser->lexer);
3418 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3419 str.len = TREE_STRING_LENGTH (string_tree);
3420 count = 1;
3422 if (curr_tok_is_userdef_p)
3424 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3425 have_suffix_p = 1;
3426 curr_type = cpp_userdef_string_remove_type (tok->type);
3428 else
3429 curr_type = tok->type;
3431 strs = &str;
3433 else
3435 gcc_obstack_init (&str_ob);
3436 count = 0;
3440 cp_lexer_consume_token (parser->lexer);
3441 count++;
3442 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3443 str.len = TREE_STRING_LENGTH (string_tree);
3445 if (curr_tok_is_userdef_p)
3447 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3448 if (have_suffix_p == 0)
3450 suffix_id = curr_suffix_id;
3451 have_suffix_p = 1;
3453 else if (have_suffix_p == 1
3454 && curr_suffix_id != suffix_id)
3456 error ("inconsistent user-defined literal suffixes"
3457 " %qD and %qD in string literal",
3458 suffix_id, curr_suffix_id);
3459 have_suffix_p = -1;
3461 curr_type = cpp_userdef_string_remove_type (tok->type);
3463 else
3464 curr_type = tok->type;
3466 if (type != curr_type)
3468 if (type == CPP_STRING)
3469 type = curr_type;
3470 else if (curr_type != CPP_STRING)
3471 error_at (tok->location,
3472 "unsupported non-standard concatenation "
3473 "of string literals");
3476 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3478 tok = cp_lexer_peek_token (parser->lexer);
3479 if (cpp_userdef_string_p (tok->type))
3481 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3482 curr_type = cpp_userdef_string_remove_type (tok->type);
3483 curr_tok_is_userdef_p = true;
3485 else
3487 string_tree = tok->u.value;
3488 curr_type = tok->type;
3489 curr_tok_is_userdef_p = false;
3492 while (cp_parser_is_string_literal (tok));
3494 strs = (cpp_string *) obstack_finish (&str_ob);
3497 if (type != CPP_STRING && !wide_ok)
3499 cp_parser_error (parser, "a wide string is invalid in this context");
3500 type = CPP_STRING;
3503 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3504 (parse_in, strs, count, &istr, type))
3506 value = build_string (istr.len, (const char *)istr.text);
3507 free (CONST_CAST (unsigned char *, istr.text));
3509 switch (type)
3511 default:
3512 case CPP_STRING:
3513 case CPP_UTF8STRING:
3514 TREE_TYPE (value) = char_array_type_node;
3515 break;
3516 case CPP_STRING16:
3517 TREE_TYPE (value) = char16_array_type_node;
3518 break;
3519 case CPP_STRING32:
3520 TREE_TYPE (value) = char32_array_type_node;
3521 break;
3522 case CPP_WSTRING:
3523 TREE_TYPE (value) = wchar_array_type_node;
3524 break;
3527 value = fix_string_type (value);
3529 if (have_suffix_p)
3531 tree literal = build_userdef_literal (suffix_id, value,
3532 OT_NONE, NULL_TREE);
3533 tok->u.value = literal;
3534 return cp_parser_userdef_string_literal (tok);
3537 else
3538 /* cpp_interpret_string has issued an error. */
3539 value = error_mark_node;
3541 if (count > 1)
3542 obstack_free (&str_ob, 0);
3544 return value;
3547 /* Look up a literal operator with the name and the exact arguments. */
3549 static tree
3550 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3552 tree decl, fns;
3553 decl = lookup_name (name);
3554 if (!decl || !is_overloaded_fn (decl))
3555 return error_mark_node;
3557 for (fns = decl; fns; fns = OVL_NEXT (fns))
3559 unsigned int ix;
3560 bool found = true;
3561 tree fn = OVL_CURRENT (fns);
3562 tree argtypes = NULL_TREE;
3563 argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3564 if (argtypes != NULL_TREE)
3566 for (ix = 0; ix < vec_safe_length (args) && argtypes != NULL_TREE;
3567 ++ix, argtypes = TREE_CHAIN (argtypes))
3569 tree targ = TREE_VALUE (argtypes);
3570 tree tparm = TREE_TYPE ((*args)[ix]);
3571 bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3572 bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3573 if ((ptr || arr || !same_type_p (targ, tparm))
3574 && (!ptr || !arr
3575 || !same_type_p (TREE_TYPE (targ),
3576 TREE_TYPE (tparm))))
3577 found = false;
3579 if (found
3580 && ix == vec_safe_length (args)
3581 /* May be this should be sufficient_parms_p instead,
3582 depending on how exactly should user-defined literals
3583 work in presence of default arguments on the literal
3584 operator parameters. */
3585 && argtypes == void_list_node)
3586 return fn;
3590 return error_mark_node;
3593 /* Parse a user-defined char constant. Returns a call to a user-defined
3594 literal operator taking the character as an argument. */
3596 static tree
3597 cp_parser_userdef_char_literal (cp_parser *parser)
3599 cp_token *token = cp_lexer_consume_token (parser->lexer);
3600 tree literal = token->u.value;
3601 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3602 tree value = USERDEF_LITERAL_VALUE (literal);
3603 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3604 tree decl, result;
3606 /* Build up a call to the user-defined operator */
3607 /* Lookup the name we got back from the id-expression. */
3608 vec<tree, va_gc> *args = make_tree_vector ();
3609 vec_safe_push (args, value);
3610 decl = lookup_literal_operator (name, args);
3611 if (!decl || decl == error_mark_node)
3613 error ("unable to find character literal operator %qD with %qT argument",
3614 name, TREE_TYPE (value));
3615 release_tree_vector (args);
3616 return error_mark_node;
3618 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3619 release_tree_vector (args);
3620 if (result != error_mark_node)
3621 return result;
3623 error ("unable to find character literal operator %qD with %qT argument",
3624 name, TREE_TYPE (value));
3625 return error_mark_node;
3628 /* A subroutine of cp_parser_userdef_numeric_literal to
3629 create a char... template parameter pack from a string node. */
3631 static tree
3632 make_char_string_pack (tree value)
3634 tree charvec;
3635 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3636 const char *str = TREE_STRING_POINTER (value);
3637 int i, len = TREE_STRING_LENGTH (value) - 1;
3638 tree argvec = make_tree_vec (1);
3640 /* Fill in CHARVEC with all of the parameters. */
3641 charvec = make_tree_vec (len);
3642 for (i = 0; i < len; ++i)
3643 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3645 /* Build the argument packs. */
3646 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3647 TREE_TYPE (argpack) = char_type_node;
3649 TREE_VEC_ELT (argvec, 0) = argpack;
3651 return argvec;
3654 /* Parse a user-defined numeric constant. returns a call to a user-defined
3655 literal operator. */
3657 static tree
3658 cp_parser_userdef_numeric_literal (cp_parser *parser)
3660 cp_token *token = cp_lexer_consume_token (parser->lexer);
3661 tree literal = token->u.value;
3662 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3663 tree value = USERDEF_LITERAL_VALUE (literal);
3664 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3665 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3666 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3667 tree decl, result;
3668 vec<tree, va_gc> *args;
3670 /* Look for a literal operator taking the exact type of numeric argument
3671 as the literal value. */
3672 args = make_tree_vector ();
3673 vec_safe_push (args, value);
3674 decl = lookup_literal_operator (name, args);
3675 if (decl && decl != error_mark_node)
3677 result = finish_call_expr (decl, &args, false, true, tf_none);
3678 if (result != error_mark_node)
3680 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3681 warning_at (token->location, OPT_Woverflow,
3682 "integer literal exceeds range of %qT type",
3683 long_long_unsigned_type_node);
3684 else
3686 if (overflow > 0)
3687 warning_at (token->location, OPT_Woverflow,
3688 "floating literal exceeds range of %qT type",
3689 long_double_type_node);
3690 else if (overflow < 0)
3691 warning_at (token->location, OPT_Woverflow,
3692 "floating literal truncated to zero");
3694 release_tree_vector (args);
3695 return result;
3698 release_tree_vector (args);
3700 /* If the numeric argument didn't work, look for a raw literal
3701 operator taking a const char* argument consisting of the number
3702 in string format. */
3703 args = make_tree_vector ();
3704 vec_safe_push (args, num_string);
3705 decl = lookup_literal_operator (name, args);
3706 if (decl && decl != error_mark_node)
3708 result = finish_call_expr (decl, &args, false, true, tf_none);
3709 if (result != error_mark_node)
3711 release_tree_vector (args);
3712 return result;
3715 release_tree_vector (args);
3717 /* If the raw literal didn't work, look for a non-type template
3718 function with parameter pack char.... Call the function with
3719 template parameter characters representing the number. */
3720 args = make_tree_vector ();
3721 decl = lookup_literal_operator (name, args);
3722 if (decl && decl != error_mark_node)
3724 tree tmpl_args = make_char_string_pack (num_string);
3725 decl = lookup_template_function (decl, tmpl_args);
3726 result = finish_call_expr (decl, &args, false, true, tf_none);
3727 if (result != error_mark_node)
3729 release_tree_vector (args);
3730 return result;
3733 release_tree_vector (args);
3735 error ("unable to find numeric literal operator %qD", name);
3736 return error_mark_node;
3739 /* Parse a user-defined string constant. Returns a call to a user-defined
3740 literal operator taking a character pointer and the length of the string
3741 as arguments. */
3743 static tree
3744 cp_parser_userdef_string_literal (cp_token *token)
3746 tree literal = token->u.value;
3747 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3748 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3749 tree value = USERDEF_LITERAL_VALUE (literal);
3750 int len = TREE_STRING_LENGTH (value)
3751 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3752 tree decl, result;
3754 /* Build up a call to the user-defined operator */
3755 /* Lookup the name we got back from the id-expression. */
3756 vec<tree, va_gc> *args = make_tree_vector ();
3757 vec_safe_push (args, value);
3758 vec_safe_push (args, build_int_cst (size_type_node, len));
3759 decl = lookup_name (name);
3760 if (!decl || decl == error_mark_node)
3762 error ("unable to find string literal operator %qD", name);
3763 release_tree_vector (args);
3764 return error_mark_node;
3766 result = finish_call_expr (decl, &args, false, true, tf_none);
3767 release_tree_vector (args);
3768 if (result != error_mark_node)
3769 return result;
3771 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3772 name, TREE_TYPE (value), size_type_node);
3773 return error_mark_node;
3777 /* Basic concepts [gram.basic] */
3779 /* Parse a translation-unit.
3781 translation-unit:
3782 declaration-seq [opt]
3784 Returns TRUE if all went well. */
3786 static bool
3787 cp_parser_translation_unit (cp_parser* parser)
3789 /* The address of the first non-permanent object on the declarator
3790 obstack. */
3791 static void *declarator_obstack_base;
3793 bool success;
3795 /* Create the declarator obstack, if necessary. */
3796 if (!cp_error_declarator)
3798 gcc_obstack_init (&declarator_obstack);
3799 /* Create the error declarator. */
3800 cp_error_declarator = make_declarator (cdk_error);
3801 /* Create the empty parameter list. */
3802 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3803 /* Remember where the base of the declarator obstack lies. */
3804 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3807 cp_parser_declaration_seq_opt (parser);
3809 /* If there are no tokens left then all went well. */
3810 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3812 /* Get rid of the token array; we don't need it any more. */
3813 cp_lexer_destroy (parser->lexer);
3814 parser->lexer = NULL;
3816 /* This file might have been a context that's implicitly extern
3817 "C". If so, pop the lang context. (Only relevant for PCH.) */
3818 if (parser->implicit_extern_c)
3820 pop_lang_context ();
3821 parser->implicit_extern_c = false;
3824 /* Finish up. */
3825 finish_translation_unit ();
3827 success = true;
3829 else
3831 cp_parser_error (parser, "expected declaration");
3832 success = false;
3835 /* Make sure the declarator obstack was fully cleaned up. */
3836 gcc_assert (obstack_next_free (&declarator_obstack)
3837 == declarator_obstack_base);
3839 /* All went well. */
3840 return success;
3843 /* Expressions [gram.expr] */
3845 /* Parse a primary-expression.
3847 primary-expression:
3848 literal
3849 this
3850 ( expression )
3851 id-expression
3853 GNU Extensions:
3855 primary-expression:
3856 ( compound-statement )
3857 __builtin_va_arg ( assignment-expression , type-id )
3858 __builtin_offsetof ( type-id , offsetof-expression )
3860 C++ Extensions:
3861 __has_nothrow_assign ( type-id )
3862 __has_nothrow_constructor ( type-id )
3863 __has_nothrow_copy ( type-id )
3864 __has_trivial_assign ( type-id )
3865 __has_trivial_constructor ( type-id )
3866 __has_trivial_copy ( type-id )
3867 __has_trivial_destructor ( type-id )
3868 __has_virtual_destructor ( type-id )
3869 __is_abstract ( type-id )
3870 __is_base_of ( type-id , type-id )
3871 __is_class ( type-id )
3872 __is_convertible_to ( type-id , type-id )
3873 __is_empty ( type-id )
3874 __is_enum ( type-id )
3875 __is_final ( type-id )
3876 __is_literal_type ( type-id )
3877 __is_pod ( type-id )
3878 __is_polymorphic ( type-id )
3879 __is_std_layout ( type-id )
3880 __is_trivial ( type-id )
3881 __is_union ( type-id )
3883 Objective-C++ Extension:
3885 primary-expression:
3886 objc-expression
3888 literal:
3889 __null
3891 ADDRESS_P is true iff this expression was immediately preceded by
3892 "&" and therefore might denote a pointer-to-member. CAST_P is true
3893 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3894 true iff this expression is a template argument.
3896 Returns a representation of the expression. Upon return, *IDK
3897 indicates what kind of id-expression (if any) was present. */
3899 static tree
3900 cp_parser_primary_expression (cp_parser *parser,
3901 bool address_p,
3902 bool cast_p,
3903 bool template_arg_p,
3904 cp_id_kind *idk)
3906 cp_token *token = NULL;
3908 /* Assume the primary expression is not an id-expression. */
3909 *idk = CP_ID_KIND_NONE;
3911 /* Peek at the next token. */
3912 token = cp_lexer_peek_token (parser->lexer);
3913 switch (token->type)
3915 /* literal:
3916 integer-literal
3917 character-literal
3918 floating-literal
3919 string-literal
3920 boolean-literal
3921 pointer-literal
3922 user-defined-literal */
3923 case CPP_CHAR:
3924 case CPP_CHAR16:
3925 case CPP_CHAR32:
3926 case CPP_WCHAR:
3927 case CPP_NUMBER:
3928 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3929 return cp_parser_userdef_numeric_literal (parser);
3930 token = cp_lexer_consume_token (parser->lexer);
3931 if (TREE_CODE (token->u.value) == FIXED_CST)
3933 error_at (token->location,
3934 "fixed-point types not supported in C++");
3935 return error_mark_node;
3937 /* Floating-point literals are only allowed in an integral
3938 constant expression if they are cast to an integral or
3939 enumeration type. */
3940 if (TREE_CODE (token->u.value) == REAL_CST
3941 && parser->integral_constant_expression_p
3942 && pedantic)
3944 /* CAST_P will be set even in invalid code like "int(2.7 +
3945 ...)". Therefore, we have to check that the next token
3946 is sure to end the cast. */
3947 if (cast_p)
3949 cp_token *next_token;
3951 next_token = cp_lexer_peek_token (parser->lexer);
3952 if (/* The comma at the end of an
3953 enumerator-definition. */
3954 next_token->type != CPP_COMMA
3955 /* The curly brace at the end of an enum-specifier. */
3956 && next_token->type != CPP_CLOSE_BRACE
3957 /* The end of a statement. */
3958 && next_token->type != CPP_SEMICOLON
3959 /* The end of the cast-expression. */
3960 && next_token->type != CPP_CLOSE_PAREN
3961 /* The end of an array bound. */
3962 && next_token->type != CPP_CLOSE_SQUARE
3963 /* The closing ">" in a template-argument-list. */
3964 && (next_token->type != CPP_GREATER
3965 || parser->greater_than_is_operator_p)
3966 /* C++0x only: A ">>" treated like two ">" tokens,
3967 in a template-argument-list. */
3968 && (next_token->type != CPP_RSHIFT
3969 || (cxx_dialect == cxx98)
3970 || parser->greater_than_is_operator_p))
3971 cast_p = false;
3974 /* If we are within a cast, then the constraint that the
3975 cast is to an integral or enumeration type will be
3976 checked at that point. If we are not within a cast, then
3977 this code is invalid. */
3978 if (!cast_p)
3979 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3981 return token->u.value;
3983 case CPP_CHAR_USERDEF:
3984 case CPP_CHAR16_USERDEF:
3985 case CPP_CHAR32_USERDEF:
3986 case CPP_WCHAR_USERDEF:
3987 return cp_parser_userdef_char_literal (parser);
3989 case CPP_STRING:
3990 case CPP_STRING16:
3991 case CPP_STRING32:
3992 case CPP_WSTRING:
3993 case CPP_UTF8STRING:
3994 case CPP_STRING_USERDEF:
3995 case CPP_STRING16_USERDEF:
3996 case CPP_STRING32_USERDEF:
3997 case CPP_WSTRING_USERDEF:
3998 case CPP_UTF8STRING_USERDEF:
3999 /* ??? Should wide strings be allowed when parser->translate_strings_p
4000 is false (i.e. in attributes)? If not, we can kill the third
4001 argument to cp_parser_string_literal. */
4002 return cp_parser_string_literal (parser,
4003 parser->translate_strings_p,
4004 true);
4006 case CPP_OPEN_PAREN:
4008 tree expr;
4009 bool saved_greater_than_is_operator_p;
4011 /* Consume the `('. */
4012 cp_lexer_consume_token (parser->lexer);
4013 /* Within a parenthesized expression, a `>' token is always
4014 the greater-than operator. */
4015 saved_greater_than_is_operator_p
4016 = parser->greater_than_is_operator_p;
4017 parser->greater_than_is_operator_p = true;
4018 /* If we see `( { ' then we are looking at the beginning of
4019 a GNU statement-expression. */
4020 if (cp_parser_allow_gnu_extensions_p (parser)
4021 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4023 /* Statement-expressions are not allowed by the standard. */
4024 pedwarn (token->location, OPT_Wpedantic,
4025 "ISO C++ forbids braced-groups within expressions");
4027 /* And they're not allowed outside of a function-body; you
4028 cannot, for example, write:
4030 int i = ({ int j = 3; j + 1; });
4032 at class or namespace scope. */
4033 if (!parser->in_function_body
4034 || parser->in_template_argument_list_p)
4036 error_at (token->location,
4037 "statement-expressions are not allowed outside "
4038 "functions nor in template-argument lists");
4039 cp_parser_skip_to_end_of_block_or_statement (parser);
4040 expr = error_mark_node;
4042 else
4044 /* Start the statement-expression. */
4045 expr = begin_stmt_expr ();
4046 /* Parse the compound-statement. */
4047 cp_parser_compound_statement (parser, expr, false, false);
4048 /* Finish up. */
4049 expr = finish_stmt_expr (expr, false);
4052 else
4054 /* Parse the parenthesized expression. */
4055 expr = cp_parser_expression (parser, cast_p, idk);
4056 /* Let the front end know that this expression was
4057 enclosed in parentheses. This matters in case, for
4058 example, the expression is of the form `A::B', since
4059 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4060 not. */
4061 finish_parenthesized_expr (expr);
4062 /* DR 705: Wrapping an unqualified name in parentheses
4063 suppresses arg-dependent lookup. We want to pass back
4064 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4065 (c++/37862), but none of the others. */
4066 if (*idk != CP_ID_KIND_QUALIFIED)
4067 *idk = CP_ID_KIND_NONE;
4069 /* The `>' token might be the end of a template-id or
4070 template-parameter-list now. */
4071 parser->greater_than_is_operator_p
4072 = saved_greater_than_is_operator_p;
4073 /* Consume the `)'. */
4074 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4075 cp_parser_skip_to_end_of_statement (parser);
4077 return expr;
4080 case CPP_OPEN_SQUARE:
4081 if (c_dialect_objc ())
4082 /* We have an Objective-C++ message. */
4083 return cp_parser_objc_expression (parser);
4085 tree lam = cp_parser_lambda_expression (parser);
4086 /* Don't warn about a failed tentative parse. */
4087 if (cp_parser_error_occurred (parser))
4088 return error_mark_node;
4089 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4090 return lam;
4093 case CPP_OBJC_STRING:
4094 if (c_dialect_objc ())
4095 /* We have an Objective-C++ string literal. */
4096 return cp_parser_objc_expression (parser);
4097 cp_parser_error (parser, "expected primary-expression");
4098 return error_mark_node;
4100 case CPP_KEYWORD:
4101 switch (token->keyword)
4103 /* These two are the boolean literals. */
4104 case RID_TRUE:
4105 cp_lexer_consume_token (parser->lexer);
4106 return boolean_true_node;
4107 case RID_FALSE:
4108 cp_lexer_consume_token (parser->lexer);
4109 return boolean_false_node;
4111 /* The `__null' literal. */
4112 case RID_NULL:
4113 cp_lexer_consume_token (parser->lexer);
4114 return null_node;
4116 /* The `nullptr' literal. */
4117 case RID_NULLPTR:
4118 cp_lexer_consume_token (parser->lexer);
4119 return nullptr_node;
4121 /* Recognize the `this' keyword. */
4122 case RID_THIS:
4123 cp_lexer_consume_token (parser->lexer);
4124 if (parser->local_variables_forbidden_p)
4126 error_at (token->location,
4127 "%<this%> may not be used in this context");
4128 return error_mark_node;
4130 /* Pointers cannot appear in constant-expressions. */
4131 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4132 return error_mark_node;
4133 return finish_this_expr ();
4135 /* The `operator' keyword can be the beginning of an
4136 id-expression. */
4137 case RID_OPERATOR:
4138 goto id_expression;
4140 case RID_FUNCTION_NAME:
4141 case RID_PRETTY_FUNCTION_NAME:
4142 case RID_C99_FUNCTION_NAME:
4144 non_integral_constant name;
4146 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4147 __func__ are the names of variables -- but they are
4148 treated specially. Therefore, they are handled here,
4149 rather than relying on the generic id-expression logic
4150 below. Grammatically, these names are id-expressions.
4152 Consume the token. */
4153 token = cp_lexer_consume_token (parser->lexer);
4155 switch (token->keyword)
4157 case RID_FUNCTION_NAME:
4158 name = NIC_FUNC_NAME;
4159 break;
4160 case RID_PRETTY_FUNCTION_NAME:
4161 name = NIC_PRETTY_FUNC;
4162 break;
4163 case RID_C99_FUNCTION_NAME:
4164 name = NIC_C99_FUNC;
4165 break;
4166 default:
4167 gcc_unreachable ();
4170 if (cp_parser_non_integral_constant_expression (parser, name))
4171 return error_mark_node;
4173 /* Look up the name. */
4174 return finish_fname (token->u.value);
4177 case RID_VA_ARG:
4179 tree expression;
4180 tree type;
4181 source_location type_location;
4183 /* The `__builtin_va_arg' construct is used to handle
4184 `va_arg'. Consume the `__builtin_va_arg' token. */
4185 cp_lexer_consume_token (parser->lexer);
4186 /* Look for the opening `('. */
4187 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4188 /* Now, parse the assignment-expression. */
4189 expression = cp_parser_assignment_expression (parser,
4190 /*cast_p=*/false, NULL);
4191 /* Look for the `,'. */
4192 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4193 type_location = cp_lexer_peek_token (parser->lexer)->location;
4194 /* Parse the type-id. */
4195 type = cp_parser_type_id (parser);
4196 /* Look for the closing `)'. */
4197 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4198 /* Using `va_arg' in a constant-expression is not
4199 allowed. */
4200 if (cp_parser_non_integral_constant_expression (parser,
4201 NIC_VA_ARG))
4202 return error_mark_node;
4203 return build_x_va_arg (type_location, expression, type);
4206 case RID_OFFSETOF:
4207 return cp_parser_builtin_offsetof (parser);
4209 case RID_HAS_NOTHROW_ASSIGN:
4210 case RID_HAS_NOTHROW_CONSTRUCTOR:
4211 case RID_HAS_NOTHROW_COPY:
4212 case RID_HAS_TRIVIAL_ASSIGN:
4213 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4214 case RID_HAS_TRIVIAL_COPY:
4215 case RID_HAS_TRIVIAL_DESTRUCTOR:
4216 case RID_HAS_VIRTUAL_DESTRUCTOR:
4217 case RID_IS_ABSTRACT:
4218 case RID_IS_BASE_OF:
4219 case RID_IS_CLASS:
4220 case RID_IS_CONVERTIBLE_TO:
4221 case RID_IS_EMPTY:
4222 case RID_IS_ENUM:
4223 case RID_IS_FINAL:
4224 case RID_IS_LITERAL_TYPE:
4225 case RID_IS_POD:
4226 case RID_IS_POLYMORPHIC:
4227 case RID_IS_STD_LAYOUT:
4228 case RID_IS_TRIVIAL:
4229 case RID_IS_UNION:
4230 return cp_parser_trait_expr (parser, token->keyword);
4232 /* Objective-C++ expressions. */
4233 case RID_AT_ENCODE:
4234 case RID_AT_PROTOCOL:
4235 case RID_AT_SELECTOR:
4236 return cp_parser_objc_expression (parser);
4238 case RID_TEMPLATE:
4239 if (parser->in_function_body
4240 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4241 == CPP_LESS))
4243 error_at (token->location,
4244 "a template declaration cannot appear at block scope");
4245 cp_parser_skip_to_end_of_block_or_statement (parser);
4246 return error_mark_node;
4248 default:
4249 cp_parser_error (parser, "expected primary-expression");
4250 return error_mark_node;
4253 /* An id-expression can start with either an identifier, a
4254 `::' as the beginning of a qualified-id, or the "operator"
4255 keyword. */
4256 case CPP_NAME:
4257 case CPP_SCOPE:
4258 case CPP_TEMPLATE_ID:
4259 case CPP_NESTED_NAME_SPECIFIER:
4261 tree id_expression;
4262 tree decl;
4263 const char *error_msg;
4264 bool template_p;
4265 bool done;
4266 cp_token *id_expr_token;
4268 id_expression:
4269 /* Parse the id-expression. */
4270 id_expression
4271 = cp_parser_id_expression (parser,
4272 /*template_keyword_p=*/false,
4273 /*check_dependency_p=*/true,
4274 &template_p,
4275 /*declarator_p=*/false,
4276 /*optional_p=*/false);
4277 if (id_expression == error_mark_node)
4278 return error_mark_node;
4279 id_expr_token = token;
4280 token = cp_lexer_peek_token (parser->lexer);
4281 done = (token->type != CPP_OPEN_SQUARE
4282 && token->type != CPP_OPEN_PAREN
4283 && token->type != CPP_DOT
4284 && token->type != CPP_DEREF
4285 && token->type != CPP_PLUS_PLUS
4286 && token->type != CPP_MINUS_MINUS);
4287 /* If we have a template-id, then no further lookup is
4288 required. If the template-id was for a template-class, we
4289 will sometimes have a TYPE_DECL at this point. */
4290 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4291 || TREE_CODE (id_expression) == TYPE_DECL)
4292 decl = id_expression;
4293 /* Look up the name. */
4294 else
4296 tree ambiguous_decls;
4298 /* If we already know that this lookup is ambiguous, then
4299 we've already issued an error message; there's no reason
4300 to check again. */
4301 if (id_expr_token->type == CPP_NAME
4302 && id_expr_token->ambiguous_p)
4304 cp_parser_simulate_error (parser);
4305 return error_mark_node;
4308 decl = cp_parser_lookup_name (parser, id_expression,
4309 none_type,
4310 template_p,
4311 /*is_namespace=*/false,
4312 /*check_dependency=*/true,
4313 &ambiguous_decls,
4314 id_expr_token->location);
4315 /* If the lookup was ambiguous, an error will already have
4316 been issued. */
4317 if (ambiguous_decls)
4318 return error_mark_node;
4320 /* In Objective-C++, we may have an Objective-C 2.0
4321 dot-syntax for classes here. */
4322 if (c_dialect_objc ()
4323 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4324 && TREE_CODE (decl) == TYPE_DECL
4325 && objc_is_class_name (decl))
4327 tree component;
4328 cp_lexer_consume_token (parser->lexer);
4329 component = cp_parser_identifier (parser);
4330 if (component == error_mark_node)
4331 return error_mark_node;
4333 return objc_build_class_component_ref (id_expression, component);
4336 /* In Objective-C++, an instance variable (ivar) may be preferred
4337 to whatever cp_parser_lookup_name() found. */
4338 decl = objc_lookup_ivar (decl, id_expression);
4340 /* If name lookup gives us a SCOPE_REF, then the
4341 qualifying scope was dependent. */
4342 if (TREE_CODE (decl) == SCOPE_REF)
4344 /* At this point, we do not know if DECL is a valid
4345 integral constant expression. We assume that it is
4346 in fact such an expression, so that code like:
4348 template <int N> struct A {
4349 int a[B<N>::i];
4352 is accepted. At template-instantiation time, we
4353 will check that B<N>::i is actually a constant. */
4354 return decl;
4356 /* Check to see if DECL is a local variable in a context
4357 where that is forbidden. */
4358 if (parser->local_variables_forbidden_p
4359 && local_variable_p (decl))
4361 /* It might be that we only found DECL because we are
4362 trying to be generous with pre-ISO scoping rules.
4363 For example, consider:
4365 int i;
4366 void g() {
4367 for (int i = 0; i < 10; ++i) {}
4368 extern void f(int j = i);
4371 Here, name look up will originally find the out
4372 of scope `i'. We need to issue a warning message,
4373 but then use the global `i'. */
4374 decl = check_for_out_of_scope_variable (decl);
4375 if (local_variable_p (decl))
4377 error_at (id_expr_token->location,
4378 "local variable %qD may not appear in this context",
4379 decl);
4380 return error_mark_node;
4385 decl = (finish_id_expression
4386 (id_expression, decl, parser->scope,
4387 idk,
4388 parser->integral_constant_expression_p,
4389 parser->allow_non_integral_constant_expression_p,
4390 &parser->non_integral_constant_expression_p,
4391 template_p, done, address_p,
4392 template_arg_p,
4393 &error_msg,
4394 id_expr_token->location));
4395 if (error_msg)
4396 cp_parser_error (parser, error_msg);
4397 return decl;
4400 /* Anything else is an error. */
4401 default:
4402 cp_parser_error (parser, "expected primary-expression");
4403 return error_mark_node;
4407 /* Parse an id-expression.
4409 id-expression:
4410 unqualified-id
4411 qualified-id
4413 qualified-id:
4414 :: [opt] nested-name-specifier template [opt] unqualified-id
4415 :: identifier
4416 :: operator-function-id
4417 :: template-id
4419 Return a representation of the unqualified portion of the
4420 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4421 a `::' or nested-name-specifier.
4423 Often, if the id-expression was a qualified-id, the caller will
4424 want to make a SCOPE_REF to represent the qualified-id. This
4425 function does not do this in order to avoid wastefully creating
4426 SCOPE_REFs when they are not required.
4428 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4429 `template' keyword.
4431 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4432 uninstantiated templates.
4434 If *TEMPLATE_P is non-NULL, it is set to true iff the
4435 `template' keyword is used to explicitly indicate that the entity
4436 named is a template.
4438 If DECLARATOR_P is true, the id-expression is appearing as part of
4439 a declarator, rather than as part of an expression. */
4441 static tree
4442 cp_parser_id_expression (cp_parser *parser,
4443 bool template_keyword_p,
4444 bool check_dependency_p,
4445 bool *template_p,
4446 bool declarator_p,
4447 bool optional_p)
4449 bool global_scope_p;
4450 bool nested_name_specifier_p;
4452 /* Assume the `template' keyword was not used. */
4453 if (template_p)
4454 *template_p = template_keyword_p;
4456 /* Look for the optional `::' operator. */
4457 global_scope_p
4458 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4459 != NULL_TREE);
4460 /* Look for the optional nested-name-specifier. */
4461 nested_name_specifier_p
4462 = (cp_parser_nested_name_specifier_opt (parser,
4463 /*typename_keyword_p=*/false,
4464 check_dependency_p,
4465 /*type_p=*/false,
4466 declarator_p)
4467 != NULL_TREE);
4468 /* If there is a nested-name-specifier, then we are looking at
4469 the first qualified-id production. */
4470 if (nested_name_specifier_p)
4472 tree saved_scope;
4473 tree saved_object_scope;
4474 tree saved_qualifying_scope;
4475 tree unqualified_id;
4476 bool is_template;
4478 /* See if the next token is the `template' keyword. */
4479 if (!template_p)
4480 template_p = &is_template;
4481 *template_p = cp_parser_optional_template_keyword (parser);
4482 /* Name lookup we do during the processing of the
4483 unqualified-id might obliterate SCOPE. */
4484 saved_scope = parser->scope;
4485 saved_object_scope = parser->object_scope;
4486 saved_qualifying_scope = parser->qualifying_scope;
4487 /* Process the final unqualified-id. */
4488 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4489 check_dependency_p,
4490 declarator_p,
4491 /*optional_p=*/false);
4492 /* Restore the SAVED_SCOPE for our caller. */
4493 parser->scope = saved_scope;
4494 parser->object_scope = saved_object_scope;
4495 parser->qualifying_scope = saved_qualifying_scope;
4497 return unqualified_id;
4499 /* Otherwise, if we are in global scope, then we are looking at one
4500 of the other qualified-id productions. */
4501 else if (global_scope_p)
4503 cp_token *token;
4504 tree id;
4506 /* Peek at the next token. */
4507 token = cp_lexer_peek_token (parser->lexer);
4509 /* If it's an identifier, and the next token is not a "<", then
4510 we can avoid the template-id case. This is an optimization
4511 for this common case. */
4512 if (token->type == CPP_NAME
4513 && !cp_parser_nth_token_starts_template_argument_list_p
4514 (parser, 2))
4515 return cp_parser_identifier (parser);
4517 cp_parser_parse_tentatively (parser);
4518 /* Try a template-id. */
4519 id = cp_parser_template_id (parser,
4520 /*template_keyword_p=*/false,
4521 /*check_dependency_p=*/true,
4522 none_type,
4523 declarator_p);
4524 /* If that worked, we're done. */
4525 if (cp_parser_parse_definitely (parser))
4526 return id;
4528 /* Peek at the next token. (Changes in the token buffer may
4529 have invalidated the pointer obtained above.) */
4530 token = cp_lexer_peek_token (parser->lexer);
4532 switch (token->type)
4534 case CPP_NAME:
4535 return cp_parser_identifier (parser);
4537 case CPP_KEYWORD:
4538 if (token->keyword == RID_OPERATOR)
4539 return cp_parser_operator_function_id (parser);
4540 /* Fall through. */
4542 default:
4543 cp_parser_error (parser, "expected id-expression");
4544 return error_mark_node;
4547 else
4548 return cp_parser_unqualified_id (parser, template_keyword_p,
4549 /*check_dependency_p=*/true,
4550 declarator_p,
4551 optional_p);
4554 /* Parse an unqualified-id.
4556 unqualified-id:
4557 identifier
4558 operator-function-id
4559 conversion-function-id
4560 ~ class-name
4561 template-id
4563 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4564 keyword, in a construct like `A::template ...'.
4566 Returns a representation of unqualified-id. For the `identifier'
4567 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4568 production a BIT_NOT_EXPR is returned; the operand of the
4569 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4570 other productions, see the documentation accompanying the
4571 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4572 names are looked up in uninstantiated templates. If DECLARATOR_P
4573 is true, the unqualified-id is appearing as part of a declarator,
4574 rather than as part of an expression. */
4576 static tree
4577 cp_parser_unqualified_id (cp_parser* parser,
4578 bool template_keyword_p,
4579 bool check_dependency_p,
4580 bool declarator_p,
4581 bool optional_p)
4583 cp_token *token;
4585 /* Peek at the next token. */
4586 token = cp_lexer_peek_token (parser->lexer);
4588 switch (token->type)
4590 case CPP_NAME:
4592 tree id;
4594 /* We don't know yet whether or not this will be a
4595 template-id. */
4596 cp_parser_parse_tentatively (parser);
4597 /* Try a template-id. */
4598 id = cp_parser_template_id (parser, template_keyword_p,
4599 check_dependency_p,
4600 none_type,
4601 declarator_p);
4602 /* If it worked, we're done. */
4603 if (cp_parser_parse_definitely (parser))
4604 return id;
4605 /* Otherwise, it's an ordinary identifier. */
4606 return cp_parser_identifier (parser);
4609 case CPP_TEMPLATE_ID:
4610 return cp_parser_template_id (parser, template_keyword_p,
4611 check_dependency_p,
4612 none_type,
4613 declarator_p);
4615 case CPP_COMPL:
4617 tree type_decl;
4618 tree qualifying_scope;
4619 tree object_scope;
4620 tree scope;
4621 bool done;
4623 /* Consume the `~' token. */
4624 cp_lexer_consume_token (parser->lexer);
4625 /* Parse the class-name. The standard, as written, seems to
4626 say that:
4628 template <typename T> struct S { ~S (); };
4629 template <typename T> S<T>::~S() {}
4631 is invalid, since `~' must be followed by a class-name, but
4632 `S<T>' is dependent, and so not known to be a class.
4633 That's not right; we need to look in uninstantiated
4634 templates. A further complication arises from:
4636 template <typename T> void f(T t) {
4637 t.T::~T();
4640 Here, it is not possible to look up `T' in the scope of `T'
4641 itself. We must look in both the current scope, and the
4642 scope of the containing complete expression.
4644 Yet another issue is:
4646 struct S {
4647 int S;
4648 ~S();
4651 S::~S() {}
4653 The standard does not seem to say that the `S' in `~S'
4654 should refer to the type `S' and not the data member
4655 `S::S'. */
4657 /* DR 244 says that we look up the name after the "~" in the
4658 same scope as we looked up the qualifying name. That idea
4659 isn't fully worked out; it's more complicated than that. */
4660 scope = parser->scope;
4661 object_scope = parser->object_scope;
4662 qualifying_scope = parser->qualifying_scope;
4664 /* Check for invalid scopes. */
4665 if (scope == error_mark_node)
4667 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4668 cp_lexer_consume_token (parser->lexer);
4669 return error_mark_node;
4671 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4673 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4674 error_at (token->location,
4675 "scope %qT before %<~%> is not a class-name",
4676 scope);
4677 cp_parser_simulate_error (parser);
4678 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4679 cp_lexer_consume_token (parser->lexer);
4680 return error_mark_node;
4682 gcc_assert (!scope || TYPE_P (scope));
4684 /* If the name is of the form "X::~X" it's OK even if X is a
4685 typedef. */
4686 token = cp_lexer_peek_token (parser->lexer);
4687 if (scope
4688 && token->type == CPP_NAME
4689 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4690 != CPP_LESS)
4691 && (token->u.value == TYPE_IDENTIFIER (scope)
4692 || (CLASS_TYPE_P (scope)
4693 && constructor_name_p (token->u.value, scope))))
4695 cp_lexer_consume_token (parser->lexer);
4696 return build_nt (BIT_NOT_EXPR, scope);
4699 /* If there was an explicit qualification (S::~T), first look
4700 in the scope given by the qualification (i.e., S).
4702 Note: in the calls to cp_parser_class_name below we pass
4703 typename_type so that lookup finds the injected-class-name
4704 rather than the constructor. */
4705 done = false;
4706 type_decl = NULL_TREE;
4707 if (scope)
4709 cp_parser_parse_tentatively (parser);
4710 type_decl = cp_parser_class_name (parser,
4711 /*typename_keyword_p=*/false,
4712 /*template_keyword_p=*/false,
4713 typename_type,
4714 /*check_dependency=*/false,
4715 /*class_head_p=*/false,
4716 declarator_p);
4717 if (cp_parser_parse_definitely (parser))
4718 done = true;
4720 /* In "N::S::~S", look in "N" as well. */
4721 if (!done && scope && qualifying_scope)
4723 cp_parser_parse_tentatively (parser);
4724 parser->scope = qualifying_scope;
4725 parser->object_scope = NULL_TREE;
4726 parser->qualifying_scope = NULL_TREE;
4727 type_decl
4728 = cp_parser_class_name (parser,
4729 /*typename_keyword_p=*/false,
4730 /*template_keyword_p=*/false,
4731 typename_type,
4732 /*check_dependency=*/false,
4733 /*class_head_p=*/false,
4734 declarator_p);
4735 if (cp_parser_parse_definitely (parser))
4736 done = true;
4738 /* In "p->S::~T", look in the scope given by "*p" as well. */
4739 else if (!done && object_scope)
4741 cp_parser_parse_tentatively (parser);
4742 parser->scope = object_scope;
4743 parser->object_scope = NULL_TREE;
4744 parser->qualifying_scope = NULL_TREE;
4745 type_decl
4746 = cp_parser_class_name (parser,
4747 /*typename_keyword_p=*/false,
4748 /*template_keyword_p=*/false,
4749 typename_type,
4750 /*check_dependency=*/false,
4751 /*class_head_p=*/false,
4752 declarator_p);
4753 if (cp_parser_parse_definitely (parser))
4754 done = true;
4756 /* Look in the surrounding context. */
4757 if (!done)
4759 parser->scope = NULL_TREE;
4760 parser->object_scope = NULL_TREE;
4761 parser->qualifying_scope = NULL_TREE;
4762 if (processing_template_decl)
4763 cp_parser_parse_tentatively (parser);
4764 type_decl
4765 = cp_parser_class_name (parser,
4766 /*typename_keyword_p=*/false,
4767 /*template_keyword_p=*/false,
4768 typename_type,
4769 /*check_dependency=*/false,
4770 /*class_head_p=*/false,
4771 declarator_p);
4772 if (processing_template_decl
4773 && ! cp_parser_parse_definitely (parser))
4775 /* We couldn't find a type with this name, so just accept
4776 it and check for a match at instantiation time. */
4777 type_decl = cp_parser_identifier (parser);
4778 if (type_decl != error_mark_node)
4779 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4780 return type_decl;
4783 /* If an error occurred, assume that the name of the
4784 destructor is the same as the name of the qualifying
4785 class. That allows us to keep parsing after running
4786 into ill-formed destructor names. */
4787 if (type_decl == error_mark_node && scope)
4788 return build_nt (BIT_NOT_EXPR, scope);
4789 else if (type_decl == error_mark_node)
4790 return error_mark_node;
4792 /* Check that destructor name and scope match. */
4793 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4795 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4796 error_at (token->location,
4797 "declaration of %<~%T%> as member of %qT",
4798 type_decl, scope);
4799 cp_parser_simulate_error (parser);
4800 return error_mark_node;
4803 /* [class.dtor]
4805 A typedef-name that names a class shall not be used as the
4806 identifier in the declarator for a destructor declaration. */
4807 if (declarator_p
4808 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4809 && !DECL_SELF_REFERENCE_P (type_decl)
4810 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4811 error_at (token->location,
4812 "typedef-name %qD used as destructor declarator",
4813 type_decl);
4815 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4818 case CPP_KEYWORD:
4819 if (token->keyword == RID_OPERATOR)
4821 tree id;
4823 /* This could be a template-id, so we try that first. */
4824 cp_parser_parse_tentatively (parser);
4825 /* Try a template-id. */
4826 id = cp_parser_template_id (parser, template_keyword_p,
4827 /*check_dependency_p=*/true,
4828 none_type,
4829 declarator_p);
4830 /* If that worked, we're done. */
4831 if (cp_parser_parse_definitely (parser))
4832 return id;
4833 /* We still don't know whether we're looking at an
4834 operator-function-id or a conversion-function-id. */
4835 cp_parser_parse_tentatively (parser);
4836 /* Try an operator-function-id. */
4837 id = cp_parser_operator_function_id (parser);
4838 /* If that didn't work, try a conversion-function-id. */
4839 if (!cp_parser_parse_definitely (parser))
4840 id = cp_parser_conversion_function_id (parser);
4841 else if (UDLIT_OPER_P (id))
4843 /* 17.6.3.3.5 */
4844 const char *name = UDLIT_OP_SUFFIX (id);
4845 if (name[0] != '_' && !in_system_header)
4846 warning (0, "literal operator suffixes not preceded by %<_%>"
4847 " are reserved for future standardization");
4850 return id;
4852 /* Fall through. */
4854 default:
4855 if (optional_p)
4856 return NULL_TREE;
4857 cp_parser_error (parser, "expected unqualified-id");
4858 return error_mark_node;
4862 /* Parse an (optional) nested-name-specifier.
4864 nested-name-specifier: [C++98]
4865 class-or-namespace-name :: nested-name-specifier [opt]
4866 class-or-namespace-name :: template nested-name-specifier [opt]
4868 nested-name-specifier: [C++0x]
4869 type-name ::
4870 namespace-name ::
4871 nested-name-specifier identifier ::
4872 nested-name-specifier template [opt] simple-template-id ::
4874 PARSER->SCOPE should be set appropriately before this function is
4875 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4876 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4877 in name lookups.
4879 Sets PARSER->SCOPE to the class (TYPE) or namespace
4880 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4881 it unchanged if there is no nested-name-specifier. Returns the new
4882 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4884 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4885 part of a declaration and/or decl-specifier. */
4887 static tree
4888 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4889 bool typename_keyword_p,
4890 bool check_dependency_p,
4891 bool type_p,
4892 bool is_declaration)
4894 bool success = false;
4895 cp_token_position start = 0;
4896 cp_token *token;
4898 /* Remember where the nested-name-specifier starts. */
4899 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4901 start = cp_lexer_token_position (parser->lexer, false);
4902 push_deferring_access_checks (dk_deferred);
4905 while (true)
4907 tree new_scope;
4908 tree old_scope;
4909 tree saved_qualifying_scope;
4910 bool template_keyword_p;
4912 /* Spot cases that cannot be the beginning of a
4913 nested-name-specifier. */
4914 token = cp_lexer_peek_token (parser->lexer);
4916 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4917 the already parsed nested-name-specifier. */
4918 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4920 /* Grab the nested-name-specifier and continue the loop. */
4921 cp_parser_pre_parsed_nested_name_specifier (parser);
4922 /* If we originally encountered this nested-name-specifier
4923 with IS_DECLARATION set to false, we will not have
4924 resolved TYPENAME_TYPEs, so we must do so here. */
4925 if (is_declaration
4926 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4928 new_scope = resolve_typename_type (parser->scope,
4929 /*only_current_p=*/false);
4930 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4931 parser->scope = new_scope;
4933 success = true;
4934 continue;
4937 /* Spot cases that cannot be the beginning of a
4938 nested-name-specifier. On the second and subsequent times
4939 through the loop, we look for the `template' keyword. */
4940 if (success && token->keyword == RID_TEMPLATE)
4942 /* A template-id can start a nested-name-specifier. */
4943 else if (token->type == CPP_TEMPLATE_ID)
4945 /* DR 743: decltype can be used in a nested-name-specifier. */
4946 else if (token_is_decltype (token))
4948 else
4950 /* If the next token is not an identifier, then it is
4951 definitely not a type-name or namespace-name. */
4952 if (token->type != CPP_NAME)
4953 break;
4954 /* If the following token is neither a `<' (to begin a
4955 template-id), nor a `::', then we are not looking at a
4956 nested-name-specifier. */
4957 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4959 if (token->type == CPP_COLON
4960 && parser->colon_corrects_to_scope_p
4961 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4963 error_at (token->location,
4964 "found %<:%> in nested-name-specifier, expected %<::%>");
4965 token->type = CPP_SCOPE;
4968 if (token->type != CPP_SCOPE
4969 && !cp_parser_nth_token_starts_template_argument_list_p
4970 (parser, 2))
4971 break;
4974 /* The nested-name-specifier is optional, so we parse
4975 tentatively. */
4976 cp_parser_parse_tentatively (parser);
4978 /* Look for the optional `template' keyword, if this isn't the
4979 first time through the loop. */
4980 if (success)
4981 template_keyword_p = cp_parser_optional_template_keyword (parser);
4982 else
4983 template_keyword_p = false;
4985 /* Save the old scope since the name lookup we are about to do
4986 might destroy it. */
4987 old_scope = parser->scope;
4988 saved_qualifying_scope = parser->qualifying_scope;
4989 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4990 look up names in "X<T>::I" in order to determine that "Y" is
4991 a template. So, if we have a typename at this point, we make
4992 an effort to look through it. */
4993 if (is_declaration
4994 && !typename_keyword_p
4995 && parser->scope
4996 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4997 parser->scope = resolve_typename_type (parser->scope,
4998 /*only_current_p=*/false);
4999 /* Parse the qualifying entity. */
5000 new_scope
5001 = cp_parser_qualifying_entity (parser,
5002 typename_keyword_p,
5003 template_keyword_p,
5004 check_dependency_p,
5005 type_p,
5006 is_declaration);
5007 /* Look for the `::' token. */
5008 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5010 /* If we found what we wanted, we keep going; otherwise, we're
5011 done. */
5012 if (!cp_parser_parse_definitely (parser))
5014 bool error_p = false;
5016 /* Restore the OLD_SCOPE since it was valid before the
5017 failed attempt at finding the last
5018 class-or-namespace-name. */
5019 parser->scope = old_scope;
5020 parser->qualifying_scope = saved_qualifying_scope;
5022 /* If the next token is a decltype, and the one after that is a
5023 `::', then the decltype has failed to resolve to a class or
5024 enumeration type. Give this error even when parsing
5025 tentatively since it can't possibly be valid--and we're going
5026 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5027 won't get another chance.*/
5028 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5029 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5030 == CPP_SCOPE))
5032 token = cp_lexer_consume_token (parser->lexer);
5033 error_at (token->location, "decltype evaluates to %qT, "
5034 "which is not a class or enumeration type",
5035 token->u.value);
5036 parser->scope = error_mark_node;
5037 error_p = true;
5038 /* As below. */
5039 success = true;
5040 cp_lexer_consume_token (parser->lexer);
5043 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5044 break;
5045 /* If the next token is an identifier, and the one after
5046 that is a `::', then any valid interpretation would have
5047 found a class-or-namespace-name. */
5048 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5049 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5050 == CPP_SCOPE)
5051 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5052 != CPP_COMPL))
5054 token = cp_lexer_consume_token (parser->lexer);
5055 if (!error_p)
5057 if (!token->ambiguous_p)
5059 tree decl;
5060 tree ambiguous_decls;
5062 decl = cp_parser_lookup_name (parser, token->u.value,
5063 none_type,
5064 /*is_template=*/false,
5065 /*is_namespace=*/false,
5066 /*check_dependency=*/true,
5067 &ambiguous_decls,
5068 token->location);
5069 if (TREE_CODE (decl) == TEMPLATE_DECL)
5070 error_at (token->location,
5071 "%qD used without template parameters",
5072 decl);
5073 else if (ambiguous_decls)
5075 error_at (token->location,
5076 "reference to %qD is ambiguous",
5077 token->u.value);
5078 print_candidates (ambiguous_decls);
5079 decl = error_mark_node;
5081 else
5083 if (cxx_dialect != cxx98)
5084 cp_parser_name_lookup_error
5085 (parser, token->u.value, decl, NLE_NOT_CXX98,
5086 token->location);
5087 else
5088 cp_parser_name_lookup_error
5089 (parser, token->u.value, decl, NLE_CXX98,
5090 token->location);
5093 parser->scope = error_mark_node;
5094 error_p = true;
5095 /* Treat this as a successful nested-name-specifier
5096 due to:
5098 [basic.lookup.qual]
5100 If the name found is not a class-name (clause
5101 _class_) or namespace-name (_namespace.def_), the
5102 program is ill-formed. */
5103 success = true;
5105 cp_lexer_consume_token (parser->lexer);
5107 break;
5109 /* We've found one valid nested-name-specifier. */
5110 success = true;
5111 /* Name lookup always gives us a DECL. */
5112 if (TREE_CODE (new_scope) == TYPE_DECL)
5113 new_scope = TREE_TYPE (new_scope);
5114 /* Uses of "template" must be followed by actual templates. */
5115 if (template_keyword_p
5116 && !(CLASS_TYPE_P (new_scope)
5117 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5118 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5119 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5120 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5121 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5122 == TEMPLATE_ID_EXPR)))
5123 permerror (input_location, TYPE_P (new_scope)
5124 ? G_("%qT is not a template")
5125 : G_("%qD is not a template"),
5126 new_scope);
5127 /* If it is a class scope, try to complete it; we are about to
5128 be looking up names inside the class. */
5129 if (TYPE_P (new_scope)
5130 /* Since checking types for dependency can be expensive,
5131 avoid doing it if the type is already complete. */
5132 && !COMPLETE_TYPE_P (new_scope)
5133 /* Do not try to complete dependent types. */
5134 && !dependent_type_p (new_scope))
5136 new_scope = complete_type (new_scope);
5137 /* If it is a typedef to current class, use the current
5138 class instead, as the typedef won't have any names inside
5139 it yet. */
5140 if (!COMPLETE_TYPE_P (new_scope)
5141 && currently_open_class (new_scope))
5142 new_scope = TYPE_MAIN_VARIANT (new_scope);
5144 /* Make sure we look in the right scope the next time through
5145 the loop. */
5146 parser->scope = new_scope;
5149 /* If parsing tentatively, replace the sequence of tokens that makes
5150 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5151 token. That way, should we re-parse the token stream, we will
5152 not have to repeat the effort required to do the parse, nor will
5153 we issue duplicate error messages. */
5154 if (success && start)
5156 cp_token *token;
5158 token = cp_lexer_token_at (parser->lexer, start);
5159 /* Reset the contents of the START token. */
5160 token->type = CPP_NESTED_NAME_SPECIFIER;
5161 /* Retrieve any deferred checks. Do not pop this access checks yet
5162 so the memory will not be reclaimed during token replacing below. */
5163 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5164 token->u.tree_check_value->value = parser->scope;
5165 token->u.tree_check_value->checks = get_deferred_access_checks ();
5166 token->u.tree_check_value->qualifying_scope =
5167 parser->qualifying_scope;
5168 token->keyword = RID_MAX;
5170 /* Purge all subsequent tokens. */
5171 cp_lexer_purge_tokens_after (parser->lexer, start);
5174 if (start)
5175 pop_to_parent_deferring_access_checks ();
5177 return success ? parser->scope : NULL_TREE;
5180 /* Parse a nested-name-specifier. See
5181 cp_parser_nested_name_specifier_opt for details. This function
5182 behaves identically, except that it will an issue an error if no
5183 nested-name-specifier is present. */
5185 static tree
5186 cp_parser_nested_name_specifier (cp_parser *parser,
5187 bool typename_keyword_p,
5188 bool check_dependency_p,
5189 bool type_p,
5190 bool is_declaration)
5192 tree scope;
5194 /* Look for the nested-name-specifier. */
5195 scope = cp_parser_nested_name_specifier_opt (parser,
5196 typename_keyword_p,
5197 check_dependency_p,
5198 type_p,
5199 is_declaration);
5200 /* If it was not present, issue an error message. */
5201 if (!scope)
5203 cp_parser_error (parser, "expected nested-name-specifier");
5204 parser->scope = NULL_TREE;
5207 return scope;
5210 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5211 this is either a class-name or a namespace-name (which corresponds
5212 to the class-or-namespace-name production in the grammar). For
5213 C++0x, it can also be a type-name that refers to an enumeration
5214 type or a simple-template-id.
5216 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5217 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5218 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5219 TYPE_P is TRUE iff the next name should be taken as a class-name,
5220 even the same name is declared to be another entity in the same
5221 scope.
5223 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5224 specified by the class-or-namespace-name. If neither is found the
5225 ERROR_MARK_NODE is returned. */
5227 static tree
5228 cp_parser_qualifying_entity (cp_parser *parser,
5229 bool typename_keyword_p,
5230 bool template_keyword_p,
5231 bool check_dependency_p,
5232 bool type_p,
5233 bool is_declaration)
5235 tree saved_scope;
5236 tree saved_qualifying_scope;
5237 tree saved_object_scope;
5238 tree scope;
5239 bool only_class_p;
5240 bool successful_parse_p;
5242 /* DR 743: decltype can appear in a nested-name-specifier. */
5243 if (cp_lexer_next_token_is_decltype (parser->lexer))
5245 scope = cp_parser_decltype (parser);
5246 if (TREE_CODE (scope) != ENUMERAL_TYPE
5247 && !MAYBE_CLASS_TYPE_P (scope))
5249 cp_parser_simulate_error (parser);
5250 return error_mark_node;
5252 if (TYPE_NAME (scope))
5253 scope = TYPE_NAME (scope);
5254 return scope;
5257 /* Before we try to parse the class-name, we must save away the
5258 current PARSER->SCOPE since cp_parser_class_name will destroy
5259 it. */
5260 saved_scope = parser->scope;
5261 saved_qualifying_scope = parser->qualifying_scope;
5262 saved_object_scope = parser->object_scope;
5263 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5264 there is no need to look for a namespace-name. */
5265 only_class_p = template_keyword_p
5266 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5267 if (!only_class_p)
5268 cp_parser_parse_tentatively (parser);
5269 scope = cp_parser_class_name (parser,
5270 typename_keyword_p,
5271 template_keyword_p,
5272 type_p ? class_type : none_type,
5273 check_dependency_p,
5274 /*class_head_p=*/false,
5275 is_declaration);
5276 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5277 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5278 if (!only_class_p
5279 && cxx_dialect != cxx98
5280 && !successful_parse_p)
5282 /* Restore the saved scope. */
5283 parser->scope = saved_scope;
5284 parser->qualifying_scope = saved_qualifying_scope;
5285 parser->object_scope = saved_object_scope;
5287 /* Parse tentatively. */
5288 cp_parser_parse_tentatively (parser);
5290 /* Parse a type-name */
5291 scope = cp_parser_type_name (parser);
5293 /* "If the name found does not designate a namespace or a class,
5294 enumeration, or dependent type, the program is ill-formed."
5296 We cover classes and dependent types above and namespaces below,
5297 so this code is only looking for enums. */
5298 if (!scope || TREE_CODE (scope) != TYPE_DECL
5299 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5300 cp_parser_simulate_error (parser);
5302 successful_parse_p = cp_parser_parse_definitely (parser);
5304 /* If that didn't work, try for a namespace-name. */
5305 if (!only_class_p && !successful_parse_p)
5307 /* Restore the saved scope. */
5308 parser->scope = saved_scope;
5309 parser->qualifying_scope = saved_qualifying_scope;
5310 parser->object_scope = saved_object_scope;
5311 /* If we are not looking at an identifier followed by the scope
5312 resolution operator, then this is not part of a
5313 nested-name-specifier. (Note that this function is only used
5314 to parse the components of a nested-name-specifier.) */
5315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5316 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5317 return error_mark_node;
5318 scope = cp_parser_namespace_name (parser);
5321 return scope;
5324 /* Parse a postfix-expression.
5326 postfix-expression:
5327 primary-expression
5328 postfix-expression [ expression ]
5329 postfix-expression ( expression-list [opt] )
5330 simple-type-specifier ( expression-list [opt] )
5331 typename :: [opt] nested-name-specifier identifier
5332 ( expression-list [opt] )
5333 typename :: [opt] nested-name-specifier template [opt] template-id
5334 ( expression-list [opt] )
5335 postfix-expression . template [opt] id-expression
5336 postfix-expression -> template [opt] id-expression
5337 postfix-expression . pseudo-destructor-name
5338 postfix-expression -> pseudo-destructor-name
5339 postfix-expression ++
5340 postfix-expression --
5341 dynamic_cast < type-id > ( expression )
5342 static_cast < type-id > ( expression )
5343 reinterpret_cast < type-id > ( expression )
5344 const_cast < type-id > ( expression )
5345 typeid ( expression )
5346 typeid ( type-id )
5348 GNU Extension:
5350 postfix-expression:
5351 ( type-id ) { initializer-list , [opt] }
5353 This extension is a GNU version of the C99 compound-literal
5354 construct. (The C99 grammar uses `type-name' instead of `type-id',
5355 but they are essentially the same concept.)
5357 If ADDRESS_P is true, the postfix expression is the operand of the
5358 `&' operator. CAST_P is true if this expression is the target of a
5359 cast.
5361 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5362 class member access expressions [expr.ref].
5364 Returns a representation of the expression. */
5366 static tree
5367 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5368 bool member_access_only_p,
5369 cp_id_kind * pidk_return)
5371 cp_token *token;
5372 enum rid keyword;
5373 cp_id_kind idk = CP_ID_KIND_NONE;
5374 tree postfix_expression = NULL_TREE;
5375 bool is_member_access = false;
5377 /* Peek at the next token. */
5378 token = cp_lexer_peek_token (parser->lexer);
5379 /* Some of the productions are determined by keywords. */
5380 keyword = token->keyword;
5381 switch (keyword)
5383 case RID_DYNCAST:
5384 case RID_STATCAST:
5385 case RID_REINTCAST:
5386 case RID_CONSTCAST:
5388 tree type;
5389 tree expression;
5390 const char *saved_message;
5392 /* All of these can be handled in the same way from the point
5393 of view of parsing. Begin by consuming the token
5394 identifying the cast. */
5395 cp_lexer_consume_token (parser->lexer);
5397 /* New types cannot be defined in the cast. */
5398 saved_message = parser->type_definition_forbidden_message;
5399 parser->type_definition_forbidden_message
5400 = G_("types may not be defined in casts");
5402 /* Look for the opening `<'. */
5403 cp_parser_require (parser, CPP_LESS, RT_LESS);
5404 /* Parse the type to which we are casting. */
5405 type = cp_parser_type_id (parser);
5406 /* Look for the closing `>'. */
5407 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5408 /* Restore the old message. */
5409 parser->type_definition_forbidden_message = saved_message;
5411 /* And the expression which is being cast. */
5412 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5413 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5414 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5416 /* Only type conversions to integral or enumeration types
5417 can be used in constant-expressions. */
5418 if (!cast_valid_in_integral_constant_expression_p (type)
5419 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5420 return error_mark_node;
5422 switch (keyword)
5424 case RID_DYNCAST:
5425 postfix_expression
5426 = build_dynamic_cast (type, expression, tf_warning_or_error);
5427 break;
5428 case RID_STATCAST:
5429 postfix_expression
5430 = build_static_cast (type, expression, tf_warning_or_error);
5431 break;
5432 case RID_REINTCAST:
5433 postfix_expression
5434 = build_reinterpret_cast (type, expression,
5435 tf_warning_or_error);
5436 break;
5437 case RID_CONSTCAST:
5438 postfix_expression
5439 = build_const_cast (type, expression, tf_warning_or_error);
5440 break;
5441 default:
5442 gcc_unreachable ();
5445 break;
5447 case RID_TYPEID:
5449 tree type;
5450 const char *saved_message;
5451 bool saved_in_type_id_in_expr_p;
5453 /* Consume the `typeid' token. */
5454 cp_lexer_consume_token (parser->lexer);
5455 /* Look for the `(' token. */
5456 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5457 /* Types cannot be defined in a `typeid' expression. */
5458 saved_message = parser->type_definition_forbidden_message;
5459 parser->type_definition_forbidden_message
5460 = G_("types may not be defined in a %<typeid%> expression");
5461 /* We can't be sure yet whether we're looking at a type-id or an
5462 expression. */
5463 cp_parser_parse_tentatively (parser);
5464 /* Try a type-id first. */
5465 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5466 parser->in_type_id_in_expr_p = true;
5467 type = cp_parser_type_id (parser);
5468 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5469 /* Look for the `)' token. Otherwise, we can't be sure that
5470 we're not looking at an expression: consider `typeid (int
5471 (3))', for example. */
5472 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5473 /* If all went well, simply lookup the type-id. */
5474 if (cp_parser_parse_definitely (parser))
5475 postfix_expression = get_typeid (type, tf_warning_or_error);
5476 /* Otherwise, fall back to the expression variant. */
5477 else
5479 tree expression;
5481 /* Look for an expression. */
5482 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5483 /* Compute its typeid. */
5484 postfix_expression = build_typeid (expression, tf_warning_or_error);
5485 /* Look for the `)' token. */
5486 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5488 /* Restore the saved message. */
5489 parser->type_definition_forbidden_message = saved_message;
5490 /* `typeid' may not appear in an integral constant expression. */
5491 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5492 return error_mark_node;
5494 break;
5496 case RID_TYPENAME:
5498 tree type;
5499 /* The syntax permitted here is the same permitted for an
5500 elaborated-type-specifier. */
5501 type = cp_parser_elaborated_type_specifier (parser,
5502 /*is_friend=*/false,
5503 /*is_declaration=*/false);
5504 postfix_expression = cp_parser_functional_cast (parser, type);
5506 break;
5508 case RID_BUILTIN_SHUFFLE:
5510 vec<tree, va_gc> *vec;
5511 unsigned int i;
5512 tree p;
5513 location_t loc = token->location;
5515 cp_lexer_consume_token (parser->lexer);
5516 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5517 /*cast_p=*/false, /*allow_expansion_p=*/true,
5518 /*non_constant_p=*/NULL);
5519 if (vec == NULL)
5520 return error_mark_node;
5522 FOR_EACH_VEC_ELT (*vec, i, p)
5523 mark_exp_read (p);
5525 if (vec->length () == 2)
5526 return c_build_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1]);
5527 else if (vec->length () == 3)
5528 return c_build_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2]);
5529 else
5531 error_at (loc, "wrong number of arguments to "
5532 "%<__builtin_shuffle%>");
5533 return error_mark_node;
5535 break;
5538 default:
5540 tree type;
5542 /* If the next thing is a simple-type-specifier, we may be
5543 looking at a functional cast. We could also be looking at
5544 an id-expression. So, we try the functional cast, and if
5545 that doesn't work we fall back to the primary-expression. */
5546 cp_parser_parse_tentatively (parser);
5547 /* Look for the simple-type-specifier. */
5548 type = cp_parser_simple_type_specifier (parser,
5549 /*decl_specs=*/NULL,
5550 CP_PARSER_FLAGS_NONE);
5551 /* Parse the cast itself. */
5552 if (!cp_parser_error_occurred (parser))
5553 postfix_expression
5554 = cp_parser_functional_cast (parser, type);
5555 /* If that worked, we're done. */
5556 if (cp_parser_parse_definitely (parser))
5557 break;
5559 /* If the functional-cast didn't work out, try a
5560 compound-literal. */
5561 if (cp_parser_allow_gnu_extensions_p (parser)
5562 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5564 vec<constructor_elt, va_gc> *initializer_list = NULL;
5565 bool saved_in_type_id_in_expr_p;
5567 cp_parser_parse_tentatively (parser);
5568 /* Consume the `('. */
5569 cp_lexer_consume_token (parser->lexer);
5570 /* Parse the type. */
5571 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5572 parser->in_type_id_in_expr_p = true;
5573 type = cp_parser_type_id (parser);
5574 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5575 /* Look for the `)'. */
5576 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5577 /* Look for the `{'. */
5578 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5579 /* If things aren't going well, there's no need to
5580 keep going. */
5581 if (!cp_parser_error_occurred (parser))
5583 bool non_constant_p;
5584 /* Parse the initializer-list. */
5585 initializer_list
5586 = cp_parser_initializer_list (parser, &non_constant_p);
5587 /* Allow a trailing `,'. */
5588 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5589 cp_lexer_consume_token (parser->lexer);
5590 /* Look for the final `}'. */
5591 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5593 /* If that worked, we're definitely looking at a
5594 compound-literal expression. */
5595 if (cp_parser_parse_definitely (parser))
5597 /* Warn the user that a compound literal is not
5598 allowed in standard C++. */
5599 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5600 /* For simplicity, we disallow compound literals in
5601 constant-expressions. We could
5602 allow compound literals of integer type, whose
5603 initializer was a constant, in constant
5604 expressions. Permitting that usage, as a further
5605 extension, would not change the meaning of any
5606 currently accepted programs. (Of course, as
5607 compound literals are not part of ISO C++, the
5608 standard has nothing to say.) */
5609 if (cp_parser_non_integral_constant_expression (parser,
5610 NIC_NCC))
5612 postfix_expression = error_mark_node;
5613 break;
5615 /* Form the representation of the compound-literal. */
5616 postfix_expression
5617 = (finish_compound_literal
5618 (type, build_constructor (init_list_type_node,
5619 initializer_list),
5620 tf_warning_or_error));
5621 break;
5625 /* It must be a primary-expression. */
5626 postfix_expression
5627 = cp_parser_primary_expression (parser, address_p, cast_p,
5628 /*template_arg_p=*/false,
5629 &idk);
5631 break;
5634 /* Keep looping until the postfix-expression is complete. */
5635 while (true)
5637 if (idk == CP_ID_KIND_UNQUALIFIED
5638 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5639 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5640 /* It is not a Koenig lookup function call. */
5641 postfix_expression
5642 = unqualified_name_lookup_error (postfix_expression);
5644 /* Peek at the next token. */
5645 token = cp_lexer_peek_token (parser->lexer);
5647 switch (token->type)
5649 case CPP_OPEN_SQUARE:
5650 if (cp_next_tokens_can_be_std_attribute_p (parser))
5652 cp_parser_error (parser,
5653 "two consecutive %<[%> shall "
5654 "only introduce an attribute");
5655 return error_mark_node;
5657 postfix_expression
5658 = cp_parser_postfix_open_square_expression (parser,
5659 postfix_expression,
5660 false);
5661 idk = CP_ID_KIND_NONE;
5662 is_member_access = false;
5663 break;
5665 case CPP_OPEN_PAREN:
5666 /* postfix-expression ( expression-list [opt] ) */
5668 bool koenig_p;
5669 bool is_builtin_constant_p;
5670 bool saved_integral_constant_expression_p = false;
5671 bool saved_non_integral_constant_expression_p = false;
5672 vec<tree, va_gc> *args;
5674 is_member_access = false;
5676 is_builtin_constant_p
5677 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5678 if (is_builtin_constant_p)
5680 /* The whole point of __builtin_constant_p is to allow
5681 non-constant expressions to appear as arguments. */
5682 saved_integral_constant_expression_p
5683 = parser->integral_constant_expression_p;
5684 saved_non_integral_constant_expression_p
5685 = parser->non_integral_constant_expression_p;
5686 parser->integral_constant_expression_p = false;
5688 args = (cp_parser_parenthesized_expression_list
5689 (parser, non_attr,
5690 /*cast_p=*/false, /*allow_expansion_p=*/true,
5691 /*non_constant_p=*/NULL));
5692 if (is_builtin_constant_p)
5694 parser->integral_constant_expression_p
5695 = saved_integral_constant_expression_p;
5696 parser->non_integral_constant_expression_p
5697 = saved_non_integral_constant_expression_p;
5700 if (args == NULL)
5702 postfix_expression = error_mark_node;
5703 break;
5706 /* Function calls are not permitted in
5707 constant-expressions. */
5708 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5709 && cp_parser_non_integral_constant_expression (parser,
5710 NIC_FUNC_CALL))
5712 postfix_expression = error_mark_node;
5713 release_tree_vector (args);
5714 break;
5717 koenig_p = false;
5718 if (idk == CP_ID_KIND_UNQUALIFIED
5719 || idk == CP_ID_KIND_TEMPLATE_ID)
5721 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5723 if (!args->is_empty ())
5725 koenig_p = true;
5726 if (!any_type_dependent_arguments_p (args))
5727 postfix_expression
5728 = perform_koenig_lookup (postfix_expression, args,
5729 /*include_std=*/false,
5730 tf_warning_or_error);
5732 else
5733 postfix_expression
5734 = unqualified_fn_lookup_error (postfix_expression);
5736 /* We do not perform argument-dependent lookup if
5737 normal lookup finds a non-function, in accordance
5738 with the expected resolution of DR 218. */
5739 else if (!args->is_empty ()
5740 && is_overloaded_fn (postfix_expression))
5742 tree fn = get_first_fn (postfix_expression);
5743 fn = STRIP_TEMPLATE (fn);
5745 /* Do not do argument dependent lookup if regular
5746 lookup finds a member function or a block-scope
5747 function declaration. [basic.lookup.argdep]/3 */
5748 if (!DECL_FUNCTION_MEMBER_P (fn)
5749 && !DECL_LOCAL_FUNCTION_P (fn))
5751 koenig_p = true;
5752 if (!any_type_dependent_arguments_p (args))
5753 postfix_expression
5754 = perform_koenig_lookup (postfix_expression, args,
5755 /*include_std=*/false,
5756 tf_warning_or_error);
5761 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5763 tree instance = TREE_OPERAND (postfix_expression, 0);
5764 tree fn = TREE_OPERAND (postfix_expression, 1);
5766 if (processing_template_decl
5767 && (type_dependent_expression_p (instance)
5768 || (!BASELINK_P (fn)
5769 && TREE_CODE (fn) != FIELD_DECL)
5770 || type_dependent_expression_p (fn)
5771 || any_type_dependent_arguments_p (args)))
5773 postfix_expression
5774 = build_nt_call_vec (postfix_expression, args);
5775 release_tree_vector (args);
5776 break;
5779 if (BASELINK_P (fn))
5781 postfix_expression
5782 = (build_new_method_call
5783 (instance, fn, &args, NULL_TREE,
5784 (idk == CP_ID_KIND_QUALIFIED
5785 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5786 : LOOKUP_NORMAL),
5787 /*fn_p=*/NULL,
5788 tf_warning_or_error));
5790 else
5791 postfix_expression
5792 = finish_call_expr (postfix_expression, &args,
5793 /*disallow_virtual=*/false,
5794 /*koenig_p=*/false,
5795 tf_warning_or_error);
5797 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5798 || TREE_CODE (postfix_expression) == MEMBER_REF
5799 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5800 postfix_expression = (build_offset_ref_call_from_tree
5801 (postfix_expression, &args,
5802 tf_warning_or_error));
5803 else if (idk == CP_ID_KIND_QUALIFIED)
5804 /* A call to a static class member, or a namespace-scope
5805 function. */
5806 postfix_expression
5807 = finish_call_expr (postfix_expression, &args,
5808 /*disallow_virtual=*/true,
5809 koenig_p,
5810 tf_warning_or_error);
5811 else
5812 /* All other function calls. */
5813 postfix_expression
5814 = finish_call_expr (postfix_expression, &args,
5815 /*disallow_virtual=*/false,
5816 koenig_p,
5817 tf_warning_or_error);
5819 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5820 idk = CP_ID_KIND_NONE;
5822 release_tree_vector (args);
5824 break;
5826 case CPP_DOT:
5827 case CPP_DEREF:
5828 /* postfix-expression . template [opt] id-expression
5829 postfix-expression . pseudo-destructor-name
5830 postfix-expression -> template [opt] id-expression
5831 postfix-expression -> pseudo-destructor-name */
5833 /* Consume the `.' or `->' operator. */
5834 cp_lexer_consume_token (parser->lexer);
5836 postfix_expression
5837 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5838 postfix_expression,
5839 false, &idk,
5840 token->location);
5842 is_member_access = true;
5843 break;
5845 case CPP_PLUS_PLUS:
5846 /* postfix-expression ++ */
5847 /* Consume the `++' token. */
5848 cp_lexer_consume_token (parser->lexer);
5849 /* Generate a representation for the complete expression. */
5850 postfix_expression
5851 = finish_increment_expr (postfix_expression,
5852 POSTINCREMENT_EXPR);
5853 /* Increments may not appear in constant-expressions. */
5854 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5855 postfix_expression = error_mark_node;
5856 idk = CP_ID_KIND_NONE;
5857 is_member_access = false;
5858 break;
5860 case CPP_MINUS_MINUS:
5861 /* postfix-expression -- */
5862 /* Consume the `--' token. */
5863 cp_lexer_consume_token (parser->lexer);
5864 /* Generate a representation for the complete expression. */
5865 postfix_expression
5866 = finish_increment_expr (postfix_expression,
5867 POSTDECREMENT_EXPR);
5868 /* Decrements may not appear in constant-expressions. */
5869 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5870 postfix_expression = error_mark_node;
5871 idk = CP_ID_KIND_NONE;
5872 is_member_access = false;
5873 break;
5875 default:
5876 if (pidk_return != NULL)
5877 * pidk_return = idk;
5878 if (member_access_only_p)
5879 return is_member_access? postfix_expression : error_mark_node;
5880 else
5881 return postfix_expression;
5885 /* We should never get here. */
5886 gcc_unreachable ();
5887 return error_mark_node;
5890 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5891 by cp_parser_builtin_offsetof. We're looking for
5893 postfix-expression [ expression ]
5894 postfix-expression [ braced-init-list ] (C++11)
5896 FOR_OFFSETOF is set if we're being called in that context, which
5897 changes how we deal with integer constant expressions. */
5899 static tree
5900 cp_parser_postfix_open_square_expression (cp_parser *parser,
5901 tree postfix_expression,
5902 bool for_offsetof)
5904 tree index;
5905 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5907 /* Consume the `[' token. */
5908 cp_lexer_consume_token (parser->lexer);
5910 /* Parse the index expression. */
5911 /* ??? For offsetof, there is a question of what to allow here. If
5912 offsetof is not being used in an integral constant expression context,
5913 then we *could* get the right answer by computing the value at runtime.
5914 If we are in an integral constant expression context, then we might
5915 could accept any constant expression; hard to say without analysis.
5916 Rather than open the barn door too wide right away, allow only integer
5917 constant expressions here. */
5918 if (for_offsetof)
5919 index = cp_parser_constant_expression (parser, false, NULL);
5920 else
5922 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5924 bool expr_nonconst_p;
5925 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5926 index = cp_parser_braced_list (parser, &expr_nonconst_p);
5928 else
5929 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5932 /* Look for the closing `]'. */
5933 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5935 /* Build the ARRAY_REF. */
5936 postfix_expression = grok_array_decl (loc, postfix_expression, index);
5938 /* When not doing offsetof, array references are not permitted in
5939 constant-expressions. */
5940 if (!for_offsetof
5941 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5942 postfix_expression = error_mark_node;
5944 return postfix_expression;
5947 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5948 by cp_parser_builtin_offsetof. We're looking for
5950 postfix-expression . template [opt] id-expression
5951 postfix-expression . pseudo-destructor-name
5952 postfix-expression -> template [opt] id-expression
5953 postfix-expression -> pseudo-destructor-name
5955 FOR_OFFSETOF is set if we're being called in that context. That sorta
5956 limits what of the above we'll actually accept, but nevermind.
5957 TOKEN_TYPE is the "." or "->" token, which will already have been
5958 removed from the stream. */
5960 static tree
5961 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5962 enum cpp_ttype token_type,
5963 tree postfix_expression,
5964 bool for_offsetof, cp_id_kind *idk,
5965 location_t location)
5967 tree name;
5968 bool dependent_p;
5969 bool pseudo_destructor_p;
5970 tree scope = NULL_TREE;
5972 /* If this is a `->' operator, dereference the pointer. */
5973 if (token_type == CPP_DEREF)
5974 postfix_expression = build_x_arrow (location, postfix_expression,
5975 tf_warning_or_error);
5976 /* Check to see whether or not the expression is type-dependent. */
5977 dependent_p = type_dependent_expression_p (postfix_expression);
5978 /* The identifier following the `->' or `.' is not qualified. */
5979 parser->scope = NULL_TREE;
5980 parser->qualifying_scope = NULL_TREE;
5981 parser->object_scope = NULL_TREE;
5982 *idk = CP_ID_KIND_NONE;
5984 /* Enter the scope corresponding to the type of the object
5985 given by the POSTFIX_EXPRESSION. */
5986 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5988 scope = TREE_TYPE (postfix_expression);
5989 /* According to the standard, no expression should ever have
5990 reference type. Unfortunately, we do not currently match
5991 the standard in this respect in that our internal representation
5992 of an expression may have reference type even when the standard
5993 says it does not. Therefore, we have to manually obtain the
5994 underlying type here. */
5995 scope = non_reference (scope);
5996 /* The type of the POSTFIX_EXPRESSION must be complete. */
5997 if (scope == unknown_type_node)
5999 error_at (location, "%qE does not have class type",
6000 postfix_expression);
6001 scope = NULL_TREE;
6003 /* Unlike the object expression in other contexts, *this is not
6004 required to be of complete type for purposes of class member
6005 access (5.2.5) outside the member function body. */
6006 else if (scope != current_class_ref
6007 && !(processing_template_decl && scope == current_class_type))
6008 scope = complete_type_or_else (scope, NULL_TREE);
6009 /* Let the name lookup machinery know that we are processing a
6010 class member access expression. */
6011 parser->context->object_type = scope;
6012 /* If something went wrong, we want to be able to discern that case,
6013 as opposed to the case where there was no SCOPE due to the type
6014 of expression being dependent. */
6015 if (!scope)
6016 scope = error_mark_node;
6017 /* If the SCOPE was erroneous, make the various semantic analysis
6018 functions exit quickly -- and without issuing additional error
6019 messages. */
6020 if (scope == error_mark_node)
6021 postfix_expression = error_mark_node;
6024 /* Assume this expression is not a pseudo-destructor access. */
6025 pseudo_destructor_p = false;
6027 /* If the SCOPE is a scalar type, then, if this is a valid program,
6028 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6029 is type dependent, it can be pseudo-destructor-name or something else.
6030 Try to parse it as pseudo-destructor-name first. */
6031 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6033 tree s;
6034 tree type;
6036 cp_parser_parse_tentatively (parser);
6037 /* Parse the pseudo-destructor-name. */
6038 s = NULL_TREE;
6039 cp_parser_pseudo_destructor_name (parser, &s, &type);
6040 if (dependent_p
6041 && (cp_parser_error_occurred (parser)
6042 || TREE_CODE (type) != TYPE_DECL
6043 || !SCALAR_TYPE_P (TREE_TYPE (type))))
6044 cp_parser_abort_tentative_parse (parser);
6045 else if (cp_parser_parse_definitely (parser))
6047 pseudo_destructor_p = true;
6048 postfix_expression
6049 = finish_pseudo_destructor_expr (postfix_expression,
6050 s, TREE_TYPE (type));
6054 if (!pseudo_destructor_p)
6056 /* If the SCOPE is not a scalar type, we are looking at an
6057 ordinary class member access expression, rather than a
6058 pseudo-destructor-name. */
6059 bool template_p;
6060 cp_token *token = cp_lexer_peek_token (parser->lexer);
6061 /* Parse the id-expression. */
6062 name = (cp_parser_id_expression
6063 (parser,
6064 cp_parser_optional_template_keyword (parser),
6065 /*check_dependency_p=*/true,
6066 &template_p,
6067 /*declarator_p=*/false,
6068 /*optional_p=*/false));
6069 /* In general, build a SCOPE_REF if the member name is qualified.
6070 However, if the name was not dependent and has already been
6071 resolved; there is no need to build the SCOPE_REF. For example;
6073 struct X { void f(); };
6074 template <typename T> void f(T* t) { t->X::f(); }
6076 Even though "t" is dependent, "X::f" is not and has been resolved
6077 to a BASELINK; there is no need to include scope information. */
6079 /* But we do need to remember that there was an explicit scope for
6080 virtual function calls. */
6081 if (parser->scope)
6082 *idk = CP_ID_KIND_QUALIFIED;
6084 /* If the name is a template-id that names a type, we will get a
6085 TYPE_DECL here. That is invalid code. */
6086 if (TREE_CODE (name) == TYPE_DECL)
6088 error_at (token->location, "invalid use of %qD", name);
6089 postfix_expression = error_mark_node;
6091 else
6093 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6095 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6097 error_at (token->location, "%<%D::%D%> is not a class member",
6098 parser->scope, name);
6099 postfix_expression = error_mark_node;
6101 else
6102 name = build_qualified_name (/*type=*/NULL_TREE,
6103 parser->scope,
6104 name,
6105 template_p);
6106 parser->scope = NULL_TREE;
6107 parser->qualifying_scope = NULL_TREE;
6108 parser->object_scope = NULL_TREE;
6110 if (parser->scope && name && BASELINK_P (name))
6111 adjust_result_of_qualified_name_lookup
6112 (name, parser->scope, scope);
6113 postfix_expression
6114 = finish_class_member_access_expr (postfix_expression, name,
6115 template_p,
6116 tf_warning_or_error);
6120 /* We no longer need to look up names in the scope of the object on
6121 the left-hand side of the `.' or `->' operator. */
6122 parser->context->object_type = NULL_TREE;
6124 /* Outside of offsetof, these operators may not appear in
6125 constant-expressions. */
6126 if (!for_offsetof
6127 && (cp_parser_non_integral_constant_expression
6128 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6129 postfix_expression = error_mark_node;
6131 return postfix_expression;
6134 /* Parse a parenthesized expression-list.
6136 expression-list:
6137 assignment-expression
6138 expression-list, assignment-expression
6140 attribute-list:
6141 expression-list
6142 identifier
6143 identifier, expression-list
6145 CAST_P is true if this expression is the target of a cast.
6147 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6148 argument pack.
6150 Returns a vector of trees. Each element is a representation of an
6151 assignment-expression. NULL is returned if the ( and or ) are
6152 missing. An empty, but allocated, vector is returned on no
6153 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6154 if we are parsing an attribute list for an attribute that wants a
6155 plain identifier argument, normal_attr for an attribute that wants
6156 an expression, or non_attr if we aren't parsing an attribute list. If
6157 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6158 not all of the expressions in the list were constant. */
6160 static vec<tree, va_gc> *
6161 cp_parser_parenthesized_expression_list (cp_parser* parser,
6162 int is_attribute_list,
6163 bool cast_p,
6164 bool allow_expansion_p,
6165 bool *non_constant_p)
6167 vec<tree, va_gc> *expression_list;
6168 bool fold_expr_p = is_attribute_list != non_attr;
6169 tree identifier = NULL_TREE;
6170 bool saved_greater_than_is_operator_p;
6172 /* Assume all the expressions will be constant. */
6173 if (non_constant_p)
6174 *non_constant_p = false;
6176 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6177 return NULL;
6179 expression_list = make_tree_vector ();
6181 /* Within a parenthesized expression, a `>' token is always
6182 the greater-than operator. */
6183 saved_greater_than_is_operator_p
6184 = parser->greater_than_is_operator_p;
6185 parser->greater_than_is_operator_p = true;
6187 /* Consume expressions until there are no more. */
6188 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6189 while (true)
6191 tree expr;
6193 /* At the beginning of attribute lists, check to see if the
6194 next token is an identifier. */
6195 if (is_attribute_list == id_attr
6196 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6198 cp_token *token;
6200 /* Consume the identifier. */
6201 token = cp_lexer_consume_token (parser->lexer);
6202 /* Save the identifier. */
6203 identifier = token->u.value;
6205 else
6207 bool expr_non_constant_p;
6209 /* Parse the next assignment-expression. */
6210 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6212 /* A braced-init-list. */
6213 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6214 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6215 if (non_constant_p && expr_non_constant_p)
6216 *non_constant_p = true;
6218 else if (non_constant_p)
6220 expr = (cp_parser_constant_expression
6221 (parser, /*allow_non_constant_p=*/true,
6222 &expr_non_constant_p));
6223 if (expr_non_constant_p)
6224 *non_constant_p = true;
6226 else
6227 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6229 if (fold_expr_p)
6230 expr = fold_non_dependent_expr (expr);
6232 /* If we have an ellipsis, then this is an expression
6233 expansion. */
6234 if (allow_expansion_p
6235 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6237 /* Consume the `...'. */
6238 cp_lexer_consume_token (parser->lexer);
6240 /* Build the argument pack. */
6241 expr = make_pack_expansion (expr);
6244 /* Add it to the list. We add error_mark_node
6245 expressions to the list, so that we can still tell if
6246 the correct form for a parenthesized expression-list
6247 is found. That gives better errors. */
6248 vec_safe_push (expression_list, expr);
6250 if (expr == error_mark_node)
6251 goto skip_comma;
6254 /* After the first item, attribute lists look the same as
6255 expression lists. */
6256 is_attribute_list = non_attr;
6258 get_comma:;
6259 /* If the next token isn't a `,', then we are done. */
6260 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6261 break;
6263 /* Otherwise, consume the `,' and keep going. */
6264 cp_lexer_consume_token (parser->lexer);
6267 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6269 int ending;
6271 skip_comma:;
6272 /* We try and resync to an unnested comma, as that will give the
6273 user better diagnostics. */
6274 ending = cp_parser_skip_to_closing_parenthesis (parser,
6275 /*recovering=*/true,
6276 /*or_comma=*/true,
6277 /*consume_paren=*/true);
6278 if (ending < 0)
6279 goto get_comma;
6280 if (!ending)
6282 parser->greater_than_is_operator_p
6283 = saved_greater_than_is_operator_p;
6284 return NULL;
6288 parser->greater_than_is_operator_p
6289 = saved_greater_than_is_operator_p;
6291 if (identifier)
6292 vec_safe_insert (expression_list, 0, identifier);
6294 return expression_list;
6297 /* Parse a pseudo-destructor-name.
6299 pseudo-destructor-name:
6300 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6301 :: [opt] nested-name-specifier template template-id :: ~ type-name
6302 :: [opt] nested-name-specifier [opt] ~ type-name
6304 If either of the first two productions is used, sets *SCOPE to the
6305 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6306 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6307 or ERROR_MARK_NODE if the parse fails. */
6309 static void
6310 cp_parser_pseudo_destructor_name (cp_parser* parser,
6311 tree* scope,
6312 tree* type)
6314 bool nested_name_specifier_p;
6316 /* Assume that things will not work out. */
6317 *type = error_mark_node;
6319 /* Look for the optional `::' operator. */
6320 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6321 /* Look for the optional nested-name-specifier. */
6322 nested_name_specifier_p
6323 = (cp_parser_nested_name_specifier_opt (parser,
6324 /*typename_keyword_p=*/false,
6325 /*check_dependency_p=*/true,
6326 /*type_p=*/false,
6327 /*is_declaration=*/false)
6328 != NULL_TREE);
6329 /* Now, if we saw a nested-name-specifier, we might be doing the
6330 second production. */
6331 if (nested_name_specifier_p
6332 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6334 /* Consume the `template' keyword. */
6335 cp_lexer_consume_token (parser->lexer);
6336 /* Parse the template-id. */
6337 cp_parser_template_id (parser,
6338 /*template_keyword_p=*/true,
6339 /*check_dependency_p=*/false,
6340 class_type,
6341 /*is_declaration=*/true);
6342 /* Look for the `::' token. */
6343 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6345 /* If the next token is not a `~', then there might be some
6346 additional qualification. */
6347 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6349 /* At this point, we're looking for "type-name :: ~". The type-name
6350 must not be a class-name, since this is a pseudo-destructor. So,
6351 it must be either an enum-name, or a typedef-name -- both of which
6352 are just identifiers. So, we peek ahead to check that the "::"
6353 and "~" tokens are present; if they are not, then we can avoid
6354 calling type_name. */
6355 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6356 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6357 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6359 cp_parser_error (parser, "non-scalar type");
6360 return;
6363 /* Look for the type-name. */
6364 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6365 if (*scope == error_mark_node)
6366 return;
6368 /* Look for the `::' token. */
6369 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6371 else
6372 *scope = NULL_TREE;
6374 /* Look for the `~'. */
6375 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6377 /* Once we see the ~, this has to be a pseudo-destructor. */
6378 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6379 cp_parser_commit_to_tentative_parse (parser);
6381 /* Look for the type-name again. We are not responsible for
6382 checking that it matches the first type-name. */
6383 *type = cp_parser_nonclass_name (parser);
6386 /* Parse a unary-expression.
6388 unary-expression:
6389 postfix-expression
6390 ++ cast-expression
6391 -- cast-expression
6392 unary-operator cast-expression
6393 sizeof unary-expression
6394 sizeof ( type-id )
6395 alignof ( type-id ) [C++0x]
6396 new-expression
6397 delete-expression
6399 GNU Extensions:
6401 unary-expression:
6402 __extension__ cast-expression
6403 __alignof__ unary-expression
6404 __alignof__ ( type-id )
6405 alignof unary-expression [C++0x]
6406 __real__ cast-expression
6407 __imag__ cast-expression
6408 && identifier
6410 ADDRESS_P is true iff the unary-expression is appearing as the
6411 operand of the `&' operator. CAST_P is true if this expression is
6412 the target of a cast.
6414 Returns a representation of the expression. */
6416 static tree
6417 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6418 cp_id_kind * pidk)
6420 cp_token *token;
6421 enum tree_code unary_operator;
6423 /* Peek at the next token. */
6424 token = cp_lexer_peek_token (parser->lexer);
6425 /* Some keywords give away the kind of expression. */
6426 if (token->type == CPP_KEYWORD)
6428 enum rid keyword = token->keyword;
6430 switch (keyword)
6432 case RID_ALIGNOF:
6433 case RID_SIZEOF:
6435 tree operand, ret;
6436 enum tree_code op;
6437 location_t first_loc;
6439 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6440 /* Consume the token. */
6441 cp_lexer_consume_token (parser->lexer);
6442 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6443 /* Parse the operand. */
6444 operand = cp_parser_sizeof_operand (parser, keyword);
6446 if (TYPE_P (operand))
6447 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6448 else
6450 /* ISO C++ defines alignof only with types, not with
6451 expressions. So pedwarn if alignof is used with a non-
6452 type expression. However, __alignof__ is ok. */
6453 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6454 pedwarn (token->location, OPT_Wpedantic,
6455 "ISO C++ does not allow %<alignof%> "
6456 "with a non-type");
6458 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6460 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6461 SIZEOF_EXPR with the original operand. */
6462 if (op == SIZEOF_EXPR && ret != error_mark_node)
6464 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6466 if (!processing_template_decl && TYPE_P (operand))
6468 ret = build_min (SIZEOF_EXPR, size_type_node,
6469 build1 (NOP_EXPR, operand,
6470 error_mark_node));
6471 SIZEOF_EXPR_TYPE_P (ret) = 1;
6473 else
6474 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6475 TREE_SIDE_EFFECTS (ret) = 0;
6476 TREE_READONLY (ret) = 1;
6478 SET_EXPR_LOCATION (ret, first_loc);
6480 return ret;
6483 case RID_NEW:
6484 return cp_parser_new_expression (parser);
6486 case RID_DELETE:
6487 return cp_parser_delete_expression (parser);
6489 case RID_EXTENSION:
6491 /* The saved value of the PEDANTIC flag. */
6492 int saved_pedantic;
6493 tree expr;
6495 /* Save away the PEDANTIC flag. */
6496 cp_parser_extension_opt (parser, &saved_pedantic);
6497 /* Parse the cast-expression. */
6498 expr = cp_parser_simple_cast_expression (parser);
6499 /* Restore the PEDANTIC flag. */
6500 pedantic = saved_pedantic;
6502 return expr;
6505 case RID_REALPART:
6506 case RID_IMAGPART:
6508 tree expression;
6510 /* Consume the `__real__' or `__imag__' token. */
6511 cp_lexer_consume_token (parser->lexer);
6512 /* Parse the cast-expression. */
6513 expression = cp_parser_simple_cast_expression (parser);
6514 /* Create the complete representation. */
6515 return build_x_unary_op (token->location,
6516 (keyword == RID_REALPART
6517 ? REALPART_EXPR : IMAGPART_EXPR),
6518 expression,
6519 tf_warning_or_error);
6521 break;
6523 case RID_TRANSACTION_ATOMIC:
6524 case RID_TRANSACTION_RELAXED:
6525 return cp_parser_transaction_expression (parser, keyword);
6527 case RID_NOEXCEPT:
6529 tree expr;
6530 const char *saved_message;
6531 bool saved_integral_constant_expression_p;
6532 bool saved_non_integral_constant_expression_p;
6533 bool saved_greater_than_is_operator_p;
6535 cp_lexer_consume_token (parser->lexer);
6536 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6538 saved_message = parser->type_definition_forbidden_message;
6539 parser->type_definition_forbidden_message
6540 = G_("types may not be defined in %<noexcept%> expressions");
6542 saved_integral_constant_expression_p
6543 = parser->integral_constant_expression_p;
6544 saved_non_integral_constant_expression_p
6545 = parser->non_integral_constant_expression_p;
6546 parser->integral_constant_expression_p = false;
6548 saved_greater_than_is_operator_p
6549 = parser->greater_than_is_operator_p;
6550 parser->greater_than_is_operator_p = true;
6552 ++cp_unevaluated_operand;
6553 ++c_inhibit_evaluation_warnings;
6554 expr = cp_parser_expression (parser, false, NULL);
6555 --c_inhibit_evaluation_warnings;
6556 --cp_unevaluated_operand;
6558 parser->greater_than_is_operator_p
6559 = saved_greater_than_is_operator_p;
6561 parser->integral_constant_expression_p
6562 = saved_integral_constant_expression_p;
6563 parser->non_integral_constant_expression_p
6564 = saved_non_integral_constant_expression_p;
6566 parser->type_definition_forbidden_message = saved_message;
6568 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6569 return finish_noexcept_expr (expr, tf_warning_or_error);
6572 default:
6573 break;
6577 /* Look for the `:: new' and `:: delete', which also signal the
6578 beginning of a new-expression, or delete-expression,
6579 respectively. If the next token is `::', then it might be one of
6580 these. */
6581 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6583 enum rid keyword;
6585 /* See if the token after the `::' is one of the keywords in
6586 which we're interested. */
6587 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6588 /* If it's `new', we have a new-expression. */
6589 if (keyword == RID_NEW)
6590 return cp_parser_new_expression (parser);
6591 /* Similarly, for `delete'. */
6592 else if (keyword == RID_DELETE)
6593 return cp_parser_delete_expression (parser);
6596 /* Look for a unary operator. */
6597 unary_operator = cp_parser_unary_operator (token);
6598 /* The `++' and `--' operators can be handled similarly, even though
6599 they are not technically unary-operators in the grammar. */
6600 if (unary_operator == ERROR_MARK)
6602 if (token->type == CPP_PLUS_PLUS)
6603 unary_operator = PREINCREMENT_EXPR;
6604 else if (token->type == CPP_MINUS_MINUS)
6605 unary_operator = PREDECREMENT_EXPR;
6606 /* Handle the GNU address-of-label extension. */
6607 else if (cp_parser_allow_gnu_extensions_p (parser)
6608 && token->type == CPP_AND_AND)
6610 tree identifier;
6611 tree expression;
6612 location_t loc = token->location;
6614 /* Consume the '&&' token. */
6615 cp_lexer_consume_token (parser->lexer);
6616 /* Look for the identifier. */
6617 identifier = cp_parser_identifier (parser);
6618 /* Create an expression representing the address. */
6619 expression = finish_label_address_expr (identifier, loc);
6620 if (cp_parser_non_integral_constant_expression (parser,
6621 NIC_ADDR_LABEL))
6622 expression = error_mark_node;
6623 return expression;
6626 if (unary_operator != ERROR_MARK)
6628 tree cast_expression;
6629 tree expression = error_mark_node;
6630 non_integral_constant non_constant_p = NIC_NONE;
6631 location_t loc = token->location;
6633 /* Consume the operator token. */
6634 token = cp_lexer_consume_token (parser->lexer);
6635 /* Parse the cast-expression. */
6636 cast_expression
6637 = cp_parser_cast_expression (parser,
6638 unary_operator == ADDR_EXPR,
6639 /*cast_p=*/false, pidk);
6640 /* Now, build an appropriate representation. */
6641 switch (unary_operator)
6643 case INDIRECT_REF:
6644 non_constant_p = NIC_STAR;
6645 expression = build_x_indirect_ref (loc, cast_expression,
6646 RO_UNARY_STAR,
6647 tf_warning_or_error);
6648 break;
6650 case ADDR_EXPR:
6651 non_constant_p = NIC_ADDR;
6652 /* Fall through. */
6653 case BIT_NOT_EXPR:
6654 expression = build_x_unary_op (loc, unary_operator,
6655 cast_expression,
6656 tf_warning_or_error);
6657 break;
6659 case PREINCREMENT_EXPR:
6660 case PREDECREMENT_EXPR:
6661 non_constant_p = unary_operator == PREINCREMENT_EXPR
6662 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6663 /* Fall through. */
6664 case UNARY_PLUS_EXPR:
6665 case NEGATE_EXPR:
6666 case TRUTH_NOT_EXPR:
6667 expression = finish_unary_op_expr (loc, unary_operator,
6668 cast_expression);
6669 break;
6671 default:
6672 gcc_unreachable ();
6675 if (non_constant_p != NIC_NONE
6676 && cp_parser_non_integral_constant_expression (parser,
6677 non_constant_p))
6678 expression = error_mark_node;
6680 return expression;
6683 return cp_parser_postfix_expression (parser, address_p, cast_p,
6684 /*member_access_only_p=*/false,
6685 pidk);
6688 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6689 unary-operator, the corresponding tree code is returned. */
6691 static enum tree_code
6692 cp_parser_unary_operator (cp_token* token)
6694 switch (token->type)
6696 case CPP_MULT:
6697 return INDIRECT_REF;
6699 case CPP_AND:
6700 return ADDR_EXPR;
6702 case CPP_PLUS:
6703 return UNARY_PLUS_EXPR;
6705 case CPP_MINUS:
6706 return NEGATE_EXPR;
6708 case CPP_NOT:
6709 return TRUTH_NOT_EXPR;
6711 case CPP_COMPL:
6712 return BIT_NOT_EXPR;
6714 default:
6715 return ERROR_MARK;
6719 /* Parse a new-expression.
6721 new-expression:
6722 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6723 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6725 Returns a representation of the expression. */
6727 static tree
6728 cp_parser_new_expression (cp_parser* parser)
6730 bool global_scope_p;
6731 vec<tree, va_gc> *placement;
6732 tree type;
6733 vec<tree, va_gc> *initializer;
6734 tree nelts = NULL_TREE;
6735 tree ret;
6737 /* Look for the optional `::' operator. */
6738 global_scope_p
6739 = (cp_parser_global_scope_opt (parser,
6740 /*current_scope_valid_p=*/false)
6741 != NULL_TREE);
6742 /* Look for the `new' operator. */
6743 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6744 /* There's no easy way to tell a new-placement from the
6745 `( type-id )' construct. */
6746 cp_parser_parse_tentatively (parser);
6747 /* Look for a new-placement. */
6748 placement = cp_parser_new_placement (parser);
6749 /* If that didn't work out, there's no new-placement. */
6750 if (!cp_parser_parse_definitely (parser))
6752 if (placement != NULL)
6753 release_tree_vector (placement);
6754 placement = NULL;
6757 /* If the next token is a `(', then we have a parenthesized
6758 type-id. */
6759 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6761 cp_token *token;
6762 const char *saved_message = parser->type_definition_forbidden_message;
6764 /* Consume the `('. */
6765 cp_lexer_consume_token (parser->lexer);
6767 /* Parse the type-id. */
6768 parser->type_definition_forbidden_message
6769 = G_("types may not be defined in a new-expression");
6770 type = cp_parser_type_id (parser);
6771 parser->type_definition_forbidden_message = saved_message;
6773 /* Look for the closing `)'. */
6774 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6775 token = cp_lexer_peek_token (parser->lexer);
6776 /* There should not be a direct-new-declarator in this production,
6777 but GCC used to allowed this, so we check and emit a sensible error
6778 message for this case. */
6779 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6781 error_at (token->location,
6782 "array bound forbidden after parenthesized type-id");
6783 inform (token->location,
6784 "try removing the parentheses around the type-id");
6785 cp_parser_direct_new_declarator (parser);
6788 /* Otherwise, there must be a new-type-id. */
6789 else
6790 type = cp_parser_new_type_id (parser, &nelts);
6792 /* If the next token is a `(' or '{', then we have a new-initializer. */
6793 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6794 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6795 initializer = cp_parser_new_initializer (parser);
6796 else
6797 initializer = NULL;
6799 /* A new-expression may not appear in an integral constant
6800 expression. */
6801 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6802 ret = error_mark_node;
6803 else
6805 /* Create a representation of the new-expression. */
6806 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6807 tf_warning_or_error);
6810 if (placement != NULL)
6811 release_tree_vector (placement);
6812 if (initializer != NULL)
6813 release_tree_vector (initializer);
6815 return ret;
6818 /* Parse a new-placement.
6820 new-placement:
6821 ( expression-list )
6823 Returns the same representation as for an expression-list. */
6825 static vec<tree, va_gc> *
6826 cp_parser_new_placement (cp_parser* parser)
6828 vec<tree, va_gc> *expression_list;
6830 /* Parse the expression-list. */
6831 expression_list = (cp_parser_parenthesized_expression_list
6832 (parser, non_attr, /*cast_p=*/false,
6833 /*allow_expansion_p=*/true,
6834 /*non_constant_p=*/NULL));
6836 return expression_list;
6839 /* Parse a new-type-id.
6841 new-type-id:
6842 type-specifier-seq new-declarator [opt]
6844 Returns the TYPE allocated. If the new-type-id indicates an array
6845 type, *NELTS is set to the number of elements in the last array
6846 bound; the TYPE will not include the last array bound. */
6848 static tree
6849 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6851 cp_decl_specifier_seq type_specifier_seq;
6852 cp_declarator *new_declarator;
6853 cp_declarator *declarator;
6854 cp_declarator *outer_declarator;
6855 const char *saved_message;
6857 /* The type-specifier sequence must not contain type definitions.
6858 (It cannot contain declarations of new types either, but if they
6859 are not definitions we will catch that because they are not
6860 complete.) */
6861 saved_message = parser->type_definition_forbidden_message;
6862 parser->type_definition_forbidden_message
6863 = G_("types may not be defined in a new-type-id");
6864 /* Parse the type-specifier-seq. */
6865 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6866 /*is_trailing_return=*/false,
6867 &type_specifier_seq);
6868 /* Restore the old message. */
6869 parser->type_definition_forbidden_message = saved_message;
6871 if (type_specifier_seq.type == error_mark_node)
6872 return error_mark_node;
6874 /* Parse the new-declarator. */
6875 new_declarator = cp_parser_new_declarator_opt (parser);
6877 /* Determine the number of elements in the last array dimension, if
6878 any. */
6879 *nelts = NULL_TREE;
6880 /* Skip down to the last array dimension. */
6881 declarator = new_declarator;
6882 outer_declarator = NULL;
6883 while (declarator && (declarator->kind == cdk_pointer
6884 || declarator->kind == cdk_ptrmem))
6886 outer_declarator = declarator;
6887 declarator = declarator->declarator;
6889 while (declarator
6890 && declarator->kind == cdk_array
6891 && declarator->declarator
6892 && declarator->declarator->kind == cdk_array)
6894 outer_declarator = declarator;
6895 declarator = declarator->declarator;
6898 if (declarator && declarator->kind == cdk_array)
6900 *nelts = declarator->u.array.bounds;
6901 if (*nelts == error_mark_node)
6902 *nelts = integer_one_node;
6904 if (outer_declarator)
6905 outer_declarator->declarator = declarator->declarator;
6906 else
6907 new_declarator = NULL;
6910 return groktypename (&type_specifier_seq, new_declarator, false);
6913 /* Parse an (optional) new-declarator.
6915 new-declarator:
6916 ptr-operator new-declarator [opt]
6917 direct-new-declarator
6919 Returns the declarator. */
6921 static cp_declarator *
6922 cp_parser_new_declarator_opt (cp_parser* parser)
6924 enum tree_code code;
6925 tree type, std_attributes = NULL_TREE;
6926 cp_cv_quals cv_quals;
6928 /* We don't know if there's a ptr-operator next, or not. */
6929 cp_parser_parse_tentatively (parser);
6930 /* Look for a ptr-operator. */
6931 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
6932 /* If that worked, look for more new-declarators. */
6933 if (cp_parser_parse_definitely (parser))
6935 cp_declarator *declarator;
6937 /* Parse another optional declarator. */
6938 declarator = cp_parser_new_declarator_opt (parser);
6940 declarator = cp_parser_make_indirect_declarator
6941 (code, type, cv_quals, declarator, std_attributes);
6943 return declarator;
6946 /* If the next token is a `[', there is a direct-new-declarator. */
6947 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6948 return cp_parser_direct_new_declarator (parser);
6950 return NULL;
6953 /* Parse a direct-new-declarator.
6955 direct-new-declarator:
6956 [ expression ]
6957 direct-new-declarator [constant-expression]
6961 static cp_declarator *
6962 cp_parser_direct_new_declarator (cp_parser* parser)
6964 cp_declarator *declarator = NULL;
6966 while (true)
6968 tree expression;
6969 cp_token *token;
6971 /* Look for the opening `['. */
6972 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6974 token = cp_lexer_peek_token (parser->lexer);
6975 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6976 /* The standard requires that the expression have integral
6977 type. DR 74 adds enumeration types. We believe that the
6978 real intent is that these expressions be handled like the
6979 expression in a `switch' condition, which also allows
6980 classes with a single conversion to integral or
6981 enumeration type. */
6982 if (!processing_template_decl)
6984 expression
6985 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6986 expression,
6987 /*complain=*/true);
6988 if (!expression)
6990 error_at (token->location,
6991 "expression in new-declarator must have integral "
6992 "or enumeration type");
6993 expression = error_mark_node;
6997 /* Look for the closing `]'. */
6998 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7000 /* Add this bound to the declarator. */
7001 declarator = make_array_declarator (declarator, expression);
7003 /* If the next token is not a `[', then there are no more
7004 bounds. */
7005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7006 break;
7009 return declarator;
7012 /* Parse a new-initializer.
7014 new-initializer:
7015 ( expression-list [opt] )
7016 braced-init-list
7018 Returns a representation of the expression-list. */
7020 static vec<tree, va_gc> *
7021 cp_parser_new_initializer (cp_parser* parser)
7023 vec<tree, va_gc> *expression_list;
7025 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7027 tree t;
7028 bool expr_non_constant_p;
7029 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7030 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7031 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7032 expression_list = make_tree_vector_single (t);
7034 else
7035 expression_list = (cp_parser_parenthesized_expression_list
7036 (parser, non_attr, /*cast_p=*/false,
7037 /*allow_expansion_p=*/true,
7038 /*non_constant_p=*/NULL));
7040 return expression_list;
7043 /* Parse a delete-expression.
7045 delete-expression:
7046 :: [opt] delete cast-expression
7047 :: [opt] delete [ ] cast-expression
7049 Returns a representation of the expression. */
7051 static tree
7052 cp_parser_delete_expression (cp_parser* parser)
7054 bool global_scope_p;
7055 bool array_p;
7056 tree expression;
7058 /* Look for the optional `::' operator. */
7059 global_scope_p
7060 = (cp_parser_global_scope_opt (parser,
7061 /*current_scope_valid_p=*/false)
7062 != NULL_TREE);
7063 /* Look for the `delete' keyword. */
7064 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7065 /* See if the array syntax is in use. */
7066 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7068 /* Consume the `[' token. */
7069 cp_lexer_consume_token (parser->lexer);
7070 /* Look for the `]' token. */
7071 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7072 /* Remember that this is the `[]' construct. */
7073 array_p = true;
7075 else
7076 array_p = false;
7078 /* Parse the cast-expression. */
7079 expression = cp_parser_simple_cast_expression (parser);
7081 /* A delete-expression may not appear in an integral constant
7082 expression. */
7083 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7084 return error_mark_node;
7086 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7087 tf_warning_or_error);
7090 /* Returns true if TOKEN may start a cast-expression and false
7091 otherwise. */
7093 static bool
7094 cp_parser_token_starts_cast_expression (cp_token *token)
7096 switch (token->type)
7098 case CPP_COMMA:
7099 case CPP_SEMICOLON:
7100 case CPP_QUERY:
7101 case CPP_COLON:
7102 case CPP_CLOSE_SQUARE:
7103 case CPP_CLOSE_PAREN:
7104 case CPP_CLOSE_BRACE:
7105 case CPP_DOT:
7106 case CPP_DOT_STAR:
7107 case CPP_DEREF:
7108 case CPP_DEREF_STAR:
7109 case CPP_DIV:
7110 case CPP_MOD:
7111 case CPP_LSHIFT:
7112 case CPP_RSHIFT:
7113 case CPP_LESS:
7114 case CPP_GREATER:
7115 case CPP_LESS_EQ:
7116 case CPP_GREATER_EQ:
7117 case CPP_EQ_EQ:
7118 case CPP_NOT_EQ:
7119 case CPP_EQ:
7120 case CPP_MULT_EQ:
7121 case CPP_DIV_EQ:
7122 case CPP_MOD_EQ:
7123 case CPP_PLUS_EQ:
7124 case CPP_MINUS_EQ:
7125 case CPP_RSHIFT_EQ:
7126 case CPP_LSHIFT_EQ:
7127 case CPP_AND_EQ:
7128 case CPP_XOR_EQ:
7129 case CPP_OR_EQ:
7130 case CPP_XOR:
7131 case CPP_OR:
7132 case CPP_OR_OR:
7133 case CPP_EOF:
7134 return false;
7136 /* '[' may start a primary-expression in obj-c++. */
7137 case CPP_OPEN_SQUARE:
7138 return c_dialect_objc ();
7140 default:
7141 return true;
7145 /* Parse a cast-expression.
7147 cast-expression:
7148 unary-expression
7149 ( type-id ) cast-expression
7151 ADDRESS_P is true iff the unary-expression is appearing as the
7152 operand of the `&' operator. CAST_P is true if this expression is
7153 the target of a cast.
7155 Returns a representation of the expression. */
7157 static tree
7158 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7159 cp_id_kind * pidk)
7161 /* If it's a `(', then we might be looking at a cast. */
7162 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7164 tree type = NULL_TREE;
7165 tree expr = NULL_TREE;
7166 bool compound_literal_p;
7167 const char *saved_message;
7169 /* There's no way to know yet whether or not this is a cast.
7170 For example, `(int (3))' is a unary-expression, while `(int)
7171 3' is a cast. So, we resort to parsing tentatively. */
7172 cp_parser_parse_tentatively (parser);
7173 /* Types may not be defined in a cast. */
7174 saved_message = parser->type_definition_forbidden_message;
7175 parser->type_definition_forbidden_message
7176 = G_("types may not be defined in casts");
7177 /* Consume the `('. */
7178 cp_lexer_consume_token (parser->lexer);
7179 /* A very tricky bit is that `(struct S) { 3 }' is a
7180 compound-literal (which we permit in C++ as an extension).
7181 But, that construct is not a cast-expression -- it is a
7182 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7183 is legal; if the compound-literal were a cast-expression,
7184 you'd need an extra set of parentheses.) But, if we parse
7185 the type-id, and it happens to be a class-specifier, then we
7186 will commit to the parse at that point, because we cannot
7187 undo the action that is done when creating a new class. So,
7188 then we cannot back up and do a postfix-expression.
7190 Therefore, we scan ahead to the closing `)', and check to see
7191 if the token after the `)' is a `{'. If so, we are not
7192 looking at a cast-expression.
7194 Save tokens so that we can put them back. */
7195 cp_lexer_save_tokens (parser->lexer);
7196 /* Skip tokens until the next token is a closing parenthesis.
7197 If we find the closing `)', and the next token is a `{', then
7198 we are looking at a compound-literal. */
7199 compound_literal_p
7200 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7201 /*consume_paren=*/true)
7202 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7203 /* Roll back the tokens we skipped. */
7204 cp_lexer_rollback_tokens (parser->lexer);
7205 /* If we were looking at a compound-literal, simulate an error
7206 so that the call to cp_parser_parse_definitely below will
7207 fail. */
7208 if (compound_literal_p)
7209 cp_parser_simulate_error (parser);
7210 else
7212 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7213 parser->in_type_id_in_expr_p = true;
7214 /* Look for the type-id. */
7215 type = cp_parser_type_id (parser);
7216 /* Look for the closing `)'. */
7217 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7218 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7221 /* Restore the saved message. */
7222 parser->type_definition_forbidden_message = saved_message;
7224 /* At this point this can only be either a cast or a
7225 parenthesized ctor such as `(T ())' that looks like a cast to
7226 function returning T. */
7227 if (!cp_parser_error_occurred (parser)
7228 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7229 (parser->lexer)))
7231 cp_parser_parse_definitely (parser);
7232 expr = cp_parser_cast_expression (parser,
7233 /*address_p=*/false,
7234 /*cast_p=*/true, pidk);
7236 /* Warn about old-style casts, if so requested. */
7237 if (warn_old_style_cast
7238 && !in_system_header
7239 && !VOID_TYPE_P (type)
7240 && current_lang_name != lang_name_c)
7241 warning (OPT_Wold_style_cast, "use of old-style cast");
7243 /* Only type conversions to integral or enumeration types
7244 can be used in constant-expressions. */
7245 if (!cast_valid_in_integral_constant_expression_p (type)
7246 && cp_parser_non_integral_constant_expression (parser,
7247 NIC_CAST))
7248 return error_mark_node;
7250 /* Perform the cast. */
7251 expr = build_c_cast (input_location, type, expr);
7252 return expr;
7254 else
7255 cp_parser_abort_tentative_parse (parser);
7258 /* If we get here, then it's not a cast, so it must be a
7259 unary-expression. */
7260 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7263 /* Parse a binary expression of the general form:
7265 pm-expression:
7266 cast-expression
7267 pm-expression .* cast-expression
7268 pm-expression ->* cast-expression
7270 multiplicative-expression:
7271 pm-expression
7272 multiplicative-expression * pm-expression
7273 multiplicative-expression / pm-expression
7274 multiplicative-expression % pm-expression
7276 additive-expression:
7277 multiplicative-expression
7278 additive-expression + multiplicative-expression
7279 additive-expression - multiplicative-expression
7281 shift-expression:
7282 additive-expression
7283 shift-expression << additive-expression
7284 shift-expression >> additive-expression
7286 relational-expression:
7287 shift-expression
7288 relational-expression < shift-expression
7289 relational-expression > shift-expression
7290 relational-expression <= shift-expression
7291 relational-expression >= shift-expression
7293 GNU Extension:
7295 relational-expression:
7296 relational-expression <? shift-expression
7297 relational-expression >? shift-expression
7299 equality-expression:
7300 relational-expression
7301 equality-expression == relational-expression
7302 equality-expression != relational-expression
7304 and-expression:
7305 equality-expression
7306 and-expression & equality-expression
7308 exclusive-or-expression:
7309 and-expression
7310 exclusive-or-expression ^ and-expression
7312 inclusive-or-expression:
7313 exclusive-or-expression
7314 inclusive-or-expression | exclusive-or-expression
7316 logical-and-expression:
7317 inclusive-or-expression
7318 logical-and-expression && inclusive-or-expression
7320 logical-or-expression:
7321 logical-and-expression
7322 logical-or-expression || logical-and-expression
7324 All these are implemented with a single function like:
7326 binary-expression:
7327 simple-cast-expression
7328 binary-expression <token> binary-expression
7330 CAST_P is true if this expression is the target of a cast.
7332 The binops_by_token map is used to get the tree codes for each <token> type.
7333 binary-expressions are associated according to a precedence table. */
7335 #define TOKEN_PRECEDENCE(token) \
7336 (((token->type == CPP_GREATER \
7337 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7338 && !parser->greater_than_is_operator_p) \
7339 ? PREC_NOT_OPERATOR \
7340 : binops_by_token[token->type].prec)
7342 static tree
7343 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7344 bool no_toplevel_fold_p,
7345 enum cp_parser_prec prec,
7346 cp_id_kind * pidk)
7348 cp_parser_expression_stack stack;
7349 cp_parser_expression_stack_entry *sp = &stack[0];
7350 cp_parser_expression_stack_entry current;
7351 tree rhs;
7352 cp_token *token;
7353 enum tree_code rhs_type;
7354 enum cp_parser_prec new_prec, lookahead_prec;
7355 tree overload;
7357 /* Parse the first expression. */
7358 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7359 cast_p, pidk);
7360 current.lhs_type = ERROR_MARK;
7361 current.prec = prec;
7363 if (cp_parser_error_occurred (parser))
7364 return error_mark_node;
7366 for (;;)
7368 /* Get an operator token. */
7369 token = cp_lexer_peek_token (parser->lexer);
7371 if (warn_cxx0x_compat
7372 && token->type == CPP_RSHIFT
7373 && !parser->greater_than_is_operator_p)
7375 if (warning_at (token->location, OPT_Wc__0x_compat,
7376 "%<>>%> operator is treated"
7377 " as two right angle brackets in C++11"))
7378 inform (token->location,
7379 "suggest parentheses around %<>>%> expression");
7382 new_prec = TOKEN_PRECEDENCE (token);
7384 /* Popping an entry off the stack means we completed a subexpression:
7385 - either we found a token which is not an operator (`>' where it is not
7386 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7387 will happen repeatedly;
7388 - or, we found an operator which has lower priority. This is the case
7389 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7390 parsing `3 * 4'. */
7391 if (new_prec <= current.prec)
7393 if (sp == stack)
7394 break;
7395 else
7396 goto pop;
7399 get_rhs:
7400 current.tree_type = binops_by_token[token->type].tree_type;
7401 current.loc = token->location;
7403 /* We used the operator token. */
7404 cp_lexer_consume_token (parser->lexer);
7406 /* For "false && x" or "true || x", x will never be executed;
7407 disable warnings while evaluating it. */
7408 if (current.tree_type == TRUTH_ANDIF_EXPR)
7409 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7410 else if (current.tree_type == TRUTH_ORIF_EXPR)
7411 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7413 /* Extract another operand. It may be the RHS of this expression
7414 or the LHS of a new, higher priority expression. */
7415 rhs = cp_parser_simple_cast_expression (parser);
7416 rhs_type = ERROR_MARK;
7418 /* Get another operator token. Look up its precedence to avoid
7419 building a useless (immediately popped) stack entry for common
7420 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7421 token = cp_lexer_peek_token (parser->lexer);
7422 lookahead_prec = TOKEN_PRECEDENCE (token);
7423 if (lookahead_prec > new_prec)
7425 /* ... and prepare to parse the RHS of the new, higher priority
7426 expression. Since precedence levels on the stack are
7427 monotonically increasing, we do not have to care about
7428 stack overflows. */
7429 *sp = current;
7430 ++sp;
7431 current.lhs = rhs;
7432 current.lhs_type = rhs_type;
7433 current.prec = new_prec;
7434 new_prec = lookahead_prec;
7435 goto get_rhs;
7437 pop:
7438 lookahead_prec = new_prec;
7439 /* If the stack is not empty, we have parsed into LHS the right side
7440 (`4' in the example above) of an expression we had suspended.
7441 We can use the information on the stack to recover the LHS (`3')
7442 from the stack together with the tree code (`MULT_EXPR'), and
7443 the precedence of the higher level subexpression
7444 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7445 which will be used to actually build the additive expression. */
7446 rhs = current.lhs;
7447 rhs_type = current.lhs_type;
7448 --sp;
7449 current = *sp;
7452 /* Undo the disabling of warnings done above. */
7453 if (current.tree_type == TRUTH_ANDIF_EXPR)
7454 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7455 else if (current.tree_type == TRUTH_ORIF_EXPR)
7456 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7458 overload = NULL;
7459 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7460 ERROR_MARK for everything that is not a binary expression.
7461 This makes warn_about_parentheses miss some warnings that
7462 involve unary operators. For unary expressions we should
7463 pass the correct tree_code unless the unary expression was
7464 surrounded by parentheses.
7466 if (no_toplevel_fold_p
7467 && lookahead_prec <= current.prec
7468 && sp == stack
7469 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7470 current.lhs = build2 (current.tree_type, boolean_type_node,
7471 current.lhs, rhs);
7472 else
7473 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7474 current.lhs, current.lhs_type,
7475 rhs, rhs_type, &overload,
7476 tf_warning_or_error);
7477 current.lhs_type = current.tree_type;
7478 if (EXPR_P (current.lhs))
7479 SET_EXPR_LOCATION (current.lhs, current.loc);
7481 /* If the binary operator required the use of an overloaded operator,
7482 then this expression cannot be an integral constant-expression.
7483 An overloaded operator can be used even if both operands are
7484 otherwise permissible in an integral constant-expression if at
7485 least one of the operands is of enumeration type. */
7487 if (overload
7488 && cp_parser_non_integral_constant_expression (parser,
7489 NIC_OVERLOADED))
7490 return error_mark_node;
7493 return current.lhs;
7497 /* Parse the `? expression : assignment-expression' part of a
7498 conditional-expression. The LOGICAL_OR_EXPR is the
7499 logical-or-expression that started the conditional-expression.
7500 Returns a representation of the entire conditional-expression.
7502 This routine is used by cp_parser_assignment_expression.
7504 ? expression : assignment-expression
7506 GNU Extensions:
7508 ? : assignment-expression */
7510 static tree
7511 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7513 tree expr;
7514 tree assignment_expr;
7515 struct cp_token *token;
7516 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7518 /* Consume the `?' token. */
7519 cp_lexer_consume_token (parser->lexer);
7520 token = cp_lexer_peek_token (parser->lexer);
7521 if (cp_parser_allow_gnu_extensions_p (parser)
7522 && token->type == CPP_COLON)
7524 pedwarn (token->location, OPT_Wpedantic,
7525 "ISO C++ does not allow ?: with omitted middle operand");
7526 /* Implicit true clause. */
7527 expr = NULL_TREE;
7528 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7529 warn_for_omitted_condop (token->location, logical_or_expr);
7531 else
7533 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7534 parser->colon_corrects_to_scope_p = false;
7535 /* Parse the expression. */
7536 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7537 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7538 c_inhibit_evaluation_warnings +=
7539 ((logical_or_expr == truthvalue_true_node)
7540 - (logical_or_expr == truthvalue_false_node));
7541 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7544 /* The next token should be a `:'. */
7545 cp_parser_require (parser, CPP_COLON, RT_COLON);
7546 /* Parse the assignment-expression. */
7547 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7548 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7550 /* Build the conditional-expression. */
7551 return build_x_conditional_expr (loc, logical_or_expr,
7552 expr,
7553 assignment_expr,
7554 tf_warning_or_error);
7557 /* Parse an assignment-expression.
7559 assignment-expression:
7560 conditional-expression
7561 logical-or-expression assignment-operator assignment_expression
7562 throw-expression
7564 CAST_P is true if this expression is the target of a cast.
7566 Returns a representation for the expression. */
7568 static tree
7569 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7570 cp_id_kind * pidk)
7572 tree expr;
7574 /* If the next token is the `throw' keyword, then we're looking at
7575 a throw-expression. */
7576 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7577 expr = cp_parser_throw_expression (parser);
7578 /* Otherwise, it must be that we are looking at a
7579 logical-or-expression. */
7580 else
7582 /* Parse the binary expressions (logical-or-expression). */
7583 expr = cp_parser_binary_expression (parser, cast_p, false,
7584 PREC_NOT_OPERATOR, pidk);
7585 /* If the next token is a `?' then we're actually looking at a
7586 conditional-expression. */
7587 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7588 return cp_parser_question_colon_clause (parser, expr);
7589 else
7591 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7593 /* If it's an assignment-operator, we're using the second
7594 production. */
7595 enum tree_code assignment_operator
7596 = cp_parser_assignment_operator_opt (parser);
7597 if (assignment_operator != ERROR_MARK)
7599 bool non_constant_p;
7600 location_t saved_input_location;
7602 /* Parse the right-hand side of the assignment. */
7603 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7605 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7606 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7608 /* An assignment may not appear in a
7609 constant-expression. */
7610 if (cp_parser_non_integral_constant_expression (parser,
7611 NIC_ASSIGNMENT))
7612 return error_mark_node;
7613 /* Build the assignment expression. Its default
7614 location is the location of the '=' token. */
7615 saved_input_location = input_location;
7616 input_location = loc;
7617 expr = build_x_modify_expr (loc, expr,
7618 assignment_operator,
7619 rhs,
7620 tf_warning_or_error);
7621 input_location = saved_input_location;
7626 return expr;
7629 /* Parse an (optional) assignment-operator.
7631 assignment-operator: one of
7632 = *= /= %= += -= >>= <<= &= ^= |=
7634 GNU Extension:
7636 assignment-operator: one of
7637 <?= >?=
7639 If the next token is an assignment operator, the corresponding tree
7640 code is returned, and the token is consumed. For example, for
7641 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7642 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7643 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7644 operator, ERROR_MARK is returned. */
7646 static enum tree_code
7647 cp_parser_assignment_operator_opt (cp_parser* parser)
7649 enum tree_code op;
7650 cp_token *token;
7652 /* Peek at the next token. */
7653 token = cp_lexer_peek_token (parser->lexer);
7655 switch (token->type)
7657 case CPP_EQ:
7658 op = NOP_EXPR;
7659 break;
7661 case CPP_MULT_EQ:
7662 op = MULT_EXPR;
7663 break;
7665 case CPP_DIV_EQ:
7666 op = TRUNC_DIV_EXPR;
7667 break;
7669 case CPP_MOD_EQ:
7670 op = TRUNC_MOD_EXPR;
7671 break;
7673 case CPP_PLUS_EQ:
7674 op = PLUS_EXPR;
7675 break;
7677 case CPP_MINUS_EQ:
7678 op = MINUS_EXPR;
7679 break;
7681 case CPP_RSHIFT_EQ:
7682 op = RSHIFT_EXPR;
7683 break;
7685 case CPP_LSHIFT_EQ:
7686 op = LSHIFT_EXPR;
7687 break;
7689 case CPP_AND_EQ:
7690 op = BIT_AND_EXPR;
7691 break;
7693 case CPP_XOR_EQ:
7694 op = BIT_XOR_EXPR;
7695 break;
7697 case CPP_OR_EQ:
7698 op = BIT_IOR_EXPR;
7699 break;
7701 default:
7702 /* Nothing else is an assignment operator. */
7703 op = ERROR_MARK;
7706 /* If it was an assignment operator, consume it. */
7707 if (op != ERROR_MARK)
7708 cp_lexer_consume_token (parser->lexer);
7710 return op;
7713 /* Parse an expression.
7715 expression:
7716 assignment-expression
7717 expression , assignment-expression
7719 CAST_P is true if this expression is the target of a cast.
7721 Returns a representation of the expression. */
7723 static tree
7724 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7726 tree expression = NULL_TREE;
7727 location_t loc = UNKNOWN_LOCATION;
7729 while (true)
7731 tree assignment_expression;
7733 /* Parse the next assignment-expression. */
7734 assignment_expression
7735 = cp_parser_assignment_expression (parser, cast_p, pidk);
7736 /* If this is the first assignment-expression, we can just
7737 save it away. */
7738 if (!expression)
7739 expression = assignment_expression;
7740 else
7741 expression = build_x_compound_expr (loc, expression,
7742 assignment_expression,
7743 tf_warning_or_error);
7744 /* If the next token is not a comma, then we are done with the
7745 expression. */
7746 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7747 break;
7748 /* Consume the `,'. */
7749 loc = cp_lexer_peek_token (parser->lexer)->location;
7750 cp_lexer_consume_token (parser->lexer);
7751 /* A comma operator cannot appear in a constant-expression. */
7752 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7753 expression = error_mark_node;
7756 return expression;
7759 /* Parse a constant-expression.
7761 constant-expression:
7762 conditional-expression
7764 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7765 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7766 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7767 is false, NON_CONSTANT_P should be NULL. */
7769 static tree
7770 cp_parser_constant_expression (cp_parser* parser,
7771 bool allow_non_constant_p,
7772 bool *non_constant_p)
7774 bool saved_integral_constant_expression_p;
7775 bool saved_allow_non_integral_constant_expression_p;
7776 bool saved_non_integral_constant_expression_p;
7777 tree expression;
7779 /* It might seem that we could simply parse the
7780 conditional-expression, and then check to see if it were
7781 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7782 one that the compiler can figure out is constant, possibly after
7783 doing some simplifications or optimizations. The standard has a
7784 precise definition of constant-expression, and we must honor
7785 that, even though it is somewhat more restrictive.
7787 For example:
7789 int i[(2, 3)];
7791 is not a legal declaration, because `(2, 3)' is not a
7792 constant-expression. The `,' operator is forbidden in a
7793 constant-expression. However, GCC's constant-folding machinery
7794 will fold this operation to an INTEGER_CST for `3'. */
7796 /* Save the old settings. */
7797 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7798 saved_allow_non_integral_constant_expression_p
7799 = parser->allow_non_integral_constant_expression_p;
7800 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7801 /* We are now parsing a constant-expression. */
7802 parser->integral_constant_expression_p = true;
7803 parser->allow_non_integral_constant_expression_p
7804 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7805 parser->non_integral_constant_expression_p = false;
7806 /* Although the grammar says "conditional-expression", we parse an
7807 "assignment-expression", which also permits "throw-expression"
7808 and the use of assignment operators. In the case that
7809 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7810 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7811 actually essential that we look for an assignment-expression.
7812 For example, cp_parser_initializer_clauses uses this function to
7813 determine whether a particular assignment-expression is in fact
7814 constant. */
7815 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7816 /* Restore the old settings. */
7817 parser->integral_constant_expression_p
7818 = saved_integral_constant_expression_p;
7819 parser->allow_non_integral_constant_expression_p
7820 = saved_allow_non_integral_constant_expression_p;
7821 if (cxx_dialect >= cxx0x)
7823 /* Require an rvalue constant expression here; that's what our
7824 callers expect. Reference constant expressions are handled
7825 separately in e.g. cp_parser_template_argument. */
7826 bool is_const = potential_rvalue_constant_expression (expression);
7827 parser->non_integral_constant_expression_p = !is_const;
7828 if (!is_const && !allow_non_constant_p)
7829 require_potential_rvalue_constant_expression (expression);
7831 if (allow_non_constant_p)
7832 *non_constant_p = parser->non_integral_constant_expression_p;
7833 parser->non_integral_constant_expression_p
7834 = saved_non_integral_constant_expression_p;
7836 return expression;
7839 /* Parse __builtin_offsetof.
7841 offsetof-expression:
7842 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7844 offsetof-member-designator:
7845 id-expression
7846 | offsetof-member-designator "." id-expression
7847 | offsetof-member-designator "[" expression "]"
7848 | offsetof-member-designator "->" id-expression */
7850 static tree
7851 cp_parser_builtin_offsetof (cp_parser *parser)
7853 int save_ice_p, save_non_ice_p;
7854 tree type, expr;
7855 cp_id_kind dummy;
7856 cp_token *token;
7858 /* We're about to accept non-integral-constant things, but will
7859 definitely yield an integral constant expression. Save and
7860 restore these values around our local parsing. */
7861 save_ice_p = parser->integral_constant_expression_p;
7862 save_non_ice_p = parser->non_integral_constant_expression_p;
7864 /* Consume the "__builtin_offsetof" token. */
7865 cp_lexer_consume_token (parser->lexer);
7866 /* Consume the opening `('. */
7867 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7868 /* Parse the type-id. */
7869 type = cp_parser_type_id (parser);
7870 /* Look for the `,'. */
7871 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7872 token = cp_lexer_peek_token (parser->lexer);
7874 /* Build the (type *)null that begins the traditional offsetof macro. */
7875 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7876 tf_warning_or_error);
7878 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7879 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7880 true, &dummy, token->location);
7881 while (true)
7883 token = cp_lexer_peek_token (parser->lexer);
7884 switch (token->type)
7886 case CPP_OPEN_SQUARE:
7887 /* offsetof-member-designator "[" expression "]" */
7888 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7889 break;
7891 case CPP_DEREF:
7892 /* offsetof-member-designator "->" identifier */
7893 expr = grok_array_decl (token->location, expr, integer_zero_node);
7894 /* FALLTHRU */
7896 case CPP_DOT:
7897 /* offsetof-member-designator "." identifier */
7898 cp_lexer_consume_token (parser->lexer);
7899 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7900 expr, true, &dummy,
7901 token->location);
7902 break;
7904 case CPP_CLOSE_PAREN:
7905 /* Consume the ")" token. */
7906 cp_lexer_consume_token (parser->lexer);
7907 goto success;
7909 default:
7910 /* Error. We know the following require will fail, but
7911 that gives the proper error message. */
7912 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7913 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7914 expr = error_mark_node;
7915 goto failure;
7919 success:
7920 /* If we're processing a template, we can't finish the semantics yet.
7921 Otherwise we can fold the entire expression now. */
7922 if (processing_template_decl)
7923 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7924 else
7925 expr = finish_offsetof (expr);
7927 failure:
7928 parser->integral_constant_expression_p = save_ice_p;
7929 parser->non_integral_constant_expression_p = save_non_ice_p;
7931 return expr;
7934 /* Parse a trait expression.
7936 Returns a representation of the expression, the underlying type
7937 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7939 static tree
7940 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7942 cp_trait_kind kind;
7943 tree type1, type2 = NULL_TREE;
7944 bool binary = false;
7945 cp_decl_specifier_seq decl_specs;
7947 switch (keyword)
7949 case RID_HAS_NOTHROW_ASSIGN:
7950 kind = CPTK_HAS_NOTHROW_ASSIGN;
7951 break;
7952 case RID_HAS_NOTHROW_CONSTRUCTOR:
7953 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7954 break;
7955 case RID_HAS_NOTHROW_COPY:
7956 kind = CPTK_HAS_NOTHROW_COPY;
7957 break;
7958 case RID_HAS_TRIVIAL_ASSIGN:
7959 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7960 break;
7961 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7962 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7963 break;
7964 case RID_HAS_TRIVIAL_COPY:
7965 kind = CPTK_HAS_TRIVIAL_COPY;
7966 break;
7967 case RID_HAS_TRIVIAL_DESTRUCTOR:
7968 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7969 break;
7970 case RID_HAS_VIRTUAL_DESTRUCTOR:
7971 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7972 break;
7973 case RID_IS_ABSTRACT:
7974 kind = CPTK_IS_ABSTRACT;
7975 break;
7976 case RID_IS_BASE_OF:
7977 kind = CPTK_IS_BASE_OF;
7978 binary = true;
7979 break;
7980 case RID_IS_CLASS:
7981 kind = CPTK_IS_CLASS;
7982 break;
7983 case RID_IS_CONVERTIBLE_TO:
7984 kind = CPTK_IS_CONVERTIBLE_TO;
7985 binary = true;
7986 break;
7987 case RID_IS_EMPTY:
7988 kind = CPTK_IS_EMPTY;
7989 break;
7990 case RID_IS_ENUM:
7991 kind = CPTK_IS_ENUM;
7992 break;
7993 case RID_IS_FINAL:
7994 kind = CPTK_IS_FINAL;
7995 break;
7996 case RID_IS_LITERAL_TYPE:
7997 kind = CPTK_IS_LITERAL_TYPE;
7998 break;
7999 case RID_IS_POD:
8000 kind = CPTK_IS_POD;
8001 break;
8002 case RID_IS_POLYMORPHIC:
8003 kind = CPTK_IS_POLYMORPHIC;
8004 break;
8005 case RID_IS_STD_LAYOUT:
8006 kind = CPTK_IS_STD_LAYOUT;
8007 break;
8008 case RID_IS_TRIVIAL:
8009 kind = CPTK_IS_TRIVIAL;
8010 break;
8011 case RID_IS_UNION:
8012 kind = CPTK_IS_UNION;
8013 break;
8014 case RID_UNDERLYING_TYPE:
8015 kind = CPTK_UNDERLYING_TYPE;
8016 break;
8017 case RID_BASES:
8018 kind = CPTK_BASES;
8019 break;
8020 case RID_DIRECT_BASES:
8021 kind = CPTK_DIRECT_BASES;
8022 break;
8023 default:
8024 gcc_unreachable ();
8027 /* Consume the token. */
8028 cp_lexer_consume_token (parser->lexer);
8030 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8032 type1 = cp_parser_type_id (parser);
8034 if (type1 == error_mark_node)
8035 return error_mark_node;
8037 /* Build a trivial decl-specifier-seq. */
8038 clear_decl_specs (&decl_specs);
8039 decl_specs.type = type1;
8041 /* Call grokdeclarator to figure out what type this is. */
8042 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8043 /*initialized=*/0, /*attrlist=*/NULL);
8045 if (binary)
8047 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8049 type2 = cp_parser_type_id (parser);
8051 if (type2 == error_mark_node)
8052 return error_mark_node;
8054 /* Build a trivial decl-specifier-seq. */
8055 clear_decl_specs (&decl_specs);
8056 decl_specs.type = type2;
8058 /* Call grokdeclarator to figure out what type this is. */
8059 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8060 /*initialized=*/0, /*attrlist=*/NULL);
8063 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8065 /* Complete the trait expression, which may mean either processing
8066 the trait expr now or saving it for template instantiation. */
8067 switch(kind)
8069 case CPTK_UNDERLYING_TYPE:
8070 return finish_underlying_type (type1);
8071 case CPTK_BASES:
8072 return finish_bases (type1, false);
8073 case CPTK_DIRECT_BASES:
8074 return finish_bases (type1, true);
8075 default:
8076 return finish_trait_expr (kind, type1, type2);
8080 /* Lambdas that appear in variable initializer or default argument scope
8081 get that in their mangling, so we need to record it. We might as well
8082 use the count for function and namespace scopes as well. */
8083 static GTY(()) tree lambda_scope;
8084 static GTY(()) int lambda_count;
8085 typedef struct GTY(()) tree_int
8087 tree t;
8088 int i;
8089 } tree_int;
8090 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8092 static void
8093 start_lambda_scope (tree decl)
8095 tree_int ti;
8096 gcc_assert (decl);
8097 /* Once we're inside a function, we ignore other scopes and just push
8098 the function again so that popping works properly. */
8099 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8100 decl = current_function_decl;
8101 ti.t = lambda_scope;
8102 ti.i = lambda_count;
8103 vec_safe_push (lambda_scope_stack, ti);
8104 if (lambda_scope != decl)
8106 /* Don't reset the count if we're still in the same function. */
8107 lambda_scope = decl;
8108 lambda_count = 0;
8112 static void
8113 record_lambda_scope (tree lambda)
8115 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8116 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8119 static void
8120 finish_lambda_scope (void)
8122 tree_int *p = &lambda_scope_stack->last ();
8123 if (lambda_scope != p->t)
8125 lambda_scope = p->t;
8126 lambda_count = p->i;
8128 lambda_scope_stack->pop ();
8131 /* Parse a lambda expression.
8133 lambda-expression:
8134 lambda-introducer lambda-declarator [opt] compound-statement
8136 Returns a representation of the expression. */
8138 static tree
8139 cp_parser_lambda_expression (cp_parser* parser)
8141 tree lambda_expr = build_lambda_expr ();
8142 tree type;
8143 bool ok;
8145 LAMBDA_EXPR_LOCATION (lambda_expr)
8146 = cp_lexer_peek_token (parser->lexer)->location;
8148 if (cp_unevaluated_operand)
8149 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8150 "lambda-expression in unevaluated context");
8152 /* We may be in the middle of deferred access check. Disable
8153 it now. */
8154 push_deferring_access_checks (dk_no_deferred);
8156 cp_parser_lambda_introducer (parser, lambda_expr);
8158 type = begin_lambda_type (lambda_expr);
8159 if (type == error_mark_node)
8160 return error_mark_node;
8162 record_lambda_scope (lambda_expr);
8164 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8165 determine_visibility (TYPE_NAME (type));
8167 /* Now that we've started the type, add the capture fields for any
8168 explicit captures. */
8169 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8172 /* Inside the class, surrounding template-parameter-lists do not apply. */
8173 unsigned int saved_num_template_parameter_lists
8174 = parser->num_template_parameter_lists;
8175 unsigned char in_statement = parser->in_statement;
8176 bool in_switch_statement_p = parser->in_switch_statement_p;
8178 parser->num_template_parameter_lists = 0;
8179 parser->in_statement = 0;
8180 parser->in_switch_statement_p = false;
8182 /* By virtue of defining a local class, a lambda expression has access to
8183 the private variables of enclosing classes. */
8185 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8187 if (ok)
8188 cp_parser_lambda_body (parser, lambda_expr);
8189 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8190 cp_parser_skip_to_end_of_block_or_statement (parser);
8192 /* The capture list was built up in reverse order; fix that now. */
8194 tree newlist = NULL_TREE;
8195 tree elt, next;
8197 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8198 elt; elt = next)
8200 next = TREE_CHAIN (elt);
8201 TREE_CHAIN (elt) = newlist;
8202 newlist = elt;
8204 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8207 if (ok)
8208 maybe_add_lambda_conv_op (type);
8210 type = finish_struct (type, /*attributes=*/NULL_TREE);
8212 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8213 parser->in_statement = in_statement;
8214 parser->in_switch_statement_p = in_switch_statement_p;
8217 pop_deferring_access_checks ();
8219 /* This field is only used during parsing of the lambda. */
8220 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8222 /* This lambda shouldn't have any proxies left at this point. */
8223 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8224 /* And now that we're done, push proxies for an enclosing lambda. */
8225 insert_pending_capture_proxies ();
8227 if (ok)
8228 return build_lambda_object (lambda_expr);
8229 else
8230 return error_mark_node;
8233 /* Parse the beginning of a lambda expression.
8235 lambda-introducer:
8236 [ lambda-capture [opt] ]
8238 LAMBDA_EXPR is the current representation of the lambda expression. */
8240 static void
8241 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8243 /* Need commas after the first capture. */
8244 bool first = true;
8246 /* Eat the leading `['. */
8247 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8249 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8250 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8251 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8252 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8253 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8254 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8256 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8258 cp_lexer_consume_token (parser->lexer);
8259 first = false;
8262 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8264 cp_token* capture_token;
8265 tree capture_id;
8266 tree capture_init_expr;
8267 cp_id_kind idk = CP_ID_KIND_NONE;
8268 bool explicit_init_p = false;
8270 enum capture_kind_type
8272 BY_COPY,
8273 BY_REFERENCE
8275 enum capture_kind_type capture_kind = BY_COPY;
8277 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8279 error ("expected end of capture-list");
8280 return;
8283 if (first)
8284 first = false;
8285 else
8286 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8288 /* Possibly capture `this'. */
8289 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8291 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8292 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8293 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8294 "with by-copy capture default");
8295 cp_lexer_consume_token (parser->lexer);
8296 add_capture (lambda_expr,
8297 /*id=*/this_identifier,
8298 /*initializer=*/finish_this_expr(),
8299 /*by_reference_p=*/false,
8300 explicit_init_p);
8301 continue;
8304 /* Remember whether we want to capture as a reference or not. */
8305 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8307 capture_kind = BY_REFERENCE;
8308 cp_lexer_consume_token (parser->lexer);
8311 /* Get the identifier. */
8312 capture_token = cp_lexer_peek_token (parser->lexer);
8313 capture_id = cp_parser_identifier (parser);
8315 if (capture_id == error_mark_node)
8316 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8317 delimiters, but I modified this to stop on unnested ']' as well. It
8318 was already changed to stop on unnested '}', so the
8319 "closing_parenthesis" name is no more misleading with my change. */
8321 cp_parser_skip_to_closing_parenthesis (parser,
8322 /*recovering=*/true,
8323 /*or_comma=*/true,
8324 /*consume_paren=*/true);
8325 break;
8328 /* Find the initializer for this capture. */
8329 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8331 /* An explicit expression exists. */
8332 cp_lexer_consume_token (parser->lexer);
8333 pedwarn (input_location, OPT_Wpedantic,
8334 "ISO C++ does not allow initializers "
8335 "in lambda expression capture lists");
8336 capture_init_expr = cp_parser_assignment_expression (parser,
8337 /*cast_p=*/true,
8338 &idk);
8339 explicit_init_p = true;
8341 else
8343 const char* error_msg;
8345 /* Turn the identifier into an id-expression. */
8346 capture_init_expr
8347 = cp_parser_lookup_name
8348 (parser,
8349 capture_id,
8350 none_type,
8351 /*is_template=*/false,
8352 /*is_namespace=*/false,
8353 /*check_dependency=*/true,
8354 /*ambiguous_decls=*/NULL,
8355 capture_token->location);
8357 if (capture_init_expr == error_mark_node)
8359 unqualified_name_lookup_error (capture_id);
8360 continue;
8362 else if (DECL_P (capture_init_expr)
8363 && (TREE_CODE (capture_init_expr) != VAR_DECL
8364 && TREE_CODE (capture_init_expr) != PARM_DECL))
8366 error_at (capture_token->location,
8367 "capture of non-variable %qD ",
8368 capture_init_expr);
8369 inform (0, "%q+#D declared here", capture_init_expr);
8370 continue;
8372 if (TREE_CODE (capture_init_expr) == VAR_DECL
8373 && decl_storage_duration (capture_init_expr) != dk_auto)
8375 pedwarn (capture_token->location, 0, "capture of variable "
8376 "%qD with non-automatic storage duration",
8377 capture_init_expr);
8378 inform (0, "%q+#D declared here", capture_init_expr);
8379 continue;
8382 capture_init_expr
8383 = finish_id_expression
8384 (capture_id,
8385 capture_init_expr,
8386 parser->scope,
8387 &idk,
8388 /*integral_constant_expression_p=*/false,
8389 /*allow_non_integral_constant_expression_p=*/false,
8390 /*non_integral_constant_expression_p=*/NULL,
8391 /*template_p=*/false,
8392 /*done=*/true,
8393 /*address_p=*/false,
8394 /*template_arg_p=*/false,
8395 &error_msg,
8396 capture_token->location);
8399 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8400 && !explicit_init_p)
8402 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8403 && capture_kind == BY_COPY)
8404 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8405 "of %qD redundant with by-copy capture default",
8406 capture_id);
8407 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8408 && capture_kind == BY_REFERENCE)
8409 pedwarn (capture_token->location, 0, "explicit by-reference "
8410 "capture of %qD redundant with by-reference capture "
8411 "default", capture_id);
8414 add_capture (lambda_expr,
8415 capture_id,
8416 capture_init_expr,
8417 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8418 explicit_init_p);
8421 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8424 /* Parse the (optional) middle of a lambda expression.
8426 lambda-declarator:
8427 ( parameter-declaration-clause [opt] )
8428 attribute-specifier [opt]
8429 mutable [opt]
8430 exception-specification [opt]
8431 lambda-return-type-clause [opt]
8433 LAMBDA_EXPR is the current representation of the lambda expression. */
8435 static bool
8436 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8438 /* 5.1.1.4 of the standard says:
8439 If a lambda-expression does not include a lambda-declarator, it is as if
8440 the lambda-declarator were ().
8441 This means an empty parameter list, no attributes, and no exception
8442 specification. */
8443 tree param_list = void_list_node;
8444 tree attributes = NULL_TREE;
8445 tree exception_spec = NULL_TREE;
8446 tree t;
8448 /* The lambda-declarator is optional, but must begin with an opening
8449 parenthesis if present. */
8450 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8452 cp_lexer_consume_token (parser->lexer);
8454 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8456 /* Parse parameters. */
8457 param_list = cp_parser_parameter_declaration_clause (parser);
8459 /* Default arguments shall not be specified in the
8460 parameter-declaration-clause of a lambda-declarator. */
8461 for (t = param_list; t; t = TREE_CHAIN (t))
8462 if (TREE_PURPOSE (t))
8463 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8464 "default argument specified for lambda parameter");
8466 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8468 attributes = cp_parser_attributes_opt (parser);
8470 /* Parse optional `mutable' keyword. */
8471 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8473 cp_lexer_consume_token (parser->lexer);
8474 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8477 /* Parse optional exception specification. */
8478 exception_spec = cp_parser_exception_specification_opt (parser);
8480 /* Parse optional trailing return type. */
8481 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8483 cp_lexer_consume_token (parser->lexer);
8484 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8487 /* The function parameters must be in scope all the way until after the
8488 trailing-return-type in case of decltype. */
8489 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8490 pop_binding (DECL_NAME (t), t);
8492 leave_scope ();
8495 /* Create the function call operator.
8497 Messing with declarators like this is no uglier than building up the
8498 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8499 other code. */
8501 cp_decl_specifier_seq return_type_specs;
8502 cp_declarator* declarator;
8503 tree fco;
8504 int quals;
8505 void *p;
8507 clear_decl_specs (&return_type_specs);
8508 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8509 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8510 else
8511 /* Maybe we will deduce the return type later. */
8512 return_type_specs.type = make_auto ();
8514 p = obstack_alloc (&declarator_obstack, 0);
8516 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8517 sfk_none);
8519 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8520 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8521 declarator = make_call_declarator (declarator, param_list, quals,
8522 VIRT_SPEC_UNSPECIFIED,
8523 exception_spec,
8524 /*late_return_type=*/NULL_TREE);
8525 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8527 fco = grokmethod (&return_type_specs,
8528 declarator,
8529 attributes);
8530 if (fco != error_mark_node)
8532 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8533 DECL_ARTIFICIAL (fco) = 1;
8534 /* Give the object parameter a different name. */
8535 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8538 finish_member_declaration (fco);
8540 obstack_free (&declarator_obstack, p);
8542 return (fco != error_mark_node);
8546 /* Parse the body of a lambda expression, which is simply
8548 compound-statement
8550 but which requires special handling.
8551 LAMBDA_EXPR is the current representation of the lambda expression. */
8553 static void
8554 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8556 bool nested = (current_function_decl != NULL_TREE);
8557 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8558 if (nested)
8559 push_function_context ();
8560 else
8561 /* Still increment function_depth so that we don't GC in the
8562 middle of an expression. */
8563 ++function_depth;
8564 /* Clear this in case we're in the middle of a default argument. */
8565 parser->local_variables_forbidden_p = false;
8567 /* Finish the function call operator
8568 - class_specifier
8569 + late_parsing_for_member
8570 + function_definition_after_declarator
8571 + ctor_initializer_opt_and_function_body */
8573 tree fco = lambda_function (lambda_expr);
8574 tree body;
8575 bool done = false;
8576 tree compound_stmt;
8577 tree cap;
8579 /* Let the front end know that we are going to be defining this
8580 function. */
8581 start_preparsed_function (fco,
8582 NULL_TREE,
8583 SF_PRE_PARSED | SF_INCLASS_INLINE);
8585 start_lambda_scope (fco);
8586 body = begin_function_body ();
8588 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8589 goto out;
8591 /* Push the proxies for any explicit captures. */
8592 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8593 cap = TREE_CHAIN (cap))
8594 build_capture_proxy (TREE_PURPOSE (cap));
8596 compound_stmt = begin_compound_stmt (0);
8598 /* 5.1.1.4 of the standard says:
8599 If a lambda-expression does not include a trailing-return-type, it
8600 is as if the trailing-return-type denotes the following type:
8601 * if the compound-statement is of the form
8602 { return attribute-specifier [opt] expression ; }
8603 the type of the returned expression after lvalue-to-rvalue
8604 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8605 (_conv.array_ 4.2), and function-to-pointer conversion
8606 (_conv.func_ 4.3);
8607 * otherwise, void. */
8609 /* In a lambda that has neither a lambda-return-type-clause
8610 nor a deducible form, errors should be reported for return statements
8611 in the body. Since we used void as the placeholder return type, parsing
8612 the body as usual will give such desired behavior. */
8613 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8614 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8615 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8617 tree expr = NULL_TREE;
8618 cp_id_kind idk = CP_ID_KIND_NONE;
8620 /* Parse tentatively in case there's more after the initial return
8621 statement. */
8622 cp_parser_parse_tentatively (parser);
8624 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8626 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8628 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8629 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8631 if (cp_parser_parse_definitely (parser))
8633 if (!processing_template_decl)
8634 apply_deduced_return_type (fco, lambda_return_type (expr));
8636 /* Will get error here if type not deduced yet. */
8637 finish_return_stmt (expr);
8639 done = true;
8643 if (!done)
8645 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8646 cp_parser_label_declaration (parser);
8647 cp_parser_statement_seq_opt (parser, NULL_TREE);
8648 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8651 finish_compound_stmt (compound_stmt);
8653 out:
8654 finish_function_body (body);
8655 finish_lambda_scope ();
8657 /* Finish the function and generate code for it if necessary. */
8658 expand_or_defer_fn (finish_function (/*inline*/2));
8661 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8662 if (nested)
8663 pop_function_context();
8664 else
8665 --function_depth;
8668 /* Statements [gram.stmt.stmt] */
8670 /* Parse a statement.
8672 statement:
8673 labeled-statement
8674 expression-statement
8675 compound-statement
8676 selection-statement
8677 iteration-statement
8678 jump-statement
8679 declaration-statement
8680 try-block
8682 C++11:
8684 statement:
8685 labeled-statement
8686 attribute-specifier-seq (opt) expression-statement
8687 attribute-specifier-seq (opt) compound-statement
8688 attribute-specifier-seq (opt) selection-statement
8689 attribute-specifier-seq (opt) iteration-statement
8690 attribute-specifier-seq (opt) jump-statement
8691 declaration-statement
8692 attribute-specifier-seq (opt) try-block
8694 TM Extension:
8696 statement:
8697 atomic-statement
8699 IN_COMPOUND is true when the statement is nested inside a
8700 cp_parser_compound_statement; this matters for certain pragmas.
8702 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8703 is a (possibly labeled) if statement which is not enclosed in braces
8704 and has an else clause. This is used to implement -Wparentheses. */
8706 static void
8707 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8708 bool in_compound, bool *if_p)
8710 tree statement, std_attrs = NULL_TREE;
8711 cp_token *token;
8712 location_t statement_location, attrs_location;
8714 restart:
8715 if (if_p != NULL)
8716 *if_p = false;
8717 /* There is no statement yet. */
8718 statement = NULL_TREE;
8720 cp_lexer_save_tokens (parser->lexer);
8721 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
8722 if (c_dialect_objc ())
8723 /* In obj-c++, seing '[[' might be the either the beginning of
8724 c++11 attributes, or a nested objc-message-expression. So
8725 let's parse the c++11 attributes tentatively. */
8726 cp_parser_parse_tentatively (parser);
8727 std_attrs = cp_parser_std_attribute_spec_seq (parser);
8728 if (c_dialect_objc ())
8730 if (!cp_parser_parse_definitely (parser))
8731 std_attrs = NULL_TREE;
8734 /* Peek at the next token. */
8735 token = cp_lexer_peek_token (parser->lexer);
8736 /* Remember the location of the first token in the statement. */
8737 statement_location = token->location;
8738 /* If this is a keyword, then that will often determine what kind of
8739 statement we have. */
8740 if (token->type == CPP_KEYWORD)
8742 enum rid keyword = token->keyword;
8744 switch (keyword)
8746 case RID_CASE:
8747 case RID_DEFAULT:
8748 /* Looks like a labeled-statement with a case label.
8749 Parse the label, and then use tail recursion to parse
8750 the statement. */
8751 cp_parser_label_for_labeled_statement (parser, std_attrs);
8752 goto restart;
8754 case RID_IF:
8755 case RID_SWITCH:
8756 statement = cp_parser_selection_statement (parser, if_p);
8757 break;
8759 case RID_WHILE:
8760 case RID_DO:
8761 case RID_FOR:
8762 statement = cp_parser_iteration_statement (parser);
8763 break;
8765 case RID_BREAK:
8766 case RID_CONTINUE:
8767 case RID_RETURN:
8768 case RID_GOTO:
8769 statement = cp_parser_jump_statement (parser);
8770 break;
8772 /* Objective-C++ exception-handling constructs. */
8773 case RID_AT_TRY:
8774 case RID_AT_CATCH:
8775 case RID_AT_FINALLY:
8776 case RID_AT_SYNCHRONIZED:
8777 case RID_AT_THROW:
8778 statement = cp_parser_objc_statement (parser);
8779 break;
8781 case RID_TRY:
8782 statement = cp_parser_try_block (parser);
8783 break;
8785 case RID_NAMESPACE:
8786 /* This must be a namespace alias definition. */
8787 cp_parser_declaration_statement (parser);
8788 return;
8790 case RID_TRANSACTION_ATOMIC:
8791 case RID_TRANSACTION_RELAXED:
8792 statement = cp_parser_transaction (parser, keyword);
8793 break;
8794 case RID_TRANSACTION_CANCEL:
8795 statement = cp_parser_transaction_cancel (parser);
8796 break;
8798 default:
8799 /* It might be a keyword like `int' that can start a
8800 declaration-statement. */
8801 break;
8804 else if (token->type == CPP_NAME)
8806 /* If the next token is a `:', then we are looking at a
8807 labeled-statement. */
8808 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8809 if (token->type == CPP_COLON)
8811 /* Looks like a labeled-statement with an ordinary label.
8812 Parse the label, and then use tail recursion to parse
8813 the statement. */
8815 cp_parser_label_for_labeled_statement (parser, std_attrs);
8816 goto restart;
8819 /* Anything that starts with a `{' must be a compound-statement. */
8820 else if (token->type == CPP_OPEN_BRACE)
8821 statement = cp_parser_compound_statement (parser, NULL, false, false);
8822 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8823 a statement all its own. */
8824 else if (token->type == CPP_PRAGMA)
8826 /* Only certain OpenMP pragmas are attached to statements, and thus
8827 are considered statements themselves. All others are not. In
8828 the context of a compound, accept the pragma as a "statement" and
8829 return so that we can check for a close brace. Otherwise we
8830 require a real statement and must go back and read one. */
8831 if (in_compound)
8832 cp_parser_pragma (parser, pragma_compound);
8833 else if (!cp_parser_pragma (parser, pragma_stmt))
8834 goto restart;
8835 return;
8837 else if (token->type == CPP_EOF)
8839 cp_parser_error (parser, "expected statement");
8840 return;
8843 /* Everything else must be a declaration-statement or an
8844 expression-statement. Try for the declaration-statement
8845 first, unless we are looking at a `;', in which case we know that
8846 we have an expression-statement. */
8847 if (!statement)
8849 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8851 if (std_attrs != NULL_TREE)
8853 /* Attributes should be parsed as part of the the
8854 declaration, so let's un-parse them. */
8855 cp_lexer_rollback_tokens (parser->lexer);
8856 std_attrs = NULL_TREE;
8859 cp_parser_parse_tentatively (parser);
8860 /* Try to parse the declaration-statement. */
8861 cp_parser_declaration_statement (parser);
8862 /* If that worked, we're done. */
8863 if (cp_parser_parse_definitely (parser))
8864 return;
8866 /* Look for an expression-statement instead. */
8867 statement = cp_parser_expression_statement (parser, in_statement_expr);
8870 /* Set the line number for the statement. */
8871 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8872 SET_EXPR_LOCATION (statement, statement_location);
8874 /* Note that for now, we don't do anything with c++11 statements
8875 parsed at this level. */
8876 if (std_attrs != NULL_TREE)
8877 warning_at (attrs_location,
8878 OPT_Wattributes,
8879 "attributes at the beginning of statement are ignored");
8882 /* Parse the label for a labeled-statement, i.e.
8884 identifier :
8885 case constant-expression :
8886 default :
8888 GNU Extension:
8889 case constant-expression ... constant-expression : statement
8891 When a label is parsed without errors, the label is added to the
8892 parse tree by the finish_* functions, so this function doesn't
8893 have to return the label. */
8895 static void
8896 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
8898 cp_token *token;
8899 tree label = NULL_TREE;
8900 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8902 /* The next token should be an identifier. */
8903 token = cp_lexer_peek_token (parser->lexer);
8904 if (token->type != CPP_NAME
8905 && token->type != CPP_KEYWORD)
8907 cp_parser_error (parser, "expected labeled-statement");
8908 return;
8911 parser->colon_corrects_to_scope_p = false;
8912 switch (token->keyword)
8914 case RID_CASE:
8916 tree expr, expr_hi;
8917 cp_token *ellipsis;
8919 /* Consume the `case' token. */
8920 cp_lexer_consume_token (parser->lexer);
8921 /* Parse the constant-expression. */
8922 expr = cp_parser_constant_expression (parser,
8923 /*allow_non_constant_p=*/false,
8924 NULL);
8926 ellipsis = cp_lexer_peek_token (parser->lexer);
8927 if (ellipsis->type == CPP_ELLIPSIS)
8929 /* Consume the `...' token. */
8930 cp_lexer_consume_token (parser->lexer);
8931 expr_hi =
8932 cp_parser_constant_expression (parser,
8933 /*allow_non_constant_p=*/false,
8934 NULL);
8935 /* We don't need to emit warnings here, as the common code
8936 will do this for us. */
8938 else
8939 expr_hi = NULL_TREE;
8941 if (parser->in_switch_statement_p)
8942 finish_case_label (token->location, expr, expr_hi);
8943 else
8944 error_at (token->location,
8945 "case label %qE not within a switch statement",
8946 expr);
8948 break;
8950 case RID_DEFAULT:
8951 /* Consume the `default' token. */
8952 cp_lexer_consume_token (parser->lexer);
8954 if (parser->in_switch_statement_p)
8955 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8956 else
8957 error_at (token->location, "case label not within a switch statement");
8958 break;
8960 default:
8961 /* Anything else must be an ordinary label. */
8962 label = finish_label_stmt (cp_parser_identifier (parser));
8963 break;
8966 /* Require the `:' token. */
8967 cp_parser_require (parser, CPP_COLON, RT_COLON);
8969 /* An ordinary label may optionally be followed by attributes.
8970 However, this is only permitted if the attributes are then
8971 followed by a semicolon. This is because, for backward
8972 compatibility, when parsing
8973 lab: __attribute__ ((unused)) int i;
8974 we want the attribute to attach to "i", not "lab". */
8975 if (label != NULL_TREE
8976 && cp_next_tokens_can_be_gnu_attribute_p (parser))
8978 tree attrs;
8979 cp_parser_parse_tentatively (parser);
8980 attrs = cp_parser_gnu_attributes_opt (parser);
8981 if (attrs == NULL_TREE
8982 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8983 cp_parser_abort_tentative_parse (parser);
8984 else if (!cp_parser_parse_definitely (parser))
8986 else
8987 attributes = chainon (attributes, attrs);
8990 if (attributes != NULL_TREE)
8991 cplus_decl_attributes (&label, attributes, 0);
8993 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8996 /* Parse an expression-statement.
8998 expression-statement:
8999 expression [opt] ;
9001 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9002 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9003 indicates whether this expression-statement is part of an
9004 expression statement. */
9006 static tree
9007 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9009 tree statement = NULL_TREE;
9010 cp_token *token = cp_lexer_peek_token (parser->lexer);
9012 /* If the next token is a ';', then there is no expression
9013 statement. */
9014 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9015 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9017 /* Give a helpful message for "A<T>::type t;" and the like. */
9018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9019 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9021 if (TREE_CODE (statement) == SCOPE_REF)
9022 error_at (token->location, "need %<typename%> before %qE because "
9023 "%qT is a dependent scope",
9024 statement, TREE_OPERAND (statement, 0));
9025 else if (is_overloaded_fn (statement)
9026 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9028 /* A::A a; */
9029 tree fn = get_first_fn (statement);
9030 error_at (token->location,
9031 "%<%T::%D%> names the constructor, not the type",
9032 DECL_CONTEXT (fn), DECL_NAME (fn));
9036 /* Consume the final `;'. */
9037 cp_parser_consume_semicolon_at_end_of_statement (parser);
9039 if (in_statement_expr
9040 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9041 /* This is the final expression statement of a statement
9042 expression. */
9043 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9044 else if (statement)
9045 statement = finish_expr_stmt (statement);
9046 else
9047 finish_stmt ();
9049 return statement;
9052 /* Parse a compound-statement.
9054 compound-statement:
9055 { statement-seq [opt] }
9057 GNU extension:
9059 compound-statement:
9060 { label-declaration-seq [opt] statement-seq [opt] }
9062 label-declaration-seq:
9063 label-declaration
9064 label-declaration-seq label-declaration
9066 Returns a tree representing the statement. */
9068 static tree
9069 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9070 bool in_try, bool function_body)
9072 tree compound_stmt;
9074 /* Consume the `{'. */
9075 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9076 return error_mark_node;
9077 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9078 && !function_body)
9079 pedwarn (input_location, OPT_Wpedantic,
9080 "compound-statement in constexpr function");
9081 /* Begin the compound-statement. */
9082 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9083 /* If the next keyword is `__label__' we have a label declaration. */
9084 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9085 cp_parser_label_declaration (parser);
9086 /* Parse an (optional) statement-seq. */
9087 cp_parser_statement_seq_opt (parser, in_statement_expr);
9088 /* Finish the compound-statement. */
9089 finish_compound_stmt (compound_stmt);
9090 /* Consume the `}'. */
9091 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9093 return compound_stmt;
9096 /* Parse an (optional) statement-seq.
9098 statement-seq:
9099 statement
9100 statement-seq [opt] statement */
9102 static void
9103 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9105 /* Scan statements until there aren't any more. */
9106 while (true)
9108 cp_token *token = cp_lexer_peek_token (parser->lexer);
9110 /* If we are looking at a `}', then we have run out of
9111 statements; the same is true if we have reached the end
9112 of file, or have stumbled upon a stray '@end'. */
9113 if (token->type == CPP_CLOSE_BRACE
9114 || token->type == CPP_EOF
9115 || token->type == CPP_PRAGMA_EOL
9116 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9117 break;
9119 /* If we are in a compound statement and find 'else' then
9120 something went wrong. */
9121 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9123 if (parser->in_statement & IN_IF_STMT)
9124 break;
9125 else
9127 token = cp_lexer_consume_token (parser->lexer);
9128 error_at (token->location, "%<else%> without a previous %<if%>");
9132 /* Parse the statement. */
9133 cp_parser_statement (parser, in_statement_expr, true, NULL);
9137 /* Parse a selection-statement.
9139 selection-statement:
9140 if ( condition ) statement
9141 if ( condition ) statement else statement
9142 switch ( condition ) statement
9144 Returns the new IF_STMT or SWITCH_STMT.
9146 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9147 is a (possibly labeled) if statement which is not enclosed in
9148 braces and has an else clause. This is used to implement
9149 -Wparentheses. */
9151 static tree
9152 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9154 cp_token *token;
9155 enum rid keyword;
9157 if (if_p != NULL)
9158 *if_p = false;
9160 /* Peek at the next token. */
9161 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9163 /* See what kind of keyword it is. */
9164 keyword = token->keyword;
9165 switch (keyword)
9167 case RID_IF:
9168 case RID_SWITCH:
9170 tree statement;
9171 tree condition;
9173 /* Look for the `('. */
9174 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9176 cp_parser_skip_to_end_of_statement (parser);
9177 return error_mark_node;
9180 /* Begin the selection-statement. */
9181 if (keyword == RID_IF)
9182 statement = begin_if_stmt ();
9183 else
9184 statement = begin_switch_stmt ();
9186 /* Parse the condition. */
9187 condition = cp_parser_condition (parser);
9188 /* Look for the `)'. */
9189 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9190 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9191 /*consume_paren=*/true);
9193 if (keyword == RID_IF)
9195 bool nested_if;
9196 unsigned char in_statement;
9198 /* Add the condition. */
9199 finish_if_stmt_cond (condition, statement);
9201 /* Parse the then-clause. */
9202 in_statement = parser->in_statement;
9203 parser->in_statement |= IN_IF_STMT;
9204 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9206 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9207 add_stmt (build_empty_stmt (loc));
9208 cp_lexer_consume_token (parser->lexer);
9209 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9210 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9211 "empty body in an %<if%> statement");
9212 nested_if = false;
9214 else
9215 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9216 parser->in_statement = in_statement;
9218 finish_then_clause (statement);
9220 /* If the next token is `else', parse the else-clause. */
9221 if (cp_lexer_next_token_is_keyword (parser->lexer,
9222 RID_ELSE))
9224 /* Consume the `else' keyword. */
9225 cp_lexer_consume_token (parser->lexer);
9226 begin_else_clause (statement);
9227 /* Parse the else-clause. */
9228 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9230 location_t loc;
9231 loc = cp_lexer_peek_token (parser->lexer)->location;
9232 warning_at (loc,
9233 OPT_Wempty_body, "suggest braces around "
9234 "empty body in an %<else%> statement");
9235 add_stmt (build_empty_stmt (loc));
9236 cp_lexer_consume_token (parser->lexer);
9238 else
9239 cp_parser_implicitly_scoped_statement (parser, NULL);
9241 finish_else_clause (statement);
9243 /* If we are currently parsing a then-clause, then
9244 IF_P will not be NULL. We set it to true to
9245 indicate that this if statement has an else clause.
9246 This may trigger the Wparentheses warning below
9247 when we get back up to the parent if statement. */
9248 if (if_p != NULL)
9249 *if_p = true;
9251 else
9253 /* This if statement does not have an else clause. If
9254 NESTED_IF is true, then the then-clause is an if
9255 statement which does have an else clause. We warn
9256 about the potential ambiguity. */
9257 if (nested_if)
9258 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9259 "suggest explicit braces to avoid ambiguous"
9260 " %<else%>");
9263 /* Now we're all done with the if-statement. */
9264 finish_if_stmt (statement);
9266 else
9268 bool in_switch_statement_p;
9269 unsigned char in_statement;
9271 /* Add the condition. */
9272 finish_switch_cond (condition, statement);
9274 /* Parse the body of the switch-statement. */
9275 in_switch_statement_p = parser->in_switch_statement_p;
9276 in_statement = parser->in_statement;
9277 parser->in_switch_statement_p = true;
9278 parser->in_statement |= IN_SWITCH_STMT;
9279 cp_parser_implicitly_scoped_statement (parser, NULL);
9280 parser->in_switch_statement_p = in_switch_statement_p;
9281 parser->in_statement = in_statement;
9283 /* Now we're all done with the switch-statement. */
9284 finish_switch_stmt (statement);
9287 return statement;
9289 break;
9291 default:
9292 cp_parser_error (parser, "expected selection-statement");
9293 return error_mark_node;
9297 /* Parse a condition.
9299 condition:
9300 expression
9301 type-specifier-seq declarator = initializer-clause
9302 type-specifier-seq declarator braced-init-list
9304 GNU Extension:
9306 condition:
9307 type-specifier-seq declarator asm-specification [opt]
9308 attributes [opt] = assignment-expression
9310 Returns the expression that should be tested. */
9312 static tree
9313 cp_parser_condition (cp_parser* parser)
9315 cp_decl_specifier_seq type_specifiers;
9316 const char *saved_message;
9317 int declares_class_or_enum;
9319 /* Try the declaration first. */
9320 cp_parser_parse_tentatively (parser);
9321 /* New types are not allowed in the type-specifier-seq for a
9322 condition. */
9323 saved_message = parser->type_definition_forbidden_message;
9324 parser->type_definition_forbidden_message
9325 = G_("types may not be defined in conditions");
9326 /* Parse the type-specifier-seq. */
9327 cp_parser_decl_specifier_seq (parser,
9328 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9329 &type_specifiers,
9330 &declares_class_or_enum);
9331 /* Restore the saved message. */
9332 parser->type_definition_forbidden_message = saved_message;
9333 /* If all is well, we might be looking at a declaration. */
9334 if (!cp_parser_error_occurred (parser))
9336 tree decl;
9337 tree asm_specification;
9338 tree attributes;
9339 cp_declarator *declarator;
9340 tree initializer = NULL_TREE;
9342 /* Parse the declarator. */
9343 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9344 /*ctor_dtor_or_conv_p=*/NULL,
9345 /*parenthesized_p=*/NULL,
9346 /*member_p=*/false);
9347 /* Parse the attributes. */
9348 attributes = cp_parser_attributes_opt (parser);
9349 /* Parse the asm-specification. */
9350 asm_specification = cp_parser_asm_specification_opt (parser);
9351 /* If the next token is not an `=' or '{', then we might still be
9352 looking at an expression. For example:
9354 if (A(a).x)
9356 looks like a decl-specifier-seq and a declarator -- but then
9357 there is no `=', so this is an expression. */
9358 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9359 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9360 cp_parser_simulate_error (parser);
9362 /* If we did see an `=' or '{', then we are looking at a declaration
9363 for sure. */
9364 if (cp_parser_parse_definitely (parser))
9366 tree pushed_scope;
9367 bool non_constant_p;
9368 bool flags = LOOKUP_ONLYCONVERTING;
9370 /* Create the declaration. */
9371 decl = start_decl (declarator, &type_specifiers,
9372 /*initialized_p=*/true,
9373 attributes, /*prefix_attributes=*/NULL_TREE,
9374 &pushed_scope);
9376 /* Parse the initializer. */
9377 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9379 initializer = cp_parser_braced_list (parser, &non_constant_p);
9380 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9381 flags = 0;
9383 else
9385 /* Consume the `='. */
9386 cp_parser_require (parser, CPP_EQ, RT_EQ);
9387 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9389 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9390 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9392 /* Process the initializer. */
9393 cp_finish_decl (decl,
9394 initializer, !non_constant_p,
9395 asm_specification,
9396 flags);
9398 if (pushed_scope)
9399 pop_scope (pushed_scope);
9401 return convert_from_reference (decl);
9404 /* If we didn't even get past the declarator successfully, we are
9405 definitely not looking at a declaration. */
9406 else
9407 cp_parser_abort_tentative_parse (parser);
9409 /* Otherwise, we are looking at an expression. */
9410 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9413 /* Parses a for-statement or range-for-statement until the closing ')',
9414 not included. */
9416 static tree
9417 cp_parser_for (cp_parser *parser)
9419 tree init, scope, decl;
9420 bool is_range_for;
9422 /* Begin the for-statement. */
9423 scope = begin_for_scope (&init);
9425 /* Parse the initialization. */
9426 is_range_for = cp_parser_for_init_statement (parser, &decl);
9428 if (is_range_for)
9429 return cp_parser_range_for (parser, scope, init, decl);
9430 else
9431 return cp_parser_c_for (parser, scope, init);
9434 static tree
9435 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9437 /* Normal for loop */
9438 tree condition = NULL_TREE;
9439 tree expression = NULL_TREE;
9440 tree stmt;
9442 stmt = begin_for_stmt (scope, init);
9443 /* The for-init-statement has already been parsed in
9444 cp_parser_for_init_statement, so no work is needed here. */
9445 finish_for_init_stmt (stmt);
9447 /* If there's a condition, process it. */
9448 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9449 condition = cp_parser_condition (parser);
9450 finish_for_cond (condition, stmt);
9451 /* Look for the `;'. */
9452 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9454 /* If there's an expression, process it. */
9455 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9456 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9457 finish_for_expr (expression, stmt);
9459 return stmt;
9462 /* Tries to parse a range-based for-statement:
9464 range-based-for:
9465 decl-specifier-seq declarator : expression
9467 The decl-specifier-seq declarator and the `:' are already parsed by
9468 cp_parser_for_init_statement. If processing_template_decl it returns a
9469 newly created RANGE_FOR_STMT; if not, it is converted to a
9470 regular FOR_STMT. */
9472 static tree
9473 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9475 tree stmt, range_expr;
9477 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9479 bool expr_non_constant_p;
9480 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9482 else
9483 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9485 /* If in template, STMT is converted to a normal for-statement
9486 at instantiation. If not, it is done just ahead. */
9487 if (processing_template_decl)
9489 if (check_for_bare_parameter_packs (range_expr))
9490 range_expr = error_mark_node;
9491 stmt = begin_range_for_stmt (scope, init);
9492 finish_range_for_decl (stmt, range_decl, range_expr);
9493 if (!type_dependent_expression_p (range_expr)
9494 /* do_auto_deduction doesn't mess with template init-lists. */
9495 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9496 do_range_for_auto_deduction (range_decl, range_expr);
9498 else
9500 stmt = begin_for_stmt (scope, init);
9501 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9503 return stmt;
9506 /* Subroutine of cp_convert_range_for: given the initializer expression,
9507 builds up the range temporary. */
9509 static tree
9510 build_range_temp (tree range_expr)
9512 tree range_type, range_temp;
9514 /* Find out the type deduced by the declaration
9515 `auto &&__range = range_expr'. */
9516 range_type = cp_build_reference_type (make_auto (), true);
9517 range_type = do_auto_deduction (range_type, range_expr,
9518 type_uses_auto (range_type));
9520 /* Create the __range variable. */
9521 range_temp = build_decl (input_location, VAR_DECL,
9522 get_identifier ("__for_range"), range_type);
9523 TREE_USED (range_temp) = 1;
9524 DECL_ARTIFICIAL (range_temp) = 1;
9526 return range_temp;
9529 /* Used by cp_parser_range_for in template context: we aren't going to
9530 do a full conversion yet, but we still need to resolve auto in the
9531 type of the for-range-declaration if present. This is basically
9532 a shortcut version of cp_convert_range_for. */
9534 static void
9535 do_range_for_auto_deduction (tree decl, tree range_expr)
9537 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9538 if (auto_node)
9540 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9541 range_temp = convert_from_reference (build_range_temp (range_expr));
9542 iter_type = (cp_parser_perform_range_for_lookup
9543 (range_temp, &begin_dummy, &end_dummy));
9544 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9545 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9546 tf_warning_or_error);
9547 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9548 iter_decl, auto_node);
9552 /* Converts a range-based for-statement into a normal
9553 for-statement, as per the definition.
9555 for (RANGE_DECL : RANGE_EXPR)
9556 BLOCK
9558 should be equivalent to:
9561 auto &&__range = RANGE_EXPR;
9562 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9563 __begin != __end;
9564 ++__begin)
9566 RANGE_DECL = *__begin;
9567 BLOCK
9571 If RANGE_EXPR is an array:
9572 BEGIN_EXPR = __range
9573 END_EXPR = __range + ARRAY_SIZE(__range)
9574 Else if RANGE_EXPR has a member 'begin' or 'end':
9575 BEGIN_EXPR = __range.begin()
9576 END_EXPR = __range.end()
9577 Else:
9578 BEGIN_EXPR = begin(__range)
9579 END_EXPR = end(__range);
9581 If __range has a member 'begin' but not 'end', or vice versa, we must
9582 still use the second alternative (it will surely fail, however).
9583 When calling begin()/end() in the third alternative we must use
9584 argument dependent lookup, but always considering 'std' as an associated
9585 namespace. */
9587 tree
9588 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9590 tree begin, end;
9591 tree iter_type, begin_expr, end_expr;
9592 tree condition, expression;
9594 if (range_decl == error_mark_node || range_expr == error_mark_node)
9595 /* If an error happened previously do nothing or else a lot of
9596 unhelpful errors would be issued. */
9597 begin_expr = end_expr = iter_type = error_mark_node;
9598 else
9600 tree range_temp = build_range_temp (range_expr);
9601 pushdecl (range_temp);
9602 cp_finish_decl (range_temp, range_expr,
9603 /*is_constant_init*/false, NULL_TREE,
9604 LOOKUP_ONLYCONVERTING);
9606 range_temp = convert_from_reference (range_temp);
9607 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9608 &begin_expr, &end_expr);
9611 /* The new for initialization statement. */
9612 begin = build_decl (input_location, VAR_DECL,
9613 get_identifier ("__for_begin"), iter_type);
9614 TREE_USED (begin) = 1;
9615 DECL_ARTIFICIAL (begin) = 1;
9616 pushdecl (begin);
9617 cp_finish_decl (begin, begin_expr,
9618 /*is_constant_init*/false, NULL_TREE,
9619 LOOKUP_ONLYCONVERTING);
9621 end = build_decl (input_location, VAR_DECL,
9622 get_identifier ("__for_end"), iter_type);
9623 TREE_USED (end) = 1;
9624 DECL_ARTIFICIAL (end) = 1;
9625 pushdecl (end);
9626 cp_finish_decl (end, end_expr,
9627 /*is_constant_init*/false, NULL_TREE,
9628 LOOKUP_ONLYCONVERTING);
9630 finish_for_init_stmt (statement);
9632 /* The new for condition. */
9633 condition = build_x_binary_op (input_location, NE_EXPR,
9634 begin, ERROR_MARK,
9635 end, ERROR_MARK,
9636 NULL, tf_warning_or_error);
9637 finish_for_cond (condition, statement);
9639 /* The new increment expression. */
9640 expression = finish_unary_op_expr (input_location,
9641 PREINCREMENT_EXPR, begin);
9642 finish_for_expr (expression, statement);
9644 /* The declaration is initialized with *__begin inside the loop body. */
9645 cp_finish_decl (range_decl,
9646 build_x_indirect_ref (input_location, begin, RO_NULL,
9647 tf_warning_or_error),
9648 /*is_constant_init*/false, NULL_TREE,
9649 LOOKUP_ONLYCONVERTING);
9651 return statement;
9654 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9655 We need to solve both at the same time because the method used
9656 depends on the existence of members begin or end.
9657 Returns the type deduced for the iterator expression. */
9659 static tree
9660 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9662 if (error_operand_p (range))
9664 *begin = *end = error_mark_node;
9665 return error_mark_node;
9668 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9670 error ("range-based %<for%> expression of type %qT "
9671 "has incomplete type", TREE_TYPE (range));
9672 *begin = *end = error_mark_node;
9673 return error_mark_node;
9675 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9677 /* If RANGE is an array, we will use pointer arithmetic. */
9678 *begin = range;
9679 *end = build_binary_op (input_location, PLUS_EXPR,
9680 range,
9681 array_type_nelts_top (TREE_TYPE (range)),
9683 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9685 else
9687 /* If it is not an array, we must do a bit of magic. */
9688 tree id_begin, id_end;
9689 tree member_begin, member_end;
9691 *begin = *end = error_mark_node;
9693 id_begin = get_identifier ("begin");
9694 id_end = get_identifier ("end");
9695 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9696 /*protect=*/2, /*want_type=*/false,
9697 tf_warning_or_error);
9698 member_end = lookup_member (TREE_TYPE (range), id_end,
9699 /*protect=*/2, /*want_type=*/false,
9700 tf_warning_or_error);
9702 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9704 /* Use the member functions. */
9705 if (member_begin != NULL_TREE)
9706 *begin = cp_parser_range_for_member_function (range, id_begin);
9707 else
9708 error ("range-based %<for%> expression of type %qT has an "
9709 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9711 if (member_end != NULL_TREE)
9712 *end = cp_parser_range_for_member_function (range, id_end);
9713 else
9714 error ("range-based %<for%> expression of type %qT has a "
9715 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9717 else
9719 /* Use global functions with ADL. */
9720 vec<tree, va_gc> *vec;
9721 vec = make_tree_vector ();
9723 vec_safe_push (vec, range);
9725 member_begin = perform_koenig_lookup (id_begin, vec,
9726 /*include_std=*/true,
9727 tf_warning_or_error);
9728 *begin = finish_call_expr (member_begin, &vec, false, true,
9729 tf_warning_or_error);
9730 member_end = perform_koenig_lookup (id_end, vec,
9731 /*include_std=*/true,
9732 tf_warning_or_error);
9733 *end = finish_call_expr (member_end, &vec, false, true,
9734 tf_warning_or_error);
9736 release_tree_vector (vec);
9739 /* Last common checks. */
9740 if (*begin == error_mark_node || *end == error_mark_node)
9742 /* If one of the expressions is an error do no more checks. */
9743 *begin = *end = error_mark_node;
9744 return error_mark_node;
9746 else
9748 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9749 /* The unqualified type of the __begin and __end temporaries should
9750 be the same, as required by the multiple auto declaration. */
9751 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9752 error ("inconsistent begin/end types in range-based %<for%> "
9753 "statement: %qT and %qT",
9754 TREE_TYPE (*begin), TREE_TYPE (*end));
9755 return iter_type;
9760 /* Helper function for cp_parser_perform_range_for_lookup.
9761 Builds a tree for RANGE.IDENTIFIER(). */
9763 static tree
9764 cp_parser_range_for_member_function (tree range, tree identifier)
9766 tree member, res;
9767 vec<tree, va_gc> *vec;
9769 member = finish_class_member_access_expr (range, identifier,
9770 false, tf_warning_or_error);
9771 if (member == error_mark_node)
9772 return error_mark_node;
9774 vec = make_tree_vector ();
9775 res = finish_call_expr (member, &vec,
9776 /*disallow_virtual=*/false,
9777 /*koenig_p=*/false,
9778 tf_warning_or_error);
9779 release_tree_vector (vec);
9780 return res;
9783 /* Parse an iteration-statement.
9785 iteration-statement:
9786 while ( condition ) statement
9787 do statement while ( expression ) ;
9788 for ( for-init-statement condition [opt] ; expression [opt] )
9789 statement
9791 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9793 static tree
9794 cp_parser_iteration_statement (cp_parser* parser)
9796 cp_token *token;
9797 enum rid keyword;
9798 tree statement;
9799 unsigned char in_statement;
9801 /* Peek at the next token. */
9802 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9803 if (!token)
9804 return error_mark_node;
9806 /* Remember whether or not we are already within an iteration
9807 statement. */
9808 in_statement = parser->in_statement;
9810 /* See what kind of keyword it is. */
9811 keyword = token->keyword;
9812 switch (keyword)
9814 case RID_WHILE:
9816 tree condition;
9818 /* Begin the while-statement. */
9819 statement = begin_while_stmt ();
9820 /* Look for the `('. */
9821 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9822 /* Parse the condition. */
9823 condition = cp_parser_condition (parser);
9824 finish_while_stmt_cond (condition, statement);
9825 /* Look for the `)'. */
9826 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9827 /* Parse the dependent statement. */
9828 parser->in_statement = IN_ITERATION_STMT;
9829 cp_parser_already_scoped_statement (parser);
9830 parser->in_statement = in_statement;
9831 /* We're done with the while-statement. */
9832 finish_while_stmt (statement);
9834 break;
9836 case RID_DO:
9838 tree expression;
9840 /* Begin the do-statement. */
9841 statement = begin_do_stmt ();
9842 /* Parse the body of the do-statement. */
9843 parser->in_statement = IN_ITERATION_STMT;
9844 cp_parser_implicitly_scoped_statement (parser, NULL);
9845 parser->in_statement = in_statement;
9846 finish_do_body (statement);
9847 /* Look for the `while' keyword. */
9848 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9849 /* Look for the `('. */
9850 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9851 /* Parse the expression. */
9852 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9853 /* We're done with the do-statement. */
9854 finish_do_stmt (expression, statement);
9855 /* Look for the `)'. */
9856 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9857 /* Look for the `;'. */
9858 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9860 break;
9862 case RID_FOR:
9864 /* Look for the `('. */
9865 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9867 statement = cp_parser_for (parser);
9869 /* Look for the `)'. */
9870 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9872 /* Parse the body of the for-statement. */
9873 parser->in_statement = IN_ITERATION_STMT;
9874 cp_parser_already_scoped_statement (parser);
9875 parser->in_statement = in_statement;
9877 /* We're done with the for-statement. */
9878 finish_for_stmt (statement);
9880 break;
9882 default:
9883 cp_parser_error (parser, "expected iteration-statement");
9884 statement = error_mark_node;
9885 break;
9888 return statement;
9891 /* Parse a for-init-statement or the declarator of a range-based-for.
9892 Returns true if a range-based-for declaration is seen.
9894 for-init-statement:
9895 expression-statement
9896 simple-declaration */
9898 static bool
9899 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9901 /* If the next token is a `;', then we have an empty
9902 expression-statement. Grammatically, this is also a
9903 simple-declaration, but an invalid one, because it does not
9904 declare anything. Therefore, if we did not handle this case
9905 specially, we would issue an error message about an invalid
9906 declaration. */
9907 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9909 bool is_range_for = false;
9910 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9912 parser->colon_corrects_to_scope_p = false;
9914 /* We're going to speculatively look for a declaration, falling back
9915 to an expression, if necessary. */
9916 cp_parser_parse_tentatively (parser);
9917 /* Parse the declaration. */
9918 cp_parser_simple_declaration (parser,
9919 /*function_definition_allowed_p=*/false,
9920 decl);
9921 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9922 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9924 /* It is a range-for, consume the ':' */
9925 cp_lexer_consume_token (parser->lexer);
9926 is_range_for = true;
9927 if (cxx_dialect < cxx0x)
9929 error_at (cp_lexer_peek_token (parser->lexer)->location,
9930 "range-based %<for%> loops are not allowed "
9931 "in C++98 mode");
9932 *decl = error_mark_node;
9935 else
9936 /* The ';' is not consumed yet because we told
9937 cp_parser_simple_declaration not to. */
9938 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9940 if (cp_parser_parse_definitely (parser))
9941 return is_range_for;
9942 /* If the tentative parse failed, then we shall need to look for an
9943 expression-statement. */
9945 /* If we are here, it is an expression-statement. */
9946 cp_parser_expression_statement (parser, NULL_TREE);
9947 return false;
9950 /* Parse a jump-statement.
9952 jump-statement:
9953 break ;
9954 continue ;
9955 return expression [opt] ;
9956 return braced-init-list ;
9957 goto identifier ;
9959 GNU extension:
9961 jump-statement:
9962 goto * expression ;
9964 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9966 static tree
9967 cp_parser_jump_statement (cp_parser* parser)
9969 tree statement = error_mark_node;
9970 cp_token *token;
9971 enum rid keyword;
9972 unsigned char in_statement;
9974 /* Peek at the next token. */
9975 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9976 if (!token)
9977 return error_mark_node;
9979 /* See what kind of keyword it is. */
9980 keyword = token->keyword;
9981 switch (keyword)
9983 case RID_BREAK:
9984 in_statement = parser->in_statement & ~IN_IF_STMT;
9985 switch (in_statement)
9987 case 0:
9988 error_at (token->location, "break statement not within loop or switch");
9989 break;
9990 default:
9991 gcc_assert ((in_statement & IN_SWITCH_STMT)
9992 || in_statement == IN_ITERATION_STMT);
9993 statement = finish_break_stmt ();
9994 break;
9995 case IN_OMP_BLOCK:
9996 error_at (token->location, "invalid exit from OpenMP structured block");
9997 break;
9998 case IN_OMP_FOR:
9999 error_at (token->location, "break statement used with OpenMP for loop");
10000 break;
10002 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10003 break;
10005 case RID_CONTINUE:
10006 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10008 case 0:
10009 error_at (token->location, "continue statement not within a loop");
10010 break;
10011 case IN_ITERATION_STMT:
10012 case IN_OMP_FOR:
10013 statement = finish_continue_stmt ();
10014 break;
10015 case IN_OMP_BLOCK:
10016 error_at (token->location, "invalid exit from OpenMP structured block");
10017 break;
10018 default:
10019 gcc_unreachable ();
10021 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10022 break;
10024 case RID_RETURN:
10026 tree expr;
10027 bool expr_non_constant_p;
10029 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10031 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10032 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10034 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10035 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10036 else
10037 /* If the next token is a `;', then there is no
10038 expression. */
10039 expr = NULL_TREE;
10040 /* Build the return-statement. */
10041 statement = finish_return_stmt (expr);
10042 /* Look for the final `;'. */
10043 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10045 break;
10047 case RID_GOTO:
10048 /* Create the goto-statement. */
10049 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10051 /* Issue a warning about this use of a GNU extension. */
10052 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10053 /* Consume the '*' token. */
10054 cp_lexer_consume_token (parser->lexer);
10055 /* Parse the dependent expression. */
10056 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10058 else
10059 finish_goto_stmt (cp_parser_identifier (parser));
10060 /* Look for the final `;'. */
10061 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10062 break;
10064 default:
10065 cp_parser_error (parser, "expected jump-statement");
10066 break;
10069 return statement;
10072 /* Parse a declaration-statement.
10074 declaration-statement:
10075 block-declaration */
10077 static void
10078 cp_parser_declaration_statement (cp_parser* parser)
10080 void *p;
10082 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10083 p = obstack_alloc (&declarator_obstack, 0);
10085 /* Parse the block-declaration. */
10086 cp_parser_block_declaration (parser, /*statement_p=*/true);
10088 /* Free any declarators allocated. */
10089 obstack_free (&declarator_obstack, p);
10091 /* Finish off the statement. */
10092 finish_stmt ();
10095 /* Some dependent statements (like `if (cond) statement'), are
10096 implicitly in their own scope. In other words, if the statement is
10097 a single statement (as opposed to a compound-statement), it is
10098 none-the-less treated as if it were enclosed in braces. Any
10099 declarations appearing in the dependent statement are out of scope
10100 after control passes that point. This function parses a statement,
10101 but ensures that is in its own scope, even if it is not a
10102 compound-statement.
10104 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10105 is a (possibly labeled) if statement which is not enclosed in
10106 braces and has an else clause. This is used to implement
10107 -Wparentheses.
10109 Returns the new statement. */
10111 static tree
10112 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10114 tree statement;
10116 if (if_p != NULL)
10117 *if_p = false;
10119 /* Mark if () ; with a special NOP_EXPR. */
10120 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10122 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10123 cp_lexer_consume_token (parser->lexer);
10124 statement = add_stmt (build_empty_stmt (loc));
10126 /* if a compound is opened, we simply parse the statement directly. */
10127 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10128 statement = cp_parser_compound_statement (parser, NULL, false, false);
10129 /* If the token is not a `{', then we must take special action. */
10130 else
10132 /* Create a compound-statement. */
10133 statement = begin_compound_stmt (0);
10134 /* Parse the dependent-statement. */
10135 cp_parser_statement (parser, NULL_TREE, false, if_p);
10136 /* Finish the dummy compound-statement. */
10137 finish_compound_stmt (statement);
10140 /* Return the statement. */
10141 return statement;
10144 /* For some dependent statements (like `while (cond) statement'), we
10145 have already created a scope. Therefore, even if the dependent
10146 statement is a compound-statement, we do not want to create another
10147 scope. */
10149 static void
10150 cp_parser_already_scoped_statement (cp_parser* parser)
10152 /* If the token is a `{', then we must take special action. */
10153 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10154 cp_parser_statement (parser, NULL_TREE, false, NULL);
10155 else
10157 /* Avoid calling cp_parser_compound_statement, so that we
10158 don't create a new scope. Do everything else by hand. */
10159 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10160 /* If the next keyword is `__label__' we have a label declaration. */
10161 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10162 cp_parser_label_declaration (parser);
10163 /* Parse an (optional) statement-seq. */
10164 cp_parser_statement_seq_opt (parser, NULL_TREE);
10165 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10169 /* Declarations [gram.dcl.dcl] */
10171 /* Parse an optional declaration-sequence.
10173 declaration-seq:
10174 declaration
10175 declaration-seq declaration */
10177 static void
10178 cp_parser_declaration_seq_opt (cp_parser* parser)
10180 while (true)
10182 cp_token *token;
10184 token = cp_lexer_peek_token (parser->lexer);
10186 if (token->type == CPP_CLOSE_BRACE
10187 || token->type == CPP_EOF
10188 || token->type == CPP_PRAGMA_EOL)
10189 break;
10191 if (token->type == CPP_SEMICOLON)
10193 /* A declaration consisting of a single semicolon is
10194 invalid. Allow it unless we're being pedantic. */
10195 cp_lexer_consume_token (parser->lexer);
10196 if (!in_system_header)
10197 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10198 continue;
10201 /* If we're entering or exiting a region that's implicitly
10202 extern "C", modify the lang context appropriately. */
10203 if (!parser->implicit_extern_c && token->implicit_extern_c)
10205 push_lang_context (lang_name_c);
10206 parser->implicit_extern_c = true;
10208 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10210 pop_lang_context ();
10211 parser->implicit_extern_c = false;
10214 if (token->type == CPP_PRAGMA)
10216 /* A top-level declaration can consist solely of a #pragma.
10217 A nested declaration cannot, so this is done here and not
10218 in cp_parser_declaration. (A #pragma at block scope is
10219 handled in cp_parser_statement.) */
10220 cp_parser_pragma (parser, pragma_external);
10221 continue;
10224 /* Parse the declaration itself. */
10225 cp_parser_declaration (parser);
10229 /* Parse a declaration.
10231 declaration:
10232 block-declaration
10233 function-definition
10234 template-declaration
10235 explicit-instantiation
10236 explicit-specialization
10237 linkage-specification
10238 namespace-definition
10240 GNU extension:
10242 declaration:
10243 __extension__ declaration */
10245 static void
10246 cp_parser_declaration (cp_parser* parser)
10248 cp_token token1;
10249 cp_token token2;
10250 int saved_pedantic;
10251 void *p;
10252 tree attributes = NULL_TREE;
10254 /* Check for the `__extension__' keyword. */
10255 if (cp_parser_extension_opt (parser, &saved_pedantic))
10257 /* Parse the qualified declaration. */
10258 cp_parser_declaration (parser);
10259 /* Restore the PEDANTIC flag. */
10260 pedantic = saved_pedantic;
10262 return;
10265 /* Try to figure out what kind of declaration is present. */
10266 token1 = *cp_lexer_peek_token (parser->lexer);
10268 if (token1.type != CPP_EOF)
10269 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10270 else
10272 token2.type = CPP_EOF;
10273 token2.keyword = RID_MAX;
10276 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10277 p = obstack_alloc (&declarator_obstack, 0);
10279 /* If the next token is `extern' and the following token is a string
10280 literal, then we have a linkage specification. */
10281 if (token1.keyword == RID_EXTERN
10282 && cp_parser_is_pure_string_literal (&token2))
10283 cp_parser_linkage_specification (parser);
10284 /* If the next token is `template', then we have either a template
10285 declaration, an explicit instantiation, or an explicit
10286 specialization. */
10287 else if (token1.keyword == RID_TEMPLATE)
10289 /* `template <>' indicates a template specialization. */
10290 if (token2.type == CPP_LESS
10291 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10292 cp_parser_explicit_specialization (parser);
10293 /* `template <' indicates a template declaration. */
10294 else if (token2.type == CPP_LESS)
10295 cp_parser_template_declaration (parser, /*member_p=*/false);
10296 /* Anything else must be an explicit instantiation. */
10297 else
10298 cp_parser_explicit_instantiation (parser);
10300 /* If the next token is `export', then we have a template
10301 declaration. */
10302 else if (token1.keyword == RID_EXPORT)
10303 cp_parser_template_declaration (parser, /*member_p=*/false);
10304 /* If the next token is `extern', 'static' or 'inline' and the one
10305 after that is `template', we have a GNU extended explicit
10306 instantiation directive. */
10307 else if (cp_parser_allow_gnu_extensions_p (parser)
10308 && (token1.keyword == RID_EXTERN
10309 || token1.keyword == RID_STATIC
10310 || token1.keyword == RID_INLINE)
10311 && token2.keyword == RID_TEMPLATE)
10312 cp_parser_explicit_instantiation (parser);
10313 /* If the next token is `namespace', check for a named or unnamed
10314 namespace definition. */
10315 else if (token1.keyword == RID_NAMESPACE
10316 && (/* A named namespace definition. */
10317 (token2.type == CPP_NAME
10318 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10319 != CPP_EQ))
10320 /* An unnamed namespace definition. */
10321 || token2.type == CPP_OPEN_BRACE
10322 || token2.keyword == RID_ATTRIBUTE))
10323 cp_parser_namespace_definition (parser);
10324 /* An inline (associated) namespace definition. */
10325 else if (token1.keyword == RID_INLINE
10326 && token2.keyword == RID_NAMESPACE)
10327 cp_parser_namespace_definition (parser);
10328 /* Objective-C++ declaration/definition. */
10329 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10330 cp_parser_objc_declaration (parser, NULL_TREE);
10331 else if (c_dialect_objc ()
10332 && token1.keyword == RID_ATTRIBUTE
10333 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10334 cp_parser_objc_declaration (parser, attributes);
10335 /* We must have either a block declaration or a function
10336 definition. */
10337 else
10338 /* Try to parse a block-declaration, or a function-definition. */
10339 cp_parser_block_declaration (parser, /*statement_p=*/false);
10341 /* Free any declarators allocated. */
10342 obstack_free (&declarator_obstack, p);
10345 /* Parse a block-declaration.
10347 block-declaration:
10348 simple-declaration
10349 asm-definition
10350 namespace-alias-definition
10351 using-declaration
10352 using-directive
10354 GNU Extension:
10356 block-declaration:
10357 __extension__ block-declaration
10359 C++0x Extension:
10361 block-declaration:
10362 static_assert-declaration
10364 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10365 part of a declaration-statement. */
10367 static void
10368 cp_parser_block_declaration (cp_parser *parser,
10369 bool statement_p)
10371 cp_token *token1;
10372 int saved_pedantic;
10374 /* Check for the `__extension__' keyword. */
10375 if (cp_parser_extension_opt (parser, &saved_pedantic))
10377 /* Parse the qualified declaration. */
10378 cp_parser_block_declaration (parser, statement_p);
10379 /* Restore the PEDANTIC flag. */
10380 pedantic = saved_pedantic;
10382 return;
10385 /* Peek at the next token to figure out which kind of declaration is
10386 present. */
10387 token1 = cp_lexer_peek_token (parser->lexer);
10389 /* If the next keyword is `asm', we have an asm-definition. */
10390 if (token1->keyword == RID_ASM)
10392 if (statement_p)
10393 cp_parser_commit_to_tentative_parse (parser);
10394 cp_parser_asm_definition (parser);
10396 /* If the next keyword is `namespace', we have a
10397 namespace-alias-definition. */
10398 else if (token1->keyword == RID_NAMESPACE)
10399 cp_parser_namespace_alias_definition (parser);
10400 /* If the next keyword is `using', we have a
10401 using-declaration, a using-directive, or an alias-declaration. */
10402 else if (token1->keyword == RID_USING)
10404 cp_token *token2;
10406 if (statement_p)
10407 cp_parser_commit_to_tentative_parse (parser);
10408 /* If the token after `using' is `namespace', then we have a
10409 using-directive. */
10410 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10411 if (token2->keyword == RID_NAMESPACE)
10412 cp_parser_using_directive (parser);
10413 /* If the second token after 'using' is '=', then we have an
10414 alias-declaration. */
10415 else if (cxx_dialect >= cxx0x
10416 && token2->type == CPP_NAME
10417 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10418 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
10419 cp_parser_alias_declaration (parser);
10420 /* Otherwise, it's a using-declaration. */
10421 else
10422 cp_parser_using_declaration (parser,
10423 /*access_declaration_p=*/false);
10425 /* If the next keyword is `__label__' we have a misplaced label
10426 declaration. */
10427 else if (token1->keyword == RID_LABEL)
10429 cp_lexer_consume_token (parser->lexer);
10430 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10431 cp_parser_skip_to_end_of_statement (parser);
10432 /* If the next token is now a `;', consume it. */
10433 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10434 cp_lexer_consume_token (parser->lexer);
10436 /* If the next token is `static_assert' we have a static assertion. */
10437 else if (token1->keyword == RID_STATIC_ASSERT)
10438 cp_parser_static_assert (parser, /*member_p=*/false);
10439 /* Anything else must be a simple-declaration. */
10440 else
10441 cp_parser_simple_declaration (parser, !statement_p,
10442 /*maybe_range_for_decl*/NULL);
10445 /* Parse a simple-declaration.
10447 simple-declaration:
10448 decl-specifier-seq [opt] init-declarator-list [opt] ;
10450 init-declarator-list:
10451 init-declarator
10452 init-declarator-list , init-declarator
10454 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10455 function-definition as a simple-declaration.
10457 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10458 parsed declaration if it is an uninitialized single declarator not followed
10459 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10460 if present, will not be consumed. */
10462 static void
10463 cp_parser_simple_declaration (cp_parser* parser,
10464 bool function_definition_allowed_p,
10465 tree *maybe_range_for_decl)
10467 cp_decl_specifier_seq decl_specifiers;
10468 int declares_class_or_enum;
10469 bool saw_declarator;
10471 if (maybe_range_for_decl)
10472 *maybe_range_for_decl = NULL_TREE;
10474 /* Defer access checks until we know what is being declared; the
10475 checks for names appearing in the decl-specifier-seq should be
10476 done as if we were in the scope of the thing being declared. */
10477 push_deferring_access_checks (dk_deferred);
10479 /* Parse the decl-specifier-seq. We have to keep track of whether
10480 or not the decl-specifier-seq declares a named class or
10481 enumeration type, since that is the only case in which the
10482 init-declarator-list is allowed to be empty.
10484 [dcl.dcl]
10486 In a simple-declaration, the optional init-declarator-list can be
10487 omitted only when declaring a class or enumeration, that is when
10488 the decl-specifier-seq contains either a class-specifier, an
10489 elaborated-type-specifier, or an enum-specifier. */
10490 cp_parser_decl_specifier_seq (parser,
10491 CP_PARSER_FLAGS_OPTIONAL,
10492 &decl_specifiers,
10493 &declares_class_or_enum);
10494 /* We no longer need to defer access checks. */
10495 stop_deferring_access_checks ();
10497 /* In a block scope, a valid declaration must always have a
10498 decl-specifier-seq. By not trying to parse declarators, we can
10499 resolve the declaration/expression ambiguity more quickly. */
10500 if (!function_definition_allowed_p
10501 && !decl_specifiers.any_specifiers_p)
10503 cp_parser_error (parser, "expected declaration");
10504 goto done;
10507 /* If the next two tokens are both identifiers, the code is
10508 erroneous. The usual cause of this situation is code like:
10510 T t;
10512 where "T" should name a type -- but does not. */
10513 if (!decl_specifiers.any_type_specifiers_p
10514 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10516 /* If parsing tentatively, we should commit; we really are
10517 looking at a declaration. */
10518 cp_parser_commit_to_tentative_parse (parser);
10519 /* Give up. */
10520 goto done;
10523 /* If we have seen at least one decl-specifier, and the next token
10524 is not a parenthesis, then we must be looking at a declaration.
10525 (After "int (" we might be looking at a functional cast.) */
10526 if (decl_specifiers.any_specifiers_p
10527 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10528 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10529 && !cp_parser_error_occurred (parser))
10530 cp_parser_commit_to_tentative_parse (parser);
10532 /* Keep going until we hit the `;' at the end of the simple
10533 declaration. */
10534 saw_declarator = false;
10535 while (cp_lexer_next_token_is_not (parser->lexer,
10536 CPP_SEMICOLON))
10538 cp_token *token;
10539 bool function_definition_p;
10540 tree decl;
10542 if (saw_declarator)
10544 /* If we are processing next declarator, coma is expected */
10545 token = cp_lexer_peek_token (parser->lexer);
10546 gcc_assert (token->type == CPP_COMMA);
10547 cp_lexer_consume_token (parser->lexer);
10548 if (maybe_range_for_decl)
10549 *maybe_range_for_decl = error_mark_node;
10551 else
10552 saw_declarator = true;
10554 /* Parse the init-declarator. */
10555 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10556 /*checks=*/NULL,
10557 function_definition_allowed_p,
10558 /*member_p=*/false,
10559 declares_class_or_enum,
10560 &function_definition_p,
10561 maybe_range_for_decl);
10562 /* If an error occurred while parsing tentatively, exit quickly.
10563 (That usually happens when in the body of a function; each
10564 statement is treated as a declaration-statement until proven
10565 otherwise.) */
10566 if (cp_parser_error_occurred (parser))
10567 goto done;
10568 /* Handle function definitions specially. */
10569 if (function_definition_p)
10571 /* If the next token is a `,', then we are probably
10572 processing something like:
10574 void f() {}, *p;
10576 which is erroneous. */
10577 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10579 cp_token *token = cp_lexer_peek_token (parser->lexer);
10580 error_at (token->location,
10581 "mixing"
10582 " declarations and function-definitions is forbidden");
10584 /* Otherwise, we're done with the list of declarators. */
10585 else
10587 pop_deferring_access_checks ();
10588 return;
10591 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10592 *maybe_range_for_decl = decl;
10593 /* The next token should be either a `,' or a `;'. */
10594 token = cp_lexer_peek_token (parser->lexer);
10595 /* If it's a `,', there are more declarators to come. */
10596 if (token->type == CPP_COMMA)
10597 /* will be consumed next time around */;
10598 /* If it's a `;', we are done. */
10599 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10600 break;
10601 /* Anything else is an error. */
10602 else
10604 /* If we have already issued an error message we don't need
10605 to issue another one. */
10606 if (decl != error_mark_node
10607 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10608 cp_parser_error (parser, "expected %<,%> or %<;%>");
10609 /* Skip tokens until we reach the end of the statement. */
10610 cp_parser_skip_to_end_of_statement (parser);
10611 /* If the next token is now a `;', consume it. */
10612 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10613 cp_lexer_consume_token (parser->lexer);
10614 goto done;
10616 /* After the first time around, a function-definition is not
10617 allowed -- even if it was OK at first. For example:
10619 int i, f() {}
10621 is not valid. */
10622 function_definition_allowed_p = false;
10625 /* Issue an error message if no declarators are present, and the
10626 decl-specifier-seq does not itself declare a class or
10627 enumeration. */
10628 if (!saw_declarator)
10630 if (cp_parser_declares_only_class_p (parser))
10631 shadow_tag (&decl_specifiers);
10632 /* Perform any deferred access checks. */
10633 perform_deferred_access_checks (tf_warning_or_error);
10636 /* Consume the `;'. */
10637 if (!maybe_range_for_decl)
10638 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10640 done:
10641 pop_deferring_access_checks ();
10644 /* Parse a decl-specifier-seq.
10646 decl-specifier-seq:
10647 decl-specifier-seq [opt] decl-specifier
10648 decl-specifier attribute-specifier-seq [opt] (C++11)
10650 decl-specifier:
10651 storage-class-specifier
10652 type-specifier
10653 function-specifier
10654 friend
10655 typedef
10657 GNU Extension:
10659 decl-specifier:
10660 attributes
10662 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10664 The parser flags FLAGS is used to control type-specifier parsing.
10666 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10667 flags:
10669 1: one of the decl-specifiers is an elaborated-type-specifier
10670 (i.e., a type declaration)
10671 2: one of the decl-specifiers is an enum-specifier or a
10672 class-specifier (i.e., a type definition)
10676 static void
10677 cp_parser_decl_specifier_seq (cp_parser* parser,
10678 cp_parser_flags flags,
10679 cp_decl_specifier_seq *decl_specs,
10680 int* declares_class_or_enum)
10682 bool constructor_possible_p = !parser->in_declarator_p;
10683 bool found_decl_spec = false;
10684 cp_token *start_token = NULL;
10685 cp_decl_spec ds;
10687 /* Clear DECL_SPECS. */
10688 clear_decl_specs (decl_specs);
10690 /* Assume no class or enumeration type is declared. */
10691 *declares_class_or_enum = 0;
10693 /* Keep reading specifiers until there are no more to read. */
10694 while (true)
10696 bool constructor_p;
10697 cp_token *token;
10698 ds = ds_last;
10700 /* Peek at the next token. */
10701 token = cp_lexer_peek_token (parser->lexer);
10703 /* Save the first token of the decl spec list for error
10704 reporting. */
10705 if (!start_token)
10706 start_token = token;
10707 /* Handle attributes. */
10708 if (cp_next_tokens_can_be_attribute_p (parser))
10710 /* Parse the attributes. */
10711 tree attrs = cp_parser_attributes_opt (parser);
10713 /* In a sequence of declaration specifiers, c++11 attributes
10714 appertain to the type that precede them. In that case
10715 [dcl.spec]/1 says:
10717 The attribute-specifier-seq affects the type only for
10718 the declaration it appears in, not other declarations
10719 involving the same type.
10721 But for now let's force the user to position the
10722 attribute either at the beginning of the declaration or
10723 after the declarator-id, which would clearly mean that it
10724 applies to the declarator. */
10725 if (cxx11_attribute_p (attrs))
10727 if (!found_decl_spec)
10728 /* The c++11 attribute is at the beginning of the
10729 declaration. It appertains to the entity being
10730 declared. */;
10731 else
10733 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
10735 /* This is an attribute following a
10736 class-specifier. */
10737 if (decl_specs->type_definition_p)
10738 warn_misplaced_attr_for_class_type (token->location,
10739 decl_specs->type);
10740 attrs = NULL_TREE;
10742 else
10744 decl_specs->std_attributes
10745 = chainon (decl_specs->std_attributes,
10746 attrs);
10747 if (decl_specs->locations[ds_std_attribute] == 0)
10748 decl_specs->locations[ds_std_attribute] = token->location;
10750 continue;
10754 decl_specs->attributes
10755 = chainon (decl_specs->attributes,
10756 attrs);
10757 if (decl_specs->locations[ds_attribute] == 0)
10758 decl_specs->locations[ds_attribute] = token->location;
10759 continue;
10761 /* Assume we will find a decl-specifier keyword. */
10762 found_decl_spec = true;
10763 /* If the next token is an appropriate keyword, we can simply
10764 add it to the list. */
10765 switch (token->keyword)
10767 /* decl-specifier:
10768 friend
10769 constexpr */
10770 case RID_FRIEND:
10771 if (!at_class_scope_p ())
10773 error_at (token->location, "%<friend%> used outside of class");
10774 cp_lexer_purge_token (parser->lexer);
10776 else
10778 ds = ds_friend;
10779 /* Consume the token. */
10780 cp_lexer_consume_token (parser->lexer);
10782 break;
10784 case RID_CONSTEXPR:
10785 ds = ds_constexpr;
10786 cp_lexer_consume_token (parser->lexer);
10787 break;
10789 /* function-specifier:
10790 inline
10791 virtual
10792 explicit */
10793 case RID_INLINE:
10794 case RID_VIRTUAL:
10795 case RID_EXPLICIT:
10796 cp_parser_function_specifier_opt (parser, decl_specs);
10797 break;
10799 /* decl-specifier:
10800 typedef */
10801 case RID_TYPEDEF:
10802 ds = ds_typedef;
10803 /* Consume the token. */
10804 cp_lexer_consume_token (parser->lexer);
10805 /* A constructor declarator cannot appear in a typedef. */
10806 constructor_possible_p = false;
10807 /* The "typedef" keyword can only occur in a declaration; we
10808 may as well commit at this point. */
10809 cp_parser_commit_to_tentative_parse (parser);
10811 if (decl_specs->storage_class != sc_none)
10812 decl_specs->conflicting_specifiers_p = true;
10813 break;
10815 /* storage-class-specifier:
10816 auto
10817 register
10818 static
10819 extern
10820 mutable
10822 GNU Extension:
10823 thread */
10824 case RID_AUTO:
10825 if (cxx_dialect == cxx98)
10827 /* Consume the token. */
10828 cp_lexer_consume_token (parser->lexer);
10830 /* Complain about `auto' as a storage specifier, if
10831 we're complaining about C++0x compatibility. */
10832 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10833 " changes meaning in C++11; please remove it");
10835 /* Set the storage class anyway. */
10836 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10837 token);
10839 else
10840 /* C++0x auto type-specifier. */
10841 found_decl_spec = false;
10842 break;
10844 case RID_REGISTER:
10845 case RID_STATIC:
10846 case RID_EXTERN:
10847 case RID_MUTABLE:
10848 /* Consume the token. */
10849 cp_lexer_consume_token (parser->lexer);
10850 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10851 token);
10852 break;
10853 case RID_THREAD:
10854 /* Consume the token. */
10855 ds = ds_thread;
10856 cp_lexer_consume_token (parser->lexer);
10857 break;
10859 default:
10860 /* We did not yet find a decl-specifier yet. */
10861 found_decl_spec = false;
10862 break;
10865 if (found_decl_spec
10866 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10867 && token->keyword != RID_CONSTEXPR)
10868 error ("decl-specifier invalid in condition");
10870 if (ds != ds_last)
10871 set_and_check_decl_spec_loc (decl_specs, ds, token);
10873 /* Constructors are a special case. The `S' in `S()' is not a
10874 decl-specifier; it is the beginning of the declarator. */
10875 constructor_p
10876 = (!found_decl_spec
10877 && constructor_possible_p
10878 && (cp_parser_constructor_declarator_p
10879 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
10881 /* If we don't have a DECL_SPEC yet, then we must be looking at
10882 a type-specifier. */
10883 if (!found_decl_spec && !constructor_p)
10885 int decl_spec_declares_class_or_enum;
10886 bool is_cv_qualifier;
10887 tree type_spec;
10889 type_spec
10890 = cp_parser_type_specifier (parser, flags,
10891 decl_specs,
10892 /*is_declaration=*/true,
10893 &decl_spec_declares_class_or_enum,
10894 &is_cv_qualifier);
10895 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10897 /* If this type-specifier referenced a user-defined type
10898 (a typedef, class-name, etc.), then we can't allow any
10899 more such type-specifiers henceforth.
10901 [dcl.spec]
10903 The longest sequence of decl-specifiers that could
10904 possibly be a type name is taken as the
10905 decl-specifier-seq of a declaration. The sequence shall
10906 be self-consistent as described below.
10908 [dcl.type]
10910 As a general rule, at most one type-specifier is allowed
10911 in the complete decl-specifier-seq of a declaration. The
10912 only exceptions are the following:
10914 -- const or volatile can be combined with any other
10915 type-specifier.
10917 -- signed or unsigned can be combined with char, long,
10918 short, or int.
10920 -- ..
10922 Example:
10924 typedef char* Pc;
10925 void g (const int Pc);
10927 Here, Pc is *not* part of the decl-specifier seq; it's
10928 the declarator. Therefore, once we see a type-specifier
10929 (other than a cv-qualifier), we forbid any additional
10930 user-defined types. We *do* still allow things like `int
10931 int' to be considered a decl-specifier-seq, and issue the
10932 error message later. */
10933 if (type_spec && !is_cv_qualifier)
10934 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10935 /* A constructor declarator cannot follow a type-specifier. */
10936 if (type_spec)
10938 constructor_possible_p = false;
10939 found_decl_spec = true;
10940 if (!is_cv_qualifier)
10941 decl_specs->any_type_specifiers_p = true;
10945 /* If we still do not have a DECL_SPEC, then there are no more
10946 decl-specifiers. */
10947 if (!found_decl_spec)
10948 break;
10950 decl_specs->any_specifiers_p = true;
10951 /* After we see one decl-specifier, further decl-specifiers are
10952 always optional. */
10953 flags |= CP_PARSER_FLAGS_OPTIONAL;
10956 /* Don't allow a friend specifier with a class definition. */
10957 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
10958 && (*declares_class_or_enum & 2))
10959 error_at (decl_specs->locations[ds_friend],
10960 "class definition may not be declared a friend");
10963 /* Parse an (optional) storage-class-specifier.
10965 storage-class-specifier:
10966 auto
10967 register
10968 static
10969 extern
10970 mutable
10972 GNU Extension:
10974 storage-class-specifier:
10975 thread
10977 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10979 static tree
10980 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10982 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10984 case RID_AUTO:
10985 if (cxx_dialect != cxx98)
10986 return NULL_TREE;
10987 /* Fall through for C++98. */
10989 case RID_REGISTER:
10990 case RID_STATIC:
10991 case RID_EXTERN:
10992 case RID_MUTABLE:
10993 case RID_THREAD:
10994 /* Consume the token. */
10995 return cp_lexer_consume_token (parser->lexer)->u.value;
10997 default:
10998 return NULL_TREE;
11002 /* Parse an (optional) function-specifier.
11004 function-specifier:
11005 inline
11006 virtual
11007 explicit
11009 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11010 Updates DECL_SPECS, if it is non-NULL. */
11012 static tree
11013 cp_parser_function_specifier_opt (cp_parser* parser,
11014 cp_decl_specifier_seq *decl_specs)
11016 cp_token *token = cp_lexer_peek_token (parser->lexer);
11017 switch (token->keyword)
11019 case RID_INLINE:
11020 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11021 break;
11023 case RID_VIRTUAL:
11024 /* 14.5.2.3 [temp.mem]
11026 A member function template shall not be virtual. */
11027 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11028 error_at (token->location, "templates may not be %<virtual%>");
11029 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11030 break;
11032 case RID_EXPLICIT:
11033 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11034 break;
11036 default:
11037 return NULL_TREE;
11040 /* Consume the token. */
11041 return cp_lexer_consume_token (parser->lexer)->u.value;
11044 /* Parse a linkage-specification.
11046 linkage-specification:
11047 extern string-literal { declaration-seq [opt] }
11048 extern string-literal declaration */
11050 static void
11051 cp_parser_linkage_specification (cp_parser* parser)
11053 tree linkage;
11055 /* Look for the `extern' keyword. */
11056 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11058 /* Look for the string-literal. */
11059 linkage = cp_parser_string_literal (parser, false, false);
11061 /* Transform the literal into an identifier. If the literal is a
11062 wide-character string, or contains embedded NULs, then we can't
11063 handle it as the user wants. */
11064 if (strlen (TREE_STRING_POINTER (linkage))
11065 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11067 cp_parser_error (parser, "invalid linkage-specification");
11068 /* Assume C++ linkage. */
11069 linkage = lang_name_cplusplus;
11071 else
11072 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11074 /* We're now using the new linkage. */
11075 push_lang_context (linkage);
11077 /* If the next token is a `{', then we're using the first
11078 production. */
11079 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11081 /* Consume the `{' token. */
11082 cp_lexer_consume_token (parser->lexer);
11083 /* Parse the declarations. */
11084 cp_parser_declaration_seq_opt (parser);
11085 /* Look for the closing `}'. */
11086 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11088 /* Otherwise, there's just one declaration. */
11089 else
11091 bool saved_in_unbraced_linkage_specification_p;
11093 saved_in_unbraced_linkage_specification_p
11094 = parser->in_unbraced_linkage_specification_p;
11095 parser->in_unbraced_linkage_specification_p = true;
11096 cp_parser_declaration (parser);
11097 parser->in_unbraced_linkage_specification_p
11098 = saved_in_unbraced_linkage_specification_p;
11101 /* We're done with the linkage-specification. */
11102 pop_lang_context ();
11105 /* Parse a static_assert-declaration.
11107 static_assert-declaration:
11108 static_assert ( constant-expression , string-literal ) ;
11110 If MEMBER_P, this static_assert is a class member. */
11112 static void
11113 cp_parser_static_assert(cp_parser *parser, bool member_p)
11115 tree condition;
11116 tree message;
11117 cp_token *token;
11118 location_t saved_loc;
11119 bool dummy;
11121 /* Peek at the `static_assert' token so we can keep track of exactly
11122 where the static assertion started. */
11123 token = cp_lexer_peek_token (parser->lexer);
11124 saved_loc = token->location;
11126 /* Look for the `static_assert' keyword. */
11127 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11128 RT_STATIC_ASSERT))
11129 return;
11131 /* We know we are in a static assertion; commit to any tentative
11132 parse. */
11133 if (cp_parser_parsing_tentatively (parser))
11134 cp_parser_commit_to_tentative_parse (parser);
11136 /* Parse the `(' starting the static assertion condition. */
11137 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11139 /* Parse the constant-expression. Allow a non-constant expression
11140 here in order to give better diagnostics in finish_static_assert. */
11141 condition =
11142 cp_parser_constant_expression (parser,
11143 /*allow_non_constant_p=*/true,
11144 /*non_constant_p=*/&dummy);
11146 /* Parse the separating `,'. */
11147 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11149 /* Parse the string-literal message. */
11150 message = cp_parser_string_literal (parser,
11151 /*translate=*/false,
11152 /*wide_ok=*/true);
11154 /* A `)' completes the static assertion. */
11155 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11156 cp_parser_skip_to_closing_parenthesis (parser,
11157 /*recovering=*/true,
11158 /*or_comma=*/false,
11159 /*consume_paren=*/true);
11161 /* A semicolon terminates the declaration. */
11162 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11164 /* Complete the static assertion, which may mean either processing
11165 the static assert now or saving it for template instantiation. */
11166 finish_static_assert (condition, message, saved_loc, member_p);
11169 /* Parse a `decltype' type. Returns the type.
11171 simple-type-specifier:
11172 decltype ( expression ) */
11174 static tree
11175 cp_parser_decltype (cp_parser *parser)
11177 tree expr;
11178 bool id_expression_or_member_access_p = false;
11179 const char *saved_message;
11180 bool saved_integral_constant_expression_p;
11181 bool saved_non_integral_constant_expression_p;
11182 cp_token *id_expr_start_token;
11183 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11185 if (start_token->type == CPP_DECLTYPE)
11187 /* Already parsed. */
11188 cp_lexer_consume_token (parser->lexer);
11189 return start_token->u.value;
11192 /* Look for the `decltype' token. */
11193 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11194 return error_mark_node;
11196 /* Types cannot be defined in a `decltype' expression. Save away the
11197 old message. */
11198 saved_message = parser->type_definition_forbidden_message;
11200 /* And create the new one. */
11201 parser->type_definition_forbidden_message
11202 = G_("types may not be defined in %<decltype%> expressions");
11204 /* The restrictions on constant-expressions do not apply inside
11205 decltype expressions. */
11206 saved_integral_constant_expression_p
11207 = parser->integral_constant_expression_p;
11208 saved_non_integral_constant_expression_p
11209 = parser->non_integral_constant_expression_p;
11210 parser->integral_constant_expression_p = false;
11212 /* Do not actually evaluate the expression. */
11213 ++cp_unevaluated_operand;
11215 /* Do not warn about problems with the expression. */
11216 ++c_inhibit_evaluation_warnings;
11218 /* Parse the opening `('. */
11219 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11220 return error_mark_node;
11222 /* First, try parsing an id-expression. */
11223 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11224 cp_parser_parse_tentatively (parser);
11225 expr = cp_parser_id_expression (parser,
11226 /*template_keyword_p=*/false,
11227 /*check_dependency_p=*/true,
11228 /*template_p=*/NULL,
11229 /*declarator_p=*/false,
11230 /*optional_p=*/false);
11232 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11234 bool non_integral_constant_expression_p = false;
11235 tree id_expression = expr;
11236 cp_id_kind idk;
11237 const char *error_msg;
11239 if (TREE_CODE (expr) == IDENTIFIER_NODE)
11240 /* Lookup the name we got back from the id-expression. */
11241 expr = cp_parser_lookup_name (parser, expr,
11242 none_type,
11243 /*is_template=*/false,
11244 /*is_namespace=*/false,
11245 /*check_dependency=*/true,
11246 /*ambiguous_decls=*/NULL,
11247 id_expr_start_token->location);
11249 if (expr
11250 && expr != error_mark_node
11251 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11252 && TREE_CODE (expr) != TYPE_DECL
11253 && (TREE_CODE (expr) != BIT_NOT_EXPR
11254 || !TYPE_P (TREE_OPERAND (expr, 0)))
11255 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11257 /* Complete lookup of the id-expression. */
11258 expr = (finish_id_expression
11259 (id_expression, expr, parser->scope, &idk,
11260 /*integral_constant_expression_p=*/false,
11261 /*allow_non_integral_constant_expression_p=*/true,
11262 &non_integral_constant_expression_p,
11263 /*template_p=*/false,
11264 /*done=*/true,
11265 /*address_p=*/false,
11266 /*template_arg_p=*/false,
11267 &error_msg,
11268 id_expr_start_token->location));
11270 if (expr == error_mark_node)
11271 /* We found an id-expression, but it was something that we
11272 should not have found. This is an error, not something
11273 we can recover from, so note that we found an
11274 id-expression and we'll recover as gracefully as
11275 possible. */
11276 id_expression_or_member_access_p = true;
11279 if (expr
11280 && expr != error_mark_node
11281 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11282 /* We have an id-expression. */
11283 id_expression_or_member_access_p = true;
11286 if (!id_expression_or_member_access_p)
11288 /* Abort the id-expression parse. */
11289 cp_parser_abort_tentative_parse (parser);
11291 /* Parsing tentatively, again. */
11292 cp_parser_parse_tentatively (parser);
11294 /* Parse a class member access. */
11295 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11296 /*cast_p=*/false,
11297 /*member_access_only_p=*/true, NULL);
11299 if (expr
11300 && expr != error_mark_node
11301 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11302 /* We have an id-expression. */
11303 id_expression_or_member_access_p = true;
11306 if (id_expression_or_member_access_p)
11307 /* We have parsed the complete id-expression or member access. */
11308 cp_parser_parse_definitely (parser);
11309 else
11311 bool saved_greater_than_is_operator_p;
11313 /* Abort our attempt to parse an id-expression or member access
11314 expression. */
11315 cp_parser_abort_tentative_parse (parser);
11317 /* Within a parenthesized expression, a `>' token is always
11318 the greater-than operator. */
11319 saved_greater_than_is_operator_p
11320 = parser->greater_than_is_operator_p;
11321 parser->greater_than_is_operator_p = true;
11323 /* Parse a full expression. */
11324 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11326 /* The `>' token might be the end of a template-id or
11327 template-parameter-list now. */
11328 parser->greater_than_is_operator_p
11329 = saved_greater_than_is_operator_p;
11332 /* Go back to evaluating expressions. */
11333 --cp_unevaluated_operand;
11334 --c_inhibit_evaluation_warnings;
11336 /* Restore the old message and the integral constant expression
11337 flags. */
11338 parser->type_definition_forbidden_message = saved_message;
11339 parser->integral_constant_expression_p
11340 = saved_integral_constant_expression_p;
11341 parser->non_integral_constant_expression_p
11342 = saved_non_integral_constant_expression_p;
11344 /* Parse to the closing `)'. */
11345 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11347 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11348 /*consume_paren=*/true);
11349 return error_mark_node;
11352 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11353 tf_warning_or_error);
11355 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11356 it again. */
11357 start_token->type = CPP_DECLTYPE;
11358 start_token->u.value = expr;
11359 start_token->keyword = RID_MAX;
11360 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11362 return expr;
11365 /* Special member functions [gram.special] */
11367 /* Parse a conversion-function-id.
11369 conversion-function-id:
11370 operator conversion-type-id
11372 Returns an IDENTIFIER_NODE representing the operator. */
11374 static tree
11375 cp_parser_conversion_function_id (cp_parser* parser)
11377 tree type;
11378 tree saved_scope;
11379 tree saved_qualifying_scope;
11380 tree saved_object_scope;
11381 tree pushed_scope = NULL_TREE;
11383 /* Look for the `operator' token. */
11384 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11385 return error_mark_node;
11386 /* When we parse the conversion-type-id, the current scope will be
11387 reset. However, we need that information in able to look up the
11388 conversion function later, so we save it here. */
11389 saved_scope = parser->scope;
11390 saved_qualifying_scope = parser->qualifying_scope;
11391 saved_object_scope = parser->object_scope;
11392 /* We must enter the scope of the class so that the names of
11393 entities declared within the class are available in the
11394 conversion-type-id. For example, consider:
11396 struct S {
11397 typedef int I;
11398 operator I();
11401 S::operator I() { ... }
11403 In order to see that `I' is a type-name in the definition, we
11404 must be in the scope of `S'. */
11405 if (saved_scope)
11406 pushed_scope = push_scope (saved_scope);
11407 /* Parse the conversion-type-id. */
11408 type = cp_parser_conversion_type_id (parser);
11409 /* Leave the scope of the class, if any. */
11410 if (pushed_scope)
11411 pop_scope (pushed_scope);
11412 /* Restore the saved scope. */
11413 parser->scope = saved_scope;
11414 parser->qualifying_scope = saved_qualifying_scope;
11415 parser->object_scope = saved_object_scope;
11416 /* If the TYPE is invalid, indicate failure. */
11417 if (type == error_mark_node)
11418 return error_mark_node;
11419 return mangle_conv_op_name_for_type (type);
11422 /* Parse a conversion-type-id:
11424 conversion-type-id:
11425 type-specifier-seq conversion-declarator [opt]
11427 Returns the TYPE specified. */
11429 static tree
11430 cp_parser_conversion_type_id (cp_parser* parser)
11432 tree attributes;
11433 cp_decl_specifier_seq type_specifiers;
11434 cp_declarator *declarator;
11435 tree type_specified;
11437 /* Parse the attributes. */
11438 attributes = cp_parser_attributes_opt (parser);
11439 /* Parse the type-specifiers. */
11440 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11441 /*is_trailing_return=*/false,
11442 &type_specifiers);
11443 /* If that didn't work, stop. */
11444 if (type_specifiers.type == error_mark_node)
11445 return error_mark_node;
11446 /* Parse the conversion-declarator. */
11447 declarator = cp_parser_conversion_declarator_opt (parser);
11449 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11450 /*initialized=*/0, &attributes);
11451 if (attributes)
11452 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11454 /* Don't give this error when parsing tentatively. This happens to
11455 work because we always parse this definitively once. */
11456 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11457 && type_uses_auto (type_specified))
11459 if (cxx_dialect < cxx1y)
11461 error ("invalid use of %<auto%> in conversion operator");
11462 return error_mark_node;
11464 else if (template_parm_scope_p ())
11465 warning (0, "use of %<auto%> in member template "
11466 "conversion operator can never be deduced");
11469 return type_specified;
11472 /* Parse an (optional) conversion-declarator.
11474 conversion-declarator:
11475 ptr-operator conversion-declarator [opt]
11479 static cp_declarator *
11480 cp_parser_conversion_declarator_opt (cp_parser* parser)
11482 enum tree_code code;
11483 tree class_type, std_attributes = NULL_TREE;
11484 cp_cv_quals cv_quals;
11486 /* We don't know if there's a ptr-operator next, or not. */
11487 cp_parser_parse_tentatively (parser);
11488 /* Try the ptr-operator. */
11489 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
11490 &std_attributes);
11491 /* If it worked, look for more conversion-declarators. */
11492 if (cp_parser_parse_definitely (parser))
11494 cp_declarator *declarator;
11496 /* Parse another optional declarator. */
11497 declarator = cp_parser_conversion_declarator_opt (parser);
11499 declarator = cp_parser_make_indirect_declarator
11500 (code, class_type, cv_quals, declarator, std_attributes);
11502 return declarator;
11505 return NULL;
11508 /* Parse an (optional) ctor-initializer.
11510 ctor-initializer:
11511 : mem-initializer-list
11513 Returns TRUE iff the ctor-initializer was actually present. */
11515 static bool
11516 cp_parser_ctor_initializer_opt (cp_parser* parser)
11518 /* If the next token is not a `:', then there is no
11519 ctor-initializer. */
11520 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11522 /* Do default initialization of any bases and members. */
11523 if (DECL_CONSTRUCTOR_P (current_function_decl))
11524 finish_mem_initializers (NULL_TREE);
11526 return false;
11529 /* Consume the `:' token. */
11530 cp_lexer_consume_token (parser->lexer);
11531 /* And the mem-initializer-list. */
11532 cp_parser_mem_initializer_list (parser);
11534 return true;
11537 /* Parse a mem-initializer-list.
11539 mem-initializer-list:
11540 mem-initializer ... [opt]
11541 mem-initializer ... [opt] , mem-initializer-list */
11543 static void
11544 cp_parser_mem_initializer_list (cp_parser* parser)
11546 tree mem_initializer_list = NULL_TREE;
11547 tree target_ctor = error_mark_node;
11548 cp_token *token = cp_lexer_peek_token (parser->lexer);
11550 /* Let the semantic analysis code know that we are starting the
11551 mem-initializer-list. */
11552 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11553 error_at (token->location,
11554 "only constructors take member initializers");
11556 /* Loop through the list. */
11557 while (true)
11559 tree mem_initializer;
11561 token = cp_lexer_peek_token (parser->lexer);
11562 /* Parse the mem-initializer. */
11563 mem_initializer = cp_parser_mem_initializer (parser);
11564 /* If the next token is a `...', we're expanding member initializers. */
11565 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11567 /* Consume the `...'. */
11568 cp_lexer_consume_token (parser->lexer);
11570 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11571 can be expanded but members cannot. */
11572 if (mem_initializer != error_mark_node
11573 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11575 error_at (token->location,
11576 "cannot expand initializer for member %<%D%>",
11577 TREE_PURPOSE (mem_initializer));
11578 mem_initializer = error_mark_node;
11581 /* Construct the pack expansion type. */
11582 if (mem_initializer != error_mark_node)
11583 mem_initializer = make_pack_expansion (mem_initializer);
11585 if (target_ctor != error_mark_node
11586 && mem_initializer != error_mark_node)
11588 error ("mem-initializer for %qD follows constructor delegation",
11589 TREE_PURPOSE (mem_initializer));
11590 mem_initializer = error_mark_node;
11592 /* Look for a target constructor. */
11593 if (mem_initializer != error_mark_node
11594 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
11595 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11597 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11598 if (mem_initializer_list)
11600 error ("constructor delegation follows mem-initializer for %qD",
11601 TREE_PURPOSE (mem_initializer_list));
11602 mem_initializer = error_mark_node;
11604 target_ctor = mem_initializer;
11606 /* Add it to the list, unless it was erroneous. */
11607 if (mem_initializer != error_mark_node)
11609 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11610 mem_initializer_list = mem_initializer;
11612 /* If the next token is not a `,', we're done. */
11613 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11614 break;
11615 /* Consume the `,' token. */
11616 cp_lexer_consume_token (parser->lexer);
11619 /* Perform semantic analysis. */
11620 if (DECL_CONSTRUCTOR_P (current_function_decl))
11621 finish_mem_initializers (mem_initializer_list);
11624 /* Parse a mem-initializer.
11626 mem-initializer:
11627 mem-initializer-id ( expression-list [opt] )
11628 mem-initializer-id braced-init-list
11630 GNU extension:
11632 mem-initializer:
11633 ( expression-list [opt] )
11635 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11636 class) or FIELD_DECL (for a non-static data member) to initialize;
11637 the TREE_VALUE is the expression-list. An empty initialization
11638 list is represented by void_list_node. */
11640 static tree
11641 cp_parser_mem_initializer (cp_parser* parser)
11643 tree mem_initializer_id;
11644 tree expression_list;
11645 tree member;
11646 cp_token *token = cp_lexer_peek_token (parser->lexer);
11648 /* Find out what is being initialized. */
11649 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11651 permerror (token->location,
11652 "anachronistic old-style base class initializer");
11653 mem_initializer_id = NULL_TREE;
11655 else
11657 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11658 if (mem_initializer_id == error_mark_node)
11659 return mem_initializer_id;
11661 member = expand_member_init (mem_initializer_id);
11662 if (member && !DECL_P (member))
11663 in_base_initializer = 1;
11665 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11667 bool expr_non_constant_p;
11668 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11669 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11670 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11671 expression_list = build_tree_list (NULL_TREE, expression_list);
11673 else
11675 vec<tree, va_gc> *vec;
11676 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11677 /*cast_p=*/false,
11678 /*allow_expansion_p=*/true,
11679 /*non_constant_p=*/NULL);
11680 if (vec == NULL)
11681 return error_mark_node;
11682 expression_list = build_tree_list_vec (vec);
11683 release_tree_vector (vec);
11686 if (expression_list == error_mark_node)
11687 return error_mark_node;
11688 if (!expression_list)
11689 expression_list = void_type_node;
11691 in_base_initializer = 0;
11693 return member ? build_tree_list (member, expression_list) : error_mark_node;
11696 /* Parse a mem-initializer-id.
11698 mem-initializer-id:
11699 :: [opt] nested-name-specifier [opt] class-name
11700 identifier
11702 Returns a TYPE indicating the class to be initializer for the first
11703 production. Returns an IDENTIFIER_NODE indicating the data member
11704 to be initialized for the second production. */
11706 static tree
11707 cp_parser_mem_initializer_id (cp_parser* parser)
11709 bool global_scope_p;
11710 bool nested_name_specifier_p;
11711 bool template_p = false;
11712 tree id;
11714 cp_token *token = cp_lexer_peek_token (parser->lexer);
11716 /* `typename' is not allowed in this context ([temp.res]). */
11717 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11719 error_at (token->location,
11720 "keyword %<typename%> not allowed in this context (a qualified "
11721 "member initializer is implicitly a type)");
11722 cp_lexer_consume_token (parser->lexer);
11724 /* Look for the optional `::' operator. */
11725 global_scope_p
11726 = (cp_parser_global_scope_opt (parser,
11727 /*current_scope_valid_p=*/false)
11728 != NULL_TREE);
11729 /* Look for the optional nested-name-specifier. The simplest way to
11730 implement:
11732 [temp.res]
11734 The keyword `typename' is not permitted in a base-specifier or
11735 mem-initializer; in these contexts a qualified name that
11736 depends on a template-parameter is implicitly assumed to be a
11737 type name.
11739 is to assume that we have seen the `typename' keyword at this
11740 point. */
11741 nested_name_specifier_p
11742 = (cp_parser_nested_name_specifier_opt (parser,
11743 /*typename_keyword_p=*/true,
11744 /*check_dependency_p=*/true,
11745 /*type_p=*/true,
11746 /*is_declaration=*/true)
11747 != NULL_TREE);
11748 if (nested_name_specifier_p)
11749 template_p = cp_parser_optional_template_keyword (parser);
11750 /* If there is a `::' operator or a nested-name-specifier, then we
11751 are definitely looking for a class-name. */
11752 if (global_scope_p || nested_name_specifier_p)
11753 return cp_parser_class_name (parser,
11754 /*typename_keyword_p=*/true,
11755 /*template_keyword_p=*/template_p,
11756 typename_type,
11757 /*check_dependency_p=*/true,
11758 /*class_head_p=*/false,
11759 /*is_declaration=*/true);
11760 /* Otherwise, we could also be looking for an ordinary identifier. */
11761 cp_parser_parse_tentatively (parser);
11762 /* Try a class-name. */
11763 id = cp_parser_class_name (parser,
11764 /*typename_keyword_p=*/true,
11765 /*template_keyword_p=*/false,
11766 none_type,
11767 /*check_dependency_p=*/true,
11768 /*class_head_p=*/false,
11769 /*is_declaration=*/true);
11770 /* If we found one, we're done. */
11771 if (cp_parser_parse_definitely (parser))
11772 return id;
11773 /* Otherwise, look for an ordinary identifier. */
11774 return cp_parser_identifier (parser);
11777 /* Overloading [gram.over] */
11779 /* Parse an operator-function-id.
11781 operator-function-id:
11782 operator operator
11784 Returns an IDENTIFIER_NODE for the operator which is a
11785 human-readable spelling of the identifier, e.g., `operator +'. */
11787 static tree
11788 cp_parser_operator_function_id (cp_parser* parser)
11790 /* Look for the `operator' keyword. */
11791 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11792 return error_mark_node;
11793 /* And then the name of the operator itself. */
11794 return cp_parser_operator (parser);
11797 /* Return an identifier node for a user-defined literal operator.
11798 The suffix identifier is chained to the operator name identifier. */
11800 static tree
11801 cp_literal_operator_id (const char* name)
11803 tree identifier;
11804 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11805 + strlen (name) + 10);
11806 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11807 identifier = get_identifier (buffer);
11808 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11810 return identifier;
11813 /* Parse an operator.
11815 operator:
11816 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11817 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11818 || ++ -- , ->* -> () []
11820 GNU Extensions:
11822 operator:
11823 <? >? <?= >?=
11825 Returns an IDENTIFIER_NODE for the operator which is a
11826 human-readable spelling of the identifier, e.g., `operator +'. */
11828 static tree
11829 cp_parser_operator (cp_parser* parser)
11831 tree id = NULL_TREE;
11832 cp_token *token;
11834 /* Peek at the next token. */
11835 token = cp_lexer_peek_token (parser->lexer);
11836 /* Figure out which operator we have. */
11837 switch (token->type)
11839 case CPP_KEYWORD:
11841 enum tree_code op;
11843 /* The keyword should be either `new' or `delete'. */
11844 if (token->keyword == RID_NEW)
11845 op = NEW_EXPR;
11846 else if (token->keyword == RID_DELETE)
11847 op = DELETE_EXPR;
11848 else
11849 break;
11851 /* Consume the `new' or `delete' token. */
11852 cp_lexer_consume_token (parser->lexer);
11854 /* Peek at the next token. */
11855 token = cp_lexer_peek_token (parser->lexer);
11856 /* If it's a `[' token then this is the array variant of the
11857 operator. */
11858 if (token->type == CPP_OPEN_SQUARE)
11860 /* Consume the `[' token. */
11861 cp_lexer_consume_token (parser->lexer);
11862 /* Look for the `]' token. */
11863 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11864 id = ansi_opname (op == NEW_EXPR
11865 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11867 /* Otherwise, we have the non-array variant. */
11868 else
11869 id = ansi_opname (op);
11871 return id;
11874 case CPP_PLUS:
11875 id = ansi_opname (PLUS_EXPR);
11876 break;
11878 case CPP_MINUS:
11879 id = ansi_opname (MINUS_EXPR);
11880 break;
11882 case CPP_MULT:
11883 id = ansi_opname (MULT_EXPR);
11884 break;
11886 case CPP_DIV:
11887 id = ansi_opname (TRUNC_DIV_EXPR);
11888 break;
11890 case CPP_MOD:
11891 id = ansi_opname (TRUNC_MOD_EXPR);
11892 break;
11894 case CPP_XOR:
11895 id = ansi_opname (BIT_XOR_EXPR);
11896 break;
11898 case CPP_AND:
11899 id = ansi_opname (BIT_AND_EXPR);
11900 break;
11902 case CPP_OR:
11903 id = ansi_opname (BIT_IOR_EXPR);
11904 break;
11906 case CPP_COMPL:
11907 id = ansi_opname (BIT_NOT_EXPR);
11908 break;
11910 case CPP_NOT:
11911 id = ansi_opname (TRUTH_NOT_EXPR);
11912 break;
11914 case CPP_EQ:
11915 id = ansi_assopname (NOP_EXPR);
11916 break;
11918 case CPP_LESS:
11919 id = ansi_opname (LT_EXPR);
11920 break;
11922 case CPP_GREATER:
11923 id = ansi_opname (GT_EXPR);
11924 break;
11926 case CPP_PLUS_EQ:
11927 id = ansi_assopname (PLUS_EXPR);
11928 break;
11930 case CPP_MINUS_EQ:
11931 id = ansi_assopname (MINUS_EXPR);
11932 break;
11934 case CPP_MULT_EQ:
11935 id = ansi_assopname (MULT_EXPR);
11936 break;
11938 case CPP_DIV_EQ:
11939 id = ansi_assopname (TRUNC_DIV_EXPR);
11940 break;
11942 case CPP_MOD_EQ:
11943 id = ansi_assopname (TRUNC_MOD_EXPR);
11944 break;
11946 case CPP_XOR_EQ:
11947 id = ansi_assopname (BIT_XOR_EXPR);
11948 break;
11950 case CPP_AND_EQ:
11951 id = ansi_assopname (BIT_AND_EXPR);
11952 break;
11954 case CPP_OR_EQ:
11955 id = ansi_assopname (BIT_IOR_EXPR);
11956 break;
11958 case CPP_LSHIFT:
11959 id = ansi_opname (LSHIFT_EXPR);
11960 break;
11962 case CPP_RSHIFT:
11963 id = ansi_opname (RSHIFT_EXPR);
11964 break;
11966 case CPP_LSHIFT_EQ:
11967 id = ansi_assopname (LSHIFT_EXPR);
11968 break;
11970 case CPP_RSHIFT_EQ:
11971 id = ansi_assopname (RSHIFT_EXPR);
11972 break;
11974 case CPP_EQ_EQ:
11975 id = ansi_opname (EQ_EXPR);
11976 break;
11978 case CPP_NOT_EQ:
11979 id = ansi_opname (NE_EXPR);
11980 break;
11982 case CPP_LESS_EQ:
11983 id = ansi_opname (LE_EXPR);
11984 break;
11986 case CPP_GREATER_EQ:
11987 id = ansi_opname (GE_EXPR);
11988 break;
11990 case CPP_AND_AND:
11991 id = ansi_opname (TRUTH_ANDIF_EXPR);
11992 break;
11994 case CPP_OR_OR:
11995 id = ansi_opname (TRUTH_ORIF_EXPR);
11996 break;
11998 case CPP_PLUS_PLUS:
11999 id = ansi_opname (POSTINCREMENT_EXPR);
12000 break;
12002 case CPP_MINUS_MINUS:
12003 id = ansi_opname (PREDECREMENT_EXPR);
12004 break;
12006 case CPP_COMMA:
12007 id = ansi_opname (COMPOUND_EXPR);
12008 break;
12010 case CPP_DEREF_STAR:
12011 id = ansi_opname (MEMBER_REF);
12012 break;
12014 case CPP_DEREF:
12015 id = ansi_opname (COMPONENT_REF);
12016 break;
12018 case CPP_OPEN_PAREN:
12019 /* Consume the `('. */
12020 cp_lexer_consume_token (parser->lexer);
12021 /* Look for the matching `)'. */
12022 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12023 return ansi_opname (CALL_EXPR);
12025 case CPP_OPEN_SQUARE:
12026 /* Consume the `['. */
12027 cp_lexer_consume_token (parser->lexer);
12028 /* Look for the matching `]'. */
12029 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12030 return ansi_opname (ARRAY_REF);
12032 case CPP_STRING:
12033 if (cxx_dialect == cxx98)
12034 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12035 if (TREE_STRING_LENGTH (token->u.value) > 2)
12037 error ("expected empty string after %<operator%> keyword");
12038 return error_mark_node;
12040 /* Consume the string. */
12041 cp_lexer_consume_token (parser->lexer);
12042 /* Look for the suffix identifier. */
12043 token = cp_lexer_peek_token (parser->lexer);
12044 if (token->type == CPP_NAME)
12046 id = cp_parser_identifier (parser);
12047 if (id != error_mark_node)
12049 const char *name = IDENTIFIER_POINTER (id);
12050 return cp_literal_operator_id (name);
12053 else
12055 error ("expected suffix identifier");
12056 return error_mark_node;
12059 case CPP_STRING_USERDEF:
12060 error ("missing space between %<\"\"%> and suffix identifier");
12061 return error_mark_node;
12063 default:
12064 /* Anything else is an error. */
12065 break;
12068 /* If we have selected an identifier, we need to consume the
12069 operator token. */
12070 if (id)
12071 cp_lexer_consume_token (parser->lexer);
12072 /* Otherwise, no valid operator name was present. */
12073 else
12075 cp_parser_error (parser, "expected operator");
12076 id = error_mark_node;
12079 return id;
12082 /* Parse a template-declaration.
12084 template-declaration:
12085 export [opt] template < template-parameter-list > declaration
12087 If MEMBER_P is TRUE, this template-declaration occurs within a
12088 class-specifier.
12090 The grammar rule given by the standard isn't correct. What
12091 is really meant is:
12093 template-declaration:
12094 export [opt] template-parameter-list-seq
12095 decl-specifier-seq [opt] init-declarator [opt] ;
12096 export [opt] template-parameter-list-seq
12097 function-definition
12099 template-parameter-list-seq:
12100 template-parameter-list-seq [opt]
12101 template < template-parameter-list > */
12103 static void
12104 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12106 /* Check for `export'. */
12107 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12109 /* Consume the `export' token. */
12110 cp_lexer_consume_token (parser->lexer);
12111 /* Warn that we do not support `export'. */
12112 warning (0, "keyword %<export%> not implemented, and will be ignored");
12115 cp_parser_template_declaration_after_export (parser, member_p);
12118 /* Parse a template-parameter-list.
12120 template-parameter-list:
12121 template-parameter
12122 template-parameter-list , template-parameter
12124 Returns a TREE_LIST. Each node represents a template parameter.
12125 The nodes are connected via their TREE_CHAINs. */
12127 static tree
12128 cp_parser_template_parameter_list (cp_parser* parser)
12130 tree parameter_list = NULL_TREE;
12132 begin_template_parm_list ();
12134 /* The loop below parses the template parms. We first need to know
12135 the total number of template parms to be able to compute proper
12136 canonical types of each dependent type. So after the loop, when
12137 we know the total number of template parms,
12138 end_template_parm_list computes the proper canonical types and
12139 fixes up the dependent types accordingly. */
12140 while (true)
12142 tree parameter;
12143 bool is_non_type;
12144 bool is_parameter_pack;
12145 location_t parm_loc;
12147 /* Parse the template-parameter. */
12148 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12149 parameter = cp_parser_template_parameter (parser,
12150 &is_non_type,
12151 &is_parameter_pack);
12152 /* Add it to the list. */
12153 if (parameter != error_mark_node)
12154 parameter_list = process_template_parm (parameter_list,
12155 parm_loc,
12156 parameter,
12157 is_non_type,
12158 is_parameter_pack);
12159 else
12161 tree err_parm = build_tree_list (parameter, parameter);
12162 parameter_list = chainon (parameter_list, err_parm);
12165 /* If the next token is not a `,', we're done. */
12166 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12167 break;
12168 /* Otherwise, consume the `,' token. */
12169 cp_lexer_consume_token (parser->lexer);
12172 return end_template_parm_list (parameter_list);
12175 /* Parse a template-parameter.
12177 template-parameter:
12178 type-parameter
12179 parameter-declaration
12181 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12182 the parameter. The TREE_PURPOSE is the default value, if any.
12183 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12184 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12185 set to true iff this parameter is a parameter pack. */
12187 static tree
12188 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12189 bool *is_parameter_pack)
12191 cp_token *token;
12192 cp_parameter_declarator *parameter_declarator;
12193 cp_declarator *id_declarator;
12194 tree parm;
12196 /* Assume it is a type parameter or a template parameter. */
12197 *is_non_type = false;
12198 /* Assume it not a parameter pack. */
12199 *is_parameter_pack = false;
12200 /* Peek at the next token. */
12201 token = cp_lexer_peek_token (parser->lexer);
12202 /* If it is `class' or `template', we have a type-parameter. */
12203 if (token->keyword == RID_TEMPLATE)
12204 return cp_parser_type_parameter (parser, is_parameter_pack);
12205 /* If it is `class' or `typename' we do not know yet whether it is a
12206 type parameter or a non-type parameter. Consider:
12208 template <typename T, typename T::X X> ...
12212 template <class C, class D*> ...
12214 Here, the first parameter is a type parameter, and the second is
12215 a non-type parameter. We can tell by looking at the token after
12216 the identifier -- if it is a `,', `=', or `>' then we have a type
12217 parameter. */
12218 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12220 /* Peek at the token after `class' or `typename'. */
12221 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12222 /* If it's an ellipsis, we have a template type parameter
12223 pack. */
12224 if (token->type == CPP_ELLIPSIS)
12225 return cp_parser_type_parameter (parser, is_parameter_pack);
12226 /* If it's an identifier, skip it. */
12227 if (token->type == CPP_NAME)
12228 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12229 /* Now, see if the token looks like the end of a template
12230 parameter. */
12231 if (token->type == CPP_COMMA
12232 || token->type == CPP_EQ
12233 || token->type == CPP_GREATER)
12234 return cp_parser_type_parameter (parser, is_parameter_pack);
12237 /* Otherwise, it is a non-type parameter.
12239 [temp.param]
12241 When parsing a default template-argument for a non-type
12242 template-parameter, the first non-nested `>' is taken as the end
12243 of the template parameter-list rather than a greater-than
12244 operator. */
12245 *is_non_type = true;
12246 parameter_declarator
12247 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12248 /*parenthesized_p=*/NULL);
12250 /* If the parameter declaration is marked as a parameter pack, set
12251 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12252 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12253 grokdeclarator. */
12254 if (parameter_declarator
12255 && parameter_declarator->declarator
12256 && parameter_declarator->declarator->parameter_pack_p)
12258 *is_parameter_pack = true;
12259 parameter_declarator->declarator->parameter_pack_p = false;
12262 if (parameter_declarator
12263 && parameter_declarator->default_argument)
12265 /* Can happen in some cases of erroneous input (c++/34892). */
12266 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12267 /* Consume the `...' for better error recovery. */
12268 cp_lexer_consume_token (parser->lexer);
12270 /* If the next token is an ellipsis, and we don't already have it
12271 marked as a parameter pack, then we have a parameter pack (that
12272 has no declarator). */
12273 else if (!*is_parameter_pack
12274 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12275 && (declarator_can_be_parameter_pack
12276 (parameter_declarator->declarator)))
12278 /* Consume the `...'. */
12279 cp_lexer_consume_token (parser->lexer);
12280 maybe_warn_variadic_templates ();
12282 *is_parameter_pack = true;
12284 /* We might end up with a pack expansion as the type of the non-type
12285 template parameter, in which case this is a non-type template
12286 parameter pack. */
12287 else if (parameter_declarator
12288 && parameter_declarator->decl_specifiers.type
12289 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12291 *is_parameter_pack = true;
12292 parameter_declarator->decl_specifiers.type =
12293 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12296 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12298 /* Parameter packs cannot have default arguments. However, a
12299 user may try to do so, so we'll parse them and give an
12300 appropriate diagnostic here. */
12302 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12304 /* Find the name of the parameter pack. */
12305 id_declarator = parameter_declarator->declarator;
12306 while (id_declarator && id_declarator->kind != cdk_id)
12307 id_declarator = id_declarator->declarator;
12309 if (id_declarator && id_declarator->kind == cdk_id)
12310 error_at (start_token->location,
12311 "template parameter pack %qD cannot have a default argument",
12312 id_declarator->u.id.unqualified_name);
12313 else
12314 error_at (start_token->location,
12315 "template parameter pack cannot have a default argument");
12317 /* Parse the default argument, but throw away the result. */
12318 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12321 parm = grokdeclarator (parameter_declarator->declarator,
12322 &parameter_declarator->decl_specifiers,
12323 TPARM, /*initialized=*/0,
12324 /*attrlist=*/NULL);
12325 if (parm == error_mark_node)
12326 return error_mark_node;
12328 return build_tree_list (parameter_declarator->default_argument, parm);
12331 /* Parse a type-parameter.
12333 type-parameter:
12334 class identifier [opt]
12335 class identifier [opt] = type-id
12336 typename identifier [opt]
12337 typename identifier [opt] = type-id
12338 template < template-parameter-list > class identifier [opt]
12339 template < template-parameter-list > class identifier [opt]
12340 = id-expression
12342 GNU Extension (variadic templates):
12344 type-parameter:
12345 class ... identifier [opt]
12346 typename ... identifier [opt]
12348 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12349 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12350 the declaration of the parameter.
12352 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12354 static tree
12355 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12357 cp_token *token;
12358 tree parameter;
12360 /* Look for a keyword to tell us what kind of parameter this is. */
12361 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12362 if (!token)
12363 return error_mark_node;
12365 switch (token->keyword)
12367 case RID_CLASS:
12368 case RID_TYPENAME:
12370 tree identifier;
12371 tree default_argument;
12373 /* If the next token is an ellipsis, we have a template
12374 argument pack. */
12375 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12377 /* Consume the `...' token. */
12378 cp_lexer_consume_token (parser->lexer);
12379 maybe_warn_variadic_templates ();
12381 *is_parameter_pack = true;
12384 /* If the next token is an identifier, then it names the
12385 parameter. */
12386 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12387 identifier = cp_parser_identifier (parser);
12388 else
12389 identifier = NULL_TREE;
12391 /* Create the parameter. */
12392 parameter = finish_template_type_parm (class_type_node, identifier);
12394 /* If the next token is an `=', we have a default argument. */
12395 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12397 /* Consume the `=' token. */
12398 cp_lexer_consume_token (parser->lexer);
12399 /* Parse the default-argument. */
12400 push_deferring_access_checks (dk_no_deferred);
12401 default_argument = cp_parser_type_id (parser);
12403 /* Template parameter packs cannot have default
12404 arguments. */
12405 if (*is_parameter_pack)
12407 if (identifier)
12408 error_at (token->location,
12409 "template parameter pack %qD cannot have a "
12410 "default argument", identifier);
12411 else
12412 error_at (token->location,
12413 "template parameter packs cannot have "
12414 "default arguments");
12415 default_argument = NULL_TREE;
12417 pop_deferring_access_checks ();
12419 else
12420 default_argument = NULL_TREE;
12422 /* Create the combined representation of the parameter and the
12423 default argument. */
12424 parameter = build_tree_list (default_argument, parameter);
12426 break;
12428 case RID_TEMPLATE:
12430 tree identifier;
12431 tree default_argument;
12433 /* Look for the `<'. */
12434 cp_parser_require (parser, CPP_LESS, RT_LESS);
12435 /* Parse the template-parameter-list. */
12436 cp_parser_template_parameter_list (parser);
12437 /* Look for the `>'. */
12438 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12439 /* Look for the `class' keyword. */
12440 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12441 /* If the next token is an ellipsis, we have a template
12442 argument pack. */
12443 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12445 /* Consume the `...' token. */
12446 cp_lexer_consume_token (parser->lexer);
12447 maybe_warn_variadic_templates ();
12449 *is_parameter_pack = true;
12451 /* If the next token is an `=', then there is a
12452 default-argument. If the next token is a `>', we are at
12453 the end of the parameter-list. If the next token is a `,',
12454 then we are at the end of this parameter. */
12455 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12456 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12457 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12459 identifier = cp_parser_identifier (parser);
12460 /* Treat invalid names as if the parameter were nameless. */
12461 if (identifier == error_mark_node)
12462 identifier = NULL_TREE;
12464 else
12465 identifier = NULL_TREE;
12467 /* Create the template parameter. */
12468 parameter = finish_template_template_parm (class_type_node,
12469 identifier);
12471 /* If the next token is an `=', then there is a
12472 default-argument. */
12473 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12475 bool is_template;
12477 /* Consume the `='. */
12478 cp_lexer_consume_token (parser->lexer);
12479 /* Parse the id-expression. */
12480 push_deferring_access_checks (dk_no_deferred);
12481 /* save token before parsing the id-expression, for error
12482 reporting */
12483 token = cp_lexer_peek_token (parser->lexer);
12484 default_argument
12485 = cp_parser_id_expression (parser,
12486 /*template_keyword_p=*/false,
12487 /*check_dependency_p=*/true,
12488 /*template_p=*/&is_template,
12489 /*declarator_p=*/false,
12490 /*optional_p=*/false);
12491 if (TREE_CODE (default_argument) == TYPE_DECL)
12492 /* If the id-expression was a template-id that refers to
12493 a template-class, we already have the declaration here,
12494 so no further lookup is needed. */
12496 else
12497 /* Look up the name. */
12498 default_argument
12499 = cp_parser_lookup_name (parser, default_argument,
12500 none_type,
12501 /*is_template=*/is_template,
12502 /*is_namespace=*/false,
12503 /*check_dependency=*/true,
12504 /*ambiguous_decls=*/NULL,
12505 token->location);
12506 /* See if the default argument is valid. */
12507 default_argument
12508 = check_template_template_default_arg (default_argument);
12510 /* Template parameter packs cannot have default
12511 arguments. */
12512 if (*is_parameter_pack)
12514 if (identifier)
12515 error_at (token->location,
12516 "template parameter pack %qD cannot "
12517 "have a default argument",
12518 identifier);
12519 else
12520 error_at (token->location, "template parameter packs cannot "
12521 "have default arguments");
12522 default_argument = NULL_TREE;
12524 pop_deferring_access_checks ();
12526 else
12527 default_argument = NULL_TREE;
12529 /* Create the combined representation of the parameter and the
12530 default argument. */
12531 parameter = build_tree_list (default_argument, parameter);
12533 break;
12535 default:
12536 gcc_unreachable ();
12537 break;
12540 return parameter;
12543 /* Parse a template-id.
12545 template-id:
12546 template-name < template-argument-list [opt] >
12548 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12549 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12550 returned. Otherwise, if the template-name names a function, or set
12551 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12552 names a class, returns a TYPE_DECL for the specialization.
12554 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12555 uninstantiated templates. */
12557 static tree
12558 cp_parser_template_id (cp_parser *parser,
12559 bool template_keyword_p,
12560 bool check_dependency_p,
12561 enum tag_types tag_type,
12562 bool is_declaration)
12564 int i;
12565 tree templ;
12566 tree arguments;
12567 tree template_id;
12568 cp_token_position start_of_id = 0;
12569 deferred_access_check *chk;
12570 vec<deferred_access_check, va_gc> *access_check;
12571 cp_token *next_token = NULL, *next_token_2 = NULL;
12572 bool is_identifier;
12574 /* If the next token corresponds to a template-id, there is no need
12575 to reparse it. */
12576 next_token = cp_lexer_peek_token (parser->lexer);
12577 if (next_token->type == CPP_TEMPLATE_ID)
12579 struct tree_check *check_value;
12581 /* Get the stored value. */
12582 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12583 /* Perform any access checks that were deferred. */
12584 access_check = check_value->checks;
12585 if (access_check)
12587 FOR_EACH_VEC_ELT (*access_check, i, chk)
12588 perform_or_defer_access_check (chk->binfo,
12589 chk->decl,
12590 chk->diag_decl,
12591 tf_warning_or_error);
12593 /* Return the stored value. */
12594 return check_value->value;
12597 /* Avoid performing name lookup if there is no possibility of
12598 finding a template-id. */
12599 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12600 || (next_token->type == CPP_NAME
12601 && !cp_parser_nth_token_starts_template_argument_list_p
12602 (parser, 2)))
12604 cp_parser_error (parser, "expected template-id");
12605 return error_mark_node;
12608 /* Remember where the template-id starts. */
12609 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12610 start_of_id = cp_lexer_token_position (parser->lexer, false);
12612 push_deferring_access_checks (dk_deferred);
12614 /* Parse the template-name. */
12615 is_identifier = false;
12616 templ = cp_parser_template_name (parser, template_keyword_p,
12617 check_dependency_p,
12618 is_declaration,
12619 tag_type,
12620 &is_identifier);
12621 if (templ == error_mark_node || is_identifier)
12623 pop_deferring_access_checks ();
12624 return templ;
12627 /* If we find the sequence `[:' after a template-name, it's probably
12628 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12629 parse correctly the argument list. */
12630 next_token = cp_lexer_peek_token (parser->lexer);
12631 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12632 if (next_token->type == CPP_OPEN_SQUARE
12633 && next_token->flags & DIGRAPH
12634 && next_token_2->type == CPP_COLON
12635 && !(next_token_2->flags & PREV_WHITE))
12637 cp_parser_parse_tentatively (parser);
12638 /* Change `:' into `::'. */
12639 next_token_2->type = CPP_SCOPE;
12640 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12641 CPP_LESS. */
12642 cp_lexer_consume_token (parser->lexer);
12644 /* Parse the arguments. */
12645 arguments = cp_parser_enclosed_template_argument_list (parser);
12646 if (!cp_parser_parse_definitely (parser))
12648 /* If we couldn't parse an argument list, then we revert our changes
12649 and return simply an error. Maybe this is not a template-id
12650 after all. */
12651 next_token_2->type = CPP_COLON;
12652 cp_parser_error (parser, "expected %<<%>");
12653 pop_deferring_access_checks ();
12654 return error_mark_node;
12656 /* Otherwise, emit an error about the invalid digraph, but continue
12657 parsing because we got our argument list. */
12658 if (permerror (next_token->location,
12659 "%<<::%> cannot begin a template-argument list"))
12661 static bool hint = false;
12662 inform (next_token->location,
12663 "%<<:%> is an alternate spelling for %<[%>."
12664 " Insert whitespace between %<<%> and %<::%>");
12665 if (!hint && !flag_permissive)
12667 inform (next_token->location, "(if you use %<-fpermissive%> "
12668 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
12669 "accept your code)");
12670 hint = true;
12674 else
12676 /* Look for the `<' that starts the template-argument-list. */
12677 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12679 pop_deferring_access_checks ();
12680 return error_mark_node;
12682 /* Parse the arguments. */
12683 arguments = cp_parser_enclosed_template_argument_list (parser);
12686 /* Build a representation of the specialization. */
12687 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12688 template_id = build_min_nt_loc (next_token->location,
12689 TEMPLATE_ID_EXPR,
12690 templ, arguments);
12691 else if (DECL_TYPE_TEMPLATE_P (templ)
12692 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12694 bool entering_scope;
12695 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12696 template (rather than some instantiation thereof) only if
12697 is not nested within some other construct. For example, in
12698 "template <typename T> void f(T) { A<T>::", A<T> is just an
12699 instantiation of A. */
12700 entering_scope = (template_parm_scope_p ()
12701 && cp_lexer_next_token_is (parser->lexer,
12702 CPP_SCOPE));
12703 template_id
12704 = finish_template_type (templ, arguments, entering_scope);
12706 else
12708 /* If it's not a class-template or a template-template, it should be
12709 a function-template. */
12710 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12711 || TREE_CODE (templ) == OVERLOAD
12712 || BASELINK_P (templ)));
12714 template_id = lookup_template_function (templ, arguments);
12717 /* If parsing tentatively, replace the sequence of tokens that makes
12718 up the template-id with a CPP_TEMPLATE_ID token. That way,
12719 should we re-parse the token stream, we will not have to repeat
12720 the effort required to do the parse, nor will we issue duplicate
12721 error messages about problems during instantiation of the
12722 template. */
12723 if (start_of_id)
12725 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12727 /* Reset the contents of the START_OF_ID token. */
12728 token->type = CPP_TEMPLATE_ID;
12729 /* Retrieve any deferred checks. Do not pop this access checks yet
12730 so the memory will not be reclaimed during token replacing below. */
12731 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12732 token->u.tree_check_value->value = template_id;
12733 token->u.tree_check_value->checks = get_deferred_access_checks ();
12734 token->keyword = RID_MAX;
12736 /* Purge all subsequent tokens. */
12737 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12739 /* ??? Can we actually assume that, if template_id ==
12740 error_mark_node, we will have issued a diagnostic to the
12741 user, as opposed to simply marking the tentative parse as
12742 failed? */
12743 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12744 error_at (token->location, "parse error in template argument list");
12747 pop_deferring_access_checks ();
12748 return template_id;
12751 /* Parse a template-name.
12753 template-name:
12754 identifier
12756 The standard should actually say:
12758 template-name:
12759 identifier
12760 operator-function-id
12762 A defect report has been filed about this issue.
12764 A conversion-function-id cannot be a template name because they cannot
12765 be part of a template-id. In fact, looking at this code:
12767 a.operator K<int>()
12769 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12770 It is impossible to call a templated conversion-function-id with an
12771 explicit argument list, since the only allowed template parameter is
12772 the type to which it is converting.
12774 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12775 `template' keyword, in a construction like:
12777 T::template f<3>()
12779 In that case `f' is taken to be a template-name, even though there
12780 is no way of knowing for sure.
12782 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12783 name refers to a set of overloaded functions, at least one of which
12784 is a template, or an IDENTIFIER_NODE with the name of the template,
12785 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12786 names are looked up inside uninstantiated templates. */
12788 static tree
12789 cp_parser_template_name (cp_parser* parser,
12790 bool template_keyword_p,
12791 bool check_dependency_p,
12792 bool is_declaration,
12793 enum tag_types tag_type,
12794 bool *is_identifier)
12796 tree identifier;
12797 tree decl;
12798 tree fns;
12799 cp_token *token = cp_lexer_peek_token (parser->lexer);
12801 /* If the next token is `operator', then we have either an
12802 operator-function-id or a conversion-function-id. */
12803 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12805 /* We don't know whether we're looking at an
12806 operator-function-id or a conversion-function-id. */
12807 cp_parser_parse_tentatively (parser);
12808 /* Try an operator-function-id. */
12809 identifier = cp_parser_operator_function_id (parser);
12810 /* If that didn't work, try a conversion-function-id. */
12811 if (!cp_parser_parse_definitely (parser))
12813 cp_parser_error (parser, "expected template-name");
12814 return error_mark_node;
12817 /* Look for the identifier. */
12818 else
12819 identifier = cp_parser_identifier (parser);
12821 /* If we didn't find an identifier, we don't have a template-id. */
12822 if (identifier == error_mark_node)
12823 return error_mark_node;
12825 /* If the name immediately followed the `template' keyword, then it
12826 is a template-name. However, if the next token is not `<', then
12827 we do not treat it as a template-name, since it is not being used
12828 as part of a template-id. This enables us to handle constructs
12829 like:
12831 template <typename T> struct S { S(); };
12832 template <typename T> S<T>::S();
12834 correctly. We would treat `S' as a template -- if it were `S<T>'
12835 -- but we do not if there is no `<'. */
12837 if (processing_template_decl
12838 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12840 /* In a declaration, in a dependent context, we pretend that the
12841 "template" keyword was present in order to improve error
12842 recovery. For example, given:
12844 template <typename T> void f(T::X<int>);
12846 we want to treat "X<int>" as a template-id. */
12847 if (is_declaration
12848 && !template_keyword_p
12849 && parser->scope && TYPE_P (parser->scope)
12850 && check_dependency_p
12851 && dependent_scope_p (parser->scope)
12852 /* Do not do this for dtors (or ctors), since they never
12853 need the template keyword before their name. */
12854 && !constructor_name_p (identifier, parser->scope))
12856 cp_token_position start = 0;
12858 /* Explain what went wrong. */
12859 error_at (token->location, "non-template %qD used as template",
12860 identifier);
12861 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12862 parser->scope, identifier);
12863 /* If parsing tentatively, find the location of the "<" token. */
12864 if (cp_parser_simulate_error (parser))
12865 start = cp_lexer_token_position (parser->lexer, true);
12866 /* Parse the template arguments so that we can issue error
12867 messages about them. */
12868 cp_lexer_consume_token (parser->lexer);
12869 cp_parser_enclosed_template_argument_list (parser);
12870 /* Skip tokens until we find a good place from which to
12871 continue parsing. */
12872 cp_parser_skip_to_closing_parenthesis (parser,
12873 /*recovering=*/true,
12874 /*or_comma=*/true,
12875 /*consume_paren=*/false);
12876 /* If parsing tentatively, permanently remove the
12877 template argument list. That will prevent duplicate
12878 error messages from being issued about the missing
12879 "template" keyword. */
12880 if (start)
12881 cp_lexer_purge_tokens_after (parser->lexer, start);
12882 if (is_identifier)
12883 *is_identifier = true;
12884 return identifier;
12887 /* If the "template" keyword is present, then there is generally
12888 no point in doing name-lookup, so we just return IDENTIFIER.
12889 But, if the qualifying scope is non-dependent then we can
12890 (and must) do name-lookup normally. */
12891 if (template_keyword_p
12892 && (!parser->scope
12893 || (TYPE_P (parser->scope)
12894 && dependent_type_p (parser->scope))))
12895 return identifier;
12898 /* Look up the name. */
12899 decl = cp_parser_lookup_name (parser, identifier,
12900 tag_type,
12901 /*is_template=*/true,
12902 /*is_namespace=*/false,
12903 check_dependency_p,
12904 /*ambiguous_decls=*/NULL,
12905 token->location);
12907 /* If DECL is a template, then the name was a template-name. */
12908 if (TREE_CODE (decl) == TEMPLATE_DECL)
12910 else
12912 tree fn = NULL_TREE;
12914 /* The standard does not explicitly indicate whether a name that
12915 names a set of overloaded declarations, some of which are
12916 templates, is a template-name. However, such a name should
12917 be a template-name; otherwise, there is no way to form a
12918 template-id for the overloaded templates. */
12919 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12920 if (TREE_CODE (fns) == OVERLOAD)
12921 for (fn = fns; fn; fn = OVL_NEXT (fn))
12922 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12923 break;
12925 if (!fn)
12927 /* The name does not name a template. */
12928 cp_parser_error (parser, "expected template-name");
12929 return error_mark_node;
12933 /* If DECL is dependent, and refers to a function, then just return
12934 its name; we will look it up again during template instantiation. */
12935 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12937 tree scope = ovl_scope (decl);
12938 if (TYPE_P (scope) && dependent_type_p (scope))
12939 return identifier;
12942 return decl;
12945 /* Parse a template-argument-list.
12947 template-argument-list:
12948 template-argument ... [opt]
12949 template-argument-list , template-argument ... [opt]
12951 Returns a TREE_VEC containing the arguments. */
12953 static tree
12954 cp_parser_template_argument_list (cp_parser* parser)
12956 tree fixed_args[10];
12957 unsigned n_args = 0;
12958 unsigned alloced = 10;
12959 tree *arg_ary = fixed_args;
12960 tree vec;
12961 bool saved_in_template_argument_list_p;
12962 bool saved_ice_p;
12963 bool saved_non_ice_p;
12965 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12966 parser->in_template_argument_list_p = true;
12967 /* Even if the template-id appears in an integral
12968 constant-expression, the contents of the argument list do
12969 not. */
12970 saved_ice_p = parser->integral_constant_expression_p;
12971 parser->integral_constant_expression_p = false;
12972 saved_non_ice_p = parser->non_integral_constant_expression_p;
12973 parser->non_integral_constant_expression_p = false;
12975 /* Parse the arguments. */
12978 tree argument;
12980 if (n_args)
12981 /* Consume the comma. */
12982 cp_lexer_consume_token (parser->lexer);
12984 /* Parse the template-argument. */
12985 argument = cp_parser_template_argument (parser);
12987 /* If the next token is an ellipsis, we're expanding a template
12988 argument pack. */
12989 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12991 if (argument == error_mark_node)
12993 cp_token *token = cp_lexer_peek_token (parser->lexer);
12994 error_at (token->location,
12995 "expected parameter pack before %<...%>");
12997 /* Consume the `...' token. */
12998 cp_lexer_consume_token (parser->lexer);
13000 /* Make the argument into a TYPE_PACK_EXPANSION or
13001 EXPR_PACK_EXPANSION. */
13002 argument = make_pack_expansion (argument);
13005 if (n_args == alloced)
13007 alloced *= 2;
13009 if (arg_ary == fixed_args)
13011 arg_ary = XNEWVEC (tree, alloced);
13012 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13014 else
13015 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13017 arg_ary[n_args++] = argument;
13019 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13021 vec = make_tree_vec (n_args);
13023 while (n_args--)
13024 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13026 if (arg_ary != fixed_args)
13027 free (arg_ary);
13028 parser->non_integral_constant_expression_p = saved_non_ice_p;
13029 parser->integral_constant_expression_p = saved_ice_p;
13030 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13031 #ifdef ENABLE_CHECKING
13032 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13033 #endif
13034 return vec;
13037 /* Parse a template-argument.
13039 template-argument:
13040 assignment-expression
13041 type-id
13042 id-expression
13044 The representation is that of an assignment-expression, type-id, or
13045 id-expression -- except that the qualified id-expression is
13046 evaluated, so that the value returned is either a DECL or an
13047 OVERLOAD.
13049 Although the standard says "assignment-expression", it forbids
13050 throw-expressions or assignments in the template argument.
13051 Therefore, we use "conditional-expression" instead. */
13053 static tree
13054 cp_parser_template_argument (cp_parser* parser)
13056 tree argument;
13057 bool template_p;
13058 bool address_p;
13059 bool maybe_type_id = false;
13060 cp_token *token = NULL, *argument_start_token = NULL;
13061 location_t loc = 0;
13062 cp_id_kind idk;
13064 /* There's really no way to know what we're looking at, so we just
13065 try each alternative in order.
13067 [temp.arg]
13069 In a template-argument, an ambiguity between a type-id and an
13070 expression is resolved to a type-id, regardless of the form of
13071 the corresponding template-parameter.
13073 Therefore, we try a type-id first. */
13074 cp_parser_parse_tentatively (parser);
13075 argument = cp_parser_template_type_arg (parser);
13076 /* If there was no error parsing the type-id but the next token is a
13077 '>>', our behavior depends on which dialect of C++ we're
13078 parsing. In C++98, we probably found a typo for '> >'. But there
13079 are type-id which are also valid expressions. For instance:
13081 struct X { int operator >> (int); };
13082 template <int V> struct Foo {};
13083 Foo<X () >> 5> r;
13085 Here 'X()' is a valid type-id of a function type, but the user just
13086 wanted to write the expression "X() >> 5". Thus, we remember that we
13087 found a valid type-id, but we still try to parse the argument as an
13088 expression to see what happens.
13090 In C++0x, the '>>' will be considered two separate '>'
13091 tokens. */
13092 if (!cp_parser_error_occurred (parser)
13093 && cxx_dialect == cxx98
13094 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13096 maybe_type_id = true;
13097 cp_parser_abort_tentative_parse (parser);
13099 else
13101 /* If the next token isn't a `,' or a `>', then this argument wasn't
13102 really finished. This means that the argument is not a valid
13103 type-id. */
13104 if (!cp_parser_next_token_ends_template_argument_p (parser))
13105 cp_parser_error (parser, "expected template-argument");
13106 /* If that worked, we're done. */
13107 if (cp_parser_parse_definitely (parser))
13108 return argument;
13110 /* We're still not sure what the argument will be. */
13111 cp_parser_parse_tentatively (parser);
13112 /* Try a template. */
13113 argument_start_token = cp_lexer_peek_token (parser->lexer);
13114 argument = cp_parser_id_expression (parser,
13115 /*template_keyword_p=*/false,
13116 /*check_dependency_p=*/true,
13117 &template_p,
13118 /*declarator_p=*/false,
13119 /*optional_p=*/false);
13120 /* If the next token isn't a `,' or a `>', then this argument wasn't
13121 really finished. */
13122 if (!cp_parser_next_token_ends_template_argument_p (parser))
13123 cp_parser_error (parser, "expected template-argument");
13124 if (!cp_parser_error_occurred (parser))
13126 /* Figure out what is being referred to. If the id-expression
13127 was for a class template specialization, then we will have a
13128 TYPE_DECL at this point. There is no need to do name lookup
13129 at this point in that case. */
13130 if (TREE_CODE (argument) != TYPE_DECL)
13131 argument = cp_parser_lookup_name (parser, argument,
13132 none_type,
13133 /*is_template=*/template_p,
13134 /*is_namespace=*/false,
13135 /*check_dependency=*/true,
13136 /*ambiguous_decls=*/NULL,
13137 argument_start_token->location);
13138 if (TREE_CODE (argument) != TEMPLATE_DECL
13139 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13140 cp_parser_error (parser, "expected template-name");
13142 if (cp_parser_parse_definitely (parser))
13143 return argument;
13144 /* It must be a non-type argument. There permitted cases are given
13145 in [temp.arg.nontype]:
13147 -- an integral constant-expression of integral or enumeration
13148 type; or
13150 -- the name of a non-type template-parameter; or
13152 -- the name of an object or function with external linkage...
13154 -- the address of an object or function with external linkage...
13156 -- a pointer to member... */
13157 /* Look for a non-type template parameter. */
13158 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13160 cp_parser_parse_tentatively (parser);
13161 argument = cp_parser_primary_expression (parser,
13162 /*address_p=*/false,
13163 /*cast_p=*/false,
13164 /*template_arg_p=*/true,
13165 &idk);
13166 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13167 || !cp_parser_next_token_ends_template_argument_p (parser))
13168 cp_parser_simulate_error (parser);
13169 if (cp_parser_parse_definitely (parser))
13170 return argument;
13173 /* If the next token is "&", the argument must be the address of an
13174 object or function with external linkage. */
13175 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13176 if (address_p)
13178 loc = cp_lexer_peek_token (parser->lexer)->location;
13179 cp_lexer_consume_token (parser->lexer);
13181 /* See if we might have an id-expression. */
13182 token = cp_lexer_peek_token (parser->lexer);
13183 if (token->type == CPP_NAME
13184 || token->keyword == RID_OPERATOR
13185 || token->type == CPP_SCOPE
13186 || token->type == CPP_TEMPLATE_ID
13187 || token->type == CPP_NESTED_NAME_SPECIFIER)
13189 cp_parser_parse_tentatively (parser);
13190 argument = cp_parser_primary_expression (parser,
13191 address_p,
13192 /*cast_p=*/false,
13193 /*template_arg_p=*/true,
13194 &idk);
13195 if (cp_parser_error_occurred (parser)
13196 || !cp_parser_next_token_ends_template_argument_p (parser))
13197 cp_parser_abort_tentative_parse (parser);
13198 else
13200 tree probe;
13202 if (TREE_CODE (argument) == INDIRECT_REF)
13204 gcc_assert (REFERENCE_REF_P (argument));
13205 argument = TREE_OPERAND (argument, 0);
13208 /* If we're in a template, we represent a qualified-id referring
13209 to a static data member as a SCOPE_REF even if the scope isn't
13210 dependent so that we can check access control later. */
13211 probe = argument;
13212 if (TREE_CODE (probe) == SCOPE_REF)
13213 probe = TREE_OPERAND (probe, 1);
13214 if (TREE_CODE (probe) == VAR_DECL)
13216 /* A variable without external linkage might still be a
13217 valid constant-expression, so no error is issued here
13218 if the external-linkage check fails. */
13219 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13220 cp_parser_simulate_error (parser);
13222 else if (is_overloaded_fn (argument))
13223 /* All overloaded functions are allowed; if the external
13224 linkage test does not pass, an error will be issued
13225 later. */
13227 else if (address_p
13228 && (TREE_CODE (argument) == OFFSET_REF
13229 || TREE_CODE (argument) == SCOPE_REF))
13230 /* A pointer-to-member. */
13232 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13234 else
13235 cp_parser_simulate_error (parser);
13237 if (cp_parser_parse_definitely (parser))
13239 if (address_p)
13240 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13241 tf_warning_or_error);
13242 return argument;
13246 /* If the argument started with "&", there are no other valid
13247 alternatives at this point. */
13248 if (address_p)
13250 cp_parser_error (parser, "invalid non-type template argument");
13251 return error_mark_node;
13254 /* If the argument wasn't successfully parsed as a type-id followed
13255 by '>>', the argument can only be a constant expression now.
13256 Otherwise, we try parsing the constant-expression tentatively,
13257 because the argument could really be a type-id. */
13258 if (maybe_type_id)
13259 cp_parser_parse_tentatively (parser);
13260 argument = cp_parser_constant_expression (parser,
13261 /*allow_non_constant_p=*/false,
13262 /*non_constant_p=*/NULL);
13263 argument = fold_non_dependent_expr (argument);
13264 if (!maybe_type_id)
13265 return argument;
13266 if (!cp_parser_next_token_ends_template_argument_p (parser))
13267 cp_parser_error (parser, "expected template-argument");
13268 if (cp_parser_parse_definitely (parser))
13269 return argument;
13270 /* We did our best to parse the argument as a non type-id, but that
13271 was the only alternative that matched (albeit with a '>' after
13272 it). We can assume it's just a typo from the user, and a
13273 diagnostic will then be issued. */
13274 return cp_parser_template_type_arg (parser);
13277 /* Parse an explicit-instantiation.
13279 explicit-instantiation:
13280 template declaration
13282 Although the standard says `declaration', what it really means is:
13284 explicit-instantiation:
13285 template decl-specifier-seq [opt] declarator [opt] ;
13287 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13288 supposed to be allowed. A defect report has been filed about this
13289 issue.
13291 GNU Extension:
13293 explicit-instantiation:
13294 storage-class-specifier template
13295 decl-specifier-seq [opt] declarator [opt] ;
13296 function-specifier template
13297 decl-specifier-seq [opt] declarator [opt] ; */
13299 static void
13300 cp_parser_explicit_instantiation (cp_parser* parser)
13302 int declares_class_or_enum;
13303 cp_decl_specifier_seq decl_specifiers;
13304 tree extension_specifier = NULL_TREE;
13306 timevar_push (TV_TEMPLATE_INST);
13308 /* Look for an (optional) storage-class-specifier or
13309 function-specifier. */
13310 if (cp_parser_allow_gnu_extensions_p (parser))
13312 extension_specifier
13313 = cp_parser_storage_class_specifier_opt (parser);
13314 if (!extension_specifier)
13315 extension_specifier
13316 = cp_parser_function_specifier_opt (parser,
13317 /*decl_specs=*/NULL);
13320 /* Look for the `template' keyword. */
13321 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13322 /* Let the front end know that we are processing an explicit
13323 instantiation. */
13324 begin_explicit_instantiation ();
13325 /* [temp.explicit] says that we are supposed to ignore access
13326 control while processing explicit instantiation directives. */
13327 push_deferring_access_checks (dk_no_check);
13328 /* Parse a decl-specifier-seq. */
13329 cp_parser_decl_specifier_seq (parser,
13330 CP_PARSER_FLAGS_OPTIONAL,
13331 &decl_specifiers,
13332 &declares_class_or_enum);
13333 /* If there was exactly one decl-specifier, and it declared a class,
13334 and there's no declarator, then we have an explicit type
13335 instantiation. */
13336 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13338 tree type;
13340 type = check_tag_decl (&decl_specifiers,
13341 /*explicit_type_instantiation_p=*/true);
13342 /* Turn access control back on for names used during
13343 template instantiation. */
13344 pop_deferring_access_checks ();
13345 if (type)
13346 do_type_instantiation (type, extension_specifier,
13347 /*complain=*/tf_error);
13349 else
13351 cp_declarator *declarator;
13352 tree decl;
13354 /* Parse the declarator. */
13355 declarator
13356 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13357 /*ctor_dtor_or_conv_p=*/NULL,
13358 /*parenthesized_p=*/NULL,
13359 /*member_p=*/false);
13360 if (declares_class_or_enum & 2)
13361 cp_parser_check_for_definition_in_return_type (declarator,
13362 decl_specifiers.type,
13363 decl_specifiers.locations[ds_type_spec]);
13364 if (declarator != cp_error_declarator)
13366 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13367 permerror (decl_specifiers.locations[ds_inline],
13368 "explicit instantiation shall not use"
13369 " %<inline%> specifier");
13370 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13371 permerror (decl_specifiers.locations[ds_constexpr],
13372 "explicit instantiation shall not use"
13373 " %<constexpr%> specifier");
13375 decl = grokdeclarator (declarator, &decl_specifiers,
13376 NORMAL, 0, &decl_specifiers.attributes);
13377 /* Turn access control back on for names used during
13378 template instantiation. */
13379 pop_deferring_access_checks ();
13380 /* Do the explicit instantiation. */
13381 do_decl_instantiation (decl, extension_specifier);
13383 else
13385 pop_deferring_access_checks ();
13386 /* Skip the body of the explicit instantiation. */
13387 cp_parser_skip_to_end_of_statement (parser);
13390 /* We're done with the instantiation. */
13391 end_explicit_instantiation ();
13393 cp_parser_consume_semicolon_at_end_of_statement (parser);
13395 timevar_pop (TV_TEMPLATE_INST);
13398 /* Parse an explicit-specialization.
13400 explicit-specialization:
13401 template < > declaration
13403 Although the standard says `declaration', what it really means is:
13405 explicit-specialization:
13406 template <> decl-specifier [opt] init-declarator [opt] ;
13407 template <> function-definition
13408 template <> explicit-specialization
13409 template <> template-declaration */
13411 static void
13412 cp_parser_explicit_specialization (cp_parser* parser)
13414 bool need_lang_pop;
13415 cp_token *token = cp_lexer_peek_token (parser->lexer);
13417 /* Look for the `template' keyword. */
13418 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13419 /* Look for the `<'. */
13420 cp_parser_require (parser, CPP_LESS, RT_LESS);
13421 /* Look for the `>'. */
13422 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13423 /* We have processed another parameter list. */
13424 ++parser->num_template_parameter_lists;
13425 /* [temp]
13427 A template ... explicit specialization ... shall not have C
13428 linkage. */
13429 if (current_lang_name == lang_name_c)
13431 error_at (token->location, "template specialization with C linkage");
13432 /* Give it C++ linkage to avoid confusing other parts of the
13433 front end. */
13434 push_lang_context (lang_name_cplusplus);
13435 need_lang_pop = true;
13437 else
13438 need_lang_pop = false;
13439 /* Let the front end know that we are beginning a specialization. */
13440 if (!begin_specialization ())
13442 end_specialization ();
13443 return;
13446 /* If the next keyword is `template', we need to figure out whether
13447 or not we're looking a template-declaration. */
13448 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13450 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13451 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13452 cp_parser_template_declaration_after_export (parser,
13453 /*member_p=*/false);
13454 else
13455 cp_parser_explicit_specialization (parser);
13457 else
13458 /* Parse the dependent declaration. */
13459 cp_parser_single_declaration (parser,
13460 /*checks=*/NULL,
13461 /*member_p=*/false,
13462 /*explicit_specialization_p=*/true,
13463 /*friend_p=*/NULL);
13464 /* We're done with the specialization. */
13465 end_specialization ();
13466 /* For the erroneous case of a template with C linkage, we pushed an
13467 implicit C++ linkage scope; exit that scope now. */
13468 if (need_lang_pop)
13469 pop_lang_context ();
13470 /* We're done with this parameter list. */
13471 --parser->num_template_parameter_lists;
13474 /* Parse a type-specifier.
13476 type-specifier:
13477 simple-type-specifier
13478 class-specifier
13479 enum-specifier
13480 elaborated-type-specifier
13481 cv-qualifier
13483 GNU Extension:
13485 type-specifier:
13486 __complex__
13488 Returns a representation of the type-specifier. For a
13489 class-specifier, enum-specifier, or elaborated-type-specifier, a
13490 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13492 The parser flags FLAGS is used to control type-specifier parsing.
13494 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13495 in a decl-specifier-seq.
13497 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13498 class-specifier, enum-specifier, or elaborated-type-specifier, then
13499 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13500 if a type is declared; 2 if it is defined. Otherwise, it is set to
13501 zero.
13503 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13504 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13505 is set to FALSE. */
13507 static tree
13508 cp_parser_type_specifier (cp_parser* parser,
13509 cp_parser_flags flags,
13510 cp_decl_specifier_seq *decl_specs,
13511 bool is_declaration,
13512 int* declares_class_or_enum,
13513 bool* is_cv_qualifier)
13515 tree type_spec = NULL_TREE;
13516 cp_token *token;
13517 enum rid keyword;
13518 cp_decl_spec ds = ds_last;
13520 /* Assume this type-specifier does not declare a new type. */
13521 if (declares_class_or_enum)
13522 *declares_class_or_enum = 0;
13523 /* And that it does not specify a cv-qualifier. */
13524 if (is_cv_qualifier)
13525 *is_cv_qualifier = false;
13526 /* Peek at the next token. */
13527 token = cp_lexer_peek_token (parser->lexer);
13529 /* If we're looking at a keyword, we can use that to guide the
13530 production we choose. */
13531 keyword = token->keyword;
13532 switch (keyword)
13534 case RID_ENUM:
13535 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13536 goto elaborated_type_specifier;
13538 /* Look for the enum-specifier. */
13539 type_spec = cp_parser_enum_specifier (parser);
13540 /* If that worked, we're done. */
13541 if (type_spec)
13543 if (declares_class_or_enum)
13544 *declares_class_or_enum = 2;
13545 if (decl_specs)
13546 cp_parser_set_decl_spec_type (decl_specs,
13547 type_spec,
13548 token,
13549 /*type_definition_p=*/true);
13550 return type_spec;
13552 else
13553 goto elaborated_type_specifier;
13555 /* Any of these indicate either a class-specifier, or an
13556 elaborated-type-specifier. */
13557 case RID_CLASS:
13558 case RID_STRUCT:
13559 case RID_UNION:
13560 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13561 goto elaborated_type_specifier;
13563 /* Parse tentatively so that we can back up if we don't find a
13564 class-specifier. */
13565 cp_parser_parse_tentatively (parser);
13566 /* Look for the class-specifier. */
13567 type_spec = cp_parser_class_specifier (parser);
13568 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13569 /* If that worked, we're done. */
13570 if (cp_parser_parse_definitely (parser))
13572 if (declares_class_or_enum)
13573 *declares_class_or_enum = 2;
13574 if (decl_specs)
13575 cp_parser_set_decl_spec_type (decl_specs,
13576 type_spec,
13577 token,
13578 /*type_definition_p=*/true);
13579 return type_spec;
13582 /* Fall through. */
13583 elaborated_type_specifier:
13584 /* We're declaring (not defining) a class or enum. */
13585 if (declares_class_or_enum)
13586 *declares_class_or_enum = 1;
13588 /* Fall through. */
13589 case RID_TYPENAME:
13590 /* Look for an elaborated-type-specifier. */
13591 type_spec
13592 = (cp_parser_elaborated_type_specifier
13593 (parser,
13594 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
13595 is_declaration));
13596 if (decl_specs)
13597 cp_parser_set_decl_spec_type (decl_specs,
13598 type_spec,
13599 token,
13600 /*type_definition_p=*/false);
13601 return type_spec;
13603 case RID_CONST:
13604 ds = ds_const;
13605 if (is_cv_qualifier)
13606 *is_cv_qualifier = true;
13607 break;
13609 case RID_VOLATILE:
13610 ds = ds_volatile;
13611 if (is_cv_qualifier)
13612 *is_cv_qualifier = true;
13613 break;
13615 case RID_RESTRICT:
13616 ds = ds_restrict;
13617 if (is_cv_qualifier)
13618 *is_cv_qualifier = true;
13619 break;
13621 case RID_COMPLEX:
13622 /* The `__complex__' keyword is a GNU extension. */
13623 ds = ds_complex;
13624 break;
13626 default:
13627 break;
13630 /* Handle simple keywords. */
13631 if (ds != ds_last)
13633 if (decl_specs)
13635 set_and_check_decl_spec_loc (decl_specs, ds, token);
13636 decl_specs->any_specifiers_p = true;
13638 return cp_lexer_consume_token (parser->lexer)->u.value;
13641 /* If we do not already have a type-specifier, assume we are looking
13642 at a simple-type-specifier. */
13643 type_spec = cp_parser_simple_type_specifier (parser,
13644 decl_specs,
13645 flags);
13647 /* If we didn't find a type-specifier, and a type-specifier was not
13648 optional in this context, issue an error message. */
13649 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13651 cp_parser_error (parser, "expected type specifier");
13652 return error_mark_node;
13655 return type_spec;
13658 /* Parse a simple-type-specifier.
13660 simple-type-specifier:
13661 :: [opt] nested-name-specifier [opt] type-name
13662 :: [opt] nested-name-specifier template template-id
13663 char
13664 wchar_t
13665 bool
13666 short
13668 long
13669 signed
13670 unsigned
13671 float
13672 double
13673 void
13675 C++0x Extension:
13677 simple-type-specifier:
13678 auto
13679 decltype ( expression )
13680 char16_t
13681 char32_t
13682 __underlying_type ( type-id )
13684 GNU Extension:
13686 simple-type-specifier:
13687 __int128
13688 __typeof__ unary-expression
13689 __typeof__ ( type-id )
13691 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13692 appropriately updated. */
13694 static tree
13695 cp_parser_simple_type_specifier (cp_parser* parser,
13696 cp_decl_specifier_seq *decl_specs,
13697 cp_parser_flags flags)
13699 tree type = NULL_TREE;
13700 cp_token *token;
13702 /* Peek at the next token. */
13703 token = cp_lexer_peek_token (parser->lexer);
13705 /* If we're looking at a keyword, things are easy. */
13706 switch (token->keyword)
13708 case RID_CHAR:
13709 if (decl_specs)
13710 decl_specs->explicit_char_p = true;
13711 type = char_type_node;
13712 break;
13713 case RID_CHAR16:
13714 type = char16_type_node;
13715 break;
13716 case RID_CHAR32:
13717 type = char32_type_node;
13718 break;
13719 case RID_WCHAR:
13720 type = wchar_type_node;
13721 break;
13722 case RID_BOOL:
13723 type = boolean_type_node;
13724 break;
13725 case RID_SHORT:
13726 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
13727 type = short_integer_type_node;
13728 break;
13729 case RID_INT:
13730 if (decl_specs)
13731 decl_specs->explicit_int_p = true;
13732 type = integer_type_node;
13733 break;
13734 case RID_INT128:
13735 if (!int128_integer_type_node)
13736 break;
13737 if (decl_specs)
13738 decl_specs->explicit_int128_p = true;
13739 type = int128_integer_type_node;
13740 break;
13741 case RID_LONG:
13742 if (decl_specs)
13743 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
13744 type = long_integer_type_node;
13745 break;
13746 case RID_SIGNED:
13747 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
13748 type = integer_type_node;
13749 break;
13750 case RID_UNSIGNED:
13751 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
13752 type = unsigned_type_node;
13753 break;
13754 case RID_FLOAT:
13755 type = float_type_node;
13756 break;
13757 case RID_DOUBLE:
13758 type = double_type_node;
13759 break;
13760 case RID_VOID:
13761 type = void_type_node;
13762 break;
13764 case RID_AUTO:
13765 maybe_warn_cpp0x (CPP0X_AUTO);
13766 type = make_auto ();
13767 break;
13769 case RID_DECLTYPE:
13770 /* Since DR 743, decltype can either be a simple-type-specifier by
13771 itself or begin a nested-name-specifier. Parsing it will replace
13772 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13773 handling below decide what to do. */
13774 cp_parser_decltype (parser);
13775 cp_lexer_set_token_position (parser->lexer, token);
13776 break;
13778 case RID_TYPEOF:
13779 /* Consume the `typeof' token. */
13780 cp_lexer_consume_token (parser->lexer);
13781 /* Parse the operand to `typeof'. */
13782 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13783 /* If it is not already a TYPE, take its type. */
13784 if (!TYPE_P (type))
13785 type = finish_typeof (type);
13787 if (decl_specs)
13788 cp_parser_set_decl_spec_type (decl_specs, type,
13789 token,
13790 /*type_definition_p=*/false);
13792 return type;
13794 case RID_UNDERLYING_TYPE:
13795 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13796 if (decl_specs)
13797 cp_parser_set_decl_spec_type (decl_specs, type,
13798 token,
13799 /*type_definition_p=*/false);
13801 return type;
13803 case RID_BASES:
13804 case RID_DIRECT_BASES:
13805 type = cp_parser_trait_expr (parser, token->keyword);
13806 if (decl_specs)
13807 cp_parser_set_decl_spec_type (decl_specs, type,
13808 token,
13809 /*type_definition_p=*/false);
13810 return type;
13811 default:
13812 break;
13815 /* If token is an already-parsed decltype not followed by ::,
13816 it's a simple-type-specifier. */
13817 if (token->type == CPP_DECLTYPE
13818 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13820 type = token->u.value;
13821 if (decl_specs)
13822 cp_parser_set_decl_spec_type (decl_specs, type,
13823 token,
13824 /*type_definition_p=*/false);
13825 cp_lexer_consume_token (parser->lexer);
13826 return type;
13829 /* If the type-specifier was for a built-in type, we're done. */
13830 if (type)
13832 /* Record the type. */
13833 if (decl_specs
13834 && (token->keyword != RID_SIGNED
13835 && token->keyword != RID_UNSIGNED
13836 && token->keyword != RID_SHORT
13837 && token->keyword != RID_LONG))
13838 cp_parser_set_decl_spec_type (decl_specs,
13839 type,
13840 token,
13841 /*type_definition_p=*/false);
13842 if (decl_specs)
13843 decl_specs->any_specifiers_p = true;
13845 /* Consume the token. */
13846 cp_lexer_consume_token (parser->lexer);
13848 /* There is no valid C++ program where a non-template type is
13849 followed by a "<". That usually indicates that the user thought
13850 that the type was a template. */
13851 cp_parser_check_for_invalid_template_id (parser, type, none_type,
13852 token->location);
13854 return TYPE_NAME (type);
13857 /* The type-specifier must be a user-defined type. */
13858 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13860 bool qualified_p;
13861 bool global_p;
13863 /* Don't gobble tokens or issue error messages if this is an
13864 optional type-specifier. */
13865 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13866 cp_parser_parse_tentatively (parser);
13868 /* Look for the optional `::' operator. */
13869 global_p
13870 = (cp_parser_global_scope_opt (parser,
13871 /*current_scope_valid_p=*/false)
13872 != NULL_TREE);
13873 /* Look for the nested-name specifier. */
13874 qualified_p
13875 = (cp_parser_nested_name_specifier_opt (parser,
13876 /*typename_keyword_p=*/false,
13877 /*check_dependency_p=*/true,
13878 /*type_p=*/false,
13879 /*is_declaration=*/false)
13880 != NULL_TREE);
13881 token = cp_lexer_peek_token (parser->lexer);
13882 /* If we have seen a nested-name-specifier, and the next token
13883 is `template', then we are using the template-id production. */
13884 if (parser->scope
13885 && cp_parser_optional_template_keyword (parser))
13887 /* Look for the template-id. */
13888 type = cp_parser_template_id (parser,
13889 /*template_keyword_p=*/true,
13890 /*check_dependency_p=*/true,
13891 none_type,
13892 /*is_declaration=*/false);
13893 /* If the template-id did not name a type, we are out of
13894 luck. */
13895 if (TREE_CODE (type) != TYPE_DECL)
13897 cp_parser_error (parser, "expected template-id for type");
13898 type = NULL_TREE;
13901 /* Otherwise, look for a type-name. */
13902 else
13903 type = cp_parser_type_name (parser);
13904 /* Keep track of all name-lookups performed in class scopes. */
13905 if (type
13906 && !global_p
13907 && !qualified_p
13908 && TREE_CODE (type) == TYPE_DECL
13909 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13910 maybe_note_name_used_in_class (DECL_NAME (type), type);
13911 /* If it didn't work out, we don't have a TYPE. */
13912 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13913 && !cp_parser_parse_definitely (parser))
13914 type = NULL_TREE;
13915 if (type && decl_specs)
13916 cp_parser_set_decl_spec_type (decl_specs, type,
13917 token,
13918 /*type_definition_p=*/false);
13921 /* If we didn't get a type-name, issue an error message. */
13922 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13924 cp_parser_error (parser, "expected type-name");
13925 return error_mark_node;
13928 if (type && type != error_mark_node)
13930 /* See if TYPE is an Objective-C type, and if so, parse and
13931 accept any protocol references following it. Do this before
13932 the cp_parser_check_for_invalid_template_id() call, because
13933 Objective-C types can be followed by '<...>' which would
13934 enclose protocol names rather than template arguments, and so
13935 everything is fine. */
13936 if (c_dialect_objc () && !parser->scope
13937 && (objc_is_id (type) || objc_is_class_name (type)))
13939 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13940 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13942 /* Clobber the "unqualified" type previously entered into
13943 DECL_SPECS with the new, improved protocol-qualified version. */
13944 if (decl_specs)
13945 decl_specs->type = qual_type;
13947 return qual_type;
13950 /* There is no valid C++ program where a non-template type is
13951 followed by a "<". That usually indicates that the user
13952 thought that the type was a template. */
13953 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13954 none_type,
13955 token->location);
13958 return type;
13961 /* Parse a type-name.
13963 type-name:
13964 class-name
13965 enum-name
13966 typedef-name
13967 simple-template-id [in c++0x]
13969 enum-name:
13970 identifier
13972 typedef-name:
13973 identifier
13975 Returns a TYPE_DECL for the type. */
13977 static tree
13978 cp_parser_type_name (cp_parser* parser)
13980 tree type_decl;
13982 /* We can't know yet whether it is a class-name or not. */
13983 cp_parser_parse_tentatively (parser);
13984 /* Try a class-name. */
13985 type_decl = cp_parser_class_name (parser,
13986 /*typename_keyword_p=*/false,
13987 /*template_keyword_p=*/false,
13988 none_type,
13989 /*check_dependency_p=*/true,
13990 /*class_head_p=*/false,
13991 /*is_declaration=*/false);
13992 /* If it's not a class-name, keep looking. */
13993 if (!cp_parser_parse_definitely (parser))
13995 if (cxx_dialect < cxx0x)
13996 /* It must be a typedef-name or an enum-name. */
13997 return cp_parser_nonclass_name (parser);
13999 cp_parser_parse_tentatively (parser);
14000 /* It is either a simple-template-id representing an
14001 instantiation of an alias template... */
14002 type_decl = cp_parser_template_id (parser,
14003 /*template_keyword_p=*/false,
14004 /*check_dependency_p=*/false,
14005 none_type,
14006 /*is_declaration=*/false);
14007 /* Note that this must be an instantiation of an alias template
14008 because [temp.names]/6 says:
14010 A template-id that names an alias template specialization
14011 is a type-name.
14013 Whereas [temp.names]/7 says:
14015 A simple-template-id that names a class template
14016 specialization is a class-name. */
14017 if (type_decl != NULL_TREE
14018 && TREE_CODE (type_decl) == TYPE_DECL
14019 && TYPE_DECL_ALIAS_P (type_decl))
14020 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14021 else
14022 cp_parser_simulate_error (parser);
14024 if (!cp_parser_parse_definitely (parser))
14025 /* ... Or a typedef-name or an enum-name. */
14026 return cp_parser_nonclass_name (parser);
14029 return type_decl;
14032 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14034 enum-name:
14035 identifier
14037 typedef-name:
14038 identifier
14040 Returns a TYPE_DECL for the type. */
14042 static tree
14043 cp_parser_nonclass_name (cp_parser* parser)
14045 tree type_decl;
14046 tree identifier;
14048 cp_token *token = cp_lexer_peek_token (parser->lexer);
14049 identifier = cp_parser_identifier (parser);
14050 if (identifier == error_mark_node)
14051 return error_mark_node;
14053 /* Look up the type-name. */
14054 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14056 if (TREE_CODE (type_decl) == USING_DECL)
14058 if (!DECL_DEPENDENT_P (type_decl))
14059 type_decl = strip_using_decl (type_decl);
14060 else if (USING_DECL_TYPENAME_P (type_decl))
14062 /* We have found a type introduced by a using
14063 declaration at class scope that refers to a dependent
14064 type.
14066 using typename :: [opt] nested-name-specifier unqualified-id ;
14068 type_decl = make_typename_type (TREE_TYPE (type_decl),
14069 DECL_NAME (type_decl),
14070 typename_type, tf_error);
14071 if (type_decl != error_mark_node)
14072 type_decl = TYPE_NAME (type_decl);
14076 if (TREE_CODE (type_decl) != TYPE_DECL
14077 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14079 /* See if this is an Objective-C type. */
14080 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14081 tree type = objc_get_protocol_qualified_type (identifier, protos);
14082 if (type)
14083 type_decl = TYPE_NAME (type);
14086 /* Issue an error if we did not find a type-name. */
14087 if (TREE_CODE (type_decl) != TYPE_DECL
14088 /* In Objective-C, we have the complication that class names are
14089 normally type names and start declarations (eg, the
14090 "NSObject" in "NSObject *object;"), but can be used in an
14091 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14092 is an expression. So, a classname followed by a dot is not a
14093 valid type-name. */
14094 || (objc_is_class_name (TREE_TYPE (type_decl))
14095 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14097 if (!cp_parser_simulate_error (parser))
14098 cp_parser_name_lookup_error (parser, identifier, type_decl,
14099 NLE_TYPE, token->location);
14100 return error_mark_node;
14102 /* Remember that the name was used in the definition of the
14103 current class so that we can check later to see if the
14104 meaning would have been different after the class was
14105 entirely defined. */
14106 else if (type_decl != error_mark_node
14107 && !parser->scope)
14108 maybe_note_name_used_in_class (identifier, type_decl);
14110 return type_decl;
14113 /* Parse an elaborated-type-specifier. Note that the grammar given
14114 here incorporates the resolution to DR68.
14116 elaborated-type-specifier:
14117 class-key :: [opt] nested-name-specifier [opt] identifier
14118 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14119 enum-key :: [opt] nested-name-specifier [opt] identifier
14120 typename :: [opt] nested-name-specifier identifier
14121 typename :: [opt] nested-name-specifier template [opt]
14122 template-id
14124 GNU extension:
14126 elaborated-type-specifier:
14127 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14128 class-key attributes :: [opt] nested-name-specifier [opt]
14129 template [opt] template-id
14130 enum attributes :: [opt] nested-name-specifier [opt] identifier
14132 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14133 declared `friend'. If IS_DECLARATION is TRUE, then this
14134 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14135 something is being declared.
14137 Returns the TYPE specified. */
14139 static tree
14140 cp_parser_elaborated_type_specifier (cp_parser* parser,
14141 bool is_friend,
14142 bool is_declaration)
14144 enum tag_types tag_type;
14145 tree identifier;
14146 tree type = NULL_TREE;
14147 tree attributes = NULL_TREE;
14148 tree globalscope;
14149 cp_token *token = NULL;
14151 /* See if we're looking at the `enum' keyword. */
14152 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14154 /* Consume the `enum' token. */
14155 cp_lexer_consume_token (parser->lexer);
14156 /* Remember that it's an enumeration type. */
14157 tag_type = enum_type;
14158 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14159 enums) is used here. */
14160 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14161 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14163 pedwarn (input_location, 0, "elaborated-type-specifier "
14164 "for a scoped enum must not use the %<%D%> keyword",
14165 cp_lexer_peek_token (parser->lexer)->u.value);
14166 /* Consume the `struct' or `class' and parse it anyway. */
14167 cp_lexer_consume_token (parser->lexer);
14169 /* Parse the attributes. */
14170 attributes = cp_parser_attributes_opt (parser);
14172 /* Or, it might be `typename'. */
14173 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14174 RID_TYPENAME))
14176 /* Consume the `typename' token. */
14177 cp_lexer_consume_token (parser->lexer);
14178 /* Remember that it's a `typename' type. */
14179 tag_type = typename_type;
14181 /* Otherwise it must be a class-key. */
14182 else
14184 tag_type = cp_parser_class_key (parser);
14185 if (tag_type == none_type)
14186 return error_mark_node;
14187 /* Parse the attributes. */
14188 attributes = cp_parser_attributes_opt (parser);
14191 /* Look for the `::' operator. */
14192 globalscope = cp_parser_global_scope_opt (parser,
14193 /*current_scope_valid_p=*/false);
14194 /* Look for the nested-name-specifier. */
14195 if (tag_type == typename_type && !globalscope)
14197 if (!cp_parser_nested_name_specifier (parser,
14198 /*typename_keyword_p=*/true,
14199 /*check_dependency_p=*/true,
14200 /*type_p=*/true,
14201 is_declaration))
14202 return error_mark_node;
14204 else
14205 /* Even though `typename' is not present, the proposed resolution
14206 to Core Issue 180 says that in `class A<T>::B', `B' should be
14207 considered a type-name, even if `A<T>' is dependent. */
14208 cp_parser_nested_name_specifier_opt (parser,
14209 /*typename_keyword_p=*/true,
14210 /*check_dependency_p=*/true,
14211 /*type_p=*/true,
14212 is_declaration);
14213 /* For everything but enumeration types, consider a template-id.
14214 For an enumeration type, consider only a plain identifier. */
14215 if (tag_type != enum_type)
14217 bool template_p = false;
14218 tree decl;
14220 /* Allow the `template' keyword. */
14221 template_p = cp_parser_optional_template_keyword (parser);
14222 /* If we didn't see `template', we don't know if there's a
14223 template-id or not. */
14224 if (!template_p)
14225 cp_parser_parse_tentatively (parser);
14226 /* Parse the template-id. */
14227 token = cp_lexer_peek_token (parser->lexer);
14228 decl = cp_parser_template_id (parser, template_p,
14229 /*check_dependency_p=*/true,
14230 tag_type,
14231 is_declaration);
14232 /* If we didn't find a template-id, look for an ordinary
14233 identifier. */
14234 if (!template_p && !cp_parser_parse_definitely (parser))
14236 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14237 in effect, then we must assume that, upon instantiation, the
14238 template will correspond to a class. */
14239 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14240 && tag_type == typename_type)
14241 type = make_typename_type (parser->scope, decl,
14242 typename_type,
14243 /*complain=*/tf_error);
14244 /* If the `typename' keyword is in effect and DECL is not a type
14245 decl. Then type is non existant. */
14246 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14247 type = NULL_TREE;
14248 else
14249 type = check_elaborated_type_specifier (tag_type, decl,
14250 /*allow_template_p=*/true);
14253 if (!type)
14255 token = cp_lexer_peek_token (parser->lexer);
14256 identifier = cp_parser_identifier (parser);
14258 if (identifier == error_mark_node)
14260 parser->scope = NULL_TREE;
14261 return error_mark_node;
14264 /* For a `typename', we needn't call xref_tag. */
14265 if (tag_type == typename_type
14266 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14267 return cp_parser_make_typename_type (parser, parser->scope,
14268 identifier,
14269 token->location);
14270 /* Look up a qualified name in the usual way. */
14271 if (parser->scope)
14273 tree decl;
14274 tree ambiguous_decls;
14276 decl = cp_parser_lookup_name (parser, identifier,
14277 tag_type,
14278 /*is_template=*/false,
14279 /*is_namespace=*/false,
14280 /*check_dependency=*/true,
14281 &ambiguous_decls,
14282 token->location);
14284 /* If the lookup was ambiguous, an error will already have been
14285 issued. */
14286 if (ambiguous_decls)
14287 return error_mark_node;
14289 /* If we are parsing friend declaration, DECL may be a
14290 TEMPLATE_DECL tree node here. However, we need to check
14291 whether this TEMPLATE_DECL results in valid code. Consider
14292 the following example:
14294 namespace N {
14295 template <class T> class C {};
14297 class X {
14298 template <class T> friend class N::C; // #1, valid code
14300 template <class T> class Y {
14301 friend class N::C; // #2, invalid code
14304 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14305 name lookup of `N::C'. We see that friend declaration must
14306 be template for the code to be valid. Note that
14307 processing_template_decl does not work here since it is
14308 always 1 for the above two cases. */
14310 decl = (cp_parser_maybe_treat_template_as_class
14311 (decl, /*tag_name_p=*/is_friend
14312 && parser->num_template_parameter_lists));
14314 if (TREE_CODE (decl) != TYPE_DECL)
14316 cp_parser_diagnose_invalid_type_name (parser,
14317 parser->scope,
14318 identifier,
14319 token->location);
14320 return error_mark_node;
14323 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14325 bool allow_template = (parser->num_template_parameter_lists
14326 || DECL_SELF_REFERENCE_P (decl));
14327 type = check_elaborated_type_specifier (tag_type, decl,
14328 allow_template);
14330 if (type == error_mark_node)
14331 return error_mark_node;
14334 /* Forward declarations of nested types, such as
14336 class C1::C2;
14337 class C1::C2::C3;
14339 are invalid unless all components preceding the final '::'
14340 are complete. If all enclosing types are complete, these
14341 declarations become merely pointless.
14343 Invalid forward declarations of nested types are errors
14344 caught elsewhere in parsing. Those that are pointless arrive
14345 here. */
14347 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14348 && !is_friend && !processing_explicit_instantiation)
14349 warning (0, "declaration %qD does not declare anything", decl);
14351 type = TREE_TYPE (decl);
14353 else
14355 /* An elaborated-type-specifier sometimes introduces a new type and
14356 sometimes names an existing type. Normally, the rule is that it
14357 introduces a new type only if there is not an existing type of
14358 the same name already in scope. For example, given:
14360 struct S {};
14361 void f() { struct S s; }
14363 the `struct S' in the body of `f' is the same `struct S' as in
14364 the global scope; the existing definition is used. However, if
14365 there were no global declaration, this would introduce a new
14366 local class named `S'.
14368 An exception to this rule applies to the following code:
14370 namespace N { struct S; }
14372 Here, the elaborated-type-specifier names a new type
14373 unconditionally; even if there is already an `S' in the
14374 containing scope this declaration names a new type.
14375 This exception only applies if the elaborated-type-specifier
14376 forms the complete declaration:
14378 [class.name]
14380 A declaration consisting solely of `class-key identifier ;' is
14381 either a redeclaration of the name in the current scope or a
14382 forward declaration of the identifier as a class name. It
14383 introduces the name into the current scope.
14385 We are in this situation precisely when the next token is a `;'.
14387 An exception to the exception is that a `friend' declaration does
14388 *not* name a new type; i.e., given:
14390 struct S { friend struct T; };
14392 `T' is not a new type in the scope of `S'.
14394 Also, `new struct S' or `sizeof (struct S)' never results in the
14395 definition of a new type; a new type can only be declared in a
14396 declaration context. */
14398 tag_scope ts;
14399 bool template_p;
14401 if (is_friend)
14402 /* Friends have special name lookup rules. */
14403 ts = ts_within_enclosing_non_class;
14404 else if (is_declaration
14405 && cp_lexer_next_token_is (parser->lexer,
14406 CPP_SEMICOLON))
14407 /* This is a `class-key identifier ;' */
14408 ts = ts_current;
14409 else
14410 ts = ts_global;
14412 template_p =
14413 (parser->num_template_parameter_lists
14414 && (cp_parser_next_token_starts_class_definition_p (parser)
14415 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14416 /* An unqualified name was used to reference this type, so
14417 there were no qualifying templates. */
14418 if (!cp_parser_check_template_parameters (parser,
14419 /*num_templates=*/0,
14420 token->location,
14421 /*declarator=*/NULL))
14422 return error_mark_node;
14423 type = xref_tag (tag_type, identifier, ts, template_p);
14427 if (type == error_mark_node)
14428 return error_mark_node;
14430 /* Allow attributes on forward declarations of classes. */
14431 if (attributes)
14433 if (TREE_CODE (type) == TYPENAME_TYPE)
14434 warning (OPT_Wattributes,
14435 "attributes ignored on uninstantiated type");
14436 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14437 && ! processing_explicit_instantiation)
14438 warning (OPT_Wattributes,
14439 "attributes ignored on template instantiation");
14440 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14441 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14442 else
14443 warning (OPT_Wattributes,
14444 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14447 if (tag_type != enum_type)
14449 /* Indicate whether this class was declared as a `class' or as a
14450 `struct'. */
14451 if (TREE_CODE (type) == RECORD_TYPE)
14452 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14453 cp_parser_check_class_key (tag_type, type);
14456 /* A "<" cannot follow an elaborated type specifier. If that
14457 happens, the user was probably trying to form a template-id. */
14458 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
14459 token->location);
14461 return type;
14464 /* Parse an enum-specifier.
14466 enum-specifier:
14467 enum-head { enumerator-list [opt] }
14468 enum-head { enumerator-list , } [C++0x]
14470 enum-head:
14471 enum-key identifier [opt] enum-base [opt]
14472 enum-key nested-name-specifier identifier enum-base [opt]
14474 enum-key:
14475 enum
14476 enum class [C++0x]
14477 enum struct [C++0x]
14479 enum-base: [C++0x]
14480 : type-specifier-seq
14482 opaque-enum-specifier:
14483 enum-key identifier enum-base [opt] ;
14485 GNU Extensions:
14486 enum-key attributes[opt] identifier [opt] enum-base [opt]
14487 { enumerator-list [opt] }attributes[opt]
14488 enum-key attributes[opt] identifier [opt] enum-base [opt]
14489 { enumerator-list, }attributes[opt] [C++0x]
14491 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14492 if the token stream isn't an enum-specifier after all. */
14494 static tree
14495 cp_parser_enum_specifier (cp_parser* parser)
14497 tree identifier;
14498 tree type = NULL_TREE;
14499 tree prev_scope;
14500 tree nested_name_specifier = NULL_TREE;
14501 tree attributes;
14502 bool scoped_enum_p = false;
14503 bool has_underlying_type = false;
14504 bool nested_being_defined = false;
14505 bool new_value_list = false;
14506 bool is_new_type = false;
14507 bool is_anonymous = false;
14508 tree underlying_type = NULL_TREE;
14509 cp_token *type_start_token = NULL;
14510 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14512 parser->colon_corrects_to_scope_p = false;
14514 /* Parse tentatively so that we can back up if we don't find a
14515 enum-specifier. */
14516 cp_parser_parse_tentatively (parser);
14518 /* Caller guarantees that the current token is 'enum', an identifier
14519 possibly follows, and the token after that is an opening brace.
14520 If we don't have an identifier, fabricate an anonymous name for
14521 the enumeration being defined. */
14522 cp_lexer_consume_token (parser->lexer);
14524 /* Parse the "class" or "struct", which indicates a scoped
14525 enumeration type in C++0x. */
14526 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14527 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14529 if (cxx_dialect < cxx0x)
14530 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14532 /* Consume the `struct' or `class' token. */
14533 cp_lexer_consume_token (parser->lexer);
14535 scoped_enum_p = true;
14538 attributes = cp_parser_attributes_opt (parser);
14540 /* Clear the qualification. */
14541 parser->scope = NULL_TREE;
14542 parser->qualifying_scope = NULL_TREE;
14543 parser->object_scope = NULL_TREE;
14545 /* Figure out in what scope the declaration is being placed. */
14546 prev_scope = current_scope ();
14548 type_start_token = cp_lexer_peek_token (parser->lexer);
14550 push_deferring_access_checks (dk_no_check);
14551 nested_name_specifier
14552 = cp_parser_nested_name_specifier_opt (parser,
14553 /*typename_keyword_p=*/true,
14554 /*check_dependency_p=*/false,
14555 /*type_p=*/false,
14556 /*is_declaration=*/false);
14558 if (nested_name_specifier)
14560 tree name;
14562 identifier = cp_parser_identifier (parser);
14563 name = cp_parser_lookup_name (parser, identifier,
14564 enum_type,
14565 /*is_template=*/false,
14566 /*is_namespace=*/false,
14567 /*check_dependency=*/true,
14568 /*ambiguous_decls=*/NULL,
14569 input_location);
14570 if (name)
14572 type = TREE_TYPE (name);
14573 if (TREE_CODE (type) == TYPENAME_TYPE)
14575 /* Are template enums allowed in ISO? */
14576 if (template_parm_scope_p ())
14577 pedwarn (type_start_token->location, OPT_Wpedantic,
14578 "%qD is an enumeration template", name);
14579 /* ignore a typename reference, for it will be solved by name
14580 in start_enum. */
14581 type = NULL_TREE;
14584 else
14585 error_at (type_start_token->location,
14586 "%qD is not an enumerator-name", identifier);
14588 else
14590 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14591 identifier = cp_parser_identifier (parser);
14592 else
14594 identifier = make_anon_name ();
14595 is_anonymous = true;
14598 pop_deferring_access_checks ();
14600 /* Check for the `:' that denotes a specified underlying type in C++0x.
14601 Note that a ':' could also indicate a bitfield width, however. */
14602 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14604 cp_decl_specifier_seq type_specifiers;
14606 /* Consume the `:'. */
14607 cp_lexer_consume_token (parser->lexer);
14609 /* Parse the type-specifier-seq. */
14610 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14611 /*is_trailing_return=*/false,
14612 &type_specifiers);
14614 /* At this point this is surely not elaborated type specifier. */
14615 if (!cp_parser_parse_definitely (parser))
14616 return NULL_TREE;
14618 if (cxx_dialect < cxx0x)
14619 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14621 has_underlying_type = true;
14623 /* If that didn't work, stop. */
14624 if (type_specifiers.type != error_mark_node)
14626 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14627 /*initialized=*/0, NULL);
14628 if (underlying_type == error_mark_node)
14629 underlying_type = NULL_TREE;
14633 /* Look for the `{' but don't consume it yet. */
14634 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14636 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14638 cp_parser_error (parser, "expected %<{%>");
14639 if (has_underlying_type)
14641 type = NULL_TREE;
14642 goto out;
14645 /* An opaque-enum-specifier must have a ';' here. */
14646 if ((scoped_enum_p || underlying_type)
14647 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14649 cp_parser_error (parser, "expected %<;%> or %<{%>");
14650 if (has_underlying_type)
14652 type = NULL_TREE;
14653 goto out;
14658 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14659 return NULL_TREE;
14661 if (nested_name_specifier)
14663 if (CLASS_TYPE_P (nested_name_specifier))
14665 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14666 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14667 push_scope (nested_name_specifier);
14669 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14671 push_nested_namespace (nested_name_specifier);
14675 /* Issue an error message if type-definitions are forbidden here. */
14676 if (!cp_parser_check_type_definition (parser))
14677 type = error_mark_node;
14678 else
14679 /* Create the new type. We do this before consuming the opening
14680 brace so the enum will be recorded as being on the line of its
14681 tag (or the 'enum' keyword, if there is no tag). */
14682 type = start_enum (identifier, type, underlying_type,
14683 scoped_enum_p, &is_new_type);
14685 /* If the next token is not '{' it is an opaque-enum-specifier or an
14686 elaborated-type-specifier. */
14687 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14689 timevar_push (TV_PARSE_ENUM);
14690 if (nested_name_specifier)
14692 /* The following catches invalid code such as:
14693 enum class S<int>::E { A, B, C }; */
14694 if (!processing_specialization
14695 && CLASS_TYPE_P (nested_name_specifier)
14696 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14697 error_at (type_start_token->location, "cannot add an enumerator "
14698 "list to a template instantiation");
14700 /* If that scope does not contain the scope in which the
14701 class was originally declared, the program is invalid. */
14702 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14704 if (at_namespace_scope_p ())
14705 error_at (type_start_token->location,
14706 "declaration of %qD in namespace %qD which does not "
14707 "enclose %qD",
14708 type, prev_scope, nested_name_specifier);
14709 else
14710 error_at (type_start_token->location,
14711 "declaration of %qD in %qD which does not enclose %qD",
14712 type, prev_scope, nested_name_specifier);
14713 type = error_mark_node;
14717 if (scoped_enum_p)
14718 begin_scope (sk_scoped_enum, type);
14720 /* Consume the opening brace. */
14721 cp_lexer_consume_token (parser->lexer);
14723 if (type == error_mark_node)
14724 ; /* Nothing to add */
14725 else if (OPAQUE_ENUM_P (type)
14726 || (cxx_dialect > cxx98 && processing_specialization))
14728 new_value_list = true;
14729 SET_OPAQUE_ENUM_P (type, false);
14730 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14732 else
14734 error_at (type_start_token->location, "multiple definition of %q#T", type);
14735 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14736 "previous definition here");
14737 type = error_mark_node;
14740 if (type == error_mark_node)
14741 cp_parser_skip_to_end_of_block_or_statement (parser);
14742 /* If the next token is not '}', then there are some enumerators. */
14743 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14744 cp_parser_enumerator_list (parser, type);
14746 /* Consume the final '}'. */
14747 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14749 if (scoped_enum_p)
14750 finish_scope ();
14751 timevar_pop (TV_PARSE_ENUM);
14753 else
14755 /* If a ';' follows, then it is an opaque-enum-specifier
14756 and additional restrictions apply. */
14757 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14759 if (is_anonymous)
14760 error_at (type_start_token->location,
14761 "opaque-enum-specifier without name");
14762 else if (nested_name_specifier)
14763 error_at (type_start_token->location,
14764 "opaque-enum-specifier must use a simple identifier");
14768 /* Look for trailing attributes to apply to this enumeration, and
14769 apply them if appropriate. */
14770 if (cp_parser_allow_gnu_extensions_p (parser))
14772 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
14773 trailing_attr = chainon (trailing_attr, attributes);
14774 cplus_decl_attributes (&type,
14775 trailing_attr,
14776 (int) ATTR_FLAG_TYPE_IN_PLACE);
14779 /* Finish up the enumeration. */
14780 if (type != error_mark_node)
14782 if (new_value_list)
14783 finish_enum_value_list (type);
14784 if (is_new_type)
14785 finish_enum (type);
14788 if (nested_name_specifier)
14790 if (CLASS_TYPE_P (nested_name_specifier))
14792 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14793 pop_scope (nested_name_specifier);
14795 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14797 pop_nested_namespace (nested_name_specifier);
14800 out:
14801 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14802 return type;
14805 /* Parse an enumerator-list. The enumerators all have the indicated
14806 TYPE.
14808 enumerator-list:
14809 enumerator-definition
14810 enumerator-list , enumerator-definition */
14812 static void
14813 cp_parser_enumerator_list (cp_parser* parser, tree type)
14815 while (true)
14817 /* Parse an enumerator-definition. */
14818 cp_parser_enumerator_definition (parser, type);
14820 /* If the next token is not a ',', we've reached the end of
14821 the list. */
14822 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14823 break;
14824 /* Otherwise, consume the `,' and keep going. */
14825 cp_lexer_consume_token (parser->lexer);
14826 /* If the next token is a `}', there is a trailing comma. */
14827 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14829 if (cxx_dialect < cxx0x && !in_system_header)
14830 pedwarn (input_location, OPT_Wpedantic,
14831 "comma at end of enumerator list");
14832 break;
14837 /* Parse an enumerator-definition. The enumerator has the indicated
14838 TYPE.
14840 enumerator-definition:
14841 enumerator
14842 enumerator = constant-expression
14844 enumerator:
14845 identifier */
14847 static void
14848 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14850 tree identifier;
14851 tree value;
14852 location_t loc;
14854 /* Save the input location because we are interested in the location
14855 of the identifier and not the location of the explicit value. */
14856 loc = cp_lexer_peek_token (parser->lexer)->location;
14858 /* Look for the identifier. */
14859 identifier = cp_parser_identifier (parser);
14860 if (identifier == error_mark_node)
14861 return;
14863 /* If the next token is an '=', then there is an explicit value. */
14864 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14866 /* Consume the `=' token. */
14867 cp_lexer_consume_token (parser->lexer);
14868 /* Parse the value. */
14869 value = cp_parser_constant_expression (parser,
14870 /*allow_non_constant_p=*/false,
14871 NULL);
14873 else
14874 value = NULL_TREE;
14876 /* If we are processing a template, make sure the initializer of the
14877 enumerator doesn't contain any bare template parameter pack. */
14878 if (check_for_bare_parameter_packs (value))
14879 value = error_mark_node;
14881 /* integral_constant_value will pull out this expression, so make sure
14882 it's folded as appropriate. */
14883 value = fold_non_dependent_expr (value);
14885 /* Create the enumerator. */
14886 build_enumerator (identifier, value, type, loc);
14889 /* Parse a namespace-name.
14891 namespace-name:
14892 original-namespace-name
14893 namespace-alias
14895 Returns the NAMESPACE_DECL for the namespace. */
14897 static tree
14898 cp_parser_namespace_name (cp_parser* parser)
14900 tree identifier;
14901 tree namespace_decl;
14903 cp_token *token = cp_lexer_peek_token (parser->lexer);
14905 /* Get the name of the namespace. */
14906 identifier = cp_parser_identifier (parser);
14907 if (identifier == error_mark_node)
14908 return error_mark_node;
14910 /* Look up the identifier in the currently active scope. Look only
14911 for namespaces, due to:
14913 [basic.lookup.udir]
14915 When looking up a namespace-name in a using-directive or alias
14916 definition, only namespace names are considered.
14918 And:
14920 [basic.lookup.qual]
14922 During the lookup of a name preceding the :: scope resolution
14923 operator, object, function, and enumerator names are ignored.
14925 (Note that cp_parser_qualifying_entity only calls this
14926 function if the token after the name is the scope resolution
14927 operator.) */
14928 namespace_decl = cp_parser_lookup_name (parser, identifier,
14929 none_type,
14930 /*is_template=*/false,
14931 /*is_namespace=*/true,
14932 /*check_dependency=*/true,
14933 /*ambiguous_decls=*/NULL,
14934 token->location);
14935 /* If it's not a namespace, issue an error. */
14936 if (namespace_decl == error_mark_node
14937 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14939 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14940 error_at (token->location, "%qD is not a namespace-name", identifier);
14941 cp_parser_error (parser, "expected namespace-name");
14942 namespace_decl = error_mark_node;
14945 return namespace_decl;
14948 /* Parse a namespace-definition.
14950 namespace-definition:
14951 named-namespace-definition
14952 unnamed-namespace-definition
14954 named-namespace-definition:
14955 original-namespace-definition
14956 extension-namespace-definition
14958 original-namespace-definition:
14959 namespace identifier { namespace-body }
14961 extension-namespace-definition:
14962 namespace original-namespace-name { namespace-body }
14964 unnamed-namespace-definition:
14965 namespace { namespace-body } */
14967 static void
14968 cp_parser_namespace_definition (cp_parser* parser)
14970 tree identifier, attribs;
14971 bool has_visibility;
14972 bool is_inline;
14974 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14976 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14977 is_inline = true;
14978 cp_lexer_consume_token (parser->lexer);
14980 else
14981 is_inline = false;
14983 /* Look for the `namespace' keyword. */
14984 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14986 /* Get the name of the namespace. We do not attempt to distinguish
14987 between an original-namespace-definition and an
14988 extension-namespace-definition at this point. The semantic
14989 analysis routines are responsible for that. */
14990 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14991 identifier = cp_parser_identifier (parser);
14992 else
14993 identifier = NULL_TREE;
14995 /* Parse any specified attributes. */
14996 attribs = cp_parser_attributes_opt (parser);
14998 /* Look for the `{' to start the namespace. */
14999 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15000 /* Start the namespace. */
15001 push_namespace (identifier);
15003 /* "inline namespace" is equivalent to a stub namespace definition
15004 followed by a strong using directive. */
15005 if (is_inline)
15007 tree name_space = current_namespace;
15008 /* Set up namespace association. */
15009 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15010 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15011 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15012 /* Import the contents of the inline namespace. */
15013 pop_namespace ();
15014 do_using_directive (name_space);
15015 push_namespace (identifier);
15018 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15020 /* Parse the body of the namespace. */
15021 cp_parser_namespace_body (parser);
15023 if (has_visibility)
15024 pop_visibility (1);
15026 /* Finish the namespace. */
15027 pop_namespace ();
15028 /* Look for the final `}'. */
15029 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15032 /* Parse a namespace-body.
15034 namespace-body:
15035 declaration-seq [opt] */
15037 static void
15038 cp_parser_namespace_body (cp_parser* parser)
15040 cp_parser_declaration_seq_opt (parser);
15043 /* Parse a namespace-alias-definition.
15045 namespace-alias-definition:
15046 namespace identifier = qualified-namespace-specifier ; */
15048 static void
15049 cp_parser_namespace_alias_definition (cp_parser* parser)
15051 tree identifier;
15052 tree namespace_specifier;
15054 cp_token *token = cp_lexer_peek_token (parser->lexer);
15056 /* Look for the `namespace' keyword. */
15057 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15058 /* Look for the identifier. */
15059 identifier = cp_parser_identifier (parser);
15060 if (identifier == error_mark_node)
15061 return;
15062 /* Look for the `=' token. */
15063 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15064 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15066 error_at (token->location, "%<namespace%> definition is not allowed here");
15067 /* Skip the definition. */
15068 cp_lexer_consume_token (parser->lexer);
15069 if (cp_parser_skip_to_closing_brace (parser))
15070 cp_lexer_consume_token (parser->lexer);
15071 return;
15073 cp_parser_require (parser, CPP_EQ, RT_EQ);
15074 /* Look for the qualified-namespace-specifier. */
15075 namespace_specifier
15076 = cp_parser_qualified_namespace_specifier (parser);
15077 /* Look for the `;' token. */
15078 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15080 /* Register the alias in the symbol table. */
15081 do_namespace_alias (identifier, namespace_specifier);
15084 /* Parse a qualified-namespace-specifier.
15086 qualified-namespace-specifier:
15087 :: [opt] nested-name-specifier [opt] namespace-name
15089 Returns a NAMESPACE_DECL corresponding to the specified
15090 namespace. */
15092 static tree
15093 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15095 /* Look for the optional `::'. */
15096 cp_parser_global_scope_opt (parser,
15097 /*current_scope_valid_p=*/false);
15099 /* Look for the optional nested-name-specifier. */
15100 cp_parser_nested_name_specifier_opt (parser,
15101 /*typename_keyword_p=*/false,
15102 /*check_dependency_p=*/true,
15103 /*type_p=*/false,
15104 /*is_declaration=*/true);
15106 return cp_parser_namespace_name (parser);
15109 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15110 access declaration.
15112 using-declaration:
15113 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15114 using :: unqualified-id ;
15116 access-declaration:
15117 qualified-id ;
15121 static bool
15122 cp_parser_using_declaration (cp_parser* parser,
15123 bool access_declaration_p)
15125 cp_token *token;
15126 bool typename_p = false;
15127 bool global_scope_p;
15128 tree decl;
15129 tree identifier;
15130 tree qscope;
15131 int oldcount = errorcount;
15132 cp_token *diag_token = NULL;
15134 if (access_declaration_p)
15136 diag_token = cp_lexer_peek_token (parser->lexer);
15137 cp_parser_parse_tentatively (parser);
15139 else
15141 /* Look for the `using' keyword. */
15142 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15144 /* Peek at the next token. */
15145 token = cp_lexer_peek_token (parser->lexer);
15146 /* See if it's `typename'. */
15147 if (token->keyword == RID_TYPENAME)
15149 /* Remember that we've seen it. */
15150 typename_p = true;
15151 /* Consume the `typename' token. */
15152 cp_lexer_consume_token (parser->lexer);
15156 /* Look for the optional global scope qualification. */
15157 global_scope_p
15158 = (cp_parser_global_scope_opt (parser,
15159 /*current_scope_valid_p=*/false)
15160 != NULL_TREE);
15162 /* If we saw `typename', or didn't see `::', then there must be a
15163 nested-name-specifier present. */
15164 if (typename_p || !global_scope_p)
15165 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15166 /*check_dependency_p=*/true,
15167 /*type_p=*/false,
15168 /*is_declaration=*/true);
15169 /* Otherwise, we could be in either of the two productions. In that
15170 case, treat the nested-name-specifier as optional. */
15171 else
15172 qscope = cp_parser_nested_name_specifier_opt (parser,
15173 /*typename_keyword_p=*/false,
15174 /*check_dependency_p=*/true,
15175 /*type_p=*/false,
15176 /*is_declaration=*/true);
15177 if (!qscope)
15178 qscope = global_namespace;
15180 if (access_declaration_p && cp_parser_error_occurred (parser))
15181 /* Something has already gone wrong; there's no need to parse
15182 further. Since an error has occurred, the return value of
15183 cp_parser_parse_definitely will be false, as required. */
15184 return cp_parser_parse_definitely (parser);
15186 token = cp_lexer_peek_token (parser->lexer);
15187 /* Parse the unqualified-id. */
15188 identifier = cp_parser_unqualified_id (parser,
15189 /*template_keyword_p=*/false,
15190 /*check_dependency_p=*/true,
15191 /*declarator_p=*/true,
15192 /*optional_p=*/false);
15194 if (access_declaration_p)
15196 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15197 cp_parser_simulate_error (parser);
15198 if (!cp_parser_parse_definitely (parser))
15199 return false;
15202 /* The function we call to handle a using-declaration is different
15203 depending on what scope we are in. */
15204 if (qscope == error_mark_node || identifier == error_mark_node)
15206 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
15207 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15208 /* [namespace.udecl]
15210 A using declaration shall not name a template-id. */
15211 error_at (token->location,
15212 "a template-id may not appear in a using-declaration");
15213 else
15215 if (at_class_scope_p ())
15217 /* Create the USING_DECL. */
15218 decl = do_class_using_decl (parser->scope, identifier);
15220 if (decl && typename_p)
15221 USING_DECL_TYPENAME_P (decl) = 1;
15223 if (check_for_bare_parameter_packs (decl))
15224 return false;
15225 else
15226 /* Add it to the list of members in this class. */
15227 finish_member_declaration (decl);
15229 else
15231 decl = cp_parser_lookup_name_simple (parser,
15232 identifier,
15233 token->location);
15234 if (decl == error_mark_node)
15235 cp_parser_name_lookup_error (parser, identifier,
15236 decl, NLE_NULL,
15237 token->location);
15238 else if (check_for_bare_parameter_packs (decl))
15239 return false;
15240 else if (!at_namespace_scope_p ())
15241 do_local_using_decl (decl, qscope, identifier);
15242 else
15243 do_toplevel_using_decl (decl, qscope, identifier);
15247 /* Look for the final `;'. */
15248 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15250 if (access_declaration_p && errorcount == oldcount)
15251 warning_at (diag_token->location, OPT_Wdeprecated,
15252 "access declarations are deprecated "
15253 "in favour of using-declarations; "
15254 "suggestion: add the %<using%> keyword");
15256 return true;
15259 /* Parse an alias-declaration.
15261 alias-declaration:
15262 using identifier attribute-specifier-seq [opt] = type-id */
15264 static tree
15265 cp_parser_alias_declaration (cp_parser* parser)
15267 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15268 location_t id_location;
15269 cp_declarator *declarator;
15270 cp_decl_specifier_seq decl_specs;
15271 bool member_p;
15272 const char *saved_message = NULL;
15274 /* Look for the `using' keyword. */
15275 cp_token *using_token
15276 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
15277 if (using_token == NULL)
15278 return error_mark_node;
15280 id_location = cp_lexer_peek_token (parser->lexer)->location;
15281 id = cp_parser_identifier (parser);
15282 if (id == error_mark_node)
15283 return error_mark_node;
15285 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
15286 attributes = cp_parser_attributes_opt (parser);
15287 if (attributes == error_mark_node)
15288 return error_mark_node;
15290 cp_parser_require (parser, CPP_EQ, RT_EQ);
15292 if (cp_parser_error_occurred (parser))
15293 return error_mark_node;
15295 cp_parser_commit_to_tentative_parse (parser);
15297 /* Now we are going to parse the type-id of the declaration. */
15300 [dcl.type]/3 says:
15302 "A type-specifier-seq shall not define a class or enumeration
15303 unless it appears in the type-id of an alias-declaration (7.1.3) that
15304 is not the declaration of a template-declaration."
15306 In other words, if we currently are in an alias template, the
15307 type-id should not define a type.
15309 So let's set parser->type_definition_forbidden_message in that
15310 case; cp_parser_check_type_definition (called by
15311 cp_parser_class_specifier) will then emit an error if a type is
15312 defined in the type-id. */
15313 if (parser->num_template_parameter_lists)
15315 saved_message = parser->type_definition_forbidden_message;
15316 parser->type_definition_forbidden_message =
15317 G_("types may not be defined in alias template declarations");
15320 type = cp_parser_type_id (parser);
15322 /* Restore the error message if need be. */
15323 if (parser->num_template_parameter_lists)
15324 parser->type_definition_forbidden_message = saved_message;
15326 if (type == error_mark_node)
15328 cp_parser_skip_to_end_of_block_or_statement (parser);
15329 return error_mark_node;
15332 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15334 if (cp_parser_error_occurred (parser))
15336 cp_parser_skip_to_end_of_block_or_statement (parser);
15337 return error_mark_node;
15340 /* A typedef-name can also be introduced by an alias-declaration. The
15341 identifier following the using keyword becomes a typedef-name. It has
15342 the same semantics as if it were introduced by the typedef
15343 specifier. In particular, it does not define a new type and it shall
15344 not appear in the type-id. */
15346 clear_decl_specs (&decl_specs);
15347 decl_specs.type = type;
15348 if (attributes != NULL_TREE)
15350 decl_specs.attributes = attributes;
15351 set_and_check_decl_spec_loc (&decl_specs,
15352 ds_attribute,
15353 attrs_token);
15355 set_and_check_decl_spec_loc (&decl_specs,
15356 ds_typedef,
15357 using_token);
15358 set_and_check_decl_spec_loc (&decl_specs,
15359 ds_alias,
15360 using_token);
15362 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15363 declarator->id_loc = id_location;
15365 member_p = at_class_scope_p ();
15366 if (member_p)
15367 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15368 NULL_TREE, attributes);
15369 else
15370 decl = start_decl (declarator, &decl_specs, 0,
15371 attributes, NULL_TREE, &pushed_scope);
15372 if (decl == error_mark_node)
15373 return decl;
15375 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15377 if (pushed_scope)
15378 pop_scope (pushed_scope);
15380 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15381 added into the symbol table; otherwise, return the TYPE_DECL. */
15382 if (DECL_LANG_SPECIFIC (decl)
15383 && DECL_TEMPLATE_INFO (decl)
15384 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15386 decl = DECL_TI_TEMPLATE (decl);
15387 if (member_p)
15388 check_member_template (decl);
15391 return decl;
15394 /* Parse a using-directive.
15396 using-directive:
15397 using namespace :: [opt] nested-name-specifier [opt]
15398 namespace-name ; */
15400 static void
15401 cp_parser_using_directive (cp_parser* parser)
15403 tree namespace_decl;
15404 tree attribs;
15406 /* Look for the `using' keyword. */
15407 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15408 /* And the `namespace' keyword. */
15409 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15410 /* Look for the optional `::' operator. */
15411 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15412 /* And the optional nested-name-specifier. */
15413 cp_parser_nested_name_specifier_opt (parser,
15414 /*typename_keyword_p=*/false,
15415 /*check_dependency_p=*/true,
15416 /*type_p=*/false,
15417 /*is_declaration=*/true);
15418 /* Get the namespace being used. */
15419 namespace_decl = cp_parser_namespace_name (parser);
15420 /* And any specified attributes. */
15421 attribs = cp_parser_attributes_opt (parser);
15422 /* Update the symbol table. */
15423 parse_using_directive (namespace_decl, attribs);
15424 /* Look for the final `;'. */
15425 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15428 /* Parse an asm-definition.
15430 asm-definition:
15431 asm ( string-literal ) ;
15433 GNU Extension:
15435 asm-definition:
15436 asm volatile [opt] ( string-literal ) ;
15437 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15438 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15439 : asm-operand-list [opt] ) ;
15440 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15441 : asm-operand-list [opt]
15442 : asm-clobber-list [opt] ) ;
15443 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15444 : asm-clobber-list [opt]
15445 : asm-goto-list ) ; */
15447 static void
15448 cp_parser_asm_definition (cp_parser* parser)
15450 tree string;
15451 tree outputs = NULL_TREE;
15452 tree inputs = NULL_TREE;
15453 tree clobbers = NULL_TREE;
15454 tree labels = NULL_TREE;
15455 tree asm_stmt;
15456 bool volatile_p = false;
15457 bool extended_p = false;
15458 bool invalid_inputs_p = false;
15459 bool invalid_outputs_p = false;
15460 bool goto_p = false;
15461 required_token missing = RT_NONE;
15463 /* Look for the `asm' keyword. */
15464 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15465 /* See if the next token is `volatile'. */
15466 if (cp_parser_allow_gnu_extensions_p (parser)
15467 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15469 /* Remember that we saw the `volatile' keyword. */
15470 volatile_p = true;
15471 /* Consume the token. */
15472 cp_lexer_consume_token (parser->lexer);
15474 if (cp_parser_allow_gnu_extensions_p (parser)
15475 && parser->in_function_body
15476 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15478 /* Remember that we saw the `goto' keyword. */
15479 goto_p = true;
15480 /* Consume the token. */
15481 cp_lexer_consume_token (parser->lexer);
15483 /* Look for the opening `('. */
15484 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15485 return;
15486 /* Look for the string. */
15487 string = cp_parser_string_literal (parser, false, false);
15488 if (string == error_mark_node)
15490 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15491 /*consume_paren=*/true);
15492 return;
15495 /* If we're allowing GNU extensions, check for the extended assembly
15496 syntax. Unfortunately, the `:' tokens need not be separated by
15497 a space in C, and so, for compatibility, we tolerate that here
15498 too. Doing that means that we have to treat the `::' operator as
15499 two `:' tokens. */
15500 if (cp_parser_allow_gnu_extensions_p (parser)
15501 && parser->in_function_body
15502 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15503 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15505 bool inputs_p = false;
15506 bool clobbers_p = false;
15507 bool labels_p = false;
15509 /* The extended syntax was used. */
15510 extended_p = true;
15512 /* Look for outputs. */
15513 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15515 /* Consume the `:'. */
15516 cp_lexer_consume_token (parser->lexer);
15517 /* Parse the output-operands. */
15518 if (cp_lexer_next_token_is_not (parser->lexer,
15519 CPP_COLON)
15520 && cp_lexer_next_token_is_not (parser->lexer,
15521 CPP_SCOPE)
15522 && cp_lexer_next_token_is_not (parser->lexer,
15523 CPP_CLOSE_PAREN)
15524 && !goto_p)
15525 outputs = cp_parser_asm_operand_list (parser);
15527 if (outputs == error_mark_node)
15528 invalid_outputs_p = true;
15530 /* If the next token is `::', there are no outputs, and the
15531 next token is the beginning of the inputs. */
15532 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15533 /* The inputs are coming next. */
15534 inputs_p = true;
15536 /* Look for inputs. */
15537 if (inputs_p
15538 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15540 /* Consume the `:' or `::'. */
15541 cp_lexer_consume_token (parser->lexer);
15542 /* Parse the output-operands. */
15543 if (cp_lexer_next_token_is_not (parser->lexer,
15544 CPP_COLON)
15545 && cp_lexer_next_token_is_not (parser->lexer,
15546 CPP_SCOPE)
15547 && cp_lexer_next_token_is_not (parser->lexer,
15548 CPP_CLOSE_PAREN))
15549 inputs = cp_parser_asm_operand_list (parser);
15551 if (inputs == error_mark_node)
15552 invalid_inputs_p = true;
15554 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15555 /* The clobbers are coming next. */
15556 clobbers_p = true;
15558 /* Look for clobbers. */
15559 if (clobbers_p
15560 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15562 clobbers_p = true;
15563 /* Consume the `:' or `::'. */
15564 cp_lexer_consume_token (parser->lexer);
15565 /* Parse the clobbers. */
15566 if (cp_lexer_next_token_is_not (parser->lexer,
15567 CPP_COLON)
15568 && cp_lexer_next_token_is_not (parser->lexer,
15569 CPP_CLOSE_PAREN))
15570 clobbers = cp_parser_asm_clobber_list (parser);
15572 else if (goto_p
15573 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15574 /* The labels are coming next. */
15575 labels_p = true;
15577 /* Look for labels. */
15578 if (labels_p
15579 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15581 labels_p = true;
15582 /* Consume the `:' or `::'. */
15583 cp_lexer_consume_token (parser->lexer);
15584 /* Parse the labels. */
15585 labels = cp_parser_asm_label_list (parser);
15588 if (goto_p && !labels_p)
15589 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15591 else if (goto_p)
15592 missing = RT_COLON_SCOPE;
15594 /* Look for the closing `)'. */
15595 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15596 missing ? missing : RT_CLOSE_PAREN))
15597 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15598 /*consume_paren=*/true);
15599 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15601 if (!invalid_inputs_p && !invalid_outputs_p)
15603 /* Create the ASM_EXPR. */
15604 if (parser->in_function_body)
15606 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15607 inputs, clobbers, labels);
15608 /* If the extended syntax was not used, mark the ASM_EXPR. */
15609 if (!extended_p)
15611 tree temp = asm_stmt;
15612 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15613 temp = TREE_OPERAND (temp, 0);
15615 ASM_INPUT_P (temp) = 1;
15618 else
15619 add_asm_node (string);
15623 /* Declarators [gram.dcl.decl] */
15625 /* Parse an init-declarator.
15627 init-declarator:
15628 declarator initializer [opt]
15630 GNU Extension:
15632 init-declarator:
15633 declarator asm-specification [opt] attributes [opt] initializer [opt]
15635 function-definition:
15636 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15637 function-body
15638 decl-specifier-seq [opt] declarator function-try-block
15640 GNU Extension:
15642 function-definition:
15643 __extension__ function-definition
15645 TM Extension:
15647 function-definition:
15648 decl-specifier-seq [opt] declarator function-transaction-block
15650 The DECL_SPECIFIERS apply to this declarator. Returns a
15651 representation of the entity declared. If MEMBER_P is TRUE, then
15652 this declarator appears in a class scope. The new DECL created by
15653 this declarator is returned.
15655 The CHECKS are access checks that should be performed once we know
15656 what entity is being declared (and, therefore, what classes have
15657 befriended it).
15659 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15660 for a function-definition here as well. If the declarator is a
15661 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15662 be TRUE upon return. By that point, the function-definition will
15663 have been completely parsed.
15665 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15666 is FALSE.
15668 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15669 parsed declaration if it is an uninitialized single declarator not followed
15670 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15671 if present, will not be consumed. If returned, this declarator will be
15672 created with SD_INITIALIZED but will not call cp_finish_decl. */
15674 static tree
15675 cp_parser_init_declarator (cp_parser* parser,
15676 cp_decl_specifier_seq *decl_specifiers,
15677 vec<deferred_access_check, va_gc> *checks,
15678 bool function_definition_allowed_p,
15679 bool member_p,
15680 int declares_class_or_enum,
15681 bool* function_definition_p,
15682 tree* maybe_range_for_decl)
15684 cp_token *token = NULL, *asm_spec_start_token = NULL,
15685 *attributes_start_token = NULL;
15686 cp_declarator *declarator;
15687 tree prefix_attributes;
15688 tree attributes = NULL;
15689 tree asm_specification;
15690 tree initializer;
15691 tree decl = NULL_TREE;
15692 tree scope;
15693 int is_initialized;
15694 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15695 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15696 "(...)". */
15697 enum cpp_ttype initialization_kind;
15698 bool is_direct_init = false;
15699 bool is_non_constant_init;
15700 int ctor_dtor_or_conv_p;
15701 bool friend_p;
15702 tree pushed_scope = NULL_TREE;
15703 bool range_for_decl_p = false;
15705 /* Gather the attributes that were provided with the
15706 decl-specifiers. */
15707 prefix_attributes = decl_specifiers->attributes;
15709 /* Assume that this is not the declarator for a function
15710 definition. */
15711 if (function_definition_p)
15712 *function_definition_p = false;
15714 /* Defer access checks while parsing the declarator; we cannot know
15715 what names are accessible until we know what is being
15716 declared. */
15717 resume_deferring_access_checks ();
15719 /* Parse the declarator. */
15720 token = cp_lexer_peek_token (parser->lexer);
15721 declarator
15722 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15723 &ctor_dtor_or_conv_p,
15724 /*parenthesized_p=*/NULL,
15725 member_p);
15726 /* Gather up the deferred checks. */
15727 stop_deferring_access_checks ();
15729 /* If the DECLARATOR was erroneous, there's no need to go
15730 further. */
15731 if (declarator == cp_error_declarator)
15732 return error_mark_node;
15734 /* Check that the number of template-parameter-lists is OK. */
15735 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15736 token->location))
15737 return error_mark_node;
15739 if (declares_class_or_enum & 2)
15740 cp_parser_check_for_definition_in_return_type (declarator,
15741 decl_specifiers->type,
15742 decl_specifiers->locations[ds_type_spec]);
15744 /* Figure out what scope the entity declared by the DECLARATOR is
15745 located in. `grokdeclarator' sometimes changes the scope, so
15746 we compute it now. */
15747 scope = get_scope_of_declarator (declarator);
15749 /* Perform any lookups in the declared type which were thought to be
15750 dependent, but are not in the scope of the declarator. */
15751 decl_specifiers->type
15752 = maybe_update_decl_type (decl_specifiers->type, scope);
15754 /* If we're allowing GNU extensions, look for an
15755 asm-specification. */
15756 if (cp_parser_allow_gnu_extensions_p (parser))
15758 /* Look for an asm-specification. */
15759 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15760 asm_specification = cp_parser_asm_specification_opt (parser);
15762 else
15763 asm_specification = NULL_TREE;
15765 /* Look for attributes. */
15766 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15767 attributes = cp_parser_attributes_opt (parser);
15769 /* Peek at the next token. */
15770 token = cp_lexer_peek_token (parser->lexer);
15771 /* Check to see if the token indicates the start of a
15772 function-definition. */
15773 if (function_declarator_p (declarator)
15774 && cp_parser_token_starts_function_definition_p (token))
15776 if (!function_definition_allowed_p)
15778 /* If a function-definition should not appear here, issue an
15779 error message. */
15780 cp_parser_error (parser,
15781 "a function-definition is not allowed here");
15782 return error_mark_node;
15784 else
15786 location_t func_brace_location
15787 = cp_lexer_peek_token (parser->lexer)->location;
15789 /* Neither attributes nor an asm-specification are allowed
15790 on a function-definition. */
15791 if (asm_specification)
15792 error_at (asm_spec_start_token->location,
15793 "an asm-specification is not allowed "
15794 "on a function-definition");
15795 if (attributes)
15796 error_at (attributes_start_token->location,
15797 "attributes are not allowed on a function-definition");
15798 /* This is a function-definition. */
15799 *function_definition_p = true;
15801 /* Parse the function definition. */
15802 if (member_p)
15803 decl = cp_parser_save_member_function_body (parser,
15804 decl_specifiers,
15805 declarator,
15806 prefix_attributes);
15807 else
15808 decl
15809 = (cp_parser_function_definition_from_specifiers_and_declarator
15810 (parser, decl_specifiers, prefix_attributes, declarator));
15812 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15814 /* This is where the prologue starts... */
15815 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15816 = func_brace_location;
15819 return decl;
15823 /* [dcl.dcl]
15825 Only in function declarations for constructors, destructors, and
15826 type conversions can the decl-specifier-seq be omitted.
15828 We explicitly postpone this check past the point where we handle
15829 function-definitions because we tolerate function-definitions
15830 that are missing their return types in some modes. */
15831 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15833 cp_parser_error (parser,
15834 "expected constructor, destructor, or type conversion");
15835 return error_mark_node;
15838 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15839 if (token->type == CPP_EQ
15840 || token->type == CPP_OPEN_PAREN
15841 || token->type == CPP_OPEN_BRACE)
15843 is_initialized = SD_INITIALIZED;
15844 initialization_kind = token->type;
15845 if (maybe_range_for_decl)
15846 *maybe_range_for_decl = error_mark_node;
15848 if (token->type == CPP_EQ
15849 && function_declarator_p (declarator))
15851 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15852 if (t2->keyword == RID_DEFAULT)
15853 is_initialized = SD_DEFAULTED;
15854 else if (t2->keyword == RID_DELETE)
15855 is_initialized = SD_DELETED;
15858 else
15860 /* If the init-declarator isn't initialized and isn't followed by a
15861 `,' or `;', it's not a valid init-declarator. */
15862 if (token->type != CPP_COMMA
15863 && token->type != CPP_SEMICOLON)
15865 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15866 range_for_decl_p = true;
15867 else
15869 cp_parser_error (parser, "expected initializer");
15870 return error_mark_node;
15873 is_initialized = SD_UNINITIALIZED;
15874 initialization_kind = CPP_EOF;
15877 /* Because start_decl has side-effects, we should only call it if we
15878 know we're going ahead. By this point, we know that we cannot
15879 possibly be looking at any other construct. */
15880 cp_parser_commit_to_tentative_parse (parser);
15882 /* If the decl specifiers were bad, issue an error now that we're
15883 sure this was intended to be a declarator. Then continue
15884 declaring the variable(s), as int, to try to cut down on further
15885 errors. */
15886 if (decl_specifiers->any_specifiers_p
15887 && decl_specifiers->type == error_mark_node)
15889 cp_parser_error (parser, "invalid type in declaration");
15890 decl_specifiers->type = integer_type_node;
15893 /* Check to see whether or not this declaration is a friend. */
15894 friend_p = cp_parser_friend_p (decl_specifiers);
15896 /* Enter the newly declared entry in the symbol table. If we're
15897 processing a declaration in a class-specifier, we wait until
15898 after processing the initializer. */
15899 if (!member_p)
15901 if (parser->in_unbraced_linkage_specification_p)
15902 decl_specifiers->storage_class = sc_extern;
15903 decl = start_decl (declarator, decl_specifiers,
15904 range_for_decl_p? SD_INITIALIZED : is_initialized,
15905 attributes, prefix_attributes,
15906 &pushed_scope);
15907 /* Adjust location of decl if declarator->id_loc is more appropriate:
15908 set, and decl wasn't merged with another decl, in which case its
15909 location would be different from input_location, and more accurate. */
15910 if (DECL_P (decl)
15911 && declarator->id_loc != UNKNOWN_LOCATION
15912 && DECL_SOURCE_LOCATION (decl) == input_location)
15913 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15915 else if (scope)
15916 /* Enter the SCOPE. That way unqualified names appearing in the
15917 initializer will be looked up in SCOPE. */
15918 pushed_scope = push_scope (scope);
15920 /* Perform deferred access control checks, now that we know in which
15921 SCOPE the declared entity resides. */
15922 if (!member_p && decl)
15924 tree saved_current_function_decl = NULL_TREE;
15926 /* If the entity being declared is a function, pretend that we
15927 are in its scope. If it is a `friend', it may have access to
15928 things that would not otherwise be accessible. */
15929 if (TREE_CODE (decl) == FUNCTION_DECL)
15931 saved_current_function_decl = current_function_decl;
15932 current_function_decl = decl;
15935 /* Perform access checks for template parameters. */
15936 cp_parser_perform_template_parameter_access_checks (checks);
15938 /* Perform the access control checks for the declarator and the
15939 decl-specifiers. */
15940 perform_deferred_access_checks (tf_warning_or_error);
15942 /* Restore the saved value. */
15943 if (TREE_CODE (decl) == FUNCTION_DECL)
15944 current_function_decl = saved_current_function_decl;
15947 /* Parse the initializer. */
15948 initializer = NULL_TREE;
15949 is_direct_init = false;
15950 is_non_constant_init = true;
15951 if (is_initialized)
15953 if (function_declarator_p (declarator))
15955 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15956 if (initialization_kind == CPP_EQ)
15957 initializer = cp_parser_pure_specifier (parser);
15958 else
15960 /* If the declaration was erroneous, we don't really
15961 know what the user intended, so just silently
15962 consume the initializer. */
15963 if (decl != error_mark_node)
15964 error_at (initializer_start_token->location,
15965 "initializer provided for function");
15966 cp_parser_skip_to_closing_parenthesis (parser,
15967 /*recovering=*/true,
15968 /*or_comma=*/false,
15969 /*consume_paren=*/true);
15972 else
15974 /* We want to record the extra mangling scope for in-class
15975 initializers of class members and initializers of static data
15976 member templates. The former involves deferring
15977 parsing of the initializer until end of class as with default
15978 arguments. So right here we only handle the latter. */
15979 if (!member_p && processing_template_decl)
15980 start_lambda_scope (decl);
15981 initializer = cp_parser_initializer (parser,
15982 &is_direct_init,
15983 &is_non_constant_init);
15984 if (!member_p && processing_template_decl)
15985 finish_lambda_scope ();
15986 if (initializer == error_mark_node)
15987 cp_parser_skip_to_end_of_statement (parser);
15991 /* The old parser allows attributes to appear after a parenthesized
15992 initializer. Mark Mitchell proposed removing this functionality
15993 on the GCC mailing lists on 2002-08-13. This parser accepts the
15994 attributes -- but ignores them. */
15995 if (cp_parser_allow_gnu_extensions_p (parser)
15996 && initialization_kind == CPP_OPEN_PAREN)
15997 if (cp_parser_attributes_opt (parser))
15998 warning (OPT_Wattributes,
15999 "attributes after parenthesized initializer ignored");
16001 /* For an in-class declaration, use `grokfield' to create the
16002 declaration. */
16003 if (member_p)
16005 if (pushed_scope)
16007 pop_scope (pushed_scope);
16008 pushed_scope = NULL_TREE;
16010 decl = grokfield (declarator, decl_specifiers,
16011 initializer, !is_non_constant_init,
16012 /*asmspec=*/NULL_TREE,
16013 prefix_attributes);
16014 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16015 cp_parser_save_default_args (parser, decl);
16018 /* Finish processing the declaration. But, skip member
16019 declarations. */
16020 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16022 cp_finish_decl (decl,
16023 initializer, !is_non_constant_init,
16024 asm_specification,
16025 /* If the initializer is in parentheses, then this is
16026 a direct-initialization, which means that an
16027 `explicit' constructor is OK. Otherwise, an
16028 `explicit' constructor cannot be used. */
16029 ((is_direct_init || !is_initialized)
16030 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16032 else if ((cxx_dialect != cxx98) && friend_p
16033 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16034 /* Core issue #226 (C++0x only): A default template-argument
16035 shall not be specified in a friend class template
16036 declaration. */
16037 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16038 /*is_partial=*/false, /*is_friend_decl=*/1);
16040 if (!friend_p && pushed_scope)
16041 pop_scope (pushed_scope);
16043 return decl;
16046 /* Parse a declarator.
16048 declarator:
16049 direct-declarator
16050 ptr-operator declarator
16052 abstract-declarator:
16053 ptr-operator abstract-declarator [opt]
16054 direct-abstract-declarator
16056 GNU Extensions:
16058 declarator:
16059 attributes [opt] direct-declarator
16060 attributes [opt] ptr-operator declarator
16062 abstract-declarator:
16063 attributes [opt] ptr-operator abstract-declarator [opt]
16064 attributes [opt] direct-abstract-declarator
16066 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16067 detect constructor, destructor or conversion operators. It is set
16068 to -1 if the declarator is a name, and +1 if it is a
16069 function. Otherwise it is set to zero. Usually you just want to
16070 test for >0, but internally the negative value is used.
16072 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16073 a decl-specifier-seq unless it declares a constructor, destructor,
16074 or conversion. It might seem that we could check this condition in
16075 semantic analysis, rather than parsing, but that makes it difficult
16076 to handle something like `f()'. We want to notice that there are
16077 no decl-specifiers, and therefore realize that this is an
16078 expression, not a declaration.)
16080 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16081 the declarator is a direct-declarator of the form "(...)".
16083 MEMBER_P is true iff this declarator is a member-declarator. */
16085 static cp_declarator *
16086 cp_parser_declarator (cp_parser* parser,
16087 cp_parser_declarator_kind dcl_kind,
16088 int* ctor_dtor_or_conv_p,
16089 bool* parenthesized_p,
16090 bool member_p)
16092 cp_declarator *declarator;
16093 enum tree_code code;
16094 cp_cv_quals cv_quals;
16095 tree class_type;
16096 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16098 /* Assume this is not a constructor, destructor, or type-conversion
16099 operator. */
16100 if (ctor_dtor_or_conv_p)
16101 *ctor_dtor_or_conv_p = 0;
16103 if (cp_parser_allow_gnu_extensions_p (parser))
16104 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16106 /* Check for the ptr-operator production. */
16107 cp_parser_parse_tentatively (parser);
16108 /* Parse the ptr-operator. */
16109 code = cp_parser_ptr_operator (parser,
16110 &class_type,
16111 &cv_quals,
16112 &std_attributes);
16114 /* If that worked, then we have a ptr-operator. */
16115 if (cp_parser_parse_definitely (parser))
16117 /* If a ptr-operator was found, then this declarator was not
16118 parenthesized. */
16119 if (parenthesized_p)
16120 *parenthesized_p = true;
16121 /* The dependent declarator is optional if we are parsing an
16122 abstract-declarator. */
16123 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16124 cp_parser_parse_tentatively (parser);
16126 /* Parse the dependent declarator. */
16127 declarator = cp_parser_declarator (parser, dcl_kind,
16128 /*ctor_dtor_or_conv_p=*/NULL,
16129 /*parenthesized_p=*/NULL,
16130 /*member_p=*/false);
16132 /* If we are parsing an abstract-declarator, we must handle the
16133 case where the dependent declarator is absent. */
16134 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16135 && !cp_parser_parse_definitely (parser))
16136 declarator = NULL;
16138 declarator = cp_parser_make_indirect_declarator
16139 (code, class_type, cv_quals, declarator, std_attributes);
16141 /* Everything else is a direct-declarator. */
16142 else
16144 if (parenthesized_p)
16145 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
16146 CPP_OPEN_PAREN);
16147 declarator = cp_parser_direct_declarator (parser, dcl_kind,
16148 ctor_dtor_or_conv_p,
16149 member_p);
16152 if (gnu_attributes && declarator && declarator != cp_error_declarator)
16153 declarator->attributes = gnu_attributes;
16154 return declarator;
16157 /* Parse a direct-declarator or direct-abstract-declarator.
16159 direct-declarator:
16160 declarator-id
16161 direct-declarator ( parameter-declaration-clause )
16162 cv-qualifier-seq [opt]
16163 exception-specification [opt]
16164 direct-declarator [ constant-expression [opt] ]
16165 ( declarator )
16167 direct-abstract-declarator:
16168 direct-abstract-declarator [opt]
16169 ( parameter-declaration-clause )
16170 cv-qualifier-seq [opt]
16171 exception-specification [opt]
16172 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16173 ( abstract-declarator )
16175 Returns a representation of the declarator. DCL_KIND is
16176 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16177 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16178 we are parsing a direct-declarator. It is
16179 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16180 of ambiguity we prefer an abstract declarator, as per
16181 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16182 cp_parser_declarator. */
16184 static cp_declarator *
16185 cp_parser_direct_declarator (cp_parser* parser,
16186 cp_parser_declarator_kind dcl_kind,
16187 int* ctor_dtor_or_conv_p,
16188 bool member_p)
16190 cp_token *token;
16191 cp_declarator *declarator = NULL;
16192 tree scope = NULL_TREE;
16193 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16194 bool saved_in_declarator_p = parser->in_declarator_p;
16195 bool first = true;
16196 tree pushed_scope = NULL_TREE;
16198 while (true)
16200 /* Peek at the next token. */
16201 token = cp_lexer_peek_token (parser->lexer);
16202 if (token->type == CPP_OPEN_PAREN)
16204 /* This is either a parameter-declaration-clause, or a
16205 parenthesized declarator. When we know we are parsing a
16206 named declarator, it must be a parenthesized declarator
16207 if FIRST is true. For instance, `(int)' is a
16208 parameter-declaration-clause, with an omitted
16209 direct-abstract-declarator. But `((*))', is a
16210 parenthesized abstract declarator. Finally, when T is a
16211 template parameter `(T)' is a
16212 parameter-declaration-clause, and not a parenthesized
16213 named declarator.
16215 We first try and parse a parameter-declaration-clause,
16216 and then try a nested declarator (if FIRST is true).
16218 It is not an error for it not to be a
16219 parameter-declaration-clause, even when FIRST is
16220 false. Consider,
16222 int i (int);
16223 int i (3);
16225 The first is the declaration of a function while the
16226 second is the definition of a variable, including its
16227 initializer.
16229 Having seen only the parenthesis, we cannot know which of
16230 these two alternatives should be selected. Even more
16231 complex are examples like:
16233 int i (int (a));
16234 int i (int (3));
16236 The former is a function-declaration; the latter is a
16237 variable initialization.
16239 Thus again, we try a parameter-declaration-clause, and if
16240 that fails, we back out and return. */
16242 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16244 tree params;
16245 unsigned saved_num_template_parameter_lists;
16246 bool is_declarator = false;
16247 tree t;
16249 /* In a member-declarator, the only valid interpretation
16250 of a parenthesis is the start of a
16251 parameter-declaration-clause. (It is invalid to
16252 initialize a static data member with a parenthesized
16253 initializer; only the "=" form of initialization is
16254 permitted.) */
16255 if (!member_p)
16256 cp_parser_parse_tentatively (parser);
16258 /* Consume the `('. */
16259 cp_lexer_consume_token (parser->lexer);
16260 if (first)
16262 /* If this is going to be an abstract declarator, we're
16263 in a declarator and we can't have default args. */
16264 parser->default_arg_ok_p = false;
16265 parser->in_declarator_p = true;
16268 /* Inside the function parameter list, surrounding
16269 template-parameter-lists do not apply. */
16270 saved_num_template_parameter_lists
16271 = parser->num_template_parameter_lists;
16272 parser->num_template_parameter_lists = 0;
16274 begin_scope (sk_function_parms, NULL_TREE);
16276 /* Parse the parameter-declaration-clause. */
16277 params = cp_parser_parameter_declaration_clause (parser);
16279 parser->num_template_parameter_lists
16280 = saved_num_template_parameter_lists;
16282 /* Consume the `)'. */
16283 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16285 /* If all went well, parse the cv-qualifier-seq and the
16286 exception-specification. */
16287 if (member_p || cp_parser_parse_definitely (parser))
16289 cp_cv_quals cv_quals;
16290 cp_virt_specifiers virt_specifiers;
16291 tree exception_specification;
16292 tree late_return;
16293 tree attrs;
16295 is_declarator = true;
16297 if (ctor_dtor_or_conv_p)
16298 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16299 first = false;
16301 /* Parse the cv-qualifier-seq. */
16302 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16303 /* And the exception-specification. */
16304 exception_specification
16305 = cp_parser_exception_specification_opt (parser);
16307 attrs = cp_parser_std_attribute_spec_seq (parser);
16309 late_return = (cp_parser_late_return_type_opt
16310 (parser, member_p ? cv_quals : -1));
16312 /* Parse the virt-specifier-seq. */
16313 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16315 /* Create the function-declarator. */
16316 declarator = make_call_declarator (declarator,
16317 params,
16318 cv_quals,
16319 virt_specifiers,
16320 exception_specification,
16321 late_return);
16322 declarator->std_attributes = attrs;
16323 /* Any subsequent parameter lists are to do with
16324 return type, so are not those of the declared
16325 function. */
16326 parser->default_arg_ok_p = false;
16329 /* Remove the function parms from scope. */
16330 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16331 pop_binding (DECL_NAME (t), t);
16332 leave_scope();
16334 if (is_declarator)
16335 /* Repeat the main loop. */
16336 continue;
16339 /* If this is the first, we can try a parenthesized
16340 declarator. */
16341 if (first)
16343 bool saved_in_type_id_in_expr_p;
16345 parser->default_arg_ok_p = saved_default_arg_ok_p;
16346 parser->in_declarator_p = saved_in_declarator_p;
16348 /* Consume the `('. */
16349 cp_lexer_consume_token (parser->lexer);
16350 /* Parse the nested declarator. */
16351 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16352 parser->in_type_id_in_expr_p = true;
16353 declarator
16354 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16355 /*parenthesized_p=*/NULL,
16356 member_p);
16357 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16358 first = false;
16359 /* Expect a `)'. */
16360 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16361 declarator = cp_error_declarator;
16362 if (declarator == cp_error_declarator)
16363 break;
16365 goto handle_declarator;
16367 /* Otherwise, we must be done. */
16368 else
16369 break;
16371 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16372 && token->type == CPP_OPEN_SQUARE
16373 && !cp_next_tokens_can_be_attribute_p (parser))
16375 /* Parse an array-declarator. */
16376 tree bounds, attrs;
16378 if (ctor_dtor_or_conv_p)
16379 *ctor_dtor_or_conv_p = 0;
16381 first = false;
16382 parser->default_arg_ok_p = false;
16383 parser->in_declarator_p = true;
16384 /* Consume the `['. */
16385 cp_lexer_consume_token (parser->lexer);
16386 /* Peek at the next token. */
16387 token = cp_lexer_peek_token (parser->lexer);
16388 /* If the next token is `]', then there is no
16389 constant-expression. */
16390 if (token->type != CPP_CLOSE_SQUARE)
16392 bool non_constant_p;
16394 bounds
16395 = cp_parser_constant_expression (parser,
16396 /*allow_non_constant=*/true,
16397 &non_constant_p);
16398 if (!non_constant_p)
16399 /* OK */;
16400 else if (error_operand_p (bounds))
16401 /* Already gave an error. */;
16402 else if (!parser->in_function_body
16403 || current_binding_level->kind == sk_function_parms)
16405 /* Normally, the array bound must be an integral constant
16406 expression. However, as an extension, we allow VLAs
16407 in function scopes as long as they aren't part of a
16408 parameter declaration. */
16409 cp_parser_error (parser,
16410 "array bound is not an integer constant");
16411 bounds = error_mark_node;
16413 else if (processing_template_decl)
16415 /* Remember this wasn't a constant-expression. */
16416 bounds = build_nop (TREE_TYPE (bounds), bounds);
16417 TREE_SIDE_EFFECTS (bounds) = 1;
16420 else
16421 bounds = NULL_TREE;
16422 /* Look for the closing `]'. */
16423 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16425 declarator = cp_error_declarator;
16426 break;
16429 attrs = cp_parser_std_attribute_spec_seq (parser);
16430 declarator = make_array_declarator (declarator, bounds);
16431 declarator->std_attributes = attrs;
16433 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16436 tree qualifying_scope;
16437 tree unqualified_name;
16438 tree attrs;
16439 special_function_kind sfk;
16440 bool abstract_ok;
16441 bool pack_expansion_p = false;
16442 cp_token *declarator_id_start_token;
16444 /* Parse a declarator-id */
16445 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16446 if (abstract_ok)
16448 cp_parser_parse_tentatively (parser);
16450 /* If we see an ellipsis, we should be looking at a
16451 parameter pack. */
16452 if (token->type == CPP_ELLIPSIS)
16454 /* Consume the `...' */
16455 cp_lexer_consume_token (parser->lexer);
16457 pack_expansion_p = true;
16461 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16462 unqualified_name
16463 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16464 qualifying_scope = parser->scope;
16465 if (abstract_ok)
16467 bool okay = false;
16469 if (!unqualified_name && pack_expansion_p)
16471 /* Check whether an error occurred. */
16472 okay = !cp_parser_error_occurred (parser);
16474 /* We already consumed the ellipsis to mark a
16475 parameter pack, but we have no way to report it,
16476 so abort the tentative parse. We will be exiting
16477 immediately anyway. */
16478 cp_parser_abort_tentative_parse (parser);
16480 else
16481 okay = cp_parser_parse_definitely (parser);
16483 if (!okay)
16484 unqualified_name = error_mark_node;
16485 else if (unqualified_name
16486 && (qualifying_scope
16487 || (TREE_CODE (unqualified_name)
16488 != IDENTIFIER_NODE)))
16490 cp_parser_error (parser, "expected unqualified-id");
16491 unqualified_name = error_mark_node;
16495 if (!unqualified_name)
16496 return NULL;
16497 if (unqualified_name == error_mark_node)
16499 declarator = cp_error_declarator;
16500 pack_expansion_p = false;
16501 declarator->parameter_pack_p = false;
16502 break;
16505 attrs = cp_parser_std_attribute_spec_seq (parser);
16507 if (qualifying_scope && at_namespace_scope_p ()
16508 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16510 /* In the declaration of a member of a template class
16511 outside of the class itself, the SCOPE will sometimes
16512 be a TYPENAME_TYPE. For example, given:
16514 template <typename T>
16515 int S<T>::R::i = 3;
16517 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16518 this context, we must resolve S<T>::R to an ordinary
16519 type, rather than a typename type.
16521 The reason we normally avoid resolving TYPENAME_TYPEs
16522 is that a specialization of `S' might render
16523 `S<T>::R' not a type. However, if `S' is
16524 specialized, then this `i' will not be used, so there
16525 is no harm in resolving the types here. */
16526 tree type;
16528 /* Resolve the TYPENAME_TYPE. */
16529 type = resolve_typename_type (qualifying_scope,
16530 /*only_current_p=*/false);
16531 /* If that failed, the declarator is invalid. */
16532 if (TREE_CODE (type) == TYPENAME_TYPE)
16534 if (typedef_variant_p (type))
16535 error_at (declarator_id_start_token->location,
16536 "cannot define member of dependent typedef "
16537 "%qT", type);
16538 else
16539 error_at (declarator_id_start_token->location,
16540 "%<%T::%E%> is not a type",
16541 TYPE_CONTEXT (qualifying_scope),
16542 TYPE_IDENTIFIER (qualifying_scope));
16544 qualifying_scope = type;
16547 sfk = sfk_none;
16549 if (unqualified_name)
16551 tree class_type;
16553 if (qualifying_scope
16554 && CLASS_TYPE_P (qualifying_scope))
16555 class_type = qualifying_scope;
16556 else
16557 class_type = current_class_type;
16559 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16561 tree name_type = TREE_TYPE (unqualified_name);
16562 if (class_type && same_type_p (name_type, class_type))
16564 if (qualifying_scope
16565 && CLASSTYPE_USE_TEMPLATE (name_type))
16567 error_at (declarator_id_start_token->location,
16568 "invalid use of constructor as a template");
16569 inform (declarator_id_start_token->location,
16570 "use %<%T::%D%> instead of %<%T::%D%> to "
16571 "name the constructor in a qualified name",
16572 class_type,
16573 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16574 class_type, name_type);
16575 declarator = cp_error_declarator;
16576 break;
16578 else
16579 unqualified_name = constructor_name (class_type);
16581 else
16583 /* We do not attempt to print the declarator
16584 here because we do not have enough
16585 information about its original syntactic
16586 form. */
16587 cp_parser_error (parser, "invalid declarator");
16588 declarator = cp_error_declarator;
16589 break;
16593 if (class_type)
16595 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16596 sfk = sfk_destructor;
16597 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16598 sfk = sfk_conversion;
16599 else if (/* There's no way to declare a constructor
16600 for an anonymous type, even if the type
16601 got a name for linkage purposes. */
16602 !TYPE_WAS_ANONYMOUS (class_type)
16603 && constructor_name_p (unqualified_name,
16604 class_type))
16606 unqualified_name = constructor_name (class_type);
16607 sfk = sfk_constructor;
16609 else if (is_overloaded_fn (unqualified_name)
16610 && DECL_CONSTRUCTOR_P (get_first_fn
16611 (unqualified_name)))
16612 sfk = sfk_constructor;
16614 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16615 *ctor_dtor_or_conv_p = -1;
16618 declarator = make_id_declarator (qualifying_scope,
16619 unqualified_name,
16620 sfk);
16621 declarator->std_attributes = attrs;
16622 declarator->id_loc = token->location;
16623 declarator->parameter_pack_p = pack_expansion_p;
16625 if (pack_expansion_p)
16626 maybe_warn_variadic_templates ();
16629 handle_declarator:;
16630 scope = get_scope_of_declarator (declarator);
16631 if (scope)
16632 /* Any names that appear after the declarator-id for a
16633 member are looked up in the containing scope. */
16634 pushed_scope = push_scope (scope);
16635 parser->in_declarator_p = true;
16636 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16637 || (declarator && declarator->kind == cdk_id))
16638 /* Default args are only allowed on function
16639 declarations. */
16640 parser->default_arg_ok_p = saved_default_arg_ok_p;
16641 else
16642 parser->default_arg_ok_p = false;
16644 first = false;
16646 /* We're done. */
16647 else
16648 break;
16651 /* For an abstract declarator, we might wind up with nothing at this
16652 point. That's an error; the declarator is not optional. */
16653 if (!declarator)
16654 cp_parser_error (parser, "expected declarator");
16656 /* If we entered a scope, we must exit it now. */
16657 if (pushed_scope)
16658 pop_scope (pushed_scope);
16660 parser->default_arg_ok_p = saved_default_arg_ok_p;
16661 parser->in_declarator_p = saved_in_declarator_p;
16663 return declarator;
16666 /* Parse a ptr-operator.
16668 ptr-operator:
16669 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16670 * cv-qualifier-seq [opt]
16672 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16673 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16675 GNU Extension:
16677 ptr-operator:
16678 & cv-qualifier-seq [opt]
16680 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16681 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16682 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16683 filled in with the TYPE containing the member. *CV_QUALS is
16684 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16685 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16686 Note that the tree codes returned by this function have nothing
16687 to do with the types of trees that will be eventually be created
16688 to represent the pointer or reference type being parsed. They are
16689 just constants with suggestive names. */
16690 static enum tree_code
16691 cp_parser_ptr_operator (cp_parser* parser,
16692 tree* type,
16693 cp_cv_quals *cv_quals,
16694 tree *attributes)
16696 enum tree_code code = ERROR_MARK;
16697 cp_token *token;
16698 tree attrs = NULL_TREE;
16700 /* Assume that it's not a pointer-to-member. */
16701 *type = NULL_TREE;
16702 /* And that there are no cv-qualifiers. */
16703 *cv_quals = TYPE_UNQUALIFIED;
16705 /* Peek at the next token. */
16706 token = cp_lexer_peek_token (parser->lexer);
16708 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16709 if (token->type == CPP_MULT)
16710 code = INDIRECT_REF;
16711 else if (token->type == CPP_AND)
16712 code = ADDR_EXPR;
16713 else if ((cxx_dialect != cxx98) &&
16714 token->type == CPP_AND_AND) /* C++0x only */
16715 code = NON_LVALUE_EXPR;
16717 if (code != ERROR_MARK)
16719 /* Consume the `*', `&' or `&&'. */
16720 cp_lexer_consume_token (parser->lexer);
16722 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16723 `&', if we are allowing GNU extensions. (The only qualifier
16724 that can legally appear after `&' is `restrict', but that is
16725 enforced during semantic analysis. */
16726 if (code == INDIRECT_REF
16727 || cp_parser_allow_gnu_extensions_p (parser))
16728 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16730 attrs = cp_parser_std_attribute_spec_seq (parser);
16731 if (attributes != NULL)
16732 *attributes = attrs;
16734 else
16736 /* Try the pointer-to-member case. */
16737 cp_parser_parse_tentatively (parser);
16738 /* Look for the optional `::' operator. */
16739 cp_parser_global_scope_opt (parser,
16740 /*current_scope_valid_p=*/false);
16741 /* Look for the nested-name specifier. */
16742 token = cp_lexer_peek_token (parser->lexer);
16743 cp_parser_nested_name_specifier (parser,
16744 /*typename_keyword_p=*/false,
16745 /*check_dependency_p=*/true,
16746 /*type_p=*/false,
16747 /*is_declaration=*/false);
16748 /* If we found it, and the next token is a `*', then we are
16749 indeed looking at a pointer-to-member operator. */
16750 if (!cp_parser_error_occurred (parser)
16751 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16753 /* Indicate that the `*' operator was used. */
16754 code = INDIRECT_REF;
16756 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16757 error_at (token->location, "%qD is a namespace", parser->scope);
16758 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16759 error_at (token->location, "cannot form pointer to member of "
16760 "non-class %q#T", parser->scope);
16761 else
16763 /* The type of which the member is a member is given by the
16764 current SCOPE. */
16765 *type = parser->scope;
16766 /* The next name will not be qualified. */
16767 parser->scope = NULL_TREE;
16768 parser->qualifying_scope = NULL_TREE;
16769 parser->object_scope = NULL_TREE;
16770 /* Look for optional c++11 attributes. */
16771 attrs = cp_parser_std_attribute_spec_seq (parser);
16772 if (attributes != NULL)
16773 *attributes = attrs;
16774 /* Look for the optional cv-qualifier-seq. */
16775 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16778 /* If that didn't work we don't have a ptr-operator. */
16779 if (!cp_parser_parse_definitely (parser))
16780 cp_parser_error (parser, "expected ptr-operator");
16783 return code;
16786 /* Parse an (optional) cv-qualifier-seq.
16788 cv-qualifier-seq:
16789 cv-qualifier cv-qualifier-seq [opt]
16791 cv-qualifier:
16792 const
16793 volatile
16795 GNU Extension:
16797 cv-qualifier:
16798 __restrict__
16800 Returns a bitmask representing the cv-qualifiers. */
16802 static cp_cv_quals
16803 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16805 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16807 while (true)
16809 cp_token *token;
16810 cp_cv_quals cv_qualifier;
16812 /* Peek at the next token. */
16813 token = cp_lexer_peek_token (parser->lexer);
16814 /* See if it's a cv-qualifier. */
16815 switch (token->keyword)
16817 case RID_CONST:
16818 cv_qualifier = TYPE_QUAL_CONST;
16819 break;
16821 case RID_VOLATILE:
16822 cv_qualifier = TYPE_QUAL_VOLATILE;
16823 break;
16825 case RID_RESTRICT:
16826 cv_qualifier = TYPE_QUAL_RESTRICT;
16827 break;
16829 default:
16830 cv_qualifier = TYPE_UNQUALIFIED;
16831 break;
16834 if (!cv_qualifier)
16835 break;
16837 if (cv_quals & cv_qualifier)
16839 error_at (token->location, "duplicate cv-qualifier");
16840 cp_lexer_purge_token (parser->lexer);
16842 else
16844 cp_lexer_consume_token (parser->lexer);
16845 cv_quals |= cv_qualifier;
16849 return cv_quals;
16852 /* Parse an (optional) virt-specifier-seq.
16854 virt-specifier-seq:
16855 virt-specifier virt-specifier-seq [opt]
16857 virt-specifier:
16858 override
16859 final
16861 Returns a bitmask representing the virt-specifiers. */
16863 static cp_virt_specifiers
16864 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16866 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16868 while (true)
16870 cp_token *token;
16871 cp_virt_specifiers virt_specifier;
16873 /* Peek at the next token. */
16874 token = cp_lexer_peek_token (parser->lexer);
16875 /* See if it's a virt-specifier-qualifier. */
16876 if (token->type != CPP_NAME)
16877 break;
16878 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16880 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16881 virt_specifier = VIRT_SPEC_OVERRIDE;
16883 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16885 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16886 virt_specifier = VIRT_SPEC_FINAL;
16888 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16890 virt_specifier = VIRT_SPEC_FINAL;
16892 else
16893 break;
16895 if (virt_specifiers & virt_specifier)
16897 error_at (token->location, "duplicate virt-specifier");
16898 cp_lexer_purge_token (parser->lexer);
16900 else
16902 cp_lexer_consume_token (parser->lexer);
16903 virt_specifiers |= virt_specifier;
16906 return virt_specifiers;
16909 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16910 is in scope even though it isn't real. */
16912 static void
16913 inject_this_parameter (tree ctype, cp_cv_quals quals)
16915 tree this_parm;
16917 if (current_class_ptr)
16919 /* We don't clear this between NSDMIs. Is it already what we want? */
16920 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16921 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16922 && cp_type_quals (type) == quals)
16923 return;
16926 this_parm = build_this_parm (ctype, quals);
16927 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16928 current_class_ptr = NULL_TREE;
16929 current_class_ref
16930 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16931 current_class_ptr = this_parm;
16934 /* Parse a late-specified return type, if any. This is not a separate
16935 non-terminal, but part of a function declarator, which looks like
16937 -> trailing-type-specifier-seq abstract-declarator(opt)
16939 Returns the type indicated by the type-id.
16941 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16942 function. */
16944 static tree
16945 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16947 cp_token *token;
16948 tree type;
16950 /* Peek at the next token. */
16951 token = cp_lexer_peek_token (parser->lexer);
16952 /* A late-specified return type is indicated by an initial '->'. */
16953 if (token->type != CPP_DEREF)
16954 return NULL_TREE;
16956 /* Consume the ->. */
16957 cp_lexer_consume_token (parser->lexer);
16959 if (quals >= 0)
16961 /* DR 1207: 'this' is in scope in the trailing return type. */
16962 gcc_assert (current_class_ptr == NULL_TREE);
16963 inject_this_parameter (current_class_type, quals);
16966 type = cp_parser_trailing_type_id (parser);
16968 if (quals >= 0)
16969 current_class_ptr = current_class_ref = NULL_TREE;
16971 return type;
16974 /* Parse a declarator-id.
16976 declarator-id:
16977 id-expression
16978 :: [opt] nested-name-specifier [opt] type-name
16980 In the `id-expression' case, the value returned is as for
16981 cp_parser_id_expression if the id-expression was an unqualified-id.
16982 If the id-expression was a qualified-id, then a SCOPE_REF is
16983 returned. The first operand is the scope (either a NAMESPACE_DECL
16984 or TREE_TYPE), but the second is still just a representation of an
16985 unqualified-id. */
16987 static tree
16988 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16990 tree id;
16991 /* The expression must be an id-expression. Assume that qualified
16992 names are the names of types so that:
16994 template <class T>
16995 int S<T>::R::i = 3;
16997 will work; we must treat `S<T>::R' as the name of a type.
16998 Similarly, assume that qualified names are templates, where
16999 required, so that:
17001 template <class T>
17002 int S<T>::R<T>::i = 3;
17004 will work, too. */
17005 id = cp_parser_id_expression (parser,
17006 /*template_keyword_p=*/false,
17007 /*check_dependency_p=*/false,
17008 /*template_p=*/NULL,
17009 /*declarator_p=*/true,
17010 optional_p);
17011 if (id && BASELINK_P (id))
17012 id = BASELINK_FUNCTIONS (id);
17013 return id;
17016 /* Parse a type-id.
17018 type-id:
17019 type-specifier-seq abstract-declarator [opt]
17021 Returns the TYPE specified. */
17023 static tree
17024 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17025 bool is_trailing_return)
17027 cp_decl_specifier_seq type_specifier_seq;
17028 cp_declarator *abstract_declarator;
17030 /* Parse the type-specifier-seq. */
17031 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17032 is_trailing_return,
17033 &type_specifier_seq);
17034 if (type_specifier_seq.type == error_mark_node)
17035 return error_mark_node;
17037 /* There might or might not be an abstract declarator. */
17038 cp_parser_parse_tentatively (parser);
17039 /* Look for the declarator. */
17040 abstract_declarator
17041 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
17042 /*parenthesized_p=*/NULL,
17043 /*member_p=*/false);
17044 /* Check to see if there really was a declarator. */
17045 if (!cp_parser_parse_definitely (parser))
17046 abstract_declarator = NULL;
17048 if (type_specifier_seq.type
17049 && type_uses_auto (type_specifier_seq.type))
17051 /* A type-id with type 'auto' is only ok if the abstract declarator
17052 is a function declarator with a late-specified return type. */
17053 if (abstract_declarator
17054 && abstract_declarator->kind == cdk_function
17055 && abstract_declarator->u.function.late_return_type)
17056 /* OK */;
17057 else
17059 error ("invalid use of %<auto%>");
17060 return error_mark_node;
17064 return groktypename (&type_specifier_seq, abstract_declarator,
17065 is_template_arg);
17068 static tree cp_parser_type_id (cp_parser *parser)
17070 return cp_parser_type_id_1 (parser, false, false);
17073 static tree cp_parser_template_type_arg (cp_parser *parser)
17075 tree r;
17076 const char *saved_message = parser->type_definition_forbidden_message;
17077 parser->type_definition_forbidden_message
17078 = G_("types may not be defined in template arguments");
17079 r = cp_parser_type_id_1 (parser, true, false);
17080 parser->type_definition_forbidden_message = saved_message;
17081 return r;
17084 static tree cp_parser_trailing_type_id (cp_parser *parser)
17086 return cp_parser_type_id_1 (parser, false, true);
17089 /* Parse a type-specifier-seq.
17091 type-specifier-seq:
17092 type-specifier type-specifier-seq [opt]
17094 GNU extension:
17096 type-specifier-seq:
17097 attributes type-specifier-seq [opt]
17099 If IS_DECLARATION is true, we are at the start of a "condition" or
17100 exception-declaration, so we might be followed by a declarator-id.
17102 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17103 i.e. we've just seen "->".
17105 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17107 static void
17108 cp_parser_type_specifier_seq (cp_parser* parser,
17109 bool is_declaration,
17110 bool is_trailing_return,
17111 cp_decl_specifier_seq *type_specifier_seq)
17113 bool seen_type_specifier = false;
17114 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
17115 cp_token *start_token = NULL;
17117 /* Clear the TYPE_SPECIFIER_SEQ. */
17118 clear_decl_specs (type_specifier_seq);
17120 /* In the context of a trailing return type, enum E { } is an
17121 elaborated-type-specifier followed by a function-body, not an
17122 enum-specifier. */
17123 if (is_trailing_return)
17124 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
17126 /* Parse the type-specifiers and attributes. */
17127 while (true)
17129 tree type_specifier;
17130 bool is_cv_qualifier;
17132 /* Check for attributes first. */
17133 if (cp_next_tokens_can_be_attribute_p (parser))
17135 type_specifier_seq->attributes =
17136 chainon (type_specifier_seq->attributes,
17137 cp_parser_attributes_opt (parser));
17138 continue;
17141 /* record the token of the beginning of the type specifier seq,
17142 for error reporting purposes*/
17143 if (!start_token)
17144 start_token = cp_lexer_peek_token (parser->lexer);
17146 /* Look for the type-specifier. */
17147 type_specifier = cp_parser_type_specifier (parser,
17148 flags,
17149 type_specifier_seq,
17150 /*is_declaration=*/false,
17151 NULL,
17152 &is_cv_qualifier);
17153 if (!type_specifier)
17155 /* If the first type-specifier could not be found, this is not a
17156 type-specifier-seq at all. */
17157 if (!seen_type_specifier)
17159 cp_parser_error (parser, "expected type-specifier");
17160 type_specifier_seq->type = error_mark_node;
17161 return;
17163 /* If subsequent type-specifiers could not be found, the
17164 type-specifier-seq is complete. */
17165 break;
17168 seen_type_specifier = true;
17169 /* The standard says that a condition can be:
17171 type-specifier-seq declarator = assignment-expression
17173 However, given:
17175 struct S {};
17176 if (int S = ...)
17178 we should treat the "S" as a declarator, not as a
17179 type-specifier. The standard doesn't say that explicitly for
17180 type-specifier-seq, but it does say that for
17181 decl-specifier-seq in an ordinary declaration. Perhaps it
17182 would be clearer just to allow a decl-specifier-seq here, and
17183 then add a semantic restriction that if any decl-specifiers
17184 that are not type-specifiers appear, the program is invalid. */
17185 if (is_declaration && !is_cv_qualifier)
17186 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
17190 /* Parse a parameter-declaration-clause.
17192 parameter-declaration-clause:
17193 parameter-declaration-list [opt] ... [opt]
17194 parameter-declaration-list , ...
17196 Returns a representation for the parameter declarations. A return
17197 value of NULL indicates a parameter-declaration-clause consisting
17198 only of an ellipsis. */
17200 static tree
17201 cp_parser_parameter_declaration_clause (cp_parser* parser)
17203 tree parameters;
17204 cp_token *token;
17205 bool ellipsis_p;
17206 bool is_error;
17208 /* Peek at the next token. */
17209 token = cp_lexer_peek_token (parser->lexer);
17210 /* Check for trivial parameter-declaration-clauses. */
17211 if (token->type == CPP_ELLIPSIS)
17213 /* Consume the `...' token. */
17214 cp_lexer_consume_token (parser->lexer);
17215 return NULL_TREE;
17217 else if (token->type == CPP_CLOSE_PAREN)
17218 /* There are no parameters. */
17220 #ifndef NO_IMPLICIT_EXTERN_C
17221 if (in_system_header && current_class_type == NULL
17222 && current_lang_name == lang_name_c)
17223 return NULL_TREE;
17224 else
17225 #endif
17226 return void_list_node;
17228 /* Check for `(void)', too, which is a special case. */
17229 else if (token->keyword == RID_VOID
17230 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17231 == CPP_CLOSE_PAREN))
17233 /* Consume the `void' token. */
17234 cp_lexer_consume_token (parser->lexer);
17235 /* There are no parameters. */
17236 return void_list_node;
17239 /* Parse the parameter-declaration-list. */
17240 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17241 /* If a parse error occurred while parsing the
17242 parameter-declaration-list, then the entire
17243 parameter-declaration-clause is erroneous. */
17244 if (is_error)
17245 return NULL;
17247 /* Peek at the next token. */
17248 token = cp_lexer_peek_token (parser->lexer);
17249 /* If it's a `,', the clause should terminate with an ellipsis. */
17250 if (token->type == CPP_COMMA)
17252 /* Consume the `,'. */
17253 cp_lexer_consume_token (parser->lexer);
17254 /* Expect an ellipsis. */
17255 ellipsis_p
17256 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17258 /* It might also be `...' if the optional trailing `,' was
17259 omitted. */
17260 else if (token->type == CPP_ELLIPSIS)
17262 /* Consume the `...' token. */
17263 cp_lexer_consume_token (parser->lexer);
17264 /* And remember that we saw it. */
17265 ellipsis_p = true;
17267 else
17268 ellipsis_p = false;
17270 /* Finish the parameter list. */
17271 if (!ellipsis_p)
17272 parameters = chainon (parameters, void_list_node);
17274 return parameters;
17277 /* Parse a parameter-declaration-list.
17279 parameter-declaration-list:
17280 parameter-declaration
17281 parameter-declaration-list , parameter-declaration
17283 Returns a representation of the parameter-declaration-list, as for
17284 cp_parser_parameter_declaration_clause. However, the
17285 `void_list_node' is never appended to the list. Upon return,
17286 *IS_ERROR will be true iff an error occurred. */
17288 static tree
17289 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17291 tree parameters = NULL_TREE;
17292 tree *tail = &parameters;
17293 bool saved_in_unbraced_linkage_specification_p;
17294 int index = 0;
17296 /* Assume all will go well. */
17297 *is_error = false;
17298 /* The special considerations that apply to a function within an
17299 unbraced linkage specifications do not apply to the parameters
17300 to the function. */
17301 saved_in_unbraced_linkage_specification_p
17302 = parser->in_unbraced_linkage_specification_p;
17303 parser->in_unbraced_linkage_specification_p = false;
17305 /* Look for more parameters. */
17306 while (true)
17308 cp_parameter_declarator *parameter;
17309 tree decl = error_mark_node;
17310 bool parenthesized_p = false;
17311 /* Parse the parameter. */
17312 parameter
17313 = cp_parser_parameter_declaration (parser,
17314 /*template_parm_p=*/false,
17315 &parenthesized_p);
17317 /* We don't know yet if the enclosing context is deprecated, so wait
17318 and warn in grokparms if appropriate. */
17319 deprecated_state = DEPRECATED_SUPPRESS;
17321 if (parameter)
17322 decl = grokdeclarator (parameter->declarator,
17323 &parameter->decl_specifiers,
17324 PARM,
17325 parameter->default_argument != NULL_TREE,
17326 &parameter->decl_specifiers.attributes);
17328 deprecated_state = DEPRECATED_NORMAL;
17330 /* If a parse error occurred parsing the parameter declaration,
17331 then the entire parameter-declaration-list is erroneous. */
17332 if (decl == error_mark_node)
17334 *is_error = true;
17335 parameters = error_mark_node;
17336 break;
17339 if (parameter->decl_specifiers.attributes)
17340 cplus_decl_attributes (&decl,
17341 parameter->decl_specifiers.attributes,
17343 if (DECL_NAME (decl))
17344 decl = pushdecl (decl);
17346 if (decl != error_mark_node)
17348 retrofit_lang_decl (decl);
17349 DECL_PARM_INDEX (decl) = ++index;
17350 DECL_PARM_LEVEL (decl) = function_parm_depth ();
17353 /* Add the new parameter to the list. */
17354 *tail = build_tree_list (parameter->default_argument, decl);
17355 tail = &TREE_CHAIN (*tail);
17357 /* Peek at the next token. */
17358 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17359 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17360 /* These are for Objective-C++ */
17361 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17362 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17363 /* The parameter-declaration-list is complete. */
17364 break;
17365 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17367 cp_token *token;
17369 /* Peek at the next token. */
17370 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17371 /* If it's an ellipsis, then the list is complete. */
17372 if (token->type == CPP_ELLIPSIS)
17373 break;
17374 /* Otherwise, there must be more parameters. Consume the
17375 `,'. */
17376 cp_lexer_consume_token (parser->lexer);
17377 /* When parsing something like:
17379 int i(float f, double d)
17381 we can tell after seeing the declaration for "f" that we
17382 are not looking at an initialization of a variable "i",
17383 but rather at the declaration of a function "i".
17385 Due to the fact that the parsing of template arguments
17386 (as specified to a template-id) requires backtracking we
17387 cannot use this technique when inside a template argument
17388 list. */
17389 if (!parser->in_template_argument_list_p
17390 && !parser->in_type_id_in_expr_p
17391 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17392 /* However, a parameter-declaration of the form
17393 "foat(f)" (which is a valid declaration of a
17394 parameter "f") can also be interpreted as an
17395 expression (the conversion of "f" to "float"). */
17396 && !parenthesized_p)
17397 cp_parser_commit_to_tentative_parse (parser);
17399 else
17401 cp_parser_error (parser, "expected %<,%> or %<...%>");
17402 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17403 cp_parser_skip_to_closing_parenthesis (parser,
17404 /*recovering=*/true,
17405 /*or_comma=*/false,
17406 /*consume_paren=*/false);
17407 break;
17411 parser->in_unbraced_linkage_specification_p
17412 = saved_in_unbraced_linkage_specification_p;
17414 return parameters;
17417 /* Parse a parameter declaration.
17419 parameter-declaration:
17420 decl-specifier-seq ... [opt] declarator
17421 decl-specifier-seq declarator = assignment-expression
17422 decl-specifier-seq ... [opt] abstract-declarator [opt]
17423 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17425 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17426 declares a template parameter. (In that case, a non-nested `>'
17427 token encountered during the parsing of the assignment-expression
17428 is not interpreted as a greater-than operator.)
17430 Returns a representation of the parameter, or NULL if an error
17431 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17432 true iff the declarator is of the form "(p)". */
17434 static cp_parameter_declarator *
17435 cp_parser_parameter_declaration (cp_parser *parser,
17436 bool template_parm_p,
17437 bool *parenthesized_p)
17439 int declares_class_or_enum;
17440 cp_decl_specifier_seq decl_specifiers;
17441 cp_declarator *declarator;
17442 tree default_argument;
17443 cp_token *token = NULL, *declarator_token_start = NULL;
17444 const char *saved_message;
17446 /* In a template parameter, `>' is not an operator.
17448 [temp.param]
17450 When parsing a default template-argument for a non-type
17451 template-parameter, the first non-nested `>' is taken as the end
17452 of the template parameter-list rather than a greater-than
17453 operator. */
17455 /* Type definitions may not appear in parameter types. */
17456 saved_message = parser->type_definition_forbidden_message;
17457 parser->type_definition_forbidden_message
17458 = G_("types may not be defined in parameter types");
17460 /* Parse the declaration-specifiers. */
17461 cp_parser_decl_specifier_seq (parser,
17462 CP_PARSER_FLAGS_NONE,
17463 &decl_specifiers,
17464 &declares_class_or_enum);
17466 /* Complain about missing 'typename' or other invalid type names. */
17467 if (!decl_specifiers.any_type_specifiers_p)
17468 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17470 /* If an error occurred, there's no reason to attempt to parse the
17471 rest of the declaration. */
17472 if (cp_parser_error_occurred (parser))
17474 parser->type_definition_forbidden_message = saved_message;
17475 return NULL;
17478 /* Peek at the next token. */
17479 token = cp_lexer_peek_token (parser->lexer);
17481 /* If the next token is a `)', `,', `=', `>', or `...', then there
17482 is no declarator. However, when variadic templates are enabled,
17483 there may be a declarator following `...'. */
17484 if (token->type == CPP_CLOSE_PAREN
17485 || token->type == CPP_COMMA
17486 || token->type == CPP_EQ
17487 || token->type == CPP_GREATER)
17489 declarator = NULL;
17490 if (parenthesized_p)
17491 *parenthesized_p = false;
17493 /* Otherwise, there should be a declarator. */
17494 else
17496 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17497 parser->default_arg_ok_p = false;
17499 /* After seeing a decl-specifier-seq, if the next token is not a
17500 "(", there is no possibility that the code is a valid
17501 expression. Therefore, if parsing tentatively, we commit at
17502 this point. */
17503 if (!parser->in_template_argument_list_p
17504 /* In an expression context, having seen:
17506 (int((char ...
17508 we cannot be sure whether we are looking at a
17509 function-type (taking a "char" as a parameter) or a cast
17510 of some object of type "char" to "int". */
17511 && !parser->in_type_id_in_expr_p
17512 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17513 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17514 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17515 cp_parser_commit_to_tentative_parse (parser);
17516 /* Parse the declarator. */
17517 declarator_token_start = token;
17518 declarator = cp_parser_declarator (parser,
17519 CP_PARSER_DECLARATOR_EITHER,
17520 /*ctor_dtor_or_conv_p=*/NULL,
17521 parenthesized_p,
17522 /*member_p=*/false);
17523 parser->default_arg_ok_p = saved_default_arg_ok_p;
17524 /* After the declarator, allow more attributes. */
17525 decl_specifiers.attributes
17526 = chainon (decl_specifiers.attributes,
17527 cp_parser_attributes_opt (parser));
17530 /* If the next token is an ellipsis, and we have not seen a
17531 declarator name, and the type of the declarator contains parameter
17532 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17533 a parameter pack expansion expression. Otherwise, leave the
17534 ellipsis for a C-style variadic function. */
17535 token = cp_lexer_peek_token (parser->lexer);
17536 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17538 tree type = decl_specifiers.type;
17540 if (type && DECL_P (type))
17541 type = TREE_TYPE (type);
17543 if (type
17544 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17545 && declarator_can_be_parameter_pack (declarator)
17546 && (!declarator || !declarator->parameter_pack_p)
17547 && uses_parameter_packs (type))
17549 /* Consume the `...'. */
17550 cp_lexer_consume_token (parser->lexer);
17551 maybe_warn_variadic_templates ();
17553 /* Build a pack expansion type */
17554 if (declarator)
17555 declarator->parameter_pack_p = true;
17556 else
17557 decl_specifiers.type = make_pack_expansion (type);
17561 /* The restriction on defining new types applies only to the type
17562 of the parameter, not to the default argument. */
17563 parser->type_definition_forbidden_message = saved_message;
17565 /* If the next token is `=', then process a default argument. */
17566 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17568 token = cp_lexer_peek_token (parser->lexer);
17569 /* If we are defining a class, then the tokens that make up the
17570 default argument must be saved and processed later. */
17571 if (!template_parm_p && at_class_scope_p ()
17572 && TYPE_BEING_DEFINED (current_class_type)
17573 && !LAMBDA_TYPE_P (current_class_type))
17574 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17575 /* Outside of a class definition, we can just parse the
17576 assignment-expression. */
17577 else
17578 default_argument
17579 = cp_parser_default_argument (parser, template_parm_p);
17581 if (!parser->default_arg_ok_p)
17583 if (flag_permissive)
17584 warning (0, "deprecated use of default argument for parameter of non-function");
17585 else
17587 error_at (token->location,
17588 "default arguments are only "
17589 "permitted for function parameters");
17590 default_argument = NULL_TREE;
17593 else if ((declarator && declarator->parameter_pack_p)
17594 || (decl_specifiers.type
17595 && PACK_EXPANSION_P (decl_specifiers.type)))
17597 /* Find the name of the parameter pack. */
17598 cp_declarator *id_declarator = declarator;
17599 while (id_declarator && id_declarator->kind != cdk_id)
17600 id_declarator = id_declarator->declarator;
17602 if (id_declarator && id_declarator->kind == cdk_id)
17603 error_at (declarator_token_start->location,
17604 template_parm_p
17605 ? G_("template parameter pack %qD "
17606 "cannot have a default argument")
17607 : G_("parameter pack %qD cannot have "
17608 "a default argument"),
17609 id_declarator->u.id.unqualified_name);
17610 else
17611 error_at (declarator_token_start->location,
17612 template_parm_p
17613 ? G_("template parameter pack cannot have "
17614 "a default argument")
17615 : G_("parameter pack cannot have a "
17616 "default argument"));
17618 default_argument = NULL_TREE;
17621 else
17622 default_argument = NULL_TREE;
17624 return make_parameter_declarator (&decl_specifiers,
17625 declarator,
17626 default_argument);
17629 /* Parse a default argument and return it.
17631 TEMPLATE_PARM_P is true if this is a default argument for a
17632 non-type template parameter. */
17633 static tree
17634 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17636 tree default_argument = NULL_TREE;
17637 bool saved_greater_than_is_operator_p;
17638 bool saved_local_variables_forbidden_p;
17639 bool non_constant_p, is_direct_init;
17641 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17642 set correctly. */
17643 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17644 parser->greater_than_is_operator_p = !template_parm_p;
17645 /* Local variable names (and the `this' keyword) may not
17646 appear in a default argument. */
17647 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17648 parser->local_variables_forbidden_p = true;
17649 /* Parse the assignment-expression. */
17650 if (template_parm_p)
17651 push_deferring_access_checks (dk_no_deferred);
17652 default_argument
17653 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17654 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17655 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17656 if (template_parm_p)
17657 pop_deferring_access_checks ();
17658 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17659 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17661 return default_argument;
17664 /* Parse a function-body.
17666 function-body:
17667 compound_statement */
17669 static void
17670 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
17672 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
17675 /* Parse a ctor-initializer-opt followed by a function-body. Return
17676 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
17677 is true we are parsing a function-try-block. */
17679 static bool
17680 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
17681 bool in_function_try_block)
17683 tree body, list;
17684 bool ctor_initializer_p;
17685 const bool check_body_p =
17686 DECL_CONSTRUCTOR_P (current_function_decl)
17687 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17688 tree last = NULL;
17690 /* Begin the function body. */
17691 body = begin_function_body ();
17692 /* Parse the optional ctor-initializer. */
17693 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17695 /* If we're parsing a constexpr constructor definition, we need
17696 to check that the constructor body is indeed empty. However,
17697 before we get to cp_parser_function_body lot of junk has been
17698 generated, so we can't just check that we have an empty block.
17699 Rather we take a snapshot of the outermost block, and check whether
17700 cp_parser_function_body changed its state. */
17701 if (check_body_p)
17703 list = cur_stmt_list;
17704 if (STATEMENT_LIST_TAIL (list))
17705 last = STATEMENT_LIST_TAIL (list)->stmt;
17707 /* Parse the function-body. */
17708 cp_parser_function_body (parser, in_function_try_block);
17709 if (check_body_p)
17710 check_constexpr_ctor_body (last, list);
17711 /* Finish the function body. */
17712 finish_function_body (body);
17714 return ctor_initializer_p;
17717 /* Parse an initializer.
17719 initializer:
17720 = initializer-clause
17721 ( expression-list )
17723 Returns an expression representing the initializer. If no
17724 initializer is present, NULL_TREE is returned.
17726 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17727 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17728 set to TRUE if there is no initializer present. If there is an
17729 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17730 is set to true; otherwise it is set to false. */
17732 static tree
17733 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17734 bool* non_constant_p)
17736 cp_token *token;
17737 tree init;
17739 /* Peek at the next token. */
17740 token = cp_lexer_peek_token (parser->lexer);
17742 /* Let our caller know whether or not this initializer was
17743 parenthesized. */
17744 *is_direct_init = (token->type != CPP_EQ);
17745 /* Assume that the initializer is constant. */
17746 *non_constant_p = false;
17748 if (token->type == CPP_EQ)
17750 /* Consume the `='. */
17751 cp_lexer_consume_token (parser->lexer);
17752 /* Parse the initializer-clause. */
17753 init = cp_parser_initializer_clause (parser, non_constant_p);
17755 else if (token->type == CPP_OPEN_PAREN)
17757 vec<tree, va_gc> *vec;
17758 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17759 /*cast_p=*/false,
17760 /*allow_expansion_p=*/true,
17761 non_constant_p);
17762 if (vec == NULL)
17763 return error_mark_node;
17764 init = build_tree_list_vec (vec);
17765 release_tree_vector (vec);
17767 else if (token->type == CPP_OPEN_BRACE)
17769 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17770 init = cp_parser_braced_list (parser, non_constant_p);
17771 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17773 else
17775 /* Anything else is an error. */
17776 cp_parser_error (parser, "expected initializer");
17777 init = error_mark_node;
17780 return init;
17783 /* Parse an initializer-clause.
17785 initializer-clause:
17786 assignment-expression
17787 braced-init-list
17789 Returns an expression representing the initializer.
17791 If the `assignment-expression' production is used the value
17792 returned is simply a representation for the expression.
17794 Otherwise, calls cp_parser_braced_list. */
17796 static tree
17797 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17799 tree initializer;
17801 /* Assume the expression is constant. */
17802 *non_constant_p = false;
17804 /* If it is not a `{', then we are looking at an
17805 assignment-expression. */
17806 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17808 initializer
17809 = cp_parser_constant_expression (parser,
17810 /*allow_non_constant_p=*/true,
17811 non_constant_p);
17813 else
17814 initializer = cp_parser_braced_list (parser, non_constant_p);
17816 return initializer;
17819 /* Parse a brace-enclosed initializer list.
17821 braced-init-list:
17822 { initializer-list , [opt] }
17825 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17826 the elements of the initializer-list (or NULL, if the last
17827 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17828 NULL_TREE. There is no way to detect whether or not the optional
17829 trailing `,' was provided. NON_CONSTANT_P is as for
17830 cp_parser_initializer. */
17832 static tree
17833 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17835 tree initializer;
17837 /* Consume the `{' token. */
17838 cp_lexer_consume_token (parser->lexer);
17839 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17840 initializer = make_node (CONSTRUCTOR);
17841 /* If it's not a `}', then there is a non-trivial initializer. */
17842 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17844 /* Parse the initializer list. */
17845 CONSTRUCTOR_ELTS (initializer)
17846 = cp_parser_initializer_list (parser, non_constant_p);
17847 /* A trailing `,' token is allowed. */
17848 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17849 cp_lexer_consume_token (parser->lexer);
17851 /* Now, there should be a trailing `}'. */
17852 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17853 TREE_TYPE (initializer) = init_list_type_node;
17854 return initializer;
17857 /* Parse an initializer-list.
17859 initializer-list:
17860 initializer-clause ... [opt]
17861 initializer-list , initializer-clause ... [opt]
17863 GNU Extension:
17865 initializer-list:
17866 designation initializer-clause ...[opt]
17867 initializer-list , designation initializer-clause ...[opt]
17869 designation:
17870 . identifier =
17871 identifier :
17872 [ constant-expression ] =
17874 Returns a vec of constructor_elt. The VALUE of each elt is an expression
17875 for the initializer. If the INDEX of the elt is non-NULL, it is the
17876 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17877 as for cp_parser_initializer. */
17879 static vec<constructor_elt, va_gc> *
17880 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17882 vec<constructor_elt, va_gc> *v = NULL;
17884 /* Assume all of the expressions are constant. */
17885 *non_constant_p = false;
17887 /* Parse the rest of the list. */
17888 while (true)
17890 cp_token *token;
17891 tree designator;
17892 tree initializer;
17893 bool clause_non_constant_p;
17895 /* If the next token is an identifier and the following one is a
17896 colon, we are looking at the GNU designated-initializer
17897 syntax. */
17898 if (cp_parser_allow_gnu_extensions_p (parser)
17899 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17900 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17902 /* Warn the user that they are using an extension. */
17903 pedwarn (input_location, OPT_Wpedantic,
17904 "ISO C++ does not allow designated initializers");
17905 /* Consume the identifier. */
17906 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17907 /* Consume the `:'. */
17908 cp_lexer_consume_token (parser->lexer);
17910 /* Also handle the C99 syntax, '. id ='. */
17911 else if (cp_parser_allow_gnu_extensions_p (parser)
17912 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17913 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17914 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17916 /* Warn the user that they are using an extension. */
17917 pedwarn (input_location, OPT_Wpedantic,
17918 "ISO C++ does not allow C99 designated initializers");
17919 /* Consume the `.'. */
17920 cp_lexer_consume_token (parser->lexer);
17921 /* Consume the identifier. */
17922 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17923 /* Consume the `='. */
17924 cp_lexer_consume_token (parser->lexer);
17926 /* Also handle C99 array designators, '[ const ] ='. */
17927 else if (cp_parser_allow_gnu_extensions_p (parser)
17928 && !c_dialect_objc ()
17929 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17931 /* In C++11, [ could start a lambda-introducer. */
17932 bool non_const = false;
17934 cp_parser_parse_tentatively (parser);
17935 cp_lexer_consume_token (parser->lexer);
17936 designator = cp_parser_constant_expression (parser, true, &non_const);
17937 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17938 cp_parser_require (parser, CPP_EQ, RT_EQ);
17939 if (!cp_parser_parse_definitely (parser))
17940 designator = NULL_TREE;
17941 else if (non_const)
17942 require_potential_rvalue_constant_expression (designator);
17944 else
17945 designator = NULL_TREE;
17947 /* Parse the initializer. */
17948 initializer = cp_parser_initializer_clause (parser,
17949 &clause_non_constant_p);
17950 /* If any clause is non-constant, so is the entire initializer. */
17951 if (clause_non_constant_p)
17952 *non_constant_p = true;
17954 /* If we have an ellipsis, this is an initializer pack
17955 expansion. */
17956 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17958 /* Consume the `...'. */
17959 cp_lexer_consume_token (parser->lexer);
17961 /* Turn the initializer into an initializer expansion. */
17962 initializer = make_pack_expansion (initializer);
17965 /* Add it to the vector. */
17966 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17968 /* If the next token is not a comma, we have reached the end of
17969 the list. */
17970 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17971 break;
17973 /* Peek at the next token. */
17974 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17975 /* If the next token is a `}', then we're still done. An
17976 initializer-clause can have a trailing `,' after the
17977 initializer-list and before the closing `}'. */
17978 if (token->type == CPP_CLOSE_BRACE)
17979 break;
17981 /* Consume the `,' token. */
17982 cp_lexer_consume_token (parser->lexer);
17985 return v;
17988 /* Classes [gram.class] */
17990 /* Parse a class-name.
17992 class-name:
17993 identifier
17994 template-id
17996 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17997 to indicate that names looked up in dependent types should be
17998 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17999 keyword has been used to indicate that the name that appears next
18000 is a template. TAG_TYPE indicates the explicit tag given before
18001 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
18002 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
18003 is the class being defined in a class-head.
18005 Returns the TYPE_DECL representing the class. */
18007 static tree
18008 cp_parser_class_name (cp_parser *parser,
18009 bool typename_keyword_p,
18010 bool template_keyword_p,
18011 enum tag_types tag_type,
18012 bool check_dependency_p,
18013 bool class_head_p,
18014 bool is_declaration)
18016 tree decl;
18017 tree scope;
18018 bool typename_p;
18019 cp_token *token;
18020 tree identifier = NULL_TREE;
18022 /* All class-names start with an identifier. */
18023 token = cp_lexer_peek_token (parser->lexer);
18024 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
18026 cp_parser_error (parser, "expected class-name");
18027 return error_mark_node;
18030 /* PARSER->SCOPE can be cleared when parsing the template-arguments
18031 to a template-id, so we save it here. */
18032 scope = parser->scope;
18033 if (scope == error_mark_node)
18034 return error_mark_node;
18036 /* Any name names a type if we're following the `typename' keyword
18037 in a qualified name where the enclosing scope is type-dependent. */
18038 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
18039 && dependent_type_p (scope));
18040 /* Handle the common case (an identifier, but not a template-id)
18041 efficiently. */
18042 if (token->type == CPP_NAME
18043 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
18045 cp_token *identifier_token;
18046 bool ambiguous_p;
18048 /* Look for the identifier. */
18049 identifier_token = cp_lexer_peek_token (parser->lexer);
18050 ambiguous_p = identifier_token->ambiguous_p;
18051 identifier = cp_parser_identifier (parser);
18052 /* If the next token isn't an identifier, we are certainly not
18053 looking at a class-name. */
18054 if (identifier == error_mark_node)
18055 decl = error_mark_node;
18056 /* If we know this is a type-name, there's no need to look it
18057 up. */
18058 else if (typename_p)
18059 decl = identifier;
18060 else
18062 tree ambiguous_decls;
18063 /* If we already know that this lookup is ambiguous, then
18064 we've already issued an error message; there's no reason
18065 to check again. */
18066 if (ambiguous_p)
18068 cp_parser_simulate_error (parser);
18069 return error_mark_node;
18071 /* If the next token is a `::', then the name must be a type
18072 name.
18074 [basic.lookup.qual]
18076 During the lookup for a name preceding the :: scope
18077 resolution operator, object, function, and enumerator
18078 names are ignored. */
18079 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18080 tag_type = typename_type;
18081 /* Look up the name. */
18082 decl = cp_parser_lookup_name (parser, identifier,
18083 tag_type,
18084 /*is_template=*/false,
18085 /*is_namespace=*/false,
18086 check_dependency_p,
18087 &ambiguous_decls,
18088 identifier_token->location);
18089 if (ambiguous_decls)
18091 if (cp_parser_parsing_tentatively (parser))
18092 cp_parser_simulate_error (parser);
18093 return error_mark_node;
18097 else
18099 /* Try a template-id. */
18100 decl = cp_parser_template_id (parser, template_keyword_p,
18101 check_dependency_p,
18102 tag_type,
18103 is_declaration);
18104 if (decl == error_mark_node)
18105 return error_mark_node;
18108 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
18110 /* If this is a typename, create a TYPENAME_TYPE. */
18111 if (typename_p && decl != error_mark_node)
18113 decl = make_typename_type (scope, decl, typename_type,
18114 /*complain=*/tf_error);
18115 if (decl != error_mark_node)
18116 decl = TYPE_NAME (decl);
18119 decl = strip_using_decl (decl);
18121 /* Check to see that it is really the name of a class. */
18122 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18123 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
18124 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18125 /* Situations like this:
18127 template <typename T> struct A {
18128 typename T::template X<int>::I i;
18131 are problematic. Is `T::template X<int>' a class-name? The
18132 standard does not seem to be definitive, but there is no other
18133 valid interpretation of the following `::'. Therefore, those
18134 names are considered class-names. */
18136 decl = make_typename_type (scope, decl, tag_type, tf_error);
18137 if (decl != error_mark_node)
18138 decl = TYPE_NAME (decl);
18140 else if (TREE_CODE (decl) != TYPE_DECL
18141 || TREE_TYPE (decl) == error_mark_node
18142 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
18143 /* In Objective-C 2.0, a classname followed by '.' starts a
18144 dot-syntax expression, and it's not a type-name. */
18145 || (c_dialect_objc ()
18146 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
18147 && objc_is_class_name (decl)))
18148 decl = error_mark_node;
18150 if (decl == error_mark_node)
18151 cp_parser_error (parser, "expected class-name");
18152 else if (identifier && !parser->scope)
18153 maybe_note_name_used_in_class (identifier, decl);
18155 return decl;
18158 /* Parse a class-specifier.
18160 class-specifier:
18161 class-head { member-specification [opt] }
18163 Returns the TREE_TYPE representing the class. */
18165 static tree
18166 cp_parser_class_specifier_1 (cp_parser* parser)
18168 tree type;
18169 tree attributes = NULL_TREE;
18170 bool nested_name_specifier_p;
18171 unsigned saved_num_template_parameter_lists;
18172 bool saved_in_function_body;
18173 unsigned char in_statement;
18174 bool in_switch_statement_p;
18175 bool saved_in_unbraced_linkage_specification_p;
18176 tree old_scope = NULL_TREE;
18177 tree scope = NULL_TREE;
18178 cp_token *closing_brace;
18180 push_deferring_access_checks (dk_no_deferred);
18182 /* Parse the class-head. */
18183 type = cp_parser_class_head (parser,
18184 &nested_name_specifier_p);
18185 /* If the class-head was a semantic disaster, skip the entire body
18186 of the class. */
18187 if (!type)
18189 cp_parser_skip_to_end_of_block_or_statement (parser);
18190 pop_deferring_access_checks ();
18191 return error_mark_node;
18194 /* Look for the `{'. */
18195 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
18197 pop_deferring_access_checks ();
18198 return error_mark_node;
18201 /* Issue an error message if type-definitions are forbidden here. */
18202 cp_parser_check_type_definition (parser);
18203 /* Remember that we are defining one more class. */
18204 ++parser->num_classes_being_defined;
18205 /* Inside the class, surrounding template-parameter-lists do not
18206 apply. */
18207 saved_num_template_parameter_lists
18208 = parser->num_template_parameter_lists;
18209 parser->num_template_parameter_lists = 0;
18210 /* We are not in a function body. */
18211 saved_in_function_body = parser->in_function_body;
18212 parser->in_function_body = false;
18213 /* Or in a loop. */
18214 in_statement = parser->in_statement;
18215 parser->in_statement = 0;
18216 /* Or in a switch. */
18217 in_switch_statement_p = parser->in_switch_statement_p;
18218 parser->in_switch_statement_p = false;
18219 /* We are not immediately inside an extern "lang" block. */
18220 saved_in_unbraced_linkage_specification_p
18221 = parser->in_unbraced_linkage_specification_p;
18222 parser->in_unbraced_linkage_specification_p = false;
18224 /* Start the class. */
18225 if (nested_name_specifier_p)
18227 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18228 old_scope = push_inner_scope (scope);
18230 type = begin_class_definition (type);
18232 if (type == error_mark_node)
18233 /* If the type is erroneous, skip the entire body of the class. */
18234 cp_parser_skip_to_closing_brace (parser);
18235 else
18236 /* Parse the member-specification. */
18237 cp_parser_member_specification_opt (parser);
18239 /* Look for the trailing `}'. */
18240 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18241 /* Look for trailing attributes to apply to this class. */
18242 if (cp_parser_allow_gnu_extensions_p (parser))
18243 attributes = cp_parser_gnu_attributes_opt (parser);
18244 if (type != error_mark_node)
18245 type = finish_struct (type, attributes);
18246 if (nested_name_specifier_p)
18247 pop_inner_scope (old_scope, scope);
18249 /* We've finished a type definition. Check for the common syntax
18250 error of forgetting a semicolon after the definition. We need to
18251 be careful, as we can't just check for not-a-semicolon and be done
18252 with it; the user might have typed:
18254 class X { } c = ...;
18255 class X { } *p = ...;
18257 and so forth. Instead, enumerate all the possible tokens that
18258 might follow this production; if we don't see one of them, then
18259 complain and silently insert the semicolon. */
18261 cp_token *token = cp_lexer_peek_token (parser->lexer);
18262 bool want_semicolon = true;
18264 if (cp_next_tokens_can_be_std_attribute_p (parser))
18265 /* Don't try to parse c++11 attributes here. As per the
18266 grammar, that should be a task for
18267 cp_parser_decl_specifier_seq. */
18268 want_semicolon = false;
18270 switch (token->type)
18272 case CPP_NAME:
18273 case CPP_SEMICOLON:
18274 case CPP_MULT:
18275 case CPP_AND:
18276 case CPP_OPEN_PAREN:
18277 case CPP_CLOSE_PAREN:
18278 case CPP_COMMA:
18279 want_semicolon = false;
18280 break;
18282 /* While it's legal for type qualifiers and storage class
18283 specifiers to follow type definitions in the grammar, only
18284 compiler testsuites contain code like that. Assume that if
18285 we see such code, then what we're really seeing is a case
18286 like:
18288 class X { }
18289 const <type> var = ...;
18293 class Y { }
18294 static <type> func (...) ...
18296 i.e. the qualifier or specifier applies to the next
18297 declaration. To do so, however, we need to look ahead one
18298 more token to see if *that* token is a type specifier.
18300 This code could be improved to handle:
18302 class Z { }
18303 static const <type> var = ...; */
18304 case CPP_KEYWORD:
18305 if (keyword_is_decl_specifier (token->keyword))
18307 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18309 /* Handling user-defined types here would be nice, but very
18310 tricky. */
18311 want_semicolon
18312 = (lookahead->type == CPP_KEYWORD
18313 && keyword_begins_type_specifier (lookahead->keyword));
18315 break;
18316 default:
18317 break;
18320 /* If we don't have a type, then something is very wrong and we
18321 shouldn't try to do anything clever. Likewise for not seeing the
18322 closing brace. */
18323 if (closing_brace && TYPE_P (type) && want_semicolon)
18325 cp_token_position prev
18326 = cp_lexer_previous_token_position (parser->lexer);
18327 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18328 location_t loc = prev_token->location;
18330 if (CLASSTYPE_DECLARED_CLASS (type))
18331 error_at (loc, "expected %<;%> after class definition");
18332 else if (TREE_CODE (type) == RECORD_TYPE)
18333 error_at (loc, "expected %<;%> after struct definition");
18334 else if (TREE_CODE (type) == UNION_TYPE)
18335 error_at (loc, "expected %<;%> after union definition");
18336 else
18337 gcc_unreachable ();
18339 /* Unget one token and smash it to look as though we encountered
18340 a semicolon in the input stream. */
18341 cp_lexer_set_token_position (parser->lexer, prev);
18342 token = cp_lexer_peek_token (parser->lexer);
18343 token->type = CPP_SEMICOLON;
18344 token->keyword = RID_MAX;
18348 /* If this class is not itself within the scope of another class,
18349 then we need to parse the bodies of all of the queued function
18350 definitions. Note that the queued functions defined in a class
18351 are not always processed immediately following the
18352 class-specifier for that class. Consider:
18354 struct A {
18355 struct B { void f() { sizeof (A); } };
18358 If `f' were processed before the processing of `A' were
18359 completed, there would be no way to compute the size of `A'.
18360 Note that the nesting we are interested in here is lexical --
18361 not the semantic nesting given by TYPE_CONTEXT. In particular,
18362 for:
18364 struct A { struct B; };
18365 struct A::B { void f() { } };
18367 there is no need to delay the parsing of `A::B::f'. */
18368 if (--parser->num_classes_being_defined == 0)
18370 tree decl;
18371 tree class_type = NULL_TREE;
18372 tree pushed_scope = NULL_TREE;
18373 unsigned ix;
18374 cp_default_arg_entry *e;
18375 tree save_ccp, save_ccr;
18377 /* In a first pass, parse default arguments to the functions.
18378 Then, in a second pass, parse the bodies of the functions.
18379 This two-phased approach handles cases like:
18381 struct S {
18382 void f() { g(); }
18383 void g(int i = 3);
18387 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
18389 decl = e->decl;
18390 /* If there are default arguments that have not yet been processed,
18391 take care of them now. */
18392 if (class_type != e->class_type)
18394 if (pushed_scope)
18395 pop_scope (pushed_scope);
18396 class_type = e->class_type;
18397 pushed_scope = push_scope (class_type);
18399 /* Make sure that any template parameters are in scope. */
18400 maybe_begin_member_template_processing (decl);
18401 /* Parse the default argument expressions. */
18402 cp_parser_late_parsing_default_args (parser, decl);
18403 /* Remove any template parameters from the symbol table. */
18404 maybe_end_member_template_processing ();
18406 vec_safe_truncate (unparsed_funs_with_default_args, 0);
18407 /* Now parse any NSDMIs. */
18408 save_ccp = current_class_ptr;
18409 save_ccr = current_class_ref;
18410 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
18412 if (class_type != DECL_CONTEXT (decl))
18414 if (pushed_scope)
18415 pop_scope (pushed_scope);
18416 class_type = DECL_CONTEXT (decl);
18417 pushed_scope = push_scope (class_type);
18419 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18420 cp_parser_late_parsing_nsdmi (parser, decl);
18422 vec_safe_truncate (unparsed_nsdmis, 0);
18423 current_class_ptr = save_ccp;
18424 current_class_ref = save_ccr;
18425 if (pushed_scope)
18426 pop_scope (pushed_scope);
18427 /* Now parse the body of the functions. */
18428 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
18429 cp_parser_late_parsing_for_member (parser, decl);
18430 vec_safe_truncate (unparsed_funs_with_definitions, 0);
18433 /* Put back any saved access checks. */
18434 pop_deferring_access_checks ();
18436 /* Restore saved state. */
18437 parser->in_switch_statement_p = in_switch_statement_p;
18438 parser->in_statement = in_statement;
18439 parser->in_function_body = saved_in_function_body;
18440 parser->num_template_parameter_lists
18441 = saved_num_template_parameter_lists;
18442 parser->in_unbraced_linkage_specification_p
18443 = saved_in_unbraced_linkage_specification_p;
18445 return type;
18448 static tree
18449 cp_parser_class_specifier (cp_parser* parser)
18451 tree ret;
18452 timevar_push (TV_PARSE_STRUCT);
18453 ret = cp_parser_class_specifier_1 (parser);
18454 timevar_pop (TV_PARSE_STRUCT);
18455 return ret;
18458 /* Parse a class-head.
18460 class-head:
18461 class-key identifier [opt] base-clause [opt]
18462 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18463 class-key nested-name-specifier [opt] template-id
18464 base-clause [opt]
18466 class-virt-specifier:
18467 final
18469 GNU Extensions:
18470 class-key attributes identifier [opt] base-clause [opt]
18471 class-key attributes nested-name-specifier identifier base-clause [opt]
18472 class-key attributes nested-name-specifier [opt] template-id
18473 base-clause [opt]
18475 Upon return BASES is initialized to the list of base classes (or
18476 NULL, if there are none) in the same form returned by
18477 cp_parser_base_clause.
18479 Returns the TYPE of the indicated class. Sets
18480 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18481 involving a nested-name-specifier was used, and FALSE otherwise.
18483 Returns error_mark_node if this is not a class-head.
18485 Returns NULL_TREE if the class-head is syntactically valid, but
18486 semantically invalid in a way that means we should skip the entire
18487 body of the class. */
18489 static tree
18490 cp_parser_class_head (cp_parser* parser,
18491 bool* nested_name_specifier_p)
18493 tree nested_name_specifier;
18494 enum tag_types class_key;
18495 tree id = NULL_TREE;
18496 tree type = NULL_TREE;
18497 tree attributes;
18498 tree bases;
18499 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18500 bool template_id_p = false;
18501 bool qualified_p = false;
18502 bool invalid_nested_name_p = false;
18503 bool invalid_explicit_specialization_p = false;
18504 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18505 tree pushed_scope = NULL_TREE;
18506 unsigned num_templates;
18507 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18508 /* Assume no nested-name-specifier will be present. */
18509 *nested_name_specifier_p = false;
18510 /* Assume no template parameter lists will be used in defining the
18511 type. */
18512 num_templates = 0;
18513 parser->colon_corrects_to_scope_p = false;
18515 /* Look for the class-key. */
18516 class_key = cp_parser_class_key (parser);
18517 if (class_key == none_type)
18518 return error_mark_node;
18520 /* Parse the attributes. */
18521 attributes = cp_parser_attributes_opt (parser);
18523 /* If the next token is `::', that is invalid -- but sometimes
18524 people do try to write:
18526 struct ::S {};
18528 Handle this gracefully by accepting the extra qualifier, and then
18529 issuing an error about it later if this really is a
18530 class-head. If it turns out just to be an elaborated type
18531 specifier, remain silent. */
18532 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18533 qualified_p = true;
18535 push_deferring_access_checks (dk_no_check);
18537 /* Determine the name of the class. Begin by looking for an
18538 optional nested-name-specifier. */
18539 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18540 nested_name_specifier
18541 = cp_parser_nested_name_specifier_opt (parser,
18542 /*typename_keyword_p=*/false,
18543 /*check_dependency_p=*/false,
18544 /*type_p=*/true,
18545 /*is_declaration=*/false);
18546 /* If there was a nested-name-specifier, then there *must* be an
18547 identifier. */
18548 if (nested_name_specifier)
18550 type_start_token = cp_lexer_peek_token (parser->lexer);
18551 /* Although the grammar says `identifier', it really means
18552 `class-name' or `template-name'. You are only allowed to
18553 define a class that has already been declared with this
18554 syntax.
18556 The proposed resolution for Core Issue 180 says that wherever
18557 you see `class T::X' you should treat `X' as a type-name.
18559 It is OK to define an inaccessible class; for example:
18561 class A { class B; };
18562 class A::B {};
18564 We do not know if we will see a class-name, or a
18565 template-name. We look for a class-name first, in case the
18566 class-name is a template-id; if we looked for the
18567 template-name first we would stop after the template-name. */
18568 cp_parser_parse_tentatively (parser);
18569 type = cp_parser_class_name (parser,
18570 /*typename_keyword_p=*/false,
18571 /*template_keyword_p=*/false,
18572 class_type,
18573 /*check_dependency_p=*/false,
18574 /*class_head_p=*/true,
18575 /*is_declaration=*/false);
18576 /* If that didn't work, ignore the nested-name-specifier. */
18577 if (!cp_parser_parse_definitely (parser))
18579 invalid_nested_name_p = true;
18580 type_start_token = cp_lexer_peek_token (parser->lexer);
18581 id = cp_parser_identifier (parser);
18582 if (id == error_mark_node)
18583 id = NULL_TREE;
18585 /* If we could not find a corresponding TYPE, treat this
18586 declaration like an unqualified declaration. */
18587 if (type == error_mark_node)
18588 nested_name_specifier = NULL_TREE;
18589 /* Otherwise, count the number of templates used in TYPE and its
18590 containing scopes. */
18591 else
18593 tree scope;
18595 for (scope = TREE_TYPE (type);
18596 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18597 scope = (TYPE_P (scope)
18598 ? TYPE_CONTEXT (scope)
18599 : DECL_CONTEXT (scope)))
18600 if (TYPE_P (scope)
18601 && CLASS_TYPE_P (scope)
18602 && CLASSTYPE_TEMPLATE_INFO (scope)
18603 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18604 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
18605 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
18606 ++num_templates;
18609 /* Otherwise, the identifier is optional. */
18610 else
18612 /* We don't know whether what comes next is a template-id,
18613 an identifier, or nothing at all. */
18614 cp_parser_parse_tentatively (parser);
18615 /* Check for a template-id. */
18616 type_start_token = cp_lexer_peek_token (parser->lexer);
18617 id = cp_parser_template_id (parser,
18618 /*template_keyword_p=*/false,
18619 /*check_dependency_p=*/true,
18620 class_key,
18621 /*is_declaration=*/true);
18622 /* If that didn't work, it could still be an identifier. */
18623 if (!cp_parser_parse_definitely (parser))
18625 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18627 type_start_token = cp_lexer_peek_token (parser->lexer);
18628 id = cp_parser_identifier (parser);
18630 else
18631 id = NULL_TREE;
18633 else
18635 template_id_p = true;
18636 ++num_templates;
18640 pop_deferring_access_checks ();
18642 if (id)
18644 cp_parser_check_for_invalid_template_id (parser, id,
18645 class_key,
18646 type_start_token->location);
18648 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18650 /* If it's not a `:' or a `{' then we can't really be looking at a
18651 class-head, since a class-head only appears as part of a
18652 class-specifier. We have to detect this situation before calling
18653 xref_tag, since that has irreversible side-effects. */
18654 if (!cp_parser_next_token_starts_class_definition_p (parser))
18656 cp_parser_error (parser, "expected %<{%> or %<:%>");
18657 type = error_mark_node;
18658 goto out;
18661 /* At this point, we're going ahead with the class-specifier, even
18662 if some other problem occurs. */
18663 cp_parser_commit_to_tentative_parse (parser);
18664 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18666 cp_parser_error (parser,
18667 "cannot specify %<override%> for a class");
18668 type = error_mark_node;
18669 goto out;
18671 /* Issue the error about the overly-qualified name now. */
18672 if (qualified_p)
18674 cp_parser_error (parser,
18675 "global qualification of class name is invalid");
18676 type = error_mark_node;
18677 goto out;
18679 else if (invalid_nested_name_p)
18681 cp_parser_error (parser,
18682 "qualified name does not name a class");
18683 type = error_mark_node;
18684 goto out;
18686 else if (nested_name_specifier)
18688 tree scope;
18690 /* Reject typedef-names in class heads. */
18691 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18693 error_at (type_start_token->location,
18694 "invalid class name in declaration of %qD",
18695 type);
18696 type = NULL_TREE;
18697 goto done;
18700 /* Figure out in what scope the declaration is being placed. */
18701 scope = current_scope ();
18702 /* If that scope does not contain the scope in which the
18703 class was originally declared, the program is invalid. */
18704 if (scope && !is_ancestor (scope, nested_name_specifier))
18706 if (at_namespace_scope_p ())
18707 error_at (type_start_token->location,
18708 "declaration of %qD in namespace %qD which does not "
18709 "enclose %qD",
18710 type, scope, nested_name_specifier);
18711 else
18712 error_at (type_start_token->location,
18713 "declaration of %qD in %qD which does not enclose %qD",
18714 type, scope, nested_name_specifier);
18715 type = NULL_TREE;
18716 goto done;
18718 /* [dcl.meaning]
18720 A declarator-id shall not be qualified except for the
18721 definition of a ... nested class outside of its class
18722 ... [or] the definition or explicit instantiation of a
18723 class member of a namespace outside of its namespace. */
18724 if (scope == nested_name_specifier)
18726 permerror (nested_name_specifier_token_start->location,
18727 "extra qualification not allowed");
18728 nested_name_specifier = NULL_TREE;
18729 num_templates = 0;
18732 /* An explicit-specialization must be preceded by "template <>". If
18733 it is not, try to recover gracefully. */
18734 if (at_namespace_scope_p ()
18735 && parser->num_template_parameter_lists == 0
18736 && template_id_p)
18738 error_at (type_start_token->location,
18739 "an explicit specialization must be preceded by %<template <>%>");
18740 invalid_explicit_specialization_p = true;
18741 /* Take the same action that would have been taken by
18742 cp_parser_explicit_specialization. */
18743 ++parser->num_template_parameter_lists;
18744 begin_specialization ();
18746 /* There must be no "return" statements between this point and the
18747 end of this function; set "type "to the correct return value and
18748 use "goto done;" to return. */
18749 /* Make sure that the right number of template parameters were
18750 present. */
18751 if (!cp_parser_check_template_parameters (parser, num_templates,
18752 type_start_token->location,
18753 /*declarator=*/NULL))
18755 /* If something went wrong, there is no point in even trying to
18756 process the class-definition. */
18757 type = NULL_TREE;
18758 goto done;
18761 /* Look up the type. */
18762 if (template_id_p)
18764 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18765 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18766 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18768 error_at (type_start_token->location,
18769 "function template %qD redeclared as a class template", id);
18770 type = error_mark_node;
18772 else
18774 type = TREE_TYPE (id);
18775 type = maybe_process_partial_specialization (type);
18777 if (nested_name_specifier)
18778 pushed_scope = push_scope (nested_name_specifier);
18780 else if (nested_name_specifier)
18782 tree class_type;
18784 /* Given:
18786 template <typename T> struct S { struct T };
18787 template <typename T> struct S<T>::T { };
18789 we will get a TYPENAME_TYPE when processing the definition of
18790 `S::T'. We need to resolve it to the actual type before we
18791 try to define it. */
18792 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18794 class_type = resolve_typename_type (TREE_TYPE (type),
18795 /*only_current_p=*/false);
18796 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18797 type = TYPE_NAME (class_type);
18798 else
18800 cp_parser_error (parser, "could not resolve typename type");
18801 type = error_mark_node;
18805 if (maybe_process_partial_specialization (TREE_TYPE (type))
18806 == error_mark_node)
18808 type = NULL_TREE;
18809 goto done;
18812 class_type = current_class_type;
18813 /* Enter the scope indicated by the nested-name-specifier. */
18814 pushed_scope = push_scope (nested_name_specifier);
18815 /* Get the canonical version of this type. */
18816 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18817 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18818 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18820 type = push_template_decl (type);
18821 if (type == error_mark_node)
18823 type = NULL_TREE;
18824 goto done;
18828 type = TREE_TYPE (type);
18829 *nested_name_specifier_p = true;
18831 else /* The name is not a nested name. */
18833 /* If the class was unnamed, create a dummy name. */
18834 if (!id)
18835 id = make_anon_name ();
18836 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18837 parser->num_template_parameter_lists);
18840 /* Indicate whether this class was declared as a `class' or as a
18841 `struct'. */
18842 if (TREE_CODE (type) == RECORD_TYPE)
18843 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18844 cp_parser_check_class_key (class_key, type);
18846 /* If this type was already complete, and we see another definition,
18847 that's an error. */
18848 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18850 error_at (type_start_token->location, "redefinition of %q#T",
18851 type);
18852 error_at (type_start_token->location, "previous definition of %q+#T",
18853 type);
18854 type = NULL_TREE;
18855 goto done;
18857 else if (type == error_mark_node)
18858 type = NULL_TREE;
18860 if (type)
18862 /* Apply attributes now, before any use of the class as a template
18863 argument in its base list. */
18864 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18865 fixup_attribute_variants (type);
18868 /* We will have entered the scope containing the class; the names of
18869 base classes should be looked up in that context. For example:
18871 struct A { struct B {}; struct C; };
18872 struct A::C : B {};
18874 is valid. */
18876 /* Get the list of base-classes, if there is one. */
18877 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18878 bases = cp_parser_base_clause (parser);
18879 else
18880 bases = NULL_TREE;
18882 /* If we're really defining a class, process the base classes.
18883 If they're invalid, fail. */
18884 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
18885 && !xref_basetypes (type, bases))
18886 type = NULL_TREE;
18888 done:
18889 /* Leave the scope given by the nested-name-specifier. We will
18890 enter the class scope itself while processing the members. */
18891 if (pushed_scope)
18892 pop_scope (pushed_scope);
18894 if (invalid_explicit_specialization_p)
18896 end_specialization ();
18897 --parser->num_template_parameter_lists;
18900 if (type)
18901 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18902 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18903 CLASSTYPE_FINAL (type) = 1;
18904 out:
18905 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18906 return type;
18909 /* Parse a class-key.
18911 class-key:
18912 class
18913 struct
18914 union
18916 Returns the kind of class-key specified, or none_type to indicate
18917 error. */
18919 static enum tag_types
18920 cp_parser_class_key (cp_parser* parser)
18922 cp_token *token;
18923 enum tag_types tag_type;
18925 /* Look for the class-key. */
18926 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18927 if (!token)
18928 return none_type;
18930 /* Check to see if the TOKEN is a class-key. */
18931 tag_type = cp_parser_token_is_class_key (token);
18932 if (!tag_type)
18933 cp_parser_error (parser, "expected class-key");
18934 return tag_type;
18937 /* Parse an (optional) member-specification.
18939 member-specification:
18940 member-declaration member-specification [opt]
18941 access-specifier : member-specification [opt] */
18943 static void
18944 cp_parser_member_specification_opt (cp_parser* parser)
18946 while (true)
18948 cp_token *token;
18949 enum rid keyword;
18951 /* Peek at the next token. */
18952 token = cp_lexer_peek_token (parser->lexer);
18953 /* If it's a `}', or EOF then we've seen all the members. */
18954 if (token->type == CPP_CLOSE_BRACE
18955 || token->type == CPP_EOF
18956 || token->type == CPP_PRAGMA_EOL)
18957 break;
18959 /* See if this token is a keyword. */
18960 keyword = token->keyword;
18961 switch (keyword)
18963 case RID_PUBLIC:
18964 case RID_PROTECTED:
18965 case RID_PRIVATE:
18966 /* Consume the access-specifier. */
18967 cp_lexer_consume_token (parser->lexer);
18968 /* Remember which access-specifier is active. */
18969 current_access_specifier = token->u.value;
18970 /* Look for the `:'. */
18971 cp_parser_require (parser, CPP_COLON, RT_COLON);
18972 break;
18974 default:
18975 /* Accept #pragmas at class scope. */
18976 if (token->type == CPP_PRAGMA)
18978 cp_parser_pragma (parser, pragma_external);
18979 break;
18982 /* Otherwise, the next construction must be a
18983 member-declaration. */
18984 cp_parser_member_declaration (parser);
18989 /* Parse a member-declaration.
18991 member-declaration:
18992 decl-specifier-seq [opt] member-declarator-list [opt] ;
18993 function-definition ; [opt]
18994 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18995 using-declaration
18996 template-declaration
18997 alias-declaration
18999 member-declarator-list:
19000 member-declarator
19001 member-declarator-list , member-declarator
19003 member-declarator:
19004 declarator pure-specifier [opt]
19005 declarator constant-initializer [opt]
19006 identifier [opt] : constant-expression
19008 GNU Extensions:
19010 member-declaration:
19011 __extension__ member-declaration
19013 member-declarator:
19014 declarator attributes [opt] pure-specifier [opt]
19015 declarator attributes [opt] constant-initializer [opt]
19016 identifier [opt] attributes [opt] : constant-expression
19018 C++0x Extensions:
19020 member-declaration:
19021 static_assert-declaration */
19023 static void
19024 cp_parser_member_declaration (cp_parser* parser)
19026 cp_decl_specifier_seq decl_specifiers;
19027 tree prefix_attributes;
19028 tree decl;
19029 int declares_class_or_enum;
19030 bool friend_p;
19031 cp_token *token = NULL;
19032 cp_token *decl_spec_token_start = NULL;
19033 cp_token *initializer_token_start = NULL;
19034 int saved_pedantic;
19035 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19037 /* Check for the `__extension__' keyword. */
19038 if (cp_parser_extension_opt (parser, &saved_pedantic))
19040 /* Recurse. */
19041 cp_parser_member_declaration (parser);
19042 /* Restore the old value of the PEDANTIC flag. */
19043 pedantic = saved_pedantic;
19045 return;
19048 /* Check for a template-declaration. */
19049 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19051 /* An explicit specialization here is an error condition, and we
19052 expect the specialization handler to detect and report this. */
19053 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19054 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
19055 cp_parser_explicit_specialization (parser);
19056 else
19057 cp_parser_template_declaration (parser, /*member_p=*/true);
19059 return;
19062 /* Check for a using-declaration. */
19063 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
19065 if (cxx_dialect < cxx0x)
19067 /* Parse the using-declaration. */
19068 cp_parser_using_declaration (parser,
19069 /*access_declaration_p=*/false);
19070 return;
19072 else
19074 tree decl;
19075 bool alias_decl_expected;
19076 cp_parser_parse_tentatively (parser);
19077 decl = cp_parser_alias_declaration (parser);
19078 /* Note that if we actually see the '=' token after the
19079 identifier, cp_parser_alias_declaration commits the
19080 tentative parse. In that case, we really expects an
19081 alias-declaration. Otherwise, we expect a using
19082 declaration. */
19083 alias_decl_expected =
19084 !cp_parser_uncommitted_to_tentative_parse_p (parser);
19085 cp_parser_parse_definitely (parser);
19087 if (alias_decl_expected)
19088 finish_member_declaration (decl);
19089 else
19090 cp_parser_using_declaration (parser,
19091 /*access_declaration_p=*/false);
19092 return;
19096 /* Check for @defs. */
19097 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
19099 tree ivar, member;
19100 tree ivar_chains = cp_parser_objc_defs_expression (parser);
19101 ivar = ivar_chains;
19102 while (ivar)
19104 member = ivar;
19105 ivar = TREE_CHAIN (member);
19106 TREE_CHAIN (member) = NULL_TREE;
19107 finish_member_declaration (member);
19109 return;
19112 /* If the next token is `static_assert' we have a static assertion. */
19113 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
19115 cp_parser_static_assert (parser, /*member_p=*/true);
19116 return;
19119 parser->colon_corrects_to_scope_p = false;
19121 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
19122 goto out;
19124 /* Parse the decl-specifier-seq. */
19125 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19126 cp_parser_decl_specifier_seq (parser,
19127 CP_PARSER_FLAGS_OPTIONAL,
19128 &decl_specifiers,
19129 &declares_class_or_enum);
19130 /* Check for an invalid type-name. */
19131 if (!decl_specifiers.any_type_specifiers_p
19132 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19133 goto out;
19134 /* If there is no declarator, then the decl-specifier-seq should
19135 specify a type. */
19136 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19138 /* If there was no decl-specifier-seq, and the next token is a
19139 `;', then we have something like:
19141 struct S { ; };
19143 [class.mem]
19145 Each member-declaration shall declare at least one member
19146 name of the class. */
19147 if (!decl_specifiers.any_specifiers_p)
19149 cp_token *token = cp_lexer_peek_token (parser->lexer);
19150 if (!in_system_header_at (token->location))
19151 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
19153 else
19155 tree type;
19157 /* See if this declaration is a friend. */
19158 friend_p = cp_parser_friend_p (&decl_specifiers);
19159 /* If there were decl-specifiers, check to see if there was
19160 a class-declaration. */
19161 type = check_tag_decl (&decl_specifiers,
19162 /*explicit_type_instantiation_p=*/false);
19163 /* Nested classes have already been added to the class, but
19164 a `friend' needs to be explicitly registered. */
19165 if (friend_p)
19167 /* If the `friend' keyword was present, the friend must
19168 be introduced with a class-key. */
19169 if (!declares_class_or_enum && cxx_dialect < cxx0x)
19170 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
19171 "in C++03 a class-key must be used "
19172 "when declaring a friend");
19173 /* In this case:
19175 template <typename T> struct A {
19176 friend struct A<T>::B;
19179 A<T>::B will be represented by a TYPENAME_TYPE, and
19180 therefore not recognized by check_tag_decl. */
19181 if (!type)
19183 type = decl_specifiers.type;
19184 if (type && TREE_CODE (type) == TYPE_DECL)
19185 type = TREE_TYPE (type);
19187 if (!type || !TYPE_P (type))
19188 error_at (decl_spec_token_start->location,
19189 "friend declaration does not name a class or "
19190 "function");
19191 else
19192 make_friend_class (current_class_type, type,
19193 /*complain=*/true);
19195 /* If there is no TYPE, an error message will already have
19196 been issued. */
19197 else if (!type || type == error_mark_node)
19199 /* An anonymous aggregate has to be handled specially; such
19200 a declaration really declares a data member (with a
19201 particular type), as opposed to a nested class. */
19202 else if (ANON_AGGR_TYPE_P (type))
19204 /* C++11 9.5/6. */
19205 if (decl_specifiers.storage_class != sc_none)
19206 error_at (decl_spec_token_start->location,
19207 "a storage class on an anonymous aggregate "
19208 "in class scope is not allowed");
19210 /* Remove constructors and such from TYPE, now that we
19211 know it is an anonymous aggregate. */
19212 fixup_anonymous_aggr (type);
19213 /* And make the corresponding data member. */
19214 decl = build_decl (decl_spec_token_start->location,
19215 FIELD_DECL, NULL_TREE, type);
19216 /* Add it to the class. */
19217 finish_member_declaration (decl);
19219 else
19220 cp_parser_check_access_in_redeclaration
19221 (TYPE_NAME (type),
19222 decl_spec_token_start->location);
19225 else
19227 bool assume_semicolon = false;
19229 /* Clear attributes from the decl_specifiers but keep them
19230 around as prefix attributes that apply them to the entity
19231 being declared. */
19232 prefix_attributes = decl_specifiers.attributes;
19233 decl_specifiers.attributes = NULL_TREE;
19235 /* See if these declarations will be friends. */
19236 friend_p = cp_parser_friend_p (&decl_specifiers);
19238 /* Keep going until we hit the `;' at the end of the
19239 declaration. */
19240 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19242 tree attributes = NULL_TREE;
19243 tree first_attribute;
19245 /* Peek at the next token. */
19246 token = cp_lexer_peek_token (parser->lexer);
19248 /* Check for a bitfield declaration. */
19249 if (token->type == CPP_COLON
19250 || (token->type == CPP_NAME
19251 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19252 == CPP_COLON))
19254 tree identifier;
19255 tree width;
19257 /* Get the name of the bitfield. Note that we cannot just
19258 check TOKEN here because it may have been invalidated by
19259 the call to cp_lexer_peek_nth_token above. */
19260 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19261 identifier = cp_parser_identifier (parser);
19262 else
19263 identifier = NULL_TREE;
19265 /* Consume the `:' token. */
19266 cp_lexer_consume_token (parser->lexer);
19267 /* Get the width of the bitfield. */
19268 width
19269 = cp_parser_constant_expression (parser,
19270 /*allow_non_constant=*/false,
19271 NULL);
19273 /* Look for attributes that apply to the bitfield. */
19274 attributes = cp_parser_attributes_opt (parser);
19275 /* Remember which attributes are prefix attributes and
19276 which are not. */
19277 first_attribute = attributes;
19278 /* Combine the attributes. */
19279 attributes = chainon (prefix_attributes, attributes);
19281 /* Create the bitfield declaration. */
19282 decl = grokbitfield (identifier
19283 ? make_id_declarator (NULL_TREE,
19284 identifier,
19285 sfk_none)
19286 : NULL,
19287 &decl_specifiers,
19288 width,
19289 attributes);
19291 else
19293 cp_declarator *declarator;
19294 tree initializer;
19295 tree asm_specification;
19296 int ctor_dtor_or_conv_p;
19298 /* Parse the declarator. */
19299 declarator
19300 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19301 &ctor_dtor_or_conv_p,
19302 /*parenthesized_p=*/NULL,
19303 /*member_p=*/true);
19305 /* If something went wrong parsing the declarator, make sure
19306 that we at least consume some tokens. */
19307 if (declarator == cp_error_declarator)
19309 /* Skip to the end of the statement. */
19310 cp_parser_skip_to_end_of_statement (parser);
19311 /* If the next token is not a semicolon, that is
19312 probably because we just skipped over the body of
19313 a function. So, we consume a semicolon if
19314 present, but do not issue an error message if it
19315 is not present. */
19316 if (cp_lexer_next_token_is (parser->lexer,
19317 CPP_SEMICOLON))
19318 cp_lexer_consume_token (parser->lexer);
19319 goto out;
19322 if (declares_class_or_enum & 2)
19323 cp_parser_check_for_definition_in_return_type
19324 (declarator, decl_specifiers.type,
19325 decl_specifiers.locations[ds_type_spec]);
19327 /* Look for an asm-specification. */
19328 asm_specification = cp_parser_asm_specification_opt (parser);
19329 /* Look for attributes that apply to the declaration. */
19330 attributes = cp_parser_attributes_opt (parser);
19331 /* Remember which attributes are prefix attributes and
19332 which are not. */
19333 first_attribute = attributes;
19334 /* Combine the attributes. */
19335 attributes = chainon (prefix_attributes, attributes);
19337 /* If it's an `=', then we have a constant-initializer or a
19338 pure-specifier. It is not correct to parse the
19339 initializer before registering the member declaration
19340 since the member declaration should be in scope while
19341 its initializer is processed. However, the rest of the
19342 front end does not yet provide an interface that allows
19343 us to handle this correctly. */
19344 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19346 /* In [class.mem]:
19348 A pure-specifier shall be used only in the declaration of
19349 a virtual function.
19351 A member-declarator can contain a constant-initializer
19352 only if it declares a static member of integral or
19353 enumeration type.
19355 Therefore, if the DECLARATOR is for a function, we look
19356 for a pure-specifier; otherwise, we look for a
19357 constant-initializer. When we call `grokfield', it will
19358 perform more stringent semantics checks. */
19359 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19360 if (function_declarator_p (declarator)
19361 || (decl_specifiers.type
19362 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19363 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19364 == FUNCTION_TYPE)))
19365 initializer = cp_parser_pure_specifier (parser);
19366 else if (decl_specifiers.storage_class != sc_static)
19367 initializer = cp_parser_save_nsdmi (parser);
19368 else if (cxx_dialect >= cxx0x)
19370 bool nonconst;
19371 /* Don't require a constant rvalue in C++11, since we
19372 might want a reference constant. We'll enforce
19373 constancy later. */
19374 cp_lexer_consume_token (parser->lexer);
19375 /* Parse the initializer. */
19376 initializer = cp_parser_initializer_clause (parser,
19377 &nonconst);
19379 else
19380 /* Parse the initializer. */
19381 initializer = cp_parser_constant_initializer (parser);
19383 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19384 && !function_declarator_p (declarator))
19386 bool x;
19387 if (decl_specifiers.storage_class != sc_static)
19388 initializer = cp_parser_save_nsdmi (parser);
19389 else
19390 initializer = cp_parser_initializer (parser, &x, &x);
19392 /* Otherwise, there is no initializer. */
19393 else
19394 initializer = NULL_TREE;
19396 /* See if we are probably looking at a function
19397 definition. We are certainly not looking at a
19398 member-declarator. Calling `grokfield' has
19399 side-effects, so we must not do it unless we are sure
19400 that we are looking at a member-declarator. */
19401 if (cp_parser_token_starts_function_definition_p
19402 (cp_lexer_peek_token (parser->lexer)))
19404 /* The grammar does not allow a pure-specifier to be
19405 used when a member function is defined. (It is
19406 possible that this fact is an oversight in the
19407 standard, since a pure function may be defined
19408 outside of the class-specifier. */
19409 if (initializer && initializer_token_start)
19410 error_at (initializer_token_start->location,
19411 "pure-specifier on function-definition");
19412 decl = cp_parser_save_member_function_body (parser,
19413 &decl_specifiers,
19414 declarator,
19415 attributes);
19416 /* If the member was not a friend, declare it here. */
19417 if (!friend_p)
19418 finish_member_declaration (decl);
19419 /* Peek at the next token. */
19420 token = cp_lexer_peek_token (parser->lexer);
19421 /* If the next token is a semicolon, consume it. */
19422 if (token->type == CPP_SEMICOLON)
19423 cp_lexer_consume_token (parser->lexer);
19424 goto out;
19426 else
19427 if (declarator->kind == cdk_function)
19428 declarator->id_loc = token->location;
19429 /* Create the declaration. */
19430 decl = grokfield (declarator, &decl_specifiers,
19431 initializer, /*init_const_expr_p=*/true,
19432 asm_specification,
19433 attributes);
19436 /* Reset PREFIX_ATTRIBUTES. */
19437 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19438 attributes = TREE_CHAIN (attributes);
19439 if (attributes)
19440 TREE_CHAIN (attributes) = NULL_TREE;
19442 /* If there is any qualification still in effect, clear it
19443 now; we will be starting fresh with the next declarator. */
19444 parser->scope = NULL_TREE;
19445 parser->qualifying_scope = NULL_TREE;
19446 parser->object_scope = NULL_TREE;
19447 /* If it's a `,', then there are more declarators. */
19448 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19450 cp_lexer_consume_token (parser->lexer);
19451 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19453 cp_token *token = cp_lexer_previous_token (parser->lexer);
19454 error_at (token->location,
19455 "stray %<,%> at end of member declaration");
19458 /* If the next token isn't a `;', then we have a parse error. */
19459 else if (cp_lexer_next_token_is_not (parser->lexer,
19460 CPP_SEMICOLON))
19462 /* The next token might be a ways away from where the
19463 actual semicolon is missing. Find the previous token
19464 and use that for our error position. */
19465 cp_token *token = cp_lexer_previous_token (parser->lexer);
19466 error_at (token->location,
19467 "expected %<;%> at end of member declaration");
19469 /* Assume that the user meant to provide a semicolon. If
19470 we were to cp_parser_skip_to_end_of_statement, we might
19471 skip to a semicolon inside a member function definition
19472 and issue nonsensical error messages. */
19473 assume_semicolon = true;
19476 if (decl)
19478 /* Add DECL to the list of members. */
19479 if (!friend_p)
19480 finish_member_declaration (decl);
19482 if (TREE_CODE (decl) == FUNCTION_DECL)
19483 cp_parser_save_default_args (parser, decl);
19484 else if (TREE_CODE (decl) == FIELD_DECL
19485 && !DECL_C_BIT_FIELD (decl)
19486 && DECL_INITIAL (decl))
19487 /* Add DECL to the queue of NSDMI to be parsed later. */
19488 vec_safe_push (unparsed_nsdmis, decl);
19491 if (assume_semicolon)
19492 goto out;
19496 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19497 out:
19498 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19501 /* Parse a pure-specifier.
19503 pure-specifier:
19506 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19507 Otherwise, ERROR_MARK_NODE is returned. */
19509 static tree
19510 cp_parser_pure_specifier (cp_parser* parser)
19512 cp_token *token;
19514 /* Look for the `=' token. */
19515 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19516 return error_mark_node;
19517 /* Look for the `0' token. */
19518 token = cp_lexer_peek_token (parser->lexer);
19520 if (token->type == CPP_EOF
19521 || token->type == CPP_PRAGMA_EOL)
19522 return error_mark_node;
19524 cp_lexer_consume_token (parser->lexer);
19526 /* Accept = default or = delete in c++0x mode. */
19527 if (token->keyword == RID_DEFAULT
19528 || token->keyword == RID_DELETE)
19530 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19531 return token->u.value;
19534 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19535 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19537 cp_parser_error (parser,
19538 "invalid pure specifier (only %<= 0%> is allowed)");
19539 cp_parser_skip_to_end_of_statement (parser);
19540 return error_mark_node;
19542 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19544 error_at (token->location, "templates may not be %<virtual%>");
19545 return error_mark_node;
19548 return integer_zero_node;
19551 /* Parse a constant-initializer.
19553 constant-initializer:
19554 = constant-expression
19556 Returns a representation of the constant-expression. */
19558 static tree
19559 cp_parser_constant_initializer (cp_parser* parser)
19561 /* Look for the `=' token. */
19562 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19563 return error_mark_node;
19565 /* It is invalid to write:
19567 struct S { static const int i = { 7 }; };
19570 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19572 cp_parser_error (parser,
19573 "a brace-enclosed initializer is not allowed here");
19574 /* Consume the opening brace. */
19575 cp_lexer_consume_token (parser->lexer);
19576 /* Skip the initializer. */
19577 cp_parser_skip_to_closing_brace (parser);
19578 /* Look for the trailing `}'. */
19579 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19581 return error_mark_node;
19584 return cp_parser_constant_expression (parser,
19585 /*allow_non_constant=*/false,
19586 NULL);
19589 /* Derived classes [gram.class.derived] */
19591 /* Parse a base-clause.
19593 base-clause:
19594 : base-specifier-list
19596 base-specifier-list:
19597 base-specifier ... [opt]
19598 base-specifier-list , base-specifier ... [opt]
19600 Returns a TREE_LIST representing the base-classes, in the order in
19601 which they were declared. The representation of each node is as
19602 described by cp_parser_base_specifier.
19604 In the case that no bases are specified, this function will return
19605 NULL_TREE, not ERROR_MARK_NODE. */
19607 static tree
19608 cp_parser_base_clause (cp_parser* parser)
19610 tree bases = NULL_TREE;
19612 /* Look for the `:' that begins the list. */
19613 cp_parser_require (parser, CPP_COLON, RT_COLON);
19615 /* Scan the base-specifier-list. */
19616 while (true)
19618 cp_token *token;
19619 tree base;
19620 bool pack_expansion_p = false;
19622 /* Look for the base-specifier. */
19623 base = cp_parser_base_specifier (parser);
19624 /* Look for the (optional) ellipsis. */
19625 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19627 /* Consume the `...'. */
19628 cp_lexer_consume_token (parser->lexer);
19630 pack_expansion_p = true;
19633 /* Add BASE to the front of the list. */
19634 if (base && base != error_mark_node)
19636 if (pack_expansion_p)
19637 /* Make this a pack expansion type. */
19638 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19640 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19642 TREE_CHAIN (base) = bases;
19643 bases = base;
19646 /* Peek at the next token. */
19647 token = cp_lexer_peek_token (parser->lexer);
19648 /* If it's not a comma, then the list is complete. */
19649 if (token->type != CPP_COMMA)
19650 break;
19651 /* Consume the `,'. */
19652 cp_lexer_consume_token (parser->lexer);
19655 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19656 base class had a qualified name. However, the next name that
19657 appears is certainly not qualified. */
19658 parser->scope = NULL_TREE;
19659 parser->qualifying_scope = NULL_TREE;
19660 parser->object_scope = NULL_TREE;
19662 return nreverse (bases);
19665 /* Parse a base-specifier.
19667 base-specifier:
19668 :: [opt] nested-name-specifier [opt] class-name
19669 virtual access-specifier [opt] :: [opt] nested-name-specifier
19670 [opt] class-name
19671 access-specifier virtual [opt] :: [opt] nested-name-specifier
19672 [opt] class-name
19674 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19675 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19676 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19677 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19679 static tree
19680 cp_parser_base_specifier (cp_parser* parser)
19682 cp_token *token;
19683 bool done = false;
19684 bool virtual_p = false;
19685 bool duplicate_virtual_error_issued_p = false;
19686 bool duplicate_access_error_issued_p = false;
19687 bool class_scope_p, template_p;
19688 tree access = access_default_node;
19689 tree type;
19691 /* Process the optional `virtual' and `access-specifier'. */
19692 while (!done)
19694 /* Peek at the next token. */
19695 token = cp_lexer_peek_token (parser->lexer);
19696 /* Process `virtual'. */
19697 switch (token->keyword)
19699 case RID_VIRTUAL:
19700 /* If `virtual' appears more than once, issue an error. */
19701 if (virtual_p && !duplicate_virtual_error_issued_p)
19703 cp_parser_error (parser,
19704 "%<virtual%> specified more than once in base-specified");
19705 duplicate_virtual_error_issued_p = true;
19708 virtual_p = true;
19710 /* Consume the `virtual' token. */
19711 cp_lexer_consume_token (parser->lexer);
19713 break;
19715 case RID_PUBLIC:
19716 case RID_PROTECTED:
19717 case RID_PRIVATE:
19718 /* If more than one access specifier appears, issue an
19719 error. */
19720 if (access != access_default_node
19721 && !duplicate_access_error_issued_p)
19723 cp_parser_error (parser,
19724 "more than one access specifier in base-specified");
19725 duplicate_access_error_issued_p = true;
19728 access = ridpointers[(int) token->keyword];
19730 /* Consume the access-specifier. */
19731 cp_lexer_consume_token (parser->lexer);
19733 break;
19735 default:
19736 done = true;
19737 break;
19740 /* It is not uncommon to see programs mechanically, erroneously, use
19741 the 'typename' keyword to denote (dependent) qualified types
19742 as base classes. */
19743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19745 token = cp_lexer_peek_token (parser->lexer);
19746 if (!processing_template_decl)
19747 error_at (token->location,
19748 "keyword %<typename%> not allowed outside of templates");
19749 else
19750 error_at (token->location,
19751 "keyword %<typename%> not allowed in this context "
19752 "(the base class is implicitly a type)");
19753 cp_lexer_consume_token (parser->lexer);
19756 /* Look for the optional `::' operator. */
19757 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19758 /* Look for the nested-name-specifier. The simplest way to
19759 implement:
19761 [temp.res]
19763 The keyword `typename' is not permitted in a base-specifier or
19764 mem-initializer; in these contexts a qualified name that
19765 depends on a template-parameter is implicitly assumed to be a
19766 type name.
19768 is to pretend that we have seen the `typename' keyword at this
19769 point. */
19770 cp_parser_nested_name_specifier_opt (parser,
19771 /*typename_keyword_p=*/true,
19772 /*check_dependency_p=*/true,
19773 typename_type,
19774 /*is_declaration=*/true);
19775 /* If the base class is given by a qualified name, assume that names
19776 we see are type names or templates, as appropriate. */
19777 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19778 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19780 if (!parser->scope
19781 && cp_lexer_next_token_is_decltype (parser->lexer))
19782 /* DR 950 allows decltype as a base-specifier. */
19783 type = cp_parser_decltype (parser);
19784 else
19786 /* Otherwise, look for the class-name. */
19787 type = cp_parser_class_name (parser,
19788 class_scope_p,
19789 template_p,
19790 typename_type,
19791 /*check_dependency_p=*/true,
19792 /*class_head_p=*/false,
19793 /*is_declaration=*/true);
19794 type = TREE_TYPE (type);
19797 if (type == error_mark_node)
19798 return error_mark_node;
19800 return finish_base_specifier (type, access, virtual_p);
19803 /* Exception handling [gram.exception] */
19805 /* Parse an (optional) noexcept-specification.
19807 noexcept-specification:
19808 noexcept ( constant-expression ) [opt]
19810 If no noexcept-specification is present, returns NULL_TREE.
19811 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19812 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19813 there are no parentheses. CONSUMED_EXPR will be set accordingly.
19814 Otherwise, returns a noexcept specification unless RETURN_COND is true,
19815 in which case a boolean condition is returned instead. */
19817 static tree
19818 cp_parser_noexcept_specification_opt (cp_parser* parser,
19819 bool require_constexpr,
19820 bool* consumed_expr,
19821 bool return_cond)
19823 cp_token *token;
19824 const char *saved_message;
19826 /* Peek at the next token. */
19827 token = cp_lexer_peek_token (parser->lexer);
19829 /* Is it a noexcept-specification? */
19830 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19832 tree expr;
19833 cp_lexer_consume_token (parser->lexer);
19835 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19837 cp_lexer_consume_token (parser->lexer);
19839 if (require_constexpr)
19841 /* Types may not be defined in an exception-specification. */
19842 saved_message = parser->type_definition_forbidden_message;
19843 parser->type_definition_forbidden_message
19844 = G_("types may not be defined in an exception-specification");
19846 expr = cp_parser_constant_expression (parser, false, NULL);
19848 /* Restore the saved message. */
19849 parser->type_definition_forbidden_message = saved_message;
19851 else
19853 expr = cp_parser_expression (parser, false, NULL);
19854 *consumed_expr = true;
19857 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19859 else
19861 expr = boolean_true_node;
19862 if (!require_constexpr)
19863 *consumed_expr = false;
19866 /* We cannot build a noexcept-spec right away because this will check
19867 that expr is a constexpr. */
19868 if (!return_cond)
19869 return build_noexcept_spec (expr, tf_warning_or_error);
19870 else
19871 return expr;
19873 else
19874 return NULL_TREE;
19877 /* Parse an (optional) exception-specification.
19879 exception-specification:
19880 throw ( type-id-list [opt] )
19882 Returns a TREE_LIST representing the exception-specification. The
19883 TREE_VALUE of each node is a type. */
19885 static tree
19886 cp_parser_exception_specification_opt (cp_parser* parser)
19888 cp_token *token;
19889 tree type_id_list;
19890 const char *saved_message;
19892 /* Peek at the next token. */
19893 token = cp_lexer_peek_token (parser->lexer);
19895 /* Is it a noexcept-specification? */
19896 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19897 false);
19898 if (type_id_list != NULL_TREE)
19899 return type_id_list;
19901 /* If it's not `throw', then there's no exception-specification. */
19902 if (!cp_parser_is_keyword (token, RID_THROW))
19903 return NULL_TREE;
19905 #if 0
19906 /* Enable this once a lot of code has transitioned to noexcept? */
19907 if (cxx_dialect >= cxx0x && !in_system_header)
19908 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19909 "deprecated in C++0x; use %<noexcept%> instead");
19910 #endif
19912 /* Consume the `throw'. */
19913 cp_lexer_consume_token (parser->lexer);
19915 /* Look for the `('. */
19916 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19918 /* Peek at the next token. */
19919 token = cp_lexer_peek_token (parser->lexer);
19920 /* If it's not a `)', then there is a type-id-list. */
19921 if (token->type != CPP_CLOSE_PAREN)
19923 /* Types may not be defined in an exception-specification. */
19924 saved_message = parser->type_definition_forbidden_message;
19925 parser->type_definition_forbidden_message
19926 = G_("types may not be defined in an exception-specification");
19927 /* Parse the type-id-list. */
19928 type_id_list = cp_parser_type_id_list (parser);
19929 /* Restore the saved message. */
19930 parser->type_definition_forbidden_message = saved_message;
19932 else
19933 type_id_list = empty_except_spec;
19935 /* Look for the `)'. */
19936 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19938 return type_id_list;
19941 /* Parse an (optional) type-id-list.
19943 type-id-list:
19944 type-id ... [opt]
19945 type-id-list , type-id ... [opt]
19947 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19948 in the order that the types were presented. */
19950 static tree
19951 cp_parser_type_id_list (cp_parser* parser)
19953 tree types = NULL_TREE;
19955 while (true)
19957 cp_token *token;
19958 tree type;
19960 /* Get the next type-id. */
19961 type = cp_parser_type_id (parser);
19962 /* Parse the optional ellipsis. */
19963 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19965 /* Consume the `...'. */
19966 cp_lexer_consume_token (parser->lexer);
19968 /* Turn the type into a pack expansion expression. */
19969 type = make_pack_expansion (type);
19971 /* Add it to the list. */
19972 types = add_exception_specifier (types, type, /*complain=*/1);
19973 /* Peek at the next token. */
19974 token = cp_lexer_peek_token (parser->lexer);
19975 /* If it is not a `,', we are done. */
19976 if (token->type != CPP_COMMA)
19977 break;
19978 /* Consume the `,'. */
19979 cp_lexer_consume_token (parser->lexer);
19982 return nreverse (types);
19985 /* Parse a try-block.
19987 try-block:
19988 try compound-statement handler-seq */
19990 static tree
19991 cp_parser_try_block (cp_parser* parser)
19993 tree try_block;
19995 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19996 try_block = begin_try_block ();
19997 cp_parser_compound_statement (parser, NULL, true, false);
19998 finish_try_block (try_block);
19999 cp_parser_handler_seq (parser);
20000 finish_handler_sequence (try_block);
20002 return try_block;
20005 /* Parse a function-try-block.
20007 function-try-block:
20008 try ctor-initializer [opt] function-body handler-seq */
20010 static bool
20011 cp_parser_function_try_block (cp_parser* parser)
20013 tree compound_stmt;
20014 tree try_block;
20015 bool ctor_initializer_p;
20017 /* Look for the `try' keyword. */
20018 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
20019 return false;
20020 /* Let the rest of the front end know where we are. */
20021 try_block = begin_function_try_block (&compound_stmt);
20022 /* Parse the function-body. */
20023 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
20024 (parser, /*in_function_try_block=*/true);
20025 /* We're done with the `try' part. */
20026 finish_function_try_block (try_block);
20027 /* Parse the handlers. */
20028 cp_parser_handler_seq (parser);
20029 /* We're done with the handlers. */
20030 finish_function_handler_sequence (try_block, compound_stmt);
20032 return ctor_initializer_p;
20035 /* Parse a handler-seq.
20037 handler-seq:
20038 handler handler-seq [opt] */
20040 static void
20041 cp_parser_handler_seq (cp_parser* parser)
20043 while (true)
20045 cp_token *token;
20047 /* Parse the handler. */
20048 cp_parser_handler (parser);
20049 /* Peek at the next token. */
20050 token = cp_lexer_peek_token (parser->lexer);
20051 /* If it's not `catch' then there are no more handlers. */
20052 if (!cp_parser_is_keyword (token, RID_CATCH))
20053 break;
20057 /* Parse a handler.
20059 handler:
20060 catch ( exception-declaration ) compound-statement */
20062 static void
20063 cp_parser_handler (cp_parser* parser)
20065 tree handler;
20066 tree declaration;
20068 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
20069 handler = begin_handler ();
20070 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20071 declaration = cp_parser_exception_declaration (parser);
20072 finish_handler_parms (declaration, handler);
20073 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20074 cp_parser_compound_statement (parser, NULL, false, false);
20075 finish_handler (handler);
20078 /* Parse an exception-declaration.
20080 exception-declaration:
20081 type-specifier-seq declarator
20082 type-specifier-seq abstract-declarator
20083 type-specifier-seq
20086 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20087 ellipsis variant is used. */
20089 static tree
20090 cp_parser_exception_declaration (cp_parser* parser)
20092 cp_decl_specifier_seq type_specifiers;
20093 cp_declarator *declarator;
20094 const char *saved_message;
20096 /* If it's an ellipsis, it's easy to handle. */
20097 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20099 /* Consume the `...' token. */
20100 cp_lexer_consume_token (parser->lexer);
20101 return NULL_TREE;
20104 /* Types may not be defined in exception-declarations. */
20105 saved_message = parser->type_definition_forbidden_message;
20106 parser->type_definition_forbidden_message
20107 = G_("types may not be defined in exception-declarations");
20109 /* Parse the type-specifier-seq. */
20110 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
20111 /*is_trailing_return=*/false,
20112 &type_specifiers);
20113 /* If it's a `)', then there is no declarator. */
20114 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
20115 declarator = NULL;
20116 else
20117 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
20118 /*ctor_dtor_or_conv_p=*/NULL,
20119 /*parenthesized_p=*/NULL,
20120 /*member_p=*/false);
20122 /* Restore the saved message. */
20123 parser->type_definition_forbidden_message = saved_message;
20125 if (!type_specifiers.any_specifiers_p)
20126 return error_mark_node;
20128 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
20131 /* Parse a throw-expression.
20133 throw-expression:
20134 throw assignment-expression [opt]
20136 Returns a THROW_EXPR representing the throw-expression. */
20138 static tree
20139 cp_parser_throw_expression (cp_parser* parser)
20141 tree expression;
20142 cp_token* token;
20144 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
20145 token = cp_lexer_peek_token (parser->lexer);
20146 /* Figure out whether or not there is an assignment-expression
20147 following the "throw" keyword. */
20148 if (token->type == CPP_COMMA
20149 || token->type == CPP_SEMICOLON
20150 || token->type == CPP_CLOSE_PAREN
20151 || token->type == CPP_CLOSE_SQUARE
20152 || token->type == CPP_CLOSE_BRACE
20153 || token->type == CPP_COLON)
20154 expression = NULL_TREE;
20155 else
20156 expression = cp_parser_assignment_expression (parser,
20157 /*cast_p=*/false, NULL);
20159 return build_throw (expression);
20162 /* GNU Extensions */
20164 /* Parse an (optional) asm-specification.
20166 asm-specification:
20167 asm ( string-literal )
20169 If the asm-specification is present, returns a STRING_CST
20170 corresponding to the string-literal. Otherwise, returns
20171 NULL_TREE. */
20173 static tree
20174 cp_parser_asm_specification_opt (cp_parser* parser)
20176 cp_token *token;
20177 tree asm_specification;
20179 /* Peek at the next token. */
20180 token = cp_lexer_peek_token (parser->lexer);
20181 /* If the next token isn't the `asm' keyword, then there's no
20182 asm-specification. */
20183 if (!cp_parser_is_keyword (token, RID_ASM))
20184 return NULL_TREE;
20186 /* Consume the `asm' token. */
20187 cp_lexer_consume_token (parser->lexer);
20188 /* Look for the `('. */
20189 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20191 /* Look for the string-literal. */
20192 asm_specification = cp_parser_string_literal (parser, false, false);
20194 /* Look for the `)'. */
20195 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20197 return asm_specification;
20200 /* Parse an asm-operand-list.
20202 asm-operand-list:
20203 asm-operand
20204 asm-operand-list , asm-operand
20206 asm-operand:
20207 string-literal ( expression )
20208 [ string-literal ] string-literal ( expression )
20210 Returns a TREE_LIST representing the operands. The TREE_VALUE of
20211 each node is the expression. The TREE_PURPOSE is itself a
20212 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
20213 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
20214 is a STRING_CST for the string literal before the parenthesis. Returns
20215 ERROR_MARK_NODE if any of the operands are invalid. */
20217 static tree
20218 cp_parser_asm_operand_list (cp_parser* parser)
20220 tree asm_operands = NULL_TREE;
20221 bool invalid_operands = false;
20223 while (true)
20225 tree string_literal;
20226 tree expression;
20227 tree name;
20229 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20231 /* Consume the `[' token. */
20232 cp_lexer_consume_token (parser->lexer);
20233 /* Read the operand name. */
20234 name = cp_parser_identifier (parser);
20235 if (name != error_mark_node)
20236 name = build_string (IDENTIFIER_LENGTH (name),
20237 IDENTIFIER_POINTER (name));
20238 /* Look for the closing `]'. */
20239 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20241 else
20242 name = NULL_TREE;
20243 /* Look for the string-literal. */
20244 string_literal = cp_parser_string_literal (parser, false, false);
20246 /* Look for the `('. */
20247 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20248 /* Parse the expression. */
20249 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20250 /* Look for the `)'. */
20251 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20253 if (name == error_mark_node
20254 || string_literal == error_mark_node
20255 || expression == error_mark_node)
20256 invalid_operands = true;
20258 /* Add this operand to the list. */
20259 asm_operands = tree_cons (build_tree_list (name, string_literal),
20260 expression,
20261 asm_operands);
20262 /* If the next token is not a `,', there are no more
20263 operands. */
20264 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20265 break;
20266 /* Consume the `,'. */
20267 cp_lexer_consume_token (parser->lexer);
20270 return invalid_operands ? error_mark_node : nreverse (asm_operands);
20273 /* Parse an asm-clobber-list.
20275 asm-clobber-list:
20276 string-literal
20277 asm-clobber-list , string-literal
20279 Returns a TREE_LIST, indicating the clobbers in the order that they
20280 appeared. The TREE_VALUE of each node is a STRING_CST. */
20282 static tree
20283 cp_parser_asm_clobber_list (cp_parser* parser)
20285 tree clobbers = NULL_TREE;
20287 while (true)
20289 tree string_literal;
20291 /* Look for the string literal. */
20292 string_literal = cp_parser_string_literal (parser, false, false);
20293 /* Add it to the list. */
20294 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20295 /* If the next token is not a `,', then the list is
20296 complete. */
20297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20298 break;
20299 /* Consume the `,' token. */
20300 cp_lexer_consume_token (parser->lexer);
20303 return clobbers;
20306 /* Parse an asm-label-list.
20308 asm-label-list:
20309 identifier
20310 asm-label-list , identifier
20312 Returns a TREE_LIST, indicating the labels in the order that they
20313 appeared. The TREE_VALUE of each node is a label. */
20315 static tree
20316 cp_parser_asm_label_list (cp_parser* parser)
20318 tree labels = NULL_TREE;
20320 while (true)
20322 tree identifier, label, name;
20324 /* Look for the identifier. */
20325 identifier = cp_parser_identifier (parser);
20326 if (!error_operand_p (identifier))
20328 label = lookup_label (identifier);
20329 if (TREE_CODE (label) == LABEL_DECL)
20331 TREE_USED (label) = 1;
20332 check_goto (label);
20333 name = build_string (IDENTIFIER_LENGTH (identifier),
20334 IDENTIFIER_POINTER (identifier));
20335 labels = tree_cons (name, label, labels);
20338 /* If the next token is not a `,', then the list is
20339 complete. */
20340 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20341 break;
20342 /* Consume the `,' token. */
20343 cp_lexer_consume_token (parser->lexer);
20346 return nreverse (labels);
20349 /* Return TRUE iff the next tokens in the stream are possibly the
20350 beginning of a GNU extension attribute. */
20352 static bool
20353 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
20355 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
20358 /* Return TRUE iff the next tokens in the stream are possibly the
20359 beginning of a standard C++-11 attribute specifier. */
20361 static bool
20362 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
20364 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
20367 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20368 beginning of a standard C++-11 attribute specifier. */
20370 static bool
20371 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
20373 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20375 return (cxx_dialect >= cxx0x
20376 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
20377 || (token->type == CPP_OPEN_SQUARE
20378 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
20379 && token->type == CPP_OPEN_SQUARE)));
20382 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20383 beginning of a GNU extension attribute. */
20385 static bool
20386 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
20388 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20390 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
20393 /* Return true iff the next tokens can be the beginning of either a
20394 GNU attribute list, or a standard C++11 attribute sequence. */
20396 static bool
20397 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
20399 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
20400 || cp_next_tokens_can_be_std_attribute_p (parser));
20403 /* Return true iff the next Nth tokens can be the beginning of either
20404 a GNU attribute list, or a standard C++11 attribute sequence. */
20406 static bool
20407 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
20409 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
20410 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
20413 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
20414 of GNU attributes, or return NULL. */
20416 static tree
20417 cp_parser_attributes_opt (cp_parser *parser)
20419 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
20420 return cp_parser_gnu_attributes_opt (parser);
20421 return cp_parser_std_attribute_spec_seq (parser);
20424 /* Parse an (optional) series of attributes.
20426 attributes:
20427 attributes attribute
20429 attribute:
20430 __attribute__ (( attribute-list [opt] ))
20432 The return value is as for cp_parser_gnu_attribute_list. */
20434 static tree
20435 cp_parser_gnu_attributes_opt (cp_parser* parser)
20437 tree attributes = NULL_TREE;
20439 while (true)
20441 cp_token *token;
20442 tree attribute_list;
20443 bool ok = true;
20445 /* Peek at the next token. */
20446 token = cp_lexer_peek_token (parser->lexer);
20447 /* If it's not `__attribute__', then we're done. */
20448 if (token->keyword != RID_ATTRIBUTE)
20449 break;
20451 /* Consume the `__attribute__' keyword. */
20452 cp_lexer_consume_token (parser->lexer);
20453 /* Look for the two `(' tokens. */
20454 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20455 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20457 /* Peek at the next token. */
20458 token = cp_lexer_peek_token (parser->lexer);
20459 if (token->type != CPP_CLOSE_PAREN)
20460 /* Parse the attribute-list. */
20461 attribute_list = cp_parser_gnu_attribute_list (parser);
20462 else
20463 /* If the next token is a `)', then there is no attribute
20464 list. */
20465 attribute_list = NULL;
20467 /* Look for the two `)' tokens. */
20468 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20469 ok = false;
20470 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20471 ok = false;
20472 if (!ok)
20473 cp_parser_skip_to_end_of_statement (parser);
20475 /* Add these new attributes to the list. */
20476 attributes = chainon (attributes, attribute_list);
20479 return attributes;
20482 /* Parse a GNU attribute-list.
20484 attribute-list:
20485 attribute
20486 attribute-list , attribute
20488 attribute:
20489 identifier
20490 identifier ( identifier )
20491 identifier ( identifier , expression-list )
20492 identifier ( expression-list )
20494 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20495 to an attribute. The TREE_PURPOSE of each node is the identifier
20496 indicating which attribute is in use. The TREE_VALUE represents
20497 the arguments, if any. */
20499 static tree
20500 cp_parser_gnu_attribute_list (cp_parser* parser)
20502 tree attribute_list = NULL_TREE;
20503 bool save_translate_strings_p = parser->translate_strings_p;
20505 parser->translate_strings_p = false;
20506 while (true)
20508 cp_token *token;
20509 tree identifier;
20510 tree attribute;
20512 /* Look for the identifier. We also allow keywords here; for
20513 example `__attribute__ ((const))' is legal. */
20514 token = cp_lexer_peek_token (parser->lexer);
20515 if (token->type == CPP_NAME
20516 || token->type == CPP_KEYWORD)
20518 tree arguments = NULL_TREE;
20520 /* Consume the token. */
20521 token = cp_lexer_consume_token (parser->lexer);
20523 /* Save away the identifier that indicates which attribute
20524 this is. */
20525 identifier = (token->type == CPP_KEYWORD)
20526 /* For keywords, use the canonical spelling, not the
20527 parsed identifier. */
20528 ? ridpointers[(int) token->keyword]
20529 : token->u.value;
20531 attribute = build_tree_list (identifier, NULL_TREE);
20533 /* Peek at the next token. */
20534 token = cp_lexer_peek_token (parser->lexer);
20535 /* If it's an `(', then parse the attribute arguments. */
20536 if (token->type == CPP_OPEN_PAREN)
20538 vec<tree, va_gc> *vec;
20539 int attr_flag = (attribute_takes_identifier_p (identifier)
20540 ? id_attr : normal_attr);
20541 vec = cp_parser_parenthesized_expression_list
20542 (parser, attr_flag, /*cast_p=*/false,
20543 /*allow_expansion_p=*/false,
20544 /*non_constant_p=*/NULL);
20545 if (vec == NULL)
20546 arguments = error_mark_node;
20547 else
20549 arguments = build_tree_list_vec (vec);
20550 release_tree_vector (vec);
20552 /* Save the arguments away. */
20553 TREE_VALUE (attribute) = arguments;
20556 if (arguments != error_mark_node)
20558 /* Add this attribute to the list. */
20559 TREE_CHAIN (attribute) = attribute_list;
20560 attribute_list = attribute;
20563 token = cp_lexer_peek_token (parser->lexer);
20565 /* Now, look for more attributes. If the next token isn't a
20566 `,', we're done. */
20567 if (token->type != CPP_COMMA)
20568 break;
20570 /* Consume the comma and keep going. */
20571 cp_lexer_consume_token (parser->lexer);
20573 parser->translate_strings_p = save_translate_strings_p;
20575 /* We built up the list in reverse order. */
20576 return nreverse (attribute_list);
20579 /* Parse a standard C++11 attribute.
20581 The returned representation is a TREE_LIST which TREE_PURPOSE is
20582 the scoped name of the attribute, and the TREE_VALUE is its
20583 arguments list.
20585 Note that the scoped name of the attribute is itself a TREE_LIST
20586 which TREE_PURPOSE is the namespace of the attribute, and
20587 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
20588 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
20589 and which TREE_PURPOSE is directly the attribute name.
20591 Clients of the attribute code should use get_attribute_namespace
20592 and get_attribute_name to get the actual namespace and name of
20593 attributes, regardless of their being GNU or C++11 attributes.
20595 attribute:
20596 attribute-token attribute-argument-clause [opt]
20598 attribute-token:
20599 identifier
20600 attribute-scoped-token
20602 attribute-scoped-token:
20603 attribute-namespace :: identifier
20605 attribute-namespace:
20606 identifier
20608 attribute-argument-clause:
20609 ( balanced-token-seq )
20611 balanced-token-seq:
20612 balanced-token [opt]
20613 balanced-token-seq balanced-token
20615 balanced-token:
20616 ( balanced-token-seq )
20617 [ balanced-token-seq ]
20618 { balanced-token-seq }. */
20620 static tree
20621 cp_parser_std_attribute (cp_parser *parser)
20623 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
20624 cp_token *token;
20626 /* First, parse name of the the attribute, a.k.a
20627 attribute-token. */
20629 token = cp_lexer_peek_token (parser->lexer);
20630 if (token->type == CPP_NAME)
20631 attr_id = token->u.value;
20632 else if (token->type == CPP_KEYWORD)
20633 attr_id = ridpointers[(int) token->keyword];
20634 else if (token->flags & NAMED_OP)
20635 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
20637 if (attr_id == NULL_TREE)
20638 return NULL_TREE;
20640 cp_lexer_consume_token (parser->lexer);
20642 token = cp_lexer_peek_token (parser->lexer);
20643 if (token->type == CPP_SCOPE)
20645 /* We are seeing a scoped attribute token. */
20647 cp_lexer_consume_token (parser->lexer);
20648 attr_ns = attr_id;
20650 token = cp_lexer_consume_token (parser->lexer);
20651 if (token->type == CPP_NAME)
20652 attr_id = token->u.value;
20653 else if (token->type == CPP_KEYWORD)
20654 attr_id = ridpointers[(int) token->keyword];
20655 else
20657 error_at (token->location,
20658 "expected an identifier for the attribute name");
20659 return error_mark_node;
20661 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
20662 NULL_TREE);
20663 token = cp_lexer_peek_token (parser->lexer);
20665 else
20666 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
20667 NULL_TREE);
20669 /* Now parse the optional argument clause of the attribute. */
20671 if (token->type != CPP_OPEN_PAREN)
20672 return attribute;
20675 vec<tree, va_gc> *vec;
20676 int attr_flag = normal_attr;
20678 if (attr_ns == get_identifier ("gnu")
20679 && attribute_takes_identifier_p (attr_id))
20680 /* A GNU attribute that takes an identifier in parameter. */
20681 attr_flag = id_attr;
20683 vec = cp_parser_parenthesized_expression_list
20684 (parser, attr_flag, /*cast_p=*/false,
20685 /*allow_expansion_p=*/true,
20686 /*non_constant_p=*/NULL);
20687 if (vec == NULL)
20688 arguments = error_mark_node;
20689 else
20691 arguments = build_tree_list_vec (vec);
20692 release_tree_vector (vec);
20695 if (arguments == error_mark_node)
20696 attribute = error_mark_node;
20697 else
20698 TREE_VALUE (attribute) = arguments;
20701 return attribute;
20704 /* Parse a list of standard C++-11 attributes.
20706 attribute-list:
20707 attribute [opt]
20708 attribute-list , attribute[opt]
20709 attribute ...
20710 attribute-list , attribute ...
20713 static tree
20714 cp_parser_std_attribute_list (cp_parser *parser)
20716 tree attributes = NULL_TREE, attribute = NULL_TREE;
20717 cp_token *token = NULL;
20719 while (true)
20721 attribute = cp_parser_std_attribute (parser);
20722 if (attribute == error_mark_node)
20723 break;
20724 if (attribute != NULL_TREE)
20726 TREE_CHAIN (attribute) = attributes;
20727 attributes = attribute;
20729 token = cp_lexer_peek_token (parser->lexer);
20730 if (token->type != CPP_COMMA)
20731 break;
20732 cp_lexer_consume_token (parser->lexer);
20734 attributes = nreverse (attributes);
20735 return attributes;
20738 /* Parse a standard C++-11 attribute specifier.
20740 attribute-specifier:
20741 [ [ attribute-list ] ]
20742 alignment-specifier
20744 alignment-specifier:
20745 alignas ( type-id ... [opt] )
20746 alignas ( alignment-expression ... [opt] ). */
20748 static tree
20749 cp_parser_std_attribute_spec (cp_parser *parser)
20751 tree attributes = NULL_TREE;
20752 cp_token *token = cp_lexer_peek_token (parser->lexer);
20754 if (token->type == CPP_OPEN_SQUARE
20755 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
20757 cp_lexer_consume_token (parser->lexer);
20758 cp_lexer_consume_token (parser->lexer);
20760 attributes = cp_parser_std_attribute_list (parser);
20762 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
20763 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20764 cp_parser_skip_to_end_of_statement (parser);
20765 else
20766 /* Warn about parsing c++11 attribute in non-c++1 mode, only
20767 when we are sure that we have actually parsed them. */
20768 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
20770 else
20772 tree alignas_expr;
20774 /* Look for an alignment-specifier. */
20776 token = cp_lexer_peek_token (parser->lexer);
20778 if (token->type != CPP_KEYWORD
20779 || token->keyword != RID_ALIGNAS)
20780 return NULL_TREE;
20782 cp_lexer_consume_token (parser->lexer);
20783 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
20785 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
20787 cp_parser_error (parser, "expected %<(%>");
20788 return error_mark_node;
20791 cp_parser_parse_tentatively (parser);
20792 alignas_expr = cp_parser_type_id (parser);
20794 if (!cp_parser_parse_definitely (parser))
20796 gcc_assert (alignas_expr == error_mark_node
20797 || alignas_expr == NULL_TREE);
20799 alignas_expr =
20800 cp_parser_assignment_expression (parser, /*cast_p=*/false,
20801 /**cp_id_kind=*/NULL);
20802 if (alignas_expr == NULL_TREE
20803 || alignas_expr == error_mark_node)
20804 return alignas_expr;
20807 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
20809 cp_parser_error (parser, "expected %<)%>");
20810 return error_mark_node;
20813 alignas_expr = cxx_alignas_expr (alignas_expr);
20815 /* Build the C++-11 representation of an 'aligned'
20816 attribute. */
20817 attributes =
20818 build_tree_list (build_tree_list (get_identifier ("gnu"),
20819 get_identifier ("aligned")),
20820 build_tree_list (NULL_TREE, alignas_expr));
20823 return attributes;
20826 /* Parse a standard C++-11 attribute-specifier-seq.
20828 attribute-specifier-seq:
20829 attribute-specifier-seq [opt] attribute-specifier
20832 static tree
20833 cp_parser_std_attribute_spec_seq (cp_parser *parser)
20835 tree attr_specs = NULL;
20837 while (true)
20839 tree attr_spec = cp_parser_std_attribute_spec (parser);
20840 if (attr_spec == NULL_TREE)
20841 break;
20842 if (attr_spec == error_mark_node)
20843 return error_mark_node;
20845 TREE_CHAIN (attr_spec) = attr_specs;
20846 attr_specs = attr_spec;
20849 attr_specs = nreverse (attr_specs);
20850 return attr_specs;
20853 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20854 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20855 current value of the PEDANTIC flag, regardless of whether or not
20856 the `__extension__' keyword is present. The caller is responsible
20857 for restoring the value of the PEDANTIC flag. */
20859 static bool
20860 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20862 /* Save the old value of the PEDANTIC flag. */
20863 *saved_pedantic = pedantic;
20865 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20867 /* Consume the `__extension__' token. */
20868 cp_lexer_consume_token (parser->lexer);
20869 /* We're not being pedantic while the `__extension__' keyword is
20870 in effect. */
20871 pedantic = 0;
20873 return true;
20876 return false;
20879 /* Parse a label declaration.
20881 label-declaration:
20882 __label__ label-declarator-seq ;
20884 label-declarator-seq:
20885 identifier , label-declarator-seq
20886 identifier */
20888 static void
20889 cp_parser_label_declaration (cp_parser* parser)
20891 /* Look for the `__label__' keyword. */
20892 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20894 while (true)
20896 tree identifier;
20898 /* Look for an identifier. */
20899 identifier = cp_parser_identifier (parser);
20900 /* If we failed, stop. */
20901 if (identifier == error_mark_node)
20902 break;
20903 /* Declare it as a label. */
20904 finish_label_decl (identifier);
20905 /* If the next token is a `;', stop. */
20906 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20907 break;
20908 /* Look for the `,' separating the label declarations. */
20909 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20912 /* Look for the final `;'. */
20913 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20916 /* Support Functions */
20918 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20919 NAME should have one of the representations used for an
20920 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20921 is returned. If PARSER->SCOPE is a dependent type, then a
20922 SCOPE_REF is returned.
20924 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20925 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20926 was formed. Abstractly, such entities should not be passed to this
20927 function, because they do not need to be looked up, but it is
20928 simpler to check for this special case here, rather than at the
20929 call-sites.
20931 In cases not explicitly covered above, this function returns a
20932 DECL, OVERLOAD, or baselink representing the result of the lookup.
20933 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20934 is returned.
20936 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20937 (e.g., "struct") that was used. In that case bindings that do not
20938 refer to types are ignored.
20940 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20941 ignored.
20943 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20944 are ignored.
20946 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20947 types.
20949 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20950 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20951 NULL_TREE otherwise. */
20953 static tree
20954 cp_parser_lookup_name (cp_parser *parser, tree name,
20955 enum tag_types tag_type,
20956 bool is_template,
20957 bool is_namespace,
20958 bool check_dependency,
20959 tree *ambiguous_decls,
20960 location_t name_location)
20962 tree decl;
20963 tree object_type = parser->context->object_type;
20965 /* Assume that the lookup will be unambiguous. */
20966 if (ambiguous_decls)
20967 *ambiguous_decls = NULL_TREE;
20969 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20970 no longer valid. Note that if we are parsing tentatively, and
20971 the parse fails, OBJECT_TYPE will be automatically restored. */
20972 parser->context->object_type = NULL_TREE;
20974 if (name == error_mark_node)
20975 return error_mark_node;
20977 /* A template-id has already been resolved; there is no lookup to
20978 do. */
20979 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20980 return name;
20981 if (BASELINK_P (name))
20983 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20984 == TEMPLATE_ID_EXPR);
20985 return name;
20988 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20989 it should already have been checked to make sure that the name
20990 used matches the type being destroyed. */
20991 if (TREE_CODE (name) == BIT_NOT_EXPR)
20993 tree type;
20995 /* Figure out to which type this destructor applies. */
20996 if (parser->scope)
20997 type = parser->scope;
20998 else if (object_type)
20999 type = object_type;
21000 else
21001 type = current_class_type;
21002 /* If that's not a class type, there is no destructor. */
21003 if (!type || !CLASS_TYPE_P (type))
21004 return error_mark_node;
21005 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
21006 lazily_declare_fn (sfk_destructor, type);
21007 if (!CLASSTYPE_DESTRUCTORS (type))
21008 return error_mark_node;
21009 /* If it was a class type, return the destructor. */
21010 return CLASSTYPE_DESTRUCTORS (type);
21013 /* By this point, the NAME should be an ordinary identifier. If
21014 the id-expression was a qualified name, the qualifying scope is
21015 stored in PARSER->SCOPE at this point. */
21016 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
21018 /* Perform the lookup. */
21019 if (parser->scope)
21021 bool dependent_p;
21023 if (parser->scope == error_mark_node)
21024 return error_mark_node;
21026 /* If the SCOPE is dependent, the lookup must be deferred until
21027 the template is instantiated -- unless we are explicitly
21028 looking up names in uninstantiated templates. Even then, we
21029 cannot look up the name if the scope is not a class type; it
21030 might, for example, be a template type parameter. */
21031 dependent_p = (TYPE_P (parser->scope)
21032 && dependent_scope_p (parser->scope));
21033 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
21034 && dependent_p)
21035 /* Defer lookup. */
21036 decl = error_mark_node;
21037 else
21039 tree pushed_scope = NULL_TREE;
21041 /* If PARSER->SCOPE is a dependent type, then it must be a
21042 class type, and we must not be checking dependencies;
21043 otherwise, we would have processed this lookup above. So
21044 that PARSER->SCOPE is not considered a dependent base by
21045 lookup_member, we must enter the scope here. */
21046 if (dependent_p)
21047 pushed_scope = push_scope (parser->scope);
21049 /* If the PARSER->SCOPE is a template specialization, it
21050 may be instantiated during name lookup. In that case,
21051 errors may be issued. Even if we rollback the current
21052 tentative parse, those errors are valid. */
21053 decl = lookup_qualified_name (parser->scope, name,
21054 tag_type != none_type,
21055 /*complain=*/true);
21057 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21058 lookup result and the nested-name-specifier nominates a class C:
21059 * if the name specified after the nested-name-specifier, when
21060 looked up in C, is the injected-class-name of C (Clause 9), or
21061 * if the name specified after the nested-name-specifier is the
21062 same as the identifier or the simple-template-id's template-
21063 name in the last component of the nested-name-specifier,
21064 the name is instead considered to name the constructor of
21065 class C. [ Note: for example, the constructor is not an
21066 acceptable lookup result in an elaborated-type-specifier so
21067 the constructor would not be used in place of the
21068 injected-class-name. --end note ] Such a constructor name
21069 shall be used only in the declarator-id of a declaration that
21070 names a constructor or in a using-declaration. */
21071 if (tag_type == none_type
21072 && DECL_SELF_REFERENCE_P (decl)
21073 && same_type_p (DECL_CONTEXT (decl), parser->scope))
21074 decl = lookup_qualified_name (parser->scope, ctor_identifier,
21075 tag_type != none_type,
21076 /*complain=*/true);
21078 /* If we have a single function from a using decl, pull it out. */
21079 if (TREE_CODE (decl) == OVERLOAD
21080 && !really_overloaded_fn (decl))
21081 decl = OVL_FUNCTION (decl);
21083 if (pushed_scope)
21084 pop_scope (pushed_scope);
21087 /* If the scope is a dependent type and either we deferred lookup or
21088 we did lookup but didn't find the name, rememeber the name. */
21089 if (decl == error_mark_node && TYPE_P (parser->scope)
21090 && dependent_type_p (parser->scope))
21092 if (tag_type)
21094 tree type;
21096 /* The resolution to Core Issue 180 says that `struct
21097 A::B' should be considered a type-name, even if `A'
21098 is dependent. */
21099 type = make_typename_type (parser->scope, name, tag_type,
21100 /*complain=*/tf_error);
21101 decl = TYPE_NAME (type);
21103 else if (is_template
21104 && (cp_parser_next_token_ends_template_argument_p (parser)
21105 || cp_lexer_next_token_is (parser->lexer,
21106 CPP_CLOSE_PAREN)))
21107 decl = make_unbound_class_template (parser->scope,
21108 name, NULL_TREE,
21109 /*complain=*/tf_error);
21110 else
21111 decl = build_qualified_name (/*type=*/NULL_TREE,
21112 parser->scope, name,
21113 is_template);
21115 parser->qualifying_scope = parser->scope;
21116 parser->object_scope = NULL_TREE;
21118 else if (object_type)
21120 tree object_decl = NULL_TREE;
21121 /* Look up the name in the scope of the OBJECT_TYPE, unless the
21122 OBJECT_TYPE is not a class. */
21123 if (CLASS_TYPE_P (object_type))
21124 /* If the OBJECT_TYPE is a template specialization, it may
21125 be instantiated during name lookup. In that case, errors
21126 may be issued. Even if we rollback the current tentative
21127 parse, those errors are valid. */
21128 object_decl = lookup_member (object_type,
21129 name,
21130 /*protect=*/0,
21131 tag_type != none_type,
21132 tf_warning_or_error);
21133 /* Look it up in the enclosing context, too. */
21134 decl = lookup_name_real (name, tag_type != none_type,
21135 /*nonclass=*/0,
21136 /*block_p=*/true, is_namespace, 0);
21137 parser->object_scope = object_type;
21138 parser->qualifying_scope = NULL_TREE;
21139 if (object_decl)
21140 decl = object_decl;
21142 else
21144 decl = lookup_name_real (name, tag_type != none_type,
21145 /*nonclass=*/0,
21146 /*block_p=*/true, is_namespace, 0);
21147 parser->qualifying_scope = NULL_TREE;
21148 parser->object_scope = NULL_TREE;
21151 /* If the lookup failed, let our caller know. */
21152 if (!decl || decl == error_mark_node)
21153 return error_mark_node;
21155 /* Pull out the template from an injected-class-name (or multiple). */
21156 if (is_template)
21157 decl = maybe_get_template_decl_from_type_decl (decl);
21159 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
21160 if (TREE_CODE (decl) == TREE_LIST)
21162 if (ambiguous_decls)
21163 *ambiguous_decls = decl;
21164 /* The error message we have to print is too complicated for
21165 cp_parser_error, so we incorporate its actions directly. */
21166 if (!cp_parser_simulate_error (parser))
21168 error_at (name_location, "reference to %qD is ambiguous",
21169 name);
21170 print_candidates (decl);
21172 return error_mark_node;
21175 gcc_assert (DECL_P (decl)
21176 || TREE_CODE (decl) == OVERLOAD
21177 || TREE_CODE (decl) == SCOPE_REF
21178 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
21179 || BASELINK_P (decl));
21181 /* If we have resolved the name of a member declaration, check to
21182 see if the declaration is accessible. When the name resolves to
21183 set of overloaded functions, accessibility is checked when
21184 overload resolution is done.
21186 During an explicit instantiation, access is not checked at all,
21187 as per [temp.explicit]. */
21188 if (DECL_P (decl))
21189 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
21191 maybe_record_typedef_use (decl);
21193 return decl;
21196 /* Like cp_parser_lookup_name, but for use in the typical case where
21197 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
21198 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
21200 static tree
21201 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
21203 return cp_parser_lookup_name (parser, name,
21204 none_type,
21205 /*is_template=*/false,
21206 /*is_namespace=*/false,
21207 /*check_dependency=*/true,
21208 /*ambiguous_decls=*/NULL,
21209 location);
21212 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
21213 the current context, return the TYPE_DECL. If TAG_NAME_P is
21214 true, the DECL indicates the class being defined in a class-head,
21215 or declared in an elaborated-type-specifier.
21217 Otherwise, return DECL. */
21219 static tree
21220 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
21222 /* If the TEMPLATE_DECL is being declared as part of a class-head,
21223 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
21225 struct A {
21226 template <typename T> struct B;
21229 template <typename T> struct A::B {};
21231 Similarly, in an elaborated-type-specifier:
21233 namespace N { struct X{}; }
21235 struct A {
21236 template <typename T> friend struct N::X;
21239 However, if the DECL refers to a class type, and we are in
21240 the scope of the class, then the name lookup automatically
21241 finds the TYPE_DECL created by build_self_reference rather
21242 than a TEMPLATE_DECL. For example, in:
21244 template <class T> struct S {
21245 S s;
21248 there is no need to handle such case. */
21250 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
21251 return DECL_TEMPLATE_RESULT (decl);
21253 return decl;
21256 /* If too many, or too few, template-parameter lists apply to the
21257 declarator, issue an error message. Returns TRUE if all went well,
21258 and FALSE otherwise. */
21260 static bool
21261 cp_parser_check_declarator_template_parameters (cp_parser* parser,
21262 cp_declarator *declarator,
21263 location_t declarator_location)
21265 switch (declarator->kind)
21267 case cdk_id:
21269 unsigned num_templates = 0;
21270 tree scope = declarator->u.id.qualifying_scope;
21272 if (scope)
21273 num_templates = num_template_headers_for_class (scope);
21274 else if (TREE_CODE (declarator->u.id.unqualified_name)
21275 == TEMPLATE_ID_EXPR)
21276 /* If the DECLARATOR has the form `X<y>' then it uses one
21277 additional level of template parameters. */
21278 ++num_templates;
21280 return cp_parser_check_template_parameters
21281 (parser, num_templates, declarator_location, declarator);
21284 case cdk_function:
21285 case cdk_array:
21286 case cdk_pointer:
21287 case cdk_reference:
21288 case cdk_ptrmem:
21289 return (cp_parser_check_declarator_template_parameters
21290 (parser, declarator->declarator, declarator_location));
21292 case cdk_error:
21293 return true;
21295 default:
21296 gcc_unreachable ();
21298 return false;
21301 /* NUM_TEMPLATES were used in the current declaration. If that is
21302 invalid, return FALSE and issue an error messages. Otherwise,
21303 return TRUE. If DECLARATOR is non-NULL, then we are checking a
21304 declarator and we can print more accurate diagnostics. */
21306 static bool
21307 cp_parser_check_template_parameters (cp_parser* parser,
21308 unsigned num_templates,
21309 location_t location,
21310 cp_declarator *declarator)
21312 /* If there are the same number of template classes and parameter
21313 lists, that's OK. */
21314 if (parser->num_template_parameter_lists == num_templates)
21315 return true;
21316 /* If there are more, but only one more, then we are referring to a
21317 member template. That's OK too. */
21318 if (parser->num_template_parameter_lists == num_templates + 1)
21319 return true;
21320 /* If there are more template classes than parameter lists, we have
21321 something like:
21323 template <class T> void S<T>::R<T>::f (); */
21324 if (parser->num_template_parameter_lists < num_templates)
21326 if (declarator && !current_function_decl)
21327 error_at (location, "specializing member %<%T::%E%> "
21328 "requires %<template<>%> syntax",
21329 declarator->u.id.qualifying_scope,
21330 declarator->u.id.unqualified_name);
21331 else if (declarator)
21332 error_at (location, "invalid declaration of %<%T::%E%>",
21333 declarator->u.id.qualifying_scope,
21334 declarator->u.id.unqualified_name);
21335 else
21336 error_at (location, "too few template-parameter-lists");
21337 return false;
21339 /* Otherwise, there are too many template parameter lists. We have
21340 something like:
21342 template <class T> template <class U> void S::f(); */
21343 error_at (location, "too many template-parameter-lists");
21344 return false;
21347 /* Parse an optional `::' token indicating that the following name is
21348 from the global namespace. If so, PARSER->SCOPE is set to the
21349 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
21350 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
21351 Returns the new value of PARSER->SCOPE, if the `::' token is
21352 present, and NULL_TREE otherwise. */
21354 static tree
21355 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
21357 cp_token *token;
21359 /* Peek at the next token. */
21360 token = cp_lexer_peek_token (parser->lexer);
21361 /* If we're looking at a `::' token then we're starting from the
21362 global namespace, not our current location. */
21363 if (token->type == CPP_SCOPE)
21365 /* Consume the `::' token. */
21366 cp_lexer_consume_token (parser->lexer);
21367 /* Set the SCOPE so that we know where to start the lookup. */
21368 parser->scope = global_namespace;
21369 parser->qualifying_scope = global_namespace;
21370 parser->object_scope = NULL_TREE;
21372 return parser->scope;
21374 else if (!current_scope_valid_p)
21376 parser->scope = NULL_TREE;
21377 parser->qualifying_scope = NULL_TREE;
21378 parser->object_scope = NULL_TREE;
21381 return NULL_TREE;
21384 /* Returns TRUE if the upcoming token sequence is the start of a
21385 constructor declarator. If FRIEND_P is true, the declarator is
21386 preceded by the `friend' specifier. */
21388 static bool
21389 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
21391 bool constructor_p;
21392 tree nested_name_specifier;
21393 cp_token *next_token;
21395 /* The common case is that this is not a constructor declarator, so
21396 try to avoid doing lots of work if at all possible. It's not
21397 valid declare a constructor at function scope. */
21398 if (parser->in_function_body)
21399 return false;
21400 /* And only certain tokens can begin a constructor declarator. */
21401 next_token = cp_lexer_peek_token (parser->lexer);
21402 if (next_token->type != CPP_NAME
21403 && next_token->type != CPP_SCOPE
21404 && next_token->type != CPP_NESTED_NAME_SPECIFIER
21405 && next_token->type != CPP_TEMPLATE_ID)
21406 return false;
21408 /* Parse tentatively; we are going to roll back all of the tokens
21409 consumed here. */
21410 cp_parser_parse_tentatively (parser);
21411 /* Assume that we are looking at a constructor declarator. */
21412 constructor_p = true;
21414 /* Look for the optional `::' operator. */
21415 cp_parser_global_scope_opt (parser,
21416 /*current_scope_valid_p=*/false);
21417 /* Look for the nested-name-specifier. */
21418 nested_name_specifier
21419 = (cp_parser_nested_name_specifier_opt (parser,
21420 /*typename_keyword_p=*/false,
21421 /*check_dependency_p=*/false,
21422 /*type_p=*/false,
21423 /*is_declaration=*/false));
21424 /* Outside of a class-specifier, there must be a
21425 nested-name-specifier. */
21426 if (!nested_name_specifier &&
21427 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
21428 || friend_p))
21429 constructor_p = false;
21430 else if (nested_name_specifier == error_mark_node)
21431 constructor_p = false;
21433 /* If we have a class scope, this is easy; DR 147 says that S::S always
21434 names the constructor, and no other qualified name could. */
21435 if (constructor_p && nested_name_specifier
21436 && CLASS_TYPE_P (nested_name_specifier))
21438 tree id = cp_parser_unqualified_id (parser,
21439 /*template_keyword_p=*/false,
21440 /*check_dependency_p=*/false,
21441 /*declarator_p=*/true,
21442 /*optional_p=*/false);
21443 if (is_overloaded_fn (id))
21444 id = DECL_NAME (get_first_fn (id));
21445 if (!constructor_name_p (id, nested_name_specifier))
21446 constructor_p = false;
21448 /* If we still think that this might be a constructor-declarator,
21449 look for a class-name. */
21450 else if (constructor_p)
21452 /* If we have:
21454 template <typename T> struct S {
21455 S();
21458 we must recognize that the nested `S' names a class. */
21459 tree type_decl;
21460 type_decl = cp_parser_class_name (parser,
21461 /*typename_keyword_p=*/false,
21462 /*template_keyword_p=*/false,
21463 none_type,
21464 /*check_dependency_p=*/false,
21465 /*class_head_p=*/false,
21466 /*is_declaration=*/false);
21467 /* If there was no class-name, then this is not a constructor. */
21468 constructor_p = !cp_parser_error_occurred (parser);
21470 /* If we're still considering a constructor, we have to see a `(',
21471 to begin the parameter-declaration-clause, followed by either a
21472 `)', an `...', or a decl-specifier. We need to check for a
21473 type-specifier to avoid being fooled into thinking that:
21475 S (f) (int);
21477 is a constructor. (It is actually a function named `f' that
21478 takes one parameter (of type `int') and returns a value of type
21479 `S'. */
21480 if (constructor_p
21481 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21482 constructor_p = false;
21484 if (constructor_p
21485 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
21486 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
21487 /* A parameter declaration begins with a decl-specifier,
21488 which is either the "attribute" keyword, a storage class
21489 specifier, or (usually) a type-specifier. */
21490 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
21492 tree type;
21493 tree pushed_scope = NULL_TREE;
21494 unsigned saved_num_template_parameter_lists;
21496 /* Names appearing in the type-specifier should be looked up
21497 in the scope of the class. */
21498 if (current_class_type)
21499 type = NULL_TREE;
21500 else
21502 type = TREE_TYPE (type_decl);
21503 if (TREE_CODE (type) == TYPENAME_TYPE)
21505 type = resolve_typename_type (type,
21506 /*only_current_p=*/false);
21507 if (TREE_CODE (type) == TYPENAME_TYPE)
21509 cp_parser_abort_tentative_parse (parser);
21510 return false;
21513 pushed_scope = push_scope (type);
21516 /* Inside the constructor parameter list, surrounding
21517 template-parameter-lists do not apply. */
21518 saved_num_template_parameter_lists
21519 = parser->num_template_parameter_lists;
21520 parser->num_template_parameter_lists = 0;
21522 /* Look for the type-specifier. */
21523 cp_parser_type_specifier (parser,
21524 CP_PARSER_FLAGS_NONE,
21525 /*decl_specs=*/NULL,
21526 /*is_declarator=*/true,
21527 /*declares_class_or_enum=*/NULL,
21528 /*is_cv_qualifier=*/NULL);
21530 parser->num_template_parameter_lists
21531 = saved_num_template_parameter_lists;
21533 /* Leave the scope of the class. */
21534 if (pushed_scope)
21535 pop_scope (pushed_scope);
21537 constructor_p = !cp_parser_error_occurred (parser);
21541 /* We did not really want to consume any tokens. */
21542 cp_parser_abort_tentative_parse (parser);
21544 return constructor_p;
21547 /* Parse the definition of the function given by the DECL_SPECIFIERS,
21548 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
21549 they must be performed once we are in the scope of the function.
21551 Returns the function defined. */
21553 static tree
21554 cp_parser_function_definition_from_specifiers_and_declarator
21555 (cp_parser* parser,
21556 cp_decl_specifier_seq *decl_specifiers,
21557 tree attributes,
21558 const cp_declarator *declarator)
21560 tree fn;
21561 bool success_p;
21563 /* Begin the function-definition. */
21564 success_p = start_function (decl_specifiers, declarator, attributes);
21566 /* The things we're about to see are not directly qualified by any
21567 template headers we've seen thus far. */
21568 reset_specialization ();
21570 /* If there were names looked up in the decl-specifier-seq that we
21571 did not check, check them now. We must wait until we are in the
21572 scope of the function to perform the checks, since the function
21573 might be a friend. */
21574 perform_deferred_access_checks (tf_warning_or_error);
21576 if (!success_p)
21578 /* Skip the entire function. */
21579 cp_parser_skip_to_end_of_block_or_statement (parser);
21580 fn = error_mark_node;
21582 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
21584 /* Seen already, skip it. An error message has already been output. */
21585 cp_parser_skip_to_end_of_block_or_statement (parser);
21586 fn = current_function_decl;
21587 current_function_decl = NULL_TREE;
21588 /* If this is a function from a class, pop the nested class. */
21589 if (current_class_name)
21590 pop_nested_class ();
21592 else
21594 timevar_id_t tv;
21595 if (DECL_DECLARED_INLINE_P (current_function_decl))
21596 tv = TV_PARSE_INLINE;
21597 else
21598 tv = TV_PARSE_FUNC;
21599 timevar_push (tv);
21600 fn = cp_parser_function_definition_after_declarator (parser,
21601 /*inline_p=*/false);
21602 timevar_pop (tv);
21605 return fn;
21608 /* Parse the part of a function-definition that follows the
21609 declarator. INLINE_P is TRUE iff this function is an inline
21610 function defined within a class-specifier.
21612 Returns the function defined. */
21614 static tree
21615 cp_parser_function_definition_after_declarator (cp_parser* parser,
21616 bool inline_p)
21618 tree fn;
21619 bool ctor_initializer_p = false;
21620 bool saved_in_unbraced_linkage_specification_p;
21621 bool saved_in_function_body;
21622 unsigned saved_num_template_parameter_lists;
21623 cp_token *token;
21625 saved_in_function_body = parser->in_function_body;
21626 parser->in_function_body = true;
21627 /* If the next token is `return', then the code may be trying to
21628 make use of the "named return value" extension that G++ used to
21629 support. */
21630 token = cp_lexer_peek_token (parser->lexer);
21631 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21633 /* Consume the `return' keyword. */
21634 cp_lexer_consume_token (parser->lexer);
21635 /* Look for the identifier that indicates what value is to be
21636 returned. */
21637 cp_parser_identifier (parser);
21638 /* Issue an error message. */
21639 error_at (token->location,
21640 "named return values are no longer supported");
21641 /* Skip tokens until we reach the start of the function body. */
21642 while (true)
21644 cp_token *token = cp_lexer_peek_token (parser->lexer);
21645 if (token->type == CPP_OPEN_BRACE
21646 || token->type == CPP_EOF
21647 || token->type == CPP_PRAGMA_EOL)
21648 break;
21649 cp_lexer_consume_token (parser->lexer);
21652 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21653 anything declared inside `f'. */
21654 saved_in_unbraced_linkage_specification_p
21655 = parser->in_unbraced_linkage_specification_p;
21656 parser->in_unbraced_linkage_specification_p = false;
21657 /* Inside the function, surrounding template-parameter-lists do not
21658 apply. */
21659 saved_num_template_parameter_lists
21660 = parser->num_template_parameter_lists;
21661 parser->num_template_parameter_lists = 0;
21663 start_lambda_scope (current_function_decl);
21665 /* If the next token is `try', `__transaction_atomic', or
21666 `__transaction_relaxed`, then we are looking at either function-try-block
21667 or function-transaction-block. Note that all of these include the
21668 function-body. */
21669 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21670 ctor_initializer_p = cp_parser_function_transaction (parser,
21671 RID_TRANSACTION_ATOMIC);
21672 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21673 RID_TRANSACTION_RELAXED))
21674 ctor_initializer_p = cp_parser_function_transaction (parser,
21675 RID_TRANSACTION_RELAXED);
21676 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21677 ctor_initializer_p = cp_parser_function_try_block (parser);
21678 else
21679 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21680 (parser, /*in_function_try_block=*/false);
21682 finish_lambda_scope ();
21684 /* Finish the function. */
21685 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21686 (inline_p ? 2 : 0));
21687 /* Generate code for it, if necessary. */
21688 expand_or_defer_fn (fn);
21689 /* Restore the saved values. */
21690 parser->in_unbraced_linkage_specification_p
21691 = saved_in_unbraced_linkage_specification_p;
21692 parser->num_template_parameter_lists
21693 = saved_num_template_parameter_lists;
21694 parser->in_function_body = saved_in_function_body;
21696 return fn;
21699 /* Parse a template-declaration, assuming that the `export' (and
21700 `extern') keywords, if present, has already been scanned. MEMBER_P
21701 is as for cp_parser_template_declaration. */
21703 static void
21704 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21706 tree decl = NULL_TREE;
21707 vec<deferred_access_check, va_gc> *checks;
21708 tree parameter_list;
21709 bool friend_p = false;
21710 bool need_lang_pop;
21711 cp_token *token;
21713 /* Look for the `template' keyword. */
21714 token = cp_lexer_peek_token (parser->lexer);
21715 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21716 return;
21718 /* And the `<'. */
21719 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21720 return;
21721 if (at_class_scope_p () && current_function_decl)
21723 /* 14.5.2.2 [temp.mem]
21725 A local class shall not have member templates. */
21726 error_at (token->location,
21727 "invalid declaration of member template in local class");
21728 cp_parser_skip_to_end_of_block_or_statement (parser);
21729 return;
21731 /* [temp]
21733 A template ... shall not have C linkage. */
21734 if (current_lang_name == lang_name_c)
21736 error_at (token->location, "template with C linkage");
21737 /* Give it C++ linkage to avoid confusing other parts of the
21738 front end. */
21739 push_lang_context (lang_name_cplusplus);
21740 need_lang_pop = true;
21742 else
21743 need_lang_pop = false;
21745 /* We cannot perform access checks on the template parameter
21746 declarations until we know what is being declared, just as we
21747 cannot check the decl-specifier list. */
21748 push_deferring_access_checks (dk_deferred);
21750 /* If the next token is `>', then we have an invalid
21751 specialization. Rather than complain about an invalid template
21752 parameter, issue an error message here. */
21753 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21755 cp_parser_error (parser, "invalid explicit specialization");
21756 begin_specialization ();
21757 parameter_list = NULL_TREE;
21759 else
21761 /* Parse the template parameters. */
21762 parameter_list = cp_parser_template_parameter_list (parser);
21765 /* Get the deferred access checks from the parameter list. These
21766 will be checked once we know what is being declared, as for a
21767 member template the checks must be performed in the scope of the
21768 class containing the member. */
21769 checks = get_deferred_access_checks ();
21771 /* Look for the `>'. */
21772 cp_parser_skip_to_end_of_template_parameter_list (parser);
21773 /* We just processed one more parameter list. */
21774 ++parser->num_template_parameter_lists;
21775 /* If the next token is `template', there are more template
21776 parameters. */
21777 if (cp_lexer_next_token_is_keyword (parser->lexer,
21778 RID_TEMPLATE))
21779 cp_parser_template_declaration_after_export (parser, member_p);
21780 else if (cxx_dialect >= cxx0x
21781 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21782 decl = cp_parser_alias_declaration (parser);
21783 else
21785 /* There are no access checks when parsing a template, as we do not
21786 know if a specialization will be a friend. */
21787 push_deferring_access_checks (dk_no_check);
21788 token = cp_lexer_peek_token (parser->lexer);
21789 decl = cp_parser_single_declaration (parser,
21790 checks,
21791 member_p,
21792 /*explicit_specialization_p=*/false,
21793 &friend_p);
21794 pop_deferring_access_checks ();
21796 /* If this is a member template declaration, let the front
21797 end know. */
21798 if (member_p && !friend_p && decl)
21800 if (TREE_CODE (decl) == TYPE_DECL)
21801 cp_parser_check_access_in_redeclaration (decl, token->location);
21803 decl = finish_member_template_decl (decl);
21805 else if (friend_p && decl
21806 && (TREE_CODE (decl) == TYPE_DECL
21807 || DECL_TYPE_TEMPLATE_P (decl)))
21808 make_friend_class (current_class_type, TREE_TYPE (decl),
21809 /*complain=*/true);
21811 /* We are done with the current parameter list. */
21812 --parser->num_template_parameter_lists;
21814 pop_deferring_access_checks ();
21816 /* Finish up. */
21817 finish_template_decl (parameter_list);
21819 /* Check the template arguments for a literal operator template. */
21820 if (decl
21821 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21822 && UDLIT_OPER_P (DECL_NAME (decl)))
21824 bool ok = true;
21825 if (parameter_list == NULL_TREE)
21826 ok = false;
21827 else
21829 int num_parms = TREE_VEC_LENGTH (parameter_list);
21830 if (num_parms != 1)
21831 ok = false;
21832 else
21834 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21835 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21836 if (TREE_TYPE (parm) != char_type_node
21837 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21838 ok = false;
21841 if (!ok)
21842 error ("literal operator template %qD has invalid parameter list."
21843 " Expected non-type template argument pack <char...>",
21844 decl);
21846 /* Register member declarations. */
21847 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21848 finish_member_declaration (decl);
21849 /* For the erroneous case of a template with C linkage, we pushed an
21850 implicit C++ linkage scope; exit that scope now. */
21851 if (need_lang_pop)
21852 pop_lang_context ();
21853 /* If DECL is a function template, we must return to parse it later.
21854 (Even though there is no definition, there might be default
21855 arguments that need handling.) */
21856 if (member_p && decl
21857 && (TREE_CODE (decl) == FUNCTION_DECL
21858 || DECL_FUNCTION_TEMPLATE_P (decl)))
21859 vec_safe_push (unparsed_funs_with_definitions, decl);
21862 /* Perform the deferred access checks from a template-parameter-list.
21863 CHECKS is a TREE_LIST of access checks, as returned by
21864 get_deferred_access_checks. */
21866 static void
21867 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
21869 ++processing_template_parmlist;
21870 perform_access_checks (checks, tf_warning_or_error);
21871 --processing_template_parmlist;
21874 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21875 `function-definition' sequence that follows a template header.
21876 If MEMBER_P is true, this declaration appears in a class scope.
21878 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21879 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21881 static tree
21882 cp_parser_single_declaration (cp_parser* parser,
21883 vec<deferred_access_check, va_gc> *checks,
21884 bool member_p,
21885 bool explicit_specialization_p,
21886 bool* friend_p)
21888 int declares_class_or_enum;
21889 tree decl = NULL_TREE;
21890 cp_decl_specifier_seq decl_specifiers;
21891 bool function_definition_p = false;
21892 cp_token *decl_spec_token_start;
21894 /* This function is only used when processing a template
21895 declaration. */
21896 gcc_assert (innermost_scope_kind () == sk_template_parms
21897 || innermost_scope_kind () == sk_template_spec);
21899 /* Defer access checks until we know what is being declared. */
21900 push_deferring_access_checks (dk_deferred);
21902 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21903 alternative. */
21904 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21905 cp_parser_decl_specifier_seq (parser,
21906 CP_PARSER_FLAGS_OPTIONAL,
21907 &decl_specifiers,
21908 &declares_class_or_enum);
21909 if (friend_p)
21910 *friend_p = cp_parser_friend_p (&decl_specifiers);
21912 /* There are no template typedefs. */
21913 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
21915 error_at (decl_spec_token_start->location,
21916 "template declaration of %<typedef%>");
21917 decl = error_mark_node;
21920 /* Gather up the access checks that occurred the
21921 decl-specifier-seq. */
21922 stop_deferring_access_checks ();
21924 /* Check for the declaration of a template class. */
21925 if (declares_class_or_enum)
21927 if (cp_parser_declares_only_class_p (parser))
21929 decl = shadow_tag (&decl_specifiers);
21931 /* In this case:
21933 struct C {
21934 friend template <typename T> struct A<T>::B;
21937 A<T>::B will be represented by a TYPENAME_TYPE, and
21938 therefore not recognized by shadow_tag. */
21939 if (friend_p && *friend_p
21940 && !decl
21941 && decl_specifiers.type
21942 && TYPE_P (decl_specifiers.type))
21943 decl = decl_specifiers.type;
21945 if (decl && decl != error_mark_node)
21946 decl = TYPE_NAME (decl);
21947 else
21948 decl = error_mark_node;
21950 /* Perform access checks for template parameters. */
21951 cp_parser_perform_template_parameter_access_checks (checks);
21955 /* Complain about missing 'typename' or other invalid type names. */
21956 if (!decl_specifiers.any_type_specifiers_p
21957 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21959 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21960 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21961 the rest of this declaration. */
21962 decl = error_mark_node;
21963 goto out;
21966 /* If it's not a template class, try for a template function. If
21967 the next token is a `;', then this declaration does not declare
21968 anything. But, if there were errors in the decl-specifiers, then
21969 the error might well have come from an attempted class-specifier.
21970 In that case, there's no need to warn about a missing declarator. */
21971 if (!decl
21972 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21973 || decl_specifiers.type != error_mark_node))
21975 decl = cp_parser_init_declarator (parser,
21976 &decl_specifiers,
21977 checks,
21978 /*function_definition_allowed_p=*/true,
21979 member_p,
21980 declares_class_or_enum,
21981 &function_definition_p,
21982 NULL);
21984 /* 7.1.1-1 [dcl.stc]
21986 A storage-class-specifier shall not be specified in an explicit
21987 specialization... */
21988 if (decl
21989 && explicit_specialization_p
21990 && decl_specifiers.storage_class != sc_none)
21992 error_at (decl_spec_token_start->location,
21993 "explicit template specialization cannot have a storage class");
21994 decl = error_mark_node;
21997 if (decl && TREE_CODE (decl) == VAR_DECL)
21998 check_template_variable (decl);
22001 /* Look for a trailing `;' after the declaration. */
22002 if (!function_definition_p
22003 && (decl == error_mark_node
22004 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
22005 cp_parser_skip_to_end_of_block_or_statement (parser);
22007 out:
22008 pop_deferring_access_checks ();
22010 /* Clear any current qualification; whatever comes next is the start
22011 of something new. */
22012 parser->scope = NULL_TREE;
22013 parser->qualifying_scope = NULL_TREE;
22014 parser->object_scope = NULL_TREE;
22016 return decl;
22019 /* Parse a cast-expression that is not the operand of a unary "&". */
22021 static tree
22022 cp_parser_simple_cast_expression (cp_parser *parser)
22024 return cp_parser_cast_expression (parser, /*address_p=*/false,
22025 /*cast_p=*/false, NULL);
22028 /* Parse a functional cast to TYPE. Returns an expression
22029 representing the cast. */
22031 static tree
22032 cp_parser_functional_cast (cp_parser* parser, tree type)
22034 vec<tree, va_gc> *vec;
22035 tree expression_list;
22036 tree cast;
22037 bool nonconst_p;
22039 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22041 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22042 expression_list = cp_parser_braced_list (parser, &nonconst_p);
22043 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
22044 if (TREE_CODE (type) == TYPE_DECL)
22045 type = TREE_TYPE (type);
22046 return finish_compound_literal (type, expression_list,
22047 tf_warning_or_error);
22051 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22052 /*cast_p=*/true,
22053 /*allow_expansion_p=*/true,
22054 /*non_constant_p=*/NULL);
22055 if (vec == NULL)
22056 expression_list = error_mark_node;
22057 else
22059 expression_list = build_tree_list_vec (vec);
22060 release_tree_vector (vec);
22063 cast = build_functional_cast (type, expression_list,
22064 tf_warning_or_error);
22065 /* [expr.const]/1: In an integral constant expression "only type
22066 conversions to integral or enumeration type can be used". */
22067 if (TREE_CODE (type) == TYPE_DECL)
22068 type = TREE_TYPE (type);
22069 if (cast != error_mark_node
22070 && !cast_valid_in_integral_constant_expression_p (type)
22071 && cp_parser_non_integral_constant_expression (parser,
22072 NIC_CONSTRUCTOR))
22073 return error_mark_node;
22074 return cast;
22077 /* Save the tokens that make up the body of a member function defined
22078 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
22079 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
22080 specifiers applied to the declaration. Returns the FUNCTION_DECL
22081 for the member function. */
22083 static tree
22084 cp_parser_save_member_function_body (cp_parser* parser,
22085 cp_decl_specifier_seq *decl_specifiers,
22086 cp_declarator *declarator,
22087 tree attributes)
22089 cp_token *first;
22090 cp_token *last;
22091 tree fn;
22093 /* Create the FUNCTION_DECL. */
22094 fn = grokmethod (decl_specifiers, declarator, attributes);
22095 /* If something went badly wrong, bail out now. */
22096 if (fn == error_mark_node)
22098 /* If there's a function-body, skip it. */
22099 if (cp_parser_token_starts_function_definition_p
22100 (cp_lexer_peek_token (parser->lexer)))
22101 cp_parser_skip_to_end_of_block_or_statement (parser);
22102 return error_mark_node;
22105 /* Remember it, if there default args to post process. */
22106 cp_parser_save_default_args (parser, fn);
22108 /* Save away the tokens that make up the body of the
22109 function. */
22110 first = parser->lexer->next_token;
22111 /* We can have braced-init-list mem-initializers before the fn body. */
22112 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22114 cp_lexer_consume_token (parser->lexer);
22115 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22116 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
22118 /* cache_group will stop after an un-nested { } pair, too. */
22119 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
22120 break;
22122 /* variadic mem-inits have ... after the ')'. */
22123 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22124 cp_lexer_consume_token (parser->lexer);
22127 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22128 /* Handle function try blocks. */
22129 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
22130 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22131 last = parser->lexer->next_token;
22133 /* Save away the inline definition; we will process it when the
22134 class is complete. */
22135 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
22136 DECL_PENDING_INLINE_P (fn) = 1;
22138 /* We need to know that this was defined in the class, so that
22139 friend templates are handled correctly. */
22140 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
22142 /* Add FN to the queue of functions to be parsed later. */
22143 vec_safe_push (unparsed_funs_with_definitions, fn);
22145 return fn;
22148 /* Save the tokens that make up the in-class initializer for a non-static
22149 data member. Returns a DEFAULT_ARG. */
22151 static tree
22152 cp_parser_save_nsdmi (cp_parser* parser)
22154 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
22157 /* Parse a template-argument-list, as well as the trailing ">" (but
22158 not the opening "<"). See cp_parser_template_argument_list for the
22159 return value. */
22161 static tree
22162 cp_parser_enclosed_template_argument_list (cp_parser* parser)
22164 tree arguments;
22165 tree saved_scope;
22166 tree saved_qualifying_scope;
22167 tree saved_object_scope;
22168 bool saved_greater_than_is_operator_p;
22169 int saved_unevaluated_operand;
22170 int saved_inhibit_evaluation_warnings;
22172 /* [temp.names]
22174 When parsing a template-id, the first non-nested `>' is taken as
22175 the end of the template-argument-list rather than a greater-than
22176 operator. */
22177 saved_greater_than_is_operator_p
22178 = parser->greater_than_is_operator_p;
22179 parser->greater_than_is_operator_p = false;
22180 /* Parsing the argument list may modify SCOPE, so we save it
22181 here. */
22182 saved_scope = parser->scope;
22183 saved_qualifying_scope = parser->qualifying_scope;
22184 saved_object_scope = parser->object_scope;
22185 /* We need to evaluate the template arguments, even though this
22186 template-id may be nested within a "sizeof". */
22187 saved_unevaluated_operand = cp_unevaluated_operand;
22188 cp_unevaluated_operand = 0;
22189 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22190 c_inhibit_evaluation_warnings = 0;
22191 /* Parse the template-argument-list itself. */
22192 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
22193 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22194 arguments = NULL_TREE;
22195 else
22196 arguments = cp_parser_template_argument_list (parser);
22197 /* Look for the `>' that ends the template-argument-list. If we find
22198 a '>>' instead, it's probably just a typo. */
22199 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22201 if (cxx_dialect != cxx98)
22203 /* In C++0x, a `>>' in a template argument list or cast
22204 expression is considered to be two separate `>'
22205 tokens. So, change the current token to a `>', but don't
22206 consume it: it will be consumed later when the outer
22207 template argument list (or cast expression) is parsed.
22208 Note that this replacement of `>' for `>>' is necessary
22209 even if we are parsing tentatively: in the tentative
22210 case, after calling
22211 cp_parser_enclosed_template_argument_list we will always
22212 throw away all of the template arguments and the first
22213 closing `>', either because the template argument list
22214 was erroneous or because we are replacing those tokens
22215 with a CPP_TEMPLATE_ID token. The second `>' (which will
22216 not have been thrown away) is needed either to close an
22217 outer template argument list or to complete a new-style
22218 cast. */
22219 cp_token *token = cp_lexer_peek_token (parser->lexer);
22220 token->type = CPP_GREATER;
22222 else if (!saved_greater_than_is_operator_p)
22224 /* If we're in a nested template argument list, the '>>' has
22225 to be a typo for '> >'. We emit the error message, but we
22226 continue parsing and we push a '>' as next token, so that
22227 the argument list will be parsed correctly. Note that the
22228 global source location is still on the token before the
22229 '>>', so we need to say explicitly where we want it. */
22230 cp_token *token = cp_lexer_peek_token (parser->lexer);
22231 error_at (token->location, "%<>>%> should be %<> >%> "
22232 "within a nested template argument list");
22234 token->type = CPP_GREATER;
22236 else
22238 /* If this is not a nested template argument list, the '>>'
22239 is a typo for '>'. Emit an error message and continue.
22240 Same deal about the token location, but here we can get it
22241 right by consuming the '>>' before issuing the diagnostic. */
22242 cp_token *token = cp_lexer_consume_token (parser->lexer);
22243 error_at (token->location,
22244 "spurious %<>>%>, use %<>%> to terminate "
22245 "a template argument list");
22248 else
22249 cp_parser_skip_to_end_of_template_parameter_list (parser);
22250 /* The `>' token might be a greater-than operator again now. */
22251 parser->greater_than_is_operator_p
22252 = saved_greater_than_is_operator_p;
22253 /* Restore the SAVED_SCOPE. */
22254 parser->scope = saved_scope;
22255 parser->qualifying_scope = saved_qualifying_scope;
22256 parser->object_scope = saved_object_scope;
22257 cp_unevaluated_operand = saved_unevaluated_operand;
22258 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22260 return arguments;
22263 /* MEMBER_FUNCTION is a member function, or a friend. If default
22264 arguments, or the body of the function have not yet been parsed,
22265 parse them now. */
22267 static void
22268 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
22270 timevar_push (TV_PARSE_INMETH);
22271 /* If this member is a template, get the underlying
22272 FUNCTION_DECL. */
22273 if (DECL_FUNCTION_TEMPLATE_P (member_function))
22274 member_function = DECL_TEMPLATE_RESULT (member_function);
22276 /* There should not be any class definitions in progress at this
22277 point; the bodies of members are only parsed outside of all class
22278 definitions. */
22279 gcc_assert (parser->num_classes_being_defined == 0);
22280 /* While we're parsing the member functions we might encounter more
22281 classes. We want to handle them right away, but we don't want
22282 them getting mixed up with functions that are currently in the
22283 queue. */
22284 push_unparsed_function_queues (parser);
22286 /* Make sure that any template parameters are in scope. */
22287 maybe_begin_member_template_processing (member_function);
22289 /* If the body of the function has not yet been parsed, parse it
22290 now. */
22291 if (DECL_PENDING_INLINE_P (member_function))
22293 tree function_scope;
22294 cp_token_cache *tokens;
22296 /* The function is no longer pending; we are processing it. */
22297 tokens = DECL_PENDING_INLINE_INFO (member_function);
22298 DECL_PENDING_INLINE_INFO (member_function) = NULL;
22299 DECL_PENDING_INLINE_P (member_function) = 0;
22301 /* If this is a local class, enter the scope of the containing
22302 function. */
22303 function_scope = current_function_decl;
22304 if (function_scope)
22305 push_function_context ();
22307 /* Push the body of the function onto the lexer stack. */
22308 cp_parser_push_lexer_for_tokens (parser, tokens);
22310 /* Let the front end know that we going to be defining this
22311 function. */
22312 start_preparsed_function (member_function, NULL_TREE,
22313 SF_PRE_PARSED | SF_INCLASS_INLINE);
22315 /* Don't do access checking if it is a templated function. */
22316 if (processing_template_decl)
22317 push_deferring_access_checks (dk_no_check);
22319 /* Now, parse the body of the function. */
22320 cp_parser_function_definition_after_declarator (parser,
22321 /*inline_p=*/true);
22323 if (processing_template_decl)
22324 pop_deferring_access_checks ();
22326 /* Leave the scope of the containing function. */
22327 if (function_scope)
22328 pop_function_context ();
22329 cp_parser_pop_lexer (parser);
22332 /* Remove any template parameters from the symbol table. */
22333 maybe_end_member_template_processing ();
22335 /* Restore the queue. */
22336 pop_unparsed_function_queues (parser);
22337 timevar_pop (TV_PARSE_INMETH);
22340 /* If DECL contains any default args, remember it on the unparsed
22341 functions queue. */
22343 static void
22344 cp_parser_save_default_args (cp_parser* parser, tree decl)
22346 tree probe;
22348 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
22349 probe;
22350 probe = TREE_CHAIN (probe))
22351 if (TREE_PURPOSE (probe))
22353 cp_default_arg_entry entry = {current_class_type, decl};
22354 vec_safe_push (unparsed_funs_with_default_args, entry);
22355 break;
22359 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
22360 which is either a FIELD_DECL or PARM_DECL. Parse it and return
22361 the result. For a PARM_DECL, PARMTYPE is the corresponding type
22362 from the parameter-type-list. */
22364 static tree
22365 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
22366 tree default_arg, tree parmtype)
22368 cp_token_cache *tokens;
22369 tree parsed_arg;
22370 bool dummy;
22372 if (default_arg == error_mark_node)
22373 return error_mark_node;
22375 /* Push the saved tokens for the default argument onto the parser's
22376 lexer stack. */
22377 tokens = DEFARG_TOKENS (default_arg);
22378 cp_parser_push_lexer_for_tokens (parser, tokens);
22380 start_lambda_scope (decl);
22382 /* Parse the default argument. */
22383 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
22384 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
22385 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22387 finish_lambda_scope ();
22389 if (parsed_arg == error_mark_node)
22390 cp_parser_skip_to_end_of_statement (parser);
22392 if (!processing_template_decl)
22394 /* In a non-template class, check conversions now. In a template,
22395 we'll wait and instantiate these as needed. */
22396 if (TREE_CODE (decl) == PARM_DECL)
22397 parsed_arg = check_default_argument (parmtype, parsed_arg);
22398 else
22400 int flags = LOOKUP_IMPLICIT;
22401 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
22402 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
22403 flags = LOOKUP_NORMAL;
22404 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
22408 /* If the token stream has not been completely used up, then
22409 there was extra junk after the end of the default
22410 argument. */
22411 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22413 if (TREE_CODE (decl) == PARM_DECL)
22414 cp_parser_error (parser, "expected %<,%>");
22415 else
22416 cp_parser_error (parser, "expected %<;%>");
22419 /* Revert to the main lexer. */
22420 cp_parser_pop_lexer (parser);
22422 return parsed_arg;
22425 /* FIELD is a non-static data member with an initializer which we saved for
22426 later; parse it now. */
22428 static void
22429 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
22431 tree def;
22433 push_unparsed_function_queues (parser);
22434 def = cp_parser_late_parse_one_default_arg (parser, field,
22435 DECL_INITIAL (field),
22436 NULL_TREE);
22437 pop_unparsed_function_queues (parser);
22439 DECL_INITIAL (field) = def;
22442 /* FN is a FUNCTION_DECL which may contains a parameter with an
22443 unparsed DEFAULT_ARG. Parse the default args now. This function
22444 assumes that the current scope is the scope in which the default
22445 argument should be processed. */
22447 static void
22448 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
22450 bool saved_local_variables_forbidden_p;
22451 tree parm, parmdecl;
22453 /* While we're parsing the default args, we might (due to the
22454 statement expression extension) encounter more classes. We want
22455 to handle them right away, but we don't want them getting mixed
22456 up with default args that are currently in the queue. */
22457 push_unparsed_function_queues (parser);
22459 /* Local variable names (and the `this' keyword) may not appear
22460 in a default argument. */
22461 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22462 parser->local_variables_forbidden_p = true;
22464 push_defarg_context (fn);
22466 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
22467 parmdecl = DECL_ARGUMENTS (fn);
22468 parm && parm != void_list_node;
22469 parm = TREE_CHAIN (parm),
22470 parmdecl = DECL_CHAIN (parmdecl))
22472 tree default_arg = TREE_PURPOSE (parm);
22473 tree parsed_arg;
22474 vec<tree, va_gc> *insts;
22475 tree copy;
22476 unsigned ix;
22478 if (!default_arg)
22479 continue;
22481 if (TREE_CODE (default_arg) != DEFAULT_ARG)
22482 /* This can happen for a friend declaration for a function
22483 already declared with default arguments. */
22484 continue;
22486 parsed_arg
22487 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
22488 default_arg,
22489 TREE_VALUE (parm));
22490 if (parsed_arg == error_mark_node)
22492 continue;
22495 TREE_PURPOSE (parm) = parsed_arg;
22497 /* Update any instantiations we've already created. */
22498 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
22499 vec_safe_iterate (insts, ix, &copy); ix++)
22500 TREE_PURPOSE (copy) = parsed_arg;
22503 pop_defarg_context ();
22505 /* Make sure no default arg is missing. */
22506 check_default_args (fn);
22508 /* Restore the state of local_variables_forbidden_p. */
22509 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22511 /* Restore the queue. */
22512 pop_unparsed_function_queues (parser);
22515 /* Parse the operand of `sizeof' (or a similar operator). Returns
22516 either a TYPE or an expression, depending on the form of the
22517 input. The KEYWORD indicates which kind of expression we have
22518 encountered. */
22520 static tree
22521 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
22523 tree expr = NULL_TREE;
22524 const char *saved_message;
22525 char *tmp;
22526 bool saved_integral_constant_expression_p;
22527 bool saved_non_integral_constant_expression_p;
22528 bool pack_expansion_p = false;
22530 /* Types cannot be defined in a `sizeof' expression. Save away the
22531 old message. */
22532 saved_message = parser->type_definition_forbidden_message;
22533 /* And create the new one. */
22534 tmp = concat ("types may not be defined in %<",
22535 IDENTIFIER_POINTER (ridpointers[keyword]),
22536 "%> expressions", NULL);
22537 parser->type_definition_forbidden_message = tmp;
22539 /* The restrictions on constant-expressions do not apply inside
22540 sizeof expressions. */
22541 saved_integral_constant_expression_p
22542 = parser->integral_constant_expression_p;
22543 saved_non_integral_constant_expression_p
22544 = parser->non_integral_constant_expression_p;
22545 parser->integral_constant_expression_p = false;
22547 /* If it's a `...', then we are computing the length of a parameter
22548 pack. */
22549 if (keyword == RID_SIZEOF
22550 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22552 /* Consume the `...'. */
22553 cp_lexer_consume_token (parser->lexer);
22554 maybe_warn_variadic_templates ();
22556 /* Note that this is an expansion. */
22557 pack_expansion_p = true;
22560 /* Do not actually evaluate the expression. */
22561 ++cp_unevaluated_operand;
22562 ++c_inhibit_evaluation_warnings;
22563 /* If it's a `(', then we might be looking at the type-id
22564 construction. */
22565 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22567 tree type;
22568 bool saved_in_type_id_in_expr_p;
22570 /* We can't be sure yet whether we're looking at a type-id or an
22571 expression. */
22572 cp_parser_parse_tentatively (parser);
22573 /* Consume the `('. */
22574 cp_lexer_consume_token (parser->lexer);
22575 /* Parse the type-id. */
22576 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
22577 parser->in_type_id_in_expr_p = true;
22578 type = cp_parser_type_id (parser);
22579 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
22580 /* Now, look for the trailing `)'. */
22581 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22582 /* If all went well, then we're done. */
22583 if (cp_parser_parse_definitely (parser))
22585 cp_decl_specifier_seq decl_specs;
22587 /* Build a trivial decl-specifier-seq. */
22588 clear_decl_specs (&decl_specs);
22589 decl_specs.type = type;
22591 /* Call grokdeclarator to figure out what type this is. */
22592 expr = grokdeclarator (NULL,
22593 &decl_specs,
22594 TYPENAME,
22595 /*initialized=*/0,
22596 /*attrlist=*/NULL);
22599 else if (pack_expansion_p)
22600 permerror (cp_lexer_peek_token (parser->lexer)->location,
22601 "%<sizeof...%> argument must be surrounded by parentheses");
22603 /* If the type-id production did not work out, then we must be
22604 looking at the unary-expression production. */
22605 if (!expr)
22606 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
22607 /*cast_p=*/false, NULL);
22609 if (pack_expansion_p)
22610 /* Build a pack expansion. */
22611 expr = make_pack_expansion (expr);
22613 /* Go back to evaluating expressions. */
22614 --cp_unevaluated_operand;
22615 --c_inhibit_evaluation_warnings;
22617 /* Free the message we created. */
22618 free (tmp);
22619 /* And restore the old one. */
22620 parser->type_definition_forbidden_message = saved_message;
22621 parser->integral_constant_expression_p
22622 = saved_integral_constant_expression_p;
22623 parser->non_integral_constant_expression_p
22624 = saved_non_integral_constant_expression_p;
22626 return expr;
22629 /* If the current declaration has no declarator, return true. */
22631 static bool
22632 cp_parser_declares_only_class_p (cp_parser *parser)
22634 /* If the next token is a `;' or a `,' then there is no
22635 declarator. */
22636 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22637 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22640 /* Update the DECL_SPECS to reflect the storage class indicated by
22641 KEYWORD. */
22643 static void
22644 cp_parser_set_storage_class (cp_parser *parser,
22645 cp_decl_specifier_seq *decl_specs,
22646 enum rid keyword,
22647 cp_token *token)
22649 cp_storage_class storage_class;
22651 if (parser->in_unbraced_linkage_specification_p)
22653 error_at (token->location, "invalid use of %qD in linkage specification",
22654 ridpointers[keyword]);
22655 return;
22657 else if (decl_specs->storage_class != sc_none)
22659 decl_specs->conflicting_specifiers_p = true;
22660 return;
22663 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22664 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
22665 && decl_specs->gnu_thread_keyword_p)
22667 pedwarn (decl_specs->locations[ds_thread], 0,
22668 "%<__thread%> before %qD", ridpointers[keyword]);
22671 switch (keyword)
22673 case RID_AUTO:
22674 storage_class = sc_auto;
22675 break;
22676 case RID_REGISTER:
22677 storage_class = sc_register;
22678 break;
22679 case RID_STATIC:
22680 storage_class = sc_static;
22681 break;
22682 case RID_EXTERN:
22683 storage_class = sc_extern;
22684 break;
22685 case RID_MUTABLE:
22686 storage_class = sc_mutable;
22687 break;
22688 default:
22689 gcc_unreachable ();
22691 decl_specs->storage_class = storage_class;
22692 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
22694 /* A storage class specifier cannot be applied alongside a typedef
22695 specifier. If there is a typedef specifier present then set
22696 conflicting_specifiers_p which will trigger an error later
22697 on in grokdeclarator. */
22698 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
22699 decl_specs->conflicting_specifiers_p = true;
22702 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22703 is true, the type is a class or enum definition. */
22705 static void
22706 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22707 tree type_spec,
22708 cp_token *token,
22709 bool type_definition_p)
22711 decl_specs->any_specifiers_p = true;
22713 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22714 (with, for example, in "typedef int wchar_t;") we remember that
22715 this is what happened. In system headers, we ignore these
22716 declarations so that G++ can work with system headers that are not
22717 C++-safe. */
22718 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
22719 && !type_definition_p
22720 && (type_spec == boolean_type_node
22721 || type_spec == char16_type_node
22722 || type_spec == char32_type_node
22723 || type_spec == wchar_type_node)
22724 && (decl_specs->type
22725 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
22726 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
22727 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
22728 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
22730 decl_specs->redefined_builtin_type = type_spec;
22731 set_and_check_decl_spec_loc (decl_specs,
22732 ds_redefined_builtin_type_spec,
22733 token);
22734 if (!decl_specs->type)
22736 decl_specs->type = type_spec;
22737 decl_specs->type_definition_p = false;
22738 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
22741 else if (decl_specs->type)
22742 decl_specs->multiple_types_p = true;
22743 else
22745 decl_specs->type = type_spec;
22746 decl_specs->type_definition_p = type_definition_p;
22747 decl_specs->redefined_builtin_type = NULL_TREE;
22748 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
22752 /* True iff TOKEN is the GNU keyword __thread. */
22754 static bool
22755 token_is__thread (cp_token *token)
22757 gcc_assert (token->keyword == RID_THREAD);
22758 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
22761 /* Set the location for a declarator specifier and check if it is
22762 duplicated.
22764 DECL_SPECS is the sequence of declarator specifiers onto which to
22765 set the location.
22767 DS is the single declarator specifier to set which location is to
22768 be set onto the existing sequence of declarators.
22770 LOCATION is the location for the declarator specifier to
22771 consider. */
22773 static void
22774 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
22775 cp_decl_spec ds, cp_token *token)
22777 gcc_assert (ds < ds_last);
22779 if (decl_specs == NULL)
22780 return;
22782 source_location location = token->location;
22784 if (decl_specs->locations[ds] == 0)
22786 decl_specs->locations[ds] = location;
22787 if (ds == ds_thread)
22788 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
22790 else
22792 if (ds == ds_long)
22794 if (decl_specs->locations[ds_long_long] != 0)
22795 error_at (location,
22796 "%<long long long%> is too long for GCC");
22797 else
22799 decl_specs->locations[ds_long_long] = location;
22800 pedwarn_cxx98 (location,
22801 OPT_Wlong_long,
22802 "ISO C++ 1998 does not support %<long long%>");
22805 else if (ds == ds_thread)
22807 bool gnu = token_is__thread (token);
22808 if (gnu != decl_specs->gnu_thread_keyword_p)
22809 error_at (location,
22810 "both %<__thread%> and %<thread_local%> specified");
22811 else
22812 error_at (location, "duplicate %qD", token->u.value);
22814 else
22816 static const char *const decl_spec_names[] = {
22817 "signed",
22818 "unsigned",
22819 "short",
22820 "long",
22821 "const",
22822 "volatile",
22823 "restrict",
22824 "inline",
22825 "virtual",
22826 "explicit",
22827 "friend",
22828 "typedef",
22829 "using",
22830 "constexpr",
22831 "__complex"
22833 error_at (location,
22834 "duplicate %qs", decl_spec_names[ds]);
22839 /* Return true iff the declarator specifier DS is present in the
22840 sequence of declarator specifiers DECL_SPECS. */
22842 bool
22843 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
22844 cp_decl_spec ds)
22846 gcc_assert (ds < ds_last);
22848 if (decl_specs == NULL)
22849 return false;
22851 return decl_specs->locations[ds] != 0;
22854 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22855 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22857 static bool
22858 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22860 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
22863 /* Issue an error message indicating that TOKEN_DESC was expected.
22864 If KEYWORD is true, it indicated this function is called by
22865 cp_parser_require_keword and the required token can only be
22866 a indicated keyword. */
22868 static void
22869 cp_parser_required_error (cp_parser *parser,
22870 required_token token_desc,
22871 bool keyword)
22873 switch (token_desc)
22875 case RT_NEW:
22876 cp_parser_error (parser, "expected %<new%>");
22877 return;
22878 case RT_DELETE:
22879 cp_parser_error (parser, "expected %<delete%>");
22880 return;
22881 case RT_RETURN:
22882 cp_parser_error (parser, "expected %<return%>");
22883 return;
22884 case RT_WHILE:
22885 cp_parser_error (parser, "expected %<while%>");
22886 return;
22887 case RT_EXTERN:
22888 cp_parser_error (parser, "expected %<extern%>");
22889 return;
22890 case RT_STATIC_ASSERT:
22891 cp_parser_error (parser, "expected %<static_assert%>");
22892 return;
22893 case RT_DECLTYPE:
22894 cp_parser_error (parser, "expected %<decltype%>");
22895 return;
22896 case RT_OPERATOR:
22897 cp_parser_error (parser, "expected %<operator%>");
22898 return;
22899 case RT_CLASS:
22900 cp_parser_error (parser, "expected %<class%>");
22901 return;
22902 case RT_TEMPLATE:
22903 cp_parser_error (parser, "expected %<template%>");
22904 return;
22905 case RT_NAMESPACE:
22906 cp_parser_error (parser, "expected %<namespace%>");
22907 return;
22908 case RT_USING:
22909 cp_parser_error (parser, "expected %<using%>");
22910 return;
22911 case RT_ASM:
22912 cp_parser_error (parser, "expected %<asm%>");
22913 return;
22914 case RT_TRY:
22915 cp_parser_error (parser, "expected %<try%>");
22916 return;
22917 case RT_CATCH:
22918 cp_parser_error (parser, "expected %<catch%>");
22919 return;
22920 case RT_THROW:
22921 cp_parser_error (parser, "expected %<throw%>");
22922 return;
22923 case RT_LABEL:
22924 cp_parser_error (parser, "expected %<__label__%>");
22925 return;
22926 case RT_AT_TRY:
22927 cp_parser_error (parser, "expected %<@try%>");
22928 return;
22929 case RT_AT_SYNCHRONIZED:
22930 cp_parser_error (parser, "expected %<@synchronized%>");
22931 return;
22932 case RT_AT_THROW:
22933 cp_parser_error (parser, "expected %<@throw%>");
22934 return;
22935 case RT_TRANSACTION_ATOMIC:
22936 cp_parser_error (parser, "expected %<__transaction_atomic%>");
22937 return;
22938 case RT_TRANSACTION_RELAXED:
22939 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22940 return;
22941 default:
22942 break;
22944 if (!keyword)
22946 switch (token_desc)
22948 case RT_SEMICOLON:
22949 cp_parser_error (parser, "expected %<;%>");
22950 return;
22951 case RT_OPEN_PAREN:
22952 cp_parser_error (parser, "expected %<(%>");
22953 return;
22954 case RT_CLOSE_BRACE:
22955 cp_parser_error (parser, "expected %<}%>");
22956 return;
22957 case RT_OPEN_BRACE:
22958 cp_parser_error (parser, "expected %<{%>");
22959 return;
22960 case RT_CLOSE_SQUARE:
22961 cp_parser_error (parser, "expected %<]%>");
22962 return;
22963 case RT_OPEN_SQUARE:
22964 cp_parser_error (parser, "expected %<[%>");
22965 return;
22966 case RT_COMMA:
22967 cp_parser_error (parser, "expected %<,%>");
22968 return;
22969 case RT_SCOPE:
22970 cp_parser_error (parser, "expected %<::%>");
22971 return;
22972 case RT_LESS:
22973 cp_parser_error (parser, "expected %<<%>");
22974 return;
22975 case RT_GREATER:
22976 cp_parser_error (parser, "expected %<>%>");
22977 return;
22978 case RT_EQ:
22979 cp_parser_error (parser, "expected %<=%>");
22980 return;
22981 case RT_ELLIPSIS:
22982 cp_parser_error (parser, "expected %<...%>");
22983 return;
22984 case RT_MULT:
22985 cp_parser_error (parser, "expected %<*%>");
22986 return;
22987 case RT_COMPL:
22988 cp_parser_error (parser, "expected %<~%>");
22989 return;
22990 case RT_COLON:
22991 cp_parser_error (parser, "expected %<:%>");
22992 return;
22993 case RT_COLON_SCOPE:
22994 cp_parser_error (parser, "expected %<:%> or %<::%>");
22995 return;
22996 case RT_CLOSE_PAREN:
22997 cp_parser_error (parser, "expected %<)%>");
22998 return;
22999 case RT_COMMA_CLOSE_PAREN:
23000 cp_parser_error (parser, "expected %<,%> or %<)%>");
23001 return;
23002 case RT_PRAGMA_EOL:
23003 cp_parser_error (parser, "expected end of line");
23004 return;
23005 case RT_NAME:
23006 cp_parser_error (parser, "expected identifier");
23007 return;
23008 case RT_SELECT:
23009 cp_parser_error (parser, "expected selection-statement");
23010 return;
23011 case RT_INTERATION:
23012 cp_parser_error (parser, "expected iteration-statement");
23013 return;
23014 case RT_JUMP:
23015 cp_parser_error (parser, "expected jump-statement");
23016 return;
23017 case RT_CLASS_KEY:
23018 cp_parser_error (parser, "expected class-key");
23019 return;
23020 case RT_CLASS_TYPENAME_TEMPLATE:
23021 cp_parser_error (parser,
23022 "expected %<class%>, %<typename%>, or %<template%>");
23023 return;
23024 default:
23025 gcc_unreachable ();
23028 else
23029 gcc_unreachable ();
23034 /* If the next token is of the indicated TYPE, consume it. Otherwise,
23035 issue an error message indicating that TOKEN_DESC was expected.
23037 Returns the token consumed, if the token had the appropriate type.
23038 Otherwise, returns NULL. */
23040 static cp_token *
23041 cp_parser_require (cp_parser* parser,
23042 enum cpp_ttype type,
23043 required_token token_desc)
23045 if (cp_lexer_next_token_is (parser->lexer, type))
23046 return cp_lexer_consume_token (parser->lexer);
23047 else
23049 /* Output the MESSAGE -- unless we're parsing tentatively. */
23050 if (!cp_parser_simulate_error (parser))
23051 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
23052 return NULL;
23056 /* An error message is produced if the next token is not '>'.
23057 All further tokens are skipped until the desired token is
23058 found or '{', '}', ';' or an unbalanced ')' or ']'. */
23060 static void
23061 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
23063 /* Current level of '< ... >'. */
23064 unsigned level = 0;
23065 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
23066 unsigned nesting_depth = 0;
23068 /* Are we ready, yet? If not, issue error message. */
23069 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
23070 return;
23072 /* Skip tokens until the desired token is found. */
23073 while (true)
23075 /* Peek at the next token. */
23076 switch (cp_lexer_peek_token (parser->lexer)->type)
23078 case CPP_LESS:
23079 if (!nesting_depth)
23080 ++level;
23081 break;
23083 case CPP_RSHIFT:
23084 if (cxx_dialect == cxx98)
23085 /* C++0x views the `>>' operator as two `>' tokens, but
23086 C++98 does not. */
23087 break;
23088 else if (!nesting_depth && level-- == 0)
23090 /* We've hit a `>>' where the first `>' closes the
23091 template argument list, and the second `>' is
23092 spurious. Just consume the `>>' and stop; we've
23093 already produced at least one error. */
23094 cp_lexer_consume_token (parser->lexer);
23095 return;
23097 /* Fall through for C++0x, so we handle the second `>' in
23098 the `>>'. */
23100 case CPP_GREATER:
23101 if (!nesting_depth && level-- == 0)
23103 /* We've reached the token we want, consume it and stop. */
23104 cp_lexer_consume_token (parser->lexer);
23105 return;
23107 break;
23109 case CPP_OPEN_PAREN:
23110 case CPP_OPEN_SQUARE:
23111 ++nesting_depth;
23112 break;
23114 case CPP_CLOSE_PAREN:
23115 case CPP_CLOSE_SQUARE:
23116 if (nesting_depth-- == 0)
23117 return;
23118 break;
23120 case CPP_EOF:
23121 case CPP_PRAGMA_EOL:
23122 case CPP_SEMICOLON:
23123 case CPP_OPEN_BRACE:
23124 case CPP_CLOSE_BRACE:
23125 /* The '>' was probably forgotten, don't look further. */
23126 return;
23128 default:
23129 break;
23132 /* Consume this token. */
23133 cp_lexer_consume_token (parser->lexer);
23137 /* If the next token is the indicated keyword, consume it. Otherwise,
23138 issue an error message indicating that TOKEN_DESC was expected.
23140 Returns the token consumed, if the token had the appropriate type.
23141 Otherwise, returns NULL. */
23143 static cp_token *
23144 cp_parser_require_keyword (cp_parser* parser,
23145 enum rid keyword,
23146 required_token token_desc)
23148 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
23150 if (token && token->keyword != keyword)
23152 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
23153 return NULL;
23156 return token;
23159 /* Returns TRUE iff TOKEN is a token that can begin the body of a
23160 function-definition. */
23162 static bool
23163 cp_parser_token_starts_function_definition_p (cp_token* token)
23165 return (/* An ordinary function-body begins with an `{'. */
23166 token->type == CPP_OPEN_BRACE
23167 /* A ctor-initializer begins with a `:'. */
23168 || token->type == CPP_COLON
23169 /* A function-try-block begins with `try'. */
23170 || token->keyword == RID_TRY
23171 /* A function-transaction-block begins with `__transaction_atomic'
23172 or `__transaction_relaxed'. */
23173 || token->keyword == RID_TRANSACTION_ATOMIC
23174 || token->keyword == RID_TRANSACTION_RELAXED
23175 /* The named return value extension begins with `return'. */
23176 || token->keyword == RID_RETURN);
23179 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
23180 definition. */
23182 static bool
23183 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
23185 cp_token *token;
23187 token = cp_lexer_peek_token (parser->lexer);
23188 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
23191 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
23192 C++0x) ending a template-argument. */
23194 static bool
23195 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
23197 cp_token *token;
23199 token = cp_lexer_peek_token (parser->lexer);
23200 return (token->type == CPP_COMMA
23201 || token->type == CPP_GREATER
23202 || token->type == CPP_ELLIPSIS
23203 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
23206 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
23207 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
23209 static bool
23210 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
23211 size_t n)
23213 cp_token *token;
23215 token = cp_lexer_peek_nth_token (parser->lexer, n);
23216 if (token->type == CPP_LESS)
23217 return true;
23218 /* Check for the sequence `<::' in the original code. It would be lexed as
23219 `[:', where `[' is a digraph, and there is no whitespace before
23220 `:'. */
23221 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
23223 cp_token *token2;
23224 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
23225 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
23226 return true;
23228 return false;
23231 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
23232 or none_type otherwise. */
23234 static enum tag_types
23235 cp_parser_token_is_class_key (cp_token* token)
23237 switch (token->keyword)
23239 case RID_CLASS:
23240 return class_type;
23241 case RID_STRUCT:
23242 return record_type;
23243 case RID_UNION:
23244 return union_type;
23246 default:
23247 return none_type;
23251 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
23253 static void
23254 cp_parser_check_class_key (enum tag_types class_key, tree type)
23256 if (type == error_mark_node)
23257 return;
23258 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
23260 permerror (input_location, "%qs tag used in naming %q#T",
23261 class_key == union_type ? "union"
23262 : class_key == record_type ? "struct" : "class",
23263 type);
23264 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
23265 "%q#T was previously declared here", type);
23269 /* Issue an error message if DECL is redeclared with different
23270 access than its original declaration [class.access.spec/3].
23271 This applies to nested classes and nested class templates.
23272 [class.mem/1]. */
23274 static void
23275 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
23277 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
23278 return;
23280 if ((TREE_PRIVATE (decl)
23281 != (current_access_specifier == access_private_node))
23282 || (TREE_PROTECTED (decl)
23283 != (current_access_specifier == access_protected_node)))
23284 error_at (location, "%qD redeclared with different access", decl);
23287 /* Look for the `template' keyword, as a syntactic disambiguator.
23288 Return TRUE iff it is present, in which case it will be
23289 consumed. */
23291 static bool
23292 cp_parser_optional_template_keyword (cp_parser *parser)
23294 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23296 /* In C++98 the `template' keyword can only be used within templates;
23297 outside templates the parser can always figure out what is a
23298 template and what is not. In C++11, per the resolution of DR 468,
23299 `template' is allowed in cases where it is not strictly necessary. */
23300 if (!processing_template_decl
23301 && pedantic && cxx_dialect == cxx98)
23303 cp_token *token = cp_lexer_peek_token (parser->lexer);
23304 pedwarn (token->location, OPT_Wpedantic,
23305 "in C++98 %<template%> (as a disambiguator) is only "
23306 "allowed within templates");
23307 /* If this part of the token stream is rescanned, the same
23308 error message would be generated. So, we purge the token
23309 from the stream. */
23310 cp_lexer_purge_token (parser->lexer);
23311 return false;
23313 else
23315 /* Consume the `template' keyword. */
23316 cp_lexer_consume_token (parser->lexer);
23317 return true;
23320 return false;
23323 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
23324 set PARSER->SCOPE, and perform other related actions. */
23326 static void
23327 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
23329 int i;
23330 struct tree_check *check_value;
23331 deferred_access_check *chk;
23332 vec<deferred_access_check, va_gc> *checks;
23334 /* Get the stored value. */
23335 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
23336 /* Perform any access checks that were deferred. */
23337 checks = check_value->checks;
23338 if (checks)
23340 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
23341 perform_or_defer_access_check (chk->binfo,
23342 chk->decl,
23343 chk->diag_decl, tf_warning_or_error);
23345 /* Set the scope from the stored value. */
23346 parser->scope = check_value->value;
23347 parser->qualifying_scope = check_value->qualifying_scope;
23348 parser->object_scope = NULL_TREE;
23351 /* Consume tokens up through a non-nested END token. Returns TRUE if we
23352 encounter the end of a block before what we were looking for. */
23354 static bool
23355 cp_parser_cache_group (cp_parser *parser,
23356 enum cpp_ttype end,
23357 unsigned depth)
23359 while (true)
23361 cp_token *token = cp_lexer_peek_token (parser->lexer);
23363 /* Abort a parenthesized expression if we encounter a semicolon. */
23364 if ((end == CPP_CLOSE_PAREN || depth == 0)
23365 && token->type == CPP_SEMICOLON)
23366 return true;
23367 /* If we've reached the end of the file, stop. */
23368 if (token->type == CPP_EOF
23369 || (end != CPP_PRAGMA_EOL
23370 && token->type == CPP_PRAGMA_EOL))
23371 return true;
23372 if (token->type == CPP_CLOSE_BRACE && depth == 0)
23373 /* We've hit the end of an enclosing block, so there's been some
23374 kind of syntax error. */
23375 return true;
23377 /* Consume the token. */
23378 cp_lexer_consume_token (parser->lexer);
23379 /* See if it starts a new group. */
23380 if (token->type == CPP_OPEN_BRACE)
23382 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
23383 /* In theory this should probably check end == '}', but
23384 cp_parser_save_member_function_body needs it to exit
23385 after either '}' or ')' when called with ')'. */
23386 if (depth == 0)
23387 return false;
23389 else if (token->type == CPP_OPEN_PAREN)
23391 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
23392 if (depth == 0 && end == CPP_CLOSE_PAREN)
23393 return false;
23395 else if (token->type == CPP_PRAGMA)
23396 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
23397 else if (token->type == end)
23398 return false;
23402 /* Like above, for caching a default argument or NSDMI. Both of these are
23403 terminated by a non-nested comma, but it can be unclear whether or not a
23404 comma is nested in a template argument list unless we do more parsing.
23405 In order to handle this ambiguity, when we encounter a ',' after a '<'
23406 we try to parse what follows as a parameter-declaration-list (in the
23407 case of a default argument) or a member-declarator (in the case of an
23408 NSDMI). If that succeeds, then we stop caching. */
23410 static tree
23411 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
23413 unsigned depth = 0;
23414 int maybe_template_id = 0;
23415 cp_token *first_token;
23416 cp_token *token;
23417 tree default_argument;
23419 /* Add tokens until we have processed the entire default
23420 argument. We add the range [first_token, token). */
23421 first_token = cp_lexer_peek_token (parser->lexer);
23422 if (first_token->type == CPP_OPEN_BRACE)
23424 /* For list-initialization, this is straightforward. */
23425 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23426 token = cp_lexer_peek_token (parser->lexer);
23428 else while (true)
23430 bool done = false;
23432 /* Peek at the next token. */
23433 token = cp_lexer_peek_token (parser->lexer);
23434 /* What we do depends on what token we have. */
23435 switch (token->type)
23437 /* In valid code, a default argument must be
23438 immediately followed by a `,' `)', or `...'. */
23439 case CPP_COMMA:
23440 if (depth == 0 && maybe_template_id)
23442 /* If we've seen a '<', we might be in a
23443 template-argument-list. Until Core issue 325 is
23444 resolved, we don't know how this situation ought
23445 to be handled, so try to DTRT. We check whether
23446 what comes after the comma is a valid parameter
23447 declaration list. If it is, then the comma ends
23448 the default argument; otherwise the default
23449 argument continues. */
23450 bool error = false;
23451 tree t;
23453 /* Set ITALP so cp_parser_parameter_declaration_list
23454 doesn't decide to commit to this parse. */
23455 bool saved_italp = parser->in_template_argument_list_p;
23456 parser->in_template_argument_list_p = true;
23458 cp_parser_parse_tentatively (parser);
23459 cp_lexer_consume_token (parser->lexer);
23461 if (nsdmi)
23463 int ctor_dtor_or_conv_p;
23464 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23465 &ctor_dtor_or_conv_p,
23466 /*parenthesized_p=*/NULL,
23467 /*member_p=*/true);
23469 else
23471 begin_scope (sk_function_parms, NULL_TREE);
23472 cp_parser_parameter_declaration_list (parser, &error);
23473 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
23474 pop_binding (DECL_NAME (t), t);
23475 leave_scope ();
23477 if (!cp_parser_error_occurred (parser) && !error)
23478 done = true;
23479 cp_parser_abort_tentative_parse (parser);
23481 parser->in_template_argument_list_p = saved_italp;
23482 break;
23484 case CPP_CLOSE_PAREN:
23485 case CPP_ELLIPSIS:
23486 /* If we run into a non-nested `;', `}', or `]',
23487 then the code is invalid -- but the default
23488 argument is certainly over. */
23489 case CPP_SEMICOLON:
23490 case CPP_CLOSE_BRACE:
23491 case CPP_CLOSE_SQUARE:
23492 if (depth == 0)
23493 done = true;
23494 /* Update DEPTH, if necessary. */
23495 else if (token->type == CPP_CLOSE_PAREN
23496 || token->type == CPP_CLOSE_BRACE
23497 || token->type == CPP_CLOSE_SQUARE)
23498 --depth;
23499 break;
23501 case CPP_OPEN_PAREN:
23502 case CPP_OPEN_SQUARE:
23503 case CPP_OPEN_BRACE:
23504 ++depth;
23505 break;
23507 case CPP_LESS:
23508 if (depth == 0)
23509 /* This might be the comparison operator, or it might
23510 start a template argument list. */
23511 ++maybe_template_id;
23512 break;
23514 case CPP_RSHIFT:
23515 if (cxx_dialect == cxx98)
23516 break;
23517 /* Fall through for C++0x, which treats the `>>'
23518 operator like two `>' tokens in certain
23519 cases. */
23521 case CPP_GREATER:
23522 if (depth == 0)
23524 /* This might be an operator, or it might close a
23525 template argument list. But if a previous '<'
23526 started a template argument list, this will have
23527 closed it, so we can't be in one anymore. */
23528 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
23529 if (maybe_template_id < 0)
23530 maybe_template_id = 0;
23532 break;
23534 /* If we run out of tokens, issue an error message. */
23535 case CPP_EOF:
23536 case CPP_PRAGMA_EOL:
23537 error_at (token->location, "file ends in default argument");
23538 done = true;
23539 break;
23541 case CPP_NAME:
23542 case CPP_SCOPE:
23543 /* In these cases, we should look for template-ids.
23544 For example, if the default argument is
23545 `X<int, double>()', we need to do name lookup to
23546 figure out whether or not `X' is a template; if
23547 so, the `,' does not end the default argument.
23549 That is not yet done. */
23550 break;
23552 default:
23553 break;
23556 /* If we've reached the end, stop. */
23557 if (done)
23558 break;
23560 /* Add the token to the token block. */
23561 token = cp_lexer_consume_token (parser->lexer);
23564 /* Create a DEFAULT_ARG to represent the unparsed default
23565 argument. */
23566 default_argument = make_node (DEFAULT_ARG);
23567 DEFARG_TOKENS (default_argument)
23568 = cp_token_cache_new (first_token, token);
23569 DEFARG_INSTANTIATIONS (default_argument) = NULL;
23571 return default_argument;
23574 /* Begin parsing tentatively. We always save tokens while parsing
23575 tentatively so that if the tentative parsing fails we can restore the
23576 tokens. */
23578 static void
23579 cp_parser_parse_tentatively (cp_parser* parser)
23581 /* Enter a new parsing context. */
23582 parser->context = cp_parser_context_new (parser->context);
23583 /* Begin saving tokens. */
23584 cp_lexer_save_tokens (parser->lexer);
23585 /* In order to avoid repetitive access control error messages,
23586 access checks are queued up until we are no longer parsing
23587 tentatively. */
23588 push_deferring_access_checks (dk_deferred);
23591 /* Commit to the currently active tentative parse. */
23593 static void
23594 cp_parser_commit_to_tentative_parse (cp_parser* parser)
23596 cp_parser_context *context;
23597 cp_lexer *lexer;
23599 /* Mark all of the levels as committed. */
23600 lexer = parser->lexer;
23601 for (context = parser->context; context->next; context = context->next)
23603 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
23604 break;
23605 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
23606 while (!cp_lexer_saving_tokens (lexer))
23607 lexer = lexer->next;
23608 cp_lexer_commit_tokens (lexer);
23612 /* Abort the currently active tentative parse. All consumed tokens
23613 will be rolled back, and no diagnostics will be issued. */
23615 static void
23616 cp_parser_abort_tentative_parse (cp_parser* parser)
23618 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
23619 || errorcount > 0);
23620 cp_parser_simulate_error (parser);
23621 /* Now, pretend that we want to see if the construct was
23622 successfully parsed. */
23623 cp_parser_parse_definitely (parser);
23626 /* Stop parsing tentatively. If a parse error has occurred, restore the
23627 token stream. Otherwise, commit to the tokens we have consumed.
23628 Returns true if no error occurred; false otherwise. */
23630 static bool
23631 cp_parser_parse_definitely (cp_parser* parser)
23633 bool error_occurred;
23634 cp_parser_context *context;
23636 /* Remember whether or not an error occurred, since we are about to
23637 destroy that information. */
23638 error_occurred = cp_parser_error_occurred (parser);
23639 /* Remove the topmost context from the stack. */
23640 context = parser->context;
23641 parser->context = context->next;
23642 /* If no parse errors occurred, commit to the tentative parse. */
23643 if (!error_occurred)
23645 /* Commit to the tokens read tentatively, unless that was
23646 already done. */
23647 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
23648 cp_lexer_commit_tokens (parser->lexer);
23650 pop_to_parent_deferring_access_checks ();
23652 /* Otherwise, if errors occurred, roll back our state so that things
23653 are just as they were before we began the tentative parse. */
23654 else
23656 cp_lexer_rollback_tokens (parser->lexer);
23657 pop_deferring_access_checks ();
23659 /* Add the context to the front of the free list. */
23660 context->next = cp_parser_context_free_list;
23661 cp_parser_context_free_list = context;
23663 return !error_occurred;
23666 /* Returns true if we are parsing tentatively and are not committed to
23667 this tentative parse. */
23669 static bool
23670 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
23672 return (cp_parser_parsing_tentatively (parser)
23673 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
23676 /* Returns nonzero iff an error has occurred during the most recent
23677 tentative parse. */
23679 static bool
23680 cp_parser_error_occurred (cp_parser* parser)
23682 return (cp_parser_parsing_tentatively (parser)
23683 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
23686 /* Returns nonzero if GNU extensions are allowed. */
23688 static bool
23689 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
23691 return parser->allow_gnu_extensions_p;
23694 /* Objective-C++ Productions */
23697 /* Parse an Objective-C expression, which feeds into a primary-expression
23698 above.
23700 objc-expression:
23701 objc-message-expression
23702 objc-string-literal
23703 objc-encode-expression
23704 objc-protocol-expression
23705 objc-selector-expression
23707 Returns a tree representation of the expression. */
23709 static tree
23710 cp_parser_objc_expression (cp_parser* parser)
23712 /* Try to figure out what kind of declaration is present. */
23713 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23715 switch (kwd->type)
23717 case CPP_OPEN_SQUARE:
23718 return cp_parser_objc_message_expression (parser);
23720 case CPP_OBJC_STRING:
23721 kwd = cp_lexer_consume_token (parser->lexer);
23722 return objc_build_string_object (kwd->u.value);
23724 case CPP_KEYWORD:
23725 switch (kwd->keyword)
23727 case RID_AT_ENCODE:
23728 return cp_parser_objc_encode_expression (parser);
23730 case RID_AT_PROTOCOL:
23731 return cp_parser_objc_protocol_expression (parser);
23733 case RID_AT_SELECTOR:
23734 return cp_parser_objc_selector_expression (parser);
23736 default:
23737 break;
23739 default:
23740 error_at (kwd->location,
23741 "misplaced %<@%D%> Objective-C++ construct",
23742 kwd->u.value);
23743 cp_parser_skip_to_end_of_block_or_statement (parser);
23746 return error_mark_node;
23749 /* Parse an Objective-C message expression.
23751 objc-message-expression:
23752 [ objc-message-receiver objc-message-args ]
23754 Returns a representation of an Objective-C message. */
23756 static tree
23757 cp_parser_objc_message_expression (cp_parser* parser)
23759 tree receiver, messageargs;
23761 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
23762 receiver = cp_parser_objc_message_receiver (parser);
23763 messageargs = cp_parser_objc_message_args (parser);
23764 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23766 return objc_build_message_expr (receiver, messageargs);
23769 /* Parse an objc-message-receiver.
23771 objc-message-receiver:
23772 expression
23773 simple-type-specifier
23775 Returns a representation of the type or expression. */
23777 static tree
23778 cp_parser_objc_message_receiver (cp_parser* parser)
23780 tree rcv;
23782 /* An Objective-C message receiver may be either (1) a type
23783 or (2) an expression. */
23784 cp_parser_parse_tentatively (parser);
23785 rcv = cp_parser_expression (parser, false, NULL);
23787 if (cp_parser_parse_definitely (parser))
23788 return rcv;
23790 rcv = cp_parser_simple_type_specifier (parser,
23791 /*decl_specs=*/NULL,
23792 CP_PARSER_FLAGS_NONE);
23794 return objc_get_class_reference (rcv);
23797 /* Parse the arguments and selectors comprising an Objective-C message.
23799 objc-message-args:
23800 objc-selector
23801 objc-selector-args
23802 objc-selector-args , objc-comma-args
23804 objc-selector-args:
23805 objc-selector [opt] : assignment-expression
23806 objc-selector-args objc-selector [opt] : assignment-expression
23808 objc-comma-args:
23809 assignment-expression
23810 objc-comma-args , assignment-expression
23812 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23813 selector arguments and TREE_VALUE containing a list of comma
23814 arguments. */
23816 static tree
23817 cp_parser_objc_message_args (cp_parser* parser)
23819 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23820 bool maybe_unary_selector_p = true;
23821 cp_token *token = cp_lexer_peek_token (parser->lexer);
23823 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23825 tree selector = NULL_TREE, arg;
23827 if (token->type != CPP_COLON)
23828 selector = cp_parser_objc_selector (parser);
23830 /* Detect if we have a unary selector. */
23831 if (maybe_unary_selector_p
23832 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23833 return build_tree_list (selector, NULL_TREE);
23835 maybe_unary_selector_p = false;
23836 cp_parser_require (parser, CPP_COLON, RT_COLON);
23837 arg = cp_parser_assignment_expression (parser, false, NULL);
23839 sel_args
23840 = chainon (sel_args,
23841 build_tree_list (selector, arg));
23843 token = cp_lexer_peek_token (parser->lexer);
23846 /* Handle non-selector arguments, if any. */
23847 while (token->type == CPP_COMMA)
23849 tree arg;
23851 cp_lexer_consume_token (parser->lexer);
23852 arg = cp_parser_assignment_expression (parser, false, NULL);
23854 addl_args
23855 = chainon (addl_args,
23856 build_tree_list (NULL_TREE, arg));
23858 token = cp_lexer_peek_token (parser->lexer);
23861 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23863 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23864 return build_tree_list (error_mark_node, error_mark_node);
23867 return build_tree_list (sel_args, addl_args);
23870 /* Parse an Objective-C encode expression.
23872 objc-encode-expression:
23873 @encode objc-typename
23875 Returns an encoded representation of the type argument. */
23877 static tree
23878 cp_parser_objc_encode_expression (cp_parser* parser)
23880 tree type;
23881 cp_token *token;
23883 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
23884 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23885 token = cp_lexer_peek_token (parser->lexer);
23886 type = complete_type (cp_parser_type_id (parser));
23887 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23889 if (!type)
23891 error_at (token->location,
23892 "%<@encode%> must specify a type as an argument");
23893 return error_mark_node;
23896 /* This happens if we find @encode(T) (where T is a template
23897 typename or something dependent on a template typename) when
23898 parsing a template. In that case, we can't compile it
23899 immediately, but we rather create an AT_ENCODE_EXPR which will
23900 need to be instantiated when the template is used.
23902 if (dependent_type_p (type))
23904 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23905 TREE_READONLY (value) = 1;
23906 return value;
23909 return objc_build_encode_expr (type);
23912 /* Parse an Objective-C @defs expression. */
23914 static tree
23915 cp_parser_objc_defs_expression (cp_parser *parser)
23917 tree name;
23919 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
23920 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23921 name = cp_parser_identifier (parser);
23922 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23924 return objc_get_class_ivars (name);
23927 /* Parse an Objective-C protocol expression.
23929 objc-protocol-expression:
23930 @protocol ( identifier )
23932 Returns a representation of the protocol expression. */
23934 static tree
23935 cp_parser_objc_protocol_expression (cp_parser* parser)
23937 tree proto;
23939 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23940 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23941 proto = cp_parser_identifier (parser);
23942 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23944 return objc_build_protocol_expr (proto);
23947 /* Parse an Objective-C selector expression.
23949 objc-selector-expression:
23950 @selector ( objc-method-signature )
23952 objc-method-signature:
23953 objc-selector
23954 objc-selector-seq
23956 objc-selector-seq:
23957 objc-selector :
23958 objc-selector-seq objc-selector :
23960 Returns a representation of the method selector. */
23962 static tree
23963 cp_parser_objc_selector_expression (cp_parser* parser)
23965 tree sel_seq = NULL_TREE;
23966 bool maybe_unary_selector_p = true;
23967 cp_token *token;
23968 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23970 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
23971 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23972 token = cp_lexer_peek_token (parser->lexer);
23974 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23975 || token->type == CPP_SCOPE)
23977 tree selector = NULL_TREE;
23979 if (token->type != CPP_COLON
23980 || token->type == CPP_SCOPE)
23981 selector = cp_parser_objc_selector (parser);
23983 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23984 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23986 /* Detect if we have a unary selector. */
23987 if (maybe_unary_selector_p)
23989 sel_seq = selector;
23990 goto finish_selector;
23992 else
23994 cp_parser_error (parser, "expected %<:%>");
23997 maybe_unary_selector_p = false;
23998 token = cp_lexer_consume_token (parser->lexer);
24000 if (token->type == CPP_SCOPE)
24002 sel_seq
24003 = chainon (sel_seq,
24004 build_tree_list (selector, NULL_TREE));
24005 sel_seq
24006 = chainon (sel_seq,
24007 build_tree_list (NULL_TREE, NULL_TREE));
24009 else
24010 sel_seq
24011 = chainon (sel_seq,
24012 build_tree_list (selector, NULL_TREE));
24014 token = cp_lexer_peek_token (parser->lexer);
24017 finish_selector:
24018 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24020 return objc_build_selector_expr (loc, sel_seq);
24023 /* Parse a list of identifiers.
24025 objc-identifier-list:
24026 identifier
24027 objc-identifier-list , identifier
24029 Returns a TREE_LIST of identifier nodes. */
24031 static tree
24032 cp_parser_objc_identifier_list (cp_parser* parser)
24034 tree identifier;
24035 tree list;
24036 cp_token *sep;
24038 identifier = cp_parser_identifier (parser);
24039 if (identifier == error_mark_node)
24040 return error_mark_node;
24042 list = build_tree_list (NULL_TREE, identifier);
24043 sep = cp_lexer_peek_token (parser->lexer);
24045 while (sep->type == CPP_COMMA)
24047 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24048 identifier = cp_parser_identifier (parser);
24049 if (identifier == error_mark_node)
24050 return list;
24052 list = chainon (list, build_tree_list (NULL_TREE,
24053 identifier));
24054 sep = cp_lexer_peek_token (parser->lexer);
24057 return list;
24060 /* Parse an Objective-C alias declaration.
24062 objc-alias-declaration:
24063 @compatibility_alias identifier identifier ;
24065 This function registers the alias mapping with the Objective-C front end.
24066 It returns nothing. */
24068 static void
24069 cp_parser_objc_alias_declaration (cp_parser* parser)
24071 tree alias, orig;
24073 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
24074 alias = cp_parser_identifier (parser);
24075 orig = cp_parser_identifier (parser);
24076 objc_declare_alias (alias, orig);
24077 cp_parser_consume_semicolon_at_end_of_statement (parser);
24080 /* Parse an Objective-C class forward-declaration.
24082 objc-class-declaration:
24083 @class objc-identifier-list ;
24085 The function registers the forward declarations with the Objective-C
24086 front end. It returns nothing. */
24088 static void
24089 cp_parser_objc_class_declaration (cp_parser* parser)
24091 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
24092 while (true)
24094 tree id;
24096 id = cp_parser_identifier (parser);
24097 if (id == error_mark_node)
24098 break;
24100 objc_declare_class (id);
24102 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24103 cp_lexer_consume_token (parser->lexer);
24104 else
24105 break;
24107 cp_parser_consume_semicolon_at_end_of_statement (parser);
24110 /* Parse a list of Objective-C protocol references.
24112 objc-protocol-refs-opt:
24113 objc-protocol-refs [opt]
24115 objc-protocol-refs:
24116 < objc-identifier-list >
24118 Returns a TREE_LIST of identifiers, if any. */
24120 static tree
24121 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
24123 tree protorefs = NULL_TREE;
24125 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
24127 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
24128 protorefs = cp_parser_objc_identifier_list (parser);
24129 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
24132 return protorefs;
24135 /* Parse a Objective-C visibility specification. */
24137 static void
24138 cp_parser_objc_visibility_spec (cp_parser* parser)
24140 cp_token *vis = cp_lexer_peek_token (parser->lexer);
24142 switch (vis->keyword)
24144 case RID_AT_PRIVATE:
24145 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
24146 break;
24147 case RID_AT_PROTECTED:
24148 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
24149 break;
24150 case RID_AT_PUBLIC:
24151 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
24152 break;
24153 case RID_AT_PACKAGE:
24154 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
24155 break;
24156 default:
24157 return;
24160 /* Eat '@private'/'@protected'/'@public'. */
24161 cp_lexer_consume_token (parser->lexer);
24164 /* Parse an Objective-C method type. Return 'true' if it is a class
24165 (+) method, and 'false' if it is an instance (-) method. */
24167 static inline bool
24168 cp_parser_objc_method_type (cp_parser* parser)
24170 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
24171 return true;
24172 else
24173 return false;
24176 /* Parse an Objective-C protocol qualifier. */
24178 static tree
24179 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
24181 tree quals = NULL_TREE, node;
24182 cp_token *token = cp_lexer_peek_token (parser->lexer);
24184 node = token->u.value;
24186 while (node && TREE_CODE (node) == IDENTIFIER_NODE
24187 && (node == ridpointers [(int) RID_IN]
24188 || node == ridpointers [(int) RID_OUT]
24189 || node == ridpointers [(int) RID_INOUT]
24190 || node == ridpointers [(int) RID_BYCOPY]
24191 || node == ridpointers [(int) RID_BYREF]
24192 || node == ridpointers [(int) RID_ONEWAY]))
24194 quals = tree_cons (NULL_TREE, node, quals);
24195 cp_lexer_consume_token (parser->lexer);
24196 token = cp_lexer_peek_token (parser->lexer);
24197 node = token->u.value;
24200 return quals;
24203 /* Parse an Objective-C typename. */
24205 static tree
24206 cp_parser_objc_typename (cp_parser* parser)
24208 tree type_name = NULL_TREE;
24210 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24212 tree proto_quals, cp_type = NULL_TREE;
24214 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24215 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
24217 /* An ObjC type name may consist of just protocol qualifiers, in which
24218 case the type shall default to 'id'. */
24219 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24221 cp_type = cp_parser_type_id (parser);
24223 /* If the type could not be parsed, an error has already
24224 been produced. For error recovery, behave as if it had
24225 not been specified, which will use the default type
24226 'id'. */
24227 if (cp_type == error_mark_node)
24229 cp_type = NULL_TREE;
24230 /* We need to skip to the closing parenthesis as
24231 cp_parser_type_id() does not seem to do it for
24232 us. */
24233 cp_parser_skip_to_closing_parenthesis (parser,
24234 /*recovering=*/true,
24235 /*or_comma=*/false,
24236 /*consume_paren=*/false);
24240 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24241 type_name = build_tree_list (proto_quals, cp_type);
24244 return type_name;
24247 /* Check to see if TYPE refers to an Objective-C selector name. */
24249 static bool
24250 cp_parser_objc_selector_p (enum cpp_ttype type)
24252 return (type == CPP_NAME || type == CPP_KEYWORD
24253 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
24254 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
24255 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
24256 || type == CPP_XOR || type == CPP_XOR_EQ);
24259 /* Parse an Objective-C selector. */
24261 static tree
24262 cp_parser_objc_selector (cp_parser* parser)
24264 cp_token *token = cp_lexer_consume_token (parser->lexer);
24266 if (!cp_parser_objc_selector_p (token->type))
24268 error_at (token->location, "invalid Objective-C++ selector name");
24269 return error_mark_node;
24272 /* C++ operator names are allowed to appear in ObjC selectors. */
24273 switch (token->type)
24275 case CPP_AND_AND: return get_identifier ("and");
24276 case CPP_AND_EQ: return get_identifier ("and_eq");
24277 case CPP_AND: return get_identifier ("bitand");
24278 case CPP_OR: return get_identifier ("bitor");
24279 case CPP_COMPL: return get_identifier ("compl");
24280 case CPP_NOT: return get_identifier ("not");
24281 case CPP_NOT_EQ: return get_identifier ("not_eq");
24282 case CPP_OR_OR: return get_identifier ("or");
24283 case CPP_OR_EQ: return get_identifier ("or_eq");
24284 case CPP_XOR: return get_identifier ("xor");
24285 case CPP_XOR_EQ: return get_identifier ("xor_eq");
24286 default: return token->u.value;
24290 /* Parse an Objective-C params list. */
24292 static tree
24293 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
24295 tree params = NULL_TREE;
24296 bool maybe_unary_selector_p = true;
24297 cp_token *token = cp_lexer_peek_token (parser->lexer);
24299 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24301 tree selector = NULL_TREE, type_name, identifier;
24302 tree parm_attr = NULL_TREE;
24304 if (token->keyword == RID_ATTRIBUTE)
24305 break;
24307 if (token->type != CPP_COLON)
24308 selector = cp_parser_objc_selector (parser);
24310 /* Detect if we have a unary selector. */
24311 if (maybe_unary_selector_p
24312 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24314 params = selector; /* Might be followed by attributes. */
24315 break;
24318 maybe_unary_selector_p = false;
24319 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24321 /* Something went quite wrong. There should be a colon
24322 here, but there is not. Stop parsing parameters. */
24323 break;
24325 type_name = cp_parser_objc_typename (parser);
24326 /* New ObjC allows attributes on parameters too. */
24327 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
24328 parm_attr = cp_parser_attributes_opt (parser);
24329 identifier = cp_parser_identifier (parser);
24331 params
24332 = chainon (params,
24333 objc_build_keyword_decl (selector,
24334 type_name,
24335 identifier,
24336 parm_attr));
24338 token = cp_lexer_peek_token (parser->lexer);
24341 if (params == NULL_TREE)
24343 cp_parser_error (parser, "objective-c++ method declaration is expected");
24344 return error_mark_node;
24347 /* We allow tail attributes for the method. */
24348 if (token->keyword == RID_ATTRIBUTE)
24350 *attributes = cp_parser_attributes_opt (parser);
24351 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24352 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24353 return params;
24354 cp_parser_error (parser,
24355 "method attributes must be specified at the end");
24356 return error_mark_node;
24359 if (params == NULL_TREE)
24361 cp_parser_error (parser, "objective-c++ method declaration is expected");
24362 return error_mark_node;
24364 return params;
24367 /* Parse the non-keyword Objective-C params. */
24369 static tree
24370 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
24371 tree* attributes)
24373 tree params = make_node (TREE_LIST);
24374 cp_token *token = cp_lexer_peek_token (parser->lexer);
24375 *ellipsisp = false; /* Initially, assume no ellipsis. */
24377 while (token->type == CPP_COMMA)
24379 cp_parameter_declarator *parmdecl;
24380 tree parm;
24382 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24383 token = cp_lexer_peek_token (parser->lexer);
24385 if (token->type == CPP_ELLIPSIS)
24387 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
24388 *ellipsisp = true;
24389 token = cp_lexer_peek_token (parser->lexer);
24390 break;
24393 /* TODO: parse attributes for tail parameters. */
24394 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
24395 parm = grokdeclarator (parmdecl->declarator,
24396 &parmdecl->decl_specifiers,
24397 PARM, /*initialized=*/0,
24398 /*attrlist=*/NULL);
24400 chainon (params, build_tree_list (NULL_TREE, parm));
24401 token = cp_lexer_peek_token (parser->lexer);
24404 /* We allow tail attributes for the method. */
24405 if (token->keyword == RID_ATTRIBUTE)
24407 if (*attributes == NULL_TREE)
24409 *attributes = cp_parser_attributes_opt (parser);
24410 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24411 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24412 return params;
24414 else
24415 /* We have an error, but parse the attributes, so that we can
24416 carry on. */
24417 *attributes = cp_parser_attributes_opt (parser);
24419 cp_parser_error (parser,
24420 "method attributes must be specified at the end");
24421 return error_mark_node;
24424 return params;
24427 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
24429 static void
24430 cp_parser_objc_interstitial_code (cp_parser* parser)
24432 cp_token *token = cp_lexer_peek_token (parser->lexer);
24434 /* If the next token is `extern' and the following token is a string
24435 literal, then we have a linkage specification. */
24436 if (token->keyword == RID_EXTERN
24437 && cp_parser_is_pure_string_literal
24438 (cp_lexer_peek_nth_token (parser->lexer, 2)))
24439 cp_parser_linkage_specification (parser);
24440 /* Handle #pragma, if any. */
24441 else if (token->type == CPP_PRAGMA)
24442 cp_parser_pragma (parser, pragma_external);
24443 /* Allow stray semicolons. */
24444 else if (token->type == CPP_SEMICOLON)
24445 cp_lexer_consume_token (parser->lexer);
24446 /* Mark methods as optional or required, when building protocols. */
24447 else if (token->keyword == RID_AT_OPTIONAL)
24449 cp_lexer_consume_token (parser->lexer);
24450 objc_set_method_opt (true);
24452 else if (token->keyword == RID_AT_REQUIRED)
24454 cp_lexer_consume_token (parser->lexer);
24455 objc_set_method_opt (false);
24457 else if (token->keyword == RID_NAMESPACE)
24458 cp_parser_namespace_definition (parser);
24459 /* Other stray characters must generate errors. */
24460 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
24462 cp_lexer_consume_token (parser->lexer);
24463 error ("stray %qs between Objective-C++ methods",
24464 token->type == CPP_OPEN_BRACE ? "{" : "}");
24466 /* Finally, try to parse a block-declaration, or a function-definition. */
24467 else
24468 cp_parser_block_declaration (parser, /*statement_p=*/false);
24471 /* Parse a method signature. */
24473 static tree
24474 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
24476 tree rettype, kwdparms, optparms;
24477 bool ellipsis = false;
24478 bool is_class_method;
24480 is_class_method = cp_parser_objc_method_type (parser);
24481 rettype = cp_parser_objc_typename (parser);
24482 *attributes = NULL_TREE;
24483 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
24484 if (kwdparms == error_mark_node)
24485 return error_mark_node;
24486 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
24487 if (optparms == error_mark_node)
24488 return error_mark_node;
24490 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
24493 static bool
24494 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
24496 tree tattr;
24497 cp_lexer_save_tokens (parser->lexer);
24498 tattr = cp_parser_attributes_opt (parser);
24499 gcc_assert (tattr) ;
24501 /* If the attributes are followed by a method introducer, this is not allowed.
24502 Dump the attributes and flag the situation. */
24503 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
24504 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
24505 return true;
24507 /* Otherwise, the attributes introduce some interstitial code, possibly so
24508 rewind to allow that check. */
24509 cp_lexer_rollback_tokens (parser->lexer);
24510 return false;
24513 /* Parse an Objective-C method prototype list. */
24515 static void
24516 cp_parser_objc_method_prototype_list (cp_parser* parser)
24518 cp_token *token = cp_lexer_peek_token (parser->lexer);
24520 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24522 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24524 tree attributes, sig;
24525 bool is_class_method;
24526 if (token->type == CPP_PLUS)
24527 is_class_method = true;
24528 else
24529 is_class_method = false;
24530 sig = cp_parser_objc_method_signature (parser, &attributes);
24531 if (sig == error_mark_node)
24533 cp_parser_skip_to_end_of_block_or_statement (parser);
24534 token = cp_lexer_peek_token (parser->lexer);
24535 continue;
24537 objc_add_method_declaration (is_class_method, sig, attributes);
24538 cp_parser_consume_semicolon_at_end_of_statement (parser);
24540 else if (token->keyword == RID_AT_PROPERTY)
24541 cp_parser_objc_at_property_declaration (parser);
24542 else if (token->keyword == RID_ATTRIBUTE
24543 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24544 warning_at (cp_lexer_peek_token (parser->lexer)->location,
24545 OPT_Wattributes,
24546 "prefix attributes are ignored for methods");
24547 else
24548 /* Allow for interspersed non-ObjC++ code. */
24549 cp_parser_objc_interstitial_code (parser);
24551 token = cp_lexer_peek_token (parser->lexer);
24554 if (token->type != CPP_EOF)
24555 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24556 else
24557 cp_parser_error (parser, "expected %<@end%>");
24559 objc_finish_interface ();
24562 /* Parse an Objective-C method definition list. */
24564 static void
24565 cp_parser_objc_method_definition_list (cp_parser* parser)
24567 cp_token *token = cp_lexer_peek_token (parser->lexer);
24569 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24571 tree meth;
24573 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24575 cp_token *ptk;
24576 tree sig, attribute;
24577 bool is_class_method;
24578 if (token->type == CPP_PLUS)
24579 is_class_method = true;
24580 else
24581 is_class_method = false;
24582 push_deferring_access_checks (dk_deferred);
24583 sig = cp_parser_objc_method_signature (parser, &attribute);
24584 if (sig == error_mark_node)
24586 cp_parser_skip_to_end_of_block_or_statement (parser);
24587 token = cp_lexer_peek_token (parser->lexer);
24588 continue;
24590 objc_start_method_definition (is_class_method, sig, attribute,
24591 NULL_TREE);
24593 /* For historical reasons, we accept an optional semicolon. */
24594 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24595 cp_lexer_consume_token (parser->lexer);
24597 ptk = cp_lexer_peek_token (parser->lexer);
24598 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
24599 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
24601 perform_deferred_access_checks (tf_warning_or_error);
24602 stop_deferring_access_checks ();
24603 meth = cp_parser_function_definition_after_declarator (parser,
24604 false);
24605 pop_deferring_access_checks ();
24606 objc_finish_method_definition (meth);
24609 /* The following case will be removed once @synthesize is
24610 completely implemented. */
24611 else if (token->keyword == RID_AT_PROPERTY)
24612 cp_parser_objc_at_property_declaration (parser);
24613 else if (token->keyword == RID_AT_SYNTHESIZE)
24614 cp_parser_objc_at_synthesize_declaration (parser);
24615 else if (token->keyword == RID_AT_DYNAMIC)
24616 cp_parser_objc_at_dynamic_declaration (parser);
24617 else if (token->keyword == RID_ATTRIBUTE
24618 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24619 warning_at (token->location, OPT_Wattributes,
24620 "prefix attributes are ignored for methods");
24621 else
24622 /* Allow for interspersed non-ObjC++ code. */
24623 cp_parser_objc_interstitial_code (parser);
24625 token = cp_lexer_peek_token (parser->lexer);
24628 if (token->type != CPP_EOF)
24629 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24630 else
24631 cp_parser_error (parser, "expected %<@end%>");
24633 objc_finish_implementation ();
24636 /* Parse Objective-C ivars. */
24638 static void
24639 cp_parser_objc_class_ivars (cp_parser* parser)
24641 cp_token *token = cp_lexer_peek_token (parser->lexer);
24643 if (token->type != CPP_OPEN_BRACE)
24644 return; /* No ivars specified. */
24646 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
24647 token = cp_lexer_peek_token (parser->lexer);
24649 while (token->type != CPP_CLOSE_BRACE
24650 && token->keyword != RID_AT_END && token->type != CPP_EOF)
24652 cp_decl_specifier_seq declspecs;
24653 int decl_class_or_enum_p;
24654 tree prefix_attributes;
24656 cp_parser_objc_visibility_spec (parser);
24658 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24659 break;
24661 cp_parser_decl_specifier_seq (parser,
24662 CP_PARSER_FLAGS_OPTIONAL,
24663 &declspecs,
24664 &decl_class_or_enum_p);
24666 /* auto, register, static, extern, mutable. */
24667 if (declspecs.storage_class != sc_none)
24669 cp_parser_error (parser, "invalid type for instance variable");
24670 declspecs.storage_class = sc_none;
24673 /* thread_local. */
24674 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24676 cp_parser_error (parser, "invalid type for instance variable");
24677 declspecs.locations[ds_thread] = 0;
24680 /* typedef. */
24681 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24683 cp_parser_error (parser, "invalid type for instance variable");
24684 declspecs.locations[ds_typedef] = 0;
24687 prefix_attributes = declspecs.attributes;
24688 declspecs.attributes = NULL_TREE;
24690 /* Keep going until we hit the `;' at the end of the
24691 declaration. */
24692 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24694 tree width = NULL_TREE, attributes, first_attribute, decl;
24695 cp_declarator *declarator = NULL;
24696 int ctor_dtor_or_conv_p;
24698 /* Check for a (possibly unnamed) bitfield declaration. */
24699 token = cp_lexer_peek_token (parser->lexer);
24700 if (token->type == CPP_COLON)
24701 goto eat_colon;
24703 if (token->type == CPP_NAME
24704 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24705 == CPP_COLON))
24707 /* Get the name of the bitfield. */
24708 declarator = make_id_declarator (NULL_TREE,
24709 cp_parser_identifier (parser),
24710 sfk_none);
24712 eat_colon:
24713 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24714 /* Get the width of the bitfield. */
24715 width
24716 = cp_parser_constant_expression (parser,
24717 /*allow_non_constant=*/false,
24718 NULL);
24720 else
24722 /* Parse the declarator. */
24723 declarator
24724 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24725 &ctor_dtor_or_conv_p,
24726 /*parenthesized_p=*/NULL,
24727 /*member_p=*/false);
24730 /* Look for attributes that apply to the ivar. */
24731 attributes = cp_parser_attributes_opt (parser);
24732 /* Remember which attributes are prefix attributes and
24733 which are not. */
24734 first_attribute = attributes;
24735 /* Combine the attributes. */
24736 attributes = chainon (prefix_attributes, attributes);
24738 if (width)
24739 /* Create the bitfield declaration. */
24740 decl = grokbitfield (declarator, &declspecs,
24741 width,
24742 attributes);
24743 else
24744 decl = grokfield (declarator, &declspecs,
24745 NULL_TREE, /*init_const_expr_p=*/false,
24746 NULL_TREE, attributes);
24748 /* Add the instance variable. */
24749 if (decl != error_mark_node && decl != NULL_TREE)
24750 objc_add_instance_variable (decl);
24752 /* Reset PREFIX_ATTRIBUTES. */
24753 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24754 attributes = TREE_CHAIN (attributes);
24755 if (attributes)
24756 TREE_CHAIN (attributes) = NULL_TREE;
24758 token = cp_lexer_peek_token (parser->lexer);
24760 if (token->type == CPP_COMMA)
24762 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24763 continue;
24765 break;
24768 cp_parser_consume_semicolon_at_end_of_statement (parser);
24769 token = cp_lexer_peek_token (parser->lexer);
24772 if (token->keyword == RID_AT_END)
24773 cp_parser_error (parser, "expected %<}%>");
24775 /* Do not consume the RID_AT_END, so it will be read again as terminating
24776 the @interface of @implementation. */
24777 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24778 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
24780 /* For historical reasons, we accept an optional semicolon. */
24781 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24782 cp_lexer_consume_token (parser->lexer);
24785 /* Parse an Objective-C protocol declaration. */
24787 static void
24788 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24790 tree proto, protorefs;
24791 cp_token *tok;
24793 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24794 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24796 tok = cp_lexer_peek_token (parser->lexer);
24797 error_at (tok->location, "identifier expected after %<@protocol%>");
24798 cp_parser_consume_semicolon_at_end_of_statement (parser);
24799 return;
24802 /* See if we have a forward declaration or a definition. */
24803 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24805 /* Try a forward declaration first. */
24806 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24808 while (true)
24810 tree id;
24812 id = cp_parser_identifier (parser);
24813 if (id == error_mark_node)
24814 break;
24816 objc_declare_protocol (id, attributes);
24818 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24819 cp_lexer_consume_token (parser->lexer);
24820 else
24821 break;
24823 cp_parser_consume_semicolon_at_end_of_statement (parser);
24826 /* Ok, we got a full-fledged definition (or at least should). */
24827 else
24829 proto = cp_parser_identifier (parser);
24830 protorefs = cp_parser_objc_protocol_refs_opt (parser);
24831 objc_start_protocol (proto, protorefs, attributes);
24832 cp_parser_objc_method_prototype_list (parser);
24836 /* Parse an Objective-C superclass or category. */
24838 static void
24839 cp_parser_objc_superclass_or_category (cp_parser *parser,
24840 bool iface_p,
24841 tree *super,
24842 tree *categ, bool *is_class_extension)
24844 cp_token *next = cp_lexer_peek_token (parser->lexer);
24846 *super = *categ = NULL_TREE;
24847 *is_class_extension = false;
24848 if (next->type == CPP_COLON)
24850 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24851 *super = cp_parser_identifier (parser);
24853 else if (next->type == CPP_OPEN_PAREN)
24855 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24857 /* If there is no category name, and this is an @interface, we
24858 have a class extension. */
24859 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24861 *categ = NULL_TREE;
24862 *is_class_extension = true;
24864 else
24865 *categ = cp_parser_identifier (parser);
24867 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24871 /* Parse an Objective-C class interface. */
24873 static void
24874 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24876 tree name, super, categ, protos;
24877 bool is_class_extension;
24879 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
24880 name = cp_parser_identifier (parser);
24881 if (name == error_mark_node)
24883 /* It's hard to recover because even if valid @interface stuff
24884 is to follow, we can't compile it (or validate it) if we
24885 don't even know which class it refers to. Let's assume this
24886 was a stray '@interface' token in the stream and skip it.
24888 return;
24890 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24891 &is_class_extension);
24892 protos = cp_parser_objc_protocol_refs_opt (parser);
24894 /* We have either a class or a category on our hands. */
24895 if (categ || is_class_extension)
24896 objc_start_category_interface (name, categ, protos, attributes);
24897 else
24899 objc_start_class_interface (name, super, protos, attributes);
24900 /* Handle instance variable declarations, if any. */
24901 cp_parser_objc_class_ivars (parser);
24902 objc_continue_interface ();
24905 cp_parser_objc_method_prototype_list (parser);
24908 /* Parse an Objective-C class implementation. */
24910 static void
24911 cp_parser_objc_class_implementation (cp_parser* parser)
24913 tree name, super, categ;
24914 bool is_class_extension;
24916 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
24917 name = cp_parser_identifier (parser);
24918 if (name == error_mark_node)
24920 /* It's hard to recover because even if valid @implementation
24921 stuff is to follow, we can't compile it (or validate it) if
24922 we don't even know which class it refers to. Let's assume
24923 this was a stray '@implementation' token in the stream and
24924 skip it.
24926 return;
24928 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24929 &is_class_extension);
24931 /* We have either a class or a category on our hands. */
24932 if (categ)
24933 objc_start_category_implementation (name, categ);
24934 else
24936 objc_start_class_implementation (name, super);
24937 /* Handle instance variable declarations, if any. */
24938 cp_parser_objc_class_ivars (parser);
24939 objc_continue_implementation ();
24942 cp_parser_objc_method_definition_list (parser);
24945 /* Consume the @end token and finish off the implementation. */
24947 static void
24948 cp_parser_objc_end_implementation (cp_parser* parser)
24950 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24951 objc_finish_implementation ();
24954 /* Parse an Objective-C declaration. */
24956 static void
24957 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24959 /* Try to figure out what kind of declaration is present. */
24960 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24962 if (attributes)
24963 switch (kwd->keyword)
24965 case RID_AT_ALIAS:
24966 case RID_AT_CLASS:
24967 case RID_AT_END:
24968 error_at (kwd->location, "attributes may not be specified before"
24969 " the %<@%D%> Objective-C++ keyword",
24970 kwd->u.value);
24971 attributes = NULL;
24972 break;
24973 case RID_AT_IMPLEMENTATION:
24974 warning_at (kwd->location, OPT_Wattributes,
24975 "prefix attributes are ignored before %<@%D%>",
24976 kwd->u.value);
24977 attributes = NULL;
24978 default:
24979 break;
24982 switch (kwd->keyword)
24984 case RID_AT_ALIAS:
24985 cp_parser_objc_alias_declaration (parser);
24986 break;
24987 case RID_AT_CLASS:
24988 cp_parser_objc_class_declaration (parser);
24989 break;
24990 case RID_AT_PROTOCOL:
24991 cp_parser_objc_protocol_declaration (parser, attributes);
24992 break;
24993 case RID_AT_INTERFACE:
24994 cp_parser_objc_class_interface (parser, attributes);
24995 break;
24996 case RID_AT_IMPLEMENTATION:
24997 cp_parser_objc_class_implementation (parser);
24998 break;
24999 case RID_AT_END:
25000 cp_parser_objc_end_implementation (parser);
25001 break;
25002 default:
25003 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25004 kwd->u.value);
25005 cp_parser_skip_to_end_of_block_or_statement (parser);
25009 /* Parse an Objective-C try-catch-finally statement.
25011 objc-try-catch-finally-stmt:
25012 @try compound-statement objc-catch-clause-seq [opt]
25013 objc-finally-clause [opt]
25015 objc-catch-clause-seq:
25016 objc-catch-clause objc-catch-clause-seq [opt]
25018 objc-catch-clause:
25019 @catch ( objc-exception-declaration ) compound-statement
25021 objc-finally-clause:
25022 @finally compound-statement
25024 objc-exception-declaration:
25025 parameter-declaration
25026 '...'
25028 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
25030 Returns NULL_TREE.
25032 PS: This function is identical to c_parser_objc_try_catch_finally_statement
25033 for C. Keep them in sync. */
25035 static tree
25036 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
25038 location_t location;
25039 tree stmt;
25041 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
25042 location = cp_lexer_peek_token (parser->lexer)->location;
25043 objc_maybe_warn_exceptions (location);
25044 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
25045 node, lest it get absorbed into the surrounding block. */
25046 stmt = push_stmt_list ();
25047 cp_parser_compound_statement (parser, NULL, false, false);
25048 objc_begin_try_stmt (location, pop_stmt_list (stmt));
25050 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
25052 cp_parameter_declarator *parm;
25053 tree parameter_declaration = error_mark_node;
25054 bool seen_open_paren = false;
25056 cp_lexer_consume_token (parser->lexer);
25057 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25058 seen_open_paren = true;
25059 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25061 /* We have "@catch (...)" (where the '...' are literally
25062 what is in the code). Skip the '...'.
25063 parameter_declaration is set to NULL_TREE, and
25064 objc_being_catch_clauses() knows that that means
25065 '...'. */
25066 cp_lexer_consume_token (parser->lexer);
25067 parameter_declaration = NULL_TREE;
25069 else
25071 /* We have "@catch (NSException *exception)" or something
25072 like that. Parse the parameter declaration. */
25073 parm = cp_parser_parameter_declaration (parser, false, NULL);
25074 if (parm == NULL)
25075 parameter_declaration = error_mark_node;
25076 else
25077 parameter_declaration = grokdeclarator (parm->declarator,
25078 &parm->decl_specifiers,
25079 PARM, /*initialized=*/0,
25080 /*attrlist=*/NULL);
25082 if (seen_open_paren)
25083 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25084 else
25086 /* If there was no open parenthesis, we are recovering from
25087 an error, and we are trying to figure out what mistake
25088 the user has made. */
25090 /* If there is an immediate closing parenthesis, the user
25091 probably forgot the opening one (ie, they typed "@catch
25092 NSException *e)". Parse the closing parenthesis and keep
25093 going. */
25094 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25095 cp_lexer_consume_token (parser->lexer);
25097 /* If these is no immediate closing parenthesis, the user
25098 probably doesn't know that parenthesis are required at
25099 all (ie, they typed "@catch NSException *e"). So, just
25100 forget about the closing parenthesis and keep going. */
25102 objc_begin_catch_clause (parameter_declaration);
25103 cp_parser_compound_statement (parser, NULL, false, false);
25104 objc_finish_catch_clause ();
25106 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
25108 cp_lexer_consume_token (parser->lexer);
25109 location = cp_lexer_peek_token (parser->lexer)->location;
25110 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
25111 node, lest it get absorbed into the surrounding block. */
25112 stmt = push_stmt_list ();
25113 cp_parser_compound_statement (parser, NULL, false, false);
25114 objc_build_finally_clause (location, pop_stmt_list (stmt));
25117 return objc_finish_try_stmt ();
25120 /* Parse an Objective-C synchronized statement.
25122 objc-synchronized-stmt:
25123 @synchronized ( expression ) compound-statement
25125 Returns NULL_TREE. */
25127 static tree
25128 cp_parser_objc_synchronized_statement (cp_parser *parser)
25130 location_t location;
25131 tree lock, stmt;
25133 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
25135 location = cp_lexer_peek_token (parser->lexer)->location;
25136 objc_maybe_warn_exceptions (location);
25137 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25138 lock = cp_parser_expression (parser, false, NULL);
25139 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25141 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
25142 node, lest it get absorbed into the surrounding block. */
25143 stmt = push_stmt_list ();
25144 cp_parser_compound_statement (parser, NULL, false, false);
25146 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
25149 /* Parse an Objective-C throw statement.
25151 objc-throw-stmt:
25152 @throw assignment-expression [opt] ;
25154 Returns a constructed '@throw' statement. */
25156 static tree
25157 cp_parser_objc_throw_statement (cp_parser *parser)
25159 tree expr = NULL_TREE;
25160 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25162 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
25164 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25165 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25167 cp_parser_consume_semicolon_at_end_of_statement (parser);
25169 return objc_build_throw_stmt (loc, expr);
25172 /* Parse an Objective-C statement. */
25174 static tree
25175 cp_parser_objc_statement (cp_parser * parser)
25177 /* Try to figure out what kind of declaration is present. */
25178 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25180 switch (kwd->keyword)
25182 case RID_AT_TRY:
25183 return cp_parser_objc_try_catch_finally_statement (parser);
25184 case RID_AT_SYNCHRONIZED:
25185 return cp_parser_objc_synchronized_statement (parser);
25186 case RID_AT_THROW:
25187 return cp_parser_objc_throw_statement (parser);
25188 default:
25189 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25190 kwd->u.value);
25191 cp_parser_skip_to_end_of_block_or_statement (parser);
25194 return error_mark_node;
25197 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
25198 look ahead to see if an objc keyword follows the attributes. This
25199 is to detect the use of prefix attributes on ObjC @interface and
25200 @protocol. */
25202 static bool
25203 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
25205 cp_lexer_save_tokens (parser->lexer);
25206 *attrib = cp_parser_attributes_opt (parser);
25207 gcc_assert (*attrib);
25208 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
25210 cp_lexer_commit_tokens (parser->lexer);
25211 return true;
25213 cp_lexer_rollback_tokens (parser->lexer);
25214 return false;
25217 /* This routine is a minimal replacement for
25218 c_parser_struct_declaration () used when parsing the list of
25219 types/names or ObjC++ properties. For example, when parsing the
25220 code
25222 @property (readonly) int a, b, c;
25224 this function is responsible for parsing "int a, int b, int c" and
25225 returning the declarations as CHAIN of DECLs.
25227 TODO: Share this code with cp_parser_objc_class_ivars. It's very
25228 similar parsing. */
25229 static tree
25230 cp_parser_objc_struct_declaration (cp_parser *parser)
25232 tree decls = NULL_TREE;
25233 cp_decl_specifier_seq declspecs;
25234 int decl_class_or_enum_p;
25235 tree prefix_attributes;
25237 cp_parser_decl_specifier_seq (parser,
25238 CP_PARSER_FLAGS_NONE,
25239 &declspecs,
25240 &decl_class_or_enum_p);
25242 if (declspecs.type == error_mark_node)
25243 return error_mark_node;
25245 /* auto, register, static, extern, mutable. */
25246 if (declspecs.storage_class != sc_none)
25248 cp_parser_error (parser, "invalid type for property");
25249 declspecs.storage_class = sc_none;
25252 /* thread_local. */
25253 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
25255 cp_parser_error (parser, "invalid type for property");
25256 declspecs.locations[ds_thread] = 0;
25259 /* typedef. */
25260 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
25262 cp_parser_error (parser, "invalid type for property");
25263 declspecs.locations[ds_typedef] = 0;
25266 prefix_attributes = declspecs.attributes;
25267 declspecs.attributes = NULL_TREE;
25269 /* Keep going until we hit the `;' at the end of the declaration. */
25270 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25272 tree attributes, first_attribute, decl;
25273 cp_declarator *declarator;
25274 cp_token *token;
25276 /* Parse the declarator. */
25277 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25278 NULL, NULL, false);
25280 /* Look for attributes that apply to the ivar. */
25281 attributes = cp_parser_attributes_opt (parser);
25282 /* Remember which attributes are prefix attributes and
25283 which are not. */
25284 first_attribute = attributes;
25285 /* Combine the attributes. */
25286 attributes = chainon (prefix_attributes, attributes);
25288 decl = grokfield (declarator, &declspecs,
25289 NULL_TREE, /*init_const_expr_p=*/false,
25290 NULL_TREE, attributes);
25292 if (decl == error_mark_node || decl == NULL_TREE)
25293 return error_mark_node;
25295 /* Reset PREFIX_ATTRIBUTES. */
25296 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25297 attributes = TREE_CHAIN (attributes);
25298 if (attributes)
25299 TREE_CHAIN (attributes) = NULL_TREE;
25301 DECL_CHAIN (decl) = decls;
25302 decls = decl;
25304 token = cp_lexer_peek_token (parser->lexer);
25305 if (token->type == CPP_COMMA)
25307 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25308 continue;
25310 else
25311 break;
25313 return decls;
25316 /* Parse an Objective-C @property declaration. The syntax is:
25318 objc-property-declaration:
25319 '@property' objc-property-attributes[opt] struct-declaration ;
25321 objc-property-attributes:
25322 '(' objc-property-attribute-list ')'
25324 objc-property-attribute-list:
25325 objc-property-attribute
25326 objc-property-attribute-list, objc-property-attribute
25328 objc-property-attribute
25329 'getter' = identifier
25330 'setter' = identifier
25331 'readonly'
25332 'readwrite'
25333 'assign'
25334 'retain'
25335 'copy'
25336 'nonatomic'
25338 For example:
25339 @property NSString *name;
25340 @property (readonly) id object;
25341 @property (retain, nonatomic, getter=getTheName) id name;
25342 @property int a, b, c;
25344 PS: This function is identical to
25345 c_parser_objc_at_property_declaration for C. Keep them in sync. */
25346 static void
25347 cp_parser_objc_at_property_declaration (cp_parser *parser)
25349 /* The following variables hold the attributes of the properties as
25350 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
25351 seen. When we see an attribute, we set them to 'true' (if they
25352 are boolean properties) or to the identifier (if they have an
25353 argument, ie, for getter and setter). Note that here we only
25354 parse the list of attributes, check the syntax and accumulate the
25355 attributes that we find. objc_add_property_declaration() will
25356 then process the information. */
25357 bool property_assign = false;
25358 bool property_copy = false;
25359 tree property_getter_ident = NULL_TREE;
25360 bool property_nonatomic = false;
25361 bool property_readonly = false;
25362 bool property_readwrite = false;
25363 bool property_retain = false;
25364 tree property_setter_ident = NULL_TREE;
25366 /* 'properties' is the list of properties that we read. Usually a
25367 single one, but maybe more (eg, in "@property int a, b, c;" there
25368 are three). */
25369 tree properties;
25370 location_t loc;
25372 loc = cp_lexer_peek_token (parser->lexer)->location;
25374 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
25376 /* Parse the optional attribute list... */
25377 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25379 /* Eat the '('. */
25380 cp_lexer_consume_token (parser->lexer);
25382 while (true)
25384 bool syntax_error = false;
25385 cp_token *token = cp_lexer_peek_token (parser->lexer);
25386 enum rid keyword;
25388 if (token->type != CPP_NAME)
25390 cp_parser_error (parser, "expected identifier");
25391 break;
25393 keyword = C_RID_CODE (token->u.value);
25394 cp_lexer_consume_token (parser->lexer);
25395 switch (keyword)
25397 case RID_ASSIGN: property_assign = true; break;
25398 case RID_COPY: property_copy = true; break;
25399 case RID_NONATOMIC: property_nonatomic = true; break;
25400 case RID_READONLY: property_readonly = true; break;
25401 case RID_READWRITE: property_readwrite = true; break;
25402 case RID_RETAIN: property_retain = true; break;
25404 case RID_GETTER:
25405 case RID_SETTER:
25406 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
25408 if (keyword == RID_GETTER)
25409 cp_parser_error (parser,
25410 "missing %<=%> (after %<getter%> attribute)");
25411 else
25412 cp_parser_error (parser,
25413 "missing %<=%> (after %<setter%> attribute)");
25414 syntax_error = true;
25415 break;
25417 cp_lexer_consume_token (parser->lexer); /* eat the = */
25418 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
25420 cp_parser_error (parser, "expected identifier");
25421 syntax_error = true;
25422 break;
25424 if (keyword == RID_SETTER)
25426 if (property_setter_ident != NULL_TREE)
25428 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
25429 cp_lexer_consume_token (parser->lexer);
25431 else
25432 property_setter_ident = cp_parser_objc_selector (parser);
25433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25434 cp_parser_error (parser, "setter name must terminate with %<:%>");
25435 else
25436 cp_lexer_consume_token (parser->lexer);
25438 else
25440 if (property_getter_ident != NULL_TREE)
25442 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
25443 cp_lexer_consume_token (parser->lexer);
25445 else
25446 property_getter_ident = cp_parser_objc_selector (parser);
25448 break;
25449 default:
25450 cp_parser_error (parser, "unknown property attribute");
25451 syntax_error = true;
25452 break;
25455 if (syntax_error)
25456 break;
25458 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25459 cp_lexer_consume_token (parser->lexer);
25460 else
25461 break;
25464 /* FIXME: "@property (setter, assign);" will generate a spurious
25465 "error: expected ‘)’ before ‘,’ token". This is because
25466 cp_parser_require, unlike the C counterpart, will produce an
25467 error even if we are in error recovery. */
25468 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25470 cp_parser_skip_to_closing_parenthesis (parser,
25471 /*recovering=*/true,
25472 /*or_comma=*/false,
25473 /*consume_paren=*/true);
25477 /* ... and the property declaration(s). */
25478 properties = cp_parser_objc_struct_declaration (parser);
25480 if (properties == error_mark_node)
25482 cp_parser_skip_to_end_of_statement (parser);
25483 /* If the next token is now a `;', consume it. */
25484 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25485 cp_lexer_consume_token (parser->lexer);
25486 return;
25489 if (properties == NULL_TREE)
25490 cp_parser_error (parser, "expected identifier");
25491 else
25493 /* Comma-separated properties are chained together in
25494 reverse order; add them one by one. */
25495 properties = nreverse (properties);
25497 for (; properties; properties = TREE_CHAIN (properties))
25498 objc_add_property_declaration (loc, copy_node (properties),
25499 property_readonly, property_readwrite,
25500 property_assign, property_retain,
25501 property_copy, property_nonatomic,
25502 property_getter_ident, property_setter_ident);
25505 cp_parser_consume_semicolon_at_end_of_statement (parser);
25508 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
25510 objc-synthesize-declaration:
25511 @synthesize objc-synthesize-identifier-list ;
25513 objc-synthesize-identifier-list:
25514 objc-synthesize-identifier
25515 objc-synthesize-identifier-list, objc-synthesize-identifier
25517 objc-synthesize-identifier
25518 identifier
25519 identifier = identifier
25521 For example:
25522 @synthesize MyProperty;
25523 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
25525 PS: This function is identical to c_parser_objc_at_synthesize_declaration
25526 for C. Keep them in sync.
25528 static void
25529 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
25531 tree list = NULL_TREE;
25532 location_t loc;
25533 loc = cp_lexer_peek_token (parser->lexer)->location;
25535 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
25536 while (true)
25538 tree property, ivar;
25539 property = cp_parser_identifier (parser);
25540 if (property == error_mark_node)
25542 cp_parser_consume_semicolon_at_end_of_statement (parser);
25543 return;
25545 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25547 cp_lexer_consume_token (parser->lexer);
25548 ivar = cp_parser_identifier (parser);
25549 if (ivar == error_mark_node)
25551 cp_parser_consume_semicolon_at_end_of_statement (parser);
25552 return;
25555 else
25556 ivar = NULL_TREE;
25557 list = chainon (list, build_tree_list (ivar, property));
25558 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25559 cp_lexer_consume_token (parser->lexer);
25560 else
25561 break;
25563 cp_parser_consume_semicolon_at_end_of_statement (parser);
25564 objc_add_synthesize_declaration (loc, list);
25567 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
25569 objc-dynamic-declaration:
25570 @dynamic identifier-list ;
25572 For example:
25573 @dynamic MyProperty;
25574 @dynamic MyProperty, AnotherProperty;
25576 PS: This function is identical to c_parser_objc_at_dynamic_declaration
25577 for C. Keep them in sync.
25579 static void
25580 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
25582 tree list = NULL_TREE;
25583 location_t loc;
25584 loc = cp_lexer_peek_token (parser->lexer)->location;
25586 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
25587 while (true)
25589 tree property;
25590 property = cp_parser_identifier (parser);
25591 if (property == error_mark_node)
25593 cp_parser_consume_semicolon_at_end_of_statement (parser);
25594 return;
25596 list = chainon (list, build_tree_list (NULL, property));
25597 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25598 cp_lexer_consume_token (parser->lexer);
25599 else
25600 break;
25602 cp_parser_consume_semicolon_at_end_of_statement (parser);
25603 objc_add_dynamic_declaration (loc, list);
25607 /* OpenMP 2.5 parsing routines. */
25609 /* Returns name of the next clause.
25610 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
25611 the token is not consumed. Otherwise appropriate pragma_omp_clause is
25612 returned and the token is consumed. */
25614 static pragma_omp_clause
25615 cp_parser_omp_clause_name (cp_parser *parser)
25617 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
25619 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
25620 result = PRAGMA_OMP_CLAUSE_IF;
25621 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
25622 result = PRAGMA_OMP_CLAUSE_DEFAULT;
25623 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
25624 result = PRAGMA_OMP_CLAUSE_PRIVATE;
25625 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25627 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25628 const char *p = IDENTIFIER_POINTER (id);
25630 switch (p[0])
25632 case 'c':
25633 if (!strcmp ("collapse", p))
25634 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
25635 else if (!strcmp ("copyin", p))
25636 result = PRAGMA_OMP_CLAUSE_COPYIN;
25637 else if (!strcmp ("copyprivate", p))
25638 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
25639 break;
25640 case 'f':
25641 if (!strcmp ("final", p))
25642 result = PRAGMA_OMP_CLAUSE_FINAL;
25643 else if (!strcmp ("firstprivate", p))
25644 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
25645 break;
25646 case 'l':
25647 if (!strcmp ("lastprivate", p))
25648 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
25649 break;
25650 case 'm':
25651 if (!strcmp ("mergeable", p))
25652 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
25653 break;
25654 case 'n':
25655 if (!strcmp ("nowait", p))
25656 result = PRAGMA_OMP_CLAUSE_NOWAIT;
25657 else if (!strcmp ("num_threads", p))
25658 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
25659 break;
25660 case 'o':
25661 if (!strcmp ("ordered", p))
25662 result = PRAGMA_OMP_CLAUSE_ORDERED;
25663 break;
25664 case 'r':
25665 if (!strcmp ("reduction", p))
25666 result = PRAGMA_OMP_CLAUSE_REDUCTION;
25667 break;
25668 case 's':
25669 if (!strcmp ("schedule", p))
25670 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
25671 else if (!strcmp ("shared", p))
25672 result = PRAGMA_OMP_CLAUSE_SHARED;
25673 break;
25674 case 'u':
25675 if (!strcmp ("untied", p))
25676 result = PRAGMA_OMP_CLAUSE_UNTIED;
25677 break;
25681 if (result != PRAGMA_OMP_CLAUSE_NONE)
25682 cp_lexer_consume_token (parser->lexer);
25684 return result;
25687 /* Validate that a clause of the given type does not already exist. */
25689 static void
25690 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
25691 const char *name, location_t location)
25693 tree c;
25695 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25696 if (OMP_CLAUSE_CODE (c) == code)
25698 error_at (location, "too many %qs clauses", name);
25699 break;
25703 /* OpenMP 2.5:
25704 variable-list:
25705 identifier
25706 variable-list , identifier
25708 In addition, we match a closing parenthesis. An opening parenthesis
25709 will have been consumed by the caller.
25711 If KIND is nonzero, create the appropriate node and install the decl
25712 in OMP_CLAUSE_DECL and add the node to the head of the list.
25714 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25715 return the list created. */
25717 static tree
25718 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
25719 tree list)
25721 cp_token *token;
25722 while (1)
25724 tree name, decl;
25726 token = cp_lexer_peek_token (parser->lexer);
25727 name = cp_parser_id_expression (parser, /*template_p=*/false,
25728 /*check_dependency_p=*/true,
25729 /*template_p=*/NULL,
25730 /*declarator_p=*/false,
25731 /*optional_p=*/false);
25732 if (name == error_mark_node)
25733 goto skip_comma;
25735 decl = cp_parser_lookup_name_simple (parser, name, token->location);
25736 if (decl == error_mark_node)
25737 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
25738 token->location);
25739 else if (kind != 0)
25741 tree u = build_omp_clause (token->location, kind);
25742 OMP_CLAUSE_DECL (u) = decl;
25743 OMP_CLAUSE_CHAIN (u) = list;
25744 list = u;
25746 else
25747 list = tree_cons (decl, NULL_TREE, list);
25749 get_comma:
25750 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25751 break;
25752 cp_lexer_consume_token (parser->lexer);
25755 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25757 int ending;
25759 /* Try to resync to an unnested comma. Copied from
25760 cp_parser_parenthesized_expression_list. */
25761 skip_comma:
25762 ending = cp_parser_skip_to_closing_parenthesis (parser,
25763 /*recovering=*/true,
25764 /*or_comma=*/true,
25765 /*consume_paren=*/true);
25766 if (ending < 0)
25767 goto get_comma;
25770 return list;
25773 /* Similarly, but expect leading and trailing parenthesis. This is a very
25774 common case for omp clauses. */
25776 static tree
25777 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25779 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25780 return cp_parser_omp_var_list_no_open (parser, kind, list);
25781 return list;
25784 /* OpenMP 3.0:
25785 collapse ( constant-expression ) */
25787 static tree
25788 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25790 tree c, num;
25791 location_t loc;
25792 HOST_WIDE_INT n;
25794 loc = cp_lexer_peek_token (parser->lexer)->location;
25795 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25796 return list;
25798 num = cp_parser_constant_expression (parser, false, NULL);
25800 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25801 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25802 /*or_comma=*/false,
25803 /*consume_paren=*/true);
25805 if (num == error_mark_node)
25806 return list;
25807 num = fold_non_dependent_expr (num);
25808 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25809 || !host_integerp (num, 0)
25810 || (n = tree_low_cst (num, 0)) <= 0
25811 || (int) n != n)
25813 error_at (loc, "collapse argument needs positive constant integer expression");
25814 return list;
25817 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25818 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25819 OMP_CLAUSE_CHAIN (c) = list;
25820 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25822 return c;
25825 /* OpenMP 2.5:
25826 default ( shared | none ) */
25828 static tree
25829 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25831 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25832 tree c;
25834 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25835 return list;
25836 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25838 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25839 const char *p = IDENTIFIER_POINTER (id);
25841 switch (p[0])
25843 case 'n':
25844 if (strcmp ("none", p) != 0)
25845 goto invalid_kind;
25846 kind = OMP_CLAUSE_DEFAULT_NONE;
25847 break;
25849 case 's':
25850 if (strcmp ("shared", p) != 0)
25851 goto invalid_kind;
25852 kind = OMP_CLAUSE_DEFAULT_SHARED;
25853 break;
25855 default:
25856 goto invalid_kind;
25859 cp_lexer_consume_token (parser->lexer);
25861 else
25863 invalid_kind:
25864 cp_parser_error (parser, "expected %<none%> or %<shared%>");
25867 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25868 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25869 /*or_comma=*/false,
25870 /*consume_paren=*/true);
25872 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25873 return list;
25875 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25876 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25877 OMP_CLAUSE_CHAIN (c) = list;
25878 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25880 return c;
25883 /* OpenMP 3.1:
25884 final ( expression ) */
25886 static tree
25887 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25889 tree t, c;
25891 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25892 return list;
25894 t = cp_parser_condition (parser);
25896 if (t == error_mark_node
25897 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25898 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25899 /*or_comma=*/false,
25900 /*consume_paren=*/true);
25902 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25904 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25905 OMP_CLAUSE_FINAL_EXPR (c) = t;
25906 OMP_CLAUSE_CHAIN (c) = list;
25908 return c;
25911 /* OpenMP 2.5:
25912 if ( expression ) */
25914 static tree
25915 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25917 tree t, c;
25919 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25920 return list;
25922 t = cp_parser_condition (parser);
25924 if (t == error_mark_node
25925 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25926 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25927 /*or_comma=*/false,
25928 /*consume_paren=*/true);
25930 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25932 c = build_omp_clause (location, OMP_CLAUSE_IF);
25933 OMP_CLAUSE_IF_EXPR (c) = t;
25934 OMP_CLAUSE_CHAIN (c) = list;
25936 return c;
25939 /* OpenMP 3.1:
25940 mergeable */
25942 static tree
25943 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
25944 tree list, location_t location)
25946 tree c;
25948 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25949 location);
25951 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25952 OMP_CLAUSE_CHAIN (c) = list;
25953 return c;
25956 /* OpenMP 2.5:
25957 nowait */
25959 static tree
25960 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
25961 tree list, location_t location)
25963 tree c;
25965 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25967 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25968 OMP_CLAUSE_CHAIN (c) = list;
25969 return c;
25972 /* OpenMP 2.5:
25973 num_threads ( expression ) */
25975 static tree
25976 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25977 location_t location)
25979 tree t, c;
25981 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25982 return list;
25984 t = cp_parser_expression (parser, false, NULL);
25986 if (t == error_mark_node
25987 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25988 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25989 /*or_comma=*/false,
25990 /*consume_paren=*/true);
25992 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25993 "num_threads", location);
25995 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25996 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25997 OMP_CLAUSE_CHAIN (c) = list;
25999 return c;
26002 /* OpenMP 2.5:
26003 ordered */
26005 static tree
26006 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
26007 tree list, location_t location)
26009 tree c;
26011 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
26012 "ordered", location);
26014 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
26015 OMP_CLAUSE_CHAIN (c) = list;
26016 return c;
26019 /* OpenMP 2.5:
26020 reduction ( reduction-operator : variable-list )
26022 reduction-operator:
26023 One of: + * - & ^ | && ||
26025 OpenMP 3.1:
26027 reduction-operator:
26028 One of: + * - & ^ | && || min max */
26030 static tree
26031 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
26033 enum tree_code code;
26034 tree nlist, c;
26036 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26037 return list;
26039 switch (cp_lexer_peek_token (parser->lexer)->type)
26041 case CPP_PLUS:
26042 code = PLUS_EXPR;
26043 break;
26044 case CPP_MULT:
26045 code = MULT_EXPR;
26046 break;
26047 case CPP_MINUS:
26048 code = MINUS_EXPR;
26049 break;
26050 case CPP_AND:
26051 code = BIT_AND_EXPR;
26052 break;
26053 case CPP_XOR:
26054 code = BIT_XOR_EXPR;
26055 break;
26056 case CPP_OR:
26057 code = BIT_IOR_EXPR;
26058 break;
26059 case CPP_AND_AND:
26060 code = TRUTH_ANDIF_EXPR;
26061 break;
26062 case CPP_OR_OR:
26063 code = TRUTH_ORIF_EXPR;
26064 break;
26065 case CPP_NAME:
26067 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26068 const char *p = IDENTIFIER_POINTER (id);
26070 if (strcmp (p, "min") == 0)
26072 code = MIN_EXPR;
26073 break;
26075 if (strcmp (p, "max") == 0)
26077 code = MAX_EXPR;
26078 break;
26081 /* FALLTHROUGH */
26082 default:
26083 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
26084 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
26085 resync_fail:
26086 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26087 /*or_comma=*/false,
26088 /*consume_paren=*/true);
26089 return list;
26091 cp_lexer_consume_token (parser->lexer);
26093 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26094 goto resync_fail;
26096 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
26097 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
26098 OMP_CLAUSE_REDUCTION_CODE (c) = code;
26100 return nlist;
26103 /* OpenMP 2.5:
26104 schedule ( schedule-kind )
26105 schedule ( schedule-kind , expression )
26107 schedule-kind:
26108 static | dynamic | guided | runtime | auto */
26110 static tree
26111 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
26113 tree c, t;
26115 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26116 return list;
26118 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
26120 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26122 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26123 const char *p = IDENTIFIER_POINTER (id);
26125 switch (p[0])
26127 case 'd':
26128 if (strcmp ("dynamic", p) != 0)
26129 goto invalid_kind;
26130 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
26131 break;
26133 case 'g':
26134 if (strcmp ("guided", p) != 0)
26135 goto invalid_kind;
26136 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
26137 break;
26139 case 'r':
26140 if (strcmp ("runtime", p) != 0)
26141 goto invalid_kind;
26142 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
26143 break;
26145 default:
26146 goto invalid_kind;
26149 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
26150 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
26151 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
26152 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
26153 else
26154 goto invalid_kind;
26155 cp_lexer_consume_token (parser->lexer);
26157 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26159 cp_token *token;
26160 cp_lexer_consume_token (parser->lexer);
26162 token = cp_lexer_peek_token (parser->lexer);
26163 t = cp_parser_assignment_expression (parser, false, NULL);
26165 if (t == error_mark_node)
26166 goto resync_fail;
26167 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
26168 error_at (token->location, "schedule %<runtime%> does not take "
26169 "a %<chunk_size%> parameter");
26170 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
26171 error_at (token->location, "schedule %<auto%> does not take "
26172 "a %<chunk_size%> parameter");
26173 else
26174 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
26176 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26177 goto resync_fail;
26179 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
26180 goto resync_fail;
26182 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
26183 OMP_CLAUSE_CHAIN (c) = list;
26184 return c;
26186 invalid_kind:
26187 cp_parser_error (parser, "invalid schedule kind");
26188 resync_fail:
26189 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26190 /*or_comma=*/false,
26191 /*consume_paren=*/true);
26192 return list;
26195 /* OpenMP 3.0:
26196 untied */
26198 static tree
26199 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
26200 tree list, location_t location)
26202 tree c;
26204 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
26206 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
26207 OMP_CLAUSE_CHAIN (c) = list;
26208 return c;
26211 /* Parse all OpenMP clauses. The set clauses allowed by the directive
26212 is a bitmask in MASK. Return the list of clauses found; the result
26213 of clause default goes in *pdefault. */
26215 static tree
26216 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
26217 const char *where, cp_token *pragma_tok)
26219 tree clauses = NULL;
26220 bool first = true;
26221 cp_token *token = NULL;
26223 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
26225 pragma_omp_clause c_kind;
26226 const char *c_name;
26227 tree prev = clauses;
26229 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26230 cp_lexer_consume_token (parser->lexer);
26232 token = cp_lexer_peek_token (parser->lexer);
26233 c_kind = cp_parser_omp_clause_name (parser);
26234 first = false;
26236 switch (c_kind)
26238 case PRAGMA_OMP_CLAUSE_COLLAPSE:
26239 clauses = cp_parser_omp_clause_collapse (parser, clauses,
26240 token->location);
26241 c_name = "collapse";
26242 break;
26243 case PRAGMA_OMP_CLAUSE_COPYIN:
26244 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
26245 c_name = "copyin";
26246 break;
26247 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
26248 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
26249 clauses);
26250 c_name = "copyprivate";
26251 break;
26252 case PRAGMA_OMP_CLAUSE_DEFAULT:
26253 clauses = cp_parser_omp_clause_default (parser, clauses,
26254 token->location);
26255 c_name = "default";
26256 break;
26257 case PRAGMA_OMP_CLAUSE_FINAL:
26258 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
26259 c_name = "final";
26260 break;
26261 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
26262 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
26263 clauses);
26264 c_name = "firstprivate";
26265 break;
26266 case PRAGMA_OMP_CLAUSE_IF:
26267 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
26268 c_name = "if";
26269 break;
26270 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
26271 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
26272 clauses);
26273 c_name = "lastprivate";
26274 break;
26275 case PRAGMA_OMP_CLAUSE_MERGEABLE:
26276 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
26277 token->location);
26278 c_name = "mergeable";
26279 break;
26280 case PRAGMA_OMP_CLAUSE_NOWAIT:
26281 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
26282 c_name = "nowait";
26283 break;
26284 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
26285 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
26286 token->location);
26287 c_name = "num_threads";
26288 break;
26289 case PRAGMA_OMP_CLAUSE_ORDERED:
26290 clauses = cp_parser_omp_clause_ordered (parser, clauses,
26291 token->location);
26292 c_name = "ordered";
26293 break;
26294 case PRAGMA_OMP_CLAUSE_PRIVATE:
26295 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
26296 clauses);
26297 c_name = "private";
26298 break;
26299 case PRAGMA_OMP_CLAUSE_REDUCTION:
26300 clauses = cp_parser_omp_clause_reduction (parser, clauses);
26301 c_name = "reduction";
26302 break;
26303 case PRAGMA_OMP_CLAUSE_SCHEDULE:
26304 clauses = cp_parser_omp_clause_schedule (parser, clauses,
26305 token->location);
26306 c_name = "schedule";
26307 break;
26308 case PRAGMA_OMP_CLAUSE_SHARED:
26309 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
26310 clauses);
26311 c_name = "shared";
26312 break;
26313 case PRAGMA_OMP_CLAUSE_UNTIED:
26314 clauses = cp_parser_omp_clause_untied (parser, clauses,
26315 token->location);
26316 c_name = "nowait";
26317 break;
26318 default:
26319 cp_parser_error (parser, "expected %<#pragma omp%> clause");
26320 goto saw_error;
26323 if (((mask >> c_kind) & 1) == 0)
26325 /* Remove the invalid clause(s) from the list to avoid
26326 confusing the rest of the compiler. */
26327 clauses = prev;
26328 error_at (token->location, "%qs is not valid for %qs", c_name, where);
26331 saw_error:
26332 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
26333 return finish_omp_clauses (clauses);
26336 /* OpenMP 2.5:
26337 structured-block:
26338 statement
26340 In practice, we're also interested in adding the statement to an
26341 outer node. So it is convenient if we work around the fact that
26342 cp_parser_statement calls add_stmt. */
26344 static unsigned
26345 cp_parser_begin_omp_structured_block (cp_parser *parser)
26347 unsigned save = parser->in_statement;
26349 /* Only move the values to IN_OMP_BLOCK if they weren't false.
26350 This preserves the "not within loop or switch" style error messages
26351 for nonsense cases like
26352 void foo() {
26353 #pragma omp single
26354 break;
26357 if (parser->in_statement)
26358 parser->in_statement = IN_OMP_BLOCK;
26360 return save;
26363 static void
26364 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
26366 parser->in_statement = save;
26369 static tree
26370 cp_parser_omp_structured_block (cp_parser *parser)
26372 tree stmt = begin_omp_structured_block ();
26373 unsigned int save = cp_parser_begin_omp_structured_block (parser);
26375 cp_parser_statement (parser, NULL_TREE, false, NULL);
26377 cp_parser_end_omp_structured_block (parser, save);
26378 return finish_omp_structured_block (stmt);
26381 /* OpenMP 2.5:
26382 # pragma omp atomic new-line
26383 expression-stmt
26385 expression-stmt:
26386 x binop= expr | x++ | ++x | x-- | --x
26387 binop:
26388 +, *, -, /, &, ^, |, <<, >>
26390 where x is an lvalue expression with scalar type.
26392 OpenMP 3.1:
26393 # pragma omp atomic new-line
26394 update-stmt
26396 # pragma omp atomic read new-line
26397 read-stmt
26399 # pragma omp atomic write new-line
26400 write-stmt
26402 # pragma omp atomic update new-line
26403 update-stmt
26405 # pragma omp atomic capture new-line
26406 capture-stmt
26408 # pragma omp atomic capture new-line
26409 capture-block
26411 read-stmt:
26412 v = x
26413 write-stmt:
26414 x = expr
26415 update-stmt:
26416 expression-stmt | x = x binop expr
26417 capture-stmt:
26418 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
26419 capture-block:
26420 { v = x; update-stmt; } | { update-stmt; v = x; }
26422 where x and v are lvalue expressions with scalar type. */
26424 static void
26425 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
26427 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
26428 tree rhs1 = NULL_TREE, orig_lhs;
26429 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
26430 bool structured_block = false;
26432 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26434 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26435 const char *p = IDENTIFIER_POINTER (id);
26437 if (!strcmp (p, "read"))
26438 code = OMP_ATOMIC_READ;
26439 else if (!strcmp (p, "write"))
26440 code = NOP_EXPR;
26441 else if (!strcmp (p, "update"))
26442 code = OMP_ATOMIC;
26443 else if (!strcmp (p, "capture"))
26444 code = OMP_ATOMIC_CAPTURE_NEW;
26445 else
26446 p = NULL;
26447 if (p)
26448 cp_lexer_consume_token (parser->lexer);
26450 cp_parser_require_pragma_eol (parser, pragma_tok);
26452 switch (code)
26454 case OMP_ATOMIC_READ:
26455 case NOP_EXPR: /* atomic write */
26456 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26457 /*cast_p=*/false, NULL);
26458 if (v == error_mark_node)
26459 goto saw_error;
26460 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26461 goto saw_error;
26462 if (code == NOP_EXPR)
26463 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26464 else
26465 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26466 /*cast_p=*/false, NULL);
26467 if (lhs == error_mark_node)
26468 goto saw_error;
26469 if (code == NOP_EXPR)
26471 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
26472 opcode. */
26473 code = OMP_ATOMIC;
26474 rhs = lhs;
26475 lhs = v;
26476 v = NULL_TREE;
26478 goto done;
26479 case OMP_ATOMIC_CAPTURE_NEW:
26480 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26482 cp_lexer_consume_token (parser->lexer);
26483 structured_block = true;
26485 else
26487 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26488 /*cast_p=*/false, NULL);
26489 if (v == error_mark_node)
26490 goto saw_error;
26491 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26492 goto saw_error;
26494 default:
26495 break;
26498 restart:
26499 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26500 /*cast_p=*/false, NULL);
26501 orig_lhs = lhs;
26502 switch (TREE_CODE (lhs))
26504 case ERROR_MARK:
26505 goto saw_error;
26507 case POSTINCREMENT_EXPR:
26508 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26509 code = OMP_ATOMIC_CAPTURE_OLD;
26510 /* FALLTHROUGH */
26511 case PREINCREMENT_EXPR:
26512 lhs = TREE_OPERAND (lhs, 0);
26513 opcode = PLUS_EXPR;
26514 rhs = integer_one_node;
26515 break;
26517 case POSTDECREMENT_EXPR:
26518 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26519 code = OMP_ATOMIC_CAPTURE_OLD;
26520 /* FALLTHROUGH */
26521 case PREDECREMENT_EXPR:
26522 lhs = TREE_OPERAND (lhs, 0);
26523 opcode = MINUS_EXPR;
26524 rhs = integer_one_node;
26525 break;
26527 case COMPOUND_EXPR:
26528 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
26529 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
26530 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
26531 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
26532 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
26533 (TREE_OPERAND (lhs, 1), 0), 0)))
26534 == BOOLEAN_TYPE)
26535 /* Undo effects of boolean_increment for post {in,de}crement. */
26536 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
26537 /* FALLTHRU */
26538 case MODIFY_EXPR:
26539 if (TREE_CODE (lhs) == MODIFY_EXPR
26540 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
26542 /* Undo effects of boolean_increment. */
26543 if (integer_onep (TREE_OPERAND (lhs, 1)))
26545 /* This is pre or post increment. */
26546 rhs = TREE_OPERAND (lhs, 1);
26547 lhs = TREE_OPERAND (lhs, 0);
26548 opcode = NOP_EXPR;
26549 if (code == OMP_ATOMIC_CAPTURE_NEW
26550 && !structured_block
26551 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
26552 code = OMP_ATOMIC_CAPTURE_OLD;
26553 break;
26556 /* FALLTHRU */
26557 default:
26558 switch (cp_lexer_peek_token (parser->lexer)->type)
26560 case CPP_MULT_EQ:
26561 opcode = MULT_EXPR;
26562 break;
26563 case CPP_DIV_EQ:
26564 opcode = TRUNC_DIV_EXPR;
26565 break;
26566 case CPP_PLUS_EQ:
26567 opcode = PLUS_EXPR;
26568 break;
26569 case CPP_MINUS_EQ:
26570 opcode = MINUS_EXPR;
26571 break;
26572 case CPP_LSHIFT_EQ:
26573 opcode = LSHIFT_EXPR;
26574 break;
26575 case CPP_RSHIFT_EQ:
26576 opcode = RSHIFT_EXPR;
26577 break;
26578 case CPP_AND_EQ:
26579 opcode = BIT_AND_EXPR;
26580 break;
26581 case CPP_OR_EQ:
26582 opcode = BIT_IOR_EXPR;
26583 break;
26584 case CPP_XOR_EQ:
26585 opcode = BIT_XOR_EXPR;
26586 break;
26587 case CPP_EQ:
26588 if (structured_block || code == OMP_ATOMIC)
26590 enum cp_parser_prec oprec;
26591 cp_token *token;
26592 cp_lexer_consume_token (parser->lexer);
26593 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26594 /*cast_p=*/false, NULL);
26595 if (rhs1 == error_mark_node)
26596 goto saw_error;
26597 token = cp_lexer_peek_token (parser->lexer);
26598 switch (token->type)
26600 case CPP_SEMICOLON:
26601 if (code == OMP_ATOMIC_CAPTURE_NEW)
26603 code = OMP_ATOMIC_CAPTURE_OLD;
26604 v = lhs;
26605 lhs = NULL_TREE;
26606 lhs1 = rhs1;
26607 rhs1 = NULL_TREE;
26608 cp_lexer_consume_token (parser->lexer);
26609 goto restart;
26611 cp_parser_error (parser,
26612 "invalid form of %<#pragma omp atomic%>");
26613 goto saw_error;
26614 case CPP_MULT:
26615 opcode = MULT_EXPR;
26616 break;
26617 case CPP_DIV:
26618 opcode = TRUNC_DIV_EXPR;
26619 break;
26620 case CPP_PLUS:
26621 opcode = PLUS_EXPR;
26622 break;
26623 case CPP_MINUS:
26624 opcode = MINUS_EXPR;
26625 break;
26626 case CPP_LSHIFT:
26627 opcode = LSHIFT_EXPR;
26628 break;
26629 case CPP_RSHIFT:
26630 opcode = RSHIFT_EXPR;
26631 break;
26632 case CPP_AND:
26633 opcode = BIT_AND_EXPR;
26634 break;
26635 case CPP_OR:
26636 opcode = BIT_IOR_EXPR;
26637 break;
26638 case CPP_XOR:
26639 opcode = BIT_XOR_EXPR;
26640 break;
26641 default:
26642 cp_parser_error (parser,
26643 "invalid operator for %<#pragma omp atomic%>");
26644 goto saw_error;
26646 oprec = TOKEN_PRECEDENCE (token);
26647 gcc_assert (oprec != PREC_NOT_OPERATOR);
26648 if (commutative_tree_code (opcode))
26649 oprec = (enum cp_parser_prec) (oprec - 1);
26650 cp_lexer_consume_token (parser->lexer);
26651 rhs = cp_parser_binary_expression (parser, false, false,
26652 oprec, NULL);
26653 if (rhs == error_mark_node)
26654 goto saw_error;
26655 goto stmt_done;
26657 /* FALLTHROUGH */
26658 default:
26659 cp_parser_error (parser,
26660 "invalid operator for %<#pragma omp atomic%>");
26661 goto saw_error;
26663 cp_lexer_consume_token (parser->lexer);
26665 rhs = cp_parser_expression (parser, false, NULL);
26666 if (rhs == error_mark_node)
26667 goto saw_error;
26668 break;
26670 stmt_done:
26671 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
26673 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26674 goto saw_error;
26675 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26676 /*cast_p=*/false, NULL);
26677 if (v == error_mark_node)
26678 goto saw_error;
26679 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26680 goto saw_error;
26681 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26682 /*cast_p=*/false, NULL);
26683 if (lhs1 == error_mark_node)
26684 goto saw_error;
26686 if (structured_block)
26688 cp_parser_consume_semicolon_at_end_of_statement (parser);
26689 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26691 done:
26692 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
26693 if (!structured_block)
26694 cp_parser_consume_semicolon_at_end_of_statement (parser);
26695 return;
26697 saw_error:
26698 cp_parser_skip_to_end_of_block_or_statement (parser);
26699 if (structured_block)
26701 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26702 cp_lexer_consume_token (parser->lexer);
26703 else if (code == OMP_ATOMIC_CAPTURE_NEW)
26705 cp_parser_skip_to_end_of_block_or_statement (parser);
26706 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26707 cp_lexer_consume_token (parser->lexer);
26713 /* OpenMP 2.5:
26714 # pragma omp barrier new-line */
26716 static void
26717 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
26719 cp_parser_require_pragma_eol (parser, pragma_tok);
26720 finish_omp_barrier ();
26723 /* OpenMP 2.5:
26724 # pragma omp critical [(name)] new-line
26725 structured-block */
26727 static tree
26728 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
26730 tree stmt, name = NULL;
26732 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26734 cp_lexer_consume_token (parser->lexer);
26736 name = cp_parser_identifier (parser);
26738 if (name == error_mark_node
26739 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26740 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26741 /*or_comma=*/false,
26742 /*consume_paren=*/true);
26743 if (name == error_mark_node)
26744 name = NULL;
26746 cp_parser_require_pragma_eol (parser, pragma_tok);
26748 stmt = cp_parser_omp_structured_block (parser);
26749 return c_finish_omp_critical (input_location, stmt, name);
26752 /* OpenMP 2.5:
26753 # pragma omp flush flush-vars[opt] new-line
26755 flush-vars:
26756 ( variable-list ) */
26758 static void
26759 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26761 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26762 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26763 cp_parser_require_pragma_eol (parser, pragma_tok);
26765 finish_omp_flush ();
26768 /* Helper function, to parse omp for increment expression. */
26770 static tree
26771 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26773 tree cond = cp_parser_binary_expression (parser, false, true,
26774 PREC_NOT_OPERATOR, NULL);
26775 if (cond == error_mark_node
26776 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26778 cp_parser_skip_to_end_of_statement (parser);
26779 return error_mark_node;
26782 switch (TREE_CODE (cond))
26784 case GT_EXPR:
26785 case GE_EXPR:
26786 case LT_EXPR:
26787 case LE_EXPR:
26788 break;
26789 default:
26790 return error_mark_node;
26793 /* If decl is an iterator, preserve LHS and RHS of the relational
26794 expr until finish_omp_for. */
26795 if (decl
26796 && (type_dependent_expression_p (decl)
26797 || CLASS_TYPE_P (TREE_TYPE (decl))))
26798 return cond;
26800 return build_x_binary_op (input_location, TREE_CODE (cond),
26801 TREE_OPERAND (cond, 0), ERROR_MARK,
26802 TREE_OPERAND (cond, 1), ERROR_MARK,
26803 /*overload=*/NULL, tf_warning_or_error);
26806 /* Helper function, to parse omp for increment expression. */
26808 static tree
26809 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26811 cp_token *token = cp_lexer_peek_token (parser->lexer);
26812 enum tree_code op;
26813 tree lhs, rhs;
26814 cp_id_kind idk;
26815 bool decl_first;
26817 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26819 op = (token->type == CPP_PLUS_PLUS
26820 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26821 cp_lexer_consume_token (parser->lexer);
26822 lhs = cp_parser_cast_expression (parser, false, false, NULL);
26823 if (lhs != decl)
26824 return error_mark_node;
26825 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26828 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26829 if (lhs != decl)
26830 return error_mark_node;
26832 token = cp_lexer_peek_token (parser->lexer);
26833 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26835 op = (token->type == CPP_PLUS_PLUS
26836 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26837 cp_lexer_consume_token (parser->lexer);
26838 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26841 op = cp_parser_assignment_operator_opt (parser);
26842 if (op == ERROR_MARK)
26843 return error_mark_node;
26845 if (op != NOP_EXPR)
26847 rhs = cp_parser_assignment_expression (parser, false, NULL);
26848 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26849 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26852 lhs = cp_parser_binary_expression (parser, false, false,
26853 PREC_ADDITIVE_EXPRESSION, NULL);
26854 token = cp_lexer_peek_token (parser->lexer);
26855 decl_first = lhs == decl;
26856 if (decl_first)
26857 lhs = NULL_TREE;
26858 if (token->type != CPP_PLUS
26859 && token->type != CPP_MINUS)
26860 return error_mark_node;
26864 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26865 cp_lexer_consume_token (parser->lexer);
26866 rhs = cp_parser_binary_expression (parser, false, false,
26867 PREC_ADDITIVE_EXPRESSION, NULL);
26868 token = cp_lexer_peek_token (parser->lexer);
26869 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26871 if (lhs == NULL_TREE)
26873 if (op == PLUS_EXPR)
26874 lhs = rhs;
26875 else
26876 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
26877 tf_warning_or_error);
26879 else
26880 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
26881 ERROR_MARK, NULL, tf_warning_or_error);
26884 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26886 if (!decl_first)
26888 if (rhs != decl || op == MINUS_EXPR)
26889 return error_mark_node;
26890 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26892 else
26893 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26895 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26898 /* Parse the restricted form of the for statement allowed by OpenMP. */
26900 static tree
26901 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26903 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26904 tree real_decl, initv, condv, incrv, declv;
26905 tree this_pre_body, cl;
26906 location_t loc_first;
26907 bool collapse_err = false;
26908 int i, collapse = 1, nbraces = 0;
26909 vec<tree, va_gc> *for_block = make_tree_vector ();
26911 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26912 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26913 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26915 gcc_assert (collapse >= 1);
26917 declv = make_tree_vec (collapse);
26918 initv = make_tree_vec (collapse);
26919 condv = make_tree_vec (collapse);
26920 incrv = make_tree_vec (collapse);
26922 loc_first = cp_lexer_peek_token (parser->lexer)->location;
26924 for (i = 0; i < collapse; i++)
26926 int bracecount = 0;
26927 bool add_private_clause = false;
26928 location_t loc;
26930 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26932 cp_parser_error (parser, "for statement expected");
26933 return NULL;
26935 loc = cp_lexer_consume_token (parser->lexer)->location;
26937 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26938 return NULL;
26940 init = decl = real_decl = NULL;
26941 this_pre_body = push_stmt_list ();
26942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26944 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26946 init-expr:
26947 var = lb
26948 integer-type var = lb
26949 random-access-iterator-type var = lb
26950 pointer-type var = lb
26952 cp_decl_specifier_seq type_specifiers;
26954 /* First, try to parse as an initialized declaration. See
26955 cp_parser_condition, from whence the bulk of this is copied. */
26957 cp_parser_parse_tentatively (parser);
26958 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26959 /*is_trailing_return=*/false,
26960 &type_specifiers);
26961 if (cp_parser_parse_definitely (parser))
26963 /* If parsing a type specifier seq succeeded, then this
26964 MUST be a initialized declaration. */
26965 tree asm_specification, attributes;
26966 cp_declarator *declarator;
26968 declarator = cp_parser_declarator (parser,
26969 CP_PARSER_DECLARATOR_NAMED,
26970 /*ctor_dtor_or_conv_p=*/NULL,
26971 /*parenthesized_p=*/NULL,
26972 /*member_p=*/false);
26973 attributes = cp_parser_attributes_opt (parser);
26974 asm_specification = cp_parser_asm_specification_opt (parser);
26976 if (declarator == cp_error_declarator)
26977 cp_parser_skip_to_end_of_statement (parser);
26979 else
26981 tree pushed_scope, auto_node;
26983 decl = start_decl (declarator, &type_specifiers,
26984 SD_INITIALIZED, attributes,
26985 /*prefix_attributes=*/NULL_TREE,
26986 &pushed_scope);
26988 auto_node = type_uses_auto (TREE_TYPE (decl));
26989 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26991 if (cp_lexer_next_token_is (parser->lexer,
26992 CPP_OPEN_PAREN))
26993 error ("parenthesized initialization is not allowed in "
26994 "OpenMP %<for%> loop");
26995 else
26996 /* Trigger an error. */
26997 cp_parser_require (parser, CPP_EQ, RT_EQ);
26999 init = error_mark_node;
27000 cp_parser_skip_to_end_of_statement (parser);
27002 else if (CLASS_TYPE_P (TREE_TYPE (decl))
27003 || type_dependent_expression_p (decl)
27004 || auto_node)
27006 bool is_direct_init, is_non_constant_init;
27008 init = cp_parser_initializer (parser,
27009 &is_direct_init,
27010 &is_non_constant_init);
27012 if (auto_node)
27014 TREE_TYPE (decl)
27015 = do_auto_deduction (TREE_TYPE (decl), init,
27016 auto_node);
27018 if (!CLASS_TYPE_P (TREE_TYPE (decl))
27019 && !type_dependent_expression_p (decl))
27020 goto non_class;
27023 cp_finish_decl (decl, init, !is_non_constant_init,
27024 asm_specification,
27025 LOOKUP_ONLYCONVERTING);
27026 if (CLASS_TYPE_P (TREE_TYPE (decl)))
27028 vec_safe_push (for_block, this_pre_body);
27029 init = NULL_TREE;
27031 else
27032 init = pop_stmt_list (this_pre_body);
27033 this_pre_body = NULL_TREE;
27035 else
27037 /* Consume '='. */
27038 cp_lexer_consume_token (parser->lexer);
27039 init = cp_parser_assignment_expression (parser, false, NULL);
27041 non_class:
27042 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
27043 init = error_mark_node;
27044 else
27045 cp_finish_decl (decl, NULL_TREE,
27046 /*init_const_expr_p=*/false,
27047 asm_specification,
27048 LOOKUP_ONLYCONVERTING);
27051 if (pushed_scope)
27052 pop_scope (pushed_scope);
27055 else
27057 cp_id_kind idk;
27058 /* If parsing a type specifier sequence failed, then
27059 this MUST be a simple expression. */
27060 cp_parser_parse_tentatively (parser);
27061 decl = cp_parser_primary_expression (parser, false, false,
27062 false, &idk);
27063 if (!cp_parser_error_occurred (parser)
27064 && decl
27065 && DECL_P (decl)
27066 && CLASS_TYPE_P (TREE_TYPE (decl)))
27068 tree rhs;
27070 cp_parser_parse_definitely (parser);
27071 cp_parser_require (parser, CPP_EQ, RT_EQ);
27072 rhs = cp_parser_assignment_expression (parser, false, NULL);
27073 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
27074 decl, NOP_EXPR,
27075 rhs,
27076 tf_warning_or_error));
27077 add_private_clause = true;
27079 else
27081 decl = NULL;
27082 cp_parser_abort_tentative_parse (parser);
27083 init = cp_parser_expression (parser, false, NULL);
27084 if (init)
27086 if (TREE_CODE (init) == MODIFY_EXPR
27087 || TREE_CODE (init) == MODOP_EXPR)
27088 real_decl = TREE_OPERAND (init, 0);
27093 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27094 if (this_pre_body)
27096 this_pre_body = pop_stmt_list (this_pre_body);
27097 if (pre_body)
27099 tree t = pre_body;
27100 pre_body = push_stmt_list ();
27101 add_stmt (t);
27102 add_stmt (this_pre_body);
27103 pre_body = pop_stmt_list (pre_body);
27105 else
27106 pre_body = this_pre_body;
27109 if (decl)
27110 real_decl = decl;
27111 if (par_clauses != NULL && real_decl != NULL_TREE)
27113 tree *c;
27114 for (c = par_clauses; *c ; )
27115 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
27116 && OMP_CLAUSE_DECL (*c) == real_decl)
27118 error_at (loc, "iteration variable %qD"
27119 " should not be firstprivate", real_decl);
27120 *c = OMP_CLAUSE_CHAIN (*c);
27122 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
27123 && OMP_CLAUSE_DECL (*c) == real_decl)
27125 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
27126 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
27127 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
27128 OMP_CLAUSE_DECL (l) = real_decl;
27129 OMP_CLAUSE_CHAIN (l) = clauses;
27130 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
27131 clauses = l;
27132 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
27133 CP_OMP_CLAUSE_INFO (*c) = NULL;
27134 add_private_clause = false;
27136 else
27138 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
27139 && OMP_CLAUSE_DECL (*c) == real_decl)
27140 add_private_clause = false;
27141 c = &OMP_CLAUSE_CHAIN (*c);
27145 if (add_private_clause)
27147 tree c;
27148 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27150 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
27151 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
27152 && OMP_CLAUSE_DECL (c) == decl)
27153 break;
27154 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
27155 && OMP_CLAUSE_DECL (c) == decl)
27156 error_at (loc, "iteration variable %qD "
27157 "should not be firstprivate",
27158 decl);
27159 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
27160 && OMP_CLAUSE_DECL (c) == decl)
27161 error_at (loc, "iteration variable %qD should not be reduction",
27162 decl);
27164 if (c == NULL)
27166 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
27167 OMP_CLAUSE_DECL (c) = decl;
27168 c = finish_omp_clauses (c);
27169 if (c)
27171 OMP_CLAUSE_CHAIN (c) = clauses;
27172 clauses = c;
27177 cond = NULL;
27178 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27179 cond = cp_parser_omp_for_cond (parser, decl);
27180 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27182 incr = NULL;
27183 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27185 /* If decl is an iterator, preserve the operator on decl
27186 until finish_omp_for. */
27187 if (real_decl
27188 && ((processing_template_decl
27189 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
27190 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
27191 incr = cp_parser_omp_for_incr (parser, real_decl);
27192 else
27193 incr = cp_parser_expression (parser, false, NULL);
27194 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
27195 SET_EXPR_LOCATION (incr, input_location);
27198 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27199 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27200 /*or_comma=*/false,
27201 /*consume_paren=*/true);
27203 TREE_VEC_ELT (declv, i) = decl;
27204 TREE_VEC_ELT (initv, i) = init;
27205 TREE_VEC_ELT (condv, i) = cond;
27206 TREE_VEC_ELT (incrv, i) = incr;
27208 if (i == collapse - 1)
27209 break;
27211 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
27212 in between the collapsed for loops to be still considered perfectly
27213 nested. Hopefully the final version clarifies this.
27214 For now handle (multiple) {'s and empty statements. */
27215 cp_parser_parse_tentatively (parser);
27218 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27219 break;
27220 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27222 cp_lexer_consume_token (parser->lexer);
27223 bracecount++;
27225 else if (bracecount
27226 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27227 cp_lexer_consume_token (parser->lexer);
27228 else
27230 loc = cp_lexer_peek_token (parser->lexer)->location;
27231 error_at (loc, "not enough collapsed for loops");
27232 collapse_err = true;
27233 cp_parser_abort_tentative_parse (parser);
27234 declv = NULL_TREE;
27235 break;
27238 while (1);
27240 if (declv)
27242 cp_parser_parse_definitely (parser);
27243 nbraces += bracecount;
27247 /* Note that we saved the original contents of this flag when we entered
27248 the structured block, and so we don't need to re-save it here. */
27249 parser->in_statement = IN_OMP_FOR;
27251 /* Note that the grammar doesn't call for a structured block here,
27252 though the loop as a whole is a structured block. */
27253 body = push_stmt_list ();
27254 cp_parser_statement (parser, NULL_TREE, false, NULL);
27255 body = pop_stmt_list (body);
27257 if (declv == NULL_TREE)
27258 ret = NULL_TREE;
27259 else
27260 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
27261 pre_body, clauses);
27263 while (nbraces)
27265 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27267 cp_lexer_consume_token (parser->lexer);
27268 nbraces--;
27270 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27271 cp_lexer_consume_token (parser->lexer);
27272 else
27274 if (!collapse_err)
27276 error_at (cp_lexer_peek_token (parser->lexer)->location,
27277 "collapsed loops not perfectly nested");
27279 collapse_err = true;
27280 cp_parser_statement_seq_opt (parser, NULL);
27281 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27282 break;
27286 while (!for_block->is_empty ())
27287 add_stmt (pop_stmt_list (for_block->pop ()));
27288 release_tree_vector (for_block);
27290 return ret;
27293 /* OpenMP 2.5:
27294 #pragma omp for for-clause[optseq] new-line
27295 for-loop */
27297 #define OMP_FOR_CLAUSE_MASK \
27298 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27299 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27300 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27301 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27302 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
27303 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
27304 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
27305 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
27307 static tree
27308 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
27310 tree clauses, sb, ret;
27311 unsigned int save;
27313 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
27314 "#pragma omp for", pragma_tok);
27316 sb = begin_omp_structured_block ();
27317 save = cp_parser_begin_omp_structured_block (parser);
27319 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
27321 cp_parser_end_omp_structured_block (parser, save);
27322 add_stmt (finish_omp_structured_block (sb));
27324 return ret;
27327 /* OpenMP 2.5:
27328 # pragma omp master new-line
27329 structured-block */
27331 static tree
27332 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
27334 cp_parser_require_pragma_eol (parser, pragma_tok);
27335 return c_finish_omp_master (input_location,
27336 cp_parser_omp_structured_block (parser));
27339 /* OpenMP 2.5:
27340 # pragma omp ordered new-line
27341 structured-block */
27343 static tree
27344 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
27346 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27347 cp_parser_require_pragma_eol (parser, pragma_tok);
27348 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
27351 /* OpenMP 2.5:
27353 section-scope:
27354 { section-sequence }
27356 section-sequence:
27357 section-directive[opt] structured-block
27358 section-sequence section-directive structured-block */
27360 static tree
27361 cp_parser_omp_sections_scope (cp_parser *parser)
27363 tree stmt, substmt;
27364 bool error_suppress = false;
27365 cp_token *tok;
27367 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
27368 return NULL_TREE;
27370 stmt = push_stmt_list ();
27372 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
27374 unsigned save;
27376 substmt = begin_omp_structured_block ();
27377 save = cp_parser_begin_omp_structured_block (parser);
27379 while (1)
27381 cp_parser_statement (parser, NULL_TREE, false, NULL);
27383 tok = cp_lexer_peek_token (parser->lexer);
27384 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27385 break;
27386 if (tok->type == CPP_CLOSE_BRACE)
27387 break;
27388 if (tok->type == CPP_EOF)
27389 break;
27392 cp_parser_end_omp_structured_block (parser, save);
27393 substmt = finish_omp_structured_block (substmt);
27394 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27395 add_stmt (substmt);
27398 while (1)
27400 tok = cp_lexer_peek_token (parser->lexer);
27401 if (tok->type == CPP_CLOSE_BRACE)
27402 break;
27403 if (tok->type == CPP_EOF)
27404 break;
27406 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27408 cp_lexer_consume_token (parser->lexer);
27409 cp_parser_require_pragma_eol (parser, tok);
27410 error_suppress = false;
27412 else if (!error_suppress)
27414 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
27415 error_suppress = true;
27418 substmt = cp_parser_omp_structured_block (parser);
27419 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27420 add_stmt (substmt);
27422 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
27424 substmt = pop_stmt_list (stmt);
27426 stmt = make_node (OMP_SECTIONS);
27427 TREE_TYPE (stmt) = void_type_node;
27428 OMP_SECTIONS_BODY (stmt) = substmt;
27430 add_stmt (stmt);
27431 return stmt;
27434 /* OpenMP 2.5:
27435 # pragma omp sections sections-clause[optseq] newline
27436 sections-scope */
27438 #define OMP_SECTIONS_CLAUSE_MASK \
27439 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27440 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27441 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27442 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27443 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27445 static tree
27446 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
27448 tree clauses, ret;
27450 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
27451 "#pragma omp sections", pragma_tok);
27453 ret = cp_parser_omp_sections_scope (parser);
27454 if (ret)
27455 OMP_SECTIONS_CLAUSES (ret) = clauses;
27457 return ret;
27460 /* OpenMP 2.5:
27461 # pragma parallel parallel-clause new-line
27462 # pragma parallel for parallel-for-clause new-line
27463 # pragma parallel sections parallel-sections-clause new-line */
27465 #define OMP_PARALLEL_CLAUSE_MASK \
27466 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27467 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27468 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27469 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27470 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27471 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
27472 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27473 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
27475 static tree
27476 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
27478 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
27479 const char *p_name = "#pragma omp parallel";
27480 tree stmt, clauses, par_clause, ws_clause, block;
27481 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
27482 unsigned int save;
27483 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27485 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27487 cp_lexer_consume_token (parser->lexer);
27488 p_kind = PRAGMA_OMP_PARALLEL_FOR;
27489 p_name = "#pragma omp parallel for";
27490 mask |= OMP_FOR_CLAUSE_MASK;
27491 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27493 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27495 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27496 const char *p = IDENTIFIER_POINTER (id);
27497 if (strcmp (p, "sections") == 0)
27499 cp_lexer_consume_token (parser->lexer);
27500 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
27501 p_name = "#pragma omp parallel sections";
27502 mask |= OMP_SECTIONS_CLAUSE_MASK;
27503 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27507 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
27508 block = begin_omp_parallel ();
27509 save = cp_parser_begin_omp_structured_block (parser);
27511 switch (p_kind)
27513 case PRAGMA_OMP_PARALLEL:
27514 cp_parser_statement (parser, NULL_TREE, false, NULL);
27515 par_clause = clauses;
27516 break;
27518 case PRAGMA_OMP_PARALLEL_FOR:
27519 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27520 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
27521 break;
27523 case PRAGMA_OMP_PARALLEL_SECTIONS:
27524 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27525 stmt = cp_parser_omp_sections_scope (parser);
27526 if (stmt)
27527 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
27528 break;
27530 default:
27531 gcc_unreachable ();
27534 cp_parser_end_omp_structured_block (parser, save);
27535 stmt = finish_omp_parallel (par_clause, block);
27536 if (p_kind != PRAGMA_OMP_PARALLEL)
27537 OMP_PARALLEL_COMBINED (stmt) = 1;
27538 return stmt;
27541 /* OpenMP 2.5:
27542 # pragma omp single single-clause[optseq] new-line
27543 structured-block */
27545 #define OMP_SINGLE_CLAUSE_MASK \
27546 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27547 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27548 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
27549 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27551 static tree
27552 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
27554 tree stmt = make_node (OMP_SINGLE);
27555 TREE_TYPE (stmt) = void_type_node;
27557 OMP_SINGLE_CLAUSES (stmt)
27558 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
27559 "#pragma omp single", pragma_tok);
27560 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
27562 return add_stmt (stmt);
27565 /* OpenMP 3.0:
27566 # pragma omp task task-clause[optseq] new-line
27567 structured-block */
27569 #define OMP_TASK_CLAUSE_MASK \
27570 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27571 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
27572 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27573 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27574 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27575 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27576 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
27577 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
27579 static tree
27580 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
27582 tree clauses, block;
27583 unsigned int save;
27585 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
27586 "#pragma omp task", pragma_tok);
27587 block = begin_omp_task ();
27588 save = cp_parser_begin_omp_structured_block (parser);
27589 cp_parser_statement (parser, NULL_TREE, false, NULL);
27590 cp_parser_end_omp_structured_block (parser, save);
27591 return finish_omp_task (clauses, block);
27594 /* OpenMP 3.0:
27595 # pragma omp taskwait new-line */
27597 static void
27598 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
27600 cp_parser_require_pragma_eol (parser, pragma_tok);
27601 finish_omp_taskwait ();
27604 /* OpenMP 3.1:
27605 # pragma omp taskyield new-line */
27607 static void
27608 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
27610 cp_parser_require_pragma_eol (parser, pragma_tok);
27611 finish_omp_taskyield ();
27614 /* OpenMP 2.5:
27615 # pragma omp threadprivate (variable-list) */
27617 static void
27618 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
27620 tree vars;
27622 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27623 cp_parser_require_pragma_eol (parser, pragma_tok);
27625 finish_omp_threadprivate (vars);
27628 /* Main entry point to OpenMP statement pragmas. */
27630 static void
27631 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
27633 tree stmt;
27635 switch (pragma_tok->pragma_kind)
27637 case PRAGMA_OMP_ATOMIC:
27638 cp_parser_omp_atomic (parser, pragma_tok);
27639 return;
27640 case PRAGMA_OMP_CRITICAL:
27641 stmt = cp_parser_omp_critical (parser, pragma_tok);
27642 break;
27643 case PRAGMA_OMP_FOR:
27644 stmt = cp_parser_omp_for (parser, pragma_tok);
27645 break;
27646 case PRAGMA_OMP_MASTER:
27647 stmt = cp_parser_omp_master (parser, pragma_tok);
27648 break;
27649 case PRAGMA_OMP_ORDERED:
27650 stmt = cp_parser_omp_ordered (parser, pragma_tok);
27651 break;
27652 case PRAGMA_OMP_PARALLEL:
27653 stmt = cp_parser_omp_parallel (parser, pragma_tok);
27654 break;
27655 case PRAGMA_OMP_SECTIONS:
27656 stmt = cp_parser_omp_sections (parser, pragma_tok);
27657 break;
27658 case PRAGMA_OMP_SINGLE:
27659 stmt = cp_parser_omp_single (parser, pragma_tok);
27660 break;
27661 case PRAGMA_OMP_TASK:
27662 stmt = cp_parser_omp_task (parser, pragma_tok);
27663 break;
27664 default:
27665 gcc_unreachable ();
27668 if (stmt)
27669 SET_EXPR_LOCATION (stmt, pragma_tok->location);
27672 /* Transactional Memory parsing routines. */
27674 /* Parse a transaction attribute.
27676 txn-attribute:
27677 attribute
27678 [ [ identifier ] ]
27680 ??? Simplify this when C++0x bracket attributes are
27681 implemented properly. */
27683 static tree
27684 cp_parser_txn_attribute_opt (cp_parser *parser)
27686 cp_token *token;
27687 tree attr_name, attr = NULL;
27689 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27690 return cp_parser_attributes_opt (parser);
27692 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
27693 return NULL_TREE;
27694 cp_lexer_consume_token (parser->lexer);
27695 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
27696 goto error1;
27698 token = cp_lexer_peek_token (parser->lexer);
27699 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
27701 token = cp_lexer_consume_token (parser->lexer);
27703 attr_name = (token->type == CPP_KEYWORD
27704 /* For keywords, use the canonical spelling,
27705 not the parsed identifier. */
27706 ? ridpointers[(int) token->keyword]
27707 : token->u.value);
27708 attr = build_tree_list (attr_name, NULL_TREE);
27710 else
27711 cp_parser_error (parser, "expected identifier");
27713 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27714 error1:
27715 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27716 return attr;
27719 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27721 transaction-statement:
27722 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27723 compound-statement
27724 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27727 static tree
27728 cp_parser_transaction (cp_parser *parser, enum rid keyword)
27730 unsigned char old_in = parser->in_transaction;
27731 unsigned char this_in = 1, new_in;
27732 cp_token *token;
27733 tree stmt, attrs, noex;
27735 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27736 || keyword == RID_TRANSACTION_RELAXED);
27737 token = cp_parser_require_keyword (parser, keyword,
27738 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27739 : RT_TRANSACTION_RELAXED));
27740 gcc_assert (token != NULL);
27742 if (keyword == RID_TRANSACTION_RELAXED)
27743 this_in |= TM_STMT_ATTR_RELAXED;
27744 else
27746 attrs = cp_parser_txn_attribute_opt (parser);
27747 if (attrs)
27748 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27751 /* Parse a noexcept specification. */
27752 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27754 /* Keep track if we're in the lexical scope of an outer transaction. */
27755 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27757 stmt = begin_transaction_stmt (token->location, NULL, this_in);
27759 parser->in_transaction = new_in;
27760 cp_parser_compound_statement (parser, NULL, false, false);
27761 parser->in_transaction = old_in;
27763 finish_transaction_stmt (stmt, NULL, this_in, noex);
27765 return stmt;
27768 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27770 transaction-expression:
27771 __transaction_atomic txn-noexcept-spec[opt] ( expression )
27772 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27775 static tree
27776 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27778 unsigned char old_in = parser->in_transaction;
27779 unsigned char this_in = 1;
27780 cp_token *token;
27781 tree expr, noex;
27782 bool noex_expr;
27784 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27785 || keyword == RID_TRANSACTION_RELAXED);
27787 if (!flag_tm)
27788 error (keyword == RID_TRANSACTION_RELAXED
27789 ? G_("%<__transaction_relaxed%> without transactional memory "
27790 "support enabled")
27791 : G_("%<__transaction_atomic%> without transactional memory "
27792 "support enabled"));
27794 token = cp_parser_require_keyword (parser, keyword,
27795 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27796 : RT_TRANSACTION_RELAXED));
27797 gcc_assert (token != NULL);
27799 if (keyword == RID_TRANSACTION_RELAXED)
27800 this_in |= TM_STMT_ATTR_RELAXED;
27802 /* Set this early. This might mean that we allow transaction_cancel in
27803 an expression that we find out later actually has to be a constexpr.
27804 However, we expect that cxx_constant_value will be able to deal with
27805 this; also, if the noexcept has no constexpr, then what we parse next
27806 really is a transaction's body. */
27807 parser->in_transaction = this_in;
27809 /* Parse a noexcept specification. */
27810 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27811 true);
27813 if (!noex || !noex_expr
27814 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27816 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27818 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27819 finish_parenthesized_expr (expr);
27821 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27823 else
27825 /* The only expression that is available got parsed for the noexcept
27826 already. noexcept is true then. */
27827 expr = noex;
27828 noex = boolean_true_node;
27831 expr = build_transaction_expr (token->location, expr, this_in, noex);
27832 parser->in_transaction = old_in;
27834 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27835 return error_mark_node;
27837 return (flag_tm ? expr : error_mark_node);
27840 /* Parse a function-transaction-block.
27842 function-transaction-block:
27843 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27844 function-body
27845 __transaction_atomic txn-attribute[opt] function-try-block
27846 __transaction_relaxed ctor-initializer[opt] function-body
27847 __transaction_relaxed function-try-block
27850 static bool
27851 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27853 unsigned char old_in = parser->in_transaction;
27854 unsigned char new_in = 1;
27855 tree compound_stmt, stmt, attrs;
27856 bool ctor_initializer_p;
27857 cp_token *token;
27859 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27860 || keyword == RID_TRANSACTION_RELAXED);
27861 token = cp_parser_require_keyword (parser, keyword,
27862 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27863 : RT_TRANSACTION_RELAXED));
27864 gcc_assert (token != NULL);
27866 if (keyword == RID_TRANSACTION_RELAXED)
27867 new_in |= TM_STMT_ATTR_RELAXED;
27868 else
27870 attrs = cp_parser_txn_attribute_opt (parser);
27871 if (attrs)
27872 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27875 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27877 parser->in_transaction = new_in;
27879 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27880 ctor_initializer_p = cp_parser_function_try_block (parser);
27881 else
27882 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
27883 (parser, /*in_function_try_block=*/false);
27885 parser->in_transaction = old_in;
27887 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27889 return ctor_initializer_p;
27892 /* Parse a __transaction_cancel statement.
27894 cancel-statement:
27895 __transaction_cancel txn-attribute[opt] ;
27896 __transaction_cancel txn-attribute[opt] throw-expression ;
27898 ??? Cancel and throw is not yet implemented. */
27900 static tree
27901 cp_parser_transaction_cancel (cp_parser *parser)
27903 cp_token *token;
27904 bool is_outer = false;
27905 tree stmt, attrs;
27907 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27908 RT_TRANSACTION_CANCEL);
27909 gcc_assert (token != NULL);
27911 attrs = cp_parser_txn_attribute_opt (parser);
27912 if (attrs)
27913 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27915 /* ??? Parse cancel-and-throw here. */
27917 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27919 if (!flag_tm)
27921 error_at (token->location, "%<__transaction_cancel%> without "
27922 "transactional memory support enabled");
27923 return error_mark_node;
27925 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27927 error_at (token->location, "%<__transaction_cancel%> within a "
27928 "%<__transaction_relaxed%>");
27929 return error_mark_node;
27931 else if (is_outer)
27933 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27934 && !is_tm_may_cancel_outer (current_function_decl))
27936 error_at (token->location, "outer %<__transaction_cancel%> not "
27937 "within outer %<__transaction_atomic%>");
27938 error_at (token->location,
27939 " or a %<transaction_may_cancel_outer%> function");
27940 return error_mark_node;
27943 else if (parser->in_transaction == 0)
27945 error_at (token->location, "%<__transaction_cancel%> not within "
27946 "%<__transaction_atomic%>");
27947 return error_mark_node;
27950 stmt = build_tm_abort_call (token->location, is_outer);
27951 add_stmt (stmt);
27952 finish_stmt ();
27954 return stmt;
27957 /* The parser. */
27959 static GTY (()) cp_parser *the_parser;
27962 /* Special handling for the first token or line in the file. The first
27963 thing in the file might be #pragma GCC pch_preprocess, which loads a
27964 PCH file, which is a GC collection point. So we need to handle this
27965 first pragma without benefit of an existing lexer structure.
27967 Always returns one token to the caller in *FIRST_TOKEN. This is
27968 either the true first token of the file, or the first token after
27969 the initial pragma. */
27971 static void
27972 cp_parser_initial_pragma (cp_token *first_token)
27974 tree name = NULL;
27976 cp_lexer_get_preprocessor_token (NULL, first_token);
27977 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27978 return;
27980 cp_lexer_get_preprocessor_token (NULL, first_token);
27981 if (first_token->type == CPP_STRING)
27983 name = first_token->u.value;
27985 cp_lexer_get_preprocessor_token (NULL, first_token);
27986 if (first_token->type != CPP_PRAGMA_EOL)
27987 error_at (first_token->location,
27988 "junk at end of %<#pragma GCC pch_preprocess%>");
27990 else
27991 error_at (first_token->location, "expected string literal");
27993 /* Skip to the end of the pragma. */
27994 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27995 cp_lexer_get_preprocessor_token (NULL, first_token);
27997 /* Now actually load the PCH file. */
27998 if (name)
27999 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
28001 /* Read one more token to return to our caller. We have to do this
28002 after reading the PCH file in, since its pointers have to be
28003 live. */
28004 cp_lexer_get_preprocessor_token (NULL, first_token);
28007 /* Normal parsing of a pragma token. Here we can (and must) use the
28008 regular lexer. */
28010 static bool
28011 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
28013 cp_token *pragma_tok;
28014 unsigned int id;
28016 pragma_tok = cp_lexer_consume_token (parser->lexer);
28017 gcc_assert (pragma_tok->type == CPP_PRAGMA);
28018 parser->lexer->in_pragma = true;
28020 id = pragma_tok->pragma_kind;
28021 switch (id)
28023 case PRAGMA_GCC_PCH_PREPROCESS:
28024 error_at (pragma_tok->location,
28025 "%<#pragma GCC pch_preprocess%> must be first");
28026 break;
28028 case PRAGMA_OMP_BARRIER:
28029 switch (context)
28031 case pragma_compound:
28032 cp_parser_omp_barrier (parser, pragma_tok);
28033 return false;
28034 case pragma_stmt:
28035 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
28036 "used in compound statements");
28037 break;
28038 default:
28039 goto bad_stmt;
28041 break;
28043 case PRAGMA_OMP_FLUSH:
28044 switch (context)
28046 case pragma_compound:
28047 cp_parser_omp_flush (parser, pragma_tok);
28048 return false;
28049 case pragma_stmt:
28050 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
28051 "used in compound statements");
28052 break;
28053 default:
28054 goto bad_stmt;
28056 break;
28058 case PRAGMA_OMP_TASKWAIT:
28059 switch (context)
28061 case pragma_compound:
28062 cp_parser_omp_taskwait (parser, pragma_tok);
28063 return false;
28064 case pragma_stmt:
28065 error_at (pragma_tok->location,
28066 "%<#pragma omp taskwait%> may only be "
28067 "used in compound statements");
28068 break;
28069 default:
28070 goto bad_stmt;
28072 break;
28074 case PRAGMA_OMP_TASKYIELD:
28075 switch (context)
28077 case pragma_compound:
28078 cp_parser_omp_taskyield (parser, pragma_tok);
28079 return false;
28080 case pragma_stmt:
28081 error_at (pragma_tok->location,
28082 "%<#pragma omp taskyield%> may only be "
28083 "used in compound statements");
28084 break;
28085 default:
28086 goto bad_stmt;
28088 break;
28090 case PRAGMA_OMP_THREADPRIVATE:
28091 cp_parser_omp_threadprivate (parser, pragma_tok);
28092 return false;
28094 case PRAGMA_OMP_ATOMIC:
28095 case PRAGMA_OMP_CRITICAL:
28096 case PRAGMA_OMP_FOR:
28097 case PRAGMA_OMP_MASTER:
28098 case PRAGMA_OMP_ORDERED:
28099 case PRAGMA_OMP_PARALLEL:
28100 case PRAGMA_OMP_SECTIONS:
28101 case PRAGMA_OMP_SINGLE:
28102 case PRAGMA_OMP_TASK:
28103 if (context == pragma_external)
28104 goto bad_stmt;
28105 cp_parser_omp_construct (parser, pragma_tok);
28106 return true;
28108 case PRAGMA_OMP_SECTION:
28109 error_at (pragma_tok->location,
28110 "%<#pragma omp section%> may only be used in "
28111 "%<#pragma omp sections%> construct");
28112 break;
28114 default:
28115 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
28116 c_invoke_pragma_handler (id);
28117 break;
28119 bad_stmt:
28120 cp_parser_error (parser, "expected declaration specifiers");
28121 break;
28124 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28125 return false;
28128 /* The interface the pragma parsers have to the lexer. */
28130 enum cpp_ttype
28131 pragma_lex (tree *value)
28133 cp_token *tok;
28134 enum cpp_ttype ret;
28136 tok = cp_lexer_peek_token (the_parser->lexer);
28138 ret = tok->type;
28139 *value = tok->u.value;
28141 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
28142 ret = CPP_EOF;
28143 else if (ret == CPP_STRING)
28144 *value = cp_parser_string_literal (the_parser, false, false);
28145 else
28147 cp_lexer_consume_token (the_parser->lexer);
28148 if (ret == CPP_KEYWORD)
28149 ret = CPP_NAME;
28152 return ret;
28156 /* External interface. */
28158 /* Parse one entire translation unit. */
28160 void
28161 c_parse_file (void)
28163 static bool already_called = false;
28165 if (already_called)
28167 sorry ("inter-module optimizations not implemented for C++");
28168 return;
28170 already_called = true;
28172 the_parser = cp_parser_new ();
28173 push_deferring_access_checks (flag_access_control
28174 ? dk_no_deferred : dk_no_check);
28175 cp_parser_translation_unit (the_parser);
28176 the_parser = NULL;
28179 #include "gt-cp-parser.h"