PR c++/54653
[official-gcc.git] / gcc / cp / parser.c
blob190b8d94113bd6ae3e9450994867d1abc66e24f0
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "timevar.h"
27 #include "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-family/c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic-core.h"
35 #include "target.h"
36 #include "cgraph.h"
37 #include "c-family/c-common.h"
38 #include "c-family/c-objc.h"
39 #include "plugin.h"
40 #include "tree-pretty-print.h"
41 #include "parser.h"
44 /* The lexer. */
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
49 static cp_token eof_token =
51 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
54 /* The various kinds of non integral constant we encounter. */
55 typedef enum non_integral_constant {
56 NIC_NONE,
57 /* floating-point literal */
58 NIC_FLOAT,
59 /* %<this%> */
60 NIC_THIS,
61 /* %<__FUNCTION__%> */
62 NIC_FUNC_NAME,
63 /* %<__PRETTY_FUNCTION__%> */
64 NIC_PRETTY_FUNC,
65 /* %<__func__%> */
66 NIC_C99_FUNC,
67 /* "%<va_arg%> */
68 NIC_VA_ARG,
69 /* a cast */
70 NIC_CAST,
71 /* %<typeid%> operator */
72 NIC_TYPEID,
73 /* non-constant compound literals */
74 NIC_NCC,
75 /* a function call */
76 NIC_FUNC_CALL,
77 /* an increment */
78 NIC_INC,
79 /* an decrement */
80 NIC_DEC,
81 /* an array reference */
82 NIC_ARRAY_REF,
83 /* %<->%> */
84 NIC_ARROW,
85 /* %<.%> */
86 NIC_POINT,
87 /* the address of a label */
88 NIC_ADDR_LABEL,
89 /* %<*%> */
90 NIC_STAR,
91 /* %<&%> */
92 NIC_ADDR,
93 /* %<++%> */
94 NIC_PREINCREMENT,
95 /* %<--%> */
96 NIC_PREDECREMENT,
97 /* %<new%> */
98 NIC_NEW,
99 /* %<delete%> */
100 NIC_DEL,
101 /* calls to overloaded operators */
102 NIC_OVERLOADED,
103 /* an assignment */
104 NIC_ASSIGNMENT,
105 /* a comma operator */
106 NIC_COMMA,
107 /* a call to a constructor */
108 NIC_CONSTRUCTOR,
109 /* a transaction expression */
110 NIC_TRANSACTION
111 } non_integral_constant;
113 /* The various kinds of errors about name-lookup failing. */
114 typedef enum name_lookup_error {
115 /* NULL */
116 NLE_NULL,
117 /* is not a type */
118 NLE_TYPE,
119 /* is not a class or namespace */
120 NLE_CXX98,
121 /* is not a class, namespace, or enumeration */
122 NLE_NOT_CXX98
123 } name_lookup_error;
125 /* The various kinds of required token */
126 typedef enum required_token {
127 RT_NONE,
128 RT_SEMICOLON, /* ';' */
129 RT_OPEN_PAREN, /* '(' */
130 RT_CLOSE_BRACE, /* '}' */
131 RT_OPEN_BRACE, /* '{' */
132 RT_CLOSE_SQUARE, /* ']' */
133 RT_OPEN_SQUARE, /* '[' */
134 RT_COMMA, /* ',' */
135 RT_SCOPE, /* '::' */
136 RT_LESS, /* '<' */
137 RT_GREATER, /* '>' */
138 RT_EQ, /* '=' */
139 RT_ELLIPSIS, /* '...' */
140 RT_MULT, /* '*' */
141 RT_COMPL, /* '~' */
142 RT_COLON, /* ':' */
143 RT_COLON_SCOPE, /* ':' or '::' */
144 RT_CLOSE_PAREN, /* ')' */
145 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
146 RT_PRAGMA_EOL, /* end of line */
147 RT_NAME, /* identifier */
149 /* The type is CPP_KEYWORD */
150 RT_NEW, /* new */
151 RT_DELETE, /* delete */
152 RT_RETURN, /* return */
153 RT_WHILE, /* while */
154 RT_EXTERN, /* extern */
155 RT_STATIC_ASSERT, /* static_assert */
156 RT_DECLTYPE, /* decltype */
157 RT_OPERATOR, /* operator */
158 RT_CLASS, /* class */
159 RT_TEMPLATE, /* template */
160 RT_NAMESPACE, /* namespace */
161 RT_USING, /* using */
162 RT_ASM, /* asm */
163 RT_TRY, /* try */
164 RT_CATCH, /* catch */
165 RT_THROW, /* throw */
166 RT_LABEL, /* __label__ */
167 RT_AT_TRY, /* @try */
168 RT_AT_SYNCHRONIZED, /* @synchronized */
169 RT_AT_THROW, /* @throw */
171 RT_SELECT, /* selection-statement */
172 RT_INTERATION, /* iteration-statement */
173 RT_JUMP, /* jump-statement */
174 RT_CLASS_KEY, /* class-key */
175 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
176 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
177 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
178 RT_TRANSACTION_CANCEL /* __transaction_cancel */
179 } required_token;
181 /* Prototypes. */
183 static cp_lexer *cp_lexer_new_main
184 (void);
185 static cp_lexer *cp_lexer_new_from_tokens
186 (cp_token_cache *tokens);
187 static void cp_lexer_destroy
188 (cp_lexer *);
189 static int cp_lexer_saving_tokens
190 (const cp_lexer *);
191 static cp_token *cp_lexer_token_at
192 (cp_lexer *, cp_token_position);
193 static void cp_lexer_get_preprocessor_token
194 (cp_lexer *, cp_token *);
195 static inline cp_token *cp_lexer_peek_token
196 (cp_lexer *);
197 static cp_token *cp_lexer_peek_nth_token
198 (cp_lexer *, size_t);
199 static inline bool cp_lexer_next_token_is
200 (cp_lexer *, enum cpp_ttype);
201 static bool cp_lexer_next_token_is_not
202 (cp_lexer *, enum cpp_ttype);
203 static bool cp_lexer_next_token_is_keyword
204 (cp_lexer *, enum rid);
205 static cp_token *cp_lexer_consume_token
206 (cp_lexer *);
207 static void cp_lexer_purge_token
208 (cp_lexer *);
209 static void cp_lexer_purge_tokens_after
210 (cp_lexer *, cp_token_position);
211 static void cp_lexer_save_tokens
212 (cp_lexer *);
213 static void cp_lexer_commit_tokens
214 (cp_lexer *);
215 static void cp_lexer_rollback_tokens
216 (cp_lexer *);
217 static void cp_lexer_print_token
218 (FILE *, cp_token *);
219 static inline bool cp_lexer_debugging_p
220 (cp_lexer *);
221 static void cp_lexer_start_debugging
222 (cp_lexer *) ATTRIBUTE_UNUSED;
223 static void cp_lexer_stop_debugging
224 (cp_lexer *) ATTRIBUTE_UNUSED;
226 static cp_token_cache *cp_token_cache_new
227 (cp_token *, cp_token *);
229 static void cp_parser_initial_pragma
230 (cp_token *);
232 static tree cp_literal_operator_id
233 (const char *);
235 /* Manifest constants. */
236 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
237 #define CP_SAVED_TOKEN_STACK 5
239 /* Variables. */
241 /* The stream to which debugging output should be written. */
242 static FILE *cp_lexer_debug_stream;
244 /* Nonzero if we are parsing an unevaluated operand: an operand to
245 sizeof, typeof, or alignof. */
246 int cp_unevaluated_operand;
248 /* Dump up to NUM tokens in BUFFER to FILE starting with token
249 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
250 first token in BUFFER. If NUM is 0, dump all the tokens. If
251 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
252 highlighted by surrounding it in [[ ]]. */
254 static void
255 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
256 cp_token *start_token, unsigned num,
257 cp_token *curr_token)
259 unsigned i, nprinted;
260 cp_token *token;
261 bool do_print;
263 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
265 if (buffer == NULL)
266 return;
268 if (num == 0)
269 num = buffer->length ();
271 if (start_token == NULL)
272 start_token = buffer->address ();
274 if (start_token > buffer->address ())
276 cp_lexer_print_token (file, &(*buffer)[0]);
277 fprintf (file, " ... ");
280 do_print = false;
281 nprinted = 0;
282 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
284 if (token == start_token)
285 do_print = true;
287 if (!do_print)
288 continue;
290 nprinted++;
291 if (token == curr_token)
292 fprintf (file, "[[");
294 cp_lexer_print_token (file, token);
296 if (token == curr_token)
297 fprintf (file, "]]");
299 switch (token->type)
301 case CPP_SEMICOLON:
302 case CPP_OPEN_BRACE:
303 case CPP_CLOSE_BRACE:
304 case CPP_EOF:
305 fputc ('\n', file);
306 break;
308 default:
309 fputc (' ', file);
313 if (i == num && i < buffer->length ())
315 fprintf (file, " ... ");
316 cp_lexer_print_token (file, &buffer->last ());
319 fprintf (file, "\n");
323 /* Dump all tokens in BUFFER to stderr. */
325 void
326 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
328 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
332 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
333 description for T. */
335 static void
336 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
338 if (t)
340 fprintf (file, "%s: ", desc);
341 print_node_brief (file, "", t, 0);
346 /* Dump parser context C to FILE. */
348 static void
349 cp_debug_print_context (FILE *file, cp_parser_context *c)
351 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
352 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
353 print_node_brief (file, "", c->object_type, 0);
354 fprintf (file, "}\n");
358 /* Print the stack of parsing contexts to FILE starting with FIRST. */
360 static void
361 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
363 unsigned i;
364 cp_parser_context *c;
366 fprintf (file, "Parsing context stack:\n");
367 for (i = 0, c = first; c; c = c->next, i++)
369 fprintf (file, "\t#%u: ", i);
370 cp_debug_print_context (file, c);
375 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
377 static void
378 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
380 if (flag)
381 fprintf (file, "%s: true\n", desc);
385 /* Print an unparsed function entry UF to FILE. */
387 static void
388 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
390 unsigned i;
391 cp_default_arg_entry *default_arg_fn;
392 tree fn;
394 fprintf (file, "\tFunctions with default args:\n");
395 for (i = 0;
396 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
397 i++)
399 fprintf (file, "\t\tClass type: ");
400 print_node_brief (file, "", default_arg_fn->class_type, 0);
401 fprintf (file, "\t\tDeclaration: ");
402 print_node_brief (file, "", default_arg_fn->decl, 0);
403 fprintf (file, "\n");
406 fprintf (file, "\n\tFunctions with definitions that require "
407 "post-processing\n\t\t");
408 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
410 print_node_brief (file, "", fn, 0);
411 fprintf (file, " ");
413 fprintf (file, "\n");
415 fprintf (file, "\n\tNon-static data members with initializers that require "
416 "post-processing\n\t\t");
417 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
419 print_node_brief (file, "", fn, 0);
420 fprintf (file, " ");
422 fprintf (file, "\n");
426 /* Print the stack of unparsed member functions S to FILE. */
428 static void
429 cp_debug_print_unparsed_queues (FILE *file,
430 vec<cp_unparsed_functions_entry, va_gc> *s)
432 unsigned i;
433 cp_unparsed_functions_entry *uf;
435 fprintf (file, "Unparsed functions\n");
436 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
438 fprintf (file, "#%u:\n", i);
439 cp_debug_print_unparsed_function (file, uf);
444 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
445 the given PARSER. If FILE is NULL, the output is printed on stderr. */
447 static void
448 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
450 cp_token *next_token, *first_token, *start_token;
452 if (file == NULL)
453 file = stderr;
455 next_token = parser->lexer->next_token;
456 first_token = parser->lexer->buffer->address ();
457 start_token = (next_token > first_token + window_size / 2)
458 ? next_token - window_size / 2
459 : first_token;
460 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
461 next_token);
465 /* Dump debugging information for the given PARSER. If FILE is NULL,
466 the output is printed on stderr. */
468 void
469 cp_debug_parser (FILE *file, cp_parser *parser)
471 const size_t window_size = 20;
472 cp_token *token;
473 expanded_location eloc;
475 if (file == NULL)
476 file = stderr;
478 fprintf (file, "Parser state\n\n");
479 fprintf (file, "Number of tokens: %u\n",
480 vec_safe_length (parser->lexer->buffer));
481 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
482 cp_debug_print_tree_if_set (file, "Object scope",
483 parser->object_scope);
484 cp_debug_print_tree_if_set (file, "Qualifying scope",
485 parser->qualifying_scope);
486 cp_debug_print_context_stack (file, parser->context);
487 cp_debug_print_flag (file, "Allow GNU extensions",
488 parser->allow_gnu_extensions_p);
489 cp_debug_print_flag (file, "'>' token is greater-than",
490 parser->greater_than_is_operator_p);
491 cp_debug_print_flag (file, "Default args allowed in current "
492 "parameter list", parser->default_arg_ok_p);
493 cp_debug_print_flag (file, "Parsing integral constant-expression",
494 parser->integral_constant_expression_p);
495 cp_debug_print_flag (file, "Allow non-constant expression in current "
496 "constant-expression",
497 parser->allow_non_integral_constant_expression_p);
498 cp_debug_print_flag (file, "Seen non-constant expression",
499 parser->non_integral_constant_expression_p);
500 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
501 "current context",
502 parser->local_variables_forbidden_p);
503 cp_debug_print_flag (file, "In unbraced linkage specification",
504 parser->in_unbraced_linkage_specification_p);
505 cp_debug_print_flag (file, "Parsing a declarator",
506 parser->in_declarator_p);
507 cp_debug_print_flag (file, "In template argument list",
508 parser->in_template_argument_list_p);
509 cp_debug_print_flag (file, "Parsing an iteration statement",
510 parser->in_statement & IN_ITERATION_STMT);
511 cp_debug_print_flag (file, "Parsing a switch statement",
512 parser->in_statement & IN_SWITCH_STMT);
513 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
514 parser->in_statement & IN_OMP_BLOCK);
515 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
516 parser->in_statement & IN_OMP_FOR);
517 cp_debug_print_flag (file, "Parsing an if statement",
518 parser->in_statement & IN_IF_STMT);
519 cp_debug_print_flag (file, "Parsing a type-id in an expression "
520 "context", parser->in_type_id_in_expr_p);
521 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
522 parser->implicit_extern_c);
523 cp_debug_print_flag (file, "String expressions should be translated "
524 "to execution character set",
525 parser->translate_strings_p);
526 cp_debug_print_flag (file, "Parsing function body outside of a "
527 "local class", parser->in_function_body);
528 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
529 parser->colon_corrects_to_scope_p);
530 if (parser->type_definition_forbidden_message)
531 fprintf (file, "Error message for forbidden type definitions: %s\n",
532 parser->type_definition_forbidden_message);
533 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
534 fprintf (file, "Number of class definitions in progress: %u\n",
535 parser->num_classes_being_defined);
536 fprintf (file, "Number of template parameter lists for the current "
537 "declaration: %u\n", parser->num_template_parameter_lists);
538 cp_debug_parser_tokens (file, parser, window_size);
539 token = parser->lexer->next_token;
540 fprintf (file, "Next token to parse:\n");
541 fprintf (file, "\tToken: ");
542 cp_lexer_print_token (file, token);
543 eloc = expand_location (token->location);
544 fprintf (file, "\n\tFile: %s\n", eloc.file);
545 fprintf (file, "\tLine: %d\n", eloc.line);
546 fprintf (file, "\tColumn: %d\n", eloc.column);
550 /* Allocate memory for a new lexer object and return it. */
552 static cp_lexer *
553 cp_lexer_alloc (void)
555 cp_lexer *lexer;
557 c_common_no_more_pch ();
559 /* Allocate the memory. */
560 lexer = ggc_alloc_cleared_cp_lexer ();
562 /* Initially we are not debugging. */
563 lexer->debugging_p = false;
565 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
567 /* Create the buffer. */
568 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
570 return lexer;
574 /* Create a new main C++ lexer, the lexer that gets tokens from the
575 preprocessor. */
577 static cp_lexer *
578 cp_lexer_new_main (void)
580 cp_lexer *lexer;
581 cp_token token;
583 /* It's possible that parsing the first pragma will load a PCH file,
584 which is a GC collection point. So we have to do that before
585 allocating any memory. */
586 cp_parser_initial_pragma (&token);
588 lexer = cp_lexer_alloc ();
590 /* Put the first token in the buffer. */
591 lexer->buffer->quick_push (token);
593 /* Get the remaining tokens from the preprocessor. */
594 while (token.type != CPP_EOF)
596 cp_lexer_get_preprocessor_token (lexer, &token);
597 vec_safe_push (lexer->buffer, token);
600 lexer->last_token = lexer->buffer->address ()
601 + lexer->buffer->length ()
602 - 1;
603 lexer->next_token = lexer->buffer->length ()
604 ? lexer->buffer->address ()
605 : &eof_token;
607 /* Subsequent preprocessor diagnostics should use compiler
608 diagnostic functions to get the compiler source location. */
609 done_lexing = true;
611 gcc_assert (!lexer->next_token->purged_p);
612 return lexer;
615 /* Create a new lexer whose token stream is primed with the tokens in
616 CACHE. When these tokens are exhausted, no new tokens will be read. */
618 static cp_lexer *
619 cp_lexer_new_from_tokens (cp_token_cache *cache)
621 cp_token *first = cache->first;
622 cp_token *last = cache->last;
623 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
625 /* We do not own the buffer. */
626 lexer->buffer = NULL;
627 lexer->next_token = first == last ? &eof_token : first;
628 lexer->last_token = last;
630 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
632 /* Initially we are not debugging. */
633 lexer->debugging_p = false;
635 gcc_assert (!lexer->next_token->purged_p);
636 return lexer;
639 /* Frees all resources associated with LEXER. */
641 static void
642 cp_lexer_destroy (cp_lexer *lexer)
644 vec_free (lexer->buffer);
645 lexer->saved_tokens.release ();
646 ggc_free (lexer);
649 /* Returns nonzero if debugging information should be output. */
651 static inline bool
652 cp_lexer_debugging_p (cp_lexer *lexer)
654 return lexer->debugging_p;
658 static inline cp_token_position
659 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
661 gcc_assert (!previous_p || lexer->next_token != &eof_token);
663 return lexer->next_token - previous_p;
666 static inline cp_token *
667 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
669 return pos;
672 static inline void
673 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
675 lexer->next_token = cp_lexer_token_at (lexer, pos);
678 static inline cp_token_position
679 cp_lexer_previous_token_position (cp_lexer *lexer)
681 if (lexer->next_token == &eof_token)
682 return lexer->last_token - 1;
683 else
684 return cp_lexer_token_position (lexer, true);
687 static inline cp_token *
688 cp_lexer_previous_token (cp_lexer *lexer)
690 cp_token_position tp = cp_lexer_previous_token_position (lexer);
692 return cp_lexer_token_at (lexer, tp);
695 /* nonzero if we are presently saving tokens. */
697 static inline int
698 cp_lexer_saving_tokens (const cp_lexer* lexer)
700 return lexer->saved_tokens.length () != 0;
703 /* Store the next token from the preprocessor in *TOKEN. Return true
704 if we reach EOF. If LEXER is NULL, assume we are handling an
705 initial #pragma pch_preprocess, and thus want the lexer to return
706 processed strings. */
708 static void
709 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
711 static int is_extern_c = 0;
713 /* Get a new token from the preprocessor. */
714 token->type
715 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
716 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
717 token->keyword = RID_MAX;
718 token->pragma_kind = PRAGMA_NONE;
719 token->purged_p = false;
721 /* On some systems, some header files are surrounded by an
722 implicit extern "C" block. Set a flag in the token if it
723 comes from such a header. */
724 is_extern_c += pending_lang_change;
725 pending_lang_change = 0;
726 token->implicit_extern_c = is_extern_c > 0;
728 /* Check to see if this token is a keyword. */
729 if (token->type == CPP_NAME)
731 if (C_IS_RESERVED_WORD (token->u.value))
733 /* Mark this token as a keyword. */
734 token->type = CPP_KEYWORD;
735 /* Record which keyword. */
736 token->keyword = C_RID_CODE (token->u.value);
738 else
740 if (warn_cxx0x_compat
741 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
742 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
744 /* Warn about the C++0x keyword (but still treat it as
745 an identifier). */
746 warning (OPT_Wc__0x_compat,
747 "identifier %qE is a keyword in C++11",
748 token->u.value);
750 /* Clear out the C_RID_CODE so we don't warn about this
751 particular identifier-turned-keyword again. */
752 C_SET_RID_CODE (token->u.value, RID_MAX);
755 token->ambiguous_p = false;
756 token->keyword = RID_MAX;
759 else if (token->type == CPP_AT_NAME)
761 /* This only happens in Objective-C++; it must be a keyword. */
762 token->type = CPP_KEYWORD;
763 switch (C_RID_CODE (token->u.value))
765 /* Replace 'class' with '@class', 'private' with '@private',
766 etc. This prevents confusion with the C++ keyword
767 'class', and makes the tokens consistent with other
768 Objective-C 'AT' keywords. For example '@class' is
769 reported as RID_AT_CLASS which is consistent with
770 '@synchronized', which is reported as
771 RID_AT_SYNCHRONIZED.
773 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
774 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
775 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
776 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
777 case RID_THROW: token->keyword = RID_AT_THROW; break;
778 case RID_TRY: token->keyword = RID_AT_TRY; break;
779 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
780 default: token->keyword = C_RID_CODE (token->u.value);
783 else if (token->type == CPP_PRAGMA)
785 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
786 token->pragma_kind = ((enum pragma_kind)
787 TREE_INT_CST_LOW (token->u.value));
788 token->u.value = NULL_TREE;
792 /* Update the globals input_location and the input file stack from TOKEN. */
793 static inline void
794 cp_lexer_set_source_position_from_token (cp_token *token)
796 if (token->type != CPP_EOF)
798 input_location = token->location;
802 /* Return a pointer to the next token in the token stream, but do not
803 consume it. */
805 static inline cp_token *
806 cp_lexer_peek_token (cp_lexer *lexer)
808 if (cp_lexer_debugging_p (lexer))
810 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
811 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
812 putc ('\n', cp_lexer_debug_stream);
814 return lexer->next_token;
817 /* Return true if the next token has the indicated TYPE. */
819 static inline bool
820 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
822 return cp_lexer_peek_token (lexer)->type == type;
825 /* Return true if the next token does not have the indicated TYPE. */
827 static inline bool
828 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
830 return !cp_lexer_next_token_is (lexer, type);
833 /* Return true if the next token is the indicated KEYWORD. */
835 static inline bool
836 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
838 return cp_lexer_peek_token (lexer)->keyword == keyword;
841 /* Return true if the next token is not the indicated KEYWORD. */
843 static inline bool
844 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
846 return cp_lexer_peek_token (lexer)->keyword != keyword;
849 /* Return true if the next token is a keyword for a decl-specifier. */
851 static bool
852 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
854 cp_token *token;
856 token = cp_lexer_peek_token (lexer);
857 switch (token->keyword)
859 /* auto specifier: storage-class-specifier in C++,
860 simple-type-specifier in C++0x. */
861 case RID_AUTO:
862 /* Storage classes. */
863 case RID_REGISTER:
864 case RID_STATIC:
865 case RID_EXTERN:
866 case RID_MUTABLE:
867 case RID_THREAD:
868 /* Elaborated type specifiers. */
869 case RID_ENUM:
870 case RID_CLASS:
871 case RID_STRUCT:
872 case RID_UNION:
873 case RID_TYPENAME:
874 /* Simple type specifiers. */
875 case RID_CHAR:
876 case RID_CHAR16:
877 case RID_CHAR32:
878 case RID_WCHAR:
879 case RID_BOOL:
880 case RID_SHORT:
881 case RID_INT:
882 case RID_LONG:
883 case RID_INT128:
884 case RID_SIGNED:
885 case RID_UNSIGNED:
886 case RID_FLOAT:
887 case RID_DOUBLE:
888 case RID_VOID:
889 /* GNU extensions. */
890 case RID_ATTRIBUTE:
891 case RID_TYPEOF:
892 /* C++0x extensions. */
893 case RID_DECLTYPE:
894 case RID_UNDERLYING_TYPE:
895 return true;
897 default:
898 return false;
902 /* Returns TRUE iff the token T begins a decltype type. */
904 static bool
905 token_is_decltype (cp_token *t)
907 return (t->keyword == RID_DECLTYPE
908 || t->type == CPP_DECLTYPE);
911 /* Returns TRUE iff the next token begins a decltype type. */
913 static bool
914 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
916 cp_token *t = cp_lexer_peek_token (lexer);
917 return token_is_decltype (t);
920 /* Return a pointer to the Nth token in the token stream. If N is 1,
921 then this is precisely equivalent to cp_lexer_peek_token (except
922 that it is not inline). One would like to disallow that case, but
923 there is one case (cp_parser_nth_token_starts_template_id) where
924 the caller passes a variable for N and it might be 1. */
926 static cp_token *
927 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
929 cp_token *token;
931 /* N is 1-based, not zero-based. */
932 gcc_assert (n > 0);
934 if (cp_lexer_debugging_p (lexer))
935 fprintf (cp_lexer_debug_stream,
936 "cp_lexer: peeking ahead %ld at token: ", (long)n);
938 --n;
939 token = lexer->next_token;
940 gcc_assert (!n || token != &eof_token);
941 while (n != 0)
943 ++token;
944 if (token == lexer->last_token)
946 token = &eof_token;
947 break;
950 if (!token->purged_p)
951 --n;
954 if (cp_lexer_debugging_p (lexer))
956 cp_lexer_print_token (cp_lexer_debug_stream, token);
957 putc ('\n', cp_lexer_debug_stream);
960 return token;
963 /* Return the next token, and advance the lexer's next_token pointer
964 to point to the next non-purged token. */
966 static cp_token *
967 cp_lexer_consume_token (cp_lexer* lexer)
969 cp_token *token = lexer->next_token;
971 gcc_assert (token != &eof_token);
972 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
976 lexer->next_token++;
977 if (lexer->next_token == lexer->last_token)
979 lexer->next_token = &eof_token;
980 break;
984 while (lexer->next_token->purged_p);
986 cp_lexer_set_source_position_from_token (token);
988 /* Provide debugging output. */
989 if (cp_lexer_debugging_p (lexer))
991 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
992 cp_lexer_print_token (cp_lexer_debug_stream, token);
993 putc ('\n', cp_lexer_debug_stream);
996 return token;
999 /* Permanently remove the next token from the token stream, and
1000 advance the next_token pointer to refer to the next non-purged
1001 token. */
1003 static void
1004 cp_lexer_purge_token (cp_lexer *lexer)
1006 cp_token *tok = lexer->next_token;
1008 gcc_assert (tok != &eof_token);
1009 tok->purged_p = true;
1010 tok->location = UNKNOWN_LOCATION;
1011 tok->u.value = NULL_TREE;
1012 tok->keyword = RID_MAX;
1016 tok++;
1017 if (tok == lexer->last_token)
1019 tok = &eof_token;
1020 break;
1023 while (tok->purged_p);
1024 lexer->next_token = tok;
1027 /* Permanently remove all tokens after TOK, up to, but not
1028 including, the token that will be returned next by
1029 cp_lexer_peek_token. */
1031 static void
1032 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1034 cp_token *peek = lexer->next_token;
1036 if (peek == &eof_token)
1037 peek = lexer->last_token;
1039 gcc_assert (tok < peek);
1041 for ( tok += 1; tok != peek; tok += 1)
1043 tok->purged_p = true;
1044 tok->location = UNKNOWN_LOCATION;
1045 tok->u.value = NULL_TREE;
1046 tok->keyword = RID_MAX;
1050 /* Begin saving tokens. All tokens consumed after this point will be
1051 preserved. */
1053 static void
1054 cp_lexer_save_tokens (cp_lexer* lexer)
1056 /* Provide debugging output. */
1057 if (cp_lexer_debugging_p (lexer))
1058 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1060 lexer->saved_tokens.safe_push (lexer->next_token);
1063 /* Commit to the portion of the token stream most recently saved. */
1065 static void
1066 cp_lexer_commit_tokens (cp_lexer* lexer)
1068 /* Provide debugging output. */
1069 if (cp_lexer_debugging_p (lexer))
1070 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1072 lexer->saved_tokens.pop ();
1075 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1076 to the token stream. Stop saving tokens. */
1078 static void
1079 cp_lexer_rollback_tokens (cp_lexer* lexer)
1081 /* Provide debugging output. */
1082 if (cp_lexer_debugging_p (lexer))
1083 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1085 lexer->next_token = lexer->saved_tokens.pop ();
1088 /* Print a representation of the TOKEN on the STREAM. */
1090 static void
1091 cp_lexer_print_token (FILE * stream, cp_token *token)
1093 /* We don't use cpp_type2name here because the parser defines
1094 a few tokens of its own. */
1095 static const char *const token_names[] = {
1096 /* cpplib-defined token types */
1097 #define OP(e, s) #e,
1098 #define TK(e, s) #e,
1099 TTYPE_TABLE
1100 #undef OP
1101 #undef TK
1102 /* C++ parser token types - see "Manifest constants", above. */
1103 "KEYWORD",
1104 "TEMPLATE_ID",
1105 "NESTED_NAME_SPECIFIER",
1108 /* For some tokens, print the associated data. */
1109 switch (token->type)
1111 case CPP_KEYWORD:
1112 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1113 For example, `struct' is mapped to an INTEGER_CST. */
1114 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1115 break;
1116 /* else fall through */
1117 case CPP_NAME:
1118 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1119 break;
1121 case CPP_STRING:
1122 case CPP_STRING16:
1123 case CPP_STRING32:
1124 case CPP_WSTRING:
1125 case CPP_UTF8STRING:
1126 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1127 break;
1129 case CPP_NUMBER:
1130 print_generic_expr (stream, token->u.value, 0);
1131 break;
1133 default:
1134 /* If we have a name for the token, print it out. Otherwise, we
1135 simply give the numeric code. */
1136 if (token->type < ARRAY_SIZE(token_names))
1137 fputs (token_names[token->type], stream);
1138 else
1139 fprintf (stream, "[%d]", token->type);
1140 break;
1144 /* Start emitting debugging information. */
1146 static void
1147 cp_lexer_start_debugging (cp_lexer* lexer)
1149 lexer->debugging_p = true;
1150 cp_lexer_debug_stream = stderr;
1153 /* Stop emitting debugging information. */
1155 static void
1156 cp_lexer_stop_debugging (cp_lexer* lexer)
1158 lexer->debugging_p = false;
1159 cp_lexer_debug_stream = NULL;
1162 /* Create a new cp_token_cache, representing a range of tokens. */
1164 static cp_token_cache *
1165 cp_token_cache_new (cp_token *first, cp_token *last)
1167 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1168 cache->first = first;
1169 cache->last = last;
1170 return cache;
1174 /* Decl-specifiers. */
1176 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1178 static void
1179 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1181 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1184 /* Declarators. */
1186 /* Nothing other than the parser should be creating declarators;
1187 declarators are a semi-syntactic representation of C++ entities.
1188 Other parts of the front end that need to create entities (like
1189 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1191 static cp_declarator *make_call_declarator
1192 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
1193 static cp_declarator *make_array_declarator
1194 (cp_declarator *, tree);
1195 static cp_declarator *make_pointer_declarator
1196 (cp_cv_quals, cp_declarator *, tree);
1197 static cp_declarator *make_reference_declarator
1198 (cp_cv_quals, cp_declarator *, bool, tree);
1199 static cp_parameter_declarator *make_parameter_declarator
1200 (cp_decl_specifier_seq *, cp_declarator *, tree);
1201 static cp_declarator *make_ptrmem_declarator
1202 (cp_cv_quals, tree, cp_declarator *, tree);
1204 /* An erroneous declarator. */
1205 static cp_declarator *cp_error_declarator;
1207 /* The obstack on which declarators and related data structures are
1208 allocated. */
1209 static struct obstack declarator_obstack;
1211 /* Alloc BYTES from the declarator memory pool. */
1213 static inline void *
1214 alloc_declarator (size_t bytes)
1216 return obstack_alloc (&declarator_obstack, bytes);
1219 /* Allocate a declarator of the indicated KIND. Clear fields that are
1220 common to all declarators. */
1222 static cp_declarator *
1223 make_declarator (cp_declarator_kind kind)
1225 cp_declarator *declarator;
1227 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1228 declarator->kind = kind;
1229 declarator->attributes = NULL_TREE;
1230 declarator->std_attributes = NULL_TREE;
1231 declarator->declarator = NULL;
1232 declarator->parameter_pack_p = false;
1233 declarator->id_loc = UNKNOWN_LOCATION;
1235 return declarator;
1238 /* Make a declarator for a generalized identifier. If
1239 QUALIFYING_SCOPE is non-NULL, the identifier is
1240 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1241 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1242 is, if any. */
1244 static cp_declarator *
1245 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1246 special_function_kind sfk)
1248 cp_declarator *declarator;
1250 /* It is valid to write:
1252 class C { void f(); };
1253 typedef C D;
1254 void D::f();
1256 The standard is not clear about whether `typedef const C D' is
1257 legal; as of 2002-09-15 the committee is considering that
1258 question. EDG 3.0 allows that syntax. Therefore, we do as
1259 well. */
1260 if (qualifying_scope && TYPE_P (qualifying_scope))
1261 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1263 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1264 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1265 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1267 declarator = make_declarator (cdk_id);
1268 declarator->u.id.qualifying_scope = qualifying_scope;
1269 declarator->u.id.unqualified_name = unqualified_name;
1270 declarator->u.id.sfk = sfk;
1272 return declarator;
1275 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1276 of modifiers such as const or volatile to apply to the pointer
1277 type, represented as identifiers. ATTRIBUTES represent the attributes that
1278 appertain to the pointer or reference. */
1280 cp_declarator *
1281 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1282 tree attributes)
1284 cp_declarator *declarator;
1286 declarator = make_declarator (cdk_pointer);
1287 declarator->declarator = target;
1288 declarator->u.pointer.qualifiers = cv_qualifiers;
1289 declarator->u.pointer.class_type = NULL_TREE;
1290 if (target)
1292 declarator->id_loc = target->id_loc;
1293 declarator->parameter_pack_p = target->parameter_pack_p;
1294 target->parameter_pack_p = false;
1296 else
1297 declarator->parameter_pack_p = false;
1299 declarator->std_attributes = attributes;
1301 return declarator;
1304 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1305 represent the attributes that appertain to the pointer or
1306 reference. */
1308 cp_declarator *
1309 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1310 bool rvalue_ref, tree attributes)
1312 cp_declarator *declarator;
1314 declarator = make_declarator (cdk_reference);
1315 declarator->declarator = target;
1316 declarator->u.reference.qualifiers = cv_qualifiers;
1317 declarator->u.reference.rvalue_ref = rvalue_ref;
1318 if (target)
1320 declarator->id_loc = target->id_loc;
1321 declarator->parameter_pack_p = target->parameter_pack_p;
1322 target->parameter_pack_p = false;
1324 else
1325 declarator->parameter_pack_p = false;
1327 declarator->std_attributes = attributes;
1329 return declarator;
1332 /* Like make_pointer_declarator -- but for a pointer to a non-static
1333 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1334 appertain to the pointer or reference. */
1336 cp_declarator *
1337 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1338 cp_declarator *pointee,
1339 tree attributes)
1341 cp_declarator *declarator;
1343 declarator = make_declarator (cdk_ptrmem);
1344 declarator->declarator = pointee;
1345 declarator->u.pointer.qualifiers = cv_qualifiers;
1346 declarator->u.pointer.class_type = class_type;
1348 if (pointee)
1350 declarator->parameter_pack_p = pointee->parameter_pack_p;
1351 pointee->parameter_pack_p = false;
1353 else
1354 declarator->parameter_pack_p = false;
1356 declarator->std_attributes = attributes;
1358 return declarator;
1361 /* Make a declarator for the function given by TARGET, with the
1362 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1363 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1364 indicates what exceptions can be thrown. */
1366 cp_declarator *
1367 make_call_declarator (cp_declarator *target,
1368 tree parms,
1369 cp_cv_quals cv_qualifiers,
1370 cp_virt_specifiers virt_specifiers,
1371 tree exception_specification,
1372 tree late_return_type)
1374 cp_declarator *declarator;
1376 declarator = make_declarator (cdk_function);
1377 declarator->declarator = target;
1378 declarator->u.function.parameters = parms;
1379 declarator->u.function.qualifiers = cv_qualifiers;
1380 declarator->u.function.virt_specifiers = virt_specifiers;
1381 declarator->u.function.exception_specification = exception_specification;
1382 declarator->u.function.late_return_type = late_return_type;
1383 if (target)
1385 declarator->id_loc = target->id_loc;
1386 declarator->parameter_pack_p = target->parameter_pack_p;
1387 target->parameter_pack_p = false;
1389 else
1390 declarator->parameter_pack_p = false;
1392 return declarator;
1395 /* Make a declarator for an array of BOUNDS elements, each of which is
1396 defined by ELEMENT. */
1398 cp_declarator *
1399 make_array_declarator (cp_declarator *element, tree bounds)
1401 cp_declarator *declarator;
1403 declarator = make_declarator (cdk_array);
1404 declarator->declarator = element;
1405 declarator->u.array.bounds = bounds;
1406 if (element)
1408 declarator->id_loc = element->id_loc;
1409 declarator->parameter_pack_p = element->parameter_pack_p;
1410 element->parameter_pack_p = false;
1412 else
1413 declarator->parameter_pack_p = false;
1415 return declarator;
1418 /* Determine whether the declarator we've seen so far can be a
1419 parameter pack, when followed by an ellipsis. */
1420 static bool
1421 declarator_can_be_parameter_pack (cp_declarator *declarator)
1423 /* Search for a declarator name, or any other declarator that goes
1424 after the point where the ellipsis could appear in a parameter
1425 pack. If we find any of these, then this declarator can not be
1426 made into a parameter pack. */
1427 bool found = false;
1428 while (declarator && !found)
1430 switch ((int)declarator->kind)
1432 case cdk_id:
1433 case cdk_array:
1434 found = true;
1435 break;
1437 case cdk_error:
1438 return true;
1440 default:
1441 declarator = declarator->declarator;
1442 break;
1446 return !found;
1449 cp_parameter_declarator *no_parameters;
1451 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1452 DECLARATOR and DEFAULT_ARGUMENT. */
1454 cp_parameter_declarator *
1455 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1456 cp_declarator *declarator,
1457 tree default_argument)
1459 cp_parameter_declarator *parameter;
1461 parameter = ((cp_parameter_declarator *)
1462 alloc_declarator (sizeof (cp_parameter_declarator)));
1463 parameter->next = NULL;
1464 if (decl_specifiers)
1465 parameter->decl_specifiers = *decl_specifiers;
1466 else
1467 clear_decl_specs (&parameter->decl_specifiers);
1468 parameter->declarator = declarator;
1469 parameter->default_argument = default_argument;
1470 parameter->ellipsis_p = false;
1472 return parameter;
1475 /* Returns true iff DECLARATOR is a declaration for a function. */
1477 static bool
1478 function_declarator_p (const cp_declarator *declarator)
1480 while (declarator)
1482 if (declarator->kind == cdk_function
1483 && declarator->declarator->kind == cdk_id)
1484 return true;
1485 if (declarator->kind == cdk_id
1486 || declarator->kind == cdk_error)
1487 return false;
1488 declarator = declarator->declarator;
1490 return false;
1493 /* The parser. */
1495 /* Overview
1496 --------
1498 A cp_parser parses the token stream as specified by the C++
1499 grammar. Its job is purely parsing, not semantic analysis. For
1500 example, the parser breaks the token stream into declarators,
1501 expressions, statements, and other similar syntactic constructs.
1502 It does not check that the types of the expressions on either side
1503 of an assignment-statement are compatible, or that a function is
1504 not declared with a parameter of type `void'.
1506 The parser invokes routines elsewhere in the compiler to perform
1507 semantic analysis and to build up the abstract syntax tree for the
1508 code processed.
1510 The parser (and the template instantiation code, which is, in a
1511 way, a close relative of parsing) are the only parts of the
1512 compiler that should be calling push_scope and pop_scope, or
1513 related functions. The parser (and template instantiation code)
1514 keeps track of what scope is presently active; everything else
1515 should simply honor that. (The code that generates static
1516 initializers may also need to set the scope, in order to check
1517 access control correctly when emitting the initializers.)
1519 Methodology
1520 -----------
1522 The parser is of the standard recursive-descent variety. Upcoming
1523 tokens in the token stream are examined in order to determine which
1524 production to use when parsing a non-terminal. Some C++ constructs
1525 require arbitrary look ahead to disambiguate. For example, it is
1526 impossible, in the general case, to tell whether a statement is an
1527 expression or declaration without scanning the entire statement.
1528 Therefore, the parser is capable of "parsing tentatively." When the
1529 parser is not sure what construct comes next, it enters this mode.
1530 Then, while we attempt to parse the construct, the parser queues up
1531 error messages, rather than issuing them immediately, and saves the
1532 tokens it consumes. If the construct is parsed successfully, the
1533 parser "commits", i.e., it issues any queued error messages and
1534 the tokens that were being preserved are permanently discarded.
1535 If, however, the construct is not parsed successfully, the parser
1536 rolls back its state completely so that it can resume parsing using
1537 a different alternative.
1539 Future Improvements
1540 -------------------
1542 The performance of the parser could probably be improved substantially.
1543 We could often eliminate the need to parse tentatively by looking ahead
1544 a little bit. In some places, this approach might not entirely eliminate
1545 the need to parse tentatively, but it might still speed up the average
1546 case. */
1548 /* Flags that are passed to some parsing functions. These values can
1549 be bitwise-ored together. */
1551 enum
1553 /* No flags. */
1554 CP_PARSER_FLAGS_NONE = 0x0,
1555 /* The construct is optional. If it is not present, then no error
1556 should be issued. */
1557 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1558 /* When parsing a type-specifier, treat user-defined type-names
1559 as non-type identifiers. */
1560 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1561 /* When parsing a type-specifier, do not try to parse a class-specifier
1562 or enum-specifier. */
1563 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1564 /* When parsing a decl-specifier-seq, only allow type-specifier or
1565 constexpr. */
1566 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1569 /* This type is used for parameters and variables which hold
1570 combinations of the above flags. */
1571 typedef int cp_parser_flags;
1573 /* The different kinds of declarators we want to parse. */
1575 typedef enum cp_parser_declarator_kind
1577 /* We want an abstract declarator. */
1578 CP_PARSER_DECLARATOR_ABSTRACT,
1579 /* We want a named declarator. */
1580 CP_PARSER_DECLARATOR_NAMED,
1581 /* We don't mind, but the name must be an unqualified-id. */
1582 CP_PARSER_DECLARATOR_EITHER
1583 } cp_parser_declarator_kind;
1585 /* The precedence values used to parse binary expressions. The minimum value
1586 of PREC must be 1, because zero is reserved to quickly discriminate
1587 binary operators from other tokens. */
1589 enum cp_parser_prec
1591 PREC_NOT_OPERATOR,
1592 PREC_LOGICAL_OR_EXPRESSION,
1593 PREC_LOGICAL_AND_EXPRESSION,
1594 PREC_INCLUSIVE_OR_EXPRESSION,
1595 PREC_EXCLUSIVE_OR_EXPRESSION,
1596 PREC_AND_EXPRESSION,
1597 PREC_EQUALITY_EXPRESSION,
1598 PREC_RELATIONAL_EXPRESSION,
1599 PREC_SHIFT_EXPRESSION,
1600 PREC_ADDITIVE_EXPRESSION,
1601 PREC_MULTIPLICATIVE_EXPRESSION,
1602 PREC_PM_EXPRESSION,
1603 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1606 /* A mapping from a token type to a corresponding tree node type, with a
1607 precedence value. */
1609 typedef struct cp_parser_binary_operations_map_node
1611 /* The token type. */
1612 enum cpp_ttype token_type;
1613 /* The corresponding tree code. */
1614 enum tree_code tree_type;
1615 /* The precedence of this operator. */
1616 enum cp_parser_prec prec;
1617 } cp_parser_binary_operations_map_node;
1619 typedef struct cp_parser_expression_stack_entry
1621 /* Left hand side of the binary operation we are currently
1622 parsing. */
1623 tree lhs;
1624 /* Original tree code for left hand side, if it was a binary
1625 expression itself (used for -Wparentheses). */
1626 enum tree_code lhs_type;
1627 /* Tree code for the binary operation we are parsing. */
1628 enum tree_code tree_type;
1629 /* Precedence of the binary operation we are parsing. */
1630 enum cp_parser_prec prec;
1631 /* Location of the binary operation we are parsing. */
1632 location_t loc;
1633 } cp_parser_expression_stack_entry;
1635 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1636 entries because precedence levels on the stack are monotonically
1637 increasing. */
1638 typedef struct cp_parser_expression_stack_entry
1639 cp_parser_expression_stack[NUM_PREC_VALUES];
1641 /* Prototypes. */
1643 /* Constructors and destructors. */
1645 static cp_parser_context *cp_parser_context_new
1646 (cp_parser_context *);
1648 /* Class variables. */
1650 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1652 /* The operator-precedence table used by cp_parser_binary_expression.
1653 Transformed into an associative array (binops_by_token) by
1654 cp_parser_new. */
1656 static const cp_parser_binary_operations_map_node binops[] = {
1657 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1658 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1660 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1661 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1662 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1664 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1665 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1667 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1668 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1670 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1671 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1672 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1673 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1675 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1676 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1678 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1680 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1682 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1684 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1686 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1689 /* The same as binops, but initialized by cp_parser_new so that
1690 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1691 for speed. */
1692 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1694 /* Constructors and destructors. */
1696 /* Construct a new context. The context below this one on the stack
1697 is given by NEXT. */
1699 static cp_parser_context *
1700 cp_parser_context_new (cp_parser_context* next)
1702 cp_parser_context *context;
1704 /* Allocate the storage. */
1705 if (cp_parser_context_free_list != NULL)
1707 /* Pull the first entry from the free list. */
1708 context = cp_parser_context_free_list;
1709 cp_parser_context_free_list = context->next;
1710 memset (context, 0, sizeof (*context));
1712 else
1713 context = ggc_alloc_cleared_cp_parser_context ();
1715 /* No errors have occurred yet in this context. */
1716 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1717 /* If this is not the bottommost context, copy information that we
1718 need from the previous context. */
1719 if (next)
1721 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1722 expression, then we are parsing one in this context, too. */
1723 context->object_type = next->object_type;
1724 /* Thread the stack. */
1725 context->next = next;
1728 return context;
1731 /* Managing the unparsed function queues. */
1733 #define unparsed_funs_with_default_args \
1734 parser->unparsed_queues->last ().funs_with_default_args
1735 #define unparsed_funs_with_definitions \
1736 parser->unparsed_queues->last ().funs_with_definitions
1737 #define unparsed_nsdmis \
1738 parser->unparsed_queues->last ().nsdmis
1740 static void
1741 push_unparsed_function_queues (cp_parser *parser)
1743 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL};
1744 vec_safe_push (parser->unparsed_queues, e);
1747 static void
1748 pop_unparsed_function_queues (cp_parser *parser)
1750 release_tree_vector (unparsed_funs_with_definitions);
1751 parser->unparsed_queues->pop ();
1754 /* Prototypes. */
1756 /* Constructors and destructors. */
1758 static cp_parser *cp_parser_new
1759 (void);
1761 /* Routines to parse various constructs.
1763 Those that return `tree' will return the error_mark_node (rather
1764 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1765 Sometimes, they will return an ordinary node if error-recovery was
1766 attempted, even though a parse error occurred. So, to check
1767 whether or not a parse error occurred, you should always use
1768 cp_parser_error_occurred. If the construct is optional (indicated
1769 either by an `_opt' in the name of the function that does the
1770 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1771 the construct is not present. */
1773 /* Lexical conventions [gram.lex] */
1775 static tree cp_parser_identifier
1776 (cp_parser *);
1777 static tree cp_parser_string_literal
1778 (cp_parser *, bool, bool);
1779 static tree cp_parser_userdef_char_literal
1780 (cp_parser *);
1781 static tree cp_parser_userdef_string_literal
1782 (cp_token *);
1783 static tree cp_parser_userdef_numeric_literal
1784 (cp_parser *);
1786 /* Basic concepts [gram.basic] */
1788 static bool cp_parser_translation_unit
1789 (cp_parser *);
1791 /* Expressions [gram.expr] */
1793 static tree cp_parser_primary_expression
1794 (cp_parser *, bool, bool, bool, cp_id_kind *);
1795 static tree cp_parser_id_expression
1796 (cp_parser *, bool, bool, bool *, bool, bool);
1797 static tree cp_parser_unqualified_id
1798 (cp_parser *, bool, bool, bool, bool);
1799 static tree cp_parser_nested_name_specifier_opt
1800 (cp_parser *, bool, bool, bool, bool);
1801 static tree cp_parser_nested_name_specifier
1802 (cp_parser *, bool, bool, bool, bool);
1803 static tree cp_parser_qualifying_entity
1804 (cp_parser *, bool, bool, bool, bool, bool);
1805 static tree cp_parser_postfix_expression
1806 (cp_parser *, bool, bool, bool, cp_id_kind *);
1807 static tree cp_parser_postfix_open_square_expression
1808 (cp_parser *, tree, bool);
1809 static tree cp_parser_postfix_dot_deref_expression
1810 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1811 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1812 (cp_parser *, int, bool, bool, bool *);
1813 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1814 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1815 static void cp_parser_pseudo_destructor_name
1816 (cp_parser *, tree *, tree *);
1817 static tree cp_parser_unary_expression
1818 (cp_parser *, bool, bool, cp_id_kind *);
1819 static enum tree_code cp_parser_unary_operator
1820 (cp_token *);
1821 static tree cp_parser_new_expression
1822 (cp_parser *);
1823 static vec<tree, va_gc> *cp_parser_new_placement
1824 (cp_parser *);
1825 static tree cp_parser_new_type_id
1826 (cp_parser *, tree *);
1827 static cp_declarator *cp_parser_new_declarator_opt
1828 (cp_parser *);
1829 static cp_declarator *cp_parser_direct_new_declarator
1830 (cp_parser *);
1831 static vec<tree, va_gc> *cp_parser_new_initializer
1832 (cp_parser *);
1833 static tree cp_parser_delete_expression
1834 (cp_parser *);
1835 static tree cp_parser_cast_expression
1836 (cp_parser *, bool, bool, cp_id_kind *);
1837 static tree cp_parser_binary_expression
1838 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1839 static tree cp_parser_question_colon_clause
1840 (cp_parser *, tree);
1841 static tree cp_parser_assignment_expression
1842 (cp_parser *, bool, cp_id_kind *);
1843 static enum tree_code cp_parser_assignment_operator_opt
1844 (cp_parser *);
1845 static tree cp_parser_expression
1846 (cp_parser *, bool, cp_id_kind *);
1847 static tree cp_parser_constant_expression
1848 (cp_parser *, bool, bool *);
1849 static tree cp_parser_builtin_offsetof
1850 (cp_parser *);
1851 static tree cp_parser_lambda_expression
1852 (cp_parser *);
1853 static void cp_parser_lambda_introducer
1854 (cp_parser *, tree);
1855 static bool cp_parser_lambda_declarator_opt
1856 (cp_parser *, tree);
1857 static void cp_parser_lambda_body
1858 (cp_parser *, tree);
1860 /* Statements [gram.stmt.stmt] */
1862 static void cp_parser_statement
1863 (cp_parser *, tree, bool, bool *);
1864 static void cp_parser_label_for_labeled_statement
1865 (cp_parser *, tree);
1866 static tree cp_parser_expression_statement
1867 (cp_parser *, tree);
1868 static tree cp_parser_compound_statement
1869 (cp_parser *, tree, bool, bool);
1870 static void cp_parser_statement_seq_opt
1871 (cp_parser *, tree);
1872 static tree cp_parser_selection_statement
1873 (cp_parser *, bool *);
1874 static tree cp_parser_condition
1875 (cp_parser *);
1876 static tree cp_parser_iteration_statement
1877 (cp_parser *);
1878 static bool cp_parser_for_init_statement
1879 (cp_parser *, tree *decl);
1880 static tree cp_parser_for
1881 (cp_parser *);
1882 static tree cp_parser_c_for
1883 (cp_parser *, tree, tree);
1884 static tree cp_parser_range_for
1885 (cp_parser *, tree, tree, tree);
1886 static void do_range_for_auto_deduction
1887 (tree, tree);
1888 static tree cp_parser_perform_range_for_lookup
1889 (tree, tree *, tree *);
1890 static tree cp_parser_range_for_member_function
1891 (tree, tree);
1892 static tree cp_parser_jump_statement
1893 (cp_parser *);
1894 static void cp_parser_declaration_statement
1895 (cp_parser *);
1897 static tree cp_parser_implicitly_scoped_statement
1898 (cp_parser *, bool *);
1899 static void cp_parser_already_scoped_statement
1900 (cp_parser *);
1902 /* Declarations [gram.dcl.dcl] */
1904 static void cp_parser_declaration_seq_opt
1905 (cp_parser *);
1906 static void cp_parser_declaration
1907 (cp_parser *);
1908 static void cp_parser_block_declaration
1909 (cp_parser *, bool);
1910 static void cp_parser_simple_declaration
1911 (cp_parser *, bool, tree *);
1912 static void cp_parser_decl_specifier_seq
1913 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1914 static tree cp_parser_storage_class_specifier_opt
1915 (cp_parser *);
1916 static tree cp_parser_function_specifier_opt
1917 (cp_parser *, cp_decl_specifier_seq *);
1918 static tree cp_parser_type_specifier
1919 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1920 int *, bool *);
1921 static tree cp_parser_simple_type_specifier
1922 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1923 static tree cp_parser_type_name
1924 (cp_parser *);
1925 static tree cp_parser_nonclass_name
1926 (cp_parser* parser);
1927 static tree cp_parser_elaborated_type_specifier
1928 (cp_parser *, bool, bool);
1929 static tree cp_parser_enum_specifier
1930 (cp_parser *);
1931 static void cp_parser_enumerator_list
1932 (cp_parser *, tree);
1933 static void cp_parser_enumerator_definition
1934 (cp_parser *, tree);
1935 static tree cp_parser_namespace_name
1936 (cp_parser *);
1937 static void cp_parser_namespace_definition
1938 (cp_parser *);
1939 static void cp_parser_namespace_body
1940 (cp_parser *);
1941 static tree cp_parser_qualified_namespace_specifier
1942 (cp_parser *);
1943 static void cp_parser_namespace_alias_definition
1944 (cp_parser *);
1945 static bool cp_parser_using_declaration
1946 (cp_parser *, bool);
1947 static void cp_parser_using_directive
1948 (cp_parser *);
1949 static tree cp_parser_alias_declaration
1950 (cp_parser *);
1951 static void cp_parser_asm_definition
1952 (cp_parser *);
1953 static void cp_parser_linkage_specification
1954 (cp_parser *);
1955 static void cp_parser_static_assert
1956 (cp_parser *, bool);
1957 static tree cp_parser_decltype
1958 (cp_parser *);
1960 /* Declarators [gram.dcl.decl] */
1962 static tree cp_parser_init_declarator
1963 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
1964 static cp_declarator *cp_parser_declarator
1965 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1966 static cp_declarator *cp_parser_direct_declarator
1967 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1968 static enum tree_code cp_parser_ptr_operator
1969 (cp_parser *, tree *, cp_cv_quals *, tree *);
1970 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1971 (cp_parser *);
1972 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1973 (cp_parser *);
1974 static tree cp_parser_late_return_type_opt
1975 (cp_parser *, cp_cv_quals);
1976 static tree cp_parser_declarator_id
1977 (cp_parser *, bool);
1978 static tree cp_parser_type_id
1979 (cp_parser *);
1980 static tree cp_parser_template_type_arg
1981 (cp_parser *);
1982 static tree cp_parser_trailing_type_id (cp_parser *);
1983 static tree cp_parser_type_id_1
1984 (cp_parser *, bool, bool);
1985 static void cp_parser_type_specifier_seq
1986 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1987 static tree cp_parser_parameter_declaration_clause
1988 (cp_parser *);
1989 static tree cp_parser_parameter_declaration_list
1990 (cp_parser *, bool *);
1991 static cp_parameter_declarator *cp_parser_parameter_declaration
1992 (cp_parser *, bool, bool *);
1993 static tree cp_parser_default_argument
1994 (cp_parser *, bool);
1995 static void cp_parser_function_body
1996 (cp_parser *, bool);
1997 static tree cp_parser_initializer
1998 (cp_parser *, bool *, bool *);
1999 static tree cp_parser_initializer_clause
2000 (cp_parser *, bool *);
2001 static tree cp_parser_braced_list
2002 (cp_parser*, bool*);
2003 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2004 (cp_parser *, bool *);
2006 static bool cp_parser_ctor_initializer_opt_and_function_body
2007 (cp_parser *, bool);
2009 /* Classes [gram.class] */
2011 static tree cp_parser_class_name
2012 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2013 static tree cp_parser_class_specifier
2014 (cp_parser *);
2015 static tree cp_parser_class_head
2016 (cp_parser *, bool *);
2017 static enum tag_types cp_parser_class_key
2018 (cp_parser *);
2019 static void cp_parser_member_specification_opt
2020 (cp_parser *);
2021 static void cp_parser_member_declaration
2022 (cp_parser *);
2023 static tree cp_parser_pure_specifier
2024 (cp_parser *);
2025 static tree cp_parser_constant_initializer
2026 (cp_parser *);
2028 /* Derived classes [gram.class.derived] */
2030 static tree cp_parser_base_clause
2031 (cp_parser *);
2032 static tree cp_parser_base_specifier
2033 (cp_parser *);
2035 /* Special member functions [gram.special] */
2037 static tree cp_parser_conversion_function_id
2038 (cp_parser *);
2039 static tree cp_parser_conversion_type_id
2040 (cp_parser *);
2041 static cp_declarator *cp_parser_conversion_declarator_opt
2042 (cp_parser *);
2043 static bool cp_parser_ctor_initializer_opt
2044 (cp_parser *);
2045 static void cp_parser_mem_initializer_list
2046 (cp_parser *);
2047 static tree cp_parser_mem_initializer
2048 (cp_parser *);
2049 static tree cp_parser_mem_initializer_id
2050 (cp_parser *);
2052 /* Overloading [gram.over] */
2054 static tree cp_parser_operator_function_id
2055 (cp_parser *);
2056 static tree cp_parser_operator
2057 (cp_parser *);
2059 /* Templates [gram.temp] */
2061 static void cp_parser_template_declaration
2062 (cp_parser *, bool);
2063 static tree cp_parser_template_parameter_list
2064 (cp_parser *);
2065 static tree cp_parser_template_parameter
2066 (cp_parser *, bool *, bool *);
2067 static tree cp_parser_type_parameter
2068 (cp_parser *, bool *);
2069 static tree cp_parser_template_id
2070 (cp_parser *, bool, bool, enum tag_types, bool);
2071 static tree cp_parser_template_name
2072 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2073 static tree cp_parser_template_argument_list
2074 (cp_parser *);
2075 static tree cp_parser_template_argument
2076 (cp_parser *);
2077 static void cp_parser_explicit_instantiation
2078 (cp_parser *);
2079 static void cp_parser_explicit_specialization
2080 (cp_parser *);
2082 /* Exception handling [gram.exception] */
2084 static tree cp_parser_try_block
2085 (cp_parser *);
2086 static bool cp_parser_function_try_block
2087 (cp_parser *);
2088 static void cp_parser_handler_seq
2089 (cp_parser *);
2090 static void cp_parser_handler
2091 (cp_parser *);
2092 static tree cp_parser_exception_declaration
2093 (cp_parser *);
2094 static tree cp_parser_throw_expression
2095 (cp_parser *);
2096 static tree cp_parser_exception_specification_opt
2097 (cp_parser *);
2098 static tree cp_parser_type_id_list
2099 (cp_parser *);
2101 /* GNU Extensions */
2103 static tree cp_parser_asm_specification_opt
2104 (cp_parser *);
2105 static tree cp_parser_asm_operand_list
2106 (cp_parser *);
2107 static tree cp_parser_asm_clobber_list
2108 (cp_parser *);
2109 static tree cp_parser_asm_label_list
2110 (cp_parser *);
2111 static bool cp_next_tokens_can_be_attribute_p
2112 (cp_parser *);
2113 static bool cp_next_tokens_can_be_gnu_attribute_p
2114 (cp_parser *);
2115 static bool cp_next_tokens_can_be_std_attribute_p
2116 (cp_parser *);
2117 static bool cp_nth_tokens_can_be_std_attribute_p
2118 (cp_parser *, size_t);
2119 static bool cp_nth_tokens_can_be_gnu_attribute_p
2120 (cp_parser *, size_t);
2121 static bool cp_nth_tokens_can_be_attribute_p
2122 (cp_parser *, size_t);
2123 static tree cp_parser_attributes_opt
2124 (cp_parser *);
2125 static tree cp_parser_gnu_attributes_opt
2126 (cp_parser *);
2127 static tree cp_parser_gnu_attribute_list
2128 (cp_parser *);
2129 static tree cp_parser_std_attribute
2130 (cp_parser *);
2131 static tree cp_parser_std_attribute_spec
2132 (cp_parser *);
2133 static tree cp_parser_std_attribute_spec_seq
2134 (cp_parser *);
2135 static bool cp_parser_extension_opt
2136 (cp_parser *, int *);
2137 static void cp_parser_label_declaration
2138 (cp_parser *);
2140 /* Transactional Memory Extensions */
2142 static tree cp_parser_transaction
2143 (cp_parser *, enum rid);
2144 static tree cp_parser_transaction_expression
2145 (cp_parser *, enum rid);
2146 static bool cp_parser_function_transaction
2147 (cp_parser *, enum rid);
2148 static tree cp_parser_transaction_cancel
2149 (cp_parser *);
2151 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2152 static bool cp_parser_pragma
2153 (cp_parser *, enum pragma_context);
2155 /* Objective-C++ Productions */
2157 static tree cp_parser_objc_message_receiver
2158 (cp_parser *);
2159 static tree cp_parser_objc_message_args
2160 (cp_parser *);
2161 static tree cp_parser_objc_message_expression
2162 (cp_parser *);
2163 static tree cp_parser_objc_encode_expression
2164 (cp_parser *);
2165 static tree cp_parser_objc_defs_expression
2166 (cp_parser *);
2167 static tree cp_parser_objc_protocol_expression
2168 (cp_parser *);
2169 static tree cp_parser_objc_selector_expression
2170 (cp_parser *);
2171 static tree cp_parser_objc_expression
2172 (cp_parser *);
2173 static bool cp_parser_objc_selector_p
2174 (enum cpp_ttype);
2175 static tree cp_parser_objc_selector
2176 (cp_parser *);
2177 static tree cp_parser_objc_protocol_refs_opt
2178 (cp_parser *);
2179 static void cp_parser_objc_declaration
2180 (cp_parser *, tree);
2181 static tree cp_parser_objc_statement
2182 (cp_parser *);
2183 static bool cp_parser_objc_valid_prefix_attributes
2184 (cp_parser *, tree *);
2185 static void cp_parser_objc_at_property_declaration
2186 (cp_parser *) ;
2187 static void cp_parser_objc_at_synthesize_declaration
2188 (cp_parser *) ;
2189 static void cp_parser_objc_at_dynamic_declaration
2190 (cp_parser *) ;
2191 static tree cp_parser_objc_struct_declaration
2192 (cp_parser *) ;
2194 /* Utility Routines */
2196 static tree cp_parser_lookup_name
2197 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2198 static tree cp_parser_lookup_name_simple
2199 (cp_parser *, tree, location_t);
2200 static tree cp_parser_maybe_treat_template_as_class
2201 (tree, bool);
2202 static bool cp_parser_check_declarator_template_parameters
2203 (cp_parser *, cp_declarator *, location_t);
2204 static bool cp_parser_check_template_parameters
2205 (cp_parser *, unsigned, location_t, cp_declarator *);
2206 static tree cp_parser_simple_cast_expression
2207 (cp_parser *);
2208 static tree cp_parser_global_scope_opt
2209 (cp_parser *, bool);
2210 static bool cp_parser_constructor_declarator_p
2211 (cp_parser *, bool);
2212 static tree cp_parser_function_definition_from_specifiers_and_declarator
2213 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2214 static tree cp_parser_function_definition_after_declarator
2215 (cp_parser *, bool);
2216 static void cp_parser_template_declaration_after_export
2217 (cp_parser *, bool);
2218 static void cp_parser_perform_template_parameter_access_checks
2219 (vec<deferred_access_check, va_gc> *);
2220 static tree cp_parser_single_declaration
2221 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2222 static tree cp_parser_functional_cast
2223 (cp_parser *, tree);
2224 static tree cp_parser_save_member_function_body
2225 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2226 static tree cp_parser_save_nsdmi
2227 (cp_parser *);
2228 static tree cp_parser_enclosed_template_argument_list
2229 (cp_parser *);
2230 static void cp_parser_save_default_args
2231 (cp_parser *, tree);
2232 static void cp_parser_late_parsing_for_member
2233 (cp_parser *, tree);
2234 static tree cp_parser_late_parse_one_default_arg
2235 (cp_parser *, tree, tree, tree);
2236 static void cp_parser_late_parsing_nsdmi
2237 (cp_parser *, tree);
2238 static void cp_parser_late_parsing_default_args
2239 (cp_parser *, tree);
2240 static tree cp_parser_sizeof_operand
2241 (cp_parser *, enum rid);
2242 static tree cp_parser_trait_expr
2243 (cp_parser *, enum rid);
2244 static bool cp_parser_declares_only_class_p
2245 (cp_parser *);
2246 static void cp_parser_set_storage_class
2247 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2248 static void cp_parser_set_decl_spec_type
2249 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2250 static void set_and_check_decl_spec_loc
2251 (cp_decl_specifier_seq *decl_specs,
2252 cp_decl_spec ds, cp_token *);
2253 static bool cp_parser_friend_p
2254 (const cp_decl_specifier_seq *);
2255 static void cp_parser_required_error
2256 (cp_parser *, required_token, bool);
2257 static cp_token *cp_parser_require
2258 (cp_parser *, enum cpp_ttype, required_token);
2259 static cp_token *cp_parser_require_keyword
2260 (cp_parser *, enum rid, required_token);
2261 static bool cp_parser_token_starts_function_definition_p
2262 (cp_token *);
2263 static bool cp_parser_next_token_starts_class_definition_p
2264 (cp_parser *);
2265 static bool cp_parser_next_token_ends_template_argument_p
2266 (cp_parser *);
2267 static bool cp_parser_nth_token_starts_template_argument_list_p
2268 (cp_parser *, size_t);
2269 static enum tag_types cp_parser_token_is_class_key
2270 (cp_token *);
2271 static void cp_parser_check_class_key
2272 (enum tag_types, tree type);
2273 static void cp_parser_check_access_in_redeclaration
2274 (tree type, location_t location);
2275 static bool cp_parser_optional_template_keyword
2276 (cp_parser *);
2277 static void cp_parser_pre_parsed_nested_name_specifier
2278 (cp_parser *);
2279 static bool cp_parser_cache_group
2280 (cp_parser *, enum cpp_ttype, unsigned);
2281 static tree cp_parser_cache_defarg
2282 (cp_parser *parser, bool nsdmi);
2283 static void cp_parser_parse_tentatively
2284 (cp_parser *);
2285 static void cp_parser_commit_to_tentative_parse
2286 (cp_parser *);
2287 static void cp_parser_abort_tentative_parse
2288 (cp_parser *);
2289 static bool cp_parser_parse_definitely
2290 (cp_parser *);
2291 static inline bool cp_parser_parsing_tentatively
2292 (cp_parser *);
2293 static bool cp_parser_uncommitted_to_tentative_parse_p
2294 (cp_parser *);
2295 static void cp_parser_error
2296 (cp_parser *, const char *);
2297 static void cp_parser_name_lookup_error
2298 (cp_parser *, tree, tree, name_lookup_error, location_t);
2299 static bool cp_parser_simulate_error
2300 (cp_parser *);
2301 static bool cp_parser_check_type_definition
2302 (cp_parser *);
2303 static void cp_parser_check_for_definition_in_return_type
2304 (cp_declarator *, tree, location_t type_location);
2305 static void cp_parser_check_for_invalid_template_id
2306 (cp_parser *, tree, enum tag_types, location_t location);
2307 static bool cp_parser_non_integral_constant_expression
2308 (cp_parser *, non_integral_constant);
2309 static void cp_parser_diagnose_invalid_type_name
2310 (cp_parser *, tree, tree, location_t);
2311 static bool cp_parser_parse_and_diagnose_invalid_type_name
2312 (cp_parser *);
2313 static int cp_parser_skip_to_closing_parenthesis
2314 (cp_parser *, bool, bool, bool);
2315 static void cp_parser_skip_to_end_of_statement
2316 (cp_parser *);
2317 static void cp_parser_consume_semicolon_at_end_of_statement
2318 (cp_parser *);
2319 static void cp_parser_skip_to_end_of_block_or_statement
2320 (cp_parser *);
2321 static bool cp_parser_skip_to_closing_brace
2322 (cp_parser *);
2323 static void cp_parser_skip_to_end_of_template_parameter_list
2324 (cp_parser *);
2325 static void cp_parser_skip_to_pragma_eol
2326 (cp_parser*, cp_token *);
2327 static bool cp_parser_error_occurred
2328 (cp_parser *);
2329 static bool cp_parser_allow_gnu_extensions_p
2330 (cp_parser *);
2331 static bool cp_parser_is_pure_string_literal
2332 (cp_token *);
2333 static bool cp_parser_is_string_literal
2334 (cp_token *);
2335 static bool cp_parser_is_keyword
2336 (cp_token *, enum rid);
2337 static tree cp_parser_make_typename_type
2338 (cp_parser *, tree, tree, location_t location);
2339 static cp_declarator * cp_parser_make_indirect_declarator
2340 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2342 /* Returns nonzero if we are parsing tentatively. */
2344 static inline bool
2345 cp_parser_parsing_tentatively (cp_parser* parser)
2347 return parser->context->next != NULL;
2350 /* Returns nonzero if TOKEN is a string literal. */
2352 static bool
2353 cp_parser_is_pure_string_literal (cp_token* token)
2355 return (token->type == CPP_STRING ||
2356 token->type == CPP_STRING16 ||
2357 token->type == CPP_STRING32 ||
2358 token->type == CPP_WSTRING ||
2359 token->type == CPP_UTF8STRING);
2362 /* Returns nonzero if TOKEN is a string literal
2363 of a user-defined string literal. */
2365 static bool
2366 cp_parser_is_string_literal (cp_token* token)
2368 return (cp_parser_is_pure_string_literal (token) ||
2369 token->type == CPP_STRING_USERDEF ||
2370 token->type == CPP_STRING16_USERDEF ||
2371 token->type == CPP_STRING32_USERDEF ||
2372 token->type == CPP_WSTRING_USERDEF ||
2373 token->type == CPP_UTF8STRING_USERDEF);
2376 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2378 static bool
2379 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2381 return token->keyword == keyword;
2384 /* If not parsing tentatively, issue a diagnostic of the form
2385 FILE:LINE: MESSAGE before TOKEN
2386 where TOKEN is the next token in the input stream. MESSAGE
2387 (specified by the caller) is usually of the form "expected
2388 OTHER-TOKEN". */
2390 static void
2391 cp_parser_error (cp_parser* parser, const char* gmsgid)
2393 if (!cp_parser_simulate_error (parser))
2395 cp_token *token = cp_lexer_peek_token (parser->lexer);
2396 /* This diagnostic makes more sense if it is tagged to the line
2397 of the token we just peeked at. */
2398 cp_lexer_set_source_position_from_token (token);
2400 if (token->type == CPP_PRAGMA)
2402 error_at (token->location,
2403 "%<#pragma%> is not allowed here");
2404 cp_parser_skip_to_pragma_eol (parser, token);
2405 return;
2408 c_parse_error (gmsgid,
2409 /* Because c_parser_error does not understand
2410 CPP_KEYWORD, keywords are treated like
2411 identifiers. */
2412 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2413 token->u.value, token->flags);
2417 /* Issue an error about name-lookup failing. NAME is the
2418 IDENTIFIER_NODE DECL is the result of
2419 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2420 the thing that we hoped to find. */
2422 static void
2423 cp_parser_name_lookup_error (cp_parser* parser,
2424 tree name,
2425 tree decl,
2426 name_lookup_error desired,
2427 location_t location)
2429 /* If name lookup completely failed, tell the user that NAME was not
2430 declared. */
2431 if (decl == error_mark_node)
2433 if (parser->scope && parser->scope != global_namespace)
2434 error_at (location, "%<%E::%E%> has not been declared",
2435 parser->scope, name);
2436 else if (parser->scope == global_namespace)
2437 error_at (location, "%<::%E%> has not been declared", name);
2438 else if (parser->object_scope
2439 && !CLASS_TYPE_P (parser->object_scope))
2440 error_at (location, "request for member %qE in non-class type %qT",
2441 name, parser->object_scope);
2442 else if (parser->object_scope)
2443 error_at (location, "%<%T::%E%> has not been declared",
2444 parser->object_scope, name);
2445 else
2446 error_at (location, "%qE has not been declared", name);
2448 else if (parser->scope && parser->scope != global_namespace)
2450 switch (desired)
2452 case NLE_TYPE:
2453 error_at (location, "%<%E::%E%> is not a type",
2454 parser->scope, name);
2455 break;
2456 case NLE_CXX98:
2457 error_at (location, "%<%E::%E%> is not a class or namespace",
2458 parser->scope, name);
2459 break;
2460 case NLE_NOT_CXX98:
2461 error_at (location,
2462 "%<%E::%E%> is not a class, namespace, or enumeration",
2463 parser->scope, name);
2464 break;
2465 default:
2466 gcc_unreachable ();
2470 else if (parser->scope == global_namespace)
2472 switch (desired)
2474 case NLE_TYPE:
2475 error_at (location, "%<::%E%> is not a type", name);
2476 break;
2477 case NLE_CXX98:
2478 error_at (location, "%<::%E%> is not a class or namespace", name);
2479 break;
2480 case NLE_NOT_CXX98:
2481 error_at (location,
2482 "%<::%E%> is not a class, namespace, or enumeration",
2483 name);
2484 break;
2485 default:
2486 gcc_unreachable ();
2489 else
2491 switch (desired)
2493 case NLE_TYPE:
2494 error_at (location, "%qE is not a type", name);
2495 break;
2496 case NLE_CXX98:
2497 error_at (location, "%qE is not a class or namespace", name);
2498 break;
2499 case NLE_NOT_CXX98:
2500 error_at (location,
2501 "%qE is not a class, namespace, or enumeration", name);
2502 break;
2503 default:
2504 gcc_unreachable ();
2509 /* If we are parsing tentatively, remember that an error has occurred
2510 during this tentative parse. Returns true if the error was
2511 simulated; false if a message should be issued by the caller. */
2513 static bool
2514 cp_parser_simulate_error (cp_parser* parser)
2516 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2518 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2519 return true;
2521 return false;
2524 /* This function is called when a type is defined. If type
2525 definitions are forbidden at this point, an error message is
2526 issued. */
2528 static bool
2529 cp_parser_check_type_definition (cp_parser* parser)
2531 /* If types are forbidden here, issue a message. */
2532 if (parser->type_definition_forbidden_message)
2534 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2535 in the message need to be interpreted. */
2536 error (parser->type_definition_forbidden_message);
2537 return false;
2539 return true;
2542 /* This function is called when the DECLARATOR is processed. The TYPE
2543 was a type defined in the decl-specifiers. If it is invalid to
2544 define a type in the decl-specifiers for DECLARATOR, an error is
2545 issued. TYPE_LOCATION is the location of TYPE and is used
2546 for error reporting. */
2548 static void
2549 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2550 tree type, location_t type_location)
2552 /* [dcl.fct] forbids type definitions in return types.
2553 Unfortunately, it's not easy to know whether or not we are
2554 processing a return type until after the fact. */
2555 while (declarator
2556 && (declarator->kind == cdk_pointer
2557 || declarator->kind == cdk_reference
2558 || declarator->kind == cdk_ptrmem))
2559 declarator = declarator->declarator;
2560 if (declarator
2561 && declarator->kind == cdk_function)
2563 error_at (type_location,
2564 "new types may not be defined in a return type");
2565 inform (type_location,
2566 "(perhaps a semicolon is missing after the definition of %qT)",
2567 type);
2571 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2572 "<" in any valid C++ program. If the next token is indeed "<",
2573 issue a message warning the user about what appears to be an
2574 invalid attempt to form a template-id. LOCATION is the location
2575 of the type-specifier (TYPE) */
2577 static void
2578 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2579 tree type,
2580 enum tag_types tag_type,
2581 location_t location)
2583 cp_token_position start = 0;
2585 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2587 if (TYPE_P (type))
2588 error_at (location, "%qT is not a template", type);
2589 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2591 if (tag_type != none_type)
2592 error_at (location, "%qE is not a class template", type);
2593 else
2594 error_at (location, "%qE is not a template", type);
2596 else
2597 error_at (location, "invalid template-id");
2598 /* Remember the location of the invalid "<". */
2599 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2600 start = cp_lexer_token_position (parser->lexer, true);
2601 /* Consume the "<". */
2602 cp_lexer_consume_token (parser->lexer);
2603 /* Parse the template arguments. */
2604 cp_parser_enclosed_template_argument_list (parser);
2605 /* Permanently remove the invalid template arguments so that
2606 this error message is not issued again. */
2607 if (start)
2608 cp_lexer_purge_tokens_after (parser->lexer, start);
2612 /* If parsing an integral constant-expression, issue an error message
2613 about the fact that THING appeared and return true. Otherwise,
2614 return false. In either case, set
2615 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2617 static bool
2618 cp_parser_non_integral_constant_expression (cp_parser *parser,
2619 non_integral_constant thing)
2621 parser->non_integral_constant_expression_p = true;
2622 if (parser->integral_constant_expression_p)
2624 if (!parser->allow_non_integral_constant_expression_p)
2626 const char *msg = NULL;
2627 switch (thing)
2629 case NIC_FLOAT:
2630 error ("floating-point literal "
2631 "cannot appear in a constant-expression");
2632 return true;
2633 case NIC_CAST:
2634 error ("a cast to a type other than an integral or "
2635 "enumeration type cannot appear in a "
2636 "constant-expression");
2637 return true;
2638 case NIC_TYPEID:
2639 error ("%<typeid%> operator "
2640 "cannot appear in a constant-expression");
2641 return true;
2642 case NIC_NCC:
2643 error ("non-constant compound literals "
2644 "cannot appear in a constant-expression");
2645 return true;
2646 case NIC_FUNC_CALL:
2647 error ("a function call "
2648 "cannot appear in a constant-expression");
2649 return true;
2650 case NIC_INC:
2651 error ("an increment "
2652 "cannot appear in a constant-expression");
2653 return true;
2654 case NIC_DEC:
2655 error ("an decrement "
2656 "cannot appear in a constant-expression");
2657 return true;
2658 case NIC_ARRAY_REF:
2659 error ("an array reference "
2660 "cannot appear in a constant-expression");
2661 return true;
2662 case NIC_ADDR_LABEL:
2663 error ("the address of a label "
2664 "cannot appear in a constant-expression");
2665 return true;
2666 case NIC_OVERLOADED:
2667 error ("calls to overloaded operators "
2668 "cannot appear in a constant-expression");
2669 return true;
2670 case NIC_ASSIGNMENT:
2671 error ("an assignment cannot appear in a constant-expression");
2672 return true;
2673 case NIC_COMMA:
2674 error ("a comma operator "
2675 "cannot appear in a constant-expression");
2676 return true;
2677 case NIC_CONSTRUCTOR:
2678 error ("a call to a constructor "
2679 "cannot appear in a constant-expression");
2680 return true;
2681 case NIC_TRANSACTION:
2682 error ("a transaction expression "
2683 "cannot appear in a constant-expression");
2684 return true;
2685 case NIC_THIS:
2686 msg = "this";
2687 break;
2688 case NIC_FUNC_NAME:
2689 msg = "__FUNCTION__";
2690 break;
2691 case NIC_PRETTY_FUNC:
2692 msg = "__PRETTY_FUNCTION__";
2693 break;
2694 case NIC_C99_FUNC:
2695 msg = "__func__";
2696 break;
2697 case NIC_VA_ARG:
2698 msg = "va_arg";
2699 break;
2700 case NIC_ARROW:
2701 msg = "->";
2702 break;
2703 case NIC_POINT:
2704 msg = ".";
2705 break;
2706 case NIC_STAR:
2707 msg = "*";
2708 break;
2709 case NIC_ADDR:
2710 msg = "&";
2711 break;
2712 case NIC_PREINCREMENT:
2713 msg = "++";
2714 break;
2715 case NIC_PREDECREMENT:
2716 msg = "--";
2717 break;
2718 case NIC_NEW:
2719 msg = "new";
2720 break;
2721 case NIC_DEL:
2722 msg = "delete";
2723 break;
2724 default:
2725 gcc_unreachable ();
2727 if (msg)
2728 error ("%qs cannot appear in a constant-expression", msg);
2729 return true;
2732 return false;
2735 /* Emit a diagnostic for an invalid type name. SCOPE is the
2736 qualifying scope (or NULL, if none) for ID. This function commits
2737 to the current active tentative parse, if any. (Otherwise, the
2738 problematic construct might be encountered again later, resulting
2739 in duplicate error messages.) LOCATION is the location of ID. */
2741 static void
2742 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2743 tree scope, tree id,
2744 location_t location)
2746 tree decl, old_scope;
2747 cp_parser_commit_to_tentative_parse (parser);
2748 /* Try to lookup the identifier. */
2749 old_scope = parser->scope;
2750 parser->scope = scope;
2751 decl = cp_parser_lookup_name_simple (parser, id, location);
2752 parser->scope = old_scope;
2753 /* If the lookup found a template-name, it means that the user forgot
2754 to specify an argument list. Emit a useful error message. */
2755 if (TREE_CODE (decl) == TEMPLATE_DECL)
2756 error_at (location,
2757 "invalid use of template-name %qE without an argument list",
2758 decl);
2759 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2760 error_at (location, "invalid use of destructor %qD as a type", id);
2761 else if (TREE_CODE (decl) == TYPE_DECL)
2762 /* Something like 'unsigned A a;' */
2763 error_at (location, "invalid combination of multiple type-specifiers");
2764 else if (!parser->scope)
2766 /* Issue an error message. */
2767 error_at (location, "%qE does not name a type", id);
2768 /* If we're in a template class, it's possible that the user was
2769 referring to a type from a base class. For example:
2771 template <typename T> struct A { typedef T X; };
2772 template <typename T> struct B : public A<T> { X x; };
2774 The user should have said "typename A<T>::X". */
2775 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2776 inform (location, "C++11 %<constexpr%> only available with "
2777 "-std=c++11 or -std=gnu++11");
2778 else if (processing_template_decl && current_class_type
2779 && TYPE_BINFO (current_class_type))
2781 tree b;
2783 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2785 b = TREE_CHAIN (b))
2787 tree base_type = BINFO_TYPE (b);
2788 if (CLASS_TYPE_P (base_type)
2789 && dependent_type_p (base_type))
2791 tree field;
2792 /* Go from a particular instantiation of the
2793 template (which will have an empty TYPE_FIELDs),
2794 to the main version. */
2795 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2796 for (field = TYPE_FIELDS (base_type);
2797 field;
2798 field = DECL_CHAIN (field))
2799 if (TREE_CODE (field) == TYPE_DECL
2800 && DECL_NAME (field) == id)
2802 inform (location,
2803 "(perhaps %<typename %T::%E%> was intended)",
2804 BINFO_TYPE (b), id);
2805 break;
2807 if (field)
2808 break;
2813 /* Here we diagnose qualified-ids where the scope is actually correct,
2814 but the identifier does not resolve to a valid type name. */
2815 else if (parser->scope != error_mark_node)
2817 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2818 error_at (location, "%qE in namespace %qE does not name a type",
2819 id, parser->scope);
2820 else if (CLASS_TYPE_P (parser->scope)
2821 && constructor_name_p (id, parser->scope))
2823 /* A<T>::A<T>() */
2824 error_at (location, "%<%T::%E%> names the constructor, not"
2825 " the type", parser->scope, id);
2826 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2827 error_at (location, "and %qT has no template constructors",
2828 parser->scope);
2830 else if (TYPE_P (parser->scope)
2831 && dependent_scope_p (parser->scope))
2832 error_at (location, "need %<typename%> before %<%T::%E%> because "
2833 "%qT is a dependent scope",
2834 parser->scope, id, parser->scope);
2835 else if (TYPE_P (parser->scope))
2836 error_at (location, "%qE in %q#T does not name a type",
2837 id, parser->scope);
2838 else
2839 gcc_unreachable ();
2843 /* Check for a common situation where a type-name should be present,
2844 but is not, and issue a sensible error message. Returns true if an
2845 invalid type-name was detected.
2847 The situation handled by this function are variable declarations of the
2848 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2849 Usually, `ID' should name a type, but if we got here it means that it
2850 does not. We try to emit the best possible error message depending on
2851 how exactly the id-expression looks like. */
2853 static bool
2854 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2856 tree id;
2857 cp_token *token = cp_lexer_peek_token (parser->lexer);
2859 /* Avoid duplicate error about ambiguous lookup. */
2860 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2862 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2863 if (next->type == CPP_NAME && next->ambiguous_p)
2864 goto out;
2867 cp_parser_parse_tentatively (parser);
2868 id = cp_parser_id_expression (parser,
2869 /*template_keyword_p=*/false,
2870 /*check_dependency_p=*/true,
2871 /*template_p=*/NULL,
2872 /*declarator_p=*/true,
2873 /*optional_p=*/false);
2874 /* If the next token is a (, this is a function with no explicit return
2875 type, i.e. constructor, destructor or conversion op. */
2876 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2877 || TREE_CODE (id) == TYPE_DECL)
2879 cp_parser_abort_tentative_parse (parser);
2880 return false;
2882 if (!cp_parser_parse_definitely (parser))
2883 return false;
2885 /* Emit a diagnostic for the invalid type. */
2886 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2887 id, token->location);
2888 out:
2889 /* If we aren't in the middle of a declarator (i.e. in a
2890 parameter-declaration-clause), skip to the end of the declaration;
2891 there's no point in trying to process it. */
2892 if (!parser->in_declarator_p)
2893 cp_parser_skip_to_end_of_block_or_statement (parser);
2894 return true;
2897 /* Consume tokens up to, and including, the next non-nested closing `)'.
2898 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2899 are doing error recovery. Returns -1 if OR_COMMA is true and we
2900 found an unnested comma. */
2902 static int
2903 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2904 bool recovering,
2905 bool or_comma,
2906 bool consume_paren)
2908 unsigned paren_depth = 0;
2909 unsigned brace_depth = 0;
2910 unsigned square_depth = 0;
2912 if (recovering && !or_comma
2913 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2914 return 0;
2916 while (true)
2918 cp_token * token = cp_lexer_peek_token (parser->lexer);
2920 switch (token->type)
2922 case CPP_EOF:
2923 case CPP_PRAGMA_EOL:
2924 /* If we've run out of tokens, then there is no closing `)'. */
2925 return 0;
2927 /* This is good for lambda expression capture-lists. */
2928 case CPP_OPEN_SQUARE:
2929 ++square_depth;
2930 break;
2931 case CPP_CLOSE_SQUARE:
2932 if (!square_depth--)
2933 return 0;
2934 break;
2936 case CPP_SEMICOLON:
2937 /* This matches the processing in skip_to_end_of_statement. */
2938 if (!brace_depth)
2939 return 0;
2940 break;
2942 case CPP_OPEN_BRACE:
2943 ++brace_depth;
2944 break;
2945 case CPP_CLOSE_BRACE:
2946 if (!brace_depth--)
2947 return 0;
2948 break;
2950 case CPP_COMMA:
2951 if (recovering && or_comma && !brace_depth && !paren_depth
2952 && !square_depth)
2953 return -1;
2954 break;
2956 case CPP_OPEN_PAREN:
2957 if (!brace_depth)
2958 ++paren_depth;
2959 break;
2961 case CPP_CLOSE_PAREN:
2962 if (!brace_depth && !paren_depth--)
2964 if (consume_paren)
2965 cp_lexer_consume_token (parser->lexer);
2966 return 1;
2968 break;
2970 default:
2971 break;
2974 /* Consume the token. */
2975 cp_lexer_consume_token (parser->lexer);
2979 /* Consume tokens until we reach the end of the current statement.
2980 Normally, that will be just before consuming a `;'. However, if a
2981 non-nested `}' comes first, then we stop before consuming that. */
2983 static void
2984 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2986 unsigned nesting_depth = 0;
2988 while (true)
2990 cp_token *token = cp_lexer_peek_token (parser->lexer);
2992 switch (token->type)
2994 case CPP_EOF:
2995 case CPP_PRAGMA_EOL:
2996 /* If we've run out of tokens, stop. */
2997 return;
2999 case CPP_SEMICOLON:
3000 /* If the next token is a `;', we have reached the end of the
3001 statement. */
3002 if (!nesting_depth)
3003 return;
3004 break;
3006 case CPP_CLOSE_BRACE:
3007 /* If this is a non-nested '}', stop before consuming it.
3008 That way, when confronted with something like:
3010 { 3 + }
3012 we stop before consuming the closing '}', even though we
3013 have not yet reached a `;'. */
3014 if (nesting_depth == 0)
3015 return;
3017 /* If it is the closing '}' for a block that we have
3018 scanned, stop -- but only after consuming the token.
3019 That way given:
3021 void f g () { ... }
3022 typedef int I;
3024 we will stop after the body of the erroneously declared
3025 function, but before consuming the following `typedef'
3026 declaration. */
3027 if (--nesting_depth == 0)
3029 cp_lexer_consume_token (parser->lexer);
3030 return;
3033 case CPP_OPEN_BRACE:
3034 ++nesting_depth;
3035 break;
3037 default:
3038 break;
3041 /* Consume the token. */
3042 cp_lexer_consume_token (parser->lexer);
3046 /* This function is called at the end of a statement or declaration.
3047 If the next token is a semicolon, it is consumed; otherwise, error
3048 recovery is attempted. */
3050 static void
3051 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3053 /* Look for the trailing `;'. */
3054 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3056 /* If there is additional (erroneous) input, skip to the end of
3057 the statement. */
3058 cp_parser_skip_to_end_of_statement (parser);
3059 /* If the next token is now a `;', consume it. */
3060 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3061 cp_lexer_consume_token (parser->lexer);
3065 /* Skip tokens until we have consumed an entire block, or until we
3066 have consumed a non-nested `;'. */
3068 static void
3069 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3071 int nesting_depth = 0;
3073 while (nesting_depth >= 0)
3075 cp_token *token = cp_lexer_peek_token (parser->lexer);
3077 switch (token->type)
3079 case CPP_EOF:
3080 case CPP_PRAGMA_EOL:
3081 /* If we've run out of tokens, stop. */
3082 return;
3084 case CPP_SEMICOLON:
3085 /* Stop if this is an unnested ';'. */
3086 if (!nesting_depth)
3087 nesting_depth = -1;
3088 break;
3090 case CPP_CLOSE_BRACE:
3091 /* Stop if this is an unnested '}', or closes the outermost
3092 nesting level. */
3093 nesting_depth--;
3094 if (nesting_depth < 0)
3095 return;
3096 if (!nesting_depth)
3097 nesting_depth = -1;
3098 break;
3100 case CPP_OPEN_BRACE:
3101 /* Nest. */
3102 nesting_depth++;
3103 break;
3105 default:
3106 break;
3109 /* Consume the token. */
3110 cp_lexer_consume_token (parser->lexer);
3114 /* Skip tokens until a non-nested closing curly brace is the next
3115 token, or there are no more tokens. Return true in the first case,
3116 false otherwise. */
3118 static bool
3119 cp_parser_skip_to_closing_brace (cp_parser *parser)
3121 unsigned nesting_depth = 0;
3123 while (true)
3125 cp_token *token = cp_lexer_peek_token (parser->lexer);
3127 switch (token->type)
3129 case CPP_EOF:
3130 case CPP_PRAGMA_EOL:
3131 /* If we've run out of tokens, stop. */
3132 return false;
3134 case CPP_CLOSE_BRACE:
3135 /* If the next token is a non-nested `}', then we have reached
3136 the end of the current block. */
3137 if (nesting_depth-- == 0)
3138 return true;
3139 break;
3141 case CPP_OPEN_BRACE:
3142 /* If it the next token is a `{', then we are entering a new
3143 block. Consume the entire block. */
3144 ++nesting_depth;
3145 break;
3147 default:
3148 break;
3151 /* Consume the token. */
3152 cp_lexer_consume_token (parser->lexer);
3156 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3157 parameter is the PRAGMA token, allowing us to purge the entire pragma
3158 sequence. */
3160 static void
3161 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3163 cp_token *token;
3165 parser->lexer->in_pragma = false;
3168 token = cp_lexer_consume_token (parser->lexer);
3169 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3171 /* Ensure that the pragma is not parsed again. */
3172 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3175 /* Require pragma end of line, resyncing with it as necessary. The
3176 arguments are as for cp_parser_skip_to_pragma_eol. */
3178 static void
3179 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3181 parser->lexer->in_pragma = false;
3182 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3183 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3186 /* This is a simple wrapper around make_typename_type. When the id is
3187 an unresolved identifier node, we can provide a superior diagnostic
3188 using cp_parser_diagnose_invalid_type_name. */
3190 static tree
3191 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3192 tree id, location_t id_location)
3194 tree result;
3195 if (TREE_CODE (id) == IDENTIFIER_NODE)
3197 result = make_typename_type (scope, id, typename_type,
3198 /*complain=*/tf_none);
3199 if (result == error_mark_node)
3200 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3201 return result;
3203 return make_typename_type (scope, id, typename_type, tf_error);
3206 /* This is a wrapper around the
3207 make_{pointer,ptrmem,reference}_declarator functions that decides
3208 which one to call based on the CODE and CLASS_TYPE arguments. The
3209 CODE argument should be one of the values returned by
3210 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3211 appertain to the pointer or reference. */
3213 static cp_declarator *
3214 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3215 cp_cv_quals cv_qualifiers,
3216 cp_declarator *target,
3217 tree attributes)
3219 if (code == ERROR_MARK)
3220 return cp_error_declarator;
3222 if (code == INDIRECT_REF)
3223 if (class_type == NULL_TREE)
3224 return make_pointer_declarator (cv_qualifiers, target, attributes);
3225 else
3226 return make_ptrmem_declarator (cv_qualifiers, class_type,
3227 target, attributes);
3228 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3229 return make_reference_declarator (cv_qualifiers, target,
3230 false, attributes);
3231 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3232 return make_reference_declarator (cv_qualifiers, target,
3233 true, attributes);
3234 gcc_unreachable ();
3237 /* Create a new C++ parser. */
3239 static cp_parser *
3240 cp_parser_new (void)
3242 cp_parser *parser;
3243 cp_lexer *lexer;
3244 unsigned i;
3246 /* cp_lexer_new_main is called before doing GC allocation because
3247 cp_lexer_new_main might load a PCH file. */
3248 lexer = cp_lexer_new_main ();
3250 /* Initialize the binops_by_token so that we can get the tree
3251 directly from the token. */
3252 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3253 binops_by_token[binops[i].token_type] = binops[i];
3255 parser = ggc_alloc_cleared_cp_parser ();
3256 parser->lexer = lexer;
3257 parser->context = cp_parser_context_new (NULL);
3259 /* For now, we always accept GNU extensions. */
3260 parser->allow_gnu_extensions_p = 1;
3262 /* The `>' token is a greater-than operator, not the end of a
3263 template-id. */
3264 parser->greater_than_is_operator_p = true;
3266 parser->default_arg_ok_p = true;
3268 /* We are not parsing a constant-expression. */
3269 parser->integral_constant_expression_p = false;
3270 parser->allow_non_integral_constant_expression_p = false;
3271 parser->non_integral_constant_expression_p = false;
3273 /* Local variable names are not forbidden. */
3274 parser->local_variables_forbidden_p = false;
3276 /* We are not processing an `extern "C"' declaration. */
3277 parser->in_unbraced_linkage_specification_p = false;
3279 /* We are not processing a declarator. */
3280 parser->in_declarator_p = false;
3282 /* We are not processing a template-argument-list. */
3283 parser->in_template_argument_list_p = false;
3285 /* We are not in an iteration statement. */
3286 parser->in_statement = 0;
3288 /* We are not in a switch statement. */
3289 parser->in_switch_statement_p = false;
3291 /* We are not parsing a type-id inside an expression. */
3292 parser->in_type_id_in_expr_p = false;
3294 /* Declarations aren't implicitly extern "C". */
3295 parser->implicit_extern_c = false;
3297 /* String literals should be translated to the execution character set. */
3298 parser->translate_strings_p = true;
3300 /* We are not parsing a function body. */
3301 parser->in_function_body = false;
3303 /* We can correct until told otherwise. */
3304 parser->colon_corrects_to_scope_p = true;
3306 /* The unparsed function queue is empty. */
3307 push_unparsed_function_queues (parser);
3309 /* There are no classes being defined. */
3310 parser->num_classes_being_defined = 0;
3312 /* No template parameters apply. */
3313 parser->num_template_parameter_lists = 0;
3315 return parser;
3318 /* Create a cp_lexer structure which will emit the tokens in CACHE
3319 and push it onto the parser's lexer stack. This is used for delayed
3320 parsing of in-class method bodies and default arguments, and should
3321 not be confused with tentative parsing. */
3322 static void
3323 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3325 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3326 lexer->next = parser->lexer;
3327 parser->lexer = lexer;
3329 /* Move the current source position to that of the first token in the
3330 new lexer. */
3331 cp_lexer_set_source_position_from_token (lexer->next_token);
3334 /* Pop the top lexer off the parser stack. This is never used for the
3335 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3336 static void
3337 cp_parser_pop_lexer (cp_parser *parser)
3339 cp_lexer *lexer = parser->lexer;
3340 parser->lexer = lexer->next;
3341 cp_lexer_destroy (lexer);
3343 /* Put the current source position back where it was before this
3344 lexer was pushed. */
3345 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3348 /* Lexical conventions [gram.lex] */
3350 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3351 identifier. */
3353 static tree
3354 cp_parser_identifier (cp_parser* parser)
3356 cp_token *token;
3358 /* Look for the identifier. */
3359 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3360 /* Return the value. */
3361 return token ? token->u.value : error_mark_node;
3364 /* Parse a sequence of adjacent string constants. Returns a
3365 TREE_STRING representing the combined, nul-terminated string
3366 constant. If TRANSLATE is true, translate the string to the
3367 execution character set. If WIDE_OK is true, a wide string is
3368 invalid here.
3370 C++98 [lex.string] says that if a narrow string literal token is
3371 adjacent to a wide string literal token, the behavior is undefined.
3372 However, C99 6.4.5p4 says that this results in a wide string literal.
3373 We follow C99 here, for consistency with the C front end.
3375 This code is largely lifted from lex_string() in c-lex.c.
3377 FUTURE: ObjC++ will need to handle @-strings here. */
3378 static tree
3379 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3381 tree value;
3382 size_t count;
3383 struct obstack str_ob;
3384 cpp_string str, istr, *strs;
3385 cp_token *tok;
3386 enum cpp_ttype type, curr_type;
3387 int have_suffix_p = 0;
3388 tree string_tree;
3389 tree suffix_id = NULL_TREE;
3390 bool curr_tok_is_userdef_p = false;
3392 tok = cp_lexer_peek_token (parser->lexer);
3393 if (!cp_parser_is_string_literal (tok))
3395 cp_parser_error (parser, "expected string-literal");
3396 return error_mark_node;
3399 if (cpp_userdef_string_p (tok->type))
3401 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3402 curr_type = cpp_userdef_string_remove_type (tok->type);
3403 curr_tok_is_userdef_p = true;
3405 else
3407 string_tree = tok->u.value;
3408 curr_type = tok->type;
3410 type = curr_type;
3412 /* Try to avoid the overhead of creating and destroying an obstack
3413 for the common case of just one string. */
3414 if (!cp_parser_is_string_literal
3415 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3417 cp_lexer_consume_token (parser->lexer);
3419 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3420 str.len = TREE_STRING_LENGTH (string_tree);
3421 count = 1;
3423 if (curr_tok_is_userdef_p)
3425 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3426 have_suffix_p = 1;
3427 curr_type = cpp_userdef_string_remove_type (tok->type);
3429 else
3430 curr_type = tok->type;
3432 strs = &str;
3434 else
3436 gcc_obstack_init (&str_ob);
3437 count = 0;
3441 cp_lexer_consume_token (parser->lexer);
3442 count++;
3443 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3444 str.len = TREE_STRING_LENGTH (string_tree);
3446 if (curr_tok_is_userdef_p)
3448 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3449 if (have_suffix_p == 0)
3451 suffix_id = curr_suffix_id;
3452 have_suffix_p = 1;
3454 else if (have_suffix_p == 1
3455 && curr_suffix_id != suffix_id)
3457 error ("inconsistent user-defined literal suffixes"
3458 " %qD and %qD in string literal",
3459 suffix_id, curr_suffix_id);
3460 have_suffix_p = -1;
3462 curr_type = cpp_userdef_string_remove_type (tok->type);
3464 else
3465 curr_type = tok->type;
3467 if (type != curr_type)
3469 if (type == CPP_STRING)
3470 type = curr_type;
3471 else if (curr_type != CPP_STRING)
3472 error_at (tok->location,
3473 "unsupported non-standard concatenation "
3474 "of string literals");
3477 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3479 tok = cp_lexer_peek_token (parser->lexer);
3480 if (cpp_userdef_string_p (tok->type))
3482 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3483 curr_type = cpp_userdef_string_remove_type (tok->type);
3484 curr_tok_is_userdef_p = true;
3486 else
3488 string_tree = tok->u.value;
3489 curr_type = tok->type;
3490 curr_tok_is_userdef_p = false;
3493 while (cp_parser_is_string_literal (tok));
3495 strs = (cpp_string *) obstack_finish (&str_ob);
3498 if (type != CPP_STRING && !wide_ok)
3500 cp_parser_error (parser, "a wide string is invalid in this context");
3501 type = CPP_STRING;
3504 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3505 (parse_in, strs, count, &istr, type))
3507 value = build_string (istr.len, (const char *)istr.text);
3508 free (CONST_CAST (unsigned char *, istr.text));
3510 switch (type)
3512 default:
3513 case CPP_STRING:
3514 case CPP_UTF8STRING:
3515 TREE_TYPE (value) = char_array_type_node;
3516 break;
3517 case CPP_STRING16:
3518 TREE_TYPE (value) = char16_array_type_node;
3519 break;
3520 case CPP_STRING32:
3521 TREE_TYPE (value) = char32_array_type_node;
3522 break;
3523 case CPP_WSTRING:
3524 TREE_TYPE (value) = wchar_array_type_node;
3525 break;
3528 value = fix_string_type (value);
3530 if (have_suffix_p)
3532 tree literal = build_userdef_literal (suffix_id, value,
3533 OT_NONE, NULL_TREE);
3534 tok->u.value = literal;
3535 return cp_parser_userdef_string_literal (tok);
3538 else
3539 /* cpp_interpret_string has issued an error. */
3540 value = error_mark_node;
3542 if (count > 1)
3543 obstack_free (&str_ob, 0);
3545 return value;
3548 /* Look up a literal operator with the name and the exact arguments. */
3550 static tree
3551 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3553 tree decl, fns;
3554 decl = lookup_name (name);
3555 if (!decl || !is_overloaded_fn (decl))
3556 return error_mark_node;
3558 for (fns = decl; fns; fns = OVL_NEXT (fns))
3560 unsigned int ix;
3561 bool found = true;
3562 tree fn = OVL_CURRENT (fns);
3563 tree argtypes = NULL_TREE;
3564 argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3565 if (argtypes != NULL_TREE)
3567 for (ix = 0; ix < vec_safe_length (args) && argtypes != NULL_TREE;
3568 ++ix, argtypes = TREE_CHAIN (argtypes))
3570 tree targ = TREE_VALUE (argtypes);
3571 tree tparm = TREE_TYPE ((*args)[ix]);
3572 bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3573 bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3574 if ((ptr || arr || !same_type_p (targ, tparm))
3575 && (!ptr || !arr
3576 || !same_type_p (TREE_TYPE (targ),
3577 TREE_TYPE (tparm))))
3578 found = false;
3580 if (found
3581 && ix == vec_safe_length (args)
3582 /* May be this should be sufficient_parms_p instead,
3583 depending on how exactly should user-defined literals
3584 work in presence of default arguments on the literal
3585 operator parameters. */
3586 && argtypes == void_list_node)
3587 return fn;
3591 return error_mark_node;
3594 /* Parse a user-defined char constant. Returns a call to a user-defined
3595 literal operator taking the character as an argument. */
3597 static tree
3598 cp_parser_userdef_char_literal (cp_parser *parser)
3600 cp_token *token = cp_lexer_consume_token (parser->lexer);
3601 tree literal = token->u.value;
3602 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3603 tree value = USERDEF_LITERAL_VALUE (literal);
3604 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3605 tree decl, result;
3607 /* Build up a call to the user-defined operator */
3608 /* Lookup the name we got back from the id-expression. */
3609 vec<tree, va_gc> *args = make_tree_vector ();
3610 vec_safe_push (args, value);
3611 decl = lookup_literal_operator (name, args);
3612 if (!decl || decl == error_mark_node)
3614 error ("unable to find character literal operator %qD with %qT argument",
3615 name, TREE_TYPE (value));
3616 release_tree_vector (args);
3617 return error_mark_node;
3619 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3620 release_tree_vector (args);
3621 if (result != error_mark_node)
3622 return result;
3624 error ("unable to find character literal operator %qD with %qT argument",
3625 name, TREE_TYPE (value));
3626 return error_mark_node;
3629 /* A subroutine of cp_parser_userdef_numeric_literal to
3630 create a char... template parameter pack from a string node. */
3632 static tree
3633 make_char_string_pack (tree value)
3635 tree charvec;
3636 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3637 const char *str = TREE_STRING_POINTER (value);
3638 int i, len = TREE_STRING_LENGTH (value) - 1;
3639 tree argvec = make_tree_vec (1);
3641 /* Fill in CHARVEC with all of the parameters. */
3642 charvec = make_tree_vec (len);
3643 for (i = 0; i < len; ++i)
3644 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3646 /* Build the argument packs. */
3647 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3648 TREE_TYPE (argpack) = char_type_node;
3650 TREE_VEC_ELT (argvec, 0) = argpack;
3652 return argvec;
3655 /* Parse a user-defined numeric constant. returns a call to a user-defined
3656 literal operator. */
3658 static tree
3659 cp_parser_userdef_numeric_literal (cp_parser *parser)
3661 cp_token *token = cp_lexer_consume_token (parser->lexer);
3662 tree literal = token->u.value;
3663 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3664 tree value = USERDEF_LITERAL_VALUE (literal);
3665 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3666 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3667 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3668 tree decl, result;
3669 vec<tree, va_gc> *args;
3671 /* Look for a literal operator taking the exact type of numeric argument
3672 as the literal value. */
3673 args = make_tree_vector ();
3674 vec_safe_push (args, value);
3675 decl = lookup_literal_operator (name, args);
3676 if (decl && decl != error_mark_node)
3678 result = finish_call_expr (decl, &args, false, true, tf_none);
3679 if (result != error_mark_node)
3681 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3682 warning_at (token->location, OPT_Woverflow,
3683 "integer literal exceeds range of %qT type",
3684 long_long_unsigned_type_node);
3685 else
3687 if (overflow > 0)
3688 warning_at (token->location, OPT_Woverflow,
3689 "floating literal exceeds range of %qT type",
3690 long_double_type_node);
3691 else if (overflow < 0)
3692 warning_at (token->location, OPT_Woverflow,
3693 "floating literal truncated to zero");
3695 release_tree_vector (args);
3696 return result;
3699 release_tree_vector (args);
3701 /* If the numeric argument didn't work, look for a raw literal
3702 operator taking a const char* argument consisting of the number
3703 in string format. */
3704 args = make_tree_vector ();
3705 vec_safe_push (args, num_string);
3706 decl = lookup_literal_operator (name, args);
3707 if (decl && decl != error_mark_node)
3709 result = finish_call_expr (decl, &args, false, true, tf_none);
3710 if (result != error_mark_node)
3712 release_tree_vector (args);
3713 return result;
3716 release_tree_vector (args);
3718 /* If the raw literal didn't work, look for a non-type template
3719 function with parameter pack char.... Call the function with
3720 template parameter characters representing the number. */
3721 args = make_tree_vector ();
3722 decl = lookup_literal_operator (name, args);
3723 if (decl && decl != error_mark_node)
3725 tree tmpl_args = make_char_string_pack (num_string);
3726 decl = lookup_template_function (decl, tmpl_args);
3727 result = finish_call_expr (decl, &args, false, true, tf_none);
3728 if (result != error_mark_node)
3730 release_tree_vector (args);
3731 return result;
3734 release_tree_vector (args);
3736 error ("unable to find numeric literal operator %qD", name);
3737 return error_mark_node;
3740 /* Parse a user-defined string constant. Returns a call to a user-defined
3741 literal operator taking a character pointer and the length of the string
3742 as arguments. */
3744 static tree
3745 cp_parser_userdef_string_literal (cp_token *token)
3747 tree literal = token->u.value;
3748 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3749 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3750 tree value = USERDEF_LITERAL_VALUE (literal);
3751 int len = TREE_STRING_LENGTH (value)
3752 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3753 tree decl, result;
3755 /* Build up a call to the user-defined operator */
3756 /* Lookup the name we got back from the id-expression. */
3757 vec<tree, va_gc> *args = make_tree_vector ();
3758 vec_safe_push (args, value);
3759 vec_safe_push (args, build_int_cst (size_type_node, len));
3760 decl = lookup_name (name);
3761 if (!decl || decl == error_mark_node)
3763 error ("unable to find string literal operator %qD", name);
3764 release_tree_vector (args);
3765 return error_mark_node;
3767 result = finish_call_expr (decl, &args, false, true, tf_none);
3768 release_tree_vector (args);
3769 if (result != error_mark_node)
3770 return result;
3772 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3773 name, TREE_TYPE (value), size_type_node);
3774 return error_mark_node;
3778 /* Basic concepts [gram.basic] */
3780 /* Parse a translation-unit.
3782 translation-unit:
3783 declaration-seq [opt]
3785 Returns TRUE if all went well. */
3787 static bool
3788 cp_parser_translation_unit (cp_parser* parser)
3790 /* The address of the first non-permanent object on the declarator
3791 obstack. */
3792 static void *declarator_obstack_base;
3794 bool success;
3796 /* Create the declarator obstack, if necessary. */
3797 if (!cp_error_declarator)
3799 gcc_obstack_init (&declarator_obstack);
3800 /* Create the error declarator. */
3801 cp_error_declarator = make_declarator (cdk_error);
3802 /* Create the empty parameter list. */
3803 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3804 /* Remember where the base of the declarator obstack lies. */
3805 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3808 cp_parser_declaration_seq_opt (parser);
3810 /* If there are no tokens left then all went well. */
3811 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3813 /* Get rid of the token array; we don't need it any more. */
3814 cp_lexer_destroy (parser->lexer);
3815 parser->lexer = NULL;
3817 /* This file might have been a context that's implicitly extern
3818 "C". If so, pop the lang context. (Only relevant for PCH.) */
3819 if (parser->implicit_extern_c)
3821 pop_lang_context ();
3822 parser->implicit_extern_c = false;
3825 /* Finish up. */
3826 finish_translation_unit ();
3828 success = true;
3830 else
3832 cp_parser_error (parser, "expected declaration");
3833 success = false;
3836 /* Make sure the declarator obstack was fully cleaned up. */
3837 gcc_assert (obstack_next_free (&declarator_obstack)
3838 == declarator_obstack_base);
3840 /* All went well. */
3841 return success;
3844 /* Expressions [gram.expr] */
3846 /* Parse a primary-expression.
3848 primary-expression:
3849 literal
3850 this
3851 ( expression )
3852 id-expression
3854 GNU Extensions:
3856 primary-expression:
3857 ( compound-statement )
3858 __builtin_va_arg ( assignment-expression , type-id )
3859 __builtin_offsetof ( type-id , offsetof-expression )
3861 C++ Extensions:
3862 __has_nothrow_assign ( type-id )
3863 __has_nothrow_constructor ( type-id )
3864 __has_nothrow_copy ( type-id )
3865 __has_trivial_assign ( type-id )
3866 __has_trivial_constructor ( type-id )
3867 __has_trivial_copy ( type-id )
3868 __has_trivial_destructor ( type-id )
3869 __has_virtual_destructor ( type-id )
3870 __is_abstract ( type-id )
3871 __is_base_of ( type-id , type-id )
3872 __is_class ( type-id )
3873 __is_convertible_to ( type-id , type-id )
3874 __is_empty ( type-id )
3875 __is_enum ( type-id )
3876 __is_final ( type-id )
3877 __is_literal_type ( type-id )
3878 __is_pod ( type-id )
3879 __is_polymorphic ( type-id )
3880 __is_std_layout ( type-id )
3881 __is_trivial ( type-id )
3882 __is_union ( type-id )
3884 Objective-C++ Extension:
3886 primary-expression:
3887 objc-expression
3889 literal:
3890 __null
3892 ADDRESS_P is true iff this expression was immediately preceded by
3893 "&" and therefore might denote a pointer-to-member. CAST_P is true
3894 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3895 true iff this expression is a template argument.
3897 Returns a representation of the expression. Upon return, *IDK
3898 indicates what kind of id-expression (if any) was present. */
3900 static tree
3901 cp_parser_primary_expression (cp_parser *parser,
3902 bool address_p,
3903 bool cast_p,
3904 bool template_arg_p,
3905 cp_id_kind *idk)
3907 cp_token *token = NULL;
3909 /* Assume the primary expression is not an id-expression. */
3910 *idk = CP_ID_KIND_NONE;
3912 /* Peek at the next token. */
3913 token = cp_lexer_peek_token (parser->lexer);
3914 switch (token->type)
3916 /* literal:
3917 integer-literal
3918 character-literal
3919 floating-literal
3920 string-literal
3921 boolean-literal
3922 pointer-literal
3923 user-defined-literal */
3924 case CPP_CHAR:
3925 case CPP_CHAR16:
3926 case CPP_CHAR32:
3927 case CPP_WCHAR:
3928 case CPP_NUMBER:
3929 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3930 return cp_parser_userdef_numeric_literal (parser);
3931 token = cp_lexer_consume_token (parser->lexer);
3932 if (TREE_CODE (token->u.value) == FIXED_CST)
3934 error_at (token->location,
3935 "fixed-point types not supported in C++");
3936 return error_mark_node;
3938 /* Floating-point literals are only allowed in an integral
3939 constant expression if they are cast to an integral or
3940 enumeration type. */
3941 if (TREE_CODE (token->u.value) == REAL_CST
3942 && parser->integral_constant_expression_p
3943 && pedantic)
3945 /* CAST_P will be set even in invalid code like "int(2.7 +
3946 ...)". Therefore, we have to check that the next token
3947 is sure to end the cast. */
3948 if (cast_p)
3950 cp_token *next_token;
3952 next_token = cp_lexer_peek_token (parser->lexer);
3953 if (/* The comma at the end of an
3954 enumerator-definition. */
3955 next_token->type != CPP_COMMA
3956 /* The curly brace at the end of an enum-specifier. */
3957 && next_token->type != CPP_CLOSE_BRACE
3958 /* The end of a statement. */
3959 && next_token->type != CPP_SEMICOLON
3960 /* The end of the cast-expression. */
3961 && next_token->type != CPP_CLOSE_PAREN
3962 /* The end of an array bound. */
3963 && next_token->type != CPP_CLOSE_SQUARE
3964 /* The closing ">" in a template-argument-list. */
3965 && (next_token->type != CPP_GREATER
3966 || parser->greater_than_is_operator_p)
3967 /* C++0x only: A ">>" treated like two ">" tokens,
3968 in a template-argument-list. */
3969 && (next_token->type != CPP_RSHIFT
3970 || (cxx_dialect == cxx98)
3971 || parser->greater_than_is_operator_p))
3972 cast_p = false;
3975 /* If we are within a cast, then the constraint that the
3976 cast is to an integral or enumeration type will be
3977 checked at that point. If we are not within a cast, then
3978 this code is invalid. */
3979 if (!cast_p)
3980 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3982 return token->u.value;
3984 case CPP_CHAR_USERDEF:
3985 case CPP_CHAR16_USERDEF:
3986 case CPP_CHAR32_USERDEF:
3987 case CPP_WCHAR_USERDEF:
3988 return cp_parser_userdef_char_literal (parser);
3990 case CPP_STRING:
3991 case CPP_STRING16:
3992 case CPP_STRING32:
3993 case CPP_WSTRING:
3994 case CPP_UTF8STRING:
3995 case CPP_STRING_USERDEF:
3996 case CPP_STRING16_USERDEF:
3997 case CPP_STRING32_USERDEF:
3998 case CPP_WSTRING_USERDEF:
3999 case CPP_UTF8STRING_USERDEF:
4000 /* ??? Should wide strings be allowed when parser->translate_strings_p
4001 is false (i.e. in attributes)? If not, we can kill the third
4002 argument to cp_parser_string_literal. */
4003 return cp_parser_string_literal (parser,
4004 parser->translate_strings_p,
4005 true);
4007 case CPP_OPEN_PAREN:
4009 tree expr;
4010 bool saved_greater_than_is_operator_p;
4012 /* Consume the `('. */
4013 cp_lexer_consume_token (parser->lexer);
4014 /* Within a parenthesized expression, a `>' token is always
4015 the greater-than operator. */
4016 saved_greater_than_is_operator_p
4017 = parser->greater_than_is_operator_p;
4018 parser->greater_than_is_operator_p = true;
4019 /* If we see `( { ' then we are looking at the beginning of
4020 a GNU statement-expression. */
4021 if (cp_parser_allow_gnu_extensions_p (parser)
4022 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4024 /* Statement-expressions are not allowed by the standard. */
4025 pedwarn (token->location, OPT_Wpedantic,
4026 "ISO C++ forbids braced-groups within expressions");
4028 /* And they're not allowed outside of a function-body; you
4029 cannot, for example, write:
4031 int i = ({ int j = 3; j + 1; });
4033 at class or namespace scope. */
4034 if (!parser->in_function_body
4035 || parser->in_template_argument_list_p)
4037 error_at (token->location,
4038 "statement-expressions are not allowed outside "
4039 "functions nor in template-argument lists");
4040 cp_parser_skip_to_end_of_block_or_statement (parser);
4041 expr = error_mark_node;
4043 else
4045 /* Start the statement-expression. */
4046 expr = begin_stmt_expr ();
4047 /* Parse the compound-statement. */
4048 cp_parser_compound_statement (parser, expr, false, false);
4049 /* Finish up. */
4050 expr = finish_stmt_expr (expr, false);
4053 else
4055 /* Parse the parenthesized expression. */
4056 expr = cp_parser_expression (parser, cast_p, idk);
4057 /* Let the front end know that this expression was
4058 enclosed in parentheses. This matters in case, for
4059 example, the expression is of the form `A::B', since
4060 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4061 not. */
4062 finish_parenthesized_expr (expr);
4063 /* DR 705: Wrapping an unqualified name in parentheses
4064 suppresses arg-dependent lookup. We want to pass back
4065 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4066 (c++/37862), but none of the others. */
4067 if (*idk != CP_ID_KIND_QUALIFIED)
4068 *idk = CP_ID_KIND_NONE;
4070 /* The `>' token might be the end of a template-id or
4071 template-parameter-list now. */
4072 parser->greater_than_is_operator_p
4073 = saved_greater_than_is_operator_p;
4074 /* Consume the `)'. */
4075 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4076 cp_parser_skip_to_end_of_statement (parser);
4078 return expr;
4081 case CPP_OPEN_SQUARE:
4082 if (c_dialect_objc ())
4083 /* We have an Objective-C++ message. */
4084 return cp_parser_objc_expression (parser);
4086 tree lam = cp_parser_lambda_expression (parser);
4087 /* Don't warn about a failed tentative parse. */
4088 if (cp_parser_error_occurred (parser))
4089 return error_mark_node;
4090 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4091 return lam;
4094 case CPP_OBJC_STRING:
4095 if (c_dialect_objc ())
4096 /* We have an Objective-C++ string literal. */
4097 return cp_parser_objc_expression (parser);
4098 cp_parser_error (parser, "expected primary-expression");
4099 return error_mark_node;
4101 case CPP_KEYWORD:
4102 switch (token->keyword)
4104 /* These two are the boolean literals. */
4105 case RID_TRUE:
4106 cp_lexer_consume_token (parser->lexer);
4107 return boolean_true_node;
4108 case RID_FALSE:
4109 cp_lexer_consume_token (parser->lexer);
4110 return boolean_false_node;
4112 /* The `__null' literal. */
4113 case RID_NULL:
4114 cp_lexer_consume_token (parser->lexer);
4115 return null_node;
4117 /* The `nullptr' literal. */
4118 case RID_NULLPTR:
4119 cp_lexer_consume_token (parser->lexer);
4120 return nullptr_node;
4122 /* Recognize the `this' keyword. */
4123 case RID_THIS:
4124 cp_lexer_consume_token (parser->lexer);
4125 if (parser->local_variables_forbidden_p)
4127 error_at (token->location,
4128 "%<this%> may not be used in this context");
4129 return error_mark_node;
4131 /* Pointers cannot appear in constant-expressions. */
4132 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4133 return error_mark_node;
4134 return finish_this_expr ();
4136 /* The `operator' keyword can be the beginning of an
4137 id-expression. */
4138 case RID_OPERATOR:
4139 goto id_expression;
4141 case RID_FUNCTION_NAME:
4142 case RID_PRETTY_FUNCTION_NAME:
4143 case RID_C99_FUNCTION_NAME:
4145 non_integral_constant name;
4147 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4148 __func__ are the names of variables -- but they are
4149 treated specially. Therefore, they are handled here,
4150 rather than relying on the generic id-expression logic
4151 below. Grammatically, these names are id-expressions.
4153 Consume the token. */
4154 token = cp_lexer_consume_token (parser->lexer);
4156 switch (token->keyword)
4158 case RID_FUNCTION_NAME:
4159 name = NIC_FUNC_NAME;
4160 break;
4161 case RID_PRETTY_FUNCTION_NAME:
4162 name = NIC_PRETTY_FUNC;
4163 break;
4164 case RID_C99_FUNCTION_NAME:
4165 name = NIC_C99_FUNC;
4166 break;
4167 default:
4168 gcc_unreachable ();
4171 if (cp_parser_non_integral_constant_expression (parser, name))
4172 return error_mark_node;
4174 /* Look up the name. */
4175 return finish_fname (token->u.value);
4178 case RID_VA_ARG:
4180 tree expression;
4181 tree type;
4182 source_location type_location;
4184 /* The `__builtin_va_arg' construct is used to handle
4185 `va_arg'. Consume the `__builtin_va_arg' token. */
4186 cp_lexer_consume_token (parser->lexer);
4187 /* Look for the opening `('. */
4188 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4189 /* Now, parse the assignment-expression. */
4190 expression = cp_parser_assignment_expression (parser,
4191 /*cast_p=*/false, NULL);
4192 /* Look for the `,'. */
4193 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4194 type_location = cp_lexer_peek_token (parser->lexer)->location;
4195 /* Parse the type-id. */
4196 type = cp_parser_type_id (parser);
4197 /* Look for the closing `)'. */
4198 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4199 /* Using `va_arg' in a constant-expression is not
4200 allowed. */
4201 if (cp_parser_non_integral_constant_expression (parser,
4202 NIC_VA_ARG))
4203 return error_mark_node;
4204 return build_x_va_arg (type_location, expression, type);
4207 case RID_OFFSETOF:
4208 return cp_parser_builtin_offsetof (parser);
4210 case RID_HAS_NOTHROW_ASSIGN:
4211 case RID_HAS_NOTHROW_CONSTRUCTOR:
4212 case RID_HAS_NOTHROW_COPY:
4213 case RID_HAS_TRIVIAL_ASSIGN:
4214 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4215 case RID_HAS_TRIVIAL_COPY:
4216 case RID_HAS_TRIVIAL_DESTRUCTOR:
4217 case RID_HAS_VIRTUAL_DESTRUCTOR:
4218 case RID_IS_ABSTRACT:
4219 case RID_IS_BASE_OF:
4220 case RID_IS_CLASS:
4221 case RID_IS_CONVERTIBLE_TO:
4222 case RID_IS_EMPTY:
4223 case RID_IS_ENUM:
4224 case RID_IS_FINAL:
4225 case RID_IS_LITERAL_TYPE:
4226 case RID_IS_POD:
4227 case RID_IS_POLYMORPHIC:
4228 case RID_IS_STD_LAYOUT:
4229 case RID_IS_TRIVIAL:
4230 case RID_IS_UNION:
4231 return cp_parser_trait_expr (parser, token->keyword);
4233 /* Objective-C++ expressions. */
4234 case RID_AT_ENCODE:
4235 case RID_AT_PROTOCOL:
4236 case RID_AT_SELECTOR:
4237 return cp_parser_objc_expression (parser);
4239 case RID_TEMPLATE:
4240 if (parser->in_function_body
4241 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4242 == CPP_LESS))
4244 error_at (token->location,
4245 "a template declaration cannot appear at block scope");
4246 cp_parser_skip_to_end_of_block_or_statement (parser);
4247 return error_mark_node;
4249 default:
4250 cp_parser_error (parser, "expected primary-expression");
4251 return error_mark_node;
4254 /* An id-expression can start with either an identifier, a
4255 `::' as the beginning of a qualified-id, or the "operator"
4256 keyword. */
4257 case CPP_NAME:
4258 case CPP_SCOPE:
4259 case CPP_TEMPLATE_ID:
4260 case CPP_NESTED_NAME_SPECIFIER:
4262 tree id_expression;
4263 tree decl;
4264 const char *error_msg;
4265 bool template_p;
4266 bool done;
4267 cp_token *id_expr_token;
4269 id_expression:
4270 /* Parse the id-expression. */
4271 id_expression
4272 = cp_parser_id_expression (parser,
4273 /*template_keyword_p=*/false,
4274 /*check_dependency_p=*/true,
4275 &template_p,
4276 /*declarator_p=*/false,
4277 /*optional_p=*/false);
4278 if (id_expression == error_mark_node)
4279 return error_mark_node;
4280 id_expr_token = token;
4281 token = cp_lexer_peek_token (parser->lexer);
4282 done = (token->type != CPP_OPEN_SQUARE
4283 && token->type != CPP_OPEN_PAREN
4284 && token->type != CPP_DOT
4285 && token->type != CPP_DEREF
4286 && token->type != CPP_PLUS_PLUS
4287 && token->type != CPP_MINUS_MINUS);
4288 /* If we have a template-id, then no further lookup is
4289 required. If the template-id was for a template-class, we
4290 will sometimes have a TYPE_DECL at this point. */
4291 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4292 || TREE_CODE (id_expression) == TYPE_DECL)
4293 decl = id_expression;
4294 /* Look up the name. */
4295 else
4297 tree ambiguous_decls;
4299 /* If we already know that this lookup is ambiguous, then
4300 we've already issued an error message; there's no reason
4301 to check again. */
4302 if (id_expr_token->type == CPP_NAME
4303 && id_expr_token->ambiguous_p)
4305 cp_parser_simulate_error (parser);
4306 return error_mark_node;
4309 decl = cp_parser_lookup_name (parser, id_expression,
4310 none_type,
4311 template_p,
4312 /*is_namespace=*/false,
4313 /*check_dependency=*/true,
4314 &ambiguous_decls,
4315 id_expr_token->location);
4316 /* If the lookup was ambiguous, an error will already have
4317 been issued. */
4318 if (ambiguous_decls)
4319 return error_mark_node;
4321 /* In Objective-C++, we may have an Objective-C 2.0
4322 dot-syntax for classes here. */
4323 if (c_dialect_objc ()
4324 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4325 && TREE_CODE (decl) == TYPE_DECL
4326 && objc_is_class_name (decl))
4328 tree component;
4329 cp_lexer_consume_token (parser->lexer);
4330 component = cp_parser_identifier (parser);
4331 if (component == error_mark_node)
4332 return error_mark_node;
4334 return objc_build_class_component_ref (id_expression, component);
4337 /* In Objective-C++, an instance variable (ivar) may be preferred
4338 to whatever cp_parser_lookup_name() found. */
4339 decl = objc_lookup_ivar (decl, id_expression);
4341 /* If name lookup gives us a SCOPE_REF, then the
4342 qualifying scope was dependent. */
4343 if (TREE_CODE (decl) == SCOPE_REF)
4345 /* At this point, we do not know if DECL is a valid
4346 integral constant expression. We assume that it is
4347 in fact such an expression, so that code like:
4349 template <int N> struct A {
4350 int a[B<N>::i];
4353 is accepted. At template-instantiation time, we
4354 will check that B<N>::i is actually a constant. */
4355 return decl;
4357 /* Check to see if DECL is a local variable in a context
4358 where that is forbidden. */
4359 if (parser->local_variables_forbidden_p
4360 && local_variable_p (decl))
4362 /* It might be that we only found DECL because we are
4363 trying to be generous with pre-ISO scoping rules.
4364 For example, consider:
4366 int i;
4367 void g() {
4368 for (int i = 0; i < 10; ++i) {}
4369 extern void f(int j = i);
4372 Here, name look up will originally find the out
4373 of scope `i'. We need to issue a warning message,
4374 but then use the global `i'. */
4375 decl = check_for_out_of_scope_variable (decl);
4376 if (local_variable_p (decl))
4378 error_at (id_expr_token->location,
4379 "local variable %qD may not appear in this context",
4380 decl);
4381 return error_mark_node;
4386 decl = (finish_id_expression
4387 (id_expression, decl, parser->scope,
4388 idk,
4389 parser->integral_constant_expression_p,
4390 parser->allow_non_integral_constant_expression_p,
4391 &parser->non_integral_constant_expression_p,
4392 template_p, done, address_p,
4393 template_arg_p,
4394 &error_msg,
4395 id_expr_token->location));
4396 if (error_msg)
4397 cp_parser_error (parser, error_msg);
4398 return decl;
4401 /* Anything else is an error. */
4402 default:
4403 cp_parser_error (parser, "expected primary-expression");
4404 return error_mark_node;
4408 /* Parse an id-expression.
4410 id-expression:
4411 unqualified-id
4412 qualified-id
4414 qualified-id:
4415 :: [opt] nested-name-specifier template [opt] unqualified-id
4416 :: identifier
4417 :: operator-function-id
4418 :: template-id
4420 Return a representation of the unqualified portion of the
4421 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4422 a `::' or nested-name-specifier.
4424 Often, if the id-expression was a qualified-id, the caller will
4425 want to make a SCOPE_REF to represent the qualified-id. This
4426 function does not do this in order to avoid wastefully creating
4427 SCOPE_REFs when they are not required.
4429 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4430 `template' keyword.
4432 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4433 uninstantiated templates.
4435 If *TEMPLATE_P is non-NULL, it is set to true iff the
4436 `template' keyword is used to explicitly indicate that the entity
4437 named is a template.
4439 If DECLARATOR_P is true, the id-expression is appearing as part of
4440 a declarator, rather than as part of an expression. */
4442 static tree
4443 cp_parser_id_expression (cp_parser *parser,
4444 bool template_keyword_p,
4445 bool check_dependency_p,
4446 bool *template_p,
4447 bool declarator_p,
4448 bool optional_p)
4450 bool global_scope_p;
4451 bool nested_name_specifier_p;
4453 /* Assume the `template' keyword was not used. */
4454 if (template_p)
4455 *template_p = template_keyword_p;
4457 /* Look for the optional `::' operator. */
4458 global_scope_p
4459 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4460 != NULL_TREE);
4461 /* Look for the optional nested-name-specifier. */
4462 nested_name_specifier_p
4463 = (cp_parser_nested_name_specifier_opt (parser,
4464 /*typename_keyword_p=*/false,
4465 check_dependency_p,
4466 /*type_p=*/false,
4467 declarator_p)
4468 != NULL_TREE);
4469 /* If there is a nested-name-specifier, then we are looking at
4470 the first qualified-id production. */
4471 if (nested_name_specifier_p)
4473 tree saved_scope;
4474 tree saved_object_scope;
4475 tree saved_qualifying_scope;
4476 tree unqualified_id;
4477 bool is_template;
4479 /* See if the next token is the `template' keyword. */
4480 if (!template_p)
4481 template_p = &is_template;
4482 *template_p = cp_parser_optional_template_keyword (parser);
4483 /* Name lookup we do during the processing of the
4484 unqualified-id might obliterate SCOPE. */
4485 saved_scope = parser->scope;
4486 saved_object_scope = parser->object_scope;
4487 saved_qualifying_scope = parser->qualifying_scope;
4488 /* Process the final unqualified-id. */
4489 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4490 check_dependency_p,
4491 declarator_p,
4492 /*optional_p=*/false);
4493 /* Restore the SAVED_SCOPE for our caller. */
4494 parser->scope = saved_scope;
4495 parser->object_scope = saved_object_scope;
4496 parser->qualifying_scope = saved_qualifying_scope;
4498 return unqualified_id;
4500 /* Otherwise, if we are in global scope, then we are looking at one
4501 of the other qualified-id productions. */
4502 else if (global_scope_p)
4504 cp_token *token;
4505 tree id;
4507 /* Peek at the next token. */
4508 token = cp_lexer_peek_token (parser->lexer);
4510 /* If it's an identifier, and the next token is not a "<", then
4511 we can avoid the template-id case. This is an optimization
4512 for this common case. */
4513 if (token->type == CPP_NAME
4514 && !cp_parser_nth_token_starts_template_argument_list_p
4515 (parser, 2))
4516 return cp_parser_identifier (parser);
4518 cp_parser_parse_tentatively (parser);
4519 /* Try a template-id. */
4520 id = cp_parser_template_id (parser,
4521 /*template_keyword_p=*/false,
4522 /*check_dependency_p=*/true,
4523 none_type,
4524 declarator_p);
4525 /* If that worked, we're done. */
4526 if (cp_parser_parse_definitely (parser))
4527 return id;
4529 /* Peek at the next token. (Changes in the token buffer may
4530 have invalidated the pointer obtained above.) */
4531 token = cp_lexer_peek_token (parser->lexer);
4533 switch (token->type)
4535 case CPP_NAME:
4536 return cp_parser_identifier (parser);
4538 case CPP_KEYWORD:
4539 if (token->keyword == RID_OPERATOR)
4540 return cp_parser_operator_function_id (parser);
4541 /* Fall through. */
4543 default:
4544 cp_parser_error (parser, "expected id-expression");
4545 return error_mark_node;
4548 else
4549 return cp_parser_unqualified_id (parser, template_keyword_p,
4550 /*check_dependency_p=*/true,
4551 declarator_p,
4552 optional_p);
4555 /* Parse an unqualified-id.
4557 unqualified-id:
4558 identifier
4559 operator-function-id
4560 conversion-function-id
4561 ~ class-name
4562 template-id
4564 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4565 keyword, in a construct like `A::template ...'.
4567 Returns a representation of unqualified-id. For the `identifier'
4568 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4569 production a BIT_NOT_EXPR is returned; the operand of the
4570 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4571 other productions, see the documentation accompanying the
4572 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4573 names are looked up in uninstantiated templates. If DECLARATOR_P
4574 is true, the unqualified-id is appearing as part of a declarator,
4575 rather than as part of an expression. */
4577 static tree
4578 cp_parser_unqualified_id (cp_parser* parser,
4579 bool template_keyword_p,
4580 bool check_dependency_p,
4581 bool declarator_p,
4582 bool optional_p)
4584 cp_token *token;
4586 /* Peek at the next token. */
4587 token = cp_lexer_peek_token (parser->lexer);
4589 switch (token->type)
4591 case CPP_NAME:
4593 tree id;
4595 /* We don't know yet whether or not this will be a
4596 template-id. */
4597 cp_parser_parse_tentatively (parser);
4598 /* Try a template-id. */
4599 id = cp_parser_template_id (parser, template_keyword_p,
4600 check_dependency_p,
4601 none_type,
4602 declarator_p);
4603 /* If it worked, we're done. */
4604 if (cp_parser_parse_definitely (parser))
4605 return id;
4606 /* Otherwise, it's an ordinary identifier. */
4607 return cp_parser_identifier (parser);
4610 case CPP_TEMPLATE_ID:
4611 return cp_parser_template_id (parser, template_keyword_p,
4612 check_dependency_p,
4613 none_type,
4614 declarator_p);
4616 case CPP_COMPL:
4618 tree type_decl;
4619 tree qualifying_scope;
4620 tree object_scope;
4621 tree scope;
4622 bool done;
4624 /* Consume the `~' token. */
4625 cp_lexer_consume_token (parser->lexer);
4626 /* Parse the class-name. The standard, as written, seems to
4627 say that:
4629 template <typename T> struct S { ~S (); };
4630 template <typename T> S<T>::~S() {}
4632 is invalid, since `~' must be followed by a class-name, but
4633 `S<T>' is dependent, and so not known to be a class.
4634 That's not right; we need to look in uninstantiated
4635 templates. A further complication arises from:
4637 template <typename T> void f(T t) {
4638 t.T::~T();
4641 Here, it is not possible to look up `T' in the scope of `T'
4642 itself. We must look in both the current scope, and the
4643 scope of the containing complete expression.
4645 Yet another issue is:
4647 struct S {
4648 int S;
4649 ~S();
4652 S::~S() {}
4654 The standard does not seem to say that the `S' in `~S'
4655 should refer to the type `S' and not the data member
4656 `S::S'. */
4658 /* DR 244 says that we look up the name after the "~" in the
4659 same scope as we looked up the qualifying name. That idea
4660 isn't fully worked out; it's more complicated than that. */
4661 scope = parser->scope;
4662 object_scope = parser->object_scope;
4663 qualifying_scope = parser->qualifying_scope;
4665 /* Check for invalid scopes. */
4666 if (scope == error_mark_node)
4668 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4669 cp_lexer_consume_token (parser->lexer);
4670 return error_mark_node;
4672 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4674 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4675 error_at (token->location,
4676 "scope %qT before %<~%> is not a class-name",
4677 scope);
4678 cp_parser_simulate_error (parser);
4679 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4680 cp_lexer_consume_token (parser->lexer);
4681 return error_mark_node;
4683 gcc_assert (!scope || TYPE_P (scope));
4685 /* If the name is of the form "X::~X" it's OK even if X is a
4686 typedef. */
4687 token = cp_lexer_peek_token (parser->lexer);
4688 if (scope
4689 && token->type == CPP_NAME
4690 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4691 != CPP_LESS)
4692 && (token->u.value == TYPE_IDENTIFIER (scope)
4693 || (CLASS_TYPE_P (scope)
4694 && constructor_name_p (token->u.value, scope))))
4696 cp_lexer_consume_token (parser->lexer);
4697 return build_nt (BIT_NOT_EXPR, scope);
4700 /* If there was an explicit qualification (S::~T), first look
4701 in the scope given by the qualification (i.e., S).
4703 Note: in the calls to cp_parser_class_name below we pass
4704 typename_type so that lookup finds the injected-class-name
4705 rather than the constructor. */
4706 done = false;
4707 type_decl = NULL_TREE;
4708 if (scope)
4710 cp_parser_parse_tentatively (parser);
4711 type_decl = cp_parser_class_name (parser,
4712 /*typename_keyword_p=*/false,
4713 /*template_keyword_p=*/false,
4714 typename_type,
4715 /*check_dependency=*/false,
4716 /*class_head_p=*/false,
4717 declarator_p);
4718 if (cp_parser_parse_definitely (parser))
4719 done = true;
4721 /* In "N::S::~S", look in "N" as well. */
4722 if (!done && scope && qualifying_scope)
4724 cp_parser_parse_tentatively (parser);
4725 parser->scope = qualifying_scope;
4726 parser->object_scope = NULL_TREE;
4727 parser->qualifying_scope = NULL_TREE;
4728 type_decl
4729 = cp_parser_class_name (parser,
4730 /*typename_keyword_p=*/false,
4731 /*template_keyword_p=*/false,
4732 typename_type,
4733 /*check_dependency=*/false,
4734 /*class_head_p=*/false,
4735 declarator_p);
4736 if (cp_parser_parse_definitely (parser))
4737 done = true;
4739 /* In "p->S::~T", look in the scope given by "*p" as well. */
4740 else if (!done && object_scope)
4742 cp_parser_parse_tentatively (parser);
4743 parser->scope = object_scope;
4744 parser->object_scope = NULL_TREE;
4745 parser->qualifying_scope = NULL_TREE;
4746 type_decl
4747 = cp_parser_class_name (parser,
4748 /*typename_keyword_p=*/false,
4749 /*template_keyword_p=*/false,
4750 typename_type,
4751 /*check_dependency=*/false,
4752 /*class_head_p=*/false,
4753 declarator_p);
4754 if (cp_parser_parse_definitely (parser))
4755 done = true;
4757 /* Look in the surrounding context. */
4758 if (!done)
4760 parser->scope = NULL_TREE;
4761 parser->object_scope = NULL_TREE;
4762 parser->qualifying_scope = NULL_TREE;
4763 if (processing_template_decl)
4764 cp_parser_parse_tentatively (parser);
4765 type_decl
4766 = cp_parser_class_name (parser,
4767 /*typename_keyword_p=*/false,
4768 /*template_keyword_p=*/false,
4769 typename_type,
4770 /*check_dependency=*/false,
4771 /*class_head_p=*/false,
4772 declarator_p);
4773 if (processing_template_decl
4774 && ! cp_parser_parse_definitely (parser))
4776 /* We couldn't find a type with this name, so just accept
4777 it and check for a match at instantiation time. */
4778 type_decl = cp_parser_identifier (parser);
4779 if (type_decl != error_mark_node)
4780 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4781 return type_decl;
4784 /* If an error occurred, assume that the name of the
4785 destructor is the same as the name of the qualifying
4786 class. That allows us to keep parsing after running
4787 into ill-formed destructor names. */
4788 if (type_decl == error_mark_node && scope)
4789 return build_nt (BIT_NOT_EXPR, scope);
4790 else if (type_decl == error_mark_node)
4791 return error_mark_node;
4793 /* Check that destructor name and scope match. */
4794 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4796 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4797 error_at (token->location,
4798 "declaration of %<~%T%> as member of %qT",
4799 type_decl, scope);
4800 cp_parser_simulate_error (parser);
4801 return error_mark_node;
4804 /* [class.dtor]
4806 A typedef-name that names a class shall not be used as the
4807 identifier in the declarator for a destructor declaration. */
4808 if (declarator_p
4809 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4810 && !DECL_SELF_REFERENCE_P (type_decl)
4811 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4812 error_at (token->location,
4813 "typedef-name %qD used as destructor declarator",
4814 type_decl);
4816 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4819 case CPP_KEYWORD:
4820 if (token->keyword == RID_OPERATOR)
4822 tree id;
4824 /* This could be a template-id, so we try that first. */
4825 cp_parser_parse_tentatively (parser);
4826 /* Try a template-id. */
4827 id = cp_parser_template_id (parser, template_keyword_p,
4828 /*check_dependency_p=*/true,
4829 none_type,
4830 declarator_p);
4831 /* If that worked, we're done. */
4832 if (cp_parser_parse_definitely (parser))
4833 return id;
4834 /* We still don't know whether we're looking at an
4835 operator-function-id or a conversion-function-id. */
4836 cp_parser_parse_tentatively (parser);
4837 /* Try an operator-function-id. */
4838 id = cp_parser_operator_function_id (parser);
4839 /* If that didn't work, try a conversion-function-id. */
4840 if (!cp_parser_parse_definitely (parser))
4841 id = cp_parser_conversion_function_id (parser);
4842 else if (UDLIT_OPER_P (id))
4844 /* 17.6.3.3.5 */
4845 const char *name = UDLIT_OP_SUFFIX (id);
4846 if (name[0] != '_' && !in_system_header)
4847 warning (0, "literal operator suffixes not preceded by %<_%>"
4848 " are reserved for future standardization");
4851 return id;
4853 /* Fall through. */
4855 default:
4856 if (optional_p)
4857 return NULL_TREE;
4858 cp_parser_error (parser, "expected unqualified-id");
4859 return error_mark_node;
4863 /* Parse an (optional) nested-name-specifier.
4865 nested-name-specifier: [C++98]
4866 class-or-namespace-name :: nested-name-specifier [opt]
4867 class-or-namespace-name :: template nested-name-specifier [opt]
4869 nested-name-specifier: [C++0x]
4870 type-name ::
4871 namespace-name ::
4872 nested-name-specifier identifier ::
4873 nested-name-specifier template [opt] simple-template-id ::
4875 PARSER->SCOPE should be set appropriately before this function is
4876 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4877 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4878 in name lookups.
4880 Sets PARSER->SCOPE to the class (TYPE) or namespace
4881 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4882 it unchanged if there is no nested-name-specifier. Returns the new
4883 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4885 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4886 part of a declaration and/or decl-specifier. */
4888 static tree
4889 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4890 bool typename_keyword_p,
4891 bool check_dependency_p,
4892 bool type_p,
4893 bool is_declaration)
4895 bool success = false;
4896 cp_token_position start = 0;
4897 cp_token *token;
4899 /* Remember where the nested-name-specifier starts. */
4900 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4902 start = cp_lexer_token_position (parser->lexer, false);
4903 push_deferring_access_checks (dk_deferred);
4906 while (true)
4908 tree new_scope;
4909 tree old_scope;
4910 tree saved_qualifying_scope;
4911 bool template_keyword_p;
4913 /* Spot cases that cannot be the beginning of a
4914 nested-name-specifier. */
4915 token = cp_lexer_peek_token (parser->lexer);
4917 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4918 the already parsed nested-name-specifier. */
4919 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4921 /* Grab the nested-name-specifier and continue the loop. */
4922 cp_parser_pre_parsed_nested_name_specifier (parser);
4923 /* If we originally encountered this nested-name-specifier
4924 with IS_DECLARATION set to false, we will not have
4925 resolved TYPENAME_TYPEs, so we must do so here. */
4926 if (is_declaration
4927 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4929 new_scope = resolve_typename_type (parser->scope,
4930 /*only_current_p=*/false);
4931 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4932 parser->scope = new_scope;
4934 success = true;
4935 continue;
4938 /* Spot cases that cannot be the beginning of a
4939 nested-name-specifier. On the second and subsequent times
4940 through the loop, we look for the `template' keyword. */
4941 if (success && token->keyword == RID_TEMPLATE)
4943 /* A template-id can start a nested-name-specifier. */
4944 else if (token->type == CPP_TEMPLATE_ID)
4946 /* DR 743: decltype can be used in a nested-name-specifier. */
4947 else if (token_is_decltype (token))
4949 else
4951 /* If the next token is not an identifier, then it is
4952 definitely not a type-name or namespace-name. */
4953 if (token->type != CPP_NAME)
4954 break;
4955 /* If the following token is neither a `<' (to begin a
4956 template-id), nor a `::', then we are not looking at a
4957 nested-name-specifier. */
4958 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4960 if (token->type == CPP_COLON
4961 && parser->colon_corrects_to_scope_p
4962 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4964 error_at (token->location,
4965 "found %<:%> in nested-name-specifier, expected %<::%>");
4966 token->type = CPP_SCOPE;
4969 if (token->type != CPP_SCOPE
4970 && !cp_parser_nth_token_starts_template_argument_list_p
4971 (parser, 2))
4972 break;
4975 /* The nested-name-specifier is optional, so we parse
4976 tentatively. */
4977 cp_parser_parse_tentatively (parser);
4979 /* Look for the optional `template' keyword, if this isn't the
4980 first time through the loop. */
4981 if (success)
4982 template_keyword_p = cp_parser_optional_template_keyword (parser);
4983 else
4984 template_keyword_p = false;
4986 /* Save the old scope since the name lookup we are about to do
4987 might destroy it. */
4988 old_scope = parser->scope;
4989 saved_qualifying_scope = parser->qualifying_scope;
4990 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4991 look up names in "X<T>::I" in order to determine that "Y" is
4992 a template. So, if we have a typename at this point, we make
4993 an effort to look through it. */
4994 if (is_declaration
4995 && !typename_keyword_p
4996 && parser->scope
4997 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4998 parser->scope = resolve_typename_type (parser->scope,
4999 /*only_current_p=*/false);
5000 /* Parse the qualifying entity. */
5001 new_scope
5002 = cp_parser_qualifying_entity (parser,
5003 typename_keyword_p,
5004 template_keyword_p,
5005 check_dependency_p,
5006 type_p,
5007 is_declaration);
5008 /* Look for the `::' token. */
5009 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5011 /* If we found what we wanted, we keep going; otherwise, we're
5012 done. */
5013 if (!cp_parser_parse_definitely (parser))
5015 bool error_p = false;
5017 /* Restore the OLD_SCOPE since it was valid before the
5018 failed attempt at finding the last
5019 class-or-namespace-name. */
5020 parser->scope = old_scope;
5021 parser->qualifying_scope = saved_qualifying_scope;
5023 /* If the next token is a decltype, and the one after that is a
5024 `::', then the decltype has failed to resolve to a class or
5025 enumeration type. Give this error even when parsing
5026 tentatively since it can't possibly be valid--and we're going
5027 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5028 won't get another chance.*/
5029 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5030 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5031 == CPP_SCOPE))
5033 token = cp_lexer_consume_token (parser->lexer);
5034 error_at (token->location, "decltype evaluates to %qT, "
5035 "which is not a class or enumeration type",
5036 token->u.value);
5037 parser->scope = error_mark_node;
5038 error_p = true;
5039 /* As below. */
5040 success = true;
5041 cp_lexer_consume_token (parser->lexer);
5044 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5045 break;
5046 /* If the next token is an identifier, and the one after
5047 that is a `::', then any valid interpretation would have
5048 found a class-or-namespace-name. */
5049 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5050 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5051 == CPP_SCOPE)
5052 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5053 != CPP_COMPL))
5055 token = cp_lexer_consume_token (parser->lexer);
5056 if (!error_p)
5058 if (!token->ambiguous_p)
5060 tree decl;
5061 tree ambiguous_decls;
5063 decl = cp_parser_lookup_name (parser, token->u.value,
5064 none_type,
5065 /*is_template=*/false,
5066 /*is_namespace=*/false,
5067 /*check_dependency=*/true,
5068 &ambiguous_decls,
5069 token->location);
5070 if (TREE_CODE (decl) == TEMPLATE_DECL)
5071 error_at (token->location,
5072 "%qD used without template parameters",
5073 decl);
5074 else if (ambiguous_decls)
5076 error_at (token->location,
5077 "reference to %qD is ambiguous",
5078 token->u.value);
5079 print_candidates (ambiguous_decls);
5080 decl = error_mark_node;
5082 else
5084 if (cxx_dialect != cxx98)
5085 cp_parser_name_lookup_error
5086 (parser, token->u.value, decl, NLE_NOT_CXX98,
5087 token->location);
5088 else
5089 cp_parser_name_lookup_error
5090 (parser, token->u.value, decl, NLE_CXX98,
5091 token->location);
5094 parser->scope = error_mark_node;
5095 error_p = true;
5096 /* Treat this as a successful nested-name-specifier
5097 due to:
5099 [basic.lookup.qual]
5101 If the name found is not a class-name (clause
5102 _class_) or namespace-name (_namespace.def_), the
5103 program is ill-formed. */
5104 success = true;
5106 cp_lexer_consume_token (parser->lexer);
5108 break;
5110 /* We've found one valid nested-name-specifier. */
5111 success = true;
5112 /* Name lookup always gives us a DECL. */
5113 if (TREE_CODE (new_scope) == TYPE_DECL)
5114 new_scope = TREE_TYPE (new_scope);
5115 /* Uses of "template" must be followed by actual templates. */
5116 if (template_keyword_p
5117 && !(CLASS_TYPE_P (new_scope)
5118 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5119 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5120 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5121 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5122 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5123 == TEMPLATE_ID_EXPR)))
5124 permerror (input_location, TYPE_P (new_scope)
5125 ? G_("%qT is not a template")
5126 : G_("%qD is not a template"),
5127 new_scope);
5128 /* If it is a class scope, try to complete it; we are about to
5129 be looking up names inside the class. */
5130 if (TYPE_P (new_scope)
5131 /* Since checking types for dependency can be expensive,
5132 avoid doing it if the type is already complete. */
5133 && !COMPLETE_TYPE_P (new_scope)
5134 /* Do not try to complete dependent types. */
5135 && !dependent_type_p (new_scope))
5137 new_scope = complete_type (new_scope);
5138 /* If it is a typedef to current class, use the current
5139 class instead, as the typedef won't have any names inside
5140 it yet. */
5141 if (!COMPLETE_TYPE_P (new_scope)
5142 && currently_open_class (new_scope))
5143 new_scope = TYPE_MAIN_VARIANT (new_scope);
5145 /* Make sure we look in the right scope the next time through
5146 the loop. */
5147 parser->scope = new_scope;
5150 /* If parsing tentatively, replace the sequence of tokens that makes
5151 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5152 token. That way, should we re-parse the token stream, we will
5153 not have to repeat the effort required to do the parse, nor will
5154 we issue duplicate error messages. */
5155 if (success && start)
5157 cp_token *token;
5159 token = cp_lexer_token_at (parser->lexer, start);
5160 /* Reset the contents of the START token. */
5161 token->type = CPP_NESTED_NAME_SPECIFIER;
5162 /* Retrieve any deferred checks. Do not pop this access checks yet
5163 so the memory will not be reclaimed during token replacing below. */
5164 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5165 token->u.tree_check_value->value = parser->scope;
5166 token->u.tree_check_value->checks = get_deferred_access_checks ();
5167 token->u.tree_check_value->qualifying_scope =
5168 parser->qualifying_scope;
5169 token->keyword = RID_MAX;
5171 /* Purge all subsequent tokens. */
5172 cp_lexer_purge_tokens_after (parser->lexer, start);
5175 if (start)
5176 pop_to_parent_deferring_access_checks ();
5178 return success ? parser->scope : NULL_TREE;
5181 /* Parse a nested-name-specifier. See
5182 cp_parser_nested_name_specifier_opt for details. This function
5183 behaves identically, except that it will an issue an error if no
5184 nested-name-specifier is present. */
5186 static tree
5187 cp_parser_nested_name_specifier (cp_parser *parser,
5188 bool typename_keyword_p,
5189 bool check_dependency_p,
5190 bool type_p,
5191 bool is_declaration)
5193 tree scope;
5195 /* Look for the nested-name-specifier. */
5196 scope = cp_parser_nested_name_specifier_opt (parser,
5197 typename_keyword_p,
5198 check_dependency_p,
5199 type_p,
5200 is_declaration);
5201 /* If it was not present, issue an error message. */
5202 if (!scope)
5204 cp_parser_error (parser, "expected nested-name-specifier");
5205 parser->scope = NULL_TREE;
5208 return scope;
5211 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5212 this is either a class-name or a namespace-name (which corresponds
5213 to the class-or-namespace-name production in the grammar). For
5214 C++0x, it can also be a type-name that refers to an enumeration
5215 type or a simple-template-id.
5217 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5218 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5219 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5220 TYPE_P is TRUE iff the next name should be taken as a class-name,
5221 even the same name is declared to be another entity in the same
5222 scope.
5224 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5225 specified by the class-or-namespace-name. If neither is found the
5226 ERROR_MARK_NODE is returned. */
5228 static tree
5229 cp_parser_qualifying_entity (cp_parser *parser,
5230 bool typename_keyword_p,
5231 bool template_keyword_p,
5232 bool check_dependency_p,
5233 bool type_p,
5234 bool is_declaration)
5236 tree saved_scope;
5237 tree saved_qualifying_scope;
5238 tree saved_object_scope;
5239 tree scope;
5240 bool only_class_p;
5241 bool successful_parse_p;
5243 /* DR 743: decltype can appear in a nested-name-specifier. */
5244 if (cp_lexer_next_token_is_decltype (parser->lexer))
5246 scope = cp_parser_decltype (parser);
5247 if (TREE_CODE (scope) != ENUMERAL_TYPE
5248 && !MAYBE_CLASS_TYPE_P (scope))
5250 cp_parser_simulate_error (parser);
5251 return error_mark_node;
5253 if (TYPE_NAME (scope))
5254 scope = TYPE_NAME (scope);
5255 return scope;
5258 /* Before we try to parse the class-name, we must save away the
5259 current PARSER->SCOPE since cp_parser_class_name will destroy
5260 it. */
5261 saved_scope = parser->scope;
5262 saved_qualifying_scope = parser->qualifying_scope;
5263 saved_object_scope = parser->object_scope;
5264 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5265 there is no need to look for a namespace-name. */
5266 only_class_p = template_keyword_p
5267 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5268 if (!only_class_p)
5269 cp_parser_parse_tentatively (parser);
5270 scope = cp_parser_class_name (parser,
5271 typename_keyword_p,
5272 template_keyword_p,
5273 type_p ? class_type : none_type,
5274 check_dependency_p,
5275 /*class_head_p=*/false,
5276 is_declaration);
5277 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5278 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5279 if (!only_class_p
5280 && cxx_dialect != cxx98
5281 && !successful_parse_p)
5283 /* Restore the saved scope. */
5284 parser->scope = saved_scope;
5285 parser->qualifying_scope = saved_qualifying_scope;
5286 parser->object_scope = saved_object_scope;
5288 /* Parse tentatively. */
5289 cp_parser_parse_tentatively (parser);
5291 /* Parse a type-name */
5292 scope = cp_parser_type_name (parser);
5294 /* "If the name found does not designate a namespace or a class,
5295 enumeration, or dependent type, the program is ill-formed."
5297 We cover classes and dependent types above and namespaces below,
5298 so this code is only looking for enums. */
5299 if (!scope || TREE_CODE (scope) != TYPE_DECL
5300 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5301 cp_parser_simulate_error (parser);
5303 successful_parse_p = cp_parser_parse_definitely (parser);
5305 /* If that didn't work, try for a namespace-name. */
5306 if (!only_class_p && !successful_parse_p)
5308 /* Restore the saved scope. */
5309 parser->scope = saved_scope;
5310 parser->qualifying_scope = saved_qualifying_scope;
5311 parser->object_scope = saved_object_scope;
5312 /* If we are not looking at an identifier followed by the scope
5313 resolution operator, then this is not part of a
5314 nested-name-specifier. (Note that this function is only used
5315 to parse the components of a nested-name-specifier.) */
5316 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5317 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5318 return error_mark_node;
5319 scope = cp_parser_namespace_name (parser);
5322 return scope;
5325 /* Parse a postfix-expression.
5327 postfix-expression:
5328 primary-expression
5329 postfix-expression [ expression ]
5330 postfix-expression ( expression-list [opt] )
5331 simple-type-specifier ( expression-list [opt] )
5332 typename :: [opt] nested-name-specifier identifier
5333 ( expression-list [opt] )
5334 typename :: [opt] nested-name-specifier template [opt] template-id
5335 ( expression-list [opt] )
5336 postfix-expression . template [opt] id-expression
5337 postfix-expression -> template [opt] id-expression
5338 postfix-expression . pseudo-destructor-name
5339 postfix-expression -> pseudo-destructor-name
5340 postfix-expression ++
5341 postfix-expression --
5342 dynamic_cast < type-id > ( expression )
5343 static_cast < type-id > ( expression )
5344 reinterpret_cast < type-id > ( expression )
5345 const_cast < type-id > ( expression )
5346 typeid ( expression )
5347 typeid ( type-id )
5349 GNU Extension:
5351 postfix-expression:
5352 ( type-id ) { initializer-list , [opt] }
5354 This extension is a GNU version of the C99 compound-literal
5355 construct. (The C99 grammar uses `type-name' instead of `type-id',
5356 but they are essentially the same concept.)
5358 If ADDRESS_P is true, the postfix expression is the operand of the
5359 `&' operator. CAST_P is true if this expression is the target of a
5360 cast.
5362 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5363 class member access expressions [expr.ref].
5365 Returns a representation of the expression. */
5367 static tree
5368 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5369 bool member_access_only_p,
5370 cp_id_kind * pidk_return)
5372 cp_token *token;
5373 enum rid keyword;
5374 cp_id_kind idk = CP_ID_KIND_NONE;
5375 tree postfix_expression = NULL_TREE;
5376 bool is_member_access = false;
5378 /* Peek at the next token. */
5379 token = cp_lexer_peek_token (parser->lexer);
5380 /* Some of the productions are determined by keywords. */
5381 keyword = token->keyword;
5382 switch (keyword)
5384 case RID_DYNCAST:
5385 case RID_STATCAST:
5386 case RID_REINTCAST:
5387 case RID_CONSTCAST:
5389 tree type;
5390 tree expression;
5391 const char *saved_message;
5393 /* All of these can be handled in the same way from the point
5394 of view of parsing. Begin by consuming the token
5395 identifying the cast. */
5396 cp_lexer_consume_token (parser->lexer);
5398 /* New types cannot be defined in the cast. */
5399 saved_message = parser->type_definition_forbidden_message;
5400 parser->type_definition_forbidden_message
5401 = G_("types may not be defined in casts");
5403 /* Look for the opening `<'. */
5404 cp_parser_require (parser, CPP_LESS, RT_LESS);
5405 /* Parse the type to which we are casting. */
5406 type = cp_parser_type_id (parser);
5407 /* Look for the closing `>'. */
5408 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5409 /* Restore the old message. */
5410 parser->type_definition_forbidden_message = saved_message;
5412 /* And the expression which is being cast. */
5413 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5414 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5415 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5417 /* Only type conversions to integral or enumeration types
5418 can be used in constant-expressions. */
5419 if (!cast_valid_in_integral_constant_expression_p (type)
5420 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5421 return error_mark_node;
5423 switch (keyword)
5425 case RID_DYNCAST:
5426 postfix_expression
5427 = build_dynamic_cast (type, expression, tf_warning_or_error);
5428 break;
5429 case RID_STATCAST:
5430 postfix_expression
5431 = build_static_cast (type, expression, tf_warning_or_error);
5432 break;
5433 case RID_REINTCAST:
5434 postfix_expression
5435 = build_reinterpret_cast (type, expression,
5436 tf_warning_or_error);
5437 break;
5438 case RID_CONSTCAST:
5439 postfix_expression
5440 = build_const_cast (type, expression, tf_warning_or_error);
5441 break;
5442 default:
5443 gcc_unreachable ();
5446 break;
5448 case RID_TYPEID:
5450 tree type;
5451 const char *saved_message;
5452 bool saved_in_type_id_in_expr_p;
5454 /* Consume the `typeid' token. */
5455 cp_lexer_consume_token (parser->lexer);
5456 /* Look for the `(' token. */
5457 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5458 /* Types cannot be defined in a `typeid' expression. */
5459 saved_message = parser->type_definition_forbidden_message;
5460 parser->type_definition_forbidden_message
5461 = G_("types may not be defined in a %<typeid%> expression");
5462 /* We can't be sure yet whether we're looking at a type-id or an
5463 expression. */
5464 cp_parser_parse_tentatively (parser);
5465 /* Try a type-id first. */
5466 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5467 parser->in_type_id_in_expr_p = true;
5468 type = cp_parser_type_id (parser);
5469 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5470 /* Look for the `)' token. Otherwise, we can't be sure that
5471 we're not looking at an expression: consider `typeid (int
5472 (3))', for example. */
5473 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5474 /* If all went well, simply lookup the type-id. */
5475 if (cp_parser_parse_definitely (parser))
5476 postfix_expression = get_typeid (type);
5477 /* Otherwise, fall back to the expression variant. */
5478 else
5480 tree expression;
5482 /* Look for an expression. */
5483 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5484 /* Compute its typeid. */
5485 postfix_expression = build_typeid (expression);
5486 /* Look for the `)' token. */
5487 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5489 /* Restore the saved message. */
5490 parser->type_definition_forbidden_message = saved_message;
5491 /* `typeid' may not appear in an integral constant expression. */
5492 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5493 return error_mark_node;
5495 break;
5497 case RID_TYPENAME:
5499 tree type;
5500 /* The syntax permitted here is the same permitted for an
5501 elaborated-type-specifier. */
5502 type = cp_parser_elaborated_type_specifier (parser,
5503 /*is_friend=*/false,
5504 /*is_declaration=*/false);
5505 postfix_expression = cp_parser_functional_cast (parser, type);
5507 break;
5509 case RID_BUILTIN_SHUFFLE:
5511 vec<tree, va_gc> *vec;
5512 unsigned int i;
5513 tree p;
5514 location_t loc = token->location;
5516 cp_lexer_consume_token (parser->lexer);
5517 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5518 /*cast_p=*/false, /*allow_expansion_p=*/true,
5519 /*non_constant_p=*/NULL);
5520 if (vec == NULL)
5521 return error_mark_node;
5523 FOR_EACH_VEC_ELT (*vec, i, p)
5524 mark_exp_read (p);
5526 if (vec->length () == 2)
5527 return c_build_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1]);
5528 else if (vec->length () == 3)
5529 return c_build_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2]);
5530 else
5532 error_at (loc, "wrong number of arguments to "
5533 "%<__builtin_shuffle%>");
5534 return error_mark_node;
5536 break;
5539 default:
5541 tree type;
5543 /* If the next thing is a simple-type-specifier, we may be
5544 looking at a functional cast. We could also be looking at
5545 an id-expression. So, we try the functional cast, and if
5546 that doesn't work we fall back to the primary-expression. */
5547 cp_parser_parse_tentatively (parser);
5548 /* Look for the simple-type-specifier. */
5549 type = cp_parser_simple_type_specifier (parser,
5550 /*decl_specs=*/NULL,
5551 CP_PARSER_FLAGS_NONE);
5552 /* Parse the cast itself. */
5553 if (!cp_parser_error_occurred (parser))
5554 postfix_expression
5555 = cp_parser_functional_cast (parser, type);
5556 /* If that worked, we're done. */
5557 if (cp_parser_parse_definitely (parser))
5558 break;
5560 /* If the functional-cast didn't work out, try a
5561 compound-literal. */
5562 if (cp_parser_allow_gnu_extensions_p (parser)
5563 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5565 vec<constructor_elt, va_gc> *initializer_list = NULL;
5566 bool saved_in_type_id_in_expr_p;
5568 cp_parser_parse_tentatively (parser);
5569 /* Consume the `('. */
5570 cp_lexer_consume_token (parser->lexer);
5571 /* Parse the type. */
5572 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5573 parser->in_type_id_in_expr_p = true;
5574 type = cp_parser_type_id (parser);
5575 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5576 /* Look for the `)'. */
5577 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5578 /* Look for the `{'. */
5579 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5580 /* If things aren't going well, there's no need to
5581 keep going. */
5582 if (!cp_parser_error_occurred (parser))
5584 bool non_constant_p;
5585 /* Parse the initializer-list. */
5586 initializer_list
5587 = cp_parser_initializer_list (parser, &non_constant_p);
5588 /* Allow a trailing `,'. */
5589 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5590 cp_lexer_consume_token (parser->lexer);
5591 /* Look for the final `}'. */
5592 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5594 /* If that worked, we're definitely looking at a
5595 compound-literal expression. */
5596 if (cp_parser_parse_definitely (parser))
5598 /* Warn the user that a compound literal is not
5599 allowed in standard C++. */
5600 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5601 /* For simplicity, we disallow compound literals in
5602 constant-expressions. We could
5603 allow compound literals of integer type, whose
5604 initializer was a constant, in constant
5605 expressions. Permitting that usage, as a further
5606 extension, would not change the meaning of any
5607 currently accepted programs. (Of course, as
5608 compound literals are not part of ISO C++, the
5609 standard has nothing to say.) */
5610 if (cp_parser_non_integral_constant_expression (parser,
5611 NIC_NCC))
5613 postfix_expression = error_mark_node;
5614 break;
5616 /* Form the representation of the compound-literal. */
5617 postfix_expression
5618 = (finish_compound_literal
5619 (type, build_constructor (init_list_type_node,
5620 initializer_list),
5621 tf_warning_or_error));
5622 break;
5626 /* It must be a primary-expression. */
5627 postfix_expression
5628 = cp_parser_primary_expression (parser, address_p, cast_p,
5629 /*template_arg_p=*/false,
5630 &idk);
5632 break;
5635 /* Keep looping until the postfix-expression is complete. */
5636 while (true)
5638 if (idk == CP_ID_KIND_UNQUALIFIED
5639 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5640 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5641 /* It is not a Koenig lookup function call. */
5642 postfix_expression
5643 = unqualified_name_lookup_error (postfix_expression);
5645 /* Peek at the next token. */
5646 token = cp_lexer_peek_token (parser->lexer);
5648 switch (token->type)
5650 case CPP_OPEN_SQUARE:
5651 if (cp_next_tokens_can_be_std_attribute_p (parser))
5653 cp_parser_error (parser,
5654 "two consecutive %<[%> shall "
5655 "only introduce an attribute");
5656 return error_mark_node;
5658 postfix_expression
5659 = cp_parser_postfix_open_square_expression (parser,
5660 postfix_expression,
5661 false);
5662 idk = CP_ID_KIND_NONE;
5663 is_member_access = false;
5664 break;
5666 case CPP_OPEN_PAREN:
5667 /* postfix-expression ( expression-list [opt] ) */
5669 bool koenig_p;
5670 bool is_builtin_constant_p;
5671 bool saved_integral_constant_expression_p = false;
5672 bool saved_non_integral_constant_expression_p = false;
5673 vec<tree, va_gc> *args;
5675 is_member_access = false;
5677 is_builtin_constant_p
5678 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5679 if (is_builtin_constant_p)
5681 /* The whole point of __builtin_constant_p is to allow
5682 non-constant expressions to appear as arguments. */
5683 saved_integral_constant_expression_p
5684 = parser->integral_constant_expression_p;
5685 saved_non_integral_constant_expression_p
5686 = parser->non_integral_constant_expression_p;
5687 parser->integral_constant_expression_p = false;
5689 args = (cp_parser_parenthesized_expression_list
5690 (parser, non_attr,
5691 /*cast_p=*/false, /*allow_expansion_p=*/true,
5692 /*non_constant_p=*/NULL));
5693 if (is_builtin_constant_p)
5695 parser->integral_constant_expression_p
5696 = saved_integral_constant_expression_p;
5697 parser->non_integral_constant_expression_p
5698 = saved_non_integral_constant_expression_p;
5701 if (args == NULL)
5703 postfix_expression = error_mark_node;
5704 break;
5707 /* Function calls are not permitted in
5708 constant-expressions. */
5709 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5710 && cp_parser_non_integral_constant_expression (parser,
5711 NIC_FUNC_CALL))
5713 postfix_expression = error_mark_node;
5714 release_tree_vector (args);
5715 break;
5718 koenig_p = false;
5719 if (idk == CP_ID_KIND_UNQUALIFIED
5720 || idk == CP_ID_KIND_TEMPLATE_ID)
5722 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5724 if (!args->is_empty ())
5726 koenig_p = true;
5727 if (!any_type_dependent_arguments_p (args))
5728 postfix_expression
5729 = perform_koenig_lookup (postfix_expression, args,
5730 /*include_std=*/false,
5731 tf_warning_or_error);
5733 else
5734 postfix_expression
5735 = unqualified_fn_lookup_error (postfix_expression);
5737 /* We do not perform argument-dependent lookup if
5738 normal lookup finds a non-function, in accordance
5739 with the expected resolution of DR 218. */
5740 else if (!args->is_empty ()
5741 && is_overloaded_fn (postfix_expression))
5743 tree fn = get_first_fn (postfix_expression);
5744 fn = STRIP_TEMPLATE (fn);
5746 /* Do not do argument dependent lookup if regular
5747 lookup finds a member function or a block-scope
5748 function declaration. [basic.lookup.argdep]/3 */
5749 if (!DECL_FUNCTION_MEMBER_P (fn)
5750 && !DECL_LOCAL_FUNCTION_P (fn))
5752 koenig_p = true;
5753 if (!any_type_dependent_arguments_p (args))
5754 postfix_expression
5755 = perform_koenig_lookup (postfix_expression, args,
5756 /*include_std=*/false,
5757 tf_warning_or_error);
5762 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5764 tree instance = TREE_OPERAND (postfix_expression, 0);
5765 tree fn = TREE_OPERAND (postfix_expression, 1);
5767 if (processing_template_decl
5768 && (type_dependent_expression_p (instance)
5769 || (!BASELINK_P (fn)
5770 && TREE_CODE (fn) != FIELD_DECL)
5771 || type_dependent_expression_p (fn)
5772 || any_type_dependent_arguments_p (args)))
5774 postfix_expression
5775 = build_nt_call_vec (postfix_expression, args);
5776 release_tree_vector (args);
5777 break;
5780 if (BASELINK_P (fn))
5782 postfix_expression
5783 = (build_new_method_call
5784 (instance, fn, &args, NULL_TREE,
5785 (idk == CP_ID_KIND_QUALIFIED
5786 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5787 : LOOKUP_NORMAL),
5788 /*fn_p=*/NULL,
5789 tf_warning_or_error));
5791 else
5792 postfix_expression
5793 = finish_call_expr (postfix_expression, &args,
5794 /*disallow_virtual=*/false,
5795 /*koenig_p=*/false,
5796 tf_warning_or_error);
5798 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5799 || TREE_CODE (postfix_expression) == MEMBER_REF
5800 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5801 postfix_expression = (build_offset_ref_call_from_tree
5802 (postfix_expression, &args,
5803 tf_warning_or_error));
5804 else if (idk == CP_ID_KIND_QUALIFIED)
5805 /* A call to a static class member, or a namespace-scope
5806 function. */
5807 postfix_expression
5808 = finish_call_expr (postfix_expression, &args,
5809 /*disallow_virtual=*/true,
5810 koenig_p,
5811 tf_warning_or_error);
5812 else
5813 /* All other function calls. */
5814 postfix_expression
5815 = finish_call_expr (postfix_expression, &args,
5816 /*disallow_virtual=*/false,
5817 koenig_p,
5818 tf_warning_or_error);
5820 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5821 idk = CP_ID_KIND_NONE;
5823 release_tree_vector (args);
5825 break;
5827 case CPP_DOT:
5828 case CPP_DEREF:
5829 /* postfix-expression . template [opt] id-expression
5830 postfix-expression . pseudo-destructor-name
5831 postfix-expression -> template [opt] id-expression
5832 postfix-expression -> pseudo-destructor-name */
5834 /* Consume the `.' or `->' operator. */
5835 cp_lexer_consume_token (parser->lexer);
5837 postfix_expression
5838 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5839 postfix_expression,
5840 false, &idk,
5841 token->location);
5843 is_member_access = true;
5844 break;
5846 case CPP_PLUS_PLUS:
5847 /* postfix-expression ++ */
5848 /* Consume the `++' token. */
5849 cp_lexer_consume_token (parser->lexer);
5850 /* Generate a representation for the complete expression. */
5851 postfix_expression
5852 = finish_increment_expr (postfix_expression,
5853 POSTINCREMENT_EXPR);
5854 /* Increments may not appear in constant-expressions. */
5855 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5856 postfix_expression = error_mark_node;
5857 idk = CP_ID_KIND_NONE;
5858 is_member_access = false;
5859 break;
5861 case CPP_MINUS_MINUS:
5862 /* postfix-expression -- */
5863 /* Consume the `--' token. */
5864 cp_lexer_consume_token (parser->lexer);
5865 /* Generate a representation for the complete expression. */
5866 postfix_expression
5867 = finish_increment_expr (postfix_expression,
5868 POSTDECREMENT_EXPR);
5869 /* Decrements may not appear in constant-expressions. */
5870 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5871 postfix_expression = error_mark_node;
5872 idk = CP_ID_KIND_NONE;
5873 is_member_access = false;
5874 break;
5876 default:
5877 if (pidk_return != NULL)
5878 * pidk_return = idk;
5879 if (member_access_only_p)
5880 return is_member_access? postfix_expression : error_mark_node;
5881 else
5882 return postfix_expression;
5886 /* We should never get here. */
5887 gcc_unreachable ();
5888 return error_mark_node;
5891 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5892 by cp_parser_builtin_offsetof. We're looking for
5894 postfix-expression [ expression ]
5895 postfix-expression [ braced-init-list ] (C++11)
5897 FOR_OFFSETOF is set if we're being called in that context, which
5898 changes how we deal with integer constant expressions. */
5900 static tree
5901 cp_parser_postfix_open_square_expression (cp_parser *parser,
5902 tree postfix_expression,
5903 bool for_offsetof)
5905 tree index;
5906 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5908 /* Consume the `[' token. */
5909 cp_lexer_consume_token (parser->lexer);
5911 /* Parse the index expression. */
5912 /* ??? For offsetof, there is a question of what to allow here. If
5913 offsetof is not being used in an integral constant expression context,
5914 then we *could* get the right answer by computing the value at runtime.
5915 If we are in an integral constant expression context, then we might
5916 could accept any constant expression; hard to say without analysis.
5917 Rather than open the barn door too wide right away, allow only integer
5918 constant expressions here. */
5919 if (for_offsetof)
5920 index = cp_parser_constant_expression (parser, false, NULL);
5921 else
5923 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5925 bool expr_nonconst_p;
5926 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5927 index = cp_parser_braced_list (parser, &expr_nonconst_p);
5929 else
5930 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5933 /* Look for the closing `]'. */
5934 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5936 /* Build the ARRAY_REF. */
5937 postfix_expression = grok_array_decl (loc, postfix_expression, index);
5939 /* When not doing offsetof, array references are not permitted in
5940 constant-expressions. */
5941 if (!for_offsetof
5942 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5943 postfix_expression = error_mark_node;
5945 return postfix_expression;
5948 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5949 by cp_parser_builtin_offsetof. We're looking for
5951 postfix-expression . template [opt] id-expression
5952 postfix-expression . pseudo-destructor-name
5953 postfix-expression -> template [opt] id-expression
5954 postfix-expression -> pseudo-destructor-name
5956 FOR_OFFSETOF is set if we're being called in that context. That sorta
5957 limits what of the above we'll actually accept, but nevermind.
5958 TOKEN_TYPE is the "." or "->" token, which will already have been
5959 removed from the stream. */
5961 static tree
5962 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5963 enum cpp_ttype token_type,
5964 tree postfix_expression,
5965 bool for_offsetof, cp_id_kind *idk,
5966 location_t location)
5968 tree name;
5969 bool dependent_p;
5970 bool pseudo_destructor_p;
5971 tree scope = NULL_TREE;
5973 /* If this is a `->' operator, dereference the pointer. */
5974 if (token_type == CPP_DEREF)
5975 postfix_expression = build_x_arrow (location, postfix_expression,
5976 tf_warning_or_error);
5977 /* Check to see whether or not the expression is type-dependent. */
5978 dependent_p = type_dependent_expression_p (postfix_expression);
5979 /* The identifier following the `->' or `.' is not qualified. */
5980 parser->scope = NULL_TREE;
5981 parser->qualifying_scope = NULL_TREE;
5982 parser->object_scope = NULL_TREE;
5983 *idk = CP_ID_KIND_NONE;
5985 /* Enter the scope corresponding to the type of the object
5986 given by the POSTFIX_EXPRESSION. */
5987 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5989 scope = TREE_TYPE (postfix_expression);
5990 /* According to the standard, no expression should ever have
5991 reference type. Unfortunately, we do not currently match
5992 the standard in this respect in that our internal representation
5993 of an expression may have reference type even when the standard
5994 says it does not. Therefore, we have to manually obtain the
5995 underlying type here. */
5996 scope = non_reference (scope);
5997 /* The type of the POSTFIX_EXPRESSION must be complete. */
5998 if (scope == unknown_type_node)
6000 error_at (location, "%qE does not have class type",
6001 postfix_expression);
6002 scope = NULL_TREE;
6004 /* Unlike the object expression in other contexts, *this is not
6005 required to be of complete type for purposes of class member
6006 access (5.2.5) outside the member function body. */
6007 else if (scope != current_class_ref
6008 && !(processing_template_decl && scope == current_class_type))
6009 scope = complete_type_or_else (scope, NULL_TREE);
6010 /* Let the name lookup machinery know that we are processing a
6011 class member access expression. */
6012 parser->context->object_type = scope;
6013 /* If something went wrong, we want to be able to discern that case,
6014 as opposed to the case where there was no SCOPE due to the type
6015 of expression being dependent. */
6016 if (!scope)
6017 scope = error_mark_node;
6018 /* If the SCOPE was erroneous, make the various semantic analysis
6019 functions exit quickly -- and without issuing additional error
6020 messages. */
6021 if (scope == error_mark_node)
6022 postfix_expression = error_mark_node;
6025 /* Assume this expression is not a pseudo-destructor access. */
6026 pseudo_destructor_p = false;
6028 /* If the SCOPE is a scalar type, then, if this is a valid program,
6029 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6030 is type dependent, it can be pseudo-destructor-name or something else.
6031 Try to parse it as pseudo-destructor-name first. */
6032 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6034 tree s;
6035 tree type;
6037 cp_parser_parse_tentatively (parser);
6038 /* Parse the pseudo-destructor-name. */
6039 s = NULL_TREE;
6040 cp_parser_pseudo_destructor_name (parser, &s, &type);
6041 if (dependent_p
6042 && (cp_parser_error_occurred (parser)
6043 || TREE_CODE (type) != TYPE_DECL
6044 || !SCALAR_TYPE_P (TREE_TYPE (type))))
6045 cp_parser_abort_tentative_parse (parser);
6046 else if (cp_parser_parse_definitely (parser))
6048 pseudo_destructor_p = true;
6049 postfix_expression
6050 = finish_pseudo_destructor_expr (postfix_expression,
6051 s, TREE_TYPE (type));
6055 if (!pseudo_destructor_p)
6057 /* If the SCOPE is not a scalar type, we are looking at an
6058 ordinary class member access expression, rather than a
6059 pseudo-destructor-name. */
6060 bool template_p;
6061 cp_token *token = cp_lexer_peek_token (parser->lexer);
6062 /* Parse the id-expression. */
6063 name = (cp_parser_id_expression
6064 (parser,
6065 cp_parser_optional_template_keyword (parser),
6066 /*check_dependency_p=*/true,
6067 &template_p,
6068 /*declarator_p=*/false,
6069 /*optional_p=*/false));
6070 /* In general, build a SCOPE_REF if the member name is qualified.
6071 However, if the name was not dependent and has already been
6072 resolved; there is no need to build the SCOPE_REF. For example;
6074 struct X { void f(); };
6075 template <typename T> void f(T* t) { t->X::f(); }
6077 Even though "t" is dependent, "X::f" is not and has been resolved
6078 to a BASELINK; there is no need to include scope information. */
6080 /* But we do need to remember that there was an explicit scope for
6081 virtual function calls. */
6082 if (parser->scope)
6083 *idk = CP_ID_KIND_QUALIFIED;
6085 /* If the name is a template-id that names a type, we will get a
6086 TYPE_DECL here. That is invalid code. */
6087 if (TREE_CODE (name) == TYPE_DECL)
6089 error_at (token->location, "invalid use of %qD", name);
6090 postfix_expression = error_mark_node;
6092 else
6094 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6096 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6098 error_at (token->location, "%<%D::%D%> is not a class member",
6099 parser->scope, name);
6100 postfix_expression = error_mark_node;
6102 else
6103 name = build_qualified_name (/*type=*/NULL_TREE,
6104 parser->scope,
6105 name,
6106 template_p);
6107 parser->scope = NULL_TREE;
6108 parser->qualifying_scope = NULL_TREE;
6109 parser->object_scope = NULL_TREE;
6111 if (parser->scope && name && BASELINK_P (name))
6112 adjust_result_of_qualified_name_lookup
6113 (name, parser->scope, scope);
6114 postfix_expression
6115 = finish_class_member_access_expr (postfix_expression, name,
6116 template_p,
6117 tf_warning_or_error);
6121 /* We no longer need to look up names in the scope of the object on
6122 the left-hand side of the `.' or `->' operator. */
6123 parser->context->object_type = NULL_TREE;
6125 /* Outside of offsetof, these operators may not appear in
6126 constant-expressions. */
6127 if (!for_offsetof
6128 && (cp_parser_non_integral_constant_expression
6129 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6130 postfix_expression = error_mark_node;
6132 return postfix_expression;
6135 /* Parse a parenthesized expression-list.
6137 expression-list:
6138 assignment-expression
6139 expression-list, assignment-expression
6141 attribute-list:
6142 expression-list
6143 identifier
6144 identifier, expression-list
6146 CAST_P is true if this expression is the target of a cast.
6148 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6149 argument pack.
6151 Returns a vector of trees. Each element is a representation of an
6152 assignment-expression. NULL is returned if the ( and or ) are
6153 missing. An empty, but allocated, vector is returned on no
6154 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6155 if we are parsing an attribute list for an attribute that wants a
6156 plain identifier argument, normal_attr for an attribute that wants
6157 an expression, or non_attr if we aren't parsing an attribute list. If
6158 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6159 not all of the expressions in the list were constant. */
6161 static vec<tree, va_gc> *
6162 cp_parser_parenthesized_expression_list (cp_parser* parser,
6163 int is_attribute_list,
6164 bool cast_p,
6165 bool allow_expansion_p,
6166 bool *non_constant_p)
6168 vec<tree, va_gc> *expression_list;
6169 bool fold_expr_p = is_attribute_list != non_attr;
6170 tree identifier = NULL_TREE;
6171 bool saved_greater_than_is_operator_p;
6173 /* Assume all the expressions will be constant. */
6174 if (non_constant_p)
6175 *non_constant_p = false;
6177 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6178 return NULL;
6180 expression_list = make_tree_vector ();
6182 /* Within a parenthesized expression, a `>' token is always
6183 the greater-than operator. */
6184 saved_greater_than_is_operator_p
6185 = parser->greater_than_is_operator_p;
6186 parser->greater_than_is_operator_p = true;
6188 /* Consume expressions until there are no more. */
6189 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6190 while (true)
6192 tree expr;
6194 /* At the beginning of attribute lists, check to see if the
6195 next token is an identifier. */
6196 if (is_attribute_list == id_attr
6197 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6199 cp_token *token;
6201 /* Consume the identifier. */
6202 token = cp_lexer_consume_token (parser->lexer);
6203 /* Save the identifier. */
6204 identifier = token->u.value;
6206 else
6208 bool expr_non_constant_p;
6210 /* Parse the next assignment-expression. */
6211 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6213 /* A braced-init-list. */
6214 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6215 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6216 if (non_constant_p && expr_non_constant_p)
6217 *non_constant_p = true;
6219 else if (non_constant_p)
6221 expr = (cp_parser_constant_expression
6222 (parser, /*allow_non_constant_p=*/true,
6223 &expr_non_constant_p));
6224 if (expr_non_constant_p)
6225 *non_constant_p = true;
6227 else
6228 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6230 if (fold_expr_p)
6231 expr = fold_non_dependent_expr (expr);
6233 /* If we have an ellipsis, then this is an expression
6234 expansion. */
6235 if (allow_expansion_p
6236 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6238 /* Consume the `...'. */
6239 cp_lexer_consume_token (parser->lexer);
6241 /* Build the argument pack. */
6242 expr = make_pack_expansion (expr);
6245 /* Add it to the list. We add error_mark_node
6246 expressions to the list, so that we can still tell if
6247 the correct form for a parenthesized expression-list
6248 is found. That gives better errors. */
6249 vec_safe_push (expression_list, expr);
6251 if (expr == error_mark_node)
6252 goto skip_comma;
6255 /* After the first item, attribute lists look the same as
6256 expression lists. */
6257 is_attribute_list = non_attr;
6259 get_comma:;
6260 /* If the next token isn't a `,', then we are done. */
6261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6262 break;
6264 /* Otherwise, consume the `,' and keep going. */
6265 cp_lexer_consume_token (parser->lexer);
6268 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6270 int ending;
6272 skip_comma:;
6273 /* We try and resync to an unnested comma, as that will give the
6274 user better diagnostics. */
6275 ending = cp_parser_skip_to_closing_parenthesis (parser,
6276 /*recovering=*/true,
6277 /*or_comma=*/true,
6278 /*consume_paren=*/true);
6279 if (ending < 0)
6280 goto get_comma;
6281 if (!ending)
6283 parser->greater_than_is_operator_p
6284 = saved_greater_than_is_operator_p;
6285 return NULL;
6289 parser->greater_than_is_operator_p
6290 = saved_greater_than_is_operator_p;
6292 if (identifier)
6293 vec_safe_insert (expression_list, 0, identifier);
6295 return expression_list;
6298 /* Parse a pseudo-destructor-name.
6300 pseudo-destructor-name:
6301 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6302 :: [opt] nested-name-specifier template template-id :: ~ type-name
6303 :: [opt] nested-name-specifier [opt] ~ type-name
6305 If either of the first two productions is used, sets *SCOPE to the
6306 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6307 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6308 or ERROR_MARK_NODE if the parse fails. */
6310 static void
6311 cp_parser_pseudo_destructor_name (cp_parser* parser,
6312 tree* scope,
6313 tree* type)
6315 bool nested_name_specifier_p;
6317 /* Assume that things will not work out. */
6318 *type = error_mark_node;
6320 /* Look for the optional `::' operator. */
6321 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6322 /* Look for the optional nested-name-specifier. */
6323 nested_name_specifier_p
6324 = (cp_parser_nested_name_specifier_opt (parser,
6325 /*typename_keyword_p=*/false,
6326 /*check_dependency_p=*/true,
6327 /*type_p=*/false,
6328 /*is_declaration=*/false)
6329 != NULL_TREE);
6330 /* Now, if we saw a nested-name-specifier, we might be doing the
6331 second production. */
6332 if (nested_name_specifier_p
6333 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6335 /* Consume the `template' keyword. */
6336 cp_lexer_consume_token (parser->lexer);
6337 /* Parse the template-id. */
6338 cp_parser_template_id (parser,
6339 /*template_keyword_p=*/true,
6340 /*check_dependency_p=*/false,
6341 class_type,
6342 /*is_declaration=*/true);
6343 /* Look for the `::' token. */
6344 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6346 /* If the next token is not a `~', then there might be some
6347 additional qualification. */
6348 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6350 /* At this point, we're looking for "type-name :: ~". The type-name
6351 must not be a class-name, since this is a pseudo-destructor. So,
6352 it must be either an enum-name, or a typedef-name -- both of which
6353 are just identifiers. So, we peek ahead to check that the "::"
6354 and "~" tokens are present; if they are not, then we can avoid
6355 calling type_name. */
6356 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6357 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6358 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6360 cp_parser_error (parser, "non-scalar type");
6361 return;
6364 /* Look for the type-name. */
6365 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6366 if (*scope == error_mark_node)
6367 return;
6369 /* Look for the `::' token. */
6370 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6372 else
6373 *scope = NULL_TREE;
6375 /* Look for the `~'. */
6376 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6378 /* Once we see the ~, this has to be a pseudo-destructor. */
6379 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6380 cp_parser_commit_to_tentative_parse (parser);
6382 /* Look for the type-name again. We are not responsible for
6383 checking that it matches the first type-name. */
6384 *type = cp_parser_nonclass_name (parser);
6387 /* Parse a unary-expression.
6389 unary-expression:
6390 postfix-expression
6391 ++ cast-expression
6392 -- cast-expression
6393 unary-operator cast-expression
6394 sizeof unary-expression
6395 sizeof ( type-id )
6396 alignof ( type-id ) [C++0x]
6397 new-expression
6398 delete-expression
6400 GNU Extensions:
6402 unary-expression:
6403 __extension__ cast-expression
6404 __alignof__ unary-expression
6405 __alignof__ ( type-id )
6406 alignof unary-expression [C++0x]
6407 __real__ cast-expression
6408 __imag__ cast-expression
6409 && identifier
6411 ADDRESS_P is true iff the unary-expression is appearing as the
6412 operand of the `&' operator. CAST_P is true if this expression is
6413 the target of a cast.
6415 Returns a representation of the expression. */
6417 static tree
6418 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6419 cp_id_kind * pidk)
6421 cp_token *token;
6422 enum tree_code unary_operator;
6424 /* Peek at the next token. */
6425 token = cp_lexer_peek_token (parser->lexer);
6426 /* Some keywords give away the kind of expression. */
6427 if (token->type == CPP_KEYWORD)
6429 enum rid keyword = token->keyword;
6431 switch (keyword)
6433 case RID_ALIGNOF:
6434 case RID_SIZEOF:
6436 tree operand, ret;
6437 enum tree_code op;
6438 location_t first_loc;
6440 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6441 /* Consume the token. */
6442 cp_lexer_consume_token (parser->lexer);
6443 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6444 /* Parse the operand. */
6445 operand = cp_parser_sizeof_operand (parser, keyword);
6447 if (TYPE_P (operand))
6448 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6449 else
6451 /* ISO C++ defines alignof only with types, not with
6452 expressions. So pedwarn if alignof is used with a non-
6453 type expression. However, __alignof__ is ok. */
6454 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6455 pedwarn (token->location, OPT_Wpedantic,
6456 "ISO C++ does not allow %<alignof%> "
6457 "with a non-type");
6459 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6461 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6462 SIZEOF_EXPR with the original operand. */
6463 if (op == SIZEOF_EXPR && ret != error_mark_node)
6465 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6467 if (!processing_template_decl && TYPE_P (operand))
6469 ret = build_min (SIZEOF_EXPR, size_type_node,
6470 build1 (NOP_EXPR, operand,
6471 error_mark_node));
6472 SIZEOF_EXPR_TYPE_P (ret) = 1;
6474 else
6475 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6476 TREE_SIDE_EFFECTS (ret) = 0;
6477 TREE_READONLY (ret) = 1;
6479 SET_EXPR_LOCATION (ret, first_loc);
6481 return ret;
6484 case RID_NEW:
6485 return cp_parser_new_expression (parser);
6487 case RID_DELETE:
6488 return cp_parser_delete_expression (parser);
6490 case RID_EXTENSION:
6492 /* The saved value of the PEDANTIC flag. */
6493 int saved_pedantic;
6494 tree expr;
6496 /* Save away the PEDANTIC flag. */
6497 cp_parser_extension_opt (parser, &saved_pedantic);
6498 /* Parse the cast-expression. */
6499 expr = cp_parser_simple_cast_expression (parser);
6500 /* Restore the PEDANTIC flag. */
6501 pedantic = saved_pedantic;
6503 return expr;
6506 case RID_REALPART:
6507 case RID_IMAGPART:
6509 tree expression;
6511 /* Consume the `__real__' or `__imag__' token. */
6512 cp_lexer_consume_token (parser->lexer);
6513 /* Parse the cast-expression. */
6514 expression = cp_parser_simple_cast_expression (parser);
6515 /* Create the complete representation. */
6516 return build_x_unary_op (token->location,
6517 (keyword == RID_REALPART
6518 ? REALPART_EXPR : IMAGPART_EXPR),
6519 expression,
6520 tf_warning_or_error);
6522 break;
6524 case RID_TRANSACTION_ATOMIC:
6525 case RID_TRANSACTION_RELAXED:
6526 return cp_parser_transaction_expression (parser, keyword);
6528 case RID_NOEXCEPT:
6530 tree expr;
6531 const char *saved_message;
6532 bool saved_integral_constant_expression_p;
6533 bool saved_non_integral_constant_expression_p;
6534 bool saved_greater_than_is_operator_p;
6536 cp_lexer_consume_token (parser->lexer);
6537 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6539 saved_message = parser->type_definition_forbidden_message;
6540 parser->type_definition_forbidden_message
6541 = G_("types may not be defined in %<noexcept%> expressions");
6543 saved_integral_constant_expression_p
6544 = parser->integral_constant_expression_p;
6545 saved_non_integral_constant_expression_p
6546 = parser->non_integral_constant_expression_p;
6547 parser->integral_constant_expression_p = false;
6549 saved_greater_than_is_operator_p
6550 = parser->greater_than_is_operator_p;
6551 parser->greater_than_is_operator_p = true;
6553 ++cp_unevaluated_operand;
6554 ++c_inhibit_evaluation_warnings;
6555 expr = cp_parser_expression (parser, false, NULL);
6556 --c_inhibit_evaluation_warnings;
6557 --cp_unevaluated_operand;
6559 parser->greater_than_is_operator_p
6560 = saved_greater_than_is_operator_p;
6562 parser->integral_constant_expression_p
6563 = saved_integral_constant_expression_p;
6564 parser->non_integral_constant_expression_p
6565 = saved_non_integral_constant_expression_p;
6567 parser->type_definition_forbidden_message = saved_message;
6569 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6570 return finish_noexcept_expr (expr, tf_warning_or_error);
6573 default:
6574 break;
6578 /* Look for the `:: new' and `:: delete', which also signal the
6579 beginning of a new-expression, or delete-expression,
6580 respectively. If the next token is `::', then it might be one of
6581 these. */
6582 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6584 enum rid keyword;
6586 /* See if the token after the `::' is one of the keywords in
6587 which we're interested. */
6588 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6589 /* If it's `new', we have a new-expression. */
6590 if (keyword == RID_NEW)
6591 return cp_parser_new_expression (parser);
6592 /* Similarly, for `delete'. */
6593 else if (keyword == RID_DELETE)
6594 return cp_parser_delete_expression (parser);
6597 /* Look for a unary operator. */
6598 unary_operator = cp_parser_unary_operator (token);
6599 /* The `++' and `--' operators can be handled similarly, even though
6600 they are not technically unary-operators in the grammar. */
6601 if (unary_operator == ERROR_MARK)
6603 if (token->type == CPP_PLUS_PLUS)
6604 unary_operator = PREINCREMENT_EXPR;
6605 else if (token->type == CPP_MINUS_MINUS)
6606 unary_operator = PREDECREMENT_EXPR;
6607 /* Handle the GNU address-of-label extension. */
6608 else if (cp_parser_allow_gnu_extensions_p (parser)
6609 && token->type == CPP_AND_AND)
6611 tree identifier;
6612 tree expression;
6613 location_t loc = token->location;
6615 /* Consume the '&&' token. */
6616 cp_lexer_consume_token (parser->lexer);
6617 /* Look for the identifier. */
6618 identifier = cp_parser_identifier (parser);
6619 /* Create an expression representing the address. */
6620 expression = finish_label_address_expr (identifier, loc);
6621 if (cp_parser_non_integral_constant_expression (parser,
6622 NIC_ADDR_LABEL))
6623 expression = error_mark_node;
6624 return expression;
6627 if (unary_operator != ERROR_MARK)
6629 tree cast_expression;
6630 tree expression = error_mark_node;
6631 non_integral_constant non_constant_p = NIC_NONE;
6632 location_t loc = token->location;
6634 /* Consume the operator token. */
6635 token = cp_lexer_consume_token (parser->lexer);
6636 /* Parse the cast-expression. */
6637 cast_expression
6638 = cp_parser_cast_expression (parser,
6639 unary_operator == ADDR_EXPR,
6640 /*cast_p=*/false, pidk);
6641 /* Now, build an appropriate representation. */
6642 switch (unary_operator)
6644 case INDIRECT_REF:
6645 non_constant_p = NIC_STAR;
6646 expression = build_x_indirect_ref (loc, cast_expression,
6647 RO_UNARY_STAR,
6648 tf_warning_or_error);
6649 break;
6651 case ADDR_EXPR:
6652 non_constant_p = NIC_ADDR;
6653 /* Fall through. */
6654 case BIT_NOT_EXPR:
6655 expression = build_x_unary_op (loc, unary_operator,
6656 cast_expression,
6657 tf_warning_or_error);
6658 break;
6660 case PREINCREMENT_EXPR:
6661 case PREDECREMENT_EXPR:
6662 non_constant_p = unary_operator == PREINCREMENT_EXPR
6663 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6664 /* Fall through. */
6665 case UNARY_PLUS_EXPR:
6666 case NEGATE_EXPR:
6667 case TRUTH_NOT_EXPR:
6668 expression = finish_unary_op_expr (loc, unary_operator,
6669 cast_expression);
6670 break;
6672 default:
6673 gcc_unreachable ();
6676 if (non_constant_p != NIC_NONE
6677 && cp_parser_non_integral_constant_expression (parser,
6678 non_constant_p))
6679 expression = error_mark_node;
6681 return expression;
6684 return cp_parser_postfix_expression (parser, address_p, cast_p,
6685 /*member_access_only_p=*/false,
6686 pidk);
6689 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6690 unary-operator, the corresponding tree code is returned. */
6692 static enum tree_code
6693 cp_parser_unary_operator (cp_token* token)
6695 switch (token->type)
6697 case CPP_MULT:
6698 return INDIRECT_REF;
6700 case CPP_AND:
6701 return ADDR_EXPR;
6703 case CPP_PLUS:
6704 return UNARY_PLUS_EXPR;
6706 case CPP_MINUS:
6707 return NEGATE_EXPR;
6709 case CPP_NOT:
6710 return TRUTH_NOT_EXPR;
6712 case CPP_COMPL:
6713 return BIT_NOT_EXPR;
6715 default:
6716 return ERROR_MARK;
6720 /* Parse a new-expression.
6722 new-expression:
6723 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6724 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6726 Returns a representation of the expression. */
6728 static tree
6729 cp_parser_new_expression (cp_parser* parser)
6731 bool global_scope_p;
6732 vec<tree, va_gc> *placement;
6733 tree type;
6734 vec<tree, va_gc> *initializer;
6735 tree nelts = NULL_TREE;
6736 tree ret;
6738 /* Look for the optional `::' operator. */
6739 global_scope_p
6740 = (cp_parser_global_scope_opt (parser,
6741 /*current_scope_valid_p=*/false)
6742 != NULL_TREE);
6743 /* Look for the `new' operator. */
6744 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6745 /* There's no easy way to tell a new-placement from the
6746 `( type-id )' construct. */
6747 cp_parser_parse_tentatively (parser);
6748 /* Look for a new-placement. */
6749 placement = cp_parser_new_placement (parser);
6750 /* If that didn't work out, there's no new-placement. */
6751 if (!cp_parser_parse_definitely (parser))
6753 if (placement != NULL)
6754 release_tree_vector (placement);
6755 placement = NULL;
6758 /* If the next token is a `(', then we have a parenthesized
6759 type-id. */
6760 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6762 cp_token *token;
6763 const char *saved_message = parser->type_definition_forbidden_message;
6765 /* Consume the `('. */
6766 cp_lexer_consume_token (parser->lexer);
6768 /* Parse the type-id. */
6769 parser->type_definition_forbidden_message
6770 = G_("types may not be defined in a new-expression");
6771 type = cp_parser_type_id (parser);
6772 parser->type_definition_forbidden_message = saved_message;
6774 /* Look for the closing `)'. */
6775 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6776 token = cp_lexer_peek_token (parser->lexer);
6777 /* There should not be a direct-new-declarator in this production,
6778 but GCC used to allowed this, so we check and emit a sensible error
6779 message for this case. */
6780 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6782 error_at (token->location,
6783 "array bound forbidden after parenthesized type-id");
6784 inform (token->location,
6785 "try removing the parentheses around the type-id");
6786 cp_parser_direct_new_declarator (parser);
6789 /* Otherwise, there must be a new-type-id. */
6790 else
6791 type = cp_parser_new_type_id (parser, &nelts);
6793 /* If the next token is a `(' or '{', then we have a new-initializer. */
6794 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6795 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6796 initializer = cp_parser_new_initializer (parser);
6797 else
6798 initializer = NULL;
6800 /* A new-expression may not appear in an integral constant
6801 expression. */
6802 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6803 ret = error_mark_node;
6804 else
6806 /* Create a representation of the new-expression. */
6807 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6808 tf_warning_or_error);
6811 if (placement != NULL)
6812 release_tree_vector (placement);
6813 if (initializer != NULL)
6814 release_tree_vector (initializer);
6816 return ret;
6819 /* Parse a new-placement.
6821 new-placement:
6822 ( expression-list )
6824 Returns the same representation as for an expression-list. */
6826 static vec<tree, va_gc> *
6827 cp_parser_new_placement (cp_parser* parser)
6829 vec<tree, va_gc> *expression_list;
6831 /* Parse the expression-list. */
6832 expression_list = (cp_parser_parenthesized_expression_list
6833 (parser, non_attr, /*cast_p=*/false,
6834 /*allow_expansion_p=*/true,
6835 /*non_constant_p=*/NULL));
6837 return expression_list;
6840 /* Parse a new-type-id.
6842 new-type-id:
6843 type-specifier-seq new-declarator [opt]
6845 Returns the TYPE allocated. If the new-type-id indicates an array
6846 type, *NELTS is set to the number of elements in the last array
6847 bound; the TYPE will not include the last array bound. */
6849 static tree
6850 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6852 cp_decl_specifier_seq type_specifier_seq;
6853 cp_declarator *new_declarator;
6854 cp_declarator *declarator;
6855 cp_declarator *outer_declarator;
6856 const char *saved_message;
6858 /* The type-specifier sequence must not contain type definitions.
6859 (It cannot contain declarations of new types either, but if they
6860 are not definitions we will catch that because they are not
6861 complete.) */
6862 saved_message = parser->type_definition_forbidden_message;
6863 parser->type_definition_forbidden_message
6864 = G_("types may not be defined in a new-type-id");
6865 /* Parse the type-specifier-seq. */
6866 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6867 /*is_trailing_return=*/false,
6868 &type_specifier_seq);
6869 /* Restore the old message. */
6870 parser->type_definition_forbidden_message = saved_message;
6872 if (type_specifier_seq.type == error_mark_node)
6873 return error_mark_node;
6875 /* Parse the new-declarator. */
6876 new_declarator = cp_parser_new_declarator_opt (parser);
6878 /* Determine the number of elements in the last array dimension, if
6879 any. */
6880 *nelts = NULL_TREE;
6881 /* Skip down to the last array dimension. */
6882 declarator = new_declarator;
6883 outer_declarator = NULL;
6884 while (declarator && (declarator->kind == cdk_pointer
6885 || declarator->kind == cdk_ptrmem))
6887 outer_declarator = declarator;
6888 declarator = declarator->declarator;
6890 while (declarator
6891 && declarator->kind == cdk_array
6892 && declarator->declarator
6893 && declarator->declarator->kind == cdk_array)
6895 outer_declarator = declarator;
6896 declarator = declarator->declarator;
6899 if (declarator && declarator->kind == cdk_array)
6901 *nelts = declarator->u.array.bounds;
6902 if (*nelts == error_mark_node)
6903 *nelts = integer_one_node;
6905 if (outer_declarator)
6906 outer_declarator->declarator = declarator->declarator;
6907 else
6908 new_declarator = NULL;
6911 return groktypename (&type_specifier_seq, new_declarator, false);
6914 /* Parse an (optional) new-declarator.
6916 new-declarator:
6917 ptr-operator new-declarator [opt]
6918 direct-new-declarator
6920 Returns the declarator. */
6922 static cp_declarator *
6923 cp_parser_new_declarator_opt (cp_parser* parser)
6925 enum tree_code code;
6926 tree type, std_attributes = NULL_TREE;
6927 cp_cv_quals cv_quals;
6929 /* We don't know if there's a ptr-operator next, or not. */
6930 cp_parser_parse_tentatively (parser);
6931 /* Look for a ptr-operator. */
6932 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
6933 /* If that worked, look for more new-declarators. */
6934 if (cp_parser_parse_definitely (parser))
6936 cp_declarator *declarator;
6938 /* Parse another optional declarator. */
6939 declarator = cp_parser_new_declarator_opt (parser);
6941 declarator = cp_parser_make_indirect_declarator
6942 (code, type, cv_quals, declarator, std_attributes);
6944 return declarator;
6947 /* If the next token is a `[', there is a direct-new-declarator. */
6948 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6949 return cp_parser_direct_new_declarator (parser);
6951 return NULL;
6954 /* Parse a direct-new-declarator.
6956 direct-new-declarator:
6957 [ expression ]
6958 direct-new-declarator [constant-expression]
6962 static cp_declarator *
6963 cp_parser_direct_new_declarator (cp_parser* parser)
6965 cp_declarator *declarator = NULL;
6967 while (true)
6969 tree expression;
6970 cp_token *token;
6972 /* Look for the opening `['. */
6973 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6975 token = cp_lexer_peek_token (parser->lexer);
6976 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6977 /* The standard requires that the expression have integral
6978 type. DR 74 adds enumeration types. We believe that the
6979 real intent is that these expressions be handled like the
6980 expression in a `switch' condition, which also allows
6981 classes with a single conversion to integral or
6982 enumeration type. */
6983 if (!processing_template_decl)
6985 expression
6986 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6987 expression,
6988 /*complain=*/true);
6989 if (!expression)
6991 error_at (token->location,
6992 "expression in new-declarator must have integral "
6993 "or enumeration type");
6994 expression = error_mark_node;
6998 /* Look for the closing `]'. */
6999 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7001 /* Add this bound to the declarator. */
7002 declarator = make_array_declarator (declarator, expression);
7004 /* If the next token is not a `[', then there are no more
7005 bounds. */
7006 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7007 break;
7010 return declarator;
7013 /* Parse a new-initializer.
7015 new-initializer:
7016 ( expression-list [opt] )
7017 braced-init-list
7019 Returns a representation of the expression-list. */
7021 static vec<tree, va_gc> *
7022 cp_parser_new_initializer (cp_parser* parser)
7024 vec<tree, va_gc> *expression_list;
7026 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7028 tree t;
7029 bool expr_non_constant_p;
7030 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7031 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7032 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7033 expression_list = make_tree_vector_single (t);
7035 else
7036 expression_list = (cp_parser_parenthesized_expression_list
7037 (parser, non_attr, /*cast_p=*/false,
7038 /*allow_expansion_p=*/true,
7039 /*non_constant_p=*/NULL));
7041 return expression_list;
7044 /* Parse a delete-expression.
7046 delete-expression:
7047 :: [opt] delete cast-expression
7048 :: [opt] delete [ ] cast-expression
7050 Returns a representation of the expression. */
7052 static tree
7053 cp_parser_delete_expression (cp_parser* parser)
7055 bool global_scope_p;
7056 bool array_p;
7057 tree expression;
7059 /* Look for the optional `::' operator. */
7060 global_scope_p
7061 = (cp_parser_global_scope_opt (parser,
7062 /*current_scope_valid_p=*/false)
7063 != NULL_TREE);
7064 /* Look for the `delete' keyword. */
7065 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7066 /* See if the array syntax is in use. */
7067 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7069 /* Consume the `[' token. */
7070 cp_lexer_consume_token (parser->lexer);
7071 /* Look for the `]' token. */
7072 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7073 /* Remember that this is the `[]' construct. */
7074 array_p = true;
7076 else
7077 array_p = false;
7079 /* Parse the cast-expression. */
7080 expression = cp_parser_simple_cast_expression (parser);
7082 /* A delete-expression may not appear in an integral constant
7083 expression. */
7084 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7085 return error_mark_node;
7087 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7088 tf_warning_or_error);
7091 /* Returns true if TOKEN may start a cast-expression and false
7092 otherwise. */
7094 static bool
7095 cp_parser_token_starts_cast_expression (cp_token *token)
7097 switch (token->type)
7099 case CPP_COMMA:
7100 case CPP_SEMICOLON:
7101 case CPP_QUERY:
7102 case CPP_COLON:
7103 case CPP_CLOSE_SQUARE:
7104 case CPP_CLOSE_PAREN:
7105 case CPP_CLOSE_BRACE:
7106 case CPP_DOT:
7107 case CPP_DOT_STAR:
7108 case CPP_DEREF:
7109 case CPP_DEREF_STAR:
7110 case CPP_DIV:
7111 case CPP_MOD:
7112 case CPP_LSHIFT:
7113 case CPP_RSHIFT:
7114 case CPP_LESS:
7115 case CPP_GREATER:
7116 case CPP_LESS_EQ:
7117 case CPP_GREATER_EQ:
7118 case CPP_EQ_EQ:
7119 case CPP_NOT_EQ:
7120 case CPP_EQ:
7121 case CPP_MULT_EQ:
7122 case CPP_DIV_EQ:
7123 case CPP_MOD_EQ:
7124 case CPP_PLUS_EQ:
7125 case CPP_MINUS_EQ:
7126 case CPP_RSHIFT_EQ:
7127 case CPP_LSHIFT_EQ:
7128 case CPP_AND_EQ:
7129 case CPP_XOR_EQ:
7130 case CPP_OR_EQ:
7131 case CPP_XOR:
7132 case CPP_OR:
7133 case CPP_OR_OR:
7134 case CPP_EOF:
7135 return false;
7137 /* '[' may start a primary-expression in obj-c++. */
7138 case CPP_OPEN_SQUARE:
7139 return c_dialect_objc ();
7141 default:
7142 return true;
7146 /* Parse a cast-expression.
7148 cast-expression:
7149 unary-expression
7150 ( type-id ) cast-expression
7152 ADDRESS_P is true iff the unary-expression is appearing as the
7153 operand of the `&' operator. CAST_P is true if this expression is
7154 the target of a cast.
7156 Returns a representation of the expression. */
7158 static tree
7159 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7160 cp_id_kind * pidk)
7162 /* If it's a `(', then we might be looking at a cast. */
7163 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7165 tree type = NULL_TREE;
7166 tree expr = NULL_TREE;
7167 bool compound_literal_p;
7168 const char *saved_message;
7170 /* There's no way to know yet whether or not this is a cast.
7171 For example, `(int (3))' is a unary-expression, while `(int)
7172 3' is a cast. So, we resort to parsing tentatively. */
7173 cp_parser_parse_tentatively (parser);
7174 /* Types may not be defined in a cast. */
7175 saved_message = parser->type_definition_forbidden_message;
7176 parser->type_definition_forbidden_message
7177 = G_("types may not be defined in casts");
7178 /* Consume the `('. */
7179 cp_lexer_consume_token (parser->lexer);
7180 /* A very tricky bit is that `(struct S) { 3 }' is a
7181 compound-literal (which we permit in C++ as an extension).
7182 But, that construct is not a cast-expression -- it is a
7183 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7184 is legal; if the compound-literal were a cast-expression,
7185 you'd need an extra set of parentheses.) But, if we parse
7186 the type-id, and it happens to be a class-specifier, then we
7187 will commit to the parse at that point, because we cannot
7188 undo the action that is done when creating a new class. So,
7189 then we cannot back up and do a postfix-expression.
7191 Therefore, we scan ahead to the closing `)', and check to see
7192 if the token after the `)' is a `{'. If so, we are not
7193 looking at a cast-expression.
7195 Save tokens so that we can put them back. */
7196 cp_lexer_save_tokens (parser->lexer);
7197 /* Skip tokens until the next token is a closing parenthesis.
7198 If we find the closing `)', and the next token is a `{', then
7199 we are looking at a compound-literal. */
7200 compound_literal_p
7201 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7202 /*consume_paren=*/true)
7203 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7204 /* Roll back the tokens we skipped. */
7205 cp_lexer_rollback_tokens (parser->lexer);
7206 /* If we were looking at a compound-literal, simulate an error
7207 so that the call to cp_parser_parse_definitely below will
7208 fail. */
7209 if (compound_literal_p)
7210 cp_parser_simulate_error (parser);
7211 else
7213 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7214 parser->in_type_id_in_expr_p = true;
7215 /* Look for the type-id. */
7216 type = cp_parser_type_id (parser);
7217 /* Look for the closing `)'. */
7218 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7219 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7222 /* Restore the saved message. */
7223 parser->type_definition_forbidden_message = saved_message;
7225 /* At this point this can only be either a cast or a
7226 parenthesized ctor such as `(T ())' that looks like a cast to
7227 function returning T. */
7228 if (!cp_parser_error_occurred (parser)
7229 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7230 (parser->lexer)))
7232 cp_parser_parse_definitely (parser);
7233 expr = cp_parser_cast_expression (parser,
7234 /*address_p=*/false,
7235 /*cast_p=*/true, pidk);
7237 /* Warn about old-style casts, if so requested. */
7238 if (warn_old_style_cast
7239 && !in_system_header
7240 && !VOID_TYPE_P (type)
7241 && current_lang_name != lang_name_c)
7242 warning (OPT_Wold_style_cast, "use of old-style cast");
7244 /* Only type conversions to integral or enumeration types
7245 can be used in constant-expressions. */
7246 if (!cast_valid_in_integral_constant_expression_p (type)
7247 && cp_parser_non_integral_constant_expression (parser,
7248 NIC_CAST))
7249 return error_mark_node;
7251 /* Perform the cast. */
7252 expr = build_c_cast (input_location, type, expr);
7253 return expr;
7255 else
7256 cp_parser_abort_tentative_parse (parser);
7259 /* If we get here, then it's not a cast, so it must be a
7260 unary-expression. */
7261 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7264 /* Parse a binary expression of the general form:
7266 pm-expression:
7267 cast-expression
7268 pm-expression .* cast-expression
7269 pm-expression ->* cast-expression
7271 multiplicative-expression:
7272 pm-expression
7273 multiplicative-expression * pm-expression
7274 multiplicative-expression / pm-expression
7275 multiplicative-expression % pm-expression
7277 additive-expression:
7278 multiplicative-expression
7279 additive-expression + multiplicative-expression
7280 additive-expression - multiplicative-expression
7282 shift-expression:
7283 additive-expression
7284 shift-expression << additive-expression
7285 shift-expression >> additive-expression
7287 relational-expression:
7288 shift-expression
7289 relational-expression < shift-expression
7290 relational-expression > shift-expression
7291 relational-expression <= shift-expression
7292 relational-expression >= shift-expression
7294 GNU Extension:
7296 relational-expression:
7297 relational-expression <? shift-expression
7298 relational-expression >? shift-expression
7300 equality-expression:
7301 relational-expression
7302 equality-expression == relational-expression
7303 equality-expression != relational-expression
7305 and-expression:
7306 equality-expression
7307 and-expression & equality-expression
7309 exclusive-or-expression:
7310 and-expression
7311 exclusive-or-expression ^ and-expression
7313 inclusive-or-expression:
7314 exclusive-or-expression
7315 inclusive-or-expression | exclusive-or-expression
7317 logical-and-expression:
7318 inclusive-or-expression
7319 logical-and-expression && inclusive-or-expression
7321 logical-or-expression:
7322 logical-and-expression
7323 logical-or-expression || logical-and-expression
7325 All these are implemented with a single function like:
7327 binary-expression:
7328 simple-cast-expression
7329 binary-expression <token> binary-expression
7331 CAST_P is true if this expression is the target of a cast.
7333 The binops_by_token map is used to get the tree codes for each <token> type.
7334 binary-expressions are associated according to a precedence table. */
7336 #define TOKEN_PRECEDENCE(token) \
7337 (((token->type == CPP_GREATER \
7338 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7339 && !parser->greater_than_is_operator_p) \
7340 ? PREC_NOT_OPERATOR \
7341 : binops_by_token[token->type].prec)
7343 static tree
7344 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7345 bool no_toplevel_fold_p,
7346 enum cp_parser_prec prec,
7347 cp_id_kind * pidk)
7349 cp_parser_expression_stack stack;
7350 cp_parser_expression_stack_entry *sp = &stack[0];
7351 cp_parser_expression_stack_entry current;
7352 tree rhs;
7353 cp_token *token;
7354 enum tree_code rhs_type;
7355 enum cp_parser_prec new_prec, lookahead_prec;
7356 tree overload;
7358 /* Parse the first expression. */
7359 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7360 cast_p, pidk);
7361 current.lhs_type = ERROR_MARK;
7362 current.prec = prec;
7364 if (cp_parser_error_occurred (parser))
7365 return error_mark_node;
7367 for (;;)
7369 /* Get an operator token. */
7370 token = cp_lexer_peek_token (parser->lexer);
7372 if (warn_cxx0x_compat
7373 && token->type == CPP_RSHIFT
7374 && !parser->greater_than_is_operator_p)
7376 if (warning_at (token->location, OPT_Wc__0x_compat,
7377 "%<>>%> operator is treated"
7378 " as two right angle brackets in C++11"))
7379 inform (token->location,
7380 "suggest parentheses around %<>>%> expression");
7383 new_prec = TOKEN_PRECEDENCE (token);
7385 /* Popping an entry off the stack means we completed a subexpression:
7386 - either we found a token which is not an operator (`>' where it is not
7387 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7388 will happen repeatedly;
7389 - or, we found an operator which has lower priority. This is the case
7390 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7391 parsing `3 * 4'. */
7392 if (new_prec <= current.prec)
7394 if (sp == stack)
7395 break;
7396 else
7397 goto pop;
7400 get_rhs:
7401 current.tree_type = binops_by_token[token->type].tree_type;
7402 current.loc = token->location;
7404 /* We used the operator token. */
7405 cp_lexer_consume_token (parser->lexer);
7407 /* For "false && x" or "true || x", x will never be executed;
7408 disable warnings while evaluating it. */
7409 if (current.tree_type == TRUTH_ANDIF_EXPR)
7410 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7411 else if (current.tree_type == TRUTH_ORIF_EXPR)
7412 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7414 /* Extract another operand. It may be the RHS of this expression
7415 or the LHS of a new, higher priority expression. */
7416 rhs = cp_parser_simple_cast_expression (parser);
7417 rhs_type = ERROR_MARK;
7419 /* Get another operator token. Look up its precedence to avoid
7420 building a useless (immediately popped) stack entry for common
7421 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7422 token = cp_lexer_peek_token (parser->lexer);
7423 lookahead_prec = TOKEN_PRECEDENCE (token);
7424 if (lookahead_prec > new_prec)
7426 /* ... and prepare to parse the RHS of the new, higher priority
7427 expression. Since precedence levels on the stack are
7428 monotonically increasing, we do not have to care about
7429 stack overflows. */
7430 *sp = current;
7431 ++sp;
7432 current.lhs = rhs;
7433 current.lhs_type = rhs_type;
7434 current.prec = new_prec;
7435 new_prec = lookahead_prec;
7436 goto get_rhs;
7438 pop:
7439 lookahead_prec = new_prec;
7440 /* If the stack is not empty, we have parsed into LHS the right side
7441 (`4' in the example above) of an expression we had suspended.
7442 We can use the information on the stack to recover the LHS (`3')
7443 from the stack together with the tree code (`MULT_EXPR'), and
7444 the precedence of the higher level subexpression
7445 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7446 which will be used to actually build the additive expression. */
7447 rhs = current.lhs;
7448 rhs_type = current.lhs_type;
7449 --sp;
7450 current = *sp;
7453 /* Undo the disabling of warnings done above. */
7454 if (current.tree_type == TRUTH_ANDIF_EXPR)
7455 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7456 else if (current.tree_type == TRUTH_ORIF_EXPR)
7457 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7459 overload = NULL;
7460 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7461 ERROR_MARK for everything that is not a binary expression.
7462 This makes warn_about_parentheses miss some warnings that
7463 involve unary operators. For unary expressions we should
7464 pass the correct tree_code unless the unary expression was
7465 surrounded by parentheses.
7467 if (no_toplevel_fold_p
7468 && lookahead_prec <= current.prec
7469 && sp == stack
7470 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7471 current.lhs = build2 (current.tree_type, boolean_type_node,
7472 current.lhs, rhs);
7473 else
7474 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7475 current.lhs, current.lhs_type,
7476 rhs, rhs_type, &overload,
7477 tf_warning_or_error);
7478 current.lhs_type = current.tree_type;
7479 if (EXPR_P (current.lhs))
7480 SET_EXPR_LOCATION (current.lhs, current.loc);
7482 /* If the binary operator required the use of an overloaded operator,
7483 then this expression cannot be an integral constant-expression.
7484 An overloaded operator can be used even if both operands are
7485 otherwise permissible in an integral constant-expression if at
7486 least one of the operands is of enumeration type. */
7488 if (overload
7489 && cp_parser_non_integral_constant_expression (parser,
7490 NIC_OVERLOADED))
7491 return error_mark_node;
7494 return current.lhs;
7498 /* Parse the `? expression : assignment-expression' part of a
7499 conditional-expression. The LOGICAL_OR_EXPR is the
7500 logical-or-expression that started the conditional-expression.
7501 Returns a representation of the entire conditional-expression.
7503 This routine is used by cp_parser_assignment_expression.
7505 ? expression : assignment-expression
7507 GNU Extensions:
7509 ? : assignment-expression */
7511 static tree
7512 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7514 tree expr;
7515 tree assignment_expr;
7516 struct cp_token *token;
7517 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7519 /* Consume the `?' token. */
7520 cp_lexer_consume_token (parser->lexer);
7521 token = cp_lexer_peek_token (parser->lexer);
7522 if (cp_parser_allow_gnu_extensions_p (parser)
7523 && token->type == CPP_COLON)
7525 pedwarn (token->location, OPT_Wpedantic,
7526 "ISO C++ does not allow ?: with omitted middle operand");
7527 /* Implicit true clause. */
7528 expr = NULL_TREE;
7529 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7530 warn_for_omitted_condop (token->location, logical_or_expr);
7532 else
7534 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7535 parser->colon_corrects_to_scope_p = false;
7536 /* Parse the expression. */
7537 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7538 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7539 c_inhibit_evaluation_warnings +=
7540 ((logical_or_expr == truthvalue_true_node)
7541 - (logical_or_expr == truthvalue_false_node));
7542 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7545 /* The next token should be a `:'. */
7546 cp_parser_require (parser, CPP_COLON, RT_COLON);
7547 /* Parse the assignment-expression. */
7548 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7549 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7551 /* Build the conditional-expression. */
7552 return build_x_conditional_expr (loc, logical_or_expr,
7553 expr,
7554 assignment_expr,
7555 tf_warning_or_error);
7558 /* Parse an assignment-expression.
7560 assignment-expression:
7561 conditional-expression
7562 logical-or-expression assignment-operator assignment_expression
7563 throw-expression
7565 CAST_P is true if this expression is the target of a cast.
7567 Returns a representation for the expression. */
7569 static tree
7570 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7571 cp_id_kind * pidk)
7573 tree expr;
7575 /* If the next token is the `throw' keyword, then we're looking at
7576 a throw-expression. */
7577 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7578 expr = cp_parser_throw_expression (parser);
7579 /* Otherwise, it must be that we are looking at a
7580 logical-or-expression. */
7581 else
7583 /* Parse the binary expressions (logical-or-expression). */
7584 expr = cp_parser_binary_expression (parser, cast_p, false,
7585 PREC_NOT_OPERATOR, pidk);
7586 /* If the next token is a `?' then we're actually looking at a
7587 conditional-expression. */
7588 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7589 return cp_parser_question_colon_clause (parser, expr);
7590 else
7592 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7594 /* If it's an assignment-operator, we're using the second
7595 production. */
7596 enum tree_code assignment_operator
7597 = cp_parser_assignment_operator_opt (parser);
7598 if (assignment_operator != ERROR_MARK)
7600 bool non_constant_p;
7601 location_t saved_input_location;
7603 /* Parse the right-hand side of the assignment. */
7604 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7606 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7607 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7609 /* An assignment may not appear in a
7610 constant-expression. */
7611 if (cp_parser_non_integral_constant_expression (parser,
7612 NIC_ASSIGNMENT))
7613 return error_mark_node;
7614 /* Build the assignment expression. Its default
7615 location is the location of the '=' token. */
7616 saved_input_location = input_location;
7617 input_location = loc;
7618 expr = build_x_modify_expr (loc, expr,
7619 assignment_operator,
7620 rhs,
7621 tf_warning_or_error);
7622 input_location = saved_input_location;
7627 return expr;
7630 /* Parse an (optional) assignment-operator.
7632 assignment-operator: one of
7633 = *= /= %= += -= >>= <<= &= ^= |=
7635 GNU Extension:
7637 assignment-operator: one of
7638 <?= >?=
7640 If the next token is an assignment operator, the corresponding tree
7641 code is returned, and the token is consumed. For example, for
7642 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7643 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7644 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7645 operator, ERROR_MARK is returned. */
7647 static enum tree_code
7648 cp_parser_assignment_operator_opt (cp_parser* parser)
7650 enum tree_code op;
7651 cp_token *token;
7653 /* Peek at the next token. */
7654 token = cp_lexer_peek_token (parser->lexer);
7656 switch (token->type)
7658 case CPP_EQ:
7659 op = NOP_EXPR;
7660 break;
7662 case CPP_MULT_EQ:
7663 op = MULT_EXPR;
7664 break;
7666 case CPP_DIV_EQ:
7667 op = TRUNC_DIV_EXPR;
7668 break;
7670 case CPP_MOD_EQ:
7671 op = TRUNC_MOD_EXPR;
7672 break;
7674 case CPP_PLUS_EQ:
7675 op = PLUS_EXPR;
7676 break;
7678 case CPP_MINUS_EQ:
7679 op = MINUS_EXPR;
7680 break;
7682 case CPP_RSHIFT_EQ:
7683 op = RSHIFT_EXPR;
7684 break;
7686 case CPP_LSHIFT_EQ:
7687 op = LSHIFT_EXPR;
7688 break;
7690 case CPP_AND_EQ:
7691 op = BIT_AND_EXPR;
7692 break;
7694 case CPP_XOR_EQ:
7695 op = BIT_XOR_EXPR;
7696 break;
7698 case CPP_OR_EQ:
7699 op = BIT_IOR_EXPR;
7700 break;
7702 default:
7703 /* Nothing else is an assignment operator. */
7704 op = ERROR_MARK;
7707 /* If it was an assignment operator, consume it. */
7708 if (op != ERROR_MARK)
7709 cp_lexer_consume_token (parser->lexer);
7711 return op;
7714 /* Parse an expression.
7716 expression:
7717 assignment-expression
7718 expression , assignment-expression
7720 CAST_P is true if this expression is the target of a cast.
7722 Returns a representation of the expression. */
7724 static tree
7725 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7727 tree expression = NULL_TREE;
7728 location_t loc = UNKNOWN_LOCATION;
7730 while (true)
7732 tree assignment_expression;
7734 /* Parse the next assignment-expression. */
7735 assignment_expression
7736 = cp_parser_assignment_expression (parser, cast_p, pidk);
7737 /* If this is the first assignment-expression, we can just
7738 save it away. */
7739 if (!expression)
7740 expression = assignment_expression;
7741 else
7742 expression = build_x_compound_expr (loc, expression,
7743 assignment_expression,
7744 tf_warning_or_error);
7745 /* If the next token is not a comma, then we are done with the
7746 expression. */
7747 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7748 break;
7749 /* Consume the `,'. */
7750 loc = cp_lexer_peek_token (parser->lexer)->location;
7751 cp_lexer_consume_token (parser->lexer);
7752 /* A comma operator cannot appear in a constant-expression. */
7753 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7754 expression = error_mark_node;
7757 return expression;
7760 /* Parse a constant-expression.
7762 constant-expression:
7763 conditional-expression
7765 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7766 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7767 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7768 is false, NON_CONSTANT_P should be NULL. */
7770 static tree
7771 cp_parser_constant_expression (cp_parser* parser,
7772 bool allow_non_constant_p,
7773 bool *non_constant_p)
7775 bool saved_integral_constant_expression_p;
7776 bool saved_allow_non_integral_constant_expression_p;
7777 bool saved_non_integral_constant_expression_p;
7778 tree expression;
7780 /* It might seem that we could simply parse the
7781 conditional-expression, and then check to see if it were
7782 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7783 one that the compiler can figure out is constant, possibly after
7784 doing some simplifications or optimizations. The standard has a
7785 precise definition of constant-expression, and we must honor
7786 that, even though it is somewhat more restrictive.
7788 For example:
7790 int i[(2, 3)];
7792 is not a legal declaration, because `(2, 3)' is not a
7793 constant-expression. The `,' operator is forbidden in a
7794 constant-expression. However, GCC's constant-folding machinery
7795 will fold this operation to an INTEGER_CST for `3'. */
7797 /* Save the old settings. */
7798 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7799 saved_allow_non_integral_constant_expression_p
7800 = parser->allow_non_integral_constant_expression_p;
7801 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7802 /* We are now parsing a constant-expression. */
7803 parser->integral_constant_expression_p = true;
7804 parser->allow_non_integral_constant_expression_p
7805 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7806 parser->non_integral_constant_expression_p = false;
7807 /* Although the grammar says "conditional-expression", we parse an
7808 "assignment-expression", which also permits "throw-expression"
7809 and the use of assignment operators. In the case that
7810 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7811 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7812 actually essential that we look for an assignment-expression.
7813 For example, cp_parser_initializer_clauses uses this function to
7814 determine whether a particular assignment-expression is in fact
7815 constant. */
7816 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7817 /* Restore the old settings. */
7818 parser->integral_constant_expression_p
7819 = saved_integral_constant_expression_p;
7820 parser->allow_non_integral_constant_expression_p
7821 = saved_allow_non_integral_constant_expression_p;
7822 if (cxx_dialect >= cxx0x)
7824 /* Require an rvalue constant expression here; that's what our
7825 callers expect. Reference constant expressions are handled
7826 separately in e.g. cp_parser_template_argument. */
7827 bool is_const = potential_rvalue_constant_expression (expression);
7828 parser->non_integral_constant_expression_p = !is_const;
7829 if (!is_const && !allow_non_constant_p)
7830 require_potential_rvalue_constant_expression (expression);
7832 if (allow_non_constant_p)
7833 *non_constant_p = parser->non_integral_constant_expression_p;
7834 parser->non_integral_constant_expression_p
7835 = saved_non_integral_constant_expression_p;
7837 return expression;
7840 /* Parse __builtin_offsetof.
7842 offsetof-expression:
7843 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7845 offsetof-member-designator:
7846 id-expression
7847 | offsetof-member-designator "." id-expression
7848 | offsetof-member-designator "[" expression "]"
7849 | offsetof-member-designator "->" id-expression */
7851 static tree
7852 cp_parser_builtin_offsetof (cp_parser *parser)
7854 int save_ice_p, save_non_ice_p;
7855 tree type, expr;
7856 cp_id_kind dummy;
7857 cp_token *token;
7859 /* We're about to accept non-integral-constant things, but will
7860 definitely yield an integral constant expression. Save and
7861 restore these values around our local parsing. */
7862 save_ice_p = parser->integral_constant_expression_p;
7863 save_non_ice_p = parser->non_integral_constant_expression_p;
7865 /* Consume the "__builtin_offsetof" token. */
7866 cp_lexer_consume_token (parser->lexer);
7867 /* Consume the opening `('. */
7868 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7869 /* Parse the type-id. */
7870 type = cp_parser_type_id (parser);
7871 /* Look for the `,'. */
7872 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7873 token = cp_lexer_peek_token (parser->lexer);
7875 /* Build the (type *)null that begins the traditional offsetof macro. */
7876 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7877 tf_warning_or_error);
7879 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7880 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7881 true, &dummy, token->location);
7882 while (true)
7884 token = cp_lexer_peek_token (parser->lexer);
7885 switch (token->type)
7887 case CPP_OPEN_SQUARE:
7888 /* offsetof-member-designator "[" expression "]" */
7889 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7890 break;
7892 case CPP_DEREF:
7893 /* offsetof-member-designator "->" identifier */
7894 expr = grok_array_decl (token->location, expr, integer_zero_node);
7895 /* FALLTHRU */
7897 case CPP_DOT:
7898 /* offsetof-member-designator "." identifier */
7899 cp_lexer_consume_token (parser->lexer);
7900 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7901 expr, true, &dummy,
7902 token->location);
7903 break;
7905 case CPP_CLOSE_PAREN:
7906 /* Consume the ")" token. */
7907 cp_lexer_consume_token (parser->lexer);
7908 goto success;
7910 default:
7911 /* Error. We know the following require will fail, but
7912 that gives the proper error message. */
7913 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7914 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7915 expr = error_mark_node;
7916 goto failure;
7920 success:
7921 /* If we're processing a template, we can't finish the semantics yet.
7922 Otherwise we can fold the entire expression now. */
7923 if (processing_template_decl)
7924 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7925 else
7926 expr = finish_offsetof (expr);
7928 failure:
7929 parser->integral_constant_expression_p = save_ice_p;
7930 parser->non_integral_constant_expression_p = save_non_ice_p;
7932 return expr;
7935 /* Parse a trait expression.
7937 Returns a representation of the expression, the underlying type
7938 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7940 static tree
7941 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7943 cp_trait_kind kind;
7944 tree type1, type2 = NULL_TREE;
7945 bool binary = false;
7946 cp_decl_specifier_seq decl_specs;
7948 switch (keyword)
7950 case RID_HAS_NOTHROW_ASSIGN:
7951 kind = CPTK_HAS_NOTHROW_ASSIGN;
7952 break;
7953 case RID_HAS_NOTHROW_CONSTRUCTOR:
7954 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7955 break;
7956 case RID_HAS_NOTHROW_COPY:
7957 kind = CPTK_HAS_NOTHROW_COPY;
7958 break;
7959 case RID_HAS_TRIVIAL_ASSIGN:
7960 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7961 break;
7962 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7963 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7964 break;
7965 case RID_HAS_TRIVIAL_COPY:
7966 kind = CPTK_HAS_TRIVIAL_COPY;
7967 break;
7968 case RID_HAS_TRIVIAL_DESTRUCTOR:
7969 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7970 break;
7971 case RID_HAS_VIRTUAL_DESTRUCTOR:
7972 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7973 break;
7974 case RID_IS_ABSTRACT:
7975 kind = CPTK_IS_ABSTRACT;
7976 break;
7977 case RID_IS_BASE_OF:
7978 kind = CPTK_IS_BASE_OF;
7979 binary = true;
7980 break;
7981 case RID_IS_CLASS:
7982 kind = CPTK_IS_CLASS;
7983 break;
7984 case RID_IS_CONVERTIBLE_TO:
7985 kind = CPTK_IS_CONVERTIBLE_TO;
7986 binary = true;
7987 break;
7988 case RID_IS_EMPTY:
7989 kind = CPTK_IS_EMPTY;
7990 break;
7991 case RID_IS_ENUM:
7992 kind = CPTK_IS_ENUM;
7993 break;
7994 case RID_IS_FINAL:
7995 kind = CPTK_IS_FINAL;
7996 break;
7997 case RID_IS_LITERAL_TYPE:
7998 kind = CPTK_IS_LITERAL_TYPE;
7999 break;
8000 case RID_IS_POD:
8001 kind = CPTK_IS_POD;
8002 break;
8003 case RID_IS_POLYMORPHIC:
8004 kind = CPTK_IS_POLYMORPHIC;
8005 break;
8006 case RID_IS_STD_LAYOUT:
8007 kind = CPTK_IS_STD_LAYOUT;
8008 break;
8009 case RID_IS_TRIVIAL:
8010 kind = CPTK_IS_TRIVIAL;
8011 break;
8012 case RID_IS_UNION:
8013 kind = CPTK_IS_UNION;
8014 break;
8015 case RID_UNDERLYING_TYPE:
8016 kind = CPTK_UNDERLYING_TYPE;
8017 break;
8018 case RID_BASES:
8019 kind = CPTK_BASES;
8020 break;
8021 case RID_DIRECT_BASES:
8022 kind = CPTK_DIRECT_BASES;
8023 break;
8024 default:
8025 gcc_unreachable ();
8028 /* Consume the token. */
8029 cp_lexer_consume_token (parser->lexer);
8031 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8033 type1 = cp_parser_type_id (parser);
8035 if (type1 == error_mark_node)
8036 return error_mark_node;
8038 /* Build a trivial decl-specifier-seq. */
8039 clear_decl_specs (&decl_specs);
8040 decl_specs.type = type1;
8042 /* Call grokdeclarator to figure out what type this is. */
8043 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8044 /*initialized=*/0, /*attrlist=*/NULL);
8046 if (binary)
8048 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8050 type2 = cp_parser_type_id (parser);
8052 if (type2 == error_mark_node)
8053 return error_mark_node;
8055 /* Build a trivial decl-specifier-seq. */
8056 clear_decl_specs (&decl_specs);
8057 decl_specs.type = type2;
8059 /* Call grokdeclarator to figure out what type this is. */
8060 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8061 /*initialized=*/0, /*attrlist=*/NULL);
8064 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8066 /* Complete the trait expression, which may mean either processing
8067 the trait expr now or saving it for template instantiation. */
8068 switch(kind)
8070 case CPTK_UNDERLYING_TYPE:
8071 return finish_underlying_type (type1);
8072 case CPTK_BASES:
8073 return finish_bases (type1, false);
8074 case CPTK_DIRECT_BASES:
8075 return finish_bases (type1, true);
8076 default:
8077 return finish_trait_expr (kind, type1, type2);
8081 /* Lambdas that appear in variable initializer or default argument scope
8082 get that in their mangling, so we need to record it. We might as well
8083 use the count for function and namespace scopes as well. */
8084 static GTY(()) tree lambda_scope;
8085 static GTY(()) int lambda_count;
8086 typedef struct GTY(()) tree_int
8088 tree t;
8089 int i;
8090 } tree_int;
8091 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8093 static void
8094 start_lambda_scope (tree decl)
8096 tree_int ti;
8097 gcc_assert (decl);
8098 /* Once we're inside a function, we ignore other scopes and just push
8099 the function again so that popping works properly. */
8100 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8101 decl = current_function_decl;
8102 ti.t = lambda_scope;
8103 ti.i = lambda_count;
8104 vec_safe_push (lambda_scope_stack, ti);
8105 if (lambda_scope != decl)
8107 /* Don't reset the count if we're still in the same function. */
8108 lambda_scope = decl;
8109 lambda_count = 0;
8113 static void
8114 record_lambda_scope (tree lambda)
8116 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8117 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8120 static void
8121 finish_lambda_scope (void)
8123 tree_int *p = &lambda_scope_stack->last ();
8124 if (lambda_scope != p->t)
8126 lambda_scope = p->t;
8127 lambda_count = p->i;
8129 lambda_scope_stack->pop ();
8132 /* Parse a lambda expression.
8134 lambda-expression:
8135 lambda-introducer lambda-declarator [opt] compound-statement
8137 Returns a representation of the expression. */
8139 static tree
8140 cp_parser_lambda_expression (cp_parser* parser)
8142 tree lambda_expr = build_lambda_expr ();
8143 tree type;
8144 bool ok;
8146 LAMBDA_EXPR_LOCATION (lambda_expr)
8147 = cp_lexer_peek_token (parser->lexer)->location;
8149 if (cp_unevaluated_operand)
8150 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8151 "lambda-expression in unevaluated context");
8153 /* We may be in the middle of deferred access check. Disable
8154 it now. */
8155 push_deferring_access_checks (dk_no_deferred);
8157 cp_parser_lambda_introducer (parser, lambda_expr);
8159 type = begin_lambda_type (lambda_expr);
8160 if (type == error_mark_node)
8161 return error_mark_node;
8163 record_lambda_scope (lambda_expr);
8165 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8166 determine_visibility (TYPE_NAME (type));
8168 /* Now that we've started the type, add the capture fields for any
8169 explicit captures. */
8170 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8173 /* Inside the class, surrounding template-parameter-lists do not apply. */
8174 unsigned int saved_num_template_parameter_lists
8175 = parser->num_template_parameter_lists;
8176 unsigned char in_statement = parser->in_statement;
8177 bool in_switch_statement_p = parser->in_switch_statement_p;
8179 parser->num_template_parameter_lists = 0;
8180 parser->in_statement = 0;
8181 parser->in_switch_statement_p = false;
8183 /* By virtue of defining a local class, a lambda expression has access to
8184 the private variables of enclosing classes. */
8186 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8188 if (ok)
8189 cp_parser_lambda_body (parser, lambda_expr);
8190 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8191 cp_parser_skip_to_end_of_block_or_statement (parser);
8193 /* The capture list was built up in reverse order; fix that now. */
8195 tree newlist = NULL_TREE;
8196 tree elt, next;
8198 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8199 elt; elt = next)
8201 next = TREE_CHAIN (elt);
8202 TREE_CHAIN (elt) = newlist;
8203 newlist = elt;
8205 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8208 if (ok)
8209 maybe_add_lambda_conv_op (type);
8211 type = finish_struct (type, /*attributes=*/NULL_TREE);
8213 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8214 parser->in_statement = in_statement;
8215 parser->in_switch_statement_p = in_switch_statement_p;
8218 pop_deferring_access_checks ();
8220 /* This field is only used during parsing of the lambda. */
8221 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8223 /* This lambda shouldn't have any proxies left at this point. */
8224 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8225 /* And now that we're done, push proxies for an enclosing lambda. */
8226 insert_pending_capture_proxies ();
8228 if (ok)
8229 return build_lambda_object (lambda_expr);
8230 else
8231 return error_mark_node;
8234 /* Parse the beginning of a lambda expression.
8236 lambda-introducer:
8237 [ lambda-capture [opt] ]
8239 LAMBDA_EXPR is the current representation of the lambda expression. */
8241 static void
8242 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8244 /* Need commas after the first capture. */
8245 bool first = true;
8247 /* Eat the leading `['. */
8248 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8250 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8251 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8252 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8253 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8254 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8255 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8257 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8259 cp_lexer_consume_token (parser->lexer);
8260 first = false;
8263 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8265 cp_token* capture_token;
8266 tree capture_id;
8267 tree capture_init_expr;
8268 cp_id_kind idk = CP_ID_KIND_NONE;
8269 bool explicit_init_p = false;
8271 enum capture_kind_type
8273 BY_COPY,
8274 BY_REFERENCE
8276 enum capture_kind_type capture_kind = BY_COPY;
8278 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8280 error ("expected end of capture-list");
8281 return;
8284 if (first)
8285 first = false;
8286 else
8287 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8289 /* Possibly capture `this'. */
8290 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8292 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8293 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8294 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8295 "with by-copy capture default");
8296 cp_lexer_consume_token (parser->lexer);
8297 add_capture (lambda_expr,
8298 /*id=*/this_identifier,
8299 /*initializer=*/finish_this_expr(),
8300 /*by_reference_p=*/false,
8301 explicit_init_p);
8302 continue;
8305 /* Remember whether we want to capture as a reference or not. */
8306 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8308 capture_kind = BY_REFERENCE;
8309 cp_lexer_consume_token (parser->lexer);
8312 /* Get the identifier. */
8313 capture_token = cp_lexer_peek_token (parser->lexer);
8314 capture_id = cp_parser_identifier (parser);
8316 if (capture_id == error_mark_node)
8317 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8318 delimiters, but I modified this to stop on unnested ']' as well. It
8319 was already changed to stop on unnested '}', so the
8320 "closing_parenthesis" name is no more misleading with my change. */
8322 cp_parser_skip_to_closing_parenthesis (parser,
8323 /*recovering=*/true,
8324 /*or_comma=*/true,
8325 /*consume_paren=*/true);
8326 break;
8329 /* Find the initializer for this capture. */
8330 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8332 /* An explicit expression exists. */
8333 cp_lexer_consume_token (parser->lexer);
8334 pedwarn (input_location, OPT_Wpedantic,
8335 "ISO C++ does not allow initializers "
8336 "in lambda expression capture lists");
8337 capture_init_expr = cp_parser_assignment_expression (parser,
8338 /*cast_p=*/true,
8339 &idk);
8340 explicit_init_p = true;
8342 else
8344 const char* error_msg;
8346 /* Turn the identifier into an id-expression. */
8347 capture_init_expr
8348 = cp_parser_lookup_name
8349 (parser,
8350 capture_id,
8351 none_type,
8352 /*is_template=*/false,
8353 /*is_namespace=*/false,
8354 /*check_dependency=*/true,
8355 /*ambiguous_decls=*/NULL,
8356 capture_token->location);
8358 if (capture_init_expr == error_mark_node)
8360 unqualified_name_lookup_error (capture_id);
8361 continue;
8363 else if (DECL_P (capture_init_expr)
8364 && (TREE_CODE (capture_init_expr) != VAR_DECL
8365 && TREE_CODE (capture_init_expr) != PARM_DECL))
8367 error_at (capture_token->location,
8368 "capture of non-variable %qD ",
8369 capture_init_expr);
8370 inform (0, "%q+#D declared here", capture_init_expr);
8371 continue;
8373 if (TREE_CODE (capture_init_expr) == VAR_DECL
8374 && decl_storage_duration (capture_init_expr) != dk_auto)
8376 pedwarn (capture_token->location, 0, "capture of variable "
8377 "%qD with non-automatic storage duration",
8378 capture_init_expr);
8379 inform (0, "%q+#D declared here", capture_init_expr);
8380 continue;
8383 capture_init_expr
8384 = finish_id_expression
8385 (capture_id,
8386 capture_init_expr,
8387 parser->scope,
8388 &idk,
8389 /*integral_constant_expression_p=*/false,
8390 /*allow_non_integral_constant_expression_p=*/false,
8391 /*non_integral_constant_expression_p=*/NULL,
8392 /*template_p=*/false,
8393 /*done=*/true,
8394 /*address_p=*/false,
8395 /*template_arg_p=*/false,
8396 &error_msg,
8397 capture_token->location);
8400 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8401 && !explicit_init_p)
8403 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8404 && capture_kind == BY_COPY)
8405 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8406 "of %qD redundant with by-copy capture default",
8407 capture_id);
8408 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8409 && capture_kind == BY_REFERENCE)
8410 pedwarn (capture_token->location, 0, "explicit by-reference "
8411 "capture of %qD redundant with by-reference capture "
8412 "default", capture_id);
8415 add_capture (lambda_expr,
8416 capture_id,
8417 capture_init_expr,
8418 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8419 explicit_init_p);
8422 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8425 /* Parse the (optional) middle of a lambda expression.
8427 lambda-declarator:
8428 ( parameter-declaration-clause [opt] )
8429 attribute-specifier [opt]
8430 mutable [opt]
8431 exception-specification [opt]
8432 lambda-return-type-clause [opt]
8434 LAMBDA_EXPR is the current representation of the lambda expression. */
8436 static bool
8437 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8439 /* 5.1.1.4 of the standard says:
8440 If a lambda-expression does not include a lambda-declarator, it is as if
8441 the lambda-declarator were ().
8442 This means an empty parameter list, no attributes, and no exception
8443 specification. */
8444 tree param_list = void_list_node;
8445 tree attributes = NULL_TREE;
8446 tree exception_spec = NULL_TREE;
8447 tree t;
8449 /* The lambda-declarator is optional, but must begin with an opening
8450 parenthesis if present. */
8451 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8453 cp_lexer_consume_token (parser->lexer);
8455 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8457 /* Parse parameters. */
8458 param_list = cp_parser_parameter_declaration_clause (parser);
8460 /* Default arguments shall not be specified in the
8461 parameter-declaration-clause of a lambda-declarator. */
8462 for (t = param_list; t; t = TREE_CHAIN (t))
8463 if (TREE_PURPOSE (t))
8464 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8465 "default argument specified for lambda parameter");
8467 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8469 attributes = cp_parser_attributes_opt (parser);
8471 /* Parse optional `mutable' keyword. */
8472 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8474 cp_lexer_consume_token (parser->lexer);
8475 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8478 /* Parse optional exception specification. */
8479 exception_spec = cp_parser_exception_specification_opt (parser);
8481 /* Parse optional trailing return type. */
8482 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8484 cp_lexer_consume_token (parser->lexer);
8485 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8488 /* The function parameters must be in scope all the way until after the
8489 trailing-return-type in case of decltype. */
8490 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8491 pop_binding (DECL_NAME (t), t);
8493 leave_scope ();
8496 /* Create the function call operator.
8498 Messing with declarators like this is no uglier than building up the
8499 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8500 other code. */
8502 cp_decl_specifier_seq return_type_specs;
8503 cp_declarator* declarator;
8504 tree fco;
8505 int quals;
8506 void *p;
8508 clear_decl_specs (&return_type_specs);
8509 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8510 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8511 else
8512 /* Maybe we will deduce the return type later. */
8513 return_type_specs.type = make_auto ();
8515 p = obstack_alloc (&declarator_obstack, 0);
8517 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8518 sfk_none);
8520 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8521 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8522 declarator = make_call_declarator (declarator, param_list, quals,
8523 VIRT_SPEC_UNSPECIFIED,
8524 exception_spec,
8525 /*late_return_type=*/NULL_TREE);
8526 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8528 fco = grokmethod (&return_type_specs,
8529 declarator,
8530 attributes);
8531 if (fco != error_mark_node)
8533 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8534 DECL_ARTIFICIAL (fco) = 1;
8535 /* Give the object parameter a different name. */
8536 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8539 finish_member_declaration (fco);
8541 obstack_free (&declarator_obstack, p);
8543 return (fco != error_mark_node);
8547 /* Parse the body of a lambda expression, which is simply
8549 compound-statement
8551 but which requires special handling.
8552 LAMBDA_EXPR is the current representation of the lambda expression. */
8554 static void
8555 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8557 bool nested = (current_function_decl != NULL_TREE);
8558 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8559 if (nested)
8560 push_function_context ();
8561 else
8562 /* Still increment function_depth so that we don't GC in the
8563 middle of an expression. */
8564 ++function_depth;
8565 /* Clear this in case we're in the middle of a default argument. */
8566 parser->local_variables_forbidden_p = false;
8568 /* Finish the function call operator
8569 - class_specifier
8570 + late_parsing_for_member
8571 + function_definition_after_declarator
8572 + ctor_initializer_opt_and_function_body */
8574 tree fco = lambda_function (lambda_expr);
8575 tree body;
8576 bool done = false;
8577 tree compound_stmt;
8578 tree cap;
8580 /* Let the front end know that we are going to be defining this
8581 function. */
8582 start_preparsed_function (fco,
8583 NULL_TREE,
8584 SF_PRE_PARSED | SF_INCLASS_INLINE);
8586 start_lambda_scope (fco);
8587 body = begin_function_body ();
8589 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8590 goto out;
8592 /* Push the proxies for any explicit captures. */
8593 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8594 cap = TREE_CHAIN (cap))
8595 build_capture_proxy (TREE_PURPOSE (cap));
8597 compound_stmt = begin_compound_stmt (0);
8599 /* 5.1.1.4 of the standard says:
8600 If a lambda-expression does not include a trailing-return-type, it
8601 is as if the trailing-return-type denotes the following type:
8602 * if the compound-statement is of the form
8603 { return attribute-specifier [opt] expression ; }
8604 the type of the returned expression after lvalue-to-rvalue
8605 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8606 (_conv.array_ 4.2), and function-to-pointer conversion
8607 (_conv.func_ 4.3);
8608 * otherwise, void. */
8610 /* In a lambda that has neither a lambda-return-type-clause
8611 nor a deducible form, errors should be reported for return statements
8612 in the body. Since we used void as the placeholder return type, parsing
8613 the body as usual will give such desired behavior. */
8614 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8615 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8616 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8618 tree expr = NULL_TREE;
8619 cp_id_kind idk = CP_ID_KIND_NONE;
8621 /* Parse tentatively in case there's more after the initial return
8622 statement. */
8623 cp_parser_parse_tentatively (parser);
8625 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8627 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8629 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8630 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8632 if (cp_parser_parse_definitely (parser))
8634 if (!processing_template_decl)
8635 apply_deduced_return_type (fco, lambda_return_type (expr));
8637 /* Will get error here if type not deduced yet. */
8638 finish_return_stmt (expr);
8640 done = true;
8644 if (!done)
8646 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8647 cp_parser_label_declaration (parser);
8648 cp_parser_statement_seq_opt (parser, NULL_TREE);
8649 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8652 finish_compound_stmt (compound_stmt);
8654 out:
8655 finish_function_body (body);
8656 finish_lambda_scope ();
8658 /* Finish the function and generate code for it if necessary. */
8659 expand_or_defer_fn (finish_function (/*inline*/2));
8662 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8663 if (nested)
8664 pop_function_context();
8665 else
8666 --function_depth;
8669 /* Statements [gram.stmt.stmt] */
8671 /* Parse a statement.
8673 statement:
8674 labeled-statement
8675 expression-statement
8676 compound-statement
8677 selection-statement
8678 iteration-statement
8679 jump-statement
8680 declaration-statement
8681 try-block
8683 C++11:
8685 statement:
8686 labeled-statement
8687 attribute-specifier-seq (opt) expression-statement
8688 attribute-specifier-seq (opt) compound-statement
8689 attribute-specifier-seq (opt) selection-statement
8690 attribute-specifier-seq (opt) iteration-statement
8691 attribute-specifier-seq (opt) jump-statement
8692 declaration-statement
8693 attribute-specifier-seq (opt) try-block
8695 TM Extension:
8697 statement:
8698 atomic-statement
8700 IN_COMPOUND is true when the statement is nested inside a
8701 cp_parser_compound_statement; this matters for certain pragmas.
8703 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8704 is a (possibly labeled) if statement which is not enclosed in braces
8705 and has an else clause. This is used to implement -Wparentheses. */
8707 static void
8708 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8709 bool in_compound, bool *if_p)
8711 tree statement, std_attrs = NULL_TREE;
8712 cp_token *token;
8713 location_t statement_location, attrs_location;
8715 restart:
8716 if (if_p != NULL)
8717 *if_p = false;
8718 /* There is no statement yet. */
8719 statement = NULL_TREE;
8721 cp_lexer_save_tokens (parser->lexer);
8722 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
8723 if (c_dialect_objc ())
8724 /* In obj-c++, seing '[[' might be the either the beginning of
8725 c++11 attributes, or a nested objc-message-expression. So
8726 let's parse the c++11 attributes tentatively. */
8727 cp_parser_parse_tentatively (parser);
8728 std_attrs = cp_parser_std_attribute_spec_seq (parser);
8729 if (c_dialect_objc ())
8731 if (!cp_parser_parse_definitely (parser))
8732 std_attrs = NULL_TREE;
8735 /* Peek at the next token. */
8736 token = cp_lexer_peek_token (parser->lexer);
8737 /* Remember the location of the first token in the statement. */
8738 statement_location = token->location;
8739 /* If this is a keyword, then that will often determine what kind of
8740 statement we have. */
8741 if (token->type == CPP_KEYWORD)
8743 enum rid keyword = token->keyword;
8745 switch (keyword)
8747 case RID_CASE:
8748 case RID_DEFAULT:
8749 /* Looks like a labeled-statement with a case label.
8750 Parse the label, and then use tail recursion to parse
8751 the statement. */
8752 cp_parser_label_for_labeled_statement (parser, std_attrs);
8753 goto restart;
8755 case RID_IF:
8756 case RID_SWITCH:
8757 statement = cp_parser_selection_statement (parser, if_p);
8758 break;
8760 case RID_WHILE:
8761 case RID_DO:
8762 case RID_FOR:
8763 statement = cp_parser_iteration_statement (parser);
8764 break;
8766 case RID_BREAK:
8767 case RID_CONTINUE:
8768 case RID_RETURN:
8769 case RID_GOTO:
8770 statement = cp_parser_jump_statement (parser);
8771 break;
8773 /* Objective-C++ exception-handling constructs. */
8774 case RID_AT_TRY:
8775 case RID_AT_CATCH:
8776 case RID_AT_FINALLY:
8777 case RID_AT_SYNCHRONIZED:
8778 case RID_AT_THROW:
8779 statement = cp_parser_objc_statement (parser);
8780 break;
8782 case RID_TRY:
8783 statement = cp_parser_try_block (parser);
8784 break;
8786 case RID_NAMESPACE:
8787 /* This must be a namespace alias definition. */
8788 cp_parser_declaration_statement (parser);
8789 return;
8791 case RID_TRANSACTION_ATOMIC:
8792 case RID_TRANSACTION_RELAXED:
8793 statement = cp_parser_transaction (parser, keyword);
8794 break;
8795 case RID_TRANSACTION_CANCEL:
8796 statement = cp_parser_transaction_cancel (parser);
8797 break;
8799 default:
8800 /* It might be a keyword like `int' that can start a
8801 declaration-statement. */
8802 break;
8805 else if (token->type == CPP_NAME)
8807 /* If the next token is a `:', then we are looking at a
8808 labeled-statement. */
8809 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8810 if (token->type == CPP_COLON)
8812 /* Looks like a labeled-statement with an ordinary label.
8813 Parse the label, and then use tail recursion to parse
8814 the statement. */
8816 cp_parser_label_for_labeled_statement (parser, std_attrs);
8817 goto restart;
8820 /* Anything that starts with a `{' must be a compound-statement. */
8821 else if (token->type == CPP_OPEN_BRACE)
8822 statement = cp_parser_compound_statement (parser, NULL, false, false);
8823 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8824 a statement all its own. */
8825 else if (token->type == CPP_PRAGMA)
8827 /* Only certain OpenMP pragmas are attached to statements, and thus
8828 are considered statements themselves. All others are not. In
8829 the context of a compound, accept the pragma as a "statement" and
8830 return so that we can check for a close brace. Otherwise we
8831 require a real statement and must go back and read one. */
8832 if (in_compound)
8833 cp_parser_pragma (parser, pragma_compound);
8834 else if (!cp_parser_pragma (parser, pragma_stmt))
8835 goto restart;
8836 return;
8838 else if (token->type == CPP_EOF)
8840 cp_parser_error (parser, "expected statement");
8841 return;
8844 /* Everything else must be a declaration-statement or an
8845 expression-statement. Try for the declaration-statement
8846 first, unless we are looking at a `;', in which case we know that
8847 we have an expression-statement. */
8848 if (!statement)
8850 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8852 if (std_attrs != NULL_TREE)
8854 /* Attributes should be parsed as part of the the
8855 declaration, so let's un-parse them. */
8856 cp_lexer_rollback_tokens (parser->lexer);
8857 std_attrs = NULL_TREE;
8860 cp_parser_parse_tentatively (parser);
8861 /* Try to parse the declaration-statement. */
8862 cp_parser_declaration_statement (parser);
8863 /* If that worked, we're done. */
8864 if (cp_parser_parse_definitely (parser))
8865 return;
8867 /* Look for an expression-statement instead. */
8868 statement = cp_parser_expression_statement (parser, in_statement_expr);
8871 /* Set the line number for the statement. */
8872 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8873 SET_EXPR_LOCATION (statement, statement_location);
8875 /* Note that for now, we don't do anything with c++11 statements
8876 parsed at this level. */
8877 if (std_attrs != NULL_TREE)
8878 warning_at (attrs_location,
8879 OPT_Wattributes,
8880 "attributes at the beginning of statement are ignored");
8883 /* Parse the label for a labeled-statement, i.e.
8885 identifier :
8886 case constant-expression :
8887 default :
8889 GNU Extension:
8890 case constant-expression ... constant-expression : statement
8892 When a label is parsed without errors, the label is added to the
8893 parse tree by the finish_* functions, so this function doesn't
8894 have to return the label. */
8896 static void
8897 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
8899 cp_token *token;
8900 tree label = NULL_TREE;
8901 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8903 /* The next token should be an identifier. */
8904 token = cp_lexer_peek_token (parser->lexer);
8905 if (token->type != CPP_NAME
8906 && token->type != CPP_KEYWORD)
8908 cp_parser_error (parser, "expected labeled-statement");
8909 return;
8912 parser->colon_corrects_to_scope_p = false;
8913 switch (token->keyword)
8915 case RID_CASE:
8917 tree expr, expr_hi;
8918 cp_token *ellipsis;
8920 /* Consume the `case' token. */
8921 cp_lexer_consume_token (parser->lexer);
8922 /* Parse the constant-expression. */
8923 expr = cp_parser_constant_expression (parser,
8924 /*allow_non_constant_p=*/false,
8925 NULL);
8927 ellipsis = cp_lexer_peek_token (parser->lexer);
8928 if (ellipsis->type == CPP_ELLIPSIS)
8930 /* Consume the `...' token. */
8931 cp_lexer_consume_token (parser->lexer);
8932 expr_hi =
8933 cp_parser_constant_expression (parser,
8934 /*allow_non_constant_p=*/false,
8935 NULL);
8936 /* We don't need to emit warnings here, as the common code
8937 will do this for us. */
8939 else
8940 expr_hi = NULL_TREE;
8942 if (parser->in_switch_statement_p)
8943 finish_case_label (token->location, expr, expr_hi);
8944 else
8945 error_at (token->location,
8946 "case label %qE not within a switch statement",
8947 expr);
8949 break;
8951 case RID_DEFAULT:
8952 /* Consume the `default' token. */
8953 cp_lexer_consume_token (parser->lexer);
8955 if (parser->in_switch_statement_p)
8956 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8957 else
8958 error_at (token->location, "case label not within a switch statement");
8959 break;
8961 default:
8962 /* Anything else must be an ordinary label. */
8963 label = finish_label_stmt (cp_parser_identifier (parser));
8964 break;
8967 /* Require the `:' token. */
8968 cp_parser_require (parser, CPP_COLON, RT_COLON);
8970 /* An ordinary label may optionally be followed by attributes.
8971 However, this is only permitted if the attributes are then
8972 followed by a semicolon. This is because, for backward
8973 compatibility, when parsing
8974 lab: __attribute__ ((unused)) int i;
8975 we want the attribute to attach to "i", not "lab". */
8976 if (label != NULL_TREE
8977 && cp_next_tokens_can_be_gnu_attribute_p (parser))
8979 tree attrs;
8980 cp_parser_parse_tentatively (parser);
8981 attrs = cp_parser_gnu_attributes_opt (parser);
8982 if (attrs == NULL_TREE
8983 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8984 cp_parser_abort_tentative_parse (parser);
8985 else if (!cp_parser_parse_definitely (parser))
8987 else
8988 attributes = chainon (attributes, attrs);
8991 if (attributes != NULL_TREE)
8992 cplus_decl_attributes (&label, attributes, 0);
8994 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8997 /* Parse an expression-statement.
8999 expression-statement:
9000 expression [opt] ;
9002 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9003 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9004 indicates whether this expression-statement is part of an
9005 expression statement. */
9007 static tree
9008 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9010 tree statement = NULL_TREE;
9011 cp_token *token = cp_lexer_peek_token (parser->lexer);
9013 /* If the next token is a ';', then there is no expression
9014 statement. */
9015 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9016 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9018 /* Give a helpful message for "A<T>::type t;" and the like. */
9019 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9020 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9022 if (TREE_CODE (statement) == SCOPE_REF)
9023 error_at (token->location, "need %<typename%> before %qE because "
9024 "%qT is a dependent scope",
9025 statement, TREE_OPERAND (statement, 0));
9026 else if (is_overloaded_fn (statement)
9027 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9029 /* A::A a; */
9030 tree fn = get_first_fn (statement);
9031 error_at (token->location,
9032 "%<%T::%D%> names the constructor, not the type",
9033 DECL_CONTEXT (fn), DECL_NAME (fn));
9037 /* Consume the final `;'. */
9038 cp_parser_consume_semicolon_at_end_of_statement (parser);
9040 if (in_statement_expr
9041 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9042 /* This is the final expression statement of a statement
9043 expression. */
9044 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9045 else if (statement)
9046 statement = finish_expr_stmt (statement);
9047 else
9048 finish_stmt ();
9050 return statement;
9053 /* Parse a compound-statement.
9055 compound-statement:
9056 { statement-seq [opt] }
9058 GNU extension:
9060 compound-statement:
9061 { label-declaration-seq [opt] statement-seq [opt] }
9063 label-declaration-seq:
9064 label-declaration
9065 label-declaration-seq label-declaration
9067 Returns a tree representing the statement. */
9069 static tree
9070 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9071 bool in_try, bool function_body)
9073 tree compound_stmt;
9075 /* Consume the `{'. */
9076 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9077 return error_mark_node;
9078 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9079 && !function_body)
9080 pedwarn (input_location, OPT_Wpedantic,
9081 "compound-statement in constexpr function");
9082 /* Begin the compound-statement. */
9083 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9084 /* If the next keyword is `__label__' we have a label declaration. */
9085 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9086 cp_parser_label_declaration (parser);
9087 /* Parse an (optional) statement-seq. */
9088 cp_parser_statement_seq_opt (parser, in_statement_expr);
9089 /* Finish the compound-statement. */
9090 finish_compound_stmt (compound_stmt);
9091 /* Consume the `}'. */
9092 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9094 return compound_stmt;
9097 /* Parse an (optional) statement-seq.
9099 statement-seq:
9100 statement
9101 statement-seq [opt] statement */
9103 static void
9104 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9106 /* Scan statements until there aren't any more. */
9107 while (true)
9109 cp_token *token = cp_lexer_peek_token (parser->lexer);
9111 /* If we are looking at a `}', then we have run out of
9112 statements; the same is true if we have reached the end
9113 of file, or have stumbled upon a stray '@end'. */
9114 if (token->type == CPP_CLOSE_BRACE
9115 || token->type == CPP_EOF
9116 || token->type == CPP_PRAGMA_EOL
9117 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9118 break;
9120 /* If we are in a compound statement and find 'else' then
9121 something went wrong. */
9122 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9124 if (parser->in_statement & IN_IF_STMT)
9125 break;
9126 else
9128 token = cp_lexer_consume_token (parser->lexer);
9129 error_at (token->location, "%<else%> without a previous %<if%>");
9133 /* Parse the statement. */
9134 cp_parser_statement (parser, in_statement_expr, true, NULL);
9138 /* Parse a selection-statement.
9140 selection-statement:
9141 if ( condition ) statement
9142 if ( condition ) statement else statement
9143 switch ( condition ) statement
9145 Returns the new IF_STMT or SWITCH_STMT.
9147 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9148 is a (possibly labeled) if statement which is not enclosed in
9149 braces and has an else clause. This is used to implement
9150 -Wparentheses. */
9152 static tree
9153 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9155 cp_token *token;
9156 enum rid keyword;
9158 if (if_p != NULL)
9159 *if_p = false;
9161 /* Peek at the next token. */
9162 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9164 /* See what kind of keyword it is. */
9165 keyword = token->keyword;
9166 switch (keyword)
9168 case RID_IF:
9169 case RID_SWITCH:
9171 tree statement;
9172 tree condition;
9174 /* Look for the `('. */
9175 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9177 cp_parser_skip_to_end_of_statement (parser);
9178 return error_mark_node;
9181 /* Begin the selection-statement. */
9182 if (keyword == RID_IF)
9183 statement = begin_if_stmt ();
9184 else
9185 statement = begin_switch_stmt ();
9187 /* Parse the condition. */
9188 condition = cp_parser_condition (parser);
9189 /* Look for the `)'. */
9190 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9191 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9192 /*consume_paren=*/true);
9194 if (keyword == RID_IF)
9196 bool nested_if;
9197 unsigned char in_statement;
9199 /* Add the condition. */
9200 finish_if_stmt_cond (condition, statement);
9202 /* Parse the then-clause. */
9203 in_statement = parser->in_statement;
9204 parser->in_statement |= IN_IF_STMT;
9205 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9207 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9208 add_stmt (build_empty_stmt (loc));
9209 cp_lexer_consume_token (parser->lexer);
9210 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9211 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9212 "empty body in an %<if%> statement");
9213 nested_if = false;
9215 else
9216 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9217 parser->in_statement = in_statement;
9219 finish_then_clause (statement);
9221 /* If the next token is `else', parse the else-clause. */
9222 if (cp_lexer_next_token_is_keyword (parser->lexer,
9223 RID_ELSE))
9225 /* Consume the `else' keyword. */
9226 cp_lexer_consume_token (parser->lexer);
9227 begin_else_clause (statement);
9228 /* Parse the else-clause. */
9229 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9231 location_t loc;
9232 loc = cp_lexer_peek_token (parser->lexer)->location;
9233 warning_at (loc,
9234 OPT_Wempty_body, "suggest braces around "
9235 "empty body in an %<else%> statement");
9236 add_stmt (build_empty_stmt (loc));
9237 cp_lexer_consume_token (parser->lexer);
9239 else
9240 cp_parser_implicitly_scoped_statement (parser, NULL);
9242 finish_else_clause (statement);
9244 /* If we are currently parsing a then-clause, then
9245 IF_P will not be NULL. We set it to true to
9246 indicate that this if statement has an else clause.
9247 This may trigger the Wparentheses warning below
9248 when we get back up to the parent if statement. */
9249 if (if_p != NULL)
9250 *if_p = true;
9252 else
9254 /* This if statement does not have an else clause. If
9255 NESTED_IF is true, then the then-clause is an if
9256 statement which does have an else clause. We warn
9257 about the potential ambiguity. */
9258 if (nested_if)
9259 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9260 "suggest explicit braces to avoid ambiguous"
9261 " %<else%>");
9264 /* Now we're all done with the if-statement. */
9265 finish_if_stmt (statement);
9267 else
9269 bool in_switch_statement_p;
9270 unsigned char in_statement;
9272 /* Add the condition. */
9273 finish_switch_cond (condition, statement);
9275 /* Parse the body of the switch-statement. */
9276 in_switch_statement_p = parser->in_switch_statement_p;
9277 in_statement = parser->in_statement;
9278 parser->in_switch_statement_p = true;
9279 parser->in_statement |= IN_SWITCH_STMT;
9280 cp_parser_implicitly_scoped_statement (parser, NULL);
9281 parser->in_switch_statement_p = in_switch_statement_p;
9282 parser->in_statement = in_statement;
9284 /* Now we're all done with the switch-statement. */
9285 finish_switch_stmt (statement);
9288 return statement;
9290 break;
9292 default:
9293 cp_parser_error (parser, "expected selection-statement");
9294 return error_mark_node;
9298 /* Parse a condition.
9300 condition:
9301 expression
9302 type-specifier-seq declarator = initializer-clause
9303 type-specifier-seq declarator braced-init-list
9305 GNU Extension:
9307 condition:
9308 type-specifier-seq declarator asm-specification [opt]
9309 attributes [opt] = assignment-expression
9311 Returns the expression that should be tested. */
9313 static tree
9314 cp_parser_condition (cp_parser* parser)
9316 cp_decl_specifier_seq type_specifiers;
9317 const char *saved_message;
9318 int declares_class_or_enum;
9320 /* Try the declaration first. */
9321 cp_parser_parse_tentatively (parser);
9322 /* New types are not allowed in the type-specifier-seq for a
9323 condition. */
9324 saved_message = parser->type_definition_forbidden_message;
9325 parser->type_definition_forbidden_message
9326 = G_("types may not be defined in conditions");
9327 /* Parse the type-specifier-seq. */
9328 cp_parser_decl_specifier_seq (parser,
9329 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9330 &type_specifiers,
9331 &declares_class_or_enum);
9332 /* Restore the saved message. */
9333 parser->type_definition_forbidden_message = saved_message;
9334 /* If all is well, we might be looking at a declaration. */
9335 if (!cp_parser_error_occurred (parser))
9337 tree decl;
9338 tree asm_specification;
9339 tree attributes;
9340 cp_declarator *declarator;
9341 tree initializer = NULL_TREE;
9343 /* Parse the declarator. */
9344 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9345 /*ctor_dtor_or_conv_p=*/NULL,
9346 /*parenthesized_p=*/NULL,
9347 /*member_p=*/false);
9348 /* Parse the attributes. */
9349 attributes = cp_parser_attributes_opt (parser);
9350 /* Parse the asm-specification. */
9351 asm_specification = cp_parser_asm_specification_opt (parser);
9352 /* If the next token is not an `=' or '{', then we might still be
9353 looking at an expression. For example:
9355 if (A(a).x)
9357 looks like a decl-specifier-seq and a declarator -- but then
9358 there is no `=', so this is an expression. */
9359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9360 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9361 cp_parser_simulate_error (parser);
9363 /* If we did see an `=' or '{', then we are looking at a declaration
9364 for sure. */
9365 if (cp_parser_parse_definitely (parser))
9367 tree pushed_scope;
9368 bool non_constant_p;
9369 bool flags = LOOKUP_ONLYCONVERTING;
9371 /* Create the declaration. */
9372 decl = start_decl (declarator, &type_specifiers,
9373 /*initialized_p=*/true,
9374 attributes, /*prefix_attributes=*/NULL_TREE,
9375 &pushed_scope);
9377 /* Parse the initializer. */
9378 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9380 initializer = cp_parser_braced_list (parser, &non_constant_p);
9381 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9382 flags = 0;
9384 else
9386 /* Consume the `='. */
9387 cp_parser_require (parser, CPP_EQ, RT_EQ);
9388 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9390 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9391 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9393 /* Process the initializer. */
9394 cp_finish_decl (decl,
9395 initializer, !non_constant_p,
9396 asm_specification,
9397 flags);
9399 if (pushed_scope)
9400 pop_scope (pushed_scope);
9402 return convert_from_reference (decl);
9405 /* If we didn't even get past the declarator successfully, we are
9406 definitely not looking at a declaration. */
9407 else
9408 cp_parser_abort_tentative_parse (parser);
9410 /* Otherwise, we are looking at an expression. */
9411 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9414 /* Parses a for-statement or range-for-statement until the closing ')',
9415 not included. */
9417 static tree
9418 cp_parser_for (cp_parser *parser)
9420 tree init, scope, decl;
9421 bool is_range_for;
9423 /* Begin the for-statement. */
9424 scope = begin_for_scope (&init);
9426 /* Parse the initialization. */
9427 is_range_for = cp_parser_for_init_statement (parser, &decl);
9429 if (is_range_for)
9430 return cp_parser_range_for (parser, scope, init, decl);
9431 else
9432 return cp_parser_c_for (parser, scope, init);
9435 static tree
9436 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9438 /* Normal for loop */
9439 tree condition = NULL_TREE;
9440 tree expression = NULL_TREE;
9441 tree stmt;
9443 stmt = begin_for_stmt (scope, init);
9444 /* The for-init-statement has already been parsed in
9445 cp_parser_for_init_statement, so no work is needed here. */
9446 finish_for_init_stmt (stmt);
9448 /* If there's a condition, process it. */
9449 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9450 condition = cp_parser_condition (parser);
9451 finish_for_cond (condition, stmt);
9452 /* Look for the `;'. */
9453 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9455 /* If there's an expression, process it. */
9456 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9457 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9458 finish_for_expr (expression, stmt);
9460 return stmt;
9463 /* Tries to parse a range-based for-statement:
9465 range-based-for:
9466 decl-specifier-seq declarator : expression
9468 The decl-specifier-seq declarator and the `:' are already parsed by
9469 cp_parser_for_init_statement. If processing_template_decl it returns a
9470 newly created RANGE_FOR_STMT; if not, it is converted to a
9471 regular FOR_STMT. */
9473 static tree
9474 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9476 tree stmt, range_expr;
9478 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9480 bool expr_non_constant_p;
9481 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9483 else
9484 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9486 /* If in template, STMT is converted to a normal for-statement
9487 at instantiation. If not, it is done just ahead. */
9488 if (processing_template_decl)
9490 if (check_for_bare_parameter_packs (range_expr))
9491 range_expr = error_mark_node;
9492 stmt = begin_range_for_stmt (scope, init);
9493 finish_range_for_decl (stmt, range_decl, range_expr);
9494 if (!type_dependent_expression_p (range_expr)
9495 /* do_auto_deduction doesn't mess with template init-lists. */
9496 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9497 do_range_for_auto_deduction (range_decl, range_expr);
9499 else
9501 stmt = begin_for_stmt (scope, init);
9502 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9504 return stmt;
9507 /* Subroutine of cp_convert_range_for: given the initializer expression,
9508 builds up the range temporary. */
9510 static tree
9511 build_range_temp (tree range_expr)
9513 tree range_type, range_temp;
9515 /* Find out the type deduced by the declaration
9516 `auto &&__range = range_expr'. */
9517 range_type = cp_build_reference_type (make_auto (), true);
9518 range_type = do_auto_deduction (range_type, range_expr,
9519 type_uses_auto (range_type));
9521 /* Create the __range variable. */
9522 range_temp = build_decl (input_location, VAR_DECL,
9523 get_identifier ("__for_range"), range_type);
9524 TREE_USED (range_temp) = 1;
9525 DECL_ARTIFICIAL (range_temp) = 1;
9527 return range_temp;
9530 /* Used by cp_parser_range_for in template context: we aren't going to
9531 do a full conversion yet, but we still need to resolve auto in the
9532 type of the for-range-declaration if present. This is basically
9533 a shortcut version of cp_convert_range_for. */
9535 static void
9536 do_range_for_auto_deduction (tree decl, tree range_expr)
9538 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9539 if (auto_node)
9541 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9542 range_temp = convert_from_reference (build_range_temp (range_expr));
9543 iter_type = (cp_parser_perform_range_for_lookup
9544 (range_temp, &begin_dummy, &end_dummy));
9545 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9546 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9547 tf_warning_or_error);
9548 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9549 iter_decl, auto_node);
9553 /* Converts a range-based for-statement into a normal
9554 for-statement, as per the definition.
9556 for (RANGE_DECL : RANGE_EXPR)
9557 BLOCK
9559 should be equivalent to:
9562 auto &&__range = RANGE_EXPR;
9563 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9564 __begin != __end;
9565 ++__begin)
9567 RANGE_DECL = *__begin;
9568 BLOCK
9572 If RANGE_EXPR is an array:
9573 BEGIN_EXPR = __range
9574 END_EXPR = __range + ARRAY_SIZE(__range)
9575 Else if RANGE_EXPR has a member 'begin' or 'end':
9576 BEGIN_EXPR = __range.begin()
9577 END_EXPR = __range.end()
9578 Else:
9579 BEGIN_EXPR = begin(__range)
9580 END_EXPR = end(__range);
9582 If __range has a member 'begin' but not 'end', or vice versa, we must
9583 still use the second alternative (it will surely fail, however).
9584 When calling begin()/end() in the third alternative we must use
9585 argument dependent lookup, but always considering 'std' as an associated
9586 namespace. */
9588 tree
9589 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9591 tree begin, end;
9592 tree iter_type, begin_expr, end_expr;
9593 tree condition, expression;
9595 if (range_decl == error_mark_node || range_expr == error_mark_node)
9596 /* If an error happened previously do nothing or else a lot of
9597 unhelpful errors would be issued. */
9598 begin_expr = end_expr = iter_type = error_mark_node;
9599 else
9601 tree range_temp = build_range_temp (range_expr);
9602 pushdecl (range_temp);
9603 cp_finish_decl (range_temp, range_expr,
9604 /*is_constant_init*/false, NULL_TREE,
9605 LOOKUP_ONLYCONVERTING);
9607 range_temp = convert_from_reference (range_temp);
9608 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9609 &begin_expr, &end_expr);
9612 /* The new for initialization statement. */
9613 begin = build_decl (input_location, VAR_DECL,
9614 get_identifier ("__for_begin"), iter_type);
9615 TREE_USED (begin) = 1;
9616 DECL_ARTIFICIAL (begin) = 1;
9617 pushdecl (begin);
9618 cp_finish_decl (begin, begin_expr,
9619 /*is_constant_init*/false, NULL_TREE,
9620 LOOKUP_ONLYCONVERTING);
9622 end = build_decl (input_location, VAR_DECL,
9623 get_identifier ("__for_end"), iter_type);
9624 TREE_USED (end) = 1;
9625 DECL_ARTIFICIAL (end) = 1;
9626 pushdecl (end);
9627 cp_finish_decl (end, end_expr,
9628 /*is_constant_init*/false, NULL_TREE,
9629 LOOKUP_ONLYCONVERTING);
9631 finish_for_init_stmt (statement);
9633 /* The new for condition. */
9634 condition = build_x_binary_op (input_location, NE_EXPR,
9635 begin, ERROR_MARK,
9636 end, ERROR_MARK,
9637 NULL, tf_warning_or_error);
9638 finish_for_cond (condition, statement);
9640 /* The new increment expression. */
9641 expression = finish_unary_op_expr (input_location,
9642 PREINCREMENT_EXPR, begin);
9643 finish_for_expr (expression, statement);
9645 /* The declaration is initialized with *__begin inside the loop body. */
9646 cp_finish_decl (range_decl,
9647 build_x_indirect_ref (input_location, begin, RO_NULL,
9648 tf_warning_or_error),
9649 /*is_constant_init*/false, NULL_TREE,
9650 LOOKUP_ONLYCONVERTING);
9652 return statement;
9655 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9656 We need to solve both at the same time because the method used
9657 depends on the existence of members begin or end.
9658 Returns the type deduced for the iterator expression. */
9660 static tree
9661 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9663 if (error_operand_p (range))
9665 *begin = *end = error_mark_node;
9666 return error_mark_node;
9669 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9671 error ("range-based %<for%> expression of type %qT "
9672 "has incomplete type", TREE_TYPE (range));
9673 *begin = *end = error_mark_node;
9674 return error_mark_node;
9676 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9678 /* If RANGE is an array, we will use pointer arithmetic. */
9679 *begin = range;
9680 *end = build_binary_op (input_location, PLUS_EXPR,
9681 range,
9682 array_type_nelts_top (TREE_TYPE (range)),
9684 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9686 else
9688 /* If it is not an array, we must do a bit of magic. */
9689 tree id_begin, id_end;
9690 tree member_begin, member_end;
9692 *begin = *end = error_mark_node;
9694 id_begin = get_identifier ("begin");
9695 id_end = get_identifier ("end");
9696 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9697 /*protect=*/2, /*want_type=*/false,
9698 tf_warning_or_error);
9699 member_end = lookup_member (TREE_TYPE (range), id_end,
9700 /*protect=*/2, /*want_type=*/false,
9701 tf_warning_or_error);
9703 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9705 /* Use the member functions. */
9706 if (member_begin != NULL_TREE)
9707 *begin = cp_parser_range_for_member_function (range, id_begin);
9708 else
9709 error ("range-based %<for%> expression of type %qT has an "
9710 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9712 if (member_end != NULL_TREE)
9713 *end = cp_parser_range_for_member_function (range, id_end);
9714 else
9715 error ("range-based %<for%> expression of type %qT has a "
9716 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9718 else
9720 /* Use global functions with ADL. */
9721 vec<tree, va_gc> *vec;
9722 vec = make_tree_vector ();
9724 vec_safe_push (vec, range);
9726 member_begin = perform_koenig_lookup (id_begin, vec,
9727 /*include_std=*/true,
9728 tf_warning_or_error);
9729 *begin = finish_call_expr (member_begin, &vec, false, true,
9730 tf_warning_or_error);
9731 member_end = perform_koenig_lookup (id_end, vec,
9732 /*include_std=*/true,
9733 tf_warning_or_error);
9734 *end = finish_call_expr (member_end, &vec, false, true,
9735 tf_warning_or_error);
9737 release_tree_vector (vec);
9740 /* Last common checks. */
9741 if (*begin == error_mark_node || *end == error_mark_node)
9743 /* If one of the expressions is an error do no more checks. */
9744 *begin = *end = error_mark_node;
9745 return error_mark_node;
9747 else
9749 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9750 /* The unqualified type of the __begin and __end temporaries should
9751 be the same, as required by the multiple auto declaration. */
9752 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9753 error ("inconsistent begin/end types in range-based %<for%> "
9754 "statement: %qT and %qT",
9755 TREE_TYPE (*begin), TREE_TYPE (*end));
9756 return iter_type;
9761 /* Helper function for cp_parser_perform_range_for_lookup.
9762 Builds a tree for RANGE.IDENTIFIER(). */
9764 static tree
9765 cp_parser_range_for_member_function (tree range, tree identifier)
9767 tree member, res;
9768 vec<tree, va_gc> *vec;
9770 member = finish_class_member_access_expr (range, identifier,
9771 false, tf_warning_or_error);
9772 if (member == error_mark_node)
9773 return error_mark_node;
9775 vec = make_tree_vector ();
9776 res = finish_call_expr (member, &vec,
9777 /*disallow_virtual=*/false,
9778 /*koenig_p=*/false,
9779 tf_warning_or_error);
9780 release_tree_vector (vec);
9781 return res;
9784 /* Parse an iteration-statement.
9786 iteration-statement:
9787 while ( condition ) statement
9788 do statement while ( expression ) ;
9789 for ( for-init-statement condition [opt] ; expression [opt] )
9790 statement
9792 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9794 static tree
9795 cp_parser_iteration_statement (cp_parser* parser)
9797 cp_token *token;
9798 enum rid keyword;
9799 tree statement;
9800 unsigned char in_statement;
9802 /* Peek at the next token. */
9803 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9804 if (!token)
9805 return error_mark_node;
9807 /* Remember whether or not we are already within an iteration
9808 statement. */
9809 in_statement = parser->in_statement;
9811 /* See what kind of keyword it is. */
9812 keyword = token->keyword;
9813 switch (keyword)
9815 case RID_WHILE:
9817 tree condition;
9819 /* Begin the while-statement. */
9820 statement = begin_while_stmt ();
9821 /* Look for the `('. */
9822 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9823 /* Parse the condition. */
9824 condition = cp_parser_condition (parser);
9825 finish_while_stmt_cond (condition, statement);
9826 /* Look for the `)'. */
9827 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9828 /* Parse the dependent statement. */
9829 parser->in_statement = IN_ITERATION_STMT;
9830 cp_parser_already_scoped_statement (parser);
9831 parser->in_statement = in_statement;
9832 /* We're done with the while-statement. */
9833 finish_while_stmt (statement);
9835 break;
9837 case RID_DO:
9839 tree expression;
9841 /* Begin the do-statement. */
9842 statement = begin_do_stmt ();
9843 /* Parse the body of the do-statement. */
9844 parser->in_statement = IN_ITERATION_STMT;
9845 cp_parser_implicitly_scoped_statement (parser, NULL);
9846 parser->in_statement = in_statement;
9847 finish_do_body (statement);
9848 /* Look for the `while' keyword. */
9849 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9850 /* Look for the `('. */
9851 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9852 /* Parse the expression. */
9853 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9854 /* We're done with the do-statement. */
9855 finish_do_stmt (expression, statement);
9856 /* Look for the `)'. */
9857 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9858 /* Look for the `;'. */
9859 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9861 break;
9863 case RID_FOR:
9865 /* Look for the `('. */
9866 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9868 statement = cp_parser_for (parser);
9870 /* Look for the `)'. */
9871 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9873 /* Parse the body of the for-statement. */
9874 parser->in_statement = IN_ITERATION_STMT;
9875 cp_parser_already_scoped_statement (parser);
9876 parser->in_statement = in_statement;
9878 /* We're done with the for-statement. */
9879 finish_for_stmt (statement);
9881 break;
9883 default:
9884 cp_parser_error (parser, "expected iteration-statement");
9885 statement = error_mark_node;
9886 break;
9889 return statement;
9892 /* Parse a for-init-statement or the declarator of a range-based-for.
9893 Returns true if a range-based-for declaration is seen.
9895 for-init-statement:
9896 expression-statement
9897 simple-declaration */
9899 static bool
9900 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9902 /* If the next token is a `;', then we have an empty
9903 expression-statement. Grammatically, this is also a
9904 simple-declaration, but an invalid one, because it does not
9905 declare anything. Therefore, if we did not handle this case
9906 specially, we would issue an error message about an invalid
9907 declaration. */
9908 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9910 bool is_range_for = false;
9911 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9913 parser->colon_corrects_to_scope_p = false;
9915 /* We're going to speculatively look for a declaration, falling back
9916 to an expression, if necessary. */
9917 cp_parser_parse_tentatively (parser);
9918 /* Parse the declaration. */
9919 cp_parser_simple_declaration (parser,
9920 /*function_definition_allowed_p=*/false,
9921 decl);
9922 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9923 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9925 /* It is a range-for, consume the ':' */
9926 cp_lexer_consume_token (parser->lexer);
9927 is_range_for = true;
9928 if (cxx_dialect < cxx0x)
9930 error_at (cp_lexer_peek_token (parser->lexer)->location,
9931 "range-based %<for%> loops are not allowed "
9932 "in C++98 mode");
9933 *decl = error_mark_node;
9936 else
9937 /* The ';' is not consumed yet because we told
9938 cp_parser_simple_declaration not to. */
9939 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9941 if (cp_parser_parse_definitely (parser))
9942 return is_range_for;
9943 /* If the tentative parse failed, then we shall need to look for an
9944 expression-statement. */
9946 /* If we are here, it is an expression-statement. */
9947 cp_parser_expression_statement (parser, NULL_TREE);
9948 return false;
9951 /* Parse a jump-statement.
9953 jump-statement:
9954 break ;
9955 continue ;
9956 return expression [opt] ;
9957 return braced-init-list ;
9958 goto identifier ;
9960 GNU extension:
9962 jump-statement:
9963 goto * expression ;
9965 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9967 static tree
9968 cp_parser_jump_statement (cp_parser* parser)
9970 tree statement = error_mark_node;
9971 cp_token *token;
9972 enum rid keyword;
9973 unsigned char in_statement;
9975 /* Peek at the next token. */
9976 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9977 if (!token)
9978 return error_mark_node;
9980 /* See what kind of keyword it is. */
9981 keyword = token->keyword;
9982 switch (keyword)
9984 case RID_BREAK:
9985 in_statement = parser->in_statement & ~IN_IF_STMT;
9986 switch (in_statement)
9988 case 0:
9989 error_at (token->location, "break statement not within loop or switch");
9990 break;
9991 default:
9992 gcc_assert ((in_statement & IN_SWITCH_STMT)
9993 || in_statement == IN_ITERATION_STMT);
9994 statement = finish_break_stmt ();
9995 break;
9996 case IN_OMP_BLOCK:
9997 error_at (token->location, "invalid exit from OpenMP structured block");
9998 break;
9999 case IN_OMP_FOR:
10000 error_at (token->location, "break statement used with OpenMP for loop");
10001 break;
10003 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10004 break;
10006 case RID_CONTINUE:
10007 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10009 case 0:
10010 error_at (token->location, "continue statement not within a loop");
10011 break;
10012 case IN_ITERATION_STMT:
10013 case IN_OMP_FOR:
10014 statement = finish_continue_stmt ();
10015 break;
10016 case IN_OMP_BLOCK:
10017 error_at (token->location, "invalid exit from OpenMP structured block");
10018 break;
10019 default:
10020 gcc_unreachable ();
10022 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10023 break;
10025 case RID_RETURN:
10027 tree expr;
10028 bool expr_non_constant_p;
10030 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10032 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10033 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10035 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10036 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10037 else
10038 /* If the next token is a `;', then there is no
10039 expression. */
10040 expr = NULL_TREE;
10041 /* Build the return-statement. */
10042 statement = finish_return_stmt (expr);
10043 /* Look for the final `;'. */
10044 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10046 break;
10048 case RID_GOTO:
10049 /* Create the goto-statement. */
10050 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10052 /* Issue a warning about this use of a GNU extension. */
10053 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10054 /* Consume the '*' token. */
10055 cp_lexer_consume_token (parser->lexer);
10056 /* Parse the dependent expression. */
10057 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10059 else
10060 finish_goto_stmt (cp_parser_identifier (parser));
10061 /* Look for the final `;'. */
10062 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10063 break;
10065 default:
10066 cp_parser_error (parser, "expected jump-statement");
10067 break;
10070 return statement;
10073 /* Parse a declaration-statement.
10075 declaration-statement:
10076 block-declaration */
10078 static void
10079 cp_parser_declaration_statement (cp_parser* parser)
10081 void *p;
10083 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10084 p = obstack_alloc (&declarator_obstack, 0);
10086 /* Parse the block-declaration. */
10087 cp_parser_block_declaration (parser, /*statement_p=*/true);
10089 /* Free any declarators allocated. */
10090 obstack_free (&declarator_obstack, p);
10092 /* Finish off the statement. */
10093 finish_stmt ();
10096 /* Some dependent statements (like `if (cond) statement'), are
10097 implicitly in their own scope. In other words, if the statement is
10098 a single statement (as opposed to a compound-statement), it is
10099 none-the-less treated as if it were enclosed in braces. Any
10100 declarations appearing in the dependent statement are out of scope
10101 after control passes that point. This function parses a statement,
10102 but ensures that is in its own scope, even if it is not a
10103 compound-statement.
10105 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10106 is a (possibly labeled) if statement which is not enclosed in
10107 braces and has an else clause. This is used to implement
10108 -Wparentheses.
10110 Returns the new statement. */
10112 static tree
10113 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10115 tree statement;
10117 if (if_p != NULL)
10118 *if_p = false;
10120 /* Mark if () ; with a special NOP_EXPR. */
10121 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10123 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10124 cp_lexer_consume_token (parser->lexer);
10125 statement = add_stmt (build_empty_stmt (loc));
10127 /* if a compound is opened, we simply parse the statement directly. */
10128 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10129 statement = cp_parser_compound_statement (parser, NULL, false, false);
10130 /* If the token is not a `{', then we must take special action. */
10131 else
10133 /* Create a compound-statement. */
10134 statement = begin_compound_stmt (0);
10135 /* Parse the dependent-statement. */
10136 cp_parser_statement (parser, NULL_TREE, false, if_p);
10137 /* Finish the dummy compound-statement. */
10138 finish_compound_stmt (statement);
10141 /* Return the statement. */
10142 return statement;
10145 /* For some dependent statements (like `while (cond) statement'), we
10146 have already created a scope. Therefore, even if the dependent
10147 statement is a compound-statement, we do not want to create another
10148 scope. */
10150 static void
10151 cp_parser_already_scoped_statement (cp_parser* parser)
10153 /* If the token is a `{', then we must take special action. */
10154 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10155 cp_parser_statement (parser, NULL_TREE, false, NULL);
10156 else
10158 /* Avoid calling cp_parser_compound_statement, so that we
10159 don't create a new scope. Do everything else by hand. */
10160 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10161 /* If the next keyword is `__label__' we have a label declaration. */
10162 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10163 cp_parser_label_declaration (parser);
10164 /* Parse an (optional) statement-seq. */
10165 cp_parser_statement_seq_opt (parser, NULL_TREE);
10166 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10170 /* Declarations [gram.dcl.dcl] */
10172 /* Parse an optional declaration-sequence.
10174 declaration-seq:
10175 declaration
10176 declaration-seq declaration */
10178 static void
10179 cp_parser_declaration_seq_opt (cp_parser* parser)
10181 while (true)
10183 cp_token *token;
10185 token = cp_lexer_peek_token (parser->lexer);
10187 if (token->type == CPP_CLOSE_BRACE
10188 || token->type == CPP_EOF
10189 || token->type == CPP_PRAGMA_EOL)
10190 break;
10192 if (token->type == CPP_SEMICOLON)
10194 /* A declaration consisting of a single semicolon is
10195 invalid. Allow it unless we're being pedantic. */
10196 cp_lexer_consume_token (parser->lexer);
10197 if (!in_system_header)
10198 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10199 continue;
10202 /* If we're entering or exiting a region that's implicitly
10203 extern "C", modify the lang context appropriately. */
10204 if (!parser->implicit_extern_c && token->implicit_extern_c)
10206 push_lang_context (lang_name_c);
10207 parser->implicit_extern_c = true;
10209 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10211 pop_lang_context ();
10212 parser->implicit_extern_c = false;
10215 if (token->type == CPP_PRAGMA)
10217 /* A top-level declaration can consist solely of a #pragma.
10218 A nested declaration cannot, so this is done here and not
10219 in cp_parser_declaration. (A #pragma at block scope is
10220 handled in cp_parser_statement.) */
10221 cp_parser_pragma (parser, pragma_external);
10222 continue;
10225 /* Parse the declaration itself. */
10226 cp_parser_declaration (parser);
10230 /* Parse a declaration.
10232 declaration:
10233 block-declaration
10234 function-definition
10235 template-declaration
10236 explicit-instantiation
10237 explicit-specialization
10238 linkage-specification
10239 namespace-definition
10241 GNU extension:
10243 declaration:
10244 __extension__ declaration */
10246 static void
10247 cp_parser_declaration (cp_parser* parser)
10249 cp_token token1;
10250 cp_token token2;
10251 int saved_pedantic;
10252 void *p;
10253 tree attributes = NULL_TREE;
10255 /* Check for the `__extension__' keyword. */
10256 if (cp_parser_extension_opt (parser, &saved_pedantic))
10258 /* Parse the qualified declaration. */
10259 cp_parser_declaration (parser);
10260 /* Restore the PEDANTIC flag. */
10261 pedantic = saved_pedantic;
10263 return;
10266 /* Try to figure out what kind of declaration is present. */
10267 token1 = *cp_lexer_peek_token (parser->lexer);
10269 if (token1.type != CPP_EOF)
10270 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10271 else
10273 token2.type = CPP_EOF;
10274 token2.keyword = RID_MAX;
10277 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10278 p = obstack_alloc (&declarator_obstack, 0);
10280 /* If the next token is `extern' and the following token is a string
10281 literal, then we have a linkage specification. */
10282 if (token1.keyword == RID_EXTERN
10283 && cp_parser_is_pure_string_literal (&token2))
10284 cp_parser_linkage_specification (parser);
10285 /* If the next token is `template', then we have either a template
10286 declaration, an explicit instantiation, or an explicit
10287 specialization. */
10288 else if (token1.keyword == RID_TEMPLATE)
10290 /* `template <>' indicates a template specialization. */
10291 if (token2.type == CPP_LESS
10292 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10293 cp_parser_explicit_specialization (parser);
10294 /* `template <' indicates a template declaration. */
10295 else if (token2.type == CPP_LESS)
10296 cp_parser_template_declaration (parser, /*member_p=*/false);
10297 /* Anything else must be an explicit instantiation. */
10298 else
10299 cp_parser_explicit_instantiation (parser);
10301 /* If the next token is `export', then we have a template
10302 declaration. */
10303 else if (token1.keyword == RID_EXPORT)
10304 cp_parser_template_declaration (parser, /*member_p=*/false);
10305 /* If the next token is `extern', 'static' or 'inline' and the one
10306 after that is `template', we have a GNU extended explicit
10307 instantiation directive. */
10308 else if (cp_parser_allow_gnu_extensions_p (parser)
10309 && (token1.keyword == RID_EXTERN
10310 || token1.keyword == RID_STATIC
10311 || token1.keyword == RID_INLINE)
10312 && token2.keyword == RID_TEMPLATE)
10313 cp_parser_explicit_instantiation (parser);
10314 /* If the next token is `namespace', check for a named or unnamed
10315 namespace definition. */
10316 else if (token1.keyword == RID_NAMESPACE
10317 && (/* A named namespace definition. */
10318 (token2.type == CPP_NAME
10319 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10320 != CPP_EQ))
10321 /* An unnamed namespace definition. */
10322 || token2.type == CPP_OPEN_BRACE
10323 || token2.keyword == RID_ATTRIBUTE))
10324 cp_parser_namespace_definition (parser);
10325 /* An inline (associated) namespace definition. */
10326 else if (token1.keyword == RID_INLINE
10327 && token2.keyword == RID_NAMESPACE)
10328 cp_parser_namespace_definition (parser);
10329 /* Objective-C++ declaration/definition. */
10330 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10331 cp_parser_objc_declaration (parser, NULL_TREE);
10332 else if (c_dialect_objc ()
10333 && token1.keyword == RID_ATTRIBUTE
10334 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10335 cp_parser_objc_declaration (parser, attributes);
10336 /* We must have either a block declaration or a function
10337 definition. */
10338 else
10339 /* Try to parse a block-declaration, or a function-definition. */
10340 cp_parser_block_declaration (parser, /*statement_p=*/false);
10342 /* Free any declarators allocated. */
10343 obstack_free (&declarator_obstack, p);
10346 /* Parse a block-declaration.
10348 block-declaration:
10349 simple-declaration
10350 asm-definition
10351 namespace-alias-definition
10352 using-declaration
10353 using-directive
10355 GNU Extension:
10357 block-declaration:
10358 __extension__ block-declaration
10360 C++0x Extension:
10362 block-declaration:
10363 static_assert-declaration
10365 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10366 part of a declaration-statement. */
10368 static void
10369 cp_parser_block_declaration (cp_parser *parser,
10370 bool statement_p)
10372 cp_token *token1;
10373 int saved_pedantic;
10375 /* Check for the `__extension__' keyword. */
10376 if (cp_parser_extension_opt (parser, &saved_pedantic))
10378 /* Parse the qualified declaration. */
10379 cp_parser_block_declaration (parser, statement_p);
10380 /* Restore the PEDANTIC flag. */
10381 pedantic = saved_pedantic;
10383 return;
10386 /* Peek at the next token to figure out which kind of declaration is
10387 present. */
10388 token1 = cp_lexer_peek_token (parser->lexer);
10390 /* If the next keyword is `asm', we have an asm-definition. */
10391 if (token1->keyword == RID_ASM)
10393 if (statement_p)
10394 cp_parser_commit_to_tentative_parse (parser);
10395 cp_parser_asm_definition (parser);
10397 /* If the next keyword is `namespace', we have a
10398 namespace-alias-definition. */
10399 else if (token1->keyword == RID_NAMESPACE)
10400 cp_parser_namespace_alias_definition (parser);
10401 /* If the next keyword is `using', we have a
10402 using-declaration, a using-directive, or an alias-declaration. */
10403 else if (token1->keyword == RID_USING)
10405 cp_token *token2;
10407 if (statement_p)
10408 cp_parser_commit_to_tentative_parse (parser);
10409 /* If the token after `using' is `namespace', then we have a
10410 using-directive. */
10411 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10412 if (token2->keyword == RID_NAMESPACE)
10413 cp_parser_using_directive (parser);
10414 /* If the second token after 'using' is '=', then we have an
10415 alias-declaration. */
10416 else if (cxx_dialect >= cxx0x
10417 && token2->type == CPP_NAME
10418 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10419 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
10420 cp_parser_alias_declaration (parser);
10421 /* Otherwise, it's a using-declaration. */
10422 else
10423 cp_parser_using_declaration (parser,
10424 /*access_declaration_p=*/false);
10426 /* If the next keyword is `__label__' we have a misplaced label
10427 declaration. */
10428 else if (token1->keyword == RID_LABEL)
10430 cp_lexer_consume_token (parser->lexer);
10431 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10432 cp_parser_skip_to_end_of_statement (parser);
10433 /* If the next token is now a `;', consume it. */
10434 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10435 cp_lexer_consume_token (parser->lexer);
10437 /* If the next token is `static_assert' we have a static assertion. */
10438 else if (token1->keyword == RID_STATIC_ASSERT)
10439 cp_parser_static_assert (parser, /*member_p=*/false);
10440 /* Anything else must be a simple-declaration. */
10441 else
10442 cp_parser_simple_declaration (parser, !statement_p,
10443 /*maybe_range_for_decl*/NULL);
10446 /* Parse a simple-declaration.
10448 simple-declaration:
10449 decl-specifier-seq [opt] init-declarator-list [opt] ;
10451 init-declarator-list:
10452 init-declarator
10453 init-declarator-list , init-declarator
10455 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10456 function-definition as a simple-declaration.
10458 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10459 parsed declaration if it is an uninitialized single declarator not followed
10460 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10461 if present, will not be consumed. */
10463 static void
10464 cp_parser_simple_declaration (cp_parser* parser,
10465 bool function_definition_allowed_p,
10466 tree *maybe_range_for_decl)
10468 cp_decl_specifier_seq decl_specifiers;
10469 int declares_class_or_enum;
10470 bool saw_declarator;
10472 if (maybe_range_for_decl)
10473 *maybe_range_for_decl = NULL_TREE;
10475 /* Defer access checks until we know what is being declared; the
10476 checks for names appearing in the decl-specifier-seq should be
10477 done as if we were in the scope of the thing being declared. */
10478 push_deferring_access_checks (dk_deferred);
10480 /* Parse the decl-specifier-seq. We have to keep track of whether
10481 or not the decl-specifier-seq declares a named class or
10482 enumeration type, since that is the only case in which the
10483 init-declarator-list is allowed to be empty.
10485 [dcl.dcl]
10487 In a simple-declaration, the optional init-declarator-list can be
10488 omitted only when declaring a class or enumeration, that is when
10489 the decl-specifier-seq contains either a class-specifier, an
10490 elaborated-type-specifier, or an enum-specifier. */
10491 cp_parser_decl_specifier_seq (parser,
10492 CP_PARSER_FLAGS_OPTIONAL,
10493 &decl_specifiers,
10494 &declares_class_or_enum);
10495 /* We no longer need to defer access checks. */
10496 stop_deferring_access_checks ();
10498 /* In a block scope, a valid declaration must always have a
10499 decl-specifier-seq. By not trying to parse declarators, we can
10500 resolve the declaration/expression ambiguity more quickly. */
10501 if (!function_definition_allowed_p
10502 && !decl_specifiers.any_specifiers_p)
10504 cp_parser_error (parser, "expected declaration");
10505 goto done;
10508 /* If the next two tokens are both identifiers, the code is
10509 erroneous. The usual cause of this situation is code like:
10511 T t;
10513 where "T" should name a type -- but does not. */
10514 if (!decl_specifiers.any_type_specifiers_p
10515 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10517 /* If parsing tentatively, we should commit; we really are
10518 looking at a declaration. */
10519 cp_parser_commit_to_tentative_parse (parser);
10520 /* Give up. */
10521 goto done;
10524 /* If we have seen at least one decl-specifier, and the next token
10525 is not a parenthesis, then we must be looking at a declaration.
10526 (After "int (" we might be looking at a functional cast.) */
10527 if (decl_specifiers.any_specifiers_p
10528 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10529 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10530 && !cp_parser_error_occurred (parser))
10531 cp_parser_commit_to_tentative_parse (parser);
10533 /* Keep going until we hit the `;' at the end of the simple
10534 declaration. */
10535 saw_declarator = false;
10536 while (cp_lexer_next_token_is_not (parser->lexer,
10537 CPP_SEMICOLON))
10539 cp_token *token;
10540 bool function_definition_p;
10541 tree decl;
10543 if (saw_declarator)
10545 /* If we are processing next declarator, coma is expected */
10546 token = cp_lexer_peek_token (parser->lexer);
10547 gcc_assert (token->type == CPP_COMMA);
10548 cp_lexer_consume_token (parser->lexer);
10549 if (maybe_range_for_decl)
10550 *maybe_range_for_decl = error_mark_node;
10552 else
10553 saw_declarator = true;
10555 /* Parse the init-declarator. */
10556 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10557 /*checks=*/NULL,
10558 function_definition_allowed_p,
10559 /*member_p=*/false,
10560 declares_class_or_enum,
10561 &function_definition_p,
10562 maybe_range_for_decl);
10563 /* If an error occurred while parsing tentatively, exit quickly.
10564 (That usually happens when in the body of a function; each
10565 statement is treated as a declaration-statement until proven
10566 otherwise.) */
10567 if (cp_parser_error_occurred (parser))
10568 goto done;
10569 /* Handle function definitions specially. */
10570 if (function_definition_p)
10572 /* If the next token is a `,', then we are probably
10573 processing something like:
10575 void f() {}, *p;
10577 which is erroneous. */
10578 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10580 cp_token *token = cp_lexer_peek_token (parser->lexer);
10581 error_at (token->location,
10582 "mixing"
10583 " declarations and function-definitions is forbidden");
10585 /* Otherwise, we're done with the list of declarators. */
10586 else
10588 pop_deferring_access_checks ();
10589 return;
10592 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10593 *maybe_range_for_decl = decl;
10594 /* The next token should be either a `,' or a `;'. */
10595 token = cp_lexer_peek_token (parser->lexer);
10596 /* If it's a `,', there are more declarators to come. */
10597 if (token->type == CPP_COMMA)
10598 /* will be consumed next time around */;
10599 /* If it's a `;', we are done. */
10600 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10601 break;
10602 /* Anything else is an error. */
10603 else
10605 /* If we have already issued an error message we don't need
10606 to issue another one. */
10607 if (decl != error_mark_node
10608 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10609 cp_parser_error (parser, "expected %<,%> or %<;%>");
10610 /* Skip tokens until we reach the end of the statement. */
10611 cp_parser_skip_to_end_of_statement (parser);
10612 /* If the next token is now a `;', consume it. */
10613 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10614 cp_lexer_consume_token (parser->lexer);
10615 goto done;
10617 /* After the first time around, a function-definition is not
10618 allowed -- even if it was OK at first. For example:
10620 int i, f() {}
10622 is not valid. */
10623 function_definition_allowed_p = false;
10626 /* Issue an error message if no declarators are present, and the
10627 decl-specifier-seq does not itself declare a class or
10628 enumeration. */
10629 if (!saw_declarator)
10631 if (cp_parser_declares_only_class_p (parser))
10632 shadow_tag (&decl_specifiers);
10633 /* Perform any deferred access checks. */
10634 perform_deferred_access_checks (tf_warning_or_error);
10637 /* Consume the `;'. */
10638 if (!maybe_range_for_decl)
10639 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10641 done:
10642 pop_deferring_access_checks ();
10645 /* Parse a decl-specifier-seq.
10647 decl-specifier-seq:
10648 decl-specifier-seq [opt] decl-specifier
10649 decl-specifier attribute-specifier-seq [opt] (C++11)
10651 decl-specifier:
10652 storage-class-specifier
10653 type-specifier
10654 function-specifier
10655 friend
10656 typedef
10658 GNU Extension:
10660 decl-specifier:
10661 attributes
10663 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10665 The parser flags FLAGS is used to control type-specifier parsing.
10667 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10668 flags:
10670 1: one of the decl-specifiers is an elaborated-type-specifier
10671 (i.e., a type declaration)
10672 2: one of the decl-specifiers is an enum-specifier or a
10673 class-specifier (i.e., a type definition)
10677 static void
10678 cp_parser_decl_specifier_seq (cp_parser* parser,
10679 cp_parser_flags flags,
10680 cp_decl_specifier_seq *decl_specs,
10681 int* declares_class_or_enum)
10683 bool constructor_possible_p = !parser->in_declarator_p;
10684 bool found_decl_spec = false;
10685 cp_token *start_token = NULL;
10686 cp_decl_spec ds;
10688 /* Clear DECL_SPECS. */
10689 clear_decl_specs (decl_specs);
10691 /* Assume no class or enumeration type is declared. */
10692 *declares_class_or_enum = 0;
10694 /* Keep reading specifiers until there are no more to read. */
10695 while (true)
10697 bool constructor_p;
10698 cp_token *token;
10699 ds = ds_last;
10701 /* Peek at the next token. */
10702 token = cp_lexer_peek_token (parser->lexer);
10704 /* Save the first token of the decl spec list for error
10705 reporting. */
10706 if (!start_token)
10707 start_token = token;
10708 /* Handle attributes. */
10709 if (cp_next_tokens_can_be_attribute_p (parser))
10711 /* Parse the attributes. */
10712 tree attrs = cp_parser_attributes_opt (parser);
10714 /* In a sequence of declaration specifiers, c++11 attributes
10715 appertain to the type that precede them. In that case
10716 [dcl.spec]/1 says:
10718 The attribute-specifier-seq affects the type only for
10719 the declaration it appears in, not other declarations
10720 involving the same type.
10722 But for now let's force the user to position the
10723 attribute either at the beginning of the declaration or
10724 after the declarator-id, which would clearly mean that it
10725 applies to the declarator. */
10726 if (cxx11_attribute_p (attrs))
10728 if (!found_decl_spec)
10729 /* The c++11 attribute is at the beginning of the
10730 declaration. It appertains to the entity being
10731 declared. */;
10732 else
10734 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
10736 /* This is an attribute following a
10737 class-specifier. */
10738 if (decl_specs->type_definition_p)
10739 warn_misplaced_attr_for_class_type (token->location,
10740 decl_specs->type);
10741 attrs = NULL_TREE;
10743 else
10745 decl_specs->std_attributes
10746 = chainon (decl_specs->std_attributes,
10747 attrs);
10748 if (decl_specs->locations[ds_std_attribute] == 0)
10749 decl_specs->locations[ds_std_attribute] = token->location;
10751 continue;
10755 decl_specs->attributes
10756 = chainon (decl_specs->attributes,
10757 attrs);
10758 if (decl_specs->locations[ds_attribute] == 0)
10759 decl_specs->locations[ds_attribute] = token->location;
10760 continue;
10762 /* Assume we will find a decl-specifier keyword. */
10763 found_decl_spec = true;
10764 /* If the next token is an appropriate keyword, we can simply
10765 add it to the list. */
10766 switch (token->keyword)
10768 /* decl-specifier:
10769 friend
10770 constexpr */
10771 case RID_FRIEND:
10772 if (!at_class_scope_p ())
10774 error_at (token->location, "%<friend%> used outside of class");
10775 cp_lexer_purge_token (parser->lexer);
10777 else
10779 ds = ds_friend;
10780 /* Consume the token. */
10781 cp_lexer_consume_token (parser->lexer);
10783 break;
10785 case RID_CONSTEXPR:
10786 ds = ds_constexpr;
10787 cp_lexer_consume_token (parser->lexer);
10788 break;
10790 /* function-specifier:
10791 inline
10792 virtual
10793 explicit */
10794 case RID_INLINE:
10795 case RID_VIRTUAL:
10796 case RID_EXPLICIT:
10797 cp_parser_function_specifier_opt (parser, decl_specs);
10798 break;
10800 /* decl-specifier:
10801 typedef */
10802 case RID_TYPEDEF:
10803 ds = ds_typedef;
10804 /* Consume the token. */
10805 cp_lexer_consume_token (parser->lexer);
10806 /* A constructor declarator cannot appear in a typedef. */
10807 constructor_possible_p = false;
10808 /* The "typedef" keyword can only occur in a declaration; we
10809 may as well commit at this point. */
10810 cp_parser_commit_to_tentative_parse (parser);
10812 if (decl_specs->storage_class != sc_none)
10813 decl_specs->conflicting_specifiers_p = true;
10814 break;
10816 /* storage-class-specifier:
10817 auto
10818 register
10819 static
10820 extern
10821 mutable
10823 GNU Extension:
10824 thread */
10825 case RID_AUTO:
10826 if (cxx_dialect == cxx98)
10828 /* Consume the token. */
10829 cp_lexer_consume_token (parser->lexer);
10831 /* Complain about `auto' as a storage specifier, if
10832 we're complaining about C++0x compatibility. */
10833 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10834 " changes meaning in C++11; please remove it");
10836 /* Set the storage class anyway. */
10837 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10838 token);
10840 else
10841 /* C++0x auto type-specifier. */
10842 found_decl_spec = false;
10843 break;
10845 case RID_REGISTER:
10846 case RID_STATIC:
10847 case RID_EXTERN:
10848 case RID_MUTABLE:
10849 /* Consume the token. */
10850 cp_lexer_consume_token (parser->lexer);
10851 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10852 token);
10853 break;
10854 case RID_THREAD:
10855 /* Consume the token. */
10856 ds = ds_thread;
10857 cp_lexer_consume_token (parser->lexer);
10858 break;
10860 default:
10861 /* We did not yet find a decl-specifier yet. */
10862 found_decl_spec = false;
10863 break;
10866 if (found_decl_spec
10867 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10868 && token->keyword != RID_CONSTEXPR)
10869 error ("decl-specifier invalid in condition");
10871 if (ds != ds_last)
10872 set_and_check_decl_spec_loc (decl_specs, ds, token);
10874 /* Constructors are a special case. The `S' in `S()' is not a
10875 decl-specifier; it is the beginning of the declarator. */
10876 constructor_p
10877 = (!found_decl_spec
10878 && constructor_possible_p
10879 && (cp_parser_constructor_declarator_p
10880 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
10882 /* If we don't have a DECL_SPEC yet, then we must be looking at
10883 a type-specifier. */
10884 if (!found_decl_spec && !constructor_p)
10886 int decl_spec_declares_class_or_enum;
10887 bool is_cv_qualifier;
10888 tree type_spec;
10890 type_spec
10891 = cp_parser_type_specifier (parser, flags,
10892 decl_specs,
10893 /*is_declaration=*/true,
10894 &decl_spec_declares_class_or_enum,
10895 &is_cv_qualifier);
10896 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10898 /* If this type-specifier referenced a user-defined type
10899 (a typedef, class-name, etc.), then we can't allow any
10900 more such type-specifiers henceforth.
10902 [dcl.spec]
10904 The longest sequence of decl-specifiers that could
10905 possibly be a type name is taken as the
10906 decl-specifier-seq of a declaration. The sequence shall
10907 be self-consistent as described below.
10909 [dcl.type]
10911 As a general rule, at most one type-specifier is allowed
10912 in the complete decl-specifier-seq of a declaration. The
10913 only exceptions are the following:
10915 -- const or volatile can be combined with any other
10916 type-specifier.
10918 -- signed or unsigned can be combined with char, long,
10919 short, or int.
10921 -- ..
10923 Example:
10925 typedef char* Pc;
10926 void g (const int Pc);
10928 Here, Pc is *not* part of the decl-specifier seq; it's
10929 the declarator. Therefore, once we see a type-specifier
10930 (other than a cv-qualifier), we forbid any additional
10931 user-defined types. We *do* still allow things like `int
10932 int' to be considered a decl-specifier-seq, and issue the
10933 error message later. */
10934 if (type_spec && !is_cv_qualifier)
10935 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10936 /* A constructor declarator cannot follow a type-specifier. */
10937 if (type_spec)
10939 constructor_possible_p = false;
10940 found_decl_spec = true;
10941 if (!is_cv_qualifier)
10942 decl_specs->any_type_specifiers_p = true;
10946 /* If we still do not have a DECL_SPEC, then there are no more
10947 decl-specifiers. */
10948 if (!found_decl_spec)
10949 break;
10951 decl_specs->any_specifiers_p = true;
10952 /* After we see one decl-specifier, further decl-specifiers are
10953 always optional. */
10954 flags |= CP_PARSER_FLAGS_OPTIONAL;
10957 /* Don't allow a friend specifier with a class definition. */
10958 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
10959 && (*declares_class_or_enum & 2))
10960 error_at (decl_specs->locations[ds_friend],
10961 "class definition may not be declared a friend");
10964 /* Parse an (optional) storage-class-specifier.
10966 storage-class-specifier:
10967 auto
10968 register
10969 static
10970 extern
10971 mutable
10973 GNU Extension:
10975 storage-class-specifier:
10976 thread
10978 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10980 static tree
10981 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10983 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10985 case RID_AUTO:
10986 if (cxx_dialect != cxx98)
10987 return NULL_TREE;
10988 /* Fall through for C++98. */
10990 case RID_REGISTER:
10991 case RID_STATIC:
10992 case RID_EXTERN:
10993 case RID_MUTABLE:
10994 case RID_THREAD:
10995 /* Consume the token. */
10996 return cp_lexer_consume_token (parser->lexer)->u.value;
10998 default:
10999 return NULL_TREE;
11003 /* Parse an (optional) function-specifier.
11005 function-specifier:
11006 inline
11007 virtual
11008 explicit
11010 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11011 Updates DECL_SPECS, if it is non-NULL. */
11013 static tree
11014 cp_parser_function_specifier_opt (cp_parser* parser,
11015 cp_decl_specifier_seq *decl_specs)
11017 cp_token *token = cp_lexer_peek_token (parser->lexer);
11018 switch (token->keyword)
11020 case RID_INLINE:
11021 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11022 break;
11024 case RID_VIRTUAL:
11025 /* 14.5.2.3 [temp.mem]
11027 A member function template shall not be virtual. */
11028 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11029 error_at (token->location, "templates may not be %<virtual%>");
11030 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11031 break;
11033 case RID_EXPLICIT:
11034 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11035 break;
11037 default:
11038 return NULL_TREE;
11041 /* Consume the token. */
11042 return cp_lexer_consume_token (parser->lexer)->u.value;
11045 /* Parse a linkage-specification.
11047 linkage-specification:
11048 extern string-literal { declaration-seq [opt] }
11049 extern string-literal declaration */
11051 static void
11052 cp_parser_linkage_specification (cp_parser* parser)
11054 tree linkage;
11056 /* Look for the `extern' keyword. */
11057 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11059 /* Look for the string-literal. */
11060 linkage = cp_parser_string_literal (parser, false, false);
11062 /* Transform the literal into an identifier. If the literal is a
11063 wide-character string, or contains embedded NULs, then we can't
11064 handle it as the user wants. */
11065 if (strlen (TREE_STRING_POINTER (linkage))
11066 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11068 cp_parser_error (parser, "invalid linkage-specification");
11069 /* Assume C++ linkage. */
11070 linkage = lang_name_cplusplus;
11072 else
11073 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11075 /* We're now using the new linkage. */
11076 push_lang_context (linkage);
11078 /* If the next token is a `{', then we're using the first
11079 production. */
11080 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11082 /* Consume the `{' token. */
11083 cp_lexer_consume_token (parser->lexer);
11084 /* Parse the declarations. */
11085 cp_parser_declaration_seq_opt (parser);
11086 /* Look for the closing `}'. */
11087 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11089 /* Otherwise, there's just one declaration. */
11090 else
11092 bool saved_in_unbraced_linkage_specification_p;
11094 saved_in_unbraced_linkage_specification_p
11095 = parser->in_unbraced_linkage_specification_p;
11096 parser->in_unbraced_linkage_specification_p = true;
11097 cp_parser_declaration (parser);
11098 parser->in_unbraced_linkage_specification_p
11099 = saved_in_unbraced_linkage_specification_p;
11102 /* We're done with the linkage-specification. */
11103 pop_lang_context ();
11106 /* Parse a static_assert-declaration.
11108 static_assert-declaration:
11109 static_assert ( constant-expression , string-literal ) ;
11111 If MEMBER_P, this static_assert is a class member. */
11113 static void
11114 cp_parser_static_assert(cp_parser *parser, bool member_p)
11116 tree condition;
11117 tree message;
11118 cp_token *token;
11119 location_t saved_loc;
11120 bool dummy;
11122 /* Peek at the `static_assert' token so we can keep track of exactly
11123 where the static assertion started. */
11124 token = cp_lexer_peek_token (parser->lexer);
11125 saved_loc = token->location;
11127 /* Look for the `static_assert' keyword. */
11128 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11129 RT_STATIC_ASSERT))
11130 return;
11132 /* We know we are in a static assertion; commit to any tentative
11133 parse. */
11134 if (cp_parser_parsing_tentatively (parser))
11135 cp_parser_commit_to_tentative_parse (parser);
11137 /* Parse the `(' starting the static assertion condition. */
11138 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11140 /* Parse the constant-expression. Allow a non-constant expression
11141 here in order to give better diagnostics in finish_static_assert. */
11142 condition =
11143 cp_parser_constant_expression (parser,
11144 /*allow_non_constant_p=*/true,
11145 /*non_constant_p=*/&dummy);
11147 /* Parse the separating `,'. */
11148 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11150 /* Parse the string-literal message. */
11151 message = cp_parser_string_literal (parser,
11152 /*translate=*/false,
11153 /*wide_ok=*/true);
11155 /* A `)' completes the static assertion. */
11156 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11157 cp_parser_skip_to_closing_parenthesis (parser,
11158 /*recovering=*/true,
11159 /*or_comma=*/false,
11160 /*consume_paren=*/true);
11162 /* A semicolon terminates the declaration. */
11163 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11165 /* Complete the static assertion, which may mean either processing
11166 the static assert now or saving it for template instantiation. */
11167 finish_static_assert (condition, message, saved_loc, member_p);
11170 /* Parse a `decltype' type. Returns the type.
11172 simple-type-specifier:
11173 decltype ( expression ) */
11175 static tree
11176 cp_parser_decltype (cp_parser *parser)
11178 tree expr;
11179 bool id_expression_or_member_access_p = false;
11180 const char *saved_message;
11181 bool saved_integral_constant_expression_p;
11182 bool saved_non_integral_constant_expression_p;
11183 cp_token *id_expr_start_token;
11184 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11186 if (start_token->type == CPP_DECLTYPE)
11188 /* Already parsed. */
11189 cp_lexer_consume_token (parser->lexer);
11190 return start_token->u.value;
11193 /* Look for the `decltype' token. */
11194 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11195 return error_mark_node;
11197 /* Types cannot be defined in a `decltype' expression. Save away the
11198 old message. */
11199 saved_message = parser->type_definition_forbidden_message;
11201 /* And create the new one. */
11202 parser->type_definition_forbidden_message
11203 = G_("types may not be defined in %<decltype%> expressions");
11205 /* The restrictions on constant-expressions do not apply inside
11206 decltype expressions. */
11207 saved_integral_constant_expression_p
11208 = parser->integral_constant_expression_p;
11209 saved_non_integral_constant_expression_p
11210 = parser->non_integral_constant_expression_p;
11211 parser->integral_constant_expression_p = false;
11213 /* Do not actually evaluate the expression. */
11214 ++cp_unevaluated_operand;
11216 /* Do not warn about problems with the expression. */
11217 ++c_inhibit_evaluation_warnings;
11219 /* Parse the opening `('. */
11220 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11221 return error_mark_node;
11223 /* First, try parsing an id-expression. */
11224 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11225 cp_parser_parse_tentatively (parser);
11226 expr = cp_parser_id_expression (parser,
11227 /*template_keyword_p=*/false,
11228 /*check_dependency_p=*/true,
11229 /*template_p=*/NULL,
11230 /*declarator_p=*/false,
11231 /*optional_p=*/false);
11233 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11235 bool non_integral_constant_expression_p = false;
11236 tree id_expression = expr;
11237 cp_id_kind idk;
11238 const char *error_msg;
11240 if (TREE_CODE (expr) == IDENTIFIER_NODE)
11241 /* Lookup the name we got back from the id-expression. */
11242 expr = cp_parser_lookup_name (parser, expr,
11243 none_type,
11244 /*is_template=*/false,
11245 /*is_namespace=*/false,
11246 /*check_dependency=*/true,
11247 /*ambiguous_decls=*/NULL,
11248 id_expr_start_token->location);
11250 if (expr
11251 && expr != error_mark_node
11252 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11253 && TREE_CODE (expr) != TYPE_DECL
11254 && (TREE_CODE (expr) != BIT_NOT_EXPR
11255 || !TYPE_P (TREE_OPERAND (expr, 0)))
11256 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11258 /* Complete lookup of the id-expression. */
11259 expr = (finish_id_expression
11260 (id_expression, expr, parser->scope, &idk,
11261 /*integral_constant_expression_p=*/false,
11262 /*allow_non_integral_constant_expression_p=*/true,
11263 &non_integral_constant_expression_p,
11264 /*template_p=*/false,
11265 /*done=*/true,
11266 /*address_p=*/false,
11267 /*template_arg_p=*/false,
11268 &error_msg,
11269 id_expr_start_token->location));
11271 if (expr == error_mark_node)
11272 /* We found an id-expression, but it was something that we
11273 should not have found. This is an error, not something
11274 we can recover from, so note that we found an
11275 id-expression and we'll recover as gracefully as
11276 possible. */
11277 id_expression_or_member_access_p = true;
11280 if (expr
11281 && expr != error_mark_node
11282 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11283 /* We have an id-expression. */
11284 id_expression_or_member_access_p = true;
11287 if (!id_expression_or_member_access_p)
11289 /* Abort the id-expression parse. */
11290 cp_parser_abort_tentative_parse (parser);
11292 /* Parsing tentatively, again. */
11293 cp_parser_parse_tentatively (parser);
11295 /* Parse a class member access. */
11296 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11297 /*cast_p=*/false,
11298 /*member_access_only_p=*/true, NULL);
11300 if (expr
11301 && expr != error_mark_node
11302 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11303 /* We have an id-expression. */
11304 id_expression_or_member_access_p = true;
11307 if (id_expression_or_member_access_p)
11308 /* We have parsed the complete id-expression or member access. */
11309 cp_parser_parse_definitely (parser);
11310 else
11312 bool saved_greater_than_is_operator_p;
11314 /* Abort our attempt to parse an id-expression or member access
11315 expression. */
11316 cp_parser_abort_tentative_parse (parser);
11318 /* Within a parenthesized expression, a `>' token is always
11319 the greater-than operator. */
11320 saved_greater_than_is_operator_p
11321 = parser->greater_than_is_operator_p;
11322 parser->greater_than_is_operator_p = true;
11324 /* Parse a full expression. */
11325 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11327 /* The `>' token might be the end of a template-id or
11328 template-parameter-list now. */
11329 parser->greater_than_is_operator_p
11330 = saved_greater_than_is_operator_p;
11333 /* Go back to evaluating expressions. */
11334 --cp_unevaluated_operand;
11335 --c_inhibit_evaluation_warnings;
11337 /* Restore the old message and the integral constant expression
11338 flags. */
11339 parser->type_definition_forbidden_message = saved_message;
11340 parser->integral_constant_expression_p
11341 = saved_integral_constant_expression_p;
11342 parser->non_integral_constant_expression_p
11343 = saved_non_integral_constant_expression_p;
11345 /* Parse to the closing `)'. */
11346 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11348 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11349 /*consume_paren=*/true);
11350 return error_mark_node;
11353 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11354 tf_warning_or_error);
11356 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11357 it again. */
11358 start_token->type = CPP_DECLTYPE;
11359 start_token->u.value = expr;
11360 start_token->keyword = RID_MAX;
11361 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11363 return expr;
11366 /* Special member functions [gram.special] */
11368 /* Parse a conversion-function-id.
11370 conversion-function-id:
11371 operator conversion-type-id
11373 Returns an IDENTIFIER_NODE representing the operator. */
11375 static tree
11376 cp_parser_conversion_function_id (cp_parser* parser)
11378 tree type;
11379 tree saved_scope;
11380 tree saved_qualifying_scope;
11381 tree saved_object_scope;
11382 tree pushed_scope = NULL_TREE;
11384 /* Look for the `operator' token. */
11385 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11386 return error_mark_node;
11387 /* When we parse the conversion-type-id, the current scope will be
11388 reset. However, we need that information in able to look up the
11389 conversion function later, so we save it here. */
11390 saved_scope = parser->scope;
11391 saved_qualifying_scope = parser->qualifying_scope;
11392 saved_object_scope = parser->object_scope;
11393 /* We must enter the scope of the class so that the names of
11394 entities declared within the class are available in the
11395 conversion-type-id. For example, consider:
11397 struct S {
11398 typedef int I;
11399 operator I();
11402 S::operator I() { ... }
11404 In order to see that `I' is a type-name in the definition, we
11405 must be in the scope of `S'. */
11406 if (saved_scope)
11407 pushed_scope = push_scope (saved_scope);
11408 /* Parse the conversion-type-id. */
11409 type = cp_parser_conversion_type_id (parser);
11410 /* Leave the scope of the class, if any. */
11411 if (pushed_scope)
11412 pop_scope (pushed_scope);
11413 /* Restore the saved scope. */
11414 parser->scope = saved_scope;
11415 parser->qualifying_scope = saved_qualifying_scope;
11416 parser->object_scope = saved_object_scope;
11417 /* If the TYPE is invalid, indicate failure. */
11418 if (type == error_mark_node)
11419 return error_mark_node;
11420 return mangle_conv_op_name_for_type (type);
11423 /* Parse a conversion-type-id:
11425 conversion-type-id:
11426 type-specifier-seq conversion-declarator [opt]
11428 Returns the TYPE specified. */
11430 static tree
11431 cp_parser_conversion_type_id (cp_parser* parser)
11433 tree attributes;
11434 cp_decl_specifier_seq type_specifiers;
11435 cp_declarator *declarator;
11436 tree type_specified;
11438 /* Parse the attributes. */
11439 attributes = cp_parser_attributes_opt (parser);
11440 /* Parse the type-specifiers. */
11441 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11442 /*is_trailing_return=*/false,
11443 &type_specifiers);
11444 /* If that didn't work, stop. */
11445 if (type_specifiers.type == error_mark_node)
11446 return error_mark_node;
11447 /* Parse the conversion-declarator. */
11448 declarator = cp_parser_conversion_declarator_opt (parser);
11450 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11451 /*initialized=*/0, &attributes);
11452 if (attributes)
11453 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11455 /* Don't give this error when parsing tentatively. This happens to
11456 work because we always parse this definitively once. */
11457 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11458 && type_uses_auto (type_specified))
11460 if (cxx_dialect < cxx1y)
11462 error ("invalid use of %<auto%> in conversion operator");
11463 return error_mark_node;
11465 else if (template_parm_scope_p ())
11466 warning (0, "use of %<auto%> in member template "
11467 "conversion operator can never be deduced");
11470 return type_specified;
11473 /* Parse an (optional) conversion-declarator.
11475 conversion-declarator:
11476 ptr-operator conversion-declarator [opt]
11480 static cp_declarator *
11481 cp_parser_conversion_declarator_opt (cp_parser* parser)
11483 enum tree_code code;
11484 tree class_type, std_attributes = NULL_TREE;
11485 cp_cv_quals cv_quals;
11487 /* We don't know if there's a ptr-operator next, or not. */
11488 cp_parser_parse_tentatively (parser);
11489 /* Try the ptr-operator. */
11490 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
11491 &std_attributes);
11492 /* If it worked, look for more conversion-declarators. */
11493 if (cp_parser_parse_definitely (parser))
11495 cp_declarator *declarator;
11497 /* Parse another optional declarator. */
11498 declarator = cp_parser_conversion_declarator_opt (parser);
11500 declarator = cp_parser_make_indirect_declarator
11501 (code, class_type, cv_quals, declarator, std_attributes);
11503 return declarator;
11506 return NULL;
11509 /* Parse an (optional) ctor-initializer.
11511 ctor-initializer:
11512 : mem-initializer-list
11514 Returns TRUE iff the ctor-initializer was actually present. */
11516 static bool
11517 cp_parser_ctor_initializer_opt (cp_parser* parser)
11519 /* If the next token is not a `:', then there is no
11520 ctor-initializer. */
11521 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11523 /* Do default initialization of any bases and members. */
11524 if (DECL_CONSTRUCTOR_P (current_function_decl))
11525 finish_mem_initializers (NULL_TREE);
11527 return false;
11530 /* Consume the `:' token. */
11531 cp_lexer_consume_token (parser->lexer);
11532 /* And the mem-initializer-list. */
11533 cp_parser_mem_initializer_list (parser);
11535 return true;
11538 /* Parse a mem-initializer-list.
11540 mem-initializer-list:
11541 mem-initializer ... [opt]
11542 mem-initializer ... [opt] , mem-initializer-list */
11544 static void
11545 cp_parser_mem_initializer_list (cp_parser* parser)
11547 tree mem_initializer_list = NULL_TREE;
11548 tree target_ctor = error_mark_node;
11549 cp_token *token = cp_lexer_peek_token (parser->lexer);
11551 /* Let the semantic analysis code know that we are starting the
11552 mem-initializer-list. */
11553 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11554 error_at (token->location,
11555 "only constructors take member initializers");
11557 /* Loop through the list. */
11558 while (true)
11560 tree mem_initializer;
11562 token = cp_lexer_peek_token (parser->lexer);
11563 /* Parse the mem-initializer. */
11564 mem_initializer = cp_parser_mem_initializer (parser);
11565 /* If the next token is a `...', we're expanding member initializers. */
11566 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11568 /* Consume the `...'. */
11569 cp_lexer_consume_token (parser->lexer);
11571 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11572 can be expanded but members cannot. */
11573 if (mem_initializer != error_mark_node
11574 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11576 error_at (token->location,
11577 "cannot expand initializer for member %<%D%>",
11578 TREE_PURPOSE (mem_initializer));
11579 mem_initializer = error_mark_node;
11582 /* Construct the pack expansion type. */
11583 if (mem_initializer != error_mark_node)
11584 mem_initializer = make_pack_expansion (mem_initializer);
11586 if (target_ctor != error_mark_node
11587 && mem_initializer != error_mark_node)
11589 error ("mem-initializer for %qD follows constructor delegation",
11590 TREE_PURPOSE (mem_initializer));
11591 mem_initializer = error_mark_node;
11593 /* Look for a target constructor. */
11594 if (mem_initializer != error_mark_node
11595 && TYPE_P (TREE_PURPOSE (mem_initializer))
11596 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11598 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11599 if (mem_initializer_list)
11601 error ("constructor delegation follows mem-initializer for %qD",
11602 TREE_PURPOSE (mem_initializer_list));
11603 mem_initializer = error_mark_node;
11605 target_ctor = mem_initializer;
11607 /* Add it to the list, unless it was erroneous. */
11608 if (mem_initializer != error_mark_node)
11610 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11611 mem_initializer_list = mem_initializer;
11613 /* If the next token is not a `,', we're done. */
11614 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11615 break;
11616 /* Consume the `,' token. */
11617 cp_lexer_consume_token (parser->lexer);
11620 /* Perform semantic analysis. */
11621 if (DECL_CONSTRUCTOR_P (current_function_decl))
11622 finish_mem_initializers (mem_initializer_list);
11625 /* Parse a mem-initializer.
11627 mem-initializer:
11628 mem-initializer-id ( expression-list [opt] )
11629 mem-initializer-id braced-init-list
11631 GNU extension:
11633 mem-initializer:
11634 ( expression-list [opt] )
11636 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11637 class) or FIELD_DECL (for a non-static data member) to initialize;
11638 the TREE_VALUE is the expression-list. An empty initialization
11639 list is represented by void_list_node. */
11641 static tree
11642 cp_parser_mem_initializer (cp_parser* parser)
11644 tree mem_initializer_id;
11645 tree expression_list;
11646 tree member;
11647 cp_token *token = cp_lexer_peek_token (parser->lexer);
11649 /* Find out what is being initialized. */
11650 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11652 permerror (token->location,
11653 "anachronistic old-style base class initializer");
11654 mem_initializer_id = NULL_TREE;
11656 else
11658 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11659 if (mem_initializer_id == error_mark_node)
11660 return mem_initializer_id;
11662 member = expand_member_init (mem_initializer_id);
11663 if (member && !DECL_P (member))
11664 in_base_initializer = 1;
11666 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11668 bool expr_non_constant_p;
11669 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11670 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11671 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11672 expression_list = build_tree_list (NULL_TREE, expression_list);
11674 else
11676 vec<tree, va_gc> *vec;
11677 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11678 /*cast_p=*/false,
11679 /*allow_expansion_p=*/true,
11680 /*non_constant_p=*/NULL);
11681 if (vec == NULL)
11682 return error_mark_node;
11683 expression_list = build_tree_list_vec (vec);
11684 release_tree_vector (vec);
11687 if (expression_list == error_mark_node)
11688 return error_mark_node;
11689 if (!expression_list)
11690 expression_list = void_type_node;
11692 in_base_initializer = 0;
11694 return member ? build_tree_list (member, expression_list) : error_mark_node;
11697 /* Parse a mem-initializer-id.
11699 mem-initializer-id:
11700 :: [opt] nested-name-specifier [opt] class-name
11701 identifier
11703 Returns a TYPE indicating the class to be initializer for the first
11704 production. Returns an IDENTIFIER_NODE indicating the data member
11705 to be initialized for the second production. */
11707 static tree
11708 cp_parser_mem_initializer_id (cp_parser* parser)
11710 bool global_scope_p;
11711 bool nested_name_specifier_p;
11712 bool template_p = false;
11713 tree id;
11715 cp_token *token = cp_lexer_peek_token (parser->lexer);
11717 /* `typename' is not allowed in this context ([temp.res]). */
11718 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11720 error_at (token->location,
11721 "keyword %<typename%> not allowed in this context (a qualified "
11722 "member initializer is implicitly a type)");
11723 cp_lexer_consume_token (parser->lexer);
11725 /* Look for the optional `::' operator. */
11726 global_scope_p
11727 = (cp_parser_global_scope_opt (parser,
11728 /*current_scope_valid_p=*/false)
11729 != NULL_TREE);
11730 /* Look for the optional nested-name-specifier. The simplest way to
11731 implement:
11733 [temp.res]
11735 The keyword `typename' is not permitted in a base-specifier or
11736 mem-initializer; in these contexts a qualified name that
11737 depends on a template-parameter is implicitly assumed to be a
11738 type name.
11740 is to assume that we have seen the `typename' keyword at this
11741 point. */
11742 nested_name_specifier_p
11743 = (cp_parser_nested_name_specifier_opt (parser,
11744 /*typename_keyword_p=*/true,
11745 /*check_dependency_p=*/true,
11746 /*type_p=*/true,
11747 /*is_declaration=*/true)
11748 != NULL_TREE);
11749 if (nested_name_specifier_p)
11750 template_p = cp_parser_optional_template_keyword (parser);
11751 /* If there is a `::' operator or a nested-name-specifier, then we
11752 are definitely looking for a class-name. */
11753 if (global_scope_p || nested_name_specifier_p)
11754 return cp_parser_class_name (parser,
11755 /*typename_keyword_p=*/true,
11756 /*template_keyword_p=*/template_p,
11757 typename_type,
11758 /*check_dependency_p=*/true,
11759 /*class_head_p=*/false,
11760 /*is_declaration=*/true);
11761 /* Otherwise, we could also be looking for an ordinary identifier. */
11762 cp_parser_parse_tentatively (parser);
11763 /* Try a class-name. */
11764 id = cp_parser_class_name (parser,
11765 /*typename_keyword_p=*/true,
11766 /*template_keyword_p=*/false,
11767 none_type,
11768 /*check_dependency_p=*/true,
11769 /*class_head_p=*/false,
11770 /*is_declaration=*/true);
11771 /* If we found one, we're done. */
11772 if (cp_parser_parse_definitely (parser))
11773 return id;
11774 /* Otherwise, look for an ordinary identifier. */
11775 return cp_parser_identifier (parser);
11778 /* Overloading [gram.over] */
11780 /* Parse an operator-function-id.
11782 operator-function-id:
11783 operator operator
11785 Returns an IDENTIFIER_NODE for the operator which is a
11786 human-readable spelling of the identifier, e.g., `operator +'. */
11788 static tree
11789 cp_parser_operator_function_id (cp_parser* parser)
11791 /* Look for the `operator' keyword. */
11792 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11793 return error_mark_node;
11794 /* And then the name of the operator itself. */
11795 return cp_parser_operator (parser);
11798 /* Return an identifier node for a user-defined literal operator.
11799 The suffix identifier is chained to the operator name identifier. */
11801 static tree
11802 cp_literal_operator_id (const char* name)
11804 tree identifier;
11805 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11806 + strlen (name) + 10);
11807 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11808 identifier = get_identifier (buffer);
11809 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11811 return identifier;
11814 /* Parse an operator.
11816 operator:
11817 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11818 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11819 || ++ -- , ->* -> () []
11821 GNU Extensions:
11823 operator:
11824 <? >? <?= >?=
11826 Returns an IDENTIFIER_NODE for the operator which is a
11827 human-readable spelling of the identifier, e.g., `operator +'. */
11829 static tree
11830 cp_parser_operator (cp_parser* parser)
11832 tree id = NULL_TREE;
11833 cp_token *token;
11835 /* Peek at the next token. */
11836 token = cp_lexer_peek_token (parser->lexer);
11837 /* Figure out which operator we have. */
11838 switch (token->type)
11840 case CPP_KEYWORD:
11842 enum tree_code op;
11844 /* The keyword should be either `new' or `delete'. */
11845 if (token->keyword == RID_NEW)
11846 op = NEW_EXPR;
11847 else if (token->keyword == RID_DELETE)
11848 op = DELETE_EXPR;
11849 else
11850 break;
11852 /* Consume the `new' or `delete' token. */
11853 cp_lexer_consume_token (parser->lexer);
11855 /* Peek at the next token. */
11856 token = cp_lexer_peek_token (parser->lexer);
11857 /* If it's a `[' token then this is the array variant of the
11858 operator. */
11859 if (token->type == CPP_OPEN_SQUARE)
11861 /* Consume the `[' token. */
11862 cp_lexer_consume_token (parser->lexer);
11863 /* Look for the `]' token. */
11864 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11865 id = ansi_opname (op == NEW_EXPR
11866 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11868 /* Otherwise, we have the non-array variant. */
11869 else
11870 id = ansi_opname (op);
11872 return id;
11875 case CPP_PLUS:
11876 id = ansi_opname (PLUS_EXPR);
11877 break;
11879 case CPP_MINUS:
11880 id = ansi_opname (MINUS_EXPR);
11881 break;
11883 case CPP_MULT:
11884 id = ansi_opname (MULT_EXPR);
11885 break;
11887 case CPP_DIV:
11888 id = ansi_opname (TRUNC_DIV_EXPR);
11889 break;
11891 case CPP_MOD:
11892 id = ansi_opname (TRUNC_MOD_EXPR);
11893 break;
11895 case CPP_XOR:
11896 id = ansi_opname (BIT_XOR_EXPR);
11897 break;
11899 case CPP_AND:
11900 id = ansi_opname (BIT_AND_EXPR);
11901 break;
11903 case CPP_OR:
11904 id = ansi_opname (BIT_IOR_EXPR);
11905 break;
11907 case CPP_COMPL:
11908 id = ansi_opname (BIT_NOT_EXPR);
11909 break;
11911 case CPP_NOT:
11912 id = ansi_opname (TRUTH_NOT_EXPR);
11913 break;
11915 case CPP_EQ:
11916 id = ansi_assopname (NOP_EXPR);
11917 break;
11919 case CPP_LESS:
11920 id = ansi_opname (LT_EXPR);
11921 break;
11923 case CPP_GREATER:
11924 id = ansi_opname (GT_EXPR);
11925 break;
11927 case CPP_PLUS_EQ:
11928 id = ansi_assopname (PLUS_EXPR);
11929 break;
11931 case CPP_MINUS_EQ:
11932 id = ansi_assopname (MINUS_EXPR);
11933 break;
11935 case CPP_MULT_EQ:
11936 id = ansi_assopname (MULT_EXPR);
11937 break;
11939 case CPP_DIV_EQ:
11940 id = ansi_assopname (TRUNC_DIV_EXPR);
11941 break;
11943 case CPP_MOD_EQ:
11944 id = ansi_assopname (TRUNC_MOD_EXPR);
11945 break;
11947 case CPP_XOR_EQ:
11948 id = ansi_assopname (BIT_XOR_EXPR);
11949 break;
11951 case CPP_AND_EQ:
11952 id = ansi_assopname (BIT_AND_EXPR);
11953 break;
11955 case CPP_OR_EQ:
11956 id = ansi_assopname (BIT_IOR_EXPR);
11957 break;
11959 case CPP_LSHIFT:
11960 id = ansi_opname (LSHIFT_EXPR);
11961 break;
11963 case CPP_RSHIFT:
11964 id = ansi_opname (RSHIFT_EXPR);
11965 break;
11967 case CPP_LSHIFT_EQ:
11968 id = ansi_assopname (LSHIFT_EXPR);
11969 break;
11971 case CPP_RSHIFT_EQ:
11972 id = ansi_assopname (RSHIFT_EXPR);
11973 break;
11975 case CPP_EQ_EQ:
11976 id = ansi_opname (EQ_EXPR);
11977 break;
11979 case CPP_NOT_EQ:
11980 id = ansi_opname (NE_EXPR);
11981 break;
11983 case CPP_LESS_EQ:
11984 id = ansi_opname (LE_EXPR);
11985 break;
11987 case CPP_GREATER_EQ:
11988 id = ansi_opname (GE_EXPR);
11989 break;
11991 case CPP_AND_AND:
11992 id = ansi_opname (TRUTH_ANDIF_EXPR);
11993 break;
11995 case CPP_OR_OR:
11996 id = ansi_opname (TRUTH_ORIF_EXPR);
11997 break;
11999 case CPP_PLUS_PLUS:
12000 id = ansi_opname (POSTINCREMENT_EXPR);
12001 break;
12003 case CPP_MINUS_MINUS:
12004 id = ansi_opname (PREDECREMENT_EXPR);
12005 break;
12007 case CPP_COMMA:
12008 id = ansi_opname (COMPOUND_EXPR);
12009 break;
12011 case CPP_DEREF_STAR:
12012 id = ansi_opname (MEMBER_REF);
12013 break;
12015 case CPP_DEREF:
12016 id = ansi_opname (COMPONENT_REF);
12017 break;
12019 case CPP_OPEN_PAREN:
12020 /* Consume the `('. */
12021 cp_lexer_consume_token (parser->lexer);
12022 /* Look for the matching `)'. */
12023 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12024 return ansi_opname (CALL_EXPR);
12026 case CPP_OPEN_SQUARE:
12027 /* Consume the `['. */
12028 cp_lexer_consume_token (parser->lexer);
12029 /* Look for the matching `]'. */
12030 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12031 return ansi_opname (ARRAY_REF);
12033 case CPP_STRING:
12034 if (cxx_dialect == cxx98)
12035 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12036 if (TREE_STRING_LENGTH (token->u.value) > 2)
12038 error ("expected empty string after %<operator%> keyword");
12039 return error_mark_node;
12041 /* Consume the string. */
12042 cp_lexer_consume_token (parser->lexer);
12043 /* Look for the suffix identifier. */
12044 token = cp_lexer_peek_token (parser->lexer);
12045 if (token->type == CPP_NAME)
12047 id = cp_parser_identifier (parser);
12048 if (id != error_mark_node)
12050 const char *name = IDENTIFIER_POINTER (id);
12051 return cp_literal_operator_id (name);
12054 else
12056 error ("expected suffix identifier");
12057 return error_mark_node;
12060 case CPP_STRING_USERDEF:
12061 error ("missing space between %<\"\"%> and suffix identifier");
12062 return error_mark_node;
12064 default:
12065 /* Anything else is an error. */
12066 break;
12069 /* If we have selected an identifier, we need to consume the
12070 operator token. */
12071 if (id)
12072 cp_lexer_consume_token (parser->lexer);
12073 /* Otherwise, no valid operator name was present. */
12074 else
12076 cp_parser_error (parser, "expected operator");
12077 id = error_mark_node;
12080 return id;
12083 /* Parse a template-declaration.
12085 template-declaration:
12086 export [opt] template < template-parameter-list > declaration
12088 If MEMBER_P is TRUE, this template-declaration occurs within a
12089 class-specifier.
12091 The grammar rule given by the standard isn't correct. What
12092 is really meant is:
12094 template-declaration:
12095 export [opt] template-parameter-list-seq
12096 decl-specifier-seq [opt] init-declarator [opt] ;
12097 export [opt] template-parameter-list-seq
12098 function-definition
12100 template-parameter-list-seq:
12101 template-parameter-list-seq [opt]
12102 template < template-parameter-list > */
12104 static void
12105 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12107 /* Check for `export'. */
12108 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12110 /* Consume the `export' token. */
12111 cp_lexer_consume_token (parser->lexer);
12112 /* Warn that we do not support `export'. */
12113 warning (0, "keyword %<export%> not implemented, and will be ignored");
12116 cp_parser_template_declaration_after_export (parser, member_p);
12119 /* Parse a template-parameter-list.
12121 template-parameter-list:
12122 template-parameter
12123 template-parameter-list , template-parameter
12125 Returns a TREE_LIST. Each node represents a template parameter.
12126 The nodes are connected via their TREE_CHAINs. */
12128 static tree
12129 cp_parser_template_parameter_list (cp_parser* parser)
12131 tree parameter_list = NULL_TREE;
12133 begin_template_parm_list ();
12135 /* The loop below parses the template parms. We first need to know
12136 the total number of template parms to be able to compute proper
12137 canonical types of each dependent type. So after the loop, when
12138 we know the total number of template parms,
12139 end_template_parm_list computes the proper canonical types and
12140 fixes up the dependent types accordingly. */
12141 while (true)
12143 tree parameter;
12144 bool is_non_type;
12145 bool is_parameter_pack;
12146 location_t parm_loc;
12148 /* Parse the template-parameter. */
12149 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12150 parameter = cp_parser_template_parameter (parser,
12151 &is_non_type,
12152 &is_parameter_pack);
12153 /* Add it to the list. */
12154 if (parameter != error_mark_node)
12155 parameter_list = process_template_parm (parameter_list,
12156 parm_loc,
12157 parameter,
12158 is_non_type,
12159 is_parameter_pack);
12160 else
12162 tree err_parm = build_tree_list (parameter, parameter);
12163 parameter_list = chainon (parameter_list, err_parm);
12166 /* If the next token is not a `,', we're done. */
12167 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12168 break;
12169 /* Otherwise, consume the `,' token. */
12170 cp_lexer_consume_token (parser->lexer);
12173 return end_template_parm_list (parameter_list);
12176 /* Parse a template-parameter.
12178 template-parameter:
12179 type-parameter
12180 parameter-declaration
12182 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12183 the parameter. The TREE_PURPOSE is the default value, if any.
12184 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12185 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12186 set to true iff this parameter is a parameter pack. */
12188 static tree
12189 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12190 bool *is_parameter_pack)
12192 cp_token *token;
12193 cp_parameter_declarator *parameter_declarator;
12194 cp_declarator *id_declarator;
12195 tree parm;
12197 /* Assume it is a type parameter or a template parameter. */
12198 *is_non_type = false;
12199 /* Assume it not a parameter pack. */
12200 *is_parameter_pack = false;
12201 /* Peek at the next token. */
12202 token = cp_lexer_peek_token (parser->lexer);
12203 /* If it is `class' or `template', we have a type-parameter. */
12204 if (token->keyword == RID_TEMPLATE)
12205 return cp_parser_type_parameter (parser, is_parameter_pack);
12206 /* If it is `class' or `typename' we do not know yet whether it is a
12207 type parameter or a non-type parameter. Consider:
12209 template <typename T, typename T::X X> ...
12213 template <class C, class D*> ...
12215 Here, the first parameter is a type parameter, and the second is
12216 a non-type parameter. We can tell by looking at the token after
12217 the identifier -- if it is a `,', `=', or `>' then we have a type
12218 parameter. */
12219 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12221 /* Peek at the token after `class' or `typename'. */
12222 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12223 /* If it's an ellipsis, we have a template type parameter
12224 pack. */
12225 if (token->type == CPP_ELLIPSIS)
12226 return cp_parser_type_parameter (parser, is_parameter_pack);
12227 /* If it's an identifier, skip it. */
12228 if (token->type == CPP_NAME)
12229 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12230 /* Now, see if the token looks like the end of a template
12231 parameter. */
12232 if (token->type == CPP_COMMA
12233 || token->type == CPP_EQ
12234 || token->type == CPP_GREATER)
12235 return cp_parser_type_parameter (parser, is_parameter_pack);
12238 /* Otherwise, it is a non-type parameter.
12240 [temp.param]
12242 When parsing a default template-argument for a non-type
12243 template-parameter, the first non-nested `>' is taken as the end
12244 of the template parameter-list rather than a greater-than
12245 operator. */
12246 *is_non_type = true;
12247 parameter_declarator
12248 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12249 /*parenthesized_p=*/NULL);
12251 /* If the parameter declaration is marked as a parameter pack, set
12252 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12253 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12254 grokdeclarator. */
12255 if (parameter_declarator
12256 && parameter_declarator->declarator
12257 && parameter_declarator->declarator->parameter_pack_p)
12259 *is_parameter_pack = true;
12260 parameter_declarator->declarator->parameter_pack_p = false;
12263 if (parameter_declarator
12264 && parameter_declarator->default_argument)
12266 /* Can happen in some cases of erroneous input (c++/34892). */
12267 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12268 /* Consume the `...' for better error recovery. */
12269 cp_lexer_consume_token (parser->lexer);
12271 /* If the next token is an ellipsis, and we don't already have it
12272 marked as a parameter pack, then we have a parameter pack (that
12273 has no declarator). */
12274 else if (!*is_parameter_pack
12275 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12276 && (declarator_can_be_parameter_pack
12277 (parameter_declarator->declarator)))
12279 /* Consume the `...'. */
12280 cp_lexer_consume_token (parser->lexer);
12281 maybe_warn_variadic_templates ();
12283 *is_parameter_pack = true;
12285 /* We might end up with a pack expansion as the type of the non-type
12286 template parameter, in which case this is a non-type template
12287 parameter pack. */
12288 else if (parameter_declarator
12289 && parameter_declarator->decl_specifiers.type
12290 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12292 *is_parameter_pack = true;
12293 parameter_declarator->decl_specifiers.type =
12294 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12297 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12299 /* Parameter packs cannot have default arguments. However, a
12300 user may try to do so, so we'll parse them and give an
12301 appropriate diagnostic here. */
12303 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12305 /* Find the name of the parameter pack. */
12306 id_declarator = parameter_declarator->declarator;
12307 while (id_declarator && id_declarator->kind != cdk_id)
12308 id_declarator = id_declarator->declarator;
12310 if (id_declarator && id_declarator->kind == cdk_id)
12311 error_at (start_token->location,
12312 "template parameter pack %qD cannot have a default argument",
12313 id_declarator->u.id.unqualified_name);
12314 else
12315 error_at (start_token->location,
12316 "template parameter pack cannot have a default argument");
12318 /* Parse the default argument, but throw away the result. */
12319 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12322 parm = grokdeclarator (parameter_declarator->declarator,
12323 &parameter_declarator->decl_specifiers,
12324 TPARM, /*initialized=*/0,
12325 /*attrlist=*/NULL);
12326 if (parm == error_mark_node)
12327 return error_mark_node;
12329 return build_tree_list (parameter_declarator->default_argument, parm);
12332 /* Parse a type-parameter.
12334 type-parameter:
12335 class identifier [opt]
12336 class identifier [opt] = type-id
12337 typename identifier [opt]
12338 typename identifier [opt] = type-id
12339 template < template-parameter-list > class identifier [opt]
12340 template < template-parameter-list > class identifier [opt]
12341 = id-expression
12343 GNU Extension (variadic templates):
12345 type-parameter:
12346 class ... identifier [opt]
12347 typename ... identifier [opt]
12349 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12350 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12351 the declaration of the parameter.
12353 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12355 static tree
12356 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12358 cp_token *token;
12359 tree parameter;
12361 /* Look for a keyword to tell us what kind of parameter this is. */
12362 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12363 if (!token)
12364 return error_mark_node;
12366 switch (token->keyword)
12368 case RID_CLASS:
12369 case RID_TYPENAME:
12371 tree identifier;
12372 tree default_argument;
12374 /* If the next token is an ellipsis, we have a template
12375 argument pack. */
12376 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12378 /* Consume the `...' token. */
12379 cp_lexer_consume_token (parser->lexer);
12380 maybe_warn_variadic_templates ();
12382 *is_parameter_pack = true;
12385 /* If the next token is an identifier, then it names the
12386 parameter. */
12387 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12388 identifier = cp_parser_identifier (parser);
12389 else
12390 identifier = NULL_TREE;
12392 /* Create the parameter. */
12393 parameter = finish_template_type_parm (class_type_node, identifier);
12395 /* If the next token is an `=', we have a default argument. */
12396 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12398 /* Consume the `=' token. */
12399 cp_lexer_consume_token (parser->lexer);
12400 /* Parse the default-argument. */
12401 push_deferring_access_checks (dk_no_deferred);
12402 default_argument = cp_parser_type_id (parser);
12404 /* Template parameter packs cannot have default
12405 arguments. */
12406 if (*is_parameter_pack)
12408 if (identifier)
12409 error_at (token->location,
12410 "template parameter pack %qD cannot have a "
12411 "default argument", identifier);
12412 else
12413 error_at (token->location,
12414 "template parameter packs cannot have "
12415 "default arguments");
12416 default_argument = NULL_TREE;
12418 pop_deferring_access_checks ();
12420 else
12421 default_argument = NULL_TREE;
12423 /* Create the combined representation of the parameter and the
12424 default argument. */
12425 parameter = build_tree_list (default_argument, parameter);
12427 break;
12429 case RID_TEMPLATE:
12431 tree identifier;
12432 tree default_argument;
12434 /* Look for the `<'. */
12435 cp_parser_require (parser, CPP_LESS, RT_LESS);
12436 /* Parse the template-parameter-list. */
12437 cp_parser_template_parameter_list (parser);
12438 /* Look for the `>'. */
12439 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12440 /* Look for the `class' keyword. */
12441 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12442 /* If the next token is an ellipsis, we have a template
12443 argument pack. */
12444 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12446 /* Consume the `...' token. */
12447 cp_lexer_consume_token (parser->lexer);
12448 maybe_warn_variadic_templates ();
12450 *is_parameter_pack = true;
12452 /* If the next token is an `=', then there is a
12453 default-argument. If the next token is a `>', we are at
12454 the end of the parameter-list. If the next token is a `,',
12455 then we are at the end of this parameter. */
12456 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12457 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12458 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12460 identifier = cp_parser_identifier (parser);
12461 /* Treat invalid names as if the parameter were nameless. */
12462 if (identifier == error_mark_node)
12463 identifier = NULL_TREE;
12465 else
12466 identifier = NULL_TREE;
12468 /* Create the template parameter. */
12469 parameter = finish_template_template_parm (class_type_node,
12470 identifier);
12472 /* If the next token is an `=', then there is a
12473 default-argument. */
12474 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12476 bool is_template;
12478 /* Consume the `='. */
12479 cp_lexer_consume_token (parser->lexer);
12480 /* Parse the id-expression. */
12481 push_deferring_access_checks (dk_no_deferred);
12482 /* save token before parsing the id-expression, for error
12483 reporting */
12484 token = cp_lexer_peek_token (parser->lexer);
12485 default_argument
12486 = cp_parser_id_expression (parser,
12487 /*template_keyword_p=*/false,
12488 /*check_dependency_p=*/true,
12489 /*template_p=*/&is_template,
12490 /*declarator_p=*/false,
12491 /*optional_p=*/false);
12492 if (TREE_CODE (default_argument) == TYPE_DECL)
12493 /* If the id-expression was a template-id that refers to
12494 a template-class, we already have the declaration here,
12495 so no further lookup is needed. */
12497 else
12498 /* Look up the name. */
12499 default_argument
12500 = cp_parser_lookup_name (parser, default_argument,
12501 none_type,
12502 /*is_template=*/is_template,
12503 /*is_namespace=*/false,
12504 /*check_dependency=*/true,
12505 /*ambiguous_decls=*/NULL,
12506 token->location);
12507 /* See if the default argument is valid. */
12508 default_argument
12509 = check_template_template_default_arg (default_argument);
12511 /* Template parameter packs cannot have default
12512 arguments. */
12513 if (*is_parameter_pack)
12515 if (identifier)
12516 error_at (token->location,
12517 "template parameter pack %qD cannot "
12518 "have a default argument",
12519 identifier);
12520 else
12521 error_at (token->location, "template parameter packs cannot "
12522 "have default arguments");
12523 default_argument = NULL_TREE;
12525 pop_deferring_access_checks ();
12527 else
12528 default_argument = NULL_TREE;
12530 /* Create the combined representation of the parameter and the
12531 default argument. */
12532 parameter = build_tree_list (default_argument, parameter);
12534 break;
12536 default:
12537 gcc_unreachable ();
12538 break;
12541 return parameter;
12544 /* Parse a template-id.
12546 template-id:
12547 template-name < template-argument-list [opt] >
12549 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12550 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12551 returned. Otherwise, if the template-name names a function, or set
12552 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12553 names a class, returns a TYPE_DECL for the specialization.
12555 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12556 uninstantiated templates. */
12558 static tree
12559 cp_parser_template_id (cp_parser *parser,
12560 bool template_keyword_p,
12561 bool check_dependency_p,
12562 enum tag_types tag_type,
12563 bool is_declaration)
12565 int i;
12566 tree templ;
12567 tree arguments;
12568 tree template_id;
12569 cp_token_position start_of_id = 0;
12570 deferred_access_check *chk;
12571 vec<deferred_access_check, va_gc> *access_check;
12572 cp_token *next_token = NULL, *next_token_2 = NULL;
12573 bool is_identifier;
12575 /* If the next token corresponds to a template-id, there is no need
12576 to reparse it. */
12577 next_token = cp_lexer_peek_token (parser->lexer);
12578 if (next_token->type == CPP_TEMPLATE_ID)
12580 struct tree_check *check_value;
12582 /* Get the stored value. */
12583 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12584 /* Perform any access checks that were deferred. */
12585 access_check = check_value->checks;
12586 if (access_check)
12588 FOR_EACH_VEC_ELT (*access_check, i, chk)
12589 perform_or_defer_access_check (chk->binfo,
12590 chk->decl,
12591 chk->diag_decl,
12592 tf_warning_or_error);
12594 /* Return the stored value. */
12595 return check_value->value;
12598 /* Avoid performing name lookup if there is no possibility of
12599 finding a template-id. */
12600 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12601 || (next_token->type == CPP_NAME
12602 && !cp_parser_nth_token_starts_template_argument_list_p
12603 (parser, 2)))
12605 cp_parser_error (parser, "expected template-id");
12606 return error_mark_node;
12609 /* Remember where the template-id starts. */
12610 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12611 start_of_id = cp_lexer_token_position (parser->lexer, false);
12613 push_deferring_access_checks (dk_deferred);
12615 /* Parse the template-name. */
12616 is_identifier = false;
12617 templ = cp_parser_template_name (parser, template_keyword_p,
12618 check_dependency_p,
12619 is_declaration,
12620 tag_type,
12621 &is_identifier);
12622 if (templ == error_mark_node || is_identifier)
12624 pop_deferring_access_checks ();
12625 return templ;
12628 /* If we find the sequence `[:' after a template-name, it's probably
12629 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12630 parse correctly the argument list. */
12631 next_token = cp_lexer_peek_token (parser->lexer);
12632 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12633 if (next_token->type == CPP_OPEN_SQUARE
12634 && next_token->flags & DIGRAPH
12635 && next_token_2->type == CPP_COLON
12636 && !(next_token_2->flags & PREV_WHITE))
12638 cp_parser_parse_tentatively (parser);
12639 /* Change `:' into `::'. */
12640 next_token_2->type = CPP_SCOPE;
12641 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12642 CPP_LESS. */
12643 cp_lexer_consume_token (parser->lexer);
12645 /* Parse the arguments. */
12646 arguments = cp_parser_enclosed_template_argument_list (parser);
12647 if (!cp_parser_parse_definitely (parser))
12649 /* If we couldn't parse an argument list, then we revert our changes
12650 and return simply an error. Maybe this is not a template-id
12651 after all. */
12652 next_token_2->type = CPP_COLON;
12653 cp_parser_error (parser, "expected %<<%>");
12654 pop_deferring_access_checks ();
12655 return error_mark_node;
12657 /* Otherwise, emit an error about the invalid digraph, but continue
12658 parsing because we got our argument list. In C++11 do not emit
12659 any error, per 2.5/3. */
12660 if (cxx_dialect < cxx0x
12661 && permerror (next_token->location,
12662 "%<<::%> cannot begin a template-argument list"))
12664 static bool hint = false;
12665 inform (next_token->location,
12666 "%<<:%> is an alternate spelling for %<[%>."
12667 " Insert whitespace between %<<%> and %<::%>");
12668 if (!hint && !flag_permissive)
12670 inform (next_token->location, "(if you use %<-fpermissive%> "
12671 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
12672 "accept your code)");
12673 hint = true;
12677 else
12679 /* Look for the `<' that starts the template-argument-list. */
12680 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12682 pop_deferring_access_checks ();
12683 return error_mark_node;
12685 /* Parse the arguments. */
12686 arguments = cp_parser_enclosed_template_argument_list (parser);
12689 /* Build a representation of the specialization. */
12690 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12691 template_id = build_min_nt_loc (next_token->location,
12692 TEMPLATE_ID_EXPR,
12693 templ, arguments);
12694 else if (DECL_TYPE_TEMPLATE_P (templ)
12695 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12697 bool entering_scope;
12698 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12699 template (rather than some instantiation thereof) only if
12700 is not nested within some other construct. For example, in
12701 "template <typename T> void f(T) { A<T>::", A<T> is just an
12702 instantiation of A. */
12703 entering_scope = (template_parm_scope_p ()
12704 && cp_lexer_next_token_is (parser->lexer,
12705 CPP_SCOPE));
12706 template_id
12707 = finish_template_type (templ, arguments, entering_scope);
12709 else
12711 /* If it's not a class-template or a template-template, it should be
12712 a function-template. */
12713 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12714 || TREE_CODE (templ) == OVERLOAD
12715 || BASELINK_P (templ)));
12717 template_id = lookup_template_function (templ, arguments);
12720 /* If parsing tentatively, replace the sequence of tokens that makes
12721 up the template-id with a CPP_TEMPLATE_ID token. That way,
12722 should we re-parse the token stream, we will not have to repeat
12723 the effort required to do the parse, nor will we issue duplicate
12724 error messages about problems during instantiation of the
12725 template. */
12726 if (start_of_id)
12728 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12730 /* Reset the contents of the START_OF_ID token. */
12731 token->type = CPP_TEMPLATE_ID;
12732 /* Retrieve any deferred checks. Do not pop this access checks yet
12733 so the memory will not be reclaimed during token replacing below. */
12734 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12735 token->u.tree_check_value->value = template_id;
12736 token->u.tree_check_value->checks = get_deferred_access_checks ();
12737 token->keyword = RID_MAX;
12739 /* Purge all subsequent tokens. */
12740 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12742 /* ??? Can we actually assume that, if template_id ==
12743 error_mark_node, we will have issued a diagnostic to the
12744 user, as opposed to simply marking the tentative parse as
12745 failed? */
12746 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12747 error_at (token->location, "parse error in template argument list");
12750 pop_deferring_access_checks ();
12751 return template_id;
12754 /* Parse a template-name.
12756 template-name:
12757 identifier
12759 The standard should actually say:
12761 template-name:
12762 identifier
12763 operator-function-id
12765 A defect report has been filed about this issue.
12767 A conversion-function-id cannot be a template name because they cannot
12768 be part of a template-id. In fact, looking at this code:
12770 a.operator K<int>()
12772 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12773 It is impossible to call a templated conversion-function-id with an
12774 explicit argument list, since the only allowed template parameter is
12775 the type to which it is converting.
12777 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12778 `template' keyword, in a construction like:
12780 T::template f<3>()
12782 In that case `f' is taken to be a template-name, even though there
12783 is no way of knowing for sure.
12785 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12786 name refers to a set of overloaded functions, at least one of which
12787 is a template, or an IDENTIFIER_NODE with the name of the template,
12788 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12789 names are looked up inside uninstantiated templates. */
12791 static tree
12792 cp_parser_template_name (cp_parser* parser,
12793 bool template_keyword_p,
12794 bool check_dependency_p,
12795 bool is_declaration,
12796 enum tag_types tag_type,
12797 bool *is_identifier)
12799 tree identifier;
12800 tree decl;
12801 tree fns;
12802 cp_token *token = cp_lexer_peek_token (parser->lexer);
12804 /* If the next token is `operator', then we have either an
12805 operator-function-id or a conversion-function-id. */
12806 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12808 /* We don't know whether we're looking at an
12809 operator-function-id or a conversion-function-id. */
12810 cp_parser_parse_tentatively (parser);
12811 /* Try an operator-function-id. */
12812 identifier = cp_parser_operator_function_id (parser);
12813 /* If that didn't work, try a conversion-function-id. */
12814 if (!cp_parser_parse_definitely (parser))
12816 cp_parser_error (parser, "expected template-name");
12817 return error_mark_node;
12820 /* Look for the identifier. */
12821 else
12822 identifier = cp_parser_identifier (parser);
12824 /* If we didn't find an identifier, we don't have a template-id. */
12825 if (identifier == error_mark_node)
12826 return error_mark_node;
12828 /* If the name immediately followed the `template' keyword, then it
12829 is a template-name. However, if the next token is not `<', then
12830 we do not treat it as a template-name, since it is not being used
12831 as part of a template-id. This enables us to handle constructs
12832 like:
12834 template <typename T> struct S { S(); };
12835 template <typename T> S<T>::S();
12837 correctly. We would treat `S' as a template -- if it were `S<T>'
12838 -- but we do not if there is no `<'. */
12840 if (processing_template_decl
12841 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12843 /* In a declaration, in a dependent context, we pretend that the
12844 "template" keyword was present in order to improve error
12845 recovery. For example, given:
12847 template <typename T> void f(T::X<int>);
12849 we want to treat "X<int>" as a template-id. */
12850 if (is_declaration
12851 && !template_keyword_p
12852 && parser->scope && TYPE_P (parser->scope)
12853 && check_dependency_p
12854 && dependent_scope_p (parser->scope)
12855 /* Do not do this for dtors (or ctors), since they never
12856 need the template keyword before their name. */
12857 && !constructor_name_p (identifier, parser->scope))
12859 cp_token_position start = 0;
12861 /* Explain what went wrong. */
12862 error_at (token->location, "non-template %qD used as template",
12863 identifier);
12864 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12865 parser->scope, identifier);
12866 /* If parsing tentatively, find the location of the "<" token. */
12867 if (cp_parser_simulate_error (parser))
12868 start = cp_lexer_token_position (parser->lexer, true);
12869 /* Parse the template arguments so that we can issue error
12870 messages about them. */
12871 cp_lexer_consume_token (parser->lexer);
12872 cp_parser_enclosed_template_argument_list (parser);
12873 /* Skip tokens until we find a good place from which to
12874 continue parsing. */
12875 cp_parser_skip_to_closing_parenthesis (parser,
12876 /*recovering=*/true,
12877 /*or_comma=*/true,
12878 /*consume_paren=*/false);
12879 /* If parsing tentatively, permanently remove the
12880 template argument list. That will prevent duplicate
12881 error messages from being issued about the missing
12882 "template" keyword. */
12883 if (start)
12884 cp_lexer_purge_tokens_after (parser->lexer, start);
12885 if (is_identifier)
12886 *is_identifier = true;
12887 return identifier;
12890 /* If the "template" keyword is present, then there is generally
12891 no point in doing name-lookup, so we just return IDENTIFIER.
12892 But, if the qualifying scope is non-dependent then we can
12893 (and must) do name-lookup normally. */
12894 if (template_keyword_p
12895 && (!parser->scope
12896 || (TYPE_P (parser->scope)
12897 && dependent_type_p (parser->scope))))
12898 return identifier;
12901 /* Look up the name. */
12902 decl = cp_parser_lookup_name (parser, identifier,
12903 tag_type,
12904 /*is_template=*/true,
12905 /*is_namespace=*/false,
12906 check_dependency_p,
12907 /*ambiguous_decls=*/NULL,
12908 token->location);
12910 /* If DECL is a template, then the name was a template-name. */
12911 if (TREE_CODE (decl) == TEMPLATE_DECL)
12913 else
12915 tree fn = NULL_TREE;
12917 /* The standard does not explicitly indicate whether a name that
12918 names a set of overloaded declarations, some of which are
12919 templates, is a template-name. However, such a name should
12920 be a template-name; otherwise, there is no way to form a
12921 template-id for the overloaded templates. */
12922 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12923 if (TREE_CODE (fns) == OVERLOAD)
12924 for (fn = fns; fn; fn = OVL_NEXT (fn))
12925 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12926 break;
12928 if (!fn)
12930 /* The name does not name a template. */
12931 cp_parser_error (parser, "expected template-name");
12932 return error_mark_node;
12936 /* If DECL is dependent, and refers to a function, then just return
12937 its name; we will look it up again during template instantiation. */
12938 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12940 tree scope = ovl_scope (decl);
12941 if (TYPE_P (scope) && dependent_type_p (scope))
12942 return identifier;
12945 return decl;
12948 /* Parse a template-argument-list.
12950 template-argument-list:
12951 template-argument ... [opt]
12952 template-argument-list , template-argument ... [opt]
12954 Returns a TREE_VEC containing the arguments. */
12956 static tree
12957 cp_parser_template_argument_list (cp_parser* parser)
12959 tree fixed_args[10];
12960 unsigned n_args = 0;
12961 unsigned alloced = 10;
12962 tree *arg_ary = fixed_args;
12963 tree vec;
12964 bool saved_in_template_argument_list_p;
12965 bool saved_ice_p;
12966 bool saved_non_ice_p;
12968 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12969 parser->in_template_argument_list_p = true;
12970 /* Even if the template-id appears in an integral
12971 constant-expression, the contents of the argument list do
12972 not. */
12973 saved_ice_p = parser->integral_constant_expression_p;
12974 parser->integral_constant_expression_p = false;
12975 saved_non_ice_p = parser->non_integral_constant_expression_p;
12976 parser->non_integral_constant_expression_p = false;
12978 /* Parse the arguments. */
12981 tree argument;
12983 if (n_args)
12984 /* Consume the comma. */
12985 cp_lexer_consume_token (parser->lexer);
12987 /* Parse the template-argument. */
12988 argument = cp_parser_template_argument (parser);
12990 /* If the next token is an ellipsis, we're expanding a template
12991 argument pack. */
12992 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12994 if (argument == error_mark_node)
12996 cp_token *token = cp_lexer_peek_token (parser->lexer);
12997 error_at (token->location,
12998 "expected parameter pack before %<...%>");
13000 /* Consume the `...' token. */
13001 cp_lexer_consume_token (parser->lexer);
13003 /* Make the argument into a TYPE_PACK_EXPANSION or
13004 EXPR_PACK_EXPANSION. */
13005 argument = make_pack_expansion (argument);
13008 if (n_args == alloced)
13010 alloced *= 2;
13012 if (arg_ary == fixed_args)
13014 arg_ary = XNEWVEC (tree, alloced);
13015 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13017 else
13018 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13020 arg_ary[n_args++] = argument;
13022 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13024 vec = make_tree_vec (n_args);
13026 while (n_args--)
13027 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13029 if (arg_ary != fixed_args)
13030 free (arg_ary);
13031 parser->non_integral_constant_expression_p = saved_non_ice_p;
13032 parser->integral_constant_expression_p = saved_ice_p;
13033 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13034 #ifdef ENABLE_CHECKING
13035 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13036 #endif
13037 return vec;
13040 /* Parse a template-argument.
13042 template-argument:
13043 assignment-expression
13044 type-id
13045 id-expression
13047 The representation is that of an assignment-expression, type-id, or
13048 id-expression -- except that the qualified id-expression is
13049 evaluated, so that the value returned is either a DECL or an
13050 OVERLOAD.
13052 Although the standard says "assignment-expression", it forbids
13053 throw-expressions or assignments in the template argument.
13054 Therefore, we use "conditional-expression" instead. */
13056 static tree
13057 cp_parser_template_argument (cp_parser* parser)
13059 tree argument;
13060 bool template_p;
13061 bool address_p;
13062 bool maybe_type_id = false;
13063 cp_token *token = NULL, *argument_start_token = NULL;
13064 location_t loc = 0;
13065 cp_id_kind idk;
13067 /* There's really no way to know what we're looking at, so we just
13068 try each alternative in order.
13070 [temp.arg]
13072 In a template-argument, an ambiguity between a type-id and an
13073 expression is resolved to a type-id, regardless of the form of
13074 the corresponding template-parameter.
13076 Therefore, we try a type-id first. */
13077 cp_parser_parse_tentatively (parser);
13078 argument = cp_parser_template_type_arg (parser);
13079 /* If there was no error parsing the type-id but the next token is a
13080 '>>', our behavior depends on which dialect of C++ we're
13081 parsing. In C++98, we probably found a typo for '> >'. But there
13082 are type-id which are also valid expressions. For instance:
13084 struct X { int operator >> (int); };
13085 template <int V> struct Foo {};
13086 Foo<X () >> 5> r;
13088 Here 'X()' is a valid type-id of a function type, but the user just
13089 wanted to write the expression "X() >> 5". Thus, we remember that we
13090 found a valid type-id, but we still try to parse the argument as an
13091 expression to see what happens.
13093 In C++0x, the '>>' will be considered two separate '>'
13094 tokens. */
13095 if (!cp_parser_error_occurred (parser)
13096 && cxx_dialect == cxx98
13097 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13099 maybe_type_id = true;
13100 cp_parser_abort_tentative_parse (parser);
13102 else
13104 /* If the next token isn't a `,' or a `>', then this argument wasn't
13105 really finished. This means that the argument is not a valid
13106 type-id. */
13107 if (!cp_parser_next_token_ends_template_argument_p (parser))
13108 cp_parser_error (parser, "expected template-argument");
13109 /* If that worked, we're done. */
13110 if (cp_parser_parse_definitely (parser))
13111 return argument;
13113 /* We're still not sure what the argument will be. */
13114 cp_parser_parse_tentatively (parser);
13115 /* Try a template. */
13116 argument_start_token = cp_lexer_peek_token (parser->lexer);
13117 argument = cp_parser_id_expression (parser,
13118 /*template_keyword_p=*/false,
13119 /*check_dependency_p=*/true,
13120 &template_p,
13121 /*declarator_p=*/false,
13122 /*optional_p=*/false);
13123 /* If the next token isn't a `,' or a `>', then this argument wasn't
13124 really finished. */
13125 if (!cp_parser_next_token_ends_template_argument_p (parser))
13126 cp_parser_error (parser, "expected template-argument");
13127 if (!cp_parser_error_occurred (parser))
13129 /* Figure out what is being referred to. If the id-expression
13130 was for a class template specialization, then we will have a
13131 TYPE_DECL at this point. There is no need to do name lookup
13132 at this point in that case. */
13133 if (TREE_CODE (argument) != TYPE_DECL)
13134 argument = cp_parser_lookup_name (parser, argument,
13135 none_type,
13136 /*is_template=*/template_p,
13137 /*is_namespace=*/false,
13138 /*check_dependency=*/true,
13139 /*ambiguous_decls=*/NULL,
13140 argument_start_token->location);
13141 if (TREE_CODE (argument) != TEMPLATE_DECL
13142 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13143 cp_parser_error (parser, "expected template-name");
13145 if (cp_parser_parse_definitely (parser))
13146 return argument;
13147 /* It must be a non-type argument. There permitted cases are given
13148 in [temp.arg.nontype]:
13150 -- an integral constant-expression of integral or enumeration
13151 type; or
13153 -- the name of a non-type template-parameter; or
13155 -- the name of an object or function with external linkage...
13157 -- the address of an object or function with external linkage...
13159 -- a pointer to member... */
13160 /* Look for a non-type template parameter. */
13161 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13163 cp_parser_parse_tentatively (parser);
13164 argument = cp_parser_primary_expression (parser,
13165 /*address_p=*/false,
13166 /*cast_p=*/false,
13167 /*template_arg_p=*/true,
13168 &idk);
13169 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13170 || !cp_parser_next_token_ends_template_argument_p (parser))
13171 cp_parser_simulate_error (parser);
13172 if (cp_parser_parse_definitely (parser))
13173 return argument;
13176 /* If the next token is "&", the argument must be the address of an
13177 object or function with external linkage. */
13178 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13179 if (address_p)
13181 loc = cp_lexer_peek_token (parser->lexer)->location;
13182 cp_lexer_consume_token (parser->lexer);
13184 /* See if we might have an id-expression. */
13185 token = cp_lexer_peek_token (parser->lexer);
13186 if (token->type == CPP_NAME
13187 || token->keyword == RID_OPERATOR
13188 || token->type == CPP_SCOPE
13189 || token->type == CPP_TEMPLATE_ID
13190 || token->type == CPP_NESTED_NAME_SPECIFIER)
13192 cp_parser_parse_tentatively (parser);
13193 argument = cp_parser_primary_expression (parser,
13194 address_p,
13195 /*cast_p=*/false,
13196 /*template_arg_p=*/true,
13197 &idk);
13198 if (cp_parser_error_occurred (parser)
13199 || !cp_parser_next_token_ends_template_argument_p (parser))
13200 cp_parser_abort_tentative_parse (parser);
13201 else
13203 tree probe;
13205 if (TREE_CODE (argument) == INDIRECT_REF)
13207 gcc_assert (REFERENCE_REF_P (argument));
13208 argument = TREE_OPERAND (argument, 0);
13211 /* If we're in a template, we represent a qualified-id referring
13212 to a static data member as a SCOPE_REF even if the scope isn't
13213 dependent so that we can check access control later. */
13214 probe = argument;
13215 if (TREE_CODE (probe) == SCOPE_REF)
13216 probe = TREE_OPERAND (probe, 1);
13217 if (TREE_CODE (probe) == VAR_DECL)
13219 /* A variable without external linkage might still be a
13220 valid constant-expression, so no error is issued here
13221 if the external-linkage check fails. */
13222 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13223 cp_parser_simulate_error (parser);
13225 else if (is_overloaded_fn (argument))
13226 /* All overloaded functions are allowed; if the external
13227 linkage test does not pass, an error will be issued
13228 later. */
13230 else if (address_p
13231 && (TREE_CODE (argument) == OFFSET_REF
13232 || TREE_CODE (argument) == SCOPE_REF))
13233 /* A pointer-to-member. */
13235 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13237 else
13238 cp_parser_simulate_error (parser);
13240 if (cp_parser_parse_definitely (parser))
13242 if (address_p)
13243 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13244 tf_warning_or_error);
13245 return argument;
13249 /* If the argument started with "&", there are no other valid
13250 alternatives at this point. */
13251 if (address_p)
13253 cp_parser_error (parser, "invalid non-type template argument");
13254 return error_mark_node;
13257 /* If the argument wasn't successfully parsed as a type-id followed
13258 by '>>', the argument can only be a constant expression now.
13259 Otherwise, we try parsing the constant-expression tentatively,
13260 because the argument could really be a type-id. */
13261 if (maybe_type_id)
13262 cp_parser_parse_tentatively (parser);
13263 argument = cp_parser_constant_expression (parser,
13264 /*allow_non_constant_p=*/false,
13265 /*non_constant_p=*/NULL);
13266 argument = fold_non_dependent_expr (argument);
13267 if (!maybe_type_id)
13268 return argument;
13269 if (!cp_parser_next_token_ends_template_argument_p (parser))
13270 cp_parser_error (parser, "expected template-argument");
13271 if (cp_parser_parse_definitely (parser))
13272 return argument;
13273 /* We did our best to parse the argument as a non type-id, but that
13274 was the only alternative that matched (albeit with a '>' after
13275 it). We can assume it's just a typo from the user, and a
13276 diagnostic will then be issued. */
13277 return cp_parser_template_type_arg (parser);
13280 /* Parse an explicit-instantiation.
13282 explicit-instantiation:
13283 template declaration
13285 Although the standard says `declaration', what it really means is:
13287 explicit-instantiation:
13288 template decl-specifier-seq [opt] declarator [opt] ;
13290 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13291 supposed to be allowed. A defect report has been filed about this
13292 issue.
13294 GNU Extension:
13296 explicit-instantiation:
13297 storage-class-specifier template
13298 decl-specifier-seq [opt] declarator [opt] ;
13299 function-specifier template
13300 decl-specifier-seq [opt] declarator [opt] ; */
13302 static void
13303 cp_parser_explicit_instantiation (cp_parser* parser)
13305 int declares_class_or_enum;
13306 cp_decl_specifier_seq decl_specifiers;
13307 tree extension_specifier = NULL_TREE;
13309 timevar_push (TV_TEMPLATE_INST);
13311 /* Look for an (optional) storage-class-specifier or
13312 function-specifier. */
13313 if (cp_parser_allow_gnu_extensions_p (parser))
13315 extension_specifier
13316 = cp_parser_storage_class_specifier_opt (parser);
13317 if (!extension_specifier)
13318 extension_specifier
13319 = cp_parser_function_specifier_opt (parser,
13320 /*decl_specs=*/NULL);
13323 /* Look for the `template' keyword. */
13324 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13325 /* Let the front end know that we are processing an explicit
13326 instantiation. */
13327 begin_explicit_instantiation ();
13328 /* [temp.explicit] says that we are supposed to ignore access
13329 control while processing explicit instantiation directives. */
13330 push_deferring_access_checks (dk_no_check);
13331 /* Parse a decl-specifier-seq. */
13332 cp_parser_decl_specifier_seq (parser,
13333 CP_PARSER_FLAGS_OPTIONAL,
13334 &decl_specifiers,
13335 &declares_class_or_enum);
13336 /* If there was exactly one decl-specifier, and it declared a class,
13337 and there's no declarator, then we have an explicit type
13338 instantiation. */
13339 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13341 tree type;
13343 type = check_tag_decl (&decl_specifiers,
13344 /*explicit_type_instantiation_p=*/true);
13345 /* Turn access control back on for names used during
13346 template instantiation. */
13347 pop_deferring_access_checks ();
13348 if (type)
13349 do_type_instantiation (type, extension_specifier,
13350 /*complain=*/tf_error);
13352 else
13354 cp_declarator *declarator;
13355 tree decl;
13357 /* Parse the declarator. */
13358 declarator
13359 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13360 /*ctor_dtor_or_conv_p=*/NULL,
13361 /*parenthesized_p=*/NULL,
13362 /*member_p=*/false);
13363 if (declares_class_or_enum & 2)
13364 cp_parser_check_for_definition_in_return_type (declarator,
13365 decl_specifiers.type,
13366 decl_specifiers.locations[ds_type_spec]);
13367 if (declarator != cp_error_declarator)
13369 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13370 permerror (decl_specifiers.locations[ds_inline],
13371 "explicit instantiation shall not use"
13372 " %<inline%> specifier");
13373 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13374 permerror (decl_specifiers.locations[ds_constexpr],
13375 "explicit instantiation shall not use"
13376 " %<constexpr%> specifier");
13378 decl = grokdeclarator (declarator, &decl_specifiers,
13379 NORMAL, 0, &decl_specifiers.attributes);
13380 /* Turn access control back on for names used during
13381 template instantiation. */
13382 pop_deferring_access_checks ();
13383 /* Do the explicit instantiation. */
13384 do_decl_instantiation (decl, extension_specifier);
13386 else
13388 pop_deferring_access_checks ();
13389 /* Skip the body of the explicit instantiation. */
13390 cp_parser_skip_to_end_of_statement (parser);
13393 /* We're done with the instantiation. */
13394 end_explicit_instantiation ();
13396 cp_parser_consume_semicolon_at_end_of_statement (parser);
13398 timevar_pop (TV_TEMPLATE_INST);
13401 /* Parse an explicit-specialization.
13403 explicit-specialization:
13404 template < > declaration
13406 Although the standard says `declaration', what it really means is:
13408 explicit-specialization:
13409 template <> decl-specifier [opt] init-declarator [opt] ;
13410 template <> function-definition
13411 template <> explicit-specialization
13412 template <> template-declaration */
13414 static void
13415 cp_parser_explicit_specialization (cp_parser* parser)
13417 bool need_lang_pop;
13418 cp_token *token = cp_lexer_peek_token (parser->lexer);
13420 /* Look for the `template' keyword. */
13421 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13422 /* Look for the `<'. */
13423 cp_parser_require (parser, CPP_LESS, RT_LESS);
13424 /* Look for the `>'. */
13425 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13426 /* We have processed another parameter list. */
13427 ++parser->num_template_parameter_lists;
13428 /* [temp]
13430 A template ... explicit specialization ... shall not have C
13431 linkage. */
13432 if (current_lang_name == lang_name_c)
13434 error_at (token->location, "template specialization with C linkage");
13435 /* Give it C++ linkage to avoid confusing other parts of the
13436 front end. */
13437 push_lang_context (lang_name_cplusplus);
13438 need_lang_pop = true;
13440 else
13441 need_lang_pop = false;
13442 /* Let the front end know that we are beginning a specialization. */
13443 if (!begin_specialization ())
13445 end_specialization ();
13446 return;
13449 /* If the next keyword is `template', we need to figure out whether
13450 or not we're looking a template-declaration. */
13451 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13453 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13454 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13455 cp_parser_template_declaration_after_export (parser,
13456 /*member_p=*/false);
13457 else
13458 cp_parser_explicit_specialization (parser);
13460 else
13461 /* Parse the dependent declaration. */
13462 cp_parser_single_declaration (parser,
13463 /*checks=*/NULL,
13464 /*member_p=*/false,
13465 /*explicit_specialization_p=*/true,
13466 /*friend_p=*/NULL);
13467 /* We're done with the specialization. */
13468 end_specialization ();
13469 /* For the erroneous case of a template with C linkage, we pushed an
13470 implicit C++ linkage scope; exit that scope now. */
13471 if (need_lang_pop)
13472 pop_lang_context ();
13473 /* We're done with this parameter list. */
13474 --parser->num_template_parameter_lists;
13477 /* Parse a type-specifier.
13479 type-specifier:
13480 simple-type-specifier
13481 class-specifier
13482 enum-specifier
13483 elaborated-type-specifier
13484 cv-qualifier
13486 GNU Extension:
13488 type-specifier:
13489 __complex__
13491 Returns a representation of the type-specifier. For a
13492 class-specifier, enum-specifier, or elaborated-type-specifier, a
13493 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13495 The parser flags FLAGS is used to control type-specifier parsing.
13497 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13498 in a decl-specifier-seq.
13500 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13501 class-specifier, enum-specifier, or elaborated-type-specifier, then
13502 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13503 if a type is declared; 2 if it is defined. Otherwise, it is set to
13504 zero.
13506 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13507 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13508 is set to FALSE. */
13510 static tree
13511 cp_parser_type_specifier (cp_parser* parser,
13512 cp_parser_flags flags,
13513 cp_decl_specifier_seq *decl_specs,
13514 bool is_declaration,
13515 int* declares_class_or_enum,
13516 bool* is_cv_qualifier)
13518 tree type_spec = NULL_TREE;
13519 cp_token *token;
13520 enum rid keyword;
13521 cp_decl_spec ds = ds_last;
13523 /* Assume this type-specifier does not declare a new type. */
13524 if (declares_class_or_enum)
13525 *declares_class_or_enum = 0;
13526 /* And that it does not specify a cv-qualifier. */
13527 if (is_cv_qualifier)
13528 *is_cv_qualifier = false;
13529 /* Peek at the next token. */
13530 token = cp_lexer_peek_token (parser->lexer);
13532 /* If we're looking at a keyword, we can use that to guide the
13533 production we choose. */
13534 keyword = token->keyword;
13535 switch (keyword)
13537 case RID_ENUM:
13538 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13539 goto elaborated_type_specifier;
13541 /* Look for the enum-specifier. */
13542 type_spec = cp_parser_enum_specifier (parser);
13543 /* If that worked, we're done. */
13544 if (type_spec)
13546 if (declares_class_or_enum)
13547 *declares_class_or_enum = 2;
13548 if (decl_specs)
13549 cp_parser_set_decl_spec_type (decl_specs,
13550 type_spec,
13551 token,
13552 /*type_definition_p=*/true);
13553 return type_spec;
13555 else
13556 goto elaborated_type_specifier;
13558 /* Any of these indicate either a class-specifier, or an
13559 elaborated-type-specifier. */
13560 case RID_CLASS:
13561 case RID_STRUCT:
13562 case RID_UNION:
13563 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13564 goto elaborated_type_specifier;
13566 /* Parse tentatively so that we can back up if we don't find a
13567 class-specifier. */
13568 cp_parser_parse_tentatively (parser);
13569 /* Look for the class-specifier. */
13570 type_spec = cp_parser_class_specifier (parser);
13571 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13572 /* If that worked, we're done. */
13573 if (cp_parser_parse_definitely (parser))
13575 if (declares_class_or_enum)
13576 *declares_class_or_enum = 2;
13577 if (decl_specs)
13578 cp_parser_set_decl_spec_type (decl_specs,
13579 type_spec,
13580 token,
13581 /*type_definition_p=*/true);
13582 return type_spec;
13585 /* Fall through. */
13586 elaborated_type_specifier:
13587 /* We're declaring (not defining) a class or enum. */
13588 if (declares_class_or_enum)
13589 *declares_class_or_enum = 1;
13591 /* Fall through. */
13592 case RID_TYPENAME:
13593 /* Look for an elaborated-type-specifier. */
13594 type_spec
13595 = (cp_parser_elaborated_type_specifier
13596 (parser,
13597 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
13598 is_declaration));
13599 if (decl_specs)
13600 cp_parser_set_decl_spec_type (decl_specs,
13601 type_spec,
13602 token,
13603 /*type_definition_p=*/false);
13604 return type_spec;
13606 case RID_CONST:
13607 ds = ds_const;
13608 if (is_cv_qualifier)
13609 *is_cv_qualifier = true;
13610 break;
13612 case RID_VOLATILE:
13613 ds = ds_volatile;
13614 if (is_cv_qualifier)
13615 *is_cv_qualifier = true;
13616 break;
13618 case RID_RESTRICT:
13619 ds = ds_restrict;
13620 if (is_cv_qualifier)
13621 *is_cv_qualifier = true;
13622 break;
13624 case RID_COMPLEX:
13625 /* The `__complex__' keyword is a GNU extension. */
13626 ds = ds_complex;
13627 break;
13629 default:
13630 break;
13633 /* Handle simple keywords. */
13634 if (ds != ds_last)
13636 if (decl_specs)
13638 set_and_check_decl_spec_loc (decl_specs, ds, token);
13639 decl_specs->any_specifiers_p = true;
13641 return cp_lexer_consume_token (parser->lexer)->u.value;
13644 /* If we do not already have a type-specifier, assume we are looking
13645 at a simple-type-specifier. */
13646 type_spec = cp_parser_simple_type_specifier (parser,
13647 decl_specs,
13648 flags);
13650 /* If we didn't find a type-specifier, and a type-specifier was not
13651 optional in this context, issue an error message. */
13652 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13654 cp_parser_error (parser, "expected type specifier");
13655 return error_mark_node;
13658 return type_spec;
13661 /* Parse a simple-type-specifier.
13663 simple-type-specifier:
13664 :: [opt] nested-name-specifier [opt] type-name
13665 :: [opt] nested-name-specifier template template-id
13666 char
13667 wchar_t
13668 bool
13669 short
13671 long
13672 signed
13673 unsigned
13674 float
13675 double
13676 void
13678 C++0x Extension:
13680 simple-type-specifier:
13681 auto
13682 decltype ( expression )
13683 char16_t
13684 char32_t
13685 __underlying_type ( type-id )
13687 GNU Extension:
13689 simple-type-specifier:
13690 __int128
13691 __typeof__ unary-expression
13692 __typeof__ ( type-id )
13694 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13695 appropriately updated. */
13697 static tree
13698 cp_parser_simple_type_specifier (cp_parser* parser,
13699 cp_decl_specifier_seq *decl_specs,
13700 cp_parser_flags flags)
13702 tree type = NULL_TREE;
13703 cp_token *token;
13705 /* Peek at the next token. */
13706 token = cp_lexer_peek_token (parser->lexer);
13708 /* If we're looking at a keyword, things are easy. */
13709 switch (token->keyword)
13711 case RID_CHAR:
13712 if (decl_specs)
13713 decl_specs->explicit_char_p = true;
13714 type = char_type_node;
13715 break;
13716 case RID_CHAR16:
13717 type = char16_type_node;
13718 break;
13719 case RID_CHAR32:
13720 type = char32_type_node;
13721 break;
13722 case RID_WCHAR:
13723 type = wchar_type_node;
13724 break;
13725 case RID_BOOL:
13726 type = boolean_type_node;
13727 break;
13728 case RID_SHORT:
13729 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
13730 type = short_integer_type_node;
13731 break;
13732 case RID_INT:
13733 if (decl_specs)
13734 decl_specs->explicit_int_p = true;
13735 type = integer_type_node;
13736 break;
13737 case RID_INT128:
13738 if (!int128_integer_type_node)
13739 break;
13740 if (decl_specs)
13741 decl_specs->explicit_int128_p = true;
13742 type = int128_integer_type_node;
13743 break;
13744 case RID_LONG:
13745 if (decl_specs)
13746 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
13747 type = long_integer_type_node;
13748 break;
13749 case RID_SIGNED:
13750 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
13751 type = integer_type_node;
13752 break;
13753 case RID_UNSIGNED:
13754 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
13755 type = unsigned_type_node;
13756 break;
13757 case RID_FLOAT:
13758 type = float_type_node;
13759 break;
13760 case RID_DOUBLE:
13761 type = double_type_node;
13762 break;
13763 case RID_VOID:
13764 type = void_type_node;
13765 break;
13767 case RID_AUTO:
13768 maybe_warn_cpp0x (CPP0X_AUTO);
13769 type = make_auto ();
13770 break;
13772 case RID_DECLTYPE:
13773 /* Since DR 743, decltype can either be a simple-type-specifier by
13774 itself or begin a nested-name-specifier. Parsing it will replace
13775 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13776 handling below decide what to do. */
13777 cp_parser_decltype (parser);
13778 cp_lexer_set_token_position (parser->lexer, token);
13779 break;
13781 case RID_TYPEOF:
13782 /* Consume the `typeof' token. */
13783 cp_lexer_consume_token (parser->lexer);
13784 /* Parse the operand to `typeof'. */
13785 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13786 /* If it is not already a TYPE, take its type. */
13787 if (!TYPE_P (type))
13788 type = finish_typeof (type);
13790 if (decl_specs)
13791 cp_parser_set_decl_spec_type (decl_specs, type,
13792 token,
13793 /*type_definition_p=*/false);
13795 return type;
13797 case RID_UNDERLYING_TYPE:
13798 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13799 if (decl_specs)
13800 cp_parser_set_decl_spec_type (decl_specs, type,
13801 token,
13802 /*type_definition_p=*/false);
13804 return type;
13806 case RID_BASES:
13807 case RID_DIRECT_BASES:
13808 type = cp_parser_trait_expr (parser, token->keyword);
13809 if (decl_specs)
13810 cp_parser_set_decl_spec_type (decl_specs, type,
13811 token,
13812 /*type_definition_p=*/false);
13813 return type;
13814 default:
13815 break;
13818 /* If token is an already-parsed decltype not followed by ::,
13819 it's a simple-type-specifier. */
13820 if (token->type == CPP_DECLTYPE
13821 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13823 type = token->u.value;
13824 if (decl_specs)
13825 cp_parser_set_decl_spec_type (decl_specs, type,
13826 token,
13827 /*type_definition_p=*/false);
13828 cp_lexer_consume_token (parser->lexer);
13829 return type;
13832 /* If the type-specifier was for a built-in type, we're done. */
13833 if (type)
13835 /* Record the type. */
13836 if (decl_specs
13837 && (token->keyword != RID_SIGNED
13838 && token->keyword != RID_UNSIGNED
13839 && token->keyword != RID_SHORT
13840 && token->keyword != RID_LONG))
13841 cp_parser_set_decl_spec_type (decl_specs,
13842 type,
13843 token,
13844 /*type_definition_p=*/false);
13845 if (decl_specs)
13846 decl_specs->any_specifiers_p = true;
13848 /* Consume the token. */
13849 cp_lexer_consume_token (parser->lexer);
13851 /* There is no valid C++ program where a non-template type is
13852 followed by a "<". That usually indicates that the user thought
13853 that the type was a template. */
13854 cp_parser_check_for_invalid_template_id (parser, type, none_type,
13855 token->location);
13857 return TYPE_NAME (type);
13860 /* The type-specifier must be a user-defined type. */
13861 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13863 bool qualified_p;
13864 bool global_p;
13866 /* Don't gobble tokens or issue error messages if this is an
13867 optional type-specifier. */
13868 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13869 cp_parser_parse_tentatively (parser);
13871 /* Look for the optional `::' operator. */
13872 global_p
13873 = (cp_parser_global_scope_opt (parser,
13874 /*current_scope_valid_p=*/false)
13875 != NULL_TREE);
13876 /* Look for the nested-name specifier. */
13877 qualified_p
13878 = (cp_parser_nested_name_specifier_opt (parser,
13879 /*typename_keyword_p=*/false,
13880 /*check_dependency_p=*/true,
13881 /*type_p=*/false,
13882 /*is_declaration=*/false)
13883 != NULL_TREE);
13884 token = cp_lexer_peek_token (parser->lexer);
13885 /* If we have seen a nested-name-specifier, and the next token
13886 is `template', then we are using the template-id production. */
13887 if (parser->scope
13888 && cp_parser_optional_template_keyword (parser))
13890 /* Look for the template-id. */
13891 type = cp_parser_template_id (parser,
13892 /*template_keyword_p=*/true,
13893 /*check_dependency_p=*/true,
13894 none_type,
13895 /*is_declaration=*/false);
13896 /* If the template-id did not name a type, we are out of
13897 luck. */
13898 if (TREE_CODE (type) != TYPE_DECL)
13900 cp_parser_error (parser, "expected template-id for type");
13901 type = NULL_TREE;
13904 /* Otherwise, look for a type-name. */
13905 else
13906 type = cp_parser_type_name (parser);
13907 /* Keep track of all name-lookups performed in class scopes. */
13908 if (type
13909 && !global_p
13910 && !qualified_p
13911 && TREE_CODE (type) == TYPE_DECL
13912 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13913 maybe_note_name_used_in_class (DECL_NAME (type), type);
13914 /* If it didn't work out, we don't have a TYPE. */
13915 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13916 && !cp_parser_parse_definitely (parser))
13917 type = NULL_TREE;
13918 if (type && decl_specs)
13919 cp_parser_set_decl_spec_type (decl_specs, type,
13920 token,
13921 /*type_definition_p=*/false);
13924 /* If we didn't get a type-name, issue an error message. */
13925 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13927 cp_parser_error (parser, "expected type-name");
13928 return error_mark_node;
13931 if (type && type != error_mark_node)
13933 /* See if TYPE is an Objective-C type, and if so, parse and
13934 accept any protocol references following it. Do this before
13935 the cp_parser_check_for_invalid_template_id() call, because
13936 Objective-C types can be followed by '<...>' which would
13937 enclose protocol names rather than template arguments, and so
13938 everything is fine. */
13939 if (c_dialect_objc () && !parser->scope
13940 && (objc_is_id (type) || objc_is_class_name (type)))
13942 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13943 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13945 /* Clobber the "unqualified" type previously entered into
13946 DECL_SPECS with the new, improved protocol-qualified version. */
13947 if (decl_specs)
13948 decl_specs->type = qual_type;
13950 return qual_type;
13953 /* There is no valid C++ program where a non-template type is
13954 followed by a "<". That usually indicates that the user
13955 thought that the type was a template. */
13956 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13957 none_type,
13958 token->location);
13961 return type;
13964 /* Parse a type-name.
13966 type-name:
13967 class-name
13968 enum-name
13969 typedef-name
13970 simple-template-id [in c++0x]
13972 enum-name:
13973 identifier
13975 typedef-name:
13976 identifier
13978 Returns a TYPE_DECL for the type. */
13980 static tree
13981 cp_parser_type_name (cp_parser* parser)
13983 tree type_decl;
13985 /* We can't know yet whether it is a class-name or not. */
13986 cp_parser_parse_tentatively (parser);
13987 /* Try a class-name. */
13988 type_decl = cp_parser_class_name (parser,
13989 /*typename_keyword_p=*/false,
13990 /*template_keyword_p=*/false,
13991 none_type,
13992 /*check_dependency_p=*/true,
13993 /*class_head_p=*/false,
13994 /*is_declaration=*/false);
13995 /* If it's not a class-name, keep looking. */
13996 if (!cp_parser_parse_definitely (parser))
13998 if (cxx_dialect < cxx0x)
13999 /* It must be a typedef-name or an enum-name. */
14000 return cp_parser_nonclass_name (parser);
14002 cp_parser_parse_tentatively (parser);
14003 /* It is either a simple-template-id representing an
14004 instantiation of an alias template... */
14005 type_decl = cp_parser_template_id (parser,
14006 /*template_keyword_p=*/false,
14007 /*check_dependency_p=*/false,
14008 none_type,
14009 /*is_declaration=*/false);
14010 /* Note that this must be an instantiation of an alias template
14011 because [temp.names]/6 says:
14013 A template-id that names an alias template specialization
14014 is a type-name.
14016 Whereas [temp.names]/7 says:
14018 A simple-template-id that names a class template
14019 specialization is a class-name. */
14020 if (type_decl != NULL_TREE
14021 && TREE_CODE (type_decl) == TYPE_DECL
14022 && TYPE_DECL_ALIAS_P (type_decl))
14023 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14024 else
14025 cp_parser_simulate_error (parser);
14027 if (!cp_parser_parse_definitely (parser))
14028 /* ... Or a typedef-name or an enum-name. */
14029 return cp_parser_nonclass_name (parser);
14032 return type_decl;
14035 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14037 enum-name:
14038 identifier
14040 typedef-name:
14041 identifier
14043 Returns a TYPE_DECL for the type. */
14045 static tree
14046 cp_parser_nonclass_name (cp_parser* parser)
14048 tree type_decl;
14049 tree identifier;
14051 cp_token *token = cp_lexer_peek_token (parser->lexer);
14052 identifier = cp_parser_identifier (parser);
14053 if (identifier == error_mark_node)
14054 return error_mark_node;
14056 /* Look up the type-name. */
14057 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14059 if (TREE_CODE (type_decl) == USING_DECL)
14061 if (!DECL_DEPENDENT_P (type_decl))
14062 type_decl = strip_using_decl (type_decl);
14063 else if (USING_DECL_TYPENAME_P (type_decl))
14065 /* We have found a type introduced by a using
14066 declaration at class scope that refers to a dependent
14067 type.
14069 using typename :: [opt] nested-name-specifier unqualified-id ;
14071 type_decl = make_typename_type (TREE_TYPE (type_decl),
14072 DECL_NAME (type_decl),
14073 typename_type, tf_error);
14074 if (type_decl != error_mark_node)
14075 type_decl = TYPE_NAME (type_decl);
14079 if (TREE_CODE (type_decl) != TYPE_DECL
14080 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14082 /* See if this is an Objective-C type. */
14083 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14084 tree type = objc_get_protocol_qualified_type (identifier, protos);
14085 if (type)
14086 type_decl = TYPE_NAME (type);
14089 /* Issue an error if we did not find a type-name. */
14090 if (TREE_CODE (type_decl) != TYPE_DECL
14091 /* In Objective-C, we have the complication that class names are
14092 normally type names and start declarations (eg, the
14093 "NSObject" in "NSObject *object;"), but can be used in an
14094 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14095 is an expression. So, a classname followed by a dot is not a
14096 valid type-name. */
14097 || (objc_is_class_name (TREE_TYPE (type_decl))
14098 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14100 if (!cp_parser_simulate_error (parser))
14101 cp_parser_name_lookup_error (parser, identifier, type_decl,
14102 NLE_TYPE, token->location);
14103 return error_mark_node;
14105 /* Remember that the name was used in the definition of the
14106 current class so that we can check later to see if the
14107 meaning would have been different after the class was
14108 entirely defined. */
14109 else if (type_decl != error_mark_node
14110 && !parser->scope)
14111 maybe_note_name_used_in_class (identifier, type_decl);
14113 return type_decl;
14116 /* Parse an elaborated-type-specifier. Note that the grammar given
14117 here incorporates the resolution to DR68.
14119 elaborated-type-specifier:
14120 class-key :: [opt] nested-name-specifier [opt] identifier
14121 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14122 enum-key :: [opt] nested-name-specifier [opt] identifier
14123 typename :: [opt] nested-name-specifier identifier
14124 typename :: [opt] nested-name-specifier template [opt]
14125 template-id
14127 GNU extension:
14129 elaborated-type-specifier:
14130 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14131 class-key attributes :: [opt] nested-name-specifier [opt]
14132 template [opt] template-id
14133 enum attributes :: [opt] nested-name-specifier [opt] identifier
14135 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14136 declared `friend'. If IS_DECLARATION is TRUE, then this
14137 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14138 something is being declared.
14140 Returns the TYPE specified. */
14142 static tree
14143 cp_parser_elaborated_type_specifier (cp_parser* parser,
14144 bool is_friend,
14145 bool is_declaration)
14147 enum tag_types tag_type;
14148 tree identifier;
14149 tree type = NULL_TREE;
14150 tree attributes = NULL_TREE;
14151 tree globalscope;
14152 cp_token *token = NULL;
14154 /* See if we're looking at the `enum' keyword. */
14155 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14157 /* Consume the `enum' token. */
14158 cp_lexer_consume_token (parser->lexer);
14159 /* Remember that it's an enumeration type. */
14160 tag_type = enum_type;
14161 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14162 enums) is used here. */
14163 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14164 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14166 pedwarn (input_location, 0, "elaborated-type-specifier "
14167 "for a scoped enum must not use the %<%D%> keyword",
14168 cp_lexer_peek_token (parser->lexer)->u.value);
14169 /* Consume the `struct' or `class' and parse it anyway. */
14170 cp_lexer_consume_token (parser->lexer);
14172 /* Parse the attributes. */
14173 attributes = cp_parser_attributes_opt (parser);
14175 /* Or, it might be `typename'. */
14176 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14177 RID_TYPENAME))
14179 /* Consume the `typename' token. */
14180 cp_lexer_consume_token (parser->lexer);
14181 /* Remember that it's a `typename' type. */
14182 tag_type = typename_type;
14184 /* Otherwise it must be a class-key. */
14185 else
14187 tag_type = cp_parser_class_key (parser);
14188 if (tag_type == none_type)
14189 return error_mark_node;
14190 /* Parse the attributes. */
14191 attributes = cp_parser_attributes_opt (parser);
14194 /* Look for the `::' operator. */
14195 globalscope = cp_parser_global_scope_opt (parser,
14196 /*current_scope_valid_p=*/false);
14197 /* Look for the nested-name-specifier. */
14198 if (tag_type == typename_type && !globalscope)
14200 if (!cp_parser_nested_name_specifier (parser,
14201 /*typename_keyword_p=*/true,
14202 /*check_dependency_p=*/true,
14203 /*type_p=*/true,
14204 is_declaration))
14205 return error_mark_node;
14207 else
14208 /* Even though `typename' is not present, the proposed resolution
14209 to Core Issue 180 says that in `class A<T>::B', `B' should be
14210 considered a type-name, even if `A<T>' is dependent. */
14211 cp_parser_nested_name_specifier_opt (parser,
14212 /*typename_keyword_p=*/true,
14213 /*check_dependency_p=*/true,
14214 /*type_p=*/true,
14215 is_declaration);
14216 /* For everything but enumeration types, consider a template-id.
14217 For an enumeration type, consider only a plain identifier. */
14218 if (tag_type != enum_type)
14220 bool template_p = false;
14221 tree decl;
14223 /* Allow the `template' keyword. */
14224 template_p = cp_parser_optional_template_keyword (parser);
14225 /* If we didn't see `template', we don't know if there's a
14226 template-id or not. */
14227 if (!template_p)
14228 cp_parser_parse_tentatively (parser);
14229 /* Parse the template-id. */
14230 token = cp_lexer_peek_token (parser->lexer);
14231 decl = cp_parser_template_id (parser, template_p,
14232 /*check_dependency_p=*/true,
14233 tag_type,
14234 is_declaration);
14235 /* If we didn't find a template-id, look for an ordinary
14236 identifier. */
14237 if (!template_p && !cp_parser_parse_definitely (parser))
14239 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14240 in effect, then we must assume that, upon instantiation, the
14241 template will correspond to a class. */
14242 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14243 && tag_type == typename_type)
14244 type = make_typename_type (parser->scope, decl,
14245 typename_type,
14246 /*complain=*/tf_error);
14247 /* If the `typename' keyword is in effect and DECL is not a type
14248 decl. Then type is non existant. */
14249 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14250 type = NULL_TREE;
14251 else
14252 type = check_elaborated_type_specifier (tag_type, decl,
14253 /*allow_template_p=*/true);
14256 if (!type)
14258 token = cp_lexer_peek_token (parser->lexer);
14259 identifier = cp_parser_identifier (parser);
14261 if (identifier == error_mark_node)
14263 parser->scope = NULL_TREE;
14264 return error_mark_node;
14267 /* For a `typename', we needn't call xref_tag. */
14268 if (tag_type == typename_type
14269 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14270 return cp_parser_make_typename_type (parser, parser->scope,
14271 identifier,
14272 token->location);
14273 /* Look up a qualified name in the usual way. */
14274 if (parser->scope)
14276 tree decl;
14277 tree ambiguous_decls;
14279 decl = cp_parser_lookup_name (parser, identifier,
14280 tag_type,
14281 /*is_template=*/false,
14282 /*is_namespace=*/false,
14283 /*check_dependency=*/true,
14284 &ambiguous_decls,
14285 token->location);
14287 /* If the lookup was ambiguous, an error will already have been
14288 issued. */
14289 if (ambiguous_decls)
14290 return error_mark_node;
14292 /* If we are parsing friend declaration, DECL may be a
14293 TEMPLATE_DECL tree node here. However, we need to check
14294 whether this TEMPLATE_DECL results in valid code. Consider
14295 the following example:
14297 namespace N {
14298 template <class T> class C {};
14300 class X {
14301 template <class T> friend class N::C; // #1, valid code
14303 template <class T> class Y {
14304 friend class N::C; // #2, invalid code
14307 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14308 name lookup of `N::C'. We see that friend declaration must
14309 be template for the code to be valid. Note that
14310 processing_template_decl does not work here since it is
14311 always 1 for the above two cases. */
14313 decl = (cp_parser_maybe_treat_template_as_class
14314 (decl, /*tag_name_p=*/is_friend
14315 && parser->num_template_parameter_lists));
14317 if (TREE_CODE (decl) != TYPE_DECL)
14319 cp_parser_diagnose_invalid_type_name (parser,
14320 parser->scope,
14321 identifier,
14322 token->location);
14323 return error_mark_node;
14326 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14328 bool allow_template = (parser->num_template_parameter_lists
14329 || DECL_SELF_REFERENCE_P (decl));
14330 type = check_elaborated_type_specifier (tag_type, decl,
14331 allow_template);
14333 if (type == error_mark_node)
14334 return error_mark_node;
14337 /* Forward declarations of nested types, such as
14339 class C1::C2;
14340 class C1::C2::C3;
14342 are invalid unless all components preceding the final '::'
14343 are complete. If all enclosing types are complete, these
14344 declarations become merely pointless.
14346 Invalid forward declarations of nested types are errors
14347 caught elsewhere in parsing. Those that are pointless arrive
14348 here. */
14350 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14351 && !is_friend && !processing_explicit_instantiation)
14352 warning (0, "declaration %qD does not declare anything", decl);
14354 type = TREE_TYPE (decl);
14356 else
14358 /* An elaborated-type-specifier sometimes introduces a new type and
14359 sometimes names an existing type. Normally, the rule is that it
14360 introduces a new type only if there is not an existing type of
14361 the same name already in scope. For example, given:
14363 struct S {};
14364 void f() { struct S s; }
14366 the `struct S' in the body of `f' is the same `struct S' as in
14367 the global scope; the existing definition is used. However, if
14368 there were no global declaration, this would introduce a new
14369 local class named `S'.
14371 An exception to this rule applies to the following code:
14373 namespace N { struct S; }
14375 Here, the elaborated-type-specifier names a new type
14376 unconditionally; even if there is already an `S' in the
14377 containing scope this declaration names a new type.
14378 This exception only applies if the elaborated-type-specifier
14379 forms the complete declaration:
14381 [class.name]
14383 A declaration consisting solely of `class-key identifier ;' is
14384 either a redeclaration of the name in the current scope or a
14385 forward declaration of the identifier as a class name. It
14386 introduces the name into the current scope.
14388 We are in this situation precisely when the next token is a `;'.
14390 An exception to the exception is that a `friend' declaration does
14391 *not* name a new type; i.e., given:
14393 struct S { friend struct T; };
14395 `T' is not a new type in the scope of `S'.
14397 Also, `new struct S' or `sizeof (struct S)' never results in the
14398 definition of a new type; a new type can only be declared in a
14399 declaration context. */
14401 tag_scope ts;
14402 bool template_p;
14404 if (is_friend)
14405 /* Friends have special name lookup rules. */
14406 ts = ts_within_enclosing_non_class;
14407 else if (is_declaration
14408 && cp_lexer_next_token_is (parser->lexer,
14409 CPP_SEMICOLON))
14410 /* This is a `class-key identifier ;' */
14411 ts = ts_current;
14412 else
14413 ts = ts_global;
14415 template_p =
14416 (parser->num_template_parameter_lists
14417 && (cp_parser_next_token_starts_class_definition_p (parser)
14418 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14419 /* An unqualified name was used to reference this type, so
14420 there were no qualifying templates. */
14421 if (!cp_parser_check_template_parameters (parser,
14422 /*num_templates=*/0,
14423 token->location,
14424 /*declarator=*/NULL))
14425 return error_mark_node;
14426 type = xref_tag (tag_type, identifier, ts, template_p);
14430 if (type == error_mark_node)
14431 return error_mark_node;
14433 /* Allow attributes on forward declarations of classes. */
14434 if (attributes)
14436 if (TREE_CODE (type) == TYPENAME_TYPE)
14437 warning (OPT_Wattributes,
14438 "attributes ignored on uninstantiated type");
14439 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14440 && ! processing_explicit_instantiation)
14441 warning (OPT_Wattributes,
14442 "attributes ignored on template instantiation");
14443 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14444 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14445 else
14446 warning (OPT_Wattributes,
14447 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14450 if (tag_type != enum_type)
14452 /* Indicate whether this class was declared as a `class' or as a
14453 `struct'. */
14454 if (TREE_CODE (type) == RECORD_TYPE)
14455 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14456 cp_parser_check_class_key (tag_type, type);
14459 /* A "<" cannot follow an elaborated type specifier. If that
14460 happens, the user was probably trying to form a template-id. */
14461 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
14462 token->location);
14464 return type;
14467 /* Parse an enum-specifier.
14469 enum-specifier:
14470 enum-head { enumerator-list [opt] }
14471 enum-head { enumerator-list , } [C++0x]
14473 enum-head:
14474 enum-key identifier [opt] enum-base [opt]
14475 enum-key nested-name-specifier identifier enum-base [opt]
14477 enum-key:
14478 enum
14479 enum class [C++0x]
14480 enum struct [C++0x]
14482 enum-base: [C++0x]
14483 : type-specifier-seq
14485 opaque-enum-specifier:
14486 enum-key identifier enum-base [opt] ;
14488 GNU Extensions:
14489 enum-key attributes[opt] identifier [opt] enum-base [opt]
14490 { enumerator-list [opt] }attributes[opt]
14491 enum-key attributes[opt] identifier [opt] enum-base [opt]
14492 { enumerator-list, }attributes[opt] [C++0x]
14494 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14495 if the token stream isn't an enum-specifier after all. */
14497 static tree
14498 cp_parser_enum_specifier (cp_parser* parser)
14500 tree identifier;
14501 tree type = NULL_TREE;
14502 tree prev_scope;
14503 tree nested_name_specifier = NULL_TREE;
14504 tree attributes;
14505 bool scoped_enum_p = false;
14506 bool has_underlying_type = false;
14507 bool nested_being_defined = false;
14508 bool new_value_list = false;
14509 bool is_new_type = false;
14510 bool is_anonymous = false;
14511 tree underlying_type = NULL_TREE;
14512 cp_token *type_start_token = NULL;
14513 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14515 parser->colon_corrects_to_scope_p = false;
14517 /* Parse tentatively so that we can back up if we don't find a
14518 enum-specifier. */
14519 cp_parser_parse_tentatively (parser);
14521 /* Caller guarantees that the current token is 'enum', an identifier
14522 possibly follows, and the token after that is an opening brace.
14523 If we don't have an identifier, fabricate an anonymous name for
14524 the enumeration being defined. */
14525 cp_lexer_consume_token (parser->lexer);
14527 /* Parse the "class" or "struct", which indicates a scoped
14528 enumeration type in C++0x. */
14529 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14530 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14532 if (cxx_dialect < cxx0x)
14533 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14535 /* Consume the `struct' or `class' token. */
14536 cp_lexer_consume_token (parser->lexer);
14538 scoped_enum_p = true;
14541 attributes = cp_parser_attributes_opt (parser);
14543 /* Clear the qualification. */
14544 parser->scope = NULL_TREE;
14545 parser->qualifying_scope = NULL_TREE;
14546 parser->object_scope = NULL_TREE;
14548 /* Figure out in what scope the declaration is being placed. */
14549 prev_scope = current_scope ();
14551 type_start_token = cp_lexer_peek_token (parser->lexer);
14553 push_deferring_access_checks (dk_no_check);
14554 nested_name_specifier
14555 = cp_parser_nested_name_specifier_opt (parser,
14556 /*typename_keyword_p=*/true,
14557 /*check_dependency_p=*/false,
14558 /*type_p=*/false,
14559 /*is_declaration=*/false);
14561 if (nested_name_specifier)
14563 tree name;
14565 identifier = cp_parser_identifier (parser);
14566 name = cp_parser_lookup_name (parser, identifier,
14567 enum_type,
14568 /*is_template=*/false,
14569 /*is_namespace=*/false,
14570 /*check_dependency=*/true,
14571 /*ambiguous_decls=*/NULL,
14572 input_location);
14573 if (name)
14575 type = TREE_TYPE (name);
14576 if (TREE_CODE (type) == TYPENAME_TYPE)
14578 /* Are template enums allowed in ISO? */
14579 if (template_parm_scope_p ())
14580 pedwarn (type_start_token->location, OPT_Wpedantic,
14581 "%qD is an enumeration template", name);
14582 /* ignore a typename reference, for it will be solved by name
14583 in start_enum. */
14584 type = NULL_TREE;
14587 else
14588 error_at (type_start_token->location,
14589 "%qD is not an enumerator-name", identifier);
14591 else
14593 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14594 identifier = cp_parser_identifier (parser);
14595 else
14597 identifier = make_anon_name ();
14598 is_anonymous = true;
14601 pop_deferring_access_checks ();
14603 /* Check for the `:' that denotes a specified underlying type in C++0x.
14604 Note that a ':' could also indicate a bitfield width, however. */
14605 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14607 cp_decl_specifier_seq type_specifiers;
14609 /* Consume the `:'. */
14610 cp_lexer_consume_token (parser->lexer);
14612 /* Parse the type-specifier-seq. */
14613 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14614 /*is_trailing_return=*/false,
14615 &type_specifiers);
14617 /* At this point this is surely not elaborated type specifier. */
14618 if (!cp_parser_parse_definitely (parser))
14619 return NULL_TREE;
14621 if (cxx_dialect < cxx0x)
14622 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14624 has_underlying_type = true;
14626 /* If that didn't work, stop. */
14627 if (type_specifiers.type != error_mark_node)
14629 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14630 /*initialized=*/0, NULL);
14631 if (underlying_type == error_mark_node)
14632 underlying_type = NULL_TREE;
14636 /* Look for the `{' but don't consume it yet. */
14637 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14639 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14641 cp_parser_error (parser, "expected %<{%>");
14642 if (has_underlying_type)
14644 type = NULL_TREE;
14645 goto out;
14648 /* An opaque-enum-specifier must have a ';' here. */
14649 if ((scoped_enum_p || underlying_type)
14650 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14652 cp_parser_error (parser, "expected %<;%> or %<{%>");
14653 if (has_underlying_type)
14655 type = NULL_TREE;
14656 goto out;
14661 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14662 return NULL_TREE;
14664 if (nested_name_specifier)
14666 if (CLASS_TYPE_P (nested_name_specifier))
14668 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14669 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14670 push_scope (nested_name_specifier);
14672 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14674 push_nested_namespace (nested_name_specifier);
14678 /* Issue an error message if type-definitions are forbidden here. */
14679 if (!cp_parser_check_type_definition (parser))
14680 type = error_mark_node;
14681 else
14682 /* Create the new type. We do this before consuming the opening
14683 brace so the enum will be recorded as being on the line of its
14684 tag (or the 'enum' keyword, if there is no tag). */
14685 type = start_enum (identifier, type, underlying_type,
14686 scoped_enum_p, &is_new_type);
14688 /* If the next token is not '{' it is an opaque-enum-specifier or an
14689 elaborated-type-specifier. */
14690 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14692 timevar_push (TV_PARSE_ENUM);
14693 if (nested_name_specifier)
14695 /* The following catches invalid code such as:
14696 enum class S<int>::E { A, B, C }; */
14697 if (!processing_specialization
14698 && CLASS_TYPE_P (nested_name_specifier)
14699 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14700 error_at (type_start_token->location, "cannot add an enumerator "
14701 "list to a template instantiation");
14703 /* If that scope does not contain the scope in which the
14704 class was originally declared, the program is invalid. */
14705 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14707 if (at_namespace_scope_p ())
14708 error_at (type_start_token->location,
14709 "declaration of %qD in namespace %qD which does not "
14710 "enclose %qD",
14711 type, prev_scope, nested_name_specifier);
14712 else
14713 error_at (type_start_token->location,
14714 "declaration of %qD in %qD which does not enclose %qD",
14715 type, prev_scope, nested_name_specifier);
14716 type = error_mark_node;
14720 if (scoped_enum_p)
14721 begin_scope (sk_scoped_enum, type);
14723 /* Consume the opening brace. */
14724 cp_lexer_consume_token (parser->lexer);
14726 if (type == error_mark_node)
14727 ; /* Nothing to add */
14728 else if (OPAQUE_ENUM_P (type)
14729 || (cxx_dialect > cxx98 && processing_specialization))
14731 new_value_list = true;
14732 SET_OPAQUE_ENUM_P (type, false);
14733 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14735 else
14737 error_at (type_start_token->location, "multiple definition of %q#T", type);
14738 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14739 "previous definition here");
14740 type = error_mark_node;
14743 if (type == error_mark_node)
14744 cp_parser_skip_to_end_of_block_or_statement (parser);
14745 /* If the next token is not '}', then there are some enumerators. */
14746 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14747 cp_parser_enumerator_list (parser, type);
14749 /* Consume the final '}'. */
14750 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14752 if (scoped_enum_p)
14753 finish_scope ();
14754 timevar_pop (TV_PARSE_ENUM);
14756 else
14758 /* If a ';' follows, then it is an opaque-enum-specifier
14759 and additional restrictions apply. */
14760 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14762 if (is_anonymous)
14763 error_at (type_start_token->location,
14764 "opaque-enum-specifier without name");
14765 else if (nested_name_specifier)
14766 error_at (type_start_token->location,
14767 "opaque-enum-specifier must use a simple identifier");
14771 /* Look for trailing attributes to apply to this enumeration, and
14772 apply them if appropriate. */
14773 if (cp_parser_allow_gnu_extensions_p (parser))
14775 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
14776 trailing_attr = chainon (trailing_attr, attributes);
14777 cplus_decl_attributes (&type,
14778 trailing_attr,
14779 (int) ATTR_FLAG_TYPE_IN_PLACE);
14782 /* Finish up the enumeration. */
14783 if (type != error_mark_node)
14785 if (new_value_list)
14786 finish_enum_value_list (type);
14787 if (is_new_type)
14788 finish_enum (type);
14791 if (nested_name_specifier)
14793 if (CLASS_TYPE_P (nested_name_specifier))
14795 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14796 pop_scope (nested_name_specifier);
14798 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14800 pop_nested_namespace (nested_name_specifier);
14803 out:
14804 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14805 return type;
14808 /* Parse an enumerator-list. The enumerators all have the indicated
14809 TYPE.
14811 enumerator-list:
14812 enumerator-definition
14813 enumerator-list , enumerator-definition */
14815 static void
14816 cp_parser_enumerator_list (cp_parser* parser, tree type)
14818 while (true)
14820 /* Parse an enumerator-definition. */
14821 cp_parser_enumerator_definition (parser, type);
14823 /* If the next token is not a ',', we've reached the end of
14824 the list. */
14825 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14826 break;
14827 /* Otherwise, consume the `,' and keep going. */
14828 cp_lexer_consume_token (parser->lexer);
14829 /* If the next token is a `}', there is a trailing comma. */
14830 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14832 if (cxx_dialect < cxx0x && !in_system_header)
14833 pedwarn (input_location, OPT_Wpedantic,
14834 "comma at end of enumerator list");
14835 break;
14840 /* Parse an enumerator-definition. The enumerator has the indicated
14841 TYPE.
14843 enumerator-definition:
14844 enumerator
14845 enumerator = constant-expression
14847 enumerator:
14848 identifier */
14850 static void
14851 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14853 tree identifier;
14854 tree value;
14855 location_t loc;
14857 /* Save the input location because we are interested in the location
14858 of the identifier and not the location of the explicit value. */
14859 loc = cp_lexer_peek_token (parser->lexer)->location;
14861 /* Look for the identifier. */
14862 identifier = cp_parser_identifier (parser);
14863 if (identifier == error_mark_node)
14864 return;
14866 /* If the next token is an '=', then there is an explicit value. */
14867 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14869 /* Consume the `=' token. */
14870 cp_lexer_consume_token (parser->lexer);
14871 /* Parse the value. */
14872 value = cp_parser_constant_expression (parser,
14873 /*allow_non_constant_p=*/false,
14874 NULL);
14876 else
14877 value = NULL_TREE;
14879 /* If we are processing a template, make sure the initializer of the
14880 enumerator doesn't contain any bare template parameter pack. */
14881 if (check_for_bare_parameter_packs (value))
14882 value = error_mark_node;
14884 /* integral_constant_value will pull out this expression, so make sure
14885 it's folded as appropriate. */
14886 value = fold_non_dependent_expr (value);
14888 /* Create the enumerator. */
14889 build_enumerator (identifier, value, type, loc);
14892 /* Parse a namespace-name.
14894 namespace-name:
14895 original-namespace-name
14896 namespace-alias
14898 Returns the NAMESPACE_DECL for the namespace. */
14900 static tree
14901 cp_parser_namespace_name (cp_parser* parser)
14903 tree identifier;
14904 tree namespace_decl;
14906 cp_token *token = cp_lexer_peek_token (parser->lexer);
14908 /* Get the name of the namespace. */
14909 identifier = cp_parser_identifier (parser);
14910 if (identifier == error_mark_node)
14911 return error_mark_node;
14913 /* Look up the identifier in the currently active scope. Look only
14914 for namespaces, due to:
14916 [basic.lookup.udir]
14918 When looking up a namespace-name in a using-directive or alias
14919 definition, only namespace names are considered.
14921 And:
14923 [basic.lookup.qual]
14925 During the lookup of a name preceding the :: scope resolution
14926 operator, object, function, and enumerator names are ignored.
14928 (Note that cp_parser_qualifying_entity only calls this
14929 function if the token after the name is the scope resolution
14930 operator.) */
14931 namespace_decl = cp_parser_lookup_name (parser, identifier,
14932 none_type,
14933 /*is_template=*/false,
14934 /*is_namespace=*/true,
14935 /*check_dependency=*/true,
14936 /*ambiguous_decls=*/NULL,
14937 token->location);
14938 /* If it's not a namespace, issue an error. */
14939 if (namespace_decl == error_mark_node
14940 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14942 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14943 error_at (token->location, "%qD is not a namespace-name", identifier);
14944 cp_parser_error (parser, "expected namespace-name");
14945 namespace_decl = error_mark_node;
14948 return namespace_decl;
14951 /* Parse a namespace-definition.
14953 namespace-definition:
14954 named-namespace-definition
14955 unnamed-namespace-definition
14957 named-namespace-definition:
14958 original-namespace-definition
14959 extension-namespace-definition
14961 original-namespace-definition:
14962 namespace identifier { namespace-body }
14964 extension-namespace-definition:
14965 namespace original-namespace-name { namespace-body }
14967 unnamed-namespace-definition:
14968 namespace { namespace-body } */
14970 static void
14971 cp_parser_namespace_definition (cp_parser* parser)
14973 tree identifier, attribs;
14974 bool has_visibility;
14975 bool is_inline;
14977 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14979 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14980 is_inline = true;
14981 cp_lexer_consume_token (parser->lexer);
14983 else
14984 is_inline = false;
14986 /* Look for the `namespace' keyword. */
14987 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14989 /* Get the name of the namespace. We do not attempt to distinguish
14990 between an original-namespace-definition and an
14991 extension-namespace-definition at this point. The semantic
14992 analysis routines are responsible for that. */
14993 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14994 identifier = cp_parser_identifier (parser);
14995 else
14996 identifier = NULL_TREE;
14998 /* Parse any specified attributes. */
14999 attribs = cp_parser_attributes_opt (parser);
15001 /* Look for the `{' to start the namespace. */
15002 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
15003 /* Start the namespace. */
15004 push_namespace (identifier);
15006 /* "inline namespace" is equivalent to a stub namespace definition
15007 followed by a strong using directive. */
15008 if (is_inline)
15010 tree name_space = current_namespace;
15011 /* Set up namespace association. */
15012 DECL_NAMESPACE_ASSOCIATIONS (name_space)
15013 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
15014 DECL_NAMESPACE_ASSOCIATIONS (name_space));
15015 /* Import the contents of the inline namespace. */
15016 pop_namespace ();
15017 do_using_directive (name_space);
15018 push_namespace (identifier);
15021 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15023 /* Parse the body of the namespace. */
15024 cp_parser_namespace_body (parser);
15026 if (has_visibility)
15027 pop_visibility (1);
15029 /* Finish the namespace. */
15030 pop_namespace ();
15031 /* Look for the final `}'. */
15032 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15035 /* Parse a namespace-body.
15037 namespace-body:
15038 declaration-seq [opt] */
15040 static void
15041 cp_parser_namespace_body (cp_parser* parser)
15043 cp_parser_declaration_seq_opt (parser);
15046 /* Parse a namespace-alias-definition.
15048 namespace-alias-definition:
15049 namespace identifier = qualified-namespace-specifier ; */
15051 static void
15052 cp_parser_namespace_alias_definition (cp_parser* parser)
15054 tree identifier;
15055 tree namespace_specifier;
15057 cp_token *token = cp_lexer_peek_token (parser->lexer);
15059 /* Look for the `namespace' keyword. */
15060 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15061 /* Look for the identifier. */
15062 identifier = cp_parser_identifier (parser);
15063 if (identifier == error_mark_node)
15064 return;
15065 /* Look for the `=' token. */
15066 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15067 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15069 error_at (token->location, "%<namespace%> definition is not allowed here");
15070 /* Skip the definition. */
15071 cp_lexer_consume_token (parser->lexer);
15072 if (cp_parser_skip_to_closing_brace (parser))
15073 cp_lexer_consume_token (parser->lexer);
15074 return;
15076 cp_parser_require (parser, CPP_EQ, RT_EQ);
15077 /* Look for the qualified-namespace-specifier. */
15078 namespace_specifier
15079 = cp_parser_qualified_namespace_specifier (parser);
15080 /* Look for the `;' token. */
15081 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15083 /* Register the alias in the symbol table. */
15084 do_namespace_alias (identifier, namespace_specifier);
15087 /* Parse a qualified-namespace-specifier.
15089 qualified-namespace-specifier:
15090 :: [opt] nested-name-specifier [opt] namespace-name
15092 Returns a NAMESPACE_DECL corresponding to the specified
15093 namespace. */
15095 static tree
15096 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15098 /* Look for the optional `::'. */
15099 cp_parser_global_scope_opt (parser,
15100 /*current_scope_valid_p=*/false);
15102 /* Look for the optional nested-name-specifier. */
15103 cp_parser_nested_name_specifier_opt (parser,
15104 /*typename_keyword_p=*/false,
15105 /*check_dependency_p=*/true,
15106 /*type_p=*/false,
15107 /*is_declaration=*/true);
15109 return cp_parser_namespace_name (parser);
15112 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15113 access declaration.
15115 using-declaration:
15116 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15117 using :: unqualified-id ;
15119 access-declaration:
15120 qualified-id ;
15124 static bool
15125 cp_parser_using_declaration (cp_parser* parser,
15126 bool access_declaration_p)
15128 cp_token *token;
15129 bool typename_p = false;
15130 bool global_scope_p;
15131 tree decl;
15132 tree identifier;
15133 tree qscope;
15134 int oldcount = errorcount;
15135 cp_token *diag_token = NULL;
15137 if (access_declaration_p)
15139 diag_token = cp_lexer_peek_token (parser->lexer);
15140 cp_parser_parse_tentatively (parser);
15142 else
15144 /* Look for the `using' keyword. */
15145 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15147 /* Peek at the next token. */
15148 token = cp_lexer_peek_token (parser->lexer);
15149 /* See if it's `typename'. */
15150 if (token->keyword == RID_TYPENAME)
15152 /* Remember that we've seen it. */
15153 typename_p = true;
15154 /* Consume the `typename' token. */
15155 cp_lexer_consume_token (parser->lexer);
15159 /* Look for the optional global scope qualification. */
15160 global_scope_p
15161 = (cp_parser_global_scope_opt (parser,
15162 /*current_scope_valid_p=*/false)
15163 != NULL_TREE);
15165 /* If we saw `typename', or didn't see `::', then there must be a
15166 nested-name-specifier present. */
15167 if (typename_p || !global_scope_p)
15168 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15169 /*check_dependency_p=*/true,
15170 /*type_p=*/false,
15171 /*is_declaration=*/true);
15172 /* Otherwise, we could be in either of the two productions. In that
15173 case, treat the nested-name-specifier as optional. */
15174 else
15175 qscope = cp_parser_nested_name_specifier_opt (parser,
15176 /*typename_keyword_p=*/false,
15177 /*check_dependency_p=*/true,
15178 /*type_p=*/false,
15179 /*is_declaration=*/true);
15180 if (!qscope)
15181 qscope = global_namespace;
15183 if (access_declaration_p && cp_parser_error_occurred (parser))
15184 /* Something has already gone wrong; there's no need to parse
15185 further. Since an error has occurred, the return value of
15186 cp_parser_parse_definitely will be false, as required. */
15187 return cp_parser_parse_definitely (parser);
15189 token = cp_lexer_peek_token (parser->lexer);
15190 /* Parse the unqualified-id. */
15191 identifier = cp_parser_unqualified_id (parser,
15192 /*template_keyword_p=*/false,
15193 /*check_dependency_p=*/true,
15194 /*declarator_p=*/true,
15195 /*optional_p=*/false);
15197 if (access_declaration_p)
15199 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15200 cp_parser_simulate_error (parser);
15201 if (!cp_parser_parse_definitely (parser))
15202 return false;
15205 /* The function we call to handle a using-declaration is different
15206 depending on what scope we are in. */
15207 if (qscope == error_mark_node || identifier == error_mark_node)
15209 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
15210 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15211 /* [namespace.udecl]
15213 A using declaration shall not name a template-id. */
15214 error_at (token->location,
15215 "a template-id may not appear in a using-declaration");
15216 else
15218 if (at_class_scope_p ())
15220 /* Create the USING_DECL. */
15221 decl = do_class_using_decl (parser->scope, identifier);
15223 if (decl && typename_p)
15224 USING_DECL_TYPENAME_P (decl) = 1;
15226 if (check_for_bare_parameter_packs (decl))
15227 return false;
15228 else
15229 /* Add it to the list of members in this class. */
15230 finish_member_declaration (decl);
15232 else
15234 decl = cp_parser_lookup_name_simple (parser,
15235 identifier,
15236 token->location);
15237 if (decl == error_mark_node)
15238 cp_parser_name_lookup_error (parser, identifier,
15239 decl, NLE_NULL,
15240 token->location);
15241 else if (check_for_bare_parameter_packs (decl))
15242 return false;
15243 else if (!at_namespace_scope_p ())
15244 do_local_using_decl (decl, qscope, identifier);
15245 else
15246 do_toplevel_using_decl (decl, qscope, identifier);
15250 /* Look for the final `;'. */
15251 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15253 if (access_declaration_p && errorcount == oldcount)
15254 warning_at (diag_token->location, OPT_Wdeprecated,
15255 "access declarations are deprecated "
15256 "in favour of using-declarations; "
15257 "suggestion: add the %<using%> keyword");
15259 return true;
15262 /* Parse an alias-declaration.
15264 alias-declaration:
15265 using identifier attribute-specifier-seq [opt] = type-id */
15267 static tree
15268 cp_parser_alias_declaration (cp_parser* parser)
15270 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15271 location_t id_location;
15272 cp_declarator *declarator;
15273 cp_decl_specifier_seq decl_specs;
15274 bool member_p;
15275 const char *saved_message = NULL;
15277 /* Look for the `using' keyword. */
15278 cp_token *using_token
15279 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
15280 if (using_token == NULL)
15281 return error_mark_node;
15283 id_location = cp_lexer_peek_token (parser->lexer)->location;
15284 id = cp_parser_identifier (parser);
15285 if (id == error_mark_node)
15286 return error_mark_node;
15288 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
15289 attributes = cp_parser_attributes_opt (parser);
15290 if (attributes == error_mark_node)
15291 return error_mark_node;
15293 cp_parser_require (parser, CPP_EQ, RT_EQ);
15295 if (cp_parser_error_occurred (parser))
15296 return error_mark_node;
15298 /* Now we are going to parse the type-id of the declaration. */
15301 [dcl.type]/3 says:
15303 "A type-specifier-seq shall not define a class or enumeration
15304 unless it appears in the type-id of an alias-declaration (7.1.3) that
15305 is not the declaration of a template-declaration."
15307 In other words, if we currently are in an alias template, the
15308 type-id should not define a type.
15310 So let's set parser->type_definition_forbidden_message in that
15311 case; cp_parser_check_type_definition (called by
15312 cp_parser_class_specifier) will then emit an error if a type is
15313 defined in the type-id. */
15314 if (parser->num_template_parameter_lists)
15316 saved_message = parser->type_definition_forbidden_message;
15317 parser->type_definition_forbidden_message =
15318 G_("types may not be defined in alias template declarations");
15321 type = cp_parser_type_id (parser);
15323 /* Restore the error message if need be. */
15324 if (parser->num_template_parameter_lists)
15325 parser->type_definition_forbidden_message = saved_message;
15327 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15329 if (cp_parser_error_occurred (parser))
15330 return error_mark_node;
15332 /* A typedef-name can also be introduced by an alias-declaration. The
15333 identifier following the using keyword becomes a typedef-name. It has
15334 the same semantics as if it were introduced by the typedef
15335 specifier. In particular, it does not define a new type and it shall
15336 not appear in the type-id. */
15338 clear_decl_specs (&decl_specs);
15339 decl_specs.type = type;
15340 if (attributes != NULL_TREE)
15342 decl_specs.attributes = attributes;
15343 set_and_check_decl_spec_loc (&decl_specs,
15344 ds_attribute,
15345 attrs_token);
15347 set_and_check_decl_spec_loc (&decl_specs,
15348 ds_typedef,
15349 using_token);
15350 set_and_check_decl_spec_loc (&decl_specs,
15351 ds_alias,
15352 using_token);
15354 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15355 declarator->id_loc = id_location;
15357 member_p = at_class_scope_p ();
15358 if (member_p)
15359 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15360 NULL_TREE, attributes);
15361 else
15362 decl = start_decl (declarator, &decl_specs, 0,
15363 attributes, NULL_TREE, &pushed_scope);
15364 if (decl == error_mark_node)
15365 return decl;
15367 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15369 if (pushed_scope)
15370 pop_scope (pushed_scope);
15372 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15373 added into the symbol table; otherwise, return the TYPE_DECL. */
15374 if (DECL_LANG_SPECIFIC (decl)
15375 && DECL_TEMPLATE_INFO (decl)
15376 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15378 decl = DECL_TI_TEMPLATE (decl);
15379 if (member_p)
15380 check_member_template (decl);
15383 return decl;
15386 /* Parse a using-directive.
15388 using-directive:
15389 using namespace :: [opt] nested-name-specifier [opt]
15390 namespace-name ; */
15392 static void
15393 cp_parser_using_directive (cp_parser* parser)
15395 tree namespace_decl;
15396 tree attribs;
15398 /* Look for the `using' keyword. */
15399 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15400 /* And the `namespace' keyword. */
15401 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15402 /* Look for the optional `::' operator. */
15403 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15404 /* And the optional nested-name-specifier. */
15405 cp_parser_nested_name_specifier_opt (parser,
15406 /*typename_keyword_p=*/false,
15407 /*check_dependency_p=*/true,
15408 /*type_p=*/false,
15409 /*is_declaration=*/true);
15410 /* Get the namespace being used. */
15411 namespace_decl = cp_parser_namespace_name (parser);
15412 /* And any specified attributes. */
15413 attribs = cp_parser_attributes_opt (parser);
15414 /* Update the symbol table. */
15415 parse_using_directive (namespace_decl, attribs);
15416 /* Look for the final `;'. */
15417 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15420 /* Parse an asm-definition.
15422 asm-definition:
15423 asm ( string-literal ) ;
15425 GNU Extension:
15427 asm-definition:
15428 asm volatile [opt] ( string-literal ) ;
15429 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15430 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15431 : asm-operand-list [opt] ) ;
15432 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15433 : asm-operand-list [opt]
15434 : asm-clobber-list [opt] ) ;
15435 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15436 : asm-clobber-list [opt]
15437 : asm-goto-list ) ; */
15439 static void
15440 cp_parser_asm_definition (cp_parser* parser)
15442 tree string;
15443 tree outputs = NULL_TREE;
15444 tree inputs = NULL_TREE;
15445 tree clobbers = NULL_TREE;
15446 tree labels = NULL_TREE;
15447 tree asm_stmt;
15448 bool volatile_p = false;
15449 bool extended_p = false;
15450 bool invalid_inputs_p = false;
15451 bool invalid_outputs_p = false;
15452 bool goto_p = false;
15453 required_token missing = RT_NONE;
15455 /* Look for the `asm' keyword. */
15456 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15457 /* See if the next token is `volatile'. */
15458 if (cp_parser_allow_gnu_extensions_p (parser)
15459 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15461 /* Remember that we saw the `volatile' keyword. */
15462 volatile_p = true;
15463 /* Consume the token. */
15464 cp_lexer_consume_token (parser->lexer);
15466 if (cp_parser_allow_gnu_extensions_p (parser)
15467 && parser->in_function_body
15468 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15470 /* Remember that we saw the `goto' keyword. */
15471 goto_p = true;
15472 /* Consume the token. */
15473 cp_lexer_consume_token (parser->lexer);
15475 /* Look for the opening `('. */
15476 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15477 return;
15478 /* Look for the string. */
15479 string = cp_parser_string_literal (parser, false, false);
15480 if (string == error_mark_node)
15482 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15483 /*consume_paren=*/true);
15484 return;
15487 /* If we're allowing GNU extensions, check for the extended assembly
15488 syntax. Unfortunately, the `:' tokens need not be separated by
15489 a space in C, and so, for compatibility, we tolerate that here
15490 too. Doing that means that we have to treat the `::' operator as
15491 two `:' tokens. */
15492 if (cp_parser_allow_gnu_extensions_p (parser)
15493 && parser->in_function_body
15494 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15495 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15497 bool inputs_p = false;
15498 bool clobbers_p = false;
15499 bool labels_p = false;
15501 /* The extended syntax was used. */
15502 extended_p = true;
15504 /* Look for outputs. */
15505 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15507 /* Consume the `:'. */
15508 cp_lexer_consume_token (parser->lexer);
15509 /* Parse the output-operands. */
15510 if (cp_lexer_next_token_is_not (parser->lexer,
15511 CPP_COLON)
15512 && cp_lexer_next_token_is_not (parser->lexer,
15513 CPP_SCOPE)
15514 && cp_lexer_next_token_is_not (parser->lexer,
15515 CPP_CLOSE_PAREN)
15516 && !goto_p)
15517 outputs = cp_parser_asm_operand_list (parser);
15519 if (outputs == error_mark_node)
15520 invalid_outputs_p = true;
15522 /* If the next token is `::', there are no outputs, and the
15523 next token is the beginning of the inputs. */
15524 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15525 /* The inputs are coming next. */
15526 inputs_p = true;
15528 /* Look for inputs. */
15529 if (inputs_p
15530 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15532 /* Consume the `:' or `::'. */
15533 cp_lexer_consume_token (parser->lexer);
15534 /* Parse the output-operands. */
15535 if (cp_lexer_next_token_is_not (parser->lexer,
15536 CPP_COLON)
15537 && cp_lexer_next_token_is_not (parser->lexer,
15538 CPP_SCOPE)
15539 && cp_lexer_next_token_is_not (parser->lexer,
15540 CPP_CLOSE_PAREN))
15541 inputs = cp_parser_asm_operand_list (parser);
15543 if (inputs == error_mark_node)
15544 invalid_inputs_p = true;
15546 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15547 /* The clobbers are coming next. */
15548 clobbers_p = true;
15550 /* Look for clobbers. */
15551 if (clobbers_p
15552 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15554 clobbers_p = true;
15555 /* Consume the `:' or `::'. */
15556 cp_lexer_consume_token (parser->lexer);
15557 /* Parse the clobbers. */
15558 if (cp_lexer_next_token_is_not (parser->lexer,
15559 CPP_COLON)
15560 && cp_lexer_next_token_is_not (parser->lexer,
15561 CPP_CLOSE_PAREN))
15562 clobbers = cp_parser_asm_clobber_list (parser);
15564 else if (goto_p
15565 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15566 /* The labels are coming next. */
15567 labels_p = true;
15569 /* Look for labels. */
15570 if (labels_p
15571 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15573 labels_p = true;
15574 /* Consume the `:' or `::'. */
15575 cp_lexer_consume_token (parser->lexer);
15576 /* Parse the labels. */
15577 labels = cp_parser_asm_label_list (parser);
15580 if (goto_p && !labels_p)
15581 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15583 else if (goto_p)
15584 missing = RT_COLON_SCOPE;
15586 /* Look for the closing `)'. */
15587 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15588 missing ? missing : RT_CLOSE_PAREN))
15589 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15590 /*consume_paren=*/true);
15591 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15593 if (!invalid_inputs_p && !invalid_outputs_p)
15595 /* Create the ASM_EXPR. */
15596 if (parser->in_function_body)
15598 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15599 inputs, clobbers, labels);
15600 /* If the extended syntax was not used, mark the ASM_EXPR. */
15601 if (!extended_p)
15603 tree temp = asm_stmt;
15604 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15605 temp = TREE_OPERAND (temp, 0);
15607 ASM_INPUT_P (temp) = 1;
15610 else
15611 add_asm_node (string);
15615 /* Declarators [gram.dcl.decl] */
15617 /* Parse an init-declarator.
15619 init-declarator:
15620 declarator initializer [opt]
15622 GNU Extension:
15624 init-declarator:
15625 declarator asm-specification [opt] attributes [opt] initializer [opt]
15627 function-definition:
15628 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15629 function-body
15630 decl-specifier-seq [opt] declarator function-try-block
15632 GNU Extension:
15634 function-definition:
15635 __extension__ function-definition
15637 TM Extension:
15639 function-definition:
15640 decl-specifier-seq [opt] declarator function-transaction-block
15642 The DECL_SPECIFIERS apply to this declarator. Returns a
15643 representation of the entity declared. If MEMBER_P is TRUE, then
15644 this declarator appears in a class scope. The new DECL created by
15645 this declarator is returned.
15647 The CHECKS are access checks that should be performed once we know
15648 what entity is being declared (and, therefore, what classes have
15649 befriended it).
15651 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15652 for a function-definition here as well. If the declarator is a
15653 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15654 be TRUE upon return. By that point, the function-definition will
15655 have been completely parsed.
15657 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15658 is FALSE.
15660 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15661 parsed declaration if it is an uninitialized single declarator not followed
15662 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15663 if present, will not be consumed. If returned, this declarator will be
15664 created with SD_INITIALIZED but will not call cp_finish_decl. */
15666 static tree
15667 cp_parser_init_declarator (cp_parser* parser,
15668 cp_decl_specifier_seq *decl_specifiers,
15669 vec<deferred_access_check, va_gc> *checks,
15670 bool function_definition_allowed_p,
15671 bool member_p,
15672 int declares_class_or_enum,
15673 bool* function_definition_p,
15674 tree* maybe_range_for_decl)
15676 cp_token *token = NULL, *asm_spec_start_token = NULL,
15677 *attributes_start_token = NULL;
15678 cp_declarator *declarator;
15679 tree prefix_attributes;
15680 tree attributes = NULL;
15681 tree asm_specification;
15682 tree initializer;
15683 tree decl = NULL_TREE;
15684 tree scope;
15685 int is_initialized;
15686 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15687 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15688 "(...)". */
15689 enum cpp_ttype initialization_kind;
15690 bool is_direct_init = false;
15691 bool is_non_constant_init;
15692 int ctor_dtor_or_conv_p;
15693 bool friend_p;
15694 tree pushed_scope = NULL_TREE;
15695 bool range_for_decl_p = false;
15697 /* Gather the attributes that were provided with the
15698 decl-specifiers. */
15699 prefix_attributes = decl_specifiers->attributes;
15701 /* Assume that this is not the declarator for a function
15702 definition. */
15703 if (function_definition_p)
15704 *function_definition_p = false;
15706 /* Defer access checks while parsing the declarator; we cannot know
15707 what names are accessible until we know what is being
15708 declared. */
15709 resume_deferring_access_checks ();
15711 /* Parse the declarator. */
15712 token = cp_lexer_peek_token (parser->lexer);
15713 declarator
15714 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15715 &ctor_dtor_or_conv_p,
15716 /*parenthesized_p=*/NULL,
15717 member_p);
15718 /* Gather up the deferred checks. */
15719 stop_deferring_access_checks ();
15721 /* If the DECLARATOR was erroneous, there's no need to go
15722 further. */
15723 if (declarator == cp_error_declarator)
15724 return error_mark_node;
15726 /* Check that the number of template-parameter-lists is OK. */
15727 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15728 token->location))
15729 return error_mark_node;
15731 if (declares_class_or_enum & 2)
15732 cp_parser_check_for_definition_in_return_type (declarator,
15733 decl_specifiers->type,
15734 decl_specifiers->locations[ds_type_spec]);
15736 /* Figure out what scope the entity declared by the DECLARATOR is
15737 located in. `grokdeclarator' sometimes changes the scope, so
15738 we compute it now. */
15739 scope = get_scope_of_declarator (declarator);
15741 /* Perform any lookups in the declared type which were thought to be
15742 dependent, but are not in the scope of the declarator. */
15743 decl_specifiers->type
15744 = maybe_update_decl_type (decl_specifiers->type, scope);
15746 /* If we're allowing GNU extensions, look for an
15747 asm-specification. */
15748 if (cp_parser_allow_gnu_extensions_p (parser))
15750 /* Look for an asm-specification. */
15751 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15752 asm_specification = cp_parser_asm_specification_opt (parser);
15754 else
15755 asm_specification = NULL_TREE;
15757 /* Look for attributes. */
15758 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15759 attributes = cp_parser_attributes_opt (parser);
15761 /* Peek at the next token. */
15762 token = cp_lexer_peek_token (parser->lexer);
15763 /* Check to see if the token indicates the start of a
15764 function-definition. */
15765 if (function_declarator_p (declarator)
15766 && cp_parser_token_starts_function_definition_p (token))
15768 if (!function_definition_allowed_p)
15770 /* If a function-definition should not appear here, issue an
15771 error message. */
15772 cp_parser_error (parser,
15773 "a function-definition is not allowed here");
15774 return error_mark_node;
15776 else
15778 location_t func_brace_location
15779 = cp_lexer_peek_token (parser->lexer)->location;
15781 /* Neither attributes nor an asm-specification are allowed
15782 on a function-definition. */
15783 if (asm_specification)
15784 error_at (asm_spec_start_token->location,
15785 "an asm-specification is not allowed "
15786 "on a function-definition");
15787 if (attributes)
15788 error_at (attributes_start_token->location,
15789 "attributes are not allowed on a function-definition");
15790 /* This is a function-definition. */
15791 *function_definition_p = true;
15793 /* Parse the function definition. */
15794 if (member_p)
15795 decl = cp_parser_save_member_function_body (parser,
15796 decl_specifiers,
15797 declarator,
15798 prefix_attributes);
15799 else
15800 decl
15801 = (cp_parser_function_definition_from_specifiers_and_declarator
15802 (parser, decl_specifiers, prefix_attributes, declarator));
15804 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15806 /* This is where the prologue starts... */
15807 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15808 = func_brace_location;
15811 return decl;
15815 /* [dcl.dcl]
15817 Only in function declarations for constructors, destructors, and
15818 type conversions can the decl-specifier-seq be omitted.
15820 We explicitly postpone this check past the point where we handle
15821 function-definitions because we tolerate function-definitions
15822 that are missing their return types in some modes. */
15823 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15825 cp_parser_error (parser,
15826 "expected constructor, destructor, or type conversion");
15827 return error_mark_node;
15830 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15831 if (token->type == CPP_EQ
15832 || token->type == CPP_OPEN_PAREN
15833 || token->type == CPP_OPEN_BRACE)
15835 is_initialized = SD_INITIALIZED;
15836 initialization_kind = token->type;
15837 if (maybe_range_for_decl)
15838 *maybe_range_for_decl = error_mark_node;
15840 if (token->type == CPP_EQ
15841 && function_declarator_p (declarator))
15843 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15844 if (t2->keyword == RID_DEFAULT)
15845 is_initialized = SD_DEFAULTED;
15846 else if (t2->keyword == RID_DELETE)
15847 is_initialized = SD_DELETED;
15850 else
15852 /* If the init-declarator isn't initialized and isn't followed by a
15853 `,' or `;', it's not a valid init-declarator. */
15854 if (token->type != CPP_COMMA
15855 && token->type != CPP_SEMICOLON)
15857 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15858 range_for_decl_p = true;
15859 else
15861 cp_parser_error (parser, "expected initializer");
15862 return error_mark_node;
15865 is_initialized = SD_UNINITIALIZED;
15866 initialization_kind = CPP_EOF;
15869 /* Because start_decl has side-effects, we should only call it if we
15870 know we're going ahead. By this point, we know that we cannot
15871 possibly be looking at any other construct. */
15872 cp_parser_commit_to_tentative_parse (parser);
15874 /* If the decl specifiers were bad, issue an error now that we're
15875 sure this was intended to be a declarator. Then continue
15876 declaring the variable(s), as int, to try to cut down on further
15877 errors. */
15878 if (decl_specifiers->any_specifiers_p
15879 && decl_specifiers->type == error_mark_node)
15881 cp_parser_error (parser, "invalid type in declaration");
15882 decl_specifiers->type = integer_type_node;
15885 /* Check to see whether or not this declaration is a friend. */
15886 friend_p = cp_parser_friend_p (decl_specifiers);
15888 /* Enter the newly declared entry in the symbol table. If we're
15889 processing a declaration in a class-specifier, we wait until
15890 after processing the initializer. */
15891 if (!member_p)
15893 if (parser->in_unbraced_linkage_specification_p)
15894 decl_specifiers->storage_class = sc_extern;
15895 decl = start_decl (declarator, decl_specifiers,
15896 range_for_decl_p? SD_INITIALIZED : is_initialized,
15897 attributes, prefix_attributes,
15898 &pushed_scope);
15899 /* Adjust location of decl if declarator->id_loc is more appropriate:
15900 set, and decl wasn't merged with another decl, in which case its
15901 location would be different from input_location, and more accurate. */
15902 if (DECL_P (decl)
15903 && declarator->id_loc != UNKNOWN_LOCATION
15904 && DECL_SOURCE_LOCATION (decl) == input_location)
15905 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15907 else if (scope)
15908 /* Enter the SCOPE. That way unqualified names appearing in the
15909 initializer will be looked up in SCOPE. */
15910 pushed_scope = push_scope (scope);
15912 /* Perform deferred access control checks, now that we know in which
15913 SCOPE the declared entity resides. */
15914 if (!member_p && decl)
15916 tree saved_current_function_decl = NULL_TREE;
15918 /* If the entity being declared is a function, pretend that we
15919 are in its scope. If it is a `friend', it may have access to
15920 things that would not otherwise be accessible. */
15921 if (TREE_CODE (decl) == FUNCTION_DECL)
15923 saved_current_function_decl = current_function_decl;
15924 current_function_decl = decl;
15927 /* Perform access checks for template parameters. */
15928 cp_parser_perform_template_parameter_access_checks (checks);
15930 /* Perform the access control checks for the declarator and the
15931 decl-specifiers. */
15932 perform_deferred_access_checks (tf_warning_or_error);
15934 /* Restore the saved value. */
15935 if (TREE_CODE (decl) == FUNCTION_DECL)
15936 current_function_decl = saved_current_function_decl;
15939 /* Parse the initializer. */
15940 initializer = NULL_TREE;
15941 is_direct_init = false;
15942 is_non_constant_init = true;
15943 if (is_initialized)
15945 if (function_declarator_p (declarator))
15947 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15948 if (initialization_kind == CPP_EQ)
15949 initializer = cp_parser_pure_specifier (parser);
15950 else
15952 /* If the declaration was erroneous, we don't really
15953 know what the user intended, so just silently
15954 consume the initializer. */
15955 if (decl != error_mark_node)
15956 error_at (initializer_start_token->location,
15957 "initializer provided for function");
15958 cp_parser_skip_to_closing_parenthesis (parser,
15959 /*recovering=*/true,
15960 /*or_comma=*/false,
15961 /*consume_paren=*/true);
15964 else
15966 /* We want to record the extra mangling scope for in-class
15967 initializers of class members and initializers of static data
15968 member templates. The former involves deferring
15969 parsing of the initializer until end of class as with default
15970 arguments. So right here we only handle the latter. */
15971 if (!member_p && processing_template_decl)
15972 start_lambda_scope (decl);
15973 initializer = cp_parser_initializer (parser,
15974 &is_direct_init,
15975 &is_non_constant_init);
15976 if (!member_p && processing_template_decl)
15977 finish_lambda_scope ();
15978 if (initializer == error_mark_node)
15979 cp_parser_skip_to_end_of_statement (parser);
15983 /* The old parser allows attributes to appear after a parenthesized
15984 initializer. Mark Mitchell proposed removing this functionality
15985 on the GCC mailing lists on 2002-08-13. This parser accepts the
15986 attributes -- but ignores them. */
15987 if (cp_parser_allow_gnu_extensions_p (parser)
15988 && initialization_kind == CPP_OPEN_PAREN)
15989 if (cp_parser_attributes_opt (parser))
15990 warning (OPT_Wattributes,
15991 "attributes after parenthesized initializer ignored");
15993 /* For an in-class declaration, use `grokfield' to create the
15994 declaration. */
15995 if (member_p)
15997 if (pushed_scope)
15999 pop_scope (pushed_scope);
16000 pushed_scope = NULL_TREE;
16002 decl = grokfield (declarator, decl_specifiers,
16003 initializer, !is_non_constant_init,
16004 /*asmspec=*/NULL_TREE,
16005 prefix_attributes);
16006 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
16007 cp_parser_save_default_args (parser, decl);
16010 /* Finish processing the declaration. But, skip member
16011 declarations. */
16012 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
16014 cp_finish_decl (decl,
16015 initializer, !is_non_constant_init,
16016 asm_specification,
16017 /* If the initializer is in parentheses, then this is
16018 a direct-initialization, which means that an
16019 `explicit' constructor is OK. Otherwise, an
16020 `explicit' constructor cannot be used. */
16021 ((is_direct_init || !is_initialized)
16022 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16024 else if ((cxx_dialect != cxx98) && friend_p
16025 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16026 /* Core issue #226 (C++0x only): A default template-argument
16027 shall not be specified in a friend class template
16028 declaration. */
16029 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16030 /*is_partial=*/false, /*is_friend_decl=*/1);
16032 if (!friend_p && pushed_scope)
16033 pop_scope (pushed_scope);
16035 return decl;
16038 /* Parse a declarator.
16040 declarator:
16041 direct-declarator
16042 ptr-operator declarator
16044 abstract-declarator:
16045 ptr-operator abstract-declarator [opt]
16046 direct-abstract-declarator
16048 GNU Extensions:
16050 declarator:
16051 attributes [opt] direct-declarator
16052 attributes [opt] ptr-operator declarator
16054 abstract-declarator:
16055 attributes [opt] ptr-operator abstract-declarator [opt]
16056 attributes [opt] direct-abstract-declarator
16058 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16059 detect constructor, destructor or conversion operators. It is set
16060 to -1 if the declarator is a name, and +1 if it is a
16061 function. Otherwise it is set to zero. Usually you just want to
16062 test for >0, but internally the negative value is used.
16064 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16065 a decl-specifier-seq unless it declares a constructor, destructor,
16066 or conversion. It might seem that we could check this condition in
16067 semantic analysis, rather than parsing, but that makes it difficult
16068 to handle something like `f()'. We want to notice that there are
16069 no decl-specifiers, and therefore realize that this is an
16070 expression, not a declaration.)
16072 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16073 the declarator is a direct-declarator of the form "(...)".
16075 MEMBER_P is true iff this declarator is a member-declarator. */
16077 static cp_declarator *
16078 cp_parser_declarator (cp_parser* parser,
16079 cp_parser_declarator_kind dcl_kind,
16080 int* ctor_dtor_or_conv_p,
16081 bool* parenthesized_p,
16082 bool member_p)
16084 cp_declarator *declarator;
16085 enum tree_code code;
16086 cp_cv_quals cv_quals;
16087 tree class_type;
16088 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16090 /* Assume this is not a constructor, destructor, or type-conversion
16091 operator. */
16092 if (ctor_dtor_or_conv_p)
16093 *ctor_dtor_or_conv_p = 0;
16095 if (cp_parser_allow_gnu_extensions_p (parser))
16096 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16098 /* Check for the ptr-operator production. */
16099 cp_parser_parse_tentatively (parser);
16100 /* Parse the ptr-operator. */
16101 code = cp_parser_ptr_operator (parser,
16102 &class_type,
16103 &cv_quals,
16104 &std_attributes);
16106 /* If that worked, then we have a ptr-operator. */
16107 if (cp_parser_parse_definitely (parser))
16109 /* If a ptr-operator was found, then this declarator was not
16110 parenthesized. */
16111 if (parenthesized_p)
16112 *parenthesized_p = true;
16113 /* The dependent declarator is optional if we are parsing an
16114 abstract-declarator. */
16115 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16116 cp_parser_parse_tentatively (parser);
16118 /* Parse the dependent declarator. */
16119 declarator = cp_parser_declarator (parser, dcl_kind,
16120 /*ctor_dtor_or_conv_p=*/NULL,
16121 /*parenthesized_p=*/NULL,
16122 /*member_p=*/false);
16124 /* If we are parsing an abstract-declarator, we must handle the
16125 case where the dependent declarator is absent. */
16126 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16127 && !cp_parser_parse_definitely (parser))
16128 declarator = NULL;
16130 declarator = cp_parser_make_indirect_declarator
16131 (code, class_type, cv_quals, declarator, std_attributes);
16133 /* Everything else is a direct-declarator. */
16134 else
16136 if (parenthesized_p)
16137 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
16138 CPP_OPEN_PAREN);
16139 declarator = cp_parser_direct_declarator (parser, dcl_kind,
16140 ctor_dtor_or_conv_p,
16141 member_p);
16144 if (gnu_attributes && declarator && declarator != cp_error_declarator)
16145 declarator->attributes = gnu_attributes;
16146 return declarator;
16149 /* Parse a direct-declarator or direct-abstract-declarator.
16151 direct-declarator:
16152 declarator-id
16153 direct-declarator ( parameter-declaration-clause )
16154 cv-qualifier-seq [opt]
16155 exception-specification [opt]
16156 direct-declarator [ constant-expression [opt] ]
16157 ( declarator )
16159 direct-abstract-declarator:
16160 direct-abstract-declarator [opt]
16161 ( parameter-declaration-clause )
16162 cv-qualifier-seq [opt]
16163 exception-specification [opt]
16164 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16165 ( abstract-declarator )
16167 Returns a representation of the declarator. DCL_KIND is
16168 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16169 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16170 we are parsing a direct-declarator. It is
16171 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16172 of ambiguity we prefer an abstract declarator, as per
16173 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16174 cp_parser_declarator. */
16176 static cp_declarator *
16177 cp_parser_direct_declarator (cp_parser* parser,
16178 cp_parser_declarator_kind dcl_kind,
16179 int* ctor_dtor_or_conv_p,
16180 bool member_p)
16182 cp_token *token;
16183 cp_declarator *declarator = NULL;
16184 tree scope = NULL_TREE;
16185 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16186 bool saved_in_declarator_p = parser->in_declarator_p;
16187 bool first = true;
16188 tree pushed_scope = NULL_TREE;
16190 while (true)
16192 /* Peek at the next token. */
16193 token = cp_lexer_peek_token (parser->lexer);
16194 if (token->type == CPP_OPEN_PAREN)
16196 /* This is either a parameter-declaration-clause, or a
16197 parenthesized declarator. When we know we are parsing a
16198 named declarator, it must be a parenthesized declarator
16199 if FIRST is true. For instance, `(int)' is a
16200 parameter-declaration-clause, with an omitted
16201 direct-abstract-declarator. But `((*))', is a
16202 parenthesized abstract declarator. Finally, when T is a
16203 template parameter `(T)' is a
16204 parameter-declaration-clause, and not a parenthesized
16205 named declarator.
16207 We first try and parse a parameter-declaration-clause,
16208 and then try a nested declarator (if FIRST is true).
16210 It is not an error for it not to be a
16211 parameter-declaration-clause, even when FIRST is
16212 false. Consider,
16214 int i (int);
16215 int i (3);
16217 The first is the declaration of a function while the
16218 second is the definition of a variable, including its
16219 initializer.
16221 Having seen only the parenthesis, we cannot know which of
16222 these two alternatives should be selected. Even more
16223 complex are examples like:
16225 int i (int (a));
16226 int i (int (3));
16228 The former is a function-declaration; the latter is a
16229 variable initialization.
16231 Thus again, we try a parameter-declaration-clause, and if
16232 that fails, we back out and return. */
16234 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16236 tree params;
16237 unsigned saved_num_template_parameter_lists;
16238 bool is_declarator = false;
16239 tree t;
16241 /* In a member-declarator, the only valid interpretation
16242 of a parenthesis is the start of a
16243 parameter-declaration-clause. (It is invalid to
16244 initialize a static data member with a parenthesized
16245 initializer; only the "=" form of initialization is
16246 permitted.) */
16247 if (!member_p)
16248 cp_parser_parse_tentatively (parser);
16250 /* Consume the `('. */
16251 cp_lexer_consume_token (parser->lexer);
16252 if (first)
16254 /* If this is going to be an abstract declarator, we're
16255 in a declarator and we can't have default args. */
16256 parser->default_arg_ok_p = false;
16257 parser->in_declarator_p = true;
16260 /* Inside the function parameter list, surrounding
16261 template-parameter-lists do not apply. */
16262 saved_num_template_parameter_lists
16263 = parser->num_template_parameter_lists;
16264 parser->num_template_parameter_lists = 0;
16266 begin_scope (sk_function_parms, NULL_TREE);
16268 /* Parse the parameter-declaration-clause. */
16269 params = cp_parser_parameter_declaration_clause (parser);
16271 parser->num_template_parameter_lists
16272 = saved_num_template_parameter_lists;
16274 /* Consume the `)'. */
16275 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16277 /* If all went well, parse the cv-qualifier-seq and the
16278 exception-specification. */
16279 if (member_p || cp_parser_parse_definitely (parser))
16281 cp_cv_quals cv_quals;
16282 cp_virt_specifiers virt_specifiers;
16283 tree exception_specification;
16284 tree late_return;
16285 tree attrs;
16287 is_declarator = true;
16289 if (ctor_dtor_or_conv_p)
16290 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16291 first = false;
16293 /* Parse the cv-qualifier-seq. */
16294 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16295 /* And the exception-specification. */
16296 exception_specification
16297 = cp_parser_exception_specification_opt (parser);
16299 attrs = cp_parser_std_attribute_spec_seq (parser);
16301 late_return = (cp_parser_late_return_type_opt
16302 (parser, member_p ? cv_quals : -1));
16304 /* Parse the virt-specifier-seq. */
16305 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16307 /* Create the function-declarator. */
16308 declarator = make_call_declarator (declarator,
16309 params,
16310 cv_quals,
16311 virt_specifiers,
16312 exception_specification,
16313 late_return);
16314 declarator->std_attributes = attrs;
16315 /* Any subsequent parameter lists are to do with
16316 return type, so are not those of the declared
16317 function. */
16318 parser->default_arg_ok_p = false;
16321 /* Remove the function parms from scope. */
16322 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16323 pop_binding (DECL_NAME (t), t);
16324 leave_scope();
16326 if (is_declarator)
16327 /* Repeat the main loop. */
16328 continue;
16331 /* If this is the first, we can try a parenthesized
16332 declarator. */
16333 if (first)
16335 bool saved_in_type_id_in_expr_p;
16337 parser->default_arg_ok_p = saved_default_arg_ok_p;
16338 parser->in_declarator_p = saved_in_declarator_p;
16340 /* Consume the `('. */
16341 cp_lexer_consume_token (parser->lexer);
16342 /* Parse the nested declarator. */
16343 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16344 parser->in_type_id_in_expr_p = true;
16345 declarator
16346 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16347 /*parenthesized_p=*/NULL,
16348 member_p);
16349 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16350 first = false;
16351 /* Expect a `)'. */
16352 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16353 declarator = cp_error_declarator;
16354 if (declarator == cp_error_declarator)
16355 break;
16357 goto handle_declarator;
16359 /* Otherwise, we must be done. */
16360 else
16361 break;
16363 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16364 && token->type == CPP_OPEN_SQUARE
16365 && !cp_next_tokens_can_be_attribute_p (parser))
16367 /* Parse an array-declarator. */
16368 tree bounds, attrs;
16370 if (ctor_dtor_or_conv_p)
16371 *ctor_dtor_or_conv_p = 0;
16373 first = false;
16374 parser->default_arg_ok_p = false;
16375 parser->in_declarator_p = true;
16376 /* Consume the `['. */
16377 cp_lexer_consume_token (parser->lexer);
16378 /* Peek at the next token. */
16379 token = cp_lexer_peek_token (parser->lexer);
16380 /* If the next token is `]', then there is no
16381 constant-expression. */
16382 if (token->type != CPP_CLOSE_SQUARE)
16384 bool non_constant_p;
16386 bounds
16387 = cp_parser_constant_expression (parser,
16388 /*allow_non_constant=*/true,
16389 &non_constant_p);
16390 if (!non_constant_p)
16391 /* OK */;
16392 else if (error_operand_p (bounds))
16393 /* Already gave an error. */;
16394 else if (!parser->in_function_body
16395 || current_binding_level->kind == sk_function_parms)
16397 /* Normally, the array bound must be an integral constant
16398 expression. However, as an extension, we allow VLAs
16399 in function scopes as long as they aren't part of a
16400 parameter declaration. */
16401 cp_parser_error (parser,
16402 "array bound is not an integer constant");
16403 bounds = error_mark_node;
16405 else if (processing_template_decl)
16407 /* Remember this wasn't a constant-expression. */
16408 bounds = build_nop (TREE_TYPE (bounds), bounds);
16409 TREE_SIDE_EFFECTS (bounds) = 1;
16412 else
16413 bounds = NULL_TREE;
16414 /* Look for the closing `]'. */
16415 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16417 declarator = cp_error_declarator;
16418 break;
16421 attrs = cp_parser_std_attribute_spec_seq (parser);
16422 declarator = make_array_declarator (declarator, bounds);
16423 declarator->std_attributes = attrs;
16425 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16428 tree qualifying_scope;
16429 tree unqualified_name;
16430 tree attrs;
16431 special_function_kind sfk;
16432 bool abstract_ok;
16433 bool pack_expansion_p = false;
16434 cp_token *declarator_id_start_token;
16436 /* Parse a declarator-id */
16437 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16438 if (abstract_ok)
16440 cp_parser_parse_tentatively (parser);
16442 /* If we see an ellipsis, we should be looking at a
16443 parameter pack. */
16444 if (token->type == CPP_ELLIPSIS)
16446 /* Consume the `...' */
16447 cp_lexer_consume_token (parser->lexer);
16449 pack_expansion_p = true;
16453 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16454 unqualified_name
16455 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16456 qualifying_scope = parser->scope;
16457 if (abstract_ok)
16459 bool okay = false;
16461 if (!unqualified_name && pack_expansion_p)
16463 /* Check whether an error occurred. */
16464 okay = !cp_parser_error_occurred (parser);
16466 /* We already consumed the ellipsis to mark a
16467 parameter pack, but we have no way to report it,
16468 so abort the tentative parse. We will be exiting
16469 immediately anyway. */
16470 cp_parser_abort_tentative_parse (parser);
16472 else
16473 okay = cp_parser_parse_definitely (parser);
16475 if (!okay)
16476 unqualified_name = error_mark_node;
16477 else if (unqualified_name
16478 && (qualifying_scope
16479 || (TREE_CODE (unqualified_name)
16480 != IDENTIFIER_NODE)))
16482 cp_parser_error (parser, "expected unqualified-id");
16483 unqualified_name = error_mark_node;
16487 if (!unqualified_name)
16488 return NULL;
16489 if (unqualified_name == error_mark_node)
16491 declarator = cp_error_declarator;
16492 pack_expansion_p = false;
16493 declarator->parameter_pack_p = false;
16494 break;
16497 attrs = cp_parser_std_attribute_spec_seq (parser);
16499 if (qualifying_scope && at_namespace_scope_p ()
16500 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16502 /* In the declaration of a member of a template class
16503 outside of the class itself, the SCOPE will sometimes
16504 be a TYPENAME_TYPE. For example, given:
16506 template <typename T>
16507 int S<T>::R::i = 3;
16509 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16510 this context, we must resolve S<T>::R to an ordinary
16511 type, rather than a typename type.
16513 The reason we normally avoid resolving TYPENAME_TYPEs
16514 is that a specialization of `S' might render
16515 `S<T>::R' not a type. However, if `S' is
16516 specialized, then this `i' will not be used, so there
16517 is no harm in resolving the types here. */
16518 tree type;
16520 /* Resolve the TYPENAME_TYPE. */
16521 type = resolve_typename_type (qualifying_scope,
16522 /*only_current_p=*/false);
16523 /* If that failed, the declarator is invalid. */
16524 if (TREE_CODE (type) == TYPENAME_TYPE)
16526 if (typedef_variant_p (type))
16527 error_at (declarator_id_start_token->location,
16528 "cannot define member of dependent typedef "
16529 "%qT", type);
16530 else
16531 error_at (declarator_id_start_token->location,
16532 "%<%T::%E%> is not a type",
16533 TYPE_CONTEXT (qualifying_scope),
16534 TYPE_IDENTIFIER (qualifying_scope));
16536 qualifying_scope = type;
16539 sfk = sfk_none;
16541 if (unqualified_name)
16543 tree class_type;
16545 if (qualifying_scope
16546 && CLASS_TYPE_P (qualifying_scope))
16547 class_type = qualifying_scope;
16548 else
16549 class_type = current_class_type;
16551 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16553 tree name_type = TREE_TYPE (unqualified_name);
16554 if (class_type && same_type_p (name_type, class_type))
16556 if (qualifying_scope
16557 && CLASSTYPE_USE_TEMPLATE (name_type))
16559 error_at (declarator_id_start_token->location,
16560 "invalid use of constructor as a template");
16561 inform (declarator_id_start_token->location,
16562 "use %<%T::%D%> instead of %<%T::%D%> to "
16563 "name the constructor in a qualified name",
16564 class_type,
16565 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16566 class_type, name_type);
16567 declarator = cp_error_declarator;
16568 break;
16570 else
16571 unqualified_name = constructor_name (class_type);
16573 else
16575 /* We do not attempt to print the declarator
16576 here because we do not have enough
16577 information about its original syntactic
16578 form. */
16579 cp_parser_error (parser, "invalid declarator");
16580 declarator = cp_error_declarator;
16581 break;
16585 if (class_type)
16587 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16588 sfk = sfk_destructor;
16589 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16590 sfk = sfk_conversion;
16591 else if (/* There's no way to declare a constructor
16592 for an anonymous type, even if the type
16593 got a name for linkage purposes. */
16594 !TYPE_WAS_ANONYMOUS (class_type)
16595 && constructor_name_p (unqualified_name,
16596 class_type))
16598 unqualified_name = constructor_name (class_type);
16599 sfk = sfk_constructor;
16601 else if (is_overloaded_fn (unqualified_name)
16602 && DECL_CONSTRUCTOR_P (get_first_fn
16603 (unqualified_name)))
16604 sfk = sfk_constructor;
16606 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16607 *ctor_dtor_or_conv_p = -1;
16610 declarator = make_id_declarator (qualifying_scope,
16611 unqualified_name,
16612 sfk);
16613 declarator->std_attributes = attrs;
16614 declarator->id_loc = token->location;
16615 declarator->parameter_pack_p = pack_expansion_p;
16617 if (pack_expansion_p)
16618 maybe_warn_variadic_templates ();
16621 handle_declarator:;
16622 scope = get_scope_of_declarator (declarator);
16623 if (scope)
16624 /* Any names that appear after the declarator-id for a
16625 member are looked up in the containing scope. */
16626 pushed_scope = push_scope (scope);
16627 parser->in_declarator_p = true;
16628 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16629 || (declarator && declarator->kind == cdk_id))
16630 /* Default args are only allowed on function
16631 declarations. */
16632 parser->default_arg_ok_p = saved_default_arg_ok_p;
16633 else
16634 parser->default_arg_ok_p = false;
16636 first = false;
16638 /* We're done. */
16639 else
16640 break;
16643 /* For an abstract declarator, we might wind up with nothing at this
16644 point. That's an error; the declarator is not optional. */
16645 if (!declarator)
16646 cp_parser_error (parser, "expected declarator");
16648 /* If we entered a scope, we must exit it now. */
16649 if (pushed_scope)
16650 pop_scope (pushed_scope);
16652 parser->default_arg_ok_p = saved_default_arg_ok_p;
16653 parser->in_declarator_p = saved_in_declarator_p;
16655 return declarator;
16658 /* Parse a ptr-operator.
16660 ptr-operator:
16661 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16662 * cv-qualifier-seq [opt]
16664 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16665 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16667 GNU Extension:
16669 ptr-operator:
16670 & cv-qualifier-seq [opt]
16672 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16673 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16674 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16675 filled in with the TYPE containing the member. *CV_QUALS is
16676 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16677 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16678 Note that the tree codes returned by this function have nothing
16679 to do with the types of trees that will be eventually be created
16680 to represent the pointer or reference type being parsed. They are
16681 just constants with suggestive names. */
16682 static enum tree_code
16683 cp_parser_ptr_operator (cp_parser* parser,
16684 tree* type,
16685 cp_cv_quals *cv_quals,
16686 tree *attributes)
16688 enum tree_code code = ERROR_MARK;
16689 cp_token *token;
16690 tree attrs = NULL_TREE;
16692 /* Assume that it's not a pointer-to-member. */
16693 *type = NULL_TREE;
16694 /* And that there are no cv-qualifiers. */
16695 *cv_quals = TYPE_UNQUALIFIED;
16697 /* Peek at the next token. */
16698 token = cp_lexer_peek_token (parser->lexer);
16700 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16701 if (token->type == CPP_MULT)
16702 code = INDIRECT_REF;
16703 else if (token->type == CPP_AND)
16704 code = ADDR_EXPR;
16705 else if ((cxx_dialect != cxx98) &&
16706 token->type == CPP_AND_AND) /* C++0x only */
16707 code = NON_LVALUE_EXPR;
16709 if (code != ERROR_MARK)
16711 /* Consume the `*', `&' or `&&'. */
16712 cp_lexer_consume_token (parser->lexer);
16714 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16715 `&', if we are allowing GNU extensions. (The only qualifier
16716 that can legally appear after `&' is `restrict', but that is
16717 enforced during semantic analysis. */
16718 if (code == INDIRECT_REF
16719 || cp_parser_allow_gnu_extensions_p (parser))
16720 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16722 attrs = cp_parser_std_attribute_spec_seq (parser);
16723 if (attributes != NULL)
16724 *attributes = attrs;
16726 else
16728 /* Try the pointer-to-member case. */
16729 cp_parser_parse_tentatively (parser);
16730 /* Look for the optional `::' operator. */
16731 cp_parser_global_scope_opt (parser,
16732 /*current_scope_valid_p=*/false);
16733 /* Look for the nested-name specifier. */
16734 token = cp_lexer_peek_token (parser->lexer);
16735 cp_parser_nested_name_specifier (parser,
16736 /*typename_keyword_p=*/false,
16737 /*check_dependency_p=*/true,
16738 /*type_p=*/false,
16739 /*is_declaration=*/false);
16740 /* If we found it, and the next token is a `*', then we are
16741 indeed looking at a pointer-to-member operator. */
16742 if (!cp_parser_error_occurred (parser)
16743 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16745 /* Indicate that the `*' operator was used. */
16746 code = INDIRECT_REF;
16748 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16749 error_at (token->location, "%qD is a namespace", parser->scope);
16750 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16751 error_at (token->location, "cannot form pointer to member of "
16752 "non-class %q#T", parser->scope);
16753 else
16755 /* The type of which the member is a member is given by the
16756 current SCOPE. */
16757 *type = parser->scope;
16758 /* The next name will not be qualified. */
16759 parser->scope = NULL_TREE;
16760 parser->qualifying_scope = NULL_TREE;
16761 parser->object_scope = NULL_TREE;
16762 /* Look for optional c++11 attributes. */
16763 attrs = cp_parser_std_attribute_spec_seq (parser);
16764 if (attributes != NULL)
16765 *attributes = attrs;
16766 /* Look for the optional cv-qualifier-seq. */
16767 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16770 /* If that didn't work we don't have a ptr-operator. */
16771 if (!cp_parser_parse_definitely (parser))
16772 cp_parser_error (parser, "expected ptr-operator");
16775 return code;
16778 /* Parse an (optional) cv-qualifier-seq.
16780 cv-qualifier-seq:
16781 cv-qualifier cv-qualifier-seq [opt]
16783 cv-qualifier:
16784 const
16785 volatile
16787 GNU Extension:
16789 cv-qualifier:
16790 __restrict__
16792 Returns a bitmask representing the cv-qualifiers. */
16794 static cp_cv_quals
16795 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16797 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16799 while (true)
16801 cp_token *token;
16802 cp_cv_quals cv_qualifier;
16804 /* Peek at the next token. */
16805 token = cp_lexer_peek_token (parser->lexer);
16806 /* See if it's a cv-qualifier. */
16807 switch (token->keyword)
16809 case RID_CONST:
16810 cv_qualifier = TYPE_QUAL_CONST;
16811 break;
16813 case RID_VOLATILE:
16814 cv_qualifier = TYPE_QUAL_VOLATILE;
16815 break;
16817 case RID_RESTRICT:
16818 cv_qualifier = TYPE_QUAL_RESTRICT;
16819 break;
16821 default:
16822 cv_qualifier = TYPE_UNQUALIFIED;
16823 break;
16826 if (!cv_qualifier)
16827 break;
16829 if (cv_quals & cv_qualifier)
16831 error_at (token->location, "duplicate cv-qualifier");
16832 cp_lexer_purge_token (parser->lexer);
16834 else
16836 cp_lexer_consume_token (parser->lexer);
16837 cv_quals |= cv_qualifier;
16841 return cv_quals;
16844 /* Parse an (optional) virt-specifier-seq.
16846 virt-specifier-seq:
16847 virt-specifier virt-specifier-seq [opt]
16849 virt-specifier:
16850 override
16851 final
16853 Returns a bitmask representing the virt-specifiers. */
16855 static cp_virt_specifiers
16856 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16858 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16860 while (true)
16862 cp_token *token;
16863 cp_virt_specifiers virt_specifier;
16865 /* Peek at the next token. */
16866 token = cp_lexer_peek_token (parser->lexer);
16867 /* See if it's a virt-specifier-qualifier. */
16868 if (token->type != CPP_NAME)
16869 break;
16870 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16872 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16873 virt_specifier = VIRT_SPEC_OVERRIDE;
16875 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16877 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16878 virt_specifier = VIRT_SPEC_FINAL;
16880 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16882 virt_specifier = VIRT_SPEC_FINAL;
16884 else
16885 break;
16887 if (virt_specifiers & virt_specifier)
16889 error_at (token->location, "duplicate virt-specifier");
16890 cp_lexer_purge_token (parser->lexer);
16892 else
16894 cp_lexer_consume_token (parser->lexer);
16895 virt_specifiers |= virt_specifier;
16898 return virt_specifiers;
16901 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16902 is in scope even though it isn't real. */
16904 static void
16905 inject_this_parameter (tree ctype, cp_cv_quals quals)
16907 tree this_parm;
16909 if (current_class_ptr)
16911 /* We don't clear this between NSDMIs. Is it already what we want? */
16912 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16913 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16914 && cp_type_quals (type) == quals)
16915 return;
16918 this_parm = build_this_parm (ctype, quals);
16919 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16920 current_class_ptr = NULL_TREE;
16921 current_class_ref
16922 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16923 current_class_ptr = this_parm;
16926 /* Parse a late-specified return type, if any. This is not a separate
16927 non-terminal, but part of a function declarator, which looks like
16929 -> trailing-type-specifier-seq abstract-declarator(opt)
16931 Returns the type indicated by the type-id.
16933 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16934 function. */
16936 static tree
16937 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16939 cp_token *token;
16940 tree type;
16942 /* Peek at the next token. */
16943 token = cp_lexer_peek_token (parser->lexer);
16944 /* A late-specified return type is indicated by an initial '->'. */
16945 if (token->type != CPP_DEREF)
16946 return NULL_TREE;
16948 /* Consume the ->. */
16949 cp_lexer_consume_token (parser->lexer);
16951 if (quals >= 0)
16953 /* DR 1207: 'this' is in scope in the trailing return type. */
16954 gcc_assert (current_class_ptr == NULL_TREE);
16955 inject_this_parameter (current_class_type, quals);
16958 type = cp_parser_trailing_type_id (parser);
16960 if (quals >= 0)
16961 current_class_ptr = current_class_ref = NULL_TREE;
16963 return type;
16966 /* Parse a declarator-id.
16968 declarator-id:
16969 id-expression
16970 :: [opt] nested-name-specifier [opt] type-name
16972 In the `id-expression' case, the value returned is as for
16973 cp_parser_id_expression if the id-expression was an unqualified-id.
16974 If the id-expression was a qualified-id, then a SCOPE_REF is
16975 returned. The first operand is the scope (either a NAMESPACE_DECL
16976 or TREE_TYPE), but the second is still just a representation of an
16977 unqualified-id. */
16979 static tree
16980 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16982 tree id;
16983 /* The expression must be an id-expression. Assume that qualified
16984 names are the names of types so that:
16986 template <class T>
16987 int S<T>::R::i = 3;
16989 will work; we must treat `S<T>::R' as the name of a type.
16990 Similarly, assume that qualified names are templates, where
16991 required, so that:
16993 template <class T>
16994 int S<T>::R<T>::i = 3;
16996 will work, too. */
16997 id = cp_parser_id_expression (parser,
16998 /*template_keyword_p=*/false,
16999 /*check_dependency_p=*/false,
17000 /*template_p=*/NULL,
17001 /*declarator_p=*/true,
17002 optional_p);
17003 if (id && BASELINK_P (id))
17004 id = BASELINK_FUNCTIONS (id);
17005 return id;
17008 /* Parse a type-id.
17010 type-id:
17011 type-specifier-seq abstract-declarator [opt]
17013 Returns the TYPE specified. */
17015 static tree
17016 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17017 bool is_trailing_return)
17019 cp_decl_specifier_seq type_specifier_seq;
17020 cp_declarator *abstract_declarator;
17022 /* Parse the type-specifier-seq. */
17023 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17024 is_trailing_return,
17025 &type_specifier_seq);
17026 if (type_specifier_seq.type == error_mark_node)
17027 return error_mark_node;
17029 /* There might or might not be an abstract declarator. */
17030 cp_parser_parse_tentatively (parser);
17031 /* Look for the declarator. */
17032 abstract_declarator
17033 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
17034 /*parenthesized_p=*/NULL,
17035 /*member_p=*/false);
17036 /* Check to see if there really was a declarator. */
17037 if (!cp_parser_parse_definitely (parser))
17038 abstract_declarator = NULL;
17040 if (type_specifier_seq.type
17041 && type_uses_auto (type_specifier_seq.type))
17043 /* A type-id with type 'auto' is only ok if the abstract declarator
17044 is a function declarator with a late-specified return type. */
17045 if (abstract_declarator
17046 && abstract_declarator->kind == cdk_function
17047 && abstract_declarator->u.function.late_return_type)
17048 /* OK */;
17049 else
17051 error ("invalid use of %<auto%>");
17052 return error_mark_node;
17056 return groktypename (&type_specifier_seq, abstract_declarator,
17057 is_template_arg);
17060 static tree cp_parser_type_id (cp_parser *parser)
17062 return cp_parser_type_id_1 (parser, false, false);
17065 static tree cp_parser_template_type_arg (cp_parser *parser)
17067 tree r;
17068 const char *saved_message = parser->type_definition_forbidden_message;
17069 parser->type_definition_forbidden_message
17070 = G_("types may not be defined in template arguments");
17071 r = cp_parser_type_id_1 (parser, true, false);
17072 parser->type_definition_forbidden_message = saved_message;
17073 return r;
17076 static tree cp_parser_trailing_type_id (cp_parser *parser)
17078 return cp_parser_type_id_1 (parser, false, true);
17081 /* Parse a type-specifier-seq.
17083 type-specifier-seq:
17084 type-specifier type-specifier-seq [opt]
17086 GNU extension:
17088 type-specifier-seq:
17089 attributes type-specifier-seq [opt]
17091 If IS_DECLARATION is true, we are at the start of a "condition" or
17092 exception-declaration, so we might be followed by a declarator-id.
17094 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17095 i.e. we've just seen "->".
17097 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17099 static void
17100 cp_parser_type_specifier_seq (cp_parser* parser,
17101 bool is_declaration,
17102 bool is_trailing_return,
17103 cp_decl_specifier_seq *type_specifier_seq)
17105 bool seen_type_specifier = false;
17106 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
17107 cp_token *start_token = NULL;
17109 /* Clear the TYPE_SPECIFIER_SEQ. */
17110 clear_decl_specs (type_specifier_seq);
17112 /* In the context of a trailing return type, enum E { } is an
17113 elaborated-type-specifier followed by a function-body, not an
17114 enum-specifier. */
17115 if (is_trailing_return)
17116 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
17118 /* Parse the type-specifiers and attributes. */
17119 while (true)
17121 tree type_specifier;
17122 bool is_cv_qualifier;
17124 /* Check for attributes first. */
17125 if (cp_next_tokens_can_be_attribute_p (parser))
17127 type_specifier_seq->attributes =
17128 chainon (type_specifier_seq->attributes,
17129 cp_parser_attributes_opt (parser));
17130 continue;
17133 /* record the token of the beginning of the type specifier seq,
17134 for error reporting purposes*/
17135 if (!start_token)
17136 start_token = cp_lexer_peek_token (parser->lexer);
17138 /* Look for the type-specifier. */
17139 type_specifier = cp_parser_type_specifier (parser,
17140 flags,
17141 type_specifier_seq,
17142 /*is_declaration=*/false,
17143 NULL,
17144 &is_cv_qualifier);
17145 if (!type_specifier)
17147 /* If the first type-specifier could not be found, this is not a
17148 type-specifier-seq at all. */
17149 if (!seen_type_specifier)
17151 cp_parser_error (parser, "expected type-specifier");
17152 type_specifier_seq->type = error_mark_node;
17153 return;
17155 /* If subsequent type-specifiers could not be found, the
17156 type-specifier-seq is complete. */
17157 break;
17160 seen_type_specifier = true;
17161 /* The standard says that a condition can be:
17163 type-specifier-seq declarator = assignment-expression
17165 However, given:
17167 struct S {};
17168 if (int S = ...)
17170 we should treat the "S" as a declarator, not as a
17171 type-specifier. The standard doesn't say that explicitly for
17172 type-specifier-seq, but it does say that for
17173 decl-specifier-seq in an ordinary declaration. Perhaps it
17174 would be clearer just to allow a decl-specifier-seq here, and
17175 then add a semantic restriction that if any decl-specifiers
17176 that are not type-specifiers appear, the program is invalid. */
17177 if (is_declaration && !is_cv_qualifier)
17178 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
17182 /* Parse a parameter-declaration-clause.
17184 parameter-declaration-clause:
17185 parameter-declaration-list [opt] ... [opt]
17186 parameter-declaration-list , ...
17188 Returns a representation for the parameter declarations. A return
17189 value of NULL indicates a parameter-declaration-clause consisting
17190 only of an ellipsis. */
17192 static tree
17193 cp_parser_parameter_declaration_clause (cp_parser* parser)
17195 tree parameters;
17196 cp_token *token;
17197 bool ellipsis_p;
17198 bool is_error;
17200 /* Peek at the next token. */
17201 token = cp_lexer_peek_token (parser->lexer);
17202 /* Check for trivial parameter-declaration-clauses. */
17203 if (token->type == CPP_ELLIPSIS)
17205 /* Consume the `...' token. */
17206 cp_lexer_consume_token (parser->lexer);
17207 return NULL_TREE;
17209 else if (token->type == CPP_CLOSE_PAREN)
17210 /* There are no parameters. */
17212 #ifndef NO_IMPLICIT_EXTERN_C
17213 if (in_system_header && current_class_type == NULL
17214 && current_lang_name == lang_name_c)
17215 return NULL_TREE;
17216 else
17217 #endif
17218 return void_list_node;
17220 /* Check for `(void)', too, which is a special case. */
17221 else if (token->keyword == RID_VOID
17222 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17223 == CPP_CLOSE_PAREN))
17225 /* Consume the `void' token. */
17226 cp_lexer_consume_token (parser->lexer);
17227 /* There are no parameters. */
17228 return void_list_node;
17231 /* Parse the parameter-declaration-list. */
17232 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17233 /* If a parse error occurred while parsing the
17234 parameter-declaration-list, then the entire
17235 parameter-declaration-clause is erroneous. */
17236 if (is_error)
17237 return NULL;
17239 /* Peek at the next token. */
17240 token = cp_lexer_peek_token (parser->lexer);
17241 /* If it's a `,', the clause should terminate with an ellipsis. */
17242 if (token->type == CPP_COMMA)
17244 /* Consume the `,'. */
17245 cp_lexer_consume_token (parser->lexer);
17246 /* Expect an ellipsis. */
17247 ellipsis_p
17248 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17250 /* It might also be `...' if the optional trailing `,' was
17251 omitted. */
17252 else if (token->type == CPP_ELLIPSIS)
17254 /* Consume the `...' token. */
17255 cp_lexer_consume_token (parser->lexer);
17256 /* And remember that we saw it. */
17257 ellipsis_p = true;
17259 else
17260 ellipsis_p = false;
17262 /* Finish the parameter list. */
17263 if (!ellipsis_p)
17264 parameters = chainon (parameters, void_list_node);
17266 return parameters;
17269 /* Parse a parameter-declaration-list.
17271 parameter-declaration-list:
17272 parameter-declaration
17273 parameter-declaration-list , parameter-declaration
17275 Returns a representation of the parameter-declaration-list, as for
17276 cp_parser_parameter_declaration_clause. However, the
17277 `void_list_node' is never appended to the list. Upon return,
17278 *IS_ERROR will be true iff an error occurred. */
17280 static tree
17281 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17283 tree parameters = NULL_TREE;
17284 tree *tail = &parameters;
17285 bool saved_in_unbraced_linkage_specification_p;
17286 int index = 0;
17288 /* Assume all will go well. */
17289 *is_error = false;
17290 /* The special considerations that apply to a function within an
17291 unbraced linkage specifications do not apply to the parameters
17292 to the function. */
17293 saved_in_unbraced_linkage_specification_p
17294 = parser->in_unbraced_linkage_specification_p;
17295 parser->in_unbraced_linkage_specification_p = false;
17297 /* Look for more parameters. */
17298 while (true)
17300 cp_parameter_declarator *parameter;
17301 tree decl = error_mark_node;
17302 bool parenthesized_p = false;
17303 /* Parse the parameter. */
17304 parameter
17305 = cp_parser_parameter_declaration (parser,
17306 /*template_parm_p=*/false,
17307 &parenthesized_p);
17309 /* We don't know yet if the enclosing context is deprecated, so wait
17310 and warn in grokparms if appropriate. */
17311 deprecated_state = DEPRECATED_SUPPRESS;
17313 if (parameter)
17314 decl = grokdeclarator (parameter->declarator,
17315 &parameter->decl_specifiers,
17316 PARM,
17317 parameter->default_argument != NULL_TREE,
17318 &parameter->decl_specifiers.attributes);
17320 deprecated_state = DEPRECATED_NORMAL;
17322 /* If a parse error occurred parsing the parameter declaration,
17323 then the entire parameter-declaration-list is erroneous. */
17324 if (decl == error_mark_node)
17326 *is_error = true;
17327 parameters = error_mark_node;
17328 break;
17331 if (parameter->decl_specifiers.attributes)
17332 cplus_decl_attributes (&decl,
17333 parameter->decl_specifiers.attributes,
17335 if (DECL_NAME (decl))
17336 decl = pushdecl (decl);
17338 if (decl != error_mark_node)
17340 retrofit_lang_decl (decl);
17341 DECL_PARM_INDEX (decl) = ++index;
17342 DECL_PARM_LEVEL (decl) = function_parm_depth ();
17345 /* Add the new parameter to the list. */
17346 *tail = build_tree_list (parameter->default_argument, decl);
17347 tail = &TREE_CHAIN (*tail);
17349 /* Peek at the next token. */
17350 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17351 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17352 /* These are for Objective-C++ */
17353 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17354 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17355 /* The parameter-declaration-list is complete. */
17356 break;
17357 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17359 cp_token *token;
17361 /* Peek at the next token. */
17362 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17363 /* If it's an ellipsis, then the list is complete. */
17364 if (token->type == CPP_ELLIPSIS)
17365 break;
17366 /* Otherwise, there must be more parameters. Consume the
17367 `,'. */
17368 cp_lexer_consume_token (parser->lexer);
17369 /* When parsing something like:
17371 int i(float f, double d)
17373 we can tell after seeing the declaration for "f" that we
17374 are not looking at an initialization of a variable "i",
17375 but rather at the declaration of a function "i".
17377 Due to the fact that the parsing of template arguments
17378 (as specified to a template-id) requires backtracking we
17379 cannot use this technique when inside a template argument
17380 list. */
17381 if (!parser->in_template_argument_list_p
17382 && !parser->in_type_id_in_expr_p
17383 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17384 /* However, a parameter-declaration of the form
17385 "foat(f)" (which is a valid declaration of a
17386 parameter "f") can also be interpreted as an
17387 expression (the conversion of "f" to "float"). */
17388 && !parenthesized_p)
17389 cp_parser_commit_to_tentative_parse (parser);
17391 else
17393 cp_parser_error (parser, "expected %<,%> or %<...%>");
17394 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17395 cp_parser_skip_to_closing_parenthesis (parser,
17396 /*recovering=*/true,
17397 /*or_comma=*/false,
17398 /*consume_paren=*/false);
17399 break;
17403 parser->in_unbraced_linkage_specification_p
17404 = saved_in_unbraced_linkage_specification_p;
17406 return parameters;
17409 /* Parse a parameter declaration.
17411 parameter-declaration:
17412 decl-specifier-seq ... [opt] declarator
17413 decl-specifier-seq declarator = assignment-expression
17414 decl-specifier-seq ... [opt] abstract-declarator [opt]
17415 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17417 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17418 declares a template parameter. (In that case, a non-nested `>'
17419 token encountered during the parsing of the assignment-expression
17420 is not interpreted as a greater-than operator.)
17422 Returns a representation of the parameter, or NULL if an error
17423 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17424 true iff the declarator is of the form "(p)". */
17426 static cp_parameter_declarator *
17427 cp_parser_parameter_declaration (cp_parser *parser,
17428 bool template_parm_p,
17429 bool *parenthesized_p)
17431 int declares_class_or_enum;
17432 cp_decl_specifier_seq decl_specifiers;
17433 cp_declarator *declarator;
17434 tree default_argument;
17435 cp_token *token = NULL, *declarator_token_start = NULL;
17436 const char *saved_message;
17438 /* In a template parameter, `>' is not an operator.
17440 [temp.param]
17442 When parsing a default template-argument for a non-type
17443 template-parameter, the first non-nested `>' is taken as the end
17444 of the template parameter-list rather than a greater-than
17445 operator. */
17447 /* Type definitions may not appear in parameter types. */
17448 saved_message = parser->type_definition_forbidden_message;
17449 parser->type_definition_forbidden_message
17450 = G_("types may not be defined in parameter types");
17452 /* Parse the declaration-specifiers. */
17453 cp_parser_decl_specifier_seq (parser,
17454 CP_PARSER_FLAGS_NONE,
17455 &decl_specifiers,
17456 &declares_class_or_enum);
17458 /* Complain about missing 'typename' or other invalid type names. */
17459 if (!decl_specifiers.any_type_specifiers_p)
17460 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17462 /* If an error occurred, there's no reason to attempt to parse the
17463 rest of the declaration. */
17464 if (cp_parser_error_occurred (parser))
17466 parser->type_definition_forbidden_message = saved_message;
17467 return NULL;
17470 /* Peek at the next token. */
17471 token = cp_lexer_peek_token (parser->lexer);
17473 /* If the next token is a `)', `,', `=', `>', or `...', then there
17474 is no declarator. However, when variadic templates are enabled,
17475 there may be a declarator following `...'. */
17476 if (token->type == CPP_CLOSE_PAREN
17477 || token->type == CPP_COMMA
17478 || token->type == CPP_EQ
17479 || token->type == CPP_GREATER)
17481 declarator = NULL;
17482 if (parenthesized_p)
17483 *parenthesized_p = false;
17485 /* Otherwise, there should be a declarator. */
17486 else
17488 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17489 parser->default_arg_ok_p = false;
17491 /* After seeing a decl-specifier-seq, if the next token is not a
17492 "(", there is no possibility that the code is a valid
17493 expression. Therefore, if parsing tentatively, we commit at
17494 this point. */
17495 if (!parser->in_template_argument_list_p
17496 /* In an expression context, having seen:
17498 (int((char ...
17500 we cannot be sure whether we are looking at a
17501 function-type (taking a "char" as a parameter) or a cast
17502 of some object of type "char" to "int". */
17503 && !parser->in_type_id_in_expr_p
17504 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17505 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17506 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17507 cp_parser_commit_to_tentative_parse (parser);
17508 /* Parse the declarator. */
17509 declarator_token_start = token;
17510 declarator = cp_parser_declarator (parser,
17511 CP_PARSER_DECLARATOR_EITHER,
17512 /*ctor_dtor_or_conv_p=*/NULL,
17513 parenthesized_p,
17514 /*member_p=*/false);
17515 parser->default_arg_ok_p = saved_default_arg_ok_p;
17516 /* After the declarator, allow more attributes. */
17517 decl_specifiers.attributes
17518 = chainon (decl_specifiers.attributes,
17519 cp_parser_attributes_opt (parser));
17522 /* If the next token is an ellipsis, and we have not seen a
17523 declarator name, and the type of the declarator contains parameter
17524 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17525 a parameter pack expansion expression. Otherwise, leave the
17526 ellipsis for a C-style variadic function. */
17527 token = cp_lexer_peek_token (parser->lexer);
17528 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17530 tree type = decl_specifiers.type;
17532 if (type && DECL_P (type))
17533 type = TREE_TYPE (type);
17535 if (type
17536 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17537 && declarator_can_be_parameter_pack (declarator)
17538 && (!declarator || !declarator->parameter_pack_p)
17539 && uses_parameter_packs (type))
17541 /* Consume the `...'. */
17542 cp_lexer_consume_token (parser->lexer);
17543 maybe_warn_variadic_templates ();
17545 /* Build a pack expansion type */
17546 if (declarator)
17547 declarator->parameter_pack_p = true;
17548 else
17549 decl_specifiers.type = make_pack_expansion (type);
17553 /* The restriction on defining new types applies only to the type
17554 of the parameter, not to the default argument. */
17555 parser->type_definition_forbidden_message = saved_message;
17557 /* If the next token is `=', then process a default argument. */
17558 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17560 token = cp_lexer_peek_token (parser->lexer);
17561 /* If we are defining a class, then the tokens that make up the
17562 default argument must be saved and processed later. */
17563 if (!template_parm_p && at_class_scope_p ()
17564 && TYPE_BEING_DEFINED (current_class_type)
17565 && !LAMBDA_TYPE_P (current_class_type))
17566 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17567 /* Outside of a class definition, we can just parse the
17568 assignment-expression. */
17569 else
17570 default_argument
17571 = cp_parser_default_argument (parser, template_parm_p);
17573 if (!parser->default_arg_ok_p)
17575 if (flag_permissive)
17576 warning (0, "deprecated use of default argument for parameter of non-function");
17577 else
17579 error_at (token->location,
17580 "default arguments are only "
17581 "permitted for function parameters");
17582 default_argument = NULL_TREE;
17585 else if ((declarator && declarator->parameter_pack_p)
17586 || (decl_specifiers.type
17587 && PACK_EXPANSION_P (decl_specifiers.type)))
17589 /* Find the name of the parameter pack. */
17590 cp_declarator *id_declarator = declarator;
17591 while (id_declarator && id_declarator->kind != cdk_id)
17592 id_declarator = id_declarator->declarator;
17594 if (id_declarator && id_declarator->kind == cdk_id)
17595 error_at (declarator_token_start->location,
17596 template_parm_p
17597 ? G_("template parameter pack %qD "
17598 "cannot have a default argument")
17599 : G_("parameter pack %qD cannot have "
17600 "a default argument"),
17601 id_declarator->u.id.unqualified_name);
17602 else
17603 error_at (declarator_token_start->location,
17604 template_parm_p
17605 ? G_("template parameter pack cannot have "
17606 "a default argument")
17607 : G_("parameter pack cannot have a "
17608 "default argument"));
17610 default_argument = NULL_TREE;
17613 else
17614 default_argument = NULL_TREE;
17616 return make_parameter_declarator (&decl_specifiers,
17617 declarator,
17618 default_argument);
17621 /* Parse a default argument and return it.
17623 TEMPLATE_PARM_P is true if this is a default argument for a
17624 non-type template parameter. */
17625 static tree
17626 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17628 tree default_argument = NULL_TREE;
17629 bool saved_greater_than_is_operator_p;
17630 bool saved_local_variables_forbidden_p;
17631 bool non_constant_p, is_direct_init;
17633 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17634 set correctly. */
17635 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17636 parser->greater_than_is_operator_p = !template_parm_p;
17637 /* Local variable names (and the `this' keyword) may not
17638 appear in a default argument. */
17639 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17640 parser->local_variables_forbidden_p = true;
17641 /* Parse the assignment-expression. */
17642 if (template_parm_p)
17643 push_deferring_access_checks (dk_no_deferred);
17644 default_argument
17645 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17646 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17647 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17648 if (template_parm_p)
17649 pop_deferring_access_checks ();
17650 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17651 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17653 return default_argument;
17656 /* Parse a function-body.
17658 function-body:
17659 compound_statement */
17661 static void
17662 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
17664 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
17667 /* Parse a ctor-initializer-opt followed by a function-body. Return
17668 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
17669 is true we are parsing a function-try-block. */
17671 static bool
17672 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
17673 bool in_function_try_block)
17675 tree body, list;
17676 bool ctor_initializer_p;
17677 const bool check_body_p =
17678 DECL_CONSTRUCTOR_P (current_function_decl)
17679 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17680 tree last = NULL;
17682 /* Begin the function body. */
17683 body = begin_function_body ();
17684 /* Parse the optional ctor-initializer. */
17685 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17687 /* If we're parsing a constexpr constructor definition, we need
17688 to check that the constructor body is indeed empty. However,
17689 before we get to cp_parser_function_body lot of junk has been
17690 generated, so we can't just check that we have an empty block.
17691 Rather we take a snapshot of the outermost block, and check whether
17692 cp_parser_function_body changed its state. */
17693 if (check_body_p)
17695 list = cur_stmt_list;
17696 if (STATEMENT_LIST_TAIL (list))
17697 last = STATEMENT_LIST_TAIL (list)->stmt;
17699 /* Parse the function-body. */
17700 cp_parser_function_body (parser, in_function_try_block);
17701 if (check_body_p)
17702 check_constexpr_ctor_body (last, list);
17703 /* Finish the function body. */
17704 finish_function_body (body);
17706 return ctor_initializer_p;
17709 /* Parse an initializer.
17711 initializer:
17712 = initializer-clause
17713 ( expression-list )
17715 Returns an expression representing the initializer. If no
17716 initializer is present, NULL_TREE is returned.
17718 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17719 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17720 set to TRUE if there is no initializer present. If there is an
17721 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17722 is set to true; otherwise it is set to false. */
17724 static tree
17725 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17726 bool* non_constant_p)
17728 cp_token *token;
17729 tree init;
17731 /* Peek at the next token. */
17732 token = cp_lexer_peek_token (parser->lexer);
17734 /* Let our caller know whether or not this initializer was
17735 parenthesized. */
17736 *is_direct_init = (token->type != CPP_EQ);
17737 /* Assume that the initializer is constant. */
17738 *non_constant_p = false;
17740 if (token->type == CPP_EQ)
17742 /* Consume the `='. */
17743 cp_lexer_consume_token (parser->lexer);
17744 /* Parse the initializer-clause. */
17745 init = cp_parser_initializer_clause (parser, non_constant_p);
17747 else if (token->type == CPP_OPEN_PAREN)
17749 vec<tree, va_gc> *vec;
17750 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17751 /*cast_p=*/false,
17752 /*allow_expansion_p=*/true,
17753 non_constant_p);
17754 if (vec == NULL)
17755 return error_mark_node;
17756 init = build_tree_list_vec (vec);
17757 release_tree_vector (vec);
17759 else if (token->type == CPP_OPEN_BRACE)
17761 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17762 init = cp_parser_braced_list (parser, non_constant_p);
17763 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17765 else
17767 /* Anything else is an error. */
17768 cp_parser_error (parser, "expected initializer");
17769 init = error_mark_node;
17772 return init;
17775 /* Parse an initializer-clause.
17777 initializer-clause:
17778 assignment-expression
17779 braced-init-list
17781 Returns an expression representing the initializer.
17783 If the `assignment-expression' production is used the value
17784 returned is simply a representation for the expression.
17786 Otherwise, calls cp_parser_braced_list. */
17788 static tree
17789 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17791 tree initializer;
17793 /* Assume the expression is constant. */
17794 *non_constant_p = false;
17796 /* If it is not a `{', then we are looking at an
17797 assignment-expression. */
17798 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17800 initializer
17801 = cp_parser_constant_expression (parser,
17802 /*allow_non_constant_p=*/true,
17803 non_constant_p);
17805 else
17806 initializer = cp_parser_braced_list (parser, non_constant_p);
17808 return initializer;
17811 /* Parse a brace-enclosed initializer list.
17813 braced-init-list:
17814 { initializer-list , [opt] }
17817 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17818 the elements of the initializer-list (or NULL, if the last
17819 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17820 NULL_TREE. There is no way to detect whether or not the optional
17821 trailing `,' was provided. NON_CONSTANT_P is as for
17822 cp_parser_initializer. */
17824 static tree
17825 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17827 tree initializer;
17829 /* Consume the `{' token. */
17830 cp_lexer_consume_token (parser->lexer);
17831 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17832 initializer = make_node (CONSTRUCTOR);
17833 /* If it's not a `}', then there is a non-trivial initializer. */
17834 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17836 /* Parse the initializer list. */
17837 CONSTRUCTOR_ELTS (initializer)
17838 = cp_parser_initializer_list (parser, non_constant_p);
17839 /* A trailing `,' token is allowed. */
17840 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17841 cp_lexer_consume_token (parser->lexer);
17843 /* Now, there should be a trailing `}'. */
17844 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17845 TREE_TYPE (initializer) = init_list_type_node;
17846 return initializer;
17849 /* Parse an initializer-list.
17851 initializer-list:
17852 initializer-clause ... [opt]
17853 initializer-list , initializer-clause ... [opt]
17855 GNU Extension:
17857 initializer-list:
17858 designation initializer-clause ...[opt]
17859 initializer-list , designation initializer-clause ...[opt]
17861 designation:
17862 . identifier =
17863 identifier :
17864 [ constant-expression ] =
17866 Returns a vec of constructor_elt. The VALUE of each elt is an expression
17867 for the initializer. If the INDEX of the elt is non-NULL, it is the
17868 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17869 as for cp_parser_initializer. */
17871 static vec<constructor_elt, va_gc> *
17872 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17874 vec<constructor_elt, va_gc> *v = NULL;
17876 /* Assume all of the expressions are constant. */
17877 *non_constant_p = false;
17879 /* Parse the rest of the list. */
17880 while (true)
17882 cp_token *token;
17883 tree designator;
17884 tree initializer;
17885 bool clause_non_constant_p;
17887 /* If the next token is an identifier and the following one is a
17888 colon, we are looking at the GNU designated-initializer
17889 syntax. */
17890 if (cp_parser_allow_gnu_extensions_p (parser)
17891 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17892 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17894 /* Warn the user that they are using an extension. */
17895 pedwarn (input_location, OPT_Wpedantic,
17896 "ISO C++ does not allow designated initializers");
17897 /* Consume the identifier. */
17898 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17899 /* Consume the `:'. */
17900 cp_lexer_consume_token (parser->lexer);
17902 /* Also handle the C99 syntax, '. id ='. */
17903 else if (cp_parser_allow_gnu_extensions_p (parser)
17904 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17905 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17906 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17908 /* Warn the user that they are using an extension. */
17909 pedwarn (input_location, OPT_Wpedantic,
17910 "ISO C++ does not allow C99 designated initializers");
17911 /* Consume the `.'. */
17912 cp_lexer_consume_token (parser->lexer);
17913 /* Consume the identifier. */
17914 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17915 /* Consume the `='. */
17916 cp_lexer_consume_token (parser->lexer);
17918 /* Also handle C99 array designators, '[ const ] ='. */
17919 else if (cp_parser_allow_gnu_extensions_p (parser)
17920 && !c_dialect_objc ()
17921 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17923 /* In C++11, [ could start a lambda-introducer. */
17924 cp_parser_parse_tentatively (parser);
17925 cp_lexer_consume_token (parser->lexer);
17926 designator = cp_parser_constant_expression (parser, false, NULL);
17927 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17928 cp_parser_require (parser, CPP_EQ, RT_EQ);
17929 if (!cp_parser_parse_definitely (parser))
17930 designator = NULL_TREE;
17932 else
17933 designator = NULL_TREE;
17935 /* Parse the initializer. */
17936 initializer = cp_parser_initializer_clause (parser,
17937 &clause_non_constant_p);
17938 /* If any clause is non-constant, so is the entire initializer. */
17939 if (clause_non_constant_p)
17940 *non_constant_p = true;
17942 /* If we have an ellipsis, this is an initializer pack
17943 expansion. */
17944 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17946 /* Consume the `...'. */
17947 cp_lexer_consume_token (parser->lexer);
17949 /* Turn the initializer into an initializer expansion. */
17950 initializer = make_pack_expansion (initializer);
17953 /* Add it to the vector. */
17954 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17956 /* If the next token is not a comma, we have reached the end of
17957 the list. */
17958 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17959 break;
17961 /* Peek at the next token. */
17962 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17963 /* If the next token is a `}', then we're still done. An
17964 initializer-clause can have a trailing `,' after the
17965 initializer-list and before the closing `}'. */
17966 if (token->type == CPP_CLOSE_BRACE)
17967 break;
17969 /* Consume the `,' token. */
17970 cp_lexer_consume_token (parser->lexer);
17973 return v;
17976 /* Classes [gram.class] */
17978 /* Parse a class-name.
17980 class-name:
17981 identifier
17982 template-id
17984 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17985 to indicate that names looked up in dependent types should be
17986 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17987 keyword has been used to indicate that the name that appears next
17988 is a template. TAG_TYPE indicates the explicit tag given before
17989 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17990 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17991 is the class being defined in a class-head.
17993 Returns the TYPE_DECL representing the class. */
17995 static tree
17996 cp_parser_class_name (cp_parser *parser,
17997 bool typename_keyword_p,
17998 bool template_keyword_p,
17999 enum tag_types tag_type,
18000 bool check_dependency_p,
18001 bool class_head_p,
18002 bool is_declaration)
18004 tree decl;
18005 tree scope;
18006 bool typename_p;
18007 cp_token *token;
18008 tree identifier = NULL_TREE;
18010 /* All class-names start with an identifier. */
18011 token = cp_lexer_peek_token (parser->lexer);
18012 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
18014 cp_parser_error (parser, "expected class-name");
18015 return error_mark_node;
18018 /* PARSER->SCOPE can be cleared when parsing the template-arguments
18019 to a template-id, so we save it here. */
18020 scope = parser->scope;
18021 if (scope == error_mark_node)
18022 return error_mark_node;
18024 /* Any name names a type if we're following the `typename' keyword
18025 in a qualified name where the enclosing scope is type-dependent. */
18026 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
18027 && dependent_type_p (scope));
18028 /* Handle the common case (an identifier, but not a template-id)
18029 efficiently. */
18030 if (token->type == CPP_NAME
18031 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
18033 cp_token *identifier_token;
18034 bool ambiguous_p;
18036 /* Look for the identifier. */
18037 identifier_token = cp_lexer_peek_token (parser->lexer);
18038 ambiguous_p = identifier_token->ambiguous_p;
18039 identifier = cp_parser_identifier (parser);
18040 /* If the next token isn't an identifier, we are certainly not
18041 looking at a class-name. */
18042 if (identifier == error_mark_node)
18043 decl = error_mark_node;
18044 /* If we know this is a type-name, there's no need to look it
18045 up. */
18046 else if (typename_p)
18047 decl = identifier;
18048 else
18050 tree ambiguous_decls;
18051 /* If we already know that this lookup is ambiguous, then
18052 we've already issued an error message; there's no reason
18053 to check again. */
18054 if (ambiguous_p)
18056 cp_parser_simulate_error (parser);
18057 return error_mark_node;
18059 /* If the next token is a `::', then the name must be a type
18060 name.
18062 [basic.lookup.qual]
18064 During the lookup for a name preceding the :: scope
18065 resolution operator, object, function, and enumerator
18066 names are ignored. */
18067 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18068 tag_type = typename_type;
18069 /* Look up the name. */
18070 decl = cp_parser_lookup_name (parser, identifier,
18071 tag_type,
18072 /*is_template=*/false,
18073 /*is_namespace=*/false,
18074 check_dependency_p,
18075 &ambiguous_decls,
18076 identifier_token->location);
18077 if (ambiguous_decls)
18079 if (cp_parser_parsing_tentatively (parser))
18080 cp_parser_simulate_error (parser);
18081 return error_mark_node;
18085 else
18087 /* Try a template-id. */
18088 decl = cp_parser_template_id (parser, template_keyword_p,
18089 check_dependency_p,
18090 tag_type,
18091 is_declaration);
18092 if (decl == error_mark_node)
18093 return error_mark_node;
18096 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
18098 /* If this is a typename, create a TYPENAME_TYPE. */
18099 if (typename_p && decl != error_mark_node)
18101 decl = make_typename_type (scope, decl, typename_type,
18102 /*complain=*/tf_error);
18103 if (decl != error_mark_node)
18104 decl = TYPE_NAME (decl);
18107 decl = strip_using_decl (decl);
18109 /* Check to see that it is really the name of a class. */
18110 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18111 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
18112 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18113 /* Situations like this:
18115 template <typename T> struct A {
18116 typename T::template X<int>::I i;
18119 are problematic. Is `T::template X<int>' a class-name? The
18120 standard does not seem to be definitive, but there is no other
18121 valid interpretation of the following `::'. Therefore, those
18122 names are considered class-names. */
18124 decl = make_typename_type (scope, decl, tag_type, tf_error);
18125 if (decl != error_mark_node)
18126 decl = TYPE_NAME (decl);
18128 else if (TREE_CODE (decl) != TYPE_DECL
18129 || TREE_TYPE (decl) == error_mark_node
18130 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
18131 /* In Objective-C 2.0, a classname followed by '.' starts a
18132 dot-syntax expression, and it's not a type-name. */
18133 || (c_dialect_objc ()
18134 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
18135 && objc_is_class_name (decl)))
18136 decl = error_mark_node;
18138 if (decl == error_mark_node)
18139 cp_parser_error (parser, "expected class-name");
18140 else if (identifier && !parser->scope)
18141 maybe_note_name_used_in_class (identifier, decl);
18143 return decl;
18146 /* Parse a class-specifier.
18148 class-specifier:
18149 class-head { member-specification [opt] }
18151 Returns the TREE_TYPE representing the class. */
18153 static tree
18154 cp_parser_class_specifier_1 (cp_parser* parser)
18156 tree type;
18157 tree attributes = NULL_TREE;
18158 bool nested_name_specifier_p;
18159 unsigned saved_num_template_parameter_lists;
18160 bool saved_in_function_body;
18161 unsigned char in_statement;
18162 bool in_switch_statement_p;
18163 bool saved_in_unbraced_linkage_specification_p;
18164 tree old_scope = NULL_TREE;
18165 tree scope = NULL_TREE;
18166 cp_token *closing_brace;
18168 push_deferring_access_checks (dk_no_deferred);
18170 /* Parse the class-head. */
18171 type = cp_parser_class_head (parser,
18172 &nested_name_specifier_p);
18173 /* If the class-head was a semantic disaster, skip the entire body
18174 of the class. */
18175 if (!type)
18177 cp_parser_skip_to_end_of_block_or_statement (parser);
18178 pop_deferring_access_checks ();
18179 return error_mark_node;
18182 /* Look for the `{'. */
18183 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
18185 pop_deferring_access_checks ();
18186 return error_mark_node;
18189 /* Issue an error message if type-definitions are forbidden here. */
18190 cp_parser_check_type_definition (parser);
18191 /* Remember that we are defining one more class. */
18192 ++parser->num_classes_being_defined;
18193 /* Inside the class, surrounding template-parameter-lists do not
18194 apply. */
18195 saved_num_template_parameter_lists
18196 = parser->num_template_parameter_lists;
18197 parser->num_template_parameter_lists = 0;
18198 /* We are not in a function body. */
18199 saved_in_function_body = parser->in_function_body;
18200 parser->in_function_body = false;
18201 /* Or in a loop. */
18202 in_statement = parser->in_statement;
18203 parser->in_statement = 0;
18204 /* Or in a switch. */
18205 in_switch_statement_p = parser->in_switch_statement_p;
18206 parser->in_switch_statement_p = false;
18207 /* We are not immediately inside an extern "lang" block. */
18208 saved_in_unbraced_linkage_specification_p
18209 = parser->in_unbraced_linkage_specification_p;
18210 parser->in_unbraced_linkage_specification_p = false;
18212 /* Start the class. */
18213 if (nested_name_specifier_p)
18215 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18216 old_scope = push_inner_scope (scope);
18218 type = begin_class_definition (type);
18220 if (type == error_mark_node)
18221 /* If the type is erroneous, skip the entire body of the class. */
18222 cp_parser_skip_to_closing_brace (parser);
18223 else
18224 /* Parse the member-specification. */
18225 cp_parser_member_specification_opt (parser);
18227 /* Look for the trailing `}'. */
18228 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18229 /* Look for trailing attributes to apply to this class. */
18230 if (cp_parser_allow_gnu_extensions_p (parser))
18231 attributes = cp_parser_gnu_attributes_opt (parser);
18232 if (type != error_mark_node)
18233 type = finish_struct (type, attributes);
18234 if (nested_name_specifier_p)
18235 pop_inner_scope (old_scope, scope);
18237 /* We've finished a type definition. Check for the common syntax
18238 error of forgetting a semicolon after the definition. We need to
18239 be careful, as we can't just check for not-a-semicolon and be done
18240 with it; the user might have typed:
18242 class X { } c = ...;
18243 class X { } *p = ...;
18245 and so forth. Instead, enumerate all the possible tokens that
18246 might follow this production; if we don't see one of them, then
18247 complain and silently insert the semicolon. */
18249 cp_token *token = cp_lexer_peek_token (parser->lexer);
18250 bool want_semicolon = true;
18252 if (cp_next_tokens_can_be_std_attribute_p (parser))
18253 /* Don't try to parse c++11 attributes here. As per the
18254 grammar, that should be a task for
18255 cp_parser_decl_specifier_seq. */
18256 want_semicolon = false;
18258 switch (token->type)
18260 case CPP_NAME:
18261 case CPP_SEMICOLON:
18262 case CPP_MULT:
18263 case CPP_AND:
18264 case CPP_OPEN_PAREN:
18265 case CPP_CLOSE_PAREN:
18266 case CPP_COMMA:
18267 want_semicolon = false;
18268 break;
18270 /* While it's legal for type qualifiers and storage class
18271 specifiers to follow type definitions in the grammar, only
18272 compiler testsuites contain code like that. Assume that if
18273 we see such code, then what we're really seeing is a case
18274 like:
18276 class X { }
18277 const <type> var = ...;
18281 class Y { }
18282 static <type> func (...) ...
18284 i.e. the qualifier or specifier applies to the next
18285 declaration. To do so, however, we need to look ahead one
18286 more token to see if *that* token is a type specifier.
18288 This code could be improved to handle:
18290 class Z { }
18291 static const <type> var = ...; */
18292 case CPP_KEYWORD:
18293 if (keyword_is_decl_specifier (token->keyword))
18295 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18297 /* Handling user-defined types here would be nice, but very
18298 tricky. */
18299 want_semicolon
18300 = (lookahead->type == CPP_KEYWORD
18301 && keyword_begins_type_specifier (lookahead->keyword));
18303 break;
18304 default:
18305 break;
18308 /* If we don't have a type, then something is very wrong and we
18309 shouldn't try to do anything clever. Likewise for not seeing the
18310 closing brace. */
18311 if (closing_brace && TYPE_P (type) && want_semicolon)
18313 cp_token_position prev
18314 = cp_lexer_previous_token_position (parser->lexer);
18315 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18316 location_t loc = prev_token->location;
18318 if (CLASSTYPE_DECLARED_CLASS (type))
18319 error_at (loc, "expected %<;%> after class definition");
18320 else if (TREE_CODE (type) == RECORD_TYPE)
18321 error_at (loc, "expected %<;%> after struct definition");
18322 else if (TREE_CODE (type) == UNION_TYPE)
18323 error_at (loc, "expected %<;%> after union definition");
18324 else
18325 gcc_unreachable ();
18327 /* Unget one token and smash it to look as though we encountered
18328 a semicolon in the input stream. */
18329 cp_lexer_set_token_position (parser->lexer, prev);
18330 token = cp_lexer_peek_token (parser->lexer);
18331 token->type = CPP_SEMICOLON;
18332 token->keyword = RID_MAX;
18336 /* If this class is not itself within the scope of another class,
18337 then we need to parse the bodies of all of the queued function
18338 definitions. Note that the queued functions defined in a class
18339 are not always processed immediately following the
18340 class-specifier for that class. Consider:
18342 struct A {
18343 struct B { void f() { sizeof (A); } };
18346 If `f' were processed before the processing of `A' were
18347 completed, there would be no way to compute the size of `A'.
18348 Note that the nesting we are interested in here is lexical --
18349 not the semantic nesting given by TYPE_CONTEXT. In particular,
18350 for:
18352 struct A { struct B; };
18353 struct A::B { void f() { } };
18355 there is no need to delay the parsing of `A::B::f'. */
18356 if (--parser->num_classes_being_defined == 0)
18358 tree decl;
18359 tree class_type = NULL_TREE;
18360 tree pushed_scope = NULL_TREE;
18361 unsigned ix;
18362 cp_default_arg_entry *e;
18363 tree save_ccp, save_ccr;
18365 /* In a first pass, parse default arguments to the functions.
18366 Then, in a second pass, parse the bodies of the functions.
18367 This two-phased approach handles cases like:
18369 struct S {
18370 void f() { g(); }
18371 void g(int i = 3);
18375 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
18377 decl = e->decl;
18378 /* If there are default arguments that have not yet been processed,
18379 take care of them now. */
18380 if (class_type != e->class_type)
18382 if (pushed_scope)
18383 pop_scope (pushed_scope);
18384 class_type = e->class_type;
18385 pushed_scope = push_scope (class_type);
18387 /* Make sure that any template parameters are in scope. */
18388 maybe_begin_member_template_processing (decl);
18389 /* Parse the default argument expressions. */
18390 cp_parser_late_parsing_default_args (parser, decl);
18391 /* Remove any template parameters from the symbol table. */
18392 maybe_end_member_template_processing ();
18394 vec_safe_truncate (unparsed_funs_with_default_args, 0);
18395 /* Now parse any NSDMIs. */
18396 save_ccp = current_class_ptr;
18397 save_ccr = current_class_ref;
18398 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
18400 if (class_type != DECL_CONTEXT (decl))
18402 if (pushed_scope)
18403 pop_scope (pushed_scope);
18404 class_type = DECL_CONTEXT (decl);
18405 pushed_scope = push_scope (class_type);
18407 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18408 cp_parser_late_parsing_nsdmi (parser, decl);
18410 vec_safe_truncate (unparsed_nsdmis, 0);
18411 current_class_ptr = save_ccp;
18412 current_class_ref = save_ccr;
18413 if (pushed_scope)
18414 pop_scope (pushed_scope);
18415 /* Now parse the body of the functions. */
18416 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
18417 cp_parser_late_parsing_for_member (parser, decl);
18418 vec_safe_truncate (unparsed_funs_with_definitions, 0);
18421 /* Put back any saved access checks. */
18422 pop_deferring_access_checks ();
18424 /* Restore saved state. */
18425 parser->in_switch_statement_p = in_switch_statement_p;
18426 parser->in_statement = in_statement;
18427 parser->in_function_body = saved_in_function_body;
18428 parser->num_template_parameter_lists
18429 = saved_num_template_parameter_lists;
18430 parser->in_unbraced_linkage_specification_p
18431 = saved_in_unbraced_linkage_specification_p;
18433 return type;
18436 static tree
18437 cp_parser_class_specifier (cp_parser* parser)
18439 tree ret;
18440 timevar_push (TV_PARSE_STRUCT);
18441 ret = cp_parser_class_specifier_1 (parser);
18442 timevar_pop (TV_PARSE_STRUCT);
18443 return ret;
18446 /* Parse a class-head.
18448 class-head:
18449 class-key identifier [opt] base-clause [opt]
18450 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18451 class-key nested-name-specifier [opt] template-id
18452 base-clause [opt]
18454 class-virt-specifier:
18455 final
18457 GNU Extensions:
18458 class-key attributes identifier [opt] base-clause [opt]
18459 class-key attributes nested-name-specifier identifier base-clause [opt]
18460 class-key attributes nested-name-specifier [opt] template-id
18461 base-clause [opt]
18463 Upon return BASES is initialized to the list of base classes (or
18464 NULL, if there are none) in the same form returned by
18465 cp_parser_base_clause.
18467 Returns the TYPE of the indicated class. Sets
18468 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18469 involving a nested-name-specifier was used, and FALSE otherwise.
18471 Returns error_mark_node if this is not a class-head.
18473 Returns NULL_TREE if the class-head is syntactically valid, but
18474 semantically invalid in a way that means we should skip the entire
18475 body of the class. */
18477 static tree
18478 cp_parser_class_head (cp_parser* parser,
18479 bool* nested_name_specifier_p)
18481 tree nested_name_specifier;
18482 enum tag_types class_key;
18483 tree id = NULL_TREE;
18484 tree type = NULL_TREE;
18485 tree attributes;
18486 tree bases;
18487 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18488 bool template_id_p = false;
18489 bool qualified_p = false;
18490 bool invalid_nested_name_p = false;
18491 bool invalid_explicit_specialization_p = false;
18492 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18493 tree pushed_scope = NULL_TREE;
18494 unsigned num_templates;
18495 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18496 /* Assume no nested-name-specifier will be present. */
18497 *nested_name_specifier_p = false;
18498 /* Assume no template parameter lists will be used in defining the
18499 type. */
18500 num_templates = 0;
18501 parser->colon_corrects_to_scope_p = false;
18503 /* Look for the class-key. */
18504 class_key = cp_parser_class_key (parser);
18505 if (class_key == none_type)
18506 return error_mark_node;
18508 /* Parse the attributes. */
18509 attributes = cp_parser_attributes_opt (parser);
18511 /* If the next token is `::', that is invalid -- but sometimes
18512 people do try to write:
18514 struct ::S {};
18516 Handle this gracefully by accepting the extra qualifier, and then
18517 issuing an error about it later if this really is a
18518 class-head. If it turns out just to be an elaborated type
18519 specifier, remain silent. */
18520 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18521 qualified_p = true;
18523 push_deferring_access_checks (dk_no_check);
18525 /* Determine the name of the class. Begin by looking for an
18526 optional nested-name-specifier. */
18527 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18528 nested_name_specifier
18529 = cp_parser_nested_name_specifier_opt (parser,
18530 /*typename_keyword_p=*/false,
18531 /*check_dependency_p=*/false,
18532 /*type_p=*/true,
18533 /*is_declaration=*/false);
18534 /* If there was a nested-name-specifier, then there *must* be an
18535 identifier. */
18536 if (nested_name_specifier)
18538 type_start_token = cp_lexer_peek_token (parser->lexer);
18539 /* Although the grammar says `identifier', it really means
18540 `class-name' or `template-name'. You are only allowed to
18541 define a class that has already been declared with this
18542 syntax.
18544 The proposed resolution for Core Issue 180 says that wherever
18545 you see `class T::X' you should treat `X' as a type-name.
18547 It is OK to define an inaccessible class; for example:
18549 class A { class B; };
18550 class A::B {};
18552 We do not know if we will see a class-name, or a
18553 template-name. We look for a class-name first, in case the
18554 class-name is a template-id; if we looked for the
18555 template-name first we would stop after the template-name. */
18556 cp_parser_parse_tentatively (parser);
18557 type = cp_parser_class_name (parser,
18558 /*typename_keyword_p=*/false,
18559 /*template_keyword_p=*/false,
18560 class_type,
18561 /*check_dependency_p=*/false,
18562 /*class_head_p=*/true,
18563 /*is_declaration=*/false);
18564 /* If that didn't work, ignore the nested-name-specifier. */
18565 if (!cp_parser_parse_definitely (parser))
18567 invalid_nested_name_p = true;
18568 type_start_token = cp_lexer_peek_token (parser->lexer);
18569 id = cp_parser_identifier (parser);
18570 if (id == error_mark_node)
18571 id = NULL_TREE;
18573 /* If we could not find a corresponding TYPE, treat this
18574 declaration like an unqualified declaration. */
18575 if (type == error_mark_node)
18576 nested_name_specifier = NULL_TREE;
18577 /* Otherwise, count the number of templates used in TYPE and its
18578 containing scopes. */
18579 else
18581 tree scope;
18583 for (scope = TREE_TYPE (type);
18584 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18585 scope = (TYPE_P (scope)
18586 ? TYPE_CONTEXT (scope)
18587 : DECL_CONTEXT (scope)))
18588 if (TYPE_P (scope)
18589 && CLASS_TYPE_P (scope)
18590 && CLASSTYPE_TEMPLATE_INFO (scope)
18591 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18592 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
18593 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
18594 ++num_templates;
18597 /* Otherwise, the identifier is optional. */
18598 else
18600 /* We don't know whether what comes next is a template-id,
18601 an identifier, or nothing at all. */
18602 cp_parser_parse_tentatively (parser);
18603 /* Check for a template-id. */
18604 type_start_token = cp_lexer_peek_token (parser->lexer);
18605 id = cp_parser_template_id (parser,
18606 /*template_keyword_p=*/false,
18607 /*check_dependency_p=*/true,
18608 class_key,
18609 /*is_declaration=*/true);
18610 /* If that didn't work, it could still be an identifier. */
18611 if (!cp_parser_parse_definitely (parser))
18613 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18615 type_start_token = cp_lexer_peek_token (parser->lexer);
18616 id = cp_parser_identifier (parser);
18618 else
18619 id = NULL_TREE;
18621 else
18623 template_id_p = true;
18624 ++num_templates;
18628 pop_deferring_access_checks ();
18630 if (id)
18632 cp_parser_check_for_invalid_template_id (parser, id,
18633 class_key,
18634 type_start_token->location);
18636 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18638 /* If it's not a `:' or a `{' then we can't really be looking at a
18639 class-head, since a class-head only appears as part of a
18640 class-specifier. We have to detect this situation before calling
18641 xref_tag, since that has irreversible side-effects. */
18642 if (!cp_parser_next_token_starts_class_definition_p (parser))
18644 cp_parser_error (parser, "expected %<{%> or %<:%>");
18645 type = error_mark_node;
18646 goto out;
18649 /* At this point, we're going ahead with the class-specifier, even
18650 if some other problem occurs. */
18651 cp_parser_commit_to_tentative_parse (parser);
18652 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18654 cp_parser_error (parser,
18655 "cannot specify %<override%> for a class");
18656 type = error_mark_node;
18657 goto out;
18659 /* Issue the error about the overly-qualified name now. */
18660 if (qualified_p)
18662 cp_parser_error (parser,
18663 "global qualification of class name is invalid");
18664 type = error_mark_node;
18665 goto out;
18667 else if (invalid_nested_name_p)
18669 cp_parser_error (parser,
18670 "qualified name does not name a class");
18671 type = error_mark_node;
18672 goto out;
18674 else if (nested_name_specifier)
18676 tree scope;
18678 /* Reject typedef-names in class heads. */
18679 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18681 error_at (type_start_token->location,
18682 "invalid class name in declaration of %qD",
18683 type);
18684 type = NULL_TREE;
18685 goto done;
18688 /* Figure out in what scope the declaration is being placed. */
18689 scope = current_scope ();
18690 /* If that scope does not contain the scope in which the
18691 class was originally declared, the program is invalid. */
18692 if (scope && !is_ancestor (scope, nested_name_specifier))
18694 if (at_namespace_scope_p ())
18695 error_at (type_start_token->location,
18696 "declaration of %qD in namespace %qD which does not "
18697 "enclose %qD",
18698 type, scope, nested_name_specifier);
18699 else
18700 error_at (type_start_token->location,
18701 "declaration of %qD in %qD which does not enclose %qD",
18702 type, scope, nested_name_specifier);
18703 type = NULL_TREE;
18704 goto done;
18706 /* [dcl.meaning]
18708 A declarator-id shall not be qualified except for the
18709 definition of a ... nested class outside of its class
18710 ... [or] the definition or explicit instantiation of a
18711 class member of a namespace outside of its namespace. */
18712 if (scope == nested_name_specifier)
18714 permerror (nested_name_specifier_token_start->location,
18715 "extra qualification not allowed");
18716 nested_name_specifier = NULL_TREE;
18717 num_templates = 0;
18720 /* An explicit-specialization must be preceded by "template <>". If
18721 it is not, try to recover gracefully. */
18722 if (at_namespace_scope_p ()
18723 && parser->num_template_parameter_lists == 0
18724 && template_id_p)
18726 error_at (type_start_token->location,
18727 "an explicit specialization must be preceded by %<template <>%>");
18728 invalid_explicit_specialization_p = true;
18729 /* Take the same action that would have been taken by
18730 cp_parser_explicit_specialization. */
18731 ++parser->num_template_parameter_lists;
18732 begin_specialization ();
18734 /* There must be no "return" statements between this point and the
18735 end of this function; set "type "to the correct return value and
18736 use "goto done;" to return. */
18737 /* Make sure that the right number of template parameters were
18738 present. */
18739 if (!cp_parser_check_template_parameters (parser, num_templates,
18740 type_start_token->location,
18741 /*declarator=*/NULL))
18743 /* If something went wrong, there is no point in even trying to
18744 process the class-definition. */
18745 type = NULL_TREE;
18746 goto done;
18749 /* Look up the type. */
18750 if (template_id_p)
18752 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18753 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18754 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18756 error_at (type_start_token->location,
18757 "function template %qD redeclared as a class template", id);
18758 type = error_mark_node;
18760 else
18762 type = TREE_TYPE (id);
18763 type = maybe_process_partial_specialization (type);
18765 if (nested_name_specifier)
18766 pushed_scope = push_scope (nested_name_specifier);
18768 else if (nested_name_specifier)
18770 tree class_type;
18772 /* Given:
18774 template <typename T> struct S { struct T };
18775 template <typename T> struct S<T>::T { };
18777 we will get a TYPENAME_TYPE when processing the definition of
18778 `S::T'. We need to resolve it to the actual type before we
18779 try to define it. */
18780 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18782 class_type = resolve_typename_type (TREE_TYPE (type),
18783 /*only_current_p=*/false);
18784 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18785 type = TYPE_NAME (class_type);
18786 else
18788 cp_parser_error (parser, "could not resolve typename type");
18789 type = error_mark_node;
18793 if (maybe_process_partial_specialization (TREE_TYPE (type))
18794 == error_mark_node)
18796 type = NULL_TREE;
18797 goto done;
18800 class_type = current_class_type;
18801 /* Enter the scope indicated by the nested-name-specifier. */
18802 pushed_scope = push_scope (nested_name_specifier);
18803 /* Get the canonical version of this type. */
18804 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18805 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18806 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18808 type = push_template_decl (type);
18809 if (type == error_mark_node)
18811 type = NULL_TREE;
18812 goto done;
18816 type = TREE_TYPE (type);
18817 *nested_name_specifier_p = true;
18819 else /* The name is not a nested name. */
18821 /* If the class was unnamed, create a dummy name. */
18822 if (!id)
18823 id = make_anon_name ();
18824 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18825 parser->num_template_parameter_lists);
18828 /* Indicate whether this class was declared as a `class' or as a
18829 `struct'. */
18830 if (TREE_CODE (type) == RECORD_TYPE)
18831 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18832 cp_parser_check_class_key (class_key, type);
18834 /* If this type was already complete, and we see another definition,
18835 that's an error. */
18836 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18838 error_at (type_start_token->location, "redefinition of %q#T",
18839 type);
18840 error_at (type_start_token->location, "previous definition of %q+#T",
18841 type);
18842 type = NULL_TREE;
18843 goto done;
18845 else if (type == error_mark_node)
18846 type = NULL_TREE;
18848 if (type)
18850 /* Apply attributes now, before any use of the class as a template
18851 argument in its base list. */
18852 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18853 fixup_attribute_variants (type);
18856 /* We will have entered the scope containing the class; the names of
18857 base classes should be looked up in that context. For example:
18859 struct A { struct B {}; struct C; };
18860 struct A::C : B {};
18862 is valid. */
18864 /* Get the list of base-classes, if there is one. */
18865 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18866 bases = cp_parser_base_clause (parser);
18867 else
18868 bases = NULL_TREE;
18870 /* If we're really defining a class, process the base classes.
18871 If they're invalid, fail. */
18872 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
18873 && !xref_basetypes (type, bases))
18874 type = NULL_TREE;
18876 done:
18877 /* Leave the scope given by the nested-name-specifier. We will
18878 enter the class scope itself while processing the members. */
18879 if (pushed_scope)
18880 pop_scope (pushed_scope);
18882 if (invalid_explicit_specialization_p)
18884 end_specialization ();
18885 --parser->num_template_parameter_lists;
18888 if (type)
18889 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18890 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18891 CLASSTYPE_FINAL (type) = 1;
18892 out:
18893 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18894 return type;
18897 /* Parse a class-key.
18899 class-key:
18900 class
18901 struct
18902 union
18904 Returns the kind of class-key specified, or none_type to indicate
18905 error. */
18907 static enum tag_types
18908 cp_parser_class_key (cp_parser* parser)
18910 cp_token *token;
18911 enum tag_types tag_type;
18913 /* Look for the class-key. */
18914 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18915 if (!token)
18916 return none_type;
18918 /* Check to see if the TOKEN is a class-key. */
18919 tag_type = cp_parser_token_is_class_key (token);
18920 if (!tag_type)
18921 cp_parser_error (parser, "expected class-key");
18922 return tag_type;
18925 /* Parse an (optional) member-specification.
18927 member-specification:
18928 member-declaration member-specification [opt]
18929 access-specifier : member-specification [opt] */
18931 static void
18932 cp_parser_member_specification_opt (cp_parser* parser)
18934 while (true)
18936 cp_token *token;
18937 enum rid keyword;
18939 /* Peek at the next token. */
18940 token = cp_lexer_peek_token (parser->lexer);
18941 /* If it's a `}', or EOF then we've seen all the members. */
18942 if (token->type == CPP_CLOSE_BRACE
18943 || token->type == CPP_EOF
18944 || token->type == CPP_PRAGMA_EOL)
18945 break;
18947 /* See if this token is a keyword. */
18948 keyword = token->keyword;
18949 switch (keyword)
18951 case RID_PUBLIC:
18952 case RID_PROTECTED:
18953 case RID_PRIVATE:
18954 /* Consume the access-specifier. */
18955 cp_lexer_consume_token (parser->lexer);
18956 /* Remember which access-specifier is active. */
18957 current_access_specifier = token->u.value;
18958 /* Look for the `:'. */
18959 cp_parser_require (parser, CPP_COLON, RT_COLON);
18960 break;
18962 default:
18963 /* Accept #pragmas at class scope. */
18964 if (token->type == CPP_PRAGMA)
18966 cp_parser_pragma (parser, pragma_external);
18967 break;
18970 /* Otherwise, the next construction must be a
18971 member-declaration. */
18972 cp_parser_member_declaration (parser);
18977 /* Parse a member-declaration.
18979 member-declaration:
18980 decl-specifier-seq [opt] member-declarator-list [opt] ;
18981 function-definition ; [opt]
18982 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18983 using-declaration
18984 template-declaration
18985 alias-declaration
18987 member-declarator-list:
18988 member-declarator
18989 member-declarator-list , member-declarator
18991 member-declarator:
18992 declarator pure-specifier [opt]
18993 declarator constant-initializer [opt]
18994 identifier [opt] : constant-expression
18996 GNU Extensions:
18998 member-declaration:
18999 __extension__ member-declaration
19001 member-declarator:
19002 declarator attributes [opt] pure-specifier [opt]
19003 declarator attributes [opt] constant-initializer [opt]
19004 identifier [opt] attributes [opt] : constant-expression
19006 C++0x Extensions:
19008 member-declaration:
19009 static_assert-declaration */
19011 static void
19012 cp_parser_member_declaration (cp_parser* parser)
19014 cp_decl_specifier_seq decl_specifiers;
19015 tree prefix_attributes;
19016 tree decl;
19017 int declares_class_or_enum;
19018 bool friend_p;
19019 cp_token *token = NULL;
19020 cp_token *decl_spec_token_start = NULL;
19021 cp_token *initializer_token_start = NULL;
19022 int saved_pedantic;
19023 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19025 /* Check for the `__extension__' keyword. */
19026 if (cp_parser_extension_opt (parser, &saved_pedantic))
19028 /* Recurse. */
19029 cp_parser_member_declaration (parser);
19030 /* Restore the old value of the PEDANTIC flag. */
19031 pedantic = saved_pedantic;
19033 return;
19036 /* Check for a template-declaration. */
19037 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19039 /* An explicit specialization here is an error condition, and we
19040 expect the specialization handler to detect and report this. */
19041 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19042 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
19043 cp_parser_explicit_specialization (parser);
19044 else
19045 cp_parser_template_declaration (parser, /*member_p=*/true);
19047 return;
19050 /* Check for a using-declaration. */
19051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
19053 if (cxx_dialect < cxx0x)
19055 /* Parse the using-declaration. */
19056 cp_parser_using_declaration (parser,
19057 /*access_declaration_p=*/false);
19058 return;
19060 else
19062 tree decl;
19063 cp_parser_parse_tentatively (parser);
19064 decl = cp_parser_alias_declaration (parser);
19065 if (cp_parser_parse_definitely (parser))
19066 finish_member_declaration (decl);
19067 else
19068 cp_parser_using_declaration (parser,
19069 /*access_declaration_p=*/false);
19070 return;
19074 /* Check for @defs. */
19075 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
19077 tree ivar, member;
19078 tree ivar_chains = cp_parser_objc_defs_expression (parser);
19079 ivar = ivar_chains;
19080 while (ivar)
19082 member = ivar;
19083 ivar = TREE_CHAIN (member);
19084 TREE_CHAIN (member) = NULL_TREE;
19085 finish_member_declaration (member);
19087 return;
19090 /* If the next token is `static_assert' we have a static assertion. */
19091 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
19093 cp_parser_static_assert (parser, /*member_p=*/true);
19094 return;
19097 parser->colon_corrects_to_scope_p = false;
19099 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
19100 goto out;
19102 /* Parse the decl-specifier-seq. */
19103 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19104 cp_parser_decl_specifier_seq (parser,
19105 CP_PARSER_FLAGS_OPTIONAL,
19106 &decl_specifiers,
19107 &declares_class_or_enum);
19108 /* Check for an invalid type-name. */
19109 if (!decl_specifiers.any_type_specifiers_p
19110 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19111 goto out;
19112 /* If there is no declarator, then the decl-specifier-seq should
19113 specify a type. */
19114 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19116 /* If there was no decl-specifier-seq, and the next token is a
19117 `;', then we have something like:
19119 struct S { ; };
19121 [class.mem]
19123 Each member-declaration shall declare at least one member
19124 name of the class. */
19125 if (!decl_specifiers.any_specifiers_p)
19127 cp_token *token = cp_lexer_peek_token (parser->lexer);
19128 if (!in_system_header_at (token->location))
19129 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
19131 else
19133 tree type;
19135 /* See if this declaration is a friend. */
19136 friend_p = cp_parser_friend_p (&decl_specifiers);
19137 /* If there were decl-specifiers, check to see if there was
19138 a class-declaration. */
19139 type = check_tag_decl (&decl_specifiers,
19140 /*explicit_type_instantiation_p=*/false);
19141 /* Nested classes have already been added to the class, but
19142 a `friend' needs to be explicitly registered. */
19143 if (friend_p)
19145 /* If the `friend' keyword was present, the friend must
19146 be introduced with a class-key. */
19147 if (!declares_class_or_enum && cxx_dialect < cxx0x)
19148 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
19149 "in C++03 a class-key must be used "
19150 "when declaring a friend");
19151 /* In this case:
19153 template <typename T> struct A {
19154 friend struct A<T>::B;
19157 A<T>::B will be represented by a TYPENAME_TYPE, and
19158 therefore not recognized by check_tag_decl. */
19159 if (!type)
19161 type = decl_specifiers.type;
19162 if (type && TREE_CODE (type) == TYPE_DECL)
19163 type = TREE_TYPE (type);
19165 if (!type || !TYPE_P (type))
19166 error_at (decl_spec_token_start->location,
19167 "friend declaration does not name a class or "
19168 "function");
19169 else
19170 make_friend_class (current_class_type, type,
19171 /*complain=*/true);
19173 /* If there is no TYPE, an error message will already have
19174 been issued. */
19175 else if (!type || type == error_mark_node)
19177 /* An anonymous aggregate has to be handled specially; such
19178 a declaration really declares a data member (with a
19179 particular type), as opposed to a nested class. */
19180 else if (ANON_AGGR_TYPE_P (type))
19182 /* C++11 9.5/6. */
19183 if (decl_specifiers.storage_class != sc_none)
19184 error_at (decl_spec_token_start->location,
19185 "a storage class on an anonymous aggregate "
19186 "in class scope is not allowed");
19188 /* Remove constructors and such from TYPE, now that we
19189 know it is an anonymous aggregate. */
19190 fixup_anonymous_aggr (type);
19191 /* And make the corresponding data member. */
19192 decl = build_decl (decl_spec_token_start->location,
19193 FIELD_DECL, NULL_TREE, type);
19194 /* Add it to the class. */
19195 finish_member_declaration (decl);
19197 else
19198 cp_parser_check_access_in_redeclaration
19199 (TYPE_NAME (type),
19200 decl_spec_token_start->location);
19203 else
19205 bool assume_semicolon = false;
19207 /* Clear attributes from the decl_specifiers but keep them
19208 around as prefix attributes that apply them to the entity
19209 being declared. */
19210 prefix_attributes = decl_specifiers.attributes;
19211 decl_specifiers.attributes = NULL_TREE;
19213 /* See if these declarations will be friends. */
19214 friend_p = cp_parser_friend_p (&decl_specifiers);
19216 /* Keep going until we hit the `;' at the end of the
19217 declaration. */
19218 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19220 tree attributes = NULL_TREE;
19221 tree first_attribute;
19223 /* Peek at the next token. */
19224 token = cp_lexer_peek_token (parser->lexer);
19226 /* Check for a bitfield declaration. */
19227 if (token->type == CPP_COLON
19228 || (token->type == CPP_NAME
19229 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19230 == CPP_COLON))
19232 tree identifier;
19233 tree width;
19235 /* Get the name of the bitfield. Note that we cannot just
19236 check TOKEN here because it may have been invalidated by
19237 the call to cp_lexer_peek_nth_token above. */
19238 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19239 identifier = cp_parser_identifier (parser);
19240 else
19241 identifier = NULL_TREE;
19243 /* Consume the `:' token. */
19244 cp_lexer_consume_token (parser->lexer);
19245 /* Get the width of the bitfield. */
19246 width
19247 = cp_parser_constant_expression (parser,
19248 /*allow_non_constant=*/false,
19249 NULL);
19251 /* Look for attributes that apply to the bitfield. */
19252 attributes = cp_parser_attributes_opt (parser);
19253 /* Remember which attributes are prefix attributes and
19254 which are not. */
19255 first_attribute = attributes;
19256 /* Combine the attributes. */
19257 attributes = chainon (prefix_attributes, attributes);
19259 /* Create the bitfield declaration. */
19260 decl = grokbitfield (identifier
19261 ? make_id_declarator (NULL_TREE,
19262 identifier,
19263 sfk_none)
19264 : NULL,
19265 &decl_specifiers,
19266 width,
19267 attributes);
19269 else
19271 cp_declarator *declarator;
19272 tree initializer;
19273 tree asm_specification;
19274 int ctor_dtor_or_conv_p;
19276 /* Parse the declarator. */
19277 declarator
19278 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19279 &ctor_dtor_or_conv_p,
19280 /*parenthesized_p=*/NULL,
19281 /*member_p=*/true);
19283 /* If something went wrong parsing the declarator, make sure
19284 that we at least consume some tokens. */
19285 if (declarator == cp_error_declarator)
19287 /* Skip to the end of the statement. */
19288 cp_parser_skip_to_end_of_statement (parser);
19289 /* If the next token is not a semicolon, that is
19290 probably because we just skipped over the body of
19291 a function. So, we consume a semicolon if
19292 present, but do not issue an error message if it
19293 is not present. */
19294 if (cp_lexer_next_token_is (parser->lexer,
19295 CPP_SEMICOLON))
19296 cp_lexer_consume_token (parser->lexer);
19297 goto out;
19300 if (declares_class_or_enum & 2)
19301 cp_parser_check_for_definition_in_return_type
19302 (declarator, decl_specifiers.type,
19303 decl_specifiers.locations[ds_type_spec]);
19305 /* Look for an asm-specification. */
19306 asm_specification = cp_parser_asm_specification_opt (parser);
19307 /* Look for attributes that apply to the declaration. */
19308 attributes = cp_parser_attributes_opt (parser);
19309 /* Remember which attributes are prefix attributes and
19310 which are not. */
19311 first_attribute = attributes;
19312 /* Combine the attributes. */
19313 attributes = chainon (prefix_attributes, attributes);
19315 /* If it's an `=', then we have a constant-initializer or a
19316 pure-specifier. It is not correct to parse the
19317 initializer before registering the member declaration
19318 since the member declaration should be in scope while
19319 its initializer is processed. However, the rest of the
19320 front end does not yet provide an interface that allows
19321 us to handle this correctly. */
19322 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19324 /* In [class.mem]:
19326 A pure-specifier shall be used only in the declaration of
19327 a virtual function.
19329 A member-declarator can contain a constant-initializer
19330 only if it declares a static member of integral or
19331 enumeration type.
19333 Therefore, if the DECLARATOR is for a function, we look
19334 for a pure-specifier; otherwise, we look for a
19335 constant-initializer. When we call `grokfield', it will
19336 perform more stringent semantics checks. */
19337 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19338 if (function_declarator_p (declarator)
19339 || (decl_specifiers.type
19340 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19341 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19342 == FUNCTION_TYPE)))
19343 initializer = cp_parser_pure_specifier (parser);
19344 else if (decl_specifiers.storage_class != sc_static)
19345 initializer = cp_parser_save_nsdmi (parser);
19346 else if (cxx_dialect >= cxx0x)
19348 bool nonconst;
19349 /* Don't require a constant rvalue in C++11, since we
19350 might want a reference constant. We'll enforce
19351 constancy later. */
19352 cp_lexer_consume_token (parser->lexer);
19353 /* Parse the initializer. */
19354 initializer = cp_parser_initializer_clause (parser,
19355 &nonconst);
19357 else
19358 /* Parse the initializer. */
19359 initializer = cp_parser_constant_initializer (parser);
19361 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19362 && !function_declarator_p (declarator))
19364 bool x;
19365 if (decl_specifiers.storage_class != sc_static)
19366 initializer = cp_parser_save_nsdmi (parser);
19367 else
19368 initializer = cp_parser_initializer (parser, &x, &x);
19370 /* Otherwise, there is no initializer. */
19371 else
19372 initializer = NULL_TREE;
19374 /* See if we are probably looking at a function
19375 definition. We are certainly not looking at a
19376 member-declarator. Calling `grokfield' has
19377 side-effects, so we must not do it unless we are sure
19378 that we are looking at a member-declarator. */
19379 if (cp_parser_token_starts_function_definition_p
19380 (cp_lexer_peek_token (parser->lexer)))
19382 /* The grammar does not allow a pure-specifier to be
19383 used when a member function is defined. (It is
19384 possible that this fact is an oversight in the
19385 standard, since a pure function may be defined
19386 outside of the class-specifier. */
19387 if (initializer && initializer_token_start)
19388 error_at (initializer_token_start->location,
19389 "pure-specifier on function-definition");
19390 decl = cp_parser_save_member_function_body (parser,
19391 &decl_specifiers,
19392 declarator,
19393 attributes);
19394 /* If the member was not a friend, declare it here. */
19395 if (!friend_p)
19396 finish_member_declaration (decl);
19397 /* Peek at the next token. */
19398 token = cp_lexer_peek_token (parser->lexer);
19399 /* If the next token is a semicolon, consume it. */
19400 if (token->type == CPP_SEMICOLON)
19401 cp_lexer_consume_token (parser->lexer);
19402 goto out;
19404 else
19405 if (declarator->kind == cdk_function)
19406 declarator->id_loc = token->location;
19407 /* Create the declaration. */
19408 decl = grokfield (declarator, &decl_specifiers,
19409 initializer, /*init_const_expr_p=*/true,
19410 asm_specification,
19411 attributes);
19414 /* Reset PREFIX_ATTRIBUTES. */
19415 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19416 attributes = TREE_CHAIN (attributes);
19417 if (attributes)
19418 TREE_CHAIN (attributes) = NULL_TREE;
19420 /* If there is any qualification still in effect, clear it
19421 now; we will be starting fresh with the next declarator. */
19422 parser->scope = NULL_TREE;
19423 parser->qualifying_scope = NULL_TREE;
19424 parser->object_scope = NULL_TREE;
19425 /* If it's a `,', then there are more declarators. */
19426 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19428 cp_lexer_consume_token (parser->lexer);
19429 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19431 cp_token *token = cp_lexer_previous_token (parser->lexer);
19432 error_at (token->location,
19433 "stray %<,%> at end of member declaration");
19436 /* If the next token isn't a `;', then we have a parse error. */
19437 else if (cp_lexer_next_token_is_not (parser->lexer,
19438 CPP_SEMICOLON))
19440 /* The next token might be a ways away from where the
19441 actual semicolon is missing. Find the previous token
19442 and use that for our error position. */
19443 cp_token *token = cp_lexer_previous_token (parser->lexer);
19444 error_at (token->location,
19445 "expected %<;%> at end of member declaration");
19447 /* Assume that the user meant to provide a semicolon. If
19448 we were to cp_parser_skip_to_end_of_statement, we might
19449 skip to a semicolon inside a member function definition
19450 and issue nonsensical error messages. */
19451 assume_semicolon = true;
19454 if (decl)
19456 /* Add DECL to the list of members. */
19457 if (!friend_p)
19458 finish_member_declaration (decl);
19460 if (TREE_CODE (decl) == FUNCTION_DECL)
19461 cp_parser_save_default_args (parser, decl);
19462 else if (TREE_CODE (decl) == FIELD_DECL
19463 && !DECL_C_BIT_FIELD (decl)
19464 && DECL_INITIAL (decl))
19465 /* Add DECL to the queue of NSDMI to be parsed later. */
19466 vec_safe_push (unparsed_nsdmis, decl);
19469 if (assume_semicolon)
19470 goto out;
19474 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19475 out:
19476 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19479 /* Parse a pure-specifier.
19481 pure-specifier:
19484 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19485 Otherwise, ERROR_MARK_NODE is returned. */
19487 static tree
19488 cp_parser_pure_specifier (cp_parser* parser)
19490 cp_token *token;
19492 /* Look for the `=' token. */
19493 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19494 return error_mark_node;
19495 /* Look for the `0' token. */
19496 token = cp_lexer_peek_token (parser->lexer);
19498 if (token->type == CPP_EOF
19499 || token->type == CPP_PRAGMA_EOL)
19500 return error_mark_node;
19502 cp_lexer_consume_token (parser->lexer);
19504 /* Accept = default or = delete in c++0x mode. */
19505 if (token->keyword == RID_DEFAULT
19506 || token->keyword == RID_DELETE)
19508 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19509 return token->u.value;
19512 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19513 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19515 cp_parser_error (parser,
19516 "invalid pure specifier (only %<= 0%> is allowed)");
19517 cp_parser_skip_to_end_of_statement (parser);
19518 return error_mark_node;
19520 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19522 error_at (token->location, "templates may not be %<virtual%>");
19523 return error_mark_node;
19526 return integer_zero_node;
19529 /* Parse a constant-initializer.
19531 constant-initializer:
19532 = constant-expression
19534 Returns a representation of the constant-expression. */
19536 static tree
19537 cp_parser_constant_initializer (cp_parser* parser)
19539 /* Look for the `=' token. */
19540 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19541 return error_mark_node;
19543 /* It is invalid to write:
19545 struct S { static const int i = { 7 }; };
19548 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19550 cp_parser_error (parser,
19551 "a brace-enclosed initializer is not allowed here");
19552 /* Consume the opening brace. */
19553 cp_lexer_consume_token (parser->lexer);
19554 /* Skip the initializer. */
19555 cp_parser_skip_to_closing_brace (parser);
19556 /* Look for the trailing `}'. */
19557 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19559 return error_mark_node;
19562 return cp_parser_constant_expression (parser,
19563 /*allow_non_constant=*/false,
19564 NULL);
19567 /* Derived classes [gram.class.derived] */
19569 /* Parse a base-clause.
19571 base-clause:
19572 : base-specifier-list
19574 base-specifier-list:
19575 base-specifier ... [opt]
19576 base-specifier-list , base-specifier ... [opt]
19578 Returns a TREE_LIST representing the base-classes, in the order in
19579 which they were declared. The representation of each node is as
19580 described by cp_parser_base_specifier.
19582 In the case that no bases are specified, this function will return
19583 NULL_TREE, not ERROR_MARK_NODE. */
19585 static tree
19586 cp_parser_base_clause (cp_parser* parser)
19588 tree bases = NULL_TREE;
19590 /* Look for the `:' that begins the list. */
19591 cp_parser_require (parser, CPP_COLON, RT_COLON);
19593 /* Scan the base-specifier-list. */
19594 while (true)
19596 cp_token *token;
19597 tree base;
19598 bool pack_expansion_p = false;
19600 /* Look for the base-specifier. */
19601 base = cp_parser_base_specifier (parser);
19602 /* Look for the (optional) ellipsis. */
19603 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19605 /* Consume the `...'. */
19606 cp_lexer_consume_token (parser->lexer);
19608 pack_expansion_p = true;
19611 /* Add BASE to the front of the list. */
19612 if (base && base != error_mark_node)
19614 if (pack_expansion_p)
19615 /* Make this a pack expansion type. */
19616 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19618 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19620 TREE_CHAIN (base) = bases;
19621 bases = base;
19624 /* Peek at the next token. */
19625 token = cp_lexer_peek_token (parser->lexer);
19626 /* If it's not a comma, then the list is complete. */
19627 if (token->type != CPP_COMMA)
19628 break;
19629 /* Consume the `,'. */
19630 cp_lexer_consume_token (parser->lexer);
19633 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19634 base class had a qualified name. However, the next name that
19635 appears is certainly not qualified. */
19636 parser->scope = NULL_TREE;
19637 parser->qualifying_scope = NULL_TREE;
19638 parser->object_scope = NULL_TREE;
19640 return nreverse (bases);
19643 /* Parse a base-specifier.
19645 base-specifier:
19646 :: [opt] nested-name-specifier [opt] class-name
19647 virtual access-specifier [opt] :: [opt] nested-name-specifier
19648 [opt] class-name
19649 access-specifier virtual [opt] :: [opt] nested-name-specifier
19650 [opt] class-name
19652 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19653 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19654 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19655 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19657 static tree
19658 cp_parser_base_specifier (cp_parser* parser)
19660 cp_token *token;
19661 bool done = false;
19662 bool virtual_p = false;
19663 bool duplicate_virtual_error_issued_p = false;
19664 bool duplicate_access_error_issued_p = false;
19665 bool class_scope_p, template_p;
19666 tree access = access_default_node;
19667 tree type;
19669 /* Process the optional `virtual' and `access-specifier'. */
19670 while (!done)
19672 /* Peek at the next token. */
19673 token = cp_lexer_peek_token (parser->lexer);
19674 /* Process `virtual'. */
19675 switch (token->keyword)
19677 case RID_VIRTUAL:
19678 /* If `virtual' appears more than once, issue an error. */
19679 if (virtual_p && !duplicate_virtual_error_issued_p)
19681 cp_parser_error (parser,
19682 "%<virtual%> specified more than once in base-specified");
19683 duplicate_virtual_error_issued_p = true;
19686 virtual_p = true;
19688 /* Consume the `virtual' token. */
19689 cp_lexer_consume_token (parser->lexer);
19691 break;
19693 case RID_PUBLIC:
19694 case RID_PROTECTED:
19695 case RID_PRIVATE:
19696 /* If more than one access specifier appears, issue an
19697 error. */
19698 if (access != access_default_node
19699 && !duplicate_access_error_issued_p)
19701 cp_parser_error (parser,
19702 "more than one access specifier in base-specified");
19703 duplicate_access_error_issued_p = true;
19706 access = ridpointers[(int) token->keyword];
19708 /* Consume the access-specifier. */
19709 cp_lexer_consume_token (parser->lexer);
19711 break;
19713 default:
19714 done = true;
19715 break;
19718 /* It is not uncommon to see programs mechanically, erroneously, use
19719 the 'typename' keyword to denote (dependent) qualified types
19720 as base classes. */
19721 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19723 token = cp_lexer_peek_token (parser->lexer);
19724 if (!processing_template_decl)
19725 error_at (token->location,
19726 "keyword %<typename%> not allowed outside of templates");
19727 else
19728 error_at (token->location,
19729 "keyword %<typename%> not allowed in this context "
19730 "(the base class is implicitly a type)");
19731 cp_lexer_consume_token (parser->lexer);
19734 /* Look for the optional `::' operator. */
19735 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19736 /* Look for the nested-name-specifier. The simplest way to
19737 implement:
19739 [temp.res]
19741 The keyword `typename' is not permitted in a base-specifier or
19742 mem-initializer; in these contexts a qualified name that
19743 depends on a template-parameter is implicitly assumed to be a
19744 type name.
19746 is to pretend that we have seen the `typename' keyword at this
19747 point. */
19748 cp_parser_nested_name_specifier_opt (parser,
19749 /*typename_keyword_p=*/true,
19750 /*check_dependency_p=*/true,
19751 typename_type,
19752 /*is_declaration=*/true);
19753 /* If the base class is given by a qualified name, assume that names
19754 we see are type names or templates, as appropriate. */
19755 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19756 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19758 if (!parser->scope
19759 && cp_lexer_next_token_is_decltype (parser->lexer))
19760 /* DR 950 allows decltype as a base-specifier. */
19761 type = cp_parser_decltype (parser);
19762 else
19764 /* Otherwise, look for the class-name. */
19765 type = cp_parser_class_name (parser,
19766 class_scope_p,
19767 template_p,
19768 typename_type,
19769 /*check_dependency_p=*/true,
19770 /*class_head_p=*/false,
19771 /*is_declaration=*/true);
19772 type = TREE_TYPE (type);
19775 if (type == error_mark_node)
19776 return error_mark_node;
19778 return finish_base_specifier (type, access, virtual_p);
19781 /* Exception handling [gram.exception] */
19783 /* Parse an (optional) noexcept-specification.
19785 noexcept-specification:
19786 noexcept ( constant-expression ) [opt]
19788 If no noexcept-specification is present, returns NULL_TREE.
19789 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19790 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19791 there are no parentheses. CONSUMED_EXPR will be set accordingly.
19792 Otherwise, returns a noexcept specification unless RETURN_COND is true,
19793 in which case a boolean condition is returned instead. */
19795 static tree
19796 cp_parser_noexcept_specification_opt (cp_parser* parser,
19797 bool require_constexpr,
19798 bool* consumed_expr,
19799 bool return_cond)
19801 cp_token *token;
19802 const char *saved_message;
19804 /* Peek at the next token. */
19805 token = cp_lexer_peek_token (parser->lexer);
19807 /* Is it a noexcept-specification? */
19808 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19810 tree expr;
19811 cp_lexer_consume_token (parser->lexer);
19813 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19815 cp_lexer_consume_token (parser->lexer);
19817 if (require_constexpr)
19819 /* Types may not be defined in an exception-specification. */
19820 saved_message = parser->type_definition_forbidden_message;
19821 parser->type_definition_forbidden_message
19822 = G_("types may not be defined in an exception-specification");
19824 expr = cp_parser_constant_expression (parser, false, NULL);
19826 /* Restore the saved message. */
19827 parser->type_definition_forbidden_message = saved_message;
19829 else
19831 expr = cp_parser_expression (parser, false, NULL);
19832 *consumed_expr = true;
19835 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19837 else
19839 expr = boolean_true_node;
19840 if (!require_constexpr)
19841 *consumed_expr = false;
19844 /* We cannot build a noexcept-spec right away because this will check
19845 that expr is a constexpr. */
19846 if (!return_cond)
19847 return build_noexcept_spec (expr, tf_warning_or_error);
19848 else
19849 return expr;
19851 else
19852 return NULL_TREE;
19855 /* Parse an (optional) exception-specification.
19857 exception-specification:
19858 throw ( type-id-list [opt] )
19860 Returns a TREE_LIST representing the exception-specification. The
19861 TREE_VALUE of each node is a type. */
19863 static tree
19864 cp_parser_exception_specification_opt (cp_parser* parser)
19866 cp_token *token;
19867 tree type_id_list;
19868 const char *saved_message;
19870 /* Peek at the next token. */
19871 token = cp_lexer_peek_token (parser->lexer);
19873 /* Is it a noexcept-specification? */
19874 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19875 false);
19876 if (type_id_list != NULL_TREE)
19877 return type_id_list;
19879 /* If it's not `throw', then there's no exception-specification. */
19880 if (!cp_parser_is_keyword (token, RID_THROW))
19881 return NULL_TREE;
19883 #if 0
19884 /* Enable this once a lot of code has transitioned to noexcept? */
19885 if (cxx_dialect >= cxx0x && !in_system_header)
19886 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19887 "deprecated in C++0x; use %<noexcept%> instead");
19888 #endif
19890 /* Consume the `throw'. */
19891 cp_lexer_consume_token (parser->lexer);
19893 /* Look for the `('. */
19894 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19896 /* Peek at the next token. */
19897 token = cp_lexer_peek_token (parser->lexer);
19898 /* If it's not a `)', then there is a type-id-list. */
19899 if (token->type != CPP_CLOSE_PAREN)
19901 /* Types may not be defined in an exception-specification. */
19902 saved_message = parser->type_definition_forbidden_message;
19903 parser->type_definition_forbidden_message
19904 = G_("types may not be defined in an exception-specification");
19905 /* Parse the type-id-list. */
19906 type_id_list = cp_parser_type_id_list (parser);
19907 /* Restore the saved message. */
19908 parser->type_definition_forbidden_message = saved_message;
19910 else
19911 type_id_list = empty_except_spec;
19913 /* Look for the `)'. */
19914 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19916 return type_id_list;
19919 /* Parse an (optional) type-id-list.
19921 type-id-list:
19922 type-id ... [opt]
19923 type-id-list , type-id ... [opt]
19925 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19926 in the order that the types were presented. */
19928 static tree
19929 cp_parser_type_id_list (cp_parser* parser)
19931 tree types = NULL_TREE;
19933 while (true)
19935 cp_token *token;
19936 tree type;
19938 /* Get the next type-id. */
19939 type = cp_parser_type_id (parser);
19940 /* Parse the optional ellipsis. */
19941 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19943 /* Consume the `...'. */
19944 cp_lexer_consume_token (parser->lexer);
19946 /* Turn the type into a pack expansion expression. */
19947 type = make_pack_expansion (type);
19949 /* Add it to the list. */
19950 types = add_exception_specifier (types, type, /*complain=*/1);
19951 /* Peek at the next token. */
19952 token = cp_lexer_peek_token (parser->lexer);
19953 /* If it is not a `,', we are done. */
19954 if (token->type != CPP_COMMA)
19955 break;
19956 /* Consume the `,'. */
19957 cp_lexer_consume_token (parser->lexer);
19960 return nreverse (types);
19963 /* Parse a try-block.
19965 try-block:
19966 try compound-statement handler-seq */
19968 static tree
19969 cp_parser_try_block (cp_parser* parser)
19971 tree try_block;
19973 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19974 try_block = begin_try_block ();
19975 cp_parser_compound_statement (parser, NULL, true, false);
19976 finish_try_block (try_block);
19977 cp_parser_handler_seq (parser);
19978 finish_handler_sequence (try_block);
19980 return try_block;
19983 /* Parse a function-try-block.
19985 function-try-block:
19986 try ctor-initializer [opt] function-body handler-seq */
19988 static bool
19989 cp_parser_function_try_block (cp_parser* parser)
19991 tree compound_stmt;
19992 tree try_block;
19993 bool ctor_initializer_p;
19995 /* Look for the `try' keyword. */
19996 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19997 return false;
19998 /* Let the rest of the front end know where we are. */
19999 try_block = begin_function_try_block (&compound_stmt);
20000 /* Parse the function-body. */
20001 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
20002 (parser, /*in_function_try_block=*/true);
20003 /* We're done with the `try' part. */
20004 finish_function_try_block (try_block);
20005 /* Parse the handlers. */
20006 cp_parser_handler_seq (parser);
20007 /* We're done with the handlers. */
20008 finish_function_handler_sequence (try_block, compound_stmt);
20010 return ctor_initializer_p;
20013 /* Parse a handler-seq.
20015 handler-seq:
20016 handler handler-seq [opt] */
20018 static void
20019 cp_parser_handler_seq (cp_parser* parser)
20021 while (true)
20023 cp_token *token;
20025 /* Parse the handler. */
20026 cp_parser_handler (parser);
20027 /* Peek at the next token. */
20028 token = cp_lexer_peek_token (parser->lexer);
20029 /* If it's not `catch' then there are no more handlers. */
20030 if (!cp_parser_is_keyword (token, RID_CATCH))
20031 break;
20035 /* Parse a handler.
20037 handler:
20038 catch ( exception-declaration ) compound-statement */
20040 static void
20041 cp_parser_handler (cp_parser* parser)
20043 tree handler;
20044 tree declaration;
20046 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
20047 handler = begin_handler ();
20048 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20049 declaration = cp_parser_exception_declaration (parser);
20050 finish_handler_parms (declaration, handler);
20051 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20052 cp_parser_compound_statement (parser, NULL, false, false);
20053 finish_handler (handler);
20056 /* Parse an exception-declaration.
20058 exception-declaration:
20059 type-specifier-seq declarator
20060 type-specifier-seq abstract-declarator
20061 type-specifier-seq
20064 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20065 ellipsis variant is used. */
20067 static tree
20068 cp_parser_exception_declaration (cp_parser* parser)
20070 cp_decl_specifier_seq type_specifiers;
20071 cp_declarator *declarator;
20072 const char *saved_message;
20074 /* If it's an ellipsis, it's easy to handle. */
20075 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20077 /* Consume the `...' token. */
20078 cp_lexer_consume_token (parser->lexer);
20079 return NULL_TREE;
20082 /* Types may not be defined in exception-declarations. */
20083 saved_message = parser->type_definition_forbidden_message;
20084 parser->type_definition_forbidden_message
20085 = G_("types may not be defined in exception-declarations");
20087 /* Parse the type-specifier-seq. */
20088 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
20089 /*is_trailing_return=*/false,
20090 &type_specifiers);
20091 /* If it's a `)', then there is no declarator. */
20092 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
20093 declarator = NULL;
20094 else
20095 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
20096 /*ctor_dtor_or_conv_p=*/NULL,
20097 /*parenthesized_p=*/NULL,
20098 /*member_p=*/false);
20100 /* Restore the saved message. */
20101 parser->type_definition_forbidden_message = saved_message;
20103 if (!type_specifiers.any_specifiers_p)
20104 return error_mark_node;
20106 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
20109 /* Parse a throw-expression.
20111 throw-expression:
20112 throw assignment-expression [opt]
20114 Returns a THROW_EXPR representing the throw-expression. */
20116 static tree
20117 cp_parser_throw_expression (cp_parser* parser)
20119 tree expression;
20120 cp_token* token;
20122 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
20123 token = cp_lexer_peek_token (parser->lexer);
20124 /* Figure out whether or not there is an assignment-expression
20125 following the "throw" keyword. */
20126 if (token->type == CPP_COMMA
20127 || token->type == CPP_SEMICOLON
20128 || token->type == CPP_CLOSE_PAREN
20129 || token->type == CPP_CLOSE_SQUARE
20130 || token->type == CPP_CLOSE_BRACE
20131 || token->type == CPP_COLON)
20132 expression = NULL_TREE;
20133 else
20134 expression = cp_parser_assignment_expression (parser,
20135 /*cast_p=*/false, NULL);
20137 return build_throw (expression);
20140 /* GNU Extensions */
20142 /* Parse an (optional) asm-specification.
20144 asm-specification:
20145 asm ( string-literal )
20147 If the asm-specification is present, returns a STRING_CST
20148 corresponding to the string-literal. Otherwise, returns
20149 NULL_TREE. */
20151 static tree
20152 cp_parser_asm_specification_opt (cp_parser* parser)
20154 cp_token *token;
20155 tree asm_specification;
20157 /* Peek at the next token. */
20158 token = cp_lexer_peek_token (parser->lexer);
20159 /* If the next token isn't the `asm' keyword, then there's no
20160 asm-specification. */
20161 if (!cp_parser_is_keyword (token, RID_ASM))
20162 return NULL_TREE;
20164 /* Consume the `asm' token. */
20165 cp_lexer_consume_token (parser->lexer);
20166 /* Look for the `('. */
20167 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20169 /* Look for the string-literal. */
20170 asm_specification = cp_parser_string_literal (parser, false, false);
20172 /* Look for the `)'. */
20173 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20175 return asm_specification;
20178 /* Parse an asm-operand-list.
20180 asm-operand-list:
20181 asm-operand
20182 asm-operand-list , asm-operand
20184 asm-operand:
20185 string-literal ( expression )
20186 [ string-literal ] string-literal ( expression )
20188 Returns a TREE_LIST representing the operands. The TREE_VALUE of
20189 each node is the expression. The TREE_PURPOSE is itself a
20190 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
20191 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
20192 is a STRING_CST for the string literal before the parenthesis. Returns
20193 ERROR_MARK_NODE if any of the operands are invalid. */
20195 static tree
20196 cp_parser_asm_operand_list (cp_parser* parser)
20198 tree asm_operands = NULL_TREE;
20199 bool invalid_operands = false;
20201 while (true)
20203 tree string_literal;
20204 tree expression;
20205 tree name;
20207 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20209 /* Consume the `[' token. */
20210 cp_lexer_consume_token (parser->lexer);
20211 /* Read the operand name. */
20212 name = cp_parser_identifier (parser);
20213 if (name != error_mark_node)
20214 name = build_string (IDENTIFIER_LENGTH (name),
20215 IDENTIFIER_POINTER (name));
20216 /* Look for the closing `]'. */
20217 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20219 else
20220 name = NULL_TREE;
20221 /* Look for the string-literal. */
20222 string_literal = cp_parser_string_literal (parser, false, false);
20224 /* Look for the `('. */
20225 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20226 /* Parse the expression. */
20227 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20228 /* Look for the `)'. */
20229 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20231 if (name == error_mark_node
20232 || string_literal == error_mark_node
20233 || expression == error_mark_node)
20234 invalid_operands = true;
20236 /* Add this operand to the list. */
20237 asm_operands = tree_cons (build_tree_list (name, string_literal),
20238 expression,
20239 asm_operands);
20240 /* If the next token is not a `,', there are no more
20241 operands. */
20242 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20243 break;
20244 /* Consume the `,'. */
20245 cp_lexer_consume_token (parser->lexer);
20248 return invalid_operands ? error_mark_node : nreverse (asm_operands);
20251 /* Parse an asm-clobber-list.
20253 asm-clobber-list:
20254 string-literal
20255 asm-clobber-list , string-literal
20257 Returns a TREE_LIST, indicating the clobbers in the order that they
20258 appeared. The TREE_VALUE of each node is a STRING_CST. */
20260 static tree
20261 cp_parser_asm_clobber_list (cp_parser* parser)
20263 tree clobbers = NULL_TREE;
20265 while (true)
20267 tree string_literal;
20269 /* Look for the string literal. */
20270 string_literal = cp_parser_string_literal (parser, false, false);
20271 /* Add it to the list. */
20272 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20273 /* If the next token is not a `,', then the list is
20274 complete. */
20275 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20276 break;
20277 /* Consume the `,' token. */
20278 cp_lexer_consume_token (parser->lexer);
20281 return clobbers;
20284 /* Parse an asm-label-list.
20286 asm-label-list:
20287 identifier
20288 asm-label-list , identifier
20290 Returns a TREE_LIST, indicating the labels in the order that they
20291 appeared. The TREE_VALUE of each node is a label. */
20293 static tree
20294 cp_parser_asm_label_list (cp_parser* parser)
20296 tree labels = NULL_TREE;
20298 while (true)
20300 tree identifier, label, name;
20302 /* Look for the identifier. */
20303 identifier = cp_parser_identifier (parser);
20304 if (!error_operand_p (identifier))
20306 label = lookup_label (identifier);
20307 if (TREE_CODE (label) == LABEL_DECL)
20309 TREE_USED (label) = 1;
20310 check_goto (label);
20311 name = build_string (IDENTIFIER_LENGTH (identifier),
20312 IDENTIFIER_POINTER (identifier));
20313 labels = tree_cons (name, label, labels);
20316 /* If the next token is not a `,', then the list is
20317 complete. */
20318 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20319 break;
20320 /* Consume the `,' token. */
20321 cp_lexer_consume_token (parser->lexer);
20324 return nreverse (labels);
20327 /* Return TRUE iff the next tokens in the stream are possibly the
20328 beginning of a GNU extension attribute. */
20330 static bool
20331 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
20333 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
20336 /* Return TRUE iff the next tokens in the stream are possibly the
20337 beginning of a standard C++-11 attribute specifier. */
20339 static bool
20340 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
20342 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
20345 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20346 beginning of a standard C++-11 attribute specifier. */
20348 static bool
20349 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
20351 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20353 return (cxx_dialect >= cxx0x
20354 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
20355 || (token->type == CPP_OPEN_SQUARE
20356 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
20357 && token->type == CPP_OPEN_SQUARE)));
20360 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20361 beginning of a GNU extension attribute. */
20363 static bool
20364 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
20366 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20368 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
20371 /* Return true iff the next tokens can be the beginning of either a
20372 GNU attribute list, or a standard C++11 attribute sequence. */
20374 static bool
20375 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
20377 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
20378 || cp_next_tokens_can_be_std_attribute_p (parser));
20381 /* Return true iff the next Nth tokens can be the beginning of either
20382 a GNU attribute list, or a standard C++11 attribute sequence. */
20384 static bool
20385 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
20387 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
20388 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
20391 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
20392 of GNU attributes, or return NULL. */
20394 static tree
20395 cp_parser_attributes_opt (cp_parser *parser)
20397 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
20398 return cp_parser_gnu_attributes_opt (parser);
20399 return cp_parser_std_attribute_spec_seq (parser);
20402 /* Parse an (optional) series of attributes.
20404 attributes:
20405 attributes attribute
20407 attribute:
20408 __attribute__ (( attribute-list [opt] ))
20410 The return value is as for cp_parser_gnu_attribute_list. */
20412 static tree
20413 cp_parser_gnu_attributes_opt (cp_parser* parser)
20415 tree attributes = NULL_TREE;
20417 while (true)
20419 cp_token *token;
20420 tree attribute_list;
20421 bool ok = true;
20423 /* Peek at the next token. */
20424 token = cp_lexer_peek_token (parser->lexer);
20425 /* If it's not `__attribute__', then we're done. */
20426 if (token->keyword != RID_ATTRIBUTE)
20427 break;
20429 /* Consume the `__attribute__' keyword. */
20430 cp_lexer_consume_token (parser->lexer);
20431 /* Look for the two `(' tokens. */
20432 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20433 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20435 /* Peek at the next token. */
20436 token = cp_lexer_peek_token (parser->lexer);
20437 if (token->type != CPP_CLOSE_PAREN)
20438 /* Parse the attribute-list. */
20439 attribute_list = cp_parser_gnu_attribute_list (parser);
20440 else
20441 /* If the next token is a `)', then there is no attribute
20442 list. */
20443 attribute_list = NULL;
20445 /* Look for the two `)' tokens. */
20446 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20447 ok = false;
20448 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20449 ok = false;
20450 if (!ok)
20451 cp_parser_skip_to_end_of_statement (parser);
20453 /* Add these new attributes to the list. */
20454 attributes = chainon (attributes, attribute_list);
20457 return attributes;
20460 /* Parse a GNU attribute-list.
20462 attribute-list:
20463 attribute
20464 attribute-list , attribute
20466 attribute:
20467 identifier
20468 identifier ( identifier )
20469 identifier ( identifier , expression-list )
20470 identifier ( expression-list )
20472 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20473 to an attribute. The TREE_PURPOSE of each node is the identifier
20474 indicating which attribute is in use. The TREE_VALUE represents
20475 the arguments, if any. */
20477 static tree
20478 cp_parser_gnu_attribute_list (cp_parser* parser)
20480 tree attribute_list = NULL_TREE;
20481 bool save_translate_strings_p = parser->translate_strings_p;
20483 parser->translate_strings_p = false;
20484 while (true)
20486 cp_token *token;
20487 tree identifier;
20488 tree attribute;
20490 /* Look for the identifier. We also allow keywords here; for
20491 example `__attribute__ ((const))' is legal. */
20492 token = cp_lexer_peek_token (parser->lexer);
20493 if (token->type == CPP_NAME
20494 || token->type == CPP_KEYWORD)
20496 tree arguments = NULL_TREE;
20498 /* Consume the token. */
20499 token = cp_lexer_consume_token (parser->lexer);
20501 /* Save away the identifier that indicates which attribute
20502 this is. */
20503 identifier = (token->type == CPP_KEYWORD)
20504 /* For keywords, use the canonical spelling, not the
20505 parsed identifier. */
20506 ? ridpointers[(int) token->keyword]
20507 : token->u.value;
20509 attribute = build_tree_list (identifier, NULL_TREE);
20511 /* Peek at the next token. */
20512 token = cp_lexer_peek_token (parser->lexer);
20513 /* If it's an `(', then parse the attribute arguments. */
20514 if (token->type == CPP_OPEN_PAREN)
20516 vec<tree, va_gc> *vec;
20517 int attr_flag = (attribute_takes_identifier_p (identifier)
20518 ? id_attr : normal_attr);
20519 vec = cp_parser_parenthesized_expression_list
20520 (parser, attr_flag, /*cast_p=*/false,
20521 /*allow_expansion_p=*/false,
20522 /*non_constant_p=*/NULL);
20523 if (vec == NULL)
20524 arguments = error_mark_node;
20525 else
20527 arguments = build_tree_list_vec (vec);
20528 release_tree_vector (vec);
20530 /* Save the arguments away. */
20531 TREE_VALUE (attribute) = arguments;
20534 if (arguments != error_mark_node)
20536 /* Add this attribute to the list. */
20537 TREE_CHAIN (attribute) = attribute_list;
20538 attribute_list = attribute;
20541 token = cp_lexer_peek_token (parser->lexer);
20543 /* Now, look for more attributes. If the next token isn't a
20544 `,', we're done. */
20545 if (token->type != CPP_COMMA)
20546 break;
20548 /* Consume the comma and keep going. */
20549 cp_lexer_consume_token (parser->lexer);
20551 parser->translate_strings_p = save_translate_strings_p;
20553 /* We built up the list in reverse order. */
20554 return nreverse (attribute_list);
20557 /* Parse a standard C++11 attribute.
20559 The returned representation is a TREE_LIST which TREE_PURPOSE is
20560 the scoped name of the attribute, and the TREE_VALUE is its
20561 arguments list.
20563 Note that the scoped name of the attribute is itself a TREE_LIST
20564 which TREE_PURPOSE is the namespace of the attribute, and
20565 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
20566 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
20567 and which TREE_PURPOSE is directly the attribute name.
20569 Clients of the attribute code should use get_attribute_namespace
20570 and get_attribute_name to get the actual namespace and name of
20571 attributes, regardless of their being GNU or C++11 attributes.
20573 attribute:
20574 attribute-token attribute-argument-clause [opt]
20576 attribute-token:
20577 identifier
20578 attribute-scoped-token
20580 attribute-scoped-token:
20581 attribute-namespace :: identifier
20583 attribute-namespace:
20584 identifier
20586 attribute-argument-clause:
20587 ( balanced-token-seq )
20589 balanced-token-seq:
20590 balanced-token [opt]
20591 balanced-token-seq balanced-token
20593 balanced-token:
20594 ( balanced-token-seq )
20595 [ balanced-token-seq ]
20596 { balanced-token-seq }. */
20598 static tree
20599 cp_parser_std_attribute (cp_parser *parser)
20601 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
20602 cp_token *token;
20604 /* First, parse name of the the attribute, a.k.a
20605 attribute-token. */
20607 token = cp_lexer_peek_token (parser->lexer);
20608 if (token->type == CPP_NAME)
20609 attr_id = token->u.value;
20610 else if (token->type == CPP_KEYWORD)
20611 attr_id = ridpointers[(int) token->keyword];
20612 else if (token->flags & NAMED_OP)
20613 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
20615 if (attr_id == NULL_TREE)
20616 return NULL_TREE;
20618 cp_lexer_consume_token (parser->lexer);
20620 token = cp_lexer_peek_token (parser->lexer);
20621 if (token->type == CPP_SCOPE)
20623 /* We are seeing a scoped attribute token. */
20625 cp_lexer_consume_token (parser->lexer);
20626 attr_ns = attr_id;
20628 token = cp_lexer_consume_token (parser->lexer);
20629 if (token->type == CPP_NAME)
20630 attr_id = token->u.value;
20631 else if (token->type == CPP_KEYWORD)
20632 attr_id = ridpointers[(int) token->keyword];
20633 else
20635 error_at (token->location,
20636 "expected an identifier for the attribute name");
20637 return error_mark_node;
20639 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
20640 NULL_TREE);
20641 token = cp_lexer_peek_token (parser->lexer);
20643 else
20644 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
20645 NULL_TREE);
20647 /* Now parse the optional argument clause of the attribute. */
20649 if (token->type != CPP_OPEN_PAREN)
20650 return attribute;
20653 vec<tree, va_gc> *vec;
20654 int attr_flag = normal_attr;
20656 if (attr_ns == get_identifier ("gnu")
20657 && attribute_takes_identifier_p (attr_id))
20658 /* A GNU attribute that takes an identifier in parameter. */
20659 attr_flag = id_attr;
20661 vec = cp_parser_parenthesized_expression_list
20662 (parser, attr_flag, /*cast_p=*/false,
20663 /*allow_expansion_p=*/true,
20664 /*non_constant_p=*/NULL);
20665 if (vec == NULL)
20666 arguments = error_mark_node;
20667 else
20669 arguments = build_tree_list_vec (vec);
20670 release_tree_vector (vec);
20673 if (arguments == error_mark_node)
20674 attribute = error_mark_node;
20675 else
20676 TREE_VALUE (attribute) = arguments;
20679 return attribute;
20682 /* Parse a list of standard C++-11 attributes.
20684 attribute-list:
20685 attribute [opt]
20686 attribute-list , attribute[opt]
20687 attribute ...
20688 attribute-list , attribute ...
20691 static tree
20692 cp_parser_std_attribute_list (cp_parser *parser)
20694 tree attributes = NULL_TREE, attribute = NULL_TREE;
20695 cp_token *token = NULL;
20697 while (true)
20699 attribute = cp_parser_std_attribute (parser);
20700 if (attribute == error_mark_node)
20701 break;
20702 if (attribute != NULL_TREE)
20704 TREE_CHAIN (attribute) = attributes;
20705 attributes = attribute;
20707 token = cp_lexer_peek_token (parser->lexer);
20708 if (token->type != CPP_COMMA)
20709 break;
20710 cp_lexer_consume_token (parser->lexer);
20712 attributes = nreverse (attributes);
20713 return attributes;
20716 /* Parse a standard C++-11 attribute specifier.
20718 attribute-specifier:
20719 [ [ attribute-list ] ]
20720 alignment-specifier
20722 alignment-specifier:
20723 alignas ( type-id ... [opt] )
20724 alignas ( alignment-expression ... [opt] ). */
20726 static tree
20727 cp_parser_std_attribute_spec (cp_parser *parser)
20729 tree attributes = NULL_TREE;
20730 cp_token *token = cp_lexer_peek_token (parser->lexer);
20732 if (token->type == CPP_OPEN_SQUARE
20733 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
20735 cp_lexer_consume_token (parser->lexer);
20736 cp_lexer_consume_token (parser->lexer);
20738 attributes = cp_parser_std_attribute_list (parser);
20740 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
20741 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20742 cp_parser_skip_to_end_of_statement (parser);
20743 else
20744 /* Warn about parsing c++11 attribute in non-c++1 mode, only
20745 when we are sure that we have actually parsed them. */
20746 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
20748 else
20750 tree alignas_expr;
20752 /* Look for an alignment-specifier. */
20754 token = cp_lexer_peek_token (parser->lexer);
20756 if (token->type != CPP_KEYWORD
20757 || token->keyword != RID_ALIGNAS)
20758 return NULL_TREE;
20760 cp_lexer_consume_token (parser->lexer);
20761 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
20763 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
20765 cp_parser_error (parser, "expected %<(%>");
20766 return error_mark_node;
20769 cp_parser_parse_tentatively (parser);
20770 alignas_expr = cp_parser_type_id (parser);
20772 if (!cp_parser_parse_definitely (parser))
20774 gcc_assert (alignas_expr == error_mark_node
20775 || alignas_expr == NULL_TREE);
20777 alignas_expr =
20778 cp_parser_assignment_expression (parser, /*cast_p=*/false,
20779 /**cp_id_kind=*/NULL);
20780 if (alignas_expr == NULL_TREE
20781 || alignas_expr == error_mark_node)
20782 return alignas_expr;
20785 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
20787 cp_parser_error (parser, "expected %<)%>");
20788 return error_mark_node;
20791 alignas_expr = cxx_alignas_expr (alignas_expr);
20793 /* Build the C++-11 representation of an 'aligned'
20794 attribute. */
20795 attributes =
20796 build_tree_list (build_tree_list (get_identifier ("gnu"),
20797 get_identifier ("aligned")),
20798 build_tree_list (NULL_TREE, alignas_expr));
20801 return attributes;
20804 /* Parse a standard C++-11 attribute-specifier-seq.
20806 attribute-specifier-seq:
20807 attribute-specifier-seq [opt] attribute-specifier
20810 static tree
20811 cp_parser_std_attribute_spec_seq (cp_parser *parser)
20813 tree attr_specs = NULL;
20815 while (true)
20817 tree attr_spec = cp_parser_std_attribute_spec (parser);
20818 if (attr_spec == NULL_TREE)
20819 break;
20820 if (attr_spec == error_mark_node)
20821 return error_mark_node;
20823 TREE_CHAIN (attr_spec) = attr_specs;
20824 attr_specs = attr_spec;
20827 attr_specs = nreverse (attr_specs);
20828 return attr_specs;
20831 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20832 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20833 current value of the PEDANTIC flag, regardless of whether or not
20834 the `__extension__' keyword is present. The caller is responsible
20835 for restoring the value of the PEDANTIC flag. */
20837 static bool
20838 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20840 /* Save the old value of the PEDANTIC flag. */
20841 *saved_pedantic = pedantic;
20843 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20845 /* Consume the `__extension__' token. */
20846 cp_lexer_consume_token (parser->lexer);
20847 /* We're not being pedantic while the `__extension__' keyword is
20848 in effect. */
20849 pedantic = 0;
20851 return true;
20854 return false;
20857 /* Parse a label declaration.
20859 label-declaration:
20860 __label__ label-declarator-seq ;
20862 label-declarator-seq:
20863 identifier , label-declarator-seq
20864 identifier */
20866 static void
20867 cp_parser_label_declaration (cp_parser* parser)
20869 /* Look for the `__label__' keyword. */
20870 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20872 while (true)
20874 tree identifier;
20876 /* Look for an identifier. */
20877 identifier = cp_parser_identifier (parser);
20878 /* If we failed, stop. */
20879 if (identifier == error_mark_node)
20880 break;
20881 /* Declare it as a label. */
20882 finish_label_decl (identifier);
20883 /* If the next token is a `;', stop. */
20884 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20885 break;
20886 /* Look for the `,' separating the label declarations. */
20887 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20890 /* Look for the final `;'. */
20891 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20894 /* Support Functions */
20896 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20897 NAME should have one of the representations used for an
20898 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20899 is returned. If PARSER->SCOPE is a dependent type, then a
20900 SCOPE_REF is returned.
20902 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20903 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20904 was formed. Abstractly, such entities should not be passed to this
20905 function, because they do not need to be looked up, but it is
20906 simpler to check for this special case here, rather than at the
20907 call-sites.
20909 In cases not explicitly covered above, this function returns a
20910 DECL, OVERLOAD, or baselink representing the result of the lookup.
20911 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20912 is returned.
20914 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20915 (e.g., "struct") that was used. In that case bindings that do not
20916 refer to types are ignored.
20918 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20919 ignored.
20921 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20922 are ignored.
20924 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20925 types.
20927 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20928 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20929 NULL_TREE otherwise. */
20931 static tree
20932 cp_parser_lookup_name (cp_parser *parser, tree name,
20933 enum tag_types tag_type,
20934 bool is_template,
20935 bool is_namespace,
20936 bool check_dependency,
20937 tree *ambiguous_decls,
20938 location_t name_location)
20940 tree decl;
20941 tree object_type = parser->context->object_type;
20943 /* Assume that the lookup will be unambiguous. */
20944 if (ambiguous_decls)
20945 *ambiguous_decls = NULL_TREE;
20947 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20948 no longer valid. Note that if we are parsing tentatively, and
20949 the parse fails, OBJECT_TYPE will be automatically restored. */
20950 parser->context->object_type = NULL_TREE;
20952 if (name == error_mark_node)
20953 return error_mark_node;
20955 /* A template-id has already been resolved; there is no lookup to
20956 do. */
20957 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20958 return name;
20959 if (BASELINK_P (name))
20961 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20962 == TEMPLATE_ID_EXPR);
20963 return name;
20966 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20967 it should already have been checked to make sure that the name
20968 used matches the type being destroyed. */
20969 if (TREE_CODE (name) == BIT_NOT_EXPR)
20971 tree type;
20973 /* Figure out to which type this destructor applies. */
20974 if (parser->scope)
20975 type = parser->scope;
20976 else if (object_type)
20977 type = object_type;
20978 else
20979 type = current_class_type;
20980 /* If that's not a class type, there is no destructor. */
20981 if (!type || !CLASS_TYPE_P (type))
20982 return error_mark_node;
20983 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20984 lazily_declare_fn (sfk_destructor, type);
20985 if (!CLASSTYPE_DESTRUCTORS (type))
20986 return error_mark_node;
20987 /* If it was a class type, return the destructor. */
20988 return CLASSTYPE_DESTRUCTORS (type);
20991 /* By this point, the NAME should be an ordinary identifier. If
20992 the id-expression was a qualified name, the qualifying scope is
20993 stored in PARSER->SCOPE at this point. */
20994 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20996 /* Perform the lookup. */
20997 if (parser->scope)
20999 bool dependent_p;
21001 if (parser->scope == error_mark_node)
21002 return error_mark_node;
21004 /* If the SCOPE is dependent, the lookup must be deferred until
21005 the template is instantiated -- unless we are explicitly
21006 looking up names in uninstantiated templates. Even then, we
21007 cannot look up the name if the scope is not a class type; it
21008 might, for example, be a template type parameter. */
21009 dependent_p = (TYPE_P (parser->scope)
21010 && dependent_scope_p (parser->scope));
21011 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
21012 && dependent_p)
21013 /* Defer lookup. */
21014 decl = error_mark_node;
21015 else
21017 tree pushed_scope = NULL_TREE;
21019 /* If PARSER->SCOPE is a dependent type, then it must be a
21020 class type, and we must not be checking dependencies;
21021 otherwise, we would have processed this lookup above. So
21022 that PARSER->SCOPE is not considered a dependent base by
21023 lookup_member, we must enter the scope here. */
21024 if (dependent_p)
21025 pushed_scope = push_scope (parser->scope);
21027 /* If the PARSER->SCOPE is a template specialization, it
21028 may be instantiated during name lookup. In that case,
21029 errors may be issued. Even if we rollback the current
21030 tentative parse, those errors are valid. */
21031 decl = lookup_qualified_name (parser->scope, name,
21032 tag_type != none_type,
21033 /*complain=*/true);
21035 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21036 lookup result and the nested-name-specifier nominates a class C:
21037 * if the name specified after the nested-name-specifier, when
21038 looked up in C, is the injected-class-name of C (Clause 9), or
21039 * if the name specified after the nested-name-specifier is the
21040 same as the identifier or the simple-template-id's template-
21041 name in the last component of the nested-name-specifier,
21042 the name is instead considered to name the constructor of
21043 class C. [ Note: for example, the constructor is not an
21044 acceptable lookup result in an elaborated-type-specifier so
21045 the constructor would not be used in place of the
21046 injected-class-name. --end note ] Such a constructor name
21047 shall be used only in the declarator-id of a declaration that
21048 names a constructor or in a using-declaration. */
21049 if (tag_type == none_type
21050 && DECL_SELF_REFERENCE_P (decl)
21051 && same_type_p (DECL_CONTEXT (decl), parser->scope))
21052 decl = lookup_qualified_name (parser->scope, ctor_identifier,
21053 tag_type != none_type,
21054 /*complain=*/true);
21056 /* If we have a single function from a using decl, pull it out. */
21057 if (TREE_CODE (decl) == OVERLOAD
21058 && !really_overloaded_fn (decl))
21059 decl = OVL_FUNCTION (decl);
21061 if (pushed_scope)
21062 pop_scope (pushed_scope);
21065 /* If the scope is a dependent type and either we deferred lookup or
21066 we did lookup but didn't find the name, rememeber the name. */
21067 if (decl == error_mark_node && TYPE_P (parser->scope)
21068 && dependent_type_p (parser->scope))
21070 if (tag_type)
21072 tree type;
21074 /* The resolution to Core Issue 180 says that `struct
21075 A::B' should be considered a type-name, even if `A'
21076 is dependent. */
21077 type = make_typename_type (parser->scope, name, tag_type,
21078 /*complain=*/tf_error);
21079 decl = TYPE_NAME (type);
21081 else if (is_template
21082 && (cp_parser_next_token_ends_template_argument_p (parser)
21083 || cp_lexer_next_token_is (parser->lexer,
21084 CPP_CLOSE_PAREN)))
21085 decl = make_unbound_class_template (parser->scope,
21086 name, NULL_TREE,
21087 /*complain=*/tf_error);
21088 else
21089 decl = build_qualified_name (/*type=*/NULL_TREE,
21090 parser->scope, name,
21091 is_template);
21093 parser->qualifying_scope = parser->scope;
21094 parser->object_scope = NULL_TREE;
21096 else if (object_type)
21098 tree object_decl = NULL_TREE;
21099 /* Look up the name in the scope of the OBJECT_TYPE, unless the
21100 OBJECT_TYPE is not a class. */
21101 if (CLASS_TYPE_P (object_type))
21102 /* If the OBJECT_TYPE is a template specialization, it may
21103 be instantiated during name lookup. In that case, errors
21104 may be issued. Even if we rollback the current tentative
21105 parse, those errors are valid. */
21106 object_decl = lookup_member (object_type,
21107 name,
21108 /*protect=*/0,
21109 tag_type != none_type,
21110 tf_warning_or_error);
21111 /* Look it up in the enclosing context, too. */
21112 decl = lookup_name_real (name, tag_type != none_type,
21113 /*nonclass=*/0,
21114 /*block_p=*/true, is_namespace, 0);
21115 parser->object_scope = object_type;
21116 parser->qualifying_scope = NULL_TREE;
21117 if (object_decl)
21118 decl = object_decl;
21120 else
21122 decl = lookup_name_real (name, tag_type != none_type,
21123 /*nonclass=*/0,
21124 /*block_p=*/true, is_namespace, 0);
21125 parser->qualifying_scope = NULL_TREE;
21126 parser->object_scope = NULL_TREE;
21129 /* If the lookup failed, let our caller know. */
21130 if (!decl || decl == error_mark_node)
21131 return error_mark_node;
21133 /* Pull out the template from an injected-class-name (or multiple). */
21134 if (is_template)
21135 decl = maybe_get_template_decl_from_type_decl (decl);
21137 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
21138 if (TREE_CODE (decl) == TREE_LIST)
21140 if (ambiguous_decls)
21141 *ambiguous_decls = decl;
21142 /* The error message we have to print is too complicated for
21143 cp_parser_error, so we incorporate its actions directly. */
21144 if (!cp_parser_simulate_error (parser))
21146 error_at (name_location, "reference to %qD is ambiguous",
21147 name);
21148 print_candidates (decl);
21150 return error_mark_node;
21153 gcc_assert (DECL_P (decl)
21154 || TREE_CODE (decl) == OVERLOAD
21155 || TREE_CODE (decl) == SCOPE_REF
21156 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
21157 || BASELINK_P (decl));
21159 /* If we have resolved the name of a member declaration, check to
21160 see if the declaration is accessible. When the name resolves to
21161 set of overloaded functions, accessibility is checked when
21162 overload resolution is done.
21164 During an explicit instantiation, access is not checked at all,
21165 as per [temp.explicit]. */
21166 if (DECL_P (decl))
21167 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
21169 maybe_record_typedef_use (decl);
21171 return decl;
21174 /* Like cp_parser_lookup_name, but for use in the typical case where
21175 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
21176 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
21178 static tree
21179 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
21181 return cp_parser_lookup_name (parser, name,
21182 none_type,
21183 /*is_template=*/false,
21184 /*is_namespace=*/false,
21185 /*check_dependency=*/true,
21186 /*ambiguous_decls=*/NULL,
21187 location);
21190 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
21191 the current context, return the TYPE_DECL. If TAG_NAME_P is
21192 true, the DECL indicates the class being defined in a class-head,
21193 or declared in an elaborated-type-specifier.
21195 Otherwise, return DECL. */
21197 static tree
21198 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
21200 /* If the TEMPLATE_DECL is being declared as part of a class-head,
21201 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
21203 struct A {
21204 template <typename T> struct B;
21207 template <typename T> struct A::B {};
21209 Similarly, in an elaborated-type-specifier:
21211 namespace N { struct X{}; }
21213 struct A {
21214 template <typename T> friend struct N::X;
21217 However, if the DECL refers to a class type, and we are in
21218 the scope of the class, then the name lookup automatically
21219 finds the TYPE_DECL created by build_self_reference rather
21220 than a TEMPLATE_DECL. For example, in:
21222 template <class T> struct S {
21223 S s;
21226 there is no need to handle such case. */
21228 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
21229 return DECL_TEMPLATE_RESULT (decl);
21231 return decl;
21234 /* If too many, or too few, template-parameter lists apply to the
21235 declarator, issue an error message. Returns TRUE if all went well,
21236 and FALSE otherwise. */
21238 static bool
21239 cp_parser_check_declarator_template_parameters (cp_parser* parser,
21240 cp_declarator *declarator,
21241 location_t declarator_location)
21243 switch (declarator->kind)
21245 case cdk_id:
21247 unsigned num_templates = 0;
21248 tree scope = declarator->u.id.qualifying_scope;
21250 if (scope)
21251 num_templates = num_template_headers_for_class (scope);
21252 else if (TREE_CODE (declarator->u.id.unqualified_name)
21253 == TEMPLATE_ID_EXPR)
21254 /* If the DECLARATOR has the form `X<y>' then it uses one
21255 additional level of template parameters. */
21256 ++num_templates;
21258 return cp_parser_check_template_parameters
21259 (parser, num_templates, declarator_location, declarator);
21262 case cdk_function:
21263 case cdk_array:
21264 case cdk_pointer:
21265 case cdk_reference:
21266 case cdk_ptrmem:
21267 return (cp_parser_check_declarator_template_parameters
21268 (parser, declarator->declarator, declarator_location));
21270 case cdk_error:
21271 return true;
21273 default:
21274 gcc_unreachable ();
21276 return false;
21279 /* NUM_TEMPLATES were used in the current declaration. If that is
21280 invalid, return FALSE and issue an error messages. Otherwise,
21281 return TRUE. If DECLARATOR is non-NULL, then we are checking a
21282 declarator and we can print more accurate diagnostics. */
21284 static bool
21285 cp_parser_check_template_parameters (cp_parser* parser,
21286 unsigned num_templates,
21287 location_t location,
21288 cp_declarator *declarator)
21290 /* If there are the same number of template classes and parameter
21291 lists, that's OK. */
21292 if (parser->num_template_parameter_lists == num_templates)
21293 return true;
21294 /* If there are more, but only one more, then we are referring to a
21295 member template. That's OK too. */
21296 if (parser->num_template_parameter_lists == num_templates + 1)
21297 return true;
21298 /* If there are more template classes than parameter lists, we have
21299 something like:
21301 template <class T> void S<T>::R<T>::f (); */
21302 if (parser->num_template_parameter_lists < num_templates)
21304 if (declarator && !current_function_decl)
21305 error_at (location, "specializing member %<%T::%E%> "
21306 "requires %<template<>%> syntax",
21307 declarator->u.id.qualifying_scope,
21308 declarator->u.id.unqualified_name);
21309 else if (declarator)
21310 error_at (location, "invalid declaration of %<%T::%E%>",
21311 declarator->u.id.qualifying_scope,
21312 declarator->u.id.unqualified_name);
21313 else
21314 error_at (location, "too few template-parameter-lists");
21315 return false;
21317 /* Otherwise, there are too many template parameter lists. We have
21318 something like:
21320 template <class T> template <class U> void S::f(); */
21321 error_at (location, "too many template-parameter-lists");
21322 return false;
21325 /* Parse an optional `::' token indicating that the following name is
21326 from the global namespace. If so, PARSER->SCOPE is set to the
21327 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
21328 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
21329 Returns the new value of PARSER->SCOPE, if the `::' token is
21330 present, and NULL_TREE otherwise. */
21332 static tree
21333 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
21335 cp_token *token;
21337 /* Peek at the next token. */
21338 token = cp_lexer_peek_token (parser->lexer);
21339 /* If we're looking at a `::' token then we're starting from the
21340 global namespace, not our current location. */
21341 if (token->type == CPP_SCOPE)
21343 /* Consume the `::' token. */
21344 cp_lexer_consume_token (parser->lexer);
21345 /* Set the SCOPE so that we know where to start the lookup. */
21346 parser->scope = global_namespace;
21347 parser->qualifying_scope = global_namespace;
21348 parser->object_scope = NULL_TREE;
21350 return parser->scope;
21352 else if (!current_scope_valid_p)
21354 parser->scope = NULL_TREE;
21355 parser->qualifying_scope = NULL_TREE;
21356 parser->object_scope = NULL_TREE;
21359 return NULL_TREE;
21362 /* Returns TRUE if the upcoming token sequence is the start of a
21363 constructor declarator. If FRIEND_P is true, the declarator is
21364 preceded by the `friend' specifier. */
21366 static bool
21367 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
21369 bool constructor_p;
21370 tree nested_name_specifier;
21371 cp_token *next_token;
21373 /* The common case is that this is not a constructor declarator, so
21374 try to avoid doing lots of work if at all possible. It's not
21375 valid declare a constructor at function scope. */
21376 if (parser->in_function_body)
21377 return false;
21378 /* And only certain tokens can begin a constructor declarator. */
21379 next_token = cp_lexer_peek_token (parser->lexer);
21380 if (next_token->type != CPP_NAME
21381 && next_token->type != CPP_SCOPE
21382 && next_token->type != CPP_NESTED_NAME_SPECIFIER
21383 && next_token->type != CPP_TEMPLATE_ID)
21384 return false;
21386 /* Parse tentatively; we are going to roll back all of the tokens
21387 consumed here. */
21388 cp_parser_parse_tentatively (parser);
21389 /* Assume that we are looking at a constructor declarator. */
21390 constructor_p = true;
21392 /* Look for the optional `::' operator. */
21393 cp_parser_global_scope_opt (parser,
21394 /*current_scope_valid_p=*/false);
21395 /* Look for the nested-name-specifier. */
21396 nested_name_specifier
21397 = (cp_parser_nested_name_specifier_opt (parser,
21398 /*typename_keyword_p=*/false,
21399 /*check_dependency_p=*/false,
21400 /*type_p=*/false,
21401 /*is_declaration=*/false));
21402 /* Outside of a class-specifier, there must be a
21403 nested-name-specifier. */
21404 if (!nested_name_specifier &&
21405 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
21406 || friend_p))
21407 constructor_p = false;
21408 else if (nested_name_specifier == error_mark_node)
21409 constructor_p = false;
21411 /* If we have a class scope, this is easy; DR 147 says that S::S always
21412 names the constructor, and no other qualified name could. */
21413 if (constructor_p && nested_name_specifier
21414 && CLASS_TYPE_P (nested_name_specifier))
21416 tree id = cp_parser_unqualified_id (parser,
21417 /*template_keyword_p=*/false,
21418 /*check_dependency_p=*/false,
21419 /*declarator_p=*/true,
21420 /*optional_p=*/false);
21421 if (is_overloaded_fn (id))
21422 id = DECL_NAME (get_first_fn (id));
21423 if (!constructor_name_p (id, nested_name_specifier))
21424 constructor_p = false;
21426 /* If we still think that this might be a constructor-declarator,
21427 look for a class-name. */
21428 else if (constructor_p)
21430 /* If we have:
21432 template <typename T> struct S {
21433 S();
21436 we must recognize that the nested `S' names a class. */
21437 tree type_decl;
21438 type_decl = cp_parser_class_name (parser,
21439 /*typename_keyword_p=*/false,
21440 /*template_keyword_p=*/false,
21441 none_type,
21442 /*check_dependency_p=*/false,
21443 /*class_head_p=*/false,
21444 /*is_declaration=*/false);
21445 /* If there was no class-name, then this is not a constructor. */
21446 constructor_p = !cp_parser_error_occurred (parser);
21448 /* If we're still considering a constructor, we have to see a `(',
21449 to begin the parameter-declaration-clause, followed by either a
21450 `)', an `...', or a decl-specifier. We need to check for a
21451 type-specifier to avoid being fooled into thinking that:
21453 S (f) (int);
21455 is a constructor. (It is actually a function named `f' that
21456 takes one parameter (of type `int') and returns a value of type
21457 `S'. */
21458 if (constructor_p
21459 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21460 constructor_p = false;
21462 if (constructor_p
21463 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
21464 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
21465 /* A parameter declaration begins with a decl-specifier,
21466 which is either the "attribute" keyword, a storage class
21467 specifier, or (usually) a type-specifier. */
21468 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
21470 tree type;
21471 tree pushed_scope = NULL_TREE;
21472 unsigned saved_num_template_parameter_lists;
21474 /* Names appearing in the type-specifier should be looked up
21475 in the scope of the class. */
21476 if (current_class_type)
21477 type = NULL_TREE;
21478 else
21480 type = TREE_TYPE (type_decl);
21481 if (TREE_CODE (type) == TYPENAME_TYPE)
21483 type = resolve_typename_type (type,
21484 /*only_current_p=*/false);
21485 if (TREE_CODE (type) == TYPENAME_TYPE)
21487 cp_parser_abort_tentative_parse (parser);
21488 return false;
21491 pushed_scope = push_scope (type);
21494 /* Inside the constructor parameter list, surrounding
21495 template-parameter-lists do not apply. */
21496 saved_num_template_parameter_lists
21497 = parser->num_template_parameter_lists;
21498 parser->num_template_parameter_lists = 0;
21500 /* Look for the type-specifier. */
21501 cp_parser_type_specifier (parser,
21502 CP_PARSER_FLAGS_NONE,
21503 /*decl_specs=*/NULL,
21504 /*is_declarator=*/true,
21505 /*declares_class_or_enum=*/NULL,
21506 /*is_cv_qualifier=*/NULL);
21508 parser->num_template_parameter_lists
21509 = saved_num_template_parameter_lists;
21511 /* Leave the scope of the class. */
21512 if (pushed_scope)
21513 pop_scope (pushed_scope);
21515 constructor_p = !cp_parser_error_occurred (parser);
21519 /* We did not really want to consume any tokens. */
21520 cp_parser_abort_tentative_parse (parser);
21522 return constructor_p;
21525 /* Parse the definition of the function given by the DECL_SPECIFIERS,
21526 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
21527 they must be performed once we are in the scope of the function.
21529 Returns the function defined. */
21531 static tree
21532 cp_parser_function_definition_from_specifiers_and_declarator
21533 (cp_parser* parser,
21534 cp_decl_specifier_seq *decl_specifiers,
21535 tree attributes,
21536 const cp_declarator *declarator)
21538 tree fn;
21539 bool success_p;
21541 /* Begin the function-definition. */
21542 success_p = start_function (decl_specifiers, declarator, attributes);
21544 /* The things we're about to see are not directly qualified by any
21545 template headers we've seen thus far. */
21546 reset_specialization ();
21548 /* If there were names looked up in the decl-specifier-seq that we
21549 did not check, check them now. We must wait until we are in the
21550 scope of the function to perform the checks, since the function
21551 might be a friend. */
21552 perform_deferred_access_checks (tf_warning_or_error);
21554 if (!success_p)
21556 /* Skip the entire function. */
21557 cp_parser_skip_to_end_of_block_or_statement (parser);
21558 fn = error_mark_node;
21560 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
21562 /* Seen already, skip it. An error message has already been output. */
21563 cp_parser_skip_to_end_of_block_or_statement (parser);
21564 fn = current_function_decl;
21565 current_function_decl = NULL_TREE;
21566 /* If this is a function from a class, pop the nested class. */
21567 if (current_class_name)
21568 pop_nested_class ();
21570 else
21572 timevar_id_t tv;
21573 if (DECL_DECLARED_INLINE_P (current_function_decl))
21574 tv = TV_PARSE_INLINE;
21575 else
21576 tv = TV_PARSE_FUNC;
21577 timevar_push (tv);
21578 fn = cp_parser_function_definition_after_declarator (parser,
21579 /*inline_p=*/false);
21580 timevar_pop (tv);
21583 return fn;
21586 /* Parse the part of a function-definition that follows the
21587 declarator. INLINE_P is TRUE iff this function is an inline
21588 function defined within a class-specifier.
21590 Returns the function defined. */
21592 static tree
21593 cp_parser_function_definition_after_declarator (cp_parser* parser,
21594 bool inline_p)
21596 tree fn;
21597 bool ctor_initializer_p = false;
21598 bool saved_in_unbraced_linkage_specification_p;
21599 bool saved_in_function_body;
21600 unsigned saved_num_template_parameter_lists;
21601 cp_token *token;
21603 saved_in_function_body = parser->in_function_body;
21604 parser->in_function_body = true;
21605 /* If the next token is `return', then the code may be trying to
21606 make use of the "named return value" extension that G++ used to
21607 support. */
21608 token = cp_lexer_peek_token (parser->lexer);
21609 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21611 /* Consume the `return' keyword. */
21612 cp_lexer_consume_token (parser->lexer);
21613 /* Look for the identifier that indicates what value is to be
21614 returned. */
21615 cp_parser_identifier (parser);
21616 /* Issue an error message. */
21617 error_at (token->location,
21618 "named return values are no longer supported");
21619 /* Skip tokens until we reach the start of the function body. */
21620 while (true)
21622 cp_token *token = cp_lexer_peek_token (parser->lexer);
21623 if (token->type == CPP_OPEN_BRACE
21624 || token->type == CPP_EOF
21625 || token->type == CPP_PRAGMA_EOL)
21626 break;
21627 cp_lexer_consume_token (parser->lexer);
21630 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21631 anything declared inside `f'. */
21632 saved_in_unbraced_linkage_specification_p
21633 = parser->in_unbraced_linkage_specification_p;
21634 parser->in_unbraced_linkage_specification_p = false;
21635 /* Inside the function, surrounding template-parameter-lists do not
21636 apply. */
21637 saved_num_template_parameter_lists
21638 = parser->num_template_parameter_lists;
21639 parser->num_template_parameter_lists = 0;
21641 start_lambda_scope (current_function_decl);
21643 /* If the next token is `try', `__transaction_atomic', or
21644 `__transaction_relaxed`, then we are looking at either function-try-block
21645 or function-transaction-block. Note that all of these include the
21646 function-body. */
21647 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21648 ctor_initializer_p = cp_parser_function_transaction (parser,
21649 RID_TRANSACTION_ATOMIC);
21650 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21651 RID_TRANSACTION_RELAXED))
21652 ctor_initializer_p = cp_parser_function_transaction (parser,
21653 RID_TRANSACTION_RELAXED);
21654 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21655 ctor_initializer_p = cp_parser_function_try_block (parser);
21656 else
21657 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21658 (parser, /*in_function_try_block=*/false);
21660 finish_lambda_scope ();
21662 /* Finish the function. */
21663 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21664 (inline_p ? 2 : 0));
21665 /* Generate code for it, if necessary. */
21666 expand_or_defer_fn (fn);
21667 /* Restore the saved values. */
21668 parser->in_unbraced_linkage_specification_p
21669 = saved_in_unbraced_linkage_specification_p;
21670 parser->num_template_parameter_lists
21671 = saved_num_template_parameter_lists;
21672 parser->in_function_body = saved_in_function_body;
21674 return fn;
21677 /* Parse a template-declaration, assuming that the `export' (and
21678 `extern') keywords, if present, has already been scanned. MEMBER_P
21679 is as for cp_parser_template_declaration. */
21681 static void
21682 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21684 tree decl = NULL_TREE;
21685 vec<deferred_access_check, va_gc> *checks;
21686 tree parameter_list;
21687 bool friend_p = false;
21688 bool need_lang_pop;
21689 cp_token *token;
21691 /* Look for the `template' keyword. */
21692 token = cp_lexer_peek_token (parser->lexer);
21693 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21694 return;
21696 /* And the `<'. */
21697 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21698 return;
21699 if (at_class_scope_p () && current_function_decl)
21701 /* 14.5.2.2 [temp.mem]
21703 A local class shall not have member templates. */
21704 error_at (token->location,
21705 "invalid declaration of member template in local class");
21706 cp_parser_skip_to_end_of_block_or_statement (parser);
21707 return;
21709 /* [temp]
21711 A template ... shall not have C linkage. */
21712 if (current_lang_name == lang_name_c)
21714 error_at (token->location, "template with C linkage");
21715 /* Give it C++ linkage to avoid confusing other parts of the
21716 front end. */
21717 push_lang_context (lang_name_cplusplus);
21718 need_lang_pop = true;
21720 else
21721 need_lang_pop = false;
21723 /* We cannot perform access checks on the template parameter
21724 declarations until we know what is being declared, just as we
21725 cannot check the decl-specifier list. */
21726 push_deferring_access_checks (dk_deferred);
21728 /* If the next token is `>', then we have an invalid
21729 specialization. Rather than complain about an invalid template
21730 parameter, issue an error message here. */
21731 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21733 cp_parser_error (parser, "invalid explicit specialization");
21734 begin_specialization ();
21735 parameter_list = NULL_TREE;
21737 else
21739 /* Parse the template parameters. */
21740 parameter_list = cp_parser_template_parameter_list (parser);
21743 /* Get the deferred access checks from the parameter list. These
21744 will be checked once we know what is being declared, as for a
21745 member template the checks must be performed in the scope of the
21746 class containing the member. */
21747 checks = get_deferred_access_checks ();
21749 /* Look for the `>'. */
21750 cp_parser_skip_to_end_of_template_parameter_list (parser);
21751 /* We just processed one more parameter list. */
21752 ++parser->num_template_parameter_lists;
21753 /* If the next token is `template', there are more template
21754 parameters. */
21755 if (cp_lexer_next_token_is_keyword (parser->lexer,
21756 RID_TEMPLATE))
21757 cp_parser_template_declaration_after_export (parser, member_p);
21758 else if (cxx_dialect >= cxx0x
21759 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21760 decl = cp_parser_alias_declaration (parser);
21761 else
21763 /* There are no access checks when parsing a template, as we do not
21764 know if a specialization will be a friend. */
21765 push_deferring_access_checks (dk_no_check);
21766 token = cp_lexer_peek_token (parser->lexer);
21767 decl = cp_parser_single_declaration (parser,
21768 checks,
21769 member_p,
21770 /*explicit_specialization_p=*/false,
21771 &friend_p);
21772 pop_deferring_access_checks ();
21774 /* If this is a member template declaration, let the front
21775 end know. */
21776 if (member_p && !friend_p && decl)
21778 if (TREE_CODE (decl) == TYPE_DECL)
21779 cp_parser_check_access_in_redeclaration (decl, token->location);
21781 decl = finish_member_template_decl (decl);
21783 else if (friend_p && decl
21784 && (TREE_CODE (decl) == TYPE_DECL
21785 || DECL_TYPE_TEMPLATE_P (decl)))
21786 make_friend_class (current_class_type, TREE_TYPE (decl),
21787 /*complain=*/true);
21789 /* We are done with the current parameter list. */
21790 --parser->num_template_parameter_lists;
21792 pop_deferring_access_checks ();
21794 /* Finish up. */
21795 finish_template_decl (parameter_list);
21797 /* Check the template arguments for a literal operator template. */
21798 if (decl
21799 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21800 && UDLIT_OPER_P (DECL_NAME (decl)))
21802 bool ok = true;
21803 if (parameter_list == NULL_TREE)
21804 ok = false;
21805 else
21807 int num_parms = TREE_VEC_LENGTH (parameter_list);
21808 if (num_parms != 1)
21809 ok = false;
21810 else
21812 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21813 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21814 if (TREE_TYPE (parm) != char_type_node
21815 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21816 ok = false;
21819 if (!ok)
21820 error ("literal operator template %qD has invalid parameter list."
21821 " Expected non-type template argument pack <char...>",
21822 decl);
21824 /* Register member declarations. */
21825 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21826 finish_member_declaration (decl);
21827 /* For the erroneous case of a template with C linkage, we pushed an
21828 implicit C++ linkage scope; exit that scope now. */
21829 if (need_lang_pop)
21830 pop_lang_context ();
21831 /* If DECL is a function template, we must return to parse it later.
21832 (Even though there is no definition, there might be default
21833 arguments that need handling.) */
21834 if (member_p && decl
21835 && (TREE_CODE (decl) == FUNCTION_DECL
21836 || DECL_FUNCTION_TEMPLATE_P (decl)))
21837 vec_safe_push (unparsed_funs_with_definitions, decl);
21840 /* Perform the deferred access checks from a template-parameter-list.
21841 CHECKS is a TREE_LIST of access checks, as returned by
21842 get_deferred_access_checks. */
21844 static void
21845 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
21847 ++processing_template_parmlist;
21848 perform_access_checks (checks, tf_warning_or_error);
21849 --processing_template_parmlist;
21852 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21853 `function-definition' sequence that follows a template header.
21854 If MEMBER_P is true, this declaration appears in a class scope.
21856 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21857 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21859 static tree
21860 cp_parser_single_declaration (cp_parser* parser,
21861 vec<deferred_access_check, va_gc> *checks,
21862 bool member_p,
21863 bool explicit_specialization_p,
21864 bool* friend_p)
21866 int declares_class_or_enum;
21867 tree decl = NULL_TREE;
21868 cp_decl_specifier_seq decl_specifiers;
21869 bool function_definition_p = false;
21870 cp_token *decl_spec_token_start;
21872 /* This function is only used when processing a template
21873 declaration. */
21874 gcc_assert (innermost_scope_kind () == sk_template_parms
21875 || innermost_scope_kind () == sk_template_spec);
21877 /* Defer access checks until we know what is being declared. */
21878 push_deferring_access_checks (dk_deferred);
21880 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21881 alternative. */
21882 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21883 cp_parser_decl_specifier_seq (parser,
21884 CP_PARSER_FLAGS_OPTIONAL,
21885 &decl_specifiers,
21886 &declares_class_or_enum);
21887 if (friend_p)
21888 *friend_p = cp_parser_friend_p (&decl_specifiers);
21890 /* There are no template typedefs. */
21891 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
21893 error_at (decl_spec_token_start->location,
21894 "template declaration of %<typedef%>");
21895 decl = error_mark_node;
21898 /* Gather up the access checks that occurred the
21899 decl-specifier-seq. */
21900 stop_deferring_access_checks ();
21902 /* Check for the declaration of a template class. */
21903 if (declares_class_or_enum)
21905 if (cp_parser_declares_only_class_p (parser))
21907 decl = shadow_tag (&decl_specifiers);
21909 /* In this case:
21911 struct C {
21912 friend template <typename T> struct A<T>::B;
21915 A<T>::B will be represented by a TYPENAME_TYPE, and
21916 therefore not recognized by shadow_tag. */
21917 if (friend_p && *friend_p
21918 && !decl
21919 && decl_specifiers.type
21920 && TYPE_P (decl_specifiers.type))
21921 decl = decl_specifiers.type;
21923 if (decl && decl != error_mark_node)
21924 decl = TYPE_NAME (decl);
21925 else
21926 decl = error_mark_node;
21928 /* Perform access checks for template parameters. */
21929 cp_parser_perform_template_parameter_access_checks (checks);
21933 /* Complain about missing 'typename' or other invalid type names. */
21934 if (!decl_specifiers.any_type_specifiers_p
21935 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21937 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21938 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21939 the rest of this declaration. */
21940 decl = error_mark_node;
21941 goto out;
21944 /* If it's not a template class, try for a template function. If
21945 the next token is a `;', then this declaration does not declare
21946 anything. But, if there were errors in the decl-specifiers, then
21947 the error might well have come from an attempted class-specifier.
21948 In that case, there's no need to warn about a missing declarator. */
21949 if (!decl
21950 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21951 || decl_specifiers.type != error_mark_node))
21953 decl = cp_parser_init_declarator (parser,
21954 &decl_specifiers,
21955 checks,
21956 /*function_definition_allowed_p=*/true,
21957 member_p,
21958 declares_class_or_enum,
21959 &function_definition_p,
21960 NULL);
21962 /* 7.1.1-1 [dcl.stc]
21964 A storage-class-specifier shall not be specified in an explicit
21965 specialization... */
21966 if (decl
21967 && explicit_specialization_p
21968 && decl_specifiers.storage_class != sc_none)
21970 error_at (decl_spec_token_start->location,
21971 "explicit template specialization cannot have a storage class");
21972 decl = error_mark_node;
21975 if (decl && TREE_CODE (decl) == VAR_DECL)
21976 check_template_variable (decl);
21979 /* Look for a trailing `;' after the declaration. */
21980 if (!function_definition_p
21981 && (decl == error_mark_node
21982 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21983 cp_parser_skip_to_end_of_block_or_statement (parser);
21985 out:
21986 pop_deferring_access_checks ();
21988 /* Clear any current qualification; whatever comes next is the start
21989 of something new. */
21990 parser->scope = NULL_TREE;
21991 parser->qualifying_scope = NULL_TREE;
21992 parser->object_scope = NULL_TREE;
21994 return decl;
21997 /* Parse a cast-expression that is not the operand of a unary "&". */
21999 static tree
22000 cp_parser_simple_cast_expression (cp_parser *parser)
22002 return cp_parser_cast_expression (parser, /*address_p=*/false,
22003 /*cast_p=*/false, NULL);
22006 /* Parse a functional cast to TYPE. Returns an expression
22007 representing the cast. */
22009 static tree
22010 cp_parser_functional_cast (cp_parser* parser, tree type)
22012 vec<tree, va_gc> *vec;
22013 tree expression_list;
22014 tree cast;
22015 bool nonconst_p;
22017 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22019 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22020 expression_list = cp_parser_braced_list (parser, &nonconst_p);
22021 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
22022 if (TREE_CODE (type) == TYPE_DECL)
22023 type = TREE_TYPE (type);
22024 return finish_compound_literal (type, expression_list,
22025 tf_warning_or_error);
22029 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22030 /*cast_p=*/true,
22031 /*allow_expansion_p=*/true,
22032 /*non_constant_p=*/NULL);
22033 if (vec == NULL)
22034 expression_list = error_mark_node;
22035 else
22037 expression_list = build_tree_list_vec (vec);
22038 release_tree_vector (vec);
22041 cast = build_functional_cast (type, expression_list,
22042 tf_warning_or_error);
22043 /* [expr.const]/1: In an integral constant expression "only type
22044 conversions to integral or enumeration type can be used". */
22045 if (TREE_CODE (type) == TYPE_DECL)
22046 type = TREE_TYPE (type);
22047 if (cast != error_mark_node
22048 && !cast_valid_in_integral_constant_expression_p (type)
22049 && cp_parser_non_integral_constant_expression (parser,
22050 NIC_CONSTRUCTOR))
22051 return error_mark_node;
22052 return cast;
22055 /* Save the tokens that make up the body of a member function defined
22056 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
22057 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
22058 specifiers applied to the declaration. Returns the FUNCTION_DECL
22059 for the member function. */
22061 static tree
22062 cp_parser_save_member_function_body (cp_parser* parser,
22063 cp_decl_specifier_seq *decl_specifiers,
22064 cp_declarator *declarator,
22065 tree attributes)
22067 cp_token *first;
22068 cp_token *last;
22069 tree fn;
22071 /* Create the FUNCTION_DECL. */
22072 fn = grokmethod (decl_specifiers, declarator, attributes);
22073 /* If something went badly wrong, bail out now. */
22074 if (fn == error_mark_node)
22076 /* If there's a function-body, skip it. */
22077 if (cp_parser_token_starts_function_definition_p
22078 (cp_lexer_peek_token (parser->lexer)))
22079 cp_parser_skip_to_end_of_block_or_statement (parser);
22080 return error_mark_node;
22083 /* Remember it, if there default args to post process. */
22084 cp_parser_save_default_args (parser, fn);
22086 /* Save away the tokens that make up the body of the
22087 function. */
22088 first = parser->lexer->next_token;
22089 /* We can have braced-init-list mem-initializers before the fn body. */
22090 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22092 cp_lexer_consume_token (parser->lexer);
22093 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22094 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
22096 /* cache_group will stop after an un-nested { } pair, too. */
22097 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
22098 break;
22100 /* variadic mem-inits have ... after the ')'. */
22101 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22102 cp_lexer_consume_token (parser->lexer);
22105 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22106 /* Handle function try blocks. */
22107 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
22108 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22109 last = parser->lexer->next_token;
22111 /* Save away the inline definition; we will process it when the
22112 class is complete. */
22113 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
22114 DECL_PENDING_INLINE_P (fn) = 1;
22116 /* We need to know that this was defined in the class, so that
22117 friend templates are handled correctly. */
22118 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
22120 /* Add FN to the queue of functions to be parsed later. */
22121 vec_safe_push (unparsed_funs_with_definitions, fn);
22123 return fn;
22126 /* Save the tokens that make up the in-class initializer for a non-static
22127 data member. Returns a DEFAULT_ARG. */
22129 static tree
22130 cp_parser_save_nsdmi (cp_parser* parser)
22132 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
22135 /* Parse a template-argument-list, as well as the trailing ">" (but
22136 not the opening "<"). See cp_parser_template_argument_list for the
22137 return value. */
22139 static tree
22140 cp_parser_enclosed_template_argument_list (cp_parser* parser)
22142 tree arguments;
22143 tree saved_scope;
22144 tree saved_qualifying_scope;
22145 tree saved_object_scope;
22146 bool saved_greater_than_is_operator_p;
22147 int saved_unevaluated_operand;
22148 int saved_inhibit_evaluation_warnings;
22150 /* [temp.names]
22152 When parsing a template-id, the first non-nested `>' is taken as
22153 the end of the template-argument-list rather than a greater-than
22154 operator. */
22155 saved_greater_than_is_operator_p
22156 = parser->greater_than_is_operator_p;
22157 parser->greater_than_is_operator_p = false;
22158 /* Parsing the argument list may modify SCOPE, so we save it
22159 here. */
22160 saved_scope = parser->scope;
22161 saved_qualifying_scope = parser->qualifying_scope;
22162 saved_object_scope = parser->object_scope;
22163 /* We need to evaluate the template arguments, even though this
22164 template-id may be nested within a "sizeof". */
22165 saved_unevaluated_operand = cp_unevaluated_operand;
22166 cp_unevaluated_operand = 0;
22167 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22168 c_inhibit_evaluation_warnings = 0;
22169 /* Parse the template-argument-list itself. */
22170 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
22171 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22172 arguments = NULL_TREE;
22173 else
22174 arguments = cp_parser_template_argument_list (parser);
22175 /* Look for the `>' that ends the template-argument-list. If we find
22176 a '>>' instead, it's probably just a typo. */
22177 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22179 if (cxx_dialect != cxx98)
22181 /* In C++0x, a `>>' in a template argument list or cast
22182 expression is considered to be two separate `>'
22183 tokens. So, change the current token to a `>', but don't
22184 consume it: it will be consumed later when the outer
22185 template argument list (or cast expression) is parsed.
22186 Note that this replacement of `>' for `>>' is necessary
22187 even if we are parsing tentatively: in the tentative
22188 case, after calling
22189 cp_parser_enclosed_template_argument_list we will always
22190 throw away all of the template arguments and the first
22191 closing `>', either because the template argument list
22192 was erroneous or because we are replacing those tokens
22193 with a CPP_TEMPLATE_ID token. The second `>' (which will
22194 not have been thrown away) is needed either to close an
22195 outer template argument list or to complete a new-style
22196 cast. */
22197 cp_token *token = cp_lexer_peek_token (parser->lexer);
22198 token->type = CPP_GREATER;
22200 else if (!saved_greater_than_is_operator_p)
22202 /* If we're in a nested template argument list, the '>>' has
22203 to be a typo for '> >'. We emit the error message, but we
22204 continue parsing and we push a '>' as next token, so that
22205 the argument list will be parsed correctly. Note that the
22206 global source location is still on the token before the
22207 '>>', so we need to say explicitly where we want it. */
22208 cp_token *token = cp_lexer_peek_token (parser->lexer);
22209 error_at (token->location, "%<>>%> should be %<> >%> "
22210 "within a nested template argument list");
22212 token->type = CPP_GREATER;
22214 else
22216 /* If this is not a nested template argument list, the '>>'
22217 is a typo for '>'. Emit an error message and continue.
22218 Same deal about the token location, but here we can get it
22219 right by consuming the '>>' before issuing the diagnostic. */
22220 cp_token *token = cp_lexer_consume_token (parser->lexer);
22221 error_at (token->location,
22222 "spurious %<>>%>, use %<>%> to terminate "
22223 "a template argument list");
22226 else
22227 cp_parser_skip_to_end_of_template_parameter_list (parser);
22228 /* The `>' token might be a greater-than operator again now. */
22229 parser->greater_than_is_operator_p
22230 = saved_greater_than_is_operator_p;
22231 /* Restore the SAVED_SCOPE. */
22232 parser->scope = saved_scope;
22233 parser->qualifying_scope = saved_qualifying_scope;
22234 parser->object_scope = saved_object_scope;
22235 cp_unevaluated_operand = saved_unevaluated_operand;
22236 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22238 return arguments;
22241 /* MEMBER_FUNCTION is a member function, or a friend. If default
22242 arguments, or the body of the function have not yet been parsed,
22243 parse them now. */
22245 static void
22246 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
22248 timevar_push (TV_PARSE_INMETH);
22249 /* If this member is a template, get the underlying
22250 FUNCTION_DECL. */
22251 if (DECL_FUNCTION_TEMPLATE_P (member_function))
22252 member_function = DECL_TEMPLATE_RESULT (member_function);
22254 /* There should not be any class definitions in progress at this
22255 point; the bodies of members are only parsed outside of all class
22256 definitions. */
22257 gcc_assert (parser->num_classes_being_defined == 0);
22258 /* While we're parsing the member functions we might encounter more
22259 classes. We want to handle them right away, but we don't want
22260 them getting mixed up with functions that are currently in the
22261 queue. */
22262 push_unparsed_function_queues (parser);
22264 /* Make sure that any template parameters are in scope. */
22265 maybe_begin_member_template_processing (member_function);
22267 /* If the body of the function has not yet been parsed, parse it
22268 now. */
22269 if (DECL_PENDING_INLINE_P (member_function))
22271 tree function_scope;
22272 cp_token_cache *tokens;
22274 /* The function is no longer pending; we are processing it. */
22275 tokens = DECL_PENDING_INLINE_INFO (member_function);
22276 DECL_PENDING_INLINE_INFO (member_function) = NULL;
22277 DECL_PENDING_INLINE_P (member_function) = 0;
22279 /* If this is a local class, enter the scope of the containing
22280 function. */
22281 function_scope = current_function_decl;
22282 if (function_scope)
22283 push_function_context ();
22285 /* Push the body of the function onto the lexer stack. */
22286 cp_parser_push_lexer_for_tokens (parser, tokens);
22288 /* Let the front end know that we going to be defining this
22289 function. */
22290 start_preparsed_function (member_function, NULL_TREE,
22291 SF_PRE_PARSED | SF_INCLASS_INLINE);
22293 /* Don't do access checking if it is a templated function. */
22294 if (processing_template_decl)
22295 push_deferring_access_checks (dk_no_check);
22297 /* Now, parse the body of the function. */
22298 cp_parser_function_definition_after_declarator (parser,
22299 /*inline_p=*/true);
22301 if (processing_template_decl)
22302 pop_deferring_access_checks ();
22304 /* Leave the scope of the containing function. */
22305 if (function_scope)
22306 pop_function_context ();
22307 cp_parser_pop_lexer (parser);
22310 /* Remove any template parameters from the symbol table. */
22311 maybe_end_member_template_processing ();
22313 /* Restore the queue. */
22314 pop_unparsed_function_queues (parser);
22315 timevar_pop (TV_PARSE_INMETH);
22318 /* If DECL contains any default args, remember it on the unparsed
22319 functions queue. */
22321 static void
22322 cp_parser_save_default_args (cp_parser* parser, tree decl)
22324 tree probe;
22326 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
22327 probe;
22328 probe = TREE_CHAIN (probe))
22329 if (TREE_PURPOSE (probe))
22331 cp_default_arg_entry entry = {current_class_type, decl};
22332 vec_safe_push (unparsed_funs_with_default_args, entry);
22333 break;
22337 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
22338 which is either a FIELD_DECL or PARM_DECL. Parse it and return
22339 the result. For a PARM_DECL, PARMTYPE is the corresponding type
22340 from the parameter-type-list. */
22342 static tree
22343 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
22344 tree default_arg, tree parmtype)
22346 cp_token_cache *tokens;
22347 tree parsed_arg;
22348 bool dummy;
22350 if (default_arg == error_mark_node)
22351 return error_mark_node;
22353 /* Push the saved tokens for the default argument onto the parser's
22354 lexer stack. */
22355 tokens = DEFARG_TOKENS (default_arg);
22356 cp_parser_push_lexer_for_tokens (parser, tokens);
22358 start_lambda_scope (decl);
22360 /* Parse the default argument. */
22361 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
22362 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
22363 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22365 finish_lambda_scope ();
22367 if (parsed_arg == error_mark_node)
22368 cp_parser_skip_to_end_of_statement (parser);
22370 if (!processing_template_decl)
22372 /* In a non-template class, check conversions now. In a template,
22373 we'll wait and instantiate these as needed. */
22374 if (TREE_CODE (decl) == PARM_DECL)
22375 parsed_arg = check_default_argument (parmtype, parsed_arg);
22376 else
22378 int flags = LOOKUP_IMPLICIT;
22379 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
22380 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
22381 flags = LOOKUP_NORMAL;
22382 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
22386 /* If the token stream has not been completely used up, then
22387 there was extra junk after the end of the default
22388 argument. */
22389 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22391 if (TREE_CODE (decl) == PARM_DECL)
22392 cp_parser_error (parser, "expected %<,%>");
22393 else
22394 cp_parser_error (parser, "expected %<;%>");
22397 /* Revert to the main lexer. */
22398 cp_parser_pop_lexer (parser);
22400 return parsed_arg;
22403 /* FIELD is a non-static data member with an initializer which we saved for
22404 later; parse it now. */
22406 static void
22407 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
22409 tree def;
22411 push_unparsed_function_queues (parser);
22412 def = cp_parser_late_parse_one_default_arg (parser, field,
22413 DECL_INITIAL (field),
22414 NULL_TREE);
22415 pop_unparsed_function_queues (parser);
22417 DECL_INITIAL (field) = def;
22420 /* FN is a FUNCTION_DECL which may contains a parameter with an
22421 unparsed DEFAULT_ARG. Parse the default args now. This function
22422 assumes that the current scope is the scope in which the default
22423 argument should be processed. */
22425 static void
22426 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
22428 bool saved_local_variables_forbidden_p;
22429 tree parm, parmdecl;
22431 /* While we're parsing the default args, we might (due to the
22432 statement expression extension) encounter more classes. We want
22433 to handle them right away, but we don't want them getting mixed
22434 up with default args that are currently in the queue. */
22435 push_unparsed_function_queues (parser);
22437 /* Local variable names (and the `this' keyword) may not appear
22438 in a default argument. */
22439 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22440 parser->local_variables_forbidden_p = true;
22442 push_defarg_context (fn);
22444 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
22445 parmdecl = DECL_ARGUMENTS (fn);
22446 parm && parm != void_list_node;
22447 parm = TREE_CHAIN (parm),
22448 parmdecl = DECL_CHAIN (parmdecl))
22450 tree default_arg = TREE_PURPOSE (parm);
22451 tree parsed_arg;
22452 vec<tree, va_gc> *insts;
22453 tree copy;
22454 unsigned ix;
22456 if (!default_arg)
22457 continue;
22459 if (TREE_CODE (default_arg) != DEFAULT_ARG)
22460 /* This can happen for a friend declaration for a function
22461 already declared with default arguments. */
22462 continue;
22464 parsed_arg
22465 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
22466 default_arg,
22467 TREE_VALUE (parm));
22468 if (parsed_arg == error_mark_node)
22470 continue;
22473 TREE_PURPOSE (parm) = parsed_arg;
22475 /* Update any instantiations we've already created. */
22476 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
22477 vec_safe_iterate (insts, ix, &copy); ix++)
22478 TREE_PURPOSE (copy) = parsed_arg;
22481 pop_defarg_context ();
22483 /* Make sure no default arg is missing. */
22484 check_default_args (fn);
22486 /* Restore the state of local_variables_forbidden_p. */
22487 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22489 /* Restore the queue. */
22490 pop_unparsed_function_queues (parser);
22493 /* Parse the operand of `sizeof' (or a similar operator). Returns
22494 either a TYPE or an expression, depending on the form of the
22495 input. The KEYWORD indicates which kind of expression we have
22496 encountered. */
22498 static tree
22499 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
22501 tree expr = NULL_TREE;
22502 const char *saved_message;
22503 char *tmp;
22504 bool saved_integral_constant_expression_p;
22505 bool saved_non_integral_constant_expression_p;
22506 bool pack_expansion_p = false;
22508 /* Types cannot be defined in a `sizeof' expression. Save away the
22509 old message. */
22510 saved_message = parser->type_definition_forbidden_message;
22511 /* And create the new one. */
22512 tmp = concat ("types may not be defined in %<",
22513 IDENTIFIER_POINTER (ridpointers[keyword]),
22514 "%> expressions", NULL);
22515 parser->type_definition_forbidden_message = tmp;
22517 /* The restrictions on constant-expressions do not apply inside
22518 sizeof expressions. */
22519 saved_integral_constant_expression_p
22520 = parser->integral_constant_expression_p;
22521 saved_non_integral_constant_expression_p
22522 = parser->non_integral_constant_expression_p;
22523 parser->integral_constant_expression_p = false;
22525 /* If it's a `...', then we are computing the length of a parameter
22526 pack. */
22527 if (keyword == RID_SIZEOF
22528 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22530 /* Consume the `...'. */
22531 cp_lexer_consume_token (parser->lexer);
22532 maybe_warn_variadic_templates ();
22534 /* Note that this is an expansion. */
22535 pack_expansion_p = true;
22538 /* Do not actually evaluate the expression. */
22539 ++cp_unevaluated_operand;
22540 ++c_inhibit_evaluation_warnings;
22541 /* If it's a `(', then we might be looking at the type-id
22542 construction. */
22543 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22545 tree type;
22546 bool saved_in_type_id_in_expr_p;
22548 /* We can't be sure yet whether we're looking at a type-id or an
22549 expression. */
22550 cp_parser_parse_tentatively (parser);
22551 /* Consume the `('. */
22552 cp_lexer_consume_token (parser->lexer);
22553 /* Parse the type-id. */
22554 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
22555 parser->in_type_id_in_expr_p = true;
22556 type = cp_parser_type_id (parser);
22557 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
22558 /* Now, look for the trailing `)'. */
22559 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22560 /* If all went well, then we're done. */
22561 if (cp_parser_parse_definitely (parser))
22563 cp_decl_specifier_seq decl_specs;
22565 /* Build a trivial decl-specifier-seq. */
22566 clear_decl_specs (&decl_specs);
22567 decl_specs.type = type;
22569 /* Call grokdeclarator to figure out what type this is. */
22570 expr = grokdeclarator (NULL,
22571 &decl_specs,
22572 TYPENAME,
22573 /*initialized=*/0,
22574 /*attrlist=*/NULL);
22577 else if (pack_expansion_p)
22578 permerror (cp_lexer_peek_token (parser->lexer)->location,
22579 "%<sizeof...%> argument must be surrounded by parentheses");
22581 /* If the type-id production did not work out, then we must be
22582 looking at the unary-expression production. */
22583 if (!expr)
22584 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
22585 /*cast_p=*/false, NULL);
22587 if (pack_expansion_p)
22588 /* Build a pack expansion. */
22589 expr = make_pack_expansion (expr);
22591 /* Go back to evaluating expressions. */
22592 --cp_unevaluated_operand;
22593 --c_inhibit_evaluation_warnings;
22595 /* Free the message we created. */
22596 free (tmp);
22597 /* And restore the old one. */
22598 parser->type_definition_forbidden_message = saved_message;
22599 parser->integral_constant_expression_p
22600 = saved_integral_constant_expression_p;
22601 parser->non_integral_constant_expression_p
22602 = saved_non_integral_constant_expression_p;
22604 return expr;
22607 /* If the current declaration has no declarator, return true. */
22609 static bool
22610 cp_parser_declares_only_class_p (cp_parser *parser)
22612 /* If the next token is a `;' or a `,' then there is no
22613 declarator. */
22614 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22615 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22618 /* Update the DECL_SPECS to reflect the storage class indicated by
22619 KEYWORD. */
22621 static void
22622 cp_parser_set_storage_class (cp_parser *parser,
22623 cp_decl_specifier_seq *decl_specs,
22624 enum rid keyword,
22625 cp_token *token)
22627 cp_storage_class storage_class;
22629 if (parser->in_unbraced_linkage_specification_p)
22631 error_at (token->location, "invalid use of %qD in linkage specification",
22632 ridpointers[keyword]);
22633 return;
22635 else if (decl_specs->storage_class != sc_none)
22637 decl_specs->conflicting_specifiers_p = true;
22638 return;
22641 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22642 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
22643 && decl_specs->gnu_thread_keyword_p)
22645 pedwarn (decl_specs->locations[ds_thread], 0,
22646 "%<__thread%> before %qD", ridpointers[keyword]);
22649 switch (keyword)
22651 case RID_AUTO:
22652 storage_class = sc_auto;
22653 break;
22654 case RID_REGISTER:
22655 storage_class = sc_register;
22656 break;
22657 case RID_STATIC:
22658 storage_class = sc_static;
22659 break;
22660 case RID_EXTERN:
22661 storage_class = sc_extern;
22662 break;
22663 case RID_MUTABLE:
22664 storage_class = sc_mutable;
22665 break;
22666 default:
22667 gcc_unreachable ();
22669 decl_specs->storage_class = storage_class;
22670 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
22672 /* A storage class specifier cannot be applied alongside a typedef
22673 specifier. If there is a typedef specifier present then set
22674 conflicting_specifiers_p which will trigger an error later
22675 on in grokdeclarator. */
22676 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
22677 decl_specs->conflicting_specifiers_p = true;
22680 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22681 is true, the type is a class or enum definition. */
22683 static void
22684 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22685 tree type_spec,
22686 cp_token *token,
22687 bool type_definition_p)
22689 decl_specs->any_specifiers_p = true;
22691 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22692 (with, for example, in "typedef int wchar_t;") we remember that
22693 this is what happened. In system headers, we ignore these
22694 declarations so that G++ can work with system headers that are not
22695 C++-safe. */
22696 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
22697 && !type_definition_p
22698 && (type_spec == boolean_type_node
22699 || type_spec == char16_type_node
22700 || type_spec == char32_type_node
22701 || type_spec == wchar_type_node)
22702 && (decl_specs->type
22703 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
22704 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
22705 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
22706 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
22708 decl_specs->redefined_builtin_type = type_spec;
22709 set_and_check_decl_spec_loc (decl_specs,
22710 ds_redefined_builtin_type_spec,
22711 token);
22712 if (!decl_specs->type)
22714 decl_specs->type = type_spec;
22715 decl_specs->type_definition_p = false;
22716 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
22719 else if (decl_specs->type)
22720 decl_specs->multiple_types_p = true;
22721 else
22723 decl_specs->type = type_spec;
22724 decl_specs->type_definition_p = type_definition_p;
22725 decl_specs->redefined_builtin_type = NULL_TREE;
22726 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
22730 /* True iff TOKEN is the GNU keyword __thread. */
22732 static bool
22733 token_is__thread (cp_token *token)
22735 gcc_assert (token->keyword == RID_THREAD);
22736 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
22739 /* Set the location for a declarator specifier and check if it is
22740 duplicated.
22742 DECL_SPECS is the sequence of declarator specifiers onto which to
22743 set the location.
22745 DS is the single declarator specifier to set which location is to
22746 be set onto the existing sequence of declarators.
22748 LOCATION is the location for the declarator specifier to
22749 consider. */
22751 static void
22752 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
22753 cp_decl_spec ds, cp_token *token)
22755 gcc_assert (ds < ds_last);
22757 if (decl_specs == NULL)
22758 return;
22760 source_location location = token->location;
22762 if (decl_specs->locations[ds] == 0)
22764 decl_specs->locations[ds] = location;
22765 if (ds == ds_thread)
22766 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
22768 else
22770 if (ds == ds_long)
22772 if (decl_specs->locations[ds_long_long] != 0)
22773 error_at (location,
22774 "%<long long long%> is too long for GCC");
22775 else
22777 decl_specs->locations[ds_long_long] = location;
22778 pedwarn_cxx98 (location,
22779 OPT_Wlong_long,
22780 "ISO C++ 1998 does not support %<long long%>");
22783 else if (ds == ds_thread)
22785 bool gnu = token_is__thread (token);
22786 if (gnu != decl_specs->gnu_thread_keyword_p)
22787 error_at (location,
22788 "both %<__thread%> and %<thread_local%> specified");
22789 else
22790 error_at (location, "duplicate %qD", token->u.value);
22792 else
22794 static const char *const decl_spec_names[] = {
22795 "signed",
22796 "unsigned",
22797 "short",
22798 "long",
22799 "const",
22800 "volatile",
22801 "restrict",
22802 "inline",
22803 "virtual",
22804 "explicit",
22805 "friend",
22806 "typedef",
22807 "using",
22808 "constexpr",
22809 "__complex"
22811 error_at (location,
22812 "duplicate %qs", decl_spec_names[ds]);
22817 /* Return true iff the declarator specifier DS is present in the
22818 sequence of declarator specifiers DECL_SPECS. */
22820 bool
22821 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
22822 cp_decl_spec ds)
22824 gcc_assert (ds < ds_last);
22826 if (decl_specs == NULL)
22827 return false;
22829 return decl_specs->locations[ds] != 0;
22832 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22833 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22835 static bool
22836 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22838 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
22841 /* Issue an error message indicating that TOKEN_DESC was expected.
22842 If KEYWORD is true, it indicated this function is called by
22843 cp_parser_require_keword and the required token can only be
22844 a indicated keyword. */
22846 static void
22847 cp_parser_required_error (cp_parser *parser,
22848 required_token token_desc,
22849 bool keyword)
22851 switch (token_desc)
22853 case RT_NEW:
22854 cp_parser_error (parser, "expected %<new%>");
22855 return;
22856 case RT_DELETE:
22857 cp_parser_error (parser, "expected %<delete%>");
22858 return;
22859 case RT_RETURN:
22860 cp_parser_error (parser, "expected %<return%>");
22861 return;
22862 case RT_WHILE:
22863 cp_parser_error (parser, "expected %<while%>");
22864 return;
22865 case RT_EXTERN:
22866 cp_parser_error (parser, "expected %<extern%>");
22867 return;
22868 case RT_STATIC_ASSERT:
22869 cp_parser_error (parser, "expected %<static_assert%>");
22870 return;
22871 case RT_DECLTYPE:
22872 cp_parser_error (parser, "expected %<decltype%>");
22873 return;
22874 case RT_OPERATOR:
22875 cp_parser_error (parser, "expected %<operator%>");
22876 return;
22877 case RT_CLASS:
22878 cp_parser_error (parser, "expected %<class%>");
22879 return;
22880 case RT_TEMPLATE:
22881 cp_parser_error (parser, "expected %<template%>");
22882 return;
22883 case RT_NAMESPACE:
22884 cp_parser_error (parser, "expected %<namespace%>");
22885 return;
22886 case RT_USING:
22887 cp_parser_error (parser, "expected %<using%>");
22888 return;
22889 case RT_ASM:
22890 cp_parser_error (parser, "expected %<asm%>");
22891 return;
22892 case RT_TRY:
22893 cp_parser_error (parser, "expected %<try%>");
22894 return;
22895 case RT_CATCH:
22896 cp_parser_error (parser, "expected %<catch%>");
22897 return;
22898 case RT_THROW:
22899 cp_parser_error (parser, "expected %<throw%>");
22900 return;
22901 case RT_LABEL:
22902 cp_parser_error (parser, "expected %<__label__%>");
22903 return;
22904 case RT_AT_TRY:
22905 cp_parser_error (parser, "expected %<@try%>");
22906 return;
22907 case RT_AT_SYNCHRONIZED:
22908 cp_parser_error (parser, "expected %<@synchronized%>");
22909 return;
22910 case RT_AT_THROW:
22911 cp_parser_error (parser, "expected %<@throw%>");
22912 return;
22913 case RT_TRANSACTION_ATOMIC:
22914 cp_parser_error (parser, "expected %<__transaction_atomic%>");
22915 return;
22916 case RT_TRANSACTION_RELAXED:
22917 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22918 return;
22919 default:
22920 break;
22922 if (!keyword)
22924 switch (token_desc)
22926 case RT_SEMICOLON:
22927 cp_parser_error (parser, "expected %<;%>");
22928 return;
22929 case RT_OPEN_PAREN:
22930 cp_parser_error (parser, "expected %<(%>");
22931 return;
22932 case RT_CLOSE_BRACE:
22933 cp_parser_error (parser, "expected %<}%>");
22934 return;
22935 case RT_OPEN_BRACE:
22936 cp_parser_error (parser, "expected %<{%>");
22937 return;
22938 case RT_CLOSE_SQUARE:
22939 cp_parser_error (parser, "expected %<]%>");
22940 return;
22941 case RT_OPEN_SQUARE:
22942 cp_parser_error (parser, "expected %<[%>");
22943 return;
22944 case RT_COMMA:
22945 cp_parser_error (parser, "expected %<,%>");
22946 return;
22947 case RT_SCOPE:
22948 cp_parser_error (parser, "expected %<::%>");
22949 return;
22950 case RT_LESS:
22951 cp_parser_error (parser, "expected %<<%>");
22952 return;
22953 case RT_GREATER:
22954 cp_parser_error (parser, "expected %<>%>");
22955 return;
22956 case RT_EQ:
22957 cp_parser_error (parser, "expected %<=%>");
22958 return;
22959 case RT_ELLIPSIS:
22960 cp_parser_error (parser, "expected %<...%>");
22961 return;
22962 case RT_MULT:
22963 cp_parser_error (parser, "expected %<*%>");
22964 return;
22965 case RT_COMPL:
22966 cp_parser_error (parser, "expected %<~%>");
22967 return;
22968 case RT_COLON:
22969 cp_parser_error (parser, "expected %<:%>");
22970 return;
22971 case RT_COLON_SCOPE:
22972 cp_parser_error (parser, "expected %<:%> or %<::%>");
22973 return;
22974 case RT_CLOSE_PAREN:
22975 cp_parser_error (parser, "expected %<)%>");
22976 return;
22977 case RT_COMMA_CLOSE_PAREN:
22978 cp_parser_error (parser, "expected %<,%> or %<)%>");
22979 return;
22980 case RT_PRAGMA_EOL:
22981 cp_parser_error (parser, "expected end of line");
22982 return;
22983 case RT_NAME:
22984 cp_parser_error (parser, "expected identifier");
22985 return;
22986 case RT_SELECT:
22987 cp_parser_error (parser, "expected selection-statement");
22988 return;
22989 case RT_INTERATION:
22990 cp_parser_error (parser, "expected iteration-statement");
22991 return;
22992 case RT_JUMP:
22993 cp_parser_error (parser, "expected jump-statement");
22994 return;
22995 case RT_CLASS_KEY:
22996 cp_parser_error (parser, "expected class-key");
22997 return;
22998 case RT_CLASS_TYPENAME_TEMPLATE:
22999 cp_parser_error (parser,
23000 "expected %<class%>, %<typename%>, or %<template%>");
23001 return;
23002 default:
23003 gcc_unreachable ();
23006 else
23007 gcc_unreachable ();
23012 /* If the next token is of the indicated TYPE, consume it. Otherwise,
23013 issue an error message indicating that TOKEN_DESC was expected.
23015 Returns the token consumed, if the token had the appropriate type.
23016 Otherwise, returns NULL. */
23018 static cp_token *
23019 cp_parser_require (cp_parser* parser,
23020 enum cpp_ttype type,
23021 required_token token_desc)
23023 if (cp_lexer_next_token_is (parser->lexer, type))
23024 return cp_lexer_consume_token (parser->lexer);
23025 else
23027 /* Output the MESSAGE -- unless we're parsing tentatively. */
23028 if (!cp_parser_simulate_error (parser))
23029 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
23030 return NULL;
23034 /* An error message is produced if the next token is not '>'.
23035 All further tokens are skipped until the desired token is
23036 found or '{', '}', ';' or an unbalanced ')' or ']'. */
23038 static void
23039 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
23041 /* Current level of '< ... >'. */
23042 unsigned level = 0;
23043 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
23044 unsigned nesting_depth = 0;
23046 /* Are we ready, yet? If not, issue error message. */
23047 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
23048 return;
23050 /* Skip tokens until the desired token is found. */
23051 while (true)
23053 /* Peek at the next token. */
23054 switch (cp_lexer_peek_token (parser->lexer)->type)
23056 case CPP_LESS:
23057 if (!nesting_depth)
23058 ++level;
23059 break;
23061 case CPP_RSHIFT:
23062 if (cxx_dialect == cxx98)
23063 /* C++0x views the `>>' operator as two `>' tokens, but
23064 C++98 does not. */
23065 break;
23066 else if (!nesting_depth && level-- == 0)
23068 /* We've hit a `>>' where the first `>' closes the
23069 template argument list, and the second `>' is
23070 spurious. Just consume the `>>' and stop; we've
23071 already produced at least one error. */
23072 cp_lexer_consume_token (parser->lexer);
23073 return;
23075 /* Fall through for C++0x, so we handle the second `>' in
23076 the `>>'. */
23078 case CPP_GREATER:
23079 if (!nesting_depth && level-- == 0)
23081 /* We've reached the token we want, consume it and stop. */
23082 cp_lexer_consume_token (parser->lexer);
23083 return;
23085 break;
23087 case CPP_OPEN_PAREN:
23088 case CPP_OPEN_SQUARE:
23089 ++nesting_depth;
23090 break;
23092 case CPP_CLOSE_PAREN:
23093 case CPP_CLOSE_SQUARE:
23094 if (nesting_depth-- == 0)
23095 return;
23096 break;
23098 case CPP_EOF:
23099 case CPP_PRAGMA_EOL:
23100 case CPP_SEMICOLON:
23101 case CPP_OPEN_BRACE:
23102 case CPP_CLOSE_BRACE:
23103 /* The '>' was probably forgotten, don't look further. */
23104 return;
23106 default:
23107 break;
23110 /* Consume this token. */
23111 cp_lexer_consume_token (parser->lexer);
23115 /* If the next token is the indicated keyword, consume it. Otherwise,
23116 issue an error message indicating that TOKEN_DESC was expected.
23118 Returns the token consumed, if the token had the appropriate type.
23119 Otherwise, returns NULL. */
23121 static cp_token *
23122 cp_parser_require_keyword (cp_parser* parser,
23123 enum rid keyword,
23124 required_token token_desc)
23126 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
23128 if (token && token->keyword != keyword)
23130 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
23131 return NULL;
23134 return token;
23137 /* Returns TRUE iff TOKEN is a token that can begin the body of a
23138 function-definition. */
23140 static bool
23141 cp_parser_token_starts_function_definition_p (cp_token* token)
23143 return (/* An ordinary function-body begins with an `{'. */
23144 token->type == CPP_OPEN_BRACE
23145 /* A ctor-initializer begins with a `:'. */
23146 || token->type == CPP_COLON
23147 /* A function-try-block begins with `try'. */
23148 || token->keyword == RID_TRY
23149 /* A function-transaction-block begins with `__transaction_atomic'
23150 or `__transaction_relaxed'. */
23151 || token->keyword == RID_TRANSACTION_ATOMIC
23152 || token->keyword == RID_TRANSACTION_RELAXED
23153 /* The named return value extension begins with `return'. */
23154 || token->keyword == RID_RETURN);
23157 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
23158 definition. */
23160 static bool
23161 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
23163 cp_token *token;
23165 token = cp_lexer_peek_token (parser->lexer);
23166 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
23169 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
23170 C++0x) ending a template-argument. */
23172 static bool
23173 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
23175 cp_token *token;
23177 token = cp_lexer_peek_token (parser->lexer);
23178 return (token->type == CPP_COMMA
23179 || token->type == CPP_GREATER
23180 || token->type == CPP_ELLIPSIS
23181 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
23184 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
23185 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
23187 static bool
23188 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
23189 size_t n)
23191 cp_token *token;
23193 token = cp_lexer_peek_nth_token (parser->lexer, n);
23194 if (token->type == CPP_LESS)
23195 return true;
23196 /* Check for the sequence `<::' in the original code. It would be lexed as
23197 `[:', where `[' is a digraph, and there is no whitespace before
23198 `:'. */
23199 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
23201 cp_token *token2;
23202 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
23203 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
23204 return true;
23206 return false;
23209 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
23210 or none_type otherwise. */
23212 static enum tag_types
23213 cp_parser_token_is_class_key (cp_token* token)
23215 switch (token->keyword)
23217 case RID_CLASS:
23218 return class_type;
23219 case RID_STRUCT:
23220 return record_type;
23221 case RID_UNION:
23222 return union_type;
23224 default:
23225 return none_type;
23229 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
23231 static void
23232 cp_parser_check_class_key (enum tag_types class_key, tree type)
23234 if (type == error_mark_node)
23235 return;
23236 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
23238 permerror (input_location, "%qs tag used in naming %q#T",
23239 class_key == union_type ? "union"
23240 : class_key == record_type ? "struct" : "class",
23241 type);
23242 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
23243 "%q#T was previously declared here", type);
23247 /* Issue an error message if DECL is redeclared with different
23248 access than its original declaration [class.access.spec/3].
23249 This applies to nested classes and nested class templates.
23250 [class.mem/1]. */
23252 static void
23253 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
23255 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
23256 return;
23258 if ((TREE_PRIVATE (decl)
23259 != (current_access_specifier == access_private_node))
23260 || (TREE_PROTECTED (decl)
23261 != (current_access_specifier == access_protected_node)))
23262 error_at (location, "%qD redeclared with different access", decl);
23265 /* Look for the `template' keyword, as a syntactic disambiguator.
23266 Return TRUE iff it is present, in which case it will be
23267 consumed. */
23269 static bool
23270 cp_parser_optional_template_keyword (cp_parser *parser)
23272 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23274 /* In C++98 the `template' keyword can only be used within templates;
23275 outside templates the parser can always figure out what is a
23276 template and what is not. In C++11, per the resolution of DR 468,
23277 `template' is allowed in cases where it is not strictly necessary. */
23278 if (!processing_template_decl
23279 && pedantic && cxx_dialect == cxx98)
23281 cp_token *token = cp_lexer_peek_token (parser->lexer);
23282 pedwarn (token->location, OPT_Wpedantic,
23283 "in C++98 %<template%> (as a disambiguator) is only "
23284 "allowed within templates");
23285 /* If this part of the token stream is rescanned, the same
23286 error message would be generated. So, we purge the token
23287 from the stream. */
23288 cp_lexer_purge_token (parser->lexer);
23289 return false;
23291 else
23293 /* Consume the `template' keyword. */
23294 cp_lexer_consume_token (parser->lexer);
23295 return true;
23298 return false;
23301 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
23302 set PARSER->SCOPE, and perform other related actions. */
23304 static void
23305 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
23307 int i;
23308 struct tree_check *check_value;
23309 deferred_access_check *chk;
23310 vec<deferred_access_check, va_gc> *checks;
23312 /* Get the stored value. */
23313 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
23314 /* Perform any access checks that were deferred. */
23315 checks = check_value->checks;
23316 if (checks)
23318 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
23319 perform_or_defer_access_check (chk->binfo,
23320 chk->decl,
23321 chk->diag_decl, tf_warning_or_error);
23323 /* Set the scope from the stored value. */
23324 parser->scope = check_value->value;
23325 parser->qualifying_scope = check_value->qualifying_scope;
23326 parser->object_scope = NULL_TREE;
23329 /* Consume tokens up through a non-nested END token. Returns TRUE if we
23330 encounter the end of a block before what we were looking for. */
23332 static bool
23333 cp_parser_cache_group (cp_parser *parser,
23334 enum cpp_ttype end,
23335 unsigned depth)
23337 while (true)
23339 cp_token *token = cp_lexer_peek_token (parser->lexer);
23341 /* Abort a parenthesized expression if we encounter a semicolon. */
23342 if ((end == CPP_CLOSE_PAREN || depth == 0)
23343 && token->type == CPP_SEMICOLON)
23344 return true;
23345 /* If we've reached the end of the file, stop. */
23346 if (token->type == CPP_EOF
23347 || (end != CPP_PRAGMA_EOL
23348 && token->type == CPP_PRAGMA_EOL))
23349 return true;
23350 if (token->type == CPP_CLOSE_BRACE && depth == 0)
23351 /* We've hit the end of an enclosing block, so there's been some
23352 kind of syntax error. */
23353 return true;
23355 /* Consume the token. */
23356 cp_lexer_consume_token (parser->lexer);
23357 /* See if it starts a new group. */
23358 if (token->type == CPP_OPEN_BRACE)
23360 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
23361 /* In theory this should probably check end == '}', but
23362 cp_parser_save_member_function_body needs it to exit
23363 after either '}' or ')' when called with ')'. */
23364 if (depth == 0)
23365 return false;
23367 else if (token->type == CPP_OPEN_PAREN)
23369 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
23370 if (depth == 0 && end == CPP_CLOSE_PAREN)
23371 return false;
23373 else if (token->type == CPP_PRAGMA)
23374 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
23375 else if (token->type == end)
23376 return false;
23380 /* Like above, for caching a default argument or NSDMI. Both of these are
23381 terminated by a non-nested comma, but it can be unclear whether or not a
23382 comma is nested in a template argument list unless we do more parsing.
23383 In order to handle this ambiguity, when we encounter a ',' after a '<'
23384 we try to parse what follows as a parameter-declaration-list (in the
23385 case of a default argument) or a member-declarator (in the case of an
23386 NSDMI). If that succeeds, then we stop caching. */
23388 static tree
23389 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
23391 unsigned depth = 0;
23392 int maybe_template_id = 0;
23393 cp_token *first_token;
23394 cp_token *token;
23395 tree default_argument;
23397 /* Add tokens until we have processed the entire default
23398 argument. We add the range [first_token, token). */
23399 first_token = cp_lexer_peek_token (parser->lexer);
23400 if (first_token->type == CPP_OPEN_BRACE)
23402 /* For list-initialization, this is straightforward. */
23403 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23404 token = cp_lexer_peek_token (parser->lexer);
23406 else while (true)
23408 bool done = false;
23410 /* Peek at the next token. */
23411 token = cp_lexer_peek_token (parser->lexer);
23412 /* What we do depends on what token we have. */
23413 switch (token->type)
23415 /* In valid code, a default argument must be
23416 immediately followed by a `,' `)', or `...'. */
23417 case CPP_COMMA:
23418 if (depth == 0 && maybe_template_id)
23420 /* If we've seen a '<', we might be in a
23421 template-argument-list. Until Core issue 325 is
23422 resolved, we don't know how this situation ought
23423 to be handled, so try to DTRT. We check whether
23424 what comes after the comma is a valid parameter
23425 declaration list. If it is, then the comma ends
23426 the default argument; otherwise the default
23427 argument continues. */
23428 bool error = false;
23429 tree t;
23431 /* Set ITALP so cp_parser_parameter_declaration_list
23432 doesn't decide to commit to this parse. */
23433 bool saved_italp = parser->in_template_argument_list_p;
23434 parser->in_template_argument_list_p = true;
23436 cp_parser_parse_tentatively (parser);
23437 cp_lexer_consume_token (parser->lexer);
23439 if (nsdmi)
23441 int ctor_dtor_or_conv_p;
23442 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23443 &ctor_dtor_or_conv_p,
23444 /*parenthesized_p=*/NULL,
23445 /*member_p=*/true);
23447 else
23449 begin_scope (sk_function_parms, NULL_TREE);
23450 cp_parser_parameter_declaration_list (parser, &error);
23451 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
23452 pop_binding (DECL_NAME (t), t);
23453 leave_scope ();
23455 if (!cp_parser_error_occurred (parser) && !error)
23456 done = true;
23457 cp_parser_abort_tentative_parse (parser);
23459 parser->in_template_argument_list_p = saved_italp;
23460 break;
23462 case CPP_CLOSE_PAREN:
23463 case CPP_ELLIPSIS:
23464 /* If we run into a non-nested `;', `}', or `]',
23465 then the code is invalid -- but the default
23466 argument is certainly over. */
23467 case CPP_SEMICOLON:
23468 case CPP_CLOSE_BRACE:
23469 case CPP_CLOSE_SQUARE:
23470 if (depth == 0)
23471 done = true;
23472 /* Update DEPTH, if necessary. */
23473 else if (token->type == CPP_CLOSE_PAREN
23474 || token->type == CPP_CLOSE_BRACE
23475 || token->type == CPP_CLOSE_SQUARE)
23476 --depth;
23477 break;
23479 case CPP_OPEN_PAREN:
23480 case CPP_OPEN_SQUARE:
23481 case CPP_OPEN_BRACE:
23482 ++depth;
23483 break;
23485 case CPP_LESS:
23486 if (depth == 0)
23487 /* This might be the comparison operator, or it might
23488 start a template argument list. */
23489 ++maybe_template_id;
23490 break;
23492 case CPP_RSHIFT:
23493 if (cxx_dialect == cxx98)
23494 break;
23495 /* Fall through for C++0x, which treats the `>>'
23496 operator like two `>' tokens in certain
23497 cases. */
23499 case CPP_GREATER:
23500 if (depth == 0)
23502 /* This might be an operator, or it might close a
23503 template argument list. But if a previous '<'
23504 started a template argument list, this will have
23505 closed it, so we can't be in one anymore. */
23506 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
23507 if (maybe_template_id < 0)
23508 maybe_template_id = 0;
23510 break;
23512 /* If we run out of tokens, issue an error message. */
23513 case CPP_EOF:
23514 case CPP_PRAGMA_EOL:
23515 error_at (token->location, "file ends in default argument");
23516 done = true;
23517 break;
23519 case CPP_NAME:
23520 case CPP_SCOPE:
23521 /* In these cases, we should look for template-ids.
23522 For example, if the default argument is
23523 `X<int, double>()', we need to do name lookup to
23524 figure out whether or not `X' is a template; if
23525 so, the `,' does not end the default argument.
23527 That is not yet done. */
23528 break;
23530 default:
23531 break;
23534 /* If we've reached the end, stop. */
23535 if (done)
23536 break;
23538 /* Add the token to the token block. */
23539 token = cp_lexer_consume_token (parser->lexer);
23542 /* Create a DEFAULT_ARG to represent the unparsed default
23543 argument. */
23544 default_argument = make_node (DEFAULT_ARG);
23545 DEFARG_TOKENS (default_argument)
23546 = cp_token_cache_new (first_token, token);
23547 DEFARG_INSTANTIATIONS (default_argument) = NULL;
23549 return default_argument;
23552 /* Begin parsing tentatively. We always save tokens while parsing
23553 tentatively so that if the tentative parsing fails we can restore the
23554 tokens. */
23556 static void
23557 cp_parser_parse_tentatively (cp_parser* parser)
23559 /* Enter a new parsing context. */
23560 parser->context = cp_parser_context_new (parser->context);
23561 /* Begin saving tokens. */
23562 cp_lexer_save_tokens (parser->lexer);
23563 /* In order to avoid repetitive access control error messages,
23564 access checks are queued up until we are no longer parsing
23565 tentatively. */
23566 push_deferring_access_checks (dk_deferred);
23569 /* Commit to the currently active tentative parse. */
23571 static void
23572 cp_parser_commit_to_tentative_parse (cp_parser* parser)
23574 cp_parser_context *context;
23575 cp_lexer *lexer;
23577 /* Mark all of the levels as committed. */
23578 lexer = parser->lexer;
23579 for (context = parser->context; context->next; context = context->next)
23581 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
23582 break;
23583 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
23584 while (!cp_lexer_saving_tokens (lexer))
23585 lexer = lexer->next;
23586 cp_lexer_commit_tokens (lexer);
23590 /* Abort the currently active tentative parse. All consumed tokens
23591 will be rolled back, and no diagnostics will be issued. */
23593 static void
23594 cp_parser_abort_tentative_parse (cp_parser* parser)
23596 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
23597 || errorcount > 0);
23598 cp_parser_simulate_error (parser);
23599 /* Now, pretend that we want to see if the construct was
23600 successfully parsed. */
23601 cp_parser_parse_definitely (parser);
23604 /* Stop parsing tentatively. If a parse error has occurred, restore the
23605 token stream. Otherwise, commit to the tokens we have consumed.
23606 Returns true if no error occurred; false otherwise. */
23608 static bool
23609 cp_parser_parse_definitely (cp_parser* parser)
23611 bool error_occurred;
23612 cp_parser_context *context;
23614 /* Remember whether or not an error occurred, since we are about to
23615 destroy that information. */
23616 error_occurred = cp_parser_error_occurred (parser);
23617 /* Remove the topmost context from the stack. */
23618 context = parser->context;
23619 parser->context = context->next;
23620 /* If no parse errors occurred, commit to the tentative parse. */
23621 if (!error_occurred)
23623 /* Commit to the tokens read tentatively, unless that was
23624 already done. */
23625 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
23626 cp_lexer_commit_tokens (parser->lexer);
23628 pop_to_parent_deferring_access_checks ();
23630 /* Otherwise, if errors occurred, roll back our state so that things
23631 are just as they were before we began the tentative parse. */
23632 else
23634 cp_lexer_rollback_tokens (parser->lexer);
23635 pop_deferring_access_checks ();
23637 /* Add the context to the front of the free list. */
23638 context->next = cp_parser_context_free_list;
23639 cp_parser_context_free_list = context;
23641 return !error_occurred;
23644 /* Returns true if we are parsing tentatively and are not committed to
23645 this tentative parse. */
23647 static bool
23648 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
23650 return (cp_parser_parsing_tentatively (parser)
23651 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
23654 /* Returns nonzero iff an error has occurred during the most recent
23655 tentative parse. */
23657 static bool
23658 cp_parser_error_occurred (cp_parser* parser)
23660 return (cp_parser_parsing_tentatively (parser)
23661 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
23664 /* Returns nonzero if GNU extensions are allowed. */
23666 static bool
23667 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
23669 return parser->allow_gnu_extensions_p;
23672 /* Objective-C++ Productions */
23675 /* Parse an Objective-C expression, which feeds into a primary-expression
23676 above.
23678 objc-expression:
23679 objc-message-expression
23680 objc-string-literal
23681 objc-encode-expression
23682 objc-protocol-expression
23683 objc-selector-expression
23685 Returns a tree representation of the expression. */
23687 static tree
23688 cp_parser_objc_expression (cp_parser* parser)
23690 /* Try to figure out what kind of declaration is present. */
23691 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23693 switch (kwd->type)
23695 case CPP_OPEN_SQUARE:
23696 return cp_parser_objc_message_expression (parser);
23698 case CPP_OBJC_STRING:
23699 kwd = cp_lexer_consume_token (parser->lexer);
23700 return objc_build_string_object (kwd->u.value);
23702 case CPP_KEYWORD:
23703 switch (kwd->keyword)
23705 case RID_AT_ENCODE:
23706 return cp_parser_objc_encode_expression (parser);
23708 case RID_AT_PROTOCOL:
23709 return cp_parser_objc_protocol_expression (parser);
23711 case RID_AT_SELECTOR:
23712 return cp_parser_objc_selector_expression (parser);
23714 default:
23715 break;
23717 default:
23718 error_at (kwd->location,
23719 "misplaced %<@%D%> Objective-C++ construct",
23720 kwd->u.value);
23721 cp_parser_skip_to_end_of_block_or_statement (parser);
23724 return error_mark_node;
23727 /* Parse an Objective-C message expression.
23729 objc-message-expression:
23730 [ objc-message-receiver objc-message-args ]
23732 Returns a representation of an Objective-C message. */
23734 static tree
23735 cp_parser_objc_message_expression (cp_parser* parser)
23737 tree receiver, messageargs;
23739 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
23740 receiver = cp_parser_objc_message_receiver (parser);
23741 messageargs = cp_parser_objc_message_args (parser);
23742 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23744 return objc_build_message_expr (receiver, messageargs);
23747 /* Parse an objc-message-receiver.
23749 objc-message-receiver:
23750 expression
23751 simple-type-specifier
23753 Returns a representation of the type or expression. */
23755 static tree
23756 cp_parser_objc_message_receiver (cp_parser* parser)
23758 tree rcv;
23760 /* An Objective-C message receiver may be either (1) a type
23761 or (2) an expression. */
23762 cp_parser_parse_tentatively (parser);
23763 rcv = cp_parser_expression (parser, false, NULL);
23765 if (cp_parser_parse_definitely (parser))
23766 return rcv;
23768 rcv = cp_parser_simple_type_specifier (parser,
23769 /*decl_specs=*/NULL,
23770 CP_PARSER_FLAGS_NONE);
23772 return objc_get_class_reference (rcv);
23775 /* Parse the arguments and selectors comprising an Objective-C message.
23777 objc-message-args:
23778 objc-selector
23779 objc-selector-args
23780 objc-selector-args , objc-comma-args
23782 objc-selector-args:
23783 objc-selector [opt] : assignment-expression
23784 objc-selector-args objc-selector [opt] : assignment-expression
23786 objc-comma-args:
23787 assignment-expression
23788 objc-comma-args , assignment-expression
23790 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23791 selector arguments and TREE_VALUE containing a list of comma
23792 arguments. */
23794 static tree
23795 cp_parser_objc_message_args (cp_parser* parser)
23797 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23798 bool maybe_unary_selector_p = true;
23799 cp_token *token = cp_lexer_peek_token (parser->lexer);
23801 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23803 tree selector = NULL_TREE, arg;
23805 if (token->type != CPP_COLON)
23806 selector = cp_parser_objc_selector (parser);
23808 /* Detect if we have a unary selector. */
23809 if (maybe_unary_selector_p
23810 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23811 return build_tree_list (selector, NULL_TREE);
23813 maybe_unary_selector_p = false;
23814 cp_parser_require (parser, CPP_COLON, RT_COLON);
23815 arg = cp_parser_assignment_expression (parser, false, NULL);
23817 sel_args
23818 = chainon (sel_args,
23819 build_tree_list (selector, arg));
23821 token = cp_lexer_peek_token (parser->lexer);
23824 /* Handle non-selector arguments, if any. */
23825 while (token->type == CPP_COMMA)
23827 tree arg;
23829 cp_lexer_consume_token (parser->lexer);
23830 arg = cp_parser_assignment_expression (parser, false, NULL);
23832 addl_args
23833 = chainon (addl_args,
23834 build_tree_list (NULL_TREE, arg));
23836 token = cp_lexer_peek_token (parser->lexer);
23839 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23841 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23842 return build_tree_list (error_mark_node, error_mark_node);
23845 return build_tree_list (sel_args, addl_args);
23848 /* Parse an Objective-C encode expression.
23850 objc-encode-expression:
23851 @encode objc-typename
23853 Returns an encoded representation of the type argument. */
23855 static tree
23856 cp_parser_objc_encode_expression (cp_parser* parser)
23858 tree type;
23859 cp_token *token;
23861 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
23862 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23863 token = cp_lexer_peek_token (parser->lexer);
23864 type = complete_type (cp_parser_type_id (parser));
23865 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23867 if (!type)
23869 error_at (token->location,
23870 "%<@encode%> must specify a type as an argument");
23871 return error_mark_node;
23874 /* This happens if we find @encode(T) (where T is a template
23875 typename or something dependent on a template typename) when
23876 parsing a template. In that case, we can't compile it
23877 immediately, but we rather create an AT_ENCODE_EXPR which will
23878 need to be instantiated when the template is used.
23880 if (dependent_type_p (type))
23882 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23883 TREE_READONLY (value) = 1;
23884 return value;
23887 return objc_build_encode_expr (type);
23890 /* Parse an Objective-C @defs expression. */
23892 static tree
23893 cp_parser_objc_defs_expression (cp_parser *parser)
23895 tree name;
23897 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
23898 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23899 name = cp_parser_identifier (parser);
23900 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23902 return objc_get_class_ivars (name);
23905 /* Parse an Objective-C protocol expression.
23907 objc-protocol-expression:
23908 @protocol ( identifier )
23910 Returns a representation of the protocol expression. */
23912 static tree
23913 cp_parser_objc_protocol_expression (cp_parser* parser)
23915 tree proto;
23917 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23918 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23919 proto = cp_parser_identifier (parser);
23920 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23922 return objc_build_protocol_expr (proto);
23925 /* Parse an Objective-C selector expression.
23927 objc-selector-expression:
23928 @selector ( objc-method-signature )
23930 objc-method-signature:
23931 objc-selector
23932 objc-selector-seq
23934 objc-selector-seq:
23935 objc-selector :
23936 objc-selector-seq objc-selector :
23938 Returns a representation of the method selector. */
23940 static tree
23941 cp_parser_objc_selector_expression (cp_parser* parser)
23943 tree sel_seq = NULL_TREE;
23944 bool maybe_unary_selector_p = true;
23945 cp_token *token;
23946 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23948 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
23949 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23950 token = cp_lexer_peek_token (parser->lexer);
23952 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23953 || token->type == CPP_SCOPE)
23955 tree selector = NULL_TREE;
23957 if (token->type != CPP_COLON
23958 || token->type == CPP_SCOPE)
23959 selector = cp_parser_objc_selector (parser);
23961 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23962 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23964 /* Detect if we have a unary selector. */
23965 if (maybe_unary_selector_p)
23967 sel_seq = selector;
23968 goto finish_selector;
23970 else
23972 cp_parser_error (parser, "expected %<:%>");
23975 maybe_unary_selector_p = false;
23976 token = cp_lexer_consume_token (parser->lexer);
23978 if (token->type == CPP_SCOPE)
23980 sel_seq
23981 = chainon (sel_seq,
23982 build_tree_list (selector, NULL_TREE));
23983 sel_seq
23984 = chainon (sel_seq,
23985 build_tree_list (NULL_TREE, NULL_TREE));
23987 else
23988 sel_seq
23989 = chainon (sel_seq,
23990 build_tree_list (selector, NULL_TREE));
23992 token = cp_lexer_peek_token (parser->lexer);
23995 finish_selector:
23996 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23998 return objc_build_selector_expr (loc, sel_seq);
24001 /* Parse a list of identifiers.
24003 objc-identifier-list:
24004 identifier
24005 objc-identifier-list , identifier
24007 Returns a TREE_LIST of identifier nodes. */
24009 static tree
24010 cp_parser_objc_identifier_list (cp_parser* parser)
24012 tree identifier;
24013 tree list;
24014 cp_token *sep;
24016 identifier = cp_parser_identifier (parser);
24017 if (identifier == error_mark_node)
24018 return error_mark_node;
24020 list = build_tree_list (NULL_TREE, identifier);
24021 sep = cp_lexer_peek_token (parser->lexer);
24023 while (sep->type == CPP_COMMA)
24025 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24026 identifier = cp_parser_identifier (parser);
24027 if (identifier == error_mark_node)
24028 return list;
24030 list = chainon (list, build_tree_list (NULL_TREE,
24031 identifier));
24032 sep = cp_lexer_peek_token (parser->lexer);
24035 return list;
24038 /* Parse an Objective-C alias declaration.
24040 objc-alias-declaration:
24041 @compatibility_alias identifier identifier ;
24043 This function registers the alias mapping with the Objective-C front end.
24044 It returns nothing. */
24046 static void
24047 cp_parser_objc_alias_declaration (cp_parser* parser)
24049 tree alias, orig;
24051 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
24052 alias = cp_parser_identifier (parser);
24053 orig = cp_parser_identifier (parser);
24054 objc_declare_alias (alias, orig);
24055 cp_parser_consume_semicolon_at_end_of_statement (parser);
24058 /* Parse an Objective-C class forward-declaration.
24060 objc-class-declaration:
24061 @class objc-identifier-list ;
24063 The function registers the forward declarations with the Objective-C
24064 front end. It returns nothing. */
24066 static void
24067 cp_parser_objc_class_declaration (cp_parser* parser)
24069 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
24070 while (true)
24072 tree id;
24074 id = cp_parser_identifier (parser);
24075 if (id == error_mark_node)
24076 break;
24078 objc_declare_class (id);
24080 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24081 cp_lexer_consume_token (parser->lexer);
24082 else
24083 break;
24085 cp_parser_consume_semicolon_at_end_of_statement (parser);
24088 /* Parse a list of Objective-C protocol references.
24090 objc-protocol-refs-opt:
24091 objc-protocol-refs [opt]
24093 objc-protocol-refs:
24094 < objc-identifier-list >
24096 Returns a TREE_LIST of identifiers, if any. */
24098 static tree
24099 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
24101 tree protorefs = NULL_TREE;
24103 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
24105 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
24106 protorefs = cp_parser_objc_identifier_list (parser);
24107 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
24110 return protorefs;
24113 /* Parse a Objective-C visibility specification. */
24115 static void
24116 cp_parser_objc_visibility_spec (cp_parser* parser)
24118 cp_token *vis = cp_lexer_peek_token (parser->lexer);
24120 switch (vis->keyword)
24122 case RID_AT_PRIVATE:
24123 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
24124 break;
24125 case RID_AT_PROTECTED:
24126 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
24127 break;
24128 case RID_AT_PUBLIC:
24129 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
24130 break;
24131 case RID_AT_PACKAGE:
24132 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
24133 break;
24134 default:
24135 return;
24138 /* Eat '@private'/'@protected'/'@public'. */
24139 cp_lexer_consume_token (parser->lexer);
24142 /* Parse an Objective-C method type. Return 'true' if it is a class
24143 (+) method, and 'false' if it is an instance (-) method. */
24145 static inline bool
24146 cp_parser_objc_method_type (cp_parser* parser)
24148 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
24149 return true;
24150 else
24151 return false;
24154 /* Parse an Objective-C protocol qualifier. */
24156 static tree
24157 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
24159 tree quals = NULL_TREE, node;
24160 cp_token *token = cp_lexer_peek_token (parser->lexer);
24162 node = token->u.value;
24164 while (node && TREE_CODE (node) == IDENTIFIER_NODE
24165 && (node == ridpointers [(int) RID_IN]
24166 || node == ridpointers [(int) RID_OUT]
24167 || node == ridpointers [(int) RID_INOUT]
24168 || node == ridpointers [(int) RID_BYCOPY]
24169 || node == ridpointers [(int) RID_BYREF]
24170 || node == ridpointers [(int) RID_ONEWAY]))
24172 quals = tree_cons (NULL_TREE, node, quals);
24173 cp_lexer_consume_token (parser->lexer);
24174 token = cp_lexer_peek_token (parser->lexer);
24175 node = token->u.value;
24178 return quals;
24181 /* Parse an Objective-C typename. */
24183 static tree
24184 cp_parser_objc_typename (cp_parser* parser)
24186 tree type_name = NULL_TREE;
24188 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24190 tree proto_quals, cp_type = NULL_TREE;
24192 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24193 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
24195 /* An ObjC type name may consist of just protocol qualifiers, in which
24196 case the type shall default to 'id'. */
24197 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24199 cp_type = cp_parser_type_id (parser);
24201 /* If the type could not be parsed, an error has already
24202 been produced. For error recovery, behave as if it had
24203 not been specified, which will use the default type
24204 'id'. */
24205 if (cp_type == error_mark_node)
24207 cp_type = NULL_TREE;
24208 /* We need to skip to the closing parenthesis as
24209 cp_parser_type_id() does not seem to do it for
24210 us. */
24211 cp_parser_skip_to_closing_parenthesis (parser,
24212 /*recovering=*/true,
24213 /*or_comma=*/false,
24214 /*consume_paren=*/false);
24218 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24219 type_name = build_tree_list (proto_quals, cp_type);
24222 return type_name;
24225 /* Check to see if TYPE refers to an Objective-C selector name. */
24227 static bool
24228 cp_parser_objc_selector_p (enum cpp_ttype type)
24230 return (type == CPP_NAME || type == CPP_KEYWORD
24231 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
24232 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
24233 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
24234 || type == CPP_XOR || type == CPP_XOR_EQ);
24237 /* Parse an Objective-C selector. */
24239 static tree
24240 cp_parser_objc_selector (cp_parser* parser)
24242 cp_token *token = cp_lexer_consume_token (parser->lexer);
24244 if (!cp_parser_objc_selector_p (token->type))
24246 error_at (token->location, "invalid Objective-C++ selector name");
24247 return error_mark_node;
24250 /* C++ operator names are allowed to appear in ObjC selectors. */
24251 switch (token->type)
24253 case CPP_AND_AND: return get_identifier ("and");
24254 case CPP_AND_EQ: return get_identifier ("and_eq");
24255 case CPP_AND: return get_identifier ("bitand");
24256 case CPP_OR: return get_identifier ("bitor");
24257 case CPP_COMPL: return get_identifier ("compl");
24258 case CPP_NOT: return get_identifier ("not");
24259 case CPP_NOT_EQ: return get_identifier ("not_eq");
24260 case CPP_OR_OR: return get_identifier ("or");
24261 case CPP_OR_EQ: return get_identifier ("or_eq");
24262 case CPP_XOR: return get_identifier ("xor");
24263 case CPP_XOR_EQ: return get_identifier ("xor_eq");
24264 default: return token->u.value;
24268 /* Parse an Objective-C params list. */
24270 static tree
24271 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
24273 tree params = NULL_TREE;
24274 bool maybe_unary_selector_p = true;
24275 cp_token *token = cp_lexer_peek_token (parser->lexer);
24277 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24279 tree selector = NULL_TREE, type_name, identifier;
24280 tree parm_attr = NULL_TREE;
24282 if (token->keyword == RID_ATTRIBUTE)
24283 break;
24285 if (token->type != CPP_COLON)
24286 selector = cp_parser_objc_selector (parser);
24288 /* Detect if we have a unary selector. */
24289 if (maybe_unary_selector_p
24290 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24292 params = selector; /* Might be followed by attributes. */
24293 break;
24296 maybe_unary_selector_p = false;
24297 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24299 /* Something went quite wrong. There should be a colon
24300 here, but there is not. Stop parsing parameters. */
24301 break;
24303 type_name = cp_parser_objc_typename (parser);
24304 /* New ObjC allows attributes on parameters too. */
24305 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
24306 parm_attr = cp_parser_attributes_opt (parser);
24307 identifier = cp_parser_identifier (parser);
24309 params
24310 = chainon (params,
24311 objc_build_keyword_decl (selector,
24312 type_name,
24313 identifier,
24314 parm_attr));
24316 token = cp_lexer_peek_token (parser->lexer);
24319 if (params == NULL_TREE)
24321 cp_parser_error (parser, "objective-c++ method declaration is expected");
24322 return error_mark_node;
24325 /* We allow tail attributes for the method. */
24326 if (token->keyword == RID_ATTRIBUTE)
24328 *attributes = cp_parser_attributes_opt (parser);
24329 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24330 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24331 return params;
24332 cp_parser_error (parser,
24333 "method attributes must be specified at the end");
24334 return error_mark_node;
24337 if (params == NULL_TREE)
24339 cp_parser_error (parser, "objective-c++ method declaration is expected");
24340 return error_mark_node;
24342 return params;
24345 /* Parse the non-keyword Objective-C params. */
24347 static tree
24348 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
24349 tree* attributes)
24351 tree params = make_node (TREE_LIST);
24352 cp_token *token = cp_lexer_peek_token (parser->lexer);
24353 *ellipsisp = false; /* Initially, assume no ellipsis. */
24355 while (token->type == CPP_COMMA)
24357 cp_parameter_declarator *parmdecl;
24358 tree parm;
24360 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24361 token = cp_lexer_peek_token (parser->lexer);
24363 if (token->type == CPP_ELLIPSIS)
24365 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
24366 *ellipsisp = true;
24367 token = cp_lexer_peek_token (parser->lexer);
24368 break;
24371 /* TODO: parse attributes for tail parameters. */
24372 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
24373 parm = grokdeclarator (parmdecl->declarator,
24374 &parmdecl->decl_specifiers,
24375 PARM, /*initialized=*/0,
24376 /*attrlist=*/NULL);
24378 chainon (params, build_tree_list (NULL_TREE, parm));
24379 token = cp_lexer_peek_token (parser->lexer);
24382 /* We allow tail attributes for the method. */
24383 if (token->keyword == RID_ATTRIBUTE)
24385 if (*attributes == NULL_TREE)
24387 *attributes = cp_parser_attributes_opt (parser);
24388 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24389 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24390 return params;
24392 else
24393 /* We have an error, but parse the attributes, so that we can
24394 carry on. */
24395 *attributes = cp_parser_attributes_opt (parser);
24397 cp_parser_error (parser,
24398 "method attributes must be specified at the end");
24399 return error_mark_node;
24402 return params;
24405 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
24407 static void
24408 cp_parser_objc_interstitial_code (cp_parser* parser)
24410 cp_token *token = cp_lexer_peek_token (parser->lexer);
24412 /* If the next token is `extern' and the following token is a string
24413 literal, then we have a linkage specification. */
24414 if (token->keyword == RID_EXTERN
24415 && cp_parser_is_pure_string_literal
24416 (cp_lexer_peek_nth_token (parser->lexer, 2)))
24417 cp_parser_linkage_specification (parser);
24418 /* Handle #pragma, if any. */
24419 else if (token->type == CPP_PRAGMA)
24420 cp_parser_pragma (parser, pragma_external);
24421 /* Allow stray semicolons. */
24422 else if (token->type == CPP_SEMICOLON)
24423 cp_lexer_consume_token (parser->lexer);
24424 /* Mark methods as optional or required, when building protocols. */
24425 else if (token->keyword == RID_AT_OPTIONAL)
24427 cp_lexer_consume_token (parser->lexer);
24428 objc_set_method_opt (true);
24430 else if (token->keyword == RID_AT_REQUIRED)
24432 cp_lexer_consume_token (parser->lexer);
24433 objc_set_method_opt (false);
24435 else if (token->keyword == RID_NAMESPACE)
24436 cp_parser_namespace_definition (parser);
24437 /* Other stray characters must generate errors. */
24438 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
24440 cp_lexer_consume_token (parser->lexer);
24441 error ("stray %qs between Objective-C++ methods",
24442 token->type == CPP_OPEN_BRACE ? "{" : "}");
24444 /* Finally, try to parse a block-declaration, or a function-definition. */
24445 else
24446 cp_parser_block_declaration (parser, /*statement_p=*/false);
24449 /* Parse a method signature. */
24451 static tree
24452 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
24454 tree rettype, kwdparms, optparms;
24455 bool ellipsis = false;
24456 bool is_class_method;
24458 is_class_method = cp_parser_objc_method_type (parser);
24459 rettype = cp_parser_objc_typename (parser);
24460 *attributes = NULL_TREE;
24461 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
24462 if (kwdparms == error_mark_node)
24463 return error_mark_node;
24464 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
24465 if (optparms == error_mark_node)
24466 return error_mark_node;
24468 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
24471 static bool
24472 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
24474 tree tattr;
24475 cp_lexer_save_tokens (parser->lexer);
24476 tattr = cp_parser_attributes_opt (parser);
24477 gcc_assert (tattr) ;
24479 /* If the attributes are followed by a method introducer, this is not allowed.
24480 Dump the attributes and flag the situation. */
24481 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
24482 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
24483 return true;
24485 /* Otherwise, the attributes introduce some interstitial code, possibly so
24486 rewind to allow that check. */
24487 cp_lexer_rollback_tokens (parser->lexer);
24488 return false;
24491 /* Parse an Objective-C method prototype list. */
24493 static void
24494 cp_parser_objc_method_prototype_list (cp_parser* parser)
24496 cp_token *token = cp_lexer_peek_token (parser->lexer);
24498 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24500 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24502 tree attributes, sig;
24503 bool is_class_method;
24504 if (token->type == CPP_PLUS)
24505 is_class_method = true;
24506 else
24507 is_class_method = false;
24508 sig = cp_parser_objc_method_signature (parser, &attributes);
24509 if (sig == error_mark_node)
24511 cp_parser_skip_to_end_of_block_or_statement (parser);
24512 token = cp_lexer_peek_token (parser->lexer);
24513 continue;
24515 objc_add_method_declaration (is_class_method, sig, attributes);
24516 cp_parser_consume_semicolon_at_end_of_statement (parser);
24518 else if (token->keyword == RID_AT_PROPERTY)
24519 cp_parser_objc_at_property_declaration (parser);
24520 else if (token->keyword == RID_ATTRIBUTE
24521 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24522 warning_at (cp_lexer_peek_token (parser->lexer)->location,
24523 OPT_Wattributes,
24524 "prefix attributes are ignored for methods");
24525 else
24526 /* Allow for interspersed non-ObjC++ code. */
24527 cp_parser_objc_interstitial_code (parser);
24529 token = cp_lexer_peek_token (parser->lexer);
24532 if (token->type != CPP_EOF)
24533 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24534 else
24535 cp_parser_error (parser, "expected %<@end%>");
24537 objc_finish_interface ();
24540 /* Parse an Objective-C method definition list. */
24542 static void
24543 cp_parser_objc_method_definition_list (cp_parser* parser)
24545 cp_token *token = cp_lexer_peek_token (parser->lexer);
24547 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24549 tree meth;
24551 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24553 cp_token *ptk;
24554 tree sig, attribute;
24555 bool is_class_method;
24556 if (token->type == CPP_PLUS)
24557 is_class_method = true;
24558 else
24559 is_class_method = false;
24560 push_deferring_access_checks (dk_deferred);
24561 sig = cp_parser_objc_method_signature (parser, &attribute);
24562 if (sig == error_mark_node)
24564 cp_parser_skip_to_end_of_block_or_statement (parser);
24565 token = cp_lexer_peek_token (parser->lexer);
24566 continue;
24568 objc_start_method_definition (is_class_method, sig, attribute,
24569 NULL_TREE);
24571 /* For historical reasons, we accept an optional semicolon. */
24572 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24573 cp_lexer_consume_token (parser->lexer);
24575 ptk = cp_lexer_peek_token (parser->lexer);
24576 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
24577 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
24579 perform_deferred_access_checks (tf_warning_or_error);
24580 stop_deferring_access_checks ();
24581 meth = cp_parser_function_definition_after_declarator (parser,
24582 false);
24583 pop_deferring_access_checks ();
24584 objc_finish_method_definition (meth);
24587 /* The following case will be removed once @synthesize is
24588 completely implemented. */
24589 else if (token->keyword == RID_AT_PROPERTY)
24590 cp_parser_objc_at_property_declaration (parser);
24591 else if (token->keyword == RID_AT_SYNTHESIZE)
24592 cp_parser_objc_at_synthesize_declaration (parser);
24593 else if (token->keyword == RID_AT_DYNAMIC)
24594 cp_parser_objc_at_dynamic_declaration (parser);
24595 else if (token->keyword == RID_ATTRIBUTE
24596 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24597 warning_at (token->location, OPT_Wattributes,
24598 "prefix attributes are ignored for methods");
24599 else
24600 /* Allow for interspersed non-ObjC++ code. */
24601 cp_parser_objc_interstitial_code (parser);
24603 token = cp_lexer_peek_token (parser->lexer);
24606 if (token->type != CPP_EOF)
24607 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24608 else
24609 cp_parser_error (parser, "expected %<@end%>");
24611 objc_finish_implementation ();
24614 /* Parse Objective-C ivars. */
24616 static void
24617 cp_parser_objc_class_ivars (cp_parser* parser)
24619 cp_token *token = cp_lexer_peek_token (parser->lexer);
24621 if (token->type != CPP_OPEN_BRACE)
24622 return; /* No ivars specified. */
24624 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
24625 token = cp_lexer_peek_token (parser->lexer);
24627 while (token->type != CPP_CLOSE_BRACE
24628 && token->keyword != RID_AT_END && token->type != CPP_EOF)
24630 cp_decl_specifier_seq declspecs;
24631 int decl_class_or_enum_p;
24632 tree prefix_attributes;
24634 cp_parser_objc_visibility_spec (parser);
24636 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24637 break;
24639 cp_parser_decl_specifier_seq (parser,
24640 CP_PARSER_FLAGS_OPTIONAL,
24641 &declspecs,
24642 &decl_class_or_enum_p);
24644 /* auto, register, static, extern, mutable. */
24645 if (declspecs.storage_class != sc_none)
24647 cp_parser_error (parser, "invalid type for instance variable");
24648 declspecs.storage_class = sc_none;
24651 /* thread_local. */
24652 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24654 cp_parser_error (parser, "invalid type for instance variable");
24655 declspecs.locations[ds_thread] = 0;
24658 /* typedef. */
24659 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24661 cp_parser_error (parser, "invalid type for instance variable");
24662 declspecs.locations[ds_typedef] = 0;
24665 prefix_attributes = declspecs.attributes;
24666 declspecs.attributes = NULL_TREE;
24668 /* Keep going until we hit the `;' at the end of the
24669 declaration. */
24670 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24672 tree width = NULL_TREE, attributes, first_attribute, decl;
24673 cp_declarator *declarator = NULL;
24674 int ctor_dtor_or_conv_p;
24676 /* Check for a (possibly unnamed) bitfield declaration. */
24677 token = cp_lexer_peek_token (parser->lexer);
24678 if (token->type == CPP_COLON)
24679 goto eat_colon;
24681 if (token->type == CPP_NAME
24682 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24683 == CPP_COLON))
24685 /* Get the name of the bitfield. */
24686 declarator = make_id_declarator (NULL_TREE,
24687 cp_parser_identifier (parser),
24688 sfk_none);
24690 eat_colon:
24691 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24692 /* Get the width of the bitfield. */
24693 width
24694 = cp_parser_constant_expression (parser,
24695 /*allow_non_constant=*/false,
24696 NULL);
24698 else
24700 /* Parse the declarator. */
24701 declarator
24702 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24703 &ctor_dtor_or_conv_p,
24704 /*parenthesized_p=*/NULL,
24705 /*member_p=*/false);
24708 /* Look for attributes that apply to the ivar. */
24709 attributes = cp_parser_attributes_opt (parser);
24710 /* Remember which attributes are prefix attributes and
24711 which are not. */
24712 first_attribute = attributes;
24713 /* Combine the attributes. */
24714 attributes = chainon (prefix_attributes, attributes);
24716 if (width)
24717 /* Create the bitfield declaration. */
24718 decl = grokbitfield (declarator, &declspecs,
24719 width,
24720 attributes);
24721 else
24722 decl = grokfield (declarator, &declspecs,
24723 NULL_TREE, /*init_const_expr_p=*/false,
24724 NULL_TREE, attributes);
24726 /* Add the instance variable. */
24727 if (decl != error_mark_node && decl != NULL_TREE)
24728 objc_add_instance_variable (decl);
24730 /* Reset PREFIX_ATTRIBUTES. */
24731 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24732 attributes = TREE_CHAIN (attributes);
24733 if (attributes)
24734 TREE_CHAIN (attributes) = NULL_TREE;
24736 token = cp_lexer_peek_token (parser->lexer);
24738 if (token->type == CPP_COMMA)
24740 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24741 continue;
24743 break;
24746 cp_parser_consume_semicolon_at_end_of_statement (parser);
24747 token = cp_lexer_peek_token (parser->lexer);
24750 if (token->keyword == RID_AT_END)
24751 cp_parser_error (parser, "expected %<}%>");
24753 /* Do not consume the RID_AT_END, so it will be read again as terminating
24754 the @interface of @implementation. */
24755 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24756 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
24758 /* For historical reasons, we accept an optional semicolon. */
24759 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24760 cp_lexer_consume_token (parser->lexer);
24763 /* Parse an Objective-C protocol declaration. */
24765 static void
24766 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24768 tree proto, protorefs;
24769 cp_token *tok;
24771 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24772 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24774 tok = cp_lexer_peek_token (parser->lexer);
24775 error_at (tok->location, "identifier expected after %<@protocol%>");
24776 cp_parser_consume_semicolon_at_end_of_statement (parser);
24777 return;
24780 /* See if we have a forward declaration or a definition. */
24781 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24783 /* Try a forward declaration first. */
24784 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24786 while (true)
24788 tree id;
24790 id = cp_parser_identifier (parser);
24791 if (id == error_mark_node)
24792 break;
24794 objc_declare_protocol (id, attributes);
24796 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24797 cp_lexer_consume_token (parser->lexer);
24798 else
24799 break;
24801 cp_parser_consume_semicolon_at_end_of_statement (parser);
24804 /* Ok, we got a full-fledged definition (or at least should). */
24805 else
24807 proto = cp_parser_identifier (parser);
24808 protorefs = cp_parser_objc_protocol_refs_opt (parser);
24809 objc_start_protocol (proto, protorefs, attributes);
24810 cp_parser_objc_method_prototype_list (parser);
24814 /* Parse an Objective-C superclass or category. */
24816 static void
24817 cp_parser_objc_superclass_or_category (cp_parser *parser,
24818 bool iface_p,
24819 tree *super,
24820 tree *categ, bool *is_class_extension)
24822 cp_token *next = cp_lexer_peek_token (parser->lexer);
24824 *super = *categ = NULL_TREE;
24825 *is_class_extension = false;
24826 if (next->type == CPP_COLON)
24828 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24829 *super = cp_parser_identifier (parser);
24831 else if (next->type == CPP_OPEN_PAREN)
24833 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24835 /* If there is no category name, and this is an @interface, we
24836 have a class extension. */
24837 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24839 *categ = NULL_TREE;
24840 *is_class_extension = true;
24842 else
24843 *categ = cp_parser_identifier (parser);
24845 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24849 /* Parse an Objective-C class interface. */
24851 static void
24852 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24854 tree name, super, categ, protos;
24855 bool is_class_extension;
24857 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
24858 name = cp_parser_identifier (parser);
24859 if (name == error_mark_node)
24861 /* It's hard to recover because even if valid @interface stuff
24862 is to follow, we can't compile it (or validate it) if we
24863 don't even know which class it refers to. Let's assume this
24864 was a stray '@interface' token in the stream and skip it.
24866 return;
24868 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24869 &is_class_extension);
24870 protos = cp_parser_objc_protocol_refs_opt (parser);
24872 /* We have either a class or a category on our hands. */
24873 if (categ || is_class_extension)
24874 objc_start_category_interface (name, categ, protos, attributes);
24875 else
24877 objc_start_class_interface (name, super, protos, attributes);
24878 /* Handle instance variable declarations, if any. */
24879 cp_parser_objc_class_ivars (parser);
24880 objc_continue_interface ();
24883 cp_parser_objc_method_prototype_list (parser);
24886 /* Parse an Objective-C class implementation. */
24888 static void
24889 cp_parser_objc_class_implementation (cp_parser* parser)
24891 tree name, super, categ;
24892 bool is_class_extension;
24894 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
24895 name = cp_parser_identifier (parser);
24896 if (name == error_mark_node)
24898 /* It's hard to recover because even if valid @implementation
24899 stuff is to follow, we can't compile it (or validate it) if
24900 we don't even know which class it refers to. Let's assume
24901 this was a stray '@implementation' token in the stream and
24902 skip it.
24904 return;
24906 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24907 &is_class_extension);
24909 /* We have either a class or a category on our hands. */
24910 if (categ)
24911 objc_start_category_implementation (name, categ);
24912 else
24914 objc_start_class_implementation (name, super);
24915 /* Handle instance variable declarations, if any. */
24916 cp_parser_objc_class_ivars (parser);
24917 objc_continue_implementation ();
24920 cp_parser_objc_method_definition_list (parser);
24923 /* Consume the @end token and finish off the implementation. */
24925 static void
24926 cp_parser_objc_end_implementation (cp_parser* parser)
24928 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24929 objc_finish_implementation ();
24932 /* Parse an Objective-C declaration. */
24934 static void
24935 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24937 /* Try to figure out what kind of declaration is present. */
24938 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24940 if (attributes)
24941 switch (kwd->keyword)
24943 case RID_AT_ALIAS:
24944 case RID_AT_CLASS:
24945 case RID_AT_END:
24946 error_at (kwd->location, "attributes may not be specified before"
24947 " the %<@%D%> Objective-C++ keyword",
24948 kwd->u.value);
24949 attributes = NULL;
24950 break;
24951 case RID_AT_IMPLEMENTATION:
24952 warning_at (kwd->location, OPT_Wattributes,
24953 "prefix attributes are ignored before %<@%D%>",
24954 kwd->u.value);
24955 attributes = NULL;
24956 default:
24957 break;
24960 switch (kwd->keyword)
24962 case RID_AT_ALIAS:
24963 cp_parser_objc_alias_declaration (parser);
24964 break;
24965 case RID_AT_CLASS:
24966 cp_parser_objc_class_declaration (parser);
24967 break;
24968 case RID_AT_PROTOCOL:
24969 cp_parser_objc_protocol_declaration (parser, attributes);
24970 break;
24971 case RID_AT_INTERFACE:
24972 cp_parser_objc_class_interface (parser, attributes);
24973 break;
24974 case RID_AT_IMPLEMENTATION:
24975 cp_parser_objc_class_implementation (parser);
24976 break;
24977 case RID_AT_END:
24978 cp_parser_objc_end_implementation (parser);
24979 break;
24980 default:
24981 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24982 kwd->u.value);
24983 cp_parser_skip_to_end_of_block_or_statement (parser);
24987 /* Parse an Objective-C try-catch-finally statement.
24989 objc-try-catch-finally-stmt:
24990 @try compound-statement objc-catch-clause-seq [opt]
24991 objc-finally-clause [opt]
24993 objc-catch-clause-seq:
24994 objc-catch-clause objc-catch-clause-seq [opt]
24996 objc-catch-clause:
24997 @catch ( objc-exception-declaration ) compound-statement
24999 objc-finally-clause:
25000 @finally compound-statement
25002 objc-exception-declaration:
25003 parameter-declaration
25004 '...'
25006 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
25008 Returns NULL_TREE.
25010 PS: This function is identical to c_parser_objc_try_catch_finally_statement
25011 for C. Keep them in sync. */
25013 static tree
25014 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
25016 location_t location;
25017 tree stmt;
25019 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
25020 location = cp_lexer_peek_token (parser->lexer)->location;
25021 objc_maybe_warn_exceptions (location);
25022 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
25023 node, lest it get absorbed into the surrounding block. */
25024 stmt = push_stmt_list ();
25025 cp_parser_compound_statement (parser, NULL, false, false);
25026 objc_begin_try_stmt (location, pop_stmt_list (stmt));
25028 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
25030 cp_parameter_declarator *parm;
25031 tree parameter_declaration = error_mark_node;
25032 bool seen_open_paren = false;
25034 cp_lexer_consume_token (parser->lexer);
25035 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25036 seen_open_paren = true;
25037 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25039 /* We have "@catch (...)" (where the '...' are literally
25040 what is in the code). Skip the '...'.
25041 parameter_declaration is set to NULL_TREE, and
25042 objc_being_catch_clauses() knows that that means
25043 '...'. */
25044 cp_lexer_consume_token (parser->lexer);
25045 parameter_declaration = NULL_TREE;
25047 else
25049 /* We have "@catch (NSException *exception)" or something
25050 like that. Parse the parameter declaration. */
25051 parm = cp_parser_parameter_declaration (parser, false, NULL);
25052 if (parm == NULL)
25053 parameter_declaration = error_mark_node;
25054 else
25055 parameter_declaration = grokdeclarator (parm->declarator,
25056 &parm->decl_specifiers,
25057 PARM, /*initialized=*/0,
25058 /*attrlist=*/NULL);
25060 if (seen_open_paren)
25061 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25062 else
25064 /* If there was no open parenthesis, we are recovering from
25065 an error, and we are trying to figure out what mistake
25066 the user has made. */
25068 /* If there is an immediate closing parenthesis, the user
25069 probably forgot the opening one (ie, they typed "@catch
25070 NSException *e)". Parse the closing parenthesis and keep
25071 going. */
25072 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25073 cp_lexer_consume_token (parser->lexer);
25075 /* If these is no immediate closing parenthesis, the user
25076 probably doesn't know that parenthesis are required at
25077 all (ie, they typed "@catch NSException *e"). So, just
25078 forget about the closing parenthesis and keep going. */
25080 objc_begin_catch_clause (parameter_declaration);
25081 cp_parser_compound_statement (parser, NULL, false, false);
25082 objc_finish_catch_clause ();
25084 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
25086 cp_lexer_consume_token (parser->lexer);
25087 location = cp_lexer_peek_token (parser->lexer)->location;
25088 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
25089 node, lest it get absorbed into the surrounding block. */
25090 stmt = push_stmt_list ();
25091 cp_parser_compound_statement (parser, NULL, false, false);
25092 objc_build_finally_clause (location, pop_stmt_list (stmt));
25095 return objc_finish_try_stmt ();
25098 /* Parse an Objective-C synchronized statement.
25100 objc-synchronized-stmt:
25101 @synchronized ( expression ) compound-statement
25103 Returns NULL_TREE. */
25105 static tree
25106 cp_parser_objc_synchronized_statement (cp_parser *parser)
25108 location_t location;
25109 tree lock, stmt;
25111 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
25113 location = cp_lexer_peek_token (parser->lexer)->location;
25114 objc_maybe_warn_exceptions (location);
25115 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25116 lock = cp_parser_expression (parser, false, NULL);
25117 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25119 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
25120 node, lest it get absorbed into the surrounding block. */
25121 stmt = push_stmt_list ();
25122 cp_parser_compound_statement (parser, NULL, false, false);
25124 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
25127 /* Parse an Objective-C throw statement.
25129 objc-throw-stmt:
25130 @throw assignment-expression [opt] ;
25132 Returns a constructed '@throw' statement. */
25134 static tree
25135 cp_parser_objc_throw_statement (cp_parser *parser)
25137 tree expr = NULL_TREE;
25138 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25140 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
25142 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25143 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25145 cp_parser_consume_semicolon_at_end_of_statement (parser);
25147 return objc_build_throw_stmt (loc, expr);
25150 /* Parse an Objective-C statement. */
25152 static tree
25153 cp_parser_objc_statement (cp_parser * parser)
25155 /* Try to figure out what kind of declaration is present. */
25156 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25158 switch (kwd->keyword)
25160 case RID_AT_TRY:
25161 return cp_parser_objc_try_catch_finally_statement (parser);
25162 case RID_AT_SYNCHRONIZED:
25163 return cp_parser_objc_synchronized_statement (parser);
25164 case RID_AT_THROW:
25165 return cp_parser_objc_throw_statement (parser);
25166 default:
25167 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25168 kwd->u.value);
25169 cp_parser_skip_to_end_of_block_or_statement (parser);
25172 return error_mark_node;
25175 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
25176 look ahead to see if an objc keyword follows the attributes. This
25177 is to detect the use of prefix attributes on ObjC @interface and
25178 @protocol. */
25180 static bool
25181 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
25183 cp_lexer_save_tokens (parser->lexer);
25184 *attrib = cp_parser_attributes_opt (parser);
25185 gcc_assert (*attrib);
25186 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
25188 cp_lexer_commit_tokens (parser->lexer);
25189 return true;
25191 cp_lexer_rollback_tokens (parser->lexer);
25192 return false;
25195 /* This routine is a minimal replacement for
25196 c_parser_struct_declaration () used when parsing the list of
25197 types/names or ObjC++ properties. For example, when parsing the
25198 code
25200 @property (readonly) int a, b, c;
25202 this function is responsible for parsing "int a, int b, int c" and
25203 returning the declarations as CHAIN of DECLs.
25205 TODO: Share this code with cp_parser_objc_class_ivars. It's very
25206 similar parsing. */
25207 static tree
25208 cp_parser_objc_struct_declaration (cp_parser *parser)
25210 tree decls = NULL_TREE;
25211 cp_decl_specifier_seq declspecs;
25212 int decl_class_or_enum_p;
25213 tree prefix_attributes;
25215 cp_parser_decl_specifier_seq (parser,
25216 CP_PARSER_FLAGS_NONE,
25217 &declspecs,
25218 &decl_class_or_enum_p);
25220 if (declspecs.type == error_mark_node)
25221 return error_mark_node;
25223 /* auto, register, static, extern, mutable. */
25224 if (declspecs.storage_class != sc_none)
25226 cp_parser_error (parser, "invalid type for property");
25227 declspecs.storage_class = sc_none;
25230 /* thread_local. */
25231 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
25233 cp_parser_error (parser, "invalid type for property");
25234 declspecs.locations[ds_thread] = 0;
25237 /* typedef. */
25238 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
25240 cp_parser_error (parser, "invalid type for property");
25241 declspecs.locations[ds_typedef] = 0;
25244 prefix_attributes = declspecs.attributes;
25245 declspecs.attributes = NULL_TREE;
25247 /* Keep going until we hit the `;' at the end of the declaration. */
25248 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25250 tree attributes, first_attribute, decl;
25251 cp_declarator *declarator;
25252 cp_token *token;
25254 /* Parse the declarator. */
25255 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25256 NULL, NULL, false);
25258 /* Look for attributes that apply to the ivar. */
25259 attributes = cp_parser_attributes_opt (parser);
25260 /* Remember which attributes are prefix attributes and
25261 which are not. */
25262 first_attribute = attributes;
25263 /* Combine the attributes. */
25264 attributes = chainon (prefix_attributes, attributes);
25266 decl = grokfield (declarator, &declspecs,
25267 NULL_TREE, /*init_const_expr_p=*/false,
25268 NULL_TREE, attributes);
25270 if (decl == error_mark_node || decl == NULL_TREE)
25271 return error_mark_node;
25273 /* Reset PREFIX_ATTRIBUTES. */
25274 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25275 attributes = TREE_CHAIN (attributes);
25276 if (attributes)
25277 TREE_CHAIN (attributes) = NULL_TREE;
25279 DECL_CHAIN (decl) = decls;
25280 decls = decl;
25282 token = cp_lexer_peek_token (parser->lexer);
25283 if (token->type == CPP_COMMA)
25285 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25286 continue;
25288 else
25289 break;
25291 return decls;
25294 /* Parse an Objective-C @property declaration. The syntax is:
25296 objc-property-declaration:
25297 '@property' objc-property-attributes[opt] struct-declaration ;
25299 objc-property-attributes:
25300 '(' objc-property-attribute-list ')'
25302 objc-property-attribute-list:
25303 objc-property-attribute
25304 objc-property-attribute-list, objc-property-attribute
25306 objc-property-attribute
25307 'getter' = identifier
25308 'setter' = identifier
25309 'readonly'
25310 'readwrite'
25311 'assign'
25312 'retain'
25313 'copy'
25314 'nonatomic'
25316 For example:
25317 @property NSString *name;
25318 @property (readonly) id object;
25319 @property (retain, nonatomic, getter=getTheName) id name;
25320 @property int a, b, c;
25322 PS: This function is identical to
25323 c_parser_objc_at_property_declaration for C. Keep them in sync. */
25324 static void
25325 cp_parser_objc_at_property_declaration (cp_parser *parser)
25327 /* The following variables hold the attributes of the properties as
25328 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
25329 seen. When we see an attribute, we set them to 'true' (if they
25330 are boolean properties) or to the identifier (if they have an
25331 argument, ie, for getter and setter). Note that here we only
25332 parse the list of attributes, check the syntax and accumulate the
25333 attributes that we find. objc_add_property_declaration() will
25334 then process the information. */
25335 bool property_assign = false;
25336 bool property_copy = false;
25337 tree property_getter_ident = NULL_TREE;
25338 bool property_nonatomic = false;
25339 bool property_readonly = false;
25340 bool property_readwrite = false;
25341 bool property_retain = false;
25342 tree property_setter_ident = NULL_TREE;
25344 /* 'properties' is the list of properties that we read. Usually a
25345 single one, but maybe more (eg, in "@property int a, b, c;" there
25346 are three). */
25347 tree properties;
25348 location_t loc;
25350 loc = cp_lexer_peek_token (parser->lexer)->location;
25352 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
25354 /* Parse the optional attribute list... */
25355 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25357 /* Eat the '('. */
25358 cp_lexer_consume_token (parser->lexer);
25360 while (true)
25362 bool syntax_error = false;
25363 cp_token *token = cp_lexer_peek_token (parser->lexer);
25364 enum rid keyword;
25366 if (token->type != CPP_NAME)
25368 cp_parser_error (parser, "expected identifier");
25369 break;
25371 keyword = C_RID_CODE (token->u.value);
25372 cp_lexer_consume_token (parser->lexer);
25373 switch (keyword)
25375 case RID_ASSIGN: property_assign = true; break;
25376 case RID_COPY: property_copy = true; break;
25377 case RID_NONATOMIC: property_nonatomic = true; break;
25378 case RID_READONLY: property_readonly = true; break;
25379 case RID_READWRITE: property_readwrite = true; break;
25380 case RID_RETAIN: property_retain = true; break;
25382 case RID_GETTER:
25383 case RID_SETTER:
25384 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
25386 if (keyword == RID_GETTER)
25387 cp_parser_error (parser,
25388 "missing %<=%> (after %<getter%> attribute)");
25389 else
25390 cp_parser_error (parser,
25391 "missing %<=%> (after %<setter%> attribute)");
25392 syntax_error = true;
25393 break;
25395 cp_lexer_consume_token (parser->lexer); /* eat the = */
25396 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
25398 cp_parser_error (parser, "expected identifier");
25399 syntax_error = true;
25400 break;
25402 if (keyword == RID_SETTER)
25404 if (property_setter_ident != NULL_TREE)
25406 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
25407 cp_lexer_consume_token (parser->lexer);
25409 else
25410 property_setter_ident = cp_parser_objc_selector (parser);
25411 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25412 cp_parser_error (parser, "setter name must terminate with %<:%>");
25413 else
25414 cp_lexer_consume_token (parser->lexer);
25416 else
25418 if (property_getter_ident != NULL_TREE)
25420 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
25421 cp_lexer_consume_token (parser->lexer);
25423 else
25424 property_getter_ident = cp_parser_objc_selector (parser);
25426 break;
25427 default:
25428 cp_parser_error (parser, "unknown property attribute");
25429 syntax_error = true;
25430 break;
25433 if (syntax_error)
25434 break;
25436 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25437 cp_lexer_consume_token (parser->lexer);
25438 else
25439 break;
25442 /* FIXME: "@property (setter, assign);" will generate a spurious
25443 "error: expected ‘)’ before ‘,’ token". This is because
25444 cp_parser_require, unlike the C counterpart, will produce an
25445 error even if we are in error recovery. */
25446 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25448 cp_parser_skip_to_closing_parenthesis (parser,
25449 /*recovering=*/true,
25450 /*or_comma=*/false,
25451 /*consume_paren=*/true);
25455 /* ... and the property declaration(s). */
25456 properties = cp_parser_objc_struct_declaration (parser);
25458 if (properties == error_mark_node)
25460 cp_parser_skip_to_end_of_statement (parser);
25461 /* If the next token is now a `;', consume it. */
25462 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25463 cp_lexer_consume_token (parser->lexer);
25464 return;
25467 if (properties == NULL_TREE)
25468 cp_parser_error (parser, "expected identifier");
25469 else
25471 /* Comma-separated properties are chained together in
25472 reverse order; add them one by one. */
25473 properties = nreverse (properties);
25475 for (; properties; properties = TREE_CHAIN (properties))
25476 objc_add_property_declaration (loc, copy_node (properties),
25477 property_readonly, property_readwrite,
25478 property_assign, property_retain,
25479 property_copy, property_nonatomic,
25480 property_getter_ident, property_setter_ident);
25483 cp_parser_consume_semicolon_at_end_of_statement (parser);
25486 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
25488 objc-synthesize-declaration:
25489 @synthesize objc-synthesize-identifier-list ;
25491 objc-synthesize-identifier-list:
25492 objc-synthesize-identifier
25493 objc-synthesize-identifier-list, objc-synthesize-identifier
25495 objc-synthesize-identifier
25496 identifier
25497 identifier = identifier
25499 For example:
25500 @synthesize MyProperty;
25501 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
25503 PS: This function is identical to c_parser_objc_at_synthesize_declaration
25504 for C. Keep them in sync.
25506 static void
25507 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
25509 tree list = NULL_TREE;
25510 location_t loc;
25511 loc = cp_lexer_peek_token (parser->lexer)->location;
25513 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
25514 while (true)
25516 tree property, ivar;
25517 property = cp_parser_identifier (parser);
25518 if (property == error_mark_node)
25520 cp_parser_consume_semicolon_at_end_of_statement (parser);
25521 return;
25523 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25525 cp_lexer_consume_token (parser->lexer);
25526 ivar = cp_parser_identifier (parser);
25527 if (ivar == error_mark_node)
25529 cp_parser_consume_semicolon_at_end_of_statement (parser);
25530 return;
25533 else
25534 ivar = NULL_TREE;
25535 list = chainon (list, build_tree_list (ivar, property));
25536 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25537 cp_lexer_consume_token (parser->lexer);
25538 else
25539 break;
25541 cp_parser_consume_semicolon_at_end_of_statement (parser);
25542 objc_add_synthesize_declaration (loc, list);
25545 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
25547 objc-dynamic-declaration:
25548 @dynamic identifier-list ;
25550 For example:
25551 @dynamic MyProperty;
25552 @dynamic MyProperty, AnotherProperty;
25554 PS: This function is identical to c_parser_objc_at_dynamic_declaration
25555 for C. Keep them in sync.
25557 static void
25558 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
25560 tree list = NULL_TREE;
25561 location_t loc;
25562 loc = cp_lexer_peek_token (parser->lexer)->location;
25564 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
25565 while (true)
25567 tree property;
25568 property = cp_parser_identifier (parser);
25569 if (property == error_mark_node)
25571 cp_parser_consume_semicolon_at_end_of_statement (parser);
25572 return;
25574 list = chainon (list, build_tree_list (NULL, property));
25575 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25576 cp_lexer_consume_token (parser->lexer);
25577 else
25578 break;
25580 cp_parser_consume_semicolon_at_end_of_statement (parser);
25581 objc_add_dynamic_declaration (loc, list);
25585 /* OpenMP 2.5 parsing routines. */
25587 /* Returns name of the next clause.
25588 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
25589 the token is not consumed. Otherwise appropriate pragma_omp_clause is
25590 returned and the token is consumed. */
25592 static pragma_omp_clause
25593 cp_parser_omp_clause_name (cp_parser *parser)
25595 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
25597 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
25598 result = PRAGMA_OMP_CLAUSE_IF;
25599 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
25600 result = PRAGMA_OMP_CLAUSE_DEFAULT;
25601 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
25602 result = PRAGMA_OMP_CLAUSE_PRIVATE;
25603 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25605 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25606 const char *p = IDENTIFIER_POINTER (id);
25608 switch (p[0])
25610 case 'c':
25611 if (!strcmp ("collapse", p))
25612 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
25613 else if (!strcmp ("copyin", p))
25614 result = PRAGMA_OMP_CLAUSE_COPYIN;
25615 else if (!strcmp ("copyprivate", p))
25616 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
25617 break;
25618 case 'f':
25619 if (!strcmp ("final", p))
25620 result = PRAGMA_OMP_CLAUSE_FINAL;
25621 else if (!strcmp ("firstprivate", p))
25622 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
25623 break;
25624 case 'l':
25625 if (!strcmp ("lastprivate", p))
25626 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
25627 break;
25628 case 'm':
25629 if (!strcmp ("mergeable", p))
25630 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
25631 break;
25632 case 'n':
25633 if (!strcmp ("nowait", p))
25634 result = PRAGMA_OMP_CLAUSE_NOWAIT;
25635 else if (!strcmp ("num_threads", p))
25636 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
25637 break;
25638 case 'o':
25639 if (!strcmp ("ordered", p))
25640 result = PRAGMA_OMP_CLAUSE_ORDERED;
25641 break;
25642 case 'r':
25643 if (!strcmp ("reduction", p))
25644 result = PRAGMA_OMP_CLAUSE_REDUCTION;
25645 break;
25646 case 's':
25647 if (!strcmp ("schedule", p))
25648 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
25649 else if (!strcmp ("shared", p))
25650 result = PRAGMA_OMP_CLAUSE_SHARED;
25651 break;
25652 case 'u':
25653 if (!strcmp ("untied", p))
25654 result = PRAGMA_OMP_CLAUSE_UNTIED;
25655 break;
25659 if (result != PRAGMA_OMP_CLAUSE_NONE)
25660 cp_lexer_consume_token (parser->lexer);
25662 return result;
25665 /* Validate that a clause of the given type does not already exist. */
25667 static void
25668 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
25669 const char *name, location_t location)
25671 tree c;
25673 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25674 if (OMP_CLAUSE_CODE (c) == code)
25676 error_at (location, "too many %qs clauses", name);
25677 break;
25681 /* OpenMP 2.5:
25682 variable-list:
25683 identifier
25684 variable-list , identifier
25686 In addition, we match a closing parenthesis. An opening parenthesis
25687 will have been consumed by the caller.
25689 If KIND is nonzero, create the appropriate node and install the decl
25690 in OMP_CLAUSE_DECL and add the node to the head of the list.
25692 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25693 return the list created. */
25695 static tree
25696 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
25697 tree list)
25699 cp_token *token;
25700 while (1)
25702 tree name, decl;
25704 token = cp_lexer_peek_token (parser->lexer);
25705 name = cp_parser_id_expression (parser, /*template_p=*/false,
25706 /*check_dependency_p=*/true,
25707 /*template_p=*/NULL,
25708 /*declarator_p=*/false,
25709 /*optional_p=*/false);
25710 if (name == error_mark_node)
25711 goto skip_comma;
25713 decl = cp_parser_lookup_name_simple (parser, name, token->location);
25714 if (decl == error_mark_node)
25715 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
25716 token->location);
25717 else if (kind != 0)
25719 tree u = build_omp_clause (token->location, kind);
25720 OMP_CLAUSE_DECL (u) = decl;
25721 OMP_CLAUSE_CHAIN (u) = list;
25722 list = u;
25724 else
25725 list = tree_cons (decl, NULL_TREE, list);
25727 get_comma:
25728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25729 break;
25730 cp_lexer_consume_token (parser->lexer);
25733 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25735 int ending;
25737 /* Try to resync to an unnested comma. Copied from
25738 cp_parser_parenthesized_expression_list. */
25739 skip_comma:
25740 ending = cp_parser_skip_to_closing_parenthesis (parser,
25741 /*recovering=*/true,
25742 /*or_comma=*/true,
25743 /*consume_paren=*/true);
25744 if (ending < 0)
25745 goto get_comma;
25748 return list;
25751 /* Similarly, but expect leading and trailing parenthesis. This is a very
25752 common case for omp clauses. */
25754 static tree
25755 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25757 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25758 return cp_parser_omp_var_list_no_open (parser, kind, list);
25759 return list;
25762 /* OpenMP 3.0:
25763 collapse ( constant-expression ) */
25765 static tree
25766 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25768 tree c, num;
25769 location_t loc;
25770 HOST_WIDE_INT n;
25772 loc = cp_lexer_peek_token (parser->lexer)->location;
25773 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25774 return list;
25776 num = cp_parser_constant_expression (parser, false, NULL);
25778 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25779 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25780 /*or_comma=*/false,
25781 /*consume_paren=*/true);
25783 if (num == error_mark_node)
25784 return list;
25785 num = fold_non_dependent_expr (num);
25786 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25787 || !host_integerp (num, 0)
25788 || (n = tree_low_cst (num, 0)) <= 0
25789 || (int) n != n)
25791 error_at (loc, "collapse argument needs positive constant integer expression");
25792 return list;
25795 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25796 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25797 OMP_CLAUSE_CHAIN (c) = list;
25798 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25800 return c;
25803 /* OpenMP 2.5:
25804 default ( shared | none ) */
25806 static tree
25807 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25809 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25810 tree c;
25812 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25813 return list;
25814 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25816 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25817 const char *p = IDENTIFIER_POINTER (id);
25819 switch (p[0])
25821 case 'n':
25822 if (strcmp ("none", p) != 0)
25823 goto invalid_kind;
25824 kind = OMP_CLAUSE_DEFAULT_NONE;
25825 break;
25827 case 's':
25828 if (strcmp ("shared", p) != 0)
25829 goto invalid_kind;
25830 kind = OMP_CLAUSE_DEFAULT_SHARED;
25831 break;
25833 default:
25834 goto invalid_kind;
25837 cp_lexer_consume_token (parser->lexer);
25839 else
25841 invalid_kind:
25842 cp_parser_error (parser, "expected %<none%> or %<shared%>");
25845 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25846 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25847 /*or_comma=*/false,
25848 /*consume_paren=*/true);
25850 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25851 return list;
25853 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25854 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25855 OMP_CLAUSE_CHAIN (c) = list;
25856 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25858 return c;
25861 /* OpenMP 3.1:
25862 final ( expression ) */
25864 static tree
25865 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25867 tree t, c;
25869 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25870 return list;
25872 t = cp_parser_condition (parser);
25874 if (t == error_mark_node
25875 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25876 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25877 /*or_comma=*/false,
25878 /*consume_paren=*/true);
25880 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25882 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25883 OMP_CLAUSE_FINAL_EXPR (c) = t;
25884 OMP_CLAUSE_CHAIN (c) = list;
25886 return c;
25889 /* OpenMP 2.5:
25890 if ( expression ) */
25892 static tree
25893 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25895 tree t, c;
25897 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25898 return list;
25900 t = cp_parser_condition (parser);
25902 if (t == error_mark_node
25903 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25904 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25905 /*or_comma=*/false,
25906 /*consume_paren=*/true);
25908 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25910 c = build_omp_clause (location, OMP_CLAUSE_IF);
25911 OMP_CLAUSE_IF_EXPR (c) = t;
25912 OMP_CLAUSE_CHAIN (c) = list;
25914 return c;
25917 /* OpenMP 3.1:
25918 mergeable */
25920 static tree
25921 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
25922 tree list, location_t location)
25924 tree c;
25926 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25927 location);
25929 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25930 OMP_CLAUSE_CHAIN (c) = list;
25931 return c;
25934 /* OpenMP 2.5:
25935 nowait */
25937 static tree
25938 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
25939 tree list, location_t location)
25941 tree c;
25943 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25945 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25946 OMP_CLAUSE_CHAIN (c) = list;
25947 return c;
25950 /* OpenMP 2.5:
25951 num_threads ( expression ) */
25953 static tree
25954 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25955 location_t location)
25957 tree t, c;
25959 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25960 return list;
25962 t = cp_parser_expression (parser, false, NULL);
25964 if (t == error_mark_node
25965 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25966 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25967 /*or_comma=*/false,
25968 /*consume_paren=*/true);
25970 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25971 "num_threads", location);
25973 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25974 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25975 OMP_CLAUSE_CHAIN (c) = list;
25977 return c;
25980 /* OpenMP 2.5:
25981 ordered */
25983 static tree
25984 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
25985 tree list, location_t location)
25987 tree c;
25989 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25990 "ordered", location);
25992 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25993 OMP_CLAUSE_CHAIN (c) = list;
25994 return c;
25997 /* OpenMP 2.5:
25998 reduction ( reduction-operator : variable-list )
26000 reduction-operator:
26001 One of: + * - & ^ | && ||
26003 OpenMP 3.1:
26005 reduction-operator:
26006 One of: + * - & ^ | && || min max */
26008 static tree
26009 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
26011 enum tree_code code;
26012 tree nlist, c;
26014 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26015 return list;
26017 switch (cp_lexer_peek_token (parser->lexer)->type)
26019 case CPP_PLUS:
26020 code = PLUS_EXPR;
26021 break;
26022 case CPP_MULT:
26023 code = MULT_EXPR;
26024 break;
26025 case CPP_MINUS:
26026 code = MINUS_EXPR;
26027 break;
26028 case CPP_AND:
26029 code = BIT_AND_EXPR;
26030 break;
26031 case CPP_XOR:
26032 code = BIT_XOR_EXPR;
26033 break;
26034 case CPP_OR:
26035 code = BIT_IOR_EXPR;
26036 break;
26037 case CPP_AND_AND:
26038 code = TRUTH_ANDIF_EXPR;
26039 break;
26040 case CPP_OR_OR:
26041 code = TRUTH_ORIF_EXPR;
26042 break;
26043 case CPP_NAME:
26045 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26046 const char *p = IDENTIFIER_POINTER (id);
26048 if (strcmp (p, "min") == 0)
26050 code = MIN_EXPR;
26051 break;
26053 if (strcmp (p, "max") == 0)
26055 code = MAX_EXPR;
26056 break;
26059 /* FALLTHROUGH */
26060 default:
26061 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
26062 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
26063 resync_fail:
26064 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26065 /*or_comma=*/false,
26066 /*consume_paren=*/true);
26067 return list;
26069 cp_lexer_consume_token (parser->lexer);
26071 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26072 goto resync_fail;
26074 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
26075 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
26076 OMP_CLAUSE_REDUCTION_CODE (c) = code;
26078 return nlist;
26081 /* OpenMP 2.5:
26082 schedule ( schedule-kind )
26083 schedule ( schedule-kind , expression )
26085 schedule-kind:
26086 static | dynamic | guided | runtime | auto */
26088 static tree
26089 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
26091 tree c, t;
26093 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26094 return list;
26096 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
26098 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26100 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26101 const char *p = IDENTIFIER_POINTER (id);
26103 switch (p[0])
26105 case 'd':
26106 if (strcmp ("dynamic", p) != 0)
26107 goto invalid_kind;
26108 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
26109 break;
26111 case 'g':
26112 if (strcmp ("guided", p) != 0)
26113 goto invalid_kind;
26114 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
26115 break;
26117 case 'r':
26118 if (strcmp ("runtime", p) != 0)
26119 goto invalid_kind;
26120 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
26121 break;
26123 default:
26124 goto invalid_kind;
26127 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
26128 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
26129 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
26130 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
26131 else
26132 goto invalid_kind;
26133 cp_lexer_consume_token (parser->lexer);
26135 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26137 cp_token *token;
26138 cp_lexer_consume_token (parser->lexer);
26140 token = cp_lexer_peek_token (parser->lexer);
26141 t = cp_parser_assignment_expression (parser, false, NULL);
26143 if (t == error_mark_node)
26144 goto resync_fail;
26145 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
26146 error_at (token->location, "schedule %<runtime%> does not take "
26147 "a %<chunk_size%> parameter");
26148 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
26149 error_at (token->location, "schedule %<auto%> does not take "
26150 "a %<chunk_size%> parameter");
26151 else
26152 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
26154 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26155 goto resync_fail;
26157 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
26158 goto resync_fail;
26160 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
26161 OMP_CLAUSE_CHAIN (c) = list;
26162 return c;
26164 invalid_kind:
26165 cp_parser_error (parser, "invalid schedule kind");
26166 resync_fail:
26167 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26168 /*or_comma=*/false,
26169 /*consume_paren=*/true);
26170 return list;
26173 /* OpenMP 3.0:
26174 untied */
26176 static tree
26177 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
26178 tree list, location_t location)
26180 tree c;
26182 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
26184 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
26185 OMP_CLAUSE_CHAIN (c) = list;
26186 return c;
26189 /* Parse all OpenMP clauses. The set clauses allowed by the directive
26190 is a bitmask in MASK. Return the list of clauses found; the result
26191 of clause default goes in *pdefault. */
26193 static tree
26194 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
26195 const char *where, cp_token *pragma_tok)
26197 tree clauses = NULL;
26198 bool first = true;
26199 cp_token *token = NULL;
26201 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
26203 pragma_omp_clause c_kind;
26204 const char *c_name;
26205 tree prev = clauses;
26207 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26208 cp_lexer_consume_token (parser->lexer);
26210 token = cp_lexer_peek_token (parser->lexer);
26211 c_kind = cp_parser_omp_clause_name (parser);
26212 first = false;
26214 switch (c_kind)
26216 case PRAGMA_OMP_CLAUSE_COLLAPSE:
26217 clauses = cp_parser_omp_clause_collapse (parser, clauses,
26218 token->location);
26219 c_name = "collapse";
26220 break;
26221 case PRAGMA_OMP_CLAUSE_COPYIN:
26222 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
26223 c_name = "copyin";
26224 break;
26225 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
26226 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
26227 clauses);
26228 c_name = "copyprivate";
26229 break;
26230 case PRAGMA_OMP_CLAUSE_DEFAULT:
26231 clauses = cp_parser_omp_clause_default (parser, clauses,
26232 token->location);
26233 c_name = "default";
26234 break;
26235 case PRAGMA_OMP_CLAUSE_FINAL:
26236 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
26237 c_name = "final";
26238 break;
26239 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
26240 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
26241 clauses);
26242 c_name = "firstprivate";
26243 break;
26244 case PRAGMA_OMP_CLAUSE_IF:
26245 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
26246 c_name = "if";
26247 break;
26248 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
26249 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
26250 clauses);
26251 c_name = "lastprivate";
26252 break;
26253 case PRAGMA_OMP_CLAUSE_MERGEABLE:
26254 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
26255 token->location);
26256 c_name = "mergeable";
26257 break;
26258 case PRAGMA_OMP_CLAUSE_NOWAIT:
26259 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
26260 c_name = "nowait";
26261 break;
26262 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
26263 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
26264 token->location);
26265 c_name = "num_threads";
26266 break;
26267 case PRAGMA_OMP_CLAUSE_ORDERED:
26268 clauses = cp_parser_omp_clause_ordered (parser, clauses,
26269 token->location);
26270 c_name = "ordered";
26271 break;
26272 case PRAGMA_OMP_CLAUSE_PRIVATE:
26273 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
26274 clauses);
26275 c_name = "private";
26276 break;
26277 case PRAGMA_OMP_CLAUSE_REDUCTION:
26278 clauses = cp_parser_omp_clause_reduction (parser, clauses);
26279 c_name = "reduction";
26280 break;
26281 case PRAGMA_OMP_CLAUSE_SCHEDULE:
26282 clauses = cp_parser_omp_clause_schedule (parser, clauses,
26283 token->location);
26284 c_name = "schedule";
26285 break;
26286 case PRAGMA_OMP_CLAUSE_SHARED:
26287 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
26288 clauses);
26289 c_name = "shared";
26290 break;
26291 case PRAGMA_OMP_CLAUSE_UNTIED:
26292 clauses = cp_parser_omp_clause_untied (parser, clauses,
26293 token->location);
26294 c_name = "nowait";
26295 break;
26296 default:
26297 cp_parser_error (parser, "expected %<#pragma omp%> clause");
26298 goto saw_error;
26301 if (((mask >> c_kind) & 1) == 0)
26303 /* Remove the invalid clause(s) from the list to avoid
26304 confusing the rest of the compiler. */
26305 clauses = prev;
26306 error_at (token->location, "%qs is not valid for %qs", c_name, where);
26309 saw_error:
26310 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
26311 return finish_omp_clauses (clauses);
26314 /* OpenMP 2.5:
26315 structured-block:
26316 statement
26318 In practice, we're also interested in adding the statement to an
26319 outer node. So it is convenient if we work around the fact that
26320 cp_parser_statement calls add_stmt. */
26322 static unsigned
26323 cp_parser_begin_omp_structured_block (cp_parser *parser)
26325 unsigned save = parser->in_statement;
26327 /* Only move the values to IN_OMP_BLOCK if they weren't false.
26328 This preserves the "not within loop or switch" style error messages
26329 for nonsense cases like
26330 void foo() {
26331 #pragma omp single
26332 break;
26335 if (parser->in_statement)
26336 parser->in_statement = IN_OMP_BLOCK;
26338 return save;
26341 static void
26342 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
26344 parser->in_statement = save;
26347 static tree
26348 cp_parser_omp_structured_block (cp_parser *parser)
26350 tree stmt = begin_omp_structured_block ();
26351 unsigned int save = cp_parser_begin_omp_structured_block (parser);
26353 cp_parser_statement (parser, NULL_TREE, false, NULL);
26355 cp_parser_end_omp_structured_block (parser, save);
26356 return finish_omp_structured_block (stmt);
26359 /* OpenMP 2.5:
26360 # pragma omp atomic new-line
26361 expression-stmt
26363 expression-stmt:
26364 x binop= expr | x++ | ++x | x-- | --x
26365 binop:
26366 +, *, -, /, &, ^, |, <<, >>
26368 where x is an lvalue expression with scalar type.
26370 OpenMP 3.1:
26371 # pragma omp atomic new-line
26372 update-stmt
26374 # pragma omp atomic read new-line
26375 read-stmt
26377 # pragma omp atomic write new-line
26378 write-stmt
26380 # pragma omp atomic update new-line
26381 update-stmt
26383 # pragma omp atomic capture new-line
26384 capture-stmt
26386 # pragma omp atomic capture new-line
26387 capture-block
26389 read-stmt:
26390 v = x
26391 write-stmt:
26392 x = expr
26393 update-stmt:
26394 expression-stmt | x = x binop expr
26395 capture-stmt:
26396 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
26397 capture-block:
26398 { v = x; update-stmt; } | { update-stmt; v = x; }
26400 where x and v are lvalue expressions with scalar type. */
26402 static void
26403 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
26405 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
26406 tree rhs1 = NULL_TREE, orig_lhs;
26407 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
26408 bool structured_block = false;
26410 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26412 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26413 const char *p = IDENTIFIER_POINTER (id);
26415 if (!strcmp (p, "read"))
26416 code = OMP_ATOMIC_READ;
26417 else if (!strcmp (p, "write"))
26418 code = NOP_EXPR;
26419 else if (!strcmp (p, "update"))
26420 code = OMP_ATOMIC;
26421 else if (!strcmp (p, "capture"))
26422 code = OMP_ATOMIC_CAPTURE_NEW;
26423 else
26424 p = NULL;
26425 if (p)
26426 cp_lexer_consume_token (parser->lexer);
26428 cp_parser_require_pragma_eol (parser, pragma_tok);
26430 switch (code)
26432 case OMP_ATOMIC_READ:
26433 case NOP_EXPR: /* atomic write */
26434 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26435 /*cast_p=*/false, NULL);
26436 if (v == error_mark_node)
26437 goto saw_error;
26438 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26439 goto saw_error;
26440 if (code == NOP_EXPR)
26441 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26442 else
26443 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26444 /*cast_p=*/false, NULL);
26445 if (lhs == error_mark_node)
26446 goto saw_error;
26447 if (code == NOP_EXPR)
26449 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
26450 opcode. */
26451 code = OMP_ATOMIC;
26452 rhs = lhs;
26453 lhs = v;
26454 v = NULL_TREE;
26456 goto done;
26457 case OMP_ATOMIC_CAPTURE_NEW:
26458 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26460 cp_lexer_consume_token (parser->lexer);
26461 structured_block = true;
26463 else
26465 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26466 /*cast_p=*/false, NULL);
26467 if (v == error_mark_node)
26468 goto saw_error;
26469 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26470 goto saw_error;
26472 default:
26473 break;
26476 restart:
26477 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26478 /*cast_p=*/false, NULL);
26479 orig_lhs = lhs;
26480 switch (TREE_CODE (lhs))
26482 case ERROR_MARK:
26483 goto saw_error;
26485 case POSTINCREMENT_EXPR:
26486 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26487 code = OMP_ATOMIC_CAPTURE_OLD;
26488 /* FALLTHROUGH */
26489 case PREINCREMENT_EXPR:
26490 lhs = TREE_OPERAND (lhs, 0);
26491 opcode = PLUS_EXPR;
26492 rhs = integer_one_node;
26493 break;
26495 case POSTDECREMENT_EXPR:
26496 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26497 code = OMP_ATOMIC_CAPTURE_OLD;
26498 /* FALLTHROUGH */
26499 case PREDECREMENT_EXPR:
26500 lhs = TREE_OPERAND (lhs, 0);
26501 opcode = MINUS_EXPR;
26502 rhs = integer_one_node;
26503 break;
26505 case COMPOUND_EXPR:
26506 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
26507 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
26508 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
26509 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
26510 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
26511 (TREE_OPERAND (lhs, 1), 0), 0)))
26512 == BOOLEAN_TYPE)
26513 /* Undo effects of boolean_increment for post {in,de}crement. */
26514 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
26515 /* FALLTHRU */
26516 case MODIFY_EXPR:
26517 if (TREE_CODE (lhs) == MODIFY_EXPR
26518 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
26520 /* Undo effects of boolean_increment. */
26521 if (integer_onep (TREE_OPERAND (lhs, 1)))
26523 /* This is pre or post increment. */
26524 rhs = TREE_OPERAND (lhs, 1);
26525 lhs = TREE_OPERAND (lhs, 0);
26526 opcode = NOP_EXPR;
26527 if (code == OMP_ATOMIC_CAPTURE_NEW
26528 && !structured_block
26529 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
26530 code = OMP_ATOMIC_CAPTURE_OLD;
26531 break;
26534 /* FALLTHRU */
26535 default:
26536 switch (cp_lexer_peek_token (parser->lexer)->type)
26538 case CPP_MULT_EQ:
26539 opcode = MULT_EXPR;
26540 break;
26541 case CPP_DIV_EQ:
26542 opcode = TRUNC_DIV_EXPR;
26543 break;
26544 case CPP_PLUS_EQ:
26545 opcode = PLUS_EXPR;
26546 break;
26547 case CPP_MINUS_EQ:
26548 opcode = MINUS_EXPR;
26549 break;
26550 case CPP_LSHIFT_EQ:
26551 opcode = LSHIFT_EXPR;
26552 break;
26553 case CPP_RSHIFT_EQ:
26554 opcode = RSHIFT_EXPR;
26555 break;
26556 case CPP_AND_EQ:
26557 opcode = BIT_AND_EXPR;
26558 break;
26559 case CPP_OR_EQ:
26560 opcode = BIT_IOR_EXPR;
26561 break;
26562 case CPP_XOR_EQ:
26563 opcode = BIT_XOR_EXPR;
26564 break;
26565 case CPP_EQ:
26566 if (structured_block || code == OMP_ATOMIC)
26568 enum cp_parser_prec oprec;
26569 cp_token *token;
26570 cp_lexer_consume_token (parser->lexer);
26571 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26572 /*cast_p=*/false, NULL);
26573 if (rhs1 == error_mark_node)
26574 goto saw_error;
26575 token = cp_lexer_peek_token (parser->lexer);
26576 switch (token->type)
26578 case CPP_SEMICOLON:
26579 if (code == OMP_ATOMIC_CAPTURE_NEW)
26581 code = OMP_ATOMIC_CAPTURE_OLD;
26582 v = lhs;
26583 lhs = NULL_TREE;
26584 lhs1 = rhs1;
26585 rhs1 = NULL_TREE;
26586 cp_lexer_consume_token (parser->lexer);
26587 goto restart;
26589 cp_parser_error (parser,
26590 "invalid form of %<#pragma omp atomic%>");
26591 goto saw_error;
26592 case CPP_MULT:
26593 opcode = MULT_EXPR;
26594 break;
26595 case CPP_DIV:
26596 opcode = TRUNC_DIV_EXPR;
26597 break;
26598 case CPP_PLUS:
26599 opcode = PLUS_EXPR;
26600 break;
26601 case CPP_MINUS:
26602 opcode = MINUS_EXPR;
26603 break;
26604 case CPP_LSHIFT:
26605 opcode = LSHIFT_EXPR;
26606 break;
26607 case CPP_RSHIFT:
26608 opcode = RSHIFT_EXPR;
26609 break;
26610 case CPP_AND:
26611 opcode = BIT_AND_EXPR;
26612 break;
26613 case CPP_OR:
26614 opcode = BIT_IOR_EXPR;
26615 break;
26616 case CPP_XOR:
26617 opcode = BIT_XOR_EXPR;
26618 break;
26619 default:
26620 cp_parser_error (parser,
26621 "invalid operator for %<#pragma omp atomic%>");
26622 goto saw_error;
26624 oprec = TOKEN_PRECEDENCE (token);
26625 gcc_assert (oprec != PREC_NOT_OPERATOR);
26626 if (commutative_tree_code (opcode))
26627 oprec = (enum cp_parser_prec) (oprec - 1);
26628 cp_lexer_consume_token (parser->lexer);
26629 rhs = cp_parser_binary_expression (parser, false, false,
26630 oprec, NULL);
26631 if (rhs == error_mark_node)
26632 goto saw_error;
26633 goto stmt_done;
26635 /* FALLTHROUGH */
26636 default:
26637 cp_parser_error (parser,
26638 "invalid operator for %<#pragma omp atomic%>");
26639 goto saw_error;
26641 cp_lexer_consume_token (parser->lexer);
26643 rhs = cp_parser_expression (parser, false, NULL);
26644 if (rhs == error_mark_node)
26645 goto saw_error;
26646 break;
26648 stmt_done:
26649 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
26651 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26652 goto saw_error;
26653 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26654 /*cast_p=*/false, NULL);
26655 if (v == error_mark_node)
26656 goto saw_error;
26657 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26658 goto saw_error;
26659 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26660 /*cast_p=*/false, NULL);
26661 if (lhs1 == error_mark_node)
26662 goto saw_error;
26664 if (structured_block)
26666 cp_parser_consume_semicolon_at_end_of_statement (parser);
26667 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26669 done:
26670 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
26671 if (!structured_block)
26672 cp_parser_consume_semicolon_at_end_of_statement (parser);
26673 return;
26675 saw_error:
26676 cp_parser_skip_to_end_of_block_or_statement (parser);
26677 if (structured_block)
26679 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26680 cp_lexer_consume_token (parser->lexer);
26681 else if (code == OMP_ATOMIC_CAPTURE_NEW)
26683 cp_parser_skip_to_end_of_block_or_statement (parser);
26684 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26685 cp_lexer_consume_token (parser->lexer);
26691 /* OpenMP 2.5:
26692 # pragma omp barrier new-line */
26694 static void
26695 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
26697 cp_parser_require_pragma_eol (parser, pragma_tok);
26698 finish_omp_barrier ();
26701 /* OpenMP 2.5:
26702 # pragma omp critical [(name)] new-line
26703 structured-block */
26705 static tree
26706 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
26708 tree stmt, name = NULL;
26710 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26712 cp_lexer_consume_token (parser->lexer);
26714 name = cp_parser_identifier (parser);
26716 if (name == error_mark_node
26717 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26718 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26719 /*or_comma=*/false,
26720 /*consume_paren=*/true);
26721 if (name == error_mark_node)
26722 name = NULL;
26724 cp_parser_require_pragma_eol (parser, pragma_tok);
26726 stmt = cp_parser_omp_structured_block (parser);
26727 return c_finish_omp_critical (input_location, stmt, name);
26730 /* OpenMP 2.5:
26731 # pragma omp flush flush-vars[opt] new-line
26733 flush-vars:
26734 ( variable-list ) */
26736 static void
26737 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26739 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26740 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26741 cp_parser_require_pragma_eol (parser, pragma_tok);
26743 finish_omp_flush ();
26746 /* Helper function, to parse omp for increment expression. */
26748 static tree
26749 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26751 tree cond = cp_parser_binary_expression (parser, false, true,
26752 PREC_NOT_OPERATOR, NULL);
26753 if (cond == error_mark_node
26754 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26756 cp_parser_skip_to_end_of_statement (parser);
26757 return error_mark_node;
26760 switch (TREE_CODE (cond))
26762 case GT_EXPR:
26763 case GE_EXPR:
26764 case LT_EXPR:
26765 case LE_EXPR:
26766 break;
26767 default:
26768 return error_mark_node;
26771 /* If decl is an iterator, preserve LHS and RHS of the relational
26772 expr until finish_omp_for. */
26773 if (decl
26774 && (type_dependent_expression_p (decl)
26775 || CLASS_TYPE_P (TREE_TYPE (decl))))
26776 return cond;
26778 return build_x_binary_op (input_location, TREE_CODE (cond),
26779 TREE_OPERAND (cond, 0), ERROR_MARK,
26780 TREE_OPERAND (cond, 1), ERROR_MARK,
26781 /*overload=*/NULL, tf_warning_or_error);
26784 /* Helper function, to parse omp for increment expression. */
26786 static tree
26787 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26789 cp_token *token = cp_lexer_peek_token (parser->lexer);
26790 enum tree_code op;
26791 tree lhs, rhs;
26792 cp_id_kind idk;
26793 bool decl_first;
26795 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26797 op = (token->type == CPP_PLUS_PLUS
26798 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26799 cp_lexer_consume_token (parser->lexer);
26800 lhs = cp_parser_cast_expression (parser, false, false, NULL);
26801 if (lhs != decl)
26802 return error_mark_node;
26803 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26806 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26807 if (lhs != decl)
26808 return error_mark_node;
26810 token = cp_lexer_peek_token (parser->lexer);
26811 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26813 op = (token->type == CPP_PLUS_PLUS
26814 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26815 cp_lexer_consume_token (parser->lexer);
26816 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26819 op = cp_parser_assignment_operator_opt (parser);
26820 if (op == ERROR_MARK)
26821 return error_mark_node;
26823 if (op != NOP_EXPR)
26825 rhs = cp_parser_assignment_expression (parser, false, NULL);
26826 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26827 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26830 lhs = cp_parser_binary_expression (parser, false, false,
26831 PREC_ADDITIVE_EXPRESSION, NULL);
26832 token = cp_lexer_peek_token (parser->lexer);
26833 decl_first = lhs == decl;
26834 if (decl_first)
26835 lhs = NULL_TREE;
26836 if (token->type != CPP_PLUS
26837 && token->type != CPP_MINUS)
26838 return error_mark_node;
26842 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26843 cp_lexer_consume_token (parser->lexer);
26844 rhs = cp_parser_binary_expression (parser, false, false,
26845 PREC_ADDITIVE_EXPRESSION, NULL);
26846 token = cp_lexer_peek_token (parser->lexer);
26847 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26849 if (lhs == NULL_TREE)
26851 if (op == PLUS_EXPR)
26852 lhs = rhs;
26853 else
26854 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
26855 tf_warning_or_error);
26857 else
26858 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
26859 ERROR_MARK, NULL, tf_warning_or_error);
26862 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26864 if (!decl_first)
26866 if (rhs != decl || op == MINUS_EXPR)
26867 return error_mark_node;
26868 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26870 else
26871 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26873 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26876 /* Parse the restricted form of the for statement allowed by OpenMP. */
26878 static tree
26879 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26881 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26882 tree real_decl, initv, condv, incrv, declv;
26883 tree this_pre_body, cl;
26884 location_t loc_first;
26885 bool collapse_err = false;
26886 int i, collapse = 1, nbraces = 0;
26887 vec<tree, va_gc> *for_block = make_tree_vector ();
26889 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26890 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26891 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26893 gcc_assert (collapse >= 1);
26895 declv = make_tree_vec (collapse);
26896 initv = make_tree_vec (collapse);
26897 condv = make_tree_vec (collapse);
26898 incrv = make_tree_vec (collapse);
26900 loc_first = cp_lexer_peek_token (parser->lexer)->location;
26902 for (i = 0; i < collapse; i++)
26904 int bracecount = 0;
26905 bool add_private_clause = false;
26906 location_t loc;
26908 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26910 cp_parser_error (parser, "for statement expected");
26911 return NULL;
26913 loc = cp_lexer_consume_token (parser->lexer)->location;
26915 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26916 return NULL;
26918 init = decl = real_decl = NULL;
26919 this_pre_body = push_stmt_list ();
26920 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26922 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26924 init-expr:
26925 var = lb
26926 integer-type var = lb
26927 random-access-iterator-type var = lb
26928 pointer-type var = lb
26930 cp_decl_specifier_seq type_specifiers;
26932 /* First, try to parse as an initialized declaration. See
26933 cp_parser_condition, from whence the bulk of this is copied. */
26935 cp_parser_parse_tentatively (parser);
26936 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26937 /*is_trailing_return=*/false,
26938 &type_specifiers);
26939 if (cp_parser_parse_definitely (parser))
26941 /* If parsing a type specifier seq succeeded, then this
26942 MUST be a initialized declaration. */
26943 tree asm_specification, attributes;
26944 cp_declarator *declarator;
26946 declarator = cp_parser_declarator (parser,
26947 CP_PARSER_DECLARATOR_NAMED,
26948 /*ctor_dtor_or_conv_p=*/NULL,
26949 /*parenthesized_p=*/NULL,
26950 /*member_p=*/false);
26951 attributes = cp_parser_attributes_opt (parser);
26952 asm_specification = cp_parser_asm_specification_opt (parser);
26954 if (declarator == cp_error_declarator)
26955 cp_parser_skip_to_end_of_statement (parser);
26957 else
26959 tree pushed_scope, auto_node;
26961 decl = start_decl (declarator, &type_specifiers,
26962 SD_INITIALIZED, attributes,
26963 /*prefix_attributes=*/NULL_TREE,
26964 &pushed_scope);
26966 auto_node = type_uses_auto (TREE_TYPE (decl));
26967 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26969 if (cp_lexer_next_token_is (parser->lexer,
26970 CPP_OPEN_PAREN))
26971 error ("parenthesized initialization is not allowed in "
26972 "OpenMP %<for%> loop");
26973 else
26974 /* Trigger an error. */
26975 cp_parser_require (parser, CPP_EQ, RT_EQ);
26977 init = error_mark_node;
26978 cp_parser_skip_to_end_of_statement (parser);
26980 else if (CLASS_TYPE_P (TREE_TYPE (decl))
26981 || type_dependent_expression_p (decl)
26982 || auto_node)
26984 bool is_direct_init, is_non_constant_init;
26986 init = cp_parser_initializer (parser,
26987 &is_direct_init,
26988 &is_non_constant_init);
26990 if (auto_node)
26992 TREE_TYPE (decl)
26993 = do_auto_deduction (TREE_TYPE (decl), init,
26994 auto_node);
26996 if (!CLASS_TYPE_P (TREE_TYPE (decl))
26997 && !type_dependent_expression_p (decl))
26998 goto non_class;
27001 cp_finish_decl (decl, init, !is_non_constant_init,
27002 asm_specification,
27003 LOOKUP_ONLYCONVERTING);
27004 if (CLASS_TYPE_P (TREE_TYPE (decl)))
27006 vec_safe_push (for_block, this_pre_body);
27007 init = NULL_TREE;
27009 else
27010 init = pop_stmt_list (this_pre_body);
27011 this_pre_body = NULL_TREE;
27013 else
27015 /* Consume '='. */
27016 cp_lexer_consume_token (parser->lexer);
27017 init = cp_parser_assignment_expression (parser, false, NULL);
27019 non_class:
27020 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
27021 init = error_mark_node;
27022 else
27023 cp_finish_decl (decl, NULL_TREE,
27024 /*init_const_expr_p=*/false,
27025 asm_specification,
27026 LOOKUP_ONLYCONVERTING);
27029 if (pushed_scope)
27030 pop_scope (pushed_scope);
27033 else
27035 cp_id_kind idk;
27036 /* If parsing a type specifier sequence failed, then
27037 this MUST be a simple expression. */
27038 cp_parser_parse_tentatively (parser);
27039 decl = cp_parser_primary_expression (parser, false, false,
27040 false, &idk);
27041 if (!cp_parser_error_occurred (parser)
27042 && decl
27043 && DECL_P (decl)
27044 && CLASS_TYPE_P (TREE_TYPE (decl)))
27046 tree rhs;
27048 cp_parser_parse_definitely (parser);
27049 cp_parser_require (parser, CPP_EQ, RT_EQ);
27050 rhs = cp_parser_assignment_expression (parser, false, NULL);
27051 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
27052 decl, NOP_EXPR,
27053 rhs,
27054 tf_warning_or_error));
27055 add_private_clause = true;
27057 else
27059 decl = NULL;
27060 cp_parser_abort_tentative_parse (parser);
27061 init = cp_parser_expression (parser, false, NULL);
27062 if (init)
27064 if (TREE_CODE (init) == MODIFY_EXPR
27065 || TREE_CODE (init) == MODOP_EXPR)
27066 real_decl = TREE_OPERAND (init, 0);
27071 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27072 if (this_pre_body)
27074 this_pre_body = pop_stmt_list (this_pre_body);
27075 if (pre_body)
27077 tree t = pre_body;
27078 pre_body = push_stmt_list ();
27079 add_stmt (t);
27080 add_stmt (this_pre_body);
27081 pre_body = pop_stmt_list (pre_body);
27083 else
27084 pre_body = this_pre_body;
27087 if (decl)
27088 real_decl = decl;
27089 if (par_clauses != NULL && real_decl != NULL_TREE)
27091 tree *c;
27092 for (c = par_clauses; *c ; )
27093 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
27094 && OMP_CLAUSE_DECL (*c) == real_decl)
27096 error_at (loc, "iteration variable %qD"
27097 " should not be firstprivate", real_decl);
27098 *c = OMP_CLAUSE_CHAIN (*c);
27100 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
27101 && OMP_CLAUSE_DECL (*c) == real_decl)
27103 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
27104 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
27105 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
27106 OMP_CLAUSE_DECL (l) = real_decl;
27107 OMP_CLAUSE_CHAIN (l) = clauses;
27108 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
27109 clauses = l;
27110 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
27111 CP_OMP_CLAUSE_INFO (*c) = NULL;
27112 add_private_clause = false;
27114 else
27116 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
27117 && OMP_CLAUSE_DECL (*c) == real_decl)
27118 add_private_clause = false;
27119 c = &OMP_CLAUSE_CHAIN (*c);
27123 if (add_private_clause)
27125 tree c;
27126 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27128 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
27129 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
27130 && OMP_CLAUSE_DECL (c) == decl)
27131 break;
27132 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
27133 && OMP_CLAUSE_DECL (c) == decl)
27134 error_at (loc, "iteration variable %qD "
27135 "should not be firstprivate",
27136 decl);
27137 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
27138 && OMP_CLAUSE_DECL (c) == decl)
27139 error_at (loc, "iteration variable %qD should not be reduction",
27140 decl);
27142 if (c == NULL)
27144 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
27145 OMP_CLAUSE_DECL (c) = decl;
27146 c = finish_omp_clauses (c);
27147 if (c)
27149 OMP_CLAUSE_CHAIN (c) = clauses;
27150 clauses = c;
27155 cond = NULL;
27156 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27157 cond = cp_parser_omp_for_cond (parser, decl);
27158 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27160 incr = NULL;
27161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27163 /* If decl is an iterator, preserve the operator on decl
27164 until finish_omp_for. */
27165 if (real_decl
27166 && ((processing_template_decl
27167 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
27168 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
27169 incr = cp_parser_omp_for_incr (parser, real_decl);
27170 else
27171 incr = cp_parser_expression (parser, false, NULL);
27172 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
27173 SET_EXPR_LOCATION (incr, input_location);
27176 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27177 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27178 /*or_comma=*/false,
27179 /*consume_paren=*/true);
27181 TREE_VEC_ELT (declv, i) = decl;
27182 TREE_VEC_ELT (initv, i) = init;
27183 TREE_VEC_ELT (condv, i) = cond;
27184 TREE_VEC_ELT (incrv, i) = incr;
27186 if (i == collapse - 1)
27187 break;
27189 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
27190 in between the collapsed for loops to be still considered perfectly
27191 nested. Hopefully the final version clarifies this.
27192 For now handle (multiple) {'s and empty statements. */
27193 cp_parser_parse_tentatively (parser);
27196 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27197 break;
27198 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27200 cp_lexer_consume_token (parser->lexer);
27201 bracecount++;
27203 else if (bracecount
27204 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27205 cp_lexer_consume_token (parser->lexer);
27206 else
27208 loc = cp_lexer_peek_token (parser->lexer)->location;
27209 error_at (loc, "not enough collapsed for loops");
27210 collapse_err = true;
27211 cp_parser_abort_tentative_parse (parser);
27212 declv = NULL_TREE;
27213 break;
27216 while (1);
27218 if (declv)
27220 cp_parser_parse_definitely (parser);
27221 nbraces += bracecount;
27225 /* Note that we saved the original contents of this flag when we entered
27226 the structured block, and so we don't need to re-save it here. */
27227 parser->in_statement = IN_OMP_FOR;
27229 /* Note that the grammar doesn't call for a structured block here,
27230 though the loop as a whole is a structured block. */
27231 body = push_stmt_list ();
27232 cp_parser_statement (parser, NULL_TREE, false, NULL);
27233 body = pop_stmt_list (body);
27235 if (declv == NULL_TREE)
27236 ret = NULL_TREE;
27237 else
27238 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
27239 pre_body, clauses);
27241 while (nbraces)
27243 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27245 cp_lexer_consume_token (parser->lexer);
27246 nbraces--;
27248 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27249 cp_lexer_consume_token (parser->lexer);
27250 else
27252 if (!collapse_err)
27254 error_at (cp_lexer_peek_token (parser->lexer)->location,
27255 "collapsed loops not perfectly nested");
27257 collapse_err = true;
27258 cp_parser_statement_seq_opt (parser, NULL);
27259 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27260 break;
27264 while (!for_block->is_empty ())
27265 add_stmt (pop_stmt_list (for_block->pop ()));
27266 release_tree_vector (for_block);
27268 return ret;
27271 /* OpenMP 2.5:
27272 #pragma omp for for-clause[optseq] new-line
27273 for-loop */
27275 #define OMP_FOR_CLAUSE_MASK \
27276 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27277 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27278 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27279 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27280 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
27281 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
27282 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
27283 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
27285 static tree
27286 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
27288 tree clauses, sb, ret;
27289 unsigned int save;
27291 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
27292 "#pragma omp for", pragma_tok);
27294 sb = begin_omp_structured_block ();
27295 save = cp_parser_begin_omp_structured_block (parser);
27297 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
27299 cp_parser_end_omp_structured_block (parser, save);
27300 add_stmt (finish_omp_structured_block (sb));
27302 return ret;
27305 /* OpenMP 2.5:
27306 # pragma omp master new-line
27307 structured-block */
27309 static tree
27310 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
27312 cp_parser_require_pragma_eol (parser, pragma_tok);
27313 return c_finish_omp_master (input_location,
27314 cp_parser_omp_structured_block (parser));
27317 /* OpenMP 2.5:
27318 # pragma omp ordered new-line
27319 structured-block */
27321 static tree
27322 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
27324 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27325 cp_parser_require_pragma_eol (parser, pragma_tok);
27326 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
27329 /* OpenMP 2.5:
27331 section-scope:
27332 { section-sequence }
27334 section-sequence:
27335 section-directive[opt] structured-block
27336 section-sequence section-directive structured-block */
27338 static tree
27339 cp_parser_omp_sections_scope (cp_parser *parser)
27341 tree stmt, substmt;
27342 bool error_suppress = false;
27343 cp_token *tok;
27345 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
27346 return NULL_TREE;
27348 stmt = push_stmt_list ();
27350 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
27352 unsigned save;
27354 substmt = begin_omp_structured_block ();
27355 save = cp_parser_begin_omp_structured_block (parser);
27357 while (1)
27359 cp_parser_statement (parser, NULL_TREE, false, NULL);
27361 tok = cp_lexer_peek_token (parser->lexer);
27362 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27363 break;
27364 if (tok->type == CPP_CLOSE_BRACE)
27365 break;
27366 if (tok->type == CPP_EOF)
27367 break;
27370 cp_parser_end_omp_structured_block (parser, save);
27371 substmt = finish_omp_structured_block (substmt);
27372 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27373 add_stmt (substmt);
27376 while (1)
27378 tok = cp_lexer_peek_token (parser->lexer);
27379 if (tok->type == CPP_CLOSE_BRACE)
27380 break;
27381 if (tok->type == CPP_EOF)
27382 break;
27384 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27386 cp_lexer_consume_token (parser->lexer);
27387 cp_parser_require_pragma_eol (parser, tok);
27388 error_suppress = false;
27390 else if (!error_suppress)
27392 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
27393 error_suppress = true;
27396 substmt = cp_parser_omp_structured_block (parser);
27397 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27398 add_stmt (substmt);
27400 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
27402 substmt = pop_stmt_list (stmt);
27404 stmt = make_node (OMP_SECTIONS);
27405 TREE_TYPE (stmt) = void_type_node;
27406 OMP_SECTIONS_BODY (stmt) = substmt;
27408 add_stmt (stmt);
27409 return stmt;
27412 /* OpenMP 2.5:
27413 # pragma omp sections sections-clause[optseq] newline
27414 sections-scope */
27416 #define OMP_SECTIONS_CLAUSE_MASK \
27417 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27418 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27419 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27420 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27421 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27423 static tree
27424 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
27426 tree clauses, ret;
27428 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
27429 "#pragma omp sections", pragma_tok);
27431 ret = cp_parser_omp_sections_scope (parser);
27432 if (ret)
27433 OMP_SECTIONS_CLAUSES (ret) = clauses;
27435 return ret;
27438 /* OpenMP 2.5:
27439 # pragma parallel parallel-clause new-line
27440 # pragma parallel for parallel-for-clause new-line
27441 # pragma parallel sections parallel-sections-clause new-line */
27443 #define OMP_PARALLEL_CLAUSE_MASK \
27444 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27445 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27446 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27447 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27448 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27449 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
27450 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27451 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
27453 static tree
27454 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
27456 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
27457 const char *p_name = "#pragma omp parallel";
27458 tree stmt, clauses, par_clause, ws_clause, block;
27459 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
27460 unsigned int save;
27461 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27463 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27465 cp_lexer_consume_token (parser->lexer);
27466 p_kind = PRAGMA_OMP_PARALLEL_FOR;
27467 p_name = "#pragma omp parallel for";
27468 mask |= OMP_FOR_CLAUSE_MASK;
27469 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27471 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27473 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27474 const char *p = IDENTIFIER_POINTER (id);
27475 if (strcmp (p, "sections") == 0)
27477 cp_lexer_consume_token (parser->lexer);
27478 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
27479 p_name = "#pragma omp parallel sections";
27480 mask |= OMP_SECTIONS_CLAUSE_MASK;
27481 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27485 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
27486 block = begin_omp_parallel ();
27487 save = cp_parser_begin_omp_structured_block (parser);
27489 switch (p_kind)
27491 case PRAGMA_OMP_PARALLEL:
27492 cp_parser_statement (parser, NULL_TREE, false, NULL);
27493 par_clause = clauses;
27494 break;
27496 case PRAGMA_OMP_PARALLEL_FOR:
27497 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27498 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
27499 break;
27501 case PRAGMA_OMP_PARALLEL_SECTIONS:
27502 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27503 stmt = cp_parser_omp_sections_scope (parser);
27504 if (stmt)
27505 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
27506 break;
27508 default:
27509 gcc_unreachable ();
27512 cp_parser_end_omp_structured_block (parser, save);
27513 stmt = finish_omp_parallel (par_clause, block);
27514 if (p_kind != PRAGMA_OMP_PARALLEL)
27515 OMP_PARALLEL_COMBINED (stmt) = 1;
27516 return stmt;
27519 /* OpenMP 2.5:
27520 # pragma omp single single-clause[optseq] new-line
27521 structured-block */
27523 #define OMP_SINGLE_CLAUSE_MASK \
27524 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27525 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27526 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
27527 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27529 static tree
27530 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
27532 tree stmt = make_node (OMP_SINGLE);
27533 TREE_TYPE (stmt) = void_type_node;
27535 OMP_SINGLE_CLAUSES (stmt)
27536 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
27537 "#pragma omp single", pragma_tok);
27538 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
27540 return add_stmt (stmt);
27543 /* OpenMP 3.0:
27544 # pragma omp task task-clause[optseq] new-line
27545 structured-block */
27547 #define OMP_TASK_CLAUSE_MASK \
27548 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27549 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
27550 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27551 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27552 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27553 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27554 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
27555 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
27557 static tree
27558 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
27560 tree clauses, block;
27561 unsigned int save;
27563 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
27564 "#pragma omp task", pragma_tok);
27565 block = begin_omp_task ();
27566 save = cp_parser_begin_omp_structured_block (parser);
27567 cp_parser_statement (parser, NULL_TREE, false, NULL);
27568 cp_parser_end_omp_structured_block (parser, save);
27569 return finish_omp_task (clauses, block);
27572 /* OpenMP 3.0:
27573 # pragma omp taskwait new-line */
27575 static void
27576 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
27578 cp_parser_require_pragma_eol (parser, pragma_tok);
27579 finish_omp_taskwait ();
27582 /* OpenMP 3.1:
27583 # pragma omp taskyield new-line */
27585 static void
27586 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
27588 cp_parser_require_pragma_eol (parser, pragma_tok);
27589 finish_omp_taskyield ();
27592 /* OpenMP 2.5:
27593 # pragma omp threadprivate (variable-list) */
27595 static void
27596 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
27598 tree vars;
27600 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27601 cp_parser_require_pragma_eol (parser, pragma_tok);
27603 finish_omp_threadprivate (vars);
27606 /* Main entry point to OpenMP statement pragmas. */
27608 static void
27609 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
27611 tree stmt;
27613 switch (pragma_tok->pragma_kind)
27615 case PRAGMA_OMP_ATOMIC:
27616 cp_parser_omp_atomic (parser, pragma_tok);
27617 return;
27618 case PRAGMA_OMP_CRITICAL:
27619 stmt = cp_parser_omp_critical (parser, pragma_tok);
27620 break;
27621 case PRAGMA_OMP_FOR:
27622 stmt = cp_parser_omp_for (parser, pragma_tok);
27623 break;
27624 case PRAGMA_OMP_MASTER:
27625 stmt = cp_parser_omp_master (parser, pragma_tok);
27626 break;
27627 case PRAGMA_OMP_ORDERED:
27628 stmt = cp_parser_omp_ordered (parser, pragma_tok);
27629 break;
27630 case PRAGMA_OMP_PARALLEL:
27631 stmt = cp_parser_omp_parallel (parser, pragma_tok);
27632 break;
27633 case PRAGMA_OMP_SECTIONS:
27634 stmt = cp_parser_omp_sections (parser, pragma_tok);
27635 break;
27636 case PRAGMA_OMP_SINGLE:
27637 stmt = cp_parser_omp_single (parser, pragma_tok);
27638 break;
27639 case PRAGMA_OMP_TASK:
27640 stmt = cp_parser_omp_task (parser, pragma_tok);
27641 break;
27642 default:
27643 gcc_unreachable ();
27646 if (stmt)
27647 SET_EXPR_LOCATION (stmt, pragma_tok->location);
27650 /* Transactional Memory parsing routines. */
27652 /* Parse a transaction attribute.
27654 txn-attribute:
27655 attribute
27656 [ [ identifier ] ]
27658 ??? Simplify this when C++0x bracket attributes are
27659 implemented properly. */
27661 static tree
27662 cp_parser_txn_attribute_opt (cp_parser *parser)
27664 cp_token *token;
27665 tree attr_name, attr = NULL;
27667 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27668 return cp_parser_attributes_opt (parser);
27670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
27671 return NULL_TREE;
27672 cp_lexer_consume_token (parser->lexer);
27673 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
27674 goto error1;
27676 token = cp_lexer_peek_token (parser->lexer);
27677 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
27679 token = cp_lexer_consume_token (parser->lexer);
27681 attr_name = (token->type == CPP_KEYWORD
27682 /* For keywords, use the canonical spelling,
27683 not the parsed identifier. */
27684 ? ridpointers[(int) token->keyword]
27685 : token->u.value);
27686 attr = build_tree_list (attr_name, NULL_TREE);
27688 else
27689 cp_parser_error (parser, "expected identifier");
27691 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27692 error1:
27693 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27694 return attr;
27697 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27699 transaction-statement:
27700 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27701 compound-statement
27702 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27705 static tree
27706 cp_parser_transaction (cp_parser *parser, enum rid keyword)
27708 unsigned char old_in = parser->in_transaction;
27709 unsigned char this_in = 1, new_in;
27710 cp_token *token;
27711 tree stmt, attrs, noex;
27713 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27714 || keyword == RID_TRANSACTION_RELAXED);
27715 token = cp_parser_require_keyword (parser, keyword,
27716 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27717 : RT_TRANSACTION_RELAXED));
27718 gcc_assert (token != NULL);
27720 if (keyword == RID_TRANSACTION_RELAXED)
27721 this_in |= TM_STMT_ATTR_RELAXED;
27722 else
27724 attrs = cp_parser_txn_attribute_opt (parser);
27725 if (attrs)
27726 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27729 /* Parse a noexcept specification. */
27730 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27732 /* Keep track if we're in the lexical scope of an outer transaction. */
27733 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27735 stmt = begin_transaction_stmt (token->location, NULL, this_in);
27737 parser->in_transaction = new_in;
27738 cp_parser_compound_statement (parser, NULL, false, false);
27739 parser->in_transaction = old_in;
27741 finish_transaction_stmt (stmt, NULL, this_in, noex);
27743 return stmt;
27746 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27748 transaction-expression:
27749 __transaction_atomic txn-noexcept-spec[opt] ( expression )
27750 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27753 static tree
27754 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27756 unsigned char old_in = parser->in_transaction;
27757 unsigned char this_in = 1;
27758 cp_token *token;
27759 tree expr, noex;
27760 bool noex_expr;
27762 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27763 || keyword == RID_TRANSACTION_RELAXED);
27765 if (!flag_tm)
27766 error (keyword == RID_TRANSACTION_RELAXED
27767 ? G_("%<__transaction_relaxed%> without transactional memory "
27768 "support enabled")
27769 : G_("%<__transaction_atomic%> without transactional memory "
27770 "support enabled"));
27772 token = cp_parser_require_keyword (parser, keyword,
27773 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27774 : RT_TRANSACTION_RELAXED));
27775 gcc_assert (token != NULL);
27777 if (keyword == RID_TRANSACTION_RELAXED)
27778 this_in |= TM_STMT_ATTR_RELAXED;
27780 /* Set this early. This might mean that we allow transaction_cancel in
27781 an expression that we find out later actually has to be a constexpr.
27782 However, we expect that cxx_constant_value will be able to deal with
27783 this; also, if the noexcept has no constexpr, then what we parse next
27784 really is a transaction's body. */
27785 parser->in_transaction = this_in;
27787 /* Parse a noexcept specification. */
27788 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27789 true);
27791 if (!noex || !noex_expr
27792 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27794 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27796 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27797 finish_parenthesized_expr (expr);
27799 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27801 else
27803 /* The only expression that is available got parsed for the noexcept
27804 already. noexcept is true then. */
27805 expr = noex;
27806 noex = boolean_true_node;
27809 expr = build_transaction_expr (token->location, expr, this_in, noex);
27810 parser->in_transaction = old_in;
27812 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27813 return error_mark_node;
27815 return (flag_tm ? expr : error_mark_node);
27818 /* Parse a function-transaction-block.
27820 function-transaction-block:
27821 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27822 function-body
27823 __transaction_atomic txn-attribute[opt] function-try-block
27824 __transaction_relaxed ctor-initializer[opt] function-body
27825 __transaction_relaxed function-try-block
27828 static bool
27829 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27831 unsigned char old_in = parser->in_transaction;
27832 unsigned char new_in = 1;
27833 tree compound_stmt, stmt, attrs;
27834 bool ctor_initializer_p;
27835 cp_token *token;
27837 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27838 || keyword == RID_TRANSACTION_RELAXED);
27839 token = cp_parser_require_keyword (parser, keyword,
27840 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27841 : RT_TRANSACTION_RELAXED));
27842 gcc_assert (token != NULL);
27844 if (keyword == RID_TRANSACTION_RELAXED)
27845 new_in |= TM_STMT_ATTR_RELAXED;
27846 else
27848 attrs = cp_parser_txn_attribute_opt (parser);
27849 if (attrs)
27850 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27853 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27855 parser->in_transaction = new_in;
27857 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27858 ctor_initializer_p = cp_parser_function_try_block (parser);
27859 else
27860 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
27861 (parser, /*in_function_try_block=*/false);
27863 parser->in_transaction = old_in;
27865 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27867 return ctor_initializer_p;
27870 /* Parse a __transaction_cancel statement.
27872 cancel-statement:
27873 __transaction_cancel txn-attribute[opt] ;
27874 __transaction_cancel txn-attribute[opt] throw-expression ;
27876 ??? Cancel and throw is not yet implemented. */
27878 static tree
27879 cp_parser_transaction_cancel (cp_parser *parser)
27881 cp_token *token;
27882 bool is_outer = false;
27883 tree stmt, attrs;
27885 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27886 RT_TRANSACTION_CANCEL);
27887 gcc_assert (token != NULL);
27889 attrs = cp_parser_txn_attribute_opt (parser);
27890 if (attrs)
27891 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27893 /* ??? Parse cancel-and-throw here. */
27895 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27897 if (!flag_tm)
27899 error_at (token->location, "%<__transaction_cancel%> without "
27900 "transactional memory support enabled");
27901 return error_mark_node;
27903 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27905 error_at (token->location, "%<__transaction_cancel%> within a "
27906 "%<__transaction_relaxed%>");
27907 return error_mark_node;
27909 else if (is_outer)
27911 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27912 && !is_tm_may_cancel_outer (current_function_decl))
27914 error_at (token->location, "outer %<__transaction_cancel%> not "
27915 "within outer %<__transaction_atomic%>");
27916 error_at (token->location,
27917 " or a %<transaction_may_cancel_outer%> function");
27918 return error_mark_node;
27921 else if (parser->in_transaction == 0)
27923 error_at (token->location, "%<__transaction_cancel%> not within "
27924 "%<__transaction_atomic%>");
27925 return error_mark_node;
27928 stmt = build_tm_abort_call (token->location, is_outer);
27929 add_stmt (stmt);
27930 finish_stmt ();
27932 return stmt;
27935 /* The parser. */
27937 static GTY (()) cp_parser *the_parser;
27940 /* Special handling for the first token or line in the file. The first
27941 thing in the file might be #pragma GCC pch_preprocess, which loads a
27942 PCH file, which is a GC collection point. So we need to handle this
27943 first pragma without benefit of an existing lexer structure.
27945 Always returns one token to the caller in *FIRST_TOKEN. This is
27946 either the true first token of the file, or the first token after
27947 the initial pragma. */
27949 static void
27950 cp_parser_initial_pragma (cp_token *first_token)
27952 tree name = NULL;
27954 cp_lexer_get_preprocessor_token (NULL, first_token);
27955 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27956 return;
27958 cp_lexer_get_preprocessor_token (NULL, first_token);
27959 if (first_token->type == CPP_STRING)
27961 name = first_token->u.value;
27963 cp_lexer_get_preprocessor_token (NULL, first_token);
27964 if (first_token->type != CPP_PRAGMA_EOL)
27965 error_at (first_token->location,
27966 "junk at end of %<#pragma GCC pch_preprocess%>");
27968 else
27969 error_at (first_token->location, "expected string literal");
27971 /* Skip to the end of the pragma. */
27972 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27973 cp_lexer_get_preprocessor_token (NULL, first_token);
27975 /* Now actually load the PCH file. */
27976 if (name)
27977 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27979 /* Read one more token to return to our caller. We have to do this
27980 after reading the PCH file in, since its pointers have to be
27981 live. */
27982 cp_lexer_get_preprocessor_token (NULL, first_token);
27985 /* Normal parsing of a pragma token. Here we can (and must) use the
27986 regular lexer. */
27988 static bool
27989 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27991 cp_token *pragma_tok;
27992 unsigned int id;
27994 pragma_tok = cp_lexer_consume_token (parser->lexer);
27995 gcc_assert (pragma_tok->type == CPP_PRAGMA);
27996 parser->lexer->in_pragma = true;
27998 id = pragma_tok->pragma_kind;
27999 switch (id)
28001 case PRAGMA_GCC_PCH_PREPROCESS:
28002 error_at (pragma_tok->location,
28003 "%<#pragma GCC pch_preprocess%> must be first");
28004 break;
28006 case PRAGMA_OMP_BARRIER:
28007 switch (context)
28009 case pragma_compound:
28010 cp_parser_omp_barrier (parser, pragma_tok);
28011 return false;
28012 case pragma_stmt:
28013 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
28014 "used in compound statements");
28015 break;
28016 default:
28017 goto bad_stmt;
28019 break;
28021 case PRAGMA_OMP_FLUSH:
28022 switch (context)
28024 case pragma_compound:
28025 cp_parser_omp_flush (parser, pragma_tok);
28026 return false;
28027 case pragma_stmt:
28028 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
28029 "used in compound statements");
28030 break;
28031 default:
28032 goto bad_stmt;
28034 break;
28036 case PRAGMA_OMP_TASKWAIT:
28037 switch (context)
28039 case pragma_compound:
28040 cp_parser_omp_taskwait (parser, pragma_tok);
28041 return false;
28042 case pragma_stmt:
28043 error_at (pragma_tok->location,
28044 "%<#pragma omp taskwait%> may only be "
28045 "used in compound statements");
28046 break;
28047 default:
28048 goto bad_stmt;
28050 break;
28052 case PRAGMA_OMP_TASKYIELD:
28053 switch (context)
28055 case pragma_compound:
28056 cp_parser_omp_taskyield (parser, pragma_tok);
28057 return false;
28058 case pragma_stmt:
28059 error_at (pragma_tok->location,
28060 "%<#pragma omp taskyield%> may only be "
28061 "used in compound statements");
28062 break;
28063 default:
28064 goto bad_stmt;
28066 break;
28068 case PRAGMA_OMP_THREADPRIVATE:
28069 cp_parser_omp_threadprivate (parser, pragma_tok);
28070 return false;
28072 case PRAGMA_OMP_ATOMIC:
28073 case PRAGMA_OMP_CRITICAL:
28074 case PRAGMA_OMP_FOR:
28075 case PRAGMA_OMP_MASTER:
28076 case PRAGMA_OMP_ORDERED:
28077 case PRAGMA_OMP_PARALLEL:
28078 case PRAGMA_OMP_SECTIONS:
28079 case PRAGMA_OMP_SINGLE:
28080 case PRAGMA_OMP_TASK:
28081 if (context == pragma_external)
28082 goto bad_stmt;
28083 cp_parser_omp_construct (parser, pragma_tok);
28084 return true;
28086 case PRAGMA_OMP_SECTION:
28087 error_at (pragma_tok->location,
28088 "%<#pragma omp section%> may only be used in "
28089 "%<#pragma omp sections%> construct");
28090 break;
28092 default:
28093 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
28094 c_invoke_pragma_handler (id);
28095 break;
28097 bad_stmt:
28098 cp_parser_error (parser, "expected declaration specifiers");
28099 break;
28102 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28103 return false;
28106 /* The interface the pragma parsers have to the lexer. */
28108 enum cpp_ttype
28109 pragma_lex (tree *value)
28111 cp_token *tok;
28112 enum cpp_ttype ret;
28114 tok = cp_lexer_peek_token (the_parser->lexer);
28116 ret = tok->type;
28117 *value = tok->u.value;
28119 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
28120 ret = CPP_EOF;
28121 else if (ret == CPP_STRING)
28122 *value = cp_parser_string_literal (the_parser, false, false);
28123 else
28125 cp_lexer_consume_token (the_parser->lexer);
28126 if (ret == CPP_KEYWORD)
28127 ret = CPP_NAME;
28130 return ret;
28134 /* External interface. */
28136 /* Parse one entire translation unit. */
28138 void
28139 c_parse_file (void)
28141 static bool already_called = false;
28143 if (already_called)
28145 sorry ("inter-module optimizations not implemented for C++");
28146 return;
28148 already_called = true;
28150 the_parser = cp_parser_new ();
28151 push_deferring_access_checks (flag_access_control
28152 ? dk_no_deferred : dk_no_check);
28153 cp_parser_translation_unit (the_parser);
28154 the_parser = NULL;
28157 #include "gt-cp-parser.h"