/cp
[official-gcc.git] / gcc / cp / parser.c
bloba2d8062d7eb0dfc3a8f7364225e1599b04213422
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, NULL_TREE);
3533 tok->u.value = literal;
3534 return cp_parser_userdef_string_literal (tok);
3537 else
3538 /* cpp_interpret_string has issued an error. */
3539 value = error_mark_node;
3541 if (count > 1)
3542 obstack_free (&str_ob, 0);
3544 return value;
3547 /* Look up a literal operator with the name and the exact arguments. */
3549 static tree
3550 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3552 tree decl, fns;
3553 decl = lookup_name (name);
3554 if (!decl || !is_overloaded_fn (decl))
3555 return error_mark_node;
3557 for (fns = decl; fns; fns = OVL_NEXT (fns))
3559 unsigned int ix;
3560 bool found = true;
3561 tree fn = OVL_CURRENT (fns);
3562 tree argtypes = NULL_TREE;
3563 argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3564 if (argtypes != NULL_TREE)
3566 for (ix = 0; ix < vec_safe_length (args) && argtypes != NULL_TREE;
3567 ++ix, argtypes = TREE_CHAIN (argtypes))
3569 tree targ = TREE_VALUE (argtypes);
3570 tree tparm = TREE_TYPE ((*args)[ix]);
3571 bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3572 bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3573 if ((ptr || arr || !same_type_p (targ, tparm))
3574 && (!ptr || !arr
3575 || !same_type_p (TREE_TYPE (targ),
3576 TREE_TYPE (tparm))))
3577 found = false;
3579 if (found
3580 && ix == vec_safe_length (args)
3581 /* May be this should be sufficient_parms_p instead,
3582 depending on how exactly should user-defined literals
3583 work in presence of default arguments on the literal
3584 operator parameters. */
3585 && argtypes == void_list_node)
3586 return fn;
3590 return error_mark_node;
3593 /* Parse a user-defined char constant. Returns a call to a user-defined
3594 literal operator taking the character as an argument. */
3596 static tree
3597 cp_parser_userdef_char_literal (cp_parser *parser)
3599 cp_token *token = cp_lexer_consume_token (parser->lexer);
3600 tree literal = token->u.value;
3601 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3602 tree value = USERDEF_LITERAL_VALUE (literal);
3603 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3604 tree decl, result;
3606 /* Build up a call to the user-defined operator */
3607 /* Lookup the name we got back from the id-expression. */
3608 vec<tree, va_gc> *args = make_tree_vector ();
3609 vec_safe_push (args, value);
3610 decl = lookup_literal_operator (name, args);
3611 if (!decl || decl == error_mark_node)
3613 error ("unable to find character literal operator %qD with %qT argument",
3614 name, TREE_TYPE (value));
3615 release_tree_vector (args);
3616 return error_mark_node;
3618 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3619 release_tree_vector (args);
3620 if (result != error_mark_node)
3621 return result;
3623 error ("unable to find character literal operator %qD with %qT argument",
3624 name, TREE_TYPE (value));
3625 return error_mark_node;
3628 /* A subroutine of cp_parser_userdef_numeric_literal to
3629 create a char... template parameter pack from a string node. */
3631 static tree
3632 make_char_string_pack (tree value)
3634 tree charvec;
3635 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3636 const char *str = TREE_STRING_POINTER (value);
3637 int i, len = TREE_STRING_LENGTH (value) - 1;
3638 tree argvec = make_tree_vec (1);
3640 /* Fill in CHARVEC with all of the parameters. */
3641 charvec = make_tree_vec (len);
3642 for (i = 0; i < len; ++i)
3643 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3645 /* Build the argument packs. */
3646 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3647 TREE_TYPE (argpack) = char_type_node;
3649 TREE_VEC_ELT (argvec, 0) = argpack;
3651 return argvec;
3654 /* Parse a user-defined numeric constant. returns a call to a user-defined
3655 literal operator. */
3657 static tree
3658 cp_parser_userdef_numeric_literal (cp_parser *parser)
3660 cp_token *token = cp_lexer_consume_token (parser->lexer);
3661 tree literal = token->u.value;
3662 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3663 tree value = USERDEF_LITERAL_VALUE (literal);
3664 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3665 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3666 tree decl, result;
3667 vec<tree, va_gc> *args;
3669 /* Look for a literal operator taking the exact type of numeric argument
3670 as the literal value. */
3671 args = make_tree_vector ();
3672 vec_safe_push (args, value);
3673 decl = lookup_literal_operator (name, args);
3674 if (decl && decl != error_mark_node)
3676 result = finish_call_expr (decl, &args, false, true, tf_none);
3677 if (result != error_mark_node)
3679 release_tree_vector (args);
3680 return result;
3683 release_tree_vector (args);
3685 /* If the numeric argument didn't work, look for a raw literal
3686 operator taking a const char* argument consisting of the number
3687 in string format. */
3688 args = make_tree_vector ();
3689 vec_safe_push (args, num_string);
3690 decl = lookup_literal_operator (name, args);
3691 if (decl && decl != error_mark_node)
3693 result = finish_call_expr (decl, &args, false, true, tf_none);
3694 if (result != error_mark_node)
3696 release_tree_vector (args);
3697 return result;
3700 release_tree_vector (args);
3702 /* If the raw literal didn't work, look for a non-type template
3703 function with parameter pack char.... Call the function with
3704 template parameter characters representing the number. */
3705 args = make_tree_vector ();
3706 decl = lookup_literal_operator (name, args);
3707 if (decl && decl != error_mark_node)
3709 tree tmpl_args = make_char_string_pack (num_string);
3710 decl = lookup_template_function (decl, tmpl_args);
3711 result = finish_call_expr (decl, &args, false, true, tf_none);
3712 if (result != error_mark_node)
3714 release_tree_vector (args);
3715 return result;
3718 release_tree_vector (args);
3720 error ("unable to find numeric literal operator %qD", name);
3721 return error_mark_node;
3724 /* Parse a user-defined string constant. Returns a call to a user-defined
3725 literal operator taking a character pointer and the length of the string
3726 as arguments. */
3728 static tree
3729 cp_parser_userdef_string_literal (cp_token *token)
3731 tree literal = token->u.value;
3732 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3733 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3734 tree value = USERDEF_LITERAL_VALUE (literal);
3735 int len = TREE_STRING_LENGTH (value)
3736 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3737 tree decl, result;
3739 /* Build up a call to the user-defined operator */
3740 /* Lookup the name we got back from the id-expression. */
3741 vec<tree, va_gc> *args = make_tree_vector ();
3742 vec_safe_push (args, value);
3743 vec_safe_push (args, build_int_cst (size_type_node, len));
3744 decl = lookup_name (name);
3745 if (!decl || decl == error_mark_node)
3747 error ("unable to find string literal operator %qD", name);
3748 release_tree_vector (args);
3749 return error_mark_node;
3751 result = finish_call_expr (decl, &args, false, true, tf_none);
3752 release_tree_vector (args);
3753 if (result != error_mark_node)
3754 return result;
3756 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3757 name, TREE_TYPE (value), size_type_node);
3758 return error_mark_node;
3762 /* Basic concepts [gram.basic] */
3764 /* Parse a translation-unit.
3766 translation-unit:
3767 declaration-seq [opt]
3769 Returns TRUE if all went well. */
3771 static bool
3772 cp_parser_translation_unit (cp_parser* parser)
3774 /* The address of the first non-permanent object on the declarator
3775 obstack. */
3776 static void *declarator_obstack_base;
3778 bool success;
3780 /* Create the declarator obstack, if necessary. */
3781 if (!cp_error_declarator)
3783 gcc_obstack_init (&declarator_obstack);
3784 /* Create the error declarator. */
3785 cp_error_declarator = make_declarator (cdk_error);
3786 /* Create the empty parameter list. */
3787 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3788 /* Remember where the base of the declarator obstack lies. */
3789 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3792 cp_parser_declaration_seq_opt (parser);
3794 /* If there are no tokens left then all went well. */
3795 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3797 /* Get rid of the token array; we don't need it any more. */
3798 cp_lexer_destroy (parser->lexer);
3799 parser->lexer = NULL;
3801 /* This file might have been a context that's implicitly extern
3802 "C". If so, pop the lang context. (Only relevant for PCH.) */
3803 if (parser->implicit_extern_c)
3805 pop_lang_context ();
3806 parser->implicit_extern_c = false;
3809 /* Finish up. */
3810 finish_translation_unit ();
3812 success = true;
3814 else
3816 cp_parser_error (parser, "expected declaration");
3817 success = false;
3820 /* Make sure the declarator obstack was fully cleaned up. */
3821 gcc_assert (obstack_next_free (&declarator_obstack)
3822 == declarator_obstack_base);
3824 /* All went well. */
3825 return success;
3828 /* Expressions [gram.expr] */
3830 /* Parse a primary-expression.
3832 primary-expression:
3833 literal
3834 this
3835 ( expression )
3836 id-expression
3838 GNU Extensions:
3840 primary-expression:
3841 ( compound-statement )
3842 __builtin_va_arg ( assignment-expression , type-id )
3843 __builtin_offsetof ( type-id , offsetof-expression )
3845 C++ Extensions:
3846 __has_nothrow_assign ( type-id )
3847 __has_nothrow_constructor ( type-id )
3848 __has_nothrow_copy ( type-id )
3849 __has_trivial_assign ( type-id )
3850 __has_trivial_constructor ( type-id )
3851 __has_trivial_copy ( type-id )
3852 __has_trivial_destructor ( type-id )
3853 __has_virtual_destructor ( type-id )
3854 __is_abstract ( type-id )
3855 __is_base_of ( type-id , type-id )
3856 __is_class ( type-id )
3857 __is_convertible_to ( type-id , type-id )
3858 __is_empty ( type-id )
3859 __is_enum ( type-id )
3860 __is_final ( type-id )
3861 __is_literal_type ( type-id )
3862 __is_pod ( type-id )
3863 __is_polymorphic ( type-id )
3864 __is_std_layout ( type-id )
3865 __is_trivial ( type-id )
3866 __is_union ( type-id )
3868 Objective-C++ Extension:
3870 primary-expression:
3871 objc-expression
3873 literal:
3874 __null
3876 ADDRESS_P is true iff this expression was immediately preceded by
3877 "&" and therefore might denote a pointer-to-member. CAST_P is true
3878 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3879 true iff this expression is a template argument.
3881 Returns a representation of the expression. Upon return, *IDK
3882 indicates what kind of id-expression (if any) was present. */
3884 static tree
3885 cp_parser_primary_expression (cp_parser *parser,
3886 bool address_p,
3887 bool cast_p,
3888 bool template_arg_p,
3889 cp_id_kind *idk)
3891 cp_token *token = NULL;
3893 /* Assume the primary expression is not an id-expression. */
3894 *idk = CP_ID_KIND_NONE;
3896 /* Peek at the next token. */
3897 token = cp_lexer_peek_token (parser->lexer);
3898 switch (token->type)
3900 /* literal:
3901 integer-literal
3902 character-literal
3903 floating-literal
3904 string-literal
3905 boolean-literal
3906 pointer-literal
3907 user-defined-literal */
3908 case CPP_CHAR:
3909 case CPP_CHAR16:
3910 case CPP_CHAR32:
3911 case CPP_WCHAR:
3912 case CPP_NUMBER:
3913 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3914 return cp_parser_userdef_numeric_literal (parser);
3915 token = cp_lexer_consume_token (parser->lexer);
3916 if (TREE_CODE (token->u.value) == FIXED_CST)
3918 error_at (token->location,
3919 "fixed-point types not supported in C++");
3920 return error_mark_node;
3922 /* Floating-point literals are only allowed in an integral
3923 constant expression if they are cast to an integral or
3924 enumeration type. */
3925 if (TREE_CODE (token->u.value) == REAL_CST
3926 && parser->integral_constant_expression_p
3927 && pedantic)
3929 /* CAST_P will be set even in invalid code like "int(2.7 +
3930 ...)". Therefore, we have to check that the next token
3931 is sure to end the cast. */
3932 if (cast_p)
3934 cp_token *next_token;
3936 next_token = cp_lexer_peek_token (parser->lexer);
3937 if (/* The comma at the end of an
3938 enumerator-definition. */
3939 next_token->type != CPP_COMMA
3940 /* The curly brace at the end of an enum-specifier. */
3941 && next_token->type != CPP_CLOSE_BRACE
3942 /* The end of a statement. */
3943 && next_token->type != CPP_SEMICOLON
3944 /* The end of the cast-expression. */
3945 && next_token->type != CPP_CLOSE_PAREN
3946 /* The end of an array bound. */
3947 && next_token->type != CPP_CLOSE_SQUARE
3948 /* The closing ">" in a template-argument-list. */
3949 && (next_token->type != CPP_GREATER
3950 || parser->greater_than_is_operator_p)
3951 /* C++0x only: A ">>" treated like two ">" tokens,
3952 in a template-argument-list. */
3953 && (next_token->type != CPP_RSHIFT
3954 || (cxx_dialect == cxx98)
3955 || parser->greater_than_is_operator_p))
3956 cast_p = false;
3959 /* If we are within a cast, then the constraint that the
3960 cast is to an integral or enumeration type will be
3961 checked at that point. If we are not within a cast, then
3962 this code is invalid. */
3963 if (!cast_p)
3964 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3966 return token->u.value;
3968 case CPP_CHAR_USERDEF:
3969 case CPP_CHAR16_USERDEF:
3970 case CPP_CHAR32_USERDEF:
3971 case CPP_WCHAR_USERDEF:
3972 return cp_parser_userdef_char_literal (parser);
3974 case CPP_STRING:
3975 case CPP_STRING16:
3976 case CPP_STRING32:
3977 case CPP_WSTRING:
3978 case CPP_UTF8STRING:
3979 case CPP_STRING_USERDEF:
3980 case CPP_STRING16_USERDEF:
3981 case CPP_STRING32_USERDEF:
3982 case CPP_WSTRING_USERDEF:
3983 case CPP_UTF8STRING_USERDEF:
3984 /* ??? Should wide strings be allowed when parser->translate_strings_p
3985 is false (i.e. in attributes)? If not, we can kill the third
3986 argument to cp_parser_string_literal. */
3987 return cp_parser_string_literal (parser,
3988 parser->translate_strings_p,
3989 true);
3991 case CPP_OPEN_PAREN:
3993 tree expr;
3994 bool saved_greater_than_is_operator_p;
3996 /* Consume the `('. */
3997 cp_lexer_consume_token (parser->lexer);
3998 /* Within a parenthesized expression, a `>' token is always
3999 the greater-than operator. */
4000 saved_greater_than_is_operator_p
4001 = parser->greater_than_is_operator_p;
4002 parser->greater_than_is_operator_p = true;
4003 /* If we see `( { ' then we are looking at the beginning of
4004 a GNU statement-expression. */
4005 if (cp_parser_allow_gnu_extensions_p (parser)
4006 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4008 /* Statement-expressions are not allowed by the standard. */
4009 pedwarn (token->location, OPT_Wpedantic,
4010 "ISO C++ forbids braced-groups within expressions");
4012 /* And they're not allowed outside of a function-body; you
4013 cannot, for example, write:
4015 int i = ({ int j = 3; j + 1; });
4017 at class or namespace scope. */
4018 if (!parser->in_function_body
4019 || parser->in_template_argument_list_p)
4021 error_at (token->location,
4022 "statement-expressions are not allowed outside "
4023 "functions nor in template-argument lists");
4024 cp_parser_skip_to_end_of_block_or_statement (parser);
4025 expr = error_mark_node;
4027 else
4029 /* Start the statement-expression. */
4030 expr = begin_stmt_expr ();
4031 /* Parse the compound-statement. */
4032 cp_parser_compound_statement (parser, expr, false, false);
4033 /* Finish up. */
4034 expr = finish_stmt_expr (expr, false);
4037 else
4039 /* Parse the parenthesized expression. */
4040 expr = cp_parser_expression (parser, cast_p, idk);
4041 /* Let the front end know that this expression was
4042 enclosed in parentheses. This matters in case, for
4043 example, the expression is of the form `A::B', since
4044 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4045 not. */
4046 finish_parenthesized_expr (expr);
4047 /* DR 705: Wrapping an unqualified name in parentheses
4048 suppresses arg-dependent lookup. We want to pass back
4049 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4050 (c++/37862), but none of the others. */
4051 if (*idk != CP_ID_KIND_QUALIFIED)
4052 *idk = CP_ID_KIND_NONE;
4054 /* The `>' token might be the end of a template-id or
4055 template-parameter-list now. */
4056 parser->greater_than_is_operator_p
4057 = saved_greater_than_is_operator_p;
4058 /* Consume the `)'. */
4059 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4060 cp_parser_skip_to_end_of_statement (parser);
4062 return expr;
4065 case CPP_OPEN_SQUARE:
4066 if (c_dialect_objc ())
4067 /* We have an Objective-C++ message. */
4068 return cp_parser_objc_expression (parser);
4070 tree lam = cp_parser_lambda_expression (parser);
4071 /* Don't warn about a failed tentative parse. */
4072 if (cp_parser_error_occurred (parser))
4073 return error_mark_node;
4074 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4075 return lam;
4078 case CPP_OBJC_STRING:
4079 if (c_dialect_objc ())
4080 /* We have an Objective-C++ string literal. */
4081 return cp_parser_objc_expression (parser);
4082 cp_parser_error (parser, "expected primary-expression");
4083 return error_mark_node;
4085 case CPP_KEYWORD:
4086 switch (token->keyword)
4088 /* These two are the boolean literals. */
4089 case RID_TRUE:
4090 cp_lexer_consume_token (parser->lexer);
4091 return boolean_true_node;
4092 case RID_FALSE:
4093 cp_lexer_consume_token (parser->lexer);
4094 return boolean_false_node;
4096 /* The `__null' literal. */
4097 case RID_NULL:
4098 cp_lexer_consume_token (parser->lexer);
4099 return null_node;
4101 /* The `nullptr' literal. */
4102 case RID_NULLPTR:
4103 cp_lexer_consume_token (parser->lexer);
4104 return nullptr_node;
4106 /* Recognize the `this' keyword. */
4107 case RID_THIS:
4108 cp_lexer_consume_token (parser->lexer);
4109 if (parser->local_variables_forbidden_p)
4111 error_at (token->location,
4112 "%<this%> may not be used in this context");
4113 return error_mark_node;
4115 /* Pointers cannot appear in constant-expressions. */
4116 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4117 return error_mark_node;
4118 return finish_this_expr ();
4120 /* The `operator' keyword can be the beginning of an
4121 id-expression. */
4122 case RID_OPERATOR:
4123 goto id_expression;
4125 case RID_FUNCTION_NAME:
4126 case RID_PRETTY_FUNCTION_NAME:
4127 case RID_C99_FUNCTION_NAME:
4129 non_integral_constant name;
4131 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4132 __func__ are the names of variables -- but they are
4133 treated specially. Therefore, they are handled here,
4134 rather than relying on the generic id-expression logic
4135 below. Grammatically, these names are id-expressions.
4137 Consume the token. */
4138 token = cp_lexer_consume_token (parser->lexer);
4140 switch (token->keyword)
4142 case RID_FUNCTION_NAME:
4143 name = NIC_FUNC_NAME;
4144 break;
4145 case RID_PRETTY_FUNCTION_NAME:
4146 name = NIC_PRETTY_FUNC;
4147 break;
4148 case RID_C99_FUNCTION_NAME:
4149 name = NIC_C99_FUNC;
4150 break;
4151 default:
4152 gcc_unreachable ();
4155 if (cp_parser_non_integral_constant_expression (parser, name))
4156 return error_mark_node;
4158 /* Look up the name. */
4159 return finish_fname (token->u.value);
4162 case RID_VA_ARG:
4164 tree expression;
4165 tree type;
4166 source_location type_location;
4168 /* The `__builtin_va_arg' construct is used to handle
4169 `va_arg'. Consume the `__builtin_va_arg' token. */
4170 cp_lexer_consume_token (parser->lexer);
4171 /* Look for the opening `('. */
4172 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4173 /* Now, parse the assignment-expression. */
4174 expression = cp_parser_assignment_expression (parser,
4175 /*cast_p=*/false, NULL);
4176 /* Look for the `,'. */
4177 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4178 type_location = cp_lexer_peek_token (parser->lexer)->location;
4179 /* Parse the type-id. */
4180 type = cp_parser_type_id (parser);
4181 /* Look for the closing `)'. */
4182 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4183 /* Using `va_arg' in a constant-expression is not
4184 allowed. */
4185 if (cp_parser_non_integral_constant_expression (parser,
4186 NIC_VA_ARG))
4187 return error_mark_node;
4188 return build_x_va_arg (type_location, expression, type);
4191 case RID_OFFSETOF:
4192 return cp_parser_builtin_offsetof (parser);
4194 case RID_HAS_NOTHROW_ASSIGN:
4195 case RID_HAS_NOTHROW_CONSTRUCTOR:
4196 case RID_HAS_NOTHROW_COPY:
4197 case RID_HAS_TRIVIAL_ASSIGN:
4198 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4199 case RID_HAS_TRIVIAL_COPY:
4200 case RID_HAS_TRIVIAL_DESTRUCTOR:
4201 case RID_HAS_VIRTUAL_DESTRUCTOR:
4202 case RID_IS_ABSTRACT:
4203 case RID_IS_BASE_OF:
4204 case RID_IS_CLASS:
4205 case RID_IS_CONVERTIBLE_TO:
4206 case RID_IS_EMPTY:
4207 case RID_IS_ENUM:
4208 case RID_IS_FINAL:
4209 case RID_IS_LITERAL_TYPE:
4210 case RID_IS_POD:
4211 case RID_IS_POLYMORPHIC:
4212 case RID_IS_STD_LAYOUT:
4213 case RID_IS_TRIVIAL:
4214 case RID_IS_UNION:
4215 return cp_parser_trait_expr (parser, token->keyword);
4217 /* Objective-C++ expressions. */
4218 case RID_AT_ENCODE:
4219 case RID_AT_PROTOCOL:
4220 case RID_AT_SELECTOR:
4221 return cp_parser_objc_expression (parser);
4223 case RID_TEMPLATE:
4224 if (parser->in_function_body
4225 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4226 == CPP_LESS))
4228 error_at (token->location,
4229 "a template declaration cannot appear at block scope");
4230 cp_parser_skip_to_end_of_block_or_statement (parser);
4231 return error_mark_node;
4233 default:
4234 cp_parser_error (parser, "expected primary-expression");
4235 return error_mark_node;
4238 /* An id-expression can start with either an identifier, a
4239 `::' as the beginning of a qualified-id, or the "operator"
4240 keyword. */
4241 case CPP_NAME:
4242 case CPP_SCOPE:
4243 case CPP_TEMPLATE_ID:
4244 case CPP_NESTED_NAME_SPECIFIER:
4246 tree id_expression;
4247 tree decl;
4248 const char *error_msg;
4249 bool template_p;
4250 bool done;
4251 cp_token *id_expr_token;
4253 id_expression:
4254 /* Parse the id-expression. */
4255 id_expression
4256 = cp_parser_id_expression (parser,
4257 /*template_keyword_p=*/false,
4258 /*check_dependency_p=*/true,
4259 &template_p,
4260 /*declarator_p=*/false,
4261 /*optional_p=*/false);
4262 if (id_expression == error_mark_node)
4263 return error_mark_node;
4264 id_expr_token = token;
4265 token = cp_lexer_peek_token (parser->lexer);
4266 done = (token->type != CPP_OPEN_SQUARE
4267 && token->type != CPP_OPEN_PAREN
4268 && token->type != CPP_DOT
4269 && token->type != CPP_DEREF
4270 && token->type != CPP_PLUS_PLUS
4271 && token->type != CPP_MINUS_MINUS);
4272 /* If we have a template-id, then no further lookup is
4273 required. If the template-id was for a template-class, we
4274 will sometimes have a TYPE_DECL at this point. */
4275 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4276 || TREE_CODE (id_expression) == TYPE_DECL)
4277 decl = id_expression;
4278 /* Look up the name. */
4279 else
4281 tree ambiguous_decls;
4283 /* If we already know that this lookup is ambiguous, then
4284 we've already issued an error message; there's no reason
4285 to check again. */
4286 if (id_expr_token->type == CPP_NAME
4287 && id_expr_token->ambiguous_p)
4289 cp_parser_simulate_error (parser);
4290 return error_mark_node;
4293 decl = cp_parser_lookup_name (parser, id_expression,
4294 none_type,
4295 template_p,
4296 /*is_namespace=*/false,
4297 /*check_dependency=*/true,
4298 &ambiguous_decls,
4299 id_expr_token->location);
4300 /* If the lookup was ambiguous, an error will already have
4301 been issued. */
4302 if (ambiguous_decls)
4303 return error_mark_node;
4305 /* In Objective-C++, we may have an Objective-C 2.0
4306 dot-syntax for classes here. */
4307 if (c_dialect_objc ()
4308 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4309 && TREE_CODE (decl) == TYPE_DECL
4310 && objc_is_class_name (decl))
4312 tree component;
4313 cp_lexer_consume_token (parser->lexer);
4314 component = cp_parser_identifier (parser);
4315 if (component == error_mark_node)
4316 return error_mark_node;
4318 return objc_build_class_component_ref (id_expression, component);
4321 /* In Objective-C++, an instance variable (ivar) may be preferred
4322 to whatever cp_parser_lookup_name() found. */
4323 decl = objc_lookup_ivar (decl, id_expression);
4325 /* If name lookup gives us a SCOPE_REF, then the
4326 qualifying scope was dependent. */
4327 if (TREE_CODE (decl) == SCOPE_REF)
4329 /* At this point, we do not know if DECL is a valid
4330 integral constant expression. We assume that it is
4331 in fact such an expression, so that code like:
4333 template <int N> struct A {
4334 int a[B<N>::i];
4337 is accepted. At template-instantiation time, we
4338 will check that B<N>::i is actually a constant. */
4339 return decl;
4341 /* Check to see if DECL is a local variable in a context
4342 where that is forbidden. */
4343 if (parser->local_variables_forbidden_p
4344 && local_variable_p (decl))
4346 /* It might be that we only found DECL because we are
4347 trying to be generous with pre-ISO scoping rules.
4348 For example, consider:
4350 int i;
4351 void g() {
4352 for (int i = 0; i < 10; ++i) {}
4353 extern void f(int j = i);
4356 Here, name look up will originally find the out
4357 of scope `i'. We need to issue a warning message,
4358 but then use the global `i'. */
4359 decl = check_for_out_of_scope_variable (decl);
4360 if (local_variable_p (decl))
4362 error_at (id_expr_token->location,
4363 "local variable %qD may not appear in this context",
4364 decl);
4365 return error_mark_node;
4370 decl = (finish_id_expression
4371 (id_expression, decl, parser->scope,
4372 idk,
4373 parser->integral_constant_expression_p,
4374 parser->allow_non_integral_constant_expression_p,
4375 &parser->non_integral_constant_expression_p,
4376 template_p, done, address_p,
4377 template_arg_p,
4378 &error_msg,
4379 id_expr_token->location));
4380 if (error_msg)
4381 cp_parser_error (parser, error_msg);
4382 return decl;
4385 /* Anything else is an error. */
4386 default:
4387 cp_parser_error (parser, "expected primary-expression");
4388 return error_mark_node;
4392 /* Parse an id-expression.
4394 id-expression:
4395 unqualified-id
4396 qualified-id
4398 qualified-id:
4399 :: [opt] nested-name-specifier template [opt] unqualified-id
4400 :: identifier
4401 :: operator-function-id
4402 :: template-id
4404 Return a representation of the unqualified portion of the
4405 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4406 a `::' or nested-name-specifier.
4408 Often, if the id-expression was a qualified-id, the caller will
4409 want to make a SCOPE_REF to represent the qualified-id. This
4410 function does not do this in order to avoid wastefully creating
4411 SCOPE_REFs when they are not required.
4413 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4414 `template' keyword.
4416 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4417 uninstantiated templates.
4419 If *TEMPLATE_P is non-NULL, it is set to true iff the
4420 `template' keyword is used to explicitly indicate that the entity
4421 named is a template.
4423 If DECLARATOR_P is true, the id-expression is appearing as part of
4424 a declarator, rather than as part of an expression. */
4426 static tree
4427 cp_parser_id_expression (cp_parser *parser,
4428 bool template_keyword_p,
4429 bool check_dependency_p,
4430 bool *template_p,
4431 bool declarator_p,
4432 bool optional_p)
4434 bool global_scope_p;
4435 bool nested_name_specifier_p;
4437 /* Assume the `template' keyword was not used. */
4438 if (template_p)
4439 *template_p = template_keyword_p;
4441 /* Look for the optional `::' operator. */
4442 global_scope_p
4443 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4444 != NULL_TREE);
4445 /* Look for the optional nested-name-specifier. */
4446 nested_name_specifier_p
4447 = (cp_parser_nested_name_specifier_opt (parser,
4448 /*typename_keyword_p=*/false,
4449 check_dependency_p,
4450 /*type_p=*/false,
4451 declarator_p)
4452 != NULL_TREE);
4453 /* If there is a nested-name-specifier, then we are looking at
4454 the first qualified-id production. */
4455 if (nested_name_specifier_p)
4457 tree saved_scope;
4458 tree saved_object_scope;
4459 tree saved_qualifying_scope;
4460 tree unqualified_id;
4461 bool is_template;
4463 /* See if the next token is the `template' keyword. */
4464 if (!template_p)
4465 template_p = &is_template;
4466 *template_p = cp_parser_optional_template_keyword (parser);
4467 /* Name lookup we do during the processing of the
4468 unqualified-id might obliterate SCOPE. */
4469 saved_scope = parser->scope;
4470 saved_object_scope = parser->object_scope;
4471 saved_qualifying_scope = parser->qualifying_scope;
4472 /* Process the final unqualified-id. */
4473 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4474 check_dependency_p,
4475 declarator_p,
4476 /*optional_p=*/false);
4477 /* Restore the SAVED_SCOPE for our caller. */
4478 parser->scope = saved_scope;
4479 parser->object_scope = saved_object_scope;
4480 parser->qualifying_scope = saved_qualifying_scope;
4482 return unqualified_id;
4484 /* Otherwise, if we are in global scope, then we are looking at one
4485 of the other qualified-id productions. */
4486 else if (global_scope_p)
4488 cp_token *token;
4489 tree id;
4491 /* Peek at the next token. */
4492 token = cp_lexer_peek_token (parser->lexer);
4494 /* If it's an identifier, and the next token is not a "<", then
4495 we can avoid the template-id case. This is an optimization
4496 for this common case. */
4497 if (token->type == CPP_NAME
4498 && !cp_parser_nth_token_starts_template_argument_list_p
4499 (parser, 2))
4500 return cp_parser_identifier (parser);
4502 cp_parser_parse_tentatively (parser);
4503 /* Try a template-id. */
4504 id = cp_parser_template_id (parser,
4505 /*template_keyword_p=*/false,
4506 /*check_dependency_p=*/true,
4507 none_type,
4508 declarator_p);
4509 /* If that worked, we're done. */
4510 if (cp_parser_parse_definitely (parser))
4511 return id;
4513 /* Peek at the next token. (Changes in the token buffer may
4514 have invalidated the pointer obtained above.) */
4515 token = cp_lexer_peek_token (parser->lexer);
4517 switch (token->type)
4519 case CPP_NAME:
4520 return cp_parser_identifier (parser);
4522 case CPP_KEYWORD:
4523 if (token->keyword == RID_OPERATOR)
4524 return cp_parser_operator_function_id (parser);
4525 /* Fall through. */
4527 default:
4528 cp_parser_error (parser, "expected id-expression");
4529 return error_mark_node;
4532 else
4533 return cp_parser_unqualified_id (parser, template_keyword_p,
4534 /*check_dependency_p=*/true,
4535 declarator_p,
4536 optional_p);
4539 /* Parse an unqualified-id.
4541 unqualified-id:
4542 identifier
4543 operator-function-id
4544 conversion-function-id
4545 ~ class-name
4546 template-id
4548 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4549 keyword, in a construct like `A::template ...'.
4551 Returns a representation of unqualified-id. For the `identifier'
4552 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4553 production a BIT_NOT_EXPR is returned; the operand of the
4554 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4555 other productions, see the documentation accompanying the
4556 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4557 names are looked up in uninstantiated templates. If DECLARATOR_P
4558 is true, the unqualified-id is appearing as part of a declarator,
4559 rather than as part of an expression. */
4561 static tree
4562 cp_parser_unqualified_id (cp_parser* parser,
4563 bool template_keyword_p,
4564 bool check_dependency_p,
4565 bool declarator_p,
4566 bool optional_p)
4568 cp_token *token;
4570 /* Peek at the next token. */
4571 token = cp_lexer_peek_token (parser->lexer);
4573 switch (token->type)
4575 case CPP_NAME:
4577 tree id;
4579 /* We don't know yet whether or not this will be a
4580 template-id. */
4581 cp_parser_parse_tentatively (parser);
4582 /* Try a template-id. */
4583 id = cp_parser_template_id (parser, template_keyword_p,
4584 check_dependency_p,
4585 none_type,
4586 declarator_p);
4587 /* If it worked, we're done. */
4588 if (cp_parser_parse_definitely (parser))
4589 return id;
4590 /* Otherwise, it's an ordinary identifier. */
4591 return cp_parser_identifier (parser);
4594 case CPP_TEMPLATE_ID:
4595 return cp_parser_template_id (parser, template_keyword_p,
4596 check_dependency_p,
4597 none_type,
4598 declarator_p);
4600 case CPP_COMPL:
4602 tree type_decl;
4603 tree qualifying_scope;
4604 tree object_scope;
4605 tree scope;
4606 bool done;
4608 /* Consume the `~' token. */
4609 cp_lexer_consume_token (parser->lexer);
4610 /* Parse the class-name. The standard, as written, seems to
4611 say that:
4613 template <typename T> struct S { ~S (); };
4614 template <typename T> S<T>::~S() {}
4616 is invalid, since `~' must be followed by a class-name, but
4617 `S<T>' is dependent, and so not known to be a class.
4618 That's not right; we need to look in uninstantiated
4619 templates. A further complication arises from:
4621 template <typename T> void f(T t) {
4622 t.T::~T();
4625 Here, it is not possible to look up `T' in the scope of `T'
4626 itself. We must look in both the current scope, and the
4627 scope of the containing complete expression.
4629 Yet another issue is:
4631 struct S {
4632 int S;
4633 ~S();
4636 S::~S() {}
4638 The standard does not seem to say that the `S' in `~S'
4639 should refer to the type `S' and not the data member
4640 `S::S'. */
4642 /* DR 244 says that we look up the name after the "~" in the
4643 same scope as we looked up the qualifying name. That idea
4644 isn't fully worked out; it's more complicated than that. */
4645 scope = parser->scope;
4646 object_scope = parser->object_scope;
4647 qualifying_scope = parser->qualifying_scope;
4649 /* Check for invalid scopes. */
4650 if (scope == error_mark_node)
4652 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4653 cp_lexer_consume_token (parser->lexer);
4654 return error_mark_node;
4656 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4658 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4659 error_at (token->location,
4660 "scope %qT before %<~%> is not a class-name",
4661 scope);
4662 cp_parser_simulate_error (parser);
4663 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4664 cp_lexer_consume_token (parser->lexer);
4665 return error_mark_node;
4667 gcc_assert (!scope || TYPE_P (scope));
4669 /* If the name is of the form "X::~X" it's OK even if X is a
4670 typedef. */
4671 token = cp_lexer_peek_token (parser->lexer);
4672 if (scope
4673 && token->type == CPP_NAME
4674 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4675 != CPP_LESS)
4676 && (token->u.value == TYPE_IDENTIFIER (scope)
4677 || (CLASS_TYPE_P (scope)
4678 && constructor_name_p (token->u.value, scope))))
4680 cp_lexer_consume_token (parser->lexer);
4681 return build_nt (BIT_NOT_EXPR, scope);
4684 /* If there was an explicit qualification (S::~T), first look
4685 in the scope given by the qualification (i.e., S).
4687 Note: in the calls to cp_parser_class_name below we pass
4688 typename_type so that lookup finds the injected-class-name
4689 rather than the constructor. */
4690 done = false;
4691 type_decl = NULL_TREE;
4692 if (scope)
4694 cp_parser_parse_tentatively (parser);
4695 type_decl = cp_parser_class_name (parser,
4696 /*typename_keyword_p=*/false,
4697 /*template_keyword_p=*/false,
4698 typename_type,
4699 /*check_dependency=*/false,
4700 /*class_head_p=*/false,
4701 declarator_p);
4702 if (cp_parser_parse_definitely (parser))
4703 done = true;
4705 /* In "N::S::~S", look in "N" as well. */
4706 if (!done && scope && qualifying_scope)
4708 cp_parser_parse_tentatively (parser);
4709 parser->scope = qualifying_scope;
4710 parser->object_scope = NULL_TREE;
4711 parser->qualifying_scope = NULL_TREE;
4712 type_decl
4713 = cp_parser_class_name (parser,
4714 /*typename_keyword_p=*/false,
4715 /*template_keyword_p=*/false,
4716 typename_type,
4717 /*check_dependency=*/false,
4718 /*class_head_p=*/false,
4719 declarator_p);
4720 if (cp_parser_parse_definitely (parser))
4721 done = true;
4723 /* In "p->S::~T", look in the scope given by "*p" as well. */
4724 else if (!done && object_scope)
4726 cp_parser_parse_tentatively (parser);
4727 parser->scope = object_scope;
4728 parser->object_scope = NULL_TREE;
4729 parser->qualifying_scope = NULL_TREE;
4730 type_decl
4731 = cp_parser_class_name (parser,
4732 /*typename_keyword_p=*/false,
4733 /*template_keyword_p=*/false,
4734 typename_type,
4735 /*check_dependency=*/false,
4736 /*class_head_p=*/false,
4737 declarator_p);
4738 if (cp_parser_parse_definitely (parser))
4739 done = true;
4741 /* Look in the surrounding context. */
4742 if (!done)
4744 parser->scope = NULL_TREE;
4745 parser->object_scope = NULL_TREE;
4746 parser->qualifying_scope = NULL_TREE;
4747 if (processing_template_decl)
4748 cp_parser_parse_tentatively (parser);
4749 type_decl
4750 = cp_parser_class_name (parser,
4751 /*typename_keyword_p=*/false,
4752 /*template_keyword_p=*/false,
4753 typename_type,
4754 /*check_dependency=*/false,
4755 /*class_head_p=*/false,
4756 declarator_p);
4757 if (processing_template_decl
4758 && ! cp_parser_parse_definitely (parser))
4760 /* We couldn't find a type with this name, so just accept
4761 it and check for a match at instantiation time. */
4762 type_decl = cp_parser_identifier (parser);
4763 if (type_decl != error_mark_node)
4764 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4765 return type_decl;
4768 /* If an error occurred, assume that the name of the
4769 destructor is the same as the name of the qualifying
4770 class. That allows us to keep parsing after running
4771 into ill-formed destructor names. */
4772 if (type_decl == error_mark_node && scope)
4773 return build_nt (BIT_NOT_EXPR, scope);
4774 else if (type_decl == error_mark_node)
4775 return error_mark_node;
4777 /* Check that destructor name and scope match. */
4778 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4780 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4781 error_at (token->location,
4782 "declaration of %<~%T%> as member of %qT",
4783 type_decl, scope);
4784 cp_parser_simulate_error (parser);
4785 return error_mark_node;
4788 /* [class.dtor]
4790 A typedef-name that names a class shall not be used as the
4791 identifier in the declarator for a destructor declaration. */
4792 if (declarator_p
4793 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4794 && !DECL_SELF_REFERENCE_P (type_decl)
4795 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4796 error_at (token->location,
4797 "typedef-name %qD used as destructor declarator",
4798 type_decl);
4800 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4803 case CPP_KEYWORD:
4804 if (token->keyword == RID_OPERATOR)
4806 tree id;
4808 /* This could be a template-id, so we try that first. */
4809 cp_parser_parse_tentatively (parser);
4810 /* Try a template-id. */
4811 id = cp_parser_template_id (parser, template_keyword_p,
4812 /*check_dependency_p=*/true,
4813 none_type,
4814 declarator_p);
4815 /* If that worked, we're done. */
4816 if (cp_parser_parse_definitely (parser))
4817 return id;
4818 /* We still don't know whether we're looking at an
4819 operator-function-id or a conversion-function-id. */
4820 cp_parser_parse_tentatively (parser);
4821 /* Try an operator-function-id. */
4822 id = cp_parser_operator_function_id (parser);
4823 /* If that didn't work, try a conversion-function-id. */
4824 if (!cp_parser_parse_definitely (parser))
4825 id = cp_parser_conversion_function_id (parser);
4826 else if (UDLIT_OPER_P (id))
4828 /* 17.6.3.3.5 */
4829 const char *name = UDLIT_OP_SUFFIX (id);
4830 if (name[0] != '_' && !in_system_header)
4831 warning (0, "literal operator suffixes not preceded by %<_%>"
4832 " are reserved for future standardization");
4835 return id;
4837 /* Fall through. */
4839 default:
4840 if (optional_p)
4841 return NULL_TREE;
4842 cp_parser_error (parser, "expected unqualified-id");
4843 return error_mark_node;
4847 /* Parse an (optional) nested-name-specifier.
4849 nested-name-specifier: [C++98]
4850 class-or-namespace-name :: nested-name-specifier [opt]
4851 class-or-namespace-name :: template nested-name-specifier [opt]
4853 nested-name-specifier: [C++0x]
4854 type-name ::
4855 namespace-name ::
4856 nested-name-specifier identifier ::
4857 nested-name-specifier template [opt] simple-template-id ::
4859 PARSER->SCOPE should be set appropriately before this function is
4860 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4861 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4862 in name lookups.
4864 Sets PARSER->SCOPE to the class (TYPE) or namespace
4865 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4866 it unchanged if there is no nested-name-specifier. Returns the new
4867 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4869 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4870 part of a declaration and/or decl-specifier. */
4872 static tree
4873 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4874 bool typename_keyword_p,
4875 bool check_dependency_p,
4876 bool type_p,
4877 bool is_declaration)
4879 bool success = false;
4880 cp_token_position start = 0;
4881 cp_token *token;
4883 /* Remember where the nested-name-specifier starts. */
4884 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4886 start = cp_lexer_token_position (parser->lexer, false);
4887 push_deferring_access_checks (dk_deferred);
4890 while (true)
4892 tree new_scope;
4893 tree old_scope;
4894 tree saved_qualifying_scope;
4895 bool template_keyword_p;
4897 /* Spot cases that cannot be the beginning of a
4898 nested-name-specifier. */
4899 token = cp_lexer_peek_token (parser->lexer);
4901 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4902 the already parsed nested-name-specifier. */
4903 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4905 /* Grab the nested-name-specifier and continue the loop. */
4906 cp_parser_pre_parsed_nested_name_specifier (parser);
4907 /* If we originally encountered this nested-name-specifier
4908 with IS_DECLARATION set to false, we will not have
4909 resolved TYPENAME_TYPEs, so we must do so here. */
4910 if (is_declaration
4911 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4913 new_scope = resolve_typename_type (parser->scope,
4914 /*only_current_p=*/false);
4915 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4916 parser->scope = new_scope;
4918 success = true;
4919 continue;
4922 /* Spot cases that cannot be the beginning of a
4923 nested-name-specifier. On the second and subsequent times
4924 through the loop, we look for the `template' keyword. */
4925 if (success && token->keyword == RID_TEMPLATE)
4927 /* A template-id can start a nested-name-specifier. */
4928 else if (token->type == CPP_TEMPLATE_ID)
4930 /* DR 743: decltype can be used in a nested-name-specifier. */
4931 else if (token_is_decltype (token))
4933 else
4935 /* If the next token is not an identifier, then it is
4936 definitely not a type-name or namespace-name. */
4937 if (token->type != CPP_NAME)
4938 break;
4939 /* If the following token is neither a `<' (to begin a
4940 template-id), nor a `::', then we are not looking at a
4941 nested-name-specifier. */
4942 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4944 if (token->type == CPP_COLON
4945 && parser->colon_corrects_to_scope_p
4946 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4948 error_at (token->location,
4949 "found %<:%> in nested-name-specifier, expected %<::%>");
4950 token->type = CPP_SCOPE;
4953 if (token->type != CPP_SCOPE
4954 && !cp_parser_nth_token_starts_template_argument_list_p
4955 (parser, 2))
4956 break;
4959 /* The nested-name-specifier is optional, so we parse
4960 tentatively. */
4961 cp_parser_parse_tentatively (parser);
4963 /* Look for the optional `template' keyword, if this isn't the
4964 first time through the loop. */
4965 if (success)
4966 template_keyword_p = cp_parser_optional_template_keyword (parser);
4967 else
4968 template_keyword_p = false;
4970 /* Save the old scope since the name lookup we are about to do
4971 might destroy it. */
4972 old_scope = parser->scope;
4973 saved_qualifying_scope = parser->qualifying_scope;
4974 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4975 look up names in "X<T>::I" in order to determine that "Y" is
4976 a template. So, if we have a typename at this point, we make
4977 an effort to look through it. */
4978 if (is_declaration
4979 && !typename_keyword_p
4980 && parser->scope
4981 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4982 parser->scope = resolve_typename_type (parser->scope,
4983 /*only_current_p=*/false);
4984 /* Parse the qualifying entity. */
4985 new_scope
4986 = cp_parser_qualifying_entity (parser,
4987 typename_keyword_p,
4988 template_keyword_p,
4989 check_dependency_p,
4990 type_p,
4991 is_declaration);
4992 /* Look for the `::' token. */
4993 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4995 /* If we found what we wanted, we keep going; otherwise, we're
4996 done. */
4997 if (!cp_parser_parse_definitely (parser))
4999 bool error_p = false;
5001 /* Restore the OLD_SCOPE since it was valid before the
5002 failed attempt at finding the last
5003 class-or-namespace-name. */
5004 parser->scope = old_scope;
5005 parser->qualifying_scope = saved_qualifying_scope;
5007 /* If the next token is a decltype, and the one after that is a
5008 `::', then the decltype has failed to resolve to a class or
5009 enumeration type. Give this error even when parsing
5010 tentatively since it can't possibly be valid--and we're going
5011 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5012 won't get another chance.*/
5013 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5014 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5015 == CPP_SCOPE))
5017 token = cp_lexer_consume_token (parser->lexer);
5018 error_at (token->location, "decltype evaluates to %qT, "
5019 "which is not a class or enumeration type",
5020 token->u.value);
5021 parser->scope = error_mark_node;
5022 error_p = true;
5023 /* As below. */
5024 success = true;
5025 cp_lexer_consume_token (parser->lexer);
5028 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5029 break;
5030 /* If the next token is an identifier, and the one after
5031 that is a `::', then any valid interpretation would have
5032 found a class-or-namespace-name. */
5033 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5034 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5035 == CPP_SCOPE)
5036 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5037 != CPP_COMPL))
5039 token = cp_lexer_consume_token (parser->lexer);
5040 if (!error_p)
5042 if (!token->ambiguous_p)
5044 tree decl;
5045 tree ambiguous_decls;
5047 decl = cp_parser_lookup_name (parser, token->u.value,
5048 none_type,
5049 /*is_template=*/false,
5050 /*is_namespace=*/false,
5051 /*check_dependency=*/true,
5052 &ambiguous_decls,
5053 token->location);
5054 if (TREE_CODE (decl) == TEMPLATE_DECL)
5055 error_at (token->location,
5056 "%qD used without template parameters",
5057 decl);
5058 else if (ambiguous_decls)
5060 error_at (token->location,
5061 "reference to %qD is ambiguous",
5062 token->u.value);
5063 print_candidates (ambiguous_decls);
5064 decl = error_mark_node;
5066 else
5068 if (cxx_dialect != cxx98)
5069 cp_parser_name_lookup_error
5070 (parser, token->u.value, decl, NLE_NOT_CXX98,
5071 token->location);
5072 else
5073 cp_parser_name_lookup_error
5074 (parser, token->u.value, decl, NLE_CXX98,
5075 token->location);
5078 parser->scope = error_mark_node;
5079 error_p = true;
5080 /* Treat this as a successful nested-name-specifier
5081 due to:
5083 [basic.lookup.qual]
5085 If the name found is not a class-name (clause
5086 _class_) or namespace-name (_namespace.def_), the
5087 program is ill-formed. */
5088 success = true;
5090 cp_lexer_consume_token (parser->lexer);
5092 break;
5094 /* We've found one valid nested-name-specifier. */
5095 success = true;
5096 /* Name lookup always gives us a DECL. */
5097 if (TREE_CODE (new_scope) == TYPE_DECL)
5098 new_scope = TREE_TYPE (new_scope);
5099 /* Uses of "template" must be followed by actual templates. */
5100 if (template_keyword_p
5101 && !(CLASS_TYPE_P (new_scope)
5102 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5103 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5104 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5105 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5106 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5107 == TEMPLATE_ID_EXPR)))
5108 permerror (input_location, TYPE_P (new_scope)
5109 ? G_("%qT is not a template")
5110 : G_("%qD is not a template"),
5111 new_scope);
5112 /* If it is a class scope, try to complete it; we are about to
5113 be looking up names inside the class. */
5114 if (TYPE_P (new_scope)
5115 /* Since checking types for dependency can be expensive,
5116 avoid doing it if the type is already complete. */
5117 && !COMPLETE_TYPE_P (new_scope)
5118 /* Do not try to complete dependent types. */
5119 && !dependent_type_p (new_scope))
5121 new_scope = complete_type (new_scope);
5122 /* If it is a typedef to current class, use the current
5123 class instead, as the typedef won't have any names inside
5124 it yet. */
5125 if (!COMPLETE_TYPE_P (new_scope)
5126 && currently_open_class (new_scope))
5127 new_scope = TYPE_MAIN_VARIANT (new_scope);
5129 /* Make sure we look in the right scope the next time through
5130 the loop. */
5131 parser->scope = new_scope;
5134 /* If parsing tentatively, replace the sequence of tokens that makes
5135 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5136 token. That way, should we re-parse the token stream, we will
5137 not have to repeat the effort required to do the parse, nor will
5138 we issue duplicate error messages. */
5139 if (success && start)
5141 cp_token *token;
5143 token = cp_lexer_token_at (parser->lexer, start);
5144 /* Reset the contents of the START token. */
5145 token->type = CPP_NESTED_NAME_SPECIFIER;
5146 /* Retrieve any deferred checks. Do not pop this access checks yet
5147 so the memory will not be reclaimed during token replacing below. */
5148 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5149 token->u.tree_check_value->value = parser->scope;
5150 token->u.tree_check_value->checks = get_deferred_access_checks ();
5151 token->u.tree_check_value->qualifying_scope =
5152 parser->qualifying_scope;
5153 token->keyword = RID_MAX;
5155 /* Purge all subsequent tokens. */
5156 cp_lexer_purge_tokens_after (parser->lexer, start);
5159 if (start)
5160 pop_to_parent_deferring_access_checks ();
5162 return success ? parser->scope : NULL_TREE;
5165 /* Parse a nested-name-specifier. See
5166 cp_parser_nested_name_specifier_opt for details. This function
5167 behaves identically, except that it will an issue an error if no
5168 nested-name-specifier is present. */
5170 static tree
5171 cp_parser_nested_name_specifier (cp_parser *parser,
5172 bool typename_keyword_p,
5173 bool check_dependency_p,
5174 bool type_p,
5175 bool is_declaration)
5177 tree scope;
5179 /* Look for the nested-name-specifier. */
5180 scope = cp_parser_nested_name_specifier_opt (parser,
5181 typename_keyword_p,
5182 check_dependency_p,
5183 type_p,
5184 is_declaration);
5185 /* If it was not present, issue an error message. */
5186 if (!scope)
5188 cp_parser_error (parser, "expected nested-name-specifier");
5189 parser->scope = NULL_TREE;
5192 return scope;
5195 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5196 this is either a class-name or a namespace-name (which corresponds
5197 to the class-or-namespace-name production in the grammar). For
5198 C++0x, it can also be a type-name that refers to an enumeration
5199 type or a simple-template-id.
5201 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5202 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5203 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5204 TYPE_P is TRUE iff the next name should be taken as a class-name,
5205 even the same name is declared to be another entity in the same
5206 scope.
5208 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5209 specified by the class-or-namespace-name. If neither is found the
5210 ERROR_MARK_NODE is returned. */
5212 static tree
5213 cp_parser_qualifying_entity (cp_parser *parser,
5214 bool typename_keyword_p,
5215 bool template_keyword_p,
5216 bool check_dependency_p,
5217 bool type_p,
5218 bool is_declaration)
5220 tree saved_scope;
5221 tree saved_qualifying_scope;
5222 tree saved_object_scope;
5223 tree scope;
5224 bool only_class_p;
5225 bool successful_parse_p;
5227 /* DR 743: decltype can appear in a nested-name-specifier. */
5228 if (cp_lexer_next_token_is_decltype (parser->lexer))
5230 scope = cp_parser_decltype (parser);
5231 if (TREE_CODE (scope) != ENUMERAL_TYPE
5232 && !MAYBE_CLASS_TYPE_P (scope))
5234 cp_parser_simulate_error (parser);
5235 return error_mark_node;
5237 if (TYPE_NAME (scope))
5238 scope = TYPE_NAME (scope);
5239 return scope;
5242 /* Before we try to parse the class-name, we must save away the
5243 current PARSER->SCOPE since cp_parser_class_name will destroy
5244 it. */
5245 saved_scope = parser->scope;
5246 saved_qualifying_scope = parser->qualifying_scope;
5247 saved_object_scope = parser->object_scope;
5248 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5249 there is no need to look for a namespace-name. */
5250 only_class_p = template_keyword_p
5251 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5252 if (!only_class_p)
5253 cp_parser_parse_tentatively (parser);
5254 scope = cp_parser_class_name (parser,
5255 typename_keyword_p,
5256 template_keyword_p,
5257 type_p ? class_type : none_type,
5258 check_dependency_p,
5259 /*class_head_p=*/false,
5260 is_declaration);
5261 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5262 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5263 if (!only_class_p
5264 && cxx_dialect != cxx98
5265 && !successful_parse_p)
5267 /* Restore the saved scope. */
5268 parser->scope = saved_scope;
5269 parser->qualifying_scope = saved_qualifying_scope;
5270 parser->object_scope = saved_object_scope;
5272 /* Parse tentatively. */
5273 cp_parser_parse_tentatively (parser);
5275 /* Parse a type-name */
5276 scope = cp_parser_type_name (parser);
5278 /* "If the name found does not designate a namespace or a class,
5279 enumeration, or dependent type, the program is ill-formed."
5281 We cover classes and dependent types above and namespaces below,
5282 so this code is only looking for enums. */
5283 if (!scope || TREE_CODE (scope) != TYPE_DECL
5284 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5285 cp_parser_simulate_error (parser);
5287 successful_parse_p = cp_parser_parse_definitely (parser);
5289 /* If that didn't work, try for a namespace-name. */
5290 if (!only_class_p && !successful_parse_p)
5292 /* Restore the saved scope. */
5293 parser->scope = saved_scope;
5294 parser->qualifying_scope = saved_qualifying_scope;
5295 parser->object_scope = saved_object_scope;
5296 /* If we are not looking at an identifier followed by the scope
5297 resolution operator, then this is not part of a
5298 nested-name-specifier. (Note that this function is only used
5299 to parse the components of a nested-name-specifier.) */
5300 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5301 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5302 return error_mark_node;
5303 scope = cp_parser_namespace_name (parser);
5306 return scope;
5309 /* Parse a postfix-expression.
5311 postfix-expression:
5312 primary-expression
5313 postfix-expression [ expression ]
5314 postfix-expression ( expression-list [opt] )
5315 simple-type-specifier ( expression-list [opt] )
5316 typename :: [opt] nested-name-specifier identifier
5317 ( expression-list [opt] )
5318 typename :: [opt] nested-name-specifier template [opt] template-id
5319 ( expression-list [opt] )
5320 postfix-expression . template [opt] id-expression
5321 postfix-expression -> template [opt] id-expression
5322 postfix-expression . pseudo-destructor-name
5323 postfix-expression -> pseudo-destructor-name
5324 postfix-expression ++
5325 postfix-expression --
5326 dynamic_cast < type-id > ( expression )
5327 static_cast < type-id > ( expression )
5328 reinterpret_cast < type-id > ( expression )
5329 const_cast < type-id > ( expression )
5330 typeid ( expression )
5331 typeid ( type-id )
5333 GNU Extension:
5335 postfix-expression:
5336 ( type-id ) { initializer-list , [opt] }
5338 This extension is a GNU version of the C99 compound-literal
5339 construct. (The C99 grammar uses `type-name' instead of `type-id',
5340 but they are essentially the same concept.)
5342 If ADDRESS_P is true, the postfix expression is the operand of the
5343 `&' operator. CAST_P is true if this expression is the target of a
5344 cast.
5346 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5347 class member access expressions [expr.ref].
5349 Returns a representation of the expression. */
5351 static tree
5352 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5353 bool member_access_only_p,
5354 cp_id_kind * pidk_return)
5356 cp_token *token;
5357 enum rid keyword;
5358 cp_id_kind idk = CP_ID_KIND_NONE;
5359 tree postfix_expression = NULL_TREE;
5360 bool is_member_access = false;
5362 /* Peek at the next token. */
5363 token = cp_lexer_peek_token (parser->lexer);
5364 /* Some of the productions are determined by keywords. */
5365 keyword = token->keyword;
5366 switch (keyword)
5368 case RID_DYNCAST:
5369 case RID_STATCAST:
5370 case RID_REINTCAST:
5371 case RID_CONSTCAST:
5373 tree type;
5374 tree expression;
5375 const char *saved_message;
5377 /* All of these can be handled in the same way from the point
5378 of view of parsing. Begin by consuming the token
5379 identifying the cast. */
5380 cp_lexer_consume_token (parser->lexer);
5382 /* New types cannot be defined in the cast. */
5383 saved_message = parser->type_definition_forbidden_message;
5384 parser->type_definition_forbidden_message
5385 = G_("types may not be defined in casts");
5387 /* Look for the opening `<'. */
5388 cp_parser_require (parser, CPP_LESS, RT_LESS);
5389 /* Parse the type to which we are casting. */
5390 type = cp_parser_type_id (parser);
5391 /* Look for the closing `>'. */
5392 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5393 /* Restore the old message. */
5394 parser->type_definition_forbidden_message = saved_message;
5396 /* And the expression which is being cast. */
5397 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5398 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5399 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5401 /* Only type conversions to integral or enumeration types
5402 can be used in constant-expressions. */
5403 if (!cast_valid_in_integral_constant_expression_p (type)
5404 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5405 return error_mark_node;
5407 switch (keyword)
5409 case RID_DYNCAST:
5410 postfix_expression
5411 = build_dynamic_cast (type, expression, tf_warning_or_error);
5412 break;
5413 case RID_STATCAST:
5414 postfix_expression
5415 = build_static_cast (type, expression, tf_warning_or_error);
5416 break;
5417 case RID_REINTCAST:
5418 postfix_expression
5419 = build_reinterpret_cast (type, expression,
5420 tf_warning_or_error);
5421 break;
5422 case RID_CONSTCAST:
5423 postfix_expression
5424 = build_const_cast (type, expression, tf_warning_or_error);
5425 break;
5426 default:
5427 gcc_unreachable ();
5430 break;
5432 case RID_TYPEID:
5434 tree type;
5435 const char *saved_message;
5436 bool saved_in_type_id_in_expr_p;
5438 /* Consume the `typeid' token. */
5439 cp_lexer_consume_token (parser->lexer);
5440 /* Look for the `(' token. */
5441 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5442 /* Types cannot be defined in a `typeid' expression. */
5443 saved_message = parser->type_definition_forbidden_message;
5444 parser->type_definition_forbidden_message
5445 = G_("types may not be defined in a %<typeid%> expression");
5446 /* We can't be sure yet whether we're looking at a type-id or an
5447 expression. */
5448 cp_parser_parse_tentatively (parser);
5449 /* Try a type-id first. */
5450 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5451 parser->in_type_id_in_expr_p = true;
5452 type = cp_parser_type_id (parser);
5453 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5454 /* Look for the `)' token. Otherwise, we can't be sure that
5455 we're not looking at an expression: consider `typeid (int
5456 (3))', for example. */
5457 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5458 /* If all went well, simply lookup the type-id. */
5459 if (cp_parser_parse_definitely (parser))
5460 postfix_expression = get_typeid (type);
5461 /* Otherwise, fall back to the expression variant. */
5462 else
5464 tree expression;
5466 /* Look for an expression. */
5467 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5468 /* Compute its typeid. */
5469 postfix_expression = build_typeid (expression);
5470 /* Look for the `)' token. */
5471 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5473 /* Restore the saved message. */
5474 parser->type_definition_forbidden_message = saved_message;
5475 /* `typeid' may not appear in an integral constant expression. */
5476 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5477 return error_mark_node;
5479 break;
5481 case RID_TYPENAME:
5483 tree type;
5484 /* The syntax permitted here is the same permitted for an
5485 elaborated-type-specifier. */
5486 type = cp_parser_elaborated_type_specifier (parser,
5487 /*is_friend=*/false,
5488 /*is_declaration=*/false);
5489 postfix_expression = cp_parser_functional_cast (parser, type);
5491 break;
5493 case RID_BUILTIN_SHUFFLE:
5495 vec<tree, va_gc> *vec;
5496 unsigned int i;
5497 tree p;
5498 location_t loc = token->location;
5500 cp_lexer_consume_token (parser->lexer);
5501 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5502 /*cast_p=*/false, /*allow_expansion_p=*/true,
5503 /*non_constant_p=*/NULL);
5504 if (vec == NULL)
5505 return error_mark_node;
5507 FOR_EACH_VEC_ELT (*vec, i, p)
5508 mark_exp_read (p);
5510 if (vec->length () == 2)
5511 return c_build_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1]);
5512 else if (vec->length () == 3)
5513 return c_build_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2]);
5514 else
5516 error_at (loc, "wrong number of arguments to "
5517 "%<__builtin_shuffle%>");
5518 return error_mark_node;
5520 break;
5523 default:
5525 tree type;
5527 /* If the next thing is a simple-type-specifier, we may be
5528 looking at a functional cast. We could also be looking at
5529 an id-expression. So, we try the functional cast, and if
5530 that doesn't work we fall back to the primary-expression. */
5531 cp_parser_parse_tentatively (parser);
5532 /* Look for the simple-type-specifier. */
5533 type = cp_parser_simple_type_specifier (parser,
5534 /*decl_specs=*/NULL,
5535 CP_PARSER_FLAGS_NONE);
5536 /* Parse the cast itself. */
5537 if (!cp_parser_error_occurred (parser))
5538 postfix_expression
5539 = cp_parser_functional_cast (parser, type);
5540 /* If that worked, we're done. */
5541 if (cp_parser_parse_definitely (parser))
5542 break;
5544 /* If the functional-cast didn't work out, try a
5545 compound-literal. */
5546 if (cp_parser_allow_gnu_extensions_p (parser)
5547 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5549 vec<constructor_elt, va_gc> *initializer_list = NULL;
5550 bool saved_in_type_id_in_expr_p;
5552 cp_parser_parse_tentatively (parser);
5553 /* Consume the `('. */
5554 cp_lexer_consume_token (parser->lexer);
5555 /* Parse the type. */
5556 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5557 parser->in_type_id_in_expr_p = true;
5558 type = cp_parser_type_id (parser);
5559 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5560 /* Look for the `)'. */
5561 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5562 /* Look for the `{'. */
5563 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5564 /* If things aren't going well, there's no need to
5565 keep going. */
5566 if (!cp_parser_error_occurred (parser))
5568 bool non_constant_p;
5569 /* Parse the initializer-list. */
5570 initializer_list
5571 = cp_parser_initializer_list (parser, &non_constant_p);
5572 /* Allow a trailing `,'. */
5573 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5574 cp_lexer_consume_token (parser->lexer);
5575 /* Look for the final `}'. */
5576 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5578 /* If that worked, we're definitely looking at a
5579 compound-literal expression. */
5580 if (cp_parser_parse_definitely (parser))
5582 /* Warn the user that a compound literal is not
5583 allowed in standard C++. */
5584 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5585 /* For simplicity, we disallow compound literals in
5586 constant-expressions. We could
5587 allow compound literals of integer type, whose
5588 initializer was a constant, in constant
5589 expressions. Permitting that usage, as a further
5590 extension, would not change the meaning of any
5591 currently accepted programs. (Of course, as
5592 compound literals are not part of ISO C++, the
5593 standard has nothing to say.) */
5594 if (cp_parser_non_integral_constant_expression (parser,
5595 NIC_NCC))
5597 postfix_expression = error_mark_node;
5598 break;
5600 /* Form the representation of the compound-literal. */
5601 postfix_expression
5602 = (finish_compound_literal
5603 (type, build_constructor (init_list_type_node,
5604 initializer_list),
5605 tf_warning_or_error));
5606 break;
5610 /* It must be a primary-expression. */
5611 postfix_expression
5612 = cp_parser_primary_expression (parser, address_p, cast_p,
5613 /*template_arg_p=*/false,
5614 &idk);
5616 break;
5619 /* Keep looping until the postfix-expression is complete. */
5620 while (true)
5622 if (idk == CP_ID_KIND_UNQUALIFIED
5623 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5624 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5625 /* It is not a Koenig lookup function call. */
5626 postfix_expression
5627 = unqualified_name_lookup_error (postfix_expression);
5629 /* Peek at the next token. */
5630 token = cp_lexer_peek_token (parser->lexer);
5632 switch (token->type)
5634 case CPP_OPEN_SQUARE:
5635 if (cp_next_tokens_can_be_std_attribute_p (parser))
5637 cp_parser_error (parser,
5638 "two consecutive %<[%> shall "
5639 "only introduce an attribute");
5640 return error_mark_node;
5642 postfix_expression
5643 = cp_parser_postfix_open_square_expression (parser,
5644 postfix_expression,
5645 false);
5646 idk = CP_ID_KIND_NONE;
5647 is_member_access = false;
5648 break;
5650 case CPP_OPEN_PAREN:
5651 /* postfix-expression ( expression-list [opt] ) */
5653 bool koenig_p;
5654 bool is_builtin_constant_p;
5655 bool saved_integral_constant_expression_p = false;
5656 bool saved_non_integral_constant_expression_p = false;
5657 vec<tree, va_gc> *args;
5659 is_member_access = false;
5661 is_builtin_constant_p
5662 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5663 if (is_builtin_constant_p)
5665 /* The whole point of __builtin_constant_p is to allow
5666 non-constant expressions to appear as arguments. */
5667 saved_integral_constant_expression_p
5668 = parser->integral_constant_expression_p;
5669 saved_non_integral_constant_expression_p
5670 = parser->non_integral_constant_expression_p;
5671 parser->integral_constant_expression_p = false;
5673 args = (cp_parser_parenthesized_expression_list
5674 (parser, non_attr,
5675 /*cast_p=*/false, /*allow_expansion_p=*/true,
5676 /*non_constant_p=*/NULL));
5677 if (is_builtin_constant_p)
5679 parser->integral_constant_expression_p
5680 = saved_integral_constant_expression_p;
5681 parser->non_integral_constant_expression_p
5682 = saved_non_integral_constant_expression_p;
5685 if (args == NULL)
5687 postfix_expression = error_mark_node;
5688 break;
5691 /* Function calls are not permitted in
5692 constant-expressions. */
5693 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5694 && cp_parser_non_integral_constant_expression (parser,
5695 NIC_FUNC_CALL))
5697 postfix_expression = error_mark_node;
5698 release_tree_vector (args);
5699 break;
5702 koenig_p = false;
5703 if (idk == CP_ID_KIND_UNQUALIFIED
5704 || idk == CP_ID_KIND_TEMPLATE_ID)
5706 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5708 if (!args->is_empty ())
5710 koenig_p = true;
5711 if (!any_type_dependent_arguments_p (args))
5712 postfix_expression
5713 = perform_koenig_lookup (postfix_expression, args,
5714 /*include_std=*/false,
5715 tf_warning_or_error);
5717 else
5718 postfix_expression
5719 = unqualified_fn_lookup_error (postfix_expression);
5721 /* We do not perform argument-dependent lookup if
5722 normal lookup finds a non-function, in accordance
5723 with the expected resolution of DR 218. */
5724 else if (!args->is_empty ()
5725 && is_overloaded_fn (postfix_expression))
5727 tree fn = get_first_fn (postfix_expression);
5728 fn = STRIP_TEMPLATE (fn);
5730 /* Do not do argument dependent lookup if regular
5731 lookup finds a member function or a block-scope
5732 function declaration. [basic.lookup.argdep]/3 */
5733 if (!DECL_FUNCTION_MEMBER_P (fn)
5734 && !DECL_LOCAL_FUNCTION_P (fn))
5736 koenig_p = true;
5737 if (!any_type_dependent_arguments_p (args))
5738 postfix_expression
5739 = perform_koenig_lookup (postfix_expression, args,
5740 /*include_std=*/false,
5741 tf_warning_or_error);
5746 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5748 tree instance = TREE_OPERAND (postfix_expression, 0);
5749 tree fn = TREE_OPERAND (postfix_expression, 1);
5751 if (processing_template_decl
5752 && (type_dependent_expression_p (instance)
5753 || (!BASELINK_P (fn)
5754 && TREE_CODE (fn) != FIELD_DECL)
5755 || type_dependent_expression_p (fn)
5756 || any_type_dependent_arguments_p (args)))
5758 postfix_expression
5759 = build_nt_call_vec (postfix_expression, args);
5760 release_tree_vector (args);
5761 break;
5764 if (BASELINK_P (fn))
5766 postfix_expression
5767 = (build_new_method_call
5768 (instance, fn, &args, NULL_TREE,
5769 (idk == CP_ID_KIND_QUALIFIED
5770 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5771 : LOOKUP_NORMAL),
5772 /*fn_p=*/NULL,
5773 tf_warning_or_error));
5775 else
5776 postfix_expression
5777 = finish_call_expr (postfix_expression, &args,
5778 /*disallow_virtual=*/false,
5779 /*koenig_p=*/false,
5780 tf_warning_or_error);
5782 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5783 || TREE_CODE (postfix_expression) == MEMBER_REF
5784 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5785 postfix_expression = (build_offset_ref_call_from_tree
5786 (postfix_expression, &args,
5787 tf_warning_or_error));
5788 else if (idk == CP_ID_KIND_QUALIFIED)
5789 /* A call to a static class member, or a namespace-scope
5790 function. */
5791 postfix_expression
5792 = finish_call_expr (postfix_expression, &args,
5793 /*disallow_virtual=*/true,
5794 koenig_p,
5795 tf_warning_or_error);
5796 else
5797 /* All other function calls. */
5798 postfix_expression
5799 = finish_call_expr (postfix_expression, &args,
5800 /*disallow_virtual=*/false,
5801 koenig_p,
5802 tf_warning_or_error);
5804 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5805 idk = CP_ID_KIND_NONE;
5807 release_tree_vector (args);
5809 break;
5811 case CPP_DOT:
5812 case CPP_DEREF:
5813 /* postfix-expression . template [opt] id-expression
5814 postfix-expression . pseudo-destructor-name
5815 postfix-expression -> template [opt] id-expression
5816 postfix-expression -> pseudo-destructor-name */
5818 /* Consume the `.' or `->' operator. */
5819 cp_lexer_consume_token (parser->lexer);
5821 postfix_expression
5822 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5823 postfix_expression,
5824 false, &idk,
5825 token->location);
5827 is_member_access = true;
5828 break;
5830 case CPP_PLUS_PLUS:
5831 /* postfix-expression ++ */
5832 /* Consume the `++' token. */
5833 cp_lexer_consume_token (parser->lexer);
5834 /* Generate a representation for the complete expression. */
5835 postfix_expression
5836 = finish_increment_expr (postfix_expression,
5837 POSTINCREMENT_EXPR);
5838 /* Increments may not appear in constant-expressions. */
5839 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5840 postfix_expression = error_mark_node;
5841 idk = CP_ID_KIND_NONE;
5842 is_member_access = false;
5843 break;
5845 case CPP_MINUS_MINUS:
5846 /* postfix-expression -- */
5847 /* Consume the `--' token. */
5848 cp_lexer_consume_token (parser->lexer);
5849 /* Generate a representation for the complete expression. */
5850 postfix_expression
5851 = finish_increment_expr (postfix_expression,
5852 POSTDECREMENT_EXPR);
5853 /* Decrements may not appear in constant-expressions. */
5854 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5855 postfix_expression = error_mark_node;
5856 idk = CP_ID_KIND_NONE;
5857 is_member_access = false;
5858 break;
5860 default:
5861 if (pidk_return != NULL)
5862 * pidk_return = idk;
5863 if (member_access_only_p)
5864 return is_member_access? postfix_expression : error_mark_node;
5865 else
5866 return postfix_expression;
5870 /* We should never get here. */
5871 gcc_unreachable ();
5872 return error_mark_node;
5875 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5876 by cp_parser_builtin_offsetof. We're looking for
5878 postfix-expression [ expression ]
5879 postfix-expression [ braced-init-list ] (C++11)
5881 FOR_OFFSETOF is set if we're being called in that context, which
5882 changes how we deal with integer constant expressions. */
5884 static tree
5885 cp_parser_postfix_open_square_expression (cp_parser *parser,
5886 tree postfix_expression,
5887 bool for_offsetof)
5889 tree index;
5890 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5892 /* Consume the `[' token. */
5893 cp_lexer_consume_token (parser->lexer);
5895 /* Parse the index expression. */
5896 /* ??? For offsetof, there is a question of what to allow here. If
5897 offsetof is not being used in an integral constant expression context,
5898 then we *could* get the right answer by computing the value at runtime.
5899 If we are in an integral constant expression context, then we might
5900 could accept any constant expression; hard to say without analysis.
5901 Rather than open the barn door too wide right away, allow only integer
5902 constant expressions here. */
5903 if (for_offsetof)
5904 index = cp_parser_constant_expression (parser, false, NULL);
5905 else
5907 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5909 bool expr_nonconst_p;
5910 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5911 index = cp_parser_braced_list (parser, &expr_nonconst_p);
5913 else
5914 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5917 /* Look for the closing `]'. */
5918 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5920 /* Build the ARRAY_REF. */
5921 postfix_expression = grok_array_decl (loc, postfix_expression, index);
5923 /* When not doing offsetof, array references are not permitted in
5924 constant-expressions. */
5925 if (!for_offsetof
5926 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5927 postfix_expression = error_mark_node;
5929 return postfix_expression;
5932 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5933 by cp_parser_builtin_offsetof. We're looking for
5935 postfix-expression . template [opt] id-expression
5936 postfix-expression . pseudo-destructor-name
5937 postfix-expression -> template [opt] id-expression
5938 postfix-expression -> pseudo-destructor-name
5940 FOR_OFFSETOF is set if we're being called in that context. That sorta
5941 limits what of the above we'll actually accept, but nevermind.
5942 TOKEN_TYPE is the "." or "->" token, which will already have been
5943 removed from the stream. */
5945 static tree
5946 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5947 enum cpp_ttype token_type,
5948 tree postfix_expression,
5949 bool for_offsetof, cp_id_kind *idk,
5950 location_t location)
5952 tree name;
5953 bool dependent_p;
5954 bool pseudo_destructor_p;
5955 tree scope = NULL_TREE;
5957 /* If this is a `->' operator, dereference the pointer. */
5958 if (token_type == CPP_DEREF)
5959 postfix_expression = build_x_arrow (location, postfix_expression,
5960 tf_warning_or_error);
5961 /* Check to see whether or not the expression is type-dependent. */
5962 dependent_p = type_dependent_expression_p (postfix_expression);
5963 /* The identifier following the `->' or `.' is not qualified. */
5964 parser->scope = NULL_TREE;
5965 parser->qualifying_scope = NULL_TREE;
5966 parser->object_scope = NULL_TREE;
5967 *idk = CP_ID_KIND_NONE;
5969 /* Enter the scope corresponding to the type of the object
5970 given by the POSTFIX_EXPRESSION. */
5971 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5973 scope = TREE_TYPE (postfix_expression);
5974 /* According to the standard, no expression should ever have
5975 reference type. Unfortunately, we do not currently match
5976 the standard in this respect in that our internal representation
5977 of an expression may have reference type even when the standard
5978 says it does not. Therefore, we have to manually obtain the
5979 underlying type here. */
5980 scope = non_reference (scope);
5981 /* The type of the POSTFIX_EXPRESSION must be complete. */
5982 if (scope == unknown_type_node)
5984 error_at (location, "%qE does not have class type",
5985 postfix_expression);
5986 scope = NULL_TREE;
5988 /* Unlike the object expression in other contexts, *this is not
5989 required to be of complete type for purposes of class member
5990 access (5.2.5) outside the member function body. */
5991 else if (scope != current_class_ref
5992 && !(processing_template_decl && scope == current_class_type))
5993 scope = complete_type_or_else (scope, NULL_TREE);
5994 /* Let the name lookup machinery know that we are processing a
5995 class member access expression. */
5996 parser->context->object_type = scope;
5997 /* If something went wrong, we want to be able to discern that case,
5998 as opposed to the case where there was no SCOPE due to the type
5999 of expression being dependent. */
6000 if (!scope)
6001 scope = error_mark_node;
6002 /* If the SCOPE was erroneous, make the various semantic analysis
6003 functions exit quickly -- and without issuing additional error
6004 messages. */
6005 if (scope == error_mark_node)
6006 postfix_expression = error_mark_node;
6009 /* Assume this expression is not a pseudo-destructor access. */
6010 pseudo_destructor_p = false;
6012 /* If the SCOPE is a scalar type, then, if this is a valid program,
6013 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6014 is type dependent, it can be pseudo-destructor-name or something else.
6015 Try to parse it as pseudo-destructor-name first. */
6016 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6018 tree s;
6019 tree type;
6021 cp_parser_parse_tentatively (parser);
6022 /* Parse the pseudo-destructor-name. */
6023 s = NULL_TREE;
6024 cp_parser_pseudo_destructor_name (parser, &s, &type);
6025 if (dependent_p
6026 && (cp_parser_error_occurred (parser)
6027 || TREE_CODE (type) != TYPE_DECL
6028 || !SCALAR_TYPE_P (TREE_TYPE (type))))
6029 cp_parser_abort_tentative_parse (parser);
6030 else if (cp_parser_parse_definitely (parser))
6032 pseudo_destructor_p = true;
6033 postfix_expression
6034 = finish_pseudo_destructor_expr (postfix_expression,
6035 s, TREE_TYPE (type));
6039 if (!pseudo_destructor_p)
6041 /* If the SCOPE is not a scalar type, we are looking at an
6042 ordinary class member access expression, rather than a
6043 pseudo-destructor-name. */
6044 bool template_p;
6045 cp_token *token = cp_lexer_peek_token (parser->lexer);
6046 /* Parse the id-expression. */
6047 name = (cp_parser_id_expression
6048 (parser,
6049 cp_parser_optional_template_keyword (parser),
6050 /*check_dependency_p=*/true,
6051 &template_p,
6052 /*declarator_p=*/false,
6053 /*optional_p=*/false));
6054 /* In general, build a SCOPE_REF if the member name is qualified.
6055 However, if the name was not dependent and has already been
6056 resolved; there is no need to build the SCOPE_REF. For example;
6058 struct X { void f(); };
6059 template <typename T> void f(T* t) { t->X::f(); }
6061 Even though "t" is dependent, "X::f" is not and has been resolved
6062 to a BASELINK; there is no need to include scope information. */
6064 /* But we do need to remember that there was an explicit scope for
6065 virtual function calls. */
6066 if (parser->scope)
6067 *idk = CP_ID_KIND_QUALIFIED;
6069 /* If the name is a template-id that names a type, we will get a
6070 TYPE_DECL here. That is invalid code. */
6071 if (TREE_CODE (name) == TYPE_DECL)
6073 error_at (token->location, "invalid use of %qD", name);
6074 postfix_expression = error_mark_node;
6076 else
6078 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6080 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6082 error_at (token->location, "%<%D::%D%> is not a class member",
6083 parser->scope, name);
6084 postfix_expression = error_mark_node;
6086 else
6087 name = build_qualified_name (/*type=*/NULL_TREE,
6088 parser->scope,
6089 name,
6090 template_p);
6091 parser->scope = NULL_TREE;
6092 parser->qualifying_scope = NULL_TREE;
6093 parser->object_scope = NULL_TREE;
6095 if (parser->scope && name && BASELINK_P (name))
6096 adjust_result_of_qualified_name_lookup
6097 (name, parser->scope, scope);
6098 postfix_expression
6099 = finish_class_member_access_expr (postfix_expression, name,
6100 template_p,
6101 tf_warning_or_error);
6105 /* We no longer need to look up names in the scope of the object on
6106 the left-hand side of the `.' or `->' operator. */
6107 parser->context->object_type = NULL_TREE;
6109 /* Outside of offsetof, these operators may not appear in
6110 constant-expressions. */
6111 if (!for_offsetof
6112 && (cp_parser_non_integral_constant_expression
6113 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6114 postfix_expression = error_mark_node;
6116 return postfix_expression;
6119 /* Parse a parenthesized expression-list.
6121 expression-list:
6122 assignment-expression
6123 expression-list, assignment-expression
6125 attribute-list:
6126 expression-list
6127 identifier
6128 identifier, expression-list
6130 CAST_P is true if this expression is the target of a cast.
6132 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6133 argument pack.
6135 Returns a vector of trees. Each element is a representation of an
6136 assignment-expression. NULL is returned if the ( and or ) are
6137 missing. An empty, but allocated, vector is returned on no
6138 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6139 if we are parsing an attribute list for an attribute that wants a
6140 plain identifier argument, normal_attr for an attribute that wants
6141 an expression, or non_attr if we aren't parsing an attribute list. If
6142 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6143 not all of the expressions in the list were constant. */
6145 static vec<tree, va_gc> *
6146 cp_parser_parenthesized_expression_list (cp_parser* parser,
6147 int is_attribute_list,
6148 bool cast_p,
6149 bool allow_expansion_p,
6150 bool *non_constant_p)
6152 vec<tree, va_gc> *expression_list;
6153 bool fold_expr_p = is_attribute_list != non_attr;
6154 tree identifier = NULL_TREE;
6155 bool saved_greater_than_is_operator_p;
6157 /* Assume all the expressions will be constant. */
6158 if (non_constant_p)
6159 *non_constant_p = false;
6161 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6162 return NULL;
6164 expression_list = make_tree_vector ();
6166 /* Within a parenthesized expression, a `>' token is always
6167 the greater-than operator. */
6168 saved_greater_than_is_operator_p
6169 = parser->greater_than_is_operator_p;
6170 parser->greater_than_is_operator_p = true;
6172 /* Consume expressions until there are no more. */
6173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6174 while (true)
6176 tree expr;
6178 /* At the beginning of attribute lists, check to see if the
6179 next token is an identifier. */
6180 if (is_attribute_list == id_attr
6181 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6183 cp_token *token;
6185 /* Consume the identifier. */
6186 token = cp_lexer_consume_token (parser->lexer);
6187 /* Save the identifier. */
6188 identifier = token->u.value;
6190 else
6192 bool expr_non_constant_p;
6194 /* Parse the next assignment-expression. */
6195 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6197 /* A braced-init-list. */
6198 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6199 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6200 if (non_constant_p && expr_non_constant_p)
6201 *non_constant_p = true;
6203 else if (non_constant_p)
6205 expr = (cp_parser_constant_expression
6206 (parser, /*allow_non_constant_p=*/true,
6207 &expr_non_constant_p));
6208 if (expr_non_constant_p)
6209 *non_constant_p = true;
6211 else
6212 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6214 if (fold_expr_p)
6215 expr = fold_non_dependent_expr (expr);
6217 /* If we have an ellipsis, then this is an expression
6218 expansion. */
6219 if (allow_expansion_p
6220 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6222 /* Consume the `...'. */
6223 cp_lexer_consume_token (parser->lexer);
6225 /* Build the argument pack. */
6226 expr = make_pack_expansion (expr);
6229 /* Add it to the list. We add error_mark_node
6230 expressions to the list, so that we can still tell if
6231 the correct form for a parenthesized expression-list
6232 is found. That gives better errors. */
6233 vec_safe_push (expression_list, expr);
6235 if (expr == error_mark_node)
6236 goto skip_comma;
6239 /* After the first item, attribute lists look the same as
6240 expression lists. */
6241 is_attribute_list = non_attr;
6243 get_comma:;
6244 /* If the next token isn't a `,', then we are done. */
6245 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6246 break;
6248 /* Otherwise, consume the `,' and keep going. */
6249 cp_lexer_consume_token (parser->lexer);
6252 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6254 int ending;
6256 skip_comma:;
6257 /* We try and resync to an unnested comma, as that will give the
6258 user better diagnostics. */
6259 ending = cp_parser_skip_to_closing_parenthesis (parser,
6260 /*recovering=*/true,
6261 /*or_comma=*/true,
6262 /*consume_paren=*/true);
6263 if (ending < 0)
6264 goto get_comma;
6265 if (!ending)
6267 parser->greater_than_is_operator_p
6268 = saved_greater_than_is_operator_p;
6269 return NULL;
6273 parser->greater_than_is_operator_p
6274 = saved_greater_than_is_operator_p;
6276 if (identifier)
6277 vec_safe_insert (expression_list, 0, identifier);
6279 return expression_list;
6282 /* Parse a pseudo-destructor-name.
6284 pseudo-destructor-name:
6285 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6286 :: [opt] nested-name-specifier template template-id :: ~ type-name
6287 :: [opt] nested-name-specifier [opt] ~ type-name
6289 If either of the first two productions is used, sets *SCOPE to the
6290 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6291 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6292 or ERROR_MARK_NODE if the parse fails. */
6294 static void
6295 cp_parser_pseudo_destructor_name (cp_parser* parser,
6296 tree* scope,
6297 tree* type)
6299 bool nested_name_specifier_p;
6301 /* Assume that things will not work out. */
6302 *type = error_mark_node;
6304 /* Look for the optional `::' operator. */
6305 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6306 /* Look for the optional nested-name-specifier. */
6307 nested_name_specifier_p
6308 = (cp_parser_nested_name_specifier_opt (parser,
6309 /*typename_keyword_p=*/false,
6310 /*check_dependency_p=*/true,
6311 /*type_p=*/false,
6312 /*is_declaration=*/false)
6313 != NULL_TREE);
6314 /* Now, if we saw a nested-name-specifier, we might be doing the
6315 second production. */
6316 if (nested_name_specifier_p
6317 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6319 /* Consume the `template' keyword. */
6320 cp_lexer_consume_token (parser->lexer);
6321 /* Parse the template-id. */
6322 cp_parser_template_id (parser,
6323 /*template_keyword_p=*/true,
6324 /*check_dependency_p=*/false,
6325 class_type,
6326 /*is_declaration=*/true);
6327 /* Look for the `::' token. */
6328 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6330 /* If the next token is not a `~', then there might be some
6331 additional qualification. */
6332 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6334 /* At this point, we're looking for "type-name :: ~". The type-name
6335 must not be a class-name, since this is a pseudo-destructor. So,
6336 it must be either an enum-name, or a typedef-name -- both of which
6337 are just identifiers. So, we peek ahead to check that the "::"
6338 and "~" tokens are present; if they are not, then we can avoid
6339 calling type_name. */
6340 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6341 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6342 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6344 cp_parser_error (parser, "non-scalar type");
6345 return;
6348 /* Look for the type-name. */
6349 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6350 if (*scope == error_mark_node)
6351 return;
6353 /* Look for the `::' token. */
6354 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6356 else
6357 *scope = NULL_TREE;
6359 /* Look for the `~'. */
6360 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6362 /* Once we see the ~, this has to be a pseudo-destructor. */
6363 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6364 cp_parser_commit_to_tentative_parse (parser);
6366 /* Look for the type-name again. We are not responsible for
6367 checking that it matches the first type-name. */
6368 *type = cp_parser_nonclass_name (parser);
6371 /* Parse a unary-expression.
6373 unary-expression:
6374 postfix-expression
6375 ++ cast-expression
6376 -- cast-expression
6377 unary-operator cast-expression
6378 sizeof unary-expression
6379 sizeof ( type-id )
6380 alignof ( type-id ) [C++0x]
6381 new-expression
6382 delete-expression
6384 GNU Extensions:
6386 unary-expression:
6387 __extension__ cast-expression
6388 __alignof__ unary-expression
6389 __alignof__ ( type-id )
6390 alignof unary-expression [C++0x]
6391 __real__ cast-expression
6392 __imag__ cast-expression
6393 && identifier
6395 ADDRESS_P is true iff the unary-expression is appearing as the
6396 operand of the `&' operator. CAST_P is true if this expression is
6397 the target of a cast.
6399 Returns a representation of the expression. */
6401 static tree
6402 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6403 cp_id_kind * pidk)
6405 cp_token *token;
6406 enum tree_code unary_operator;
6408 /* Peek at the next token. */
6409 token = cp_lexer_peek_token (parser->lexer);
6410 /* Some keywords give away the kind of expression. */
6411 if (token->type == CPP_KEYWORD)
6413 enum rid keyword = token->keyword;
6415 switch (keyword)
6417 case RID_ALIGNOF:
6418 case RID_SIZEOF:
6420 tree operand, ret;
6421 enum tree_code op;
6422 location_t first_loc;
6424 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6425 /* Consume the token. */
6426 cp_lexer_consume_token (parser->lexer);
6427 first_loc = cp_lexer_peek_token (parser->lexer)->location;
6428 /* Parse the operand. */
6429 operand = cp_parser_sizeof_operand (parser, keyword);
6431 if (TYPE_P (operand))
6432 ret = cxx_sizeof_or_alignof_type (operand, op, true);
6433 else
6435 /* ISO C++ defines alignof only with types, not with
6436 expressions. So pedwarn if alignof is used with a non-
6437 type expression. However, __alignof__ is ok. */
6438 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6439 pedwarn (token->location, OPT_Wpedantic,
6440 "ISO C++ does not allow %<alignof%> "
6441 "with a non-type");
6443 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
6445 /* For SIZEOF_EXPR, just issue diagnostics, but keep
6446 SIZEOF_EXPR with the original operand. */
6447 if (op == SIZEOF_EXPR && ret != error_mark_node)
6449 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
6451 if (!processing_template_decl && TYPE_P (operand))
6453 ret = build_min (SIZEOF_EXPR, size_type_node,
6454 build1 (NOP_EXPR, operand,
6455 error_mark_node));
6456 SIZEOF_EXPR_TYPE_P (ret) = 1;
6458 else
6459 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
6460 TREE_SIDE_EFFECTS (ret) = 0;
6461 TREE_READONLY (ret) = 1;
6463 SET_EXPR_LOCATION (ret, first_loc);
6465 return ret;
6468 case RID_NEW:
6469 return cp_parser_new_expression (parser);
6471 case RID_DELETE:
6472 return cp_parser_delete_expression (parser);
6474 case RID_EXTENSION:
6476 /* The saved value of the PEDANTIC flag. */
6477 int saved_pedantic;
6478 tree expr;
6480 /* Save away the PEDANTIC flag. */
6481 cp_parser_extension_opt (parser, &saved_pedantic);
6482 /* Parse the cast-expression. */
6483 expr = cp_parser_simple_cast_expression (parser);
6484 /* Restore the PEDANTIC flag. */
6485 pedantic = saved_pedantic;
6487 return expr;
6490 case RID_REALPART:
6491 case RID_IMAGPART:
6493 tree expression;
6495 /* Consume the `__real__' or `__imag__' token. */
6496 cp_lexer_consume_token (parser->lexer);
6497 /* Parse the cast-expression. */
6498 expression = cp_parser_simple_cast_expression (parser);
6499 /* Create the complete representation. */
6500 return build_x_unary_op (token->location,
6501 (keyword == RID_REALPART
6502 ? REALPART_EXPR : IMAGPART_EXPR),
6503 expression,
6504 tf_warning_or_error);
6506 break;
6508 case RID_TRANSACTION_ATOMIC:
6509 case RID_TRANSACTION_RELAXED:
6510 return cp_parser_transaction_expression (parser, keyword);
6512 case RID_NOEXCEPT:
6514 tree expr;
6515 const char *saved_message;
6516 bool saved_integral_constant_expression_p;
6517 bool saved_non_integral_constant_expression_p;
6518 bool saved_greater_than_is_operator_p;
6520 cp_lexer_consume_token (parser->lexer);
6521 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6523 saved_message = parser->type_definition_forbidden_message;
6524 parser->type_definition_forbidden_message
6525 = G_("types may not be defined in %<noexcept%> expressions");
6527 saved_integral_constant_expression_p
6528 = parser->integral_constant_expression_p;
6529 saved_non_integral_constant_expression_p
6530 = parser->non_integral_constant_expression_p;
6531 parser->integral_constant_expression_p = false;
6533 saved_greater_than_is_operator_p
6534 = parser->greater_than_is_operator_p;
6535 parser->greater_than_is_operator_p = true;
6537 ++cp_unevaluated_operand;
6538 ++c_inhibit_evaluation_warnings;
6539 expr = cp_parser_expression (parser, false, NULL);
6540 --c_inhibit_evaluation_warnings;
6541 --cp_unevaluated_operand;
6543 parser->greater_than_is_operator_p
6544 = saved_greater_than_is_operator_p;
6546 parser->integral_constant_expression_p
6547 = saved_integral_constant_expression_p;
6548 parser->non_integral_constant_expression_p
6549 = saved_non_integral_constant_expression_p;
6551 parser->type_definition_forbidden_message = saved_message;
6553 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6554 return finish_noexcept_expr (expr, tf_warning_or_error);
6557 default:
6558 break;
6562 /* Look for the `:: new' and `:: delete', which also signal the
6563 beginning of a new-expression, or delete-expression,
6564 respectively. If the next token is `::', then it might be one of
6565 these. */
6566 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6568 enum rid keyword;
6570 /* See if the token after the `::' is one of the keywords in
6571 which we're interested. */
6572 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6573 /* If it's `new', we have a new-expression. */
6574 if (keyword == RID_NEW)
6575 return cp_parser_new_expression (parser);
6576 /* Similarly, for `delete'. */
6577 else if (keyword == RID_DELETE)
6578 return cp_parser_delete_expression (parser);
6581 /* Look for a unary operator. */
6582 unary_operator = cp_parser_unary_operator (token);
6583 /* The `++' and `--' operators can be handled similarly, even though
6584 they are not technically unary-operators in the grammar. */
6585 if (unary_operator == ERROR_MARK)
6587 if (token->type == CPP_PLUS_PLUS)
6588 unary_operator = PREINCREMENT_EXPR;
6589 else if (token->type == CPP_MINUS_MINUS)
6590 unary_operator = PREDECREMENT_EXPR;
6591 /* Handle the GNU address-of-label extension. */
6592 else if (cp_parser_allow_gnu_extensions_p (parser)
6593 && token->type == CPP_AND_AND)
6595 tree identifier;
6596 tree expression;
6597 location_t loc = token->location;
6599 /* Consume the '&&' token. */
6600 cp_lexer_consume_token (parser->lexer);
6601 /* Look for the identifier. */
6602 identifier = cp_parser_identifier (parser);
6603 /* Create an expression representing the address. */
6604 expression = finish_label_address_expr (identifier, loc);
6605 if (cp_parser_non_integral_constant_expression (parser,
6606 NIC_ADDR_LABEL))
6607 expression = error_mark_node;
6608 return expression;
6611 if (unary_operator != ERROR_MARK)
6613 tree cast_expression;
6614 tree expression = error_mark_node;
6615 non_integral_constant non_constant_p = NIC_NONE;
6616 location_t loc = token->location;
6618 /* Consume the operator token. */
6619 token = cp_lexer_consume_token (parser->lexer);
6620 /* Parse the cast-expression. */
6621 cast_expression
6622 = cp_parser_cast_expression (parser,
6623 unary_operator == ADDR_EXPR,
6624 /*cast_p=*/false, pidk);
6625 /* Now, build an appropriate representation. */
6626 switch (unary_operator)
6628 case INDIRECT_REF:
6629 non_constant_p = NIC_STAR;
6630 expression = build_x_indirect_ref (loc, cast_expression,
6631 RO_UNARY_STAR,
6632 tf_warning_or_error);
6633 break;
6635 case ADDR_EXPR:
6636 non_constant_p = NIC_ADDR;
6637 /* Fall through. */
6638 case BIT_NOT_EXPR:
6639 expression = build_x_unary_op (loc, unary_operator,
6640 cast_expression,
6641 tf_warning_or_error);
6642 break;
6644 case PREINCREMENT_EXPR:
6645 case PREDECREMENT_EXPR:
6646 non_constant_p = unary_operator == PREINCREMENT_EXPR
6647 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6648 /* Fall through. */
6649 case UNARY_PLUS_EXPR:
6650 case NEGATE_EXPR:
6651 case TRUTH_NOT_EXPR:
6652 expression = finish_unary_op_expr (loc, unary_operator,
6653 cast_expression);
6654 break;
6656 default:
6657 gcc_unreachable ();
6660 if (non_constant_p != NIC_NONE
6661 && cp_parser_non_integral_constant_expression (parser,
6662 non_constant_p))
6663 expression = error_mark_node;
6665 return expression;
6668 return cp_parser_postfix_expression (parser, address_p, cast_p,
6669 /*member_access_only_p=*/false,
6670 pidk);
6673 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6674 unary-operator, the corresponding tree code is returned. */
6676 static enum tree_code
6677 cp_parser_unary_operator (cp_token* token)
6679 switch (token->type)
6681 case CPP_MULT:
6682 return INDIRECT_REF;
6684 case CPP_AND:
6685 return ADDR_EXPR;
6687 case CPP_PLUS:
6688 return UNARY_PLUS_EXPR;
6690 case CPP_MINUS:
6691 return NEGATE_EXPR;
6693 case CPP_NOT:
6694 return TRUTH_NOT_EXPR;
6696 case CPP_COMPL:
6697 return BIT_NOT_EXPR;
6699 default:
6700 return ERROR_MARK;
6704 /* Parse a new-expression.
6706 new-expression:
6707 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6708 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6710 Returns a representation of the expression. */
6712 static tree
6713 cp_parser_new_expression (cp_parser* parser)
6715 bool global_scope_p;
6716 vec<tree, va_gc> *placement;
6717 tree type;
6718 vec<tree, va_gc> *initializer;
6719 tree nelts = NULL_TREE;
6720 tree ret;
6722 /* Look for the optional `::' operator. */
6723 global_scope_p
6724 = (cp_parser_global_scope_opt (parser,
6725 /*current_scope_valid_p=*/false)
6726 != NULL_TREE);
6727 /* Look for the `new' operator. */
6728 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6729 /* There's no easy way to tell a new-placement from the
6730 `( type-id )' construct. */
6731 cp_parser_parse_tentatively (parser);
6732 /* Look for a new-placement. */
6733 placement = cp_parser_new_placement (parser);
6734 /* If that didn't work out, there's no new-placement. */
6735 if (!cp_parser_parse_definitely (parser))
6737 if (placement != NULL)
6738 release_tree_vector (placement);
6739 placement = NULL;
6742 /* If the next token is a `(', then we have a parenthesized
6743 type-id. */
6744 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6746 cp_token *token;
6747 const char *saved_message = parser->type_definition_forbidden_message;
6749 /* Consume the `('. */
6750 cp_lexer_consume_token (parser->lexer);
6752 /* Parse the type-id. */
6753 parser->type_definition_forbidden_message
6754 = G_("types may not be defined in a new-expression");
6755 type = cp_parser_type_id (parser);
6756 parser->type_definition_forbidden_message = saved_message;
6758 /* Look for the closing `)'. */
6759 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6760 token = cp_lexer_peek_token (parser->lexer);
6761 /* There should not be a direct-new-declarator in this production,
6762 but GCC used to allowed this, so we check and emit a sensible error
6763 message for this case. */
6764 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6766 error_at (token->location,
6767 "array bound forbidden after parenthesized type-id");
6768 inform (token->location,
6769 "try removing the parentheses around the type-id");
6770 cp_parser_direct_new_declarator (parser);
6773 /* Otherwise, there must be a new-type-id. */
6774 else
6775 type = cp_parser_new_type_id (parser, &nelts);
6777 /* If the next token is a `(' or '{', then we have a new-initializer. */
6778 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6779 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6780 initializer = cp_parser_new_initializer (parser);
6781 else
6782 initializer = NULL;
6784 /* A new-expression may not appear in an integral constant
6785 expression. */
6786 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6787 ret = error_mark_node;
6788 else
6790 /* Create a representation of the new-expression. */
6791 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6792 tf_warning_or_error);
6795 if (placement != NULL)
6796 release_tree_vector (placement);
6797 if (initializer != NULL)
6798 release_tree_vector (initializer);
6800 return ret;
6803 /* Parse a new-placement.
6805 new-placement:
6806 ( expression-list )
6808 Returns the same representation as for an expression-list. */
6810 static vec<tree, va_gc> *
6811 cp_parser_new_placement (cp_parser* parser)
6813 vec<tree, va_gc> *expression_list;
6815 /* Parse the expression-list. */
6816 expression_list = (cp_parser_parenthesized_expression_list
6817 (parser, non_attr, /*cast_p=*/false,
6818 /*allow_expansion_p=*/true,
6819 /*non_constant_p=*/NULL));
6821 return expression_list;
6824 /* Parse a new-type-id.
6826 new-type-id:
6827 type-specifier-seq new-declarator [opt]
6829 Returns the TYPE allocated. If the new-type-id indicates an array
6830 type, *NELTS is set to the number of elements in the last array
6831 bound; the TYPE will not include the last array bound. */
6833 static tree
6834 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6836 cp_decl_specifier_seq type_specifier_seq;
6837 cp_declarator *new_declarator;
6838 cp_declarator *declarator;
6839 cp_declarator *outer_declarator;
6840 const char *saved_message;
6842 /* The type-specifier sequence must not contain type definitions.
6843 (It cannot contain declarations of new types either, but if they
6844 are not definitions we will catch that because they are not
6845 complete.) */
6846 saved_message = parser->type_definition_forbidden_message;
6847 parser->type_definition_forbidden_message
6848 = G_("types may not be defined in a new-type-id");
6849 /* Parse the type-specifier-seq. */
6850 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6851 /*is_trailing_return=*/false,
6852 &type_specifier_seq);
6853 /* Restore the old message. */
6854 parser->type_definition_forbidden_message = saved_message;
6856 if (type_specifier_seq.type == error_mark_node)
6857 return error_mark_node;
6859 /* Parse the new-declarator. */
6860 new_declarator = cp_parser_new_declarator_opt (parser);
6862 /* Determine the number of elements in the last array dimension, if
6863 any. */
6864 *nelts = NULL_TREE;
6865 /* Skip down to the last array dimension. */
6866 declarator = new_declarator;
6867 outer_declarator = NULL;
6868 while (declarator && (declarator->kind == cdk_pointer
6869 || declarator->kind == cdk_ptrmem))
6871 outer_declarator = declarator;
6872 declarator = declarator->declarator;
6874 while (declarator
6875 && declarator->kind == cdk_array
6876 && declarator->declarator
6877 && declarator->declarator->kind == cdk_array)
6879 outer_declarator = declarator;
6880 declarator = declarator->declarator;
6883 if (declarator && declarator->kind == cdk_array)
6885 *nelts = declarator->u.array.bounds;
6886 if (*nelts == error_mark_node)
6887 *nelts = integer_one_node;
6889 if (outer_declarator)
6890 outer_declarator->declarator = declarator->declarator;
6891 else
6892 new_declarator = NULL;
6895 return groktypename (&type_specifier_seq, new_declarator, false);
6898 /* Parse an (optional) new-declarator.
6900 new-declarator:
6901 ptr-operator new-declarator [opt]
6902 direct-new-declarator
6904 Returns the declarator. */
6906 static cp_declarator *
6907 cp_parser_new_declarator_opt (cp_parser* parser)
6909 enum tree_code code;
6910 tree type, std_attributes = NULL_TREE;
6911 cp_cv_quals cv_quals;
6913 /* We don't know if there's a ptr-operator next, or not. */
6914 cp_parser_parse_tentatively (parser);
6915 /* Look for a ptr-operator. */
6916 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
6917 /* If that worked, look for more new-declarators. */
6918 if (cp_parser_parse_definitely (parser))
6920 cp_declarator *declarator;
6922 /* Parse another optional declarator. */
6923 declarator = cp_parser_new_declarator_opt (parser);
6925 declarator = cp_parser_make_indirect_declarator
6926 (code, type, cv_quals, declarator, std_attributes);
6928 return declarator;
6931 /* If the next token is a `[', there is a direct-new-declarator. */
6932 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6933 return cp_parser_direct_new_declarator (parser);
6935 return NULL;
6938 /* Parse a direct-new-declarator.
6940 direct-new-declarator:
6941 [ expression ]
6942 direct-new-declarator [constant-expression]
6946 static cp_declarator *
6947 cp_parser_direct_new_declarator (cp_parser* parser)
6949 cp_declarator *declarator = NULL;
6951 while (true)
6953 tree expression;
6954 cp_token *token;
6956 /* Look for the opening `['. */
6957 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6959 token = cp_lexer_peek_token (parser->lexer);
6960 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6961 /* The standard requires that the expression have integral
6962 type. DR 74 adds enumeration types. We believe that the
6963 real intent is that these expressions be handled like the
6964 expression in a `switch' condition, which also allows
6965 classes with a single conversion to integral or
6966 enumeration type. */
6967 if (!processing_template_decl)
6969 expression
6970 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6971 expression,
6972 /*complain=*/true);
6973 if (!expression)
6975 error_at (token->location,
6976 "expression in new-declarator must have integral "
6977 "or enumeration type");
6978 expression = error_mark_node;
6982 /* Look for the closing `]'. */
6983 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6985 /* Add this bound to the declarator. */
6986 declarator = make_array_declarator (declarator, expression);
6988 /* If the next token is not a `[', then there are no more
6989 bounds. */
6990 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6991 break;
6994 return declarator;
6997 /* Parse a new-initializer.
6999 new-initializer:
7000 ( expression-list [opt] )
7001 braced-init-list
7003 Returns a representation of the expression-list. */
7005 static vec<tree, va_gc> *
7006 cp_parser_new_initializer (cp_parser* parser)
7008 vec<tree, va_gc> *expression_list;
7010 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7012 tree t;
7013 bool expr_non_constant_p;
7014 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7015 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7016 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7017 expression_list = make_tree_vector_single (t);
7019 else
7020 expression_list = (cp_parser_parenthesized_expression_list
7021 (parser, non_attr, /*cast_p=*/false,
7022 /*allow_expansion_p=*/true,
7023 /*non_constant_p=*/NULL));
7025 return expression_list;
7028 /* Parse a delete-expression.
7030 delete-expression:
7031 :: [opt] delete cast-expression
7032 :: [opt] delete [ ] cast-expression
7034 Returns a representation of the expression. */
7036 static tree
7037 cp_parser_delete_expression (cp_parser* parser)
7039 bool global_scope_p;
7040 bool array_p;
7041 tree expression;
7043 /* Look for the optional `::' operator. */
7044 global_scope_p
7045 = (cp_parser_global_scope_opt (parser,
7046 /*current_scope_valid_p=*/false)
7047 != NULL_TREE);
7048 /* Look for the `delete' keyword. */
7049 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7050 /* See if the array syntax is in use. */
7051 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7053 /* Consume the `[' token. */
7054 cp_lexer_consume_token (parser->lexer);
7055 /* Look for the `]' token. */
7056 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7057 /* Remember that this is the `[]' construct. */
7058 array_p = true;
7060 else
7061 array_p = false;
7063 /* Parse the cast-expression. */
7064 expression = cp_parser_simple_cast_expression (parser);
7066 /* A delete-expression may not appear in an integral constant
7067 expression. */
7068 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7069 return error_mark_node;
7071 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7072 tf_warning_or_error);
7075 /* Returns true if TOKEN may start a cast-expression and false
7076 otherwise. */
7078 static bool
7079 cp_parser_token_starts_cast_expression (cp_token *token)
7081 switch (token->type)
7083 case CPP_COMMA:
7084 case CPP_SEMICOLON:
7085 case CPP_QUERY:
7086 case CPP_COLON:
7087 case CPP_CLOSE_SQUARE:
7088 case CPP_CLOSE_PAREN:
7089 case CPP_CLOSE_BRACE:
7090 case CPP_DOT:
7091 case CPP_DOT_STAR:
7092 case CPP_DEREF:
7093 case CPP_DEREF_STAR:
7094 case CPP_DIV:
7095 case CPP_MOD:
7096 case CPP_LSHIFT:
7097 case CPP_RSHIFT:
7098 case CPP_LESS:
7099 case CPP_GREATER:
7100 case CPP_LESS_EQ:
7101 case CPP_GREATER_EQ:
7102 case CPP_EQ_EQ:
7103 case CPP_NOT_EQ:
7104 case CPP_EQ:
7105 case CPP_MULT_EQ:
7106 case CPP_DIV_EQ:
7107 case CPP_MOD_EQ:
7108 case CPP_PLUS_EQ:
7109 case CPP_MINUS_EQ:
7110 case CPP_RSHIFT_EQ:
7111 case CPP_LSHIFT_EQ:
7112 case CPP_AND_EQ:
7113 case CPP_XOR_EQ:
7114 case CPP_OR_EQ:
7115 case CPP_XOR:
7116 case CPP_OR:
7117 case CPP_OR_OR:
7118 case CPP_EOF:
7119 return false;
7121 /* '[' may start a primary-expression in obj-c++. */
7122 case CPP_OPEN_SQUARE:
7123 return c_dialect_objc ();
7125 default:
7126 return true;
7130 /* Parse a cast-expression.
7132 cast-expression:
7133 unary-expression
7134 ( type-id ) cast-expression
7136 ADDRESS_P is true iff the unary-expression is appearing as the
7137 operand of the `&' operator. CAST_P is true if this expression is
7138 the target of a cast.
7140 Returns a representation of the expression. */
7142 static tree
7143 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7144 cp_id_kind * pidk)
7146 /* If it's a `(', then we might be looking at a cast. */
7147 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7149 tree type = NULL_TREE;
7150 tree expr = NULL_TREE;
7151 bool compound_literal_p;
7152 const char *saved_message;
7154 /* There's no way to know yet whether or not this is a cast.
7155 For example, `(int (3))' is a unary-expression, while `(int)
7156 3' is a cast. So, we resort to parsing tentatively. */
7157 cp_parser_parse_tentatively (parser);
7158 /* Types may not be defined in a cast. */
7159 saved_message = parser->type_definition_forbidden_message;
7160 parser->type_definition_forbidden_message
7161 = G_("types may not be defined in casts");
7162 /* Consume the `('. */
7163 cp_lexer_consume_token (parser->lexer);
7164 /* A very tricky bit is that `(struct S) { 3 }' is a
7165 compound-literal (which we permit in C++ as an extension).
7166 But, that construct is not a cast-expression -- it is a
7167 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7168 is legal; if the compound-literal were a cast-expression,
7169 you'd need an extra set of parentheses.) But, if we parse
7170 the type-id, and it happens to be a class-specifier, then we
7171 will commit to the parse at that point, because we cannot
7172 undo the action that is done when creating a new class. So,
7173 then we cannot back up and do a postfix-expression.
7175 Therefore, we scan ahead to the closing `)', and check to see
7176 if the token after the `)' is a `{'. If so, we are not
7177 looking at a cast-expression.
7179 Save tokens so that we can put them back. */
7180 cp_lexer_save_tokens (parser->lexer);
7181 /* Skip tokens until the next token is a closing parenthesis.
7182 If we find the closing `)', and the next token is a `{', then
7183 we are looking at a compound-literal. */
7184 compound_literal_p
7185 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7186 /*consume_paren=*/true)
7187 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7188 /* Roll back the tokens we skipped. */
7189 cp_lexer_rollback_tokens (parser->lexer);
7190 /* If we were looking at a compound-literal, simulate an error
7191 so that the call to cp_parser_parse_definitely below will
7192 fail. */
7193 if (compound_literal_p)
7194 cp_parser_simulate_error (parser);
7195 else
7197 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7198 parser->in_type_id_in_expr_p = true;
7199 /* Look for the type-id. */
7200 type = cp_parser_type_id (parser);
7201 /* Look for the closing `)'. */
7202 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7203 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7206 /* Restore the saved message. */
7207 parser->type_definition_forbidden_message = saved_message;
7209 /* At this point this can only be either a cast or a
7210 parenthesized ctor such as `(T ())' that looks like a cast to
7211 function returning T. */
7212 if (!cp_parser_error_occurred (parser)
7213 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7214 (parser->lexer)))
7216 cp_parser_parse_definitely (parser);
7217 expr = cp_parser_cast_expression (parser,
7218 /*address_p=*/false,
7219 /*cast_p=*/true, pidk);
7221 /* Warn about old-style casts, if so requested. */
7222 if (warn_old_style_cast
7223 && !in_system_header
7224 && !VOID_TYPE_P (type)
7225 && current_lang_name != lang_name_c)
7226 warning (OPT_Wold_style_cast, "use of old-style cast");
7228 /* Only type conversions to integral or enumeration types
7229 can be used in constant-expressions. */
7230 if (!cast_valid_in_integral_constant_expression_p (type)
7231 && cp_parser_non_integral_constant_expression (parser,
7232 NIC_CAST))
7233 return error_mark_node;
7235 /* Perform the cast. */
7236 expr = build_c_cast (input_location, type, expr);
7237 return expr;
7239 else
7240 cp_parser_abort_tentative_parse (parser);
7243 /* If we get here, then it's not a cast, so it must be a
7244 unary-expression. */
7245 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7248 /* Parse a binary expression of the general form:
7250 pm-expression:
7251 cast-expression
7252 pm-expression .* cast-expression
7253 pm-expression ->* cast-expression
7255 multiplicative-expression:
7256 pm-expression
7257 multiplicative-expression * pm-expression
7258 multiplicative-expression / pm-expression
7259 multiplicative-expression % pm-expression
7261 additive-expression:
7262 multiplicative-expression
7263 additive-expression + multiplicative-expression
7264 additive-expression - multiplicative-expression
7266 shift-expression:
7267 additive-expression
7268 shift-expression << additive-expression
7269 shift-expression >> additive-expression
7271 relational-expression:
7272 shift-expression
7273 relational-expression < shift-expression
7274 relational-expression > shift-expression
7275 relational-expression <= shift-expression
7276 relational-expression >= shift-expression
7278 GNU Extension:
7280 relational-expression:
7281 relational-expression <? shift-expression
7282 relational-expression >? shift-expression
7284 equality-expression:
7285 relational-expression
7286 equality-expression == relational-expression
7287 equality-expression != relational-expression
7289 and-expression:
7290 equality-expression
7291 and-expression & equality-expression
7293 exclusive-or-expression:
7294 and-expression
7295 exclusive-or-expression ^ and-expression
7297 inclusive-or-expression:
7298 exclusive-or-expression
7299 inclusive-or-expression | exclusive-or-expression
7301 logical-and-expression:
7302 inclusive-or-expression
7303 logical-and-expression && inclusive-or-expression
7305 logical-or-expression:
7306 logical-and-expression
7307 logical-or-expression || logical-and-expression
7309 All these are implemented with a single function like:
7311 binary-expression:
7312 simple-cast-expression
7313 binary-expression <token> binary-expression
7315 CAST_P is true if this expression is the target of a cast.
7317 The binops_by_token map is used to get the tree codes for each <token> type.
7318 binary-expressions are associated according to a precedence table. */
7320 #define TOKEN_PRECEDENCE(token) \
7321 (((token->type == CPP_GREATER \
7322 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7323 && !parser->greater_than_is_operator_p) \
7324 ? PREC_NOT_OPERATOR \
7325 : binops_by_token[token->type].prec)
7327 static tree
7328 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7329 bool no_toplevel_fold_p,
7330 enum cp_parser_prec prec,
7331 cp_id_kind * pidk)
7333 cp_parser_expression_stack stack;
7334 cp_parser_expression_stack_entry *sp = &stack[0];
7335 cp_parser_expression_stack_entry current;
7336 tree rhs;
7337 cp_token *token;
7338 enum tree_code rhs_type;
7339 enum cp_parser_prec new_prec, lookahead_prec;
7340 tree overload;
7342 /* Parse the first expression. */
7343 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7344 cast_p, pidk);
7345 current.lhs_type = ERROR_MARK;
7346 current.prec = prec;
7348 if (cp_parser_error_occurred (parser))
7349 return error_mark_node;
7351 for (;;)
7353 /* Get an operator token. */
7354 token = cp_lexer_peek_token (parser->lexer);
7356 if (warn_cxx0x_compat
7357 && token->type == CPP_RSHIFT
7358 && !parser->greater_than_is_operator_p)
7360 if (warning_at (token->location, OPT_Wc__0x_compat,
7361 "%<>>%> operator is treated"
7362 " as two right angle brackets in C++11"))
7363 inform (token->location,
7364 "suggest parentheses around %<>>%> expression");
7367 new_prec = TOKEN_PRECEDENCE (token);
7369 /* Popping an entry off the stack means we completed a subexpression:
7370 - either we found a token which is not an operator (`>' where it is not
7371 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7372 will happen repeatedly;
7373 - or, we found an operator which has lower priority. This is the case
7374 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7375 parsing `3 * 4'. */
7376 if (new_prec <= current.prec)
7378 if (sp == stack)
7379 break;
7380 else
7381 goto pop;
7384 get_rhs:
7385 current.tree_type = binops_by_token[token->type].tree_type;
7386 current.loc = token->location;
7388 /* We used the operator token. */
7389 cp_lexer_consume_token (parser->lexer);
7391 /* For "false && x" or "true || x", x will never be executed;
7392 disable warnings while evaluating it. */
7393 if (current.tree_type == TRUTH_ANDIF_EXPR)
7394 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7395 else if (current.tree_type == TRUTH_ORIF_EXPR)
7396 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
7398 /* Extract another operand. It may be the RHS of this expression
7399 or the LHS of a new, higher priority expression. */
7400 rhs = cp_parser_simple_cast_expression (parser);
7401 rhs_type = ERROR_MARK;
7403 /* Get another operator token. Look up its precedence to avoid
7404 building a useless (immediately popped) stack entry for common
7405 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7406 token = cp_lexer_peek_token (parser->lexer);
7407 lookahead_prec = TOKEN_PRECEDENCE (token);
7408 if (lookahead_prec > new_prec)
7410 /* ... and prepare to parse the RHS of the new, higher priority
7411 expression. Since precedence levels on the stack are
7412 monotonically increasing, we do not have to care about
7413 stack overflows. */
7414 *sp = current;
7415 ++sp;
7416 current.lhs = rhs;
7417 current.lhs_type = rhs_type;
7418 current.prec = new_prec;
7419 new_prec = lookahead_prec;
7420 goto get_rhs;
7422 pop:
7423 lookahead_prec = new_prec;
7424 /* If the stack is not empty, we have parsed into LHS the right side
7425 (`4' in the example above) of an expression we had suspended.
7426 We can use the information on the stack to recover the LHS (`3')
7427 from the stack together with the tree code (`MULT_EXPR'), and
7428 the precedence of the higher level subexpression
7429 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7430 which will be used to actually build the additive expression. */
7431 rhs = current.lhs;
7432 rhs_type = current.lhs_type;
7433 --sp;
7434 current = *sp;
7437 /* Undo the disabling of warnings done above. */
7438 if (current.tree_type == TRUTH_ANDIF_EXPR)
7439 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7440 else if (current.tree_type == TRUTH_ORIF_EXPR)
7441 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
7443 overload = NULL;
7444 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7445 ERROR_MARK for everything that is not a binary expression.
7446 This makes warn_about_parentheses miss some warnings that
7447 involve unary operators. For unary expressions we should
7448 pass the correct tree_code unless the unary expression was
7449 surrounded by parentheses.
7451 if (no_toplevel_fold_p
7452 && lookahead_prec <= current.prec
7453 && sp == stack
7454 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7455 current.lhs = build2 (current.tree_type, boolean_type_node,
7456 current.lhs, rhs);
7457 else
7458 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7459 current.lhs, current.lhs_type,
7460 rhs, rhs_type, &overload,
7461 tf_warning_or_error);
7462 current.lhs_type = current.tree_type;
7463 if (EXPR_P (current.lhs))
7464 SET_EXPR_LOCATION (current.lhs, current.loc);
7466 /* If the binary operator required the use of an overloaded operator,
7467 then this expression cannot be an integral constant-expression.
7468 An overloaded operator can be used even if both operands are
7469 otherwise permissible in an integral constant-expression if at
7470 least one of the operands is of enumeration type. */
7472 if (overload
7473 && cp_parser_non_integral_constant_expression (parser,
7474 NIC_OVERLOADED))
7475 return error_mark_node;
7478 return current.lhs;
7482 /* Parse the `? expression : assignment-expression' part of a
7483 conditional-expression. The LOGICAL_OR_EXPR is the
7484 logical-or-expression that started the conditional-expression.
7485 Returns a representation of the entire conditional-expression.
7487 This routine is used by cp_parser_assignment_expression.
7489 ? expression : assignment-expression
7491 GNU Extensions:
7493 ? : assignment-expression */
7495 static tree
7496 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7498 tree expr;
7499 tree assignment_expr;
7500 struct cp_token *token;
7501 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7503 /* Consume the `?' token. */
7504 cp_lexer_consume_token (parser->lexer);
7505 token = cp_lexer_peek_token (parser->lexer);
7506 if (cp_parser_allow_gnu_extensions_p (parser)
7507 && token->type == CPP_COLON)
7509 pedwarn (token->location, OPT_Wpedantic,
7510 "ISO C++ does not allow ?: with omitted middle operand");
7511 /* Implicit true clause. */
7512 expr = NULL_TREE;
7513 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7514 warn_for_omitted_condop (token->location, logical_or_expr);
7516 else
7518 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7519 parser->colon_corrects_to_scope_p = false;
7520 /* Parse the expression. */
7521 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7522 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7523 c_inhibit_evaluation_warnings +=
7524 ((logical_or_expr == truthvalue_true_node)
7525 - (logical_or_expr == truthvalue_false_node));
7526 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7529 /* The next token should be a `:'. */
7530 cp_parser_require (parser, CPP_COLON, RT_COLON);
7531 /* Parse the assignment-expression. */
7532 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7533 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7535 /* Build the conditional-expression. */
7536 return build_x_conditional_expr (loc, logical_or_expr,
7537 expr,
7538 assignment_expr,
7539 tf_warning_or_error);
7542 /* Parse an assignment-expression.
7544 assignment-expression:
7545 conditional-expression
7546 logical-or-expression assignment-operator assignment_expression
7547 throw-expression
7549 CAST_P is true if this expression is the target of a cast.
7551 Returns a representation for the expression. */
7553 static tree
7554 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7555 cp_id_kind * pidk)
7557 tree expr;
7559 /* If the next token is the `throw' keyword, then we're looking at
7560 a throw-expression. */
7561 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7562 expr = cp_parser_throw_expression (parser);
7563 /* Otherwise, it must be that we are looking at a
7564 logical-or-expression. */
7565 else
7567 /* Parse the binary expressions (logical-or-expression). */
7568 expr = cp_parser_binary_expression (parser, cast_p, false,
7569 PREC_NOT_OPERATOR, pidk);
7570 /* If the next token is a `?' then we're actually looking at a
7571 conditional-expression. */
7572 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7573 return cp_parser_question_colon_clause (parser, expr);
7574 else
7576 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7578 /* If it's an assignment-operator, we're using the second
7579 production. */
7580 enum tree_code assignment_operator
7581 = cp_parser_assignment_operator_opt (parser);
7582 if (assignment_operator != ERROR_MARK)
7584 bool non_constant_p;
7585 location_t saved_input_location;
7587 /* Parse the right-hand side of the assignment. */
7588 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7590 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7591 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7593 /* An assignment may not appear in a
7594 constant-expression. */
7595 if (cp_parser_non_integral_constant_expression (parser,
7596 NIC_ASSIGNMENT))
7597 return error_mark_node;
7598 /* Build the assignment expression. Its default
7599 location is the location of the '=' token. */
7600 saved_input_location = input_location;
7601 input_location = loc;
7602 expr = build_x_modify_expr (loc, expr,
7603 assignment_operator,
7604 rhs,
7605 tf_warning_or_error);
7606 input_location = saved_input_location;
7611 return expr;
7614 /* Parse an (optional) assignment-operator.
7616 assignment-operator: one of
7617 = *= /= %= += -= >>= <<= &= ^= |=
7619 GNU Extension:
7621 assignment-operator: one of
7622 <?= >?=
7624 If the next token is an assignment operator, the corresponding tree
7625 code is returned, and the token is consumed. For example, for
7626 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7627 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7628 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7629 operator, ERROR_MARK is returned. */
7631 static enum tree_code
7632 cp_parser_assignment_operator_opt (cp_parser* parser)
7634 enum tree_code op;
7635 cp_token *token;
7637 /* Peek at the next token. */
7638 token = cp_lexer_peek_token (parser->lexer);
7640 switch (token->type)
7642 case CPP_EQ:
7643 op = NOP_EXPR;
7644 break;
7646 case CPP_MULT_EQ:
7647 op = MULT_EXPR;
7648 break;
7650 case CPP_DIV_EQ:
7651 op = TRUNC_DIV_EXPR;
7652 break;
7654 case CPP_MOD_EQ:
7655 op = TRUNC_MOD_EXPR;
7656 break;
7658 case CPP_PLUS_EQ:
7659 op = PLUS_EXPR;
7660 break;
7662 case CPP_MINUS_EQ:
7663 op = MINUS_EXPR;
7664 break;
7666 case CPP_RSHIFT_EQ:
7667 op = RSHIFT_EXPR;
7668 break;
7670 case CPP_LSHIFT_EQ:
7671 op = LSHIFT_EXPR;
7672 break;
7674 case CPP_AND_EQ:
7675 op = BIT_AND_EXPR;
7676 break;
7678 case CPP_XOR_EQ:
7679 op = BIT_XOR_EXPR;
7680 break;
7682 case CPP_OR_EQ:
7683 op = BIT_IOR_EXPR;
7684 break;
7686 default:
7687 /* Nothing else is an assignment operator. */
7688 op = ERROR_MARK;
7691 /* If it was an assignment operator, consume it. */
7692 if (op != ERROR_MARK)
7693 cp_lexer_consume_token (parser->lexer);
7695 return op;
7698 /* Parse an expression.
7700 expression:
7701 assignment-expression
7702 expression , assignment-expression
7704 CAST_P is true if this expression is the target of a cast.
7706 Returns a representation of the expression. */
7708 static tree
7709 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7711 tree expression = NULL_TREE;
7712 location_t loc = UNKNOWN_LOCATION;
7714 while (true)
7716 tree assignment_expression;
7718 /* Parse the next assignment-expression. */
7719 assignment_expression
7720 = cp_parser_assignment_expression (parser, cast_p, pidk);
7721 /* If this is the first assignment-expression, we can just
7722 save it away. */
7723 if (!expression)
7724 expression = assignment_expression;
7725 else
7726 expression = build_x_compound_expr (loc, expression,
7727 assignment_expression,
7728 tf_warning_or_error);
7729 /* If the next token is not a comma, then we are done with the
7730 expression. */
7731 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7732 break;
7733 /* Consume the `,'. */
7734 loc = cp_lexer_peek_token (parser->lexer)->location;
7735 cp_lexer_consume_token (parser->lexer);
7736 /* A comma operator cannot appear in a constant-expression. */
7737 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7738 expression = error_mark_node;
7741 return expression;
7744 /* Parse a constant-expression.
7746 constant-expression:
7747 conditional-expression
7749 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7750 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7751 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7752 is false, NON_CONSTANT_P should be NULL. */
7754 static tree
7755 cp_parser_constant_expression (cp_parser* parser,
7756 bool allow_non_constant_p,
7757 bool *non_constant_p)
7759 bool saved_integral_constant_expression_p;
7760 bool saved_allow_non_integral_constant_expression_p;
7761 bool saved_non_integral_constant_expression_p;
7762 tree expression;
7764 /* It might seem that we could simply parse the
7765 conditional-expression, and then check to see if it were
7766 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7767 one that the compiler can figure out is constant, possibly after
7768 doing some simplifications or optimizations. The standard has a
7769 precise definition of constant-expression, and we must honor
7770 that, even though it is somewhat more restrictive.
7772 For example:
7774 int i[(2, 3)];
7776 is not a legal declaration, because `(2, 3)' is not a
7777 constant-expression. The `,' operator is forbidden in a
7778 constant-expression. However, GCC's constant-folding machinery
7779 will fold this operation to an INTEGER_CST for `3'. */
7781 /* Save the old settings. */
7782 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7783 saved_allow_non_integral_constant_expression_p
7784 = parser->allow_non_integral_constant_expression_p;
7785 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7786 /* We are now parsing a constant-expression. */
7787 parser->integral_constant_expression_p = true;
7788 parser->allow_non_integral_constant_expression_p
7789 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7790 parser->non_integral_constant_expression_p = false;
7791 /* Although the grammar says "conditional-expression", we parse an
7792 "assignment-expression", which also permits "throw-expression"
7793 and the use of assignment operators. In the case that
7794 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7795 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7796 actually essential that we look for an assignment-expression.
7797 For example, cp_parser_initializer_clauses uses this function to
7798 determine whether a particular assignment-expression is in fact
7799 constant. */
7800 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7801 /* Restore the old settings. */
7802 parser->integral_constant_expression_p
7803 = saved_integral_constant_expression_p;
7804 parser->allow_non_integral_constant_expression_p
7805 = saved_allow_non_integral_constant_expression_p;
7806 if (cxx_dialect >= cxx0x)
7808 /* Require an rvalue constant expression here; that's what our
7809 callers expect. Reference constant expressions are handled
7810 separately in e.g. cp_parser_template_argument. */
7811 bool is_const = potential_rvalue_constant_expression (expression);
7812 parser->non_integral_constant_expression_p = !is_const;
7813 if (!is_const && !allow_non_constant_p)
7814 require_potential_rvalue_constant_expression (expression);
7816 if (allow_non_constant_p)
7817 *non_constant_p = parser->non_integral_constant_expression_p;
7818 parser->non_integral_constant_expression_p
7819 = saved_non_integral_constant_expression_p;
7821 return expression;
7824 /* Parse __builtin_offsetof.
7826 offsetof-expression:
7827 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7829 offsetof-member-designator:
7830 id-expression
7831 | offsetof-member-designator "." id-expression
7832 | offsetof-member-designator "[" expression "]"
7833 | offsetof-member-designator "->" id-expression */
7835 static tree
7836 cp_parser_builtin_offsetof (cp_parser *parser)
7838 int save_ice_p, save_non_ice_p;
7839 tree type, expr;
7840 cp_id_kind dummy;
7841 cp_token *token;
7843 /* We're about to accept non-integral-constant things, but will
7844 definitely yield an integral constant expression. Save and
7845 restore these values around our local parsing. */
7846 save_ice_p = parser->integral_constant_expression_p;
7847 save_non_ice_p = parser->non_integral_constant_expression_p;
7849 /* Consume the "__builtin_offsetof" token. */
7850 cp_lexer_consume_token (parser->lexer);
7851 /* Consume the opening `('. */
7852 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7853 /* Parse the type-id. */
7854 type = cp_parser_type_id (parser);
7855 /* Look for the `,'. */
7856 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7857 token = cp_lexer_peek_token (parser->lexer);
7859 /* Build the (type *)null that begins the traditional offsetof macro. */
7860 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7861 tf_warning_or_error);
7863 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7864 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7865 true, &dummy, token->location);
7866 while (true)
7868 token = cp_lexer_peek_token (parser->lexer);
7869 switch (token->type)
7871 case CPP_OPEN_SQUARE:
7872 /* offsetof-member-designator "[" expression "]" */
7873 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7874 break;
7876 case CPP_DEREF:
7877 /* offsetof-member-designator "->" identifier */
7878 expr = grok_array_decl (token->location, expr, integer_zero_node);
7879 /* FALLTHRU */
7881 case CPP_DOT:
7882 /* offsetof-member-designator "." identifier */
7883 cp_lexer_consume_token (parser->lexer);
7884 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7885 expr, true, &dummy,
7886 token->location);
7887 break;
7889 case CPP_CLOSE_PAREN:
7890 /* Consume the ")" token. */
7891 cp_lexer_consume_token (parser->lexer);
7892 goto success;
7894 default:
7895 /* Error. We know the following require will fail, but
7896 that gives the proper error message. */
7897 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7898 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7899 expr = error_mark_node;
7900 goto failure;
7904 success:
7905 /* If we're processing a template, we can't finish the semantics yet.
7906 Otherwise we can fold the entire expression now. */
7907 if (processing_template_decl)
7908 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7909 else
7910 expr = finish_offsetof (expr);
7912 failure:
7913 parser->integral_constant_expression_p = save_ice_p;
7914 parser->non_integral_constant_expression_p = save_non_ice_p;
7916 return expr;
7919 /* Parse a trait expression.
7921 Returns a representation of the expression, the underlying type
7922 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7924 static tree
7925 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7927 cp_trait_kind kind;
7928 tree type1, type2 = NULL_TREE;
7929 bool binary = false;
7930 cp_decl_specifier_seq decl_specs;
7932 switch (keyword)
7934 case RID_HAS_NOTHROW_ASSIGN:
7935 kind = CPTK_HAS_NOTHROW_ASSIGN;
7936 break;
7937 case RID_HAS_NOTHROW_CONSTRUCTOR:
7938 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7939 break;
7940 case RID_HAS_NOTHROW_COPY:
7941 kind = CPTK_HAS_NOTHROW_COPY;
7942 break;
7943 case RID_HAS_TRIVIAL_ASSIGN:
7944 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7945 break;
7946 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7947 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7948 break;
7949 case RID_HAS_TRIVIAL_COPY:
7950 kind = CPTK_HAS_TRIVIAL_COPY;
7951 break;
7952 case RID_HAS_TRIVIAL_DESTRUCTOR:
7953 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7954 break;
7955 case RID_HAS_VIRTUAL_DESTRUCTOR:
7956 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7957 break;
7958 case RID_IS_ABSTRACT:
7959 kind = CPTK_IS_ABSTRACT;
7960 break;
7961 case RID_IS_BASE_OF:
7962 kind = CPTK_IS_BASE_OF;
7963 binary = true;
7964 break;
7965 case RID_IS_CLASS:
7966 kind = CPTK_IS_CLASS;
7967 break;
7968 case RID_IS_CONVERTIBLE_TO:
7969 kind = CPTK_IS_CONVERTIBLE_TO;
7970 binary = true;
7971 break;
7972 case RID_IS_EMPTY:
7973 kind = CPTK_IS_EMPTY;
7974 break;
7975 case RID_IS_ENUM:
7976 kind = CPTK_IS_ENUM;
7977 break;
7978 case RID_IS_FINAL:
7979 kind = CPTK_IS_FINAL;
7980 break;
7981 case RID_IS_LITERAL_TYPE:
7982 kind = CPTK_IS_LITERAL_TYPE;
7983 break;
7984 case RID_IS_POD:
7985 kind = CPTK_IS_POD;
7986 break;
7987 case RID_IS_POLYMORPHIC:
7988 kind = CPTK_IS_POLYMORPHIC;
7989 break;
7990 case RID_IS_STD_LAYOUT:
7991 kind = CPTK_IS_STD_LAYOUT;
7992 break;
7993 case RID_IS_TRIVIAL:
7994 kind = CPTK_IS_TRIVIAL;
7995 break;
7996 case RID_IS_UNION:
7997 kind = CPTK_IS_UNION;
7998 break;
7999 case RID_UNDERLYING_TYPE:
8000 kind = CPTK_UNDERLYING_TYPE;
8001 break;
8002 case RID_BASES:
8003 kind = CPTK_BASES;
8004 break;
8005 case RID_DIRECT_BASES:
8006 kind = CPTK_DIRECT_BASES;
8007 break;
8008 default:
8009 gcc_unreachable ();
8012 /* Consume the token. */
8013 cp_lexer_consume_token (parser->lexer);
8015 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8017 type1 = cp_parser_type_id (parser);
8019 if (type1 == error_mark_node)
8020 return error_mark_node;
8022 /* Build a trivial decl-specifier-seq. */
8023 clear_decl_specs (&decl_specs);
8024 decl_specs.type = type1;
8026 /* Call grokdeclarator to figure out what type this is. */
8027 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8028 /*initialized=*/0, /*attrlist=*/NULL);
8030 if (binary)
8032 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8034 type2 = cp_parser_type_id (parser);
8036 if (type2 == error_mark_node)
8037 return error_mark_node;
8039 /* Build a trivial decl-specifier-seq. */
8040 clear_decl_specs (&decl_specs);
8041 decl_specs.type = type2;
8043 /* Call grokdeclarator to figure out what type this is. */
8044 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8045 /*initialized=*/0, /*attrlist=*/NULL);
8048 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8050 /* Complete the trait expression, which may mean either processing
8051 the trait expr now or saving it for template instantiation. */
8052 switch(kind)
8054 case CPTK_UNDERLYING_TYPE:
8055 return finish_underlying_type (type1);
8056 case CPTK_BASES:
8057 return finish_bases (type1, false);
8058 case CPTK_DIRECT_BASES:
8059 return finish_bases (type1, true);
8060 default:
8061 return finish_trait_expr (kind, type1, type2);
8065 /* Lambdas that appear in variable initializer or default argument scope
8066 get that in their mangling, so we need to record it. We might as well
8067 use the count for function and namespace scopes as well. */
8068 static GTY(()) tree lambda_scope;
8069 static GTY(()) int lambda_count;
8070 typedef struct GTY(()) tree_int
8072 tree t;
8073 int i;
8074 } tree_int;
8075 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8077 static void
8078 start_lambda_scope (tree decl)
8080 tree_int ti;
8081 gcc_assert (decl);
8082 /* Once we're inside a function, we ignore other scopes and just push
8083 the function again so that popping works properly. */
8084 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8085 decl = current_function_decl;
8086 ti.t = lambda_scope;
8087 ti.i = lambda_count;
8088 vec_safe_push (lambda_scope_stack, ti);
8089 if (lambda_scope != decl)
8091 /* Don't reset the count if we're still in the same function. */
8092 lambda_scope = decl;
8093 lambda_count = 0;
8097 static void
8098 record_lambda_scope (tree lambda)
8100 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8101 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8104 static void
8105 finish_lambda_scope (void)
8107 tree_int *p = &lambda_scope_stack->last ();
8108 if (lambda_scope != p->t)
8110 lambda_scope = p->t;
8111 lambda_count = p->i;
8113 lambda_scope_stack->pop ();
8116 /* Parse a lambda expression.
8118 lambda-expression:
8119 lambda-introducer lambda-declarator [opt] compound-statement
8121 Returns a representation of the expression. */
8123 static tree
8124 cp_parser_lambda_expression (cp_parser* parser)
8126 tree lambda_expr = build_lambda_expr ();
8127 tree type;
8128 bool ok;
8130 LAMBDA_EXPR_LOCATION (lambda_expr)
8131 = cp_lexer_peek_token (parser->lexer)->location;
8133 if (cp_unevaluated_operand)
8134 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8135 "lambda-expression in unevaluated context");
8137 /* We may be in the middle of deferred access check. Disable
8138 it now. */
8139 push_deferring_access_checks (dk_no_deferred);
8141 cp_parser_lambda_introducer (parser, lambda_expr);
8143 type = begin_lambda_type (lambda_expr);
8144 if (type == error_mark_node)
8145 return error_mark_node;
8147 record_lambda_scope (lambda_expr);
8149 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8150 determine_visibility (TYPE_NAME (type));
8152 /* Now that we've started the type, add the capture fields for any
8153 explicit captures. */
8154 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8157 /* Inside the class, surrounding template-parameter-lists do not apply. */
8158 unsigned int saved_num_template_parameter_lists
8159 = parser->num_template_parameter_lists;
8160 unsigned char in_statement = parser->in_statement;
8161 bool in_switch_statement_p = parser->in_switch_statement_p;
8163 parser->num_template_parameter_lists = 0;
8164 parser->in_statement = 0;
8165 parser->in_switch_statement_p = false;
8167 /* By virtue of defining a local class, a lambda expression has access to
8168 the private variables of enclosing classes. */
8170 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8172 if (ok)
8173 cp_parser_lambda_body (parser, lambda_expr);
8174 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8175 cp_parser_skip_to_end_of_block_or_statement (parser);
8177 /* The capture list was built up in reverse order; fix that now. */
8179 tree newlist = NULL_TREE;
8180 tree elt, next;
8182 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8183 elt; elt = next)
8185 next = TREE_CHAIN (elt);
8186 TREE_CHAIN (elt) = newlist;
8187 newlist = elt;
8189 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8192 if (ok)
8193 maybe_add_lambda_conv_op (type);
8195 type = finish_struct (type, /*attributes=*/NULL_TREE);
8197 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8198 parser->in_statement = in_statement;
8199 parser->in_switch_statement_p = in_switch_statement_p;
8202 pop_deferring_access_checks ();
8204 /* This field is only used during parsing of the lambda. */
8205 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8207 /* This lambda shouldn't have any proxies left at this point. */
8208 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8209 /* And now that we're done, push proxies for an enclosing lambda. */
8210 insert_pending_capture_proxies ();
8212 if (ok)
8213 return build_lambda_object (lambda_expr);
8214 else
8215 return error_mark_node;
8218 /* Parse the beginning of a lambda expression.
8220 lambda-introducer:
8221 [ lambda-capture [opt] ]
8223 LAMBDA_EXPR is the current representation of the lambda expression. */
8225 static void
8226 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8228 /* Need commas after the first capture. */
8229 bool first = true;
8231 /* Eat the leading `['. */
8232 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8234 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8235 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8236 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8237 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8238 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8239 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8241 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8243 cp_lexer_consume_token (parser->lexer);
8244 first = false;
8247 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8249 cp_token* capture_token;
8250 tree capture_id;
8251 tree capture_init_expr;
8252 cp_id_kind idk = CP_ID_KIND_NONE;
8253 bool explicit_init_p = false;
8255 enum capture_kind_type
8257 BY_COPY,
8258 BY_REFERENCE
8260 enum capture_kind_type capture_kind = BY_COPY;
8262 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8264 error ("expected end of capture-list");
8265 return;
8268 if (first)
8269 first = false;
8270 else
8271 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8273 /* Possibly capture `this'. */
8274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8276 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8277 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8278 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8279 "with by-copy capture default");
8280 cp_lexer_consume_token (parser->lexer);
8281 add_capture (lambda_expr,
8282 /*id=*/this_identifier,
8283 /*initializer=*/finish_this_expr(),
8284 /*by_reference_p=*/false,
8285 explicit_init_p);
8286 continue;
8289 /* Remember whether we want to capture as a reference or not. */
8290 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8292 capture_kind = BY_REFERENCE;
8293 cp_lexer_consume_token (parser->lexer);
8296 /* Get the identifier. */
8297 capture_token = cp_lexer_peek_token (parser->lexer);
8298 capture_id = cp_parser_identifier (parser);
8300 if (capture_id == error_mark_node)
8301 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8302 delimiters, but I modified this to stop on unnested ']' as well. It
8303 was already changed to stop on unnested '}', so the
8304 "closing_parenthesis" name is no more misleading with my change. */
8306 cp_parser_skip_to_closing_parenthesis (parser,
8307 /*recovering=*/true,
8308 /*or_comma=*/true,
8309 /*consume_paren=*/true);
8310 break;
8313 /* Find the initializer for this capture. */
8314 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8316 /* An explicit expression exists. */
8317 cp_lexer_consume_token (parser->lexer);
8318 pedwarn (input_location, OPT_Wpedantic,
8319 "ISO C++ does not allow initializers "
8320 "in lambda expression capture lists");
8321 capture_init_expr = cp_parser_assignment_expression (parser,
8322 /*cast_p=*/true,
8323 &idk);
8324 explicit_init_p = true;
8326 else
8328 const char* error_msg;
8330 /* Turn the identifier into an id-expression. */
8331 capture_init_expr
8332 = cp_parser_lookup_name
8333 (parser,
8334 capture_id,
8335 none_type,
8336 /*is_template=*/false,
8337 /*is_namespace=*/false,
8338 /*check_dependency=*/true,
8339 /*ambiguous_decls=*/NULL,
8340 capture_token->location);
8342 if (capture_init_expr == error_mark_node)
8344 unqualified_name_lookup_error (capture_id);
8345 continue;
8347 else if (DECL_P (capture_init_expr)
8348 && (TREE_CODE (capture_init_expr) != VAR_DECL
8349 && TREE_CODE (capture_init_expr) != PARM_DECL))
8351 error_at (capture_token->location,
8352 "capture of non-variable %qD ",
8353 capture_init_expr);
8354 inform (0, "%q+#D declared here", capture_init_expr);
8355 continue;
8357 if (TREE_CODE (capture_init_expr) == VAR_DECL
8358 && decl_storage_duration (capture_init_expr) != dk_auto)
8360 pedwarn (capture_token->location, 0, "capture of variable "
8361 "%qD with non-automatic storage duration",
8362 capture_init_expr);
8363 inform (0, "%q+#D declared here", capture_init_expr);
8364 continue;
8367 capture_init_expr
8368 = finish_id_expression
8369 (capture_id,
8370 capture_init_expr,
8371 parser->scope,
8372 &idk,
8373 /*integral_constant_expression_p=*/false,
8374 /*allow_non_integral_constant_expression_p=*/false,
8375 /*non_integral_constant_expression_p=*/NULL,
8376 /*template_p=*/false,
8377 /*done=*/true,
8378 /*address_p=*/false,
8379 /*template_arg_p=*/false,
8380 &error_msg,
8381 capture_token->location);
8384 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8385 && !explicit_init_p)
8387 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8388 && capture_kind == BY_COPY)
8389 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8390 "of %qD redundant with by-copy capture default",
8391 capture_id);
8392 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8393 && capture_kind == BY_REFERENCE)
8394 pedwarn (capture_token->location, 0, "explicit by-reference "
8395 "capture of %qD redundant with by-reference capture "
8396 "default", capture_id);
8399 add_capture (lambda_expr,
8400 capture_id,
8401 capture_init_expr,
8402 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8403 explicit_init_p);
8406 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8409 /* Parse the (optional) middle of a lambda expression.
8411 lambda-declarator:
8412 ( parameter-declaration-clause [opt] )
8413 attribute-specifier [opt]
8414 mutable [opt]
8415 exception-specification [opt]
8416 lambda-return-type-clause [opt]
8418 LAMBDA_EXPR is the current representation of the lambda expression. */
8420 static bool
8421 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8423 /* 5.1.1.4 of the standard says:
8424 If a lambda-expression does not include a lambda-declarator, it is as if
8425 the lambda-declarator were ().
8426 This means an empty parameter list, no attributes, and no exception
8427 specification. */
8428 tree param_list = void_list_node;
8429 tree attributes = NULL_TREE;
8430 tree exception_spec = NULL_TREE;
8431 tree t;
8433 /* The lambda-declarator is optional, but must begin with an opening
8434 parenthesis if present. */
8435 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8437 cp_lexer_consume_token (parser->lexer);
8439 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8441 /* Parse parameters. */
8442 param_list = cp_parser_parameter_declaration_clause (parser);
8444 /* Default arguments shall not be specified in the
8445 parameter-declaration-clause of a lambda-declarator. */
8446 for (t = param_list; t; t = TREE_CHAIN (t))
8447 if (TREE_PURPOSE (t))
8448 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8449 "default argument specified for lambda parameter");
8451 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8453 attributes = cp_parser_attributes_opt (parser);
8455 /* Parse optional `mutable' keyword. */
8456 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8458 cp_lexer_consume_token (parser->lexer);
8459 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8462 /* Parse optional exception specification. */
8463 exception_spec = cp_parser_exception_specification_opt (parser);
8465 /* Parse optional trailing return type. */
8466 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8468 cp_lexer_consume_token (parser->lexer);
8469 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8472 /* The function parameters must be in scope all the way until after the
8473 trailing-return-type in case of decltype. */
8474 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8475 pop_binding (DECL_NAME (t), t);
8477 leave_scope ();
8480 /* Create the function call operator.
8482 Messing with declarators like this is no uglier than building up the
8483 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8484 other code. */
8486 cp_decl_specifier_seq return_type_specs;
8487 cp_declarator* declarator;
8488 tree fco;
8489 int quals;
8490 void *p;
8492 clear_decl_specs (&return_type_specs);
8493 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8494 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8495 else
8496 /* Maybe we will deduce the return type later. */
8497 return_type_specs.type = make_auto ();
8499 p = obstack_alloc (&declarator_obstack, 0);
8501 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8502 sfk_none);
8504 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8505 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8506 declarator = make_call_declarator (declarator, param_list, quals,
8507 VIRT_SPEC_UNSPECIFIED,
8508 exception_spec,
8509 /*late_return_type=*/NULL_TREE);
8510 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8512 fco = grokmethod (&return_type_specs,
8513 declarator,
8514 attributes);
8515 if (fco != error_mark_node)
8517 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8518 DECL_ARTIFICIAL (fco) = 1;
8519 /* Give the object parameter a different name. */
8520 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8523 finish_member_declaration (fco);
8525 obstack_free (&declarator_obstack, p);
8527 return (fco != error_mark_node);
8531 /* Parse the body of a lambda expression, which is simply
8533 compound-statement
8535 but which requires special handling.
8536 LAMBDA_EXPR is the current representation of the lambda expression. */
8538 static void
8539 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8541 bool nested = (current_function_decl != NULL_TREE);
8542 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8543 if (nested)
8544 push_function_context ();
8545 else
8546 /* Still increment function_depth so that we don't GC in the
8547 middle of an expression. */
8548 ++function_depth;
8549 /* Clear this in case we're in the middle of a default argument. */
8550 parser->local_variables_forbidden_p = false;
8552 /* Finish the function call operator
8553 - class_specifier
8554 + late_parsing_for_member
8555 + function_definition_after_declarator
8556 + ctor_initializer_opt_and_function_body */
8558 tree fco = lambda_function (lambda_expr);
8559 tree body;
8560 bool done = false;
8561 tree compound_stmt;
8562 tree cap;
8564 /* Let the front end know that we are going to be defining this
8565 function. */
8566 start_preparsed_function (fco,
8567 NULL_TREE,
8568 SF_PRE_PARSED | SF_INCLASS_INLINE);
8570 start_lambda_scope (fco);
8571 body = begin_function_body ();
8573 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8574 goto out;
8576 /* Push the proxies for any explicit captures. */
8577 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8578 cap = TREE_CHAIN (cap))
8579 build_capture_proxy (TREE_PURPOSE (cap));
8581 compound_stmt = begin_compound_stmt (0);
8583 /* 5.1.1.4 of the standard says:
8584 If a lambda-expression does not include a trailing-return-type, it
8585 is as if the trailing-return-type denotes the following type:
8586 * if the compound-statement is of the form
8587 { return attribute-specifier [opt] expression ; }
8588 the type of the returned expression after lvalue-to-rvalue
8589 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8590 (_conv.array_ 4.2), and function-to-pointer conversion
8591 (_conv.func_ 4.3);
8592 * otherwise, void. */
8594 /* In a lambda that has neither a lambda-return-type-clause
8595 nor a deducible form, errors should be reported for return statements
8596 in the body. Since we used void as the placeholder return type, parsing
8597 the body as usual will give such desired behavior. */
8598 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8599 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8600 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8602 tree expr = NULL_TREE;
8603 cp_id_kind idk = CP_ID_KIND_NONE;
8605 /* Parse tentatively in case there's more after the initial return
8606 statement. */
8607 cp_parser_parse_tentatively (parser);
8609 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8611 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8613 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8614 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8616 if (cp_parser_parse_definitely (parser))
8618 if (!processing_template_decl)
8619 apply_deduced_return_type (fco, lambda_return_type (expr));
8621 /* Will get error here if type not deduced yet. */
8622 finish_return_stmt (expr);
8624 done = true;
8628 if (!done)
8630 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8631 cp_parser_label_declaration (parser);
8632 cp_parser_statement_seq_opt (parser, NULL_TREE);
8633 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8636 finish_compound_stmt (compound_stmt);
8638 out:
8639 finish_function_body (body);
8640 finish_lambda_scope ();
8642 /* Finish the function and generate code for it if necessary. */
8643 expand_or_defer_fn (finish_function (/*inline*/2));
8646 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8647 if (nested)
8648 pop_function_context();
8649 else
8650 --function_depth;
8653 /* Statements [gram.stmt.stmt] */
8655 /* Parse a statement.
8657 statement:
8658 labeled-statement
8659 expression-statement
8660 compound-statement
8661 selection-statement
8662 iteration-statement
8663 jump-statement
8664 declaration-statement
8665 try-block
8667 C++11:
8669 statement:
8670 labeled-statement
8671 attribute-specifier-seq (opt) expression-statement
8672 attribute-specifier-seq (opt) compound-statement
8673 attribute-specifier-seq (opt) selection-statement
8674 attribute-specifier-seq (opt) iteration-statement
8675 attribute-specifier-seq (opt) jump-statement
8676 declaration-statement
8677 attribute-specifier-seq (opt) try-block
8679 TM Extension:
8681 statement:
8682 atomic-statement
8684 IN_COMPOUND is true when the statement is nested inside a
8685 cp_parser_compound_statement; this matters for certain pragmas.
8687 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8688 is a (possibly labeled) if statement which is not enclosed in braces
8689 and has an else clause. This is used to implement -Wparentheses. */
8691 static void
8692 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8693 bool in_compound, bool *if_p)
8695 tree statement, std_attrs = NULL_TREE;
8696 cp_token *token;
8697 location_t statement_location, attrs_location;
8699 restart:
8700 if (if_p != NULL)
8701 *if_p = false;
8702 /* There is no statement yet. */
8703 statement = NULL_TREE;
8705 cp_lexer_save_tokens (parser->lexer);
8706 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
8707 if (c_dialect_objc ())
8708 /* In obj-c++, seing '[[' might be the either the beginning of
8709 c++11 attributes, or a nested objc-message-expression. So
8710 let's parse the c++11 attributes tentatively. */
8711 cp_parser_parse_tentatively (parser);
8712 std_attrs = cp_parser_std_attribute_spec_seq (parser);
8713 if (c_dialect_objc ())
8715 if (!cp_parser_parse_definitely (parser))
8716 std_attrs = NULL_TREE;
8719 /* Peek at the next token. */
8720 token = cp_lexer_peek_token (parser->lexer);
8721 /* Remember the location of the first token in the statement. */
8722 statement_location = token->location;
8723 /* If this is a keyword, then that will often determine what kind of
8724 statement we have. */
8725 if (token->type == CPP_KEYWORD)
8727 enum rid keyword = token->keyword;
8729 switch (keyword)
8731 case RID_CASE:
8732 case RID_DEFAULT:
8733 /* Looks like a labeled-statement with a case label.
8734 Parse the label, and then use tail recursion to parse
8735 the statement. */
8736 cp_parser_label_for_labeled_statement (parser, std_attrs);
8737 goto restart;
8739 case RID_IF:
8740 case RID_SWITCH:
8741 statement = cp_parser_selection_statement (parser, if_p);
8742 break;
8744 case RID_WHILE:
8745 case RID_DO:
8746 case RID_FOR:
8747 statement = cp_parser_iteration_statement (parser);
8748 break;
8750 case RID_BREAK:
8751 case RID_CONTINUE:
8752 case RID_RETURN:
8753 case RID_GOTO:
8754 statement = cp_parser_jump_statement (parser);
8755 break;
8757 /* Objective-C++ exception-handling constructs. */
8758 case RID_AT_TRY:
8759 case RID_AT_CATCH:
8760 case RID_AT_FINALLY:
8761 case RID_AT_SYNCHRONIZED:
8762 case RID_AT_THROW:
8763 statement = cp_parser_objc_statement (parser);
8764 break;
8766 case RID_TRY:
8767 statement = cp_parser_try_block (parser);
8768 break;
8770 case RID_NAMESPACE:
8771 /* This must be a namespace alias definition. */
8772 cp_parser_declaration_statement (parser);
8773 return;
8775 case RID_TRANSACTION_ATOMIC:
8776 case RID_TRANSACTION_RELAXED:
8777 statement = cp_parser_transaction (parser, keyword);
8778 break;
8779 case RID_TRANSACTION_CANCEL:
8780 statement = cp_parser_transaction_cancel (parser);
8781 break;
8783 default:
8784 /* It might be a keyword like `int' that can start a
8785 declaration-statement. */
8786 break;
8789 else if (token->type == CPP_NAME)
8791 /* If the next token is a `:', then we are looking at a
8792 labeled-statement. */
8793 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8794 if (token->type == CPP_COLON)
8796 /* Looks like a labeled-statement with an ordinary label.
8797 Parse the label, and then use tail recursion to parse
8798 the statement. */
8800 cp_parser_label_for_labeled_statement (parser, std_attrs);
8801 goto restart;
8804 /* Anything that starts with a `{' must be a compound-statement. */
8805 else if (token->type == CPP_OPEN_BRACE)
8806 statement = cp_parser_compound_statement (parser, NULL, false, false);
8807 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8808 a statement all its own. */
8809 else if (token->type == CPP_PRAGMA)
8811 /* Only certain OpenMP pragmas are attached to statements, and thus
8812 are considered statements themselves. All others are not. In
8813 the context of a compound, accept the pragma as a "statement" and
8814 return so that we can check for a close brace. Otherwise we
8815 require a real statement and must go back and read one. */
8816 if (in_compound)
8817 cp_parser_pragma (parser, pragma_compound);
8818 else if (!cp_parser_pragma (parser, pragma_stmt))
8819 goto restart;
8820 return;
8822 else if (token->type == CPP_EOF)
8824 cp_parser_error (parser, "expected statement");
8825 return;
8828 /* Everything else must be a declaration-statement or an
8829 expression-statement. Try for the declaration-statement
8830 first, unless we are looking at a `;', in which case we know that
8831 we have an expression-statement. */
8832 if (!statement)
8834 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8836 if (std_attrs != NULL_TREE)
8838 /* Attributes should be parsed as part of the the
8839 declaration, so let's un-parse them. */
8840 cp_lexer_rollback_tokens (parser->lexer);
8841 std_attrs = NULL_TREE;
8844 cp_parser_parse_tentatively (parser);
8845 /* Try to parse the declaration-statement. */
8846 cp_parser_declaration_statement (parser);
8847 /* If that worked, we're done. */
8848 if (cp_parser_parse_definitely (parser))
8849 return;
8851 /* Look for an expression-statement instead. */
8852 statement = cp_parser_expression_statement (parser, in_statement_expr);
8855 /* Set the line number for the statement. */
8856 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8857 SET_EXPR_LOCATION (statement, statement_location);
8859 /* Note that for now, we don't do anything with c++11 statements
8860 parsed at this level. */
8861 if (std_attrs != NULL_TREE)
8862 warning_at (attrs_location,
8863 OPT_Wattributes,
8864 "attributes at the beginning of statement are ignored");
8867 /* Parse the label for a labeled-statement, i.e.
8869 identifier :
8870 case constant-expression :
8871 default :
8873 GNU Extension:
8874 case constant-expression ... constant-expression : statement
8876 When a label is parsed without errors, the label is added to the
8877 parse tree by the finish_* functions, so this function doesn't
8878 have to return the label. */
8880 static void
8881 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
8883 cp_token *token;
8884 tree label = NULL_TREE;
8885 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8887 /* The next token should be an identifier. */
8888 token = cp_lexer_peek_token (parser->lexer);
8889 if (token->type != CPP_NAME
8890 && token->type != CPP_KEYWORD)
8892 cp_parser_error (parser, "expected labeled-statement");
8893 return;
8896 parser->colon_corrects_to_scope_p = false;
8897 switch (token->keyword)
8899 case RID_CASE:
8901 tree expr, expr_hi;
8902 cp_token *ellipsis;
8904 /* Consume the `case' token. */
8905 cp_lexer_consume_token (parser->lexer);
8906 /* Parse the constant-expression. */
8907 expr = cp_parser_constant_expression (parser,
8908 /*allow_non_constant_p=*/false,
8909 NULL);
8911 ellipsis = cp_lexer_peek_token (parser->lexer);
8912 if (ellipsis->type == CPP_ELLIPSIS)
8914 /* Consume the `...' token. */
8915 cp_lexer_consume_token (parser->lexer);
8916 expr_hi =
8917 cp_parser_constant_expression (parser,
8918 /*allow_non_constant_p=*/false,
8919 NULL);
8920 /* We don't need to emit warnings here, as the common code
8921 will do this for us. */
8923 else
8924 expr_hi = NULL_TREE;
8926 if (parser->in_switch_statement_p)
8927 finish_case_label (token->location, expr, expr_hi);
8928 else
8929 error_at (token->location,
8930 "case label %qE not within a switch statement",
8931 expr);
8933 break;
8935 case RID_DEFAULT:
8936 /* Consume the `default' token. */
8937 cp_lexer_consume_token (parser->lexer);
8939 if (parser->in_switch_statement_p)
8940 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8941 else
8942 error_at (token->location, "case label not within a switch statement");
8943 break;
8945 default:
8946 /* Anything else must be an ordinary label. */
8947 label = finish_label_stmt (cp_parser_identifier (parser));
8948 break;
8951 /* Require the `:' token. */
8952 cp_parser_require (parser, CPP_COLON, RT_COLON);
8954 /* An ordinary label may optionally be followed by attributes.
8955 However, this is only permitted if the attributes are then
8956 followed by a semicolon. This is because, for backward
8957 compatibility, when parsing
8958 lab: __attribute__ ((unused)) int i;
8959 we want the attribute to attach to "i", not "lab". */
8960 if (label != NULL_TREE
8961 && cp_next_tokens_can_be_gnu_attribute_p (parser))
8963 tree attrs;
8964 cp_parser_parse_tentatively (parser);
8965 attrs = cp_parser_gnu_attributes_opt (parser);
8966 if (attrs == NULL_TREE
8967 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8968 cp_parser_abort_tentative_parse (parser);
8969 else if (!cp_parser_parse_definitely (parser))
8971 else
8972 attributes = chainon (attributes, attrs);
8975 if (attributes != NULL_TREE)
8976 cplus_decl_attributes (&label, attributes, 0);
8978 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8981 /* Parse an expression-statement.
8983 expression-statement:
8984 expression [opt] ;
8986 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8987 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8988 indicates whether this expression-statement is part of an
8989 expression statement. */
8991 static tree
8992 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8994 tree statement = NULL_TREE;
8995 cp_token *token = cp_lexer_peek_token (parser->lexer);
8997 /* If the next token is a ';', then there is no expression
8998 statement. */
8999 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9000 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9002 /* Give a helpful message for "A<T>::type t;" and the like. */
9003 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9004 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9006 if (TREE_CODE (statement) == SCOPE_REF)
9007 error_at (token->location, "need %<typename%> before %qE because "
9008 "%qT is a dependent scope",
9009 statement, TREE_OPERAND (statement, 0));
9010 else if (is_overloaded_fn (statement)
9011 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9013 /* A::A a; */
9014 tree fn = get_first_fn (statement);
9015 error_at (token->location,
9016 "%<%T::%D%> names the constructor, not the type",
9017 DECL_CONTEXT (fn), DECL_NAME (fn));
9021 /* Consume the final `;'. */
9022 cp_parser_consume_semicolon_at_end_of_statement (parser);
9024 if (in_statement_expr
9025 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9026 /* This is the final expression statement of a statement
9027 expression. */
9028 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9029 else if (statement)
9030 statement = finish_expr_stmt (statement);
9031 else
9032 finish_stmt ();
9034 return statement;
9037 /* Parse a compound-statement.
9039 compound-statement:
9040 { statement-seq [opt] }
9042 GNU extension:
9044 compound-statement:
9045 { label-declaration-seq [opt] statement-seq [opt] }
9047 label-declaration-seq:
9048 label-declaration
9049 label-declaration-seq label-declaration
9051 Returns a tree representing the statement. */
9053 static tree
9054 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9055 bool in_try, bool function_body)
9057 tree compound_stmt;
9059 /* Consume the `{'. */
9060 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9061 return error_mark_node;
9062 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9063 && !function_body)
9064 pedwarn (input_location, OPT_Wpedantic,
9065 "compound-statement in constexpr function");
9066 /* Begin the compound-statement. */
9067 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9068 /* If the next keyword is `__label__' we have a label declaration. */
9069 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9070 cp_parser_label_declaration (parser);
9071 /* Parse an (optional) statement-seq. */
9072 cp_parser_statement_seq_opt (parser, in_statement_expr);
9073 /* Finish the compound-statement. */
9074 finish_compound_stmt (compound_stmt);
9075 /* Consume the `}'. */
9076 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9078 return compound_stmt;
9081 /* Parse an (optional) statement-seq.
9083 statement-seq:
9084 statement
9085 statement-seq [opt] statement */
9087 static void
9088 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9090 /* Scan statements until there aren't any more. */
9091 while (true)
9093 cp_token *token = cp_lexer_peek_token (parser->lexer);
9095 /* If we are looking at a `}', then we have run out of
9096 statements; the same is true if we have reached the end
9097 of file, or have stumbled upon a stray '@end'. */
9098 if (token->type == CPP_CLOSE_BRACE
9099 || token->type == CPP_EOF
9100 || token->type == CPP_PRAGMA_EOL
9101 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9102 break;
9104 /* If we are in a compound statement and find 'else' then
9105 something went wrong. */
9106 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9108 if (parser->in_statement & IN_IF_STMT)
9109 break;
9110 else
9112 token = cp_lexer_consume_token (parser->lexer);
9113 error_at (token->location, "%<else%> without a previous %<if%>");
9117 /* Parse the statement. */
9118 cp_parser_statement (parser, in_statement_expr, true, NULL);
9122 /* Parse a selection-statement.
9124 selection-statement:
9125 if ( condition ) statement
9126 if ( condition ) statement else statement
9127 switch ( condition ) statement
9129 Returns the new IF_STMT or SWITCH_STMT.
9131 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9132 is a (possibly labeled) if statement which is not enclosed in
9133 braces and has an else clause. This is used to implement
9134 -Wparentheses. */
9136 static tree
9137 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9139 cp_token *token;
9140 enum rid keyword;
9142 if (if_p != NULL)
9143 *if_p = false;
9145 /* Peek at the next token. */
9146 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9148 /* See what kind of keyword it is. */
9149 keyword = token->keyword;
9150 switch (keyword)
9152 case RID_IF:
9153 case RID_SWITCH:
9155 tree statement;
9156 tree condition;
9158 /* Look for the `('. */
9159 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9161 cp_parser_skip_to_end_of_statement (parser);
9162 return error_mark_node;
9165 /* Begin the selection-statement. */
9166 if (keyword == RID_IF)
9167 statement = begin_if_stmt ();
9168 else
9169 statement = begin_switch_stmt ();
9171 /* Parse the condition. */
9172 condition = cp_parser_condition (parser);
9173 /* Look for the `)'. */
9174 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9175 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9176 /*consume_paren=*/true);
9178 if (keyword == RID_IF)
9180 bool nested_if;
9181 unsigned char in_statement;
9183 /* Add the condition. */
9184 finish_if_stmt_cond (condition, statement);
9186 /* Parse the then-clause. */
9187 in_statement = parser->in_statement;
9188 parser->in_statement |= IN_IF_STMT;
9189 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9191 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9192 add_stmt (build_empty_stmt (loc));
9193 cp_lexer_consume_token (parser->lexer);
9194 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9195 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9196 "empty body in an %<if%> statement");
9197 nested_if = false;
9199 else
9200 cp_parser_implicitly_scoped_statement (parser, &nested_if);
9201 parser->in_statement = in_statement;
9203 finish_then_clause (statement);
9205 /* If the next token is `else', parse the else-clause. */
9206 if (cp_lexer_next_token_is_keyword (parser->lexer,
9207 RID_ELSE))
9209 /* Consume the `else' keyword. */
9210 cp_lexer_consume_token (parser->lexer);
9211 begin_else_clause (statement);
9212 /* Parse the else-clause. */
9213 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9215 location_t loc;
9216 loc = cp_lexer_peek_token (parser->lexer)->location;
9217 warning_at (loc,
9218 OPT_Wempty_body, "suggest braces around "
9219 "empty body in an %<else%> statement");
9220 add_stmt (build_empty_stmt (loc));
9221 cp_lexer_consume_token (parser->lexer);
9223 else
9224 cp_parser_implicitly_scoped_statement (parser, NULL);
9226 finish_else_clause (statement);
9228 /* If we are currently parsing a then-clause, then
9229 IF_P will not be NULL. We set it to true to
9230 indicate that this if statement has an else clause.
9231 This may trigger the Wparentheses warning below
9232 when we get back up to the parent if statement. */
9233 if (if_p != NULL)
9234 *if_p = true;
9236 else
9238 /* This if statement does not have an else clause. If
9239 NESTED_IF is true, then the then-clause is an if
9240 statement which does have an else clause. We warn
9241 about the potential ambiguity. */
9242 if (nested_if)
9243 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9244 "suggest explicit braces to avoid ambiguous"
9245 " %<else%>");
9248 /* Now we're all done with the if-statement. */
9249 finish_if_stmt (statement);
9251 else
9253 bool in_switch_statement_p;
9254 unsigned char in_statement;
9256 /* Add the condition. */
9257 finish_switch_cond (condition, statement);
9259 /* Parse the body of the switch-statement. */
9260 in_switch_statement_p = parser->in_switch_statement_p;
9261 in_statement = parser->in_statement;
9262 parser->in_switch_statement_p = true;
9263 parser->in_statement |= IN_SWITCH_STMT;
9264 cp_parser_implicitly_scoped_statement (parser, NULL);
9265 parser->in_switch_statement_p = in_switch_statement_p;
9266 parser->in_statement = in_statement;
9268 /* Now we're all done with the switch-statement. */
9269 finish_switch_stmt (statement);
9272 return statement;
9274 break;
9276 default:
9277 cp_parser_error (parser, "expected selection-statement");
9278 return error_mark_node;
9282 /* Parse a condition.
9284 condition:
9285 expression
9286 type-specifier-seq declarator = initializer-clause
9287 type-specifier-seq declarator braced-init-list
9289 GNU Extension:
9291 condition:
9292 type-specifier-seq declarator asm-specification [opt]
9293 attributes [opt] = assignment-expression
9295 Returns the expression that should be tested. */
9297 static tree
9298 cp_parser_condition (cp_parser* parser)
9300 cp_decl_specifier_seq type_specifiers;
9301 const char *saved_message;
9302 int declares_class_or_enum;
9304 /* Try the declaration first. */
9305 cp_parser_parse_tentatively (parser);
9306 /* New types are not allowed in the type-specifier-seq for a
9307 condition. */
9308 saved_message = parser->type_definition_forbidden_message;
9309 parser->type_definition_forbidden_message
9310 = G_("types may not be defined in conditions");
9311 /* Parse the type-specifier-seq. */
9312 cp_parser_decl_specifier_seq (parser,
9313 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9314 &type_specifiers,
9315 &declares_class_or_enum);
9316 /* Restore the saved message. */
9317 parser->type_definition_forbidden_message = saved_message;
9318 /* If all is well, we might be looking at a declaration. */
9319 if (!cp_parser_error_occurred (parser))
9321 tree decl;
9322 tree asm_specification;
9323 tree attributes;
9324 cp_declarator *declarator;
9325 tree initializer = NULL_TREE;
9327 /* Parse the declarator. */
9328 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9329 /*ctor_dtor_or_conv_p=*/NULL,
9330 /*parenthesized_p=*/NULL,
9331 /*member_p=*/false);
9332 /* Parse the attributes. */
9333 attributes = cp_parser_attributes_opt (parser);
9334 /* Parse the asm-specification. */
9335 asm_specification = cp_parser_asm_specification_opt (parser);
9336 /* If the next token is not an `=' or '{', then we might still be
9337 looking at an expression. For example:
9339 if (A(a).x)
9341 looks like a decl-specifier-seq and a declarator -- but then
9342 there is no `=', so this is an expression. */
9343 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9344 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9345 cp_parser_simulate_error (parser);
9347 /* If we did see an `=' or '{', then we are looking at a declaration
9348 for sure. */
9349 if (cp_parser_parse_definitely (parser))
9351 tree pushed_scope;
9352 bool non_constant_p;
9353 bool flags = LOOKUP_ONLYCONVERTING;
9355 /* Create the declaration. */
9356 decl = start_decl (declarator, &type_specifiers,
9357 /*initialized_p=*/true,
9358 attributes, /*prefix_attributes=*/NULL_TREE,
9359 &pushed_scope);
9361 /* Parse the initializer. */
9362 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9364 initializer = cp_parser_braced_list (parser, &non_constant_p);
9365 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9366 flags = 0;
9368 else
9370 /* Consume the `='. */
9371 cp_parser_require (parser, CPP_EQ, RT_EQ);
9372 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9374 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9375 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9377 /* Process the initializer. */
9378 cp_finish_decl (decl,
9379 initializer, !non_constant_p,
9380 asm_specification,
9381 flags);
9383 if (pushed_scope)
9384 pop_scope (pushed_scope);
9386 return convert_from_reference (decl);
9389 /* If we didn't even get past the declarator successfully, we are
9390 definitely not looking at a declaration. */
9391 else
9392 cp_parser_abort_tentative_parse (parser);
9394 /* Otherwise, we are looking at an expression. */
9395 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9398 /* Parses a for-statement or range-for-statement until the closing ')',
9399 not included. */
9401 static tree
9402 cp_parser_for (cp_parser *parser)
9404 tree init, scope, decl;
9405 bool is_range_for;
9407 /* Begin the for-statement. */
9408 scope = begin_for_scope (&init);
9410 /* Parse the initialization. */
9411 is_range_for = cp_parser_for_init_statement (parser, &decl);
9413 if (is_range_for)
9414 return cp_parser_range_for (parser, scope, init, decl);
9415 else
9416 return cp_parser_c_for (parser, scope, init);
9419 static tree
9420 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9422 /* Normal for loop */
9423 tree condition = NULL_TREE;
9424 tree expression = NULL_TREE;
9425 tree stmt;
9427 stmt = begin_for_stmt (scope, init);
9428 /* The for-init-statement has already been parsed in
9429 cp_parser_for_init_statement, so no work is needed here. */
9430 finish_for_init_stmt (stmt);
9432 /* If there's a condition, process it. */
9433 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9434 condition = cp_parser_condition (parser);
9435 finish_for_cond (condition, stmt);
9436 /* Look for the `;'. */
9437 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9439 /* If there's an expression, process it. */
9440 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9441 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9442 finish_for_expr (expression, stmt);
9444 return stmt;
9447 /* Tries to parse a range-based for-statement:
9449 range-based-for:
9450 decl-specifier-seq declarator : expression
9452 The decl-specifier-seq declarator and the `:' are already parsed by
9453 cp_parser_for_init_statement. If processing_template_decl it returns a
9454 newly created RANGE_FOR_STMT; if not, it is converted to a
9455 regular FOR_STMT. */
9457 static tree
9458 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9460 tree stmt, range_expr;
9462 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9464 bool expr_non_constant_p;
9465 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9467 else
9468 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9470 /* If in template, STMT is converted to a normal for-statement
9471 at instantiation. If not, it is done just ahead. */
9472 if (processing_template_decl)
9474 if (check_for_bare_parameter_packs (range_expr))
9475 range_expr = error_mark_node;
9476 stmt = begin_range_for_stmt (scope, init);
9477 finish_range_for_decl (stmt, range_decl, range_expr);
9478 if (!type_dependent_expression_p (range_expr)
9479 /* do_auto_deduction doesn't mess with template init-lists. */
9480 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9481 do_range_for_auto_deduction (range_decl, range_expr);
9483 else
9485 stmt = begin_for_stmt (scope, init);
9486 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9488 return stmt;
9491 /* Subroutine of cp_convert_range_for: given the initializer expression,
9492 builds up the range temporary. */
9494 static tree
9495 build_range_temp (tree range_expr)
9497 tree range_type, range_temp;
9499 /* Find out the type deduced by the declaration
9500 `auto &&__range = range_expr'. */
9501 range_type = cp_build_reference_type (make_auto (), true);
9502 range_type = do_auto_deduction (range_type, range_expr,
9503 type_uses_auto (range_type));
9505 /* Create the __range variable. */
9506 range_temp = build_decl (input_location, VAR_DECL,
9507 get_identifier ("__for_range"), range_type);
9508 TREE_USED (range_temp) = 1;
9509 DECL_ARTIFICIAL (range_temp) = 1;
9511 return range_temp;
9514 /* Used by cp_parser_range_for in template context: we aren't going to
9515 do a full conversion yet, but we still need to resolve auto in the
9516 type of the for-range-declaration if present. This is basically
9517 a shortcut version of cp_convert_range_for. */
9519 static void
9520 do_range_for_auto_deduction (tree decl, tree range_expr)
9522 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9523 if (auto_node)
9525 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9526 range_temp = convert_from_reference (build_range_temp (range_expr));
9527 iter_type = (cp_parser_perform_range_for_lookup
9528 (range_temp, &begin_dummy, &end_dummy));
9529 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9530 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
9531 tf_warning_or_error);
9532 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9533 iter_decl, auto_node);
9537 /* Converts a range-based for-statement into a normal
9538 for-statement, as per the definition.
9540 for (RANGE_DECL : RANGE_EXPR)
9541 BLOCK
9543 should be equivalent to:
9546 auto &&__range = RANGE_EXPR;
9547 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9548 __begin != __end;
9549 ++__begin)
9551 RANGE_DECL = *__begin;
9552 BLOCK
9556 If RANGE_EXPR is an array:
9557 BEGIN_EXPR = __range
9558 END_EXPR = __range + ARRAY_SIZE(__range)
9559 Else if RANGE_EXPR has a member 'begin' or 'end':
9560 BEGIN_EXPR = __range.begin()
9561 END_EXPR = __range.end()
9562 Else:
9563 BEGIN_EXPR = begin(__range)
9564 END_EXPR = end(__range);
9566 If __range has a member 'begin' but not 'end', or vice versa, we must
9567 still use the second alternative (it will surely fail, however).
9568 When calling begin()/end() in the third alternative we must use
9569 argument dependent lookup, but always considering 'std' as an associated
9570 namespace. */
9572 tree
9573 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9575 tree begin, end;
9576 tree iter_type, begin_expr, end_expr;
9577 tree condition, expression;
9579 if (range_decl == error_mark_node || range_expr == error_mark_node)
9580 /* If an error happened previously do nothing or else a lot of
9581 unhelpful errors would be issued. */
9582 begin_expr = end_expr = iter_type = error_mark_node;
9583 else
9585 tree range_temp = build_range_temp (range_expr);
9586 pushdecl (range_temp);
9587 cp_finish_decl (range_temp, range_expr,
9588 /*is_constant_init*/false, NULL_TREE,
9589 LOOKUP_ONLYCONVERTING);
9591 range_temp = convert_from_reference (range_temp);
9592 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9593 &begin_expr, &end_expr);
9596 /* The new for initialization statement. */
9597 begin = build_decl (input_location, VAR_DECL,
9598 get_identifier ("__for_begin"), iter_type);
9599 TREE_USED (begin) = 1;
9600 DECL_ARTIFICIAL (begin) = 1;
9601 pushdecl (begin);
9602 cp_finish_decl (begin, begin_expr,
9603 /*is_constant_init*/false, NULL_TREE,
9604 LOOKUP_ONLYCONVERTING);
9606 end = build_decl (input_location, VAR_DECL,
9607 get_identifier ("__for_end"), iter_type);
9608 TREE_USED (end) = 1;
9609 DECL_ARTIFICIAL (end) = 1;
9610 pushdecl (end);
9611 cp_finish_decl (end, end_expr,
9612 /*is_constant_init*/false, NULL_TREE,
9613 LOOKUP_ONLYCONVERTING);
9615 finish_for_init_stmt (statement);
9617 /* The new for condition. */
9618 condition = build_x_binary_op (input_location, NE_EXPR,
9619 begin, ERROR_MARK,
9620 end, ERROR_MARK,
9621 NULL, tf_warning_or_error);
9622 finish_for_cond (condition, statement);
9624 /* The new increment expression. */
9625 expression = finish_unary_op_expr (input_location,
9626 PREINCREMENT_EXPR, begin);
9627 finish_for_expr (expression, statement);
9629 /* The declaration is initialized with *__begin inside the loop body. */
9630 cp_finish_decl (range_decl,
9631 build_x_indirect_ref (input_location, begin, RO_NULL,
9632 tf_warning_or_error),
9633 /*is_constant_init*/false, NULL_TREE,
9634 LOOKUP_ONLYCONVERTING);
9636 return statement;
9639 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9640 We need to solve both at the same time because the method used
9641 depends on the existence of members begin or end.
9642 Returns the type deduced for the iterator expression. */
9644 static tree
9645 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9647 if (error_operand_p (range))
9649 *begin = *end = error_mark_node;
9650 return error_mark_node;
9653 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9655 error ("range-based %<for%> expression of type %qT "
9656 "has incomplete type", TREE_TYPE (range));
9657 *begin = *end = error_mark_node;
9658 return error_mark_node;
9660 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9662 /* If RANGE is an array, we will use pointer arithmetic. */
9663 *begin = range;
9664 *end = build_binary_op (input_location, PLUS_EXPR,
9665 range,
9666 array_type_nelts_top (TREE_TYPE (range)),
9668 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9670 else
9672 /* If it is not an array, we must do a bit of magic. */
9673 tree id_begin, id_end;
9674 tree member_begin, member_end;
9676 *begin = *end = error_mark_node;
9678 id_begin = get_identifier ("begin");
9679 id_end = get_identifier ("end");
9680 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9681 /*protect=*/2, /*want_type=*/false,
9682 tf_warning_or_error);
9683 member_end = lookup_member (TREE_TYPE (range), id_end,
9684 /*protect=*/2, /*want_type=*/false,
9685 tf_warning_or_error);
9687 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9689 /* Use the member functions. */
9690 if (member_begin != NULL_TREE)
9691 *begin = cp_parser_range_for_member_function (range, id_begin);
9692 else
9693 error ("range-based %<for%> expression of type %qT has an "
9694 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9696 if (member_end != NULL_TREE)
9697 *end = cp_parser_range_for_member_function (range, id_end);
9698 else
9699 error ("range-based %<for%> expression of type %qT has a "
9700 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9702 else
9704 /* Use global functions with ADL. */
9705 vec<tree, va_gc> *vec;
9706 vec = make_tree_vector ();
9708 vec_safe_push (vec, range);
9710 member_begin = perform_koenig_lookup (id_begin, vec,
9711 /*include_std=*/true,
9712 tf_warning_or_error);
9713 *begin = finish_call_expr (member_begin, &vec, false, true,
9714 tf_warning_or_error);
9715 member_end = perform_koenig_lookup (id_end, vec,
9716 /*include_std=*/true,
9717 tf_warning_or_error);
9718 *end = finish_call_expr (member_end, &vec, false, true,
9719 tf_warning_or_error);
9721 release_tree_vector (vec);
9724 /* Last common checks. */
9725 if (*begin == error_mark_node || *end == error_mark_node)
9727 /* If one of the expressions is an error do no more checks. */
9728 *begin = *end = error_mark_node;
9729 return error_mark_node;
9731 else
9733 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9734 /* The unqualified type of the __begin and __end temporaries should
9735 be the same, as required by the multiple auto declaration. */
9736 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9737 error ("inconsistent begin/end types in range-based %<for%> "
9738 "statement: %qT and %qT",
9739 TREE_TYPE (*begin), TREE_TYPE (*end));
9740 return iter_type;
9745 /* Helper function for cp_parser_perform_range_for_lookup.
9746 Builds a tree for RANGE.IDENTIFIER(). */
9748 static tree
9749 cp_parser_range_for_member_function (tree range, tree identifier)
9751 tree member, res;
9752 vec<tree, va_gc> *vec;
9754 member = finish_class_member_access_expr (range, identifier,
9755 false, tf_warning_or_error);
9756 if (member == error_mark_node)
9757 return error_mark_node;
9759 vec = make_tree_vector ();
9760 res = finish_call_expr (member, &vec,
9761 /*disallow_virtual=*/false,
9762 /*koenig_p=*/false,
9763 tf_warning_or_error);
9764 release_tree_vector (vec);
9765 return res;
9768 /* Parse an iteration-statement.
9770 iteration-statement:
9771 while ( condition ) statement
9772 do statement while ( expression ) ;
9773 for ( for-init-statement condition [opt] ; expression [opt] )
9774 statement
9776 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9778 static tree
9779 cp_parser_iteration_statement (cp_parser* parser)
9781 cp_token *token;
9782 enum rid keyword;
9783 tree statement;
9784 unsigned char in_statement;
9786 /* Peek at the next token. */
9787 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9788 if (!token)
9789 return error_mark_node;
9791 /* Remember whether or not we are already within an iteration
9792 statement. */
9793 in_statement = parser->in_statement;
9795 /* See what kind of keyword it is. */
9796 keyword = token->keyword;
9797 switch (keyword)
9799 case RID_WHILE:
9801 tree condition;
9803 /* Begin the while-statement. */
9804 statement = begin_while_stmt ();
9805 /* Look for the `('. */
9806 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9807 /* Parse the condition. */
9808 condition = cp_parser_condition (parser);
9809 finish_while_stmt_cond (condition, statement);
9810 /* Look for the `)'. */
9811 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9812 /* Parse the dependent statement. */
9813 parser->in_statement = IN_ITERATION_STMT;
9814 cp_parser_already_scoped_statement (parser);
9815 parser->in_statement = in_statement;
9816 /* We're done with the while-statement. */
9817 finish_while_stmt (statement);
9819 break;
9821 case RID_DO:
9823 tree expression;
9825 /* Begin the do-statement. */
9826 statement = begin_do_stmt ();
9827 /* Parse the body of the do-statement. */
9828 parser->in_statement = IN_ITERATION_STMT;
9829 cp_parser_implicitly_scoped_statement (parser, NULL);
9830 parser->in_statement = in_statement;
9831 finish_do_body (statement);
9832 /* Look for the `while' keyword. */
9833 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9834 /* Look for the `('. */
9835 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9836 /* Parse the expression. */
9837 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9838 /* We're done with the do-statement. */
9839 finish_do_stmt (expression, statement);
9840 /* Look for the `)'. */
9841 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9842 /* Look for the `;'. */
9843 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9845 break;
9847 case RID_FOR:
9849 /* Look for the `('. */
9850 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9852 statement = cp_parser_for (parser);
9854 /* Look for the `)'. */
9855 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9857 /* Parse the body of the for-statement. */
9858 parser->in_statement = IN_ITERATION_STMT;
9859 cp_parser_already_scoped_statement (parser);
9860 parser->in_statement = in_statement;
9862 /* We're done with the for-statement. */
9863 finish_for_stmt (statement);
9865 break;
9867 default:
9868 cp_parser_error (parser, "expected iteration-statement");
9869 statement = error_mark_node;
9870 break;
9873 return statement;
9876 /* Parse a for-init-statement or the declarator of a range-based-for.
9877 Returns true if a range-based-for declaration is seen.
9879 for-init-statement:
9880 expression-statement
9881 simple-declaration */
9883 static bool
9884 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9886 /* If the next token is a `;', then we have an empty
9887 expression-statement. Grammatically, this is also a
9888 simple-declaration, but an invalid one, because it does not
9889 declare anything. Therefore, if we did not handle this case
9890 specially, we would issue an error message about an invalid
9891 declaration. */
9892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9894 bool is_range_for = false;
9895 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9897 parser->colon_corrects_to_scope_p = false;
9899 /* We're going to speculatively look for a declaration, falling back
9900 to an expression, if necessary. */
9901 cp_parser_parse_tentatively (parser);
9902 /* Parse the declaration. */
9903 cp_parser_simple_declaration (parser,
9904 /*function_definition_allowed_p=*/false,
9905 decl);
9906 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9907 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9909 /* It is a range-for, consume the ':' */
9910 cp_lexer_consume_token (parser->lexer);
9911 is_range_for = true;
9912 if (cxx_dialect < cxx0x)
9914 error_at (cp_lexer_peek_token (parser->lexer)->location,
9915 "range-based %<for%> loops are not allowed "
9916 "in C++98 mode");
9917 *decl = error_mark_node;
9920 else
9921 /* The ';' is not consumed yet because we told
9922 cp_parser_simple_declaration not to. */
9923 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9925 if (cp_parser_parse_definitely (parser))
9926 return is_range_for;
9927 /* If the tentative parse failed, then we shall need to look for an
9928 expression-statement. */
9930 /* If we are here, it is an expression-statement. */
9931 cp_parser_expression_statement (parser, NULL_TREE);
9932 return false;
9935 /* Parse a jump-statement.
9937 jump-statement:
9938 break ;
9939 continue ;
9940 return expression [opt] ;
9941 return braced-init-list ;
9942 goto identifier ;
9944 GNU extension:
9946 jump-statement:
9947 goto * expression ;
9949 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9951 static tree
9952 cp_parser_jump_statement (cp_parser* parser)
9954 tree statement = error_mark_node;
9955 cp_token *token;
9956 enum rid keyword;
9957 unsigned char in_statement;
9959 /* Peek at the next token. */
9960 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9961 if (!token)
9962 return error_mark_node;
9964 /* See what kind of keyword it is. */
9965 keyword = token->keyword;
9966 switch (keyword)
9968 case RID_BREAK:
9969 in_statement = parser->in_statement & ~IN_IF_STMT;
9970 switch (in_statement)
9972 case 0:
9973 error_at (token->location, "break statement not within loop or switch");
9974 break;
9975 default:
9976 gcc_assert ((in_statement & IN_SWITCH_STMT)
9977 || in_statement == IN_ITERATION_STMT);
9978 statement = finish_break_stmt ();
9979 break;
9980 case IN_OMP_BLOCK:
9981 error_at (token->location, "invalid exit from OpenMP structured block");
9982 break;
9983 case IN_OMP_FOR:
9984 error_at (token->location, "break statement used with OpenMP for loop");
9985 break;
9987 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9988 break;
9990 case RID_CONTINUE:
9991 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9993 case 0:
9994 error_at (token->location, "continue statement not within a loop");
9995 break;
9996 case IN_ITERATION_STMT:
9997 case IN_OMP_FOR:
9998 statement = finish_continue_stmt ();
9999 break;
10000 case IN_OMP_BLOCK:
10001 error_at (token->location, "invalid exit from OpenMP structured block");
10002 break;
10003 default:
10004 gcc_unreachable ();
10006 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10007 break;
10009 case RID_RETURN:
10011 tree expr;
10012 bool expr_non_constant_p;
10014 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10016 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10017 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10019 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10020 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10021 else
10022 /* If the next token is a `;', then there is no
10023 expression. */
10024 expr = NULL_TREE;
10025 /* Build the return-statement. */
10026 statement = finish_return_stmt (expr);
10027 /* Look for the final `;'. */
10028 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10030 break;
10032 case RID_GOTO:
10033 /* Create the goto-statement. */
10034 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10036 /* Issue a warning about this use of a GNU extension. */
10037 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10038 /* Consume the '*' token. */
10039 cp_lexer_consume_token (parser->lexer);
10040 /* Parse the dependent expression. */
10041 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
10043 else
10044 finish_goto_stmt (cp_parser_identifier (parser));
10045 /* Look for the final `;'. */
10046 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10047 break;
10049 default:
10050 cp_parser_error (parser, "expected jump-statement");
10051 break;
10054 return statement;
10057 /* Parse a declaration-statement.
10059 declaration-statement:
10060 block-declaration */
10062 static void
10063 cp_parser_declaration_statement (cp_parser* parser)
10065 void *p;
10067 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10068 p = obstack_alloc (&declarator_obstack, 0);
10070 /* Parse the block-declaration. */
10071 cp_parser_block_declaration (parser, /*statement_p=*/true);
10073 /* Free any declarators allocated. */
10074 obstack_free (&declarator_obstack, p);
10076 /* Finish off the statement. */
10077 finish_stmt ();
10080 /* Some dependent statements (like `if (cond) statement'), are
10081 implicitly in their own scope. In other words, if the statement is
10082 a single statement (as opposed to a compound-statement), it is
10083 none-the-less treated as if it were enclosed in braces. Any
10084 declarations appearing in the dependent statement are out of scope
10085 after control passes that point. This function parses a statement,
10086 but ensures that is in its own scope, even if it is not a
10087 compound-statement.
10089 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10090 is a (possibly labeled) if statement which is not enclosed in
10091 braces and has an else clause. This is used to implement
10092 -Wparentheses.
10094 Returns the new statement. */
10096 static tree
10097 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10099 tree statement;
10101 if (if_p != NULL)
10102 *if_p = false;
10104 /* Mark if () ; with a special NOP_EXPR. */
10105 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10107 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10108 cp_lexer_consume_token (parser->lexer);
10109 statement = add_stmt (build_empty_stmt (loc));
10111 /* if a compound is opened, we simply parse the statement directly. */
10112 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10113 statement = cp_parser_compound_statement (parser, NULL, false, false);
10114 /* If the token is not a `{', then we must take special action. */
10115 else
10117 /* Create a compound-statement. */
10118 statement = begin_compound_stmt (0);
10119 /* Parse the dependent-statement. */
10120 cp_parser_statement (parser, NULL_TREE, false, if_p);
10121 /* Finish the dummy compound-statement. */
10122 finish_compound_stmt (statement);
10125 /* Return the statement. */
10126 return statement;
10129 /* For some dependent statements (like `while (cond) statement'), we
10130 have already created a scope. Therefore, even if the dependent
10131 statement is a compound-statement, we do not want to create another
10132 scope. */
10134 static void
10135 cp_parser_already_scoped_statement (cp_parser* parser)
10137 /* If the token is a `{', then we must take special action. */
10138 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10139 cp_parser_statement (parser, NULL_TREE, false, NULL);
10140 else
10142 /* Avoid calling cp_parser_compound_statement, so that we
10143 don't create a new scope. Do everything else by hand. */
10144 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10145 /* If the next keyword is `__label__' we have a label declaration. */
10146 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10147 cp_parser_label_declaration (parser);
10148 /* Parse an (optional) statement-seq. */
10149 cp_parser_statement_seq_opt (parser, NULL_TREE);
10150 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10154 /* Declarations [gram.dcl.dcl] */
10156 /* Parse an optional declaration-sequence.
10158 declaration-seq:
10159 declaration
10160 declaration-seq declaration */
10162 static void
10163 cp_parser_declaration_seq_opt (cp_parser* parser)
10165 while (true)
10167 cp_token *token;
10169 token = cp_lexer_peek_token (parser->lexer);
10171 if (token->type == CPP_CLOSE_BRACE
10172 || token->type == CPP_EOF
10173 || token->type == CPP_PRAGMA_EOL)
10174 break;
10176 if (token->type == CPP_SEMICOLON)
10178 /* A declaration consisting of a single semicolon is
10179 invalid. Allow it unless we're being pedantic. */
10180 cp_lexer_consume_token (parser->lexer);
10181 if (!in_system_header)
10182 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10183 continue;
10186 /* If we're entering or exiting a region that's implicitly
10187 extern "C", modify the lang context appropriately. */
10188 if (!parser->implicit_extern_c && token->implicit_extern_c)
10190 push_lang_context (lang_name_c);
10191 parser->implicit_extern_c = true;
10193 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10195 pop_lang_context ();
10196 parser->implicit_extern_c = false;
10199 if (token->type == CPP_PRAGMA)
10201 /* A top-level declaration can consist solely of a #pragma.
10202 A nested declaration cannot, so this is done here and not
10203 in cp_parser_declaration. (A #pragma at block scope is
10204 handled in cp_parser_statement.) */
10205 cp_parser_pragma (parser, pragma_external);
10206 continue;
10209 /* Parse the declaration itself. */
10210 cp_parser_declaration (parser);
10214 /* Parse a declaration.
10216 declaration:
10217 block-declaration
10218 function-definition
10219 template-declaration
10220 explicit-instantiation
10221 explicit-specialization
10222 linkage-specification
10223 namespace-definition
10225 GNU extension:
10227 declaration:
10228 __extension__ declaration */
10230 static void
10231 cp_parser_declaration (cp_parser* parser)
10233 cp_token token1;
10234 cp_token token2;
10235 int saved_pedantic;
10236 void *p;
10237 tree attributes = NULL_TREE;
10239 /* Check for the `__extension__' keyword. */
10240 if (cp_parser_extension_opt (parser, &saved_pedantic))
10242 /* Parse the qualified declaration. */
10243 cp_parser_declaration (parser);
10244 /* Restore the PEDANTIC flag. */
10245 pedantic = saved_pedantic;
10247 return;
10250 /* Try to figure out what kind of declaration is present. */
10251 token1 = *cp_lexer_peek_token (parser->lexer);
10253 if (token1.type != CPP_EOF)
10254 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10255 else
10257 token2.type = CPP_EOF;
10258 token2.keyword = RID_MAX;
10261 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10262 p = obstack_alloc (&declarator_obstack, 0);
10264 /* If the next token is `extern' and the following token is a string
10265 literal, then we have a linkage specification. */
10266 if (token1.keyword == RID_EXTERN
10267 && cp_parser_is_pure_string_literal (&token2))
10268 cp_parser_linkage_specification (parser);
10269 /* If the next token is `template', then we have either a template
10270 declaration, an explicit instantiation, or an explicit
10271 specialization. */
10272 else if (token1.keyword == RID_TEMPLATE)
10274 /* `template <>' indicates a template specialization. */
10275 if (token2.type == CPP_LESS
10276 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10277 cp_parser_explicit_specialization (parser);
10278 /* `template <' indicates a template declaration. */
10279 else if (token2.type == CPP_LESS)
10280 cp_parser_template_declaration (parser, /*member_p=*/false);
10281 /* Anything else must be an explicit instantiation. */
10282 else
10283 cp_parser_explicit_instantiation (parser);
10285 /* If the next token is `export', then we have a template
10286 declaration. */
10287 else if (token1.keyword == RID_EXPORT)
10288 cp_parser_template_declaration (parser, /*member_p=*/false);
10289 /* If the next token is `extern', 'static' or 'inline' and the one
10290 after that is `template', we have a GNU extended explicit
10291 instantiation directive. */
10292 else if (cp_parser_allow_gnu_extensions_p (parser)
10293 && (token1.keyword == RID_EXTERN
10294 || token1.keyword == RID_STATIC
10295 || token1.keyword == RID_INLINE)
10296 && token2.keyword == RID_TEMPLATE)
10297 cp_parser_explicit_instantiation (parser);
10298 /* If the next token is `namespace', check for a named or unnamed
10299 namespace definition. */
10300 else if (token1.keyword == RID_NAMESPACE
10301 && (/* A named namespace definition. */
10302 (token2.type == CPP_NAME
10303 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10304 != CPP_EQ))
10305 /* An unnamed namespace definition. */
10306 || token2.type == CPP_OPEN_BRACE
10307 || token2.keyword == RID_ATTRIBUTE))
10308 cp_parser_namespace_definition (parser);
10309 /* An inline (associated) namespace definition. */
10310 else if (token1.keyword == RID_INLINE
10311 && token2.keyword == RID_NAMESPACE)
10312 cp_parser_namespace_definition (parser);
10313 /* Objective-C++ declaration/definition. */
10314 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10315 cp_parser_objc_declaration (parser, NULL_TREE);
10316 else if (c_dialect_objc ()
10317 && token1.keyword == RID_ATTRIBUTE
10318 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10319 cp_parser_objc_declaration (parser, attributes);
10320 /* We must have either a block declaration or a function
10321 definition. */
10322 else
10323 /* Try to parse a block-declaration, or a function-definition. */
10324 cp_parser_block_declaration (parser, /*statement_p=*/false);
10326 /* Free any declarators allocated. */
10327 obstack_free (&declarator_obstack, p);
10330 /* Parse a block-declaration.
10332 block-declaration:
10333 simple-declaration
10334 asm-definition
10335 namespace-alias-definition
10336 using-declaration
10337 using-directive
10339 GNU Extension:
10341 block-declaration:
10342 __extension__ block-declaration
10344 C++0x Extension:
10346 block-declaration:
10347 static_assert-declaration
10349 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10350 part of a declaration-statement. */
10352 static void
10353 cp_parser_block_declaration (cp_parser *parser,
10354 bool statement_p)
10356 cp_token *token1;
10357 int saved_pedantic;
10359 /* Check for the `__extension__' keyword. */
10360 if (cp_parser_extension_opt (parser, &saved_pedantic))
10362 /* Parse the qualified declaration. */
10363 cp_parser_block_declaration (parser, statement_p);
10364 /* Restore the PEDANTIC flag. */
10365 pedantic = saved_pedantic;
10367 return;
10370 /* Peek at the next token to figure out which kind of declaration is
10371 present. */
10372 token1 = cp_lexer_peek_token (parser->lexer);
10374 /* If the next keyword is `asm', we have an asm-definition. */
10375 if (token1->keyword == RID_ASM)
10377 if (statement_p)
10378 cp_parser_commit_to_tentative_parse (parser);
10379 cp_parser_asm_definition (parser);
10381 /* If the next keyword is `namespace', we have a
10382 namespace-alias-definition. */
10383 else if (token1->keyword == RID_NAMESPACE)
10384 cp_parser_namespace_alias_definition (parser);
10385 /* If the next keyword is `using', we have a
10386 using-declaration, a using-directive, or an alias-declaration. */
10387 else if (token1->keyword == RID_USING)
10389 cp_token *token2;
10391 if (statement_p)
10392 cp_parser_commit_to_tentative_parse (parser);
10393 /* If the token after `using' is `namespace', then we have a
10394 using-directive. */
10395 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10396 if (token2->keyword == RID_NAMESPACE)
10397 cp_parser_using_directive (parser);
10398 /* If the second token after 'using' is '=', then we have an
10399 alias-declaration. */
10400 else if (cxx_dialect >= cxx0x
10401 && token2->type == CPP_NAME
10402 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10403 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
10404 cp_parser_alias_declaration (parser);
10405 /* Otherwise, it's a using-declaration. */
10406 else
10407 cp_parser_using_declaration (parser,
10408 /*access_declaration_p=*/false);
10410 /* If the next keyword is `__label__' we have a misplaced label
10411 declaration. */
10412 else if (token1->keyword == RID_LABEL)
10414 cp_lexer_consume_token (parser->lexer);
10415 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10416 cp_parser_skip_to_end_of_statement (parser);
10417 /* If the next token is now a `;', consume it. */
10418 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10419 cp_lexer_consume_token (parser->lexer);
10421 /* If the next token is `static_assert' we have a static assertion. */
10422 else if (token1->keyword == RID_STATIC_ASSERT)
10423 cp_parser_static_assert (parser, /*member_p=*/false);
10424 /* Anything else must be a simple-declaration. */
10425 else
10426 cp_parser_simple_declaration (parser, !statement_p,
10427 /*maybe_range_for_decl*/NULL);
10430 /* Parse a simple-declaration.
10432 simple-declaration:
10433 decl-specifier-seq [opt] init-declarator-list [opt] ;
10435 init-declarator-list:
10436 init-declarator
10437 init-declarator-list , init-declarator
10439 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10440 function-definition as a simple-declaration.
10442 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10443 parsed declaration if it is an uninitialized single declarator not followed
10444 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10445 if present, will not be consumed. */
10447 static void
10448 cp_parser_simple_declaration (cp_parser* parser,
10449 bool function_definition_allowed_p,
10450 tree *maybe_range_for_decl)
10452 cp_decl_specifier_seq decl_specifiers;
10453 int declares_class_or_enum;
10454 bool saw_declarator;
10456 if (maybe_range_for_decl)
10457 *maybe_range_for_decl = NULL_TREE;
10459 /* Defer access checks until we know what is being declared; the
10460 checks for names appearing in the decl-specifier-seq should be
10461 done as if we were in the scope of the thing being declared. */
10462 push_deferring_access_checks (dk_deferred);
10464 /* Parse the decl-specifier-seq. We have to keep track of whether
10465 or not the decl-specifier-seq declares a named class or
10466 enumeration type, since that is the only case in which the
10467 init-declarator-list is allowed to be empty.
10469 [dcl.dcl]
10471 In a simple-declaration, the optional init-declarator-list can be
10472 omitted only when declaring a class or enumeration, that is when
10473 the decl-specifier-seq contains either a class-specifier, an
10474 elaborated-type-specifier, or an enum-specifier. */
10475 cp_parser_decl_specifier_seq (parser,
10476 CP_PARSER_FLAGS_OPTIONAL,
10477 &decl_specifiers,
10478 &declares_class_or_enum);
10479 /* We no longer need to defer access checks. */
10480 stop_deferring_access_checks ();
10482 /* In a block scope, a valid declaration must always have a
10483 decl-specifier-seq. By not trying to parse declarators, we can
10484 resolve the declaration/expression ambiguity more quickly. */
10485 if (!function_definition_allowed_p
10486 && !decl_specifiers.any_specifiers_p)
10488 cp_parser_error (parser, "expected declaration");
10489 goto done;
10492 /* If the next two tokens are both identifiers, the code is
10493 erroneous. The usual cause of this situation is code like:
10495 T t;
10497 where "T" should name a type -- but does not. */
10498 if (!decl_specifiers.any_type_specifiers_p
10499 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10501 /* If parsing tentatively, we should commit; we really are
10502 looking at a declaration. */
10503 cp_parser_commit_to_tentative_parse (parser);
10504 /* Give up. */
10505 goto done;
10508 /* If we have seen at least one decl-specifier, and the next token
10509 is not a parenthesis, then we must be looking at a declaration.
10510 (After "int (" we might be looking at a functional cast.) */
10511 if (decl_specifiers.any_specifiers_p
10512 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10513 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10514 && !cp_parser_error_occurred (parser))
10515 cp_parser_commit_to_tentative_parse (parser);
10517 /* Keep going until we hit the `;' at the end of the simple
10518 declaration. */
10519 saw_declarator = false;
10520 while (cp_lexer_next_token_is_not (parser->lexer,
10521 CPP_SEMICOLON))
10523 cp_token *token;
10524 bool function_definition_p;
10525 tree decl;
10527 if (saw_declarator)
10529 /* If we are processing next declarator, coma is expected */
10530 token = cp_lexer_peek_token (parser->lexer);
10531 gcc_assert (token->type == CPP_COMMA);
10532 cp_lexer_consume_token (parser->lexer);
10533 if (maybe_range_for_decl)
10534 *maybe_range_for_decl = error_mark_node;
10536 else
10537 saw_declarator = true;
10539 /* Parse the init-declarator. */
10540 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10541 /*checks=*/NULL,
10542 function_definition_allowed_p,
10543 /*member_p=*/false,
10544 declares_class_or_enum,
10545 &function_definition_p,
10546 maybe_range_for_decl);
10547 /* If an error occurred while parsing tentatively, exit quickly.
10548 (That usually happens when in the body of a function; each
10549 statement is treated as a declaration-statement until proven
10550 otherwise.) */
10551 if (cp_parser_error_occurred (parser))
10552 goto done;
10553 /* Handle function definitions specially. */
10554 if (function_definition_p)
10556 /* If the next token is a `,', then we are probably
10557 processing something like:
10559 void f() {}, *p;
10561 which is erroneous. */
10562 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10564 cp_token *token = cp_lexer_peek_token (parser->lexer);
10565 error_at (token->location,
10566 "mixing"
10567 " declarations and function-definitions is forbidden");
10569 /* Otherwise, we're done with the list of declarators. */
10570 else
10572 pop_deferring_access_checks ();
10573 return;
10576 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10577 *maybe_range_for_decl = decl;
10578 /* The next token should be either a `,' or a `;'. */
10579 token = cp_lexer_peek_token (parser->lexer);
10580 /* If it's a `,', there are more declarators to come. */
10581 if (token->type == CPP_COMMA)
10582 /* will be consumed next time around */;
10583 /* If it's a `;', we are done. */
10584 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10585 break;
10586 /* Anything else is an error. */
10587 else
10589 /* If we have already issued an error message we don't need
10590 to issue another one. */
10591 if (decl != error_mark_node
10592 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10593 cp_parser_error (parser, "expected %<,%> or %<;%>");
10594 /* Skip tokens until we reach the end of the statement. */
10595 cp_parser_skip_to_end_of_statement (parser);
10596 /* If the next token is now a `;', consume it. */
10597 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10598 cp_lexer_consume_token (parser->lexer);
10599 goto done;
10601 /* After the first time around, a function-definition is not
10602 allowed -- even if it was OK at first. For example:
10604 int i, f() {}
10606 is not valid. */
10607 function_definition_allowed_p = false;
10610 /* Issue an error message if no declarators are present, and the
10611 decl-specifier-seq does not itself declare a class or
10612 enumeration. */
10613 if (!saw_declarator)
10615 if (cp_parser_declares_only_class_p (parser))
10616 shadow_tag (&decl_specifiers);
10617 /* Perform any deferred access checks. */
10618 perform_deferred_access_checks (tf_warning_or_error);
10621 /* Consume the `;'. */
10622 if (!maybe_range_for_decl)
10623 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10625 done:
10626 pop_deferring_access_checks ();
10629 /* Parse a decl-specifier-seq.
10631 decl-specifier-seq:
10632 decl-specifier-seq [opt] decl-specifier
10633 decl-specifier attribute-specifier-seq [opt] (C++11)
10635 decl-specifier:
10636 storage-class-specifier
10637 type-specifier
10638 function-specifier
10639 friend
10640 typedef
10642 GNU Extension:
10644 decl-specifier:
10645 attributes
10647 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10649 The parser flags FLAGS is used to control type-specifier parsing.
10651 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10652 flags:
10654 1: one of the decl-specifiers is an elaborated-type-specifier
10655 (i.e., a type declaration)
10656 2: one of the decl-specifiers is an enum-specifier or a
10657 class-specifier (i.e., a type definition)
10661 static void
10662 cp_parser_decl_specifier_seq (cp_parser* parser,
10663 cp_parser_flags flags,
10664 cp_decl_specifier_seq *decl_specs,
10665 int* declares_class_or_enum)
10667 bool constructor_possible_p = !parser->in_declarator_p;
10668 bool found_decl_spec = false;
10669 cp_token *start_token = NULL;
10670 cp_decl_spec ds;
10672 /* Clear DECL_SPECS. */
10673 clear_decl_specs (decl_specs);
10675 /* Assume no class or enumeration type is declared. */
10676 *declares_class_or_enum = 0;
10678 /* Keep reading specifiers until there are no more to read. */
10679 while (true)
10681 bool constructor_p;
10682 cp_token *token;
10683 ds = ds_last;
10685 /* Peek at the next token. */
10686 token = cp_lexer_peek_token (parser->lexer);
10688 /* Save the first token of the decl spec list for error
10689 reporting. */
10690 if (!start_token)
10691 start_token = token;
10692 /* Handle attributes. */
10693 if (cp_next_tokens_can_be_attribute_p (parser))
10695 /* Parse the attributes. */
10696 tree attrs = cp_parser_attributes_opt (parser);
10698 /* In a sequence of declaration specifiers, c++11 attributes
10699 appertain to the type that precede them. In that case
10700 [dcl.spec]/1 says:
10702 The attribute-specifier-seq affects the type only for
10703 the declaration it appears in, not other declarations
10704 involving the same type.
10706 But for now let's force the user to position the
10707 attribute either at the beginning of the declaration or
10708 after the declarator-id, which would clearly mean that it
10709 applies to the declarator. */
10710 if (cxx11_attribute_p (attrs))
10712 if (!found_decl_spec)
10713 /* The c++11 attribute is at the beginning of the
10714 declaration. It appertains to the entity being
10715 declared. */;
10716 else
10718 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
10720 /* This is an attribute following a
10721 class-specifier. */
10722 if (decl_specs->type_definition_p)
10723 warn_misplaced_attr_for_class_type (token->location,
10724 decl_specs->type);
10725 attrs = NULL_TREE;
10727 else
10729 decl_specs->std_attributes
10730 = chainon (decl_specs->std_attributes,
10731 attrs);
10732 if (decl_specs->locations[ds_std_attribute] == 0)
10733 decl_specs->locations[ds_std_attribute] = token->location;
10735 continue;
10739 decl_specs->attributes
10740 = chainon (decl_specs->attributes,
10741 attrs);
10742 if (decl_specs->locations[ds_attribute] == 0)
10743 decl_specs->locations[ds_attribute] = token->location;
10744 continue;
10746 /* Assume we will find a decl-specifier keyword. */
10747 found_decl_spec = true;
10748 /* If the next token is an appropriate keyword, we can simply
10749 add it to the list. */
10750 switch (token->keyword)
10752 /* decl-specifier:
10753 friend
10754 constexpr */
10755 case RID_FRIEND:
10756 if (!at_class_scope_p ())
10758 error_at (token->location, "%<friend%> used outside of class");
10759 cp_lexer_purge_token (parser->lexer);
10761 else
10763 ds = ds_friend;
10764 /* Consume the token. */
10765 cp_lexer_consume_token (parser->lexer);
10767 break;
10769 case RID_CONSTEXPR:
10770 ds = ds_constexpr;
10771 cp_lexer_consume_token (parser->lexer);
10772 break;
10774 /* function-specifier:
10775 inline
10776 virtual
10777 explicit */
10778 case RID_INLINE:
10779 case RID_VIRTUAL:
10780 case RID_EXPLICIT:
10781 cp_parser_function_specifier_opt (parser, decl_specs);
10782 break;
10784 /* decl-specifier:
10785 typedef */
10786 case RID_TYPEDEF:
10787 ds = ds_typedef;
10788 /* Consume the token. */
10789 cp_lexer_consume_token (parser->lexer);
10790 /* A constructor declarator cannot appear in a typedef. */
10791 constructor_possible_p = false;
10792 /* The "typedef" keyword can only occur in a declaration; we
10793 may as well commit at this point. */
10794 cp_parser_commit_to_tentative_parse (parser);
10796 if (decl_specs->storage_class != sc_none)
10797 decl_specs->conflicting_specifiers_p = true;
10798 break;
10800 /* storage-class-specifier:
10801 auto
10802 register
10803 static
10804 extern
10805 mutable
10807 GNU Extension:
10808 thread */
10809 case RID_AUTO:
10810 if (cxx_dialect == cxx98)
10812 /* Consume the token. */
10813 cp_lexer_consume_token (parser->lexer);
10815 /* Complain about `auto' as a storage specifier, if
10816 we're complaining about C++0x compatibility. */
10817 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10818 " changes meaning in C++11; please remove it");
10820 /* Set the storage class anyway. */
10821 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10822 token);
10824 else
10825 /* C++0x auto type-specifier. */
10826 found_decl_spec = false;
10827 break;
10829 case RID_REGISTER:
10830 case RID_STATIC:
10831 case RID_EXTERN:
10832 case RID_MUTABLE:
10833 /* Consume the token. */
10834 cp_lexer_consume_token (parser->lexer);
10835 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10836 token);
10837 break;
10838 case RID_THREAD:
10839 /* Consume the token. */
10840 ds = ds_thread;
10841 cp_lexer_consume_token (parser->lexer);
10842 break;
10844 default:
10845 /* We did not yet find a decl-specifier yet. */
10846 found_decl_spec = false;
10847 break;
10850 if (found_decl_spec
10851 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10852 && token->keyword != RID_CONSTEXPR)
10853 error ("decl-specifier invalid in condition");
10855 if (ds != ds_last)
10856 set_and_check_decl_spec_loc (decl_specs, ds, token);
10858 /* Constructors are a special case. The `S' in `S()' is not a
10859 decl-specifier; it is the beginning of the declarator. */
10860 constructor_p
10861 = (!found_decl_spec
10862 && constructor_possible_p
10863 && (cp_parser_constructor_declarator_p
10864 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
10866 /* If we don't have a DECL_SPEC yet, then we must be looking at
10867 a type-specifier. */
10868 if (!found_decl_spec && !constructor_p)
10870 int decl_spec_declares_class_or_enum;
10871 bool is_cv_qualifier;
10872 tree type_spec;
10874 type_spec
10875 = cp_parser_type_specifier (parser, flags,
10876 decl_specs,
10877 /*is_declaration=*/true,
10878 &decl_spec_declares_class_or_enum,
10879 &is_cv_qualifier);
10880 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10882 /* If this type-specifier referenced a user-defined type
10883 (a typedef, class-name, etc.), then we can't allow any
10884 more such type-specifiers henceforth.
10886 [dcl.spec]
10888 The longest sequence of decl-specifiers that could
10889 possibly be a type name is taken as the
10890 decl-specifier-seq of a declaration. The sequence shall
10891 be self-consistent as described below.
10893 [dcl.type]
10895 As a general rule, at most one type-specifier is allowed
10896 in the complete decl-specifier-seq of a declaration. The
10897 only exceptions are the following:
10899 -- const or volatile can be combined with any other
10900 type-specifier.
10902 -- signed or unsigned can be combined with char, long,
10903 short, or int.
10905 -- ..
10907 Example:
10909 typedef char* Pc;
10910 void g (const int Pc);
10912 Here, Pc is *not* part of the decl-specifier seq; it's
10913 the declarator. Therefore, once we see a type-specifier
10914 (other than a cv-qualifier), we forbid any additional
10915 user-defined types. We *do* still allow things like `int
10916 int' to be considered a decl-specifier-seq, and issue the
10917 error message later. */
10918 if (type_spec && !is_cv_qualifier)
10919 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10920 /* A constructor declarator cannot follow a type-specifier. */
10921 if (type_spec)
10923 constructor_possible_p = false;
10924 found_decl_spec = true;
10925 if (!is_cv_qualifier)
10926 decl_specs->any_type_specifiers_p = true;
10930 /* If we still do not have a DECL_SPEC, then there are no more
10931 decl-specifiers. */
10932 if (!found_decl_spec)
10933 break;
10935 decl_specs->any_specifiers_p = true;
10936 /* After we see one decl-specifier, further decl-specifiers are
10937 always optional. */
10938 flags |= CP_PARSER_FLAGS_OPTIONAL;
10941 /* Don't allow a friend specifier with a class definition. */
10942 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
10943 && (*declares_class_or_enum & 2))
10944 error_at (decl_specs->locations[ds_friend],
10945 "class definition may not be declared a friend");
10948 /* Parse an (optional) storage-class-specifier.
10950 storage-class-specifier:
10951 auto
10952 register
10953 static
10954 extern
10955 mutable
10957 GNU Extension:
10959 storage-class-specifier:
10960 thread
10962 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10964 static tree
10965 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10967 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10969 case RID_AUTO:
10970 if (cxx_dialect != cxx98)
10971 return NULL_TREE;
10972 /* Fall through for C++98. */
10974 case RID_REGISTER:
10975 case RID_STATIC:
10976 case RID_EXTERN:
10977 case RID_MUTABLE:
10978 case RID_THREAD:
10979 /* Consume the token. */
10980 return cp_lexer_consume_token (parser->lexer)->u.value;
10982 default:
10983 return NULL_TREE;
10987 /* Parse an (optional) function-specifier.
10989 function-specifier:
10990 inline
10991 virtual
10992 explicit
10994 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10995 Updates DECL_SPECS, if it is non-NULL. */
10997 static tree
10998 cp_parser_function_specifier_opt (cp_parser* parser,
10999 cp_decl_specifier_seq *decl_specs)
11001 cp_token *token = cp_lexer_peek_token (parser->lexer);
11002 switch (token->keyword)
11004 case RID_INLINE:
11005 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11006 break;
11008 case RID_VIRTUAL:
11009 /* 14.5.2.3 [temp.mem]
11011 A member function template shall not be virtual. */
11012 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11013 error_at (token->location, "templates may not be %<virtual%>");
11014 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11015 break;
11017 case RID_EXPLICIT:
11018 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11019 break;
11021 default:
11022 return NULL_TREE;
11025 /* Consume the token. */
11026 return cp_lexer_consume_token (parser->lexer)->u.value;
11029 /* Parse a linkage-specification.
11031 linkage-specification:
11032 extern string-literal { declaration-seq [opt] }
11033 extern string-literal declaration */
11035 static void
11036 cp_parser_linkage_specification (cp_parser* parser)
11038 tree linkage;
11040 /* Look for the `extern' keyword. */
11041 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11043 /* Look for the string-literal. */
11044 linkage = cp_parser_string_literal (parser, false, false);
11046 /* Transform the literal into an identifier. If the literal is a
11047 wide-character string, or contains embedded NULs, then we can't
11048 handle it as the user wants. */
11049 if (strlen (TREE_STRING_POINTER (linkage))
11050 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11052 cp_parser_error (parser, "invalid linkage-specification");
11053 /* Assume C++ linkage. */
11054 linkage = lang_name_cplusplus;
11056 else
11057 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11059 /* We're now using the new linkage. */
11060 push_lang_context (linkage);
11062 /* If the next token is a `{', then we're using the first
11063 production. */
11064 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11066 /* Consume the `{' token. */
11067 cp_lexer_consume_token (parser->lexer);
11068 /* Parse the declarations. */
11069 cp_parser_declaration_seq_opt (parser);
11070 /* Look for the closing `}'. */
11071 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11073 /* Otherwise, there's just one declaration. */
11074 else
11076 bool saved_in_unbraced_linkage_specification_p;
11078 saved_in_unbraced_linkage_specification_p
11079 = parser->in_unbraced_linkage_specification_p;
11080 parser->in_unbraced_linkage_specification_p = true;
11081 cp_parser_declaration (parser);
11082 parser->in_unbraced_linkage_specification_p
11083 = saved_in_unbraced_linkage_specification_p;
11086 /* We're done with the linkage-specification. */
11087 pop_lang_context ();
11090 /* Parse a static_assert-declaration.
11092 static_assert-declaration:
11093 static_assert ( constant-expression , string-literal ) ;
11095 If MEMBER_P, this static_assert is a class member. */
11097 static void
11098 cp_parser_static_assert(cp_parser *parser, bool member_p)
11100 tree condition;
11101 tree message;
11102 cp_token *token;
11103 location_t saved_loc;
11104 bool dummy;
11106 /* Peek at the `static_assert' token so we can keep track of exactly
11107 where the static assertion started. */
11108 token = cp_lexer_peek_token (parser->lexer);
11109 saved_loc = token->location;
11111 /* Look for the `static_assert' keyword. */
11112 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
11113 RT_STATIC_ASSERT))
11114 return;
11116 /* We know we are in a static assertion; commit to any tentative
11117 parse. */
11118 if (cp_parser_parsing_tentatively (parser))
11119 cp_parser_commit_to_tentative_parse (parser);
11121 /* Parse the `(' starting the static assertion condition. */
11122 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11124 /* Parse the constant-expression. Allow a non-constant expression
11125 here in order to give better diagnostics in finish_static_assert. */
11126 condition =
11127 cp_parser_constant_expression (parser,
11128 /*allow_non_constant_p=*/true,
11129 /*non_constant_p=*/&dummy);
11131 /* Parse the separating `,'. */
11132 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11134 /* Parse the string-literal message. */
11135 message = cp_parser_string_literal (parser,
11136 /*translate=*/false,
11137 /*wide_ok=*/true);
11139 /* A `)' completes the static assertion. */
11140 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11141 cp_parser_skip_to_closing_parenthesis (parser,
11142 /*recovering=*/true,
11143 /*or_comma=*/false,
11144 /*consume_paren=*/true);
11146 /* A semicolon terminates the declaration. */
11147 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11149 /* Complete the static assertion, which may mean either processing
11150 the static assert now or saving it for template instantiation. */
11151 finish_static_assert (condition, message, saved_loc, member_p);
11154 /* Parse a `decltype' type. Returns the type.
11156 simple-type-specifier:
11157 decltype ( expression ) */
11159 static tree
11160 cp_parser_decltype (cp_parser *parser)
11162 tree expr;
11163 bool id_expression_or_member_access_p = false;
11164 const char *saved_message;
11165 bool saved_integral_constant_expression_p;
11166 bool saved_non_integral_constant_expression_p;
11167 cp_token *id_expr_start_token;
11168 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11170 if (start_token->type == CPP_DECLTYPE)
11172 /* Already parsed. */
11173 cp_lexer_consume_token (parser->lexer);
11174 return start_token->u.value;
11177 /* Look for the `decltype' token. */
11178 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11179 return error_mark_node;
11181 /* Types cannot be defined in a `decltype' expression. Save away the
11182 old message. */
11183 saved_message = parser->type_definition_forbidden_message;
11185 /* And create the new one. */
11186 parser->type_definition_forbidden_message
11187 = G_("types may not be defined in %<decltype%> expressions");
11189 /* The restrictions on constant-expressions do not apply inside
11190 decltype expressions. */
11191 saved_integral_constant_expression_p
11192 = parser->integral_constant_expression_p;
11193 saved_non_integral_constant_expression_p
11194 = parser->non_integral_constant_expression_p;
11195 parser->integral_constant_expression_p = false;
11197 /* Do not actually evaluate the expression. */
11198 ++cp_unevaluated_operand;
11200 /* Do not warn about problems with the expression. */
11201 ++c_inhibit_evaluation_warnings;
11203 /* Parse the opening `('. */
11204 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11205 return error_mark_node;
11207 /* First, try parsing an id-expression. */
11208 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11209 cp_parser_parse_tentatively (parser);
11210 expr = cp_parser_id_expression (parser,
11211 /*template_keyword_p=*/false,
11212 /*check_dependency_p=*/true,
11213 /*template_p=*/NULL,
11214 /*declarator_p=*/false,
11215 /*optional_p=*/false);
11217 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11219 bool non_integral_constant_expression_p = false;
11220 tree id_expression = expr;
11221 cp_id_kind idk;
11222 const char *error_msg;
11224 if (TREE_CODE (expr) == IDENTIFIER_NODE)
11225 /* Lookup the name we got back from the id-expression. */
11226 expr = cp_parser_lookup_name (parser, expr,
11227 none_type,
11228 /*is_template=*/false,
11229 /*is_namespace=*/false,
11230 /*check_dependency=*/true,
11231 /*ambiguous_decls=*/NULL,
11232 id_expr_start_token->location);
11234 if (expr
11235 && expr != error_mark_node
11236 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11237 && TREE_CODE (expr) != TYPE_DECL
11238 && (TREE_CODE (expr) != BIT_NOT_EXPR
11239 || !TYPE_P (TREE_OPERAND (expr, 0)))
11240 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11242 /* Complete lookup of the id-expression. */
11243 expr = (finish_id_expression
11244 (id_expression, expr, parser->scope, &idk,
11245 /*integral_constant_expression_p=*/false,
11246 /*allow_non_integral_constant_expression_p=*/true,
11247 &non_integral_constant_expression_p,
11248 /*template_p=*/false,
11249 /*done=*/true,
11250 /*address_p=*/false,
11251 /*template_arg_p=*/false,
11252 &error_msg,
11253 id_expr_start_token->location));
11255 if (expr == error_mark_node)
11256 /* We found an id-expression, but it was something that we
11257 should not have found. This is an error, not something
11258 we can recover from, so note that we found an
11259 id-expression and we'll recover as gracefully as
11260 possible. */
11261 id_expression_or_member_access_p = true;
11264 if (expr
11265 && expr != error_mark_node
11266 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11267 /* We have an id-expression. */
11268 id_expression_or_member_access_p = true;
11271 if (!id_expression_or_member_access_p)
11273 /* Abort the id-expression parse. */
11274 cp_parser_abort_tentative_parse (parser);
11276 /* Parsing tentatively, again. */
11277 cp_parser_parse_tentatively (parser);
11279 /* Parse a class member access. */
11280 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11281 /*cast_p=*/false,
11282 /*member_access_only_p=*/true, NULL);
11284 if (expr
11285 && expr != error_mark_node
11286 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11287 /* We have an id-expression. */
11288 id_expression_or_member_access_p = true;
11291 if (id_expression_or_member_access_p)
11292 /* We have parsed the complete id-expression or member access. */
11293 cp_parser_parse_definitely (parser);
11294 else
11296 bool saved_greater_than_is_operator_p;
11298 /* Abort our attempt to parse an id-expression or member access
11299 expression. */
11300 cp_parser_abort_tentative_parse (parser);
11302 /* Within a parenthesized expression, a `>' token is always
11303 the greater-than operator. */
11304 saved_greater_than_is_operator_p
11305 = parser->greater_than_is_operator_p;
11306 parser->greater_than_is_operator_p = true;
11308 /* Parse a full expression. */
11309 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11311 /* The `>' token might be the end of a template-id or
11312 template-parameter-list now. */
11313 parser->greater_than_is_operator_p
11314 = saved_greater_than_is_operator_p;
11317 /* Go back to evaluating expressions. */
11318 --cp_unevaluated_operand;
11319 --c_inhibit_evaluation_warnings;
11321 /* Restore the old message and the integral constant expression
11322 flags. */
11323 parser->type_definition_forbidden_message = saved_message;
11324 parser->integral_constant_expression_p
11325 = saved_integral_constant_expression_p;
11326 parser->non_integral_constant_expression_p
11327 = saved_non_integral_constant_expression_p;
11329 /* Parse to the closing `)'. */
11330 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11332 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11333 /*consume_paren=*/true);
11334 return error_mark_node;
11337 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11338 tf_warning_or_error);
11340 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11341 it again. */
11342 start_token->type = CPP_DECLTYPE;
11343 start_token->u.value = expr;
11344 start_token->keyword = RID_MAX;
11345 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11347 return expr;
11350 /* Special member functions [gram.special] */
11352 /* Parse a conversion-function-id.
11354 conversion-function-id:
11355 operator conversion-type-id
11357 Returns an IDENTIFIER_NODE representing the operator. */
11359 static tree
11360 cp_parser_conversion_function_id (cp_parser* parser)
11362 tree type;
11363 tree saved_scope;
11364 tree saved_qualifying_scope;
11365 tree saved_object_scope;
11366 tree pushed_scope = NULL_TREE;
11368 /* Look for the `operator' token. */
11369 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11370 return error_mark_node;
11371 /* When we parse the conversion-type-id, the current scope will be
11372 reset. However, we need that information in able to look up the
11373 conversion function later, so we save it here. */
11374 saved_scope = parser->scope;
11375 saved_qualifying_scope = parser->qualifying_scope;
11376 saved_object_scope = parser->object_scope;
11377 /* We must enter the scope of the class so that the names of
11378 entities declared within the class are available in the
11379 conversion-type-id. For example, consider:
11381 struct S {
11382 typedef int I;
11383 operator I();
11386 S::operator I() { ... }
11388 In order to see that `I' is a type-name in the definition, we
11389 must be in the scope of `S'. */
11390 if (saved_scope)
11391 pushed_scope = push_scope (saved_scope);
11392 /* Parse the conversion-type-id. */
11393 type = cp_parser_conversion_type_id (parser);
11394 /* Leave the scope of the class, if any. */
11395 if (pushed_scope)
11396 pop_scope (pushed_scope);
11397 /* Restore the saved scope. */
11398 parser->scope = saved_scope;
11399 parser->qualifying_scope = saved_qualifying_scope;
11400 parser->object_scope = saved_object_scope;
11401 /* If the TYPE is invalid, indicate failure. */
11402 if (type == error_mark_node)
11403 return error_mark_node;
11404 return mangle_conv_op_name_for_type (type);
11407 /* Parse a conversion-type-id:
11409 conversion-type-id:
11410 type-specifier-seq conversion-declarator [opt]
11412 Returns the TYPE specified. */
11414 static tree
11415 cp_parser_conversion_type_id (cp_parser* parser)
11417 tree attributes;
11418 cp_decl_specifier_seq type_specifiers;
11419 cp_declarator *declarator;
11420 tree type_specified;
11422 /* Parse the attributes. */
11423 attributes = cp_parser_attributes_opt (parser);
11424 /* Parse the type-specifiers. */
11425 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11426 /*is_trailing_return=*/false,
11427 &type_specifiers);
11428 /* If that didn't work, stop. */
11429 if (type_specifiers.type == error_mark_node)
11430 return error_mark_node;
11431 /* Parse the conversion-declarator. */
11432 declarator = cp_parser_conversion_declarator_opt (parser);
11434 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11435 /*initialized=*/0, &attributes);
11436 if (attributes)
11437 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11439 /* Don't give this error when parsing tentatively. This happens to
11440 work because we always parse this definitively once. */
11441 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11442 && type_uses_auto (type_specified))
11444 if (cxx_dialect < cxx1y)
11446 error ("invalid use of %<auto%> in conversion operator");
11447 return error_mark_node;
11449 else if (template_parm_scope_p ())
11450 warning (0, "use of %<auto%> in member template "
11451 "conversion operator can never be deduced");
11454 return type_specified;
11457 /* Parse an (optional) conversion-declarator.
11459 conversion-declarator:
11460 ptr-operator conversion-declarator [opt]
11464 static cp_declarator *
11465 cp_parser_conversion_declarator_opt (cp_parser* parser)
11467 enum tree_code code;
11468 tree class_type, std_attributes = NULL_TREE;
11469 cp_cv_quals cv_quals;
11471 /* We don't know if there's a ptr-operator next, or not. */
11472 cp_parser_parse_tentatively (parser);
11473 /* Try the ptr-operator. */
11474 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
11475 &std_attributes);
11476 /* If it worked, look for more conversion-declarators. */
11477 if (cp_parser_parse_definitely (parser))
11479 cp_declarator *declarator;
11481 /* Parse another optional declarator. */
11482 declarator = cp_parser_conversion_declarator_opt (parser);
11484 declarator = cp_parser_make_indirect_declarator
11485 (code, class_type, cv_quals, declarator, std_attributes);
11487 return declarator;
11490 return NULL;
11493 /* Parse an (optional) ctor-initializer.
11495 ctor-initializer:
11496 : mem-initializer-list
11498 Returns TRUE iff the ctor-initializer was actually present. */
11500 static bool
11501 cp_parser_ctor_initializer_opt (cp_parser* parser)
11503 /* If the next token is not a `:', then there is no
11504 ctor-initializer. */
11505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11507 /* Do default initialization of any bases and members. */
11508 if (DECL_CONSTRUCTOR_P (current_function_decl))
11509 finish_mem_initializers (NULL_TREE);
11511 return false;
11514 /* Consume the `:' token. */
11515 cp_lexer_consume_token (parser->lexer);
11516 /* And the mem-initializer-list. */
11517 cp_parser_mem_initializer_list (parser);
11519 return true;
11522 /* Parse a mem-initializer-list.
11524 mem-initializer-list:
11525 mem-initializer ... [opt]
11526 mem-initializer ... [opt] , mem-initializer-list */
11528 static void
11529 cp_parser_mem_initializer_list (cp_parser* parser)
11531 tree mem_initializer_list = NULL_TREE;
11532 tree target_ctor = error_mark_node;
11533 cp_token *token = cp_lexer_peek_token (parser->lexer);
11535 /* Let the semantic analysis code know that we are starting the
11536 mem-initializer-list. */
11537 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11538 error_at (token->location,
11539 "only constructors take member initializers");
11541 /* Loop through the list. */
11542 while (true)
11544 tree mem_initializer;
11546 token = cp_lexer_peek_token (parser->lexer);
11547 /* Parse the mem-initializer. */
11548 mem_initializer = cp_parser_mem_initializer (parser);
11549 /* If the next token is a `...', we're expanding member initializers. */
11550 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11552 /* Consume the `...'. */
11553 cp_lexer_consume_token (parser->lexer);
11555 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11556 can be expanded but members cannot. */
11557 if (mem_initializer != error_mark_node
11558 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11560 error_at (token->location,
11561 "cannot expand initializer for member %<%D%>",
11562 TREE_PURPOSE (mem_initializer));
11563 mem_initializer = error_mark_node;
11566 /* Construct the pack expansion type. */
11567 if (mem_initializer != error_mark_node)
11568 mem_initializer = make_pack_expansion (mem_initializer);
11570 if (target_ctor != error_mark_node
11571 && mem_initializer != error_mark_node)
11573 error ("mem-initializer for %qD follows constructor delegation",
11574 TREE_PURPOSE (mem_initializer));
11575 mem_initializer = error_mark_node;
11577 /* Look for a target constructor. */
11578 if (mem_initializer != error_mark_node
11579 && TYPE_P (TREE_PURPOSE (mem_initializer))
11580 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11582 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11583 if (mem_initializer_list)
11585 error ("constructor delegation follows mem-initializer for %qD",
11586 TREE_PURPOSE (mem_initializer_list));
11587 mem_initializer = error_mark_node;
11589 target_ctor = mem_initializer;
11591 /* Add it to the list, unless it was erroneous. */
11592 if (mem_initializer != error_mark_node)
11594 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11595 mem_initializer_list = mem_initializer;
11597 /* If the next token is not a `,', we're done. */
11598 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11599 break;
11600 /* Consume the `,' token. */
11601 cp_lexer_consume_token (parser->lexer);
11604 /* Perform semantic analysis. */
11605 if (DECL_CONSTRUCTOR_P (current_function_decl))
11606 finish_mem_initializers (mem_initializer_list);
11609 /* Parse a mem-initializer.
11611 mem-initializer:
11612 mem-initializer-id ( expression-list [opt] )
11613 mem-initializer-id braced-init-list
11615 GNU extension:
11617 mem-initializer:
11618 ( expression-list [opt] )
11620 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11621 class) or FIELD_DECL (for a non-static data member) to initialize;
11622 the TREE_VALUE is the expression-list. An empty initialization
11623 list is represented by void_list_node. */
11625 static tree
11626 cp_parser_mem_initializer (cp_parser* parser)
11628 tree mem_initializer_id;
11629 tree expression_list;
11630 tree member;
11631 cp_token *token = cp_lexer_peek_token (parser->lexer);
11633 /* Find out what is being initialized. */
11634 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11636 permerror (token->location,
11637 "anachronistic old-style base class initializer");
11638 mem_initializer_id = NULL_TREE;
11640 else
11642 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11643 if (mem_initializer_id == error_mark_node)
11644 return mem_initializer_id;
11646 member = expand_member_init (mem_initializer_id);
11647 if (member && !DECL_P (member))
11648 in_base_initializer = 1;
11650 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11652 bool expr_non_constant_p;
11653 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11654 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11655 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11656 expression_list = build_tree_list (NULL_TREE, expression_list);
11658 else
11660 vec<tree, va_gc> *vec;
11661 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11662 /*cast_p=*/false,
11663 /*allow_expansion_p=*/true,
11664 /*non_constant_p=*/NULL);
11665 if (vec == NULL)
11666 return error_mark_node;
11667 expression_list = build_tree_list_vec (vec);
11668 release_tree_vector (vec);
11671 if (expression_list == error_mark_node)
11672 return error_mark_node;
11673 if (!expression_list)
11674 expression_list = void_type_node;
11676 in_base_initializer = 0;
11678 return member ? build_tree_list (member, expression_list) : error_mark_node;
11681 /* Parse a mem-initializer-id.
11683 mem-initializer-id:
11684 :: [opt] nested-name-specifier [opt] class-name
11685 identifier
11687 Returns a TYPE indicating the class to be initializer for the first
11688 production. Returns an IDENTIFIER_NODE indicating the data member
11689 to be initialized for the second production. */
11691 static tree
11692 cp_parser_mem_initializer_id (cp_parser* parser)
11694 bool global_scope_p;
11695 bool nested_name_specifier_p;
11696 bool template_p = false;
11697 tree id;
11699 cp_token *token = cp_lexer_peek_token (parser->lexer);
11701 /* `typename' is not allowed in this context ([temp.res]). */
11702 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11704 error_at (token->location,
11705 "keyword %<typename%> not allowed in this context (a qualified "
11706 "member initializer is implicitly a type)");
11707 cp_lexer_consume_token (parser->lexer);
11709 /* Look for the optional `::' operator. */
11710 global_scope_p
11711 = (cp_parser_global_scope_opt (parser,
11712 /*current_scope_valid_p=*/false)
11713 != NULL_TREE);
11714 /* Look for the optional nested-name-specifier. The simplest way to
11715 implement:
11717 [temp.res]
11719 The keyword `typename' is not permitted in a base-specifier or
11720 mem-initializer; in these contexts a qualified name that
11721 depends on a template-parameter is implicitly assumed to be a
11722 type name.
11724 is to assume that we have seen the `typename' keyword at this
11725 point. */
11726 nested_name_specifier_p
11727 = (cp_parser_nested_name_specifier_opt (parser,
11728 /*typename_keyword_p=*/true,
11729 /*check_dependency_p=*/true,
11730 /*type_p=*/true,
11731 /*is_declaration=*/true)
11732 != NULL_TREE);
11733 if (nested_name_specifier_p)
11734 template_p = cp_parser_optional_template_keyword (parser);
11735 /* If there is a `::' operator or a nested-name-specifier, then we
11736 are definitely looking for a class-name. */
11737 if (global_scope_p || nested_name_specifier_p)
11738 return cp_parser_class_name (parser,
11739 /*typename_keyword_p=*/true,
11740 /*template_keyword_p=*/template_p,
11741 typename_type,
11742 /*check_dependency_p=*/true,
11743 /*class_head_p=*/false,
11744 /*is_declaration=*/true);
11745 /* Otherwise, we could also be looking for an ordinary identifier. */
11746 cp_parser_parse_tentatively (parser);
11747 /* Try a class-name. */
11748 id = cp_parser_class_name (parser,
11749 /*typename_keyword_p=*/true,
11750 /*template_keyword_p=*/false,
11751 none_type,
11752 /*check_dependency_p=*/true,
11753 /*class_head_p=*/false,
11754 /*is_declaration=*/true);
11755 /* If we found one, we're done. */
11756 if (cp_parser_parse_definitely (parser))
11757 return id;
11758 /* Otherwise, look for an ordinary identifier. */
11759 return cp_parser_identifier (parser);
11762 /* Overloading [gram.over] */
11764 /* Parse an operator-function-id.
11766 operator-function-id:
11767 operator operator
11769 Returns an IDENTIFIER_NODE for the operator which is a
11770 human-readable spelling of the identifier, e.g., `operator +'. */
11772 static tree
11773 cp_parser_operator_function_id (cp_parser* parser)
11775 /* Look for the `operator' keyword. */
11776 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11777 return error_mark_node;
11778 /* And then the name of the operator itself. */
11779 return cp_parser_operator (parser);
11782 /* Return an identifier node for a user-defined literal operator.
11783 The suffix identifier is chained to the operator name identifier. */
11785 static tree
11786 cp_literal_operator_id (const char* name)
11788 tree identifier;
11789 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11790 + strlen (name) + 10);
11791 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11792 identifier = get_identifier (buffer);
11793 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11795 return identifier;
11798 /* Parse an operator.
11800 operator:
11801 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11802 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11803 || ++ -- , ->* -> () []
11805 GNU Extensions:
11807 operator:
11808 <? >? <?= >?=
11810 Returns an IDENTIFIER_NODE for the operator which is a
11811 human-readable spelling of the identifier, e.g., `operator +'. */
11813 static tree
11814 cp_parser_operator (cp_parser* parser)
11816 tree id = NULL_TREE;
11817 cp_token *token;
11819 /* Peek at the next token. */
11820 token = cp_lexer_peek_token (parser->lexer);
11821 /* Figure out which operator we have. */
11822 switch (token->type)
11824 case CPP_KEYWORD:
11826 enum tree_code op;
11828 /* The keyword should be either `new' or `delete'. */
11829 if (token->keyword == RID_NEW)
11830 op = NEW_EXPR;
11831 else if (token->keyword == RID_DELETE)
11832 op = DELETE_EXPR;
11833 else
11834 break;
11836 /* Consume the `new' or `delete' token. */
11837 cp_lexer_consume_token (parser->lexer);
11839 /* Peek at the next token. */
11840 token = cp_lexer_peek_token (parser->lexer);
11841 /* If it's a `[' token then this is the array variant of the
11842 operator. */
11843 if (token->type == CPP_OPEN_SQUARE)
11845 /* Consume the `[' token. */
11846 cp_lexer_consume_token (parser->lexer);
11847 /* Look for the `]' token. */
11848 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11849 id = ansi_opname (op == NEW_EXPR
11850 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11852 /* Otherwise, we have the non-array variant. */
11853 else
11854 id = ansi_opname (op);
11856 return id;
11859 case CPP_PLUS:
11860 id = ansi_opname (PLUS_EXPR);
11861 break;
11863 case CPP_MINUS:
11864 id = ansi_opname (MINUS_EXPR);
11865 break;
11867 case CPP_MULT:
11868 id = ansi_opname (MULT_EXPR);
11869 break;
11871 case CPP_DIV:
11872 id = ansi_opname (TRUNC_DIV_EXPR);
11873 break;
11875 case CPP_MOD:
11876 id = ansi_opname (TRUNC_MOD_EXPR);
11877 break;
11879 case CPP_XOR:
11880 id = ansi_opname (BIT_XOR_EXPR);
11881 break;
11883 case CPP_AND:
11884 id = ansi_opname (BIT_AND_EXPR);
11885 break;
11887 case CPP_OR:
11888 id = ansi_opname (BIT_IOR_EXPR);
11889 break;
11891 case CPP_COMPL:
11892 id = ansi_opname (BIT_NOT_EXPR);
11893 break;
11895 case CPP_NOT:
11896 id = ansi_opname (TRUTH_NOT_EXPR);
11897 break;
11899 case CPP_EQ:
11900 id = ansi_assopname (NOP_EXPR);
11901 break;
11903 case CPP_LESS:
11904 id = ansi_opname (LT_EXPR);
11905 break;
11907 case CPP_GREATER:
11908 id = ansi_opname (GT_EXPR);
11909 break;
11911 case CPP_PLUS_EQ:
11912 id = ansi_assopname (PLUS_EXPR);
11913 break;
11915 case CPP_MINUS_EQ:
11916 id = ansi_assopname (MINUS_EXPR);
11917 break;
11919 case CPP_MULT_EQ:
11920 id = ansi_assopname (MULT_EXPR);
11921 break;
11923 case CPP_DIV_EQ:
11924 id = ansi_assopname (TRUNC_DIV_EXPR);
11925 break;
11927 case CPP_MOD_EQ:
11928 id = ansi_assopname (TRUNC_MOD_EXPR);
11929 break;
11931 case CPP_XOR_EQ:
11932 id = ansi_assopname (BIT_XOR_EXPR);
11933 break;
11935 case CPP_AND_EQ:
11936 id = ansi_assopname (BIT_AND_EXPR);
11937 break;
11939 case CPP_OR_EQ:
11940 id = ansi_assopname (BIT_IOR_EXPR);
11941 break;
11943 case CPP_LSHIFT:
11944 id = ansi_opname (LSHIFT_EXPR);
11945 break;
11947 case CPP_RSHIFT:
11948 id = ansi_opname (RSHIFT_EXPR);
11949 break;
11951 case CPP_LSHIFT_EQ:
11952 id = ansi_assopname (LSHIFT_EXPR);
11953 break;
11955 case CPP_RSHIFT_EQ:
11956 id = ansi_assopname (RSHIFT_EXPR);
11957 break;
11959 case CPP_EQ_EQ:
11960 id = ansi_opname (EQ_EXPR);
11961 break;
11963 case CPP_NOT_EQ:
11964 id = ansi_opname (NE_EXPR);
11965 break;
11967 case CPP_LESS_EQ:
11968 id = ansi_opname (LE_EXPR);
11969 break;
11971 case CPP_GREATER_EQ:
11972 id = ansi_opname (GE_EXPR);
11973 break;
11975 case CPP_AND_AND:
11976 id = ansi_opname (TRUTH_ANDIF_EXPR);
11977 break;
11979 case CPP_OR_OR:
11980 id = ansi_opname (TRUTH_ORIF_EXPR);
11981 break;
11983 case CPP_PLUS_PLUS:
11984 id = ansi_opname (POSTINCREMENT_EXPR);
11985 break;
11987 case CPP_MINUS_MINUS:
11988 id = ansi_opname (PREDECREMENT_EXPR);
11989 break;
11991 case CPP_COMMA:
11992 id = ansi_opname (COMPOUND_EXPR);
11993 break;
11995 case CPP_DEREF_STAR:
11996 id = ansi_opname (MEMBER_REF);
11997 break;
11999 case CPP_DEREF:
12000 id = ansi_opname (COMPONENT_REF);
12001 break;
12003 case CPP_OPEN_PAREN:
12004 /* Consume the `('. */
12005 cp_lexer_consume_token (parser->lexer);
12006 /* Look for the matching `)'. */
12007 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12008 return ansi_opname (CALL_EXPR);
12010 case CPP_OPEN_SQUARE:
12011 /* Consume the `['. */
12012 cp_lexer_consume_token (parser->lexer);
12013 /* Look for the matching `]'. */
12014 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12015 return ansi_opname (ARRAY_REF);
12017 case CPP_STRING:
12018 if (cxx_dialect == cxx98)
12019 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12020 if (TREE_STRING_LENGTH (token->u.value) > 2)
12022 error ("expected empty string after %<operator%> keyword");
12023 return error_mark_node;
12025 /* Consume the string. */
12026 cp_lexer_consume_token (parser->lexer);
12027 /* Look for the suffix identifier. */
12028 token = cp_lexer_peek_token (parser->lexer);
12029 if (token->type == CPP_NAME)
12031 id = cp_parser_identifier (parser);
12032 if (id != error_mark_node)
12034 const char *name = IDENTIFIER_POINTER (id);
12035 return cp_literal_operator_id (name);
12038 else
12040 error ("expected suffix identifier");
12041 return error_mark_node;
12044 case CPP_STRING_USERDEF:
12045 error ("missing space between %<\"\"%> and suffix identifier");
12046 return error_mark_node;
12048 default:
12049 /* Anything else is an error. */
12050 break;
12053 /* If we have selected an identifier, we need to consume the
12054 operator token. */
12055 if (id)
12056 cp_lexer_consume_token (parser->lexer);
12057 /* Otherwise, no valid operator name was present. */
12058 else
12060 cp_parser_error (parser, "expected operator");
12061 id = error_mark_node;
12064 return id;
12067 /* Parse a template-declaration.
12069 template-declaration:
12070 export [opt] template < template-parameter-list > declaration
12072 If MEMBER_P is TRUE, this template-declaration occurs within a
12073 class-specifier.
12075 The grammar rule given by the standard isn't correct. What
12076 is really meant is:
12078 template-declaration:
12079 export [opt] template-parameter-list-seq
12080 decl-specifier-seq [opt] init-declarator [opt] ;
12081 export [opt] template-parameter-list-seq
12082 function-definition
12084 template-parameter-list-seq:
12085 template-parameter-list-seq [opt]
12086 template < template-parameter-list > */
12088 static void
12089 cp_parser_template_declaration (cp_parser* parser, bool member_p)
12091 /* Check for `export'. */
12092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
12094 /* Consume the `export' token. */
12095 cp_lexer_consume_token (parser->lexer);
12096 /* Warn that we do not support `export'. */
12097 warning (0, "keyword %<export%> not implemented, and will be ignored");
12100 cp_parser_template_declaration_after_export (parser, member_p);
12103 /* Parse a template-parameter-list.
12105 template-parameter-list:
12106 template-parameter
12107 template-parameter-list , template-parameter
12109 Returns a TREE_LIST. Each node represents a template parameter.
12110 The nodes are connected via their TREE_CHAINs. */
12112 static tree
12113 cp_parser_template_parameter_list (cp_parser* parser)
12115 tree parameter_list = NULL_TREE;
12117 begin_template_parm_list ();
12119 /* The loop below parses the template parms. We first need to know
12120 the total number of template parms to be able to compute proper
12121 canonical types of each dependent type. So after the loop, when
12122 we know the total number of template parms,
12123 end_template_parm_list computes the proper canonical types and
12124 fixes up the dependent types accordingly. */
12125 while (true)
12127 tree parameter;
12128 bool is_non_type;
12129 bool is_parameter_pack;
12130 location_t parm_loc;
12132 /* Parse the template-parameter. */
12133 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
12134 parameter = cp_parser_template_parameter (parser,
12135 &is_non_type,
12136 &is_parameter_pack);
12137 /* Add it to the list. */
12138 if (parameter != error_mark_node)
12139 parameter_list = process_template_parm (parameter_list,
12140 parm_loc,
12141 parameter,
12142 is_non_type,
12143 is_parameter_pack);
12144 else
12146 tree err_parm = build_tree_list (parameter, parameter);
12147 parameter_list = chainon (parameter_list, err_parm);
12150 /* If the next token is not a `,', we're done. */
12151 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12152 break;
12153 /* Otherwise, consume the `,' token. */
12154 cp_lexer_consume_token (parser->lexer);
12157 return end_template_parm_list (parameter_list);
12160 /* Parse a template-parameter.
12162 template-parameter:
12163 type-parameter
12164 parameter-declaration
12166 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
12167 the parameter. The TREE_PURPOSE is the default value, if any.
12168 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
12169 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
12170 set to true iff this parameter is a parameter pack. */
12172 static tree
12173 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12174 bool *is_parameter_pack)
12176 cp_token *token;
12177 cp_parameter_declarator *parameter_declarator;
12178 cp_declarator *id_declarator;
12179 tree parm;
12181 /* Assume it is a type parameter or a template parameter. */
12182 *is_non_type = false;
12183 /* Assume it not a parameter pack. */
12184 *is_parameter_pack = false;
12185 /* Peek at the next token. */
12186 token = cp_lexer_peek_token (parser->lexer);
12187 /* If it is `class' or `template', we have a type-parameter. */
12188 if (token->keyword == RID_TEMPLATE)
12189 return cp_parser_type_parameter (parser, is_parameter_pack);
12190 /* If it is `class' or `typename' we do not know yet whether it is a
12191 type parameter or a non-type parameter. Consider:
12193 template <typename T, typename T::X X> ...
12197 template <class C, class D*> ...
12199 Here, the first parameter is a type parameter, and the second is
12200 a non-type parameter. We can tell by looking at the token after
12201 the identifier -- if it is a `,', `=', or `>' then we have a type
12202 parameter. */
12203 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12205 /* Peek at the token after `class' or `typename'. */
12206 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12207 /* If it's an ellipsis, we have a template type parameter
12208 pack. */
12209 if (token->type == CPP_ELLIPSIS)
12210 return cp_parser_type_parameter (parser, is_parameter_pack);
12211 /* If it's an identifier, skip it. */
12212 if (token->type == CPP_NAME)
12213 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12214 /* Now, see if the token looks like the end of a template
12215 parameter. */
12216 if (token->type == CPP_COMMA
12217 || token->type == CPP_EQ
12218 || token->type == CPP_GREATER)
12219 return cp_parser_type_parameter (parser, is_parameter_pack);
12222 /* Otherwise, it is a non-type parameter.
12224 [temp.param]
12226 When parsing a default template-argument for a non-type
12227 template-parameter, the first non-nested `>' is taken as the end
12228 of the template parameter-list rather than a greater-than
12229 operator. */
12230 *is_non_type = true;
12231 parameter_declarator
12232 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12233 /*parenthesized_p=*/NULL);
12235 /* If the parameter declaration is marked as a parameter pack, set
12236 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12237 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12238 grokdeclarator. */
12239 if (parameter_declarator
12240 && parameter_declarator->declarator
12241 && parameter_declarator->declarator->parameter_pack_p)
12243 *is_parameter_pack = true;
12244 parameter_declarator->declarator->parameter_pack_p = false;
12247 if (parameter_declarator
12248 && parameter_declarator->default_argument)
12250 /* Can happen in some cases of erroneous input (c++/34892). */
12251 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12252 /* Consume the `...' for better error recovery. */
12253 cp_lexer_consume_token (parser->lexer);
12255 /* If the next token is an ellipsis, and we don't already have it
12256 marked as a parameter pack, then we have a parameter pack (that
12257 has no declarator). */
12258 else if (!*is_parameter_pack
12259 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12260 && (declarator_can_be_parameter_pack
12261 (parameter_declarator->declarator)))
12263 /* Consume the `...'. */
12264 cp_lexer_consume_token (parser->lexer);
12265 maybe_warn_variadic_templates ();
12267 *is_parameter_pack = true;
12269 /* We might end up with a pack expansion as the type of the non-type
12270 template parameter, in which case this is a non-type template
12271 parameter pack. */
12272 else if (parameter_declarator
12273 && parameter_declarator->decl_specifiers.type
12274 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12276 *is_parameter_pack = true;
12277 parameter_declarator->decl_specifiers.type =
12278 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12281 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12283 /* Parameter packs cannot have default arguments. However, a
12284 user may try to do so, so we'll parse them and give an
12285 appropriate diagnostic here. */
12287 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12289 /* Find the name of the parameter pack. */
12290 id_declarator = parameter_declarator->declarator;
12291 while (id_declarator && id_declarator->kind != cdk_id)
12292 id_declarator = id_declarator->declarator;
12294 if (id_declarator && id_declarator->kind == cdk_id)
12295 error_at (start_token->location,
12296 "template parameter pack %qD cannot have a default argument",
12297 id_declarator->u.id.unqualified_name);
12298 else
12299 error_at (start_token->location,
12300 "template parameter pack cannot have a default argument");
12302 /* Parse the default argument, but throw away the result. */
12303 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12306 parm = grokdeclarator (parameter_declarator->declarator,
12307 &parameter_declarator->decl_specifiers,
12308 TPARM, /*initialized=*/0,
12309 /*attrlist=*/NULL);
12310 if (parm == error_mark_node)
12311 return error_mark_node;
12313 return build_tree_list (parameter_declarator->default_argument, parm);
12316 /* Parse a type-parameter.
12318 type-parameter:
12319 class identifier [opt]
12320 class identifier [opt] = type-id
12321 typename identifier [opt]
12322 typename identifier [opt] = type-id
12323 template < template-parameter-list > class identifier [opt]
12324 template < template-parameter-list > class identifier [opt]
12325 = id-expression
12327 GNU Extension (variadic templates):
12329 type-parameter:
12330 class ... identifier [opt]
12331 typename ... identifier [opt]
12333 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12334 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12335 the declaration of the parameter.
12337 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12339 static tree
12340 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12342 cp_token *token;
12343 tree parameter;
12345 /* Look for a keyword to tell us what kind of parameter this is. */
12346 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12347 if (!token)
12348 return error_mark_node;
12350 switch (token->keyword)
12352 case RID_CLASS:
12353 case RID_TYPENAME:
12355 tree identifier;
12356 tree default_argument;
12358 /* If the next token is an ellipsis, we have a template
12359 argument pack. */
12360 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12362 /* Consume the `...' token. */
12363 cp_lexer_consume_token (parser->lexer);
12364 maybe_warn_variadic_templates ();
12366 *is_parameter_pack = true;
12369 /* If the next token is an identifier, then it names the
12370 parameter. */
12371 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12372 identifier = cp_parser_identifier (parser);
12373 else
12374 identifier = NULL_TREE;
12376 /* Create the parameter. */
12377 parameter = finish_template_type_parm (class_type_node, identifier);
12379 /* If the next token is an `=', we have a default argument. */
12380 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12382 /* Consume the `=' token. */
12383 cp_lexer_consume_token (parser->lexer);
12384 /* Parse the default-argument. */
12385 push_deferring_access_checks (dk_no_deferred);
12386 default_argument = cp_parser_type_id (parser);
12388 /* Template parameter packs cannot have default
12389 arguments. */
12390 if (*is_parameter_pack)
12392 if (identifier)
12393 error_at (token->location,
12394 "template parameter pack %qD cannot have a "
12395 "default argument", identifier);
12396 else
12397 error_at (token->location,
12398 "template parameter packs cannot have "
12399 "default arguments");
12400 default_argument = NULL_TREE;
12402 pop_deferring_access_checks ();
12404 else
12405 default_argument = NULL_TREE;
12407 /* Create the combined representation of the parameter and the
12408 default argument. */
12409 parameter = build_tree_list (default_argument, parameter);
12411 break;
12413 case RID_TEMPLATE:
12415 tree identifier;
12416 tree default_argument;
12418 /* Look for the `<'. */
12419 cp_parser_require (parser, CPP_LESS, RT_LESS);
12420 /* Parse the template-parameter-list. */
12421 cp_parser_template_parameter_list (parser);
12422 /* Look for the `>'. */
12423 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12424 /* Look for the `class' keyword. */
12425 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12426 /* If the next token is an ellipsis, we have a template
12427 argument pack. */
12428 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12430 /* Consume the `...' token. */
12431 cp_lexer_consume_token (parser->lexer);
12432 maybe_warn_variadic_templates ();
12434 *is_parameter_pack = true;
12436 /* If the next token is an `=', then there is a
12437 default-argument. If the next token is a `>', we are at
12438 the end of the parameter-list. If the next token is a `,',
12439 then we are at the end of this parameter. */
12440 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12441 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12442 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12444 identifier = cp_parser_identifier (parser);
12445 /* Treat invalid names as if the parameter were nameless. */
12446 if (identifier == error_mark_node)
12447 identifier = NULL_TREE;
12449 else
12450 identifier = NULL_TREE;
12452 /* Create the template parameter. */
12453 parameter = finish_template_template_parm (class_type_node,
12454 identifier);
12456 /* If the next token is an `=', then there is a
12457 default-argument. */
12458 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12460 bool is_template;
12462 /* Consume the `='. */
12463 cp_lexer_consume_token (parser->lexer);
12464 /* Parse the id-expression. */
12465 push_deferring_access_checks (dk_no_deferred);
12466 /* save token before parsing the id-expression, for error
12467 reporting */
12468 token = cp_lexer_peek_token (parser->lexer);
12469 default_argument
12470 = cp_parser_id_expression (parser,
12471 /*template_keyword_p=*/false,
12472 /*check_dependency_p=*/true,
12473 /*template_p=*/&is_template,
12474 /*declarator_p=*/false,
12475 /*optional_p=*/false);
12476 if (TREE_CODE (default_argument) == TYPE_DECL)
12477 /* If the id-expression was a template-id that refers to
12478 a template-class, we already have the declaration here,
12479 so no further lookup is needed. */
12481 else
12482 /* Look up the name. */
12483 default_argument
12484 = cp_parser_lookup_name (parser, default_argument,
12485 none_type,
12486 /*is_template=*/is_template,
12487 /*is_namespace=*/false,
12488 /*check_dependency=*/true,
12489 /*ambiguous_decls=*/NULL,
12490 token->location);
12491 /* See if the default argument is valid. */
12492 default_argument
12493 = check_template_template_default_arg (default_argument);
12495 /* Template parameter packs cannot have default
12496 arguments. */
12497 if (*is_parameter_pack)
12499 if (identifier)
12500 error_at (token->location,
12501 "template parameter pack %qD cannot "
12502 "have a default argument",
12503 identifier);
12504 else
12505 error_at (token->location, "template parameter packs cannot "
12506 "have default arguments");
12507 default_argument = NULL_TREE;
12509 pop_deferring_access_checks ();
12511 else
12512 default_argument = NULL_TREE;
12514 /* Create the combined representation of the parameter and the
12515 default argument. */
12516 parameter = build_tree_list (default_argument, parameter);
12518 break;
12520 default:
12521 gcc_unreachable ();
12522 break;
12525 return parameter;
12528 /* Parse a template-id.
12530 template-id:
12531 template-name < template-argument-list [opt] >
12533 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12534 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12535 returned. Otherwise, if the template-name names a function, or set
12536 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12537 names a class, returns a TYPE_DECL for the specialization.
12539 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12540 uninstantiated templates. */
12542 static tree
12543 cp_parser_template_id (cp_parser *parser,
12544 bool template_keyword_p,
12545 bool check_dependency_p,
12546 enum tag_types tag_type,
12547 bool is_declaration)
12549 int i;
12550 tree templ;
12551 tree arguments;
12552 tree template_id;
12553 cp_token_position start_of_id = 0;
12554 deferred_access_check *chk;
12555 vec<deferred_access_check, va_gc> *access_check;
12556 cp_token *next_token = NULL, *next_token_2 = NULL;
12557 bool is_identifier;
12559 /* If the next token corresponds to a template-id, there is no need
12560 to reparse it. */
12561 next_token = cp_lexer_peek_token (parser->lexer);
12562 if (next_token->type == CPP_TEMPLATE_ID)
12564 struct tree_check *check_value;
12566 /* Get the stored value. */
12567 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12568 /* Perform any access checks that were deferred. */
12569 access_check = check_value->checks;
12570 if (access_check)
12572 FOR_EACH_VEC_ELT (*access_check, i, chk)
12573 perform_or_defer_access_check (chk->binfo,
12574 chk->decl,
12575 chk->diag_decl,
12576 tf_warning_or_error);
12578 /* Return the stored value. */
12579 return check_value->value;
12582 /* Avoid performing name lookup if there is no possibility of
12583 finding a template-id. */
12584 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12585 || (next_token->type == CPP_NAME
12586 && !cp_parser_nth_token_starts_template_argument_list_p
12587 (parser, 2)))
12589 cp_parser_error (parser, "expected template-id");
12590 return error_mark_node;
12593 /* Remember where the template-id starts. */
12594 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12595 start_of_id = cp_lexer_token_position (parser->lexer, false);
12597 push_deferring_access_checks (dk_deferred);
12599 /* Parse the template-name. */
12600 is_identifier = false;
12601 templ = cp_parser_template_name (parser, template_keyword_p,
12602 check_dependency_p,
12603 is_declaration,
12604 tag_type,
12605 &is_identifier);
12606 if (templ == error_mark_node || is_identifier)
12608 pop_deferring_access_checks ();
12609 return templ;
12612 /* If we find the sequence `[:' after a template-name, it's probably
12613 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12614 parse correctly the argument list. */
12615 next_token = cp_lexer_peek_token (parser->lexer);
12616 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12617 if (next_token->type == CPP_OPEN_SQUARE
12618 && next_token->flags & DIGRAPH
12619 && next_token_2->type == CPP_COLON
12620 && !(next_token_2->flags & PREV_WHITE))
12622 cp_parser_parse_tentatively (parser);
12623 /* Change `:' into `::'. */
12624 next_token_2->type = CPP_SCOPE;
12625 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12626 CPP_LESS. */
12627 cp_lexer_consume_token (parser->lexer);
12629 /* Parse the arguments. */
12630 arguments = cp_parser_enclosed_template_argument_list (parser);
12631 if (!cp_parser_parse_definitely (parser))
12633 /* If we couldn't parse an argument list, then we revert our changes
12634 and return simply an error. Maybe this is not a template-id
12635 after all. */
12636 next_token_2->type = CPP_COLON;
12637 cp_parser_error (parser, "expected %<<%>");
12638 pop_deferring_access_checks ();
12639 return error_mark_node;
12641 /* Otherwise, emit an error about the invalid digraph, but continue
12642 parsing because we got our argument list. In C++11 do not emit
12643 any error, per 2.5/3. */
12644 if (cxx_dialect < cxx0x
12645 && permerror (next_token->location,
12646 "%<<::%> cannot begin a template-argument list"))
12648 static bool hint = false;
12649 inform (next_token->location,
12650 "%<<:%> is an alternate spelling for %<[%>."
12651 " Insert whitespace between %<<%> and %<::%>");
12652 if (!hint && !flag_permissive)
12654 inform (next_token->location, "(if you use %<-fpermissive%> "
12655 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
12656 "accept your code)");
12657 hint = true;
12661 else
12663 /* Look for the `<' that starts the template-argument-list. */
12664 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12666 pop_deferring_access_checks ();
12667 return error_mark_node;
12669 /* Parse the arguments. */
12670 arguments = cp_parser_enclosed_template_argument_list (parser);
12673 /* Build a representation of the specialization. */
12674 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12675 template_id = build_min_nt_loc (next_token->location,
12676 TEMPLATE_ID_EXPR,
12677 templ, arguments);
12678 else if (DECL_TYPE_TEMPLATE_P (templ)
12679 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12681 bool entering_scope;
12682 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12683 template (rather than some instantiation thereof) only if
12684 is not nested within some other construct. For example, in
12685 "template <typename T> void f(T) { A<T>::", A<T> is just an
12686 instantiation of A. */
12687 entering_scope = (template_parm_scope_p ()
12688 && cp_lexer_next_token_is (parser->lexer,
12689 CPP_SCOPE));
12690 template_id
12691 = finish_template_type (templ, arguments, entering_scope);
12693 else
12695 /* If it's not a class-template or a template-template, it should be
12696 a function-template. */
12697 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12698 || TREE_CODE (templ) == OVERLOAD
12699 || BASELINK_P (templ)));
12701 template_id = lookup_template_function (templ, arguments);
12704 /* If parsing tentatively, replace the sequence of tokens that makes
12705 up the template-id with a CPP_TEMPLATE_ID token. That way,
12706 should we re-parse the token stream, we will not have to repeat
12707 the effort required to do the parse, nor will we issue duplicate
12708 error messages about problems during instantiation of the
12709 template. */
12710 if (start_of_id)
12712 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12714 /* Reset the contents of the START_OF_ID token. */
12715 token->type = CPP_TEMPLATE_ID;
12716 /* Retrieve any deferred checks. Do not pop this access checks yet
12717 so the memory will not be reclaimed during token replacing below. */
12718 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12719 token->u.tree_check_value->value = template_id;
12720 token->u.tree_check_value->checks = get_deferred_access_checks ();
12721 token->keyword = RID_MAX;
12723 /* Purge all subsequent tokens. */
12724 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12726 /* ??? Can we actually assume that, if template_id ==
12727 error_mark_node, we will have issued a diagnostic to the
12728 user, as opposed to simply marking the tentative parse as
12729 failed? */
12730 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12731 error_at (token->location, "parse error in template argument list");
12734 pop_deferring_access_checks ();
12735 return template_id;
12738 /* Parse a template-name.
12740 template-name:
12741 identifier
12743 The standard should actually say:
12745 template-name:
12746 identifier
12747 operator-function-id
12749 A defect report has been filed about this issue.
12751 A conversion-function-id cannot be a template name because they cannot
12752 be part of a template-id. In fact, looking at this code:
12754 a.operator K<int>()
12756 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12757 It is impossible to call a templated conversion-function-id with an
12758 explicit argument list, since the only allowed template parameter is
12759 the type to which it is converting.
12761 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12762 `template' keyword, in a construction like:
12764 T::template f<3>()
12766 In that case `f' is taken to be a template-name, even though there
12767 is no way of knowing for sure.
12769 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12770 name refers to a set of overloaded functions, at least one of which
12771 is a template, or an IDENTIFIER_NODE with the name of the template,
12772 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12773 names are looked up inside uninstantiated templates. */
12775 static tree
12776 cp_parser_template_name (cp_parser* parser,
12777 bool template_keyword_p,
12778 bool check_dependency_p,
12779 bool is_declaration,
12780 enum tag_types tag_type,
12781 bool *is_identifier)
12783 tree identifier;
12784 tree decl;
12785 tree fns;
12786 cp_token *token = cp_lexer_peek_token (parser->lexer);
12788 /* If the next token is `operator', then we have either an
12789 operator-function-id or a conversion-function-id. */
12790 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12792 /* We don't know whether we're looking at an
12793 operator-function-id or a conversion-function-id. */
12794 cp_parser_parse_tentatively (parser);
12795 /* Try an operator-function-id. */
12796 identifier = cp_parser_operator_function_id (parser);
12797 /* If that didn't work, try a conversion-function-id. */
12798 if (!cp_parser_parse_definitely (parser))
12800 cp_parser_error (parser, "expected template-name");
12801 return error_mark_node;
12804 /* Look for the identifier. */
12805 else
12806 identifier = cp_parser_identifier (parser);
12808 /* If we didn't find an identifier, we don't have a template-id. */
12809 if (identifier == error_mark_node)
12810 return error_mark_node;
12812 /* If the name immediately followed the `template' keyword, then it
12813 is a template-name. However, if the next token is not `<', then
12814 we do not treat it as a template-name, since it is not being used
12815 as part of a template-id. This enables us to handle constructs
12816 like:
12818 template <typename T> struct S { S(); };
12819 template <typename T> S<T>::S();
12821 correctly. We would treat `S' as a template -- if it were `S<T>'
12822 -- but we do not if there is no `<'. */
12824 if (processing_template_decl
12825 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12827 /* In a declaration, in a dependent context, we pretend that the
12828 "template" keyword was present in order to improve error
12829 recovery. For example, given:
12831 template <typename T> void f(T::X<int>);
12833 we want to treat "X<int>" as a template-id. */
12834 if (is_declaration
12835 && !template_keyword_p
12836 && parser->scope && TYPE_P (parser->scope)
12837 && check_dependency_p
12838 && dependent_scope_p (parser->scope)
12839 /* Do not do this for dtors (or ctors), since they never
12840 need the template keyword before their name. */
12841 && !constructor_name_p (identifier, parser->scope))
12843 cp_token_position start = 0;
12845 /* Explain what went wrong. */
12846 error_at (token->location, "non-template %qD used as template",
12847 identifier);
12848 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12849 parser->scope, identifier);
12850 /* If parsing tentatively, find the location of the "<" token. */
12851 if (cp_parser_simulate_error (parser))
12852 start = cp_lexer_token_position (parser->lexer, true);
12853 /* Parse the template arguments so that we can issue error
12854 messages about them. */
12855 cp_lexer_consume_token (parser->lexer);
12856 cp_parser_enclosed_template_argument_list (parser);
12857 /* Skip tokens until we find a good place from which to
12858 continue parsing. */
12859 cp_parser_skip_to_closing_parenthesis (parser,
12860 /*recovering=*/true,
12861 /*or_comma=*/true,
12862 /*consume_paren=*/false);
12863 /* If parsing tentatively, permanently remove the
12864 template argument list. That will prevent duplicate
12865 error messages from being issued about the missing
12866 "template" keyword. */
12867 if (start)
12868 cp_lexer_purge_tokens_after (parser->lexer, start);
12869 if (is_identifier)
12870 *is_identifier = true;
12871 return identifier;
12874 /* If the "template" keyword is present, then there is generally
12875 no point in doing name-lookup, so we just return IDENTIFIER.
12876 But, if the qualifying scope is non-dependent then we can
12877 (and must) do name-lookup normally. */
12878 if (template_keyword_p
12879 && (!parser->scope
12880 || (TYPE_P (parser->scope)
12881 && dependent_type_p (parser->scope))))
12882 return identifier;
12885 /* Look up the name. */
12886 decl = cp_parser_lookup_name (parser, identifier,
12887 tag_type,
12888 /*is_template=*/true,
12889 /*is_namespace=*/false,
12890 check_dependency_p,
12891 /*ambiguous_decls=*/NULL,
12892 token->location);
12894 /* If DECL is a template, then the name was a template-name. */
12895 if (TREE_CODE (decl) == TEMPLATE_DECL)
12897 else
12899 tree fn = NULL_TREE;
12901 /* The standard does not explicitly indicate whether a name that
12902 names a set of overloaded declarations, some of which are
12903 templates, is a template-name. However, such a name should
12904 be a template-name; otherwise, there is no way to form a
12905 template-id for the overloaded templates. */
12906 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12907 if (TREE_CODE (fns) == OVERLOAD)
12908 for (fn = fns; fn; fn = OVL_NEXT (fn))
12909 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12910 break;
12912 if (!fn)
12914 /* The name does not name a template. */
12915 cp_parser_error (parser, "expected template-name");
12916 return error_mark_node;
12920 /* If DECL is dependent, and refers to a function, then just return
12921 its name; we will look it up again during template instantiation. */
12922 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12924 tree scope = ovl_scope (decl);
12925 if (TYPE_P (scope) && dependent_type_p (scope))
12926 return identifier;
12929 return decl;
12932 /* Parse a template-argument-list.
12934 template-argument-list:
12935 template-argument ... [opt]
12936 template-argument-list , template-argument ... [opt]
12938 Returns a TREE_VEC containing the arguments. */
12940 static tree
12941 cp_parser_template_argument_list (cp_parser* parser)
12943 tree fixed_args[10];
12944 unsigned n_args = 0;
12945 unsigned alloced = 10;
12946 tree *arg_ary = fixed_args;
12947 tree vec;
12948 bool saved_in_template_argument_list_p;
12949 bool saved_ice_p;
12950 bool saved_non_ice_p;
12952 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12953 parser->in_template_argument_list_p = true;
12954 /* Even if the template-id appears in an integral
12955 constant-expression, the contents of the argument list do
12956 not. */
12957 saved_ice_p = parser->integral_constant_expression_p;
12958 parser->integral_constant_expression_p = false;
12959 saved_non_ice_p = parser->non_integral_constant_expression_p;
12960 parser->non_integral_constant_expression_p = false;
12962 /* Parse the arguments. */
12965 tree argument;
12967 if (n_args)
12968 /* Consume the comma. */
12969 cp_lexer_consume_token (parser->lexer);
12971 /* Parse the template-argument. */
12972 argument = cp_parser_template_argument (parser);
12974 /* If the next token is an ellipsis, we're expanding a template
12975 argument pack. */
12976 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12978 if (argument == error_mark_node)
12980 cp_token *token = cp_lexer_peek_token (parser->lexer);
12981 error_at (token->location,
12982 "expected parameter pack before %<...%>");
12984 /* Consume the `...' token. */
12985 cp_lexer_consume_token (parser->lexer);
12987 /* Make the argument into a TYPE_PACK_EXPANSION or
12988 EXPR_PACK_EXPANSION. */
12989 argument = make_pack_expansion (argument);
12992 if (n_args == alloced)
12994 alloced *= 2;
12996 if (arg_ary == fixed_args)
12998 arg_ary = XNEWVEC (tree, alloced);
12999 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13001 else
13002 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13004 arg_ary[n_args++] = argument;
13006 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13008 vec = make_tree_vec (n_args);
13010 while (n_args--)
13011 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13013 if (arg_ary != fixed_args)
13014 free (arg_ary);
13015 parser->non_integral_constant_expression_p = saved_non_ice_p;
13016 parser->integral_constant_expression_p = saved_ice_p;
13017 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13018 #ifdef ENABLE_CHECKING
13019 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13020 #endif
13021 return vec;
13024 /* Parse a template-argument.
13026 template-argument:
13027 assignment-expression
13028 type-id
13029 id-expression
13031 The representation is that of an assignment-expression, type-id, or
13032 id-expression -- except that the qualified id-expression is
13033 evaluated, so that the value returned is either a DECL or an
13034 OVERLOAD.
13036 Although the standard says "assignment-expression", it forbids
13037 throw-expressions or assignments in the template argument.
13038 Therefore, we use "conditional-expression" instead. */
13040 static tree
13041 cp_parser_template_argument (cp_parser* parser)
13043 tree argument;
13044 bool template_p;
13045 bool address_p;
13046 bool maybe_type_id = false;
13047 cp_token *token = NULL, *argument_start_token = NULL;
13048 location_t loc = 0;
13049 cp_id_kind idk;
13051 /* There's really no way to know what we're looking at, so we just
13052 try each alternative in order.
13054 [temp.arg]
13056 In a template-argument, an ambiguity between a type-id and an
13057 expression is resolved to a type-id, regardless of the form of
13058 the corresponding template-parameter.
13060 Therefore, we try a type-id first. */
13061 cp_parser_parse_tentatively (parser);
13062 argument = cp_parser_template_type_arg (parser);
13063 /* If there was no error parsing the type-id but the next token is a
13064 '>>', our behavior depends on which dialect of C++ we're
13065 parsing. In C++98, we probably found a typo for '> >'. But there
13066 are type-id which are also valid expressions. For instance:
13068 struct X { int operator >> (int); };
13069 template <int V> struct Foo {};
13070 Foo<X () >> 5> r;
13072 Here 'X()' is a valid type-id of a function type, but the user just
13073 wanted to write the expression "X() >> 5". Thus, we remember that we
13074 found a valid type-id, but we still try to parse the argument as an
13075 expression to see what happens.
13077 In C++0x, the '>>' will be considered two separate '>'
13078 tokens. */
13079 if (!cp_parser_error_occurred (parser)
13080 && cxx_dialect == cxx98
13081 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
13083 maybe_type_id = true;
13084 cp_parser_abort_tentative_parse (parser);
13086 else
13088 /* If the next token isn't a `,' or a `>', then this argument wasn't
13089 really finished. This means that the argument is not a valid
13090 type-id. */
13091 if (!cp_parser_next_token_ends_template_argument_p (parser))
13092 cp_parser_error (parser, "expected template-argument");
13093 /* If that worked, we're done. */
13094 if (cp_parser_parse_definitely (parser))
13095 return argument;
13097 /* We're still not sure what the argument will be. */
13098 cp_parser_parse_tentatively (parser);
13099 /* Try a template. */
13100 argument_start_token = cp_lexer_peek_token (parser->lexer);
13101 argument = cp_parser_id_expression (parser,
13102 /*template_keyword_p=*/false,
13103 /*check_dependency_p=*/true,
13104 &template_p,
13105 /*declarator_p=*/false,
13106 /*optional_p=*/false);
13107 /* If the next token isn't a `,' or a `>', then this argument wasn't
13108 really finished. */
13109 if (!cp_parser_next_token_ends_template_argument_p (parser))
13110 cp_parser_error (parser, "expected template-argument");
13111 if (!cp_parser_error_occurred (parser))
13113 /* Figure out what is being referred to. If the id-expression
13114 was for a class template specialization, then we will have a
13115 TYPE_DECL at this point. There is no need to do name lookup
13116 at this point in that case. */
13117 if (TREE_CODE (argument) != TYPE_DECL)
13118 argument = cp_parser_lookup_name (parser, argument,
13119 none_type,
13120 /*is_template=*/template_p,
13121 /*is_namespace=*/false,
13122 /*check_dependency=*/true,
13123 /*ambiguous_decls=*/NULL,
13124 argument_start_token->location);
13125 if (TREE_CODE (argument) != TEMPLATE_DECL
13126 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
13127 cp_parser_error (parser, "expected template-name");
13129 if (cp_parser_parse_definitely (parser))
13130 return argument;
13131 /* It must be a non-type argument. There permitted cases are given
13132 in [temp.arg.nontype]:
13134 -- an integral constant-expression of integral or enumeration
13135 type; or
13137 -- the name of a non-type template-parameter; or
13139 -- the name of an object or function with external linkage...
13141 -- the address of an object or function with external linkage...
13143 -- a pointer to member... */
13144 /* Look for a non-type template parameter. */
13145 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13147 cp_parser_parse_tentatively (parser);
13148 argument = cp_parser_primary_expression (parser,
13149 /*address_p=*/false,
13150 /*cast_p=*/false,
13151 /*template_arg_p=*/true,
13152 &idk);
13153 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
13154 || !cp_parser_next_token_ends_template_argument_p (parser))
13155 cp_parser_simulate_error (parser);
13156 if (cp_parser_parse_definitely (parser))
13157 return argument;
13160 /* If the next token is "&", the argument must be the address of an
13161 object or function with external linkage. */
13162 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
13163 if (address_p)
13165 loc = cp_lexer_peek_token (parser->lexer)->location;
13166 cp_lexer_consume_token (parser->lexer);
13168 /* See if we might have an id-expression. */
13169 token = cp_lexer_peek_token (parser->lexer);
13170 if (token->type == CPP_NAME
13171 || token->keyword == RID_OPERATOR
13172 || token->type == CPP_SCOPE
13173 || token->type == CPP_TEMPLATE_ID
13174 || token->type == CPP_NESTED_NAME_SPECIFIER)
13176 cp_parser_parse_tentatively (parser);
13177 argument = cp_parser_primary_expression (parser,
13178 address_p,
13179 /*cast_p=*/false,
13180 /*template_arg_p=*/true,
13181 &idk);
13182 if (cp_parser_error_occurred (parser)
13183 || !cp_parser_next_token_ends_template_argument_p (parser))
13184 cp_parser_abort_tentative_parse (parser);
13185 else
13187 tree probe;
13189 if (TREE_CODE (argument) == INDIRECT_REF)
13191 gcc_assert (REFERENCE_REF_P (argument));
13192 argument = TREE_OPERAND (argument, 0);
13195 /* If we're in a template, we represent a qualified-id referring
13196 to a static data member as a SCOPE_REF even if the scope isn't
13197 dependent so that we can check access control later. */
13198 probe = argument;
13199 if (TREE_CODE (probe) == SCOPE_REF)
13200 probe = TREE_OPERAND (probe, 1);
13201 if (TREE_CODE (probe) == VAR_DECL)
13203 /* A variable without external linkage might still be a
13204 valid constant-expression, so no error is issued here
13205 if the external-linkage check fails. */
13206 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13207 cp_parser_simulate_error (parser);
13209 else if (is_overloaded_fn (argument))
13210 /* All overloaded functions are allowed; if the external
13211 linkage test does not pass, an error will be issued
13212 later. */
13214 else if (address_p
13215 && (TREE_CODE (argument) == OFFSET_REF
13216 || TREE_CODE (argument) == SCOPE_REF))
13217 /* A pointer-to-member. */
13219 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13221 else
13222 cp_parser_simulate_error (parser);
13224 if (cp_parser_parse_definitely (parser))
13226 if (address_p)
13227 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13228 tf_warning_or_error);
13229 return argument;
13233 /* If the argument started with "&", there are no other valid
13234 alternatives at this point. */
13235 if (address_p)
13237 cp_parser_error (parser, "invalid non-type template argument");
13238 return error_mark_node;
13241 /* If the argument wasn't successfully parsed as a type-id followed
13242 by '>>', the argument can only be a constant expression now.
13243 Otherwise, we try parsing the constant-expression tentatively,
13244 because the argument could really be a type-id. */
13245 if (maybe_type_id)
13246 cp_parser_parse_tentatively (parser);
13247 argument = cp_parser_constant_expression (parser,
13248 /*allow_non_constant_p=*/false,
13249 /*non_constant_p=*/NULL);
13250 argument = fold_non_dependent_expr (argument);
13251 if (!maybe_type_id)
13252 return argument;
13253 if (!cp_parser_next_token_ends_template_argument_p (parser))
13254 cp_parser_error (parser, "expected template-argument");
13255 if (cp_parser_parse_definitely (parser))
13256 return argument;
13257 /* We did our best to parse the argument as a non type-id, but that
13258 was the only alternative that matched (albeit with a '>' after
13259 it). We can assume it's just a typo from the user, and a
13260 diagnostic will then be issued. */
13261 return cp_parser_template_type_arg (parser);
13264 /* Parse an explicit-instantiation.
13266 explicit-instantiation:
13267 template declaration
13269 Although the standard says `declaration', what it really means is:
13271 explicit-instantiation:
13272 template decl-specifier-seq [opt] declarator [opt] ;
13274 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13275 supposed to be allowed. A defect report has been filed about this
13276 issue.
13278 GNU Extension:
13280 explicit-instantiation:
13281 storage-class-specifier template
13282 decl-specifier-seq [opt] declarator [opt] ;
13283 function-specifier template
13284 decl-specifier-seq [opt] declarator [opt] ; */
13286 static void
13287 cp_parser_explicit_instantiation (cp_parser* parser)
13289 int declares_class_or_enum;
13290 cp_decl_specifier_seq decl_specifiers;
13291 tree extension_specifier = NULL_TREE;
13293 timevar_push (TV_TEMPLATE_INST);
13295 /* Look for an (optional) storage-class-specifier or
13296 function-specifier. */
13297 if (cp_parser_allow_gnu_extensions_p (parser))
13299 extension_specifier
13300 = cp_parser_storage_class_specifier_opt (parser);
13301 if (!extension_specifier)
13302 extension_specifier
13303 = cp_parser_function_specifier_opt (parser,
13304 /*decl_specs=*/NULL);
13307 /* Look for the `template' keyword. */
13308 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13309 /* Let the front end know that we are processing an explicit
13310 instantiation. */
13311 begin_explicit_instantiation ();
13312 /* [temp.explicit] says that we are supposed to ignore access
13313 control while processing explicit instantiation directives. */
13314 push_deferring_access_checks (dk_no_check);
13315 /* Parse a decl-specifier-seq. */
13316 cp_parser_decl_specifier_seq (parser,
13317 CP_PARSER_FLAGS_OPTIONAL,
13318 &decl_specifiers,
13319 &declares_class_or_enum);
13320 /* If there was exactly one decl-specifier, and it declared a class,
13321 and there's no declarator, then we have an explicit type
13322 instantiation. */
13323 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13325 tree type;
13327 type = check_tag_decl (&decl_specifiers,
13328 /*explicit_type_instantiation_p=*/true);
13329 /* Turn access control back on for names used during
13330 template instantiation. */
13331 pop_deferring_access_checks ();
13332 if (type)
13333 do_type_instantiation (type, extension_specifier,
13334 /*complain=*/tf_error);
13336 else
13338 cp_declarator *declarator;
13339 tree decl;
13341 /* Parse the declarator. */
13342 declarator
13343 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13344 /*ctor_dtor_or_conv_p=*/NULL,
13345 /*parenthesized_p=*/NULL,
13346 /*member_p=*/false);
13347 if (declares_class_or_enum & 2)
13348 cp_parser_check_for_definition_in_return_type (declarator,
13349 decl_specifiers.type,
13350 decl_specifiers.locations[ds_type_spec]);
13351 if (declarator != cp_error_declarator)
13353 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13354 permerror (decl_specifiers.locations[ds_inline],
13355 "explicit instantiation shall not use"
13356 " %<inline%> specifier");
13357 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13358 permerror (decl_specifiers.locations[ds_constexpr],
13359 "explicit instantiation shall not use"
13360 " %<constexpr%> specifier");
13362 decl = grokdeclarator (declarator, &decl_specifiers,
13363 NORMAL, 0, &decl_specifiers.attributes);
13364 /* Turn access control back on for names used during
13365 template instantiation. */
13366 pop_deferring_access_checks ();
13367 /* Do the explicit instantiation. */
13368 do_decl_instantiation (decl, extension_specifier);
13370 else
13372 pop_deferring_access_checks ();
13373 /* Skip the body of the explicit instantiation. */
13374 cp_parser_skip_to_end_of_statement (parser);
13377 /* We're done with the instantiation. */
13378 end_explicit_instantiation ();
13380 cp_parser_consume_semicolon_at_end_of_statement (parser);
13382 timevar_pop (TV_TEMPLATE_INST);
13385 /* Parse an explicit-specialization.
13387 explicit-specialization:
13388 template < > declaration
13390 Although the standard says `declaration', what it really means is:
13392 explicit-specialization:
13393 template <> decl-specifier [opt] init-declarator [opt] ;
13394 template <> function-definition
13395 template <> explicit-specialization
13396 template <> template-declaration */
13398 static void
13399 cp_parser_explicit_specialization (cp_parser* parser)
13401 bool need_lang_pop;
13402 cp_token *token = cp_lexer_peek_token (parser->lexer);
13404 /* Look for the `template' keyword. */
13405 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13406 /* Look for the `<'. */
13407 cp_parser_require (parser, CPP_LESS, RT_LESS);
13408 /* Look for the `>'. */
13409 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13410 /* We have processed another parameter list. */
13411 ++parser->num_template_parameter_lists;
13412 /* [temp]
13414 A template ... explicit specialization ... shall not have C
13415 linkage. */
13416 if (current_lang_name == lang_name_c)
13418 error_at (token->location, "template specialization with C linkage");
13419 /* Give it C++ linkage to avoid confusing other parts of the
13420 front end. */
13421 push_lang_context (lang_name_cplusplus);
13422 need_lang_pop = true;
13424 else
13425 need_lang_pop = false;
13426 /* Let the front end know that we are beginning a specialization. */
13427 if (!begin_specialization ())
13429 end_specialization ();
13430 return;
13433 /* If the next keyword is `template', we need to figure out whether
13434 or not we're looking a template-declaration. */
13435 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13437 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13438 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13439 cp_parser_template_declaration_after_export (parser,
13440 /*member_p=*/false);
13441 else
13442 cp_parser_explicit_specialization (parser);
13444 else
13445 /* Parse the dependent declaration. */
13446 cp_parser_single_declaration (parser,
13447 /*checks=*/NULL,
13448 /*member_p=*/false,
13449 /*explicit_specialization_p=*/true,
13450 /*friend_p=*/NULL);
13451 /* We're done with the specialization. */
13452 end_specialization ();
13453 /* For the erroneous case of a template with C linkage, we pushed an
13454 implicit C++ linkage scope; exit that scope now. */
13455 if (need_lang_pop)
13456 pop_lang_context ();
13457 /* We're done with this parameter list. */
13458 --parser->num_template_parameter_lists;
13461 /* Parse a type-specifier.
13463 type-specifier:
13464 simple-type-specifier
13465 class-specifier
13466 enum-specifier
13467 elaborated-type-specifier
13468 cv-qualifier
13470 GNU Extension:
13472 type-specifier:
13473 __complex__
13475 Returns a representation of the type-specifier. For a
13476 class-specifier, enum-specifier, or elaborated-type-specifier, a
13477 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13479 The parser flags FLAGS is used to control type-specifier parsing.
13481 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13482 in a decl-specifier-seq.
13484 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13485 class-specifier, enum-specifier, or elaborated-type-specifier, then
13486 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13487 if a type is declared; 2 if it is defined. Otherwise, it is set to
13488 zero.
13490 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13491 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13492 is set to FALSE. */
13494 static tree
13495 cp_parser_type_specifier (cp_parser* parser,
13496 cp_parser_flags flags,
13497 cp_decl_specifier_seq *decl_specs,
13498 bool is_declaration,
13499 int* declares_class_or_enum,
13500 bool* is_cv_qualifier)
13502 tree type_spec = NULL_TREE;
13503 cp_token *token;
13504 enum rid keyword;
13505 cp_decl_spec ds = ds_last;
13507 /* Assume this type-specifier does not declare a new type. */
13508 if (declares_class_or_enum)
13509 *declares_class_or_enum = 0;
13510 /* And that it does not specify a cv-qualifier. */
13511 if (is_cv_qualifier)
13512 *is_cv_qualifier = false;
13513 /* Peek at the next token. */
13514 token = cp_lexer_peek_token (parser->lexer);
13516 /* If we're looking at a keyword, we can use that to guide the
13517 production we choose. */
13518 keyword = token->keyword;
13519 switch (keyword)
13521 case RID_ENUM:
13522 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13523 goto elaborated_type_specifier;
13525 /* Look for the enum-specifier. */
13526 type_spec = cp_parser_enum_specifier (parser);
13527 /* If that worked, we're done. */
13528 if (type_spec)
13530 if (declares_class_or_enum)
13531 *declares_class_or_enum = 2;
13532 if (decl_specs)
13533 cp_parser_set_decl_spec_type (decl_specs,
13534 type_spec,
13535 token,
13536 /*type_definition_p=*/true);
13537 return type_spec;
13539 else
13540 goto elaborated_type_specifier;
13542 /* Any of these indicate either a class-specifier, or an
13543 elaborated-type-specifier. */
13544 case RID_CLASS:
13545 case RID_STRUCT:
13546 case RID_UNION:
13547 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13548 goto elaborated_type_specifier;
13550 /* Parse tentatively so that we can back up if we don't find a
13551 class-specifier. */
13552 cp_parser_parse_tentatively (parser);
13553 /* Look for the class-specifier. */
13554 type_spec = cp_parser_class_specifier (parser);
13555 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13556 /* If that worked, we're done. */
13557 if (cp_parser_parse_definitely (parser))
13559 if (declares_class_or_enum)
13560 *declares_class_or_enum = 2;
13561 if (decl_specs)
13562 cp_parser_set_decl_spec_type (decl_specs,
13563 type_spec,
13564 token,
13565 /*type_definition_p=*/true);
13566 return type_spec;
13569 /* Fall through. */
13570 elaborated_type_specifier:
13571 /* We're declaring (not defining) a class or enum. */
13572 if (declares_class_or_enum)
13573 *declares_class_or_enum = 1;
13575 /* Fall through. */
13576 case RID_TYPENAME:
13577 /* Look for an elaborated-type-specifier. */
13578 type_spec
13579 = (cp_parser_elaborated_type_specifier
13580 (parser,
13581 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
13582 is_declaration));
13583 if (decl_specs)
13584 cp_parser_set_decl_spec_type (decl_specs,
13585 type_spec,
13586 token,
13587 /*type_definition_p=*/false);
13588 return type_spec;
13590 case RID_CONST:
13591 ds = ds_const;
13592 if (is_cv_qualifier)
13593 *is_cv_qualifier = true;
13594 break;
13596 case RID_VOLATILE:
13597 ds = ds_volatile;
13598 if (is_cv_qualifier)
13599 *is_cv_qualifier = true;
13600 break;
13602 case RID_RESTRICT:
13603 ds = ds_restrict;
13604 if (is_cv_qualifier)
13605 *is_cv_qualifier = true;
13606 break;
13608 case RID_COMPLEX:
13609 /* The `__complex__' keyword is a GNU extension. */
13610 ds = ds_complex;
13611 break;
13613 default:
13614 break;
13617 /* Handle simple keywords. */
13618 if (ds != ds_last)
13620 if (decl_specs)
13622 set_and_check_decl_spec_loc (decl_specs, ds, token);
13623 decl_specs->any_specifiers_p = true;
13625 return cp_lexer_consume_token (parser->lexer)->u.value;
13628 /* If we do not already have a type-specifier, assume we are looking
13629 at a simple-type-specifier. */
13630 type_spec = cp_parser_simple_type_specifier (parser,
13631 decl_specs,
13632 flags);
13634 /* If we didn't find a type-specifier, and a type-specifier was not
13635 optional in this context, issue an error message. */
13636 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13638 cp_parser_error (parser, "expected type specifier");
13639 return error_mark_node;
13642 return type_spec;
13645 /* Parse a simple-type-specifier.
13647 simple-type-specifier:
13648 :: [opt] nested-name-specifier [opt] type-name
13649 :: [opt] nested-name-specifier template template-id
13650 char
13651 wchar_t
13652 bool
13653 short
13655 long
13656 signed
13657 unsigned
13658 float
13659 double
13660 void
13662 C++0x Extension:
13664 simple-type-specifier:
13665 auto
13666 decltype ( expression )
13667 char16_t
13668 char32_t
13669 __underlying_type ( type-id )
13671 GNU Extension:
13673 simple-type-specifier:
13674 __int128
13675 __typeof__ unary-expression
13676 __typeof__ ( type-id )
13678 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13679 appropriately updated. */
13681 static tree
13682 cp_parser_simple_type_specifier (cp_parser* parser,
13683 cp_decl_specifier_seq *decl_specs,
13684 cp_parser_flags flags)
13686 tree type = NULL_TREE;
13687 cp_token *token;
13689 /* Peek at the next token. */
13690 token = cp_lexer_peek_token (parser->lexer);
13692 /* If we're looking at a keyword, things are easy. */
13693 switch (token->keyword)
13695 case RID_CHAR:
13696 if (decl_specs)
13697 decl_specs->explicit_char_p = true;
13698 type = char_type_node;
13699 break;
13700 case RID_CHAR16:
13701 type = char16_type_node;
13702 break;
13703 case RID_CHAR32:
13704 type = char32_type_node;
13705 break;
13706 case RID_WCHAR:
13707 type = wchar_type_node;
13708 break;
13709 case RID_BOOL:
13710 type = boolean_type_node;
13711 break;
13712 case RID_SHORT:
13713 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
13714 type = short_integer_type_node;
13715 break;
13716 case RID_INT:
13717 if (decl_specs)
13718 decl_specs->explicit_int_p = true;
13719 type = integer_type_node;
13720 break;
13721 case RID_INT128:
13722 if (!int128_integer_type_node)
13723 break;
13724 if (decl_specs)
13725 decl_specs->explicit_int128_p = true;
13726 type = int128_integer_type_node;
13727 break;
13728 case RID_LONG:
13729 if (decl_specs)
13730 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
13731 type = long_integer_type_node;
13732 break;
13733 case RID_SIGNED:
13734 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
13735 type = integer_type_node;
13736 break;
13737 case RID_UNSIGNED:
13738 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
13739 type = unsigned_type_node;
13740 break;
13741 case RID_FLOAT:
13742 type = float_type_node;
13743 break;
13744 case RID_DOUBLE:
13745 type = double_type_node;
13746 break;
13747 case RID_VOID:
13748 type = void_type_node;
13749 break;
13751 case RID_AUTO:
13752 maybe_warn_cpp0x (CPP0X_AUTO);
13753 type = make_auto ();
13754 break;
13756 case RID_DECLTYPE:
13757 /* Since DR 743, decltype can either be a simple-type-specifier by
13758 itself or begin a nested-name-specifier. Parsing it will replace
13759 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13760 handling below decide what to do. */
13761 cp_parser_decltype (parser);
13762 cp_lexer_set_token_position (parser->lexer, token);
13763 break;
13765 case RID_TYPEOF:
13766 /* Consume the `typeof' token. */
13767 cp_lexer_consume_token (parser->lexer);
13768 /* Parse the operand to `typeof'. */
13769 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13770 /* If it is not already a TYPE, take its type. */
13771 if (!TYPE_P (type))
13772 type = finish_typeof (type);
13774 if (decl_specs)
13775 cp_parser_set_decl_spec_type (decl_specs, type,
13776 token,
13777 /*type_definition_p=*/false);
13779 return type;
13781 case RID_UNDERLYING_TYPE:
13782 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13783 if (decl_specs)
13784 cp_parser_set_decl_spec_type (decl_specs, type,
13785 token,
13786 /*type_definition_p=*/false);
13788 return type;
13790 case RID_BASES:
13791 case RID_DIRECT_BASES:
13792 type = cp_parser_trait_expr (parser, token->keyword);
13793 if (decl_specs)
13794 cp_parser_set_decl_spec_type (decl_specs, type,
13795 token,
13796 /*type_definition_p=*/false);
13797 return type;
13798 default:
13799 break;
13802 /* If token is an already-parsed decltype not followed by ::,
13803 it's a simple-type-specifier. */
13804 if (token->type == CPP_DECLTYPE
13805 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13807 type = token->u.value;
13808 if (decl_specs)
13809 cp_parser_set_decl_spec_type (decl_specs, type,
13810 token,
13811 /*type_definition_p=*/false);
13812 cp_lexer_consume_token (parser->lexer);
13813 return type;
13816 /* If the type-specifier was for a built-in type, we're done. */
13817 if (type)
13819 /* Record the type. */
13820 if (decl_specs
13821 && (token->keyword != RID_SIGNED
13822 && token->keyword != RID_UNSIGNED
13823 && token->keyword != RID_SHORT
13824 && token->keyword != RID_LONG))
13825 cp_parser_set_decl_spec_type (decl_specs,
13826 type,
13827 token,
13828 /*type_definition_p=*/false);
13829 if (decl_specs)
13830 decl_specs->any_specifiers_p = true;
13832 /* Consume the token. */
13833 cp_lexer_consume_token (parser->lexer);
13835 /* There is no valid C++ program where a non-template type is
13836 followed by a "<". That usually indicates that the user thought
13837 that the type was a template. */
13838 cp_parser_check_for_invalid_template_id (parser, type, none_type,
13839 token->location);
13841 return TYPE_NAME (type);
13844 /* The type-specifier must be a user-defined type. */
13845 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13847 bool qualified_p;
13848 bool global_p;
13850 /* Don't gobble tokens or issue error messages if this is an
13851 optional type-specifier. */
13852 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13853 cp_parser_parse_tentatively (parser);
13855 /* Look for the optional `::' operator. */
13856 global_p
13857 = (cp_parser_global_scope_opt (parser,
13858 /*current_scope_valid_p=*/false)
13859 != NULL_TREE);
13860 /* Look for the nested-name specifier. */
13861 qualified_p
13862 = (cp_parser_nested_name_specifier_opt (parser,
13863 /*typename_keyword_p=*/false,
13864 /*check_dependency_p=*/true,
13865 /*type_p=*/false,
13866 /*is_declaration=*/false)
13867 != NULL_TREE);
13868 token = cp_lexer_peek_token (parser->lexer);
13869 /* If we have seen a nested-name-specifier, and the next token
13870 is `template', then we are using the template-id production. */
13871 if (parser->scope
13872 && cp_parser_optional_template_keyword (parser))
13874 /* Look for the template-id. */
13875 type = cp_parser_template_id (parser,
13876 /*template_keyword_p=*/true,
13877 /*check_dependency_p=*/true,
13878 none_type,
13879 /*is_declaration=*/false);
13880 /* If the template-id did not name a type, we are out of
13881 luck. */
13882 if (TREE_CODE (type) != TYPE_DECL)
13884 cp_parser_error (parser, "expected template-id for type");
13885 type = NULL_TREE;
13888 /* Otherwise, look for a type-name. */
13889 else
13890 type = cp_parser_type_name (parser);
13891 /* Keep track of all name-lookups performed in class scopes. */
13892 if (type
13893 && !global_p
13894 && !qualified_p
13895 && TREE_CODE (type) == TYPE_DECL
13896 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13897 maybe_note_name_used_in_class (DECL_NAME (type), type);
13898 /* If it didn't work out, we don't have a TYPE. */
13899 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13900 && !cp_parser_parse_definitely (parser))
13901 type = NULL_TREE;
13902 if (type && decl_specs)
13903 cp_parser_set_decl_spec_type (decl_specs, type,
13904 token,
13905 /*type_definition_p=*/false);
13908 /* If we didn't get a type-name, issue an error message. */
13909 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13911 cp_parser_error (parser, "expected type-name");
13912 return error_mark_node;
13915 if (type && type != error_mark_node)
13917 /* See if TYPE is an Objective-C type, and if so, parse and
13918 accept any protocol references following it. Do this before
13919 the cp_parser_check_for_invalid_template_id() call, because
13920 Objective-C types can be followed by '<...>' which would
13921 enclose protocol names rather than template arguments, and so
13922 everything is fine. */
13923 if (c_dialect_objc () && !parser->scope
13924 && (objc_is_id (type) || objc_is_class_name (type)))
13926 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13927 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13929 /* Clobber the "unqualified" type previously entered into
13930 DECL_SPECS with the new, improved protocol-qualified version. */
13931 if (decl_specs)
13932 decl_specs->type = qual_type;
13934 return qual_type;
13937 /* There is no valid C++ program where a non-template type is
13938 followed by a "<". That usually indicates that the user
13939 thought that the type was a template. */
13940 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13941 none_type,
13942 token->location);
13945 return type;
13948 /* Parse a type-name.
13950 type-name:
13951 class-name
13952 enum-name
13953 typedef-name
13954 simple-template-id [in c++0x]
13956 enum-name:
13957 identifier
13959 typedef-name:
13960 identifier
13962 Returns a TYPE_DECL for the type. */
13964 static tree
13965 cp_parser_type_name (cp_parser* parser)
13967 tree type_decl;
13969 /* We can't know yet whether it is a class-name or not. */
13970 cp_parser_parse_tentatively (parser);
13971 /* Try a class-name. */
13972 type_decl = cp_parser_class_name (parser,
13973 /*typename_keyword_p=*/false,
13974 /*template_keyword_p=*/false,
13975 none_type,
13976 /*check_dependency_p=*/true,
13977 /*class_head_p=*/false,
13978 /*is_declaration=*/false);
13979 /* If it's not a class-name, keep looking. */
13980 if (!cp_parser_parse_definitely (parser))
13982 if (cxx_dialect < cxx0x)
13983 /* It must be a typedef-name or an enum-name. */
13984 return cp_parser_nonclass_name (parser);
13986 cp_parser_parse_tentatively (parser);
13987 /* It is either a simple-template-id representing an
13988 instantiation of an alias template... */
13989 type_decl = cp_parser_template_id (parser,
13990 /*template_keyword_p=*/false,
13991 /*check_dependency_p=*/false,
13992 none_type,
13993 /*is_declaration=*/false);
13994 /* Note that this must be an instantiation of an alias template
13995 because [temp.names]/6 says:
13997 A template-id that names an alias template specialization
13998 is a type-name.
14000 Whereas [temp.names]/7 says:
14002 A simple-template-id that names a class template
14003 specialization is a class-name. */
14004 if (type_decl != NULL_TREE
14005 && TREE_CODE (type_decl) == TYPE_DECL
14006 && TYPE_DECL_ALIAS_P (type_decl))
14007 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
14008 else
14009 cp_parser_simulate_error (parser);
14011 if (!cp_parser_parse_definitely (parser))
14012 /* ... Or a typedef-name or an enum-name. */
14013 return cp_parser_nonclass_name (parser);
14016 return type_decl;
14019 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
14021 enum-name:
14022 identifier
14024 typedef-name:
14025 identifier
14027 Returns a TYPE_DECL for the type. */
14029 static tree
14030 cp_parser_nonclass_name (cp_parser* parser)
14032 tree type_decl;
14033 tree identifier;
14035 cp_token *token = cp_lexer_peek_token (parser->lexer);
14036 identifier = cp_parser_identifier (parser);
14037 if (identifier == error_mark_node)
14038 return error_mark_node;
14040 /* Look up the type-name. */
14041 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
14043 if (TREE_CODE (type_decl) == USING_DECL)
14045 if (!DECL_DEPENDENT_P (type_decl))
14046 type_decl = strip_using_decl (type_decl);
14047 else if (USING_DECL_TYPENAME_P (type_decl))
14049 /* We have found a type introduced by a using
14050 declaration at class scope that refers to a dependent
14051 type.
14053 using typename :: [opt] nested-name-specifier unqualified-id ;
14055 type_decl = make_typename_type (TREE_TYPE (type_decl),
14056 DECL_NAME (type_decl),
14057 typename_type, tf_error);
14058 if (type_decl != error_mark_node)
14059 type_decl = TYPE_NAME (type_decl);
14063 if (TREE_CODE (type_decl) != TYPE_DECL
14064 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
14066 /* See if this is an Objective-C type. */
14067 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14068 tree type = objc_get_protocol_qualified_type (identifier, protos);
14069 if (type)
14070 type_decl = TYPE_NAME (type);
14073 /* Issue an error if we did not find a type-name. */
14074 if (TREE_CODE (type_decl) != TYPE_DECL
14075 /* In Objective-C, we have the complication that class names are
14076 normally type names and start declarations (eg, the
14077 "NSObject" in "NSObject *object;"), but can be used in an
14078 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
14079 is an expression. So, a classname followed by a dot is not a
14080 valid type-name. */
14081 || (objc_is_class_name (TREE_TYPE (type_decl))
14082 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
14084 if (!cp_parser_simulate_error (parser))
14085 cp_parser_name_lookup_error (parser, identifier, type_decl,
14086 NLE_TYPE, token->location);
14087 return error_mark_node;
14089 /* Remember that the name was used in the definition of the
14090 current class so that we can check later to see if the
14091 meaning would have been different after the class was
14092 entirely defined. */
14093 else if (type_decl != error_mark_node
14094 && !parser->scope)
14095 maybe_note_name_used_in_class (identifier, type_decl);
14097 return type_decl;
14100 /* Parse an elaborated-type-specifier. Note that the grammar given
14101 here incorporates the resolution to DR68.
14103 elaborated-type-specifier:
14104 class-key :: [opt] nested-name-specifier [opt] identifier
14105 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
14106 enum-key :: [opt] nested-name-specifier [opt] identifier
14107 typename :: [opt] nested-name-specifier identifier
14108 typename :: [opt] nested-name-specifier template [opt]
14109 template-id
14111 GNU extension:
14113 elaborated-type-specifier:
14114 class-key attributes :: [opt] nested-name-specifier [opt] identifier
14115 class-key attributes :: [opt] nested-name-specifier [opt]
14116 template [opt] template-id
14117 enum attributes :: [opt] nested-name-specifier [opt] identifier
14119 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
14120 declared `friend'. If IS_DECLARATION is TRUE, then this
14121 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
14122 something is being declared.
14124 Returns the TYPE specified. */
14126 static tree
14127 cp_parser_elaborated_type_specifier (cp_parser* parser,
14128 bool is_friend,
14129 bool is_declaration)
14131 enum tag_types tag_type;
14132 tree identifier;
14133 tree type = NULL_TREE;
14134 tree attributes = NULL_TREE;
14135 tree globalscope;
14136 cp_token *token = NULL;
14138 /* See if we're looking at the `enum' keyword. */
14139 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
14141 /* Consume the `enum' token. */
14142 cp_lexer_consume_token (parser->lexer);
14143 /* Remember that it's an enumeration type. */
14144 tag_type = enum_type;
14145 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
14146 enums) is used here. */
14147 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14148 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14150 pedwarn (input_location, 0, "elaborated-type-specifier "
14151 "for a scoped enum must not use the %<%D%> keyword",
14152 cp_lexer_peek_token (parser->lexer)->u.value);
14153 /* Consume the `struct' or `class' and parse it anyway. */
14154 cp_lexer_consume_token (parser->lexer);
14156 /* Parse the attributes. */
14157 attributes = cp_parser_attributes_opt (parser);
14159 /* Or, it might be `typename'. */
14160 else if (cp_lexer_next_token_is_keyword (parser->lexer,
14161 RID_TYPENAME))
14163 /* Consume the `typename' token. */
14164 cp_lexer_consume_token (parser->lexer);
14165 /* Remember that it's a `typename' type. */
14166 tag_type = typename_type;
14168 /* Otherwise it must be a class-key. */
14169 else
14171 tag_type = cp_parser_class_key (parser);
14172 if (tag_type == none_type)
14173 return error_mark_node;
14174 /* Parse the attributes. */
14175 attributes = cp_parser_attributes_opt (parser);
14178 /* Look for the `::' operator. */
14179 globalscope = cp_parser_global_scope_opt (parser,
14180 /*current_scope_valid_p=*/false);
14181 /* Look for the nested-name-specifier. */
14182 if (tag_type == typename_type && !globalscope)
14184 if (!cp_parser_nested_name_specifier (parser,
14185 /*typename_keyword_p=*/true,
14186 /*check_dependency_p=*/true,
14187 /*type_p=*/true,
14188 is_declaration))
14189 return error_mark_node;
14191 else
14192 /* Even though `typename' is not present, the proposed resolution
14193 to Core Issue 180 says that in `class A<T>::B', `B' should be
14194 considered a type-name, even if `A<T>' is dependent. */
14195 cp_parser_nested_name_specifier_opt (parser,
14196 /*typename_keyword_p=*/true,
14197 /*check_dependency_p=*/true,
14198 /*type_p=*/true,
14199 is_declaration);
14200 /* For everything but enumeration types, consider a template-id.
14201 For an enumeration type, consider only a plain identifier. */
14202 if (tag_type != enum_type)
14204 bool template_p = false;
14205 tree decl;
14207 /* Allow the `template' keyword. */
14208 template_p = cp_parser_optional_template_keyword (parser);
14209 /* If we didn't see `template', we don't know if there's a
14210 template-id or not. */
14211 if (!template_p)
14212 cp_parser_parse_tentatively (parser);
14213 /* Parse the template-id. */
14214 token = cp_lexer_peek_token (parser->lexer);
14215 decl = cp_parser_template_id (parser, template_p,
14216 /*check_dependency_p=*/true,
14217 tag_type,
14218 is_declaration);
14219 /* If we didn't find a template-id, look for an ordinary
14220 identifier. */
14221 if (!template_p && !cp_parser_parse_definitely (parser))
14223 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14224 in effect, then we must assume that, upon instantiation, the
14225 template will correspond to a class. */
14226 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14227 && tag_type == typename_type)
14228 type = make_typename_type (parser->scope, decl,
14229 typename_type,
14230 /*complain=*/tf_error);
14231 /* If the `typename' keyword is in effect and DECL is not a type
14232 decl. Then type is non existant. */
14233 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14234 type = NULL_TREE;
14235 else
14236 type = check_elaborated_type_specifier (tag_type, decl,
14237 /*allow_template_p=*/true);
14240 if (!type)
14242 token = cp_lexer_peek_token (parser->lexer);
14243 identifier = cp_parser_identifier (parser);
14245 if (identifier == error_mark_node)
14247 parser->scope = NULL_TREE;
14248 return error_mark_node;
14251 /* For a `typename', we needn't call xref_tag. */
14252 if (tag_type == typename_type
14253 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14254 return cp_parser_make_typename_type (parser, parser->scope,
14255 identifier,
14256 token->location);
14257 /* Look up a qualified name in the usual way. */
14258 if (parser->scope)
14260 tree decl;
14261 tree ambiguous_decls;
14263 decl = cp_parser_lookup_name (parser, identifier,
14264 tag_type,
14265 /*is_template=*/false,
14266 /*is_namespace=*/false,
14267 /*check_dependency=*/true,
14268 &ambiguous_decls,
14269 token->location);
14271 /* If the lookup was ambiguous, an error will already have been
14272 issued. */
14273 if (ambiguous_decls)
14274 return error_mark_node;
14276 /* If we are parsing friend declaration, DECL may be a
14277 TEMPLATE_DECL tree node here. However, we need to check
14278 whether this TEMPLATE_DECL results in valid code. Consider
14279 the following example:
14281 namespace N {
14282 template <class T> class C {};
14284 class X {
14285 template <class T> friend class N::C; // #1, valid code
14287 template <class T> class Y {
14288 friend class N::C; // #2, invalid code
14291 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14292 name lookup of `N::C'. We see that friend declaration must
14293 be template for the code to be valid. Note that
14294 processing_template_decl does not work here since it is
14295 always 1 for the above two cases. */
14297 decl = (cp_parser_maybe_treat_template_as_class
14298 (decl, /*tag_name_p=*/is_friend
14299 && parser->num_template_parameter_lists));
14301 if (TREE_CODE (decl) != TYPE_DECL)
14303 cp_parser_diagnose_invalid_type_name (parser,
14304 parser->scope,
14305 identifier,
14306 token->location);
14307 return error_mark_node;
14310 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14312 bool allow_template = (parser->num_template_parameter_lists
14313 || DECL_SELF_REFERENCE_P (decl));
14314 type = check_elaborated_type_specifier (tag_type, decl,
14315 allow_template);
14317 if (type == error_mark_node)
14318 return error_mark_node;
14321 /* Forward declarations of nested types, such as
14323 class C1::C2;
14324 class C1::C2::C3;
14326 are invalid unless all components preceding the final '::'
14327 are complete. If all enclosing types are complete, these
14328 declarations become merely pointless.
14330 Invalid forward declarations of nested types are errors
14331 caught elsewhere in parsing. Those that are pointless arrive
14332 here. */
14334 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14335 && !is_friend && !processing_explicit_instantiation)
14336 warning (0, "declaration %qD does not declare anything", decl);
14338 type = TREE_TYPE (decl);
14340 else
14342 /* An elaborated-type-specifier sometimes introduces a new type and
14343 sometimes names an existing type. Normally, the rule is that it
14344 introduces a new type only if there is not an existing type of
14345 the same name already in scope. For example, given:
14347 struct S {};
14348 void f() { struct S s; }
14350 the `struct S' in the body of `f' is the same `struct S' as in
14351 the global scope; the existing definition is used. However, if
14352 there were no global declaration, this would introduce a new
14353 local class named `S'.
14355 An exception to this rule applies to the following code:
14357 namespace N { struct S; }
14359 Here, the elaborated-type-specifier names a new type
14360 unconditionally; even if there is already an `S' in the
14361 containing scope this declaration names a new type.
14362 This exception only applies if the elaborated-type-specifier
14363 forms the complete declaration:
14365 [class.name]
14367 A declaration consisting solely of `class-key identifier ;' is
14368 either a redeclaration of the name in the current scope or a
14369 forward declaration of the identifier as a class name. It
14370 introduces the name into the current scope.
14372 We are in this situation precisely when the next token is a `;'.
14374 An exception to the exception is that a `friend' declaration does
14375 *not* name a new type; i.e., given:
14377 struct S { friend struct T; };
14379 `T' is not a new type in the scope of `S'.
14381 Also, `new struct S' or `sizeof (struct S)' never results in the
14382 definition of a new type; a new type can only be declared in a
14383 declaration context. */
14385 tag_scope ts;
14386 bool template_p;
14388 if (is_friend)
14389 /* Friends have special name lookup rules. */
14390 ts = ts_within_enclosing_non_class;
14391 else if (is_declaration
14392 && cp_lexer_next_token_is (parser->lexer,
14393 CPP_SEMICOLON))
14394 /* This is a `class-key identifier ;' */
14395 ts = ts_current;
14396 else
14397 ts = ts_global;
14399 template_p =
14400 (parser->num_template_parameter_lists
14401 && (cp_parser_next_token_starts_class_definition_p (parser)
14402 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14403 /* An unqualified name was used to reference this type, so
14404 there were no qualifying templates. */
14405 if (!cp_parser_check_template_parameters (parser,
14406 /*num_templates=*/0,
14407 token->location,
14408 /*declarator=*/NULL))
14409 return error_mark_node;
14410 type = xref_tag (tag_type, identifier, ts, template_p);
14414 if (type == error_mark_node)
14415 return error_mark_node;
14417 /* Allow attributes on forward declarations of classes. */
14418 if (attributes)
14420 if (TREE_CODE (type) == TYPENAME_TYPE)
14421 warning (OPT_Wattributes,
14422 "attributes ignored on uninstantiated type");
14423 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14424 && ! processing_explicit_instantiation)
14425 warning (OPT_Wattributes,
14426 "attributes ignored on template instantiation");
14427 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14428 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14429 else
14430 warning (OPT_Wattributes,
14431 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14434 if (tag_type != enum_type)
14436 /* Indicate whether this class was declared as a `class' or as a
14437 `struct'. */
14438 if (TREE_CODE (type) == RECORD_TYPE)
14439 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14440 cp_parser_check_class_key (tag_type, type);
14443 /* A "<" cannot follow an elaborated type specifier. If that
14444 happens, the user was probably trying to form a template-id. */
14445 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
14446 token->location);
14448 return type;
14451 /* Parse an enum-specifier.
14453 enum-specifier:
14454 enum-head { enumerator-list [opt] }
14455 enum-head { enumerator-list , } [C++0x]
14457 enum-head:
14458 enum-key identifier [opt] enum-base [opt]
14459 enum-key nested-name-specifier identifier enum-base [opt]
14461 enum-key:
14462 enum
14463 enum class [C++0x]
14464 enum struct [C++0x]
14466 enum-base: [C++0x]
14467 : type-specifier-seq
14469 opaque-enum-specifier:
14470 enum-key identifier enum-base [opt] ;
14472 GNU Extensions:
14473 enum-key attributes[opt] identifier [opt] enum-base [opt]
14474 { enumerator-list [opt] }attributes[opt]
14475 enum-key attributes[opt] identifier [opt] enum-base [opt]
14476 { enumerator-list, }attributes[opt] [C++0x]
14478 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14479 if the token stream isn't an enum-specifier after all. */
14481 static tree
14482 cp_parser_enum_specifier (cp_parser* parser)
14484 tree identifier;
14485 tree type = NULL_TREE;
14486 tree prev_scope;
14487 tree nested_name_specifier = NULL_TREE;
14488 tree attributes;
14489 bool scoped_enum_p = false;
14490 bool has_underlying_type = false;
14491 bool nested_being_defined = false;
14492 bool new_value_list = false;
14493 bool is_new_type = false;
14494 bool is_anonymous = false;
14495 tree underlying_type = NULL_TREE;
14496 cp_token *type_start_token = NULL;
14497 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14499 parser->colon_corrects_to_scope_p = false;
14501 /* Parse tentatively so that we can back up if we don't find a
14502 enum-specifier. */
14503 cp_parser_parse_tentatively (parser);
14505 /* Caller guarantees that the current token is 'enum', an identifier
14506 possibly follows, and the token after that is an opening brace.
14507 If we don't have an identifier, fabricate an anonymous name for
14508 the enumeration being defined. */
14509 cp_lexer_consume_token (parser->lexer);
14511 /* Parse the "class" or "struct", which indicates a scoped
14512 enumeration type in C++0x. */
14513 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14514 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14516 if (cxx_dialect < cxx0x)
14517 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14519 /* Consume the `struct' or `class' token. */
14520 cp_lexer_consume_token (parser->lexer);
14522 scoped_enum_p = true;
14525 attributes = cp_parser_attributes_opt (parser);
14527 /* Clear the qualification. */
14528 parser->scope = NULL_TREE;
14529 parser->qualifying_scope = NULL_TREE;
14530 parser->object_scope = NULL_TREE;
14532 /* Figure out in what scope the declaration is being placed. */
14533 prev_scope = current_scope ();
14535 type_start_token = cp_lexer_peek_token (parser->lexer);
14537 push_deferring_access_checks (dk_no_check);
14538 nested_name_specifier
14539 = cp_parser_nested_name_specifier_opt (parser,
14540 /*typename_keyword_p=*/true,
14541 /*check_dependency_p=*/false,
14542 /*type_p=*/false,
14543 /*is_declaration=*/false);
14545 if (nested_name_specifier)
14547 tree name;
14549 identifier = cp_parser_identifier (parser);
14550 name = cp_parser_lookup_name (parser, identifier,
14551 enum_type,
14552 /*is_template=*/false,
14553 /*is_namespace=*/false,
14554 /*check_dependency=*/true,
14555 /*ambiguous_decls=*/NULL,
14556 input_location);
14557 if (name)
14559 type = TREE_TYPE (name);
14560 if (TREE_CODE (type) == TYPENAME_TYPE)
14562 /* Are template enums allowed in ISO? */
14563 if (template_parm_scope_p ())
14564 pedwarn (type_start_token->location, OPT_Wpedantic,
14565 "%qD is an enumeration template", name);
14566 /* ignore a typename reference, for it will be solved by name
14567 in start_enum. */
14568 type = NULL_TREE;
14571 else
14572 error_at (type_start_token->location,
14573 "%qD is not an enumerator-name", identifier);
14575 else
14577 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14578 identifier = cp_parser_identifier (parser);
14579 else
14581 identifier = make_anon_name ();
14582 is_anonymous = true;
14585 pop_deferring_access_checks ();
14587 /* Check for the `:' that denotes a specified underlying type in C++0x.
14588 Note that a ':' could also indicate a bitfield width, however. */
14589 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14591 cp_decl_specifier_seq type_specifiers;
14593 /* Consume the `:'. */
14594 cp_lexer_consume_token (parser->lexer);
14596 /* Parse the type-specifier-seq. */
14597 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14598 /*is_trailing_return=*/false,
14599 &type_specifiers);
14601 /* At this point this is surely not elaborated type specifier. */
14602 if (!cp_parser_parse_definitely (parser))
14603 return NULL_TREE;
14605 if (cxx_dialect < cxx0x)
14606 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14608 has_underlying_type = true;
14610 /* If that didn't work, stop. */
14611 if (type_specifiers.type != error_mark_node)
14613 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14614 /*initialized=*/0, NULL);
14615 if (underlying_type == error_mark_node)
14616 underlying_type = NULL_TREE;
14620 /* Look for the `{' but don't consume it yet. */
14621 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14623 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14625 cp_parser_error (parser, "expected %<{%>");
14626 if (has_underlying_type)
14628 type = NULL_TREE;
14629 goto out;
14632 /* An opaque-enum-specifier must have a ';' here. */
14633 if ((scoped_enum_p || underlying_type)
14634 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14636 cp_parser_error (parser, "expected %<;%> or %<{%>");
14637 if (has_underlying_type)
14639 type = NULL_TREE;
14640 goto out;
14645 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14646 return NULL_TREE;
14648 if (nested_name_specifier)
14650 if (CLASS_TYPE_P (nested_name_specifier))
14652 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14653 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14654 push_scope (nested_name_specifier);
14656 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14658 push_nested_namespace (nested_name_specifier);
14662 /* Issue an error message if type-definitions are forbidden here. */
14663 if (!cp_parser_check_type_definition (parser))
14664 type = error_mark_node;
14665 else
14666 /* Create the new type. We do this before consuming the opening
14667 brace so the enum will be recorded as being on the line of its
14668 tag (or the 'enum' keyword, if there is no tag). */
14669 type = start_enum (identifier, type, underlying_type,
14670 scoped_enum_p, &is_new_type);
14672 /* If the next token is not '{' it is an opaque-enum-specifier or an
14673 elaborated-type-specifier. */
14674 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14676 timevar_push (TV_PARSE_ENUM);
14677 if (nested_name_specifier)
14679 /* The following catches invalid code such as:
14680 enum class S<int>::E { A, B, C }; */
14681 if (!processing_specialization
14682 && CLASS_TYPE_P (nested_name_specifier)
14683 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14684 error_at (type_start_token->location, "cannot add an enumerator "
14685 "list to a template instantiation");
14687 /* If that scope does not contain the scope in which the
14688 class was originally declared, the program is invalid. */
14689 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14691 if (at_namespace_scope_p ())
14692 error_at (type_start_token->location,
14693 "declaration of %qD in namespace %qD which does not "
14694 "enclose %qD",
14695 type, prev_scope, nested_name_specifier);
14696 else
14697 error_at (type_start_token->location,
14698 "declaration of %qD in %qD which does not enclose %qD",
14699 type, prev_scope, nested_name_specifier);
14700 type = error_mark_node;
14704 if (scoped_enum_p)
14705 begin_scope (sk_scoped_enum, type);
14707 /* Consume the opening brace. */
14708 cp_lexer_consume_token (parser->lexer);
14710 if (type == error_mark_node)
14711 ; /* Nothing to add */
14712 else if (OPAQUE_ENUM_P (type)
14713 || (cxx_dialect > cxx98 && processing_specialization))
14715 new_value_list = true;
14716 SET_OPAQUE_ENUM_P (type, false);
14717 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14719 else
14721 error_at (type_start_token->location, "multiple definition of %q#T", type);
14722 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14723 "previous definition here");
14724 type = error_mark_node;
14727 if (type == error_mark_node)
14728 cp_parser_skip_to_end_of_block_or_statement (parser);
14729 /* If the next token is not '}', then there are some enumerators. */
14730 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14731 cp_parser_enumerator_list (parser, type);
14733 /* Consume the final '}'. */
14734 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14736 if (scoped_enum_p)
14737 finish_scope ();
14738 timevar_pop (TV_PARSE_ENUM);
14740 else
14742 /* If a ';' follows, then it is an opaque-enum-specifier
14743 and additional restrictions apply. */
14744 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14746 if (is_anonymous)
14747 error_at (type_start_token->location,
14748 "opaque-enum-specifier without name");
14749 else if (nested_name_specifier)
14750 error_at (type_start_token->location,
14751 "opaque-enum-specifier must use a simple identifier");
14755 /* Look for trailing attributes to apply to this enumeration, and
14756 apply them if appropriate. */
14757 if (cp_parser_allow_gnu_extensions_p (parser))
14759 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
14760 trailing_attr = chainon (trailing_attr, attributes);
14761 cplus_decl_attributes (&type,
14762 trailing_attr,
14763 (int) ATTR_FLAG_TYPE_IN_PLACE);
14766 /* Finish up the enumeration. */
14767 if (type != error_mark_node)
14769 if (new_value_list)
14770 finish_enum_value_list (type);
14771 if (is_new_type)
14772 finish_enum (type);
14775 if (nested_name_specifier)
14777 if (CLASS_TYPE_P (nested_name_specifier))
14779 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14780 pop_scope (nested_name_specifier);
14782 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14784 pop_nested_namespace (nested_name_specifier);
14787 out:
14788 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14789 return type;
14792 /* Parse an enumerator-list. The enumerators all have the indicated
14793 TYPE.
14795 enumerator-list:
14796 enumerator-definition
14797 enumerator-list , enumerator-definition */
14799 static void
14800 cp_parser_enumerator_list (cp_parser* parser, tree type)
14802 while (true)
14804 /* Parse an enumerator-definition. */
14805 cp_parser_enumerator_definition (parser, type);
14807 /* If the next token is not a ',', we've reached the end of
14808 the list. */
14809 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14810 break;
14811 /* Otherwise, consume the `,' and keep going. */
14812 cp_lexer_consume_token (parser->lexer);
14813 /* If the next token is a `}', there is a trailing comma. */
14814 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14816 if (cxx_dialect < cxx0x && !in_system_header)
14817 pedwarn (input_location, OPT_Wpedantic,
14818 "comma at end of enumerator list");
14819 break;
14824 /* Parse an enumerator-definition. The enumerator has the indicated
14825 TYPE.
14827 enumerator-definition:
14828 enumerator
14829 enumerator = constant-expression
14831 enumerator:
14832 identifier */
14834 static void
14835 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14837 tree identifier;
14838 tree value;
14839 location_t loc;
14841 /* Save the input location because we are interested in the location
14842 of the identifier and not the location of the explicit value. */
14843 loc = cp_lexer_peek_token (parser->lexer)->location;
14845 /* Look for the identifier. */
14846 identifier = cp_parser_identifier (parser);
14847 if (identifier == error_mark_node)
14848 return;
14850 /* If the next token is an '=', then there is an explicit value. */
14851 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14853 /* Consume the `=' token. */
14854 cp_lexer_consume_token (parser->lexer);
14855 /* Parse the value. */
14856 value = cp_parser_constant_expression (parser,
14857 /*allow_non_constant_p=*/false,
14858 NULL);
14860 else
14861 value = NULL_TREE;
14863 /* If we are processing a template, make sure the initializer of the
14864 enumerator doesn't contain any bare template parameter pack. */
14865 if (check_for_bare_parameter_packs (value))
14866 value = error_mark_node;
14868 /* integral_constant_value will pull out this expression, so make sure
14869 it's folded as appropriate. */
14870 value = fold_non_dependent_expr (value);
14872 /* Create the enumerator. */
14873 build_enumerator (identifier, value, type, loc);
14876 /* Parse a namespace-name.
14878 namespace-name:
14879 original-namespace-name
14880 namespace-alias
14882 Returns the NAMESPACE_DECL for the namespace. */
14884 static tree
14885 cp_parser_namespace_name (cp_parser* parser)
14887 tree identifier;
14888 tree namespace_decl;
14890 cp_token *token = cp_lexer_peek_token (parser->lexer);
14892 /* Get the name of the namespace. */
14893 identifier = cp_parser_identifier (parser);
14894 if (identifier == error_mark_node)
14895 return error_mark_node;
14897 /* Look up the identifier in the currently active scope. Look only
14898 for namespaces, due to:
14900 [basic.lookup.udir]
14902 When looking up a namespace-name in a using-directive or alias
14903 definition, only namespace names are considered.
14905 And:
14907 [basic.lookup.qual]
14909 During the lookup of a name preceding the :: scope resolution
14910 operator, object, function, and enumerator names are ignored.
14912 (Note that cp_parser_qualifying_entity only calls this
14913 function if the token after the name is the scope resolution
14914 operator.) */
14915 namespace_decl = cp_parser_lookup_name (parser, identifier,
14916 none_type,
14917 /*is_template=*/false,
14918 /*is_namespace=*/true,
14919 /*check_dependency=*/true,
14920 /*ambiguous_decls=*/NULL,
14921 token->location);
14922 /* If it's not a namespace, issue an error. */
14923 if (namespace_decl == error_mark_node
14924 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14926 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14927 error_at (token->location, "%qD is not a namespace-name", identifier);
14928 cp_parser_error (parser, "expected namespace-name");
14929 namespace_decl = error_mark_node;
14932 return namespace_decl;
14935 /* Parse a namespace-definition.
14937 namespace-definition:
14938 named-namespace-definition
14939 unnamed-namespace-definition
14941 named-namespace-definition:
14942 original-namespace-definition
14943 extension-namespace-definition
14945 original-namespace-definition:
14946 namespace identifier { namespace-body }
14948 extension-namespace-definition:
14949 namespace original-namespace-name { namespace-body }
14951 unnamed-namespace-definition:
14952 namespace { namespace-body } */
14954 static void
14955 cp_parser_namespace_definition (cp_parser* parser)
14957 tree identifier, attribs;
14958 bool has_visibility;
14959 bool is_inline;
14961 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14963 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14964 is_inline = true;
14965 cp_lexer_consume_token (parser->lexer);
14967 else
14968 is_inline = false;
14970 /* Look for the `namespace' keyword. */
14971 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14973 /* Get the name of the namespace. We do not attempt to distinguish
14974 between an original-namespace-definition and an
14975 extension-namespace-definition at this point. The semantic
14976 analysis routines are responsible for that. */
14977 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14978 identifier = cp_parser_identifier (parser);
14979 else
14980 identifier = NULL_TREE;
14982 /* Parse any specified attributes. */
14983 attribs = cp_parser_attributes_opt (parser);
14985 /* Look for the `{' to start the namespace. */
14986 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14987 /* Start the namespace. */
14988 push_namespace (identifier);
14990 /* "inline namespace" is equivalent to a stub namespace definition
14991 followed by a strong using directive. */
14992 if (is_inline)
14994 tree name_space = current_namespace;
14995 /* Set up namespace association. */
14996 DECL_NAMESPACE_ASSOCIATIONS (name_space)
14997 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14998 DECL_NAMESPACE_ASSOCIATIONS (name_space));
14999 /* Import the contents of the inline namespace. */
15000 pop_namespace ();
15001 do_using_directive (name_space);
15002 push_namespace (identifier);
15005 has_visibility = handle_namespace_attrs (current_namespace, attribs);
15007 /* Parse the body of the namespace. */
15008 cp_parser_namespace_body (parser);
15010 if (has_visibility)
15011 pop_visibility (1);
15013 /* Finish the namespace. */
15014 pop_namespace ();
15015 /* Look for the final `}'. */
15016 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15019 /* Parse a namespace-body.
15021 namespace-body:
15022 declaration-seq [opt] */
15024 static void
15025 cp_parser_namespace_body (cp_parser* parser)
15027 cp_parser_declaration_seq_opt (parser);
15030 /* Parse a namespace-alias-definition.
15032 namespace-alias-definition:
15033 namespace identifier = qualified-namespace-specifier ; */
15035 static void
15036 cp_parser_namespace_alias_definition (cp_parser* parser)
15038 tree identifier;
15039 tree namespace_specifier;
15041 cp_token *token = cp_lexer_peek_token (parser->lexer);
15043 /* Look for the `namespace' keyword. */
15044 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15045 /* Look for the identifier. */
15046 identifier = cp_parser_identifier (parser);
15047 if (identifier == error_mark_node)
15048 return;
15049 /* Look for the `=' token. */
15050 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
15051 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15053 error_at (token->location, "%<namespace%> definition is not allowed here");
15054 /* Skip the definition. */
15055 cp_lexer_consume_token (parser->lexer);
15056 if (cp_parser_skip_to_closing_brace (parser))
15057 cp_lexer_consume_token (parser->lexer);
15058 return;
15060 cp_parser_require (parser, CPP_EQ, RT_EQ);
15061 /* Look for the qualified-namespace-specifier. */
15062 namespace_specifier
15063 = cp_parser_qualified_namespace_specifier (parser);
15064 /* Look for the `;' token. */
15065 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15067 /* Register the alias in the symbol table. */
15068 do_namespace_alias (identifier, namespace_specifier);
15071 /* Parse a qualified-namespace-specifier.
15073 qualified-namespace-specifier:
15074 :: [opt] nested-name-specifier [opt] namespace-name
15076 Returns a NAMESPACE_DECL corresponding to the specified
15077 namespace. */
15079 static tree
15080 cp_parser_qualified_namespace_specifier (cp_parser* parser)
15082 /* Look for the optional `::'. */
15083 cp_parser_global_scope_opt (parser,
15084 /*current_scope_valid_p=*/false);
15086 /* Look for the optional nested-name-specifier. */
15087 cp_parser_nested_name_specifier_opt (parser,
15088 /*typename_keyword_p=*/false,
15089 /*check_dependency_p=*/true,
15090 /*type_p=*/false,
15091 /*is_declaration=*/true);
15093 return cp_parser_namespace_name (parser);
15096 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
15097 access declaration.
15099 using-declaration:
15100 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
15101 using :: unqualified-id ;
15103 access-declaration:
15104 qualified-id ;
15108 static bool
15109 cp_parser_using_declaration (cp_parser* parser,
15110 bool access_declaration_p)
15112 cp_token *token;
15113 bool typename_p = false;
15114 bool global_scope_p;
15115 tree decl;
15116 tree identifier;
15117 tree qscope;
15118 int oldcount = errorcount;
15119 cp_token *diag_token = NULL;
15121 if (access_declaration_p)
15123 diag_token = cp_lexer_peek_token (parser->lexer);
15124 cp_parser_parse_tentatively (parser);
15126 else
15128 /* Look for the `using' keyword. */
15129 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15131 /* Peek at the next token. */
15132 token = cp_lexer_peek_token (parser->lexer);
15133 /* See if it's `typename'. */
15134 if (token->keyword == RID_TYPENAME)
15136 /* Remember that we've seen it. */
15137 typename_p = true;
15138 /* Consume the `typename' token. */
15139 cp_lexer_consume_token (parser->lexer);
15143 /* Look for the optional global scope qualification. */
15144 global_scope_p
15145 = (cp_parser_global_scope_opt (parser,
15146 /*current_scope_valid_p=*/false)
15147 != NULL_TREE);
15149 /* If we saw `typename', or didn't see `::', then there must be a
15150 nested-name-specifier present. */
15151 if (typename_p || !global_scope_p)
15152 qscope = cp_parser_nested_name_specifier (parser, typename_p,
15153 /*check_dependency_p=*/true,
15154 /*type_p=*/false,
15155 /*is_declaration=*/true);
15156 /* Otherwise, we could be in either of the two productions. In that
15157 case, treat the nested-name-specifier as optional. */
15158 else
15159 qscope = cp_parser_nested_name_specifier_opt (parser,
15160 /*typename_keyword_p=*/false,
15161 /*check_dependency_p=*/true,
15162 /*type_p=*/false,
15163 /*is_declaration=*/true);
15164 if (!qscope)
15165 qscope = global_namespace;
15167 if (access_declaration_p && cp_parser_error_occurred (parser))
15168 /* Something has already gone wrong; there's no need to parse
15169 further. Since an error has occurred, the return value of
15170 cp_parser_parse_definitely will be false, as required. */
15171 return cp_parser_parse_definitely (parser);
15173 token = cp_lexer_peek_token (parser->lexer);
15174 /* Parse the unqualified-id. */
15175 identifier = cp_parser_unqualified_id (parser,
15176 /*template_keyword_p=*/false,
15177 /*check_dependency_p=*/true,
15178 /*declarator_p=*/true,
15179 /*optional_p=*/false);
15181 if (access_declaration_p)
15183 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15184 cp_parser_simulate_error (parser);
15185 if (!cp_parser_parse_definitely (parser))
15186 return false;
15189 /* The function we call to handle a using-declaration is different
15190 depending on what scope we are in. */
15191 if (qscope == error_mark_node || identifier == error_mark_node)
15193 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
15194 && TREE_CODE (identifier) != BIT_NOT_EXPR)
15195 /* [namespace.udecl]
15197 A using declaration shall not name a template-id. */
15198 error_at (token->location,
15199 "a template-id may not appear in a using-declaration");
15200 else
15202 if (at_class_scope_p ())
15204 /* Create the USING_DECL. */
15205 decl = do_class_using_decl (parser->scope, identifier);
15207 if (decl && typename_p)
15208 USING_DECL_TYPENAME_P (decl) = 1;
15210 if (check_for_bare_parameter_packs (decl))
15211 return false;
15212 else
15213 /* Add it to the list of members in this class. */
15214 finish_member_declaration (decl);
15216 else
15218 decl = cp_parser_lookup_name_simple (parser,
15219 identifier,
15220 token->location);
15221 if (decl == error_mark_node)
15222 cp_parser_name_lookup_error (parser, identifier,
15223 decl, NLE_NULL,
15224 token->location);
15225 else if (check_for_bare_parameter_packs (decl))
15226 return false;
15227 else if (!at_namespace_scope_p ())
15228 do_local_using_decl (decl, qscope, identifier);
15229 else
15230 do_toplevel_using_decl (decl, qscope, identifier);
15234 /* Look for the final `;'. */
15235 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15237 if (access_declaration_p && errorcount == oldcount)
15238 warning_at (diag_token->location, OPT_Wdeprecated,
15239 "access declarations are deprecated "
15240 "in favour of using-declarations; "
15241 "suggestion: add the %<using%> keyword");
15243 return true;
15246 /* Parse an alias-declaration.
15248 alias-declaration:
15249 using identifier attribute-specifier-seq [opt] = type-id */
15251 static tree
15252 cp_parser_alias_declaration (cp_parser* parser)
15254 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15255 location_t id_location;
15256 cp_declarator *declarator;
15257 cp_decl_specifier_seq decl_specs;
15258 bool member_p;
15259 const char *saved_message = NULL;
15261 /* Look for the `using' keyword. */
15262 cp_token *using_token
15263 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
15264 if (using_token == NULL)
15265 return error_mark_node;
15267 id_location = cp_lexer_peek_token (parser->lexer)->location;
15268 id = cp_parser_identifier (parser);
15269 if (id == error_mark_node)
15270 return error_mark_node;
15272 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
15273 attributes = cp_parser_attributes_opt (parser);
15274 if (attributes == error_mark_node)
15275 return error_mark_node;
15277 cp_parser_require (parser, CPP_EQ, RT_EQ);
15279 if (cp_parser_error_occurred (parser))
15280 return error_mark_node;
15282 /* Now we are going to parse the type-id of the declaration. */
15285 [dcl.type]/3 says:
15287 "A type-specifier-seq shall not define a class or enumeration
15288 unless it appears in the type-id of an alias-declaration (7.1.3) that
15289 is not the declaration of a template-declaration."
15291 In other words, if we currently are in an alias template, the
15292 type-id should not define a type.
15294 So let's set parser->type_definition_forbidden_message in that
15295 case; cp_parser_check_type_definition (called by
15296 cp_parser_class_specifier) will then emit an error if a type is
15297 defined in the type-id. */
15298 if (parser->num_template_parameter_lists)
15300 saved_message = parser->type_definition_forbidden_message;
15301 parser->type_definition_forbidden_message =
15302 G_("types may not be defined in alias template declarations");
15305 type = cp_parser_type_id (parser);
15307 /* Restore the error message if need be. */
15308 if (parser->num_template_parameter_lists)
15309 parser->type_definition_forbidden_message = saved_message;
15311 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15313 if (cp_parser_error_occurred (parser))
15314 return error_mark_node;
15316 /* A typedef-name can also be introduced by an alias-declaration. The
15317 identifier following the using keyword becomes a typedef-name. It has
15318 the same semantics as if it were introduced by the typedef
15319 specifier. In particular, it does not define a new type and it shall
15320 not appear in the type-id. */
15322 clear_decl_specs (&decl_specs);
15323 decl_specs.type = type;
15324 if (attributes != NULL_TREE)
15326 decl_specs.attributes = attributes;
15327 set_and_check_decl_spec_loc (&decl_specs,
15328 ds_attribute,
15329 attrs_token);
15331 set_and_check_decl_spec_loc (&decl_specs,
15332 ds_typedef,
15333 using_token);
15334 set_and_check_decl_spec_loc (&decl_specs,
15335 ds_alias,
15336 using_token);
15338 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15339 declarator->id_loc = id_location;
15341 member_p = at_class_scope_p ();
15342 if (member_p)
15343 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15344 NULL_TREE, attributes);
15345 else
15346 decl = start_decl (declarator, &decl_specs, 0,
15347 attributes, NULL_TREE, &pushed_scope);
15348 if (decl == error_mark_node)
15349 return decl;
15351 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15353 if (pushed_scope)
15354 pop_scope (pushed_scope);
15356 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15357 added into the symbol table; otherwise, return the TYPE_DECL. */
15358 if (DECL_LANG_SPECIFIC (decl)
15359 && DECL_TEMPLATE_INFO (decl)
15360 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15362 decl = DECL_TI_TEMPLATE (decl);
15363 if (member_p)
15364 check_member_template (decl);
15367 return decl;
15370 /* Parse a using-directive.
15372 using-directive:
15373 using namespace :: [opt] nested-name-specifier [opt]
15374 namespace-name ; */
15376 static void
15377 cp_parser_using_directive (cp_parser* parser)
15379 tree namespace_decl;
15380 tree attribs;
15382 /* Look for the `using' keyword. */
15383 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15384 /* And the `namespace' keyword. */
15385 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15386 /* Look for the optional `::' operator. */
15387 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15388 /* And the optional nested-name-specifier. */
15389 cp_parser_nested_name_specifier_opt (parser,
15390 /*typename_keyword_p=*/false,
15391 /*check_dependency_p=*/true,
15392 /*type_p=*/false,
15393 /*is_declaration=*/true);
15394 /* Get the namespace being used. */
15395 namespace_decl = cp_parser_namespace_name (parser);
15396 /* And any specified attributes. */
15397 attribs = cp_parser_attributes_opt (parser);
15398 /* Update the symbol table. */
15399 parse_using_directive (namespace_decl, attribs);
15400 /* Look for the final `;'. */
15401 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15404 /* Parse an asm-definition.
15406 asm-definition:
15407 asm ( string-literal ) ;
15409 GNU Extension:
15411 asm-definition:
15412 asm volatile [opt] ( string-literal ) ;
15413 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15414 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15415 : asm-operand-list [opt] ) ;
15416 asm volatile [opt] ( string-literal : asm-operand-list [opt]
15417 : asm-operand-list [opt]
15418 : asm-clobber-list [opt] ) ;
15419 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15420 : asm-clobber-list [opt]
15421 : asm-goto-list ) ; */
15423 static void
15424 cp_parser_asm_definition (cp_parser* parser)
15426 tree string;
15427 tree outputs = NULL_TREE;
15428 tree inputs = NULL_TREE;
15429 tree clobbers = NULL_TREE;
15430 tree labels = NULL_TREE;
15431 tree asm_stmt;
15432 bool volatile_p = false;
15433 bool extended_p = false;
15434 bool invalid_inputs_p = false;
15435 bool invalid_outputs_p = false;
15436 bool goto_p = false;
15437 required_token missing = RT_NONE;
15439 /* Look for the `asm' keyword. */
15440 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15441 /* See if the next token is `volatile'. */
15442 if (cp_parser_allow_gnu_extensions_p (parser)
15443 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15445 /* Remember that we saw the `volatile' keyword. */
15446 volatile_p = true;
15447 /* Consume the token. */
15448 cp_lexer_consume_token (parser->lexer);
15450 if (cp_parser_allow_gnu_extensions_p (parser)
15451 && parser->in_function_body
15452 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15454 /* Remember that we saw the `goto' keyword. */
15455 goto_p = true;
15456 /* Consume the token. */
15457 cp_lexer_consume_token (parser->lexer);
15459 /* Look for the opening `('. */
15460 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15461 return;
15462 /* Look for the string. */
15463 string = cp_parser_string_literal (parser, false, false);
15464 if (string == error_mark_node)
15466 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15467 /*consume_paren=*/true);
15468 return;
15471 /* If we're allowing GNU extensions, check for the extended assembly
15472 syntax. Unfortunately, the `:' tokens need not be separated by
15473 a space in C, and so, for compatibility, we tolerate that here
15474 too. Doing that means that we have to treat the `::' operator as
15475 two `:' tokens. */
15476 if (cp_parser_allow_gnu_extensions_p (parser)
15477 && parser->in_function_body
15478 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15479 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15481 bool inputs_p = false;
15482 bool clobbers_p = false;
15483 bool labels_p = false;
15485 /* The extended syntax was used. */
15486 extended_p = true;
15488 /* Look for outputs. */
15489 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15491 /* Consume the `:'. */
15492 cp_lexer_consume_token (parser->lexer);
15493 /* Parse the output-operands. */
15494 if (cp_lexer_next_token_is_not (parser->lexer,
15495 CPP_COLON)
15496 && cp_lexer_next_token_is_not (parser->lexer,
15497 CPP_SCOPE)
15498 && cp_lexer_next_token_is_not (parser->lexer,
15499 CPP_CLOSE_PAREN)
15500 && !goto_p)
15501 outputs = cp_parser_asm_operand_list (parser);
15503 if (outputs == error_mark_node)
15504 invalid_outputs_p = true;
15506 /* If the next token is `::', there are no outputs, and the
15507 next token is the beginning of the inputs. */
15508 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15509 /* The inputs are coming next. */
15510 inputs_p = true;
15512 /* Look for inputs. */
15513 if (inputs_p
15514 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15516 /* Consume the `:' or `::'. */
15517 cp_lexer_consume_token (parser->lexer);
15518 /* Parse the output-operands. */
15519 if (cp_lexer_next_token_is_not (parser->lexer,
15520 CPP_COLON)
15521 && cp_lexer_next_token_is_not (parser->lexer,
15522 CPP_SCOPE)
15523 && cp_lexer_next_token_is_not (parser->lexer,
15524 CPP_CLOSE_PAREN))
15525 inputs = cp_parser_asm_operand_list (parser);
15527 if (inputs == error_mark_node)
15528 invalid_inputs_p = true;
15530 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15531 /* The clobbers are coming next. */
15532 clobbers_p = true;
15534 /* Look for clobbers. */
15535 if (clobbers_p
15536 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15538 clobbers_p = true;
15539 /* Consume the `:' or `::'. */
15540 cp_lexer_consume_token (parser->lexer);
15541 /* Parse the clobbers. */
15542 if (cp_lexer_next_token_is_not (parser->lexer,
15543 CPP_COLON)
15544 && cp_lexer_next_token_is_not (parser->lexer,
15545 CPP_CLOSE_PAREN))
15546 clobbers = cp_parser_asm_clobber_list (parser);
15548 else if (goto_p
15549 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15550 /* The labels are coming next. */
15551 labels_p = true;
15553 /* Look for labels. */
15554 if (labels_p
15555 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15557 labels_p = true;
15558 /* Consume the `:' or `::'. */
15559 cp_lexer_consume_token (parser->lexer);
15560 /* Parse the labels. */
15561 labels = cp_parser_asm_label_list (parser);
15564 if (goto_p && !labels_p)
15565 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15567 else if (goto_p)
15568 missing = RT_COLON_SCOPE;
15570 /* Look for the closing `)'. */
15571 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15572 missing ? missing : RT_CLOSE_PAREN))
15573 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15574 /*consume_paren=*/true);
15575 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15577 if (!invalid_inputs_p && !invalid_outputs_p)
15579 /* Create the ASM_EXPR. */
15580 if (parser->in_function_body)
15582 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15583 inputs, clobbers, labels);
15584 /* If the extended syntax was not used, mark the ASM_EXPR. */
15585 if (!extended_p)
15587 tree temp = asm_stmt;
15588 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15589 temp = TREE_OPERAND (temp, 0);
15591 ASM_INPUT_P (temp) = 1;
15594 else
15595 add_asm_node (string);
15599 /* Declarators [gram.dcl.decl] */
15601 /* Parse an init-declarator.
15603 init-declarator:
15604 declarator initializer [opt]
15606 GNU Extension:
15608 init-declarator:
15609 declarator asm-specification [opt] attributes [opt] initializer [opt]
15611 function-definition:
15612 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15613 function-body
15614 decl-specifier-seq [opt] declarator function-try-block
15616 GNU Extension:
15618 function-definition:
15619 __extension__ function-definition
15621 TM Extension:
15623 function-definition:
15624 decl-specifier-seq [opt] declarator function-transaction-block
15626 The DECL_SPECIFIERS apply to this declarator. Returns a
15627 representation of the entity declared. If MEMBER_P is TRUE, then
15628 this declarator appears in a class scope. The new DECL created by
15629 this declarator is returned.
15631 The CHECKS are access checks that should be performed once we know
15632 what entity is being declared (and, therefore, what classes have
15633 befriended it).
15635 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15636 for a function-definition here as well. If the declarator is a
15637 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15638 be TRUE upon return. By that point, the function-definition will
15639 have been completely parsed.
15641 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15642 is FALSE.
15644 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15645 parsed declaration if it is an uninitialized single declarator not followed
15646 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15647 if present, will not be consumed. If returned, this declarator will be
15648 created with SD_INITIALIZED but will not call cp_finish_decl. */
15650 static tree
15651 cp_parser_init_declarator (cp_parser* parser,
15652 cp_decl_specifier_seq *decl_specifiers,
15653 vec<deferred_access_check, va_gc> *checks,
15654 bool function_definition_allowed_p,
15655 bool member_p,
15656 int declares_class_or_enum,
15657 bool* function_definition_p,
15658 tree* maybe_range_for_decl)
15660 cp_token *token = NULL, *asm_spec_start_token = NULL,
15661 *attributes_start_token = NULL;
15662 cp_declarator *declarator;
15663 tree prefix_attributes;
15664 tree attributes = NULL;
15665 tree asm_specification;
15666 tree initializer;
15667 tree decl = NULL_TREE;
15668 tree scope;
15669 int is_initialized;
15670 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15671 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15672 "(...)". */
15673 enum cpp_ttype initialization_kind;
15674 bool is_direct_init = false;
15675 bool is_non_constant_init;
15676 int ctor_dtor_or_conv_p;
15677 bool friend_p;
15678 tree pushed_scope = NULL_TREE;
15679 bool range_for_decl_p = false;
15681 /* Gather the attributes that were provided with the
15682 decl-specifiers. */
15683 prefix_attributes = decl_specifiers->attributes;
15685 /* Assume that this is not the declarator for a function
15686 definition. */
15687 if (function_definition_p)
15688 *function_definition_p = false;
15690 /* Defer access checks while parsing the declarator; we cannot know
15691 what names are accessible until we know what is being
15692 declared. */
15693 resume_deferring_access_checks ();
15695 /* Parse the declarator. */
15696 token = cp_lexer_peek_token (parser->lexer);
15697 declarator
15698 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15699 &ctor_dtor_or_conv_p,
15700 /*parenthesized_p=*/NULL,
15701 member_p);
15702 /* Gather up the deferred checks. */
15703 stop_deferring_access_checks ();
15705 /* If the DECLARATOR was erroneous, there's no need to go
15706 further. */
15707 if (declarator == cp_error_declarator)
15708 return error_mark_node;
15710 /* Check that the number of template-parameter-lists is OK. */
15711 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15712 token->location))
15713 return error_mark_node;
15715 if (declares_class_or_enum & 2)
15716 cp_parser_check_for_definition_in_return_type (declarator,
15717 decl_specifiers->type,
15718 decl_specifiers->locations[ds_type_spec]);
15720 /* Figure out what scope the entity declared by the DECLARATOR is
15721 located in. `grokdeclarator' sometimes changes the scope, so
15722 we compute it now. */
15723 scope = get_scope_of_declarator (declarator);
15725 /* Perform any lookups in the declared type which were thought to be
15726 dependent, but are not in the scope of the declarator. */
15727 decl_specifiers->type
15728 = maybe_update_decl_type (decl_specifiers->type, scope);
15730 /* If we're allowing GNU extensions, look for an
15731 asm-specification. */
15732 if (cp_parser_allow_gnu_extensions_p (parser))
15734 /* Look for an asm-specification. */
15735 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15736 asm_specification = cp_parser_asm_specification_opt (parser);
15738 else
15739 asm_specification = NULL_TREE;
15741 /* Look for attributes. */
15742 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15743 attributes = cp_parser_attributes_opt (parser);
15745 /* Peek at the next token. */
15746 token = cp_lexer_peek_token (parser->lexer);
15747 /* Check to see if the token indicates the start of a
15748 function-definition. */
15749 if (function_declarator_p (declarator)
15750 && cp_parser_token_starts_function_definition_p (token))
15752 if (!function_definition_allowed_p)
15754 /* If a function-definition should not appear here, issue an
15755 error message. */
15756 cp_parser_error (parser,
15757 "a function-definition is not allowed here");
15758 return error_mark_node;
15760 else
15762 location_t func_brace_location
15763 = cp_lexer_peek_token (parser->lexer)->location;
15765 /* Neither attributes nor an asm-specification are allowed
15766 on a function-definition. */
15767 if (asm_specification)
15768 error_at (asm_spec_start_token->location,
15769 "an asm-specification is not allowed "
15770 "on a function-definition");
15771 if (attributes)
15772 error_at (attributes_start_token->location,
15773 "attributes are not allowed on a function-definition");
15774 /* This is a function-definition. */
15775 *function_definition_p = true;
15777 /* Parse the function definition. */
15778 if (member_p)
15779 decl = cp_parser_save_member_function_body (parser,
15780 decl_specifiers,
15781 declarator,
15782 prefix_attributes);
15783 else
15784 decl
15785 = (cp_parser_function_definition_from_specifiers_and_declarator
15786 (parser, decl_specifiers, prefix_attributes, declarator));
15788 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15790 /* This is where the prologue starts... */
15791 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15792 = func_brace_location;
15795 return decl;
15799 /* [dcl.dcl]
15801 Only in function declarations for constructors, destructors, and
15802 type conversions can the decl-specifier-seq be omitted.
15804 We explicitly postpone this check past the point where we handle
15805 function-definitions because we tolerate function-definitions
15806 that are missing their return types in some modes. */
15807 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15809 cp_parser_error (parser,
15810 "expected constructor, destructor, or type conversion");
15811 return error_mark_node;
15814 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15815 if (token->type == CPP_EQ
15816 || token->type == CPP_OPEN_PAREN
15817 || token->type == CPP_OPEN_BRACE)
15819 is_initialized = SD_INITIALIZED;
15820 initialization_kind = token->type;
15821 if (maybe_range_for_decl)
15822 *maybe_range_for_decl = error_mark_node;
15824 if (token->type == CPP_EQ
15825 && function_declarator_p (declarator))
15827 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15828 if (t2->keyword == RID_DEFAULT)
15829 is_initialized = SD_DEFAULTED;
15830 else if (t2->keyword == RID_DELETE)
15831 is_initialized = SD_DELETED;
15834 else
15836 /* If the init-declarator isn't initialized and isn't followed by a
15837 `,' or `;', it's not a valid init-declarator. */
15838 if (token->type != CPP_COMMA
15839 && token->type != CPP_SEMICOLON)
15841 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15842 range_for_decl_p = true;
15843 else
15845 cp_parser_error (parser, "expected initializer");
15846 return error_mark_node;
15849 is_initialized = SD_UNINITIALIZED;
15850 initialization_kind = CPP_EOF;
15853 /* Because start_decl has side-effects, we should only call it if we
15854 know we're going ahead. By this point, we know that we cannot
15855 possibly be looking at any other construct. */
15856 cp_parser_commit_to_tentative_parse (parser);
15858 /* If the decl specifiers were bad, issue an error now that we're
15859 sure this was intended to be a declarator. Then continue
15860 declaring the variable(s), as int, to try to cut down on further
15861 errors. */
15862 if (decl_specifiers->any_specifiers_p
15863 && decl_specifiers->type == error_mark_node)
15865 cp_parser_error (parser, "invalid type in declaration");
15866 decl_specifiers->type = integer_type_node;
15869 /* Check to see whether or not this declaration is a friend. */
15870 friend_p = cp_parser_friend_p (decl_specifiers);
15872 /* Enter the newly declared entry in the symbol table. If we're
15873 processing a declaration in a class-specifier, we wait until
15874 after processing the initializer. */
15875 if (!member_p)
15877 if (parser->in_unbraced_linkage_specification_p)
15878 decl_specifiers->storage_class = sc_extern;
15879 decl = start_decl (declarator, decl_specifiers,
15880 range_for_decl_p? SD_INITIALIZED : is_initialized,
15881 attributes, prefix_attributes,
15882 &pushed_scope);
15883 /* Adjust location of decl if declarator->id_loc is more appropriate:
15884 set, and decl wasn't merged with another decl, in which case its
15885 location would be different from input_location, and more accurate. */
15886 if (DECL_P (decl)
15887 && declarator->id_loc != UNKNOWN_LOCATION
15888 && DECL_SOURCE_LOCATION (decl) == input_location)
15889 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15891 else if (scope)
15892 /* Enter the SCOPE. That way unqualified names appearing in the
15893 initializer will be looked up in SCOPE. */
15894 pushed_scope = push_scope (scope);
15896 /* Perform deferred access control checks, now that we know in which
15897 SCOPE the declared entity resides. */
15898 if (!member_p && decl)
15900 tree saved_current_function_decl = NULL_TREE;
15902 /* If the entity being declared is a function, pretend that we
15903 are in its scope. If it is a `friend', it may have access to
15904 things that would not otherwise be accessible. */
15905 if (TREE_CODE (decl) == FUNCTION_DECL)
15907 saved_current_function_decl = current_function_decl;
15908 current_function_decl = decl;
15911 /* Perform access checks for template parameters. */
15912 cp_parser_perform_template_parameter_access_checks (checks);
15914 /* Perform the access control checks for the declarator and the
15915 decl-specifiers. */
15916 perform_deferred_access_checks (tf_warning_or_error);
15918 /* Restore the saved value. */
15919 if (TREE_CODE (decl) == FUNCTION_DECL)
15920 current_function_decl = saved_current_function_decl;
15923 /* Parse the initializer. */
15924 initializer = NULL_TREE;
15925 is_direct_init = false;
15926 is_non_constant_init = true;
15927 if (is_initialized)
15929 if (function_declarator_p (declarator))
15931 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15932 if (initialization_kind == CPP_EQ)
15933 initializer = cp_parser_pure_specifier (parser);
15934 else
15936 /* If the declaration was erroneous, we don't really
15937 know what the user intended, so just silently
15938 consume the initializer. */
15939 if (decl != error_mark_node)
15940 error_at (initializer_start_token->location,
15941 "initializer provided for function");
15942 cp_parser_skip_to_closing_parenthesis (parser,
15943 /*recovering=*/true,
15944 /*or_comma=*/false,
15945 /*consume_paren=*/true);
15948 else
15950 /* We want to record the extra mangling scope for in-class
15951 initializers of class members and initializers of static data
15952 member templates. The former involves deferring
15953 parsing of the initializer until end of class as with default
15954 arguments. So right here we only handle the latter. */
15955 if (!member_p && processing_template_decl)
15956 start_lambda_scope (decl);
15957 initializer = cp_parser_initializer (parser,
15958 &is_direct_init,
15959 &is_non_constant_init);
15960 if (!member_p && processing_template_decl)
15961 finish_lambda_scope ();
15962 if (initializer == error_mark_node)
15963 cp_parser_skip_to_end_of_statement (parser);
15967 /* The old parser allows attributes to appear after a parenthesized
15968 initializer. Mark Mitchell proposed removing this functionality
15969 on the GCC mailing lists on 2002-08-13. This parser accepts the
15970 attributes -- but ignores them. */
15971 if (cp_parser_allow_gnu_extensions_p (parser)
15972 && initialization_kind == CPP_OPEN_PAREN)
15973 if (cp_parser_attributes_opt (parser))
15974 warning (OPT_Wattributes,
15975 "attributes after parenthesized initializer ignored");
15977 /* For an in-class declaration, use `grokfield' to create the
15978 declaration. */
15979 if (member_p)
15981 if (pushed_scope)
15983 pop_scope (pushed_scope);
15984 pushed_scope = NULL_TREE;
15986 decl = grokfield (declarator, decl_specifiers,
15987 initializer, !is_non_constant_init,
15988 /*asmspec=*/NULL_TREE,
15989 prefix_attributes);
15990 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15991 cp_parser_save_default_args (parser, decl);
15994 /* Finish processing the declaration. But, skip member
15995 declarations. */
15996 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15998 cp_finish_decl (decl,
15999 initializer, !is_non_constant_init,
16000 asm_specification,
16001 /* If the initializer is in parentheses, then this is
16002 a direct-initialization, which means that an
16003 `explicit' constructor is OK. Otherwise, an
16004 `explicit' constructor cannot be used. */
16005 ((is_direct_init || !is_initialized)
16006 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
16008 else if ((cxx_dialect != cxx98) && friend_p
16009 && decl && TREE_CODE (decl) == FUNCTION_DECL)
16010 /* Core issue #226 (C++0x only): A default template-argument
16011 shall not be specified in a friend class template
16012 declaration. */
16013 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
16014 /*is_partial=*/false, /*is_friend_decl=*/1);
16016 if (!friend_p && pushed_scope)
16017 pop_scope (pushed_scope);
16019 return decl;
16022 /* Parse a declarator.
16024 declarator:
16025 direct-declarator
16026 ptr-operator declarator
16028 abstract-declarator:
16029 ptr-operator abstract-declarator [opt]
16030 direct-abstract-declarator
16032 GNU Extensions:
16034 declarator:
16035 attributes [opt] direct-declarator
16036 attributes [opt] ptr-operator declarator
16038 abstract-declarator:
16039 attributes [opt] ptr-operator abstract-declarator [opt]
16040 attributes [opt] direct-abstract-declarator
16042 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
16043 detect constructor, destructor or conversion operators. It is set
16044 to -1 if the declarator is a name, and +1 if it is a
16045 function. Otherwise it is set to zero. Usually you just want to
16046 test for >0, but internally the negative value is used.
16048 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
16049 a decl-specifier-seq unless it declares a constructor, destructor,
16050 or conversion. It might seem that we could check this condition in
16051 semantic analysis, rather than parsing, but that makes it difficult
16052 to handle something like `f()'. We want to notice that there are
16053 no decl-specifiers, and therefore realize that this is an
16054 expression, not a declaration.)
16056 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
16057 the declarator is a direct-declarator of the form "(...)".
16059 MEMBER_P is true iff this declarator is a member-declarator. */
16061 static cp_declarator *
16062 cp_parser_declarator (cp_parser* parser,
16063 cp_parser_declarator_kind dcl_kind,
16064 int* ctor_dtor_or_conv_p,
16065 bool* parenthesized_p,
16066 bool member_p)
16068 cp_declarator *declarator;
16069 enum tree_code code;
16070 cp_cv_quals cv_quals;
16071 tree class_type;
16072 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
16074 /* Assume this is not a constructor, destructor, or type-conversion
16075 operator. */
16076 if (ctor_dtor_or_conv_p)
16077 *ctor_dtor_or_conv_p = 0;
16079 if (cp_parser_allow_gnu_extensions_p (parser))
16080 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
16082 /* Check for the ptr-operator production. */
16083 cp_parser_parse_tentatively (parser);
16084 /* Parse the ptr-operator. */
16085 code = cp_parser_ptr_operator (parser,
16086 &class_type,
16087 &cv_quals,
16088 &std_attributes);
16090 /* If that worked, then we have a ptr-operator. */
16091 if (cp_parser_parse_definitely (parser))
16093 /* If a ptr-operator was found, then this declarator was not
16094 parenthesized. */
16095 if (parenthesized_p)
16096 *parenthesized_p = true;
16097 /* The dependent declarator is optional if we are parsing an
16098 abstract-declarator. */
16099 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16100 cp_parser_parse_tentatively (parser);
16102 /* Parse the dependent declarator. */
16103 declarator = cp_parser_declarator (parser, dcl_kind,
16104 /*ctor_dtor_or_conv_p=*/NULL,
16105 /*parenthesized_p=*/NULL,
16106 /*member_p=*/false);
16108 /* If we are parsing an abstract-declarator, we must handle the
16109 case where the dependent declarator is absent. */
16110 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
16111 && !cp_parser_parse_definitely (parser))
16112 declarator = NULL;
16114 declarator = cp_parser_make_indirect_declarator
16115 (code, class_type, cv_quals, declarator, std_attributes);
16117 /* Everything else is a direct-declarator. */
16118 else
16120 if (parenthesized_p)
16121 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
16122 CPP_OPEN_PAREN);
16123 declarator = cp_parser_direct_declarator (parser, dcl_kind,
16124 ctor_dtor_or_conv_p,
16125 member_p);
16128 if (gnu_attributes && declarator && declarator != cp_error_declarator)
16129 declarator->attributes = gnu_attributes;
16130 return declarator;
16133 /* Parse a direct-declarator or direct-abstract-declarator.
16135 direct-declarator:
16136 declarator-id
16137 direct-declarator ( parameter-declaration-clause )
16138 cv-qualifier-seq [opt]
16139 exception-specification [opt]
16140 direct-declarator [ constant-expression [opt] ]
16141 ( declarator )
16143 direct-abstract-declarator:
16144 direct-abstract-declarator [opt]
16145 ( parameter-declaration-clause )
16146 cv-qualifier-seq [opt]
16147 exception-specification [opt]
16148 direct-abstract-declarator [opt] [ constant-expression [opt] ]
16149 ( abstract-declarator )
16151 Returns a representation of the declarator. DCL_KIND is
16152 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
16153 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
16154 we are parsing a direct-declarator. It is
16155 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
16156 of ambiguity we prefer an abstract declarator, as per
16157 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
16158 cp_parser_declarator. */
16160 static cp_declarator *
16161 cp_parser_direct_declarator (cp_parser* parser,
16162 cp_parser_declarator_kind dcl_kind,
16163 int* ctor_dtor_or_conv_p,
16164 bool member_p)
16166 cp_token *token;
16167 cp_declarator *declarator = NULL;
16168 tree scope = NULL_TREE;
16169 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16170 bool saved_in_declarator_p = parser->in_declarator_p;
16171 bool first = true;
16172 tree pushed_scope = NULL_TREE;
16174 while (true)
16176 /* Peek at the next token. */
16177 token = cp_lexer_peek_token (parser->lexer);
16178 if (token->type == CPP_OPEN_PAREN)
16180 /* This is either a parameter-declaration-clause, or a
16181 parenthesized declarator. When we know we are parsing a
16182 named declarator, it must be a parenthesized declarator
16183 if FIRST is true. For instance, `(int)' is a
16184 parameter-declaration-clause, with an omitted
16185 direct-abstract-declarator. But `((*))', is a
16186 parenthesized abstract declarator. Finally, when T is a
16187 template parameter `(T)' is a
16188 parameter-declaration-clause, and not a parenthesized
16189 named declarator.
16191 We first try and parse a parameter-declaration-clause,
16192 and then try a nested declarator (if FIRST is true).
16194 It is not an error for it not to be a
16195 parameter-declaration-clause, even when FIRST is
16196 false. Consider,
16198 int i (int);
16199 int i (3);
16201 The first is the declaration of a function while the
16202 second is the definition of a variable, including its
16203 initializer.
16205 Having seen only the parenthesis, we cannot know which of
16206 these two alternatives should be selected. Even more
16207 complex are examples like:
16209 int i (int (a));
16210 int i (int (3));
16212 The former is a function-declaration; the latter is a
16213 variable initialization.
16215 Thus again, we try a parameter-declaration-clause, and if
16216 that fails, we back out and return. */
16218 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16220 tree params;
16221 unsigned saved_num_template_parameter_lists;
16222 bool is_declarator = false;
16223 tree t;
16225 /* In a member-declarator, the only valid interpretation
16226 of a parenthesis is the start of a
16227 parameter-declaration-clause. (It is invalid to
16228 initialize a static data member with a parenthesized
16229 initializer; only the "=" form of initialization is
16230 permitted.) */
16231 if (!member_p)
16232 cp_parser_parse_tentatively (parser);
16234 /* Consume the `('. */
16235 cp_lexer_consume_token (parser->lexer);
16236 if (first)
16238 /* If this is going to be an abstract declarator, we're
16239 in a declarator and we can't have default args. */
16240 parser->default_arg_ok_p = false;
16241 parser->in_declarator_p = true;
16244 /* Inside the function parameter list, surrounding
16245 template-parameter-lists do not apply. */
16246 saved_num_template_parameter_lists
16247 = parser->num_template_parameter_lists;
16248 parser->num_template_parameter_lists = 0;
16250 begin_scope (sk_function_parms, NULL_TREE);
16252 /* Parse the parameter-declaration-clause. */
16253 params = cp_parser_parameter_declaration_clause (parser);
16255 parser->num_template_parameter_lists
16256 = saved_num_template_parameter_lists;
16258 /* Consume the `)'. */
16259 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16261 /* If all went well, parse the cv-qualifier-seq and the
16262 exception-specification. */
16263 if (member_p || cp_parser_parse_definitely (parser))
16265 cp_cv_quals cv_quals;
16266 cp_virt_specifiers virt_specifiers;
16267 tree exception_specification;
16268 tree late_return;
16269 tree attrs;
16271 is_declarator = true;
16273 if (ctor_dtor_or_conv_p)
16274 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16275 first = false;
16277 /* Parse the cv-qualifier-seq. */
16278 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16279 /* And the exception-specification. */
16280 exception_specification
16281 = cp_parser_exception_specification_opt (parser);
16283 attrs = cp_parser_std_attribute_spec_seq (parser);
16285 late_return = (cp_parser_late_return_type_opt
16286 (parser, member_p ? cv_quals : -1));
16288 /* Parse the virt-specifier-seq. */
16289 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16291 /* Create the function-declarator. */
16292 declarator = make_call_declarator (declarator,
16293 params,
16294 cv_quals,
16295 virt_specifiers,
16296 exception_specification,
16297 late_return);
16298 declarator->std_attributes = attrs;
16299 /* Any subsequent parameter lists are to do with
16300 return type, so are not those of the declared
16301 function. */
16302 parser->default_arg_ok_p = false;
16305 /* Remove the function parms from scope. */
16306 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16307 pop_binding (DECL_NAME (t), t);
16308 leave_scope();
16310 if (is_declarator)
16311 /* Repeat the main loop. */
16312 continue;
16315 /* If this is the first, we can try a parenthesized
16316 declarator. */
16317 if (first)
16319 bool saved_in_type_id_in_expr_p;
16321 parser->default_arg_ok_p = saved_default_arg_ok_p;
16322 parser->in_declarator_p = saved_in_declarator_p;
16324 /* Consume the `('. */
16325 cp_lexer_consume_token (parser->lexer);
16326 /* Parse the nested declarator. */
16327 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16328 parser->in_type_id_in_expr_p = true;
16329 declarator
16330 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16331 /*parenthesized_p=*/NULL,
16332 member_p);
16333 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16334 first = false;
16335 /* Expect a `)'. */
16336 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16337 declarator = cp_error_declarator;
16338 if (declarator == cp_error_declarator)
16339 break;
16341 goto handle_declarator;
16343 /* Otherwise, we must be done. */
16344 else
16345 break;
16347 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16348 && token->type == CPP_OPEN_SQUARE
16349 && !cp_next_tokens_can_be_attribute_p (parser))
16351 /* Parse an array-declarator. */
16352 tree bounds, attrs;
16354 if (ctor_dtor_or_conv_p)
16355 *ctor_dtor_or_conv_p = 0;
16357 first = false;
16358 parser->default_arg_ok_p = false;
16359 parser->in_declarator_p = true;
16360 /* Consume the `['. */
16361 cp_lexer_consume_token (parser->lexer);
16362 /* Peek at the next token. */
16363 token = cp_lexer_peek_token (parser->lexer);
16364 /* If the next token is `]', then there is no
16365 constant-expression. */
16366 if (token->type != CPP_CLOSE_SQUARE)
16368 bool non_constant_p;
16370 bounds
16371 = cp_parser_constant_expression (parser,
16372 /*allow_non_constant=*/true,
16373 &non_constant_p);
16374 if (!non_constant_p)
16375 /* OK */;
16376 else if (error_operand_p (bounds))
16377 /* Already gave an error. */;
16378 else if (!parser->in_function_body
16379 || current_binding_level->kind == sk_function_parms)
16381 /* Normally, the array bound must be an integral constant
16382 expression. However, as an extension, we allow VLAs
16383 in function scopes as long as they aren't part of a
16384 parameter declaration. */
16385 cp_parser_error (parser,
16386 "array bound is not an integer constant");
16387 bounds = error_mark_node;
16389 else if (processing_template_decl)
16391 /* Remember this wasn't a constant-expression. */
16392 bounds = build_nop (TREE_TYPE (bounds), bounds);
16393 TREE_SIDE_EFFECTS (bounds) = 1;
16396 else
16397 bounds = NULL_TREE;
16398 /* Look for the closing `]'. */
16399 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16401 declarator = cp_error_declarator;
16402 break;
16405 attrs = cp_parser_std_attribute_spec_seq (parser);
16406 declarator = make_array_declarator (declarator, bounds);
16407 declarator->std_attributes = attrs;
16409 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16412 tree qualifying_scope;
16413 tree unqualified_name;
16414 tree attrs;
16415 special_function_kind sfk;
16416 bool abstract_ok;
16417 bool pack_expansion_p = false;
16418 cp_token *declarator_id_start_token;
16420 /* Parse a declarator-id */
16421 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16422 if (abstract_ok)
16424 cp_parser_parse_tentatively (parser);
16426 /* If we see an ellipsis, we should be looking at a
16427 parameter pack. */
16428 if (token->type == CPP_ELLIPSIS)
16430 /* Consume the `...' */
16431 cp_lexer_consume_token (parser->lexer);
16433 pack_expansion_p = true;
16437 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16438 unqualified_name
16439 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16440 qualifying_scope = parser->scope;
16441 if (abstract_ok)
16443 bool okay = false;
16445 if (!unqualified_name && pack_expansion_p)
16447 /* Check whether an error occurred. */
16448 okay = !cp_parser_error_occurred (parser);
16450 /* We already consumed the ellipsis to mark a
16451 parameter pack, but we have no way to report it,
16452 so abort the tentative parse. We will be exiting
16453 immediately anyway. */
16454 cp_parser_abort_tentative_parse (parser);
16456 else
16457 okay = cp_parser_parse_definitely (parser);
16459 if (!okay)
16460 unqualified_name = error_mark_node;
16461 else if (unqualified_name
16462 && (qualifying_scope
16463 || (TREE_CODE (unqualified_name)
16464 != IDENTIFIER_NODE)))
16466 cp_parser_error (parser, "expected unqualified-id");
16467 unqualified_name = error_mark_node;
16471 if (!unqualified_name)
16472 return NULL;
16473 if (unqualified_name == error_mark_node)
16475 declarator = cp_error_declarator;
16476 pack_expansion_p = false;
16477 declarator->parameter_pack_p = false;
16478 break;
16481 attrs = cp_parser_std_attribute_spec_seq (parser);
16483 if (qualifying_scope && at_namespace_scope_p ()
16484 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16486 /* In the declaration of a member of a template class
16487 outside of the class itself, the SCOPE will sometimes
16488 be a TYPENAME_TYPE. For example, given:
16490 template <typename T>
16491 int S<T>::R::i = 3;
16493 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16494 this context, we must resolve S<T>::R to an ordinary
16495 type, rather than a typename type.
16497 The reason we normally avoid resolving TYPENAME_TYPEs
16498 is that a specialization of `S' might render
16499 `S<T>::R' not a type. However, if `S' is
16500 specialized, then this `i' will not be used, so there
16501 is no harm in resolving the types here. */
16502 tree type;
16504 /* Resolve the TYPENAME_TYPE. */
16505 type = resolve_typename_type (qualifying_scope,
16506 /*only_current_p=*/false);
16507 /* If that failed, the declarator is invalid. */
16508 if (TREE_CODE (type) == TYPENAME_TYPE)
16510 if (typedef_variant_p (type))
16511 error_at (declarator_id_start_token->location,
16512 "cannot define member of dependent typedef "
16513 "%qT", type);
16514 else
16515 error_at (declarator_id_start_token->location,
16516 "%<%T::%E%> is not a type",
16517 TYPE_CONTEXT (qualifying_scope),
16518 TYPE_IDENTIFIER (qualifying_scope));
16520 qualifying_scope = type;
16523 sfk = sfk_none;
16525 if (unqualified_name)
16527 tree class_type;
16529 if (qualifying_scope
16530 && CLASS_TYPE_P (qualifying_scope))
16531 class_type = qualifying_scope;
16532 else
16533 class_type = current_class_type;
16535 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16537 tree name_type = TREE_TYPE (unqualified_name);
16538 if (class_type && same_type_p (name_type, class_type))
16540 if (qualifying_scope
16541 && CLASSTYPE_USE_TEMPLATE (name_type))
16543 error_at (declarator_id_start_token->location,
16544 "invalid use of constructor as a template");
16545 inform (declarator_id_start_token->location,
16546 "use %<%T::%D%> instead of %<%T::%D%> to "
16547 "name the constructor in a qualified name",
16548 class_type,
16549 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16550 class_type, name_type);
16551 declarator = cp_error_declarator;
16552 break;
16554 else
16555 unqualified_name = constructor_name (class_type);
16557 else
16559 /* We do not attempt to print the declarator
16560 here because we do not have enough
16561 information about its original syntactic
16562 form. */
16563 cp_parser_error (parser, "invalid declarator");
16564 declarator = cp_error_declarator;
16565 break;
16569 if (class_type)
16571 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16572 sfk = sfk_destructor;
16573 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16574 sfk = sfk_conversion;
16575 else if (/* There's no way to declare a constructor
16576 for an anonymous type, even if the type
16577 got a name for linkage purposes. */
16578 !TYPE_WAS_ANONYMOUS (class_type)
16579 && constructor_name_p (unqualified_name,
16580 class_type))
16582 unqualified_name = constructor_name (class_type);
16583 sfk = sfk_constructor;
16585 else if (is_overloaded_fn (unqualified_name)
16586 && DECL_CONSTRUCTOR_P (get_first_fn
16587 (unqualified_name)))
16588 sfk = sfk_constructor;
16590 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16591 *ctor_dtor_or_conv_p = -1;
16594 declarator = make_id_declarator (qualifying_scope,
16595 unqualified_name,
16596 sfk);
16597 declarator->std_attributes = attrs;
16598 declarator->id_loc = token->location;
16599 declarator->parameter_pack_p = pack_expansion_p;
16601 if (pack_expansion_p)
16602 maybe_warn_variadic_templates ();
16605 handle_declarator:;
16606 scope = get_scope_of_declarator (declarator);
16607 if (scope)
16608 /* Any names that appear after the declarator-id for a
16609 member are looked up in the containing scope. */
16610 pushed_scope = push_scope (scope);
16611 parser->in_declarator_p = true;
16612 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16613 || (declarator && declarator->kind == cdk_id))
16614 /* Default args are only allowed on function
16615 declarations. */
16616 parser->default_arg_ok_p = saved_default_arg_ok_p;
16617 else
16618 parser->default_arg_ok_p = false;
16620 first = false;
16622 /* We're done. */
16623 else
16624 break;
16627 /* For an abstract declarator, we might wind up with nothing at this
16628 point. That's an error; the declarator is not optional. */
16629 if (!declarator)
16630 cp_parser_error (parser, "expected declarator");
16632 /* If we entered a scope, we must exit it now. */
16633 if (pushed_scope)
16634 pop_scope (pushed_scope);
16636 parser->default_arg_ok_p = saved_default_arg_ok_p;
16637 parser->in_declarator_p = saved_in_declarator_p;
16639 return declarator;
16642 /* Parse a ptr-operator.
16644 ptr-operator:
16645 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16646 * cv-qualifier-seq [opt]
16648 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16649 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
16651 GNU Extension:
16653 ptr-operator:
16654 & cv-qualifier-seq [opt]
16656 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16657 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16658 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16659 filled in with the TYPE containing the member. *CV_QUALS is
16660 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16661 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16662 Note that the tree codes returned by this function have nothing
16663 to do with the types of trees that will be eventually be created
16664 to represent the pointer or reference type being parsed. They are
16665 just constants with suggestive names. */
16666 static enum tree_code
16667 cp_parser_ptr_operator (cp_parser* parser,
16668 tree* type,
16669 cp_cv_quals *cv_quals,
16670 tree *attributes)
16672 enum tree_code code = ERROR_MARK;
16673 cp_token *token;
16674 tree attrs = NULL_TREE;
16676 /* Assume that it's not a pointer-to-member. */
16677 *type = NULL_TREE;
16678 /* And that there are no cv-qualifiers. */
16679 *cv_quals = TYPE_UNQUALIFIED;
16681 /* Peek at the next token. */
16682 token = cp_lexer_peek_token (parser->lexer);
16684 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16685 if (token->type == CPP_MULT)
16686 code = INDIRECT_REF;
16687 else if (token->type == CPP_AND)
16688 code = ADDR_EXPR;
16689 else if ((cxx_dialect != cxx98) &&
16690 token->type == CPP_AND_AND) /* C++0x only */
16691 code = NON_LVALUE_EXPR;
16693 if (code != ERROR_MARK)
16695 /* Consume the `*', `&' or `&&'. */
16696 cp_lexer_consume_token (parser->lexer);
16698 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16699 `&', if we are allowing GNU extensions. (The only qualifier
16700 that can legally appear after `&' is `restrict', but that is
16701 enforced during semantic analysis. */
16702 if (code == INDIRECT_REF
16703 || cp_parser_allow_gnu_extensions_p (parser))
16704 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16706 attrs = cp_parser_std_attribute_spec_seq (parser);
16707 if (attributes != NULL)
16708 *attributes = attrs;
16710 else
16712 /* Try the pointer-to-member case. */
16713 cp_parser_parse_tentatively (parser);
16714 /* Look for the optional `::' operator. */
16715 cp_parser_global_scope_opt (parser,
16716 /*current_scope_valid_p=*/false);
16717 /* Look for the nested-name specifier. */
16718 token = cp_lexer_peek_token (parser->lexer);
16719 cp_parser_nested_name_specifier (parser,
16720 /*typename_keyword_p=*/false,
16721 /*check_dependency_p=*/true,
16722 /*type_p=*/false,
16723 /*is_declaration=*/false);
16724 /* If we found it, and the next token is a `*', then we are
16725 indeed looking at a pointer-to-member operator. */
16726 if (!cp_parser_error_occurred (parser)
16727 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16729 /* Indicate that the `*' operator was used. */
16730 code = INDIRECT_REF;
16732 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16733 error_at (token->location, "%qD is a namespace", parser->scope);
16734 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16735 error_at (token->location, "cannot form pointer to member of "
16736 "non-class %q#T", parser->scope);
16737 else
16739 /* The type of which the member is a member is given by the
16740 current SCOPE. */
16741 *type = parser->scope;
16742 /* The next name will not be qualified. */
16743 parser->scope = NULL_TREE;
16744 parser->qualifying_scope = NULL_TREE;
16745 parser->object_scope = NULL_TREE;
16746 /* Look for optional c++11 attributes. */
16747 attrs = cp_parser_std_attribute_spec_seq (parser);
16748 if (attributes != NULL)
16749 *attributes = attrs;
16750 /* Look for the optional cv-qualifier-seq. */
16751 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16754 /* If that didn't work we don't have a ptr-operator. */
16755 if (!cp_parser_parse_definitely (parser))
16756 cp_parser_error (parser, "expected ptr-operator");
16759 return code;
16762 /* Parse an (optional) cv-qualifier-seq.
16764 cv-qualifier-seq:
16765 cv-qualifier cv-qualifier-seq [opt]
16767 cv-qualifier:
16768 const
16769 volatile
16771 GNU Extension:
16773 cv-qualifier:
16774 __restrict__
16776 Returns a bitmask representing the cv-qualifiers. */
16778 static cp_cv_quals
16779 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16781 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16783 while (true)
16785 cp_token *token;
16786 cp_cv_quals cv_qualifier;
16788 /* Peek at the next token. */
16789 token = cp_lexer_peek_token (parser->lexer);
16790 /* See if it's a cv-qualifier. */
16791 switch (token->keyword)
16793 case RID_CONST:
16794 cv_qualifier = TYPE_QUAL_CONST;
16795 break;
16797 case RID_VOLATILE:
16798 cv_qualifier = TYPE_QUAL_VOLATILE;
16799 break;
16801 case RID_RESTRICT:
16802 cv_qualifier = TYPE_QUAL_RESTRICT;
16803 break;
16805 default:
16806 cv_qualifier = TYPE_UNQUALIFIED;
16807 break;
16810 if (!cv_qualifier)
16811 break;
16813 if (cv_quals & cv_qualifier)
16815 error_at (token->location, "duplicate cv-qualifier");
16816 cp_lexer_purge_token (parser->lexer);
16818 else
16820 cp_lexer_consume_token (parser->lexer);
16821 cv_quals |= cv_qualifier;
16825 return cv_quals;
16828 /* Parse an (optional) virt-specifier-seq.
16830 virt-specifier-seq:
16831 virt-specifier virt-specifier-seq [opt]
16833 virt-specifier:
16834 override
16835 final
16837 Returns a bitmask representing the virt-specifiers. */
16839 static cp_virt_specifiers
16840 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16842 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16844 while (true)
16846 cp_token *token;
16847 cp_virt_specifiers virt_specifier;
16849 /* Peek at the next token. */
16850 token = cp_lexer_peek_token (parser->lexer);
16851 /* See if it's a virt-specifier-qualifier. */
16852 if (token->type != CPP_NAME)
16853 break;
16854 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16856 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16857 virt_specifier = VIRT_SPEC_OVERRIDE;
16859 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16861 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16862 virt_specifier = VIRT_SPEC_FINAL;
16864 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16866 virt_specifier = VIRT_SPEC_FINAL;
16868 else
16869 break;
16871 if (virt_specifiers & virt_specifier)
16873 error_at (token->location, "duplicate virt-specifier");
16874 cp_lexer_purge_token (parser->lexer);
16876 else
16878 cp_lexer_consume_token (parser->lexer);
16879 virt_specifiers |= virt_specifier;
16882 return virt_specifiers;
16885 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16886 is in scope even though it isn't real. */
16888 static void
16889 inject_this_parameter (tree ctype, cp_cv_quals quals)
16891 tree this_parm;
16893 if (current_class_ptr)
16895 /* We don't clear this between NSDMIs. Is it already what we want? */
16896 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16897 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16898 && cp_type_quals (type) == quals)
16899 return;
16902 this_parm = build_this_parm (ctype, quals);
16903 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16904 current_class_ptr = NULL_TREE;
16905 current_class_ref
16906 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16907 current_class_ptr = this_parm;
16910 /* Parse a late-specified return type, if any. This is not a separate
16911 non-terminal, but part of a function declarator, which looks like
16913 -> trailing-type-specifier-seq abstract-declarator(opt)
16915 Returns the type indicated by the type-id.
16917 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16918 function. */
16920 static tree
16921 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16923 cp_token *token;
16924 tree type;
16926 /* Peek at the next token. */
16927 token = cp_lexer_peek_token (parser->lexer);
16928 /* A late-specified return type is indicated by an initial '->'. */
16929 if (token->type != CPP_DEREF)
16930 return NULL_TREE;
16932 /* Consume the ->. */
16933 cp_lexer_consume_token (parser->lexer);
16935 if (quals >= 0)
16937 /* DR 1207: 'this' is in scope in the trailing return type. */
16938 gcc_assert (current_class_ptr == NULL_TREE);
16939 inject_this_parameter (current_class_type, quals);
16942 type = cp_parser_trailing_type_id (parser);
16944 if (quals >= 0)
16945 current_class_ptr = current_class_ref = NULL_TREE;
16947 return type;
16950 /* Parse a declarator-id.
16952 declarator-id:
16953 id-expression
16954 :: [opt] nested-name-specifier [opt] type-name
16956 In the `id-expression' case, the value returned is as for
16957 cp_parser_id_expression if the id-expression was an unqualified-id.
16958 If the id-expression was a qualified-id, then a SCOPE_REF is
16959 returned. The first operand is the scope (either a NAMESPACE_DECL
16960 or TREE_TYPE), but the second is still just a representation of an
16961 unqualified-id. */
16963 static tree
16964 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16966 tree id;
16967 /* The expression must be an id-expression. Assume that qualified
16968 names are the names of types so that:
16970 template <class T>
16971 int S<T>::R::i = 3;
16973 will work; we must treat `S<T>::R' as the name of a type.
16974 Similarly, assume that qualified names are templates, where
16975 required, so that:
16977 template <class T>
16978 int S<T>::R<T>::i = 3;
16980 will work, too. */
16981 id = cp_parser_id_expression (parser,
16982 /*template_keyword_p=*/false,
16983 /*check_dependency_p=*/false,
16984 /*template_p=*/NULL,
16985 /*declarator_p=*/true,
16986 optional_p);
16987 if (id && BASELINK_P (id))
16988 id = BASELINK_FUNCTIONS (id);
16989 return id;
16992 /* Parse a type-id.
16994 type-id:
16995 type-specifier-seq abstract-declarator [opt]
16997 Returns the TYPE specified. */
16999 static tree
17000 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
17001 bool is_trailing_return)
17003 cp_decl_specifier_seq type_specifier_seq;
17004 cp_declarator *abstract_declarator;
17006 /* Parse the type-specifier-seq. */
17007 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17008 is_trailing_return,
17009 &type_specifier_seq);
17010 if (type_specifier_seq.type == error_mark_node)
17011 return error_mark_node;
17013 /* There might or might not be an abstract declarator. */
17014 cp_parser_parse_tentatively (parser);
17015 /* Look for the declarator. */
17016 abstract_declarator
17017 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
17018 /*parenthesized_p=*/NULL,
17019 /*member_p=*/false);
17020 /* Check to see if there really was a declarator. */
17021 if (!cp_parser_parse_definitely (parser))
17022 abstract_declarator = NULL;
17024 if (type_specifier_seq.type
17025 && type_uses_auto (type_specifier_seq.type))
17027 /* A type-id with type 'auto' is only ok if the abstract declarator
17028 is a function declarator with a late-specified return type. */
17029 if (abstract_declarator
17030 && abstract_declarator->kind == cdk_function
17031 && abstract_declarator->u.function.late_return_type)
17032 /* OK */;
17033 else
17035 error ("invalid use of %<auto%>");
17036 return error_mark_node;
17040 return groktypename (&type_specifier_seq, abstract_declarator,
17041 is_template_arg);
17044 static tree cp_parser_type_id (cp_parser *parser)
17046 return cp_parser_type_id_1 (parser, false, false);
17049 static tree cp_parser_template_type_arg (cp_parser *parser)
17051 tree r;
17052 const char *saved_message = parser->type_definition_forbidden_message;
17053 parser->type_definition_forbidden_message
17054 = G_("types may not be defined in template arguments");
17055 r = cp_parser_type_id_1 (parser, true, false);
17056 parser->type_definition_forbidden_message = saved_message;
17057 return r;
17060 static tree cp_parser_trailing_type_id (cp_parser *parser)
17062 return cp_parser_type_id_1 (parser, false, true);
17065 /* Parse a type-specifier-seq.
17067 type-specifier-seq:
17068 type-specifier type-specifier-seq [opt]
17070 GNU extension:
17072 type-specifier-seq:
17073 attributes type-specifier-seq [opt]
17075 If IS_DECLARATION is true, we are at the start of a "condition" or
17076 exception-declaration, so we might be followed by a declarator-id.
17078 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
17079 i.e. we've just seen "->".
17081 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
17083 static void
17084 cp_parser_type_specifier_seq (cp_parser* parser,
17085 bool is_declaration,
17086 bool is_trailing_return,
17087 cp_decl_specifier_seq *type_specifier_seq)
17089 bool seen_type_specifier = false;
17090 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
17091 cp_token *start_token = NULL;
17093 /* Clear the TYPE_SPECIFIER_SEQ. */
17094 clear_decl_specs (type_specifier_seq);
17096 /* In the context of a trailing return type, enum E { } is an
17097 elaborated-type-specifier followed by a function-body, not an
17098 enum-specifier. */
17099 if (is_trailing_return)
17100 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
17102 /* Parse the type-specifiers and attributes. */
17103 while (true)
17105 tree type_specifier;
17106 bool is_cv_qualifier;
17108 /* Check for attributes first. */
17109 if (cp_next_tokens_can_be_attribute_p (parser))
17111 type_specifier_seq->attributes =
17112 chainon (type_specifier_seq->attributes,
17113 cp_parser_attributes_opt (parser));
17114 continue;
17117 /* record the token of the beginning of the type specifier seq,
17118 for error reporting purposes*/
17119 if (!start_token)
17120 start_token = cp_lexer_peek_token (parser->lexer);
17122 /* Look for the type-specifier. */
17123 type_specifier = cp_parser_type_specifier (parser,
17124 flags,
17125 type_specifier_seq,
17126 /*is_declaration=*/false,
17127 NULL,
17128 &is_cv_qualifier);
17129 if (!type_specifier)
17131 /* If the first type-specifier could not be found, this is not a
17132 type-specifier-seq at all. */
17133 if (!seen_type_specifier)
17135 cp_parser_error (parser, "expected type-specifier");
17136 type_specifier_seq->type = error_mark_node;
17137 return;
17139 /* If subsequent type-specifiers could not be found, the
17140 type-specifier-seq is complete. */
17141 break;
17144 seen_type_specifier = true;
17145 /* The standard says that a condition can be:
17147 type-specifier-seq declarator = assignment-expression
17149 However, given:
17151 struct S {};
17152 if (int S = ...)
17154 we should treat the "S" as a declarator, not as a
17155 type-specifier. The standard doesn't say that explicitly for
17156 type-specifier-seq, but it does say that for
17157 decl-specifier-seq in an ordinary declaration. Perhaps it
17158 would be clearer just to allow a decl-specifier-seq here, and
17159 then add a semantic restriction that if any decl-specifiers
17160 that are not type-specifiers appear, the program is invalid. */
17161 if (is_declaration && !is_cv_qualifier)
17162 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
17166 /* Parse a parameter-declaration-clause.
17168 parameter-declaration-clause:
17169 parameter-declaration-list [opt] ... [opt]
17170 parameter-declaration-list , ...
17172 Returns a representation for the parameter declarations. A return
17173 value of NULL indicates a parameter-declaration-clause consisting
17174 only of an ellipsis. */
17176 static tree
17177 cp_parser_parameter_declaration_clause (cp_parser* parser)
17179 tree parameters;
17180 cp_token *token;
17181 bool ellipsis_p;
17182 bool is_error;
17184 /* Peek at the next token. */
17185 token = cp_lexer_peek_token (parser->lexer);
17186 /* Check for trivial parameter-declaration-clauses. */
17187 if (token->type == CPP_ELLIPSIS)
17189 /* Consume the `...' token. */
17190 cp_lexer_consume_token (parser->lexer);
17191 return NULL_TREE;
17193 else if (token->type == CPP_CLOSE_PAREN)
17194 /* There are no parameters. */
17196 #ifndef NO_IMPLICIT_EXTERN_C
17197 if (in_system_header && current_class_type == NULL
17198 && current_lang_name == lang_name_c)
17199 return NULL_TREE;
17200 else
17201 #endif
17202 return void_list_node;
17204 /* Check for `(void)', too, which is a special case. */
17205 else if (token->keyword == RID_VOID
17206 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
17207 == CPP_CLOSE_PAREN))
17209 /* Consume the `void' token. */
17210 cp_lexer_consume_token (parser->lexer);
17211 /* There are no parameters. */
17212 return void_list_node;
17215 /* Parse the parameter-declaration-list. */
17216 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
17217 /* If a parse error occurred while parsing the
17218 parameter-declaration-list, then the entire
17219 parameter-declaration-clause is erroneous. */
17220 if (is_error)
17221 return NULL;
17223 /* Peek at the next token. */
17224 token = cp_lexer_peek_token (parser->lexer);
17225 /* If it's a `,', the clause should terminate with an ellipsis. */
17226 if (token->type == CPP_COMMA)
17228 /* Consume the `,'. */
17229 cp_lexer_consume_token (parser->lexer);
17230 /* Expect an ellipsis. */
17231 ellipsis_p
17232 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
17234 /* It might also be `...' if the optional trailing `,' was
17235 omitted. */
17236 else if (token->type == CPP_ELLIPSIS)
17238 /* Consume the `...' token. */
17239 cp_lexer_consume_token (parser->lexer);
17240 /* And remember that we saw it. */
17241 ellipsis_p = true;
17243 else
17244 ellipsis_p = false;
17246 /* Finish the parameter list. */
17247 if (!ellipsis_p)
17248 parameters = chainon (parameters, void_list_node);
17250 return parameters;
17253 /* Parse a parameter-declaration-list.
17255 parameter-declaration-list:
17256 parameter-declaration
17257 parameter-declaration-list , parameter-declaration
17259 Returns a representation of the parameter-declaration-list, as for
17260 cp_parser_parameter_declaration_clause. However, the
17261 `void_list_node' is never appended to the list. Upon return,
17262 *IS_ERROR will be true iff an error occurred. */
17264 static tree
17265 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17267 tree parameters = NULL_TREE;
17268 tree *tail = &parameters;
17269 bool saved_in_unbraced_linkage_specification_p;
17270 int index = 0;
17272 /* Assume all will go well. */
17273 *is_error = false;
17274 /* The special considerations that apply to a function within an
17275 unbraced linkage specifications do not apply to the parameters
17276 to the function. */
17277 saved_in_unbraced_linkage_specification_p
17278 = parser->in_unbraced_linkage_specification_p;
17279 parser->in_unbraced_linkage_specification_p = false;
17281 /* Look for more parameters. */
17282 while (true)
17284 cp_parameter_declarator *parameter;
17285 tree decl = error_mark_node;
17286 bool parenthesized_p = false;
17287 /* Parse the parameter. */
17288 parameter
17289 = cp_parser_parameter_declaration (parser,
17290 /*template_parm_p=*/false,
17291 &parenthesized_p);
17293 /* We don't know yet if the enclosing context is deprecated, so wait
17294 and warn in grokparms if appropriate. */
17295 deprecated_state = DEPRECATED_SUPPRESS;
17297 if (parameter)
17298 decl = grokdeclarator (parameter->declarator,
17299 &parameter->decl_specifiers,
17300 PARM,
17301 parameter->default_argument != NULL_TREE,
17302 &parameter->decl_specifiers.attributes);
17304 deprecated_state = DEPRECATED_NORMAL;
17306 /* If a parse error occurred parsing the parameter declaration,
17307 then the entire parameter-declaration-list is erroneous. */
17308 if (decl == error_mark_node)
17310 *is_error = true;
17311 parameters = error_mark_node;
17312 break;
17315 if (parameter->decl_specifiers.attributes)
17316 cplus_decl_attributes (&decl,
17317 parameter->decl_specifiers.attributes,
17319 if (DECL_NAME (decl))
17320 decl = pushdecl (decl);
17322 if (decl != error_mark_node)
17324 retrofit_lang_decl (decl);
17325 DECL_PARM_INDEX (decl) = ++index;
17326 DECL_PARM_LEVEL (decl) = function_parm_depth ();
17329 /* Add the new parameter to the list. */
17330 *tail = build_tree_list (parameter->default_argument, decl);
17331 tail = &TREE_CHAIN (*tail);
17333 /* Peek at the next token. */
17334 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17335 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17336 /* These are for Objective-C++ */
17337 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17338 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17339 /* The parameter-declaration-list is complete. */
17340 break;
17341 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17343 cp_token *token;
17345 /* Peek at the next token. */
17346 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17347 /* If it's an ellipsis, then the list is complete. */
17348 if (token->type == CPP_ELLIPSIS)
17349 break;
17350 /* Otherwise, there must be more parameters. Consume the
17351 `,'. */
17352 cp_lexer_consume_token (parser->lexer);
17353 /* When parsing something like:
17355 int i(float f, double d)
17357 we can tell after seeing the declaration for "f" that we
17358 are not looking at an initialization of a variable "i",
17359 but rather at the declaration of a function "i".
17361 Due to the fact that the parsing of template arguments
17362 (as specified to a template-id) requires backtracking we
17363 cannot use this technique when inside a template argument
17364 list. */
17365 if (!parser->in_template_argument_list_p
17366 && !parser->in_type_id_in_expr_p
17367 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17368 /* However, a parameter-declaration of the form
17369 "foat(f)" (which is a valid declaration of a
17370 parameter "f") can also be interpreted as an
17371 expression (the conversion of "f" to "float"). */
17372 && !parenthesized_p)
17373 cp_parser_commit_to_tentative_parse (parser);
17375 else
17377 cp_parser_error (parser, "expected %<,%> or %<...%>");
17378 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17379 cp_parser_skip_to_closing_parenthesis (parser,
17380 /*recovering=*/true,
17381 /*or_comma=*/false,
17382 /*consume_paren=*/false);
17383 break;
17387 parser->in_unbraced_linkage_specification_p
17388 = saved_in_unbraced_linkage_specification_p;
17390 return parameters;
17393 /* Parse a parameter declaration.
17395 parameter-declaration:
17396 decl-specifier-seq ... [opt] declarator
17397 decl-specifier-seq declarator = assignment-expression
17398 decl-specifier-seq ... [opt] abstract-declarator [opt]
17399 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17401 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17402 declares a template parameter. (In that case, a non-nested `>'
17403 token encountered during the parsing of the assignment-expression
17404 is not interpreted as a greater-than operator.)
17406 Returns a representation of the parameter, or NULL if an error
17407 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17408 true iff the declarator is of the form "(p)". */
17410 static cp_parameter_declarator *
17411 cp_parser_parameter_declaration (cp_parser *parser,
17412 bool template_parm_p,
17413 bool *parenthesized_p)
17415 int declares_class_or_enum;
17416 cp_decl_specifier_seq decl_specifiers;
17417 cp_declarator *declarator;
17418 tree default_argument;
17419 cp_token *token = NULL, *declarator_token_start = NULL;
17420 const char *saved_message;
17422 /* In a template parameter, `>' is not an operator.
17424 [temp.param]
17426 When parsing a default template-argument for a non-type
17427 template-parameter, the first non-nested `>' is taken as the end
17428 of the template parameter-list rather than a greater-than
17429 operator. */
17431 /* Type definitions may not appear in parameter types. */
17432 saved_message = parser->type_definition_forbidden_message;
17433 parser->type_definition_forbidden_message
17434 = G_("types may not be defined in parameter types");
17436 /* Parse the declaration-specifiers. */
17437 cp_parser_decl_specifier_seq (parser,
17438 CP_PARSER_FLAGS_NONE,
17439 &decl_specifiers,
17440 &declares_class_or_enum);
17442 /* Complain about missing 'typename' or other invalid type names. */
17443 if (!decl_specifiers.any_type_specifiers_p)
17444 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17446 /* If an error occurred, there's no reason to attempt to parse the
17447 rest of the declaration. */
17448 if (cp_parser_error_occurred (parser))
17450 parser->type_definition_forbidden_message = saved_message;
17451 return NULL;
17454 /* Peek at the next token. */
17455 token = cp_lexer_peek_token (parser->lexer);
17457 /* If the next token is a `)', `,', `=', `>', or `...', then there
17458 is no declarator. However, when variadic templates are enabled,
17459 there may be a declarator following `...'. */
17460 if (token->type == CPP_CLOSE_PAREN
17461 || token->type == CPP_COMMA
17462 || token->type == CPP_EQ
17463 || token->type == CPP_GREATER)
17465 declarator = NULL;
17466 if (parenthesized_p)
17467 *parenthesized_p = false;
17469 /* Otherwise, there should be a declarator. */
17470 else
17472 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17473 parser->default_arg_ok_p = false;
17475 /* After seeing a decl-specifier-seq, if the next token is not a
17476 "(", there is no possibility that the code is a valid
17477 expression. Therefore, if parsing tentatively, we commit at
17478 this point. */
17479 if (!parser->in_template_argument_list_p
17480 /* In an expression context, having seen:
17482 (int((char ...
17484 we cannot be sure whether we are looking at a
17485 function-type (taking a "char" as a parameter) or a cast
17486 of some object of type "char" to "int". */
17487 && !parser->in_type_id_in_expr_p
17488 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17489 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17490 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17491 cp_parser_commit_to_tentative_parse (parser);
17492 /* Parse the declarator. */
17493 declarator_token_start = token;
17494 declarator = cp_parser_declarator (parser,
17495 CP_PARSER_DECLARATOR_EITHER,
17496 /*ctor_dtor_or_conv_p=*/NULL,
17497 parenthesized_p,
17498 /*member_p=*/false);
17499 parser->default_arg_ok_p = saved_default_arg_ok_p;
17500 /* After the declarator, allow more attributes. */
17501 decl_specifiers.attributes
17502 = chainon (decl_specifiers.attributes,
17503 cp_parser_attributes_opt (parser));
17506 /* If the next token is an ellipsis, and we have not seen a
17507 declarator name, and the type of the declarator contains parameter
17508 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17509 a parameter pack expansion expression. Otherwise, leave the
17510 ellipsis for a C-style variadic function. */
17511 token = cp_lexer_peek_token (parser->lexer);
17512 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17514 tree type = decl_specifiers.type;
17516 if (type && DECL_P (type))
17517 type = TREE_TYPE (type);
17519 if (type
17520 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17521 && declarator_can_be_parameter_pack (declarator)
17522 && (!declarator || !declarator->parameter_pack_p)
17523 && uses_parameter_packs (type))
17525 /* Consume the `...'. */
17526 cp_lexer_consume_token (parser->lexer);
17527 maybe_warn_variadic_templates ();
17529 /* Build a pack expansion type */
17530 if (declarator)
17531 declarator->parameter_pack_p = true;
17532 else
17533 decl_specifiers.type = make_pack_expansion (type);
17537 /* The restriction on defining new types applies only to the type
17538 of the parameter, not to the default argument. */
17539 parser->type_definition_forbidden_message = saved_message;
17541 /* If the next token is `=', then process a default argument. */
17542 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17544 token = cp_lexer_peek_token (parser->lexer);
17545 /* If we are defining a class, then the tokens that make up the
17546 default argument must be saved and processed later. */
17547 if (!template_parm_p && at_class_scope_p ()
17548 && TYPE_BEING_DEFINED (current_class_type)
17549 && !LAMBDA_TYPE_P (current_class_type))
17550 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17551 /* Outside of a class definition, we can just parse the
17552 assignment-expression. */
17553 else
17554 default_argument
17555 = cp_parser_default_argument (parser, template_parm_p);
17557 if (!parser->default_arg_ok_p)
17559 if (flag_permissive)
17560 warning (0, "deprecated use of default argument for parameter of non-function");
17561 else
17563 error_at (token->location,
17564 "default arguments are only "
17565 "permitted for function parameters");
17566 default_argument = NULL_TREE;
17569 else if ((declarator && declarator->parameter_pack_p)
17570 || (decl_specifiers.type
17571 && PACK_EXPANSION_P (decl_specifiers.type)))
17573 /* Find the name of the parameter pack. */
17574 cp_declarator *id_declarator = declarator;
17575 while (id_declarator && id_declarator->kind != cdk_id)
17576 id_declarator = id_declarator->declarator;
17578 if (id_declarator && id_declarator->kind == cdk_id)
17579 error_at (declarator_token_start->location,
17580 template_parm_p
17581 ? G_("template parameter pack %qD "
17582 "cannot have a default argument")
17583 : G_("parameter pack %qD cannot have "
17584 "a default argument"),
17585 id_declarator->u.id.unqualified_name);
17586 else
17587 error_at (declarator_token_start->location,
17588 template_parm_p
17589 ? G_("template parameter pack cannot have "
17590 "a default argument")
17591 : G_("parameter pack cannot have a "
17592 "default argument"));
17594 default_argument = NULL_TREE;
17597 else
17598 default_argument = NULL_TREE;
17600 return make_parameter_declarator (&decl_specifiers,
17601 declarator,
17602 default_argument);
17605 /* Parse a default argument and return it.
17607 TEMPLATE_PARM_P is true if this is a default argument for a
17608 non-type template parameter. */
17609 static tree
17610 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17612 tree default_argument = NULL_TREE;
17613 bool saved_greater_than_is_operator_p;
17614 bool saved_local_variables_forbidden_p;
17615 bool non_constant_p, is_direct_init;
17617 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17618 set correctly. */
17619 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17620 parser->greater_than_is_operator_p = !template_parm_p;
17621 /* Local variable names (and the `this' keyword) may not
17622 appear in a default argument. */
17623 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17624 parser->local_variables_forbidden_p = true;
17625 /* Parse the assignment-expression. */
17626 if (template_parm_p)
17627 push_deferring_access_checks (dk_no_deferred);
17628 default_argument
17629 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17630 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17631 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17632 if (template_parm_p)
17633 pop_deferring_access_checks ();
17634 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17635 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17637 return default_argument;
17640 /* Parse a function-body.
17642 function-body:
17643 compound_statement */
17645 static void
17646 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
17648 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
17651 /* Parse a ctor-initializer-opt followed by a function-body. Return
17652 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
17653 is true we are parsing a function-try-block. */
17655 static bool
17656 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
17657 bool in_function_try_block)
17659 tree body, list;
17660 bool ctor_initializer_p;
17661 const bool check_body_p =
17662 DECL_CONSTRUCTOR_P (current_function_decl)
17663 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17664 tree last = NULL;
17666 /* Begin the function body. */
17667 body = begin_function_body ();
17668 /* Parse the optional ctor-initializer. */
17669 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17671 /* If we're parsing a constexpr constructor definition, we need
17672 to check that the constructor body is indeed empty. However,
17673 before we get to cp_parser_function_body lot of junk has been
17674 generated, so we can't just check that we have an empty block.
17675 Rather we take a snapshot of the outermost block, and check whether
17676 cp_parser_function_body changed its state. */
17677 if (check_body_p)
17679 list = cur_stmt_list;
17680 if (STATEMENT_LIST_TAIL (list))
17681 last = STATEMENT_LIST_TAIL (list)->stmt;
17683 /* Parse the function-body. */
17684 cp_parser_function_body (parser, in_function_try_block);
17685 if (check_body_p)
17686 check_constexpr_ctor_body (last, list);
17687 /* Finish the function body. */
17688 finish_function_body (body);
17690 return ctor_initializer_p;
17693 /* Parse an initializer.
17695 initializer:
17696 = initializer-clause
17697 ( expression-list )
17699 Returns an expression representing the initializer. If no
17700 initializer is present, NULL_TREE is returned.
17702 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17703 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17704 set to TRUE if there is no initializer present. If there is an
17705 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17706 is set to true; otherwise it is set to false. */
17708 static tree
17709 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17710 bool* non_constant_p)
17712 cp_token *token;
17713 tree init;
17715 /* Peek at the next token. */
17716 token = cp_lexer_peek_token (parser->lexer);
17718 /* Let our caller know whether or not this initializer was
17719 parenthesized. */
17720 *is_direct_init = (token->type != CPP_EQ);
17721 /* Assume that the initializer is constant. */
17722 *non_constant_p = false;
17724 if (token->type == CPP_EQ)
17726 /* Consume the `='. */
17727 cp_lexer_consume_token (parser->lexer);
17728 /* Parse the initializer-clause. */
17729 init = cp_parser_initializer_clause (parser, non_constant_p);
17731 else if (token->type == CPP_OPEN_PAREN)
17733 vec<tree, va_gc> *vec;
17734 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17735 /*cast_p=*/false,
17736 /*allow_expansion_p=*/true,
17737 non_constant_p);
17738 if (vec == NULL)
17739 return error_mark_node;
17740 init = build_tree_list_vec (vec);
17741 release_tree_vector (vec);
17743 else if (token->type == CPP_OPEN_BRACE)
17745 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17746 init = cp_parser_braced_list (parser, non_constant_p);
17747 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17749 else
17751 /* Anything else is an error. */
17752 cp_parser_error (parser, "expected initializer");
17753 init = error_mark_node;
17756 return init;
17759 /* Parse an initializer-clause.
17761 initializer-clause:
17762 assignment-expression
17763 braced-init-list
17765 Returns an expression representing the initializer.
17767 If the `assignment-expression' production is used the value
17768 returned is simply a representation for the expression.
17770 Otherwise, calls cp_parser_braced_list. */
17772 static tree
17773 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17775 tree initializer;
17777 /* Assume the expression is constant. */
17778 *non_constant_p = false;
17780 /* If it is not a `{', then we are looking at an
17781 assignment-expression. */
17782 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17784 initializer
17785 = cp_parser_constant_expression (parser,
17786 /*allow_non_constant_p=*/true,
17787 non_constant_p);
17789 else
17790 initializer = cp_parser_braced_list (parser, non_constant_p);
17792 return initializer;
17795 /* Parse a brace-enclosed initializer list.
17797 braced-init-list:
17798 { initializer-list , [opt] }
17801 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17802 the elements of the initializer-list (or NULL, if the last
17803 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17804 NULL_TREE. There is no way to detect whether or not the optional
17805 trailing `,' was provided. NON_CONSTANT_P is as for
17806 cp_parser_initializer. */
17808 static tree
17809 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17811 tree initializer;
17813 /* Consume the `{' token. */
17814 cp_lexer_consume_token (parser->lexer);
17815 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17816 initializer = make_node (CONSTRUCTOR);
17817 /* If it's not a `}', then there is a non-trivial initializer. */
17818 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17820 /* Parse the initializer list. */
17821 CONSTRUCTOR_ELTS (initializer)
17822 = cp_parser_initializer_list (parser, non_constant_p);
17823 /* A trailing `,' token is allowed. */
17824 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17825 cp_lexer_consume_token (parser->lexer);
17827 /* Now, there should be a trailing `}'. */
17828 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17829 TREE_TYPE (initializer) = init_list_type_node;
17830 return initializer;
17833 /* Parse an initializer-list.
17835 initializer-list:
17836 initializer-clause ... [opt]
17837 initializer-list , initializer-clause ... [opt]
17839 GNU Extension:
17841 initializer-list:
17842 designation initializer-clause ...[opt]
17843 initializer-list , designation initializer-clause ...[opt]
17845 designation:
17846 . identifier =
17847 identifier :
17848 [ constant-expression ] =
17850 Returns a vec of constructor_elt. The VALUE of each elt is an expression
17851 for the initializer. If the INDEX of the elt is non-NULL, it is the
17852 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17853 as for cp_parser_initializer. */
17855 static vec<constructor_elt, va_gc> *
17856 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17858 vec<constructor_elt, va_gc> *v = NULL;
17860 /* Assume all of the expressions are constant. */
17861 *non_constant_p = false;
17863 /* Parse the rest of the list. */
17864 while (true)
17866 cp_token *token;
17867 tree designator;
17868 tree initializer;
17869 bool clause_non_constant_p;
17871 /* If the next token is an identifier and the following one is a
17872 colon, we are looking at the GNU designated-initializer
17873 syntax. */
17874 if (cp_parser_allow_gnu_extensions_p (parser)
17875 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17876 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17878 /* Warn the user that they are using an extension. */
17879 pedwarn (input_location, OPT_Wpedantic,
17880 "ISO C++ does not allow designated initializers");
17881 /* Consume the identifier. */
17882 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17883 /* Consume the `:'. */
17884 cp_lexer_consume_token (parser->lexer);
17886 /* Also handle the C99 syntax, '. id ='. */
17887 else if (cp_parser_allow_gnu_extensions_p (parser)
17888 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17889 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17890 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17892 /* Warn the user that they are using an extension. */
17893 pedwarn (input_location, OPT_Wpedantic,
17894 "ISO C++ does not allow C99 designated initializers");
17895 /* Consume the `.'. */
17896 cp_lexer_consume_token (parser->lexer);
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 C99 array designators, '[ const ] ='. */
17903 else if (cp_parser_allow_gnu_extensions_p (parser)
17904 && !c_dialect_objc ()
17905 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17907 /* In C++11, [ could start a lambda-introducer. */
17908 cp_parser_parse_tentatively (parser);
17909 cp_lexer_consume_token (parser->lexer);
17910 designator = cp_parser_constant_expression (parser, false, NULL);
17911 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17912 cp_parser_require (parser, CPP_EQ, RT_EQ);
17913 if (!cp_parser_parse_definitely (parser))
17914 designator = NULL_TREE;
17916 else
17917 designator = NULL_TREE;
17919 /* Parse the initializer. */
17920 initializer = cp_parser_initializer_clause (parser,
17921 &clause_non_constant_p);
17922 /* If any clause is non-constant, so is the entire initializer. */
17923 if (clause_non_constant_p)
17924 *non_constant_p = true;
17926 /* If we have an ellipsis, this is an initializer pack
17927 expansion. */
17928 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17930 /* Consume the `...'. */
17931 cp_lexer_consume_token (parser->lexer);
17933 /* Turn the initializer into an initializer expansion. */
17934 initializer = make_pack_expansion (initializer);
17937 /* Add it to the vector. */
17938 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17940 /* If the next token is not a comma, we have reached the end of
17941 the list. */
17942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17943 break;
17945 /* Peek at the next token. */
17946 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17947 /* If the next token is a `}', then we're still done. An
17948 initializer-clause can have a trailing `,' after the
17949 initializer-list and before the closing `}'. */
17950 if (token->type == CPP_CLOSE_BRACE)
17951 break;
17953 /* Consume the `,' token. */
17954 cp_lexer_consume_token (parser->lexer);
17957 return v;
17960 /* Classes [gram.class] */
17962 /* Parse a class-name.
17964 class-name:
17965 identifier
17966 template-id
17968 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17969 to indicate that names looked up in dependent types should be
17970 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17971 keyword has been used to indicate that the name that appears next
17972 is a template. TAG_TYPE indicates the explicit tag given before
17973 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17974 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17975 is the class being defined in a class-head.
17977 Returns the TYPE_DECL representing the class. */
17979 static tree
17980 cp_parser_class_name (cp_parser *parser,
17981 bool typename_keyword_p,
17982 bool template_keyword_p,
17983 enum tag_types tag_type,
17984 bool check_dependency_p,
17985 bool class_head_p,
17986 bool is_declaration)
17988 tree decl;
17989 tree scope;
17990 bool typename_p;
17991 cp_token *token;
17992 tree identifier = NULL_TREE;
17994 /* All class-names start with an identifier. */
17995 token = cp_lexer_peek_token (parser->lexer);
17996 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17998 cp_parser_error (parser, "expected class-name");
17999 return error_mark_node;
18002 /* PARSER->SCOPE can be cleared when parsing the template-arguments
18003 to a template-id, so we save it here. */
18004 scope = parser->scope;
18005 if (scope == error_mark_node)
18006 return error_mark_node;
18008 /* Any name names a type if we're following the `typename' keyword
18009 in a qualified name where the enclosing scope is type-dependent. */
18010 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
18011 && dependent_type_p (scope));
18012 /* Handle the common case (an identifier, but not a template-id)
18013 efficiently. */
18014 if (token->type == CPP_NAME
18015 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
18017 cp_token *identifier_token;
18018 bool ambiguous_p;
18020 /* Look for the identifier. */
18021 identifier_token = cp_lexer_peek_token (parser->lexer);
18022 ambiguous_p = identifier_token->ambiguous_p;
18023 identifier = cp_parser_identifier (parser);
18024 /* If the next token isn't an identifier, we are certainly not
18025 looking at a class-name. */
18026 if (identifier == error_mark_node)
18027 decl = error_mark_node;
18028 /* If we know this is a type-name, there's no need to look it
18029 up. */
18030 else if (typename_p)
18031 decl = identifier;
18032 else
18034 tree ambiguous_decls;
18035 /* If we already know that this lookup is ambiguous, then
18036 we've already issued an error message; there's no reason
18037 to check again. */
18038 if (ambiguous_p)
18040 cp_parser_simulate_error (parser);
18041 return error_mark_node;
18043 /* If the next token is a `::', then the name must be a type
18044 name.
18046 [basic.lookup.qual]
18048 During the lookup for a name preceding the :: scope
18049 resolution operator, object, function, and enumerator
18050 names are ignored. */
18051 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18052 tag_type = typename_type;
18053 /* Look up the name. */
18054 decl = cp_parser_lookup_name (parser, identifier,
18055 tag_type,
18056 /*is_template=*/false,
18057 /*is_namespace=*/false,
18058 check_dependency_p,
18059 &ambiguous_decls,
18060 identifier_token->location);
18061 if (ambiguous_decls)
18063 if (cp_parser_parsing_tentatively (parser))
18064 cp_parser_simulate_error (parser);
18065 return error_mark_node;
18069 else
18071 /* Try a template-id. */
18072 decl = cp_parser_template_id (parser, template_keyword_p,
18073 check_dependency_p,
18074 tag_type,
18075 is_declaration);
18076 if (decl == error_mark_node)
18077 return error_mark_node;
18080 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
18082 /* If this is a typename, create a TYPENAME_TYPE. */
18083 if (typename_p && decl != error_mark_node)
18085 decl = make_typename_type (scope, decl, typename_type,
18086 /*complain=*/tf_error);
18087 if (decl != error_mark_node)
18088 decl = TYPE_NAME (decl);
18091 decl = strip_using_decl (decl);
18093 /* Check to see that it is really the name of a class. */
18094 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18095 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
18096 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18097 /* Situations like this:
18099 template <typename T> struct A {
18100 typename T::template X<int>::I i;
18103 are problematic. Is `T::template X<int>' a class-name? The
18104 standard does not seem to be definitive, but there is no other
18105 valid interpretation of the following `::'. Therefore, those
18106 names are considered class-names. */
18108 decl = make_typename_type (scope, decl, tag_type, tf_error);
18109 if (decl != error_mark_node)
18110 decl = TYPE_NAME (decl);
18112 else if (TREE_CODE (decl) != TYPE_DECL
18113 || TREE_TYPE (decl) == error_mark_node
18114 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
18115 /* In Objective-C 2.0, a classname followed by '.' starts a
18116 dot-syntax expression, and it's not a type-name. */
18117 || (c_dialect_objc ()
18118 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
18119 && objc_is_class_name (decl)))
18120 decl = error_mark_node;
18122 if (decl == error_mark_node)
18123 cp_parser_error (parser, "expected class-name");
18124 else if (identifier && !parser->scope)
18125 maybe_note_name_used_in_class (identifier, decl);
18127 return decl;
18130 /* Parse a class-specifier.
18132 class-specifier:
18133 class-head { member-specification [opt] }
18135 Returns the TREE_TYPE representing the class. */
18137 static tree
18138 cp_parser_class_specifier_1 (cp_parser* parser)
18140 tree type;
18141 tree attributes = NULL_TREE;
18142 bool nested_name_specifier_p;
18143 unsigned saved_num_template_parameter_lists;
18144 bool saved_in_function_body;
18145 unsigned char in_statement;
18146 bool in_switch_statement_p;
18147 bool saved_in_unbraced_linkage_specification_p;
18148 tree old_scope = NULL_TREE;
18149 tree scope = NULL_TREE;
18150 cp_token *closing_brace;
18152 push_deferring_access_checks (dk_no_deferred);
18154 /* Parse the class-head. */
18155 type = cp_parser_class_head (parser,
18156 &nested_name_specifier_p);
18157 /* If the class-head was a semantic disaster, skip the entire body
18158 of the class. */
18159 if (!type)
18161 cp_parser_skip_to_end_of_block_or_statement (parser);
18162 pop_deferring_access_checks ();
18163 return error_mark_node;
18166 /* Look for the `{'. */
18167 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
18169 pop_deferring_access_checks ();
18170 return error_mark_node;
18173 /* Issue an error message if type-definitions are forbidden here. */
18174 cp_parser_check_type_definition (parser);
18175 /* Remember that we are defining one more class. */
18176 ++parser->num_classes_being_defined;
18177 /* Inside the class, surrounding template-parameter-lists do not
18178 apply. */
18179 saved_num_template_parameter_lists
18180 = parser->num_template_parameter_lists;
18181 parser->num_template_parameter_lists = 0;
18182 /* We are not in a function body. */
18183 saved_in_function_body = parser->in_function_body;
18184 parser->in_function_body = false;
18185 /* Or in a loop. */
18186 in_statement = parser->in_statement;
18187 parser->in_statement = 0;
18188 /* Or in a switch. */
18189 in_switch_statement_p = parser->in_switch_statement_p;
18190 parser->in_switch_statement_p = false;
18191 /* We are not immediately inside an extern "lang" block. */
18192 saved_in_unbraced_linkage_specification_p
18193 = parser->in_unbraced_linkage_specification_p;
18194 parser->in_unbraced_linkage_specification_p = false;
18196 /* Start the class. */
18197 if (nested_name_specifier_p)
18199 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18200 old_scope = push_inner_scope (scope);
18202 type = begin_class_definition (type);
18204 if (type == error_mark_node)
18205 /* If the type is erroneous, skip the entire body of the class. */
18206 cp_parser_skip_to_closing_brace (parser);
18207 else
18208 /* Parse the member-specification. */
18209 cp_parser_member_specification_opt (parser);
18211 /* Look for the trailing `}'. */
18212 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18213 /* Look for trailing attributes to apply to this class. */
18214 if (cp_parser_allow_gnu_extensions_p (parser))
18215 attributes = cp_parser_gnu_attributes_opt (parser);
18216 if (type != error_mark_node)
18217 type = finish_struct (type, attributes);
18218 if (nested_name_specifier_p)
18219 pop_inner_scope (old_scope, scope);
18221 /* We've finished a type definition. Check for the common syntax
18222 error of forgetting a semicolon after the definition. We need to
18223 be careful, as we can't just check for not-a-semicolon and be done
18224 with it; the user might have typed:
18226 class X { } c = ...;
18227 class X { } *p = ...;
18229 and so forth. Instead, enumerate all the possible tokens that
18230 might follow this production; if we don't see one of them, then
18231 complain and silently insert the semicolon. */
18233 cp_token *token = cp_lexer_peek_token (parser->lexer);
18234 bool want_semicolon = true;
18236 if (cp_next_tokens_can_be_std_attribute_p (parser))
18237 /* Don't try to parse c++11 attributes here. As per the
18238 grammar, that should be a task for
18239 cp_parser_decl_specifier_seq. */
18240 want_semicolon = false;
18242 switch (token->type)
18244 case CPP_NAME:
18245 case CPP_SEMICOLON:
18246 case CPP_MULT:
18247 case CPP_AND:
18248 case CPP_OPEN_PAREN:
18249 case CPP_CLOSE_PAREN:
18250 case CPP_COMMA:
18251 want_semicolon = false;
18252 break;
18254 /* While it's legal for type qualifiers and storage class
18255 specifiers to follow type definitions in the grammar, only
18256 compiler testsuites contain code like that. Assume that if
18257 we see such code, then what we're really seeing is a case
18258 like:
18260 class X { }
18261 const <type> var = ...;
18265 class Y { }
18266 static <type> func (...) ...
18268 i.e. the qualifier or specifier applies to the next
18269 declaration. To do so, however, we need to look ahead one
18270 more token to see if *that* token is a type specifier.
18272 This code could be improved to handle:
18274 class Z { }
18275 static const <type> var = ...; */
18276 case CPP_KEYWORD:
18277 if (keyword_is_decl_specifier (token->keyword))
18279 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18281 /* Handling user-defined types here would be nice, but very
18282 tricky. */
18283 want_semicolon
18284 = (lookahead->type == CPP_KEYWORD
18285 && keyword_begins_type_specifier (lookahead->keyword));
18287 break;
18288 default:
18289 break;
18292 /* If we don't have a type, then something is very wrong and we
18293 shouldn't try to do anything clever. Likewise for not seeing the
18294 closing brace. */
18295 if (closing_brace && TYPE_P (type) && want_semicolon)
18297 cp_token_position prev
18298 = cp_lexer_previous_token_position (parser->lexer);
18299 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18300 location_t loc = prev_token->location;
18302 if (CLASSTYPE_DECLARED_CLASS (type))
18303 error_at (loc, "expected %<;%> after class definition");
18304 else if (TREE_CODE (type) == RECORD_TYPE)
18305 error_at (loc, "expected %<;%> after struct definition");
18306 else if (TREE_CODE (type) == UNION_TYPE)
18307 error_at (loc, "expected %<;%> after union definition");
18308 else
18309 gcc_unreachable ();
18311 /* Unget one token and smash it to look as though we encountered
18312 a semicolon in the input stream. */
18313 cp_lexer_set_token_position (parser->lexer, prev);
18314 token = cp_lexer_peek_token (parser->lexer);
18315 token->type = CPP_SEMICOLON;
18316 token->keyword = RID_MAX;
18320 /* If this class is not itself within the scope of another class,
18321 then we need to parse the bodies of all of the queued function
18322 definitions. Note that the queued functions defined in a class
18323 are not always processed immediately following the
18324 class-specifier for that class. Consider:
18326 struct A {
18327 struct B { void f() { sizeof (A); } };
18330 If `f' were processed before the processing of `A' were
18331 completed, there would be no way to compute the size of `A'.
18332 Note that the nesting we are interested in here is lexical --
18333 not the semantic nesting given by TYPE_CONTEXT. In particular,
18334 for:
18336 struct A { struct B; };
18337 struct A::B { void f() { } };
18339 there is no need to delay the parsing of `A::B::f'. */
18340 if (--parser->num_classes_being_defined == 0)
18342 tree decl;
18343 tree class_type = NULL_TREE;
18344 tree pushed_scope = NULL_TREE;
18345 unsigned ix;
18346 cp_default_arg_entry *e;
18347 tree save_ccp, save_ccr;
18349 /* In a first pass, parse default arguments to the functions.
18350 Then, in a second pass, parse the bodies of the functions.
18351 This two-phased approach handles cases like:
18353 struct S {
18354 void f() { g(); }
18355 void g(int i = 3);
18359 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
18361 decl = e->decl;
18362 /* If there are default arguments that have not yet been processed,
18363 take care of them now. */
18364 if (class_type != e->class_type)
18366 if (pushed_scope)
18367 pop_scope (pushed_scope);
18368 class_type = e->class_type;
18369 pushed_scope = push_scope (class_type);
18371 /* Make sure that any template parameters are in scope. */
18372 maybe_begin_member_template_processing (decl);
18373 /* Parse the default argument expressions. */
18374 cp_parser_late_parsing_default_args (parser, decl);
18375 /* Remove any template parameters from the symbol table. */
18376 maybe_end_member_template_processing ();
18378 vec_safe_truncate (unparsed_funs_with_default_args, 0);
18379 /* Now parse any NSDMIs. */
18380 save_ccp = current_class_ptr;
18381 save_ccr = current_class_ref;
18382 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
18384 if (class_type != DECL_CONTEXT (decl))
18386 if (pushed_scope)
18387 pop_scope (pushed_scope);
18388 class_type = DECL_CONTEXT (decl);
18389 pushed_scope = push_scope (class_type);
18391 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18392 cp_parser_late_parsing_nsdmi (parser, decl);
18394 vec_safe_truncate (unparsed_nsdmis, 0);
18395 current_class_ptr = save_ccp;
18396 current_class_ref = save_ccr;
18397 if (pushed_scope)
18398 pop_scope (pushed_scope);
18399 /* Now parse the body of the functions. */
18400 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
18401 cp_parser_late_parsing_for_member (parser, decl);
18402 vec_safe_truncate (unparsed_funs_with_definitions, 0);
18405 /* Put back any saved access checks. */
18406 pop_deferring_access_checks ();
18408 /* Restore saved state. */
18409 parser->in_switch_statement_p = in_switch_statement_p;
18410 parser->in_statement = in_statement;
18411 parser->in_function_body = saved_in_function_body;
18412 parser->num_template_parameter_lists
18413 = saved_num_template_parameter_lists;
18414 parser->in_unbraced_linkage_specification_p
18415 = saved_in_unbraced_linkage_specification_p;
18417 return type;
18420 static tree
18421 cp_parser_class_specifier (cp_parser* parser)
18423 tree ret;
18424 timevar_push (TV_PARSE_STRUCT);
18425 ret = cp_parser_class_specifier_1 (parser);
18426 timevar_pop (TV_PARSE_STRUCT);
18427 return ret;
18430 /* Parse a class-head.
18432 class-head:
18433 class-key identifier [opt] base-clause [opt]
18434 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18435 class-key nested-name-specifier [opt] template-id
18436 base-clause [opt]
18438 class-virt-specifier:
18439 final
18441 GNU Extensions:
18442 class-key attributes identifier [opt] base-clause [opt]
18443 class-key attributes nested-name-specifier identifier base-clause [opt]
18444 class-key attributes nested-name-specifier [opt] template-id
18445 base-clause [opt]
18447 Upon return BASES is initialized to the list of base classes (or
18448 NULL, if there are none) in the same form returned by
18449 cp_parser_base_clause.
18451 Returns the TYPE of the indicated class. Sets
18452 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18453 involving a nested-name-specifier was used, and FALSE otherwise.
18455 Returns error_mark_node if this is not a class-head.
18457 Returns NULL_TREE if the class-head is syntactically valid, but
18458 semantically invalid in a way that means we should skip the entire
18459 body of the class. */
18461 static tree
18462 cp_parser_class_head (cp_parser* parser,
18463 bool* nested_name_specifier_p)
18465 tree nested_name_specifier;
18466 enum tag_types class_key;
18467 tree id = NULL_TREE;
18468 tree type = NULL_TREE;
18469 tree attributes;
18470 tree bases;
18471 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18472 bool template_id_p = false;
18473 bool qualified_p = false;
18474 bool invalid_nested_name_p = false;
18475 bool invalid_explicit_specialization_p = false;
18476 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18477 tree pushed_scope = NULL_TREE;
18478 unsigned num_templates;
18479 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18480 /* Assume no nested-name-specifier will be present. */
18481 *nested_name_specifier_p = false;
18482 /* Assume no template parameter lists will be used in defining the
18483 type. */
18484 num_templates = 0;
18485 parser->colon_corrects_to_scope_p = false;
18487 /* Look for the class-key. */
18488 class_key = cp_parser_class_key (parser);
18489 if (class_key == none_type)
18490 return error_mark_node;
18492 /* Parse the attributes. */
18493 attributes = cp_parser_attributes_opt (parser);
18495 /* If the next token is `::', that is invalid -- but sometimes
18496 people do try to write:
18498 struct ::S {};
18500 Handle this gracefully by accepting the extra qualifier, and then
18501 issuing an error about it later if this really is a
18502 class-head. If it turns out just to be an elaborated type
18503 specifier, remain silent. */
18504 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18505 qualified_p = true;
18507 push_deferring_access_checks (dk_no_check);
18509 /* Determine the name of the class. Begin by looking for an
18510 optional nested-name-specifier. */
18511 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18512 nested_name_specifier
18513 = cp_parser_nested_name_specifier_opt (parser,
18514 /*typename_keyword_p=*/false,
18515 /*check_dependency_p=*/false,
18516 /*type_p=*/true,
18517 /*is_declaration=*/false);
18518 /* If there was a nested-name-specifier, then there *must* be an
18519 identifier. */
18520 if (nested_name_specifier)
18522 type_start_token = cp_lexer_peek_token (parser->lexer);
18523 /* Although the grammar says `identifier', it really means
18524 `class-name' or `template-name'. You are only allowed to
18525 define a class that has already been declared with this
18526 syntax.
18528 The proposed resolution for Core Issue 180 says that wherever
18529 you see `class T::X' you should treat `X' as a type-name.
18531 It is OK to define an inaccessible class; for example:
18533 class A { class B; };
18534 class A::B {};
18536 We do not know if we will see a class-name, or a
18537 template-name. We look for a class-name first, in case the
18538 class-name is a template-id; if we looked for the
18539 template-name first we would stop after the template-name. */
18540 cp_parser_parse_tentatively (parser);
18541 type = cp_parser_class_name (parser,
18542 /*typename_keyword_p=*/false,
18543 /*template_keyword_p=*/false,
18544 class_type,
18545 /*check_dependency_p=*/false,
18546 /*class_head_p=*/true,
18547 /*is_declaration=*/false);
18548 /* If that didn't work, ignore the nested-name-specifier. */
18549 if (!cp_parser_parse_definitely (parser))
18551 invalid_nested_name_p = true;
18552 type_start_token = cp_lexer_peek_token (parser->lexer);
18553 id = cp_parser_identifier (parser);
18554 if (id == error_mark_node)
18555 id = NULL_TREE;
18557 /* If we could not find a corresponding TYPE, treat this
18558 declaration like an unqualified declaration. */
18559 if (type == error_mark_node)
18560 nested_name_specifier = NULL_TREE;
18561 /* Otherwise, count the number of templates used in TYPE and its
18562 containing scopes. */
18563 else
18565 tree scope;
18567 for (scope = TREE_TYPE (type);
18568 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18569 scope = (TYPE_P (scope)
18570 ? TYPE_CONTEXT (scope)
18571 : DECL_CONTEXT (scope)))
18572 if (TYPE_P (scope)
18573 && CLASS_TYPE_P (scope)
18574 && CLASSTYPE_TEMPLATE_INFO (scope)
18575 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18576 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18577 ++num_templates;
18580 /* Otherwise, the identifier is optional. */
18581 else
18583 /* We don't know whether what comes next is a template-id,
18584 an identifier, or nothing at all. */
18585 cp_parser_parse_tentatively (parser);
18586 /* Check for a template-id. */
18587 type_start_token = cp_lexer_peek_token (parser->lexer);
18588 id = cp_parser_template_id (parser,
18589 /*template_keyword_p=*/false,
18590 /*check_dependency_p=*/true,
18591 class_key,
18592 /*is_declaration=*/true);
18593 /* If that didn't work, it could still be an identifier. */
18594 if (!cp_parser_parse_definitely (parser))
18596 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18598 type_start_token = cp_lexer_peek_token (parser->lexer);
18599 id = cp_parser_identifier (parser);
18601 else
18602 id = NULL_TREE;
18604 else
18606 template_id_p = true;
18607 ++num_templates;
18611 pop_deferring_access_checks ();
18613 if (id)
18615 cp_parser_check_for_invalid_template_id (parser, id,
18616 class_key,
18617 type_start_token->location);
18619 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18621 /* If it's not a `:' or a `{' then we can't really be looking at a
18622 class-head, since a class-head only appears as part of a
18623 class-specifier. We have to detect this situation before calling
18624 xref_tag, since that has irreversible side-effects. */
18625 if (!cp_parser_next_token_starts_class_definition_p (parser))
18627 cp_parser_error (parser, "expected %<{%> or %<:%>");
18628 type = error_mark_node;
18629 goto out;
18632 /* At this point, we're going ahead with the class-specifier, even
18633 if some other problem occurs. */
18634 cp_parser_commit_to_tentative_parse (parser);
18635 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18637 cp_parser_error (parser,
18638 "cannot specify %<override%> for a class");
18639 type = error_mark_node;
18640 goto out;
18642 /* Issue the error about the overly-qualified name now. */
18643 if (qualified_p)
18645 cp_parser_error (parser,
18646 "global qualification of class name is invalid");
18647 type = error_mark_node;
18648 goto out;
18650 else if (invalid_nested_name_p)
18652 cp_parser_error (parser,
18653 "qualified name does not name a class");
18654 type = error_mark_node;
18655 goto out;
18657 else if (nested_name_specifier)
18659 tree scope;
18661 /* Reject typedef-names in class heads. */
18662 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18664 error_at (type_start_token->location,
18665 "invalid class name in declaration of %qD",
18666 type);
18667 type = NULL_TREE;
18668 goto done;
18671 /* Figure out in what scope the declaration is being placed. */
18672 scope = current_scope ();
18673 /* If that scope does not contain the scope in which the
18674 class was originally declared, the program is invalid. */
18675 if (scope && !is_ancestor (scope, nested_name_specifier))
18677 if (at_namespace_scope_p ())
18678 error_at (type_start_token->location,
18679 "declaration of %qD in namespace %qD which does not "
18680 "enclose %qD",
18681 type, scope, nested_name_specifier);
18682 else
18683 error_at (type_start_token->location,
18684 "declaration of %qD in %qD which does not enclose %qD",
18685 type, scope, nested_name_specifier);
18686 type = NULL_TREE;
18687 goto done;
18689 /* [dcl.meaning]
18691 A declarator-id shall not be qualified except for the
18692 definition of a ... nested class outside of its class
18693 ... [or] the definition or explicit instantiation of a
18694 class member of a namespace outside of its namespace. */
18695 if (scope == nested_name_specifier)
18697 permerror (nested_name_specifier_token_start->location,
18698 "extra qualification not allowed");
18699 nested_name_specifier = NULL_TREE;
18700 num_templates = 0;
18703 /* An explicit-specialization must be preceded by "template <>". If
18704 it is not, try to recover gracefully. */
18705 if (at_namespace_scope_p ()
18706 && parser->num_template_parameter_lists == 0
18707 && template_id_p)
18709 error_at (type_start_token->location,
18710 "an explicit specialization must be preceded by %<template <>%>");
18711 invalid_explicit_specialization_p = true;
18712 /* Take the same action that would have been taken by
18713 cp_parser_explicit_specialization. */
18714 ++parser->num_template_parameter_lists;
18715 begin_specialization ();
18717 /* There must be no "return" statements between this point and the
18718 end of this function; set "type "to the correct return value and
18719 use "goto done;" to return. */
18720 /* Make sure that the right number of template parameters were
18721 present. */
18722 if (!cp_parser_check_template_parameters (parser, num_templates,
18723 type_start_token->location,
18724 /*declarator=*/NULL))
18726 /* If something went wrong, there is no point in even trying to
18727 process the class-definition. */
18728 type = NULL_TREE;
18729 goto done;
18732 /* Look up the type. */
18733 if (template_id_p)
18735 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18736 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18737 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18739 error_at (type_start_token->location,
18740 "function template %qD redeclared as a class template", id);
18741 type = error_mark_node;
18743 else
18745 type = TREE_TYPE (id);
18746 type = maybe_process_partial_specialization (type);
18748 if (nested_name_specifier)
18749 pushed_scope = push_scope (nested_name_specifier);
18751 else if (nested_name_specifier)
18753 tree class_type;
18755 /* Given:
18757 template <typename T> struct S { struct T };
18758 template <typename T> struct S<T>::T { };
18760 we will get a TYPENAME_TYPE when processing the definition of
18761 `S::T'. We need to resolve it to the actual type before we
18762 try to define it. */
18763 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18765 class_type = resolve_typename_type (TREE_TYPE (type),
18766 /*only_current_p=*/false);
18767 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18768 type = TYPE_NAME (class_type);
18769 else
18771 cp_parser_error (parser, "could not resolve typename type");
18772 type = error_mark_node;
18776 if (maybe_process_partial_specialization (TREE_TYPE (type))
18777 == error_mark_node)
18779 type = NULL_TREE;
18780 goto done;
18783 class_type = current_class_type;
18784 /* Enter the scope indicated by the nested-name-specifier. */
18785 pushed_scope = push_scope (nested_name_specifier);
18786 /* Get the canonical version of this type. */
18787 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18788 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18789 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18791 type = push_template_decl (type);
18792 if (type == error_mark_node)
18794 type = NULL_TREE;
18795 goto done;
18799 type = TREE_TYPE (type);
18800 *nested_name_specifier_p = true;
18802 else /* The name is not a nested name. */
18804 /* If the class was unnamed, create a dummy name. */
18805 if (!id)
18806 id = make_anon_name ();
18807 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18808 parser->num_template_parameter_lists);
18811 /* Indicate whether this class was declared as a `class' or as a
18812 `struct'. */
18813 if (TREE_CODE (type) == RECORD_TYPE)
18814 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18815 cp_parser_check_class_key (class_key, type);
18817 /* If this type was already complete, and we see another definition,
18818 that's an error. */
18819 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18821 error_at (type_start_token->location, "redefinition of %q#T",
18822 type);
18823 error_at (type_start_token->location, "previous definition of %q+#T",
18824 type);
18825 type = NULL_TREE;
18826 goto done;
18828 else if (type == error_mark_node)
18829 type = NULL_TREE;
18831 if (type)
18833 /* Apply attributes now, before any use of the class as a template
18834 argument in its base list. */
18835 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
18836 fixup_attribute_variants (type);
18839 /* We will have entered the scope containing the class; the names of
18840 base classes should be looked up in that context. For example:
18842 struct A { struct B {}; struct C; };
18843 struct A::C : B {};
18845 is valid. */
18847 /* Get the list of base-classes, if there is one. */
18848 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18849 bases = cp_parser_base_clause (parser);
18850 else
18851 bases = NULL_TREE;
18853 /* If we're really defining a class, process the base classes.
18854 If they're invalid, fail. */
18855 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
18856 && !xref_basetypes (type, bases))
18857 type = NULL_TREE;
18859 done:
18860 /* Leave the scope given by the nested-name-specifier. We will
18861 enter the class scope itself while processing the members. */
18862 if (pushed_scope)
18863 pop_scope (pushed_scope);
18865 if (invalid_explicit_specialization_p)
18867 end_specialization ();
18868 --parser->num_template_parameter_lists;
18871 if (type)
18872 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18873 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18874 CLASSTYPE_FINAL (type) = 1;
18875 out:
18876 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18877 return type;
18880 /* Parse a class-key.
18882 class-key:
18883 class
18884 struct
18885 union
18887 Returns the kind of class-key specified, or none_type to indicate
18888 error. */
18890 static enum tag_types
18891 cp_parser_class_key (cp_parser* parser)
18893 cp_token *token;
18894 enum tag_types tag_type;
18896 /* Look for the class-key. */
18897 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18898 if (!token)
18899 return none_type;
18901 /* Check to see if the TOKEN is a class-key. */
18902 tag_type = cp_parser_token_is_class_key (token);
18903 if (!tag_type)
18904 cp_parser_error (parser, "expected class-key");
18905 return tag_type;
18908 /* Parse an (optional) member-specification.
18910 member-specification:
18911 member-declaration member-specification [opt]
18912 access-specifier : member-specification [opt] */
18914 static void
18915 cp_parser_member_specification_opt (cp_parser* parser)
18917 while (true)
18919 cp_token *token;
18920 enum rid keyword;
18922 /* Peek at the next token. */
18923 token = cp_lexer_peek_token (parser->lexer);
18924 /* If it's a `}', or EOF then we've seen all the members. */
18925 if (token->type == CPP_CLOSE_BRACE
18926 || token->type == CPP_EOF
18927 || token->type == CPP_PRAGMA_EOL)
18928 break;
18930 /* See if this token is a keyword. */
18931 keyword = token->keyword;
18932 switch (keyword)
18934 case RID_PUBLIC:
18935 case RID_PROTECTED:
18936 case RID_PRIVATE:
18937 /* Consume the access-specifier. */
18938 cp_lexer_consume_token (parser->lexer);
18939 /* Remember which access-specifier is active. */
18940 current_access_specifier = token->u.value;
18941 /* Look for the `:'. */
18942 cp_parser_require (parser, CPP_COLON, RT_COLON);
18943 break;
18945 default:
18946 /* Accept #pragmas at class scope. */
18947 if (token->type == CPP_PRAGMA)
18949 cp_parser_pragma (parser, pragma_external);
18950 break;
18953 /* Otherwise, the next construction must be a
18954 member-declaration. */
18955 cp_parser_member_declaration (parser);
18960 /* Parse a member-declaration.
18962 member-declaration:
18963 decl-specifier-seq [opt] member-declarator-list [opt] ;
18964 function-definition ; [opt]
18965 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18966 using-declaration
18967 template-declaration
18968 alias-declaration
18970 member-declarator-list:
18971 member-declarator
18972 member-declarator-list , member-declarator
18974 member-declarator:
18975 declarator pure-specifier [opt]
18976 declarator constant-initializer [opt]
18977 identifier [opt] : constant-expression
18979 GNU Extensions:
18981 member-declaration:
18982 __extension__ member-declaration
18984 member-declarator:
18985 declarator attributes [opt] pure-specifier [opt]
18986 declarator attributes [opt] constant-initializer [opt]
18987 identifier [opt] attributes [opt] : constant-expression
18989 C++0x Extensions:
18991 member-declaration:
18992 static_assert-declaration */
18994 static void
18995 cp_parser_member_declaration (cp_parser* parser)
18997 cp_decl_specifier_seq decl_specifiers;
18998 tree prefix_attributes;
18999 tree decl;
19000 int declares_class_or_enum;
19001 bool friend_p;
19002 cp_token *token = NULL;
19003 cp_token *decl_spec_token_start = NULL;
19004 cp_token *initializer_token_start = NULL;
19005 int saved_pedantic;
19006 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19008 /* Check for the `__extension__' keyword. */
19009 if (cp_parser_extension_opt (parser, &saved_pedantic))
19011 /* Recurse. */
19012 cp_parser_member_declaration (parser);
19013 /* Restore the old value of the PEDANTIC flag. */
19014 pedantic = saved_pedantic;
19016 return;
19019 /* Check for a template-declaration. */
19020 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19022 /* An explicit specialization here is an error condition, and we
19023 expect the specialization handler to detect and report this. */
19024 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19025 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
19026 cp_parser_explicit_specialization (parser);
19027 else
19028 cp_parser_template_declaration (parser, /*member_p=*/true);
19030 return;
19033 /* Check for a using-declaration. */
19034 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
19036 if (cxx_dialect < cxx0x)
19038 /* Parse the using-declaration. */
19039 cp_parser_using_declaration (parser,
19040 /*access_declaration_p=*/false);
19041 return;
19043 else
19045 tree decl;
19046 cp_parser_parse_tentatively (parser);
19047 decl = cp_parser_alias_declaration (parser);
19048 if (cp_parser_parse_definitely (parser))
19049 finish_member_declaration (decl);
19050 else
19051 cp_parser_using_declaration (parser,
19052 /*access_declaration_p=*/false);
19053 return;
19057 /* Check for @defs. */
19058 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
19060 tree ivar, member;
19061 tree ivar_chains = cp_parser_objc_defs_expression (parser);
19062 ivar = ivar_chains;
19063 while (ivar)
19065 member = ivar;
19066 ivar = TREE_CHAIN (member);
19067 TREE_CHAIN (member) = NULL_TREE;
19068 finish_member_declaration (member);
19070 return;
19073 /* If the next token is `static_assert' we have a static assertion. */
19074 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
19076 cp_parser_static_assert (parser, /*member_p=*/true);
19077 return;
19080 parser->colon_corrects_to_scope_p = false;
19082 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
19083 goto out;
19085 /* Parse the decl-specifier-seq. */
19086 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19087 cp_parser_decl_specifier_seq (parser,
19088 CP_PARSER_FLAGS_OPTIONAL,
19089 &decl_specifiers,
19090 &declares_class_or_enum);
19091 /* Check for an invalid type-name. */
19092 if (!decl_specifiers.any_type_specifiers_p
19093 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19094 goto out;
19095 /* If there is no declarator, then the decl-specifier-seq should
19096 specify a type. */
19097 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19099 /* If there was no decl-specifier-seq, and the next token is a
19100 `;', then we have something like:
19102 struct S { ; };
19104 [class.mem]
19106 Each member-declaration shall declare at least one member
19107 name of the class. */
19108 if (!decl_specifiers.any_specifiers_p)
19110 cp_token *token = cp_lexer_peek_token (parser->lexer);
19111 if (!in_system_header_at (token->location))
19112 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
19114 else
19116 tree type;
19118 /* See if this declaration is a friend. */
19119 friend_p = cp_parser_friend_p (&decl_specifiers);
19120 /* If there were decl-specifiers, check to see if there was
19121 a class-declaration. */
19122 type = check_tag_decl (&decl_specifiers,
19123 /*explicit_type_instantiation_p=*/false);
19124 /* Nested classes have already been added to the class, but
19125 a `friend' needs to be explicitly registered. */
19126 if (friend_p)
19128 /* If the `friend' keyword was present, the friend must
19129 be introduced with a class-key. */
19130 if (!declares_class_or_enum && cxx_dialect < cxx0x)
19131 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
19132 "in C++03 a class-key must be used "
19133 "when declaring a friend");
19134 /* In this case:
19136 template <typename T> struct A {
19137 friend struct A<T>::B;
19140 A<T>::B will be represented by a TYPENAME_TYPE, and
19141 therefore not recognized by check_tag_decl. */
19142 if (!type)
19144 type = decl_specifiers.type;
19145 if (type && TREE_CODE (type) == TYPE_DECL)
19146 type = TREE_TYPE (type);
19148 if (!type || !TYPE_P (type))
19149 error_at (decl_spec_token_start->location,
19150 "friend declaration does not name a class or "
19151 "function");
19152 else
19153 make_friend_class (current_class_type, type,
19154 /*complain=*/true);
19156 /* If there is no TYPE, an error message will already have
19157 been issued. */
19158 else if (!type || type == error_mark_node)
19160 /* An anonymous aggregate has to be handled specially; such
19161 a declaration really declares a data member (with a
19162 particular type), as opposed to a nested class. */
19163 else if (ANON_AGGR_TYPE_P (type))
19165 /* C++11 9.5/6. */
19166 if (decl_specifiers.storage_class != sc_none)
19167 error_at (decl_spec_token_start->location,
19168 "a storage class on an anonymous aggregate "
19169 "in class scope is not allowed");
19171 /* Remove constructors and such from TYPE, now that we
19172 know it is an anonymous aggregate. */
19173 fixup_anonymous_aggr (type);
19174 /* And make the corresponding data member. */
19175 decl = build_decl (decl_spec_token_start->location,
19176 FIELD_DECL, NULL_TREE, type);
19177 /* Add it to the class. */
19178 finish_member_declaration (decl);
19180 else
19181 cp_parser_check_access_in_redeclaration
19182 (TYPE_NAME (type),
19183 decl_spec_token_start->location);
19186 else
19188 bool assume_semicolon = false;
19190 /* Clear attributes from the decl_specifiers but keep them
19191 around as prefix attributes that apply them to the entity
19192 being declared. */
19193 prefix_attributes = decl_specifiers.attributes;
19194 decl_specifiers.attributes = NULL_TREE;
19196 /* See if these declarations will be friends. */
19197 friend_p = cp_parser_friend_p (&decl_specifiers);
19199 /* Keep going until we hit the `;' at the end of the
19200 declaration. */
19201 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19203 tree attributes = NULL_TREE;
19204 tree first_attribute;
19206 /* Peek at the next token. */
19207 token = cp_lexer_peek_token (parser->lexer);
19209 /* Check for a bitfield declaration. */
19210 if (token->type == CPP_COLON
19211 || (token->type == CPP_NAME
19212 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19213 == CPP_COLON))
19215 tree identifier;
19216 tree width;
19218 /* Get the name of the bitfield. Note that we cannot just
19219 check TOKEN here because it may have been invalidated by
19220 the call to cp_lexer_peek_nth_token above. */
19221 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19222 identifier = cp_parser_identifier (parser);
19223 else
19224 identifier = NULL_TREE;
19226 /* Consume the `:' token. */
19227 cp_lexer_consume_token (parser->lexer);
19228 /* Get the width of the bitfield. */
19229 width
19230 = cp_parser_constant_expression (parser,
19231 /*allow_non_constant=*/false,
19232 NULL);
19234 /* Look for attributes that apply to the bitfield. */
19235 attributes = cp_parser_attributes_opt (parser);
19236 /* Remember which attributes are prefix attributes and
19237 which are not. */
19238 first_attribute = attributes;
19239 /* Combine the attributes. */
19240 attributes = chainon (prefix_attributes, attributes);
19242 /* Create the bitfield declaration. */
19243 decl = grokbitfield (identifier
19244 ? make_id_declarator (NULL_TREE,
19245 identifier,
19246 sfk_none)
19247 : NULL,
19248 &decl_specifiers,
19249 width,
19250 attributes);
19252 else
19254 cp_declarator *declarator;
19255 tree initializer;
19256 tree asm_specification;
19257 int ctor_dtor_or_conv_p;
19259 /* Parse the declarator. */
19260 declarator
19261 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19262 &ctor_dtor_or_conv_p,
19263 /*parenthesized_p=*/NULL,
19264 /*member_p=*/true);
19266 /* If something went wrong parsing the declarator, make sure
19267 that we at least consume some tokens. */
19268 if (declarator == cp_error_declarator)
19270 /* Skip to the end of the statement. */
19271 cp_parser_skip_to_end_of_statement (parser);
19272 /* If the next token is not a semicolon, that is
19273 probably because we just skipped over the body of
19274 a function. So, we consume a semicolon if
19275 present, but do not issue an error message if it
19276 is not present. */
19277 if (cp_lexer_next_token_is (parser->lexer,
19278 CPP_SEMICOLON))
19279 cp_lexer_consume_token (parser->lexer);
19280 goto out;
19283 if (declares_class_or_enum & 2)
19284 cp_parser_check_for_definition_in_return_type
19285 (declarator, decl_specifiers.type,
19286 decl_specifiers.locations[ds_type_spec]);
19288 /* Look for an asm-specification. */
19289 asm_specification = cp_parser_asm_specification_opt (parser);
19290 /* Look for attributes that apply to the declaration. */
19291 attributes = cp_parser_attributes_opt (parser);
19292 /* Remember which attributes are prefix attributes and
19293 which are not. */
19294 first_attribute = attributes;
19295 /* Combine the attributes. */
19296 attributes = chainon (prefix_attributes, attributes);
19298 /* If it's an `=', then we have a constant-initializer or a
19299 pure-specifier. It is not correct to parse the
19300 initializer before registering the member declaration
19301 since the member declaration should be in scope while
19302 its initializer is processed. However, the rest of the
19303 front end does not yet provide an interface that allows
19304 us to handle this correctly. */
19305 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19307 /* In [class.mem]:
19309 A pure-specifier shall be used only in the declaration of
19310 a virtual function.
19312 A member-declarator can contain a constant-initializer
19313 only if it declares a static member of integral or
19314 enumeration type.
19316 Therefore, if the DECLARATOR is for a function, we look
19317 for a pure-specifier; otherwise, we look for a
19318 constant-initializer. When we call `grokfield', it will
19319 perform more stringent semantics checks. */
19320 initializer_token_start = cp_lexer_peek_token (parser->lexer);
19321 if (function_declarator_p (declarator)
19322 || (decl_specifiers.type
19323 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19324 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19325 == FUNCTION_TYPE)))
19326 initializer = cp_parser_pure_specifier (parser);
19327 else if (decl_specifiers.storage_class != sc_static)
19328 initializer = cp_parser_save_nsdmi (parser);
19329 else if (cxx_dialect >= cxx0x)
19331 bool nonconst;
19332 /* Don't require a constant rvalue in C++11, since we
19333 might want a reference constant. We'll enforce
19334 constancy later. */
19335 cp_lexer_consume_token (parser->lexer);
19336 /* Parse the initializer. */
19337 initializer = cp_parser_initializer_clause (parser,
19338 &nonconst);
19340 else
19341 /* Parse the initializer. */
19342 initializer = cp_parser_constant_initializer (parser);
19344 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19345 && !function_declarator_p (declarator))
19347 bool x;
19348 if (decl_specifiers.storage_class != sc_static)
19349 initializer = cp_parser_save_nsdmi (parser);
19350 else
19351 initializer = cp_parser_initializer (parser, &x, &x);
19353 /* Otherwise, there is no initializer. */
19354 else
19355 initializer = NULL_TREE;
19357 /* See if we are probably looking at a function
19358 definition. We are certainly not looking at a
19359 member-declarator. Calling `grokfield' has
19360 side-effects, so we must not do it unless we are sure
19361 that we are looking at a member-declarator. */
19362 if (cp_parser_token_starts_function_definition_p
19363 (cp_lexer_peek_token (parser->lexer)))
19365 /* The grammar does not allow a pure-specifier to be
19366 used when a member function is defined. (It is
19367 possible that this fact is an oversight in the
19368 standard, since a pure function may be defined
19369 outside of the class-specifier. */
19370 if (initializer && initializer_token_start)
19371 error_at (initializer_token_start->location,
19372 "pure-specifier on function-definition");
19373 decl = cp_parser_save_member_function_body (parser,
19374 &decl_specifiers,
19375 declarator,
19376 attributes);
19377 /* If the member was not a friend, declare it here. */
19378 if (!friend_p)
19379 finish_member_declaration (decl);
19380 /* Peek at the next token. */
19381 token = cp_lexer_peek_token (parser->lexer);
19382 /* If the next token is a semicolon, consume it. */
19383 if (token->type == CPP_SEMICOLON)
19384 cp_lexer_consume_token (parser->lexer);
19385 goto out;
19387 else
19388 if (declarator->kind == cdk_function)
19389 declarator->id_loc = token->location;
19390 /* Create the declaration. */
19391 decl = grokfield (declarator, &decl_specifiers,
19392 initializer, /*init_const_expr_p=*/true,
19393 asm_specification,
19394 attributes);
19397 /* Reset PREFIX_ATTRIBUTES. */
19398 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19399 attributes = TREE_CHAIN (attributes);
19400 if (attributes)
19401 TREE_CHAIN (attributes) = NULL_TREE;
19403 /* If there is any qualification still in effect, clear it
19404 now; we will be starting fresh with the next declarator. */
19405 parser->scope = NULL_TREE;
19406 parser->qualifying_scope = NULL_TREE;
19407 parser->object_scope = NULL_TREE;
19408 /* If it's a `,', then there are more declarators. */
19409 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19411 cp_lexer_consume_token (parser->lexer);
19412 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19414 cp_token *token = cp_lexer_previous_token (parser->lexer);
19415 error_at (token->location,
19416 "stray %<,%> at end of member declaration");
19419 /* If the next token isn't a `;', then we have a parse error. */
19420 else if (cp_lexer_next_token_is_not (parser->lexer,
19421 CPP_SEMICOLON))
19423 /* The next token might be a ways away from where the
19424 actual semicolon is missing. Find the previous token
19425 and use that for our error position. */
19426 cp_token *token = cp_lexer_previous_token (parser->lexer);
19427 error_at (token->location,
19428 "expected %<;%> at end of member declaration");
19430 /* Assume that the user meant to provide a semicolon. If
19431 we were to cp_parser_skip_to_end_of_statement, we might
19432 skip to a semicolon inside a member function definition
19433 and issue nonsensical error messages. */
19434 assume_semicolon = true;
19437 if (decl)
19439 /* Add DECL to the list of members. */
19440 if (!friend_p)
19441 finish_member_declaration (decl);
19443 if (TREE_CODE (decl) == FUNCTION_DECL)
19444 cp_parser_save_default_args (parser, decl);
19445 else if (TREE_CODE (decl) == FIELD_DECL
19446 && !DECL_C_BIT_FIELD (decl)
19447 && DECL_INITIAL (decl))
19448 /* Add DECL to the queue of NSDMI to be parsed later. */
19449 vec_safe_push (unparsed_nsdmis, decl);
19452 if (assume_semicolon)
19453 goto out;
19457 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19458 out:
19459 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19462 /* Parse a pure-specifier.
19464 pure-specifier:
19467 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19468 Otherwise, ERROR_MARK_NODE is returned. */
19470 static tree
19471 cp_parser_pure_specifier (cp_parser* parser)
19473 cp_token *token;
19475 /* Look for the `=' token. */
19476 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19477 return error_mark_node;
19478 /* Look for the `0' token. */
19479 token = cp_lexer_peek_token (parser->lexer);
19481 if (token->type == CPP_EOF
19482 || token->type == CPP_PRAGMA_EOL)
19483 return error_mark_node;
19485 cp_lexer_consume_token (parser->lexer);
19487 /* Accept = default or = delete in c++0x mode. */
19488 if (token->keyword == RID_DEFAULT
19489 || token->keyword == RID_DELETE)
19491 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19492 return token->u.value;
19495 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19496 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19498 cp_parser_error (parser,
19499 "invalid pure specifier (only %<= 0%> is allowed)");
19500 cp_parser_skip_to_end_of_statement (parser);
19501 return error_mark_node;
19503 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19505 error_at (token->location, "templates may not be %<virtual%>");
19506 return error_mark_node;
19509 return integer_zero_node;
19512 /* Parse a constant-initializer.
19514 constant-initializer:
19515 = constant-expression
19517 Returns a representation of the constant-expression. */
19519 static tree
19520 cp_parser_constant_initializer (cp_parser* parser)
19522 /* Look for the `=' token. */
19523 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19524 return error_mark_node;
19526 /* It is invalid to write:
19528 struct S { static const int i = { 7 }; };
19531 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19533 cp_parser_error (parser,
19534 "a brace-enclosed initializer is not allowed here");
19535 /* Consume the opening brace. */
19536 cp_lexer_consume_token (parser->lexer);
19537 /* Skip the initializer. */
19538 cp_parser_skip_to_closing_brace (parser);
19539 /* Look for the trailing `}'. */
19540 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19542 return error_mark_node;
19545 return cp_parser_constant_expression (parser,
19546 /*allow_non_constant=*/false,
19547 NULL);
19550 /* Derived classes [gram.class.derived] */
19552 /* Parse a base-clause.
19554 base-clause:
19555 : base-specifier-list
19557 base-specifier-list:
19558 base-specifier ... [opt]
19559 base-specifier-list , base-specifier ... [opt]
19561 Returns a TREE_LIST representing the base-classes, in the order in
19562 which they were declared. The representation of each node is as
19563 described by cp_parser_base_specifier.
19565 In the case that no bases are specified, this function will return
19566 NULL_TREE, not ERROR_MARK_NODE. */
19568 static tree
19569 cp_parser_base_clause (cp_parser* parser)
19571 tree bases = NULL_TREE;
19573 /* Look for the `:' that begins the list. */
19574 cp_parser_require (parser, CPP_COLON, RT_COLON);
19576 /* Scan the base-specifier-list. */
19577 while (true)
19579 cp_token *token;
19580 tree base;
19581 bool pack_expansion_p = false;
19583 /* Look for the base-specifier. */
19584 base = cp_parser_base_specifier (parser);
19585 /* Look for the (optional) ellipsis. */
19586 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19588 /* Consume the `...'. */
19589 cp_lexer_consume_token (parser->lexer);
19591 pack_expansion_p = true;
19594 /* Add BASE to the front of the list. */
19595 if (base && base != error_mark_node)
19597 if (pack_expansion_p)
19598 /* Make this a pack expansion type. */
19599 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19601 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19603 TREE_CHAIN (base) = bases;
19604 bases = base;
19607 /* Peek at the next token. */
19608 token = cp_lexer_peek_token (parser->lexer);
19609 /* If it's not a comma, then the list is complete. */
19610 if (token->type != CPP_COMMA)
19611 break;
19612 /* Consume the `,'. */
19613 cp_lexer_consume_token (parser->lexer);
19616 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19617 base class had a qualified name. However, the next name that
19618 appears is certainly not qualified. */
19619 parser->scope = NULL_TREE;
19620 parser->qualifying_scope = NULL_TREE;
19621 parser->object_scope = NULL_TREE;
19623 return nreverse (bases);
19626 /* Parse a base-specifier.
19628 base-specifier:
19629 :: [opt] nested-name-specifier [opt] class-name
19630 virtual access-specifier [opt] :: [opt] nested-name-specifier
19631 [opt] class-name
19632 access-specifier virtual [opt] :: [opt] nested-name-specifier
19633 [opt] class-name
19635 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19636 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19637 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19638 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19640 static tree
19641 cp_parser_base_specifier (cp_parser* parser)
19643 cp_token *token;
19644 bool done = false;
19645 bool virtual_p = false;
19646 bool duplicate_virtual_error_issued_p = false;
19647 bool duplicate_access_error_issued_p = false;
19648 bool class_scope_p, template_p;
19649 tree access = access_default_node;
19650 tree type;
19652 /* Process the optional `virtual' and `access-specifier'. */
19653 while (!done)
19655 /* Peek at the next token. */
19656 token = cp_lexer_peek_token (parser->lexer);
19657 /* Process `virtual'. */
19658 switch (token->keyword)
19660 case RID_VIRTUAL:
19661 /* If `virtual' appears more than once, issue an error. */
19662 if (virtual_p && !duplicate_virtual_error_issued_p)
19664 cp_parser_error (parser,
19665 "%<virtual%> specified more than once in base-specified");
19666 duplicate_virtual_error_issued_p = true;
19669 virtual_p = true;
19671 /* Consume the `virtual' token. */
19672 cp_lexer_consume_token (parser->lexer);
19674 break;
19676 case RID_PUBLIC:
19677 case RID_PROTECTED:
19678 case RID_PRIVATE:
19679 /* If more than one access specifier appears, issue an
19680 error. */
19681 if (access != access_default_node
19682 && !duplicate_access_error_issued_p)
19684 cp_parser_error (parser,
19685 "more than one access specifier in base-specified");
19686 duplicate_access_error_issued_p = true;
19689 access = ridpointers[(int) token->keyword];
19691 /* Consume the access-specifier. */
19692 cp_lexer_consume_token (parser->lexer);
19694 break;
19696 default:
19697 done = true;
19698 break;
19701 /* It is not uncommon to see programs mechanically, erroneously, use
19702 the 'typename' keyword to denote (dependent) qualified types
19703 as base classes. */
19704 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19706 token = cp_lexer_peek_token (parser->lexer);
19707 if (!processing_template_decl)
19708 error_at (token->location,
19709 "keyword %<typename%> not allowed outside of templates");
19710 else
19711 error_at (token->location,
19712 "keyword %<typename%> not allowed in this context "
19713 "(the base class is implicitly a type)");
19714 cp_lexer_consume_token (parser->lexer);
19717 /* Look for the optional `::' operator. */
19718 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19719 /* Look for the nested-name-specifier. The simplest way to
19720 implement:
19722 [temp.res]
19724 The keyword `typename' is not permitted in a base-specifier or
19725 mem-initializer; in these contexts a qualified name that
19726 depends on a template-parameter is implicitly assumed to be a
19727 type name.
19729 is to pretend that we have seen the `typename' keyword at this
19730 point. */
19731 cp_parser_nested_name_specifier_opt (parser,
19732 /*typename_keyword_p=*/true,
19733 /*check_dependency_p=*/true,
19734 typename_type,
19735 /*is_declaration=*/true);
19736 /* If the base class is given by a qualified name, assume that names
19737 we see are type names or templates, as appropriate. */
19738 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19739 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19741 if (!parser->scope
19742 && cp_lexer_next_token_is_decltype (parser->lexer))
19743 /* DR 950 allows decltype as a base-specifier. */
19744 type = cp_parser_decltype (parser);
19745 else
19747 /* Otherwise, look for the class-name. */
19748 type = cp_parser_class_name (parser,
19749 class_scope_p,
19750 template_p,
19751 typename_type,
19752 /*check_dependency_p=*/true,
19753 /*class_head_p=*/false,
19754 /*is_declaration=*/true);
19755 type = TREE_TYPE (type);
19758 if (type == error_mark_node)
19759 return error_mark_node;
19761 return finish_base_specifier (type, access, virtual_p);
19764 /* Exception handling [gram.exception] */
19766 /* Parse an (optional) noexcept-specification.
19768 noexcept-specification:
19769 noexcept ( constant-expression ) [opt]
19771 If no noexcept-specification is present, returns NULL_TREE.
19772 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19773 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19774 there are no parentheses. CONSUMED_EXPR will be set accordingly.
19775 Otherwise, returns a noexcept specification unless RETURN_COND is true,
19776 in which case a boolean condition is returned instead. */
19778 static tree
19779 cp_parser_noexcept_specification_opt (cp_parser* parser,
19780 bool require_constexpr,
19781 bool* consumed_expr,
19782 bool return_cond)
19784 cp_token *token;
19785 const char *saved_message;
19787 /* Peek at the next token. */
19788 token = cp_lexer_peek_token (parser->lexer);
19790 /* Is it a noexcept-specification? */
19791 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19793 tree expr;
19794 cp_lexer_consume_token (parser->lexer);
19796 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19798 cp_lexer_consume_token (parser->lexer);
19800 if (require_constexpr)
19802 /* Types may not be defined in an exception-specification. */
19803 saved_message = parser->type_definition_forbidden_message;
19804 parser->type_definition_forbidden_message
19805 = G_("types may not be defined in an exception-specification");
19807 expr = cp_parser_constant_expression (parser, false, NULL);
19809 /* Restore the saved message. */
19810 parser->type_definition_forbidden_message = saved_message;
19812 else
19814 expr = cp_parser_expression (parser, false, NULL);
19815 *consumed_expr = true;
19818 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19820 else
19822 expr = boolean_true_node;
19823 if (!require_constexpr)
19824 *consumed_expr = false;
19827 /* We cannot build a noexcept-spec right away because this will check
19828 that expr is a constexpr. */
19829 if (!return_cond)
19830 return build_noexcept_spec (expr, tf_warning_or_error);
19831 else
19832 return expr;
19834 else
19835 return NULL_TREE;
19838 /* Parse an (optional) exception-specification.
19840 exception-specification:
19841 throw ( type-id-list [opt] )
19843 Returns a TREE_LIST representing the exception-specification. The
19844 TREE_VALUE of each node is a type. */
19846 static tree
19847 cp_parser_exception_specification_opt (cp_parser* parser)
19849 cp_token *token;
19850 tree type_id_list;
19851 const char *saved_message;
19853 /* Peek at the next token. */
19854 token = cp_lexer_peek_token (parser->lexer);
19856 /* Is it a noexcept-specification? */
19857 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19858 false);
19859 if (type_id_list != NULL_TREE)
19860 return type_id_list;
19862 /* If it's not `throw', then there's no exception-specification. */
19863 if (!cp_parser_is_keyword (token, RID_THROW))
19864 return NULL_TREE;
19866 #if 0
19867 /* Enable this once a lot of code has transitioned to noexcept? */
19868 if (cxx_dialect >= cxx0x && !in_system_header)
19869 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19870 "deprecated in C++0x; use %<noexcept%> instead");
19871 #endif
19873 /* Consume the `throw'. */
19874 cp_lexer_consume_token (parser->lexer);
19876 /* Look for the `('. */
19877 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19879 /* Peek at the next token. */
19880 token = cp_lexer_peek_token (parser->lexer);
19881 /* If it's not a `)', then there is a type-id-list. */
19882 if (token->type != CPP_CLOSE_PAREN)
19884 /* Types may not be defined in an exception-specification. */
19885 saved_message = parser->type_definition_forbidden_message;
19886 parser->type_definition_forbidden_message
19887 = G_("types may not be defined in an exception-specification");
19888 /* Parse the type-id-list. */
19889 type_id_list = cp_parser_type_id_list (parser);
19890 /* Restore the saved message. */
19891 parser->type_definition_forbidden_message = saved_message;
19893 else
19894 type_id_list = empty_except_spec;
19896 /* Look for the `)'. */
19897 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19899 return type_id_list;
19902 /* Parse an (optional) type-id-list.
19904 type-id-list:
19905 type-id ... [opt]
19906 type-id-list , type-id ... [opt]
19908 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19909 in the order that the types were presented. */
19911 static tree
19912 cp_parser_type_id_list (cp_parser* parser)
19914 tree types = NULL_TREE;
19916 while (true)
19918 cp_token *token;
19919 tree type;
19921 /* Get the next type-id. */
19922 type = cp_parser_type_id (parser);
19923 /* Parse the optional ellipsis. */
19924 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19926 /* Consume the `...'. */
19927 cp_lexer_consume_token (parser->lexer);
19929 /* Turn the type into a pack expansion expression. */
19930 type = make_pack_expansion (type);
19932 /* Add it to the list. */
19933 types = add_exception_specifier (types, type, /*complain=*/1);
19934 /* Peek at the next token. */
19935 token = cp_lexer_peek_token (parser->lexer);
19936 /* If it is not a `,', we are done. */
19937 if (token->type != CPP_COMMA)
19938 break;
19939 /* Consume the `,'. */
19940 cp_lexer_consume_token (parser->lexer);
19943 return nreverse (types);
19946 /* Parse a try-block.
19948 try-block:
19949 try compound-statement handler-seq */
19951 static tree
19952 cp_parser_try_block (cp_parser* parser)
19954 tree try_block;
19956 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19957 try_block = begin_try_block ();
19958 cp_parser_compound_statement (parser, NULL, true, false);
19959 finish_try_block (try_block);
19960 cp_parser_handler_seq (parser);
19961 finish_handler_sequence (try_block);
19963 return try_block;
19966 /* Parse a function-try-block.
19968 function-try-block:
19969 try ctor-initializer [opt] function-body handler-seq */
19971 static bool
19972 cp_parser_function_try_block (cp_parser* parser)
19974 tree compound_stmt;
19975 tree try_block;
19976 bool ctor_initializer_p;
19978 /* Look for the `try' keyword. */
19979 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19980 return false;
19981 /* Let the rest of the front end know where we are. */
19982 try_block = begin_function_try_block (&compound_stmt);
19983 /* Parse the function-body. */
19984 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
19985 (parser, /*in_function_try_block=*/true);
19986 /* We're done with the `try' part. */
19987 finish_function_try_block (try_block);
19988 /* Parse the handlers. */
19989 cp_parser_handler_seq (parser);
19990 /* We're done with the handlers. */
19991 finish_function_handler_sequence (try_block, compound_stmt);
19993 return ctor_initializer_p;
19996 /* Parse a handler-seq.
19998 handler-seq:
19999 handler handler-seq [opt] */
20001 static void
20002 cp_parser_handler_seq (cp_parser* parser)
20004 while (true)
20006 cp_token *token;
20008 /* Parse the handler. */
20009 cp_parser_handler (parser);
20010 /* Peek at the next token. */
20011 token = cp_lexer_peek_token (parser->lexer);
20012 /* If it's not `catch' then there are no more handlers. */
20013 if (!cp_parser_is_keyword (token, RID_CATCH))
20014 break;
20018 /* Parse a handler.
20020 handler:
20021 catch ( exception-declaration ) compound-statement */
20023 static void
20024 cp_parser_handler (cp_parser* parser)
20026 tree handler;
20027 tree declaration;
20029 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
20030 handler = begin_handler ();
20031 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20032 declaration = cp_parser_exception_declaration (parser);
20033 finish_handler_parms (declaration, handler);
20034 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20035 cp_parser_compound_statement (parser, NULL, false, false);
20036 finish_handler (handler);
20039 /* Parse an exception-declaration.
20041 exception-declaration:
20042 type-specifier-seq declarator
20043 type-specifier-seq abstract-declarator
20044 type-specifier-seq
20047 Returns a VAR_DECL for the declaration, or NULL_TREE if the
20048 ellipsis variant is used. */
20050 static tree
20051 cp_parser_exception_declaration (cp_parser* parser)
20053 cp_decl_specifier_seq type_specifiers;
20054 cp_declarator *declarator;
20055 const char *saved_message;
20057 /* If it's an ellipsis, it's easy to handle. */
20058 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20060 /* Consume the `...' token. */
20061 cp_lexer_consume_token (parser->lexer);
20062 return NULL_TREE;
20065 /* Types may not be defined in exception-declarations. */
20066 saved_message = parser->type_definition_forbidden_message;
20067 parser->type_definition_forbidden_message
20068 = G_("types may not be defined in exception-declarations");
20070 /* Parse the type-specifier-seq. */
20071 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
20072 /*is_trailing_return=*/false,
20073 &type_specifiers);
20074 /* If it's a `)', then there is no declarator. */
20075 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
20076 declarator = NULL;
20077 else
20078 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
20079 /*ctor_dtor_or_conv_p=*/NULL,
20080 /*parenthesized_p=*/NULL,
20081 /*member_p=*/false);
20083 /* Restore the saved message. */
20084 parser->type_definition_forbidden_message = saved_message;
20086 if (!type_specifiers.any_specifiers_p)
20087 return error_mark_node;
20089 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
20092 /* Parse a throw-expression.
20094 throw-expression:
20095 throw assignment-expression [opt]
20097 Returns a THROW_EXPR representing the throw-expression. */
20099 static tree
20100 cp_parser_throw_expression (cp_parser* parser)
20102 tree expression;
20103 cp_token* token;
20105 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
20106 token = cp_lexer_peek_token (parser->lexer);
20107 /* Figure out whether or not there is an assignment-expression
20108 following the "throw" keyword. */
20109 if (token->type == CPP_COMMA
20110 || token->type == CPP_SEMICOLON
20111 || token->type == CPP_CLOSE_PAREN
20112 || token->type == CPP_CLOSE_SQUARE
20113 || token->type == CPP_CLOSE_BRACE
20114 || token->type == CPP_COLON)
20115 expression = NULL_TREE;
20116 else
20117 expression = cp_parser_assignment_expression (parser,
20118 /*cast_p=*/false, NULL);
20120 return build_throw (expression);
20123 /* GNU Extensions */
20125 /* Parse an (optional) asm-specification.
20127 asm-specification:
20128 asm ( string-literal )
20130 If the asm-specification is present, returns a STRING_CST
20131 corresponding to the string-literal. Otherwise, returns
20132 NULL_TREE. */
20134 static tree
20135 cp_parser_asm_specification_opt (cp_parser* parser)
20137 cp_token *token;
20138 tree asm_specification;
20140 /* Peek at the next token. */
20141 token = cp_lexer_peek_token (parser->lexer);
20142 /* If the next token isn't the `asm' keyword, then there's no
20143 asm-specification. */
20144 if (!cp_parser_is_keyword (token, RID_ASM))
20145 return NULL_TREE;
20147 /* Consume the `asm' token. */
20148 cp_lexer_consume_token (parser->lexer);
20149 /* Look for the `('. */
20150 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20152 /* Look for the string-literal. */
20153 asm_specification = cp_parser_string_literal (parser, false, false);
20155 /* Look for the `)'. */
20156 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20158 return asm_specification;
20161 /* Parse an asm-operand-list.
20163 asm-operand-list:
20164 asm-operand
20165 asm-operand-list , asm-operand
20167 asm-operand:
20168 string-literal ( expression )
20169 [ string-literal ] string-literal ( expression )
20171 Returns a TREE_LIST representing the operands. The TREE_VALUE of
20172 each node is the expression. The TREE_PURPOSE is itself a
20173 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
20174 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
20175 is a STRING_CST for the string literal before the parenthesis. Returns
20176 ERROR_MARK_NODE if any of the operands are invalid. */
20178 static tree
20179 cp_parser_asm_operand_list (cp_parser* parser)
20181 tree asm_operands = NULL_TREE;
20182 bool invalid_operands = false;
20184 while (true)
20186 tree string_literal;
20187 tree expression;
20188 tree name;
20190 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20192 /* Consume the `[' token. */
20193 cp_lexer_consume_token (parser->lexer);
20194 /* Read the operand name. */
20195 name = cp_parser_identifier (parser);
20196 if (name != error_mark_node)
20197 name = build_string (IDENTIFIER_LENGTH (name),
20198 IDENTIFIER_POINTER (name));
20199 /* Look for the closing `]'. */
20200 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20202 else
20203 name = NULL_TREE;
20204 /* Look for the string-literal. */
20205 string_literal = cp_parser_string_literal (parser, false, false);
20207 /* Look for the `('. */
20208 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20209 /* Parse the expression. */
20210 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20211 /* Look for the `)'. */
20212 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20214 if (name == error_mark_node
20215 || string_literal == error_mark_node
20216 || expression == error_mark_node)
20217 invalid_operands = true;
20219 /* Add this operand to the list. */
20220 asm_operands = tree_cons (build_tree_list (name, string_literal),
20221 expression,
20222 asm_operands);
20223 /* If the next token is not a `,', there are no more
20224 operands. */
20225 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20226 break;
20227 /* Consume the `,'. */
20228 cp_lexer_consume_token (parser->lexer);
20231 return invalid_operands ? error_mark_node : nreverse (asm_operands);
20234 /* Parse an asm-clobber-list.
20236 asm-clobber-list:
20237 string-literal
20238 asm-clobber-list , string-literal
20240 Returns a TREE_LIST, indicating the clobbers in the order that they
20241 appeared. The TREE_VALUE of each node is a STRING_CST. */
20243 static tree
20244 cp_parser_asm_clobber_list (cp_parser* parser)
20246 tree clobbers = NULL_TREE;
20248 while (true)
20250 tree string_literal;
20252 /* Look for the string literal. */
20253 string_literal = cp_parser_string_literal (parser, false, false);
20254 /* Add it to the list. */
20255 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20256 /* If the next token is not a `,', then the list is
20257 complete. */
20258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20259 break;
20260 /* Consume the `,' token. */
20261 cp_lexer_consume_token (parser->lexer);
20264 return clobbers;
20267 /* Parse an asm-label-list.
20269 asm-label-list:
20270 identifier
20271 asm-label-list , identifier
20273 Returns a TREE_LIST, indicating the labels in the order that they
20274 appeared. The TREE_VALUE of each node is a label. */
20276 static tree
20277 cp_parser_asm_label_list (cp_parser* parser)
20279 tree labels = NULL_TREE;
20281 while (true)
20283 tree identifier, label, name;
20285 /* Look for the identifier. */
20286 identifier = cp_parser_identifier (parser);
20287 if (!error_operand_p (identifier))
20289 label = lookup_label (identifier);
20290 if (TREE_CODE (label) == LABEL_DECL)
20292 TREE_USED (label) = 1;
20293 check_goto (label);
20294 name = build_string (IDENTIFIER_LENGTH (identifier),
20295 IDENTIFIER_POINTER (identifier));
20296 labels = tree_cons (name, label, labels);
20299 /* If the next token is not a `,', then the list is
20300 complete. */
20301 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20302 break;
20303 /* Consume the `,' token. */
20304 cp_lexer_consume_token (parser->lexer);
20307 return nreverse (labels);
20310 /* Return TRUE iff the next tokens in the stream are possibly the
20311 beginning of a GNU extension attribute. */
20313 static bool
20314 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
20316 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
20319 /* Return TRUE iff the next tokens in the stream are possibly the
20320 beginning of a standard C++-11 attribute specifier. */
20322 static bool
20323 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
20325 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
20328 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20329 beginning of a standard C++-11 attribute specifier. */
20331 static bool
20332 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
20334 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20336 return (cxx_dialect >= cxx0x
20337 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
20338 || (token->type == CPP_OPEN_SQUARE
20339 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
20340 && token->type == CPP_OPEN_SQUARE)));
20343 /* Return TRUE iff the next Nth tokens in the stream are possibly the
20344 beginning of a GNU extension attribute. */
20346 static bool
20347 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
20349 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
20351 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
20354 /* Return true iff the next tokens can be the beginning of either a
20355 GNU attribute list, or a standard C++11 attribute sequence. */
20357 static bool
20358 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
20360 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
20361 || cp_next_tokens_can_be_std_attribute_p (parser));
20364 /* Return true iff the next Nth tokens can be the beginning of either
20365 a GNU attribute list, or a standard C++11 attribute sequence. */
20367 static bool
20368 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
20370 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
20371 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
20374 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
20375 of GNU attributes, or return NULL. */
20377 static tree
20378 cp_parser_attributes_opt (cp_parser *parser)
20380 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
20381 return cp_parser_gnu_attributes_opt (parser);
20382 return cp_parser_std_attribute_spec_seq (parser);
20385 /* Parse an (optional) series of attributes.
20387 attributes:
20388 attributes attribute
20390 attribute:
20391 __attribute__ (( attribute-list [opt] ))
20393 The return value is as for cp_parser_gnu_attribute_list. */
20395 static tree
20396 cp_parser_gnu_attributes_opt (cp_parser* parser)
20398 tree attributes = NULL_TREE;
20400 while (true)
20402 cp_token *token;
20403 tree attribute_list;
20404 bool ok = true;
20406 /* Peek at the next token. */
20407 token = cp_lexer_peek_token (parser->lexer);
20408 /* If it's not `__attribute__', then we're done. */
20409 if (token->keyword != RID_ATTRIBUTE)
20410 break;
20412 /* Consume the `__attribute__' keyword. */
20413 cp_lexer_consume_token (parser->lexer);
20414 /* Look for the two `(' tokens. */
20415 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20416 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20418 /* Peek at the next token. */
20419 token = cp_lexer_peek_token (parser->lexer);
20420 if (token->type != CPP_CLOSE_PAREN)
20421 /* Parse the attribute-list. */
20422 attribute_list = cp_parser_gnu_attribute_list (parser);
20423 else
20424 /* If the next token is a `)', then there is no attribute
20425 list. */
20426 attribute_list = NULL;
20428 /* Look for the two `)' tokens. */
20429 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20430 ok = false;
20431 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20432 ok = false;
20433 if (!ok)
20434 cp_parser_skip_to_end_of_statement (parser);
20436 /* Add these new attributes to the list. */
20437 attributes = chainon (attributes, attribute_list);
20440 return attributes;
20443 /* Parse a GNU attribute-list.
20445 attribute-list:
20446 attribute
20447 attribute-list , attribute
20449 attribute:
20450 identifier
20451 identifier ( identifier )
20452 identifier ( identifier , expression-list )
20453 identifier ( expression-list )
20455 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20456 to an attribute. The TREE_PURPOSE of each node is the identifier
20457 indicating which attribute is in use. The TREE_VALUE represents
20458 the arguments, if any. */
20460 static tree
20461 cp_parser_gnu_attribute_list (cp_parser* parser)
20463 tree attribute_list = NULL_TREE;
20464 bool save_translate_strings_p = parser->translate_strings_p;
20466 parser->translate_strings_p = false;
20467 while (true)
20469 cp_token *token;
20470 tree identifier;
20471 tree attribute;
20473 /* Look for the identifier. We also allow keywords here; for
20474 example `__attribute__ ((const))' is legal. */
20475 token = cp_lexer_peek_token (parser->lexer);
20476 if (token->type == CPP_NAME
20477 || token->type == CPP_KEYWORD)
20479 tree arguments = NULL_TREE;
20481 /* Consume the token. */
20482 token = cp_lexer_consume_token (parser->lexer);
20484 /* Save away the identifier that indicates which attribute
20485 this is. */
20486 identifier = (token->type == CPP_KEYWORD)
20487 /* For keywords, use the canonical spelling, not the
20488 parsed identifier. */
20489 ? ridpointers[(int) token->keyword]
20490 : token->u.value;
20492 attribute = build_tree_list (identifier, NULL_TREE);
20494 /* Peek at the next token. */
20495 token = cp_lexer_peek_token (parser->lexer);
20496 /* If it's an `(', then parse the attribute arguments. */
20497 if (token->type == CPP_OPEN_PAREN)
20499 vec<tree, va_gc> *vec;
20500 int attr_flag = (attribute_takes_identifier_p (identifier)
20501 ? id_attr : normal_attr);
20502 vec = cp_parser_parenthesized_expression_list
20503 (parser, attr_flag, /*cast_p=*/false,
20504 /*allow_expansion_p=*/false,
20505 /*non_constant_p=*/NULL);
20506 if (vec == NULL)
20507 arguments = error_mark_node;
20508 else
20510 arguments = build_tree_list_vec (vec);
20511 release_tree_vector (vec);
20513 /* Save the arguments away. */
20514 TREE_VALUE (attribute) = arguments;
20517 if (arguments != error_mark_node)
20519 /* Add this attribute to the list. */
20520 TREE_CHAIN (attribute) = attribute_list;
20521 attribute_list = attribute;
20524 token = cp_lexer_peek_token (parser->lexer);
20526 /* Now, look for more attributes. If the next token isn't a
20527 `,', we're done. */
20528 if (token->type != CPP_COMMA)
20529 break;
20531 /* Consume the comma and keep going. */
20532 cp_lexer_consume_token (parser->lexer);
20534 parser->translate_strings_p = save_translate_strings_p;
20536 /* We built up the list in reverse order. */
20537 return nreverse (attribute_list);
20540 /* Parse a standard C++11 attribute.
20542 The returned representation is a TREE_LIST which TREE_PURPOSE is
20543 the scoped name of the attribute, and the TREE_VALUE is its
20544 arguments list.
20546 Note that the scoped name of the attribute is itself a TREE_LIST
20547 which TREE_PURPOSE is the namespace of the attribute, and
20548 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
20549 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
20550 and which TREE_PURPOSE is directly the attribute name.
20552 Clients of the attribute code should use get_attribute_namespace
20553 and get_attribute_name to get the actual namespace and name of
20554 attributes, regardless of their being GNU or C++11 attributes.
20556 attribute:
20557 attribute-token attribute-argument-clause [opt]
20559 attribute-token:
20560 identifier
20561 attribute-scoped-token
20563 attribute-scoped-token:
20564 attribute-namespace :: identifier
20566 attribute-namespace:
20567 identifier
20569 attribute-argument-clause:
20570 ( balanced-token-seq )
20572 balanced-token-seq:
20573 balanced-token [opt]
20574 balanced-token-seq balanced-token
20576 balanced-token:
20577 ( balanced-token-seq )
20578 [ balanced-token-seq ]
20579 { balanced-token-seq }. */
20581 static tree
20582 cp_parser_std_attribute (cp_parser *parser)
20584 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
20585 cp_token *token;
20587 /* First, parse name of the the attribute, a.k.a
20588 attribute-token. */
20590 token = cp_lexer_peek_token (parser->lexer);
20591 if (token->type == CPP_NAME)
20592 attr_id = token->u.value;
20593 else if (token->type == CPP_KEYWORD)
20594 attr_id = ridpointers[(int) token->keyword];
20595 else if (token->flags & NAMED_OP)
20596 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
20598 if (attr_id == NULL_TREE)
20599 return NULL_TREE;
20601 cp_lexer_consume_token (parser->lexer);
20603 token = cp_lexer_peek_token (parser->lexer);
20604 if (token->type == CPP_SCOPE)
20606 /* We are seeing a scoped attribute token. */
20608 cp_lexer_consume_token (parser->lexer);
20609 attr_ns = attr_id;
20611 token = cp_lexer_consume_token (parser->lexer);
20612 if (token->type == CPP_NAME)
20613 attr_id = token->u.value;
20614 else if (token->type == CPP_KEYWORD)
20615 attr_id = ridpointers[(int) token->keyword];
20616 else
20618 error_at (token->location,
20619 "expected an identifier for the attribute name");
20620 return error_mark_node;
20622 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
20623 NULL_TREE);
20624 token = cp_lexer_peek_token (parser->lexer);
20626 else
20627 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
20628 NULL_TREE);
20630 /* Now parse the optional argument clause of the attribute. */
20632 if (token->type != CPP_OPEN_PAREN)
20633 return attribute;
20636 vec<tree, va_gc> *vec;
20637 int attr_flag = normal_attr;
20639 if (attr_ns == get_identifier ("gnu")
20640 && attribute_takes_identifier_p (attr_id))
20641 /* A GNU attribute that takes an identifier in parameter. */
20642 attr_flag = id_attr;
20644 vec = cp_parser_parenthesized_expression_list
20645 (parser, attr_flag, /*cast_p=*/false,
20646 /*allow_expansion_p=*/true,
20647 /*non_constant_p=*/NULL);
20648 if (vec == NULL)
20649 arguments = error_mark_node;
20650 else
20652 arguments = build_tree_list_vec (vec);
20653 release_tree_vector (vec);
20656 if (arguments == error_mark_node)
20657 attribute = error_mark_node;
20658 else
20659 TREE_VALUE (attribute) = arguments;
20662 return attribute;
20665 /* Parse a list of standard C++-11 attributes.
20667 attribute-list:
20668 attribute [opt]
20669 attribute-list , attribute[opt]
20670 attribute ...
20671 attribute-list , attribute ...
20674 static tree
20675 cp_parser_std_attribute_list (cp_parser *parser)
20677 tree attributes = NULL_TREE, attribute = NULL_TREE;
20678 cp_token *token = NULL;
20680 while (true)
20682 attribute = cp_parser_std_attribute (parser);
20683 if (attribute == error_mark_node)
20684 break;
20685 if (attribute != NULL_TREE)
20687 TREE_CHAIN (attribute) = attributes;
20688 attributes = attribute;
20690 token = cp_lexer_peek_token (parser->lexer);
20691 if (token->type != CPP_COMMA)
20692 break;
20693 cp_lexer_consume_token (parser->lexer);
20695 attributes = nreverse (attributes);
20696 return attributes;
20699 /* Parse a standard C++-11 attribute specifier.
20701 attribute-specifier:
20702 [ [ attribute-list ] ]
20703 alignment-specifier
20705 alignment-specifier:
20706 alignas ( type-id ... [opt] )
20707 alignas ( alignment-expression ... [opt] ). */
20709 static tree
20710 cp_parser_std_attribute_spec (cp_parser *parser)
20712 tree attributes = NULL_TREE;
20713 cp_token *token = cp_lexer_peek_token (parser->lexer);
20715 if (token->type == CPP_OPEN_SQUARE
20716 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
20718 cp_lexer_consume_token (parser->lexer);
20719 cp_lexer_consume_token (parser->lexer);
20721 attributes = cp_parser_std_attribute_list (parser);
20723 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
20724 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20725 cp_parser_skip_to_end_of_statement (parser);
20726 else
20727 /* Warn about parsing c++11 attribute in non-c++1 mode, only
20728 when we are sure that we have actually parsed them. */
20729 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
20731 else
20733 tree alignas_expr;
20735 /* Look for an alignment-specifier. */
20737 token = cp_lexer_peek_token (parser->lexer);
20739 if (token->type != CPP_KEYWORD
20740 || token->keyword != RID_ALIGNAS)
20741 return NULL_TREE;
20743 cp_lexer_consume_token (parser->lexer);
20744 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
20746 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
20748 cp_parser_error (parser, "expected %<(%>");
20749 return error_mark_node;
20752 cp_parser_parse_tentatively (parser);
20753 alignas_expr = cp_parser_type_id (parser);
20755 if (!cp_parser_parse_definitely (parser))
20757 gcc_assert (alignas_expr == error_mark_node
20758 || alignas_expr == NULL_TREE);
20760 alignas_expr =
20761 cp_parser_assignment_expression (parser, /*cast_p=*/false,
20762 /**cp_id_kind=*/NULL);
20763 if (alignas_expr == NULL_TREE
20764 || alignas_expr == error_mark_node)
20765 return alignas_expr;
20768 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
20770 cp_parser_error (parser, "expected %<)%>");
20771 return error_mark_node;
20774 alignas_expr = cxx_alignas_expr (alignas_expr);
20776 /* Build the C++-11 representation of an 'aligned'
20777 attribute. */
20778 attributes =
20779 build_tree_list (build_tree_list (get_identifier ("gnu"),
20780 get_identifier ("aligned")),
20781 build_tree_list (NULL_TREE, alignas_expr));
20784 return attributes;
20787 /* Parse a standard C++-11 attribute-specifier-seq.
20789 attribute-specifier-seq:
20790 attribute-specifier-seq [opt] attribute-specifier
20793 static tree
20794 cp_parser_std_attribute_spec_seq (cp_parser *parser)
20796 tree attr_specs = NULL;
20798 while (true)
20800 tree attr_spec = cp_parser_std_attribute_spec (parser);
20801 if (attr_spec == NULL_TREE)
20802 break;
20803 if (attr_spec == error_mark_node)
20804 return error_mark_node;
20806 TREE_CHAIN (attr_spec) = attr_specs;
20807 attr_specs = attr_spec;
20810 attr_specs = nreverse (attr_specs);
20811 return attr_specs;
20814 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20815 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20816 current value of the PEDANTIC flag, regardless of whether or not
20817 the `__extension__' keyword is present. The caller is responsible
20818 for restoring the value of the PEDANTIC flag. */
20820 static bool
20821 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20823 /* Save the old value of the PEDANTIC flag. */
20824 *saved_pedantic = pedantic;
20826 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20828 /* Consume the `__extension__' token. */
20829 cp_lexer_consume_token (parser->lexer);
20830 /* We're not being pedantic while the `__extension__' keyword is
20831 in effect. */
20832 pedantic = 0;
20834 return true;
20837 return false;
20840 /* Parse a label declaration.
20842 label-declaration:
20843 __label__ label-declarator-seq ;
20845 label-declarator-seq:
20846 identifier , label-declarator-seq
20847 identifier */
20849 static void
20850 cp_parser_label_declaration (cp_parser* parser)
20852 /* Look for the `__label__' keyword. */
20853 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20855 while (true)
20857 tree identifier;
20859 /* Look for an identifier. */
20860 identifier = cp_parser_identifier (parser);
20861 /* If we failed, stop. */
20862 if (identifier == error_mark_node)
20863 break;
20864 /* Declare it as a label. */
20865 finish_label_decl (identifier);
20866 /* If the next token is a `;', stop. */
20867 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20868 break;
20869 /* Look for the `,' separating the label declarations. */
20870 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20873 /* Look for the final `;'. */
20874 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20877 /* Support Functions */
20879 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20880 NAME should have one of the representations used for an
20881 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20882 is returned. If PARSER->SCOPE is a dependent type, then a
20883 SCOPE_REF is returned.
20885 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20886 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20887 was formed. Abstractly, such entities should not be passed to this
20888 function, because they do not need to be looked up, but it is
20889 simpler to check for this special case here, rather than at the
20890 call-sites.
20892 In cases not explicitly covered above, this function returns a
20893 DECL, OVERLOAD, or baselink representing the result of the lookup.
20894 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20895 is returned.
20897 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20898 (e.g., "struct") that was used. In that case bindings that do not
20899 refer to types are ignored.
20901 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20902 ignored.
20904 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20905 are ignored.
20907 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20908 types.
20910 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20911 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20912 NULL_TREE otherwise. */
20914 static tree
20915 cp_parser_lookup_name (cp_parser *parser, tree name,
20916 enum tag_types tag_type,
20917 bool is_template,
20918 bool is_namespace,
20919 bool check_dependency,
20920 tree *ambiguous_decls,
20921 location_t name_location)
20923 tree decl;
20924 tree object_type = parser->context->object_type;
20926 /* Assume that the lookup will be unambiguous. */
20927 if (ambiguous_decls)
20928 *ambiguous_decls = NULL_TREE;
20930 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20931 no longer valid. Note that if we are parsing tentatively, and
20932 the parse fails, OBJECT_TYPE will be automatically restored. */
20933 parser->context->object_type = NULL_TREE;
20935 if (name == error_mark_node)
20936 return error_mark_node;
20938 /* A template-id has already been resolved; there is no lookup to
20939 do. */
20940 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20941 return name;
20942 if (BASELINK_P (name))
20944 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20945 == TEMPLATE_ID_EXPR);
20946 return name;
20949 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20950 it should already have been checked to make sure that the name
20951 used matches the type being destroyed. */
20952 if (TREE_CODE (name) == BIT_NOT_EXPR)
20954 tree type;
20956 /* Figure out to which type this destructor applies. */
20957 if (parser->scope)
20958 type = parser->scope;
20959 else if (object_type)
20960 type = object_type;
20961 else
20962 type = current_class_type;
20963 /* If that's not a class type, there is no destructor. */
20964 if (!type || !CLASS_TYPE_P (type))
20965 return error_mark_node;
20966 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20967 lazily_declare_fn (sfk_destructor, type);
20968 if (!CLASSTYPE_DESTRUCTORS (type))
20969 return error_mark_node;
20970 /* If it was a class type, return the destructor. */
20971 return CLASSTYPE_DESTRUCTORS (type);
20974 /* By this point, the NAME should be an ordinary identifier. If
20975 the id-expression was a qualified name, the qualifying scope is
20976 stored in PARSER->SCOPE at this point. */
20977 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20979 /* Perform the lookup. */
20980 if (parser->scope)
20982 bool dependent_p;
20984 if (parser->scope == error_mark_node)
20985 return error_mark_node;
20987 /* If the SCOPE is dependent, the lookup must be deferred until
20988 the template is instantiated -- unless we are explicitly
20989 looking up names in uninstantiated templates. Even then, we
20990 cannot look up the name if the scope is not a class type; it
20991 might, for example, be a template type parameter. */
20992 dependent_p = (TYPE_P (parser->scope)
20993 && dependent_scope_p (parser->scope));
20994 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20995 && dependent_p)
20996 /* Defer lookup. */
20997 decl = error_mark_node;
20998 else
21000 tree pushed_scope = NULL_TREE;
21002 /* If PARSER->SCOPE is a dependent type, then it must be a
21003 class type, and we must not be checking dependencies;
21004 otherwise, we would have processed this lookup above. So
21005 that PARSER->SCOPE is not considered a dependent base by
21006 lookup_member, we must enter the scope here. */
21007 if (dependent_p)
21008 pushed_scope = push_scope (parser->scope);
21010 /* If the PARSER->SCOPE is a template specialization, it
21011 may be instantiated during name lookup. In that case,
21012 errors may be issued. Even if we rollback the current
21013 tentative parse, those errors are valid. */
21014 decl = lookup_qualified_name (parser->scope, name,
21015 tag_type != none_type,
21016 /*complain=*/true);
21018 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
21019 lookup result and the nested-name-specifier nominates a class C:
21020 * if the name specified after the nested-name-specifier, when
21021 looked up in C, is the injected-class-name of C (Clause 9), or
21022 * if the name specified after the nested-name-specifier is the
21023 same as the identifier or the simple-template-id's template-
21024 name in the last component of the nested-name-specifier,
21025 the name is instead considered to name the constructor of
21026 class C. [ Note: for example, the constructor is not an
21027 acceptable lookup result in an elaborated-type-specifier so
21028 the constructor would not be used in place of the
21029 injected-class-name. --end note ] Such a constructor name
21030 shall be used only in the declarator-id of a declaration that
21031 names a constructor or in a using-declaration. */
21032 if (tag_type == none_type
21033 && DECL_SELF_REFERENCE_P (decl)
21034 && same_type_p (DECL_CONTEXT (decl), parser->scope))
21035 decl = lookup_qualified_name (parser->scope, ctor_identifier,
21036 tag_type != none_type,
21037 /*complain=*/true);
21039 /* If we have a single function from a using decl, pull it out. */
21040 if (TREE_CODE (decl) == OVERLOAD
21041 && !really_overloaded_fn (decl))
21042 decl = OVL_FUNCTION (decl);
21044 if (pushed_scope)
21045 pop_scope (pushed_scope);
21048 /* If the scope is a dependent type and either we deferred lookup or
21049 we did lookup but didn't find the name, rememeber the name. */
21050 if (decl == error_mark_node && TYPE_P (parser->scope)
21051 && dependent_type_p (parser->scope))
21053 if (tag_type)
21055 tree type;
21057 /* The resolution to Core Issue 180 says that `struct
21058 A::B' should be considered a type-name, even if `A'
21059 is dependent. */
21060 type = make_typename_type (parser->scope, name, tag_type,
21061 /*complain=*/tf_error);
21062 decl = TYPE_NAME (type);
21064 else if (is_template
21065 && (cp_parser_next_token_ends_template_argument_p (parser)
21066 || cp_lexer_next_token_is (parser->lexer,
21067 CPP_CLOSE_PAREN)))
21068 decl = make_unbound_class_template (parser->scope,
21069 name, NULL_TREE,
21070 /*complain=*/tf_error);
21071 else
21072 decl = build_qualified_name (/*type=*/NULL_TREE,
21073 parser->scope, name,
21074 is_template);
21076 parser->qualifying_scope = parser->scope;
21077 parser->object_scope = NULL_TREE;
21079 else if (object_type)
21081 tree object_decl = NULL_TREE;
21082 /* Look up the name in the scope of the OBJECT_TYPE, unless the
21083 OBJECT_TYPE is not a class. */
21084 if (CLASS_TYPE_P (object_type))
21085 /* If the OBJECT_TYPE is a template specialization, it may
21086 be instantiated during name lookup. In that case, errors
21087 may be issued. Even if we rollback the current tentative
21088 parse, those errors are valid. */
21089 object_decl = lookup_member (object_type,
21090 name,
21091 /*protect=*/0,
21092 tag_type != none_type,
21093 tf_warning_or_error);
21094 /* Look it up in the enclosing context, too. */
21095 decl = lookup_name_real (name, tag_type != none_type,
21096 /*nonclass=*/0,
21097 /*block_p=*/true, is_namespace, 0);
21098 parser->object_scope = object_type;
21099 parser->qualifying_scope = NULL_TREE;
21100 if (object_decl)
21101 decl = object_decl;
21103 else
21105 decl = lookup_name_real (name, tag_type != none_type,
21106 /*nonclass=*/0,
21107 /*block_p=*/true, is_namespace, 0);
21108 parser->qualifying_scope = NULL_TREE;
21109 parser->object_scope = NULL_TREE;
21112 /* If the lookup failed, let our caller know. */
21113 if (!decl || decl == error_mark_node)
21114 return error_mark_node;
21116 /* Pull out the template from an injected-class-name (or multiple). */
21117 if (is_template)
21118 decl = maybe_get_template_decl_from_type_decl (decl);
21120 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
21121 if (TREE_CODE (decl) == TREE_LIST)
21123 if (ambiguous_decls)
21124 *ambiguous_decls = decl;
21125 /* The error message we have to print is too complicated for
21126 cp_parser_error, so we incorporate its actions directly. */
21127 if (!cp_parser_simulate_error (parser))
21129 error_at (name_location, "reference to %qD is ambiguous",
21130 name);
21131 print_candidates (decl);
21133 return error_mark_node;
21136 gcc_assert (DECL_P (decl)
21137 || TREE_CODE (decl) == OVERLOAD
21138 || TREE_CODE (decl) == SCOPE_REF
21139 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
21140 || BASELINK_P (decl));
21142 /* If we have resolved the name of a member declaration, check to
21143 see if the declaration is accessible. When the name resolves to
21144 set of overloaded functions, accessibility is checked when
21145 overload resolution is done.
21147 During an explicit instantiation, access is not checked at all,
21148 as per [temp.explicit]. */
21149 if (DECL_P (decl))
21150 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
21152 maybe_record_typedef_use (decl);
21154 return decl;
21157 /* Like cp_parser_lookup_name, but for use in the typical case where
21158 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
21159 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
21161 static tree
21162 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
21164 return cp_parser_lookup_name (parser, name,
21165 none_type,
21166 /*is_template=*/false,
21167 /*is_namespace=*/false,
21168 /*check_dependency=*/true,
21169 /*ambiguous_decls=*/NULL,
21170 location);
21173 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
21174 the current context, return the TYPE_DECL. If TAG_NAME_P is
21175 true, the DECL indicates the class being defined in a class-head,
21176 or declared in an elaborated-type-specifier.
21178 Otherwise, return DECL. */
21180 static tree
21181 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
21183 /* If the TEMPLATE_DECL is being declared as part of a class-head,
21184 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
21186 struct A {
21187 template <typename T> struct B;
21190 template <typename T> struct A::B {};
21192 Similarly, in an elaborated-type-specifier:
21194 namespace N { struct X{}; }
21196 struct A {
21197 template <typename T> friend struct N::X;
21200 However, if the DECL refers to a class type, and we are in
21201 the scope of the class, then the name lookup automatically
21202 finds the TYPE_DECL created by build_self_reference rather
21203 than a TEMPLATE_DECL. For example, in:
21205 template <class T> struct S {
21206 S s;
21209 there is no need to handle such case. */
21211 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
21212 return DECL_TEMPLATE_RESULT (decl);
21214 return decl;
21217 /* If too many, or too few, template-parameter lists apply to the
21218 declarator, issue an error message. Returns TRUE if all went well,
21219 and FALSE otherwise. */
21221 static bool
21222 cp_parser_check_declarator_template_parameters (cp_parser* parser,
21223 cp_declarator *declarator,
21224 location_t declarator_location)
21226 switch (declarator->kind)
21228 case cdk_id:
21230 unsigned num_templates = 0;
21231 tree scope = declarator->u.id.qualifying_scope;
21233 if (scope)
21234 num_templates = num_template_headers_for_class (scope);
21235 else if (TREE_CODE (declarator->u.id.unqualified_name)
21236 == TEMPLATE_ID_EXPR)
21237 /* If the DECLARATOR has the form `X<y>' then it uses one
21238 additional level of template parameters. */
21239 ++num_templates;
21241 return cp_parser_check_template_parameters
21242 (parser, num_templates, declarator_location, declarator);
21245 case cdk_function:
21246 case cdk_array:
21247 case cdk_pointer:
21248 case cdk_reference:
21249 case cdk_ptrmem:
21250 return (cp_parser_check_declarator_template_parameters
21251 (parser, declarator->declarator, declarator_location));
21253 case cdk_error:
21254 return true;
21256 default:
21257 gcc_unreachable ();
21259 return false;
21262 /* NUM_TEMPLATES were used in the current declaration. If that is
21263 invalid, return FALSE and issue an error messages. Otherwise,
21264 return TRUE. If DECLARATOR is non-NULL, then we are checking a
21265 declarator and we can print more accurate diagnostics. */
21267 static bool
21268 cp_parser_check_template_parameters (cp_parser* parser,
21269 unsigned num_templates,
21270 location_t location,
21271 cp_declarator *declarator)
21273 /* If there are the same number of template classes and parameter
21274 lists, that's OK. */
21275 if (parser->num_template_parameter_lists == num_templates)
21276 return true;
21277 /* If there are more, but only one more, then we are referring to a
21278 member template. That's OK too. */
21279 if (parser->num_template_parameter_lists == num_templates + 1)
21280 return true;
21281 /* If there are more template classes than parameter lists, we have
21282 something like:
21284 template <class T> void S<T>::R<T>::f (); */
21285 if (parser->num_template_parameter_lists < num_templates)
21287 if (declarator && !current_function_decl)
21288 error_at (location, "specializing member %<%T::%E%> "
21289 "requires %<template<>%> syntax",
21290 declarator->u.id.qualifying_scope,
21291 declarator->u.id.unqualified_name);
21292 else if (declarator)
21293 error_at (location, "invalid declaration of %<%T::%E%>",
21294 declarator->u.id.qualifying_scope,
21295 declarator->u.id.unqualified_name);
21296 else
21297 error_at (location, "too few template-parameter-lists");
21298 return false;
21300 /* Otherwise, there are too many template parameter lists. We have
21301 something like:
21303 template <class T> template <class U> void S::f(); */
21304 error_at (location, "too many template-parameter-lists");
21305 return false;
21308 /* Parse an optional `::' token indicating that the following name is
21309 from the global namespace. If so, PARSER->SCOPE is set to the
21310 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
21311 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
21312 Returns the new value of PARSER->SCOPE, if the `::' token is
21313 present, and NULL_TREE otherwise. */
21315 static tree
21316 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
21318 cp_token *token;
21320 /* Peek at the next token. */
21321 token = cp_lexer_peek_token (parser->lexer);
21322 /* If we're looking at a `::' token then we're starting from the
21323 global namespace, not our current location. */
21324 if (token->type == CPP_SCOPE)
21326 /* Consume the `::' token. */
21327 cp_lexer_consume_token (parser->lexer);
21328 /* Set the SCOPE so that we know where to start the lookup. */
21329 parser->scope = global_namespace;
21330 parser->qualifying_scope = global_namespace;
21331 parser->object_scope = NULL_TREE;
21333 return parser->scope;
21335 else if (!current_scope_valid_p)
21337 parser->scope = NULL_TREE;
21338 parser->qualifying_scope = NULL_TREE;
21339 parser->object_scope = NULL_TREE;
21342 return NULL_TREE;
21345 /* Returns TRUE if the upcoming token sequence is the start of a
21346 constructor declarator. If FRIEND_P is true, the declarator is
21347 preceded by the `friend' specifier. */
21349 static bool
21350 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
21352 bool constructor_p;
21353 tree nested_name_specifier;
21354 cp_token *next_token;
21356 /* The common case is that this is not a constructor declarator, so
21357 try to avoid doing lots of work if at all possible. It's not
21358 valid declare a constructor at function scope. */
21359 if (parser->in_function_body)
21360 return false;
21361 /* And only certain tokens can begin a constructor declarator. */
21362 next_token = cp_lexer_peek_token (parser->lexer);
21363 if (next_token->type != CPP_NAME
21364 && next_token->type != CPP_SCOPE
21365 && next_token->type != CPP_NESTED_NAME_SPECIFIER
21366 && next_token->type != CPP_TEMPLATE_ID)
21367 return false;
21369 /* Parse tentatively; we are going to roll back all of the tokens
21370 consumed here. */
21371 cp_parser_parse_tentatively (parser);
21372 /* Assume that we are looking at a constructor declarator. */
21373 constructor_p = true;
21375 /* Look for the optional `::' operator. */
21376 cp_parser_global_scope_opt (parser,
21377 /*current_scope_valid_p=*/false);
21378 /* Look for the nested-name-specifier. */
21379 nested_name_specifier
21380 = (cp_parser_nested_name_specifier_opt (parser,
21381 /*typename_keyword_p=*/false,
21382 /*check_dependency_p=*/false,
21383 /*type_p=*/false,
21384 /*is_declaration=*/false));
21385 /* Outside of a class-specifier, there must be a
21386 nested-name-specifier. */
21387 if (!nested_name_specifier &&
21388 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
21389 || friend_p))
21390 constructor_p = false;
21391 else if (nested_name_specifier == error_mark_node)
21392 constructor_p = false;
21394 /* If we have a class scope, this is easy; DR 147 says that S::S always
21395 names the constructor, and no other qualified name could. */
21396 if (constructor_p && nested_name_specifier
21397 && CLASS_TYPE_P (nested_name_specifier))
21399 tree id = cp_parser_unqualified_id (parser,
21400 /*template_keyword_p=*/false,
21401 /*check_dependency_p=*/false,
21402 /*declarator_p=*/true,
21403 /*optional_p=*/false);
21404 if (is_overloaded_fn (id))
21405 id = DECL_NAME (get_first_fn (id));
21406 if (!constructor_name_p (id, nested_name_specifier))
21407 constructor_p = false;
21409 /* If we still think that this might be a constructor-declarator,
21410 look for a class-name. */
21411 else if (constructor_p)
21413 /* If we have:
21415 template <typename T> struct S {
21416 S();
21419 we must recognize that the nested `S' names a class. */
21420 tree type_decl;
21421 type_decl = cp_parser_class_name (parser,
21422 /*typename_keyword_p=*/false,
21423 /*template_keyword_p=*/false,
21424 none_type,
21425 /*check_dependency_p=*/false,
21426 /*class_head_p=*/false,
21427 /*is_declaration=*/false);
21428 /* If there was no class-name, then this is not a constructor. */
21429 constructor_p = !cp_parser_error_occurred (parser);
21431 /* If we're still considering a constructor, we have to see a `(',
21432 to begin the parameter-declaration-clause, followed by either a
21433 `)', an `...', or a decl-specifier. We need to check for a
21434 type-specifier to avoid being fooled into thinking that:
21436 S (f) (int);
21438 is a constructor. (It is actually a function named `f' that
21439 takes one parameter (of type `int') and returns a value of type
21440 `S'. */
21441 if (constructor_p
21442 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
21443 constructor_p = false;
21445 if (constructor_p
21446 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
21447 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
21448 /* A parameter declaration begins with a decl-specifier,
21449 which is either the "attribute" keyword, a storage class
21450 specifier, or (usually) a type-specifier. */
21451 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
21453 tree type;
21454 tree pushed_scope = NULL_TREE;
21455 unsigned saved_num_template_parameter_lists;
21457 /* Names appearing in the type-specifier should be looked up
21458 in the scope of the class. */
21459 if (current_class_type)
21460 type = NULL_TREE;
21461 else
21463 type = TREE_TYPE (type_decl);
21464 if (TREE_CODE (type) == TYPENAME_TYPE)
21466 type = resolve_typename_type (type,
21467 /*only_current_p=*/false);
21468 if (TREE_CODE (type) == TYPENAME_TYPE)
21470 cp_parser_abort_tentative_parse (parser);
21471 return false;
21474 pushed_scope = push_scope (type);
21477 /* Inside the constructor parameter list, surrounding
21478 template-parameter-lists do not apply. */
21479 saved_num_template_parameter_lists
21480 = parser->num_template_parameter_lists;
21481 parser->num_template_parameter_lists = 0;
21483 /* Look for the type-specifier. */
21484 cp_parser_type_specifier (parser,
21485 CP_PARSER_FLAGS_NONE,
21486 /*decl_specs=*/NULL,
21487 /*is_declarator=*/true,
21488 /*declares_class_or_enum=*/NULL,
21489 /*is_cv_qualifier=*/NULL);
21491 parser->num_template_parameter_lists
21492 = saved_num_template_parameter_lists;
21494 /* Leave the scope of the class. */
21495 if (pushed_scope)
21496 pop_scope (pushed_scope);
21498 constructor_p = !cp_parser_error_occurred (parser);
21502 /* We did not really want to consume any tokens. */
21503 cp_parser_abort_tentative_parse (parser);
21505 return constructor_p;
21508 /* Parse the definition of the function given by the DECL_SPECIFIERS,
21509 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
21510 they must be performed once we are in the scope of the function.
21512 Returns the function defined. */
21514 static tree
21515 cp_parser_function_definition_from_specifiers_and_declarator
21516 (cp_parser* parser,
21517 cp_decl_specifier_seq *decl_specifiers,
21518 tree attributes,
21519 const cp_declarator *declarator)
21521 tree fn;
21522 bool success_p;
21524 /* Begin the function-definition. */
21525 success_p = start_function (decl_specifiers, declarator, attributes);
21527 /* The things we're about to see are not directly qualified by any
21528 template headers we've seen thus far. */
21529 reset_specialization ();
21531 /* If there were names looked up in the decl-specifier-seq that we
21532 did not check, check them now. We must wait until we are in the
21533 scope of the function to perform the checks, since the function
21534 might be a friend. */
21535 perform_deferred_access_checks (tf_warning_or_error);
21537 if (!success_p)
21539 /* Skip the entire function. */
21540 cp_parser_skip_to_end_of_block_or_statement (parser);
21541 fn = error_mark_node;
21543 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
21545 /* Seen already, skip it. An error message has already been output. */
21546 cp_parser_skip_to_end_of_block_or_statement (parser);
21547 fn = current_function_decl;
21548 current_function_decl = NULL_TREE;
21549 /* If this is a function from a class, pop the nested class. */
21550 if (current_class_name)
21551 pop_nested_class ();
21553 else
21555 timevar_id_t tv;
21556 if (DECL_DECLARED_INLINE_P (current_function_decl))
21557 tv = TV_PARSE_INLINE;
21558 else
21559 tv = TV_PARSE_FUNC;
21560 timevar_push (tv);
21561 fn = cp_parser_function_definition_after_declarator (parser,
21562 /*inline_p=*/false);
21563 timevar_pop (tv);
21566 return fn;
21569 /* Parse the part of a function-definition that follows the
21570 declarator. INLINE_P is TRUE iff this function is an inline
21571 function defined within a class-specifier.
21573 Returns the function defined. */
21575 static tree
21576 cp_parser_function_definition_after_declarator (cp_parser* parser,
21577 bool inline_p)
21579 tree fn;
21580 bool ctor_initializer_p = false;
21581 bool saved_in_unbraced_linkage_specification_p;
21582 bool saved_in_function_body;
21583 unsigned saved_num_template_parameter_lists;
21584 cp_token *token;
21586 saved_in_function_body = parser->in_function_body;
21587 parser->in_function_body = true;
21588 /* If the next token is `return', then the code may be trying to
21589 make use of the "named return value" extension that G++ used to
21590 support. */
21591 token = cp_lexer_peek_token (parser->lexer);
21592 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21594 /* Consume the `return' keyword. */
21595 cp_lexer_consume_token (parser->lexer);
21596 /* Look for the identifier that indicates what value is to be
21597 returned. */
21598 cp_parser_identifier (parser);
21599 /* Issue an error message. */
21600 error_at (token->location,
21601 "named return values are no longer supported");
21602 /* Skip tokens until we reach the start of the function body. */
21603 while (true)
21605 cp_token *token = cp_lexer_peek_token (parser->lexer);
21606 if (token->type == CPP_OPEN_BRACE
21607 || token->type == CPP_EOF
21608 || token->type == CPP_PRAGMA_EOL)
21609 break;
21610 cp_lexer_consume_token (parser->lexer);
21613 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21614 anything declared inside `f'. */
21615 saved_in_unbraced_linkage_specification_p
21616 = parser->in_unbraced_linkage_specification_p;
21617 parser->in_unbraced_linkage_specification_p = false;
21618 /* Inside the function, surrounding template-parameter-lists do not
21619 apply. */
21620 saved_num_template_parameter_lists
21621 = parser->num_template_parameter_lists;
21622 parser->num_template_parameter_lists = 0;
21624 start_lambda_scope (current_function_decl);
21626 /* If the next token is `try', `__transaction_atomic', or
21627 `__transaction_relaxed`, then we are looking at either function-try-block
21628 or function-transaction-block. Note that all of these include the
21629 function-body. */
21630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21631 ctor_initializer_p = cp_parser_function_transaction (parser,
21632 RID_TRANSACTION_ATOMIC);
21633 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21634 RID_TRANSACTION_RELAXED))
21635 ctor_initializer_p = cp_parser_function_transaction (parser,
21636 RID_TRANSACTION_RELAXED);
21637 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21638 ctor_initializer_p = cp_parser_function_try_block (parser);
21639 else
21640 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21641 (parser, /*in_function_try_block=*/false);
21643 finish_lambda_scope ();
21645 /* Finish the function. */
21646 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21647 (inline_p ? 2 : 0));
21648 /* Generate code for it, if necessary. */
21649 expand_or_defer_fn (fn);
21650 /* Restore the saved values. */
21651 parser->in_unbraced_linkage_specification_p
21652 = saved_in_unbraced_linkage_specification_p;
21653 parser->num_template_parameter_lists
21654 = saved_num_template_parameter_lists;
21655 parser->in_function_body = saved_in_function_body;
21657 return fn;
21660 /* Parse a template-declaration, assuming that the `export' (and
21661 `extern') keywords, if present, has already been scanned. MEMBER_P
21662 is as for cp_parser_template_declaration. */
21664 static void
21665 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21667 tree decl = NULL_TREE;
21668 vec<deferred_access_check, va_gc> *checks;
21669 tree parameter_list;
21670 bool friend_p = false;
21671 bool need_lang_pop;
21672 cp_token *token;
21674 /* Look for the `template' keyword. */
21675 token = cp_lexer_peek_token (parser->lexer);
21676 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21677 return;
21679 /* And the `<'. */
21680 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21681 return;
21682 if (at_class_scope_p () && current_function_decl)
21684 /* 14.5.2.2 [temp.mem]
21686 A local class shall not have member templates. */
21687 error_at (token->location,
21688 "invalid declaration of member template in local class");
21689 cp_parser_skip_to_end_of_block_or_statement (parser);
21690 return;
21692 /* [temp]
21694 A template ... shall not have C linkage. */
21695 if (current_lang_name == lang_name_c)
21697 error_at (token->location, "template with C linkage");
21698 /* Give it C++ linkage to avoid confusing other parts of the
21699 front end. */
21700 push_lang_context (lang_name_cplusplus);
21701 need_lang_pop = true;
21703 else
21704 need_lang_pop = false;
21706 /* We cannot perform access checks on the template parameter
21707 declarations until we know what is being declared, just as we
21708 cannot check the decl-specifier list. */
21709 push_deferring_access_checks (dk_deferred);
21711 /* If the next token is `>', then we have an invalid
21712 specialization. Rather than complain about an invalid template
21713 parameter, issue an error message here. */
21714 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21716 cp_parser_error (parser, "invalid explicit specialization");
21717 begin_specialization ();
21718 parameter_list = NULL_TREE;
21720 else
21722 /* Parse the template parameters. */
21723 parameter_list = cp_parser_template_parameter_list (parser);
21726 /* Get the deferred access checks from the parameter list. These
21727 will be checked once we know what is being declared, as for a
21728 member template the checks must be performed in the scope of the
21729 class containing the member. */
21730 checks = get_deferred_access_checks ();
21732 /* Look for the `>'. */
21733 cp_parser_skip_to_end_of_template_parameter_list (parser);
21734 /* We just processed one more parameter list. */
21735 ++parser->num_template_parameter_lists;
21736 /* If the next token is `template', there are more template
21737 parameters. */
21738 if (cp_lexer_next_token_is_keyword (parser->lexer,
21739 RID_TEMPLATE))
21740 cp_parser_template_declaration_after_export (parser, member_p);
21741 else if (cxx_dialect >= cxx0x
21742 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21743 decl = cp_parser_alias_declaration (parser);
21744 else
21746 /* There are no access checks when parsing a template, as we do not
21747 know if a specialization will be a friend. */
21748 push_deferring_access_checks (dk_no_check);
21749 token = cp_lexer_peek_token (parser->lexer);
21750 decl = cp_parser_single_declaration (parser,
21751 checks,
21752 member_p,
21753 /*explicit_specialization_p=*/false,
21754 &friend_p);
21755 pop_deferring_access_checks ();
21757 /* If this is a member template declaration, let the front
21758 end know. */
21759 if (member_p && !friend_p && decl)
21761 if (TREE_CODE (decl) == TYPE_DECL)
21762 cp_parser_check_access_in_redeclaration (decl, token->location);
21764 decl = finish_member_template_decl (decl);
21766 else if (friend_p && decl
21767 && (TREE_CODE (decl) == TYPE_DECL
21768 || DECL_TYPE_TEMPLATE_P (decl)))
21769 make_friend_class (current_class_type, TREE_TYPE (decl),
21770 /*complain=*/true);
21772 /* We are done with the current parameter list. */
21773 --parser->num_template_parameter_lists;
21775 pop_deferring_access_checks ();
21777 /* Finish up. */
21778 finish_template_decl (parameter_list);
21780 /* Check the template arguments for a literal operator template. */
21781 if (decl
21782 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21783 && UDLIT_OPER_P (DECL_NAME (decl)))
21785 bool ok = true;
21786 if (parameter_list == NULL_TREE)
21787 ok = false;
21788 else
21790 int num_parms = TREE_VEC_LENGTH (parameter_list);
21791 if (num_parms != 1)
21792 ok = false;
21793 else
21795 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21796 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21797 if (TREE_TYPE (parm) != char_type_node
21798 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21799 ok = false;
21802 if (!ok)
21803 error ("literal operator template %qD has invalid parameter list."
21804 " Expected non-type template argument pack <char...>",
21805 decl);
21807 /* Register member declarations. */
21808 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21809 finish_member_declaration (decl);
21810 /* For the erroneous case of a template with C linkage, we pushed an
21811 implicit C++ linkage scope; exit that scope now. */
21812 if (need_lang_pop)
21813 pop_lang_context ();
21814 /* If DECL is a function template, we must return to parse it later.
21815 (Even though there is no definition, there might be default
21816 arguments that need handling.) */
21817 if (member_p && decl
21818 && (TREE_CODE (decl) == FUNCTION_DECL
21819 || DECL_FUNCTION_TEMPLATE_P (decl)))
21820 vec_safe_push (unparsed_funs_with_definitions, decl);
21823 /* Perform the deferred access checks from a template-parameter-list.
21824 CHECKS is a TREE_LIST of access checks, as returned by
21825 get_deferred_access_checks. */
21827 static void
21828 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
21830 ++processing_template_parmlist;
21831 perform_access_checks (checks, tf_warning_or_error);
21832 --processing_template_parmlist;
21835 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21836 `function-definition' sequence that follows a template header.
21837 If MEMBER_P is true, this declaration appears in a class scope.
21839 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21840 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21842 static tree
21843 cp_parser_single_declaration (cp_parser* parser,
21844 vec<deferred_access_check, va_gc> *checks,
21845 bool member_p,
21846 bool explicit_specialization_p,
21847 bool* friend_p)
21849 int declares_class_or_enum;
21850 tree decl = NULL_TREE;
21851 cp_decl_specifier_seq decl_specifiers;
21852 bool function_definition_p = false;
21853 cp_token *decl_spec_token_start;
21855 /* This function is only used when processing a template
21856 declaration. */
21857 gcc_assert (innermost_scope_kind () == sk_template_parms
21858 || innermost_scope_kind () == sk_template_spec);
21860 /* Defer access checks until we know what is being declared. */
21861 push_deferring_access_checks (dk_deferred);
21863 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21864 alternative. */
21865 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21866 cp_parser_decl_specifier_seq (parser,
21867 CP_PARSER_FLAGS_OPTIONAL,
21868 &decl_specifiers,
21869 &declares_class_or_enum);
21870 if (friend_p)
21871 *friend_p = cp_parser_friend_p (&decl_specifiers);
21873 /* There are no template typedefs. */
21874 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
21876 error_at (decl_spec_token_start->location,
21877 "template declaration of %<typedef%>");
21878 decl = error_mark_node;
21881 /* Gather up the access checks that occurred the
21882 decl-specifier-seq. */
21883 stop_deferring_access_checks ();
21885 /* Check for the declaration of a template class. */
21886 if (declares_class_or_enum)
21888 if (cp_parser_declares_only_class_p (parser))
21890 decl = shadow_tag (&decl_specifiers);
21892 /* In this case:
21894 struct C {
21895 friend template <typename T> struct A<T>::B;
21898 A<T>::B will be represented by a TYPENAME_TYPE, and
21899 therefore not recognized by shadow_tag. */
21900 if (friend_p && *friend_p
21901 && !decl
21902 && decl_specifiers.type
21903 && TYPE_P (decl_specifiers.type))
21904 decl = decl_specifiers.type;
21906 if (decl && decl != error_mark_node)
21907 decl = TYPE_NAME (decl);
21908 else
21909 decl = error_mark_node;
21911 /* Perform access checks for template parameters. */
21912 cp_parser_perform_template_parameter_access_checks (checks);
21916 /* Complain about missing 'typename' or other invalid type names. */
21917 if (!decl_specifiers.any_type_specifiers_p
21918 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21920 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21921 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21922 the rest of this declaration. */
21923 decl = error_mark_node;
21924 goto out;
21927 /* If it's not a template class, try for a template function. If
21928 the next token is a `;', then this declaration does not declare
21929 anything. But, if there were errors in the decl-specifiers, then
21930 the error might well have come from an attempted class-specifier.
21931 In that case, there's no need to warn about a missing declarator. */
21932 if (!decl
21933 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21934 || decl_specifiers.type != error_mark_node))
21936 decl = cp_parser_init_declarator (parser,
21937 &decl_specifiers,
21938 checks,
21939 /*function_definition_allowed_p=*/true,
21940 member_p,
21941 declares_class_or_enum,
21942 &function_definition_p,
21943 NULL);
21945 /* 7.1.1-1 [dcl.stc]
21947 A storage-class-specifier shall not be specified in an explicit
21948 specialization... */
21949 if (decl
21950 && explicit_specialization_p
21951 && decl_specifiers.storage_class != sc_none)
21953 error_at (decl_spec_token_start->location,
21954 "explicit template specialization cannot have a storage class");
21955 decl = error_mark_node;
21958 if (decl && TREE_CODE (decl) == VAR_DECL)
21959 check_template_variable (decl);
21962 /* Look for a trailing `;' after the declaration. */
21963 if (!function_definition_p
21964 && (decl == error_mark_node
21965 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21966 cp_parser_skip_to_end_of_block_or_statement (parser);
21968 out:
21969 pop_deferring_access_checks ();
21971 /* Clear any current qualification; whatever comes next is the start
21972 of something new. */
21973 parser->scope = NULL_TREE;
21974 parser->qualifying_scope = NULL_TREE;
21975 parser->object_scope = NULL_TREE;
21977 return decl;
21980 /* Parse a cast-expression that is not the operand of a unary "&". */
21982 static tree
21983 cp_parser_simple_cast_expression (cp_parser *parser)
21985 return cp_parser_cast_expression (parser, /*address_p=*/false,
21986 /*cast_p=*/false, NULL);
21989 /* Parse a functional cast to TYPE. Returns an expression
21990 representing the cast. */
21992 static tree
21993 cp_parser_functional_cast (cp_parser* parser, tree type)
21995 vec<tree, va_gc> *vec;
21996 tree expression_list;
21997 tree cast;
21998 bool nonconst_p;
22000 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22002 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22003 expression_list = cp_parser_braced_list (parser, &nonconst_p);
22004 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
22005 if (TREE_CODE (type) == TYPE_DECL)
22006 type = TREE_TYPE (type);
22007 return finish_compound_literal (type, expression_list,
22008 tf_warning_or_error);
22012 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22013 /*cast_p=*/true,
22014 /*allow_expansion_p=*/true,
22015 /*non_constant_p=*/NULL);
22016 if (vec == NULL)
22017 expression_list = error_mark_node;
22018 else
22020 expression_list = build_tree_list_vec (vec);
22021 release_tree_vector (vec);
22024 cast = build_functional_cast (type, expression_list,
22025 tf_warning_or_error);
22026 /* [expr.const]/1: In an integral constant expression "only type
22027 conversions to integral or enumeration type can be used". */
22028 if (TREE_CODE (type) == TYPE_DECL)
22029 type = TREE_TYPE (type);
22030 if (cast != error_mark_node
22031 && !cast_valid_in_integral_constant_expression_p (type)
22032 && cp_parser_non_integral_constant_expression (parser,
22033 NIC_CONSTRUCTOR))
22034 return error_mark_node;
22035 return cast;
22038 /* Save the tokens that make up the body of a member function defined
22039 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
22040 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
22041 specifiers applied to the declaration. Returns the FUNCTION_DECL
22042 for the member function. */
22044 static tree
22045 cp_parser_save_member_function_body (cp_parser* parser,
22046 cp_decl_specifier_seq *decl_specifiers,
22047 cp_declarator *declarator,
22048 tree attributes)
22050 cp_token *first;
22051 cp_token *last;
22052 tree fn;
22054 /* Create the FUNCTION_DECL. */
22055 fn = grokmethod (decl_specifiers, declarator, attributes);
22056 /* If something went badly wrong, bail out now. */
22057 if (fn == error_mark_node)
22059 /* If there's a function-body, skip it. */
22060 if (cp_parser_token_starts_function_definition_p
22061 (cp_lexer_peek_token (parser->lexer)))
22062 cp_parser_skip_to_end_of_block_or_statement (parser);
22063 return error_mark_node;
22066 /* Remember it, if there default args to post process. */
22067 cp_parser_save_default_args (parser, fn);
22069 /* Save away the tokens that make up the body of the
22070 function. */
22071 first = parser->lexer->next_token;
22072 /* We can have braced-init-list mem-initializers before the fn body. */
22073 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22075 cp_lexer_consume_token (parser->lexer);
22076 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22077 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
22079 /* cache_group will stop after an un-nested { } pair, too. */
22080 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
22081 break;
22083 /* variadic mem-inits have ... after the ')'. */
22084 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22085 cp_lexer_consume_token (parser->lexer);
22088 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22089 /* Handle function try blocks. */
22090 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
22091 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22092 last = parser->lexer->next_token;
22094 /* Save away the inline definition; we will process it when the
22095 class is complete. */
22096 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
22097 DECL_PENDING_INLINE_P (fn) = 1;
22099 /* We need to know that this was defined in the class, so that
22100 friend templates are handled correctly. */
22101 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
22103 /* Add FN to the queue of functions to be parsed later. */
22104 vec_safe_push (unparsed_funs_with_definitions, fn);
22106 return fn;
22109 /* Save the tokens that make up the in-class initializer for a non-static
22110 data member. Returns a DEFAULT_ARG. */
22112 static tree
22113 cp_parser_save_nsdmi (cp_parser* parser)
22115 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
22118 /* Parse a template-argument-list, as well as the trailing ">" (but
22119 not the opening "<"). See cp_parser_template_argument_list for the
22120 return value. */
22122 static tree
22123 cp_parser_enclosed_template_argument_list (cp_parser* parser)
22125 tree arguments;
22126 tree saved_scope;
22127 tree saved_qualifying_scope;
22128 tree saved_object_scope;
22129 bool saved_greater_than_is_operator_p;
22130 int saved_unevaluated_operand;
22131 int saved_inhibit_evaluation_warnings;
22133 /* [temp.names]
22135 When parsing a template-id, the first non-nested `>' is taken as
22136 the end of the template-argument-list rather than a greater-than
22137 operator. */
22138 saved_greater_than_is_operator_p
22139 = parser->greater_than_is_operator_p;
22140 parser->greater_than_is_operator_p = false;
22141 /* Parsing the argument list may modify SCOPE, so we save it
22142 here. */
22143 saved_scope = parser->scope;
22144 saved_qualifying_scope = parser->qualifying_scope;
22145 saved_object_scope = parser->object_scope;
22146 /* We need to evaluate the template arguments, even though this
22147 template-id may be nested within a "sizeof". */
22148 saved_unevaluated_operand = cp_unevaluated_operand;
22149 cp_unevaluated_operand = 0;
22150 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22151 c_inhibit_evaluation_warnings = 0;
22152 /* Parse the template-argument-list itself. */
22153 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
22154 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22155 arguments = NULL_TREE;
22156 else
22157 arguments = cp_parser_template_argument_list (parser);
22158 /* Look for the `>' that ends the template-argument-list. If we find
22159 a '>>' instead, it's probably just a typo. */
22160 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
22162 if (cxx_dialect != cxx98)
22164 /* In C++0x, a `>>' in a template argument list or cast
22165 expression is considered to be two separate `>'
22166 tokens. So, change the current token to a `>', but don't
22167 consume it: it will be consumed later when the outer
22168 template argument list (or cast expression) is parsed.
22169 Note that this replacement of `>' for `>>' is necessary
22170 even if we are parsing tentatively: in the tentative
22171 case, after calling
22172 cp_parser_enclosed_template_argument_list we will always
22173 throw away all of the template arguments and the first
22174 closing `>', either because the template argument list
22175 was erroneous or because we are replacing those tokens
22176 with a CPP_TEMPLATE_ID token. The second `>' (which will
22177 not have been thrown away) is needed either to close an
22178 outer template argument list or to complete a new-style
22179 cast. */
22180 cp_token *token = cp_lexer_peek_token (parser->lexer);
22181 token->type = CPP_GREATER;
22183 else if (!saved_greater_than_is_operator_p)
22185 /* If we're in a nested template argument list, the '>>' has
22186 to be a typo for '> >'. We emit the error message, but we
22187 continue parsing and we push a '>' as next token, so that
22188 the argument list will be parsed correctly. Note that the
22189 global source location is still on the token before the
22190 '>>', so we need to say explicitly where we want it. */
22191 cp_token *token = cp_lexer_peek_token (parser->lexer);
22192 error_at (token->location, "%<>>%> should be %<> >%> "
22193 "within a nested template argument list");
22195 token->type = CPP_GREATER;
22197 else
22199 /* If this is not a nested template argument list, the '>>'
22200 is a typo for '>'. Emit an error message and continue.
22201 Same deal about the token location, but here we can get it
22202 right by consuming the '>>' before issuing the diagnostic. */
22203 cp_token *token = cp_lexer_consume_token (parser->lexer);
22204 error_at (token->location,
22205 "spurious %<>>%>, use %<>%> to terminate "
22206 "a template argument list");
22209 else
22210 cp_parser_skip_to_end_of_template_parameter_list (parser);
22211 /* The `>' token might be a greater-than operator again now. */
22212 parser->greater_than_is_operator_p
22213 = saved_greater_than_is_operator_p;
22214 /* Restore the SAVED_SCOPE. */
22215 parser->scope = saved_scope;
22216 parser->qualifying_scope = saved_qualifying_scope;
22217 parser->object_scope = saved_object_scope;
22218 cp_unevaluated_operand = saved_unevaluated_operand;
22219 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22221 return arguments;
22224 /* MEMBER_FUNCTION is a member function, or a friend. If default
22225 arguments, or the body of the function have not yet been parsed,
22226 parse them now. */
22228 static void
22229 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
22231 timevar_push (TV_PARSE_INMETH);
22232 /* If this member is a template, get the underlying
22233 FUNCTION_DECL. */
22234 if (DECL_FUNCTION_TEMPLATE_P (member_function))
22235 member_function = DECL_TEMPLATE_RESULT (member_function);
22237 /* There should not be any class definitions in progress at this
22238 point; the bodies of members are only parsed outside of all class
22239 definitions. */
22240 gcc_assert (parser->num_classes_being_defined == 0);
22241 /* While we're parsing the member functions we might encounter more
22242 classes. We want to handle them right away, but we don't want
22243 them getting mixed up with functions that are currently in the
22244 queue. */
22245 push_unparsed_function_queues (parser);
22247 /* Make sure that any template parameters are in scope. */
22248 maybe_begin_member_template_processing (member_function);
22250 /* If the body of the function has not yet been parsed, parse it
22251 now. */
22252 if (DECL_PENDING_INLINE_P (member_function))
22254 tree function_scope;
22255 cp_token_cache *tokens;
22257 /* The function is no longer pending; we are processing it. */
22258 tokens = DECL_PENDING_INLINE_INFO (member_function);
22259 DECL_PENDING_INLINE_INFO (member_function) = NULL;
22260 DECL_PENDING_INLINE_P (member_function) = 0;
22262 /* If this is a local class, enter the scope of the containing
22263 function. */
22264 function_scope = current_function_decl;
22265 if (function_scope)
22266 push_function_context ();
22268 /* Push the body of the function onto the lexer stack. */
22269 cp_parser_push_lexer_for_tokens (parser, tokens);
22271 /* Let the front end know that we going to be defining this
22272 function. */
22273 start_preparsed_function (member_function, NULL_TREE,
22274 SF_PRE_PARSED | SF_INCLASS_INLINE);
22276 /* Don't do access checking if it is a templated function. */
22277 if (processing_template_decl)
22278 push_deferring_access_checks (dk_no_check);
22280 /* Now, parse the body of the function. */
22281 cp_parser_function_definition_after_declarator (parser,
22282 /*inline_p=*/true);
22284 if (processing_template_decl)
22285 pop_deferring_access_checks ();
22287 /* Leave the scope of the containing function. */
22288 if (function_scope)
22289 pop_function_context ();
22290 cp_parser_pop_lexer (parser);
22293 /* Remove any template parameters from the symbol table. */
22294 maybe_end_member_template_processing ();
22296 /* Restore the queue. */
22297 pop_unparsed_function_queues (parser);
22298 timevar_pop (TV_PARSE_INMETH);
22301 /* If DECL contains any default args, remember it on the unparsed
22302 functions queue. */
22304 static void
22305 cp_parser_save_default_args (cp_parser* parser, tree decl)
22307 tree probe;
22309 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
22310 probe;
22311 probe = TREE_CHAIN (probe))
22312 if (TREE_PURPOSE (probe))
22314 cp_default_arg_entry entry = {current_class_type, decl};
22315 vec_safe_push (unparsed_funs_with_default_args, entry);
22316 break;
22320 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
22321 which is either a FIELD_DECL or PARM_DECL. Parse it and return
22322 the result. For a PARM_DECL, PARMTYPE is the corresponding type
22323 from the parameter-type-list. */
22325 static tree
22326 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
22327 tree default_arg, tree parmtype)
22329 cp_token_cache *tokens;
22330 tree parsed_arg;
22331 bool dummy;
22333 if (default_arg == error_mark_node)
22334 return error_mark_node;
22336 /* Push the saved tokens for the default argument onto the parser's
22337 lexer stack. */
22338 tokens = DEFARG_TOKENS (default_arg);
22339 cp_parser_push_lexer_for_tokens (parser, tokens);
22341 start_lambda_scope (decl);
22343 /* Parse the default argument. */
22344 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
22345 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
22346 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22348 finish_lambda_scope ();
22350 if (parsed_arg == error_mark_node)
22351 cp_parser_skip_to_end_of_statement (parser);
22353 if (!processing_template_decl)
22355 /* In a non-template class, check conversions now. In a template,
22356 we'll wait and instantiate these as needed. */
22357 if (TREE_CODE (decl) == PARM_DECL)
22358 parsed_arg = check_default_argument (parmtype, parsed_arg);
22359 else
22361 int flags = LOOKUP_IMPLICIT;
22362 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
22363 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
22364 flags = LOOKUP_NORMAL;
22365 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
22369 /* If the token stream has not been completely used up, then
22370 there was extra junk after the end of the default
22371 argument. */
22372 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22374 if (TREE_CODE (decl) == PARM_DECL)
22375 cp_parser_error (parser, "expected %<,%>");
22376 else
22377 cp_parser_error (parser, "expected %<;%>");
22380 /* Revert to the main lexer. */
22381 cp_parser_pop_lexer (parser);
22383 return parsed_arg;
22386 /* FIELD is a non-static data member with an initializer which we saved for
22387 later; parse it now. */
22389 static void
22390 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
22392 tree def;
22394 push_unparsed_function_queues (parser);
22395 def = cp_parser_late_parse_one_default_arg (parser, field,
22396 DECL_INITIAL (field),
22397 NULL_TREE);
22398 pop_unparsed_function_queues (parser);
22400 DECL_INITIAL (field) = def;
22403 /* FN is a FUNCTION_DECL which may contains a parameter with an
22404 unparsed DEFAULT_ARG. Parse the default args now. This function
22405 assumes that the current scope is the scope in which the default
22406 argument should be processed. */
22408 static void
22409 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
22411 bool saved_local_variables_forbidden_p;
22412 tree parm, parmdecl;
22414 /* While we're parsing the default args, we might (due to the
22415 statement expression extension) encounter more classes. We want
22416 to handle them right away, but we don't want them getting mixed
22417 up with default args that are currently in the queue. */
22418 push_unparsed_function_queues (parser);
22420 /* Local variable names (and the `this' keyword) may not appear
22421 in a default argument. */
22422 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22423 parser->local_variables_forbidden_p = true;
22425 push_defarg_context (fn);
22427 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
22428 parmdecl = DECL_ARGUMENTS (fn);
22429 parm && parm != void_list_node;
22430 parm = TREE_CHAIN (parm),
22431 parmdecl = DECL_CHAIN (parmdecl))
22433 tree default_arg = TREE_PURPOSE (parm);
22434 tree parsed_arg;
22435 vec<tree, va_gc> *insts;
22436 tree copy;
22437 unsigned ix;
22439 if (!default_arg)
22440 continue;
22442 if (TREE_CODE (default_arg) != DEFAULT_ARG)
22443 /* This can happen for a friend declaration for a function
22444 already declared with default arguments. */
22445 continue;
22447 parsed_arg
22448 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
22449 default_arg,
22450 TREE_VALUE (parm));
22451 if (parsed_arg == error_mark_node)
22453 continue;
22456 TREE_PURPOSE (parm) = parsed_arg;
22458 /* Update any instantiations we've already created. */
22459 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
22460 vec_safe_iterate (insts, ix, &copy); ix++)
22461 TREE_PURPOSE (copy) = parsed_arg;
22464 pop_defarg_context ();
22466 /* Make sure no default arg is missing. */
22467 check_default_args (fn);
22469 /* Restore the state of local_variables_forbidden_p. */
22470 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22472 /* Restore the queue. */
22473 pop_unparsed_function_queues (parser);
22476 /* Parse the operand of `sizeof' (or a similar operator). Returns
22477 either a TYPE or an expression, depending on the form of the
22478 input. The KEYWORD indicates which kind of expression we have
22479 encountered. */
22481 static tree
22482 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
22484 tree expr = NULL_TREE;
22485 const char *saved_message;
22486 char *tmp;
22487 bool saved_integral_constant_expression_p;
22488 bool saved_non_integral_constant_expression_p;
22489 bool pack_expansion_p = false;
22491 /* Types cannot be defined in a `sizeof' expression. Save away the
22492 old message. */
22493 saved_message = parser->type_definition_forbidden_message;
22494 /* And create the new one. */
22495 tmp = concat ("types may not be defined in %<",
22496 IDENTIFIER_POINTER (ridpointers[keyword]),
22497 "%> expressions", NULL);
22498 parser->type_definition_forbidden_message = tmp;
22500 /* The restrictions on constant-expressions do not apply inside
22501 sizeof expressions. */
22502 saved_integral_constant_expression_p
22503 = parser->integral_constant_expression_p;
22504 saved_non_integral_constant_expression_p
22505 = parser->non_integral_constant_expression_p;
22506 parser->integral_constant_expression_p = false;
22508 /* If it's a `...', then we are computing the length of a parameter
22509 pack. */
22510 if (keyword == RID_SIZEOF
22511 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22513 /* Consume the `...'. */
22514 cp_lexer_consume_token (parser->lexer);
22515 maybe_warn_variadic_templates ();
22517 /* Note that this is an expansion. */
22518 pack_expansion_p = true;
22521 /* Do not actually evaluate the expression. */
22522 ++cp_unevaluated_operand;
22523 ++c_inhibit_evaluation_warnings;
22524 /* If it's a `(', then we might be looking at the type-id
22525 construction. */
22526 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22528 tree type;
22529 bool saved_in_type_id_in_expr_p;
22531 /* We can't be sure yet whether we're looking at a type-id or an
22532 expression. */
22533 cp_parser_parse_tentatively (parser);
22534 /* Consume the `('. */
22535 cp_lexer_consume_token (parser->lexer);
22536 /* Parse the type-id. */
22537 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
22538 parser->in_type_id_in_expr_p = true;
22539 type = cp_parser_type_id (parser);
22540 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
22541 /* Now, look for the trailing `)'. */
22542 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22543 /* If all went well, then we're done. */
22544 if (cp_parser_parse_definitely (parser))
22546 cp_decl_specifier_seq decl_specs;
22548 /* Build a trivial decl-specifier-seq. */
22549 clear_decl_specs (&decl_specs);
22550 decl_specs.type = type;
22552 /* Call grokdeclarator to figure out what type this is. */
22553 expr = grokdeclarator (NULL,
22554 &decl_specs,
22555 TYPENAME,
22556 /*initialized=*/0,
22557 /*attrlist=*/NULL);
22560 else if (pack_expansion_p)
22561 permerror (cp_lexer_peek_token (parser->lexer)->location,
22562 "%<sizeof...%> argument must be surrounded by parentheses");
22564 /* If the type-id production did not work out, then we must be
22565 looking at the unary-expression production. */
22566 if (!expr)
22567 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
22568 /*cast_p=*/false, NULL);
22570 if (pack_expansion_p)
22571 /* Build a pack expansion. */
22572 expr = make_pack_expansion (expr);
22574 /* Go back to evaluating expressions. */
22575 --cp_unevaluated_operand;
22576 --c_inhibit_evaluation_warnings;
22578 /* Free the message we created. */
22579 free (tmp);
22580 /* And restore the old one. */
22581 parser->type_definition_forbidden_message = saved_message;
22582 parser->integral_constant_expression_p
22583 = saved_integral_constant_expression_p;
22584 parser->non_integral_constant_expression_p
22585 = saved_non_integral_constant_expression_p;
22587 return expr;
22590 /* If the current declaration has no declarator, return true. */
22592 static bool
22593 cp_parser_declares_only_class_p (cp_parser *parser)
22595 /* If the next token is a `;' or a `,' then there is no
22596 declarator. */
22597 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22598 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22601 /* Update the DECL_SPECS to reflect the storage class indicated by
22602 KEYWORD. */
22604 static void
22605 cp_parser_set_storage_class (cp_parser *parser,
22606 cp_decl_specifier_seq *decl_specs,
22607 enum rid keyword,
22608 cp_token *token)
22610 cp_storage_class storage_class;
22612 if (parser->in_unbraced_linkage_specification_p)
22614 error_at (token->location, "invalid use of %qD in linkage specification",
22615 ridpointers[keyword]);
22616 return;
22618 else if (decl_specs->storage_class != sc_none)
22620 decl_specs->conflicting_specifiers_p = true;
22621 return;
22624 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22625 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
22626 && decl_specs->gnu_thread_keyword_p)
22628 pedwarn (decl_specs->locations[ds_thread], 0,
22629 "%<__thread%> before %qD", ridpointers[keyword]);
22632 switch (keyword)
22634 case RID_AUTO:
22635 storage_class = sc_auto;
22636 break;
22637 case RID_REGISTER:
22638 storage_class = sc_register;
22639 break;
22640 case RID_STATIC:
22641 storage_class = sc_static;
22642 break;
22643 case RID_EXTERN:
22644 storage_class = sc_extern;
22645 break;
22646 case RID_MUTABLE:
22647 storage_class = sc_mutable;
22648 break;
22649 default:
22650 gcc_unreachable ();
22652 decl_specs->storage_class = storage_class;
22653 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
22655 /* A storage class specifier cannot be applied alongside a typedef
22656 specifier. If there is a typedef specifier present then set
22657 conflicting_specifiers_p which will trigger an error later
22658 on in grokdeclarator. */
22659 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
22660 decl_specs->conflicting_specifiers_p = true;
22663 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22664 is true, the type is a class or enum definition. */
22666 static void
22667 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22668 tree type_spec,
22669 cp_token *token,
22670 bool type_definition_p)
22672 decl_specs->any_specifiers_p = true;
22674 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22675 (with, for example, in "typedef int wchar_t;") we remember that
22676 this is what happened. In system headers, we ignore these
22677 declarations so that G++ can work with system headers that are not
22678 C++-safe. */
22679 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
22680 && !type_definition_p
22681 && (type_spec == boolean_type_node
22682 || type_spec == char16_type_node
22683 || type_spec == char32_type_node
22684 || type_spec == wchar_type_node)
22685 && (decl_specs->type
22686 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
22687 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
22688 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
22689 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
22691 decl_specs->redefined_builtin_type = type_spec;
22692 set_and_check_decl_spec_loc (decl_specs,
22693 ds_redefined_builtin_type_spec,
22694 token);
22695 if (!decl_specs->type)
22697 decl_specs->type = type_spec;
22698 decl_specs->type_definition_p = false;
22699 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
22702 else if (decl_specs->type)
22703 decl_specs->multiple_types_p = true;
22704 else
22706 decl_specs->type = type_spec;
22707 decl_specs->type_definition_p = type_definition_p;
22708 decl_specs->redefined_builtin_type = NULL_TREE;
22709 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
22713 /* True iff TOKEN is the GNU keyword __thread. */
22715 static bool
22716 token_is__thread (cp_token *token)
22718 gcc_assert (token->keyword == RID_THREAD);
22719 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
22722 /* Set the location for a declarator specifier and check if it is
22723 duplicated.
22725 DECL_SPECS is the sequence of declarator specifiers onto which to
22726 set the location.
22728 DS is the single declarator specifier to set which location is to
22729 be set onto the existing sequence of declarators.
22731 LOCATION is the location for the declarator specifier to
22732 consider. */
22734 static void
22735 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
22736 cp_decl_spec ds, cp_token *token)
22738 gcc_assert (ds < ds_last);
22740 if (decl_specs == NULL)
22741 return;
22743 source_location location = token->location;
22745 if (decl_specs->locations[ds] == 0)
22747 decl_specs->locations[ds] = location;
22748 if (ds == ds_thread)
22749 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
22751 else
22753 if (ds == ds_long)
22755 if (decl_specs->locations[ds_long_long] != 0)
22756 error_at (location,
22757 "%<long long long%> is too long for GCC");
22758 else
22760 decl_specs->locations[ds_long_long] = location;
22761 pedwarn_cxx98 (location,
22762 OPT_Wlong_long,
22763 "ISO C++ 1998 does not support %<long long%>");
22766 else if (ds == ds_thread)
22768 bool gnu = token_is__thread (token);
22769 if (gnu != decl_specs->gnu_thread_keyword_p)
22770 error_at (location,
22771 "both %<__thread%> and %<thread_local%> specified");
22772 else
22773 error_at (location, "duplicate %qD", token->u.value);
22775 else
22777 static const char *const decl_spec_names[] = {
22778 "signed",
22779 "unsigned",
22780 "short",
22781 "long",
22782 "const",
22783 "volatile",
22784 "restrict",
22785 "inline",
22786 "virtual",
22787 "explicit",
22788 "friend",
22789 "typedef",
22790 "using",
22791 "constexpr",
22792 "__complex"
22794 error_at (location,
22795 "duplicate %qs", decl_spec_names[ds]);
22800 /* Return true iff the declarator specifier DS is present in the
22801 sequence of declarator specifiers DECL_SPECS. */
22803 bool
22804 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
22805 cp_decl_spec ds)
22807 gcc_assert (ds < ds_last);
22809 if (decl_specs == NULL)
22810 return false;
22812 return decl_specs->locations[ds] != 0;
22815 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22816 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22818 static bool
22819 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22821 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
22824 /* Issue an error message indicating that TOKEN_DESC was expected.
22825 If KEYWORD is true, it indicated this function is called by
22826 cp_parser_require_keword and the required token can only be
22827 a indicated keyword. */
22829 static void
22830 cp_parser_required_error (cp_parser *parser,
22831 required_token token_desc,
22832 bool keyword)
22834 switch (token_desc)
22836 case RT_NEW:
22837 cp_parser_error (parser, "expected %<new%>");
22838 return;
22839 case RT_DELETE:
22840 cp_parser_error (parser, "expected %<delete%>");
22841 return;
22842 case RT_RETURN:
22843 cp_parser_error (parser, "expected %<return%>");
22844 return;
22845 case RT_WHILE:
22846 cp_parser_error (parser, "expected %<while%>");
22847 return;
22848 case RT_EXTERN:
22849 cp_parser_error (parser, "expected %<extern%>");
22850 return;
22851 case RT_STATIC_ASSERT:
22852 cp_parser_error (parser, "expected %<static_assert%>");
22853 return;
22854 case RT_DECLTYPE:
22855 cp_parser_error (parser, "expected %<decltype%>");
22856 return;
22857 case RT_OPERATOR:
22858 cp_parser_error (parser, "expected %<operator%>");
22859 return;
22860 case RT_CLASS:
22861 cp_parser_error (parser, "expected %<class%>");
22862 return;
22863 case RT_TEMPLATE:
22864 cp_parser_error (parser, "expected %<template%>");
22865 return;
22866 case RT_NAMESPACE:
22867 cp_parser_error (parser, "expected %<namespace%>");
22868 return;
22869 case RT_USING:
22870 cp_parser_error (parser, "expected %<using%>");
22871 return;
22872 case RT_ASM:
22873 cp_parser_error (parser, "expected %<asm%>");
22874 return;
22875 case RT_TRY:
22876 cp_parser_error (parser, "expected %<try%>");
22877 return;
22878 case RT_CATCH:
22879 cp_parser_error (parser, "expected %<catch%>");
22880 return;
22881 case RT_THROW:
22882 cp_parser_error (parser, "expected %<throw%>");
22883 return;
22884 case RT_LABEL:
22885 cp_parser_error (parser, "expected %<__label__%>");
22886 return;
22887 case RT_AT_TRY:
22888 cp_parser_error (parser, "expected %<@try%>");
22889 return;
22890 case RT_AT_SYNCHRONIZED:
22891 cp_parser_error (parser, "expected %<@synchronized%>");
22892 return;
22893 case RT_AT_THROW:
22894 cp_parser_error (parser, "expected %<@throw%>");
22895 return;
22896 case RT_TRANSACTION_ATOMIC:
22897 cp_parser_error (parser, "expected %<__transaction_atomic%>");
22898 return;
22899 case RT_TRANSACTION_RELAXED:
22900 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22901 return;
22902 default:
22903 break;
22905 if (!keyword)
22907 switch (token_desc)
22909 case RT_SEMICOLON:
22910 cp_parser_error (parser, "expected %<;%>");
22911 return;
22912 case RT_OPEN_PAREN:
22913 cp_parser_error (parser, "expected %<(%>");
22914 return;
22915 case RT_CLOSE_BRACE:
22916 cp_parser_error (parser, "expected %<}%>");
22917 return;
22918 case RT_OPEN_BRACE:
22919 cp_parser_error (parser, "expected %<{%>");
22920 return;
22921 case RT_CLOSE_SQUARE:
22922 cp_parser_error (parser, "expected %<]%>");
22923 return;
22924 case RT_OPEN_SQUARE:
22925 cp_parser_error (parser, "expected %<[%>");
22926 return;
22927 case RT_COMMA:
22928 cp_parser_error (parser, "expected %<,%>");
22929 return;
22930 case RT_SCOPE:
22931 cp_parser_error (parser, "expected %<::%>");
22932 return;
22933 case RT_LESS:
22934 cp_parser_error (parser, "expected %<<%>");
22935 return;
22936 case RT_GREATER:
22937 cp_parser_error (parser, "expected %<>%>");
22938 return;
22939 case RT_EQ:
22940 cp_parser_error (parser, "expected %<=%>");
22941 return;
22942 case RT_ELLIPSIS:
22943 cp_parser_error (parser, "expected %<...%>");
22944 return;
22945 case RT_MULT:
22946 cp_parser_error (parser, "expected %<*%>");
22947 return;
22948 case RT_COMPL:
22949 cp_parser_error (parser, "expected %<~%>");
22950 return;
22951 case RT_COLON:
22952 cp_parser_error (parser, "expected %<:%>");
22953 return;
22954 case RT_COLON_SCOPE:
22955 cp_parser_error (parser, "expected %<:%> or %<::%>");
22956 return;
22957 case RT_CLOSE_PAREN:
22958 cp_parser_error (parser, "expected %<)%>");
22959 return;
22960 case RT_COMMA_CLOSE_PAREN:
22961 cp_parser_error (parser, "expected %<,%> or %<)%>");
22962 return;
22963 case RT_PRAGMA_EOL:
22964 cp_parser_error (parser, "expected end of line");
22965 return;
22966 case RT_NAME:
22967 cp_parser_error (parser, "expected identifier");
22968 return;
22969 case RT_SELECT:
22970 cp_parser_error (parser, "expected selection-statement");
22971 return;
22972 case RT_INTERATION:
22973 cp_parser_error (parser, "expected iteration-statement");
22974 return;
22975 case RT_JUMP:
22976 cp_parser_error (parser, "expected jump-statement");
22977 return;
22978 case RT_CLASS_KEY:
22979 cp_parser_error (parser, "expected class-key");
22980 return;
22981 case RT_CLASS_TYPENAME_TEMPLATE:
22982 cp_parser_error (parser,
22983 "expected %<class%>, %<typename%>, or %<template%>");
22984 return;
22985 default:
22986 gcc_unreachable ();
22989 else
22990 gcc_unreachable ();
22995 /* If the next token is of the indicated TYPE, consume it. Otherwise,
22996 issue an error message indicating that TOKEN_DESC was expected.
22998 Returns the token consumed, if the token had the appropriate type.
22999 Otherwise, returns NULL. */
23001 static cp_token *
23002 cp_parser_require (cp_parser* parser,
23003 enum cpp_ttype type,
23004 required_token token_desc)
23006 if (cp_lexer_next_token_is (parser->lexer, type))
23007 return cp_lexer_consume_token (parser->lexer);
23008 else
23010 /* Output the MESSAGE -- unless we're parsing tentatively. */
23011 if (!cp_parser_simulate_error (parser))
23012 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
23013 return NULL;
23017 /* An error message is produced if the next token is not '>'.
23018 All further tokens are skipped until the desired token is
23019 found or '{', '}', ';' or an unbalanced ')' or ']'. */
23021 static void
23022 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
23024 /* Current level of '< ... >'. */
23025 unsigned level = 0;
23026 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
23027 unsigned nesting_depth = 0;
23029 /* Are we ready, yet? If not, issue error message. */
23030 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
23031 return;
23033 /* Skip tokens until the desired token is found. */
23034 while (true)
23036 /* Peek at the next token. */
23037 switch (cp_lexer_peek_token (parser->lexer)->type)
23039 case CPP_LESS:
23040 if (!nesting_depth)
23041 ++level;
23042 break;
23044 case CPP_RSHIFT:
23045 if (cxx_dialect == cxx98)
23046 /* C++0x views the `>>' operator as two `>' tokens, but
23047 C++98 does not. */
23048 break;
23049 else if (!nesting_depth && level-- == 0)
23051 /* We've hit a `>>' where the first `>' closes the
23052 template argument list, and the second `>' is
23053 spurious. Just consume the `>>' and stop; we've
23054 already produced at least one error. */
23055 cp_lexer_consume_token (parser->lexer);
23056 return;
23058 /* Fall through for C++0x, so we handle the second `>' in
23059 the `>>'. */
23061 case CPP_GREATER:
23062 if (!nesting_depth && level-- == 0)
23064 /* We've reached the token we want, consume it and stop. */
23065 cp_lexer_consume_token (parser->lexer);
23066 return;
23068 break;
23070 case CPP_OPEN_PAREN:
23071 case CPP_OPEN_SQUARE:
23072 ++nesting_depth;
23073 break;
23075 case CPP_CLOSE_PAREN:
23076 case CPP_CLOSE_SQUARE:
23077 if (nesting_depth-- == 0)
23078 return;
23079 break;
23081 case CPP_EOF:
23082 case CPP_PRAGMA_EOL:
23083 case CPP_SEMICOLON:
23084 case CPP_OPEN_BRACE:
23085 case CPP_CLOSE_BRACE:
23086 /* The '>' was probably forgotten, don't look further. */
23087 return;
23089 default:
23090 break;
23093 /* Consume this token. */
23094 cp_lexer_consume_token (parser->lexer);
23098 /* If the next token is the indicated keyword, consume it. Otherwise,
23099 issue an error message indicating that TOKEN_DESC was expected.
23101 Returns the token consumed, if the token had the appropriate type.
23102 Otherwise, returns NULL. */
23104 static cp_token *
23105 cp_parser_require_keyword (cp_parser* parser,
23106 enum rid keyword,
23107 required_token token_desc)
23109 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
23111 if (token && token->keyword != keyword)
23113 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
23114 return NULL;
23117 return token;
23120 /* Returns TRUE iff TOKEN is a token that can begin the body of a
23121 function-definition. */
23123 static bool
23124 cp_parser_token_starts_function_definition_p (cp_token* token)
23126 return (/* An ordinary function-body begins with an `{'. */
23127 token->type == CPP_OPEN_BRACE
23128 /* A ctor-initializer begins with a `:'. */
23129 || token->type == CPP_COLON
23130 /* A function-try-block begins with `try'. */
23131 || token->keyword == RID_TRY
23132 /* A function-transaction-block begins with `__transaction_atomic'
23133 or `__transaction_relaxed'. */
23134 || token->keyword == RID_TRANSACTION_ATOMIC
23135 || token->keyword == RID_TRANSACTION_RELAXED
23136 /* The named return value extension begins with `return'. */
23137 || token->keyword == RID_RETURN);
23140 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
23141 definition. */
23143 static bool
23144 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
23146 cp_token *token;
23148 token = cp_lexer_peek_token (parser->lexer);
23149 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
23152 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
23153 C++0x) ending a template-argument. */
23155 static bool
23156 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
23158 cp_token *token;
23160 token = cp_lexer_peek_token (parser->lexer);
23161 return (token->type == CPP_COMMA
23162 || token->type == CPP_GREATER
23163 || token->type == CPP_ELLIPSIS
23164 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
23167 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
23168 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
23170 static bool
23171 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
23172 size_t n)
23174 cp_token *token;
23176 token = cp_lexer_peek_nth_token (parser->lexer, n);
23177 if (token->type == CPP_LESS)
23178 return true;
23179 /* Check for the sequence `<::' in the original code. It would be lexed as
23180 `[:', where `[' is a digraph, and there is no whitespace before
23181 `:'. */
23182 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
23184 cp_token *token2;
23185 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
23186 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
23187 return true;
23189 return false;
23192 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
23193 or none_type otherwise. */
23195 static enum tag_types
23196 cp_parser_token_is_class_key (cp_token* token)
23198 switch (token->keyword)
23200 case RID_CLASS:
23201 return class_type;
23202 case RID_STRUCT:
23203 return record_type;
23204 case RID_UNION:
23205 return union_type;
23207 default:
23208 return none_type;
23212 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
23214 static void
23215 cp_parser_check_class_key (enum tag_types class_key, tree type)
23217 if (type == error_mark_node)
23218 return;
23219 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
23221 permerror (input_location, "%qs tag used in naming %q#T",
23222 class_key == union_type ? "union"
23223 : class_key == record_type ? "struct" : "class",
23224 type);
23225 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
23226 "%q#T was previously declared here", type);
23230 /* Issue an error message if DECL is redeclared with different
23231 access than its original declaration [class.access.spec/3].
23232 This applies to nested classes and nested class templates.
23233 [class.mem/1]. */
23235 static void
23236 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
23238 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
23239 return;
23241 if ((TREE_PRIVATE (decl)
23242 != (current_access_specifier == access_private_node))
23243 || (TREE_PROTECTED (decl)
23244 != (current_access_specifier == access_protected_node)))
23245 error_at (location, "%qD redeclared with different access", decl);
23248 /* Look for the `template' keyword, as a syntactic disambiguator.
23249 Return TRUE iff it is present, in which case it will be
23250 consumed. */
23252 static bool
23253 cp_parser_optional_template_keyword (cp_parser *parser)
23255 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23257 /* In C++98 the `template' keyword can only be used within templates;
23258 outside templates the parser can always figure out what is a
23259 template and what is not. In C++11, per the resolution of DR 468,
23260 `template' is allowed in cases where it is not strictly necessary. */
23261 if (!processing_template_decl
23262 && pedantic && cxx_dialect == cxx98)
23264 cp_token *token = cp_lexer_peek_token (parser->lexer);
23265 pedwarn (token->location, OPT_Wpedantic,
23266 "in C++98 %<template%> (as a disambiguator) is only "
23267 "allowed within templates");
23268 /* If this part of the token stream is rescanned, the same
23269 error message would be generated. So, we purge the token
23270 from the stream. */
23271 cp_lexer_purge_token (parser->lexer);
23272 return false;
23274 else
23276 /* Consume the `template' keyword. */
23277 cp_lexer_consume_token (parser->lexer);
23278 return true;
23281 return false;
23284 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
23285 set PARSER->SCOPE, and perform other related actions. */
23287 static void
23288 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
23290 int i;
23291 struct tree_check *check_value;
23292 deferred_access_check *chk;
23293 vec<deferred_access_check, va_gc> *checks;
23295 /* Get the stored value. */
23296 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
23297 /* Perform any access checks that were deferred. */
23298 checks = check_value->checks;
23299 if (checks)
23301 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
23302 perform_or_defer_access_check (chk->binfo,
23303 chk->decl,
23304 chk->diag_decl, tf_warning_or_error);
23306 /* Set the scope from the stored value. */
23307 parser->scope = check_value->value;
23308 parser->qualifying_scope = check_value->qualifying_scope;
23309 parser->object_scope = NULL_TREE;
23312 /* Consume tokens up through a non-nested END token. Returns TRUE if we
23313 encounter the end of a block before what we were looking for. */
23315 static bool
23316 cp_parser_cache_group (cp_parser *parser,
23317 enum cpp_ttype end,
23318 unsigned depth)
23320 while (true)
23322 cp_token *token = cp_lexer_peek_token (parser->lexer);
23324 /* Abort a parenthesized expression if we encounter a semicolon. */
23325 if ((end == CPP_CLOSE_PAREN || depth == 0)
23326 && token->type == CPP_SEMICOLON)
23327 return true;
23328 /* If we've reached the end of the file, stop. */
23329 if (token->type == CPP_EOF
23330 || (end != CPP_PRAGMA_EOL
23331 && token->type == CPP_PRAGMA_EOL))
23332 return true;
23333 if (token->type == CPP_CLOSE_BRACE && depth == 0)
23334 /* We've hit the end of an enclosing block, so there's been some
23335 kind of syntax error. */
23336 return true;
23338 /* Consume the token. */
23339 cp_lexer_consume_token (parser->lexer);
23340 /* See if it starts a new group. */
23341 if (token->type == CPP_OPEN_BRACE)
23343 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
23344 /* In theory this should probably check end == '}', but
23345 cp_parser_save_member_function_body needs it to exit
23346 after either '}' or ')' when called with ')'. */
23347 if (depth == 0)
23348 return false;
23350 else if (token->type == CPP_OPEN_PAREN)
23352 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
23353 if (depth == 0 && end == CPP_CLOSE_PAREN)
23354 return false;
23356 else if (token->type == CPP_PRAGMA)
23357 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
23358 else if (token->type == end)
23359 return false;
23363 /* Like above, for caching a default argument or NSDMI. Both of these are
23364 terminated by a non-nested comma, but it can be unclear whether or not a
23365 comma is nested in a template argument list unless we do more parsing.
23366 In order to handle this ambiguity, when we encounter a ',' after a '<'
23367 we try to parse what follows as a parameter-declaration-list (in the
23368 case of a default argument) or a member-declarator (in the case of an
23369 NSDMI). If that succeeds, then we stop caching. */
23371 static tree
23372 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
23374 unsigned depth = 0;
23375 int maybe_template_id = 0;
23376 cp_token *first_token;
23377 cp_token *token;
23378 tree default_argument;
23380 /* Add tokens until we have processed the entire default
23381 argument. We add the range [first_token, token). */
23382 first_token = cp_lexer_peek_token (parser->lexer);
23383 if (first_token->type == CPP_OPEN_BRACE)
23385 /* For list-initialization, this is straightforward. */
23386 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23387 token = cp_lexer_peek_token (parser->lexer);
23389 else while (true)
23391 bool done = false;
23393 /* Peek at the next token. */
23394 token = cp_lexer_peek_token (parser->lexer);
23395 /* What we do depends on what token we have. */
23396 switch (token->type)
23398 /* In valid code, a default argument must be
23399 immediately followed by a `,' `)', or `...'. */
23400 case CPP_COMMA:
23401 if (depth == 0 && maybe_template_id)
23403 /* If we've seen a '<', we might be in a
23404 template-argument-list. Until Core issue 325 is
23405 resolved, we don't know how this situation ought
23406 to be handled, so try to DTRT. We check whether
23407 what comes after the comma is a valid parameter
23408 declaration list. If it is, then the comma ends
23409 the default argument; otherwise the default
23410 argument continues. */
23411 bool error = false;
23412 tree t;
23414 /* Set ITALP so cp_parser_parameter_declaration_list
23415 doesn't decide to commit to this parse. */
23416 bool saved_italp = parser->in_template_argument_list_p;
23417 parser->in_template_argument_list_p = true;
23419 cp_parser_parse_tentatively (parser);
23420 cp_lexer_consume_token (parser->lexer);
23422 if (nsdmi)
23424 int ctor_dtor_or_conv_p;
23425 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23426 &ctor_dtor_or_conv_p,
23427 /*parenthesized_p=*/NULL,
23428 /*member_p=*/true);
23430 else
23432 begin_scope (sk_function_parms, NULL_TREE);
23433 cp_parser_parameter_declaration_list (parser, &error);
23434 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
23435 pop_binding (DECL_NAME (t), t);
23436 leave_scope ();
23438 if (!cp_parser_error_occurred (parser) && !error)
23439 done = true;
23440 cp_parser_abort_tentative_parse (parser);
23442 parser->in_template_argument_list_p = saved_italp;
23443 break;
23445 case CPP_CLOSE_PAREN:
23446 case CPP_ELLIPSIS:
23447 /* If we run into a non-nested `;', `}', or `]',
23448 then the code is invalid -- but the default
23449 argument is certainly over. */
23450 case CPP_SEMICOLON:
23451 case CPP_CLOSE_BRACE:
23452 case CPP_CLOSE_SQUARE:
23453 if (depth == 0)
23454 done = true;
23455 /* Update DEPTH, if necessary. */
23456 else if (token->type == CPP_CLOSE_PAREN
23457 || token->type == CPP_CLOSE_BRACE
23458 || token->type == CPP_CLOSE_SQUARE)
23459 --depth;
23460 break;
23462 case CPP_OPEN_PAREN:
23463 case CPP_OPEN_SQUARE:
23464 case CPP_OPEN_BRACE:
23465 ++depth;
23466 break;
23468 case CPP_LESS:
23469 if (depth == 0)
23470 /* This might be the comparison operator, or it might
23471 start a template argument list. */
23472 ++maybe_template_id;
23473 break;
23475 case CPP_RSHIFT:
23476 if (cxx_dialect == cxx98)
23477 break;
23478 /* Fall through for C++0x, which treats the `>>'
23479 operator like two `>' tokens in certain
23480 cases. */
23482 case CPP_GREATER:
23483 if (depth == 0)
23485 /* This might be an operator, or it might close a
23486 template argument list. But if a previous '<'
23487 started a template argument list, this will have
23488 closed it, so we can't be in one anymore. */
23489 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
23490 if (maybe_template_id < 0)
23491 maybe_template_id = 0;
23493 break;
23495 /* If we run out of tokens, issue an error message. */
23496 case CPP_EOF:
23497 case CPP_PRAGMA_EOL:
23498 error_at (token->location, "file ends in default argument");
23499 done = true;
23500 break;
23502 case CPP_NAME:
23503 case CPP_SCOPE:
23504 /* In these cases, we should look for template-ids.
23505 For example, if the default argument is
23506 `X<int, double>()', we need to do name lookup to
23507 figure out whether or not `X' is a template; if
23508 so, the `,' does not end the default argument.
23510 That is not yet done. */
23511 break;
23513 default:
23514 break;
23517 /* If we've reached the end, stop. */
23518 if (done)
23519 break;
23521 /* Add the token to the token block. */
23522 token = cp_lexer_consume_token (parser->lexer);
23525 /* Create a DEFAULT_ARG to represent the unparsed default
23526 argument. */
23527 default_argument = make_node (DEFAULT_ARG);
23528 DEFARG_TOKENS (default_argument)
23529 = cp_token_cache_new (first_token, token);
23530 DEFARG_INSTANTIATIONS (default_argument) = NULL;
23532 return default_argument;
23535 /* Begin parsing tentatively. We always save tokens while parsing
23536 tentatively so that if the tentative parsing fails we can restore the
23537 tokens. */
23539 static void
23540 cp_parser_parse_tentatively (cp_parser* parser)
23542 /* Enter a new parsing context. */
23543 parser->context = cp_parser_context_new (parser->context);
23544 /* Begin saving tokens. */
23545 cp_lexer_save_tokens (parser->lexer);
23546 /* In order to avoid repetitive access control error messages,
23547 access checks are queued up until we are no longer parsing
23548 tentatively. */
23549 push_deferring_access_checks (dk_deferred);
23552 /* Commit to the currently active tentative parse. */
23554 static void
23555 cp_parser_commit_to_tentative_parse (cp_parser* parser)
23557 cp_parser_context *context;
23558 cp_lexer *lexer;
23560 /* Mark all of the levels as committed. */
23561 lexer = parser->lexer;
23562 for (context = parser->context; context->next; context = context->next)
23564 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
23565 break;
23566 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
23567 while (!cp_lexer_saving_tokens (lexer))
23568 lexer = lexer->next;
23569 cp_lexer_commit_tokens (lexer);
23573 /* Abort the currently active tentative parse. All consumed tokens
23574 will be rolled back, and no diagnostics will be issued. */
23576 static void
23577 cp_parser_abort_tentative_parse (cp_parser* parser)
23579 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
23580 || errorcount > 0);
23581 cp_parser_simulate_error (parser);
23582 /* Now, pretend that we want to see if the construct was
23583 successfully parsed. */
23584 cp_parser_parse_definitely (parser);
23587 /* Stop parsing tentatively. If a parse error has occurred, restore the
23588 token stream. Otherwise, commit to the tokens we have consumed.
23589 Returns true if no error occurred; false otherwise. */
23591 static bool
23592 cp_parser_parse_definitely (cp_parser* parser)
23594 bool error_occurred;
23595 cp_parser_context *context;
23597 /* Remember whether or not an error occurred, since we are about to
23598 destroy that information. */
23599 error_occurred = cp_parser_error_occurred (parser);
23600 /* Remove the topmost context from the stack. */
23601 context = parser->context;
23602 parser->context = context->next;
23603 /* If no parse errors occurred, commit to the tentative parse. */
23604 if (!error_occurred)
23606 /* Commit to the tokens read tentatively, unless that was
23607 already done. */
23608 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
23609 cp_lexer_commit_tokens (parser->lexer);
23611 pop_to_parent_deferring_access_checks ();
23613 /* Otherwise, if errors occurred, roll back our state so that things
23614 are just as they were before we began the tentative parse. */
23615 else
23617 cp_lexer_rollback_tokens (parser->lexer);
23618 pop_deferring_access_checks ();
23620 /* Add the context to the front of the free list. */
23621 context->next = cp_parser_context_free_list;
23622 cp_parser_context_free_list = context;
23624 return !error_occurred;
23627 /* Returns true if we are parsing tentatively and are not committed to
23628 this tentative parse. */
23630 static bool
23631 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
23633 return (cp_parser_parsing_tentatively (parser)
23634 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
23637 /* Returns nonzero iff an error has occurred during the most recent
23638 tentative parse. */
23640 static bool
23641 cp_parser_error_occurred (cp_parser* parser)
23643 return (cp_parser_parsing_tentatively (parser)
23644 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
23647 /* Returns nonzero if GNU extensions are allowed. */
23649 static bool
23650 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
23652 return parser->allow_gnu_extensions_p;
23655 /* Objective-C++ Productions */
23658 /* Parse an Objective-C expression, which feeds into a primary-expression
23659 above.
23661 objc-expression:
23662 objc-message-expression
23663 objc-string-literal
23664 objc-encode-expression
23665 objc-protocol-expression
23666 objc-selector-expression
23668 Returns a tree representation of the expression. */
23670 static tree
23671 cp_parser_objc_expression (cp_parser* parser)
23673 /* Try to figure out what kind of declaration is present. */
23674 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23676 switch (kwd->type)
23678 case CPP_OPEN_SQUARE:
23679 return cp_parser_objc_message_expression (parser);
23681 case CPP_OBJC_STRING:
23682 kwd = cp_lexer_consume_token (parser->lexer);
23683 return objc_build_string_object (kwd->u.value);
23685 case CPP_KEYWORD:
23686 switch (kwd->keyword)
23688 case RID_AT_ENCODE:
23689 return cp_parser_objc_encode_expression (parser);
23691 case RID_AT_PROTOCOL:
23692 return cp_parser_objc_protocol_expression (parser);
23694 case RID_AT_SELECTOR:
23695 return cp_parser_objc_selector_expression (parser);
23697 default:
23698 break;
23700 default:
23701 error_at (kwd->location,
23702 "misplaced %<@%D%> Objective-C++ construct",
23703 kwd->u.value);
23704 cp_parser_skip_to_end_of_block_or_statement (parser);
23707 return error_mark_node;
23710 /* Parse an Objective-C message expression.
23712 objc-message-expression:
23713 [ objc-message-receiver objc-message-args ]
23715 Returns a representation of an Objective-C message. */
23717 static tree
23718 cp_parser_objc_message_expression (cp_parser* parser)
23720 tree receiver, messageargs;
23722 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
23723 receiver = cp_parser_objc_message_receiver (parser);
23724 messageargs = cp_parser_objc_message_args (parser);
23725 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23727 return objc_build_message_expr (receiver, messageargs);
23730 /* Parse an objc-message-receiver.
23732 objc-message-receiver:
23733 expression
23734 simple-type-specifier
23736 Returns a representation of the type or expression. */
23738 static tree
23739 cp_parser_objc_message_receiver (cp_parser* parser)
23741 tree rcv;
23743 /* An Objective-C message receiver may be either (1) a type
23744 or (2) an expression. */
23745 cp_parser_parse_tentatively (parser);
23746 rcv = cp_parser_expression (parser, false, NULL);
23748 if (cp_parser_parse_definitely (parser))
23749 return rcv;
23751 rcv = cp_parser_simple_type_specifier (parser,
23752 /*decl_specs=*/NULL,
23753 CP_PARSER_FLAGS_NONE);
23755 return objc_get_class_reference (rcv);
23758 /* Parse the arguments and selectors comprising an Objective-C message.
23760 objc-message-args:
23761 objc-selector
23762 objc-selector-args
23763 objc-selector-args , objc-comma-args
23765 objc-selector-args:
23766 objc-selector [opt] : assignment-expression
23767 objc-selector-args objc-selector [opt] : assignment-expression
23769 objc-comma-args:
23770 assignment-expression
23771 objc-comma-args , assignment-expression
23773 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23774 selector arguments and TREE_VALUE containing a list of comma
23775 arguments. */
23777 static tree
23778 cp_parser_objc_message_args (cp_parser* parser)
23780 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23781 bool maybe_unary_selector_p = true;
23782 cp_token *token = cp_lexer_peek_token (parser->lexer);
23784 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23786 tree selector = NULL_TREE, arg;
23788 if (token->type != CPP_COLON)
23789 selector = cp_parser_objc_selector (parser);
23791 /* Detect if we have a unary selector. */
23792 if (maybe_unary_selector_p
23793 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23794 return build_tree_list (selector, NULL_TREE);
23796 maybe_unary_selector_p = false;
23797 cp_parser_require (parser, CPP_COLON, RT_COLON);
23798 arg = cp_parser_assignment_expression (parser, false, NULL);
23800 sel_args
23801 = chainon (sel_args,
23802 build_tree_list (selector, arg));
23804 token = cp_lexer_peek_token (parser->lexer);
23807 /* Handle non-selector arguments, if any. */
23808 while (token->type == CPP_COMMA)
23810 tree arg;
23812 cp_lexer_consume_token (parser->lexer);
23813 arg = cp_parser_assignment_expression (parser, false, NULL);
23815 addl_args
23816 = chainon (addl_args,
23817 build_tree_list (NULL_TREE, arg));
23819 token = cp_lexer_peek_token (parser->lexer);
23822 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23824 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23825 return build_tree_list (error_mark_node, error_mark_node);
23828 return build_tree_list (sel_args, addl_args);
23831 /* Parse an Objective-C encode expression.
23833 objc-encode-expression:
23834 @encode objc-typename
23836 Returns an encoded representation of the type argument. */
23838 static tree
23839 cp_parser_objc_encode_expression (cp_parser* parser)
23841 tree type;
23842 cp_token *token;
23844 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
23845 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23846 token = cp_lexer_peek_token (parser->lexer);
23847 type = complete_type (cp_parser_type_id (parser));
23848 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23850 if (!type)
23852 error_at (token->location,
23853 "%<@encode%> must specify a type as an argument");
23854 return error_mark_node;
23857 /* This happens if we find @encode(T) (where T is a template
23858 typename or something dependent on a template typename) when
23859 parsing a template. In that case, we can't compile it
23860 immediately, but we rather create an AT_ENCODE_EXPR which will
23861 need to be instantiated when the template is used.
23863 if (dependent_type_p (type))
23865 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23866 TREE_READONLY (value) = 1;
23867 return value;
23870 return objc_build_encode_expr (type);
23873 /* Parse an Objective-C @defs expression. */
23875 static tree
23876 cp_parser_objc_defs_expression (cp_parser *parser)
23878 tree name;
23880 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
23881 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23882 name = cp_parser_identifier (parser);
23883 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23885 return objc_get_class_ivars (name);
23888 /* Parse an Objective-C protocol expression.
23890 objc-protocol-expression:
23891 @protocol ( identifier )
23893 Returns a representation of the protocol expression. */
23895 static tree
23896 cp_parser_objc_protocol_expression (cp_parser* parser)
23898 tree proto;
23900 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23901 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23902 proto = cp_parser_identifier (parser);
23903 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23905 return objc_build_protocol_expr (proto);
23908 /* Parse an Objective-C selector expression.
23910 objc-selector-expression:
23911 @selector ( objc-method-signature )
23913 objc-method-signature:
23914 objc-selector
23915 objc-selector-seq
23917 objc-selector-seq:
23918 objc-selector :
23919 objc-selector-seq objc-selector :
23921 Returns a representation of the method selector. */
23923 static tree
23924 cp_parser_objc_selector_expression (cp_parser* parser)
23926 tree sel_seq = NULL_TREE;
23927 bool maybe_unary_selector_p = true;
23928 cp_token *token;
23929 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23931 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
23932 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23933 token = cp_lexer_peek_token (parser->lexer);
23935 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23936 || token->type == CPP_SCOPE)
23938 tree selector = NULL_TREE;
23940 if (token->type != CPP_COLON
23941 || token->type == CPP_SCOPE)
23942 selector = cp_parser_objc_selector (parser);
23944 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23945 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23947 /* Detect if we have a unary selector. */
23948 if (maybe_unary_selector_p)
23950 sel_seq = selector;
23951 goto finish_selector;
23953 else
23955 cp_parser_error (parser, "expected %<:%>");
23958 maybe_unary_selector_p = false;
23959 token = cp_lexer_consume_token (parser->lexer);
23961 if (token->type == CPP_SCOPE)
23963 sel_seq
23964 = chainon (sel_seq,
23965 build_tree_list (selector, NULL_TREE));
23966 sel_seq
23967 = chainon (sel_seq,
23968 build_tree_list (NULL_TREE, NULL_TREE));
23970 else
23971 sel_seq
23972 = chainon (sel_seq,
23973 build_tree_list (selector, NULL_TREE));
23975 token = cp_lexer_peek_token (parser->lexer);
23978 finish_selector:
23979 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23981 return objc_build_selector_expr (loc, sel_seq);
23984 /* Parse a list of identifiers.
23986 objc-identifier-list:
23987 identifier
23988 objc-identifier-list , identifier
23990 Returns a TREE_LIST of identifier nodes. */
23992 static tree
23993 cp_parser_objc_identifier_list (cp_parser* parser)
23995 tree identifier;
23996 tree list;
23997 cp_token *sep;
23999 identifier = cp_parser_identifier (parser);
24000 if (identifier == error_mark_node)
24001 return error_mark_node;
24003 list = build_tree_list (NULL_TREE, identifier);
24004 sep = cp_lexer_peek_token (parser->lexer);
24006 while (sep->type == CPP_COMMA)
24008 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24009 identifier = cp_parser_identifier (parser);
24010 if (identifier == error_mark_node)
24011 return list;
24013 list = chainon (list, build_tree_list (NULL_TREE,
24014 identifier));
24015 sep = cp_lexer_peek_token (parser->lexer);
24018 return list;
24021 /* Parse an Objective-C alias declaration.
24023 objc-alias-declaration:
24024 @compatibility_alias identifier identifier ;
24026 This function registers the alias mapping with the Objective-C front end.
24027 It returns nothing. */
24029 static void
24030 cp_parser_objc_alias_declaration (cp_parser* parser)
24032 tree alias, orig;
24034 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
24035 alias = cp_parser_identifier (parser);
24036 orig = cp_parser_identifier (parser);
24037 objc_declare_alias (alias, orig);
24038 cp_parser_consume_semicolon_at_end_of_statement (parser);
24041 /* Parse an Objective-C class forward-declaration.
24043 objc-class-declaration:
24044 @class objc-identifier-list ;
24046 The function registers the forward declarations with the Objective-C
24047 front end. It returns nothing. */
24049 static void
24050 cp_parser_objc_class_declaration (cp_parser* parser)
24052 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
24053 while (true)
24055 tree id;
24057 id = cp_parser_identifier (parser);
24058 if (id == error_mark_node)
24059 break;
24061 objc_declare_class (id);
24063 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24064 cp_lexer_consume_token (parser->lexer);
24065 else
24066 break;
24068 cp_parser_consume_semicolon_at_end_of_statement (parser);
24071 /* Parse a list of Objective-C protocol references.
24073 objc-protocol-refs-opt:
24074 objc-protocol-refs [opt]
24076 objc-protocol-refs:
24077 < objc-identifier-list >
24079 Returns a TREE_LIST of identifiers, if any. */
24081 static tree
24082 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
24084 tree protorefs = NULL_TREE;
24086 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
24088 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
24089 protorefs = cp_parser_objc_identifier_list (parser);
24090 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
24093 return protorefs;
24096 /* Parse a Objective-C visibility specification. */
24098 static void
24099 cp_parser_objc_visibility_spec (cp_parser* parser)
24101 cp_token *vis = cp_lexer_peek_token (parser->lexer);
24103 switch (vis->keyword)
24105 case RID_AT_PRIVATE:
24106 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
24107 break;
24108 case RID_AT_PROTECTED:
24109 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
24110 break;
24111 case RID_AT_PUBLIC:
24112 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
24113 break;
24114 case RID_AT_PACKAGE:
24115 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
24116 break;
24117 default:
24118 return;
24121 /* Eat '@private'/'@protected'/'@public'. */
24122 cp_lexer_consume_token (parser->lexer);
24125 /* Parse an Objective-C method type. Return 'true' if it is a class
24126 (+) method, and 'false' if it is an instance (-) method. */
24128 static inline bool
24129 cp_parser_objc_method_type (cp_parser* parser)
24131 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
24132 return true;
24133 else
24134 return false;
24137 /* Parse an Objective-C protocol qualifier. */
24139 static tree
24140 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
24142 tree quals = NULL_TREE, node;
24143 cp_token *token = cp_lexer_peek_token (parser->lexer);
24145 node = token->u.value;
24147 while (node && TREE_CODE (node) == IDENTIFIER_NODE
24148 && (node == ridpointers [(int) RID_IN]
24149 || node == ridpointers [(int) RID_OUT]
24150 || node == ridpointers [(int) RID_INOUT]
24151 || node == ridpointers [(int) RID_BYCOPY]
24152 || node == ridpointers [(int) RID_BYREF]
24153 || node == ridpointers [(int) RID_ONEWAY]))
24155 quals = tree_cons (NULL_TREE, node, quals);
24156 cp_lexer_consume_token (parser->lexer);
24157 token = cp_lexer_peek_token (parser->lexer);
24158 node = token->u.value;
24161 return quals;
24164 /* Parse an Objective-C typename. */
24166 static tree
24167 cp_parser_objc_typename (cp_parser* parser)
24169 tree type_name = NULL_TREE;
24171 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24173 tree proto_quals, cp_type = NULL_TREE;
24175 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24176 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
24178 /* An ObjC type name may consist of just protocol qualifiers, in which
24179 case the type shall default to 'id'. */
24180 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24182 cp_type = cp_parser_type_id (parser);
24184 /* If the type could not be parsed, an error has already
24185 been produced. For error recovery, behave as if it had
24186 not been specified, which will use the default type
24187 'id'. */
24188 if (cp_type == error_mark_node)
24190 cp_type = NULL_TREE;
24191 /* We need to skip to the closing parenthesis as
24192 cp_parser_type_id() does not seem to do it for
24193 us. */
24194 cp_parser_skip_to_closing_parenthesis (parser,
24195 /*recovering=*/true,
24196 /*or_comma=*/false,
24197 /*consume_paren=*/false);
24201 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24202 type_name = build_tree_list (proto_quals, cp_type);
24205 return type_name;
24208 /* Check to see if TYPE refers to an Objective-C selector name. */
24210 static bool
24211 cp_parser_objc_selector_p (enum cpp_ttype type)
24213 return (type == CPP_NAME || type == CPP_KEYWORD
24214 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
24215 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
24216 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
24217 || type == CPP_XOR || type == CPP_XOR_EQ);
24220 /* Parse an Objective-C selector. */
24222 static tree
24223 cp_parser_objc_selector (cp_parser* parser)
24225 cp_token *token = cp_lexer_consume_token (parser->lexer);
24227 if (!cp_parser_objc_selector_p (token->type))
24229 error_at (token->location, "invalid Objective-C++ selector name");
24230 return error_mark_node;
24233 /* C++ operator names are allowed to appear in ObjC selectors. */
24234 switch (token->type)
24236 case CPP_AND_AND: return get_identifier ("and");
24237 case CPP_AND_EQ: return get_identifier ("and_eq");
24238 case CPP_AND: return get_identifier ("bitand");
24239 case CPP_OR: return get_identifier ("bitor");
24240 case CPP_COMPL: return get_identifier ("compl");
24241 case CPP_NOT: return get_identifier ("not");
24242 case CPP_NOT_EQ: return get_identifier ("not_eq");
24243 case CPP_OR_OR: return get_identifier ("or");
24244 case CPP_OR_EQ: return get_identifier ("or_eq");
24245 case CPP_XOR: return get_identifier ("xor");
24246 case CPP_XOR_EQ: return get_identifier ("xor_eq");
24247 default: return token->u.value;
24251 /* Parse an Objective-C params list. */
24253 static tree
24254 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
24256 tree params = NULL_TREE;
24257 bool maybe_unary_selector_p = true;
24258 cp_token *token = cp_lexer_peek_token (parser->lexer);
24260 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
24262 tree selector = NULL_TREE, type_name, identifier;
24263 tree parm_attr = NULL_TREE;
24265 if (token->keyword == RID_ATTRIBUTE)
24266 break;
24268 if (token->type != CPP_COLON)
24269 selector = cp_parser_objc_selector (parser);
24271 /* Detect if we have a unary selector. */
24272 if (maybe_unary_selector_p
24273 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24275 params = selector; /* Might be followed by attributes. */
24276 break;
24279 maybe_unary_selector_p = false;
24280 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24282 /* Something went quite wrong. There should be a colon
24283 here, but there is not. Stop parsing parameters. */
24284 break;
24286 type_name = cp_parser_objc_typename (parser);
24287 /* New ObjC allows attributes on parameters too. */
24288 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
24289 parm_attr = cp_parser_attributes_opt (parser);
24290 identifier = cp_parser_identifier (parser);
24292 params
24293 = chainon (params,
24294 objc_build_keyword_decl (selector,
24295 type_name,
24296 identifier,
24297 parm_attr));
24299 token = cp_lexer_peek_token (parser->lexer);
24302 if (params == NULL_TREE)
24304 cp_parser_error (parser, "objective-c++ method declaration is expected");
24305 return error_mark_node;
24308 /* We allow tail attributes for the method. */
24309 if (token->keyword == RID_ATTRIBUTE)
24311 *attributes = cp_parser_attributes_opt (parser);
24312 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24313 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24314 return params;
24315 cp_parser_error (parser,
24316 "method attributes must be specified at the end");
24317 return error_mark_node;
24320 if (params == NULL_TREE)
24322 cp_parser_error (parser, "objective-c++ method declaration is expected");
24323 return error_mark_node;
24325 return params;
24328 /* Parse the non-keyword Objective-C params. */
24330 static tree
24331 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
24332 tree* attributes)
24334 tree params = make_node (TREE_LIST);
24335 cp_token *token = cp_lexer_peek_token (parser->lexer);
24336 *ellipsisp = false; /* Initially, assume no ellipsis. */
24338 while (token->type == CPP_COMMA)
24340 cp_parameter_declarator *parmdecl;
24341 tree parm;
24343 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24344 token = cp_lexer_peek_token (parser->lexer);
24346 if (token->type == CPP_ELLIPSIS)
24348 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
24349 *ellipsisp = true;
24350 token = cp_lexer_peek_token (parser->lexer);
24351 break;
24354 /* TODO: parse attributes for tail parameters. */
24355 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
24356 parm = grokdeclarator (parmdecl->declarator,
24357 &parmdecl->decl_specifiers,
24358 PARM, /*initialized=*/0,
24359 /*attrlist=*/NULL);
24361 chainon (params, build_tree_list (NULL_TREE, parm));
24362 token = cp_lexer_peek_token (parser->lexer);
24365 /* We allow tail attributes for the method. */
24366 if (token->keyword == RID_ATTRIBUTE)
24368 if (*attributes == NULL_TREE)
24370 *attributes = cp_parser_attributes_opt (parser);
24371 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24372 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24373 return params;
24375 else
24376 /* We have an error, but parse the attributes, so that we can
24377 carry on. */
24378 *attributes = cp_parser_attributes_opt (parser);
24380 cp_parser_error (parser,
24381 "method attributes must be specified at the end");
24382 return error_mark_node;
24385 return params;
24388 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
24390 static void
24391 cp_parser_objc_interstitial_code (cp_parser* parser)
24393 cp_token *token = cp_lexer_peek_token (parser->lexer);
24395 /* If the next token is `extern' and the following token is a string
24396 literal, then we have a linkage specification. */
24397 if (token->keyword == RID_EXTERN
24398 && cp_parser_is_pure_string_literal
24399 (cp_lexer_peek_nth_token (parser->lexer, 2)))
24400 cp_parser_linkage_specification (parser);
24401 /* Handle #pragma, if any. */
24402 else if (token->type == CPP_PRAGMA)
24403 cp_parser_pragma (parser, pragma_external);
24404 /* Allow stray semicolons. */
24405 else if (token->type == CPP_SEMICOLON)
24406 cp_lexer_consume_token (parser->lexer);
24407 /* Mark methods as optional or required, when building protocols. */
24408 else if (token->keyword == RID_AT_OPTIONAL)
24410 cp_lexer_consume_token (parser->lexer);
24411 objc_set_method_opt (true);
24413 else if (token->keyword == RID_AT_REQUIRED)
24415 cp_lexer_consume_token (parser->lexer);
24416 objc_set_method_opt (false);
24418 else if (token->keyword == RID_NAMESPACE)
24419 cp_parser_namespace_definition (parser);
24420 /* Other stray characters must generate errors. */
24421 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
24423 cp_lexer_consume_token (parser->lexer);
24424 error ("stray %qs between Objective-C++ methods",
24425 token->type == CPP_OPEN_BRACE ? "{" : "}");
24427 /* Finally, try to parse a block-declaration, or a function-definition. */
24428 else
24429 cp_parser_block_declaration (parser, /*statement_p=*/false);
24432 /* Parse a method signature. */
24434 static tree
24435 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
24437 tree rettype, kwdparms, optparms;
24438 bool ellipsis = false;
24439 bool is_class_method;
24441 is_class_method = cp_parser_objc_method_type (parser);
24442 rettype = cp_parser_objc_typename (parser);
24443 *attributes = NULL_TREE;
24444 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
24445 if (kwdparms == error_mark_node)
24446 return error_mark_node;
24447 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
24448 if (optparms == error_mark_node)
24449 return error_mark_node;
24451 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
24454 static bool
24455 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
24457 tree tattr;
24458 cp_lexer_save_tokens (parser->lexer);
24459 tattr = cp_parser_attributes_opt (parser);
24460 gcc_assert (tattr) ;
24462 /* If the attributes are followed by a method introducer, this is not allowed.
24463 Dump the attributes and flag the situation. */
24464 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
24465 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
24466 return true;
24468 /* Otherwise, the attributes introduce some interstitial code, possibly so
24469 rewind to allow that check. */
24470 cp_lexer_rollback_tokens (parser->lexer);
24471 return false;
24474 /* Parse an Objective-C method prototype list. */
24476 static void
24477 cp_parser_objc_method_prototype_list (cp_parser* parser)
24479 cp_token *token = cp_lexer_peek_token (parser->lexer);
24481 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24483 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24485 tree attributes, sig;
24486 bool is_class_method;
24487 if (token->type == CPP_PLUS)
24488 is_class_method = true;
24489 else
24490 is_class_method = false;
24491 sig = cp_parser_objc_method_signature (parser, &attributes);
24492 if (sig == error_mark_node)
24494 cp_parser_skip_to_end_of_block_or_statement (parser);
24495 token = cp_lexer_peek_token (parser->lexer);
24496 continue;
24498 objc_add_method_declaration (is_class_method, sig, attributes);
24499 cp_parser_consume_semicolon_at_end_of_statement (parser);
24501 else if (token->keyword == RID_AT_PROPERTY)
24502 cp_parser_objc_at_property_declaration (parser);
24503 else if (token->keyword == RID_ATTRIBUTE
24504 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24505 warning_at (cp_lexer_peek_token (parser->lexer)->location,
24506 OPT_Wattributes,
24507 "prefix attributes are ignored for methods");
24508 else
24509 /* Allow for interspersed non-ObjC++ code. */
24510 cp_parser_objc_interstitial_code (parser);
24512 token = cp_lexer_peek_token (parser->lexer);
24515 if (token->type != CPP_EOF)
24516 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24517 else
24518 cp_parser_error (parser, "expected %<@end%>");
24520 objc_finish_interface ();
24523 /* Parse an Objective-C method definition list. */
24525 static void
24526 cp_parser_objc_method_definition_list (cp_parser* parser)
24528 cp_token *token = cp_lexer_peek_token (parser->lexer);
24530 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
24532 tree meth;
24534 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
24536 cp_token *ptk;
24537 tree sig, attribute;
24538 bool is_class_method;
24539 if (token->type == CPP_PLUS)
24540 is_class_method = true;
24541 else
24542 is_class_method = false;
24543 push_deferring_access_checks (dk_deferred);
24544 sig = cp_parser_objc_method_signature (parser, &attribute);
24545 if (sig == error_mark_node)
24547 cp_parser_skip_to_end_of_block_or_statement (parser);
24548 token = cp_lexer_peek_token (parser->lexer);
24549 continue;
24551 objc_start_method_definition (is_class_method, sig, attribute,
24552 NULL_TREE);
24554 /* For historical reasons, we accept an optional semicolon. */
24555 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24556 cp_lexer_consume_token (parser->lexer);
24558 ptk = cp_lexer_peek_token (parser->lexer);
24559 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
24560 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
24562 perform_deferred_access_checks (tf_warning_or_error);
24563 stop_deferring_access_checks ();
24564 meth = cp_parser_function_definition_after_declarator (parser,
24565 false);
24566 pop_deferring_access_checks ();
24567 objc_finish_method_definition (meth);
24570 /* The following case will be removed once @synthesize is
24571 completely implemented. */
24572 else if (token->keyword == RID_AT_PROPERTY)
24573 cp_parser_objc_at_property_declaration (parser);
24574 else if (token->keyword == RID_AT_SYNTHESIZE)
24575 cp_parser_objc_at_synthesize_declaration (parser);
24576 else if (token->keyword == RID_AT_DYNAMIC)
24577 cp_parser_objc_at_dynamic_declaration (parser);
24578 else if (token->keyword == RID_ATTRIBUTE
24579 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
24580 warning_at (token->location, OPT_Wattributes,
24581 "prefix attributes are ignored for methods");
24582 else
24583 /* Allow for interspersed non-ObjC++ code. */
24584 cp_parser_objc_interstitial_code (parser);
24586 token = cp_lexer_peek_token (parser->lexer);
24589 if (token->type != CPP_EOF)
24590 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24591 else
24592 cp_parser_error (parser, "expected %<@end%>");
24594 objc_finish_implementation ();
24597 /* Parse Objective-C ivars. */
24599 static void
24600 cp_parser_objc_class_ivars (cp_parser* parser)
24602 cp_token *token = cp_lexer_peek_token (parser->lexer);
24604 if (token->type != CPP_OPEN_BRACE)
24605 return; /* No ivars specified. */
24607 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
24608 token = cp_lexer_peek_token (parser->lexer);
24610 while (token->type != CPP_CLOSE_BRACE
24611 && token->keyword != RID_AT_END && token->type != CPP_EOF)
24613 cp_decl_specifier_seq declspecs;
24614 int decl_class_or_enum_p;
24615 tree prefix_attributes;
24617 cp_parser_objc_visibility_spec (parser);
24619 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24620 break;
24622 cp_parser_decl_specifier_seq (parser,
24623 CP_PARSER_FLAGS_OPTIONAL,
24624 &declspecs,
24625 &decl_class_or_enum_p);
24627 /* auto, register, static, extern, mutable. */
24628 if (declspecs.storage_class != sc_none)
24630 cp_parser_error (parser, "invalid type for instance variable");
24631 declspecs.storage_class = sc_none;
24634 /* thread_local. */
24635 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
24637 cp_parser_error (parser, "invalid type for instance variable");
24638 declspecs.locations[ds_thread] = 0;
24641 /* typedef. */
24642 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
24644 cp_parser_error (parser, "invalid type for instance variable");
24645 declspecs.locations[ds_typedef] = 0;
24648 prefix_attributes = declspecs.attributes;
24649 declspecs.attributes = NULL_TREE;
24651 /* Keep going until we hit the `;' at the end of the
24652 declaration. */
24653 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24655 tree width = NULL_TREE, attributes, first_attribute, decl;
24656 cp_declarator *declarator = NULL;
24657 int ctor_dtor_or_conv_p;
24659 /* Check for a (possibly unnamed) bitfield declaration. */
24660 token = cp_lexer_peek_token (parser->lexer);
24661 if (token->type == CPP_COLON)
24662 goto eat_colon;
24664 if (token->type == CPP_NAME
24665 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24666 == CPP_COLON))
24668 /* Get the name of the bitfield. */
24669 declarator = make_id_declarator (NULL_TREE,
24670 cp_parser_identifier (parser),
24671 sfk_none);
24673 eat_colon:
24674 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24675 /* Get the width of the bitfield. */
24676 width
24677 = cp_parser_constant_expression (parser,
24678 /*allow_non_constant=*/false,
24679 NULL);
24681 else
24683 /* Parse the declarator. */
24684 declarator
24685 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24686 &ctor_dtor_or_conv_p,
24687 /*parenthesized_p=*/NULL,
24688 /*member_p=*/false);
24691 /* Look for attributes that apply to the ivar. */
24692 attributes = cp_parser_attributes_opt (parser);
24693 /* Remember which attributes are prefix attributes and
24694 which are not. */
24695 first_attribute = attributes;
24696 /* Combine the attributes. */
24697 attributes = chainon (prefix_attributes, attributes);
24699 if (width)
24700 /* Create the bitfield declaration. */
24701 decl = grokbitfield (declarator, &declspecs,
24702 width,
24703 attributes);
24704 else
24705 decl = grokfield (declarator, &declspecs,
24706 NULL_TREE, /*init_const_expr_p=*/false,
24707 NULL_TREE, attributes);
24709 /* Add the instance variable. */
24710 if (decl != error_mark_node && decl != NULL_TREE)
24711 objc_add_instance_variable (decl);
24713 /* Reset PREFIX_ATTRIBUTES. */
24714 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24715 attributes = TREE_CHAIN (attributes);
24716 if (attributes)
24717 TREE_CHAIN (attributes) = NULL_TREE;
24719 token = cp_lexer_peek_token (parser->lexer);
24721 if (token->type == CPP_COMMA)
24723 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24724 continue;
24726 break;
24729 cp_parser_consume_semicolon_at_end_of_statement (parser);
24730 token = cp_lexer_peek_token (parser->lexer);
24733 if (token->keyword == RID_AT_END)
24734 cp_parser_error (parser, "expected %<}%>");
24736 /* Do not consume the RID_AT_END, so it will be read again as terminating
24737 the @interface of @implementation. */
24738 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24739 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
24741 /* For historical reasons, we accept an optional semicolon. */
24742 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24743 cp_lexer_consume_token (parser->lexer);
24746 /* Parse an Objective-C protocol declaration. */
24748 static void
24749 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24751 tree proto, protorefs;
24752 cp_token *tok;
24754 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24755 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24757 tok = cp_lexer_peek_token (parser->lexer);
24758 error_at (tok->location, "identifier expected after %<@protocol%>");
24759 cp_parser_consume_semicolon_at_end_of_statement (parser);
24760 return;
24763 /* See if we have a forward declaration or a definition. */
24764 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24766 /* Try a forward declaration first. */
24767 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24769 while (true)
24771 tree id;
24773 id = cp_parser_identifier (parser);
24774 if (id == error_mark_node)
24775 break;
24777 objc_declare_protocol (id, attributes);
24779 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24780 cp_lexer_consume_token (parser->lexer);
24781 else
24782 break;
24784 cp_parser_consume_semicolon_at_end_of_statement (parser);
24787 /* Ok, we got a full-fledged definition (or at least should). */
24788 else
24790 proto = cp_parser_identifier (parser);
24791 protorefs = cp_parser_objc_protocol_refs_opt (parser);
24792 objc_start_protocol (proto, protorefs, attributes);
24793 cp_parser_objc_method_prototype_list (parser);
24797 /* Parse an Objective-C superclass or category. */
24799 static void
24800 cp_parser_objc_superclass_or_category (cp_parser *parser,
24801 bool iface_p,
24802 tree *super,
24803 tree *categ, bool *is_class_extension)
24805 cp_token *next = cp_lexer_peek_token (parser->lexer);
24807 *super = *categ = NULL_TREE;
24808 *is_class_extension = false;
24809 if (next->type == CPP_COLON)
24811 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24812 *super = cp_parser_identifier (parser);
24814 else if (next->type == CPP_OPEN_PAREN)
24816 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24818 /* If there is no category name, and this is an @interface, we
24819 have a class extension. */
24820 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24822 *categ = NULL_TREE;
24823 *is_class_extension = true;
24825 else
24826 *categ = cp_parser_identifier (parser);
24828 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24832 /* Parse an Objective-C class interface. */
24834 static void
24835 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24837 tree name, super, categ, protos;
24838 bool is_class_extension;
24840 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
24841 name = cp_parser_identifier (parser);
24842 if (name == error_mark_node)
24844 /* It's hard to recover because even if valid @interface stuff
24845 is to follow, we can't compile it (or validate it) if we
24846 don't even know which class it refers to. Let's assume this
24847 was a stray '@interface' token in the stream and skip it.
24849 return;
24851 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24852 &is_class_extension);
24853 protos = cp_parser_objc_protocol_refs_opt (parser);
24855 /* We have either a class or a category on our hands. */
24856 if (categ || is_class_extension)
24857 objc_start_category_interface (name, categ, protos, attributes);
24858 else
24860 objc_start_class_interface (name, super, protos, attributes);
24861 /* Handle instance variable declarations, if any. */
24862 cp_parser_objc_class_ivars (parser);
24863 objc_continue_interface ();
24866 cp_parser_objc_method_prototype_list (parser);
24869 /* Parse an Objective-C class implementation. */
24871 static void
24872 cp_parser_objc_class_implementation (cp_parser* parser)
24874 tree name, super, categ;
24875 bool is_class_extension;
24877 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
24878 name = cp_parser_identifier (parser);
24879 if (name == error_mark_node)
24881 /* It's hard to recover because even if valid @implementation
24882 stuff is to follow, we can't compile it (or validate it) if
24883 we don't even know which class it refers to. Let's assume
24884 this was a stray '@implementation' token in the stream and
24885 skip it.
24887 return;
24889 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24890 &is_class_extension);
24892 /* We have either a class or a category on our hands. */
24893 if (categ)
24894 objc_start_category_implementation (name, categ);
24895 else
24897 objc_start_class_implementation (name, super);
24898 /* Handle instance variable declarations, if any. */
24899 cp_parser_objc_class_ivars (parser);
24900 objc_continue_implementation ();
24903 cp_parser_objc_method_definition_list (parser);
24906 /* Consume the @end token and finish off the implementation. */
24908 static void
24909 cp_parser_objc_end_implementation (cp_parser* parser)
24911 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24912 objc_finish_implementation ();
24915 /* Parse an Objective-C declaration. */
24917 static void
24918 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24920 /* Try to figure out what kind of declaration is present. */
24921 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24923 if (attributes)
24924 switch (kwd->keyword)
24926 case RID_AT_ALIAS:
24927 case RID_AT_CLASS:
24928 case RID_AT_END:
24929 error_at (kwd->location, "attributes may not be specified before"
24930 " the %<@%D%> Objective-C++ keyword",
24931 kwd->u.value);
24932 attributes = NULL;
24933 break;
24934 case RID_AT_IMPLEMENTATION:
24935 warning_at (kwd->location, OPT_Wattributes,
24936 "prefix attributes are ignored before %<@%D%>",
24937 kwd->u.value);
24938 attributes = NULL;
24939 default:
24940 break;
24943 switch (kwd->keyword)
24945 case RID_AT_ALIAS:
24946 cp_parser_objc_alias_declaration (parser);
24947 break;
24948 case RID_AT_CLASS:
24949 cp_parser_objc_class_declaration (parser);
24950 break;
24951 case RID_AT_PROTOCOL:
24952 cp_parser_objc_protocol_declaration (parser, attributes);
24953 break;
24954 case RID_AT_INTERFACE:
24955 cp_parser_objc_class_interface (parser, attributes);
24956 break;
24957 case RID_AT_IMPLEMENTATION:
24958 cp_parser_objc_class_implementation (parser);
24959 break;
24960 case RID_AT_END:
24961 cp_parser_objc_end_implementation (parser);
24962 break;
24963 default:
24964 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24965 kwd->u.value);
24966 cp_parser_skip_to_end_of_block_or_statement (parser);
24970 /* Parse an Objective-C try-catch-finally statement.
24972 objc-try-catch-finally-stmt:
24973 @try compound-statement objc-catch-clause-seq [opt]
24974 objc-finally-clause [opt]
24976 objc-catch-clause-seq:
24977 objc-catch-clause objc-catch-clause-seq [opt]
24979 objc-catch-clause:
24980 @catch ( objc-exception-declaration ) compound-statement
24982 objc-finally-clause:
24983 @finally compound-statement
24985 objc-exception-declaration:
24986 parameter-declaration
24987 '...'
24989 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24991 Returns NULL_TREE.
24993 PS: This function is identical to c_parser_objc_try_catch_finally_statement
24994 for C. Keep them in sync. */
24996 static tree
24997 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24999 location_t location;
25000 tree stmt;
25002 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
25003 location = cp_lexer_peek_token (parser->lexer)->location;
25004 objc_maybe_warn_exceptions (location);
25005 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
25006 node, lest it get absorbed into the surrounding block. */
25007 stmt = push_stmt_list ();
25008 cp_parser_compound_statement (parser, NULL, false, false);
25009 objc_begin_try_stmt (location, pop_stmt_list (stmt));
25011 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
25013 cp_parameter_declarator *parm;
25014 tree parameter_declaration = error_mark_node;
25015 bool seen_open_paren = false;
25017 cp_lexer_consume_token (parser->lexer);
25018 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25019 seen_open_paren = true;
25020 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25022 /* We have "@catch (...)" (where the '...' are literally
25023 what is in the code). Skip the '...'.
25024 parameter_declaration is set to NULL_TREE, and
25025 objc_being_catch_clauses() knows that that means
25026 '...'. */
25027 cp_lexer_consume_token (parser->lexer);
25028 parameter_declaration = NULL_TREE;
25030 else
25032 /* We have "@catch (NSException *exception)" or something
25033 like that. Parse the parameter declaration. */
25034 parm = cp_parser_parameter_declaration (parser, false, NULL);
25035 if (parm == NULL)
25036 parameter_declaration = error_mark_node;
25037 else
25038 parameter_declaration = grokdeclarator (parm->declarator,
25039 &parm->decl_specifiers,
25040 PARM, /*initialized=*/0,
25041 /*attrlist=*/NULL);
25043 if (seen_open_paren)
25044 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25045 else
25047 /* If there was no open parenthesis, we are recovering from
25048 an error, and we are trying to figure out what mistake
25049 the user has made. */
25051 /* If there is an immediate closing parenthesis, the user
25052 probably forgot the opening one (ie, they typed "@catch
25053 NSException *e)". Parse the closing parenthesis and keep
25054 going. */
25055 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25056 cp_lexer_consume_token (parser->lexer);
25058 /* If these is no immediate closing parenthesis, the user
25059 probably doesn't know that parenthesis are required at
25060 all (ie, they typed "@catch NSException *e"). So, just
25061 forget about the closing parenthesis and keep going. */
25063 objc_begin_catch_clause (parameter_declaration);
25064 cp_parser_compound_statement (parser, NULL, false, false);
25065 objc_finish_catch_clause ();
25067 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
25069 cp_lexer_consume_token (parser->lexer);
25070 location = cp_lexer_peek_token (parser->lexer)->location;
25071 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
25072 node, lest it get absorbed into the surrounding block. */
25073 stmt = push_stmt_list ();
25074 cp_parser_compound_statement (parser, NULL, false, false);
25075 objc_build_finally_clause (location, pop_stmt_list (stmt));
25078 return objc_finish_try_stmt ();
25081 /* Parse an Objective-C synchronized statement.
25083 objc-synchronized-stmt:
25084 @synchronized ( expression ) compound-statement
25086 Returns NULL_TREE. */
25088 static tree
25089 cp_parser_objc_synchronized_statement (cp_parser *parser)
25091 location_t location;
25092 tree lock, stmt;
25094 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
25096 location = cp_lexer_peek_token (parser->lexer)->location;
25097 objc_maybe_warn_exceptions (location);
25098 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25099 lock = cp_parser_expression (parser, false, NULL);
25100 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25102 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
25103 node, lest it get absorbed into the surrounding block. */
25104 stmt = push_stmt_list ();
25105 cp_parser_compound_statement (parser, NULL, false, false);
25107 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
25110 /* Parse an Objective-C throw statement.
25112 objc-throw-stmt:
25113 @throw assignment-expression [opt] ;
25115 Returns a constructed '@throw' statement. */
25117 static tree
25118 cp_parser_objc_throw_statement (cp_parser *parser)
25120 tree expr = NULL_TREE;
25121 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25123 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
25125 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25126 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25128 cp_parser_consume_semicolon_at_end_of_statement (parser);
25130 return objc_build_throw_stmt (loc, expr);
25133 /* Parse an Objective-C statement. */
25135 static tree
25136 cp_parser_objc_statement (cp_parser * parser)
25138 /* Try to figure out what kind of declaration is present. */
25139 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25141 switch (kwd->keyword)
25143 case RID_AT_TRY:
25144 return cp_parser_objc_try_catch_finally_statement (parser);
25145 case RID_AT_SYNCHRONIZED:
25146 return cp_parser_objc_synchronized_statement (parser);
25147 case RID_AT_THROW:
25148 return cp_parser_objc_throw_statement (parser);
25149 default:
25150 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
25151 kwd->u.value);
25152 cp_parser_skip_to_end_of_block_or_statement (parser);
25155 return error_mark_node;
25158 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
25159 look ahead to see if an objc keyword follows the attributes. This
25160 is to detect the use of prefix attributes on ObjC @interface and
25161 @protocol. */
25163 static bool
25164 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
25166 cp_lexer_save_tokens (parser->lexer);
25167 *attrib = cp_parser_attributes_opt (parser);
25168 gcc_assert (*attrib);
25169 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
25171 cp_lexer_commit_tokens (parser->lexer);
25172 return true;
25174 cp_lexer_rollback_tokens (parser->lexer);
25175 return false;
25178 /* This routine is a minimal replacement for
25179 c_parser_struct_declaration () used when parsing the list of
25180 types/names or ObjC++ properties. For example, when parsing the
25181 code
25183 @property (readonly) int a, b, c;
25185 this function is responsible for parsing "int a, int b, int c" and
25186 returning the declarations as CHAIN of DECLs.
25188 TODO: Share this code with cp_parser_objc_class_ivars. It's very
25189 similar parsing. */
25190 static tree
25191 cp_parser_objc_struct_declaration (cp_parser *parser)
25193 tree decls = NULL_TREE;
25194 cp_decl_specifier_seq declspecs;
25195 int decl_class_or_enum_p;
25196 tree prefix_attributes;
25198 cp_parser_decl_specifier_seq (parser,
25199 CP_PARSER_FLAGS_NONE,
25200 &declspecs,
25201 &decl_class_or_enum_p);
25203 if (declspecs.type == error_mark_node)
25204 return error_mark_node;
25206 /* auto, register, static, extern, mutable. */
25207 if (declspecs.storage_class != sc_none)
25209 cp_parser_error (parser, "invalid type for property");
25210 declspecs.storage_class = sc_none;
25213 /* thread_local. */
25214 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
25216 cp_parser_error (parser, "invalid type for property");
25217 declspecs.locations[ds_thread] = 0;
25220 /* typedef. */
25221 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
25223 cp_parser_error (parser, "invalid type for property");
25224 declspecs.locations[ds_typedef] = 0;
25227 prefix_attributes = declspecs.attributes;
25228 declspecs.attributes = NULL_TREE;
25230 /* Keep going until we hit the `;' at the end of the declaration. */
25231 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25233 tree attributes, first_attribute, decl;
25234 cp_declarator *declarator;
25235 cp_token *token;
25237 /* Parse the declarator. */
25238 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25239 NULL, NULL, false);
25241 /* Look for attributes that apply to the ivar. */
25242 attributes = cp_parser_attributes_opt (parser);
25243 /* Remember which attributes are prefix attributes and
25244 which are not. */
25245 first_attribute = attributes;
25246 /* Combine the attributes. */
25247 attributes = chainon (prefix_attributes, attributes);
25249 decl = grokfield (declarator, &declspecs,
25250 NULL_TREE, /*init_const_expr_p=*/false,
25251 NULL_TREE, attributes);
25253 if (decl == error_mark_node || decl == NULL_TREE)
25254 return error_mark_node;
25256 /* Reset PREFIX_ATTRIBUTES. */
25257 while (attributes && TREE_CHAIN (attributes) != first_attribute)
25258 attributes = TREE_CHAIN (attributes);
25259 if (attributes)
25260 TREE_CHAIN (attributes) = NULL_TREE;
25262 DECL_CHAIN (decl) = decls;
25263 decls = decl;
25265 token = cp_lexer_peek_token (parser->lexer);
25266 if (token->type == CPP_COMMA)
25268 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25269 continue;
25271 else
25272 break;
25274 return decls;
25277 /* Parse an Objective-C @property declaration. The syntax is:
25279 objc-property-declaration:
25280 '@property' objc-property-attributes[opt] struct-declaration ;
25282 objc-property-attributes:
25283 '(' objc-property-attribute-list ')'
25285 objc-property-attribute-list:
25286 objc-property-attribute
25287 objc-property-attribute-list, objc-property-attribute
25289 objc-property-attribute
25290 'getter' = identifier
25291 'setter' = identifier
25292 'readonly'
25293 'readwrite'
25294 'assign'
25295 'retain'
25296 'copy'
25297 'nonatomic'
25299 For example:
25300 @property NSString *name;
25301 @property (readonly) id object;
25302 @property (retain, nonatomic, getter=getTheName) id name;
25303 @property int a, b, c;
25305 PS: This function is identical to
25306 c_parser_objc_at_property_declaration for C. Keep them in sync. */
25307 static void
25308 cp_parser_objc_at_property_declaration (cp_parser *parser)
25310 /* The following variables hold the attributes of the properties as
25311 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
25312 seen. When we see an attribute, we set them to 'true' (if they
25313 are boolean properties) or to the identifier (if they have an
25314 argument, ie, for getter and setter). Note that here we only
25315 parse the list of attributes, check the syntax and accumulate the
25316 attributes that we find. objc_add_property_declaration() will
25317 then process the information. */
25318 bool property_assign = false;
25319 bool property_copy = false;
25320 tree property_getter_ident = NULL_TREE;
25321 bool property_nonatomic = false;
25322 bool property_readonly = false;
25323 bool property_readwrite = false;
25324 bool property_retain = false;
25325 tree property_setter_ident = NULL_TREE;
25327 /* 'properties' is the list of properties that we read. Usually a
25328 single one, but maybe more (eg, in "@property int a, b, c;" there
25329 are three). */
25330 tree properties;
25331 location_t loc;
25333 loc = cp_lexer_peek_token (parser->lexer)->location;
25335 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
25337 /* Parse the optional attribute list... */
25338 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25340 /* Eat the '('. */
25341 cp_lexer_consume_token (parser->lexer);
25343 while (true)
25345 bool syntax_error = false;
25346 cp_token *token = cp_lexer_peek_token (parser->lexer);
25347 enum rid keyword;
25349 if (token->type != CPP_NAME)
25351 cp_parser_error (parser, "expected identifier");
25352 break;
25354 keyword = C_RID_CODE (token->u.value);
25355 cp_lexer_consume_token (parser->lexer);
25356 switch (keyword)
25358 case RID_ASSIGN: property_assign = true; break;
25359 case RID_COPY: property_copy = true; break;
25360 case RID_NONATOMIC: property_nonatomic = true; break;
25361 case RID_READONLY: property_readonly = true; break;
25362 case RID_READWRITE: property_readwrite = true; break;
25363 case RID_RETAIN: property_retain = true; break;
25365 case RID_GETTER:
25366 case RID_SETTER:
25367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
25369 if (keyword == RID_GETTER)
25370 cp_parser_error (parser,
25371 "missing %<=%> (after %<getter%> attribute)");
25372 else
25373 cp_parser_error (parser,
25374 "missing %<=%> (after %<setter%> attribute)");
25375 syntax_error = true;
25376 break;
25378 cp_lexer_consume_token (parser->lexer); /* eat the = */
25379 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
25381 cp_parser_error (parser, "expected identifier");
25382 syntax_error = true;
25383 break;
25385 if (keyword == RID_SETTER)
25387 if (property_setter_ident != NULL_TREE)
25389 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
25390 cp_lexer_consume_token (parser->lexer);
25392 else
25393 property_setter_ident = cp_parser_objc_selector (parser);
25394 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25395 cp_parser_error (parser, "setter name must terminate with %<:%>");
25396 else
25397 cp_lexer_consume_token (parser->lexer);
25399 else
25401 if (property_getter_ident != NULL_TREE)
25403 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
25404 cp_lexer_consume_token (parser->lexer);
25406 else
25407 property_getter_ident = cp_parser_objc_selector (parser);
25409 break;
25410 default:
25411 cp_parser_error (parser, "unknown property attribute");
25412 syntax_error = true;
25413 break;
25416 if (syntax_error)
25417 break;
25419 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25420 cp_lexer_consume_token (parser->lexer);
25421 else
25422 break;
25425 /* FIXME: "@property (setter, assign);" will generate a spurious
25426 "error: expected ‘)’ before ‘,’ token". This is because
25427 cp_parser_require, unlike the C counterpart, will produce an
25428 error even if we are in error recovery. */
25429 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25431 cp_parser_skip_to_closing_parenthesis (parser,
25432 /*recovering=*/true,
25433 /*or_comma=*/false,
25434 /*consume_paren=*/true);
25438 /* ... and the property declaration(s). */
25439 properties = cp_parser_objc_struct_declaration (parser);
25441 if (properties == error_mark_node)
25443 cp_parser_skip_to_end_of_statement (parser);
25444 /* If the next token is now a `;', consume it. */
25445 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25446 cp_lexer_consume_token (parser->lexer);
25447 return;
25450 if (properties == NULL_TREE)
25451 cp_parser_error (parser, "expected identifier");
25452 else
25454 /* Comma-separated properties are chained together in
25455 reverse order; add them one by one. */
25456 properties = nreverse (properties);
25458 for (; properties; properties = TREE_CHAIN (properties))
25459 objc_add_property_declaration (loc, copy_node (properties),
25460 property_readonly, property_readwrite,
25461 property_assign, property_retain,
25462 property_copy, property_nonatomic,
25463 property_getter_ident, property_setter_ident);
25466 cp_parser_consume_semicolon_at_end_of_statement (parser);
25469 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
25471 objc-synthesize-declaration:
25472 @synthesize objc-synthesize-identifier-list ;
25474 objc-synthesize-identifier-list:
25475 objc-synthesize-identifier
25476 objc-synthesize-identifier-list, objc-synthesize-identifier
25478 objc-synthesize-identifier
25479 identifier
25480 identifier = identifier
25482 For example:
25483 @synthesize MyProperty;
25484 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
25486 PS: This function is identical to c_parser_objc_at_synthesize_declaration
25487 for C. Keep them in sync.
25489 static void
25490 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
25492 tree list = NULL_TREE;
25493 location_t loc;
25494 loc = cp_lexer_peek_token (parser->lexer)->location;
25496 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
25497 while (true)
25499 tree property, ivar;
25500 property = cp_parser_identifier (parser);
25501 if (property == error_mark_node)
25503 cp_parser_consume_semicolon_at_end_of_statement (parser);
25504 return;
25506 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25508 cp_lexer_consume_token (parser->lexer);
25509 ivar = cp_parser_identifier (parser);
25510 if (ivar == error_mark_node)
25512 cp_parser_consume_semicolon_at_end_of_statement (parser);
25513 return;
25516 else
25517 ivar = NULL_TREE;
25518 list = chainon (list, build_tree_list (ivar, property));
25519 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25520 cp_lexer_consume_token (parser->lexer);
25521 else
25522 break;
25524 cp_parser_consume_semicolon_at_end_of_statement (parser);
25525 objc_add_synthesize_declaration (loc, list);
25528 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
25530 objc-dynamic-declaration:
25531 @dynamic identifier-list ;
25533 For example:
25534 @dynamic MyProperty;
25535 @dynamic MyProperty, AnotherProperty;
25537 PS: This function is identical to c_parser_objc_at_dynamic_declaration
25538 for C. Keep them in sync.
25540 static void
25541 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
25543 tree list = NULL_TREE;
25544 location_t loc;
25545 loc = cp_lexer_peek_token (parser->lexer)->location;
25547 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
25548 while (true)
25550 tree property;
25551 property = cp_parser_identifier (parser);
25552 if (property == error_mark_node)
25554 cp_parser_consume_semicolon_at_end_of_statement (parser);
25555 return;
25557 list = chainon (list, build_tree_list (NULL, property));
25558 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25559 cp_lexer_consume_token (parser->lexer);
25560 else
25561 break;
25563 cp_parser_consume_semicolon_at_end_of_statement (parser);
25564 objc_add_dynamic_declaration (loc, list);
25568 /* OpenMP 2.5 parsing routines. */
25570 /* Returns name of the next clause.
25571 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
25572 the token is not consumed. Otherwise appropriate pragma_omp_clause is
25573 returned and the token is consumed. */
25575 static pragma_omp_clause
25576 cp_parser_omp_clause_name (cp_parser *parser)
25578 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
25580 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
25581 result = PRAGMA_OMP_CLAUSE_IF;
25582 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
25583 result = PRAGMA_OMP_CLAUSE_DEFAULT;
25584 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
25585 result = PRAGMA_OMP_CLAUSE_PRIVATE;
25586 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25588 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25589 const char *p = IDENTIFIER_POINTER (id);
25591 switch (p[0])
25593 case 'c':
25594 if (!strcmp ("collapse", p))
25595 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
25596 else if (!strcmp ("copyin", p))
25597 result = PRAGMA_OMP_CLAUSE_COPYIN;
25598 else if (!strcmp ("copyprivate", p))
25599 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
25600 break;
25601 case 'f':
25602 if (!strcmp ("final", p))
25603 result = PRAGMA_OMP_CLAUSE_FINAL;
25604 else if (!strcmp ("firstprivate", p))
25605 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
25606 break;
25607 case 'l':
25608 if (!strcmp ("lastprivate", p))
25609 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
25610 break;
25611 case 'm':
25612 if (!strcmp ("mergeable", p))
25613 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
25614 break;
25615 case 'n':
25616 if (!strcmp ("nowait", p))
25617 result = PRAGMA_OMP_CLAUSE_NOWAIT;
25618 else if (!strcmp ("num_threads", p))
25619 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
25620 break;
25621 case 'o':
25622 if (!strcmp ("ordered", p))
25623 result = PRAGMA_OMP_CLAUSE_ORDERED;
25624 break;
25625 case 'r':
25626 if (!strcmp ("reduction", p))
25627 result = PRAGMA_OMP_CLAUSE_REDUCTION;
25628 break;
25629 case 's':
25630 if (!strcmp ("schedule", p))
25631 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
25632 else if (!strcmp ("shared", p))
25633 result = PRAGMA_OMP_CLAUSE_SHARED;
25634 break;
25635 case 'u':
25636 if (!strcmp ("untied", p))
25637 result = PRAGMA_OMP_CLAUSE_UNTIED;
25638 break;
25642 if (result != PRAGMA_OMP_CLAUSE_NONE)
25643 cp_lexer_consume_token (parser->lexer);
25645 return result;
25648 /* Validate that a clause of the given type does not already exist. */
25650 static void
25651 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
25652 const char *name, location_t location)
25654 tree c;
25656 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25657 if (OMP_CLAUSE_CODE (c) == code)
25659 error_at (location, "too many %qs clauses", name);
25660 break;
25664 /* OpenMP 2.5:
25665 variable-list:
25666 identifier
25667 variable-list , identifier
25669 In addition, we match a closing parenthesis. An opening parenthesis
25670 will have been consumed by the caller.
25672 If KIND is nonzero, create the appropriate node and install the decl
25673 in OMP_CLAUSE_DECL and add the node to the head of the list.
25675 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25676 return the list created. */
25678 static tree
25679 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
25680 tree list)
25682 cp_token *token;
25683 while (1)
25685 tree name, decl;
25687 token = cp_lexer_peek_token (parser->lexer);
25688 name = cp_parser_id_expression (parser, /*template_p=*/false,
25689 /*check_dependency_p=*/true,
25690 /*template_p=*/NULL,
25691 /*declarator_p=*/false,
25692 /*optional_p=*/false);
25693 if (name == error_mark_node)
25694 goto skip_comma;
25696 decl = cp_parser_lookup_name_simple (parser, name, token->location);
25697 if (decl == error_mark_node)
25698 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
25699 token->location);
25700 else if (kind != 0)
25702 tree u = build_omp_clause (token->location, kind);
25703 OMP_CLAUSE_DECL (u) = decl;
25704 OMP_CLAUSE_CHAIN (u) = list;
25705 list = u;
25707 else
25708 list = tree_cons (decl, NULL_TREE, list);
25710 get_comma:
25711 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25712 break;
25713 cp_lexer_consume_token (parser->lexer);
25716 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25718 int ending;
25720 /* Try to resync to an unnested comma. Copied from
25721 cp_parser_parenthesized_expression_list. */
25722 skip_comma:
25723 ending = cp_parser_skip_to_closing_parenthesis (parser,
25724 /*recovering=*/true,
25725 /*or_comma=*/true,
25726 /*consume_paren=*/true);
25727 if (ending < 0)
25728 goto get_comma;
25731 return list;
25734 /* Similarly, but expect leading and trailing parenthesis. This is a very
25735 common case for omp clauses. */
25737 static tree
25738 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25740 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25741 return cp_parser_omp_var_list_no_open (parser, kind, list);
25742 return list;
25745 /* OpenMP 3.0:
25746 collapse ( constant-expression ) */
25748 static tree
25749 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25751 tree c, num;
25752 location_t loc;
25753 HOST_WIDE_INT n;
25755 loc = cp_lexer_peek_token (parser->lexer)->location;
25756 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25757 return list;
25759 num = cp_parser_constant_expression (parser, false, NULL);
25761 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25762 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25763 /*or_comma=*/false,
25764 /*consume_paren=*/true);
25766 if (num == error_mark_node)
25767 return list;
25768 num = fold_non_dependent_expr (num);
25769 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25770 || !host_integerp (num, 0)
25771 || (n = tree_low_cst (num, 0)) <= 0
25772 || (int) n != n)
25774 error_at (loc, "collapse argument needs positive constant integer expression");
25775 return list;
25778 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25779 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25780 OMP_CLAUSE_CHAIN (c) = list;
25781 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25783 return c;
25786 /* OpenMP 2.5:
25787 default ( shared | none ) */
25789 static tree
25790 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25792 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25793 tree c;
25795 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25796 return list;
25797 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25799 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25800 const char *p = IDENTIFIER_POINTER (id);
25802 switch (p[0])
25804 case 'n':
25805 if (strcmp ("none", p) != 0)
25806 goto invalid_kind;
25807 kind = OMP_CLAUSE_DEFAULT_NONE;
25808 break;
25810 case 's':
25811 if (strcmp ("shared", p) != 0)
25812 goto invalid_kind;
25813 kind = OMP_CLAUSE_DEFAULT_SHARED;
25814 break;
25816 default:
25817 goto invalid_kind;
25820 cp_lexer_consume_token (parser->lexer);
25822 else
25824 invalid_kind:
25825 cp_parser_error (parser, "expected %<none%> or %<shared%>");
25828 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25829 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25830 /*or_comma=*/false,
25831 /*consume_paren=*/true);
25833 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25834 return list;
25836 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25837 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25838 OMP_CLAUSE_CHAIN (c) = list;
25839 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25841 return c;
25844 /* OpenMP 3.1:
25845 final ( expression ) */
25847 static tree
25848 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25850 tree t, c;
25852 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25853 return list;
25855 t = cp_parser_condition (parser);
25857 if (t == error_mark_node
25858 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25859 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25860 /*or_comma=*/false,
25861 /*consume_paren=*/true);
25863 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25865 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25866 OMP_CLAUSE_FINAL_EXPR (c) = t;
25867 OMP_CLAUSE_CHAIN (c) = list;
25869 return c;
25872 /* OpenMP 2.5:
25873 if ( expression ) */
25875 static tree
25876 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25878 tree t, c;
25880 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25881 return list;
25883 t = cp_parser_condition (parser);
25885 if (t == error_mark_node
25886 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25887 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25888 /*or_comma=*/false,
25889 /*consume_paren=*/true);
25891 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25893 c = build_omp_clause (location, OMP_CLAUSE_IF);
25894 OMP_CLAUSE_IF_EXPR (c) = t;
25895 OMP_CLAUSE_CHAIN (c) = list;
25897 return c;
25900 /* OpenMP 3.1:
25901 mergeable */
25903 static tree
25904 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
25905 tree list, location_t location)
25907 tree c;
25909 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25910 location);
25912 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25913 OMP_CLAUSE_CHAIN (c) = list;
25914 return c;
25917 /* OpenMP 2.5:
25918 nowait */
25920 static tree
25921 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
25922 tree list, location_t location)
25924 tree c;
25926 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25928 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25929 OMP_CLAUSE_CHAIN (c) = list;
25930 return c;
25933 /* OpenMP 2.5:
25934 num_threads ( expression ) */
25936 static tree
25937 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25938 location_t location)
25940 tree t, c;
25942 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25943 return list;
25945 t = cp_parser_expression (parser, false, NULL);
25947 if (t == error_mark_node
25948 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25949 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25950 /*or_comma=*/false,
25951 /*consume_paren=*/true);
25953 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25954 "num_threads", location);
25956 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25957 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25958 OMP_CLAUSE_CHAIN (c) = list;
25960 return c;
25963 /* OpenMP 2.5:
25964 ordered */
25966 static tree
25967 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
25968 tree list, location_t location)
25970 tree c;
25972 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25973 "ordered", location);
25975 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25976 OMP_CLAUSE_CHAIN (c) = list;
25977 return c;
25980 /* OpenMP 2.5:
25981 reduction ( reduction-operator : variable-list )
25983 reduction-operator:
25984 One of: + * - & ^ | && ||
25986 OpenMP 3.1:
25988 reduction-operator:
25989 One of: + * - & ^ | && || min max */
25991 static tree
25992 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25994 enum tree_code code;
25995 tree nlist, c;
25997 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25998 return list;
26000 switch (cp_lexer_peek_token (parser->lexer)->type)
26002 case CPP_PLUS:
26003 code = PLUS_EXPR;
26004 break;
26005 case CPP_MULT:
26006 code = MULT_EXPR;
26007 break;
26008 case CPP_MINUS:
26009 code = MINUS_EXPR;
26010 break;
26011 case CPP_AND:
26012 code = BIT_AND_EXPR;
26013 break;
26014 case CPP_XOR:
26015 code = BIT_XOR_EXPR;
26016 break;
26017 case CPP_OR:
26018 code = BIT_IOR_EXPR;
26019 break;
26020 case CPP_AND_AND:
26021 code = TRUTH_ANDIF_EXPR;
26022 break;
26023 case CPP_OR_OR:
26024 code = TRUTH_ORIF_EXPR;
26025 break;
26026 case CPP_NAME:
26028 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26029 const char *p = IDENTIFIER_POINTER (id);
26031 if (strcmp (p, "min") == 0)
26033 code = MIN_EXPR;
26034 break;
26036 if (strcmp (p, "max") == 0)
26038 code = MAX_EXPR;
26039 break;
26042 /* FALLTHROUGH */
26043 default:
26044 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
26045 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
26046 resync_fail:
26047 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26048 /*or_comma=*/false,
26049 /*consume_paren=*/true);
26050 return list;
26052 cp_lexer_consume_token (parser->lexer);
26054 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
26055 goto resync_fail;
26057 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
26058 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
26059 OMP_CLAUSE_REDUCTION_CODE (c) = code;
26061 return nlist;
26064 /* OpenMP 2.5:
26065 schedule ( schedule-kind )
26066 schedule ( schedule-kind , expression )
26068 schedule-kind:
26069 static | dynamic | guided | runtime | auto */
26071 static tree
26072 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
26074 tree c, t;
26076 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26077 return list;
26079 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
26081 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26083 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26084 const char *p = IDENTIFIER_POINTER (id);
26086 switch (p[0])
26088 case 'd':
26089 if (strcmp ("dynamic", p) != 0)
26090 goto invalid_kind;
26091 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
26092 break;
26094 case 'g':
26095 if (strcmp ("guided", p) != 0)
26096 goto invalid_kind;
26097 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
26098 break;
26100 case 'r':
26101 if (strcmp ("runtime", p) != 0)
26102 goto invalid_kind;
26103 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
26104 break;
26106 default:
26107 goto invalid_kind;
26110 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
26111 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
26112 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
26113 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
26114 else
26115 goto invalid_kind;
26116 cp_lexer_consume_token (parser->lexer);
26118 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26120 cp_token *token;
26121 cp_lexer_consume_token (parser->lexer);
26123 token = cp_lexer_peek_token (parser->lexer);
26124 t = cp_parser_assignment_expression (parser, false, NULL);
26126 if (t == error_mark_node)
26127 goto resync_fail;
26128 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
26129 error_at (token->location, "schedule %<runtime%> does not take "
26130 "a %<chunk_size%> parameter");
26131 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
26132 error_at (token->location, "schedule %<auto%> does not take "
26133 "a %<chunk_size%> parameter");
26134 else
26135 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
26137 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26138 goto resync_fail;
26140 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
26141 goto resync_fail;
26143 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
26144 OMP_CLAUSE_CHAIN (c) = list;
26145 return c;
26147 invalid_kind:
26148 cp_parser_error (parser, "invalid schedule kind");
26149 resync_fail:
26150 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26151 /*or_comma=*/false,
26152 /*consume_paren=*/true);
26153 return list;
26156 /* OpenMP 3.0:
26157 untied */
26159 static tree
26160 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
26161 tree list, location_t location)
26163 tree c;
26165 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
26167 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
26168 OMP_CLAUSE_CHAIN (c) = list;
26169 return c;
26172 /* Parse all OpenMP clauses. The set clauses allowed by the directive
26173 is a bitmask in MASK. Return the list of clauses found; the result
26174 of clause default goes in *pdefault. */
26176 static tree
26177 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
26178 const char *where, cp_token *pragma_tok)
26180 tree clauses = NULL;
26181 bool first = true;
26182 cp_token *token = NULL;
26184 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
26186 pragma_omp_clause c_kind;
26187 const char *c_name;
26188 tree prev = clauses;
26190 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26191 cp_lexer_consume_token (parser->lexer);
26193 token = cp_lexer_peek_token (parser->lexer);
26194 c_kind = cp_parser_omp_clause_name (parser);
26195 first = false;
26197 switch (c_kind)
26199 case PRAGMA_OMP_CLAUSE_COLLAPSE:
26200 clauses = cp_parser_omp_clause_collapse (parser, clauses,
26201 token->location);
26202 c_name = "collapse";
26203 break;
26204 case PRAGMA_OMP_CLAUSE_COPYIN:
26205 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
26206 c_name = "copyin";
26207 break;
26208 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
26209 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
26210 clauses);
26211 c_name = "copyprivate";
26212 break;
26213 case PRAGMA_OMP_CLAUSE_DEFAULT:
26214 clauses = cp_parser_omp_clause_default (parser, clauses,
26215 token->location);
26216 c_name = "default";
26217 break;
26218 case PRAGMA_OMP_CLAUSE_FINAL:
26219 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
26220 c_name = "final";
26221 break;
26222 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
26223 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
26224 clauses);
26225 c_name = "firstprivate";
26226 break;
26227 case PRAGMA_OMP_CLAUSE_IF:
26228 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
26229 c_name = "if";
26230 break;
26231 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
26232 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
26233 clauses);
26234 c_name = "lastprivate";
26235 break;
26236 case PRAGMA_OMP_CLAUSE_MERGEABLE:
26237 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
26238 token->location);
26239 c_name = "mergeable";
26240 break;
26241 case PRAGMA_OMP_CLAUSE_NOWAIT:
26242 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
26243 c_name = "nowait";
26244 break;
26245 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
26246 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
26247 token->location);
26248 c_name = "num_threads";
26249 break;
26250 case PRAGMA_OMP_CLAUSE_ORDERED:
26251 clauses = cp_parser_omp_clause_ordered (parser, clauses,
26252 token->location);
26253 c_name = "ordered";
26254 break;
26255 case PRAGMA_OMP_CLAUSE_PRIVATE:
26256 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
26257 clauses);
26258 c_name = "private";
26259 break;
26260 case PRAGMA_OMP_CLAUSE_REDUCTION:
26261 clauses = cp_parser_omp_clause_reduction (parser, clauses);
26262 c_name = "reduction";
26263 break;
26264 case PRAGMA_OMP_CLAUSE_SCHEDULE:
26265 clauses = cp_parser_omp_clause_schedule (parser, clauses,
26266 token->location);
26267 c_name = "schedule";
26268 break;
26269 case PRAGMA_OMP_CLAUSE_SHARED:
26270 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
26271 clauses);
26272 c_name = "shared";
26273 break;
26274 case PRAGMA_OMP_CLAUSE_UNTIED:
26275 clauses = cp_parser_omp_clause_untied (parser, clauses,
26276 token->location);
26277 c_name = "nowait";
26278 break;
26279 default:
26280 cp_parser_error (parser, "expected %<#pragma omp%> clause");
26281 goto saw_error;
26284 if (((mask >> c_kind) & 1) == 0)
26286 /* Remove the invalid clause(s) from the list to avoid
26287 confusing the rest of the compiler. */
26288 clauses = prev;
26289 error_at (token->location, "%qs is not valid for %qs", c_name, where);
26292 saw_error:
26293 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
26294 return finish_omp_clauses (clauses);
26297 /* OpenMP 2.5:
26298 structured-block:
26299 statement
26301 In practice, we're also interested in adding the statement to an
26302 outer node. So it is convenient if we work around the fact that
26303 cp_parser_statement calls add_stmt. */
26305 static unsigned
26306 cp_parser_begin_omp_structured_block (cp_parser *parser)
26308 unsigned save = parser->in_statement;
26310 /* Only move the values to IN_OMP_BLOCK if they weren't false.
26311 This preserves the "not within loop or switch" style error messages
26312 for nonsense cases like
26313 void foo() {
26314 #pragma omp single
26315 break;
26318 if (parser->in_statement)
26319 parser->in_statement = IN_OMP_BLOCK;
26321 return save;
26324 static void
26325 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
26327 parser->in_statement = save;
26330 static tree
26331 cp_parser_omp_structured_block (cp_parser *parser)
26333 tree stmt = begin_omp_structured_block ();
26334 unsigned int save = cp_parser_begin_omp_structured_block (parser);
26336 cp_parser_statement (parser, NULL_TREE, false, NULL);
26338 cp_parser_end_omp_structured_block (parser, save);
26339 return finish_omp_structured_block (stmt);
26342 /* OpenMP 2.5:
26343 # pragma omp atomic new-line
26344 expression-stmt
26346 expression-stmt:
26347 x binop= expr | x++ | ++x | x-- | --x
26348 binop:
26349 +, *, -, /, &, ^, |, <<, >>
26351 where x is an lvalue expression with scalar type.
26353 OpenMP 3.1:
26354 # pragma omp atomic new-line
26355 update-stmt
26357 # pragma omp atomic read new-line
26358 read-stmt
26360 # pragma omp atomic write new-line
26361 write-stmt
26363 # pragma omp atomic update new-line
26364 update-stmt
26366 # pragma omp atomic capture new-line
26367 capture-stmt
26369 # pragma omp atomic capture new-line
26370 capture-block
26372 read-stmt:
26373 v = x
26374 write-stmt:
26375 x = expr
26376 update-stmt:
26377 expression-stmt | x = x binop expr
26378 capture-stmt:
26379 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
26380 capture-block:
26381 { v = x; update-stmt; } | { update-stmt; v = x; }
26383 where x and v are lvalue expressions with scalar type. */
26385 static void
26386 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
26388 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
26389 tree rhs1 = NULL_TREE, orig_lhs;
26390 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
26391 bool structured_block = false;
26393 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26395 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26396 const char *p = IDENTIFIER_POINTER (id);
26398 if (!strcmp (p, "read"))
26399 code = OMP_ATOMIC_READ;
26400 else if (!strcmp (p, "write"))
26401 code = NOP_EXPR;
26402 else if (!strcmp (p, "update"))
26403 code = OMP_ATOMIC;
26404 else if (!strcmp (p, "capture"))
26405 code = OMP_ATOMIC_CAPTURE_NEW;
26406 else
26407 p = NULL;
26408 if (p)
26409 cp_lexer_consume_token (parser->lexer);
26411 cp_parser_require_pragma_eol (parser, pragma_tok);
26413 switch (code)
26415 case OMP_ATOMIC_READ:
26416 case NOP_EXPR: /* atomic write */
26417 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26418 /*cast_p=*/false, NULL);
26419 if (v == error_mark_node)
26420 goto saw_error;
26421 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26422 goto saw_error;
26423 if (code == NOP_EXPR)
26424 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26425 else
26426 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26427 /*cast_p=*/false, NULL);
26428 if (lhs == error_mark_node)
26429 goto saw_error;
26430 if (code == NOP_EXPR)
26432 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
26433 opcode. */
26434 code = OMP_ATOMIC;
26435 rhs = lhs;
26436 lhs = v;
26437 v = NULL_TREE;
26439 goto done;
26440 case OMP_ATOMIC_CAPTURE_NEW:
26441 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26443 cp_lexer_consume_token (parser->lexer);
26444 structured_block = true;
26446 else
26448 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26449 /*cast_p=*/false, NULL);
26450 if (v == error_mark_node)
26451 goto saw_error;
26452 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26453 goto saw_error;
26455 default:
26456 break;
26459 restart:
26460 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
26461 /*cast_p=*/false, NULL);
26462 orig_lhs = lhs;
26463 switch (TREE_CODE (lhs))
26465 case ERROR_MARK:
26466 goto saw_error;
26468 case POSTINCREMENT_EXPR:
26469 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26470 code = OMP_ATOMIC_CAPTURE_OLD;
26471 /* FALLTHROUGH */
26472 case PREINCREMENT_EXPR:
26473 lhs = TREE_OPERAND (lhs, 0);
26474 opcode = PLUS_EXPR;
26475 rhs = integer_one_node;
26476 break;
26478 case POSTDECREMENT_EXPR:
26479 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
26480 code = OMP_ATOMIC_CAPTURE_OLD;
26481 /* FALLTHROUGH */
26482 case PREDECREMENT_EXPR:
26483 lhs = TREE_OPERAND (lhs, 0);
26484 opcode = MINUS_EXPR;
26485 rhs = integer_one_node;
26486 break;
26488 case COMPOUND_EXPR:
26489 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
26490 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
26491 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
26492 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
26493 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
26494 (TREE_OPERAND (lhs, 1), 0), 0)))
26495 == BOOLEAN_TYPE)
26496 /* Undo effects of boolean_increment for post {in,de}crement. */
26497 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
26498 /* FALLTHRU */
26499 case MODIFY_EXPR:
26500 if (TREE_CODE (lhs) == MODIFY_EXPR
26501 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
26503 /* Undo effects of boolean_increment. */
26504 if (integer_onep (TREE_OPERAND (lhs, 1)))
26506 /* This is pre or post increment. */
26507 rhs = TREE_OPERAND (lhs, 1);
26508 lhs = TREE_OPERAND (lhs, 0);
26509 opcode = NOP_EXPR;
26510 if (code == OMP_ATOMIC_CAPTURE_NEW
26511 && !structured_block
26512 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
26513 code = OMP_ATOMIC_CAPTURE_OLD;
26514 break;
26517 /* FALLTHRU */
26518 default:
26519 switch (cp_lexer_peek_token (parser->lexer)->type)
26521 case CPP_MULT_EQ:
26522 opcode = MULT_EXPR;
26523 break;
26524 case CPP_DIV_EQ:
26525 opcode = TRUNC_DIV_EXPR;
26526 break;
26527 case CPP_PLUS_EQ:
26528 opcode = PLUS_EXPR;
26529 break;
26530 case CPP_MINUS_EQ:
26531 opcode = MINUS_EXPR;
26532 break;
26533 case CPP_LSHIFT_EQ:
26534 opcode = LSHIFT_EXPR;
26535 break;
26536 case CPP_RSHIFT_EQ:
26537 opcode = RSHIFT_EXPR;
26538 break;
26539 case CPP_AND_EQ:
26540 opcode = BIT_AND_EXPR;
26541 break;
26542 case CPP_OR_EQ:
26543 opcode = BIT_IOR_EXPR;
26544 break;
26545 case CPP_XOR_EQ:
26546 opcode = BIT_XOR_EXPR;
26547 break;
26548 case CPP_EQ:
26549 if (structured_block || code == OMP_ATOMIC)
26551 enum cp_parser_prec oprec;
26552 cp_token *token;
26553 cp_lexer_consume_token (parser->lexer);
26554 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26555 /*cast_p=*/false, NULL);
26556 if (rhs1 == error_mark_node)
26557 goto saw_error;
26558 token = cp_lexer_peek_token (parser->lexer);
26559 switch (token->type)
26561 case CPP_SEMICOLON:
26562 if (code == OMP_ATOMIC_CAPTURE_NEW)
26564 code = OMP_ATOMIC_CAPTURE_OLD;
26565 v = lhs;
26566 lhs = NULL_TREE;
26567 lhs1 = rhs1;
26568 rhs1 = NULL_TREE;
26569 cp_lexer_consume_token (parser->lexer);
26570 goto restart;
26572 cp_parser_error (parser,
26573 "invalid form of %<#pragma omp atomic%>");
26574 goto saw_error;
26575 case CPP_MULT:
26576 opcode = MULT_EXPR;
26577 break;
26578 case CPP_DIV:
26579 opcode = TRUNC_DIV_EXPR;
26580 break;
26581 case CPP_PLUS:
26582 opcode = PLUS_EXPR;
26583 break;
26584 case CPP_MINUS:
26585 opcode = MINUS_EXPR;
26586 break;
26587 case CPP_LSHIFT:
26588 opcode = LSHIFT_EXPR;
26589 break;
26590 case CPP_RSHIFT:
26591 opcode = RSHIFT_EXPR;
26592 break;
26593 case CPP_AND:
26594 opcode = BIT_AND_EXPR;
26595 break;
26596 case CPP_OR:
26597 opcode = BIT_IOR_EXPR;
26598 break;
26599 case CPP_XOR:
26600 opcode = BIT_XOR_EXPR;
26601 break;
26602 default:
26603 cp_parser_error (parser,
26604 "invalid operator for %<#pragma omp atomic%>");
26605 goto saw_error;
26607 oprec = TOKEN_PRECEDENCE (token);
26608 gcc_assert (oprec != PREC_NOT_OPERATOR);
26609 if (commutative_tree_code (opcode))
26610 oprec = (enum cp_parser_prec) (oprec - 1);
26611 cp_lexer_consume_token (parser->lexer);
26612 rhs = cp_parser_binary_expression (parser, false, false,
26613 oprec, NULL);
26614 if (rhs == error_mark_node)
26615 goto saw_error;
26616 goto stmt_done;
26618 /* FALLTHROUGH */
26619 default:
26620 cp_parser_error (parser,
26621 "invalid operator for %<#pragma omp atomic%>");
26622 goto saw_error;
26624 cp_lexer_consume_token (parser->lexer);
26626 rhs = cp_parser_expression (parser, false, NULL);
26627 if (rhs == error_mark_node)
26628 goto saw_error;
26629 break;
26631 stmt_done:
26632 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
26634 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26635 goto saw_error;
26636 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26637 /*cast_p=*/false, NULL);
26638 if (v == error_mark_node)
26639 goto saw_error;
26640 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26641 goto saw_error;
26642 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26643 /*cast_p=*/false, NULL);
26644 if (lhs1 == error_mark_node)
26645 goto saw_error;
26647 if (structured_block)
26649 cp_parser_consume_semicolon_at_end_of_statement (parser);
26650 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26652 done:
26653 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
26654 if (!structured_block)
26655 cp_parser_consume_semicolon_at_end_of_statement (parser);
26656 return;
26658 saw_error:
26659 cp_parser_skip_to_end_of_block_or_statement (parser);
26660 if (structured_block)
26662 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26663 cp_lexer_consume_token (parser->lexer);
26664 else if (code == OMP_ATOMIC_CAPTURE_NEW)
26666 cp_parser_skip_to_end_of_block_or_statement (parser);
26667 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26668 cp_lexer_consume_token (parser->lexer);
26674 /* OpenMP 2.5:
26675 # pragma omp barrier new-line */
26677 static void
26678 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
26680 cp_parser_require_pragma_eol (parser, pragma_tok);
26681 finish_omp_barrier ();
26684 /* OpenMP 2.5:
26685 # pragma omp critical [(name)] new-line
26686 structured-block */
26688 static tree
26689 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
26691 tree stmt, name = NULL;
26693 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26695 cp_lexer_consume_token (parser->lexer);
26697 name = cp_parser_identifier (parser);
26699 if (name == error_mark_node
26700 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26701 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26702 /*or_comma=*/false,
26703 /*consume_paren=*/true);
26704 if (name == error_mark_node)
26705 name = NULL;
26707 cp_parser_require_pragma_eol (parser, pragma_tok);
26709 stmt = cp_parser_omp_structured_block (parser);
26710 return c_finish_omp_critical (input_location, stmt, name);
26713 /* OpenMP 2.5:
26714 # pragma omp flush flush-vars[opt] new-line
26716 flush-vars:
26717 ( variable-list ) */
26719 static void
26720 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26722 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26723 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26724 cp_parser_require_pragma_eol (parser, pragma_tok);
26726 finish_omp_flush ();
26729 /* Helper function, to parse omp for increment expression. */
26731 static tree
26732 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26734 tree cond = cp_parser_binary_expression (parser, false, true,
26735 PREC_NOT_OPERATOR, NULL);
26736 if (cond == error_mark_node
26737 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26739 cp_parser_skip_to_end_of_statement (parser);
26740 return error_mark_node;
26743 switch (TREE_CODE (cond))
26745 case GT_EXPR:
26746 case GE_EXPR:
26747 case LT_EXPR:
26748 case LE_EXPR:
26749 break;
26750 default:
26751 return error_mark_node;
26754 /* If decl is an iterator, preserve LHS and RHS of the relational
26755 expr until finish_omp_for. */
26756 if (decl
26757 && (type_dependent_expression_p (decl)
26758 || CLASS_TYPE_P (TREE_TYPE (decl))))
26759 return cond;
26761 return build_x_binary_op (input_location, TREE_CODE (cond),
26762 TREE_OPERAND (cond, 0), ERROR_MARK,
26763 TREE_OPERAND (cond, 1), ERROR_MARK,
26764 /*overload=*/NULL, tf_warning_or_error);
26767 /* Helper function, to parse omp for increment expression. */
26769 static tree
26770 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26772 cp_token *token = cp_lexer_peek_token (parser->lexer);
26773 enum tree_code op;
26774 tree lhs, rhs;
26775 cp_id_kind idk;
26776 bool decl_first;
26778 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26780 op = (token->type == CPP_PLUS_PLUS
26781 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26782 cp_lexer_consume_token (parser->lexer);
26783 lhs = cp_parser_cast_expression (parser, false, false, NULL);
26784 if (lhs != decl)
26785 return error_mark_node;
26786 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26789 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26790 if (lhs != decl)
26791 return error_mark_node;
26793 token = cp_lexer_peek_token (parser->lexer);
26794 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26796 op = (token->type == CPP_PLUS_PLUS
26797 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26798 cp_lexer_consume_token (parser->lexer);
26799 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26802 op = cp_parser_assignment_operator_opt (parser);
26803 if (op == ERROR_MARK)
26804 return error_mark_node;
26806 if (op != NOP_EXPR)
26808 rhs = cp_parser_assignment_expression (parser, false, NULL);
26809 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26810 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26813 lhs = cp_parser_binary_expression (parser, false, false,
26814 PREC_ADDITIVE_EXPRESSION, NULL);
26815 token = cp_lexer_peek_token (parser->lexer);
26816 decl_first = lhs == decl;
26817 if (decl_first)
26818 lhs = NULL_TREE;
26819 if (token->type != CPP_PLUS
26820 && token->type != CPP_MINUS)
26821 return error_mark_node;
26825 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26826 cp_lexer_consume_token (parser->lexer);
26827 rhs = cp_parser_binary_expression (parser, false, false,
26828 PREC_ADDITIVE_EXPRESSION, NULL);
26829 token = cp_lexer_peek_token (parser->lexer);
26830 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26832 if (lhs == NULL_TREE)
26834 if (op == PLUS_EXPR)
26835 lhs = rhs;
26836 else
26837 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
26838 tf_warning_or_error);
26840 else
26841 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
26842 ERROR_MARK, NULL, tf_warning_or_error);
26845 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26847 if (!decl_first)
26849 if (rhs != decl || op == MINUS_EXPR)
26850 return error_mark_node;
26851 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26853 else
26854 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26856 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26859 /* Parse the restricted form of the for statement allowed by OpenMP. */
26861 static tree
26862 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26864 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26865 tree real_decl, initv, condv, incrv, declv;
26866 tree this_pre_body, cl;
26867 location_t loc_first;
26868 bool collapse_err = false;
26869 int i, collapse = 1, nbraces = 0;
26870 vec<tree, va_gc> *for_block = make_tree_vector ();
26872 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26873 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26874 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26876 gcc_assert (collapse >= 1);
26878 declv = make_tree_vec (collapse);
26879 initv = make_tree_vec (collapse);
26880 condv = make_tree_vec (collapse);
26881 incrv = make_tree_vec (collapse);
26883 loc_first = cp_lexer_peek_token (parser->lexer)->location;
26885 for (i = 0; i < collapse; i++)
26887 int bracecount = 0;
26888 bool add_private_clause = false;
26889 location_t loc;
26891 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26893 cp_parser_error (parser, "for statement expected");
26894 return NULL;
26896 loc = cp_lexer_consume_token (parser->lexer)->location;
26898 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26899 return NULL;
26901 init = decl = real_decl = NULL;
26902 this_pre_body = push_stmt_list ();
26903 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26905 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26907 init-expr:
26908 var = lb
26909 integer-type var = lb
26910 random-access-iterator-type var = lb
26911 pointer-type var = lb
26913 cp_decl_specifier_seq type_specifiers;
26915 /* First, try to parse as an initialized declaration. See
26916 cp_parser_condition, from whence the bulk of this is copied. */
26918 cp_parser_parse_tentatively (parser);
26919 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26920 /*is_trailing_return=*/false,
26921 &type_specifiers);
26922 if (cp_parser_parse_definitely (parser))
26924 /* If parsing a type specifier seq succeeded, then this
26925 MUST be a initialized declaration. */
26926 tree asm_specification, attributes;
26927 cp_declarator *declarator;
26929 declarator = cp_parser_declarator (parser,
26930 CP_PARSER_DECLARATOR_NAMED,
26931 /*ctor_dtor_or_conv_p=*/NULL,
26932 /*parenthesized_p=*/NULL,
26933 /*member_p=*/false);
26934 attributes = cp_parser_attributes_opt (parser);
26935 asm_specification = cp_parser_asm_specification_opt (parser);
26937 if (declarator == cp_error_declarator)
26938 cp_parser_skip_to_end_of_statement (parser);
26940 else
26942 tree pushed_scope, auto_node;
26944 decl = start_decl (declarator, &type_specifiers,
26945 SD_INITIALIZED, attributes,
26946 /*prefix_attributes=*/NULL_TREE,
26947 &pushed_scope);
26949 auto_node = type_uses_auto (TREE_TYPE (decl));
26950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26952 if (cp_lexer_next_token_is (parser->lexer,
26953 CPP_OPEN_PAREN))
26954 error ("parenthesized initialization is not allowed in "
26955 "OpenMP %<for%> loop");
26956 else
26957 /* Trigger an error. */
26958 cp_parser_require (parser, CPP_EQ, RT_EQ);
26960 init = error_mark_node;
26961 cp_parser_skip_to_end_of_statement (parser);
26963 else if (CLASS_TYPE_P (TREE_TYPE (decl))
26964 || type_dependent_expression_p (decl)
26965 || auto_node)
26967 bool is_direct_init, is_non_constant_init;
26969 init = cp_parser_initializer (parser,
26970 &is_direct_init,
26971 &is_non_constant_init);
26973 if (auto_node)
26975 TREE_TYPE (decl)
26976 = do_auto_deduction (TREE_TYPE (decl), init,
26977 auto_node);
26979 if (!CLASS_TYPE_P (TREE_TYPE (decl))
26980 && !type_dependent_expression_p (decl))
26981 goto non_class;
26984 cp_finish_decl (decl, init, !is_non_constant_init,
26985 asm_specification,
26986 LOOKUP_ONLYCONVERTING);
26987 if (CLASS_TYPE_P (TREE_TYPE (decl)))
26989 vec_safe_push (for_block, this_pre_body);
26990 init = NULL_TREE;
26992 else
26993 init = pop_stmt_list (this_pre_body);
26994 this_pre_body = NULL_TREE;
26996 else
26998 /* Consume '='. */
26999 cp_lexer_consume_token (parser->lexer);
27000 init = cp_parser_assignment_expression (parser, false, NULL);
27002 non_class:
27003 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
27004 init = error_mark_node;
27005 else
27006 cp_finish_decl (decl, NULL_TREE,
27007 /*init_const_expr_p=*/false,
27008 asm_specification,
27009 LOOKUP_ONLYCONVERTING);
27012 if (pushed_scope)
27013 pop_scope (pushed_scope);
27016 else
27018 cp_id_kind idk;
27019 /* If parsing a type specifier sequence failed, then
27020 this MUST be a simple expression. */
27021 cp_parser_parse_tentatively (parser);
27022 decl = cp_parser_primary_expression (parser, false, false,
27023 false, &idk);
27024 if (!cp_parser_error_occurred (parser)
27025 && decl
27026 && DECL_P (decl)
27027 && CLASS_TYPE_P (TREE_TYPE (decl)))
27029 tree rhs;
27031 cp_parser_parse_definitely (parser);
27032 cp_parser_require (parser, CPP_EQ, RT_EQ);
27033 rhs = cp_parser_assignment_expression (parser, false, NULL);
27034 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
27035 decl, NOP_EXPR,
27036 rhs,
27037 tf_warning_or_error));
27038 add_private_clause = true;
27040 else
27042 decl = NULL;
27043 cp_parser_abort_tentative_parse (parser);
27044 init = cp_parser_expression (parser, false, NULL);
27045 if (init)
27047 if (TREE_CODE (init) == MODIFY_EXPR
27048 || TREE_CODE (init) == MODOP_EXPR)
27049 real_decl = TREE_OPERAND (init, 0);
27054 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27055 if (this_pre_body)
27057 this_pre_body = pop_stmt_list (this_pre_body);
27058 if (pre_body)
27060 tree t = pre_body;
27061 pre_body = push_stmt_list ();
27062 add_stmt (t);
27063 add_stmt (this_pre_body);
27064 pre_body = pop_stmt_list (pre_body);
27066 else
27067 pre_body = this_pre_body;
27070 if (decl)
27071 real_decl = decl;
27072 if (par_clauses != NULL && real_decl != NULL_TREE)
27074 tree *c;
27075 for (c = par_clauses; *c ; )
27076 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
27077 && OMP_CLAUSE_DECL (*c) == real_decl)
27079 error_at (loc, "iteration variable %qD"
27080 " should not be firstprivate", real_decl);
27081 *c = OMP_CLAUSE_CHAIN (*c);
27083 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
27084 && OMP_CLAUSE_DECL (*c) == real_decl)
27086 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
27087 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
27088 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
27089 OMP_CLAUSE_DECL (l) = real_decl;
27090 OMP_CLAUSE_CHAIN (l) = clauses;
27091 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
27092 clauses = l;
27093 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
27094 CP_OMP_CLAUSE_INFO (*c) = NULL;
27095 add_private_clause = false;
27097 else
27099 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
27100 && OMP_CLAUSE_DECL (*c) == real_decl)
27101 add_private_clause = false;
27102 c = &OMP_CLAUSE_CHAIN (*c);
27106 if (add_private_clause)
27108 tree c;
27109 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27111 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
27112 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
27113 && OMP_CLAUSE_DECL (c) == decl)
27114 break;
27115 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
27116 && OMP_CLAUSE_DECL (c) == decl)
27117 error_at (loc, "iteration variable %qD "
27118 "should not be firstprivate",
27119 decl);
27120 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
27121 && OMP_CLAUSE_DECL (c) == decl)
27122 error_at (loc, "iteration variable %qD should not be reduction",
27123 decl);
27125 if (c == NULL)
27127 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
27128 OMP_CLAUSE_DECL (c) = decl;
27129 c = finish_omp_clauses (c);
27130 if (c)
27132 OMP_CLAUSE_CHAIN (c) = clauses;
27133 clauses = c;
27138 cond = NULL;
27139 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27140 cond = cp_parser_omp_for_cond (parser, decl);
27141 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27143 incr = NULL;
27144 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
27146 /* If decl is an iterator, preserve the operator on decl
27147 until finish_omp_for. */
27148 if (real_decl
27149 && ((processing_template_decl
27150 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
27151 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
27152 incr = cp_parser_omp_for_incr (parser, real_decl);
27153 else
27154 incr = cp_parser_expression (parser, false, NULL);
27155 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
27156 SET_EXPR_LOCATION (incr, input_location);
27159 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27160 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27161 /*or_comma=*/false,
27162 /*consume_paren=*/true);
27164 TREE_VEC_ELT (declv, i) = decl;
27165 TREE_VEC_ELT (initv, i) = init;
27166 TREE_VEC_ELT (condv, i) = cond;
27167 TREE_VEC_ELT (incrv, i) = incr;
27169 if (i == collapse - 1)
27170 break;
27172 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
27173 in between the collapsed for loops to be still considered perfectly
27174 nested. Hopefully the final version clarifies this.
27175 For now handle (multiple) {'s and empty statements. */
27176 cp_parser_parse_tentatively (parser);
27179 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27180 break;
27181 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27183 cp_lexer_consume_token (parser->lexer);
27184 bracecount++;
27186 else if (bracecount
27187 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27188 cp_lexer_consume_token (parser->lexer);
27189 else
27191 loc = cp_lexer_peek_token (parser->lexer)->location;
27192 error_at (loc, "not enough collapsed for loops");
27193 collapse_err = true;
27194 cp_parser_abort_tentative_parse (parser);
27195 declv = NULL_TREE;
27196 break;
27199 while (1);
27201 if (declv)
27203 cp_parser_parse_definitely (parser);
27204 nbraces += bracecount;
27208 /* Note that we saved the original contents of this flag when we entered
27209 the structured block, and so we don't need to re-save it here. */
27210 parser->in_statement = IN_OMP_FOR;
27212 /* Note that the grammar doesn't call for a structured block here,
27213 though the loop as a whole is a structured block. */
27214 body = push_stmt_list ();
27215 cp_parser_statement (parser, NULL_TREE, false, NULL);
27216 body = pop_stmt_list (body);
27218 if (declv == NULL_TREE)
27219 ret = NULL_TREE;
27220 else
27221 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
27222 pre_body, clauses);
27224 while (nbraces)
27226 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
27228 cp_lexer_consume_token (parser->lexer);
27229 nbraces--;
27231 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27232 cp_lexer_consume_token (parser->lexer);
27233 else
27235 if (!collapse_err)
27237 error_at (cp_lexer_peek_token (parser->lexer)->location,
27238 "collapsed loops not perfectly nested");
27240 collapse_err = true;
27241 cp_parser_statement_seq_opt (parser, NULL);
27242 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27243 break;
27247 while (!for_block->is_empty ())
27248 add_stmt (pop_stmt_list (for_block->pop ()));
27249 release_tree_vector (for_block);
27251 return ret;
27254 /* OpenMP 2.5:
27255 #pragma omp for for-clause[optseq] new-line
27256 for-loop */
27258 #define OMP_FOR_CLAUSE_MASK \
27259 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27260 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27261 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27262 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27263 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
27264 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
27265 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
27266 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
27268 static tree
27269 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
27271 tree clauses, sb, ret;
27272 unsigned int save;
27274 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
27275 "#pragma omp for", pragma_tok);
27277 sb = begin_omp_structured_block ();
27278 save = cp_parser_begin_omp_structured_block (parser);
27280 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
27282 cp_parser_end_omp_structured_block (parser, save);
27283 add_stmt (finish_omp_structured_block (sb));
27285 return ret;
27288 /* OpenMP 2.5:
27289 # pragma omp master new-line
27290 structured-block */
27292 static tree
27293 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
27295 cp_parser_require_pragma_eol (parser, pragma_tok);
27296 return c_finish_omp_master (input_location,
27297 cp_parser_omp_structured_block (parser));
27300 /* OpenMP 2.5:
27301 # pragma omp ordered new-line
27302 structured-block */
27304 static tree
27305 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
27307 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27308 cp_parser_require_pragma_eol (parser, pragma_tok);
27309 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
27312 /* OpenMP 2.5:
27314 section-scope:
27315 { section-sequence }
27317 section-sequence:
27318 section-directive[opt] structured-block
27319 section-sequence section-directive structured-block */
27321 static tree
27322 cp_parser_omp_sections_scope (cp_parser *parser)
27324 tree stmt, substmt;
27325 bool error_suppress = false;
27326 cp_token *tok;
27328 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
27329 return NULL_TREE;
27331 stmt = push_stmt_list ();
27333 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
27335 unsigned save;
27337 substmt = begin_omp_structured_block ();
27338 save = cp_parser_begin_omp_structured_block (parser);
27340 while (1)
27342 cp_parser_statement (parser, NULL_TREE, false, NULL);
27344 tok = cp_lexer_peek_token (parser->lexer);
27345 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27346 break;
27347 if (tok->type == CPP_CLOSE_BRACE)
27348 break;
27349 if (tok->type == CPP_EOF)
27350 break;
27353 cp_parser_end_omp_structured_block (parser, save);
27354 substmt = finish_omp_structured_block (substmt);
27355 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27356 add_stmt (substmt);
27359 while (1)
27361 tok = cp_lexer_peek_token (parser->lexer);
27362 if (tok->type == CPP_CLOSE_BRACE)
27363 break;
27364 if (tok->type == CPP_EOF)
27365 break;
27367 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
27369 cp_lexer_consume_token (parser->lexer);
27370 cp_parser_require_pragma_eol (parser, tok);
27371 error_suppress = false;
27373 else if (!error_suppress)
27375 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
27376 error_suppress = true;
27379 substmt = cp_parser_omp_structured_block (parser);
27380 substmt = build1 (OMP_SECTION, void_type_node, substmt);
27381 add_stmt (substmt);
27383 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
27385 substmt = pop_stmt_list (stmt);
27387 stmt = make_node (OMP_SECTIONS);
27388 TREE_TYPE (stmt) = void_type_node;
27389 OMP_SECTIONS_BODY (stmt) = substmt;
27391 add_stmt (stmt);
27392 return stmt;
27395 /* OpenMP 2.5:
27396 # pragma omp sections sections-clause[optseq] newline
27397 sections-scope */
27399 #define OMP_SECTIONS_CLAUSE_MASK \
27400 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27401 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27402 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
27403 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27404 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27406 static tree
27407 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
27409 tree clauses, ret;
27411 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
27412 "#pragma omp sections", pragma_tok);
27414 ret = cp_parser_omp_sections_scope (parser);
27415 if (ret)
27416 OMP_SECTIONS_CLAUSES (ret) = clauses;
27418 return ret;
27421 /* OpenMP 2.5:
27422 # pragma parallel parallel-clause new-line
27423 # pragma parallel for parallel-for-clause new-line
27424 # pragma parallel sections parallel-sections-clause new-line */
27426 #define OMP_PARALLEL_CLAUSE_MASK \
27427 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27428 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27429 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27430 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27431 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27432 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
27433 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
27434 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
27436 static tree
27437 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
27439 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
27440 const char *p_name = "#pragma omp parallel";
27441 tree stmt, clauses, par_clause, ws_clause, block;
27442 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
27443 unsigned int save;
27444 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
27446 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27448 cp_lexer_consume_token (parser->lexer);
27449 p_kind = PRAGMA_OMP_PARALLEL_FOR;
27450 p_name = "#pragma omp parallel for";
27451 mask |= OMP_FOR_CLAUSE_MASK;
27452 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27454 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27456 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27457 const char *p = IDENTIFIER_POINTER (id);
27458 if (strcmp (p, "sections") == 0)
27460 cp_lexer_consume_token (parser->lexer);
27461 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
27462 p_name = "#pragma omp parallel sections";
27463 mask |= OMP_SECTIONS_CLAUSE_MASK;
27464 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
27468 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
27469 block = begin_omp_parallel ();
27470 save = cp_parser_begin_omp_structured_block (parser);
27472 switch (p_kind)
27474 case PRAGMA_OMP_PARALLEL:
27475 cp_parser_statement (parser, NULL_TREE, false, NULL);
27476 par_clause = clauses;
27477 break;
27479 case PRAGMA_OMP_PARALLEL_FOR:
27480 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27481 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
27482 break;
27484 case PRAGMA_OMP_PARALLEL_SECTIONS:
27485 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
27486 stmt = cp_parser_omp_sections_scope (parser);
27487 if (stmt)
27488 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
27489 break;
27491 default:
27492 gcc_unreachable ();
27495 cp_parser_end_omp_structured_block (parser, save);
27496 stmt = finish_omp_parallel (par_clause, block);
27497 if (p_kind != PRAGMA_OMP_PARALLEL)
27498 OMP_PARALLEL_COMBINED (stmt) = 1;
27499 return stmt;
27502 /* OpenMP 2.5:
27503 # pragma omp single single-clause[optseq] new-line
27504 structured-block */
27506 #define OMP_SINGLE_CLAUSE_MASK \
27507 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27508 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27509 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
27510 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
27512 static tree
27513 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
27515 tree stmt = make_node (OMP_SINGLE);
27516 TREE_TYPE (stmt) = void_type_node;
27518 OMP_SINGLE_CLAUSES (stmt)
27519 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
27520 "#pragma omp single", pragma_tok);
27521 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
27523 return add_stmt (stmt);
27526 /* OpenMP 3.0:
27527 # pragma omp task task-clause[optseq] new-line
27528 structured-block */
27530 #define OMP_TASK_CLAUSE_MASK \
27531 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
27532 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
27533 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
27534 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
27535 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
27536 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
27537 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
27538 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
27540 static tree
27541 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
27543 tree clauses, block;
27544 unsigned int save;
27546 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
27547 "#pragma omp task", pragma_tok);
27548 block = begin_omp_task ();
27549 save = cp_parser_begin_omp_structured_block (parser);
27550 cp_parser_statement (parser, NULL_TREE, false, NULL);
27551 cp_parser_end_omp_structured_block (parser, save);
27552 return finish_omp_task (clauses, block);
27555 /* OpenMP 3.0:
27556 # pragma omp taskwait new-line */
27558 static void
27559 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
27561 cp_parser_require_pragma_eol (parser, pragma_tok);
27562 finish_omp_taskwait ();
27565 /* OpenMP 3.1:
27566 # pragma omp taskyield new-line */
27568 static void
27569 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
27571 cp_parser_require_pragma_eol (parser, pragma_tok);
27572 finish_omp_taskyield ();
27575 /* OpenMP 2.5:
27576 # pragma omp threadprivate (variable-list) */
27578 static void
27579 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
27581 tree vars;
27583 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
27584 cp_parser_require_pragma_eol (parser, pragma_tok);
27586 finish_omp_threadprivate (vars);
27589 /* Main entry point to OpenMP statement pragmas. */
27591 static void
27592 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
27594 tree stmt;
27596 switch (pragma_tok->pragma_kind)
27598 case PRAGMA_OMP_ATOMIC:
27599 cp_parser_omp_atomic (parser, pragma_tok);
27600 return;
27601 case PRAGMA_OMP_CRITICAL:
27602 stmt = cp_parser_omp_critical (parser, pragma_tok);
27603 break;
27604 case PRAGMA_OMP_FOR:
27605 stmt = cp_parser_omp_for (parser, pragma_tok);
27606 break;
27607 case PRAGMA_OMP_MASTER:
27608 stmt = cp_parser_omp_master (parser, pragma_tok);
27609 break;
27610 case PRAGMA_OMP_ORDERED:
27611 stmt = cp_parser_omp_ordered (parser, pragma_tok);
27612 break;
27613 case PRAGMA_OMP_PARALLEL:
27614 stmt = cp_parser_omp_parallel (parser, pragma_tok);
27615 break;
27616 case PRAGMA_OMP_SECTIONS:
27617 stmt = cp_parser_omp_sections (parser, pragma_tok);
27618 break;
27619 case PRAGMA_OMP_SINGLE:
27620 stmt = cp_parser_omp_single (parser, pragma_tok);
27621 break;
27622 case PRAGMA_OMP_TASK:
27623 stmt = cp_parser_omp_task (parser, pragma_tok);
27624 break;
27625 default:
27626 gcc_unreachable ();
27629 if (stmt)
27630 SET_EXPR_LOCATION (stmt, pragma_tok->location);
27633 /* Transactional Memory parsing routines. */
27635 /* Parse a transaction attribute.
27637 txn-attribute:
27638 attribute
27639 [ [ identifier ] ]
27641 ??? Simplify this when C++0x bracket attributes are
27642 implemented properly. */
27644 static tree
27645 cp_parser_txn_attribute_opt (cp_parser *parser)
27647 cp_token *token;
27648 tree attr_name, attr = NULL;
27650 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27651 return cp_parser_attributes_opt (parser);
27653 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
27654 return NULL_TREE;
27655 cp_lexer_consume_token (parser->lexer);
27656 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
27657 goto error1;
27659 token = cp_lexer_peek_token (parser->lexer);
27660 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
27662 token = cp_lexer_consume_token (parser->lexer);
27664 attr_name = (token->type == CPP_KEYWORD
27665 /* For keywords, use the canonical spelling,
27666 not the parsed identifier. */
27667 ? ridpointers[(int) token->keyword]
27668 : token->u.value);
27669 attr = build_tree_list (attr_name, NULL_TREE);
27671 else
27672 cp_parser_error (parser, "expected identifier");
27674 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27675 error1:
27676 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27677 return attr;
27680 /* Parse a __transaction_atomic or __transaction_relaxed statement.
27682 transaction-statement:
27683 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
27684 compound-statement
27685 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
27688 static tree
27689 cp_parser_transaction (cp_parser *parser, enum rid keyword)
27691 unsigned char old_in = parser->in_transaction;
27692 unsigned char this_in = 1, new_in;
27693 cp_token *token;
27694 tree stmt, attrs, noex;
27696 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27697 || keyword == RID_TRANSACTION_RELAXED);
27698 token = cp_parser_require_keyword (parser, keyword,
27699 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27700 : RT_TRANSACTION_RELAXED));
27701 gcc_assert (token != NULL);
27703 if (keyword == RID_TRANSACTION_RELAXED)
27704 this_in |= TM_STMT_ATTR_RELAXED;
27705 else
27707 attrs = cp_parser_txn_attribute_opt (parser);
27708 if (attrs)
27709 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27712 /* Parse a noexcept specification. */
27713 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27715 /* Keep track if we're in the lexical scope of an outer transaction. */
27716 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27718 stmt = begin_transaction_stmt (token->location, NULL, this_in);
27720 parser->in_transaction = new_in;
27721 cp_parser_compound_statement (parser, NULL, false, false);
27722 parser->in_transaction = old_in;
27724 finish_transaction_stmt (stmt, NULL, this_in, noex);
27726 return stmt;
27729 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27731 transaction-expression:
27732 __transaction_atomic txn-noexcept-spec[opt] ( expression )
27733 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27736 static tree
27737 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27739 unsigned char old_in = parser->in_transaction;
27740 unsigned char this_in = 1;
27741 cp_token *token;
27742 tree expr, noex;
27743 bool noex_expr;
27745 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27746 || keyword == RID_TRANSACTION_RELAXED);
27748 if (!flag_tm)
27749 error (keyword == RID_TRANSACTION_RELAXED
27750 ? G_("%<__transaction_relaxed%> without transactional memory "
27751 "support enabled")
27752 : G_("%<__transaction_atomic%> without transactional memory "
27753 "support enabled"));
27755 token = cp_parser_require_keyword (parser, keyword,
27756 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27757 : RT_TRANSACTION_RELAXED));
27758 gcc_assert (token != NULL);
27760 if (keyword == RID_TRANSACTION_RELAXED)
27761 this_in |= TM_STMT_ATTR_RELAXED;
27763 /* Set this early. This might mean that we allow transaction_cancel in
27764 an expression that we find out later actually has to be a constexpr.
27765 However, we expect that cxx_constant_value will be able to deal with
27766 this; also, if the noexcept has no constexpr, then what we parse next
27767 really is a transaction's body. */
27768 parser->in_transaction = this_in;
27770 /* Parse a noexcept specification. */
27771 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27772 true);
27774 if (!noex || !noex_expr
27775 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27777 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27779 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27780 finish_parenthesized_expr (expr);
27782 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27784 else
27786 /* The only expression that is available got parsed for the noexcept
27787 already. noexcept is true then. */
27788 expr = noex;
27789 noex = boolean_true_node;
27792 expr = build_transaction_expr (token->location, expr, this_in, noex);
27793 parser->in_transaction = old_in;
27795 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27796 return error_mark_node;
27798 return (flag_tm ? expr : error_mark_node);
27801 /* Parse a function-transaction-block.
27803 function-transaction-block:
27804 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27805 function-body
27806 __transaction_atomic txn-attribute[opt] function-try-block
27807 __transaction_relaxed ctor-initializer[opt] function-body
27808 __transaction_relaxed function-try-block
27811 static bool
27812 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27814 unsigned char old_in = parser->in_transaction;
27815 unsigned char new_in = 1;
27816 tree compound_stmt, stmt, attrs;
27817 bool ctor_initializer_p;
27818 cp_token *token;
27820 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27821 || keyword == RID_TRANSACTION_RELAXED);
27822 token = cp_parser_require_keyword (parser, keyword,
27823 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27824 : RT_TRANSACTION_RELAXED));
27825 gcc_assert (token != NULL);
27827 if (keyword == RID_TRANSACTION_RELAXED)
27828 new_in |= TM_STMT_ATTR_RELAXED;
27829 else
27831 attrs = cp_parser_txn_attribute_opt (parser);
27832 if (attrs)
27833 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27836 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27838 parser->in_transaction = new_in;
27840 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27841 ctor_initializer_p = cp_parser_function_try_block (parser);
27842 else
27843 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
27844 (parser, /*in_function_try_block=*/false);
27846 parser->in_transaction = old_in;
27848 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27850 return ctor_initializer_p;
27853 /* Parse a __transaction_cancel statement.
27855 cancel-statement:
27856 __transaction_cancel txn-attribute[opt] ;
27857 __transaction_cancel txn-attribute[opt] throw-expression ;
27859 ??? Cancel and throw is not yet implemented. */
27861 static tree
27862 cp_parser_transaction_cancel (cp_parser *parser)
27864 cp_token *token;
27865 bool is_outer = false;
27866 tree stmt, attrs;
27868 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27869 RT_TRANSACTION_CANCEL);
27870 gcc_assert (token != NULL);
27872 attrs = cp_parser_txn_attribute_opt (parser);
27873 if (attrs)
27874 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27876 /* ??? Parse cancel-and-throw here. */
27878 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27880 if (!flag_tm)
27882 error_at (token->location, "%<__transaction_cancel%> without "
27883 "transactional memory support enabled");
27884 return error_mark_node;
27886 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27888 error_at (token->location, "%<__transaction_cancel%> within a "
27889 "%<__transaction_relaxed%>");
27890 return error_mark_node;
27892 else if (is_outer)
27894 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27895 && !is_tm_may_cancel_outer (current_function_decl))
27897 error_at (token->location, "outer %<__transaction_cancel%> not "
27898 "within outer %<__transaction_atomic%>");
27899 error_at (token->location,
27900 " or a %<transaction_may_cancel_outer%> function");
27901 return error_mark_node;
27904 else if (parser->in_transaction == 0)
27906 error_at (token->location, "%<__transaction_cancel%> not within "
27907 "%<__transaction_atomic%>");
27908 return error_mark_node;
27911 stmt = build_tm_abort_call (token->location, is_outer);
27912 add_stmt (stmt);
27913 finish_stmt ();
27915 return stmt;
27918 /* The parser. */
27920 static GTY (()) cp_parser *the_parser;
27923 /* Special handling for the first token or line in the file. The first
27924 thing in the file might be #pragma GCC pch_preprocess, which loads a
27925 PCH file, which is a GC collection point. So we need to handle this
27926 first pragma without benefit of an existing lexer structure.
27928 Always returns one token to the caller in *FIRST_TOKEN. This is
27929 either the true first token of the file, or the first token after
27930 the initial pragma. */
27932 static void
27933 cp_parser_initial_pragma (cp_token *first_token)
27935 tree name = NULL;
27937 cp_lexer_get_preprocessor_token (NULL, first_token);
27938 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27939 return;
27941 cp_lexer_get_preprocessor_token (NULL, first_token);
27942 if (first_token->type == CPP_STRING)
27944 name = first_token->u.value;
27946 cp_lexer_get_preprocessor_token (NULL, first_token);
27947 if (first_token->type != CPP_PRAGMA_EOL)
27948 error_at (first_token->location,
27949 "junk at end of %<#pragma GCC pch_preprocess%>");
27951 else
27952 error_at (first_token->location, "expected string literal");
27954 /* Skip to the end of the pragma. */
27955 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27956 cp_lexer_get_preprocessor_token (NULL, first_token);
27958 /* Now actually load the PCH file. */
27959 if (name)
27960 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27962 /* Read one more token to return to our caller. We have to do this
27963 after reading the PCH file in, since its pointers have to be
27964 live. */
27965 cp_lexer_get_preprocessor_token (NULL, first_token);
27968 /* Normal parsing of a pragma token. Here we can (and must) use the
27969 regular lexer. */
27971 static bool
27972 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27974 cp_token *pragma_tok;
27975 unsigned int id;
27977 pragma_tok = cp_lexer_consume_token (parser->lexer);
27978 gcc_assert (pragma_tok->type == CPP_PRAGMA);
27979 parser->lexer->in_pragma = true;
27981 id = pragma_tok->pragma_kind;
27982 switch (id)
27984 case PRAGMA_GCC_PCH_PREPROCESS:
27985 error_at (pragma_tok->location,
27986 "%<#pragma GCC pch_preprocess%> must be first");
27987 break;
27989 case PRAGMA_OMP_BARRIER:
27990 switch (context)
27992 case pragma_compound:
27993 cp_parser_omp_barrier (parser, pragma_tok);
27994 return false;
27995 case pragma_stmt:
27996 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27997 "used in compound statements");
27998 break;
27999 default:
28000 goto bad_stmt;
28002 break;
28004 case PRAGMA_OMP_FLUSH:
28005 switch (context)
28007 case pragma_compound:
28008 cp_parser_omp_flush (parser, pragma_tok);
28009 return false;
28010 case pragma_stmt:
28011 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
28012 "used in compound statements");
28013 break;
28014 default:
28015 goto bad_stmt;
28017 break;
28019 case PRAGMA_OMP_TASKWAIT:
28020 switch (context)
28022 case pragma_compound:
28023 cp_parser_omp_taskwait (parser, pragma_tok);
28024 return false;
28025 case pragma_stmt:
28026 error_at (pragma_tok->location,
28027 "%<#pragma omp taskwait%> may only be "
28028 "used in compound statements");
28029 break;
28030 default:
28031 goto bad_stmt;
28033 break;
28035 case PRAGMA_OMP_TASKYIELD:
28036 switch (context)
28038 case pragma_compound:
28039 cp_parser_omp_taskyield (parser, pragma_tok);
28040 return false;
28041 case pragma_stmt:
28042 error_at (pragma_tok->location,
28043 "%<#pragma omp taskyield%> may only be "
28044 "used in compound statements");
28045 break;
28046 default:
28047 goto bad_stmt;
28049 break;
28051 case PRAGMA_OMP_THREADPRIVATE:
28052 cp_parser_omp_threadprivate (parser, pragma_tok);
28053 return false;
28055 case PRAGMA_OMP_ATOMIC:
28056 case PRAGMA_OMP_CRITICAL:
28057 case PRAGMA_OMP_FOR:
28058 case PRAGMA_OMP_MASTER:
28059 case PRAGMA_OMP_ORDERED:
28060 case PRAGMA_OMP_PARALLEL:
28061 case PRAGMA_OMP_SECTIONS:
28062 case PRAGMA_OMP_SINGLE:
28063 case PRAGMA_OMP_TASK:
28064 if (context == pragma_external)
28065 goto bad_stmt;
28066 cp_parser_omp_construct (parser, pragma_tok);
28067 return true;
28069 case PRAGMA_OMP_SECTION:
28070 error_at (pragma_tok->location,
28071 "%<#pragma omp section%> may only be used in "
28072 "%<#pragma omp sections%> construct");
28073 break;
28075 default:
28076 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
28077 c_invoke_pragma_handler (id);
28078 break;
28080 bad_stmt:
28081 cp_parser_error (parser, "expected declaration specifiers");
28082 break;
28085 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28086 return false;
28089 /* The interface the pragma parsers have to the lexer. */
28091 enum cpp_ttype
28092 pragma_lex (tree *value)
28094 cp_token *tok;
28095 enum cpp_ttype ret;
28097 tok = cp_lexer_peek_token (the_parser->lexer);
28099 ret = tok->type;
28100 *value = tok->u.value;
28102 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
28103 ret = CPP_EOF;
28104 else if (ret == CPP_STRING)
28105 *value = cp_parser_string_literal (the_parser, false, false);
28106 else
28108 cp_lexer_consume_token (the_parser->lexer);
28109 if (ret == CPP_KEYWORD)
28110 ret = CPP_NAME;
28113 return ret;
28117 /* External interface. */
28119 /* Parse one entire translation unit. */
28121 void
28122 c_parse_file (void)
28124 static bool already_called = false;
28126 if (already_called)
28128 sorry ("inter-module optimizations not implemented for C++");
28129 return;
28131 already_called = true;
28133 the_parser = cp_parser_new ();
28134 push_deferring_access_checks (flag_access_control
28135 ? dk_no_deferred : dk_no_check);
28136 cp_parser_translation_unit (the_parser);
28137 the_parser = NULL;
28140 #include "gt-cp-parser.h"